Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 3 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 5 | #include <device/pci_ops.h> |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <device/device.h> |
| 8 | #include <device/pci.h> |
| 9 | #include <device/pci_ids.h> |
Kyösti Mälkki | cbf9571 | 2020-01-05 08:05:45 +0200 | [diff] [blame] | 10 | #include <option.h> |
Alexander Couzens | 7bf47ee | 2015-04-16 02:00:21 +0200 | [diff] [blame] | 11 | #include <acpi/sata.h> |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 12 | #include <types.h> |
| 13 | |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 14 | #include "chip.h" |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 15 | #include "pch.h" |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 16 | |
| 17 | typedef struct southbridge_intel_bd82x6x_config config_t; |
| 18 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame] | 19 | static inline u32 sir_read(struct device *dev, int idx) |
| 20 | { |
| 21 | pci_write_config32(dev, SATA_SIRI, idx); |
| 22 | return pci_read_config32(dev, SATA_SIRD); |
| 23 | } |
| 24 | |
| 25 | static inline void sir_write(struct device *dev, int idx, u32 value) |
| 26 | { |
| 27 | pci_write_config32(dev, SATA_SIRI, idx); |
| 28 | pci_write_config32(dev, SATA_SIRD, value); |
| 29 | } |
| 30 | |
Nico Huber | 63be060 | 2019-02-13 15:11:09 +0100 | [diff] [blame] | 31 | static void sata_read_resources(struct device *dev) |
| 32 | { |
| 33 | struct resource *res; |
| 34 | |
| 35 | pci_dev_read_resources(dev); |
| 36 | |
| 37 | /* Assign fixed resources for IDE legacy mode */ |
| 38 | |
| 39 | u8 sata_mode = 0; |
| 40 | get_option(&sata_mode, "sata_mode"); |
| 41 | if (sata_mode != 2) |
| 42 | return; |
| 43 | |
| 44 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 45 | if (res) { |
| 46 | res->base = 0x1f0; |
| 47 | res->size = 8; |
| 48 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 49 | } |
| 50 | |
| 51 | res = find_resource(dev, PCI_BASE_ADDRESS_1); |
| 52 | if (res) { |
| 53 | res->base = 0x3f4; |
| 54 | res->size = 4; |
| 55 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 56 | } |
| 57 | |
| 58 | res = find_resource(dev, PCI_BASE_ADDRESS_2); |
| 59 | if (res) { |
| 60 | res->base = 0x170; |
| 61 | res->size = 8; |
| 62 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 63 | } |
| 64 | |
| 65 | res = find_resource(dev, PCI_BASE_ADDRESS_3); |
| 66 | if (res) { |
| 67 | res->base = 0x374; |
| 68 | res->size = 4; |
| 69 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void sata_set_resources(struct device *dev) |
| 74 | { |
| 75 | /* work around bug in pci_dev_set_resources(), it bails out on FIXED */ |
| 76 | u8 sata_mode = 0; |
| 77 | get_option(&sata_mode, "sata_mode"); |
| 78 | if (sata_mode == 2) { |
| 79 | unsigned int i; |
| 80 | for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_3; i += 4) { |
| 81 | struct resource *const res = find_resource(dev, i); |
| 82 | if (res) |
| 83 | res->flags &= ~IORESOURCE_FIXED; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pci_dev_set_resources(dev); |
| 88 | } |
| 89 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 90 | static void sata_init(struct device *dev) |
| 91 | { |
| 92 | u32 reg32; |
| 93 | u16 reg16; |
| 94 | /* Get the chip configuration */ |
| 95 | config_t *config = dev->chip_info; |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 96 | u8 sata_mode; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 97 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame] | 98 | printk(BIOS_DEBUG, "SATA: Initializing...\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 99 | |
| 100 | if (config == NULL) { |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame] | 101 | printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 105 | if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS) |
| 106 | /* Default to AHCI */ |
| 107 | sata_mode = 0; |
| 108 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 109 | /* SATA configuration */ |
| 110 | |
| 111 | /* Enable BARs */ |
| 112 | pci_write_config16(dev, PCI_COMMAND, 0x0007); |
| 113 | |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 114 | /* AHCI */ |
| 115 | if (sata_mode == 0) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 116 | u8 *abar; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 117 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame] | 118 | printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 119 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 120 | /* Set timings */ |
| 121 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 122 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 123 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 124 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 125 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
| 126 | |
| 127 | /* Sync DMA */ |
| 128 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0); |
| 129 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0001); |
| 130 | |
| 131 | /* Set IDE I/O Configuration */ |
| 132 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
| 133 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 134 | |
| 135 | /* for AHCI, Port Enable is managed in memory mapped space */ |
| 136 | reg16 = pci_read_config16(dev, 0x92); |
| 137 | reg16 &= ~0x3f; /* 6 ports SKU + ORM */ |
| 138 | reg16 |= 0x8000 | config->sata_port_map; |
| 139 | pci_write_config16(dev, 0x92, reg16); |
| 140 | |
| 141 | /* SATA Initialization register */ |
| 142 | pci_write_config32(dev, 0x94, |
| 143 | ((config->sata_port_map ^ 0x3f) << 24) | 0x183); |
| 144 | |
| 145 | /* Initialize AHCI memory-mapped space */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 146 | abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5); |
| 147 | printk(BIOS_DEBUG, "ABAR: %p\n", abar); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 148 | /* CAP (HBA Capabilities) : enable power management */ |
| 149 | reg32 = read32(abar + 0x00); |
| 150 | reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS |
| 151 | reg32 &= ~0x00020060; // clear SXS+EMS+PMS |
Shawn Nematbakhsh | c9fc029 | 2013-03-14 10:44:13 -0700 | [diff] [blame] | 152 | /* Set ISS, if available */ |
| 153 | if (config->sata_interface_speed_support) |
| 154 | { |
| 155 | reg32 &= ~0x00f00000; |
| 156 | reg32 |= (config->sata_interface_speed_support & 0x03) |
| 157 | << 20; |
| 158 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 159 | write32(abar + 0x00, reg32); |
| 160 | /* PI (Ports implemented) */ |
| 161 | write32(abar + 0x0c, config->sata_port_map); |
| 162 | (void) read32(abar + 0x0c); /* Read back 1 */ |
| 163 | (void) read32(abar + 0x0c); /* Read back 2 */ |
| 164 | /* CAP2 (HBA Capabilities Extended)*/ |
| 165 | reg32 = read32(abar + 0x24); |
| 166 | reg32 &= ~0x00000002; |
| 167 | write32(abar + 0x24, reg32); |
| 168 | /* VSP (Vendor Specific Register */ |
| 169 | reg32 = read32(abar + 0xa0); |
| 170 | reg32 &= ~0x00000005; |
| 171 | write32(abar + 0xa0, reg32); |
| 172 | } else { |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 173 | /* IDE */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 174 | |
Felix Singer | 192666f | 2020-04-06 10:54:42 +0200 | [diff] [blame] | 175 | /* Without AHCI BAR no memory decoding */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 176 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 177 | reg16 &= ~PCI_COMMAND_MEMORY; |
| 178 | pci_write_config16(dev, PCI_COMMAND, reg16); |
| 179 | |
Nico Huber | 63be060 | 2019-02-13 15:11:09 +0100 | [diff] [blame] | 180 | if (sata_mode == 1) { |
| 181 | /* Native mode on both primary and secondary. */ |
| 182 | pci_or_config8(dev, 0x09, 0x05); |
| 183 | printk(BIOS_DEBUG, "SATA: Controller in IDE compat mode.\n"); |
| 184 | } else { |
| 185 | /* Legacy mode on both primary and secondary. */ |
| 186 | pci_update_config8(dev, 0x09, ~0x05, 0x00); |
| 187 | printk(BIOS_DEBUG, "SATA: Controller in IDE legacy mode.\n"); |
| 188 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 189 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 190 | /* Set timings */ |
| 191 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 192 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 193 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 194 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 195 | IDE_SITRE | IDE_ISP_3_CLOCKS | |
| 196 | IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0); |
| 197 | |
| 198 | /* Sync DMA */ |
| 199 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0); |
| 200 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0201); |
| 201 | |
| 202 | /* Set IDE I/O Configuration */ |
| 203 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
| 204 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 205 | |
| 206 | /* Port enable */ |
| 207 | reg16 = pci_read_config16(dev, 0x92); |
| 208 | reg16 &= ~0x3f; |
| 209 | reg16 |= config->sata_port_map; |
| 210 | pci_write_config16(dev, 0x92, reg16); |
| 211 | |
| 212 | /* SATA Initialization register */ |
| 213 | pci_write_config32(dev, 0x94, |
| 214 | ((config->sata_port_map ^ 0x3f) << 24) | 0x183); |
| 215 | } |
Duncan Laurie | cfb64bd | 2012-07-16 16:16:31 -0700 | [diff] [blame] | 216 | |
| 217 | /* Set Gen3 Transmitter settings if needed */ |
| 218 | if (config->sata_port0_gen3_tx) |
| 219 | pch_iobp_update(SATA_IOBP_SP0G3IR, 0, |
| 220 | config->sata_port0_gen3_tx); |
| 221 | |
| 222 | if (config->sata_port1_gen3_tx) |
| 223 | pch_iobp_update(SATA_IOBP_SP1G3IR, 0, |
| 224 | config->sata_port1_gen3_tx); |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame] | 225 | |
| 226 | /* Additional Programming Requirements */ |
| 227 | sir_write(dev, 0x04, 0x00001600); |
| 228 | sir_write(dev, 0x28, 0xa0000033); |
| 229 | reg32 = sir_read(dev, 0x54); |
| 230 | reg32 &= 0xff000000; |
| 231 | reg32 |= 0x5555aa; |
| 232 | sir_write(dev, 0x54, reg32); |
| 233 | sir_write(dev, 0x64, 0xcccc8484); |
| 234 | reg32 = sir_read(dev, 0x68); |
| 235 | reg32 &= 0xffff0000; |
| 236 | reg32 |= 0xcccc; |
| 237 | sir_write(dev, 0x68, reg32); |
| 238 | reg32 = sir_read(dev, 0x78); |
| 239 | reg32 &= 0x0000ffff; |
| 240 | reg32 |= 0x88880000; |
| 241 | sir_write(dev, 0x78, reg32); |
| 242 | sir_write(dev, 0x84, 0x001c7000); |
| 243 | sir_write(dev, 0x88, 0x88338822); |
| 244 | sir_write(dev, 0xa0, 0x001c7000); |
| 245 | // a4 |
| 246 | sir_write(dev, 0xc4, 0x0c0c0c0c); |
| 247 | sir_write(dev, 0xc8, 0x0c0c0c0c); |
| 248 | sir_write(dev, 0xd4, 0x10000000); |
| 249 | |
| 250 | pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000); |
| 251 | pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100); |
Felix Singer | 7daf3cd | 2020-04-06 11:01:28 +0200 | [diff] [blame] | 252 | |
| 253 | pci_update_config32(dev, 0x98, |
| 254 | ~(1 << 16 | 0x3f << 7 | 3 << 5 | 3 << 3), |
| 255 | 1 << 24 | 1 << 22 | 1 << 20 | 1 << 19 | |
| 256 | 1 << 18 | 1 << 14 | 0x04 << 7 | 1 << 3); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Elyes HAOUAS | 4aec340 | 2018-05-25 08:29:27 +0200 | [diff] [blame] | 259 | static void sata_enable(struct device *dev) |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 260 | { |
| 261 | /* Get the chip configuration */ |
| 262 | config_t *config = dev->chip_info; |
| 263 | u16 map = 0; |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 264 | u8 sata_mode; |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 265 | |
| 266 | if (!config) |
| 267 | return; |
| 268 | |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 269 | if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS) |
| 270 | sata_mode = 0; |
| 271 | |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 272 | /* |
| 273 | * Set SATA controller mode early so the resource allocator can |
| 274 | * properly assign IO/Memory resources for the controller. |
| 275 | */ |
Vladimir Serbinenko | 6d6298d | 2014-01-11 07:46:50 +0100 | [diff] [blame] | 276 | if (sata_mode == 0) |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 277 | map = 0x0060; |
| 278 | |
| 279 | map |= (config->sata_port_map ^ 0x3f) << 8; |
| 280 | |
| 281 | pci_write_config16(dev, 0x90, map); |
| 282 | } |
| 283 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 284 | static const char *sata_acpi_name(const struct device *dev) |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 285 | { |
| 286 | return "SATA"; |
| 287 | } |
| 288 | |
Elyes HAOUAS | 4aec340 | 2018-05-25 08:29:27 +0200 | [diff] [blame] | 289 | static void sata_fill_ssdt(struct device *dev) |
Alexander Couzens | 7bf47ee | 2015-04-16 02:00:21 +0200 | [diff] [blame] | 290 | { |
| 291 | config_t *config = dev->chip_info; |
| 292 | generate_sata_ssdt_ports("\\_SB_.PCI0.SATA", config->sata_port_map); |
| 293 | } |
| 294 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 295 | static struct pci_operations sata_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 296 | .set_subsystem = pci_dev_set_subsystem, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | static struct device_operations sata_ops = { |
Nico Huber | 63be060 | 2019-02-13 15:11:09 +0100 | [diff] [blame] | 300 | .read_resources = sata_read_resources, |
| 301 | .set_resources = sata_set_resources, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 302 | .enable_resources = pci_dev_enable_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 303 | .acpi_fill_ssdt = sata_fill_ssdt, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 304 | .init = sata_init, |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 305 | .enable = sata_enable, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 306 | .ops_pci = &sata_pci_ops, |
Patrick Rudolph | 604f698 | 2017-06-07 09:46:52 +0200 | [diff] [blame] | 307 | .acpi_name = sata_acpi_name, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 308 | }; |
| 309 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 310 | static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03, |
| 311 | 0x1e00, 0x1e01, 0x1e02, 0x1e03, |
| 312 | 0 }; |
| 313 | |
| 314 | static const struct pci_driver pch_sata __pci_driver = { |
| 315 | .ops = &sata_ops, |
| 316 | .vendor = PCI_VENDOR_ID_INTEL, |
| 317 | .devices = pci_device_ids, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 318 | }; |