blob: 594b09d057f3d982a0196e2f074f1ee2d159ba88 [file] [log] [blame]
Stefan Reinauer8e073822012-04-04 00:07:22 +02001/*
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
Paul Menzela46a7122013-02-23 18:37:27 +010018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer8e073822012-04-04 00:07:22 +020019 */
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
28typedef struct southbridge_intel_bd82x6x_config config_t;
29
Stefan Reinauer16b022a2012-07-17 16:42:51 -070030static 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
36static 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 Reinauer8e073822012-04-04 00:07:22 +020042static 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 Reinauer16b022a2012-07-17 16:42:51 -070049 printk(BIOS_DEBUG, "SATA: Initializing...\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020050
51 if (config == NULL) {
Stefan Reinauer16b022a2012-07-17 16:42:51 -070052 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020053 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 Reinauer16b022a2012-07-17 16:42:51 -070062 printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020063
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 Reinauer16b022a2012-07-17 16:42:51 -0700100 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200101
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
Shawn Nematbakhshc9fc0292013-03-14 10:44:13 -0700138 /* Set ISS, if available */
139 if (config->sata_interface_speed_support)
140 {
141 reg32 &= ~0x00f00000;
142 reg32 |= (config->sata_interface_speed_support & 0x03)
143 << 20;
144 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200145 write32(abar + 0x00, reg32);
146 /* PI (Ports implemented) */
147 write32(abar + 0x0c, config->sata_port_map);
148 (void) read32(abar + 0x0c); /* Read back 1 */
149 (void) read32(abar + 0x0c); /* Read back 2 */
150 /* CAP2 (HBA Capabilities Extended)*/
151 reg32 = read32(abar + 0x24);
152 reg32 &= ~0x00000002;
153 write32(abar + 0x24, reg32);
154 /* VSP (Vendor Specific Register */
155 reg32 = read32(abar + 0xa0);
156 reg32 &= ~0x00000005;
157 write32(abar + 0xa0, reg32);
158 } else {
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700159 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160
161 /* No AHCI: clear AHCI base */
162 pci_write_config32(dev, 0x24, 0x00000000);
163
164 /* And without AHCI BAR no memory decoding */
165 reg16 = pci_read_config16(dev, PCI_COMMAND);
166 reg16 &= ~PCI_COMMAND_MEMORY;
167 pci_write_config16(dev, PCI_COMMAND, reg16);
168
169 /* Native mode capable on both primary and secondary (0xa)
170 * or'ed with enabled (0x50) = 0xf
171 */
172 pci_write_config8(dev, 0x09, 0x8f);
173
174 /* Set Interrupt Line */
175 /* Interrupt Pin is set by D31IP.PIP */
176 pci_write_config8(dev, INTR_LN, 0xff);
177
178 /* Set timings */
179 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
180 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
181 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
182 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
183 IDE_SITRE | IDE_ISP_3_CLOCKS |
184 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
185
186 /* Sync DMA */
187 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
188 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
189
190 /* Set IDE I/O Configuration */
191 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
192 pci_write_config32(dev, IDE_CONFIG, reg32);
193
194 /* Port enable */
195 reg16 = pci_read_config16(dev, 0x92);
196 reg16 &= ~0x3f;
197 reg16 |= config->sata_port_map;
198 pci_write_config16(dev, 0x92, reg16);
199
200 /* SATA Initialization register */
201 pci_write_config32(dev, 0x94,
202 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
203 }
Duncan Lauriecfb64bd2012-07-16 16:16:31 -0700204
205 /* Set Gen3 Transmitter settings if needed */
206 if (config->sata_port0_gen3_tx)
207 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
208 config->sata_port0_gen3_tx);
209
210 if (config->sata_port1_gen3_tx)
211 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
212 config->sata_port1_gen3_tx);
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700213
214 /* Additional Programming Requirements */
215 sir_write(dev, 0x04, 0x00001600);
216 sir_write(dev, 0x28, 0xa0000033);
217 reg32 = sir_read(dev, 0x54);
218 reg32 &= 0xff000000;
219 reg32 |= 0x5555aa;
220 sir_write(dev, 0x54, reg32);
221 sir_write(dev, 0x64, 0xcccc8484);
222 reg32 = sir_read(dev, 0x68);
223 reg32 &= 0xffff0000;
224 reg32 |= 0xcccc;
225 sir_write(dev, 0x68, reg32);
226 reg32 = sir_read(dev, 0x78);
227 reg32 &= 0x0000ffff;
228 reg32 |= 0x88880000;
229 sir_write(dev, 0x78, reg32);
230 sir_write(dev, 0x84, 0x001c7000);
231 sir_write(dev, 0x88, 0x88338822);
232 sir_write(dev, 0xa0, 0x001c7000);
233 // a4
234 sir_write(dev, 0xc4, 0x0c0c0c0c);
235 sir_write(dev, 0xc8, 0x0c0c0c0c);
236 sir_write(dev, 0xd4, 0x10000000);
237
238 pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000);
239 pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200240}
241
Stefan Reinauer816d0812012-04-30 16:42:07 -0700242static void sata_enable(device_t dev)
243{
244 /* Get the chip configuration */
245 config_t *config = dev->chip_info;
246 u16 map = 0;
247
248 if (!config)
249 return;
250
251 /*
252 * Set SATA controller mode early so the resource allocator can
253 * properly assign IO/Memory resources for the controller.
254 */
255 if (config->sata_ahci)
256 map = 0x0060;
257
258 map |= (config->sata_port_map ^ 0x3f) << 8;
259
260 pci_write_config16(dev, 0x90, map);
261}
262
Stefan Reinauer8e073822012-04-04 00:07:22 +0200263static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device)
264{
265 if (!vendor || !device) {
266 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
267 pci_read_config32(dev, PCI_VENDOR_ID));
268 } else {
269 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
270 ((device & 0xffff) << 16) | (vendor & 0xffff));
271 }
272}
273
274static struct pci_operations sata_pci_ops = {
275 .set_subsystem = sata_set_subsystem,
276};
277
278static struct device_operations sata_ops = {
279 .read_resources = pci_dev_read_resources,
280 .set_resources = pci_dev_set_resources,
281 .enable_resources = pci_dev_enable_resources,
282 .init = sata_init,
Stefan Reinauer816d0812012-04-30 16:42:07 -0700283 .enable = sata_enable,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200284 .scan_bus = 0,
285 .ops_pci = &sata_pci_ops,
286};
287
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700288static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03,
289 0x1e00, 0x1e01, 0x1e02, 0x1e03,
290 0 };
291
292static const struct pci_driver pch_sata __pci_driver = {
293 .ops = &sata_ops,
294 .vendor = PCI_VENDOR_ID_INTEL,
295 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200296};
297