blob: aa18f4a59b74396f2c4415a0d9788e43377c947d [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.
Aaron Durbin76c37002012-10-30 09:03:43 -050015 */
16
17#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020018#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <console/console.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
Duncan Laurie26e7dd72012-12-19 09:12:31 -080023#include <delay.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include "pch.h"
25
26typedef struct southbridge_intel_lynxpoint_config config_t;
27
28static inline u32 sir_read(struct device *dev, int idx)
29{
30 pci_write_config32(dev, SATA_SIRI, idx);
31 return pci_read_config32(dev, SATA_SIRD);
32}
33
34static inline void sir_write(struct device *dev, int idx, u32 value)
35{
36 pci_write_config32(dev, SATA_SIRI, idx);
37 pci_write_config32(dev, SATA_SIRD, value);
38}
39
40static void sata_init(struct device *dev)
41{
42 u32 reg32;
43 u16 reg16;
44 /* Get the chip configuration */
45 config_t *config = dev->chip_info;
46
47 printk(BIOS_DEBUG, "SATA: Initializing...\n");
48
49 if (config == NULL) {
50 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
51 return;
52 }
53
54 /* SATA configuration */
55
56 /* Enable BARs */
57 pci_write_config16(dev, PCI_COMMAND, 0x0007);
58
59 if (config->ide_legacy_combined) {
60 printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n");
61
62 /* No AHCI: clear AHCI base */
63 pci_write_config32(dev, 0x24, 0x00000000);
64 /* And without AHCI BAR no memory decoding */
65 reg16 = pci_read_config16(dev, PCI_COMMAND);
66 reg16 &= ~PCI_COMMAND_MEMORY;
67 pci_write_config16(dev, PCI_COMMAND, reg16);
68
69 pci_write_config8(dev, 0x09, 0x80);
70
71 /* Set timings */
72 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
73 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
74 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
75 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
76 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
77
78 /* Sync DMA */
79 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
80 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
81
82 /* Set IDE I/O Configuration */
83 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
84 pci_write_config32(dev, IDE_CONFIG, reg32);
85
86 /* Port enable */
87 reg16 = pci_read_config16(dev, 0x92);
88 reg16 &= ~0x3f;
89 reg16 |= config->sata_port_map;
90 pci_write_config16(dev, 0x92, reg16);
91
92 /* SATA Initialization register */
93 pci_write_config32(dev, 0x94,
94 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Elyes HAOUAS70d79a42016-08-21 18:36:06 +020095 } else if (config->sata_ahci) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080096 u32 *abar;
Aaron Durbin76c37002012-10-30 09:03:43 -050097
98 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
99
100 /* Set Interrupt Line */
101 /* Interrupt Pin is set by D31IP.PIP */
102 pci_write_config8(dev, INTR_LN, 0x0a);
103
104 /* Set timings */
105 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
106 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
107 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
108 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
109 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
110
111 /* Sync DMA */
112 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
113 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
114
115 /* Set IDE I/O Configuration */
116 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
117 pci_write_config32(dev, IDE_CONFIG, reg32);
118
119 /* for AHCI, Port Enable is managed in memory mapped space */
120 reg16 = pci_read_config16(dev, 0x92);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800121 reg16 &= ~0x3f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500122 reg16 |= 0x8000 | config->sata_port_map;
123 pci_write_config16(dev, 0x92, reg16);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800124 udelay(2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500125
Duncan Laurie74c0d052012-12-17 11:31:40 -0800126 /* Setup register 98h */
127 reg32 = pci_read_config16(dev, 0x98);
128 reg32 |= 1 << 19; /* BWG step 6 */
129 reg32 |= 1 << 22; /* BWG step 5 */
130 reg32 &= ~(0x3f << 7);
131 reg32 |= 0x04 << 7; /* BWG step 7 */
132 reg32 |= 1 << 20; /* BWG step 8 */
133 reg32 &= ~(0x03 << 5);
134 reg32 |= 1 << 5; /* BWG step 9 */
135 reg32 |= 1 << 18; /* BWG step 10 */
136 reg32 |= 1 << 29; /* BWG step 11 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800137 if (pch_is_lp()) {
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700138 reg32 &= ~((1UL << 31) | (1 << 30));
Duncan Laurie70f04b42013-03-08 17:17:33 -0800139 reg32 |= 1 << 23;
140 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
141 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800142 pci_write_config32(dev, 0x98, reg32);
143
144 /* Setup register 9Ch */
145 reg16 = 0; /* Disable alternate ID */
146 reg16 = 1 << 5; /* BWG step 12 */
147 pci_write_config16(dev, 0x9c, reg16);
148
Aaron Durbin76c37002012-10-30 09:03:43 -0500149 /* SATA Initialization register */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800150 reg32 = 0x183;
151 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
152 reg32 |= (config->sata_devslp_mux & 1) << 15;
153 pci_write_config32(dev, 0x94, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500154
155 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800156 abar = (u32 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
157 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Aaron Durbin76c37002012-10-30 09:03:43 -0500158 /* CAP (HBA Capabilities) : enable power management */
159 reg32 = read32(abar + 0x00);
160 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
161 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Duncan Laurie70f04b42013-03-08 17:17:33 -0800162 if (pch_is_lp())
163 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
Aaron Durbin76c37002012-10-30 09:03:43 -0500164 write32(abar + 0x00, reg32);
165 /* PI (Ports implemented) */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800166 write32(abar + 0x03, config->sata_port_map);
167 (void) read32(abar + 0x03); /* Read back 1 */
168 (void) read32(abar + 0x03); /* Read back 2 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500169 /* CAP2 (HBA Capabilities Extended)*/
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800170 reg32 = read32(abar + 0x09);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800171 /* Enable DEVSLP */
Marc Jonese05cba22013-10-30 23:56:26 -0600172 if (pch_is_lp()) {
173 if (config->sata_devslp_disable)
174 reg32 &= ~(1 << 3);
175 else
176 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
177 } else {
Duncan Laurie70f04b42013-03-08 17:17:33 -0800178 reg32 &= ~0x00000002;
Marc Jonese05cba22013-10-30 23:56:26 -0600179 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800180 write32(abar + 0x09, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500181 } else {
182 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
183
184 /* No AHCI: clear AHCI base */
185 pci_write_config32(dev, 0x24, 0x00000000);
186
187 /* And without AHCI BAR no memory decoding */
188 reg16 = pci_read_config16(dev, PCI_COMMAND);
189 reg16 &= ~PCI_COMMAND_MEMORY;
190 pci_write_config16(dev, PCI_COMMAND, reg16);
191
192 /* Native mode capable on both primary and secondary (0xa)
193 * or'ed with enabled (0x50) = 0xf
194 */
195 pci_write_config8(dev, 0x09, 0x8f);
196
197 /* Set Interrupt Line */
198 /* Interrupt Pin is set by D31IP.PIP */
199 pci_write_config8(dev, INTR_LN, 0xff);
200
201 /* Set timings */
202 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
203 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
204 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
205 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
206 IDE_SITRE | IDE_ISP_3_CLOCKS |
207 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
208
209 /* Sync DMA */
210 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
211 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
212
213 /* Set IDE I/O Configuration */
214 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
215 pci_write_config32(dev, IDE_CONFIG, reg32);
216
217 /* Port enable */
218 reg16 = pci_read_config16(dev, 0x92);
219 reg16 &= ~0x3f;
220 reg16 |= config->sata_port_map;
221 pci_write_config16(dev, 0x92, reg16);
222
223 /* SATA Initialization register */
224 pci_write_config32(dev, 0x94,
225 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
226 }
227
228 /* Set Gen3 Transmitter settings if needed */
229 if (config->sata_port0_gen3_tx)
230 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
231 config->sata_port0_gen3_tx);
232
233 if (config->sata_port1_gen3_tx)
234 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
235 config->sata_port1_gen3_tx);
236
Shawn Nematbakhsh28752272013-08-13 10:45:21 -0700237 /* Set Gen3 DTLE DATA / EDGE registers if needed */
238 if (config->sata_port0_gen3_dtle) {
239 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
240 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
241 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
242 << SATA_DTLE_DATA_SHIFT);
243
244 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
245 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
246 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
247 << SATA_DTLE_EDGE_SHIFT);
248 }
249
250 if (config->sata_port1_gen3_dtle) {
251 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
252 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
253 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
254 << SATA_DTLE_DATA_SHIFT);
255
256 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
257 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
258 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
259 << SATA_DTLE_EDGE_SHIFT);
260 }
261
Aaron Durbin76c37002012-10-30 09:03:43 -0500262 /* Additional Programming Requirements */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800263 /* Power Optimizer */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800264
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800265 /* Step 1 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800266 if (pch_is_lp())
267 sir_write(dev, 0x64, 0x883c9003);
268 else
269 sir_write(dev, 0x64, 0x883c9001);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800270
271 /* Step 2: SIR 68h[15:0] = 880Ah */
Aaron Durbin76c37002012-10-30 09:03:43 -0500272 reg32 = sir_read(dev, 0x68);
273 reg32 &= 0xffff0000;
Duncan Laurie74c0d052012-12-17 11:31:40 -0800274 reg32 |= 0x880a;
Aaron Durbin76c37002012-10-30 09:03:43 -0500275 sir_write(dev, 0x68, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500276
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800277 /* Step 3: SIR 60h[3] = 1 */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800278 reg32 = sir_read(dev, 0x60);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800279 reg32 |= (1 << 3);
280 sir_write(dev, 0x60, reg32);
281
282 /* Step 4: SIR 60h[0] = 1 */
283 reg32 = sir_read(dev, 0x60);
284 reg32 |= (1 << 0);
285 sir_write(dev, 0x60, reg32);
286
287 /* Step 5: SIR 60h[1] = 1 */
288 reg32 = sir_read(dev, 0x60);
289 reg32 |= (1 << 1);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800290 sir_write(dev, 0x60, reg32);
291
292 /* Clock Gating */
293 sir_write(dev, 0x70, 0x3f00bf1f);
Duncan Laurie70f04b42013-03-08 17:17:33 -0800294 if (pch_is_lp()) {
295 sir_write(dev, 0x54, 0xcf000f0f);
296 sir_write(dev, 0x58, 0x00190000);
297 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800298
299 reg32 = pci_read_config32(dev, 0x300);
300 reg32 |= (1 << 17) | (1 << 16);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700301 reg32 |= (1UL << 31) | (1 << 30) | (1 << 29);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800302 pci_write_config32(dev, 0x300, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500303}
304
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200305static void sata_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500306{
307 /* Get the chip configuration */
308 config_t *config = dev->chip_info;
309 u16 map = 0;
310
311 if (!config)
312 return;
313
314 /*
315 * Set SATA controller mode early so the resource allocator can
316 * properly assign IO/Memory resources for the controller.
317 */
318 if (config->sata_ahci)
319 map = 0x0060;
320
321 map |= (config->sata_port_map ^ 0x3f) << 8;
322
323 pci_write_config16(dev, 0x90, map);
324}
325
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200326static void sata_set_subsystem(struct device *dev, unsigned vendor,
327 unsigned device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500328{
329 if (!vendor || !device) {
330 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
331 pci_read_config32(dev, PCI_VENDOR_ID));
332 } else {
333 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
334 ((device & 0xffff) << 16) | (vendor & 0xffff));
335 }
336}
337
338static struct pci_operations sata_pci_ops = {
339 .set_subsystem = sata_set_subsystem,
340};
341
342static struct device_operations sata_ops = {
343 .read_resources = pci_dev_read_resources,
344 .set_resources = pci_dev_set_resources,
345 .enable_resources = pci_dev_enable_resources,
346 .init = sata_init,
347 .enable = sata_enable,
348 .scan_bus = 0,
349 .ops_pci = &sata_pci_ops,
350};
351
Duncan Laurie74c0d052012-12-17 11:31:40 -0800352static const unsigned short pci_device_ids[] = {
353 0x8c00, 0x8c02, 0x8c04, 0x8c06, 0x8c08, 0x8c0e, /* Desktop */
354 0x8c01, 0x8c03, 0x8c05, 0x8c07, 0x8c09, 0x8c0f, /* Mobile */
355 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* Low Power */
356 0
357};
Aaron Durbin76c37002012-10-30 09:03:43 -0500358
359static const struct pci_driver pch_sata __pci_driver = {
360 .ops = &sata_ops,
361 .vendor = PCI_VENDOR_ID_INTEL,
362 .devices = pci_device_ids,
363};