blob: 57824dfe90a2f1a829b8aadaed37f4f973087f72 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05005#include <console/console.h>
6#include <device/device.h>
7#include <device/pci.h>
8#include <device/pci_ids.h>
Duncan Laurie26e7dd72012-12-19 09:12:31 -08009#include <delay.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030010#include "chip.h"
Angel Pons2178b722020-05-31 00:55:35 +020011#include "iobp.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 */
Angel Pons89739ba2020-07-25 02:46:39 +020045 pci_write_config16(dev, PCI_COMMAND,
46 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Aaron Durbin76c37002012-10-30 09:03:43 -050047
48 if (config->ide_legacy_combined) {
49 printk(BIOS_DEBUG, "SATA: Controller in combined mode.\n");
50
51 /* No AHCI: clear AHCI base */
Angel Ponsbf9bc502020-06-08 00:12:43 +020052 pci_write_config32(dev, 0x24, 0);
53
Aaron Durbin76c37002012-10-30 09:03:43 -050054 /* And without AHCI BAR no memory decoding */
Angel Ponsbf9bc502020-06-08 00:12:43 +020055 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MEMORY);
Aaron Durbin76c37002012-10-30 09:03:43 -050056
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 */
Angel Ponsbf9bc502020-06-08 00:12:43 +020081 pci_write_config32(dev, 0x94, ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Elyes HAOUAS70d79a42016-08-21 18:36:06 +020082 } else if (config->sata_ahci) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080083 u32 *abar;
Aaron Durbin76c37002012-10-30 09:03:43 -050084
85 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
86
87 /* Set Interrupt Line */
88 /* Interrupt Pin is set by D31IP.PIP */
89 pci_write_config8(dev, INTR_LN, 0x0a);
90
91 /* Set timings */
92 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
93 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
94 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
95 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
96 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
97
98 /* Sync DMA */
99 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
100 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
101
102 /* Set IDE I/O Configuration */
103 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
104 pci_write_config32(dev, IDE_CONFIG, reg32);
105
106 /* for AHCI, Port Enable is managed in memory mapped space */
107 reg16 = pci_read_config16(dev, 0x92);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800108 reg16 &= ~0x3f;
Aaron Durbin76c37002012-10-30 09:03:43 -0500109 reg16 |= 0x8000 | config->sata_port_map;
110 pci_write_config16(dev, 0x92, reg16);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800111 udelay(2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500112
Duncan Laurie74c0d052012-12-17 11:31:40 -0800113 /* Setup register 98h */
114 reg32 = pci_read_config16(dev, 0x98);
115 reg32 |= 1 << 19; /* BWG step 6 */
116 reg32 |= 1 << 22; /* BWG step 5 */
117 reg32 &= ~(0x3f << 7);
118 reg32 |= 0x04 << 7; /* BWG step 7 */
119 reg32 |= 1 << 20; /* BWG step 8 */
120 reg32 &= ~(0x03 << 5);
121 reg32 |= 1 << 5; /* BWG step 9 */
122 reg32 |= 1 << 18; /* BWG step 10 */
123 reg32 |= 1 << 29; /* BWG step 11 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800124 if (pch_is_lp()) {
Ryan Salsamendi3f2fe182017-07-04 13:14:16 -0700125 reg32 &= ~((1UL << 31) | (1 << 30));
Duncan Laurie70f04b42013-03-08 17:17:33 -0800126 reg32 |= 1 << 23;
127 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
128 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800129 pci_write_config32(dev, 0x98, reg32);
130
Angel Ponsbf9bc502020-06-08 00:12:43 +0200131 /* Setup register 9Ch: Disable alternate ID and BWG step 12 */
132 pci_write_config16(dev, 0x9c, 1 << 5);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800133
Aaron Durbin76c37002012-10-30 09:03:43 -0500134 /* SATA Initialization register */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800135 reg32 = 0x183;
136 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
137 reg32 |= (config->sata_devslp_mux & 1) << 15;
138 pci_write_config32(dev, 0x94, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500139
140 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800141 abar = (u32 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
142 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Aaron Durbin76c37002012-10-30 09:03:43 -0500143 /* CAP (HBA Capabilities) : enable power management */
144 reg32 = read32(abar + 0x00);
145 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
146 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Duncan Laurie70f04b42013-03-08 17:17:33 -0800147 if (pch_is_lp())
148 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
Aaron Durbin76c37002012-10-30 09:03:43 -0500149 write32(abar + 0x00, reg32);
150 /* PI (Ports implemented) */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800151 write32(abar + 0x03, config->sata_port_map);
152 (void) read32(abar + 0x03); /* Read back 1 */
153 (void) read32(abar + 0x03); /* Read back 2 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500154 /* CAP2 (HBA Capabilities Extended)*/
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800155 reg32 = read32(abar + 0x09);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800156 /* Enable DEVSLP */
Marc Jonese05cba22013-10-30 23:56:26 -0600157 if (pch_is_lp()) {
158 if (config->sata_devslp_disable)
159 reg32 &= ~(1 << 3);
160 else
161 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
162 } else {
Duncan Laurie70f04b42013-03-08 17:17:33 -0800163 reg32 &= ~0x00000002;
Marc Jonese05cba22013-10-30 23:56:26 -0600164 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800165 write32(abar + 0x09, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500166 } else {
167 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
168
169 /* No AHCI: clear AHCI base */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200170 pci_write_config32(dev, 0x24, 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500171
172 /* And without AHCI BAR no memory decoding */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200173 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MEMORY);
Aaron Durbin76c37002012-10-30 09:03:43 -0500174
Angel Ponsbf9bc502020-06-08 00:12:43 +0200175 /*
176 * Native mode capable on both primary and secondary (0xa)
Aaron Durbin76c37002012-10-30 09:03:43 -0500177 * or'ed with enabled (0x50) = 0xf
Angel Ponsbf9bc502020-06-08 00:12:43 +0200178 *
179 * FIXME: Does not match the code.
Aaron Durbin76c37002012-10-30 09:03:43 -0500180 */
181 pci_write_config8(dev, 0x09, 0x8f);
182
183 /* Set Interrupt Line */
184 /* Interrupt Pin is set by D31IP.PIP */
185 pci_write_config8(dev, INTR_LN, 0xff);
186
187 /* Set timings */
188 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
189 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
190 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
191 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
192 IDE_SITRE | IDE_ISP_3_CLOCKS |
193 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
194
195 /* Sync DMA */
196 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
197 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
198
199 /* Set IDE I/O Configuration */
200 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
201 pci_write_config32(dev, IDE_CONFIG, reg32);
202
203 /* Port enable */
204 reg16 = pci_read_config16(dev, 0x92);
205 reg16 &= ~0x3f;
206 reg16 |= config->sata_port_map;
207 pci_write_config16(dev, 0x92, reg16);
208
209 /* SATA Initialization register */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200210 pci_write_config32(dev, 0x94, ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Aaron Durbin76c37002012-10-30 09:03:43 -0500211 }
212
213 /* Set Gen3 Transmitter settings if needed */
214 if (config->sata_port0_gen3_tx)
215 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
216 config->sata_port0_gen3_tx);
217
218 if (config->sata_port1_gen3_tx)
219 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
220 config->sata_port1_gen3_tx);
221
Shawn Nematbakhsh28752272013-08-13 10:45:21 -0700222 /* Set Gen3 DTLE DATA / EDGE registers if needed */
223 if (config->sata_port0_gen3_dtle) {
224 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
225 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
226 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
227 << SATA_DTLE_DATA_SHIFT);
228
229 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
230 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
231 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
232 << SATA_DTLE_EDGE_SHIFT);
233 }
234
235 if (config->sata_port1_gen3_dtle) {
236 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
237 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
238 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
239 << SATA_DTLE_DATA_SHIFT);
240
241 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
242 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
243 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
244 << SATA_DTLE_EDGE_SHIFT);
245 }
246
Aaron Durbin76c37002012-10-30 09:03:43 -0500247 /* Additional Programming Requirements */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800248 /* Power Optimizer */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800249
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800250 /* Step 1 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800251 if (pch_is_lp())
252 sir_write(dev, 0x64, 0x883c9003);
253 else
254 sir_write(dev, 0x64, 0x883c9001);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800255
256 /* Step 2: SIR 68h[15:0] = 880Ah */
Aaron Durbin76c37002012-10-30 09:03:43 -0500257 reg32 = sir_read(dev, 0x68);
258 reg32 &= 0xffff0000;
Duncan Laurie74c0d052012-12-17 11:31:40 -0800259 reg32 |= 0x880a;
Aaron Durbin76c37002012-10-30 09:03:43 -0500260 sir_write(dev, 0x68, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500261
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800262 /* Step 3: SIR 60h[3] = 1 */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800263 reg32 = sir_read(dev, 0x60);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800264 reg32 |= (1 << 3);
265 sir_write(dev, 0x60, reg32);
266
267 /* Step 4: SIR 60h[0] = 1 */
268 reg32 = sir_read(dev, 0x60);
269 reg32 |= (1 << 0);
270 sir_write(dev, 0x60, reg32);
271
272 /* Step 5: SIR 60h[1] = 1 */
273 reg32 = sir_read(dev, 0x60);
274 reg32 |= (1 << 1);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800275 sir_write(dev, 0x60, reg32);
276
277 /* Clock Gating */
278 sir_write(dev, 0x70, 0x3f00bf1f);
Duncan Laurie70f04b42013-03-08 17:17:33 -0800279 if (pch_is_lp()) {
280 sir_write(dev, 0x54, 0xcf000f0f);
281 sir_write(dev, 0x58, 0x00190000);
282 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800283
284 reg32 = pci_read_config32(dev, 0x300);
285 reg32 |= (1 << 17) | (1 << 16);
Ryan Salsamendi0d9b3602017-06-30 17:15:57 -0700286 reg32 |= (1UL << 31) | (1 << 30) | (1 << 29);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800287 pci_write_config32(dev, 0x300, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500288}
289
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200290static void sata_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500291{
292 /* Get the chip configuration */
293 config_t *config = dev->chip_info;
294 u16 map = 0;
295
296 if (!config)
297 return;
298
299 /*
300 * Set SATA controller mode early so the resource allocator can
301 * properly assign IO/Memory resources for the controller.
302 */
303 if (config->sata_ahci)
304 map = 0x0060;
305
306 map |= (config->sata_port_map ^ 0x3f) << 8;
307
308 pci_write_config16(dev, 0x90, map);
309}
310
Aaron Durbin76c37002012-10-30 09:03:43 -0500311static struct device_operations sata_ops = {
312 .read_resources = pci_dev_read_resources,
313 .set_resources = pci_dev_set_resources,
314 .enable_resources = pci_dev_enable_resources,
315 .init = sata_init,
316 .enable = sata_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200317 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500318};
319
Duncan Laurie74c0d052012-12-17 11:31:40 -0800320static const unsigned short pci_device_ids[] = {
321 0x8c00, 0x8c02, 0x8c04, 0x8c06, 0x8c08, 0x8c0e, /* Desktop */
322 0x8c01, 0x8c03, 0x8c05, 0x8c07, 0x8c09, 0x8c0f, /* Mobile */
323 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* Low Power */
324 0
325};
Aaron Durbin76c37002012-10-30 09:03:43 -0500326
327static const struct pci_driver pch_sata __pci_driver = {
328 .ops = &sata_ops,
329 .vendor = PCI_VENDOR_ID_INTEL,
330 .devices = pci_device_ids,
331};