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