Angel Pons | f94ac9a | 2020-04-05 15:46:48 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 4 | #include <device/pci_ops.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <device/device.h> |
| 7 | #include <device/pci.h> |
| 8 | #include <device/pci_ids.h> |
| 9 | #include <delay.h> |
Julius Werner | 4ee4bd5 | 2014-10-20 13:46:39 -0700 | [diff] [blame] | 10 | #include <soc/iobp.h> |
| 11 | #include <soc/ramstage.h> |
| 12 | #include <soc/rcba.h> |
| 13 | #include <soc/sata.h> |
| 14 | #include <soc/intel/broadwell/chip.h> |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 15 | |
| 16 | static inline u32 sir_read(struct device *dev, int idx) |
| 17 | { |
| 18 | pci_write_config32(dev, SATA_SIRI, idx); |
| 19 | return pci_read_config32(dev, SATA_SIRD); |
| 20 | } |
| 21 | |
| 22 | static inline void sir_write(struct device *dev, int idx, u32 value) |
| 23 | { |
| 24 | pci_write_config32(dev, SATA_SIRI, idx); |
| 25 | pci_write_config32(dev, SATA_SIRD, value); |
| 26 | } |
| 27 | |
| 28 | static void sata_init(struct device *dev) |
| 29 | { |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 30 | config_t *config = config_of(dev); |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 31 | u32 reg32; |
| 32 | u8 *abar; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 33 | u16 reg16; |
Duncan Laurie | 1b0d5a3 | 2014-08-13 16:59:34 -0700 | [diff] [blame] | 34 | int port; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 35 | |
| 36 | printk(BIOS_DEBUG, "SATA: Initializing controller in AHCI mode.\n"); |
| 37 | |
| 38 | /* Enable BARs */ |
Angel Pons | 89739ba | 2020-07-25 02:46:39 +0200 | [diff] [blame] | 39 | pci_write_config16(dev, PCI_COMMAND, |
| 40 | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 41 | |
| 42 | /* Set Interrupt Line */ |
| 43 | /* Interrupt Pin is set by D31IP.PIP */ |
| 44 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0x0a); |
| 45 | |
| 46 | /* Set timings */ |
Kane Chen | 8c1fd78 | 2014-08-19 10:51:46 -0700 | [diff] [blame] | 47 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE); |
| 48 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 49 | |
| 50 | /* for AHCI, Port Enable is managed in memory mapped space */ |
| 51 | reg16 = pci_read_config16(dev, 0x92); |
Wenkai Du | 038cce2 | 2014-12-05 14:04:10 -0800 | [diff] [blame] | 52 | reg16 &= ~0xf; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 53 | reg16 |= 0x8000 | config->sata_port_map; |
| 54 | pci_write_config16(dev, 0x92, reg16); |
| 55 | udelay(2); |
| 56 | |
| 57 | /* Setup register 98h */ |
Kane Chen | 8c1fd78 | 2014-08-19 10:51:46 -0700 | [diff] [blame] | 58 | reg32 = pci_read_config32(dev, 0x98); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 59 | reg32 &= ~((1 << 31) | (1 << 30)); |
| 60 | reg32 |= 1 << 23; |
Duncan Laurie | 1b0d5a3 | 2014-08-13 16:59:34 -0700 | [diff] [blame] | 61 | reg32 |= 1 << 24; /* Enable MPHY Dynamic Power Gating */ |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 62 | pci_write_config32(dev, 0x98, reg32); |
| 63 | |
| 64 | /* Setup register 9Ch */ |
Elyes HAOUAS | 878b685 | 2019-10-18 19:52:22 +0200 | [diff] [blame] | 65 | reg16 = (1 << 5); /* BWG step 12 */ |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 66 | pci_write_config16(dev, 0x9c, reg16); |
| 67 | |
| 68 | /* SATA Initialization register */ |
| 69 | reg32 = 0x183; |
Wenkai Du | 038cce2 | 2014-12-05 14:04:10 -0800 | [diff] [blame] | 70 | reg32 |= (config->sata_port_map ^ 0xf) << 24; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 71 | reg32 |= (config->sata_devslp_mux & 1) << 15; |
| 72 | pci_write_config32(dev, 0x94, reg32); |
| 73 | |
| 74 | /* Initialize AHCI memory-mapped space */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 75 | abar = (u8 *)(pci_read_config32(dev, PCI_BASE_ADDRESS_5)); |
| 76 | printk(BIOS_DEBUG, "ABAR: %p\n", abar); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 77 | |
Duncan Laurie | 55228ba | 2014-08-25 10:14:08 -0700 | [diff] [blame] | 78 | /* CAP (HBA Capabilities) : enable power management */ |
| 79 | reg32 = read32(abar + 0x00); |
| 80 | reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */ |
| 81 | reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */ |
| 82 | reg32 |= (1 << 18); /* SAM: SATA AHCI MODE ONLY */ |
| 83 | write32(abar + 0x00, reg32); |
| 84 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 85 | /* PI (Ports implemented) */ |
| 86 | write32(abar + 0x0c, config->sata_port_map); |
| 87 | (void) read32(abar + 0x0c); /* Read back 1 */ |
| 88 | (void) read32(abar + 0x0c); /* Read back 2 */ |
| 89 | |
| 90 | /* CAP2 (HBA Capabilities Extended)*/ |
Duncan Laurie | 1b0d5a3 | 2014-08-13 16:59:34 -0700 | [diff] [blame] | 91 | if (config->sata_devslp_disable) { |
| 92 | reg32 = read32(abar + 0x24); |
| 93 | reg32 &= ~(1 << 3); |
| 94 | write32(abar + 0x24, reg32); |
| 95 | } else { |
| 96 | /* Enable DEVSLP */ |
| 97 | reg32 = read32(abar + 0x24); |
| 98 | reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2); |
| 99 | write32(abar + 0x24, reg32); |
| 100 | |
| 101 | for (port = 0; port < 4; port++) { |
| 102 | if (!(config->sata_port_map & (1 << port))) |
| 103 | continue; |
| 104 | reg32 = read32(abar + 0x144 + (0x80 * port)); |
| 105 | reg32 |= (1 << 1); /* DEVSLP DSP */ |
| 106 | write32(abar + 0x144 + (0x80 * port), reg32); |
| 107 | } |
| 108 | } |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * Static Power Gating for unused ports |
| 112 | */ |
| 113 | reg32 = RCBA32(0x3a84); |
| 114 | /* Port 3 and 2 disabled */ |
| 115 | if ((config->sata_port_map & ((1 << 3)|(1 << 2))) == 0) |
| 116 | reg32 |= (1 << 24) | (1 << 26); |
| 117 | /* Port 1 and 0 disabled */ |
| 118 | if ((config->sata_port_map & ((1 << 1)|(1 << 0))) == 0) |
| 119 | reg32 |= (1 << 20) | (1 << 18); |
| 120 | RCBA32(0x3a84) = reg32; |
| 121 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 122 | /* Set Gen3 Transmitter settings if needed */ |
| 123 | if (config->sata_port0_gen3_tx) |
Duncan Laurie | b63d341 | 2015-01-06 13:32:42 -0800 | [diff] [blame] | 124 | pch_iobp_update(SATA_IOBP_SP0_SECRT88, |
| 125 | ~(SATA_SECRT88_VADJ_MASK << |
| 126 | SATA_SECRT88_VADJ_SHIFT), |
| 127 | (config->sata_port0_gen3_tx & |
| 128 | SATA_SECRT88_VADJ_MASK) |
| 129 | << SATA_SECRT88_VADJ_SHIFT); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 130 | |
| 131 | if (config->sata_port1_gen3_tx) |
Duncan Laurie | b63d341 | 2015-01-06 13:32:42 -0800 | [diff] [blame] | 132 | pch_iobp_update(SATA_IOBP_SP1_SECRT88, |
| 133 | ~(SATA_SECRT88_VADJ_MASK << |
| 134 | SATA_SECRT88_VADJ_SHIFT), |
| 135 | (config->sata_port1_gen3_tx & |
| 136 | SATA_SECRT88_VADJ_MASK) |
| 137 | << SATA_SECRT88_VADJ_SHIFT); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 138 | |
Youness Alaoui | 696ebc2 | 2017-02-07 13:54:45 -0500 | [diff] [blame] | 139 | if (config->sata_port2_gen3_tx) |
| 140 | pch_iobp_update(SATA_IOBP_SP2_SECRT88, |
| 141 | ~(SATA_SECRT88_VADJ_MASK << |
| 142 | SATA_SECRT88_VADJ_SHIFT), |
| 143 | (config->sata_port2_gen3_tx & |
| 144 | SATA_SECRT88_VADJ_MASK) |
| 145 | << SATA_SECRT88_VADJ_SHIFT); |
| 146 | |
| 147 | if (config->sata_port3_gen3_tx) |
| 148 | pch_iobp_update(SATA_IOBP_SP3_SECRT88, |
| 149 | ~(SATA_SECRT88_VADJ_MASK << |
| 150 | SATA_SECRT88_VADJ_SHIFT), |
Youness Alaoui | 601aa31 | 2017-02-27 12:03:39 -0500 | [diff] [blame] | 151 | (config->sata_port3_gen3_tx & |
Youness Alaoui | 696ebc2 | 2017-02-07 13:54:45 -0500 | [diff] [blame] | 152 | SATA_SECRT88_VADJ_MASK) |
| 153 | << SATA_SECRT88_VADJ_SHIFT); |
| 154 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 155 | /* Set Gen3 DTLE DATA / EDGE registers if needed */ |
| 156 | if (config->sata_port0_gen3_dtle) { |
| 157 | pch_iobp_update(SATA_IOBP_SP0DTLE_DATA, |
| 158 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 159 | (config->sata_port0_gen3_dtle & SATA_DTLE_MASK) |
| 160 | << SATA_DTLE_DATA_SHIFT); |
| 161 | |
| 162 | pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE, |
| 163 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 164 | (config->sata_port0_gen3_dtle & SATA_DTLE_MASK) |
| 165 | << SATA_DTLE_EDGE_SHIFT); |
| 166 | } |
| 167 | |
| 168 | if (config->sata_port1_gen3_dtle) { |
| 169 | pch_iobp_update(SATA_IOBP_SP1DTLE_DATA, |
| 170 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 171 | (config->sata_port1_gen3_dtle & SATA_DTLE_MASK) |
| 172 | << SATA_DTLE_DATA_SHIFT); |
| 173 | |
| 174 | pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE, |
| 175 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 176 | (config->sata_port1_gen3_dtle & SATA_DTLE_MASK) |
| 177 | << SATA_DTLE_EDGE_SHIFT); |
| 178 | } |
| 179 | |
Youness Alaoui | 696ebc2 | 2017-02-07 13:54:45 -0500 | [diff] [blame] | 180 | if (config->sata_port2_gen3_dtle) { |
| 181 | pch_iobp_update(SATA_IOBP_SP2DTLE_DATA, |
| 182 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 183 | (config->sata_port2_gen3_dtle & SATA_DTLE_MASK) |
| 184 | << SATA_DTLE_DATA_SHIFT); |
| 185 | |
| 186 | pch_iobp_update(SATA_IOBP_SP2DTLE_EDGE, |
| 187 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 188 | (config->sata_port2_gen3_dtle & SATA_DTLE_MASK) |
| 189 | << SATA_DTLE_EDGE_SHIFT); |
| 190 | } |
| 191 | if (config->sata_port3_gen3_dtle) { |
| 192 | pch_iobp_update(SATA_IOBP_SP3DTLE_DATA, |
| 193 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 194 | (config->sata_port3_gen3_dtle & SATA_DTLE_MASK) |
| 195 | << SATA_DTLE_DATA_SHIFT); |
| 196 | |
| 197 | pch_iobp_update(SATA_IOBP_SP3DTLE_EDGE, |
| 198 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 199 | (config->sata_port3_gen3_dtle & SATA_DTLE_MASK) |
| 200 | << SATA_DTLE_EDGE_SHIFT); |
| 201 | } |
| 202 | |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Additional Programming Requirements for Power Optimizer |
| 205 | */ |
| 206 | |
| 207 | /* Step 1 */ |
| 208 | sir_write(dev, 0x64, 0x883c9003); |
| 209 | |
| 210 | /* Step 2: SIR 68h[15:0] = 880Ah */ |
| 211 | reg32 = sir_read(dev, 0x68); |
| 212 | reg32 &= 0xffff0000; |
| 213 | reg32 |= 0x880a; |
| 214 | sir_write(dev, 0x68, reg32); |
| 215 | |
| 216 | /* Step 3: SIR 60h[3] = 1 */ |
| 217 | reg32 = sir_read(dev, 0x60); |
| 218 | reg32 |= (1 << 3); |
| 219 | sir_write(dev, 0x60, reg32); |
| 220 | |
| 221 | /* Step 4: SIR 60h[0] = 1 */ |
| 222 | reg32 = sir_read(dev, 0x60); |
| 223 | reg32 |= (1 << 0); |
| 224 | sir_write(dev, 0x60, reg32); |
| 225 | |
| 226 | /* Step 5: SIR 60h[1] = 1 */ |
| 227 | reg32 = sir_read(dev, 0x60); |
| 228 | reg32 |= (1 << 1); |
| 229 | sir_write(dev, 0x60, reg32); |
| 230 | |
| 231 | /* Clock Gating */ |
| 232 | sir_write(dev, 0x70, 0x3f00bf1f); |
| 233 | sir_write(dev, 0x54, 0xcf000f0f); |
| 234 | sir_write(dev, 0x58, 0x00190000); |
Duncan Laurie | 446fb8e | 2014-08-08 09:59:43 -0700 | [diff] [blame] | 235 | RCBA32_AND_OR(0x333c, 0xffcfffff, 0x00c00000); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 236 | |
| 237 | reg32 = pci_read_config32(dev, 0x300); |
Duncan Laurie | 446fb8e | 2014-08-08 09:59:43 -0700 | [diff] [blame] | 238 | reg32 |= (1 << 17) | (1 << 16) | (1 << 19); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 239 | reg32 |= (1 << 31) | (1 << 30) | (1 << 29); |
| 240 | pci_write_config32(dev, 0x300, reg32); |
Kane Chen | 8c1fd78 | 2014-08-19 10:51:46 -0700 | [diff] [blame] | 241 | |
Kane Chen | 4613472 | 2014-08-28 17:05:06 -0700 | [diff] [blame] | 242 | reg32 = pci_read_config32(dev, 0x98); |
| 243 | reg32 |= 1 << 29; |
| 244 | pci_write_config32(dev, 0x98, reg32); |
| 245 | |
Kane Chen | 8c1fd78 | 2014-08-19 10:51:46 -0700 | [diff] [blame] | 246 | /* Register Lock */ |
| 247 | reg32 = pci_read_config32(dev, 0x9c); |
| 248 | reg32 |= (1 << 31); |
| 249 | pci_write_config32(dev, 0x9c, reg32); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Set SATA controller mode early so the resource allocator can |
| 254 | * properly assign IO/Memory resources for the controller. |
| 255 | */ |
Elyes HAOUAS | 040aff2 | 2018-05-27 16:30:36 +0200 | [diff] [blame] | 256 | static void sata_enable(struct device *dev) |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 257 | { |
| 258 | /* Get the chip configuration */ |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 259 | config_t *config = config_of(dev); |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 260 | u16 map = 0x0060; |
| 261 | |
Wenkai Du | 038cce2 | 2014-12-05 14:04:10 -0800 | [diff] [blame] | 262 | map |= (config->sata_port_map ^ 0xf) << 8; |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 263 | |
| 264 | pci_write_config16(dev, 0x90, map); |
| 265 | } |
| 266 | |
| 267 | static struct device_operations sata_ops = { |
Elyes HAOUAS | 1d19127 | 2018-11-27 12:23:48 +0100 | [diff] [blame] | 268 | .read_resources = pci_dev_read_resources, |
| 269 | .set_resources = pci_dev_set_resources, |
| 270 | .enable_resources = pci_dev_enable_resources, |
| 271 | .init = sata_init, |
| 272 | .enable = sata_enable, |
Duncan Laurie | c88c54c | 2014-04-30 16:36:13 -0700 | [diff] [blame] | 273 | .ops_pci = &broadwell_pci_ops, |
| 274 | }; |
| 275 | |
| 276 | static const unsigned short pci_device_ids[] = { |
| 277 | 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* LynxPoint-LP */ |
| 278 | 0x9c83, 0x9c85, 0x282a, 0x9c87, 0x282a, 0x9c8f, /* WildcatPoint */ |
| 279 | 0 |
| 280 | }; |
| 281 | |
| 282 | static const struct pci_driver pch_sata __pci_driver = { |
| 283 | .ops = &sata_ops, |
| 284 | .vendor = PCI_VENDOR_ID_INTEL, |
| 285 | .devices = pci_device_ids, |
| 286 | }; |