blob: d8eb2a814bd0e548f41c8583612e0a9fcc0fd863 [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
Angel Ponsd00af4f2020-10-30 12:56:02 +010028static inline void sir_unset_and_set_mask(struct device *dev, int idx, u32 unset, u32 set)
29{
30 pci_write_config32(dev, SATA_SIRI, idx);
31
32 const u32 value = pci_read_config32(dev, SATA_SIRD) & ~unset;
33 pci_write_config32(dev, SATA_SIRD, value | set);
34}
35
Aaron Durbin76c37002012-10-30 09:03:43 -050036static void sata_init(struct device *dev)
37{
38 u32 reg32;
Angel Pons8084b382020-10-30 10:56:31 +010039
40 u32 *abar;
41
Aaron Durbin76c37002012-10-30 09:03:43 -050042 /* Get the chip configuration */
43 config_t *config = dev->chip_info;
44
45 printk(BIOS_DEBUG, "SATA: Initializing...\n");
46
47 if (config == NULL) {
48 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
49 return;
50 }
51
52 /* SATA configuration */
53
Angel Pons1b856922020-10-30 15:30:48 +010054 /* Enable memory space decoding for ABAR */
55 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Aaron Durbin76c37002012-10-30 09:03:43 -050056
Angel Pons8084b382020-10-30 10:56:31 +010057 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -050058
Angel Pons8084b382020-10-30 10:56:31 +010059 /* Set Interrupt Line */
60 /* Interrupt Pin is set by D31IP.PIP */
61 pci_write_config8(dev, INTR_LN, 0x0a);
Angel Ponsbf9bc502020-06-08 00:12:43 +020062
Angel Pons8084b382020-10-30 10:56:31 +010063 /* Set timings */
64 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
65 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
66 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
67 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
68 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
Aaron Durbin76c37002012-10-30 09:03:43 -050069
Angel Pons8084b382020-10-30 10:56:31 +010070 /* Sync DMA */
71 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
72 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
Aaron Durbin76c37002012-10-30 09:03:43 -050073
Angel Pons8084b382020-10-30 10:56:31 +010074 /* Set IDE I/O Configuration */
75 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
76 pci_write_config32(dev, IDE_CONFIG, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -050077
Angel Pons8084b382020-10-30 10:56:31 +010078 /* for AHCI, Port Enable is managed in memory mapped space */
Angel Ponsd00af4f2020-10-30 12:56:02 +010079 pci_update_config16(dev, 0x92, ~0x3f, 0x8000 | config->sata_port_map);
Angel Pons8084b382020-10-30 10:56:31 +010080 udelay(2);
Aaron Durbin76c37002012-10-30 09:03:43 -050081
Angel Pons8084b382020-10-30 10:56:31 +010082 /* Setup register 98h */
83 reg32 = pci_read_config16(dev, 0x98);
84 reg32 |= 1 << 19; /* BWG step 6 */
85 reg32 |= 1 << 22; /* BWG step 5 */
86 reg32 &= ~(0x3f << 7);
87 reg32 |= 0x04 << 7; /* BWG step 7 */
88 reg32 |= 1 << 20; /* BWG step 8 */
89 reg32 &= ~(0x03 << 5);
90 reg32 |= 1 << 5; /* BWG step 9 */
91 reg32 |= 1 << 18; /* BWG step 10 */
92 reg32 |= 1 << 29; /* BWG step 11 */
93 if (pch_is_lp()) {
94 reg32 &= ~((1 << 31) | (1 << 30));
95 reg32 |= 1 << 23;
96 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
Aaron Durbin76c37002012-10-30 09:03:43 -050097 }
Angel Pons8084b382020-10-30 10:56:31 +010098 pci_write_config32(dev, 0x98, reg32);
99
100 /* Setup register 9Ch: Disable alternate ID and BWG step 12 */
101 pci_write_config16(dev, 0x9c, 1 << 5);
102
103 /* SATA Initialization register */
104 reg32 = 0x183;
105 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
106 reg32 |= (config->sata_devslp_mux & 1) << 15;
107 pci_write_config32(dev, 0x94, reg32);
108
109 /* Initialize AHCI memory-mapped space */
110 abar = (u32 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
111 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
112 /* CAP (HBA Capabilities) : enable power management */
113 reg32 = read32(abar + 0x00);
114 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
115 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
116 if (pch_is_lp())
117 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
118 write32(abar + 0x00, reg32);
119 /* PI (Ports implemented) */
120 write32(abar + 0x03, config->sata_port_map);
121 (void)read32(abar + 0x03); /* Read back 1 */
122 (void)read32(abar + 0x03); /* Read back 2 */
123 /* CAP2 (HBA Capabilities Extended)*/
124 reg32 = read32(abar + 0x09);
125 /* Enable DEVSLP */
126 if (pch_is_lp()) {
127 if (config->sata_devslp_disable)
128 reg32 &= ~(1 << 3);
129 else
130 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
131 } else {
132 reg32 &= ~0x00000002;
133 }
134 write32(abar + 0x09, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500135
136 /* Set Gen3 Transmitter settings if needed */
137 if (config->sata_port0_gen3_tx)
138 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
139 config->sata_port0_gen3_tx);
140
141 if (config->sata_port1_gen3_tx)
142 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
143 config->sata_port1_gen3_tx);
144
Shawn Nematbakhsh28752272013-08-13 10:45:21 -0700145 /* Set Gen3 DTLE DATA / EDGE registers if needed */
146 if (config->sata_port0_gen3_dtle) {
147 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
148 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
149 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
150 << SATA_DTLE_DATA_SHIFT);
151
152 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
153 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
154 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
155 << SATA_DTLE_EDGE_SHIFT);
156 }
157
158 if (config->sata_port1_gen3_dtle) {
159 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
160 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
161 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
162 << SATA_DTLE_DATA_SHIFT);
163
164 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
165 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
166 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
167 << SATA_DTLE_EDGE_SHIFT);
168 }
169
Aaron Durbin76c37002012-10-30 09:03:43 -0500170 /* Additional Programming Requirements */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800171 /* Power Optimizer */
Duncan Laurie74c0d052012-12-17 11:31:40 -0800172
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800173 /* Step 1 */
Duncan Laurie70f04b42013-03-08 17:17:33 -0800174 if (pch_is_lp())
175 sir_write(dev, 0x64, 0x883c9003);
176 else
177 sir_write(dev, 0x64, 0x883c9001);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800178
179 /* Step 2: SIR 68h[15:0] = 880Ah */
Angel Ponsd00af4f2020-10-30 12:56:02 +0100180 sir_unset_and_set_mask(dev, 0x68, 0xffff, 0x880a);
Aaron Durbin76c37002012-10-30 09:03:43 -0500181
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800182 /* Step 3: SIR 60h[3] = 1 */
Angel Ponsd00af4f2020-10-30 12:56:02 +0100183 sir_unset_and_set_mask(dev, 0x60, 0, 1 << 3);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800184
185 /* Step 4: SIR 60h[0] = 1 */
Angel Ponsd00af4f2020-10-30 12:56:02 +0100186 sir_unset_and_set_mask(dev, 0x60, 0, 1 << 0);
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800187
188 /* Step 5: SIR 60h[1] = 1 */
Angel Ponsd00af4f2020-10-30 12:56:02 +0100189 sir_unset_and_set_mask(dev, 0x60, 0, 1 << 1);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800190
191 /* Clock Gating */
192 sir_write(dev, 0x70, 0x3f00bf1f);
Duncan Laurie70f04b42013-03-08 17:17:33 -0800193 if (pch_is_lp()) {
194 sir_write(dev, 0x54, 0xcf000f0f);
195 sir_write(dev, 0x58, 0x00190000);
196 }
Duncan Laurie74c0d052012-12-17 11:31:40 -0800197
198 reg32 = pci_read_config32(dev, 0x300);
199 reg32 |= (1 << 17) | (1 << 16);
Angel Pons8963f7d2020-10-24 12:20:28 +0200200 reg32 |= (1 << 31) | (1 << 30) | (1 << 29);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800201 pci_write_config32(dev, 0x300, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500202}
203
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200204static void sata_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500205{
206 /* Get the chip configuration */
207 config_t *config = dev->chip_info;
Aaron Durbin76c37002012-10-30 09:03:43 -0500208
209 if (!config)
210 return;
211
212 /*
213 * Set SATA controller mode early so the resource allocator can
214 * properly assign IO/Memory resources for the controller.
215 */
Angel Pons8084b382020-10-30 10:56:31 +0100216 pci_write_config16(dev, 0x90, 0x0060 | (config->sata_port_map ^ 0x3f) << 8);
Aaron Durbin76c37002012-10-30 09:03:43 -0500217}
218
Aaron Durbin76c37002012-10-30 09:03:43 -0500219static struct device_operations sata_ops = {
220 .read_resources = pci_dev_read_resources,
221 .set_resources = pci_dev_set_resources,
222 .enable_resources = pci_dev_enable_resources,
223 .init = sata_init,
224 .enable = sata_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200225 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500226};
227
Duncan Laurie74c0d052012-12-17 11:31:40 -0800228static const unsigned short pci_device_ids[] = {
229 0x8c00, 0x8c02, 0x8c04, 0x8c06, 0x8c08, 0x8c0e, /* Desktop */
230 0x8c01, 0x8c03, 0x8c05, 0x8c07, 0x8c09, 0x8c0f, /* Mobile */
231 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* Low Power */
232 0
233};
Aaron Durbin76c37002012-10-30 09:03:43 -0500234
235static const struct pci_driver pch_sata __pci_driver = {
236 .ops = &sata_ops,
237 .vendor = PCI_VENDOR_ID_INTEL,
238 .devices = pci_device_ids,
239};