blob: 25eb94fed65c244b7fefd78fc586701a4a0e287e [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
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>
Duncan Laurie26e7dd72012-12-19 09:12:31 -080026#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050027#include "pch.h"
28
29typedef struct southbridge_intel_lynxpoint_config config_t;
30
31static inline u32 sir_read(struct device *dev, int idx)
32{
33 pci_write_config32(dev, SATA_SIRI, idx);
34 return pci_read_config32(dev, SATA_SIRD);
35}
36
37static inline void sir_write(struct device *dev, int idx, u32 value)
38{
39 pci_write_config32(dev, SATA_SIRI, idx);
40 pci_write_config32(dev, SATA_SIRD, value);
41}
42
43static void sata_init(struct device *dev)
44{
45 u32 reg32;
46 u16 reg16;
47 /* Get the chip configuration */
48 config_t *config = dev->chip_info;
49
50 printk(BIOS_DEBUG, "SATA: Initializing...\n");
51
52 if (config == NULL) {
53 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
54 return;
55 }
56
57 /* SATA configuration */
58
59 /* Enable BARs */
60 pci_write_config16(dev, PCI_COMMAND, 0x0007);
61
62 if (config->ide_legacy_combined) {
63 printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n");
64
65 /* No AHCI: clear AHCI base */
66 pci_write_config32(dev, 0x24, 0x00000000);
67 /* And without AHCI BAR no memory decoding */
68 reg16 = pci_read_config16(dev, PCI_COMMAND);
69 reg16 &= ~PCI_COMMAND_MEMORY;
70 pci_write_config16(dev, PCI_COMMAND, reg16);
71
72 pci_write_config8(dev, 0x09, 0x80);
73
74 /* Set timings */
75 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
76 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
77 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
78 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
79 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
80
81 /* Sync DMA */
82 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
83 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
84
85 /* Set IDE I/O Configuration */
86 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
87 pci_write_config32(dev, IDE_CONFIG, reg32);
88
89 /* Port enable */
90 reg16 = pci_read_config16(dev, 0x92);
91 reg16 &= ~0x3f;
92 reg16 |= config->sata_port_map;
93 pci_write_config16(dev, 0x92, reg16);
94
95 /* SATA Initialization register */
96 pci_write_config32(dev, 0x94,
97 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
98 } else if(config->sata_ahci) {
99 u32 abar;
100
101 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
102
103 /* Set Interrupt Line */
104 /* Interrupt Pin is set by D31IP.PIP */
105 pci_write_config8(dev, INTR_LN, 0x0a);
106
107 /* Set timings */
108 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
109 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
110 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
111 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
112 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
113
114 /* Sync DMA */
115 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
116 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
117
118 /* Set IDE I/O Configuration */
119 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
120 pci_write_config32(dev, IDE_CONFIG, reg32);
121
122 /* for AHCI, Port Enable is managed in memory mapped space */
123 reg16 = pci_read_config16(dev, 0x92);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800124 reg16 &= ~0x3f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500125 reg16 |= 0x8000 | config->sata_port_map;
126 pci_write_config16(dev, 0x92, reg16);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800127 udelay(2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500128
Duncan Laurie74c0d052012-12-17 11:31:40 -0800129 /* Setup register 98h */
130 reg32 = pci_read_config16(dev, 0x98);
131 reg32 |= 1 << 19; /* BWG step 6 */
132 reg32 |= 1 << 22; /* BWG step 5 */
133 reg32 &= ~(0x3f << 7);
134 reg32 |= 0x04 << 7; /* BWG step 7 */
135 reg32 |= 1 << 20; /* BWG step 8 */
136 reg32 &= ~(0x03 << 5);
137 reg32 |= 1 << 5; /* BWG step 9 */
138 reg32 |= 1 << 18; /* BWG step 10 */
139 reg32 |= 1 << 29; /* BWG step 11 */
140#if CONFIG_INTEL_LYNXPOINT_LP
141 reg32 &= ~((1 << 31) | (1 << 30));
142 reg32 |= 1 << 23;
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800143 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800144#endif
145 pci_write_config32(dev, 0x98, reg32);
146
147 /* Setup register 9Ch */
148 reg16 = 0; /* Disable alternate ID */
149 reg16 = 1 << 5; /* BWG step 12 */
150 pci_write_config16(dev, 0x9c, reg16);
151
Aaron Durbin76c37002012-10-30 09:03:43 -0500152 /* SATA Initialization register */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800153 reg32 = 0x183;
154 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
155 reg32 |= (config->sata_devslp_mux & 1) << 15;
156 pci_write_config32(dev, 0x94, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500157
158 /* Initialize AHCI memory-mapped space */
159 abar = pci_read_config32(dev, PCI_BASE_ADDRESS_5);
160 printk(BIOS_DEBUG, "ABAR: %08X\n", abar);
161 /* CAP (HBA Capabilities) : enable power management */
162 reg32 = read32(abar + 0x00);
163 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
164 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800165#if CONFIG_INTEL_LYNXPOINT_LP
166 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
167#endif
Aaron Durbin76c37002012-10-30 09:03:43 -0500168 write32(abar + 0x00, reg32);
169 /* PI (Ports implemented) */
170 write32(abar + 0x0c, config->sata_port_map);
171 (void) read32(abar + 0x0c); /* Read back 1 */
172 (void) read32(abar + 0x0c); /* Read back 2 */
173 /* CAP2 (HBA Capabilities Extended)*/
174 reg32 = read32(abar + 0x24);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800175#if CONFIG_INTEL_LYNXPOINT_LP
176 /* Enable DEVSLP */
177 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
178#else
Aaron Durbin76c37002012-10-30 09:03:43 -0500179 reg32 &= ~0x00000002;
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800180#endif
Aaron Durbin76c37002012-10-30 09:03:43 -0500181 write32(abar + 0x24, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500182 } else {
183 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
184
185 /* No AHCI: clear AHCI base */
186 pci_write_config32(dev, 0x24, 0x00000000);
187
188 /* And without AHCI BAR no memory decoding */
189 reg16 = pci_read_config16(dev, PCI_COMMAND);
190 reg16 &= ~PCI_COMMAND_MEMORY;
191 pci_write_config16(dev, PCI_COMMAND, reg16);
192
193 /* Native mode capable on both primary and secondary (0xa)
194 * or'ed with enabled (0x50) = 0xf
195 */
196 pci_write_config8(dev, 0x09, 0x8f);
197
198 /* Set Interrupt Line */
199 /* Interrupt Pin is set by D31IP.PIP */
200 pci_write_config8(dev, INTR_LN, 0xff);
201
202 /* Set timings */
203 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
204 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
205 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
206 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
207 IDE_SITRE | IDE_ISP_3_CLOCKS |
208 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
209
210 /* Sync DMA */
211 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
212 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
213
214 /* Set IDE I/O Configuration */
215 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
216 pci_write_config32(dev, IDE_CONFIG, reg32);
217
218 /* Port enable */
219 reg16 = pci_read_config16(dev, 0x92);
220 reg16 &= ~0x3f;
221 reg16 |= config->sata_port_map;
222 pci_write_config16(dev, 0x92, reg16);
223
224 /* SATA Initialization register */
225 pci_write_config32(dev, 0x94,
226 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
227 }
228
229 /* Set Gen3 Transmitter settings if needed */
230 if (config->sata_port0_gen3_tx)
231 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
232 config->sata_port0_gen3_tx);
233
234 if (config->sata_port1_gen3_tx)
235 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
236 config->sata_port1_gen3_tx);
237
238 /* Additional Programming Requirements */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800239 /* Power Optimizer */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800240
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800241 /* Step 1 */
242#if CONFIG_INTEL_LYNXPOINT_LP
243 sir_write(dev, 0x64, 0x883c9003);
244#else
245 sir_write(dev, 0x64, 0x883c9001);
246#endif
247
248 /* Step 2: SIR 68h[15:0] = 880Ah */
Aaron Durbin76c37002012-10-30 09:03:43 -0500249 reg32 = sir_read(dev, 0x68);
250 reg32 &= 0xffff0000;
Duncan Laurie74c0d052012-12-17 11:31:40 -0800251 reg32 |= 0x880a;
Aaron Durbin76c37002012-10-30 09:03:43 -0500252 sir_write(dev, 0x68, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500253
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800254 /* Step 3: SIR 60h[3] = 1 */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800255 reg32 = sir_read(dev, 0x60);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800256 reg32 |= (1 << 3);
257 sir_write(dev, 0x60, reg32);
258
259 /* Step 4: SIR 60h[0] = 1 */
260 reg32 = sir_read(dev, 0x60);
261 reg32 |= (1 << 0);
262 sir_write(dev, 0x60, reg32);
263
264 /* Step 5: SIR 60h[1] = 1 */
265 reg32 = sir_read(dev, 0x60);
266 reg32 |= (1 << 1);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800267 sir_write(dev, 0x60, reg32);
268
269 /* Clock Gating */
270 sir_write(dev, 0x70, 0x3f00bf1f);
271#if CONFIG_INTEL_LYNXPOINT_LP
272 sir_write(dev, 0x54, 0xcf000f0f);
273 sir_write(dev, 0x58, 0x00190000);
274#endif
275
276 reg32 = pci_read_config32(dev, 0x300);
277 reg32 |= (1 << 17) | (1 << 16);
278 reg32 |= (1 << 31) | (1 << 30) | (1 << 29);
279 pci_write_config32(dev, 0x300, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500280}
281
282static void sata_enable(device_t dev)
283{
284 /* Get the chip configuration */
285 config_t *config = dev->chip_info;
286 u16 map = 0;
287
288 if (!config)
289 return;
290
291 /*
292 * Set SATA controller mode early so the resource allocator can
293 * properly assign IO/Memory resources for the controller.
294 */
295 if (config->sata_ahci)
296 map = 0x0060;
297
298 map |= (config->sata_port_map ^ 0x3f) << 8;
299
300 pci_write_config16(dev, 0x90, map);
301}
302
303static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device)
304{
305 if (!vendor || !device) {
306 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
307 pci_read_config32(dev, PCI_VENDOR_ID));
308 } else {
309 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
310 ((device & 0xffff) << 16) | (vendor & 0xffff));
311 }
312}
313
314static struct pci_operations sata_pci_ops = {
315 .set_subsystem = sata_set_subsystem,
316};
317
318static struct device_operations sata_ops = {
319 .read_resources = pci_dev_read_resources,
320 .set_resources = pci_dev_set_resources,
321 .enable_resources = pci_dev_enable_resources,
322 .init = sata_init,
323 .enable = sata_enable,
324 .scan_bus = 0,
325 .ops_pci = &sata_pci_ops,
326};
327
Duncan Laurie74c0d052012-12-17 11:31:40 -0800328static const unsigned short pci_device_ids[] = {
329 0x8c00, 0x8c02, 0x8c04, 0x8c06, 0x8c08, 0x8c0e, /* Desktop */
330 0x8c01, 0x8c03, 0x8c05, 0x8c07, 0x8c09, 0x8c0f, /* Mobile */
331 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* Low Power */
332 0
333};
Aaron Durbin76c37002012-10-30 09:03:43 -0500334
335static const struct pci_driver pch_sata __pci_driver = {
336 .ops = &sata_ops,
337 .vendor = PCI_VENDOR_ID_INTEL,
338 .devices = pci_device_ids,
339};
340