Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <arch/io.h> |
| 22 | #include <console/console.h> |
| 23 | #include <device/device.h> |
| 24 | #include <device/pci.h> |
| 25 | #include <device/pci_ids.h> |
| 26 | #include "pch.h" |
| 27 | |
| 28 | typedef struct southbridge_intel_bd82x6x_config config_t; |
| 29 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 30 | static inline u32 sir_read(struct device *dev, int idx) |
| 31 | { |
| 32 | pci_write_config32(dev, SATA_SIRI, idx); |
| 33 | return pci_read_config32(dev, SATA_SIRD); |
| 34 | } |
| 35 | |
| 36 | static inline void sir_write(struct device *dev, int idx, u32 value) |
| 37 | { |
| 38 | pci_write_config32(dev, SATA_SIRI, idx); |
| 39 | pci_write_config32(dev, SATA_SIRD, value); |
| 40 | } |
| 41 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 42 | static void sata_init(struct device *dev) |
| 43 | { |
| 44 | u32 reg32; |
| 45 | u16 reg16; |
| 46 | /* Get the chip configuration */ |
| 47 | config_t *config = dev->chip_info; |
| 48 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 49 | printk(BIOS_DEBUG, "SATA: Initializing...\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 50 | |
| 51 | if (config == NULL) { |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 52 | printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | /* SATA configuration */ |
| 57 | |
| 58 | /* Enable BARs */ |
| 59 | pci_write_config16(dev, PCI_COMMAND, 0x0007); |
| 60 | |
| 61 | if (config->ide_legacy_combined) { |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 62 | printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 63 | |
| 64 | /* No AHCI: clear AHCI base */ |
| 65 | pci_write_config32(dev, 0x24, 0x00000000); |
| 66 | /* And without AHCI BAR no memory decoding */ |
| 67 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 68 | reg16 &= ~PCI_COMMAND_MEMORY; |
| 69 | pci_write_config16(dev, PCI_COMMAND, reg16); |
| 70 | |
| 71 | pci_write_config8(dev, 0x09, 0x80); |
| 72 | |
| 73 | /* Set timings */ |
| 74 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 75 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
| 76 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 77 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 78 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 79 | |
| 80 | /* Sync DMA */ |
| 81 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0); |
| 82 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0200); |
| 83 | |
| 84 | /* Set IDE I/O Configuration */ |
| 85 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
| 86 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 87 | |
| 88 | /* Port enable */ |
| 89 | reg16 = pci_read_config16(dev, 0x92); |
| 90 | reg16 &= ~0x3f; |
| 91 | reg16 |= config->sata_port_map; |
| 92 | pci_write_config16(dev, 0x92, reg16); |
| 93 | |
| 94 | /* SATA Initialization register */ |
| 95 | pci_write_config32(dev, 0x94, |
| 96 | ((config->sata_port_map ^ 0x3f) << 24) | 0x183); |
| 97 | } else if(config->sata_ahci) { |
| 98 | u32 abar; |
| 99 | |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 100 | printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 101 | |
| 102 | /* Set Interrupt Line */ |
| 103 | /* Interrupt Pin is set by D31IP.PIP */ |
| 104 | pci_write_config8(dev, INTR_LN, 0x0a); |
| 105 | |
| 106 | /* Set timings */ |
| 107 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 108 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 109 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 110 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 111 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
| 112 | |
| 113 | /* Sync DMA */ |
| 114 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0); |
| 115 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0001); |
| 116 | |
| 117 | /* Set IDE I/O Configuration */ |
| 118 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
| 119 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 120 | |
| 121 | /* for AHCI, Port Enable is managed in memory mapped space */ |
| 122 | reg16 = pci_read_config16(dev, 0x92); |
| 123 | reg16 &= ~0x3f; /* 6 ports SKU + ORM */ |
| 124 | reg16 |= 0x8000 | config->sata_port_map; |
| 125 | pci_write_config16(dev, 0x92, reg16); |
| 126 | |
| 127 | /* SATA Initialization register */ |
| 128 | pci_write_config32(dev, 0x94, |
| 129 | ((config->sata_port_map ^ 0x3f) << 24) | 0x183); |
| 130 | |
| 131 | /* Initialize AHCI memory-mapped space */ |
| 132 | abar = pci_read_config32(dev, PCI_BASE_ADDRESS_5); |
| 133 | printk(BIOS_DEBUG, "ABAR: %08X\n", abar); |
| 134 | /* CAP (HBA Capabilities) : enable power management */ |
| 135 | reg32 = read32(abar + 0x00); |
| 136 | reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS |
| 137 | reg32 &= ~0x00020060; // clear SXS+EMS+PMS |
| 138 | write32(abar + 0x00, reg32); |
| 139 | /* PI (Ports implemented) */ |
| 140 | write32(abar + 0x0c, config->sata_port_map); |
| 141 | (void) read32(abar + 0x0c); /* Read back 1 */ |
| 142 | (void) read32(abar + 0x0c); /* Read back 2 */ |
| 143 | /* CAP2 (HBA Capabilities Extended)*/ |
| 144 | reg32 = read32(abar + 0x24); |
| 145 | reg32 &= ~0x00000002; |
| 146 | write32(abar + 0x24, reg32); |
| 147 | /* VSP (Vendor Specific Register */ |
| 148 | reg32 = read32(abar + 0xa0); |
| 149 | reg32 &= ~0x00000005; |
| 150 | write32(abar + 0xa0, reg32); |
| 151 | } else { |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 152 | printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n"); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 153 | |
| 154 | /* No AHCI: clear AHCI base */ |
| 155 | pci_write_config32(dev, 0x24, 0x00000000); |
| 156 | |
| 157 | /* And without AHCI BAR no memory decoding */ |
| 158 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 159 | reg16 &= ~PCI_COMMAND_MEMORY; |
| 160 | pci_write_config16(dev, PCI_COMMAND, reg16); |
| 161 | |
| 162 | /* Native mode capable on both primary and secondary (0xa) |
| 163 | * or'ed with enabled (0x50) = 0xf |
| 164 | */ |
| 165 | pci_write_config8(dev, 0x09, 0x8f); |
| 166 | |
| 167 | /* Set Interrupt Line */ |
| 168 | /* Interrupt Pin is set by D31IP.PIP */ |
| 169 | pci_write_config8(dev, INTR_LN, 0xff); |
| 170 | |
| 171 | /* Set timings */ |
| 172 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 173 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 174 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 175 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 176 | IDE_SITRE | IDE_ISP_3_CLOCKS | |
| 177 | IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0); |
| 178 | |
| 179 | /* Sync DMA */ |
| 180 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0); |
| 181 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0201); |
| 182 | |
| 183 | /* Set IDE I/O Configuration */ |
| 184 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
| 185 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 186 | |
| 187 | /* Port enable */ |
| 188 | reg16 = pci_read_config16(dev, 0x92); |
| 189 | reg16 &= ~0x3f; |
| 190 | reg16 |= config->sata_port_map; |
| 191 | pci_write_config16(dev, 0x92, reg16); |
| 192 | |
| 193 | /* SATA Initialization register */ |
| 194 | pci_write_config32(dev, 0x94, |
| 195 | ((config->sata_port_map ^ 0x3f) << 24) | 0x183); |
| 196 | } |
Duncan Laurie | cfb64bd | 2012-07-16 16:16:31 -0700 | [diff] [blame] | 197 | |
| 198 | /* Set Gen3 Transmitter settings if needed */ |
| 199 | if (config->sata_port0_gen3_tx) |
| 200 | pch_iobp_update(SATA_IOBP_SP0G3IR, 0, |
| 201 | config->sata_port0_gen3_tx); |
| 202 | |
| 203 | if (config->sata_port1_gen3_tx) |
| 204 | pch_iobp_update(SATA_IOBP_SP1G3IR, 0, |
| 205 | config->sata_port1_gen3_tx); |
Stefan Reinauer | 16b022a | 2012-07-17 16:42:51 -0700 | [diff] [blame^] | 206 | |
| 207 | /* Additional Programming Requirements */ |
| 208 | sir_write(dev, 0x04, 0x00001600); |
| 209 | sir_write(dev, 0x28, 0xa0000033); |
| 210 | reg32 = sir_read(dev, 0x54); |
| 211 | reg32 &= 0xff000000; |
| 212 | reg32 |= 0x5555aa; |
| 213 | sir_write(dev, 0x54, reg32); |
| 214 | sir_write(dev, 0x64, 0xcccc8484); |
| 215 | reg32 = sir_read(dev, 0x68); |
| 216 | reg32 &= 0xffff0000; |
| 217 | reg32 |= 0xcccc; |
| 218 | sir_write(dev, 0x68, reg32); |
| 219 | reg32 = sir_read(dev, 0x78); |
| 220 | reg32 &= 0x0000ffff; |
| 221 | reg32 |= 0x88880000; |
| 222 | sir_write(dev, 0x78, reg32); |
| 223 | sir_write(dev, 0x84, 0x001c7000); |
| 224 | sir_write(dev, 0x88, 0x88338822); |
| 225 | sir_write(dev, 0xa0, 0x001c7000); |
| 226 | // a4 |
| 227 | sir_write(dev, 0xc4, 0x0c0c0c0c); |
| 228 | sir_write(dev, 0xc8, 0x0c0c0c0c); |
| 229 | sir_write(dev, 0xd4, 0x10000000); |
| 230 | |
| 231 | pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000); |
| 232 | pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 235 | static void sata_enable(device_t dev) |
| 236 | { |
| 237 | /* Get the chip configuration */ |
| 238 | config_t *config = dev->chip_info; |
| 239 | u16 map = 0; |
| 240 | |
| 241 | if (!config) |
| 242 | return; |
| 243 | |
| 244 | /* |
| 245 | * Set SATA controller mode early so the resource allocator can |
| 246 | * properly assign IO/Memory resources for the controller. |
| 247 | */ |
| 248 | if (config->sata_ahci) |
| 249 | map = 0x0060; |
| 250 | |
| 251 | map |= (config->sata_port_map ^ 0x3f) << 8; |
| 252 | |
| 253 | pci_write_config16(dev, 0x90, map); |
| 254 | } |
| 255 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 256 | static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
| 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, |
Stefan Reinauer | 816d081 | 2012-04-30 16:42:07 -0700 | [diff] [blame] | 276 | .enable = sata_enable, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 277 | .scan_bus = 0, |
| 278 | .ops_pci = &sata_pci_ops, |
| 279 | }; |
| 280 | |
Stefan Reinauer | 9a380ab | 2012-06-22 13:16:11 -0700 | [diff] [blame] | 281 | static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03, |
| 282 | 0x1e00, 0x1e01, 0x1e02, 0x1e03, |
| 283 | 0 }; |
| 284 | |
| 285 | static const struct pci_driver pch_sata __pci_driver = { |
| 286 | .ops = &sata_ops, |
| 287 | .vendor = PCI_VENDOR_ID_INTEL, |
| 288 | .devices = pci_device_ids, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 289 | }; |
| 290 | |