Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 coresystems GmbH |
| 5 | * 2012 secunet Security Networks AG |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; version 2 of |
| 10 | * the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <arch/io.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 19 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 20 | #include <device/pci_ops.h> |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 21 | #include <console/console.h> |
| 22 | #include <device/device.h> |
| 23 | #include <device/pci.h> |
| 24 | #include <device/pci_ids.h> |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 25 | #include <pc80/mc146818rtc.h> |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 26 | #include <types.h> |
| 27 | |
Kyösti Mälkki | 12b121c | 2019-08-18 16:33:39 +0300 | [diff] [blame] | 28 | #include "chip.h" |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 29 | #include "i82801ix.h" |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 30 | |
| 31 | typedef struct southbridge_intel_i82801ix_config config_t; |
| 32 | |
| 33 | static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map, |
| 34 | const int is_mobile) |
| 35 | { |
| 36 | int i; |
| 37 | u32 reg32; |
Patrick Rudolph | 4af2add | 2018-11-26 15:56:11 +0100 | [diff] [blame] | 38 | struct resource *res; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 39 | |
| 40 | /* Initialize AHCI memory-mapped space */ |
Patrick Rudolph | 4af2add | 2018-11-26 15:56:11 +0100 | [diff] [blame] | 41 | res = find_resource(dev, PCI_BASE_ADDRESS_5); |
| 42 | if (!res) |
| 43 | return; |
| 44 | |
| 45 | u8 *abar = res2mmio(res, 0, 0); |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 46 | printk(BIOS_DEBUG, "ABAR: %p\n", abar); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 47 | |
| 48 | /* Set AHCI access mode. |
| 49 | No other ABAR registers should be accessed before this. */ |
| 50 | reg32 = read32(abar + 0x04); |
| 51 | reg32 |= 1 << 31; |
| 52 | write32(abar + 0x04, reg32); |
| 53 | |
| 54 | /* CAP (HBA Capabilities) : enable power management */ |
| 55 | reg32 = read32(abar + 0x00); |
| 56 | /* CCCS must be set. */ |
| 57 | reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */ |
| 58 | reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */ |
| 59 | write32(abar + 0x00, reg32); |
| 60 | |
| 61 | /* PI (Ports implemented) */ |
| 62 | write32(abar + 0x0c, port_map); |
| 63 | /* PCH code reads back twice, do we need it, too? */ |
| 64 | (void) read32(abar + 0x0c); /* Read back 1 */ |
| 65 | (void) read32(abar + 0x0c); /* Read back 2 */ |
| 66 | |
| 67 | /* VSP (Vendor Specific Register) */ |
| 68 | reg32 = read32(abar + 0xa0); |
| 69 | reg32 &= ~0x00000001; /* clear SLPD */ |
| 70 | write32(abar + 0xa0, reg32); |
| 71 | |
| 72 | /* Lock R/WO bits in Port command registers. */ |
| 73 | for (i = 0; i < 6; ++i) { |
| 74 | if (((i == 2) || (i == 3)) && is_mobile) |
| 75 | continue; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 76 | u8 *addr = abar + 0x118 + (i * 0x80); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 77 | write32(addr, read32(addr)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void sata_program_indexed(struct device *const dev, const int is_mobile) |
| 82 | { |
| 83 | u32 reg32; |
| 84 | |
| 85 | pci_write_config8(dev, D31F2_SIDX, 0x18); |
| 86 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 87 | reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0)); |
| 88 | reg32 |= (3 << 3) | (3 << 0); |
| 89 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 90 | |
| 91 | pci_write_config8(dev, D31F2_SIDX, 0x28); |
| 92 | pci_write_config32(dev, D31F2_SDAT, 0x00cc2080); |
| 93 | |
| 94 | pci_write_config8(dev, D31F2_SIDX, 0x40); |
| 95 | pci_write_config8(dev, D31F2_SDAT + 2, 0x22); |
| 96 | |
| 97 | pci_write_config8(dev, D31F2_SIDX, 0x78); |
| 98 | pci_write_config8(dev, D31F2_SDAT + 2, 0x22); |
| 99 | |
| 100 | if (!is_mobile) { |
| 101 | pci_write_config8(dev, D31F2_SIDX, 0x84); |
| 102 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 103 | reg32 &= ~((7 << 3) | (7 << 0)); |
| 104 | reg32 |= (3 << 3) | (3 << 0); |
| 105 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 106 | } |
| 107 | |
| 108 | pci_write_config8(dev, D31F2_SIDX, 0x88); |
| 109 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 110 | if (!is_mobile) |
| 111 | reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8)); |
| 112 | reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0)); |
| 113 | if (!is_mobile) |
| 114 | reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8); |
| 115 | reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0); |
| 116 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 117 | |
| 118 | pci_write_config8(dev, D31F2_SIDX, 0x8c); |
| 119 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 120 | if (!is_mobile) |
| 121 | reg32 &= ~((7 << 27) | (7 << 24)); |
| 122 | reg32 &= ~((7 << 19) | (7 << 16) | 0xffff); |
| 123 | if (!is_mobile) |
| 124 | reg32 |= (2 << 27) | (2 << 24); |
| 125 | reg32 |= (2 << 19) | (2 << 16) | 0x00aa; |
| 126 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 127 | |
| 128 | pci_write_config8(dev, D31F2_SIDX, 0x94); |
| 129 | pci_write_config32(dev, D31F2_SDAT, 0x00000022); |
| 130 | |
| 131 | pci_write_config8(dev, D31F2_SIDX, 0xa0); |
| 132 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 133 | reg32 &= ~((7 << 3) | (7 << 0)); |
| 134 | reg32 |= (3 << 3) | (3 << 0); |
| 135 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 136 | |
| 137 | pci_write_config8(dev, D31F2_SIDX, 0xa8); |
| 138 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 139 | reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0)); |
| 140 | reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0); |
| 141 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 142 | |
| 143 | pci_write_config8(dev, D31F2_SIDX, 0xac); |
| 144 | reg32 = pci_read_config32(dev, D31F2_SDAT); |
| 145 | reg32 &= ~((7 << 19) | (7 << 16) | 0xffff); |
| 146 | reg32 |= (2 << 19) | (2 << 16) | 0x000a; |
| 147 | pci_write_config32(dev, D31F2_SDAT, reg32); |
| 148 | } |
| 149 | |
| 150 | static void sata_init(struct device *const dev) |
| 151 | { |
| 152 | u16 reg16; |
| 153 | |
| 154 | /* Get the chip configuration */ |
| 155 | const config_t *const config = dev->chip_info; |
| 156 | |
| 157 | const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID); |
Felix Singer | 7f8b0cd8 | 2019-11-10 11:04:08 +0100 | [diff] [blame] | 158 | const int is_mobile = (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01) || |
| 159 | (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145); |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 160 | u8 sata_mode; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 161 | |
| 162 | printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n"); |
| 163 | |
| 164 | if (config == NULL) { |
| 165 | printk(BIOS_ERR, "i82801ix_sata: error: " |
| 166 | "device not in devicetree.cb!\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 170 | if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS) |
| 171 | /* Default to AHCI */ |
| 172 | sata_mode = 0; |
| 173 | |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 174 | /* |
| 175 | * TODO: In contrast to ICH7 and PCH code we don't set |
| 176 | * timings, dma and IDE-I/O settings here. Looks like they |
| 177 | * became obsolete with the fading of real IDE ports. |
| 178 | * Maybe we can safely remove those settings from PCH code and |
| 179 | * even ICH7 code if it doesn't use the feature to combine the |
| 180 | * IDE and SATA controllers. |
| 181 | */ |
| 182 | |
| 183 | pci_write_config16(dev, PCI_COMMAND, |
| 184 | PCI_COMMAND_MASTER | |
| 185 | PCI_COMMAND_MEMORY | /* read-only in IDE modes */ |
| 186 | PCI_COMMAND_IO); |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 187 | if (sata_mode != 0) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 188 | /* No AHCI: clear AHCI base */ |
| 189 | pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000); |
| 190 | |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 191 | if (sata_mode == 0) { |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 192 | printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n"); |
| 193 | } else { |
| 194 | printk(BIOS_DEBUG, "SATA controller in native mode.\n"); |
| 195 | |
| 196 | /* Enable native mode on both primary and secondary. */ |
| 197 | pci_write_config8(dev, PCI_CLASS_PROG, 0x8f); |
| 198 | } |
| 199 | |
| 200 | /* Looks like we should only enable decoding here. */ |
| 201 | pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15)); |
| 202 | pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15)); |
| 203 | |
| 204 | /* Port enable. For AHCI, it's managed in memory mapped space. */ |
| 205 | reg16 = pci_read_config16(dev, 0x92); |
| 206 | reg16 &= ~0x3f; |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 207 | reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 208 | pci_write_config16(dev, 0x92, reg16); |
| 209 | |
| 210 | /* SATA clock settings */ |
| 211 | u32 sclkcg = 0; |
| 212 | if (config->sata_clock_request && |
| 213 | !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32)))) |
| 214 | sclkcg |= 1 << 30; /* Enable SATA clock request. */ |
| 215 | /* Disable unused ports. */ |
| 216 | sclkcg |= ((~config->sata_port_map) & 0x3f) << 24; |
| 217 | /* Must be programmed. */ |
| 218 | sclkcg |= 0x193; |
| 219 | pci_write_config32(dev, 0x94, sclkcg); |
| 220 | |
| 221 | if (is_mobile && config->sata_traffic_monitor) { |
Kyösti Mälkki | c70eed1 | 2018-05-22 02:18:00 +0300 | [diff] [blame] | 222 | struct device *const lpc_dev = pcidev_on_root(0x1f, 0); |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 223 | if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF) |
| 224 | >> 3) & 3) == 3) { |
| 225 | u8 reg8 = pci_read_config8(dev, 0x9c); |
| 226 | reg8 &= ~(0x1f << 2); |
| 227 | reg8 |= 3 << 2; |
| 228 | pci_write_config8(dev, 0x9c, reg8); |
| 229 | } |
| 230 | } |
| 231 | |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 232 | if (sata_mode == 0) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 233 | sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile); |
| 234 | |
| 235 | sata_program_indexed(dev, is_mobile); |
| 236 | } |
| 237 | |
Elyes HAOUAS | 8aa5073 | 2018-05-13 13:34:58 +0200 | [diff] [blame] | 238 | static void sata_enable(struct device *dev) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 239 | { |
| 240 | /* Get the chip configuration */ |
| 241 | const config_t *const config = dev->chip_info; |
| 242 | |
| 243 | u16 map = 0; |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 244 | u8 sata_mode; |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 245 | |
| 246 | if (!config) |
| 247 | return; |
| 248 | |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 249 | if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS) |
| 250 | /* Default to AHCI */ |
| 251 | sata_mode = 0; |
| 252 | |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 253 | /* |
| 254 | * Set SATA controller mode early so the resource allocator can |
| 255 | * properly assign IO/Memory resources for the controller. |
| 256 | */ |
Vladimir Serbinenko | 0dd5e43 | 2014-07-29 22:35:45 +0200 | [diff] [blame] | 257 | if (sata_mode == 0) |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 258 | map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */ |
| 259 | |
| 260 | map |= (config->sata_port_map ^ 0x3f) << 8; |
| 261 | |
| 262 | pci_write_config16(dev, 0x90, map); |
| 263 | } |
| 264 | |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 265 | static struct pci_operations sata_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 266 | .set_subsystem = pci_dev_set_subsystem, |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | static struct device_operations sata_ops = { |
| 270 | .read_resources = pci_dev_read_resources, |
| 271 | .set_resources = pci_dev_set_resources, |
| 272 | .enable_resources = pci_dev_enable_resources, |
| 273 | .init = sata_init, |
| 274 | .enable = sata_enable, |
| 275 | .scan_bus = 0, |
| 276 | .ops_pci = &sata_pci_ops, |
| 277 | }; |
| 278 | |
| 279 | static const unsigned short pci_device_ids[] = { |
Felix Singer | 7f8b0cd8 | 2019-11-10 11:04:08 +0100 | [diff] [blame] | 280 | PCI_DEVICE_ID_INTEL_82801IB_SATA_P0123, |
| 281 | PCI_DEVICE_ID_INTEL_82801IB_SATA_P01, |
| 282 | PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI1, |
| 283 | PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI2, |
| 284 | PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01, |
| 285 | PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145, |
Patrick Georgi | e72a8a3 | 2012-11-06 11:05:09 +0100 | [diff] [blame] | 286 | 0, |
| 287 | }; |
| 288 | |
| 289 | static const struct pci_driver pch_sata __pci_driver = { |
| 290 | .ops = &sata_ops, |
| 291 | .vendor = PCI_VENDOR_ID_INTEL, |
| 292 | .devices = pci_device_ids, |
| 293 | }; |