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