Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * From Coreboot |
| 3 | * Copyright (C) 2008-2009 coresystems GmbH |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0 |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 10 | #include <fdtdec.h> |
| 11 | #include <asm/io.h> |
Simon Glass | 9c852d7 | 2016-03-16 07:44:36 -0600 | [diff] [blame] | 12 | #include <asm/pch_common.h> |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 13 | #include <asm/pci.h> |
| 14 | #include <asm/arch/pch.h> |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 15 | |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 18 | static void common_sata_init(struct udevice *dev, unsigned int port_map) |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 19 | { |
| 20 | u32 reg32; |
| 21 | u16 reg16; |
| 22 | |
| 23 | /* Set IDE I/O Configuration */ |
| 24 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 25 | dm_pci_write_config32(dev, IDE_CONFIG, reg32); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 26 | |
| 27 | /* Port enable */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 28 | dm_pci_read_config16(dev, 0x92, ®16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 29 | reg16 &= ~0x3f; |
| 30 | reg16 |= port_map; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 31 | dm_pci_write_config16(dev, 0x92, reg16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 32 | |
| 33 | /* SATA Initialization register */ |
| 34 | port_map &= 0xff; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 35 | dm_pci_write_config32(dev, 0x94, ((port_map ^ 0x3f) << 24) | 0x183); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 38 | static void bd82x6x_sata_init(struct udevice *dev, struct udevice *pch) |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 39 | { |
| 40 | unsigned int port_map, speed_support, port_tx; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 41 | const void *blob = gd->fdt_blob; |
| 42 | int node = dev->of_offset; |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 43 | const char *mode; |
| 44 | u32 reg32; |
| 45 | u16 reg16; |
| 46 | |
| 47 | debug("SATA: Initializing...\n"); |
| 48 | |
| 49 | /* SATA configuration */ |
| 50 | port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0); |
| 51 | speed_support = fdtdec_get_int(blob, node, |
| 52 | "sata_interface_speed_support", 0); |
| 53 | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 54 | mode = fdt_getprop(blob, node, "intel,sata-mode", NULL); |
| 55 | if (!mode || !strcmp(mode, "ahci")) { |
| 56 | u32 abar; |
| 57 | |
| 58 | debug("SATA: Controller in AHCI mode\n"); |
| 59 | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 60 | /* Set timings */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 61 | dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 62 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 63 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 64 | dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 65 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
| 66 | |
| 67 | /* Sync DMA */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 68 | dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0); |
| 69 | dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0001); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 70 | |
| 71 | common_sata_init(dev, 0x8000 | port_map); |
| 72 | |
| 73 | /* Initialize AHCI memory-mapped space */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 74 | abar = dm_pci_read_bar32(dev, 5); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 75 | debug("ABAR: %08X\n", abar); |
| 76 | /* CAP (HBA Capabilities) : enable power management */ |
| 77 | reg32 = readl(abar + 0x00); |
| 78 | reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */ |
| 79 | reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */ |
| 80 | /* Set ISS, if available */ |
| 81 | if (speed_support) { |
| 82 | reg32 &= ~0x00f00000; |
| 83 | reg32 |= (speed_support & 0x03) << 20; |
| 84 | } |
| 85 | writel(reg32, abar + 0x00); |
| 86 | /* PI (Ports implemented) */ |
| 87 | writel(port_map, abar + 0x0c); |
| 88 | (void) readl(abar + 0x0c); /* Read back 1 */ |
| 89 | (void) readl(abar + 0x0c); /* Read back 2 */ |
| 90 | /* CAP2 (HBA Capabilities Extended)*/ |
| 91 | reg32 = readl(abar + 0x24); |
| 92 | reg32 &= ~0x00000002; |
| 93 | writel(reg32, abar + 0x24); |
| 94 | /* VSP (Vendor Specific Register */ |
| 95 | reg32 = readl(abar + 0xa0); |
| 96 | reg32 &= ~0x00000005; |
| 97 | writel(reg32, abar + 0xa0); |
| 98 | } else if (!strcmp(mode, "combined")) { |
| 99 | debug("SATA: Controller in combined mode\n"); |
| 100 | |
| 101 | /* No AHCI: clear AHCI base */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 102 | dm_pci_write_bar32(dev, 5, 0x00000000); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 103 | /* And without AHCI BAR no memory decoding */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 104 | dm_pci_read_config16(dev, PCI_COMMAND, ®16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 105 | reg16 &= ~PCI_COMMAND_MEMORY; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 106 | dm_pci_write_config16(dev, PCI_COMMAND, reg16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 107 | |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 108 | dm_pci_write_config8(dev, 0x09, 0x80); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 109 | |
| 110 | /* Set timings */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 111 | dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 112 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 113 | dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 114 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 115 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 116 | |
| 117 | /* Sync DMA */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 118 | dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0); |
| 119 | dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0200); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 120 | |
| 121 | common_sata_init(dev, port_map); |
| 122 | } else { |
| 123 | debug("SATA: Controller in plain-ide mode\n"); |
| 124 | |
| 125 | /* No AHCI: clear AHCI base */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 126 | dm_pci_write_bar32(dev, 5, 0x00000000); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 127 | |
| 128 | /* And without AHCI BAR no memory decoding */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 129 | dm_pci_read_config16(dev, PCI_COMMAND, ®16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 130 | reg16 &= ~PCI_COMMAND_MEMORY; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 131 | dm_pci_write_config16(dev, PCI_COMMAND, reg16); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Native mode capable on both primary and secondary (0xa) |
| 135 | * OR'ed with enabled (0x50) = 0xf |
| 136 | */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 137 | dm_pci_write_config8(dev, 0x09, 0x8f); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 138 | |
| 139 | /* Set timings */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 140 | dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 141 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 142 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 143 | dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 144 | IDE_SITRE | IDE_ISP_3_CLOCKS | |
| 145 | IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0); |
| 146 | |
| 147 | /* Sync DMA */ |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 148 | dm_pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0); |
| 149 | dm_pci_write_config16(dev, IDE_SDMA_TIM, 0x0201); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 150 | |
| 151 | common_sata_init(dev, port_map); |
| 152 | } |
| 153 | |
| 154 | /* Set Gen3 Transmitter settings if needed */ |
| 155 | port_tx = fdtdec_get_int(blob, node, "intel,sata-port0-gen3-tx", 0); |
| 156 | if (port_tx) |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 157 | pch_iobp_update(pch, SATA_IOBP_SP0G3IR, 0, port_tx); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 158 | |
| 159 | port_tx = fdtdec_get_int(blob, node, "intel,sata-port1-gen3-tx", 0); |
| 160 | if (port_tx) |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 161 | pch_iobp_update(pch, SATA_IOBP_SP1G3IR, 0, port_tx); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 162 | |
| 163 | /* Additional Programming Requirements */ |
Simon Glass | 9c852d7 | 2016-03-16 07:44:36 -0600 | [diff] [blame] | 164 | pch_common_sir_write(dev, 0x04, 0x00001600); |
| 165 | pch_common_sir_write(dev, 0x28, 0xa0000033); |
| 166 | reg32 = pch_common_sir_read(dev, 0x54); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 167 | reg32 &= 0xff000000; |
| 168 | reg32 |= 0x5555aa; |
Simon Glass | 9c852d7 | 2016-03-16 07:44:36 -0600 | [diff] [blame] | 169 | pch_common_sir_write(dev, 0x54, reg32); |
| 170 | pch_common_sir_write(dev, 0x64, 0xcccc8484); |
| 171 | reg32 = pch_common_sir_read(dev, 0x68); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 172 | reg32 &= 0xffff0000; |
| 173 | reg32 |= 0xcccc; |
Simon Glass | 9c852d7 | 2016-03-16 07:44:36 -0600 | [diff] [blame] | 174 | pch_common_sir_write(dev, 0x68, reg32); |
| 175 | reg32 = pch_common_sir_read(dev, 0x78); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 176 | reg32 &= 0x0000ffff; |
| 177 | reg32 |= 0x88880000; |
Simon Glass | 9c852d7 | 2016-03-16 07:44:36 -0600 | [diff] [blame] | 178 | pch_common_sir_write(dev, 0x78, reg32); |
| 179 | pch_common_sir_write(dev, 0x84, 0x001c7000); |
| 180 | pch_common_sir_write(dev, 0x88, 0x88338822); |
| 181 | pch_common_sir_write(dev, 0xa0, 0x001c7000); |
| 182 | pch_common_sir_write(dev, 0xc4, 0x0c0c0c0c); |
| 183 | pch_common_sir_write(dev, 0xc8, 0x0c0c0c0c); |
| 184 | pch_common_sir_write(dev, 0xd4, 0x10000000); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 185 | |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 186 | pch_iobp_update(pch, 0xea004001, 0x3fffffff, 0xc0000000); |
| 187 | pch_iobp_update(pch, 0xea00408a, 0xfffffcff, 0x00000100); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 190 | static void bd82x6x_sata_enable(struct udevice *dev) |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 191 | { |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 192 | const void *blob = gd->fdt_blob; |
| 193 | int node = dev->of_offset; |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 194 | unsigned port_map; |
| 195 | const char *mode; |
| 196 | u16 map = 0; |
| 197 | |
| 198 | /* |
| 199 | * Set SATA controller mode early so the resource allocator can |
| 200 | * properly assign IO/Memory resources for the controller. |
| 201 | */ |
| 202 | mode = fdt_getprop(blob, node, "intel,sata-mode", NULL); |
| 203 | if (mode && !strcmp(mode, "ahci")) |
| 204 | map = 0x0060; |
| 205 | port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0); |
| 206 | |
| 207 | map |= (port_map ^ 0x3f) << 8; |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 208 | dm_pci_write_config16(dev, 0x90, map); |
Simon Glass | cd0adb3 | 2014-11-14 18:18:38 -0700 | [diff] [blame] | 209 | } |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 210 | |
| 211 | static int bd82x6x_sata_probe(struct udevice *dev) |
| 212 | { |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 213 | struct udevice *pch; |
| 214 | int ret; |
| 215 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 216 | ret = uclass_first_device_err(UCLASS_PCH, &pch); |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 217 | if (ret) |
| 218 | return ret; |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 219 | |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 220 | if (!(gd->flags & GD_FLG_RELOC)) |
Simon Glass | 35c50a5 | 2016-01-17 16:11:38 -0700 | [diff] [blame] | 221 | bd82x6x_sata_enable(dev); |
Simon Glass | 39f3f8c | 2016-01-17 16:11:37 -0700 | [diff] [blame] | 222 | else |
Simon Glass | b3a9e51 | 2016-01-17 16:11:52 -0700 | [diff] [blame] | 223 | bd82x6x_sata_init(dev, pch); |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static const struct udevice_id bd82x6x_ahci_ids[] = { |
| 229 | { .compatible = "intel,pantherpoint-ahci" }, |
| 230 | { } |
| 231 | }; |
| 232 | |
| 233 | U_BOOT_DRIVER(ahci_ivybridge_drv) = { |
| 234 | .name = "ahci_ivybridge", |
Simon Glass | 85ee165 | 2016-05-01 11:35:52 -0600 | [diff] [blame] | 235 | .id = UCLASS_AHCI, |
Simon Glass | 5cc400b | 2016-01-17 16:11:35 -0700 | [diff] [blame] | 236 | .of_match = bd82x6x_ahci_ids, |
| 237 | .probe = bd82x6x_sata_probe, |
| 238 | }; |