blob: 24dbf7cf27e0aea95358c9962cd9928ab0792ebd [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer54309d62009-01-20 22:53:10 +00004 * Copyright (C) 2008-2009 coresystems GmbH
Damien Zammit647e3852016-01-15 13:44:53 +11005 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00006 *
Stefan Reinauera8e11682009-03-11 14:54:18 +00007 * 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 Reinauerdebb11f2008-10-29 04:46:52 +000011 *
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 Reinauerdebb11f2008-10-29 04:46:52 +000016 */
17
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000022#include <device/pci_ids.h>
Arthur Heymans742df5a2019-06-03 16:24:41 +020023#include "chip.h"
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000024#include "i82801gx.h"
Damien Zammit647e3852016-01-15 13:44:53 +110025#include "sata.h"
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000026
27typedef struct southbridge_intel_i82801gx_config config_t;
28
Damien Zammit647e3852016-01-15 13:44:53 +110029static u8 get_ich7_sata_ports(void)
30{
31 struct device *lpc;
32
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030033 lpc = pcidev_on_root(31, 0);
Damien Zammit647e3852016-01-15 13:44:53 +110034
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
51void sata_enable(struct device *dev)
52{
53 /* Get the chip configuration */
Arthur Heymans5eb81be2019-01-10 23:13:11 +010054 struct southbridge_intel_i82801gx_config *config = dev->chip_info;
Damien Zammit647e3852016-01-15 13:44:53 +110055
Arthur Heymans5eb81be2019-01-10 23:13:11 +010056 if (config->sata_mode == SATA_MODE_AHCI) {
57 /* Check if the southbridge supports AHCI */
58 struct device *lpc_dev = pcidev_on_root(31, 0);
59 if (!lpc_dev) {
60 /* According to the PCI spec function 0 on a bus:device
61 needs to be active for other functions to be enabled.
62 Since SATA is on the same bus:device as the LPC
63 bridge, it makes little sense to continue. */
64 die("Couldn't find the LPC device!\n");
65 }
66
67 const bool ahci_supported = !(pci_read_config32(lpc_dev, FDVCT)
68 & AHCI_UNSUPPORTED);
69
70 if (!ahci_supported) {
71 /* Fallback to IDE PLAIN for sata for the rest of the
72 initialization */
73 config->sata_mode = SATA_MODE_IDE_PLAIN;
74 printk(BIOS_DEBUG,
75 "AHCI not supported, falling back to plain mode.\n");
76 }
77
Damien Zammit647e3852016-01-15 13:44:53 +110078 }
79
Arthur Heymans5eb81be2019-01-10 23:13:11 +010080 if (config->sata_mode == SATA_MODE_AHCI) {
81 /* Set map to ahci */
82 pci_write_config8(dev, SATA_MAP,
83 (pci_read_config8(dev, SATA_MAP)
84 & ~0xc3) | 0x40);
85 } else {
86 /* Set map to ide */
87 pci_write_config8(dev, SATA_MAP,
88 pci_read_config8(dev, SATA_MAP) & ~0xc3);
89 }
Damien Zammit647e3852016-01-15 13:44:53 +110090 /* At this point, the new pci id will appear on the bus */
91}
92
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000093static void sata_init(struct device *dev)
94{
95 u32 reg32;
Stefan Reinauera8e11682009-03-11 14:54:18 +000096 u16 reg16;
Sven Schnelleb2f173e2011-10-27 13:05:40 +020097 u32 *ahci_bar;
Damien Zammit647e3852016-01-15 13:44:53 +110098 u8 ports;
Sven Schnelleb2f173e2011-10-27 13:05:40 +020099
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000100 /* Get the chip configuration */
101 config_t *config = dev->chip_info;
102
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_DEBUG, "i82801gx_sata: initializing...\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000104
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000105 if (config == NULL) {
Uwe Hermann607614d2010-11-18 20:12:13 +0000106 printk(BIOS_ERR, "i82801gx_sata: error: device not in devicetree.cb!\n");
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000107 return;
108 }
Stefan Reinauera8e11682009-03-11 14:54:18 +0000109
Damien Zammit647e3852016-01-15 13:44:53 +1100110 /* Get ICH7 SATA port config */
111 ports = get_ich7_sata_ports();
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000112
113 /* Enable BARs */
Stefan Reinauera8e11682009-03-11 14:54:18 +0000114 pci_write_config16(dev, PCI_COMMAND, 0x0007);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000115
Arthur Heymans5eb81be2019-01-10 23:13:11 +0100116 switch (config->sata_mode) {
117 case SATA_MODE_IDE_LEGACY_COMBINED:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000118 printk(BIOS_DEBUG, "SATA controller in combined mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000119 /* No AHCI: clear AHCI base */
120 pci_write_config32(dev, 0x24, 0x00000000);
121 /* And without AHCI BAR no memory decoding */
122 reg16 = pci_read_config16(dev, PCI_COMMAND);
123 reg16 &= ~PCI_COMMAND_MEMORY;
124 pci_write_config16(dev, PCI_COMMAND, reg16);
125
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000126 pci_write_config8(dev, 0x09, 0x80);
127
128 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000129 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
130 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
131 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
132 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
133 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000134
135 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000136 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
137 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000138
Stefan Reinauera8e11682009-03-11 14:54:18 +0000139 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000140 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauera8e11682009-03-11 14:54:18 +0000141 pci_write_config32(dev, IDE_CONFIG, reg32);
142
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000143 /* Combine IDE - SATA configuration */
Damien Zammit647e3852016-01-15 13:44:53 +1100144 pci_write_config8(dev, SATA_MAP, 0x02);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000145
Damien Zammit1533f132016-01-16 02:52:53 +1100146 /* Restrict ports - 0 and 2 only available */
147 ports &= 0x5;
Arthur Heymans5eb81be2019-01-10 23:13:11 +0100148 break;
149 case SATA_MODE_AHCI:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000150 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000151 /* Allow both Legacy and Native mode */
152 pci_write_config8(dev, 0x09, 0x8f);
153
154 /* Set Interrupt Line */
155 /* Interrupt Pin is set by D31IP.PIP */
156 pci_write_config8(dev, INTR_LN, 0x0a);
157
Sven Schnelleb2f173e2011-10-27 13:05:40 +0200158 ahci_bar = (u32 *)(pci_read_config32(dev, 0x27) & ~0x3ff);
159 ahci_bar[3] = config->sata_ports_implemented;
Arthur Heymans5eb81be2019-01-10 23:13:11 +0100160 break;
161 default:
162 case SATA_MODE_IDE_PLAIN:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000163 printk(BIOS_DEBUG, "SATA controller in plain mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000164 /* Set Sata Controller Mode. No Mapping(?) */
Damien Zammit647e3852016-01-15 13:44:53 +1100165 pci_write_config8(dev, SATA_MAP, 0x00);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000166
167 /* No AHCI: clear AHCI base */
168 pci_write_config32(dev, 0x24, 0x00000000);
169
170 /* And without AHCI BAR no memory decoding */
171 reg16 = pci_read_config16(dev, PCI_COMMAND);
172 reg16 &= ~PCI_COMMAND_MEMORY;
173 pci_write_config16(dev, PCI_COMMAND, reg16);
174
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000175 /* Native mode capable on both primary and secondary (0xa)
176 * or'ed with enabled (0x50) = 0xf
177 */
178 pci_write_config8(dev, 0x09, 0x8f);
179
180 /* Set Interrupt Line */
181 /* Interrupt Pin is set by D31IP.PIP */
182 pci_write_config8(dev, INTR_LN, 0xff);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000183
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000184 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000185 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
186 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
187 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
188 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Stefan Reinauer109ab312009-08-12 16:08:05 +0000189 IDE_SITRE | IDE_ISP_3_CLOCKS |
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000190 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000191
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000192 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000193 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
194 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000195
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000196 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000197 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000198 pci_write_config32(dev, IDE_CONFIG, reg32);
Arthur Heymans5eb81be2019-01-10 23:13:11 +0100199 break;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000200 }
201
Damien Zammit1533f132016-01-16 02:52:53 +1100202 /* Set port control */
203 pci_write_config8(dev, SATA_PCS, ports);
204
Damien Zammit647e3852016-01-15 13:44:53 +1100205 /* Enable clock gating for unused ports and set initialization reg */
206 pci_write_config32(dev, SATA_IR, SIF3(ports) | SIF2 | SIF1 | SCRE);
207
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000208 /* All configurations need this SATA initialization sequence */
209 pci_write_config8(dev, 0xa0, 0x40);
210 pci_write_config8(dev, 0xa6, 0x22);
211 pci_write_config8(dev, 0xa0, 0x78);
212 pci_write_config8(dev, 0xa6, 0x22);
213 pci_write_config8(dev, 0xa0, 0x88);
214 reg32 = pci_read_config32(dev, 0xa4);
215 reg32 &= 0xc0c0c0c0;
216 reg32 |= 0x1b1b1212;
217 pci_write_config32(dev, 0xa4, reg32);
218 pci_write_config8(dev, 0xa0, 0x8c);
219 reg32 = pci_read_config32(dev, 0xa4);
220 reg32 &= 0xc0c0ff00;
221 reg32 |= 0x121200aa;
222 pci_write_config32(dev, 0xa4, reg32);
223 pci_write_config8(dev, 0xa0, 0x00);
Stefan Reinauer54309d62009-01-20 22:53:10 +0000224
225 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000226
227 /* Sata Initialization Register */
Damien Zammit647e3852016-01-15 13:44:53 +1100228 reg32 = pci_read_config32(dev, SATA_IR);
229 reg32 |= SCRD; // due to some bug
230 pci_write_config32(dev, SATA_IR, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000231}
232
Stefan Reinauera8e11682009-03-11 14:54:18 +0000233static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530234 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000235};
236
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000237static struct device_operations sata_ops = {
238 .read_resources = pci_dev_read_resources,
239 .set_resources = pci_dev_set_resources,
240 .enable_resources = pci_dev_enable_resources,
241 .init = sata_init,
242 .scan_bus = 0,
243 .enable = i82801gx_enable,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000244 .ops_pci = &sata_pci_ops,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000245};
246
Patrick Georgiefff7332012-07-26 19:48:23 +0200247static const unsigned short sata_ids[] = {
248 0x27c0, /* Desktop Non-AHCI and Non-RAID Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200249 0x27c1, /* Desktop AHCI Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Damien Zammit647e3852016-01-15 13:44:53 +1100250 0x27c4, /* Mobile Non-AHCI and Non-RAID Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200251 0x27c5, /* Mobile AHCI Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
Damien Zammit647e3852016-01-15 13:44:53 +1100252 /* NOTE: Any of the below are not properly supported yet. */
253 0x27c3, /* Desktop RAID mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
Patrick Georgiefff7332012-07-26 19:48:23 +0200254 0x27c6, /* ICH7M DH Raid Mode: 82801GHM (ICH7-M DH) */
255 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000256};
257
Patrick Georgiefff7332012-07-26 19:48:23 +0200258static const struct pci_driver i82801gx_sata_driver __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100259 .ops = &sata_ops,
260 .vendor = PCI_VENDOR_ID_INTEL,
261 .devices = sata_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000262};