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