Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [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> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <device/device.h> |
| 7 | #include <device/pci.h> |
| 8 | #include <device/pci_ids.h> |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 9 | #include <delay.h> |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 10 | #include "chip.h" |
Angel Pons | 2178b72 | 2020-05-31 00:55:35 +0200 | [diff] [blame] | 11 | #include "iobp.h" |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 12 | #include "pch.h" |
| 13 | |
| 14 | typedef struct southbridge_intel_lynxpoint_config config_t; |
| 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 | |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 28 | static inline void sir_unset_and_set_mask(struct device *dev, int idx, u32 unset, u32 set) |
| 29 | { |
| 30 | pci_write_config32(dev, SATA_SIRI, idx); |
| 31 | |
| 32 | const u32 value = pci_read_config32(dev, SATA_SIRD) & ~unset; |
| 33 | pci_write_config32(dev, SATA_SIRD, value | set); |
| 34 | } |
| 35 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 36 | static void sata_init(struct device *dev) |
| 37 | { |
| 38 | u32 reg32; |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 39 | |
| 40 | u32 *abar; |
| 41 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 42 | /* Get the chip configuration */ |
| 43 | config_t *config = dev->chip_info; |
| 44 | |
| 45 | printk(BIOS_DEBUG, "SATA: Initializing...\n"); |
| 46 | |
| 47 | if (config == NULL) { |
| 48 | printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /* SATA configuration */ |
| 53 | |
Angel Pons | 1b85692 | 2020-10-30 15:30:48 +0100 | [diff] [blame] | 54 | /* Enable memory space decoding for ABAR */ |
| 55 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 56 | |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 57 | printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 58 | |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 59 | /* Set Interrupt Line */ |
| 60 | /* Interrupt Pin is set by D31IP.PIP */ |
Angel Pons | 93859e3 | 2020-11-02 12:08:50 +0100 | [diff] [blame^] | 61 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0x0a); |
Angel Pons | bf9bc50 | 2020-06-08 00:12:43 +0200 | [diff] [blame] | 62 | |
Angel Pons | 93859e3 | 2020-11-02 12:08:50 +0100 | [diff] [blame^] | 63 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE); |
| 64 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 65 | |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 66 | /* for AHCI, Port Enable is managed in memory mapped space */ |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 67 | pci_update_config16(dev, 0x92, ~0x3f, 0x8000 | config->sata_port_map); |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 68 | udelay(2); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 69 | |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 70 | /* Setup register 98h */ |
| 71 | reg32 = pci_read_config16(dev, 0x98); |
| 72 | reg32 |= 1 << 19; /* BWG step 6 */ |
| 73 | reg32 |= 1 << 22; /* BWG step 5 */ |
| 74 | reg32 &= ~(0x3f << 7); |
| 75 | reg32 |= 0x04 << 7; /* BWG step 7 */ |
| 76 | reg32 |= 1 << 20; /* BWG step 8 */ |
| 77 | reg32 &= ~(0x03 << 5); |
| 78 | reg32 |= 1 << 5; /* BWG step 9 */ |
| 79 | reg32 |= 1 << 18; /* BWG step 10 */ |
| 80 | reg32 |= 1 << 29; /* BWG step 11 */ |
| 81 | if (pch_is_lp()) { |
| 82 | reg32 &= ~((1 << 31) | (1 << 30)); |
| 83 | reg32 |= 1 << 23; |
| 84 | reg32 |= 1 << 24; /* Disable listen mode (hotplug) */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 85 | } |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 86 | pci_write_config32(dev, 0x98, reg32); |
| 87 | |
| 88 | /* Setup register 9Ch: Disable alternate ID and BWG step 12 */ |
| 89 | pci_write_config16(dev, 0x9c, 1 << 5); |
| 90 | |
| 91 | /* SATA Initialization register */ |
| 92 | reg32 = 0x183; |
| 93 | reg32 |= (config->sata_port_map ^ 0x3f) << 24; |
| 94 | reg32 |= (config->sata_devslp_mux & 1) << 15; |
| 95 | pci_write_config32(dev, 0x94, reg32); |
| 96 | |
| 97 | /* Initialize AHCI memory-mapped space */ |
| 98 | abar = (u32 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5); |
| 99 | printk(BIOS_DEBUG, "ABAR: %p\n", abar); |
| 100 | /* CAP (HBA Capabilities) : enable power management */ |
| 101 | reg32 = read32(abar + 0x00); |
| 102 | reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS |
| 103 | reg32 &= ~0x00020060; // clear SXS+EMS+PMS |
| 104 | if (pch_is_lp()) |
| 105 | reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY |
| 106 | write32(abar + 0x00, reg32); |
| 107 | /* PI (Ports implemented) */ |
| 108 | write32(abar + 0x03, config->sata_port_map); |
| 109 | (void)read32(abar + 0x03); /* Read back 1 */ |
| 110 | (void)read32(abar + 0x03); /* Read back 2 */ |
| 111 | /* CAP2 (HBA Capabilities Extended)*/ |
| 112 | reg32 = read32(abar + 0x09); |
| 113 | /* Enable DEVSLP */ |
| 114 | if (pch_is_lp()) { |
| 115 | if (config->sata_devslp_disable) |
| 116 | reg32 &= ~(1 << 3); |
| 117 | else |
| 118 | reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2); |
| 119 | } else { |
| 120 | reg32 &= ~0x00000002; |
| 121 | } |
| 122 | write32(abar + 0x09, reg32); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 123 | |
| 124 | /* Set Gen3 Transmitter settings if needed */ |
| 125 | if (config->sata_port0_gen3_tx) |
| 126 | pch_iobp_update(SATA_IOBP_SP0G3IR, 0, |
| 127 | config->sata_port0_gen3_tx); |
| 128 | |
| 129 | if (config->sata_port1_gen3_tx) |
| 130 | pch_iobp_update(SATA_IOBP_SP1G3IR, 0, |
| 131 | config->sata_port1_gen3_tx); |
| 132 | |
Shawn Nematbakhsh | 2875227 | 2013-08-13 10:45:21 -0700 | [diff] [blame] | 133 | /* Set Gen3 DTLE DATA / EDGE registers if needed */ |
| 134 | if (config->sata_port0_gen3_dtle) { |
| 135 | pch_iobp_update(SATA_IOBP_SP0DTLE_DATA, |
| 136 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 137 | (config->sata_port0_gen3_dtle & SATA_DTLE_MASK) |
| 138 | << SATA_DTLE_DATA_SHIFT); |
| 139 | |
| 140 | pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE, |
| 141 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 142 | (config->sata_port0_gen3_dtle & SATA_DTLE_MASK) |
| 143 | << SATA_DTLE_EDGE_SHIFT); |
| 144 | } |
| 145 | |
| 146 | if (config->sata_port1_gen3_dtle) { |
| 147 | pch_iobp_update(SATA_IOBP_SP1DTLE_DATA, |
| 148 | ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT), |
| 149 | (config->sata_port1_gen3_dtle & SATA_DTLE_MASK) |
| 150 | << SATA_DTLE_DATA_SHIFT); |
| 151 | |
| 152 | pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE, |
| 153 | ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT), |
| 154 | (config->sata_port1_gen3_dtle & SATA_DTLE_MASK) |
| 155 | << SATA_DTLE_EDGE_SHIFT); |
| 156 | } |
| 157 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 158 | /* Additional Programming Requirements */ |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 159 | /* Power Optimizer */ |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 160 | |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 161 | /* Step 1 */ |
Duncan Laurie | 70f04b4 | 2013-03-08 17:17:33 -0800 | [diff] [blame] | 162 | if (pch_is_lp()) |
| 163 | sir_write(dev, 0x64, 0x883c9003); |
| 164 | else |
| 165 | sir_write(dev, 0x64, 0x883c9001); |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 166 | |
| 167 | /* Step 2: SIR 68h[15:0] = 880Ah */ |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 168 | sir_unset_and_set_mask(dev, 0x68, 0xffff, 0x880a); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 169 | |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 170 | /* Step 3: SIR 60h[3] = 1 */ |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 171 | sir_unset_and_set_mask(dev, 0x60, 0, 1 << 3); |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 172 | |
| 173 | /* Step 4: SIR 60h[0] = 1 */ |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 174 | sir_unset_and_set_mask(dev, 0x60, 0, 1 << 0); |
Duncan Laurie | 26e7dd7 | 2012-12-19 09:12:31 -0800 | [diff] [blame] | 175 | |
| 176 | /* Step 5: SIR 60h[1] = 1 */ |
Angel Pons | d00af4f | 2020-10-30 12:56:02 +0100 | [diff] [blame] | 177 | sir_unset_and_set_mask(dev, 0x60, 0, 1 << 1); |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 178 | |
| 179 | /* Clock Gating */ |
| 180 | sir_write(dev, 0x70, 0x3f00bf1f); |
Duncan Laurie | 70f04b4 | 2013-03-08 17:17:33 -0800 | [diff] [blame] | 181 | if (pch_is_lp()) { |
| 182 | sir_write(dev, 0x54, 0xcf000f0f); |
| 183 | sir_write(dev, 0x58, 0x00190000); |
| 184 | } |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 185 | |
| 186 | reg32 = pci_read_config32(dev, 0x300); |
| 187 | reg32 |= (1 << 17) | (1 << 16); |
Angel Pons | 8963f7d | 2020-10-24 12:20:28 +0200 | [diff] [blame] | 188 | reg32 |= (1 << 31) | (1 << 30) | (1 << 29); |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 189 | pci_write_config32(dev, 0x300, reg32); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Elyes HAOUAS | 7a5f771 | 2018-06-08 17:20:38 +0200 | [diff] [blame] | 192 | static void sata_enable(struct device *dev) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 193 | { |
| 194 | /* Get the chip configuration */ |
| 195 | config_t *config = dev->chip_info; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 196 | |
| 197 | if (!config) |
| 198 | return; |
| 199 | |
| 200 | /* |
| 201 | * Set SATA controller mode early so the resource allocator can |
| 202 | * properly assign IO/Memory resources for the controller. |
| 203 | */ |
Angel Pons | 8084b38 | 2020-10-30 10:56:31 +0100 | [diff] [blame] | 204 | pci_write_config16(dev, 0x90, 0x0060 | (config->sata_port_map ^ 0x3f) << 8); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 207 | static struct device_operations sata_ops = { |
| 208 | .read_resources = pci_dev_read_resources, |
| 209 | .set_resources = pci_dev_set_resources, |
| 210 | .enable_resources = pci_dev_enable_resources, |
| 211 | .init = sata_init, |
| 212 | .enable = sata_enable, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 213 | .ops_pci = &pci_dev_ops_pci, |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
Duncan Laurie | 74c0d05 | 2012-12-17 11:31:40 -0800 | [diff] [blame] | 216 | static const unsigned short pci_device_ids[] = { |
| 217 | 0x8c00, 0x8c02, 0x8c04, 0x8c06, 0x8c08, 0x8c0e, /* Desktop */ |
| 218 | 0x8c01, 0x8c03, 0x8c05, 0x8c07, 0x8c09, 0x8c0f, /* Mobile */ |
| 219 | 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* Low Power */ |
| 220 | 0 |
| 221 | }; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 222 | |
| 223 | static const struct pci_driver pch_sata __pci_driver = { |
| 224 | .ops = &sata_ops, |
| 225 | .vendor = PCI_VENDOR_ID_INTEL, |
| 226 | .devices = pci_device_ids, |
| 227 | }; |