blob: a8d6f303627534d7e7ef322c002c77df8f269724 [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
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00005 *
Stefan Reinauera8e11682009-03-11 14:54:18 +00006 * 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.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000010 *
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 <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include "i82801gx.h"
26
27typedef struct southbridge_intel_i82801gx_config config_t;
28
29static void sata_init(struct device *dev)
30{
31 u32 reg32;
Stefan Reinauera8e11682009-03-11 14:54:18 +000032 u16 reg16;
Sven Schnelleb2f173e2011-10-27 13:05:40 +020033 u32 *ahci_bar;
34
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000035 /* Get the chip configuration */
36 config_t *config = dev->chip_info;
37
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000038 printk(BIOS_DEBUG, "i82801gx_sata: initializing...\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +000039
Stefan Reinauer573f7d42009-07-21 21:50:34 +000040 if (config == NULL) {
Uwe Hermann607614d2010-11-18 20:12:13 +000041 printk(BIOS_ERR, "i82801gx_sata: error: device not in devicetree.cb!\n");
Stefan Reinauer573f7d42009-07-21 21:50:34 +000042 return;
43 }
Stefan Reinauera8e11682009-03-11 14:54:18 +000044
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000045 /* SATA configuration */
46
47 /* Enable BARs */
Stefan Reinauera8e11682009-03-11 14:54:18 +000048 pci_write_config16(dev, PCI_COMMAND, 0x0007);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000049
50 if (config->ide_legacy_combined) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000051 printk(BIOS_DEBUG, "SATA controller in combined mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +000052 /* No AHCI: clear AHCI base */
53 pci_write_config32(dev, 0x24, 0x00000000);
54 /* And without AHCI BAR no memory decoding */
55 reg16 = pci_read_config16(dev, PCI_COMMAND);
56 reg16 &= ~PCI_COMMAND_MEMORY;
57 pci_write_config16(dev, PCI_COMMAND, reg16);
58
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000059 pci_write_config8(dev, 0x09, 0x80);
60
61 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +000062 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
63 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
64 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
65 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
66 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000067
68 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +000069 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
70 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000071
Stefan Reinauera8e11682009-03-11 14:54:18 +000072 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000073 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauera8e11682009-03-11 14:54:18 +000074 pci_write_config32(dev, IDE_CONFIG, reg32);
75
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000076 /* Combine IDE - SATA configuration */
77 pci_write_config8(dev, 0x90, 0x02);
78
79 /* Port 0 & 1 enable */
80 pci_write_config8(dev, 0x92, 0x0f);
81
82 /* SATA Initialization register */
Stefan Reinauera8e11682009-03-11 14:54:18 +000083 pci_write_config32(dev, 0x94, 0x5a000180);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000084 } else if(config->sata_ahci) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000085 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000086 /* Allow both Legacy and Native mode */
87 pci_write_config8(dev, 0x09, 0x8f);
88
89 /* Set Interrupt Line */
90 /* Interrupt Pin is set by D31IP.PIP */
91 pci_write_config8(dev, INTR_LN, 0x0a);
92
93 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +000094 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
95 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
96 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
97 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
98 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000099
100 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000101 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
102 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000103
104 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000105 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000106 pci_write_config32(dev, IDE_CONFIG, reg32);
107
108 /* Set Sata Controller Mode. */
Bernhard M. Wiedermann67693962010-04-22 22:47:29 +0000109 pci_write_config8(dev, 0x90, 0x40); // 40=AHCI
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000110
Sven Schnelleb2f173e2011-10-27 13:05:40 +0200111 /* In ACHI mode, bit[3:0] must always be set
112 * (Port status is controlled through AHCI BAR)
113 */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000114 pci_write_config8(dev, 0x92, 0x0f);
115
Sven Schnelleb2f173e2011-10-27 13:05:40 +0200116 ahci_bar = (u32 *)(pci_read_config32(dev, 0x27) & ~0x3ff);
117 ahci_bar[3] = config->sata_ports_implemented;
118
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000119 /* SATA Initialization register */
120 pci_write_config32(dev, 0x94, 0x1a000180);
121 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000122 printk(BIOS_DEBUG, "SATA controller in plain mode.\n");
Stefan Reinauera8e11682009-03-11 14:54:18 +0000123 /* Set Sata Controller Mode. No Mapping(?) */
124 pci_write_config8(dev, 0x90, 0x00);
125
126 /* No AHCI: clear AHCI base */
127 pci_write_config32(dev, 0x24, 0x00000000);
128
129 /* And without AHCI BAR no memory decoding */
130 reg16 = pci_read_config16(dev, PCI_COMMAND);
131 reg16 &= ~PCI_COMMAND_MEMORY;
132 pci_write_config16(dev, PCI_COMMAND, reg16);
133
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000134 /* Native mode capable on both primary and secondary (0xa)
135 * or'ed with enabled (0x50) = 0xf
136 */
137 pci_write_config8(dev, 0x09, 0x8f);
138
139 /* Set Interrupt Line */
140 /* Interrupt Pin is set by D31IP.PIP */
141 pci_write_config8(dev, INTR_LN, 0xff);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000142
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000143 /* Set timings */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000144 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
145 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
146 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
147 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
Stefan Reinauer109ab312009-08-12 16:08:05 +0000148 IDE_SITRE | IDE_ISP_3_CLOCKS |
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000149 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000150
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000151 /* Sync DMA */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000152 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
153 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000154
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000155 /* Set IDE I/O Configuration */
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000156 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000157 pci_write_config32(dev, IDE_CONFIG, reg32);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000158
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000159 /* Port 0 & 1 enable XXX */
160 pci_write_config8(dev, 0x92, 0x15);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000161
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000162 /* SATA Initialization register */
163 pci_write_config32(dev, 0x94, 0x1a000180);
164 }
165
166 /* All configurations need this SATA initialization sequence */
167 pci_write_config8(dev, 0xa0, 0x40);
168 pci_write_config8(dev, 0xa6, 0x22);
169 pci_write_config8(dev, 0xa0, 0x78);
170 pci_write_config8(dev, 0xa6, 0x22);
171 pci_write_config8(dev, 0xa0, 0x88);
172 reg32 = pci_read_config32(dev, 0xa4);
173 reg32 &= 0xc0c0c0c0;
174 reg32 |= 0x1b1b1212;
175 pci_write_config32(dev, 0xa4, reg32);
176 pci_write_config8(dev, 0xa0, 0x8c);
177 reg32 = pci_read_config32(dev, 0xa4);
178 reg32 &= 0xc0c0ff00;
179 reg32 |= 0x121200aa;
180 pci_write_config32(dev, 0xa4, reg32);
181 pci_write_config8(dev, 0xa0, 0x00);
Stefan Reinauer54309d62009-01-20 22:53:10 +0000182
183 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000184
185 /* Sata Initialization Register */
186 reg32 = pci_read_config32(dev, 0x94);
187 reg32 |= (1 << 30); // due to some bug
188 pci_write_config32(dev, 0x94, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000189}
190
Stefan Reinauera8e11682009-03-11 14:54:18 +0000191static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device)
192{
193 if (!vendor || !device) {
194 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
195 pci_read_config32(dev, PCI_VENDOR_ID));
196 } else {
197 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
198 ((device & 0xffff) << 16) | (vendor & 0xffff));
199 }
200}
201
202static struct pci_operations sata_pci_ops = {
203 .set_subsystem = sata_set_subsystem,
204};
205
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000206static struct device_operations sata_ops = {
207 .read_resources = pci_dev_read_resources,
208 .set_resources = pci_dev_set_resources,
209 .enable_resources = pci_dev_enable_resources,
210 .init = sata_init,
211 .scan_bus = 0,
212 .enable = i82801gx_enable,
Stefan Reinauera8e11682009-03-11 14:54:18 +0000213 .ops_pci = &sata_pci_ops,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000214};
215
Patrick Georgiefff7332012-07-26 19:48:23 +0200216static const unsigned short sata_ids[] = {
217 0x27c0, /* Desktop Non-AHCI and Non-RAID Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
218 0x27c4, /* Mobile Non-AHCI and Non-RAID Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
219 /* NOTE: Any of the below are not properly supported yet. */
220 0x27c1, /* Desktop AHCI Mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
221 0x27c3, /* Desktop RAID mode: 82801GB/GR/GDH (ICH7/ICH7R/ICH7DH) */
222 0x27c5, /* Mobile AHCI Mode: 82801GBM/GHM (ICH7-M/ICH7-M DH) */
223 0x27c6, /* ICH7M DH Raid Mode: 82801GHM (ICH7-M DH) */
224 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000225};
226
Patrick Georgiefff7332012-07-26 19:48:23 +0200227static const struct pci_driver i82801gx_sata_driver __pci_driver = {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000228 .ops = &sata_ops,
229 .vendor = PCI_VENDOR_ID_INTEL,
Patrick Georgiefff7332012-07-26 19:48:23 +0200230 .devices= sata_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000231};