blob: 1974e2b855e3bd5dddf802fd3201ac25e42fc2a2 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
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>
Stefan Reinauer8e073822012-04-04 00:07:22 +02005#include <console/console.h>
6#include <device/device.h>
7#include <device/pci.h>
8#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02009#include <option.h>
Furquan Shaikhc0bff972020-04-30 19:19:33 -070010#include <acpi/acpi_sata.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020011#include <types.h>
12
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030013#include "chip.h"
Elyes HAOUASab89edb2019-05-15 21:10:44 +020014#include "pch.h"
Stefan Reinauer8e073822012-04-04 00:07:22 +020015
16typedef struct southbridge_intel_bd82x6x_config config_t;
17
Stefan Reinauer16b022a2012-07-17 16:42:51 -070018static inline u32 sir_read(struct device *dev, int idx)
19{
20 pci_write_config32(dev, SATA_SIRI, idx);
21 return pci_read_config32(dev, SATA_SIRD);
22}
23
24static inline void sir_write(struct device *dev, int idx, u32 value)
25{
26 pci_write_config32(dev, SATA_SIRI, idx);
27 pci_write_config32(dev, SATA_SIRD, value);
28}
29
Nico Huber63be0602019-02-13 15:11:09 +010030static void sata_read_resources(struct device *dev)
31{
32 struct resource *res;
33
34 pci_dev_read_resources(dev);
35
36 /* Assign fixed resources for IDE legacy mode */
37
38 u8 sata_mode = 0;
39 get_option(&sata_mode, "sata_mode");
40 if (sata_mode != 2)
41 return;
42
43 res = find_resource(dev, PCI_BASE_ADDRESS_0);
44 if (res) {
45 res->base = 0x1f0;
46 res->size = 8;
47 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
48 }
49
50 res = find_resource(dev, PCI_BASE_ADDRESS_1);
51 if (res) {
52 res->base = 0x3f4;
53 res->size = 4;
54 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
55 }
56
57 res = find_resource(dev, PCI_BASE_ADDRESS_2);
58 if (res) {
59 res->base = 0x170;
60 res->size = 8;
61 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
62 }
63
64 res = find_resource(dev, PCI_BASE_ADDRESS_3);
65 if (res) {
66 res->base = 0x374;
67 res->size = 4;
68 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
69 }
70}
71
72static void sata_set_resources(struct device *dev)
73{
74 /* work around bug in pci_dev_set_resources(), it bails out on FIXED */
75 u8 sata_mode = 0;
76 get_option(&sata_mode, "sata_mode");
77 if (sata_mode == 2) {
78 unsigned int i;
79 for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_3; i += 4) {
80 struct resource *const res = find_resource(dev, i);
81 if (res)
82 res->flags &= ~IORESOURCE_FIXED;
83 }
84 }
85
86 pci_dev_set_resources(dev);
87}
88
Stefan Reinauer8e073822012-04-04 00:07:22 +020089static void sata_init(struct device *dev)
90{
91 u32 reg32;
92 u16 reg16;
93 /* Get the chip configuration */
94 config_t *config = dev->chip_info;
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +010095 u8 sata_mode;
Stefan Reinauer8e073822012-04-04 00:07:22 +020096
Stefan Reinauer16b022a2012-07-17 16:42:51 -070097 printk(BIOS_DEBUG, "SATA: Initializing...\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020098
99 if (config == NULL) {
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700100 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200101 return;
102 }
103
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100104 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
105 /* Default to AHCI */
106 sata_mode = 0;
107
Stefan Reinauer8e073822012-04-04 00:07:22 +0200108 /* SATA configuration */
109
110 /* Enable BARs */
111 pci_write_config16(dev, PCI_COMMAND, 0x0007);
112
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100113 /* AHCI */
114 if (sata_mode == 0) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800115 u8 *abar;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700117 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118
Stefan Reinauer8e073822012-04-04 00:07:22 +0200119 /* Set timings */
120 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
121 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
122 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
123 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
124 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
125
126 /* Sync DMA */
127 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
128 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
129
130 /* Set IDE I/O Configuration */
131 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
132 pci_write_config32(dev, IDE_CONFIG, reg32);
133
134 /* for AHCI, Port Enable is managed in memory mapped space */
135 reg16 = pci_read_config16(dev, 0x92);
136 reg16 &= ~0x3f; /* 6 ports SKU + ORM */
137 reg16 |= 0x8000 | config->sata_port_map;
138 pci_write_config16(dev, 0x92, reg16);
139
140 /* SATA Initialization register */
141 pci_write_config32(dev, 0x94,
142 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
143
144 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800145 abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
146 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200147 /* CAP (HBA Capabilities) : enable power management */
148 reg32 = read32(abar + 0x00);
149 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
150 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Shawn Nematbakhshc9fc0292013-03-14 10:44:13 -0700151 /* Set ISS, if available */
152 if (config->sata_interface_speed_support)
153 {
154 reg32 &= ~0x00f00000;
155 reg32 |= (config->sata_interface_speed_support & 0x03)
156 << 20;
157 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200158 write32(abar + 0x00, reg32);
159 /* PI (Ports implemented) */
160 write32(abar + 0x0c, config->sata_port_map);
161 (void) read32(abar + 0x0c); /* Read back 1 */
162 (void) read32(abar + 0x0c); /* Read back 2 */
163 /* CAP2 (HBA Capabilities Extended)*/
164 reg32 = read32(abar + 0x24);
165 reg32 &= ~0x00000002;
166 write32(abar + 0x24, reg32);
167 /* VSP (Vendor Specific Register */
168 reg32 = read32(abar + 0xa0);
169 reg32 &= ~0x00000005;
170 write32(abar + 0xa0, reg32);
171 } else {
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100172 /* IDE */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200173
Felix Singer192666f2020-04-06 10:54:42 +0200174 /* Without AHCI BAR no memory decoding */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200175 reg16 = pci_read_config16(dev, PCI_COMMAND);
176 reg16 &= ~PCI_COMMAND_MEMORY;
177 pci_write_config16(dev, PCI_COMMAND, reg16);
178
Nico Huber63be0602019-02-13 15:11:09 +0100179 if (sata_mode == 1) {
180 /* Native mode on both primary and secondary. */
181 pci_or_config8(dev, 0x09, 0x05);
182 printk(BIOS_DEBUG, "SATA: Controller in IDE compat mode.\n");
183 } else {
184 /* Legacy mode on both primary and secondary. */
185 pci_update_config8(dev, 0x09, ~0x05, 0x00);
186 printk(BIOS_DEBUG, "SATA: Controller in IDE legacy mode.\n");
187 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200188
Nico Huber56473ca2019-02-28 12:43:21 +0100189 /* Enable I/O decoding */
190 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE);
191 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200192
Nico Huber56473ca2019-02-28 12:43:21 +0100193 /* Port enable + OOB retry mode */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200194 reg16 = pci_read_config16(dev, 0x92);
195 reg16 &= ~0x3f;
Nico Huber56473ca2019-02-28 12:43:21 +0100196 reg16 |= config->sata_port_map | 0x8000;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200197 pci_write_config16(dev, 0x92, reg16);
198
199 /* SATA Initialization register */
200 pci_write_config32(dev, 0x94,
201 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
202 }
Duncan Lauriecfb64bd2012-07-16 16:16:31 -0700203
204 /* Set Gen3 Transmitter settings if needed */
205 if (config->sata_port0_gen3_tx)
206 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
207 config->sata_port0_gen3_tx);
208
209 if (config->sata_port1_gen3_tx)
210 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
211 config->sata_port1_gen3_tx);
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700212
213 /* Additional Programming Requirements */
214 sir_write(dev, 0x04, 0x00001600);
215 sir_write(dev, 0x28, 0xa0000033);
216 reg32 = sir_read(dev, 0x54);
217 reg32 &= 0xff000000;
218 reg32 |= 0x5555aa;
219 sir_write(dev, 0x54, reg32);
220 sir_write(dev, 0x64, 0xcccc8484);
221 reg32 = sir_read(dev, 0x68);
222 reg32 &= 0xffff0000;
223 reg32 |= 0xcccc;
224 sir_write(dev, 0x68, reg32);
225 reg32 = sir_read(dev, 0x78);
226 reg32 &= 0x0000ffff;
227 reg32 |= 0x88880000;
228 sir_write(dev, 0x78, reg32);
229 sir_write(dev, 0x84, 0x001c7000);
230 sir_write(dev, 0x88, 0x88338822);
231 sir_write(dev, 0xa0, 0x001c7000);
232 // a4
233 sir_write(dev, 0xc4, 0x0c0c0c0c);
234 sir_write(dev, 0xc8, 0x0c0c0c0c);
235 sir_write(dev, 0xd4, 0x10000000);
236
237 pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000);
238 pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100);
Felix Singer7daf3cd2020-04-06 11:01:28 +0200239
240 pci_update_config32(dev, 0x98,
241 ~(1 << 16 | 0x3f << 7 | 3 << 5 | 3 << 3),
242 1 << 24 | 1 << 22 | 1 << 20 | 1 << 19 |
243 1 << 18 | 1 << 14 | 0x04 << 7 | 1 << 3);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200244}
245
Elyes HAOUAS4aec3402018-05-25 08:29:27 +0200246static void sata_enable(struct device *dev)
Stefan Reinauer816d0812012-04-30 16:42:07 -0700247{
248 /* Get the chip configuration */
249 config_t *config = dev->chip_info;
250 u16 map = 0;
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100251 u8 sata_mode;
Stefan Reinauer816d0812012-04-30 16:42:07 -0700252
253 if (!config)
254 return;
255
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100256 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
257 sata_mode = 0;
258
Stefan Reinauer816d0812012-04-30 16:42:07 -0700259 /*
260 * Set SATA controller mode early so the resource allocator can
261 * properly assign IO/Memory resources for the controller.
262 */
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100263 if (sata_mode == 0)
Stefan Reinauer816d0812012-04-30 16:42:07 -0700264 map = 0x0060;
265
266 map |= (config->sata_port_map ^ 0x3f) << 8;
267
268 pci_write_config16(dev, 0x90, map);
269}
270
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600271static const char *sata_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200272{
273 return "SATA";
274}
275
Furquan Shaikh7536a392020-04-24 21:59:21 -0700276static void sata_fill_ssdt(const struct device *dev)
Alexander Couzens7bf47ee2015-04-16 02:00:21 +0200277{
278 config_t *config = dev->chip_info;
279 generate_sata_ssdt_ports("\\_SB_.PCI0.SATA", config->sata_port_map);
280}
281
Stefan Reinauer8e073822012-04-04 00:07:22 +0200282static struct device_operations sata_ops = {
Nico Huber63be0602019-02-13 15:11:09 +0100283 .read_resources = sata_read_resources,
284 .set_resources = sata_set_resources,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200285 .enable_resources = pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200286 .acpi_fill_ssdt = sata_fill_ssdt,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200287 .init = sata_init,
Stefan Reinauer816d0812012-04-30 16:42:07 -0700288 .enable = sata_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200289 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200290 .acpi_name = sata_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200291};
292
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700293static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03,
294 0x1e00, 0x1e01, 0x1e02, 0x1e03,
295 0 };
296
297static const struct pci_driver pch_sata __pci_driver = {
298 .ops = &sata_ops,
299 .vendor = PCI_VENDOR_ID_INTEL,
300 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200301};