blob: 390c67765b88aa601875af8bf421e37cd5372f6c [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgie72a8a32012-11-06 11:05:09 +01002
3#include <arch/io.h>
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>
Patrick Georgie72a8a32012-11-06 11:05:09 +01006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020010#include <option.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 "i82801ix.h"
Patrick Georgie72a8a32012-11-06 11:05:09 +010015
16typedef struct southbridge_intel_i82801ix_config config_t;
17
18static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
19 const int is_mobile)
20{
21 int i;
22 u32 reg32;
Patrick Rudolph4af2add2018-11-26 15:56:11 +010023 struct resource *res;
Patrick Georgie72a8a32012-11-06 11:05:09 +010024
25 /* Initialize AHCI memory-mapped space */
Patrick Rudolph4af2add2018-11-26 15:56:11 +010026 res = find_resource(dev, PCI_BASE_ADDRESS_5);
27 if (!res)
28 return;
29
30 u8 *abar = res2mmio(res, 0, 0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080031 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Patrick Georgie72a8a32012-11-06 11:05:09 +010032
33 /* Set AHCI access mode.
34 No other ABAR registers should be accessed before this. */
35 reg32 = read32(abar + 0x04);
36 reg32 |= 1 << 31;
37 write32(abar + 0x04, reg32);
38
39 /* CAP (HBA Capabilities) : enable power management */
40 reg32 = read32(abar + 0x00);
41 /* CCCS must be set. */
42 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
43 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
44 write32(abar + 0x00, reg32);
45
46 /* PI (Ports implemented) */
47 write32(abar + 0x0c, port_map);
48 /* PCH code reads back twice, do we need it, too? */
49 (void) read32(abar + 0x0c); /* Read back 1 */
50 (void) read32(abar + 0x0c); /* Read back 2 */
51
52 /* VSP (Vendor Specific Register) */
53 reg32 = read32(abar + 0xa0);
54 reg32 &= ~0x00000001; /* clear SLPD */
55 write32(abar + 0xa0, reg32);
56
57 /* Lock R/WO bits in Port command registers. */
58 for (i = 0; i < 6; ++i) {
59 if (((i == 2) || (i == 3)) && is_mobile)
60 continue;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080061 u8 *addr = abar + 0x118 + (i * 0x80);
Patrick Georgie72a8a32012-11-06 11:05:09 +010062 write32(addr, read32(addr));
63 }
64}
65
66static void sata_program_indexed(struct device *const dev, const int is_mobile)
67{
68 u32 reg32;
69
70 pci_write_config8(dev, D31F2_SIDX, 0x18);
71 reg32 = pci_read_config32(dev, D31F2_SDAT);
72 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
73 reg32 |= (3 << 3) | (3 << 0);
74 pci_write_config32(dev, D31F2_SDAT, reg32);
75
76 pci_write_config8(dev, D31F2_SIDX, 0x28);
77 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
78
79 pci_write_config8(dev, D31F2_SIDX, 0x40);
80 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
81
82 pci_write_config8(dev, D31F2_SIDX, 0x78);
83 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
84
85 if (!is_mobile) {
86 pci_write_config8(dev, D31F2_SIDX, 0x84);
87 reg32 = pci_read_config32(dev, D31F2_SDAT);
88 reg32 &= ~((7 << 3) | (7 << 0));
89 reg32 |= (3 << 3) | (3 << 0);
90 pci_write_config32(dev, D31F2_SDAT, reg32);
91 }
92
93 pci_write_config8(dev, D31F2_SIDX, 0x88);
94 reg32 = pci_read_config32(dev, D31F2_SDAT);
95 if (!is_mobile)
96 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
97 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
98 if (!is_mobile)
99 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
100 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
101 pci_write_config32(dev, D31F2_SDAT, reg32);
102
103 pci_write_config8(dev, D31F2_SIDX, 0x8c);
104 reg32 = pci_read_config32(dev, D31F2_SDAT);
105 if (!is_mobile)
106 reg32 &= ~((7 << 27) | (7 << 24));
107 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
108 if (!is_mobile)
109 reg32 |= (2 << 27) | (2 << 24);
110 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
111 pci_write_config32(dev, D31F2_SDAT, reg32);
112
113 pci_write_config8(dev, D31F2_SIDX, 0x94);
114 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
115
116 pci_write_config8(dev, D31F2_SIDX, 0xa0);
117 reg32 = pci_read_config32(dev, D31F2_SDAT);
118 reg32 &= ~((7 << 3) | (7 << 0));
119 reg32 |= (3 << 3) | (3 << 0);
120 pci_write_config32(dev, D31F2_SDAT, reg32);
121
122 pci_write_config8(dev, D31F2_SIDX, 0xa8);
123 reg32 = pci_read_config32(dev, D31F2_SDAT);
124 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
125 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
126 pci_write_config32(dev, D31F2_SDAT, reg32);
127
128 pci_write_config8(dev, D31F2_SIDX, 0xac);
129 reg32 = pci_read_config32(dev, D31F2_SDAT);
130 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
131 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
132 pci_write_config32(dev, D31F2_SDAT, reg32);
133}
134
135static void sata_init(struct device *const dev)
136{
137 u16 reg16;
138
139 /* Get the chip configuration */
140 const config_t *const config = dev->chip_info;
141
142 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
Felix Singer7f8b0cd2019-11-10 11:04:08 +0100143 const int is_mobile = (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01) ||
144 (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200145 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100146
147 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
148
149 if (config == NULL) {
150 printk(BIOS_ERR, "i82801ix_sata: error: "
151 "device not in devicetree.cb!\n");
152 return;
153 }
154
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200155 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
156 /* Default to AHCI */
157 sata_mode = 0;
158
Patrick Georgie72a8a32012-11-06 11:05:09 +0100159 /*
160 * TODO: In contrast to ICH7 and PCH code we don't set
161 * timings, dma and IDE-I/O settings here. Looks like they
162 * became obsolete with the fading of real IDE ports.
163 * Maybe we can safely remove those settings from PCH code and
164 * even ICH7 code if it doesn't use the feature to combine the
165 * IDE and SATA controllers.
166 */
167
168 pci_write_config16(dev, PCI_COMMAND,
169 PCI_COMMAND_MASTER |
170 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
171 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200172 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100173 /* No AHCI: clear AHCI base */
174 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
175
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200176 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100177 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
178 } else {
179 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
180
181 /* Enable native mode on both primary and secondary. */
182 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
183 }
184
185 /* Looks like we should only enable decoding here. */
186 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
187 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
188
189 /* Port enable. For AHCI, it's managed in memory mapped space. */
190 reg16 = pci_read_config16(dev, 0x92);
191 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200192 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100193 pci_write_config16(dev, 0x92, reg16);
194
195 /* SATA clock settings */
196 u32 sclkcg = 0;
197 if (config->sata_clock_request &&
198 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
199 sclkcg |= 1 << 30; /* Enable SATA clock request. */
200 /* Disable unused ports. */
201 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
202 /* Must be programmed. */
203 sclkcg |= 0x193;
204 pci_write_config32(dev, 0x94, sclkcg);
205
206 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300207 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100208 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
209 >> 3) & 3) == 3) {
210 u8 reg8 = pci_read_config8(dev, 0x9c);
211 reg8 &= ~(0x1f << 2);
212 reg8 |= 3 << 2;
213 pci_write_config8(dev, 0x9c, reg8);
214 }
215 }
216
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200217 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100218 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
219
220 sata_program_indexed(dev, is_mobile);
221}
222
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200223static void sata_enable(struct device *dev)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100224{
225 /* Get the chip configuration */
226 const config_t *const config = dev->chip_info;
227
228 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200229 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100230
231 if (!config)
232 return;
233
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200234 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
235 /* Default to AHCI */
236 sata_mode = 0;
237
Patrick Georgie72a8a32012-11-06 11:05:09 +0100238 /*
239 * Set SATA controller mode early so the resource allocator can
240 * properly assign IO/Memory resources for the controller.
241 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200242 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100243 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
244
245 map |= (config->sata_port_map ^ 0x3f) << 8;
246
247 pci_write_config16(dev, 0x90, map);
248}
249
Patrick Georgie72a8a32012-11-06 11:05:09 +0100250static struct device_operations sata_ops = {
251 .read_resources = pci_dev_read_resources,
252 .set_resources = pci_dev_set_resources,
253 .enable_resources = pci_dev_enable_resources,
254 .init = sata_init,
255 .enable = sata_enable,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200256 .ops_pci = &pci_dev_ops_pci,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100257};
258
259static const unsigned short pci_device_ids[] = {
Felix Singer7f8b0cd2019-11-10 11:04:08 +0100260 PCI_DEVICE_ID_INTEL_82801IB_SATA_P0123,
261 PCI_DEVICE_ID_INTEL_82801IB_SATA_P01,
262 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI1,
263 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI2,
264 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01,
265 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100266 0,
267};
268
269static const struct pci_driver pch_sata __pci_driver = {
270 .ops = &sata_ops,
271 .vendor = PCI_VENDOR_ID_INTEL,
272 .devices = pci_device_ids,
273};