blob: 65422c83444577502a07ca86ff71d785b9dfc6d5 [file] [log] [blame]
Patrick Georgie72a8a32012-11-06 11:05:09 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * 2012 secunet Security Networks AG
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Patrick Georgie72a8a32012-11-06 11:05:09 +010016 */
17
18#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010020#include <console/console.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include "i82801ix.h"
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +020025#include <pc80/mc146818rtc.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010026
27typedef struct southbridge_intel_i82801ix_config config_t;
28
29static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
30 const int is_mobile)
31{
32 int i;
33 u32 reg32;
Patrick Rudolph4af2add2018-11-26 15:56:11 +010034 struct resource *res;
Patrick Georgie72a8a32012-11-06 11:05:09 +010035
36 /* Initialize AHCI memory-mapped space */
Patrick Rudolph4af2add2018-11-26 15:56:11 +010037 res = find_resource(dev, PCI_BASE_ADDRESS_5);
38 if (!res)
39 return;
40
41 u8 *abar = res2mmio(res, 0, 0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Patrick Georgie72a8a32012-11-06 11:05:09 +010043
44 /* Set AHCI access mode.
45 No other ABAR registers should be accessed before this. */
46 reg32 = read32(abar + 0x04);
47 reg32 |= 1 << 31;
48 write32(abar + 0x04, reg32);
49
50 /* CAP (HBA Capabilities) : enable power management */
51 reg32 = read32(abar + 0x00);
52 /* CCCS must be set. */
53 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
54 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
55 write32(abar + 0x00, reg32);
56
57 /* PI (Ports implemented) */
58 write32(abar + 0x0c, port_map);
59 /* PCH code reads back twice, do we need it, too? */
60 (void) read32(abar + 0x0c); /* Read back 1 */
61 (void) read32(abar + 0x0c); /* Read back 2 */
62
63 /* VSP (Vendor Specific Register) */
64 reg32 = read32(abar + 0xa0);
65 reg32 &= ~0x00000001; /* clear SLPD */
66 write32(abar + 0xa0, reg32);
67
68 /* Lock R/WO bits in Port command registers. */
69 for (i = 0; i < 6; ++i) {
70 if (((i == 2) || (i == 3)) && is_mobile)
71 continue;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080072 u8 *addr = abar + 0x118 + (i * 0x80);
Patrick Georgie72a8a32012-11-06 11:05:09 +010073 write32(addr, read32(addr));
74 }
75}
76
77static void sata_program_indexed(struct device *const dev, const int is_mobile)
78{
79 u32 reg32;
80
81 pci_write_config8(dev, D31F2_SIDX, 0x18);
82 reg32 = pci_read_config32(dev, D31F2_SDAT);
83 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
84 reg32 |= (3 << 3) | (3 << 0);
85 pci_write_config32(dev, D31F2_SDAT, reg32);
86
87 pci_write_config8(dev, D31F2_SIDX, 0x28);
88 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
89
90 pci_write_config8(dev, D31F2_SIDX, 0x40);
91 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
92
93 pci_write_config8(dev, D31F2_SIDX, 0x78);
94 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
95
96 if (!is_mobile) {
97 pci_write_config8(dev, D31F2_SIDX, 0x84);
98 reg32 = pci_read_config32(dev, D31F2_SDAT);
99 reg32 &= ~((7 << 3) | (7 << 0));
100 reg32 |= (3 << 3) | (3 << 0);
101 pci_write_config32(dev, D31F2_SDAT, reg32);
102 }
103
104 pci_write_config8(dev, D31F2_SIDX, 0x88);
105 reg32 = pci_read_config32(dev, D31F2_SDAT);
106 if (!is_mobile)
107 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
108 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
109 if (!is_mobile)
110 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
111 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
112 pci_write_config32(dev, D31F2_SDAT, reg32);
113
114 pci_write_config8(dev, D31F2_SIDX, 0x8c);
115 reg32 = pci_read_config32(dev, D31F2_SDAT);
116 if (!is_mobile)
117 reg32 &= ~((7 << 27) | (7 << 24));
118 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
119 if (!is_mobile)
120 reg32 |= (2 << 27) | (2 << 24);
121 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
122 pci_write_config32(dev, D31F2_SDAT, reg32);
123
124 pci_write_config8(dev, D31F2_SIDX, 0x94);
125 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
126
127 pci_write_config8(dev, D31F2_SIDX, 0xa0);
128 reg32 = pci_read_config32(dev, D31F2_SDAT);
129 reg32 &= ~((7 << 3) | (7 << 0));
130 reg32 |= (3 << 3) | (3 << 0);
131 pci_write_config32(dev, D31F2_SDAT, reg32);
132
133 pci_write_config8(dev, D31F2_SIDX, 0xa8);
134 reg32 = pci_read_config32(dev, D31F2_SDAT);
135 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
136 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
137 pci_write_config32(dev, D31F2_SDAT, reg32);
138
139 pci_write_config8(dev, D31F2_SIDX, 0xac);
140 reg32 = pci_read_config32(dev, D31F2_SDAT);
141 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
142 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
143 pci_write_config32(dev, D31F2_SDAT, reg32);
144}
145
146static void sata_init(struct device *const dev)
147{
148 u16 reg16;
149
150 /* Get the chip configuration */
151 const config_t *const config = dev->chip_info;
152
153 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
154 const int is_mobile = (devid == 0x2928) || (devid == 0x2929);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200155 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100156
157 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
158
159 if (config == NULL) {
160 printk(BIOS_ERR, "i82801ix_sata: error: "
161 "device not in devicetree.cb!\n");
162 return;
163 }
164
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200165 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
166 /* Default to AHCI */
167 sata_mode = 0;
168
Patrick Georgie72a8a32012-11-06 11:05:09 +0100169 /*
170 * TODO: In contrast to ICH7 and PCH code we don't set
171 * timings, dma and IDE-I/O settings here. Looks like they
172 * became obsolete with the fading of real IDE ports.
173 * Maybe we can safely remove those settings from PCH code and
174 * even ICH7 code if it doesn't use the feature to combine the
175 * IDE and SATA controllers.
176 */
177
178 pci_write_config16(dev, PCI_COMMAND,
179 PCI_COMMAND_MASTER |
180 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
181 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200182 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100183 /* No AHCI: clear AHCI base */
184 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
185
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200186 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100187 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
188 } else {
189 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
190
191 /* Enable native mode on both primary and secondary. */
192 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
193 }
194
195 /* Looks like we should only enable decoding here. */
196 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
197 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
198
199 /* Port enable. For AHCI, it's managed in memory mapped space. */
200 reg16 = pci_read_config16(dev, 0x92);
201 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200202 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100203 pci_write_config16(dev, 0x92, reg16);
204
205 /* SATA clock settings */
206 u32 sclkcg = 0;
207 if (config->sata_clock_request &&
208 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
209 sclkcg |= 1 << 30; /* Enable SATA clock request. */
210 /* Disable unused ports. */
211 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
212 /* Must be programmed. */
213 sclkcg |= 0x193;
214 pci_write_config32(dev, 0x94, sclkcg);
215
216 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300217 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100218 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
219 >> 3) & 3) == 3) {
220 u8 reg8 = pci_read_config8(dev, 0x9c);
221 reg8 &= ~(0x1f << 2);
222 reg8 |= 3 << 2;
223 pci_write_config8(dev, 0x9c, reg8);
224 }
225 }
226
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200227 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100228 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
229
230 sata_program_indexed(dev, is_mobile);
231}
232
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200233static void sata_enable(struct device *dev)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100234{
235 /* Get the chip configuration */
236 const config_t *const config = dev->chip_info;
237
238 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200239 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100240
241 if (!config)
242 return;
243
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200244 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
245 /* Default to AHCI */
246 sata_mode = 0;
247
Patrick Georgie72a8a32012-11-06 11:05:09 +0100248 /*
249 * Set SATA controller mode early so the resource allocator can
250 * properly assign IO/Memory resources for the controller.
251 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200252 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100253 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
254
255 map |= (config->sata_port_map ^ 0x3f) << 8;
256
257 pci_write_config16(dev, 0x90, map);
258}
259
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200260static void sata_set_subsystem(struct device *dev, unsigned vendor,
261 unsigned device)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100262{
263 if (!vendor || !device) {
264 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
265 pci_read_config32(dev, PCI_VENDOR_ID));
266 } else {
267 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
268 ((device & 0xffff) << 16) | (vendor & 0xffff));
269 }
270}
271
272static struct pci_operations sata_pci_ops = {
273 .set_subsystem = sata_set_subsystem,
274};
275
276static struct device_operations sata_ops = {
277 .read_resources = pci_dev_read_resources,
278 .set_resources = pci_dev_set_resources,
279 .enable_resources = pci_dev_enable_resources,
280 .init = sata_init,
281 .enable = sata_enable,
282 .scan_bus = 0,
283 .ops_pci = &sata_pci_ops,
284};
285
286static const unsigned short pci_device_ids[] = {
287 0x2920, 0x2921, 0x2922, 0x2923,
288 0x2928, 0x2929,
289 0,
290};
291
292static const struct pci_driver pch_sata __pci_driver = {
293 .ops = &sata_ops,
294 .vendor = PCI_VENDOR_ID_INTEL,
295 .devices = pci_device_ids,
296};