Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * |
| 5 | * From coreboot src/soc/intel/broadwell/sata.c |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/intel_regs.h> |
| 14 | #include <asm/lpc_common.h> |
| 15 | #include <asm/pch_common.h> |
| 16 | #include <asm/pch_common.h> |
| 17 | #include <asm/arch/pch.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 19 | |
| 20 | struct sata_platdata { |
| 21 | int port_map; |
| 22 | uint port0_gen3_tx; |
| 23 | uint port1_gen3_tx; |
| 24 | uint port0_gen3_dtle; |
| 25 | uint port1_gen3_dtle; |
| 26 | |
| 27 | /* |
| 28 | * SATA DEVSLP Mux |
| 29 | * 0 = port 0 DEVSLP on DEVSLP0/GPIO33 |
| 30 | * 1 = port 3 DEVSLP on DEVSLP0/GPIO33 |
| 31 | */ |
| 32 | int devslp_mux; |
| 33 | |
| 34 | /* |
| 35 | * DEVSLP Disable |
| 36 | * 0: DEVSLP is enabled |
| 37 | * 1: DEVSLP is disabled |
| 38 | */ |
| 39 | int devslp_disable; |
| 40 | }; |
| 41 | |
| 42 | static void broadwell_sata_init(struct udevice *dev) |
| 43 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 44 | struct sata_platdata *plat = dev_get_plat(dev); |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 45 | u32 reg32; |
| 46 | u8 *abar; |
| 47 | u16 reg16; |
| 48 | int port; |
| 49 | |
| 50 | debug("SATA: Initializing controller in AHCI mode.\n"); |
| 51 | |
| 52 | /* Set timings */ |
| 53 | dm_pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE); |
| 54 | dm_pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE); |
| 55 | |
| 56 | /* for AHCI, Port Enable is managed in memory mapped space */ |
| 57 | dm_pci_read_config16(dev, 0x92, ®16); |
| 58 | reg16 &= ~0xf; |
| 59 | reg16 |= 0x8000 | plat->port_map; |
| 60 | dm_pci_write_config16(dev, 0x92, reg16); |
| 61 | udelay(2); |
| 62 | |
| 63 | /* Setup register 98h */ |
| 64 | dm_pci_read_config32(dev, 0x98, ®32); |
| 65 | reg32 &= ~((1 << 31) | (1 << 30)); |
| 66 | reg32 |= 1 << 23; |
| 67 | reg32 |= 1 << 24; /* Enable MPHY Dynamic Power Gating */ |
| 68 | dm_pci_write_config32(dev, 0x98, reg32); |
| 69 | |
| 70 | /* Setup register 9Ch */ |
| 71 | reg16 = 0; /* Disable alternate ID */ |
| 72 | reg16 = 1 << 5; /* BWG step 12 */ |
| 73 | dm_pci_write_config16(dev, 0x9c, reg16); |
| 74 | |
| 75 | /* SATA Initialization register */ |
| 76 | reg32 = 0x183; |
| 77 | reg32 |= (plat->port_map ^ 0xf) << 24; |
| 78 | reg32 |= (plat->devslp_mux & 1) << 15; |
| 79 | dm_pci_write_config32(dev, 0x94, reg32); |
| 80 | |
| 81 | /* Initialize AHCI memory-mapped space */ |
| 82 | dm_pci_read_config32(dev, PCI_BASE_ADDRESS_5, ®32); |
| 83 | abar = (u8 *)reg32; |
| 84 | debug("ABAR: %p\n", abar); |
| 85 | |
| 86 | /* CAP (HBA Capabilities) : enable power management */ |
| 87 | clrsetbits_le32(abar + 0x00, 0x00020060 /* SXS+EMS+PMS */, |
| 88 | 0x0c006000 /* PSC+SSC+SALP+SSS */ | |
| 89 | 1 << 18); /* SAM: SATA AHCI MODE ONLY */ |
| 90 | |
| 91 | /* PI (Ports implemented) */ |
| 92 | writel(plat->port_map, abar + 0x0c); |
| 93 | (void) readl(abar + 0x0c); /* Read back 1 */ |
| 94 | (void) readl(abar + 0x0c); /* Read back 2 */ |
| 95 | |
| 96 | /* CAP2 (HBA Capabilities Extended)*/ |
| 97 | if (plat->devslp_disable) { |
| 98 | clrbits_le32(abar + 0x24, 1 << 3); |
| 99 | } else { |
| 100 | /* Enable DEVSLP */ |
| 101 | setbits_le32(abar + 0x24, 1 << 5 | 1 << 4 | 1 << 3 | 1 << 2); |
| 102 | |
| 103 | for (port = 0; port < 4; port++) { |
| 104 | if (!(plat->port_map & (1 << port))) |
| 105 | continue; |
| 106 | /* DEVSLP DSP */ |
| 107 | setbits_le32(abar + 0x144 + (0x80 * port), 1 << 1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Static Power Gating for unused ports */ |
| 112 | reg32 = readl(RCB_REG(0x3a84)); |
| 113 | /* Port 3 and 2 disabled */ |
| 114 | if ((plat->port_map & ((1 << 3)|(1 << 2))) == 0) |
| 115 | reg32 |= (1 << 24) | (1 << 26); |
| 116 | /* Port 1 and 0 disabled */ |
| 117 | if ((plat->port_map & ((1 << 1)|(1 << 0))) == 0) |
| 118 | reg32 |= (1 << 20) | (1 << 18); |
| 119 | writel(reg32, RCB_REG(0x3a84)); |
| 120 | |
| 121 | /* Set Gen3 Transmitter settings if needed */ |
| 122 | if (plat->port0_gen3_tx) |
| 123 | pch_iobp_update(SATA_IOBP_SP0_SECRT88, |
| 124 | ~(SATA_SECRT88_VADJ_MASK << |
| 125 | SATA_SECRT88_VADJ_SHIFT), |
| 126 | (plat->port0_gen3_tx & |
| 127 | SATA_SECRT88_VADJ_MASK) |
| 128 | << SATA_SECRT88_VADJ_SHIFT); |
| 129 | |
| 130 | if (plat->port1_gen3_tx) |
| 131 | pch_iobp_update(SATA_IOBP_SP1_SECRT88, |
| 132 | ~(SATA_SECRT88_VADJ_MASK << |
| 133 | SATA_SECRT88_VADJ_SHIFT), |
| 134 | (plat->port1_gen3_tx & |
| 135 | SATA_SECRT88_VADJ_MASK) |
| 136 | << SATA_SECRT88_VADJ_SHIFT); |
| 137 | |
| 138 | /* Set Gen3 DTLE DATA / EDGE registers if needed */ |
| 139 | if (plat->port0_gen3_dtle) { |
| 140 | pch_iobp_update(SATA_IOBP_SP0DTLE_DATA, |
| 141 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 142 | (plat->port0_gen3_dtle & SATA_DTLE_MASK) |
| 143 | << SATA_DTLE_DATA_SHIFT); |
| 144 | |
| 145 | pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE, |
| 146 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 147 | (plat->port0_gen3_dtle & SATA_DTLE_MASK) |
| 148 | << SATA_DTLE_EDGE_SHIFT); |
| 149 | } |
| 150 | |
| 151 | if (plat->port1_gen3_dtle) { |
| 152 | pch_iobp_update(SATA_IOBP_SP1DTLE_DATA, |
| 153 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 154 | (plat->port1_gen3_dtle & SATA_DTLE_MASK) |
| 155 | << SATA_DTLE_DATA_SHIFT); |
| 156 | |
| 157 | pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE, |
| 158 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 159 | (plat->port1_gen3_dtle & SATA_DTLE_MASK) |
| 160 | << SATA_DTLE_EDGE_SHIFT); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Additional Programming Requirements for Power Optimizer |
| 165 | */ |
| 166 | |
| 167 | /* Step 1 */ |
| 168 | pch_common_sir_write(dev, 0x64, 0x883c9003); |
| 169 | |
| 170 | /* Step 2: SIR 68h[15:0] = 880Ah */ |
| 171 | reg32 = pch_common_sir_read(dev, 0x68); |
| 172 | reg32 &= 0xffff0000; |
| 173 | reg32 |= 0x880a; |
| 174 | pch_common_sir_write(dev, 0x68, reg32); |
| 175 | |
| 176 | /* Step 3: SIR 60h[3] = 1 */ |
| 177 | reg32 = pch_common_sir_read(dev, 0x60); |
| 178 | reg32 |= (1 << 3); |
| 179 | pch_common_sir_write(dev, 0x60, reg32); |
| 180 | |
| 181 | /* Step 4: SIR 60h[0] = 1 */ |
| 182 | reg32 = pch_common_sir_read(dev, 0x60); |
| 183 | reg32 |= (1 << 0); |
| 184 | pch_common_sir_write(dev, 0x60, reg32); |
| 185 | |
| 186 | /* Step 5: SIR 60h[1] = 1 */ |
| 187 | reg32 = pch_common_sir_read(dev, 0x60); |
| 188 | reg32 |= (1 << 1); |
| 189 | pch_common_sir_write(dev, 0x60, reg32); |
| 190 | |
| 191 | /* Clock Gating */ |
| 192 | pch_common_sir_write(dev, 0x70, 0x3f00bf1f); |
| 193 | pch_common_sir_write(dev, 0x54, 0xcf000f0f); |
| 194 | pch_common_sir_write(dev, 0x58, 0x00190000); |
| 195 | clrsetbits_le32(RCB_REG(0x333c), 0x00300000, 0x00c00000); |
| 196 | |
| 197 | dm_pci_read_config32(dev, 0x300, ®32); |
| 198 | reg32 |= 1 << 17 | 1 << 16 | 1 << 19; |
| 199 | reg32 |= 1 << 31 | 1 << 30 | 1 << 29; |
| 200 | dm_pci_write_config32(dev, 0x300, reg32); |
| 201 | |
| 202 | dm_pci_read_config32(dev, 0x98, ®32); |
| 203 | reg32 |= 1 << 29; |
| 204 | dm_pci_write_config32(dev, 0x98, reg32); |
| 205 | |
| 206 | /* Register Lock */ |
| 207 | dm_pci_read_config32(dev, 0x9c, ®32); |
| 208 | reg32 |= 1 << 31; |
| 209 | dm_pci_write_config32(dev, 0x9c, reg32); |
| 210 | } |
| 211 | |
| 212 | static int broadwell_sata_enable(struct udevice *dev) |
| 213 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 214 | struct sata_platdata *plat = dev_get_plat(dev); |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 215 | struct gpio_desc desc; |
| 216 | u16 map; |
| 217 | int ret; |
| 218 | |
| 219 | /* |
| 220 | * Set SATA controller mode early so the resource allocator can |
| 221 | * properly assign IO/Memory resources for the controller. |
| 222 | */ |
| 223 | map = 0x0060; |
| 224 | |
| 225 | map |= (plat->port_map ^ 0x3f) << 8; |
| 226 | dm_pci_write_config16(dev, 0x90, map); |
| 227 | |
| 228 | ret = gpio_request_by_name(dev, "reset-gpio", 0, &desc, GPIOD_IS_OUT); |
| 229 | if (ret) |
| 230 | return ret; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame^] | 235 | static int broadwell_sata_of_to_plat(struct udevice *dev) |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 236 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 237 | struct sata_platdata *plat = dev_get_plat(dev); |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 238 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 239 | int node = dev_of_offset(dev); |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 240 | |
| 241 | plat->port_map = fdtdec_get_int(blob, node, "intel,sata-port-map", 0); |
| 242 | plat->port0_gen3_tx = fdtdec_get_int(blob, node, |
| 243 | "intel,sata-port0-gen3-tx", 0); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int broadwell_sata_probe(struct udevice *dev) |
| 249 | { |
| 250 | if (!(gd->flags & GD_FLG_RELOC)) |
| 251 | return broadwell_sata_enable(dev); |
| 252 | else |
| 253 | broadwell_sata_init(dev); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static const struct udevice_id broadwell_ahci_ids[] = { |
| 259 | { .compatible = "intel,wildcatpoint-ahci" }, |
| 260 | { } |
| 261 | }; |
| 262 | |
| 263 | U_BOOT_DRIVER(ahci_broadwell_drv) = { |
| 264 | .name = "ahci_broadwell", |
Simon Glass | 85ee165 | 2016-05-01 11:35:52 -0600 | [diff] [blame] | 265 | .id = UCLASS_AHCI, |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 266 | .of_match = broadwell_ahci_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame^] | 267 | .of_to_plat = broadwell_sata_of_to_plat, |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 268 | .probe = broadwell_sata_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 269 | .plat_auto = sizeof(struct sata_platdata), |
Simon Glass | 4979d07 | 2016-03-11 22:07:21 -0700 | [diff] [blame] | 270 | }; |