blob: 22be4d169d0db561fb614b9566def1e5a86564b8 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin76c37002012-10-30 09:03:43 -05003
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02005#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
Duncan Laurie26e7dd72012-12-19 09:12:31 -080010#include <delay.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030011#include "chip.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050012#include "pch.h"
13
14typedef struct southbridge_intel_lynxpoint_config config_t;
15
16static inline u32 sir_read(struct device *dev, int idx)
17{
18 pci_write_config32(dev, SATA_SIRI, idx);
19 return pci_read_config32(dev, SATA_SIRD);
20}
21
22static inline void sir_write(struct device *dev, int idx, u32 value)
23{
24 pci_write_config32(dev, SATA_SIRI, idx);
25 pci_write_config32(dev, SATA_SIRD, value);
26}
27
28static void sata_init(struct device *dev)
29{
30 u32 reg32;
31 u16 reg16;
32 /* Get the chip configuration */
33 config_t *config = dev->chip_info;
34
35 printk(BIOS_DEBUG, "SATA: Initializing...\n");
36
37 if (config == NULL) {
38 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
39 return;
40 }
41
42 /* SATA configuration */
43
44 /* Enable BARs */
45 pci_write_config16(dev, PCI_COMMAND, 0x0007);
46
47 if (config->ide_legacy_combined) {
48 printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n");
49
50 /* No AHCI: clear AHCI base */
51 pci_write_config32(dev, 0x24, 0x00000000);
52 /* And without AHCI BAR no memory decoding */
53 reg16 = pci_read_config16(dev, PCI_COMMAND);
54 reg16 &= ~PCI_COMMAND_MEMORY;
55 pci_write_config16(dev, PCI_COMMAND, reg16);
56
57 pci_write_config8(dev, 0x09, 0x80);
58
59 /* Set timings */
60 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
61 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
62 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
63 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
64 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
65
66 /* Sync DMA */
67 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0);
68 pci_write_config16(dev, IDE_SDMA_TIM, 0x0200);
69
70 /* Set IDE I/O Configuration */
71 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
72 pci_write_config32(dev, IDE_CONFIG, reg32);
73
74 /* Port enable */
75 reg16 = pci_read_config16(dev, 0x92);
76 reg16 &= ~0x3f;
77 reg16 |= config->sata_port_map;
78 pci_write_config16(dev, 0x92, reg16);
79
80 /* SATA Initialization register */
81 pci_write_config32(dev, 0x94,
82 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Elyes HAOUAS70d79a42016-08-21 18:36:06 +020083 } else if (config->sata_ahci) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080084 u32 *abar;
Aaron Durbin76c37002012-10-30 09:03:43 -050085
86 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
87
88 /* Set Interrupt Line */
89 /* Interrupt Pin is set by D31IP.PIP */
90 pci_write_config8(dev, INTR_LN, 0x0a);
91
92 /* Set timings */
93 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
94 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
95 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
96 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
97 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
98
99 /* Sync DMA */
100 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
101 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
102
103 /* Set IDE I/O Configuration */
104 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
105 pci_write_config32(dev, IDE_CONFIG, reg32);
106
107 /* for AHCI, Port Enable is managed in memory mapped space */
108 reg16 = pci_read_config16(dev, 0x92);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800109 reg16 &= ~0x3f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500110 reg16 |= 0x8000 | config->sata_port_map;
111 pci_write_config16(dev, 0x92, reg16);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800112 udelay(2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500113
Duncan Laurie74c0d052012-12-17 11:31:40 -0800114 /* Setup register 98h */
115 reg32 = pci_read_config16(dev, 0x98);
116 reg32 |= 1 << 19; /* BWG step 6 */
117 reg32 |= 1 << 22; /* BWG step 5 */
118 reg32 &= ~(0x3f << 7);
119 reg32 |= 0x04 << 7; /* BWG step 7 */
120 reg32 |= 1 << 20; /* BWG step 8 */
121 reg32 &= ~(0x03 << 5);
122 reg32 |= 1 << 5; /* BWG step 9 */
123 reg32 |= 1 << 18; /* BWG step 10 */
124 reg32 |= 1 << 29; /* BWG step 11 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800125 if (pch_is_lp()) {
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700126 reg32 &= ~((1UL << 31) | (1 << 30));
Duncan Laurie70f04b42013-03-08 17:17:33 -0800127 reg32 |= 1 << 23;
128 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
129 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800130 pci_write_config32(dev, 0x98, reg32);
131
132 /* Setup register 9Ch */
133 reg16 = 0; /* Disable alternate ID */
Elyes HAOUAS6de151e2019-10-18 16:43:30 +0200134 reg16 |= (1 << 5); /* BWG step 12 */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800135 pci_write_config16(dev, 0x9c, reg16);
136
Aaron Durbin76c37002012-10-30 09:03:43 -0500137 /* SATA Initialization register */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800138 reg32 = 0x183;
139 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
140 reg32 |= (config->sata_devslp_mux & 1) << 15;
141 pci_write_config32(dev, 0x94, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500142
143 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800144 abar = (u32 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
145 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Aaron Durbin76c37002012-10-30 09:03:43 -0500146 /* CAP (HBA Capabilities) : enable power management */
147 reg32 = read32(abar + 0x00);
148 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
149 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Duncan Laurie70f04b42013-03-08 17:17:33 -0800150 if (pch_is_lp())
151 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
Aaron Durbin76c37002012-10-30 09:03:43 -0500152 write32(abar + 0x00, reg32);
153 /* PI (Ports implemented) */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800154 write32(abar + 0x03, config->sata_port_map);
155 (void) read32(abar + 0x03); /* Read back 1 */
156 (void) read32(abar + 0x03); /* Read back 2 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500157 /* CAP2 (HBA Capabilities Extended)*/
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800158 reg32 = read32(abar + 0x09);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800159 /* Enable DEVSLP */
Marc Jonese05cba22013-10-30 23:56:26 -0600160 if (pch_is_lp()) {
161 if (config->sata_devslp_disable)
162 reg32 &= ~(1 << 3);
163 else
164 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
165 } else {
Duncan Laurie70f04b42013-03-08 17:17:33 -0800166 reg32 &= ~0x00000002;
Marc Jonese05cba22013-10-30 23:56:26 -0600167 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800168 write32(abar + 0x09, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500169 } else {
170 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
171
172 /* No AHCI: clear AHCI base */
173 pci_write_config32(dev, 0x24, 0x00000000);
174
175 /* And without AHCI BAR no memory decoding */
176 reg16 = pci_read_config16(dev, PCI_COMMAND);
177 reg16 &= ~PCI_COMMAND_MEMORY;
178 pci_write_config16(dev, PCI_COMMAND, reg16);
179
180 /* Native mode capable on both primary and secondary (0xa)
181 * or'ed with enabled (0x50) = 0xf
182 */
183 pci_write_config8(dev, 0x09, 0x8f);
184
185 /* Set Interrupt Line */
186 /* Interrupt Pin is set by D31IP.PIP */
187 pci_write_config8(dev, INTR_LN, 0xff);
188
189 /* Set timings */
190 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
191 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
192 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
193 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
194 IDE_SITRE | IDE_ISP_3_CLOCKS |
195 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
196
197 /* Sync DMA */
198 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
199 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
200
201 /* Set IDE I/O Configuration */
202 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
203 pci_write_config32(dev, IDE_CONFIG, reg32);
204
205 /* Port enable */
206 reg16 = pci_read_config16(dev, 0x92);
207 reg16 &= ~0x3f;
208 reg16 |= config->sata_port_map;
209 pci_write_config16(dev, 0x92, reg16);
210
211 /* SATA Initialization register */
212 pci_write_config32(dev, 0x94,
213 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
214 }
215
216 /* Set Gen3 Transmitter settings if needed */
217 if (config->sata_port0_gen3_tx)
218 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
219 config->sata_port0_gen3_tx);
220
221 if (config->sata_port1_gen3_tx)
222 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
223 config->sata_port1_gen3_tx);
224
Shawn Nematbakhsh28752272013-08-13 10:45:21 -0700225 /* Set Gen3 DTLE DATA / EDGE registers if needed */
226 if (config->sata_port0_gen3_dtle) {
227 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
228 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
229 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
230 << SATA_DTLE_DATA_SHIFT);
231
232 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
233 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
234 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
235 << SATA_DTLE_EDGE_SHIFT);
236 }
237
238 if (config->sata_port1_gen3_dtle) {
239 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
240 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
241 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
242 << SATA_DTLE_DATA_SHIFT);
243
244 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
245 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
246 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
247 << SATA_DTLE_EDGE_SHIFT);
248 }
249
Aaron Durbin76c37002012-10-30 09:03:43 -0500250 /* Additional Programming Requirements */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800251 /* Power Optimizer */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800252
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800253 /* Step 1 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800254 if (pch_is_lp())
255 sir_write(dev, 0x64, 0x883c9003);
256 else
257 sir_write(dev, 0x64, 0x883c9001);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800258
259 /* Step 2: SIR 68h[15:0] = 880Ah */
Aaron Durbin76c37002012-10-30 09:03:43 -0500260 reg32 = sir_read(dev, 0x68);
261 reg32 &= 0xffff0000;
Duncan Laurie74c0d052012-12-17 11:31:40 -0800262 reg32 |= 0x880a;
Aaron Durbin76c37002012-10-30 09:03:43 -0500263 sir_write(dev, 0x68, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500264
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800265 /* Step 3: SIR 60h[3] = 1 */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800266 reg32 = sir_read(dev, 0x60);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800267 reg32 |= (1 << 3);
268 sir_write(dev, 0x60, reg32);
269
270 /* Step 4: SIR 60h[0] = 1 */
271 reg32 = sir_read(dev, 0x60);
272 reg32 |= (1 << 0);
273 sir_write(dev, 0x60, reg32);
274
275 /* Step 5: SIR 60h[1] = 1 */
276 reg32 = sir_read(dev, 0x60);
277 reg32 |= (1 << 1);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800278 sir_write(dev, 0x60, reg32);
279
280 /* Clock Gating */
281 sir_write(dev, 0x70, 0x3f00bf1f);
Duncan Laurie70f04b42013-03-08 17:17:33 -0800282 if (pch_is_lp()) {
283 sir_write(dev, 0x54, 0xcf000f0f);
284 sir_write(dev, 0x58, 0x00190000);
285 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800286
287 reg32 = pci_read_config32(dev, 0x300);
288 reg32 |= (1 << 17) | (1 << 16);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700289 reg32 |= (1UL << 31) | (1 << 30) | (1 << 29);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800290 pci_write_config32(dev, 0x300, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500291}
292
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200293static void sata_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500294{
295 /* Get the chip configuration */
296 config_t *config = dev->chip_info;
297 u16 map = 0;
298
299 if (!config)
300 return;
301
302 /*
303 * Set SATA controller mode early so the resource allocator can
304 * properly assign IO/Memory resources for the controller.
305 */
306 if (config->sata_ahci)
307 map = 0x0060;
308
309 map |= (config->sata_port_map ^ 0x3f) << 8;
310
311 pci_write_config16(dev, 0x90, map);
312}
313
Aaron Durbin76c37002012-10-30 09:03:43 -0500314static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530315 .set_subsystem = pci_dev_set_subsystem,
Aaron Durbin76c37002012-10-30 09:03:43 -0500316};
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};