blob: e243c9782c60ff7a109de8df7438b736f4551a1c [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
Angel Pons88dcb312021-04-26 17:10:28 +020038 u8 sata_mode = get_uint_option("sata_mode", 0);
Nico Huber63be0602019-02-13 15:11:09 +010039 if (sata_mode != 2)
40 return;
41
Angel Pons3d8b6e22020-11-02 13:40:11 +010042 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Nico Huber63be0602019-02-13 15:11:09 +010043 if (res) {
44 res->base = 0x1f0;
45 res->size = 8;
46 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
47 }
48
Angel Pons3d8b6e22020-11-02 13:40:11 +010049 res = probe_resource(dev, PCI_BASE_ADDRESS_1);
Nico Huber63be0602019-02-13 15:11:09 +010050 if (res) {
51 res->base = 0x3f4;
52 res->size = 4;
53 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
54 }
55
Angel Pons3d8b6e22020-11-02 13:40:11 +010056 res = probe_resource(dev, PCI_BASE_ADDRESS_2);
Nico Huber63be0602019-02-13 15:11:09 +010057 if (res) {
58 res->base = 0x170;
59 res->size = 8;
60 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
61 }
62
Angel Pons3d8b6e22020-11-02 13:40:11 +010063 res = probe_resource(dev, PCI_BASE_ADDRESS_3);
Nico Huber63be0602019-02-13 15:11:09 +010064 if (res) {
65 res->base = 0x374;
66 res->size = 4;
67 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
68 }
69}
70
71static void sata_set_resources(struct device *dev)
72{
73 /* work around bug in pci_dev_set_resources(), it bails out on FIXED */
Angel Pons88dcb312021-04-26 17:10:28 +020074 u8 sata_mode = get_uint_option("sata_mode", 0);
Nico Huber63be0602019-02-13 15:11:09 +010075 if (sata_mode == 2) {
76 unsigned int i;
77 for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_3; i += 4) {
Angel Pons3d8b6e22020-11-02 13:40:11 +010078 struct resource *const res = probe_resource(dev, i);
Nico Huber63be0602019-02-13 15:11:09 +010079 if (res)
80 res->flags &= ~IORESOURCE_FIXED;
81 }
82 }
83
84 pci_dev_set_resources(dev);
85}
86
Stefan Reinauer8e073822012-04-04 00:07:22 +020087static void sata_init(struct device *dev)
88{
89 u32 reg32;
90 u16 reg16;
91 /* Get the chip configuration */
92 config_t *config = dev->chip_info;
93
Stefan Reinauer16b022a2012-07-17 16:42:51 -070094 printk(BIOS_DEBUG, "SATA: Initializing...\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020095
96 if (config == NULL) {
Stefan Reinauer16b022a2012-07-17 16:42:51 -070097 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020098 return;
99 }
100
Angel Pons0a937752021-04-19 13:01:09 +0200101 /* Default to AHCI */
Angel Pons88dcb312021-04-26 17:10:28 +0200102 u8 sata_mode = get_uint_option("sata_mode", 0);
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100103
Stefan Reinauer8e073822012-04-04 00:07:22 +0200104 /* SATA configuration */
105
106 /* Enable BARs */
Angel Pons89739ba2020-07-25 02:46:39 +0200107 pci_write_config16(dev, PCI_COMMAND,
108 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200109
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100110 /* AHCI */
111 if (sata_mode == 0) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800112 u8 *abar;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700114 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200115
Angel Pons30ff0062020-11-02 13:30:17 +0100116 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE);
117 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118
119 /* for AHCI, Port Enable is managed in memory mapped space */
120 reg16 = pci_read_config16(dev, 0x92);
121 reg16 &= ~0x3f; /* 6 ports SKU + ORM */
122 reg16 |= 0x8000 | config->sata_port_map;
123 pci_write_config16(dev, 0x92, reg16);
124
125 /* SATA Initialization register */
Angel Ponsc803f652020-06-07 22:09:01 +0200126 pci_write_config32(dev, 0x94, ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200127
128 /* Initialize AHCI memory-mapped space */
Patrick Rudolphb50b6a52020-08-20 16:50:01 +0200129 abar = (u8 *)(uintptr_t)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800130 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200131 /* CAP (HBA Capabilities) : enable power management */
132 reg32 = read32(abar + 0x00);
133 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
134 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Shawn Nematbakhshc9fc0292013-03-14 10:44:13 -0700135 /* Set ISS, if available */
136 if (config->sata_interface_speed_support)
137 {
138 reg32 &= ~0x00f00000;
139 reg32 |= (config->sata_interface_speed_support & 0x03)
140 << 20;
141 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200142 write32(abar + 0x00, reg32);
143 /* PI (Ports implemented) */
144 write32(abar + 0x0c, config->sata_port_map);
145 (void) read32(abar + 0x0c); /* Read back 1 */
146 (void) read32(abar + 0x0c); /* Read back 2 */
147 /* CAP2 (HBA Capabilities Extended)*/
148 reg32 = read32(abar + 0x24);
149 reg32 &= ~0x00000002;
150 write32(abar + 0x24, reg32);
151 /* VSP (Vendor Specific Register */
152 reg32 = read32(abar + 0xa0);
153 reg32 &= ~0x00000005;
154 write32(abar + 0xa0, reg32);
155 } else {
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100156 /* IDE */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200157
Felix Singer192666f2020-04-06 10:54:42 +0200158 /* Without AHCI BAR no memory decoding */
Angel Ponsc803f652020-06-07 22:09:01 +0200159 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MEMORY);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160
Nico Huber63be0602019-02-13 15:11:09 +0100161 if (sata_mode == 1) {
162 /* Native mode on both primary and secondary. */
163 pci_or_config8(dev, 0x09, 0x05);
164 printk(BIOS_DEBUG, "SATA: Controller in IDE compat mode.\n");
165 } else {
166 /* Legacy mode on both primary and secondary. */
Angel Ponsc803f652020-06-07 22:09:01 +0200167 pci_and_config8(dev, 0x09, ~0x05);
Nico Huber63be0602019-02-13 15:11:09 +0100168 printk(BIOS_DEBUG, "SATA: Controller in IDE legacy mode.\n");
169 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200170
Nico Huber56473ca2019-02-28 12:43:21 +0100171 /* Enable I/O decoding */
172 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE);
173 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200174
Nico Huber56473ca2019-02-28 12:43:21 +0100175 /* Port enable + OOB retry mode */
Angel Ponsc803f652020-06-07 22:09:01 +0200176 pci_update_config16(dev, 0x92, ~0x3f, config->sata_port_map | 0x8000);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200177
178 /* SATA Initialization register */
Angel Ponsc803f652020-06-07 22:09:01 +0200179 pci_write_config32(dev, 0x94, ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200180 }
Duncan Lauriecfb64bd2012-07-16 16:16:31 -0700181
182 /* Set Gen3 Transmitter settings if needed */
183 if (config->sata_port0_gen3_tx)
184 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
185 config->sata_port0_gen3_tx);
186
187 if (config->sata_port1_gen3_tx)
188 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
189 config->sata_port1_gen3_tx);
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700190
191 /* Additional Programming Requirements */
192 sir_write(dev, 0x04, 0x00001600);
193 sir_write(dev, 0x28, 0xa0000033);
194 reg32 = sir_read(dev, 0x54);
195 reg32 &= 0xff000000;
196 reg32 |= 0x5555aa;
197 sir_write(dev, 0x54, reg32);
198 sir_write(dev, 0x64, 0xcccc8484);
199 reg32 = sir_read(dev, 0x68);
200 reg32 &= 0xffff0000;
201 reg32 |= 0xcccc;
202 sir_write(dev, 0x68, reg32);
203 reg32 = sir_read(dev, 0x78);
204 reg32 &= 0x0000ffff;
205 reg32 |= 0x88880000;
206 sir_write(dev, 0x78, reg32);
207 sir_write(dev, 0x84, 0x001c7000);
208 sir_write(dev, 0x88, 0x88338822);
209 sir_write(dev, 0xa0, 0x001c7000);
210 // a4
211 sir_write(dev, 0xc4, 0x0c0c0c0c);
212 sir_write(dev, 0xc8, 0x0c0c0c0c);
213 sir_write(dev, 0xd4, 0x10000000);
214
215 pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000);
216 pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100);
Felix Singer7daf3cd2020-04-06 11:01:28 +0200217
218 pci_update_config32(dev, 0x98,
219 ~(1 << 16 | 0x3f << 7 | 3 << 5 | 3 << 3),
220 1 << 24 | 1 << 22 | 1 << 20 | 1 << 19 |
221 1 << 18 | 1 << 14 | 0x04 << 7 | 1 << 3);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200222}
223
Elyes HAOUAS4aec3402018-05-25 08:29:27 +0200224static void sata_enable(struct device *dev)
Stefan Reinauer816d0812012-04-30 16:42:07 -0700225{
226 /* Get the chip configuration */
227 config_t *config = dev->chip_info;
228 u16 map = 0;
229
230 if (!config)
231 return;
232
Angel Pons88dcb312021-04-26 17:10:28 +0200233 u8 sata_mode = get_uint_option("sata_mode", 0);
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100234
Stefan Reinauer816d0812012-04-30 16:42:07 -0700235 /*
236 * Set SATA controller mode early so the resource allocator can
237 * properly assign IO/Memory resources for the controller.
238 */
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100239 if (sata_mode == 0)
Stefan Reinauer816d0812012-04-30 16:42:07 -0700240 map = 0x0060;
241
242 map |= (config->sata_port_map ^ 0x3f) << 8;
243
244 pci_write_config16(dev, 0x90, map);
245}
246
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600247static const char *sata_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200248{
249 return "SATA";
250}
251
Furquan Shaikh7536a392020-04-24 21:59:21 -0700252static void sata_fill_ssdt(const struct device *dev)
Alexander Couzens7bf47ee2015-04-16 02:00:21 +0200253{
254 config_t *config = dev->chip_info;
255 generate_sata_ssdt_ports("\\_SB_.PCI0.SATA", config->sata_port_map);
256}
257
Stefan Reinauer8e073822012-04-04 00:07:22 +0200258static struct device_operations sata_ops = {
Nico Huber63be0602019-02-13 15:11:09 +0100259 .read_resources = sata_read_resources,
260 .set_resources = sata_set_resources,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200261 .enable_resources = pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200262 .acpi_fill_ssdt = sata_fill_ssdt,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200263 .init = sata_init,
Stefan Reinauer816d0812012-04-30 16:42:07 -0700264 .enable = sata_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200265 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200266 .acpi_name = sata_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200267};
268
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700269static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03,
270 0x1e00, 0x1e01, 0x1e02, 0x1e03,
271 0 };
272
273static const struct pci_driver pch_sata __pci_driver = {
274 .ops = &sata_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100275 .vendor = PCI_VID_INTEL,
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700276 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200277};