blob: b6b72e3c1bf0938d1a7888d5c84c1ca524493cf6 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Patrick Georgie72a8a32012-11-06 11:05:09 +01003
4#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02005#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +01007#include <console/console.h>
8#include <device/device.h>
9#include <device/pci.h>
10#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020011#include <option.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020012#include <types.h>
13
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030014#include "chip.h"
Elyes HAOUASab89edb2019-05-15 21:10:44 +020015#include "i82801ix.h"
Patrick Georgie72a8a32012-11-06 11:05:09 +010016
17typedef struct southbridge_intel_i82801ix_config config_t;
18
19static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
20 const int is_mobile)
21{
22 int i;
23 u32 reg32;
Patrick Rudolph4af2add2018-11-26 15:56:11 +010024 struct resource *res;
Patrick Georgie72a8a32012-11-06 11:05:09 +010025
26 /* Initialize AHCI memory-mapped space */
Patrick Rudolph4af2add2018-11-26 15:56:11 +010027 res = find_resource(dev, PCI_BASE_ADDRESS_5);
28 if (!res)
29 return;
30
31 u8 *abar = res2mmio(res, 0, 0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080032 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Patrick Georgie72a8a32012-11-06 11:05:09 +010033
34 /* Set AHCI access mode.
35 No other ABAR registers should be accessed before this. */
36 reg32 = read32(abar + 0x04);
37 reg32 |= 1 << 31;
38 write32(abar + 0x04, reg32);
39
40 /* CAP (HBA Capabilities) : enable power management */
41 reg32 = read32(abar + 0x00);
42 /* CCCS must be set. */
43 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
44 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
45 write32(abar + 0x00, reg32);
46
47 /* PI (Ports implemented) */
48 write32(abar + 0x0c, port_map);
49 /* PCH code reads back twice, do we need it, too? */
50 (void) read32(abar + 0x0c); /* Read back 1 */
51 (void) read32(abar + 0x0c); /* Read back 2 */
52
53 /* VSP (Vendor Specific Register) */
54 reg32 = read32(abar + 0xa0);
55 reg32 &= ~0x00000001; /* clear SLPD */
56 write32(abar + 0xa0, reg32);
57
58 /* Lock R/WO bits in Port command registers. */
59 for (i = 0; i < 6; ++i) {
60 if (((i == 2) || (i == 3)) && is_mobile)
61 continue;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080062 u8 *addr = abar + 0x118 + (i * 0x80);
Patrick Georgie72a8a32012-11-06 11:05:09 +010063 write32(addr, read32(addr));
64 }
65}
66
67static void sata_program_indexed(struct device *const dev, const int is_mobile)
68{
69 u32 reg32;
70
71 pci_write_config8(dev, D31F2_SIDX, 0x18);
72 reg32 = pci_read_config32(dev, D31F2_SDAT);
73 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
74 reg32 |= (3 << 3) | (3 << 0);
75 pci_write_config32(dev, D31F2_SDAT, reg32);
76
77 pci_write_config8(dev, D31F2_SIDX, 0x28);
78 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
79
80 pci_write_config8(dev, D31F2_SIDX, 0x40);
81 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
82
83 pci_write_config8(dev, D31F2_SIDX, 0x78);
84 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
85
86 if (!is_mobile) {
87 pci_write_config8(dev, D31F2_SIDX, 0x84);
88 reg32 = pci_read_config32(dev, D31F2_SDAT);
89 reg32 &= ~((7 << 3) | (7 << 0));
90 reg32 |= (3 << 3) | (3 << 0);
91 pci_write_config32(dev, D31F2_SDAT, reg32);
92 }
93
94 pci_write_config8(dev, D31F2_SIDX, 0x88);
95 reg32 = pci_read_config32(dev, D31F2_SDAT);
96 if (!is_mobile)
97 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
98 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
99 if (!is_mobile)
100 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
101 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
102 pci_write_config32(dev, D31F2_SDAT, reg32);
103
104 pci_write_config8(dev, D31F2_SIDX, 0x8c);
105 reg32 = pci_read_config32(dev, D31F2_SDAT);
106 if (!is_mobile)
107 reg32 &= ~((7 << 27) | (7 << 24));
108 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
109 if (!is_mobile)
110 reg32 |= (2 << 27) | (2 << 24);
111 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
112 pci_write_config32(dev, D31F2_SDAT, reg32);
113
114 pci_write_config8(dev, D31F2_SIDX, 0x94);
115 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
116
117 pci_write_config8(dev, D31F2_SIDX, 0xa0);
118 reg32 = pci_read_config32(dev, D31F2_SDAT);
119 reg32 &= ~((7 << 3) | (7 << 0));
120 reg32 |= (3 << 3) | (3 << 0);
121 pci_write_config32(dev, D31F2_SDAT, reg32);
122
123 pci_write_config8(dev, D31F2_SIDX, 0xa8);
124 reg32 = pci_read_config32(dev, D31F2_SDAT);
125 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
126 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
127 pci_write_config32(dev, D31F2_SDAT, reg32);
128
129 pci_write_config8(dev, D31F2_SIDX, 0xac);
130 reg32 = pci_read_config32(dev, D31F2_SDAT);
131 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
132 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
133 pci_write_config32(dev, D31F2_SDAT, reg32);
134}
135
136static void sata_init(struct device *const dev)
137{
138 u16 reg16;
139
140 /* Get the chip configuration */
141 const config_t *const config = dev->chip_info;
142
143 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
Felix Singer7f8b0cd2019-11-10 11:04:08 +0100144 const int is_mobile = (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01) ||
145 (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200146 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100147
148 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
149
150 if (config == NULL) {
151 printk(BIOS_ERR, "i82801ix_sata: error: "
152 "device not in devicetree.cb!\n");
153 return;
154 }
155
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200156 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
157 /* Default to AHCI */
158 sata_mode = 0;
159
Patrick Georgie72a8a32012-11-06 11:05:09 +0100160 /*
161 * TODO: In contrast to ICH7 and PCH code we don't set
162 * timings, dma and IDE-I/O settings here. Looks like they
163 * became obsolete with the fading of real IDE ports.
164 * Maybe we can safely remove those settings from PCH code and
165 * even ICH7 code if it doesn't use the feature to combine the
166 * IDE and SATA controllers.
167 */
168
169 pci_write_config16(dev, PCI_COMMAND,
170 PCI_COMMAND_MASTER |
171 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
172 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200173 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100174 /* No AHCI: clear AHCI base */
175 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
176
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200177 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100178 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
179 } else {
180 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
181
182 /* Enable native mode on both primary and secondary. */
183 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
184 }
185
186 /* Looks like we should only enable decoding here. */
187 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
188 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
189
190 /* Port enable. For AHCI, it's managed in memory mapped space. */
191 reg16 = pci_read_config16(dev, 0x92);
192 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200193 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100194 pci_write_config16(dev, 0x92, reg16);
195
196 /* SATA clock settings */
197 u32 sclkcg = 0;
198 if (config->sata_clock_request &&
199 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
200 sclkcg |= 1 << 30; /* Enable SATA clock request. */
201 /* Disable unused ports. */
202 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
203 /* Must be programmed. */
204 sclkcg |= 0x193;
205 pci_write_config32(dev, 0x94, sclkcg);
206
207 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300208 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100209 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
210 >> 3) & 3) == 3) {
211 u8 reg8 = pci_read_config8(dev, 0x9c);
212 reg8 &= ~(0x1f << 2);
213 reg8 |= 3 << 2;
214 pci_write_config8(dev, 0x9c, reg8);
215 }
216 }
217
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200218 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100219 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
220
221 sata_program_indexed(dev, is_mobile);
222}
223
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200224static void sata_enable(struct device *dev)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100225{
226 /* Get the chip configuration */
227 const config_t *const config = dev->chip_info;
228
229 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200230 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100231
232 if (!config)
233 return;
234
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200235 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
236 /* Default to AHCI */
237 sata_mode = 0;
238
Patrick Georgie72a8a32012-11-06 11:05:09 +0100239 /*
240 * Set SATA controller mode early so the resource allocator can
241 * properly assign IO/Memory resources for the controller.
242 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200243 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100244 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
245
246 map |= (config->sata_port_map ^ 0x3f) << 8;
247
248 pci_write_config16(dev, 0x90, map);
249}
250
Patrick Georgie72a8a32012-11-06 11:05:09 +0100251static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530252 .set_subsystem = pci_dev_set_subsystem,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100253};
254
255static struct device_operations sata_ops = {
256 .read_resources = pci_dev_read_resources,
257 .set_resources = pci_dev_set_resources,
258 .enable_resources = pci_dev_enable_resources,
259 .init = sata_init,
260 .enable = sata_enable,
261 .scan_bus = 0,
262 .ops_pci = &sata_pci_ops,
263};
264
265static const unsigned short pci_device_ids[] = {
Felix Singer7f8b0cd2019-11-10 11:04:08 +0100266 PCI_DEVICE_ID_INTEL_82801IB_SATA_P0123,
267 PCI_DEVICE_ID_INTEL_82801IB_SATA_P01,
268 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI1,
269 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI2,
270 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01,
271 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100272 0,
273};
274
275static const struct pci_driver pch_sata __pci_driver = {
276 .ops = &sata_ops,
277 .vendor = PCI_VENDOR_ID_INTEL,
278 .devices = pci_device_ids,
279};