Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2009 coresystems GmbH |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 5 | * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com> |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 6 | * |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 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. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 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. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <console/console.h> |
| 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 21 | #include <device/pci_ops.h> |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 22 | #include <device/pci_ids.h> |
Arthur Heymans | 742df5a | 2019-06-03 16:24:41 +0200 | [diff] [blame^] | 23 | #include "chip.h" |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 24 | #include "i82801gx.h" |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 25 | #include "sata.h" |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 26 | |
| 27 | typedef struct southbridge_intel_i82801gx_config config_t; |
| 28 | |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 29 | static u8 get_ich7_sata_ports(void) |
| 30 | { |
| 31 | struct device *lpc; |
| 32 | |
Kyösti Mälkki | c70eed1 | 2018-05-22 02:18:00 +0300 | [diff] [blame] | 33 | lpc = pcidev_on_root(31, 0); |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 34 | |
| 35 | switch (pci_read_config16(lpc, PCI_DEVICE_ID)) { |
| 36 | case 0x27b0: |
| 37 | case 0x27b8: |
| 38 | return 0xf; |
| 39 | case 0x27b9: |
| 40 | case 0x27bd: |
| 41 | return 0x5; |
| 42 | case 0x27bc: |
| 43 | return 0x3; |
| 44 | default: |
| 45 | printk(BIOS_ERR, |
| 46 | "i82801gx_sata: error: cannot determine port config\n"); |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void sata_enable(struct device *dev) |
| 52 | { |
| 53 | /* Get the chip configuration */ |
| 54 | config_t *config = dev->chip_info; |
| 55 | |
| 56 | if (config->sata_ahci) { |
| 57 | /* Set map to ahci */ |
| 58 | pci_write_config8(dev, SATA_MAP, |
| 59 | (pci_read_config8(dev, SATA_MAP) & ~0xc3) | 0x40); |
| 60 | } else { |
| 61 | /* Set map to ide */ |
| 62 | pci_write_config8(dev, SATA_MAP, |
| 63 | pci_read_config8(dev, SATA_MAP) & ~0xc3); |
| 64 | } |
| 65 | |
| 66 | /* At this point, the new pci id will appear on the bus */ |
| 67 | } |
| 68 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 69 | static void sata_init(struct device *dev) |
| 70 | { |
| 71 | u32 reg32; |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 72 | u16 reg16; |
Sven Schnelle | b2f173e | 2011-10-27 13:05:40 +0200 | [diff] [blame] | 73 | u32 *ahci_bar; |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 74 | u8 ports; |
Sven Schnelle | b2f173e | 2011-10-27 13:05:40 +0200 | [diff] [blame] | 75 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 76 | /* Get the chip configuration */ |
| 77 | config_t *config = dev->chip_info; |
| 78 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 79 | printk(BIOS_DEBUG, "i82801gx_sata: initializing...\n"); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 80 | |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 81 | if (config == NULL) { |
Uwe Hermann | 607614d | 2010-11-18 20:12:13 +0000 | [diff] [blame] | 82 | printk(BIOS_ERR, "i82801gx_sata: error: device not in devicetree.cb!\n"); |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 83 | return; |
| 84 | } |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 85 | |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 86 | /* Get ICH7 SATA port config */ |
| 87 | ports = get_ich7_sata_ports(); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 88 | |
| 89 | /* Enable BARs */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 90 | pci_write_config16(dev, PCI_COMMAND, 0x0007); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 91 | |
| 92 | if (config->ide_legacy_combined) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 93 | printk(BIOS_DEBUG, "SATA controller in combined mode.\n"); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 94 | /* No AHCI: clear AHCI base */ |
| 95 | pci_write_config32(dev, 0x24, 0x00000000); |
| 96 | /* And without AHCI BAR no memory decoding */ |
| 97 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 98 | reg16 &= ~PCI_COMMAND_MEMORY; |
| 99 | pci_write_config16(dev, PCI_COMMAND, reg16); |
| 100 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 101 | pci_write_config8(dev, 0x09, 0x80); |
| 102 | |
| 103 | /* Set timings */ |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 104 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 105 | IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS); |
| 106 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
| 107 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 108 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 109 | |
| 110 | /* Sync DMA */ |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 111 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0); |
| 112 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0200); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 113 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 114 | /* Set IDE I/O Configuration */ |
Stefan Reinauer | aca6ec6 | 2009-10-26 17:12:21 +0000 | [diff] [blame] | 115 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 116 | pci_write_config32(dev, IDE_CONFIG, reg32); |
| 117 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 118 | /* Combine IDE - SATA configuration */ |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 119 | pci_write_config8(dev, SATA_MAP, 0x02); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 120 | |
Damien Zammit | 1533f13 | 2016-01-16 02:52:53 +1100 | [diff] [blame] | 121 | /* Restrict ports - 0 and 2 only available */ |
| 122 | ports &= 0x5; |
Elyes HAOUAS | 70d79a4 | 2016-08-21 18:36:06 +0200 | [diff] [blame] | 123 | } else if (config->sata_ahci) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 124 | printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 125 | /* Allow both Legacy and Native mode */ |
| 126 | pci_write_config8(dev, 0x09, 0x8f); |
| 127 | |
| 128 | /* Set Interrupt Line */ |
| 129 | /* Interrupt Pin is set by D31IP.PIP */ |
| 130 | pci_write_config8(dev, INTR_LN, 0x0a); |
| 131 | |
Sven Schnelle | b2f173e | 2011-10-27 13:05:40 +0200 | [diff] [blame] | 132 | ahci_bar = (u32 *)(pci_read_config32(dev, 0x27) & ~0x3ff); |
| 133 | ahci_bar[3] = config->sata_ports_implemented; |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 134 | } else { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 135 | printk(BIOS_DEBUG, "SATA controller in plain mode.\n"); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 136 | /* Set Sata Controller Mode. No Mapping(?) */ |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 137 | pci_write_config8(dev, SATA_MAP, 0x00); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 138 | |
| 139 | /* No AHCI: clear AHCI base */ |
| 140 | pci_write_config32(dev, 0x24, 0x00000000); |
| 141 | |
| 142 | /* And without AHCI BAR no memory decoding */ |
| 143 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 144 | reg16 &= ~PCI_COMMAND_MEMORY; |
| 145 | pci_write_config16(dev, PCI_COMMAND, reg16); |
| 146 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 147 | /* Native mode capable on both primary and secondary (0xa) |
| 148 | * or'ed with enabled (0x50) = 0xf |
| 149 | */ |
| 150 | pci_write_config8(dev, 0x09, 0x8f); |
| 151 | |
| 152 | /* Set Interrupt Line */ |
| 153 | /* Interrupt Pin is set by D31IP.PIP */ |
| 154 | pci_write_config8(dev, INTR_LN, 0xff); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 155 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 156 | /* Set timings */ |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 157 | pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE | |
| 158 | IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS | |
| 159 | IDE_PPE0 | IDE_IE0 | IDE_TIME0); |
| 160 | pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE | |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 161 | IDE_SITRE | IDE_ISP_3_CLOCKS | |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 162 | IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 163 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 164 | /* Sync DMA */ |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 165 | pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0); |
| 166 | pci_write_config16(dev, IDE_SDMA_TIM, 0x0201); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 167 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 168 | /* Set IDE I/O Configuration */ |
Stefan Reinauer | aca6ec6 | 2009-10-26 17:12:21 +0000 | [diff] [blame] | 169 | reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0; |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 170 | pci_write_config32(dev, IDE_CONFIG, reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Damien Zammit | 1533f13 | 2016-01-16 02:52:53 +1100 | [diff] [blame] | 173 | /* Set port control */ |
| 174 | pci_write_config8(dev, SATA_PCS, ports); |
| 175 | |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 176 | /* Enable clock gating for unused ports and set initialization reg */ |
| 177 | pci_write_config32(dev, SATA_IR, SIF3(ports) | SIF2 | SIF1 | SCRE); |
| 178 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 179 | /* All configurations need this SATA initialization sequence */ |
| 180 | pci_write_config8(dev, 0xa0, 0x40); |
| 181 | pci_write_config8(dev, 0xa6, 0x22); |
| 182 | pci_write_config8(dev, 0xa0, 0x78); |
| 183 | pci_write_config8(dev, 0xa6, 0x22); |
| 184 | pci_write_config8(dev, 0xa0, 0x88); |
| 185 | reg32 = pci_read_config32(dev, 0xa4); |
| 186 | reg32 &= 0xc0c0c0c0; |
| 187 | reg32 |= 0x1b1b1212; |
| 188 | pci_write_config32(dev, 0xa4, reg32); |
| 189 | pci_write_config8(dev, 0xa0, 0x8c); |
| 190 | reg32 = pci_read_config32(dev, 0xa4); |
| 191 | reg32 &= 0xc0c0ff00; |
| 192 | reg32 |= 0x121200aa; |
| 193 | pci_write_config32(dev, 0xa4, reg32); |
| 194 | pci_write_config8(dev, 0xa0, 0x00); |
Stefan Reinauer | 54309d6 | 2009-01-20 22:53:10 +0000 | [diff] [blame] | 195 | |
| 196 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 197 | |
| 198 | /* Sata Initialization Register */ |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 199 | reg32 = pci_read_config32(dev, SATA_IR); |
| 200 | reg32 |= SCRD; // due to some bug |
| 201 | pci_write_config32(dev, SATA_IR, reg32); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 204 | static struct pci_operations sata_pci_ops = { |
Subrata Banik | 4a0f071 | 2019-03-20 14:29:47 +0530 | [diff] [blame] | 205 | .set_subsystem = pci_dev_set_subsystem, |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 208 | static struct device_operations sata_ops = { |
| 209 | .read_resources = pci_dev_read_resources, |
| 210 | .set_resources = pci_dev_set_resources, |
| 211 | .enable_resources = pci_dev_enable_resources, |
| 212 | .init = sata_init, |
| 213 | .scan_bus = 0, |
| 214 | .enable = i82801gx_enable, |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 215 | .ops_pci = &sata_pci_ops, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 218 | static const unsigned short sata_ids[] = { |
| 219 | 0x27c0, /* Desktop Non-AHCI and Non-RAID Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */ |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 220 | 0x27c1, /* Desktop AHCI Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */ |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 221 | 0x27c4, /* Mobile Non-AHCI and Non-RAID Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */ |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 222 | 0x27c5, /* Mobile AHCI Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */ |
Damien Zammit | 647e385 | 2016-01-15 13:44:53 +1100 | [diff] [blame] | 223 | /* NOTE: Any of the below are not properly supported yet. */ |
| 224 | 0x27c3, /* Desktop RAID mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */ |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 225 | 0x27c6, /* ICH7M DH Raid Mode: 82801GHM (ICH7-M DH) */ |
| 226 | 0 |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Patrick Georgi | efff733 | 2012-07-26 19:48:23 +0200 | [diff] [blame] | 229 | static const struct pci_driver i82801gx_sata_driver __pci_driver = { |
Arthur Heymans | 3f111b0 | 2017-03-09 12:02:52 +0100 | [diff] [blame] | 230 | .ops = &sata_ops, |
| 231 | .vendor = PCI_VENDOR_ID_INTEL, |
| 232 | .devices = sata_ids, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 233 | }; |