blob: 49d60f285f1bf6454d9d1ab580f74f729d8d4954 [file] [log] [blame]
Patrick Georgie72a8a32012-11-06 11:05:09 +01001/*
2 * This file is part of the coreboot project.
3 *
Patrick Georgie72a8a32012-11-06 11:05:09 +01004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; version 2 of
8 * the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Patrick Georgie72a8a32012-11-06 11:05:09 +010014 */
15
16#include <arch/io.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020017#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020018#include <device/pci_ops.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010019#include <console/console.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020023#include <option.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020024#include <types.h>
25
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030026#include "chip.h"
Elyes HAOUASab89edb2019-05-15 21:10:44 +020027#include "i82801ix.h"
Patrick Georgie72a8a32012-11-06 11:05:09 +010028
29typedef struct southbridge_intel_i82801ix_config config_t;
30
31static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
32 const int is_mobile)
33{
34 int i;
35 u32 reg32;
Patrick Rudolph4af2add2018-11-26 15:56:11 +010036 struct resource *res;
Patrick Georgie72a8a32012-11-06 11:05:09 +010037
38 /* Initialize AHCI memory-mapped space */
Patrick Rudolph4af2add2018-11-26 15:56:11 +010039 res = find_resource(dev, PCI_BASE_ADDRESS_5);
40 if (!res)
41 return;
42
43 u8 *abar = res2mmio(res, 0, 0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Patrick Georgie72a8a32012-11-06 11:05:09 +010045
46 /* Set AHCI access mode.
47 No other ABAR registers should be accessed before this. */
48 reg32 = read32(abar + 0x04);
49 reg32 |= 1 << 31;
50 write32(abar + 0x04, reg32);
51
52 /* CAP (HBA Capabilities) : enable power management */
53 reg32 = read32(abar + 0x00);
54 /* CCCS must be set. */
55 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
56 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
57 write32(abar + 0x00, reg32);
58
59 /* PI (Ports implemented) */
60 write32(abar + 0x0c, port_map);
61 /* PCH code reads back twice, do we need it, too? */
62 (void) read32(abar + 0x0c); /* Read back 1 */
63 (void) read32(abar + 0x0c); /* Read back 2 */
64
65 /* VSP (Vendor Specific Register) */
66 reg32 = read32(abar + 0xa0);
67 reg32 &= ~0x00000001; /* clear SLPD */
68 write32(abar + 0xa0, reg32);
69
70 /* Lock R/WO bits in Port command registers. */
71 for (i = 0; i < 6; ++i) {
72 if (((i == 2) || (i == 3)) && is_mobile)
73 continue;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080074 u8 *addr = abar + 0x118 + (i * 0x80);
Patrick Georgie72a8a32012-11-06 11:05:09 +010075 write32(addr, read32(addr));
76 }
77}
78
79static void sata_program_indexed(struct device *const dev, const int is_mobile)
80{
81 u32 reg32;
82
83 pci_write_config8(dev, D31F2_SIDX, 0x18);
84 reg32 = pci_read_config32(dev, D31F2_SDAT);
85 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
86 reg32 |= (3 << 3) | (3 << 0);
87 pci_write_config32(dev, D31F2_SDAT, reg32);
88
89 pci_write_config8(dev, D31F2_SIDX, 0x28);
90 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
91
92 pci_write_config8(dev, D31F2_SIDX, 0x40);
93 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
94
95 pci_write_config8(dev, D31F2_SIDX, 0x78);
96 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
97
98 if (!is_mobile) {
99 pci_write_config8(dev, D31F2_SIDX, 0x84);
100 reg32 = pci_read_config32(dev, D31F2_SDAT);
101 reg32 &= ~((7 << 3) | (7 << 0));
102 reg32 |= (3 << 3) | (3 << 0);
103 pci_write_config32(dev, D31F2_SDAT, reg32);
104 }
105
106 pci_write_config8(dev, D31F2_SIDX, 0x88);
107 reg32 = pci_read_config32(dev, D31F2_SDAT);
108 if (!is_mobile)
109 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
110 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
111 if (!is_mobile)
112 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
113 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
114 pci_write_config32(dev, D31F2_SDAT, reg32);
115
116 pci_write_config8(dev, D31F2_SIDX, 0x8c);
117 reg32 = pci_read_config32(dev, D31F2_SDAT);
118 if (!is_mobile)
119 reg32 &= ~((7 << 27) | (7 << 24));
120 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
121 if (!is_mobile)
122 reg32 |= (2 << 27) | (2 << 24);
123 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
124 pci_write_config32(dev, D31F2_SDAT, reg32);
125
126 pci_write_config8(dev, D31F2_SIDX, 0x94);
127 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
128
129 pci_write_config8(dev, D31F2_SIDX, 0xa0);
130 reg32 = pci_read_config32(dev, D31F2_SDAT);
131 reg32 &= ~((7 << 3) | (7 << 0));
132 reg32 |= (3 << 3) | (3 << 0);
133 pci_write_config32(dev, D31F2_SDAT, reg32);
134
135 pci_write_config8(dev, D31F2_SIDX, 0xa8);
136 reg32 = pci_read_config32(dev, D31F2_SDAT);
137 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
138 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
139 pci_write_config32(dev, D31F2_SDAT, reg32);
140
141 pci_write_config8(dev, D31F2_SIDX, 0xac);
142 reg32 = pci_read_config32(dev, D31F2_SDAT);
143 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
144 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
145 pci_write_config32(dev, D31F2_SDAT, reg32);
146}
147
148static void sata_init(struct device *const dev)
149{
150 u16 reg16;
151
152 /* Get the chip configuration */
153 const config_t *const config = dev->chip_info;
154
155 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
Felix Singer7f8b0cd82019-11-10 11:04:08 +0100156 const int is_mobile = (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01) ||
157 (devid == PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200158 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100159
160 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
161
162 if (config == NULL) {
163 printk(BIOS_ERR, "i82801ix_sata: error: "
164 "device not in devicetree.cb!\n");
165 return;
166 }
167
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200168 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
169 /* Default to AHCI */
170 sata_mode = 0;
171
Patrick Georgie72a8a32012-11-06 11:05:09 +0100172 /*
173 * TODO: In contrast to ICH7 and PCH code we don't set
174 * timings, dma and IDE-I/O settings here. Looks like they
175 * became obsolete with the fading of real IDE ports.
176 * Maybe we can safely remove those settings from PCH code and
177 * even ICH7 code if it doesn't use the feature to combine the
178 * IDE and SATA controllers.
179 */
180
181 pci_write_config16(dev, PCI_COMMAND,
182 PCI_COMMAND_MASTER |
183 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
184 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200185 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100186 /* No AHCI: clear AHCI base */
187 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
188
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200189 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100190 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
191 } else {
192 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
193
194 /* Enable native mode on both primary and secondary. */
195 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
196 }
197
198 /* Looks like we should only enable decoding here. */
199 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
200 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
201
202 /* Port enable. For AHCI, it's managed in memory mapped space. */
203 reg16 = pci_read_config16(dev, 0x92);
204 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200205 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100206 pci_write_config16(dev, 0x92, reg16);
207
208 /* SATA clock settings */
209 u32 sclkcg = 0;
210 if (config->sata_clock_request &&
211 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
212 sclkcg |= 1 << 30; /* Enable SATA clock request. */
213 /* Disable unused ports. */
214 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
215 /* Must be programmed. */
216 sclkcg |= 0x193;
217 pci_write_config32(dev, 0x94, sclkcg);
218
219 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300220 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100221 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
222 >> 3) & 3) == 3) {
223 u8 reg8 = pci_read_config8(dev, 0x9c);
224 reg8 &= ~(0x1f << 2);
225 reg8 |= 3 << 2;
226 pci_write_config8(dev, 0x9c, reg8);
227 }
228 }
229
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200230 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100231 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
232
233 sata_program_indexed(dev, is_mobile);
234}
235
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200236static void sata_enable(struct device *dev)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100237{
238 /* Get the chip configuration */
239 const config_t *const config = dev->chip_info;
240
241 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200242 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100243
244 if (!config)
245 return;
246
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200247 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
248 /* Default to AHCI */
249 sata_mode = 0;
250
Patrick Georgie72a8a32012-11-06 11:05:09 +0100251 /*
252 * Set SATA controller mode early so the resource allocator can
253 * properly assign IO/Memory resources for the controller.
254 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200255 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100256 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
257
258 map |= (config->sata_port_map ^ 0x3f) << 8;
259
260 pci_write_config16(dev, 0x90, map);
261}
262
Patrick Georgie72a8a32012-11-06 11:05:09 +0100263static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530264 .set_subsystem = pci_dev_set_subsystem,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100265};
266
267static struct device_operations sata_ops = {
268 .read_resources = pci_dev_read_resources,
269 .set_resources = pci_dev_set_resources,
270 .enable_resources = pci_dev_enable_resources,
271 .init = sata_init,
272 .enable = sata_enable,
273 .scan_bus = 0,
274 .ops_pci = &sata_pci_ops,
275};
276
277static const unsigned short pci_device_ids[] = {
Felix Singer7f8b0cd82019-11-10 11:04:08 +0100278 PCI_DEVICE_ID_INTEL_82801IB_SATA_P0123,
279 PCI_DEVICE_ID_INTEL_82801IB_SATA_P01,
280 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI1,
281 PCI_DEVICE_ID_INTEL_82801IB_SATA_AHCI2,
282 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_IDE_P01,
283 PCI_DEVICE_ID_INTEL_82801IBM_IEM_SATA_AHCI_P0145,
Patrick Georgie72a8a32012-11-06 11:05:09 +0100284 0,
285};
286
287static const struct pci_driver pch_sata __pci_driver = {
288 .ops = &sata_ops,
289 .vendor = PCI_VENDOR_ID_INTEL,
290 .devices = pci_device_ids,
291};