blob: 0e7f35bce811609f6c15a7357b9652a4b1cc7066 [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älkki13f66502019-03-03 08:01:05 +020019#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010021#include <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include "i82801ix.h"
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +020026#include <pc80/mc146818rtc.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010027
28typedef struct southbridge_intel_i82801ix_config config_t;
29
30static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
31 const int is_mobile)
32{
33 int i;
34 u32 reg32;
Patrick Rudolph4af2add2018-11-26 15:56:11 +010035 struct resource *res;
Patrick Georgie72a8a32012-11-06 11:05:09 +010036
37 /* Initialize AHCI memory-mapped space */
Patrick Rudolph4af2add2018-11-26 15:56:11 +010038 res = find_resource(dev, PCI_BASE_ADDRESS_5);
39 if (!res)
40 return;
41
42 u8 *abar = res2mmio(res, 0, 0);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Patrick Georgie72a8a32012-11-06 11:05:09 +010044
45 /* Set AHCI access mode.
46 No other ABAR registers should be accessed before this. */
47 reg32 = read32(abar + 0x04);
48 reg32 |= 1 << 31;
49 write32(abar + 0x04, reg32);
50
51 /* CAP (HBA Capabilities) : enable power management */
52 reg32 = read32(abar + 0x00);
53 /* CCCS must be set. */
54 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
55 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
56 write32(abar + 0x00, reg32);
57
58 /* PI (Ports implemented) */
59 write32(abar + 0x0c, port_map);
60 /* PCH code reads back twice, do we need it, too? */
61 (void) read32(abar + 0x0c); /* Read back 1 */
62 (void) read32(abar + 0x0c); /* Read back 2 */
63
64 /* VSP (Vendor Specific Register) */
65 reg32 = read32(abar + 0xa0);
66 reg32 &= ~0x00000001; /* clear SLPD */
67 write32(abar + 0xa0, reg32);
68
69 /* Lock R/WO bits in Port command registers. */
70 for (i = 0; i < 6; ++i) {
71 if (((i == 2) || (i == 3)) && is_mobile)
72 continue;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080073 u8 *addr = abar + 0x118 + (i * 0x80);
Patrick Georgie72a8a32012-11-06 11:05:09 +010074 write32(addr, read32(addr));
75 }
76}
77
78static void sata_program_indexed(struct device *const dev, const int is_mobile)
79{
80 u32 reg32;
81
82 pci_write_config8(dev, D31F2_SIDX, 0x18);
83 reg32 = pci_read_config32(dev, D31F2_SDAT);
84 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
85 reg32 |= (3 << 3) | (3 << 0);
86 pci_write_config32(dev, D31F2_SDAT, reg32);
87
88 pci_write_config8(dev, D31F2_SIDX, 0x28);
89 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
90
91 pci_write_config8(dev, D31F2_SIDX, 0x40);
92 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
93
94 pci_write_config8(dev, D31F2_SIDX, 0x78);
95 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
96
97 if (!is_mobile) {
98 pci_write_config8(dev, D31F2_SIDX, 0x84);
99 reg32 = pci_read_config32(dev, D31F2_SDAT);
100 reg32 &= ~((7 << 3) | (7 << 0));
101 reg32 |= (3 << 3) | (3 << 0);
102 pci_write_config32(dev, D31F2_SDAT, reg32);
103 }
104
105 pci_write_config8(dev, D31F2_SIDX, 0x88);
106 reg32 = pci_read_config32(dev, D31F2_SDAT);
107 if (!is_mobile)
108 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
109 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
110 if (!is_mobile)
111 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
112 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
113 pci_write_config32(dev, D31F2_SDAT, reg32);
114
115 pci_write_config8(dev, D31F2_SIDX, 0x8c);
116 reg32 = pci_read_config32(dev, D31F2_SDAT);
117 if (!is_mobile)
118 reg32 &= ~((7 << 27) | (7 << 24));
119 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
120 if (!is_mobile)
121 reg32 |= (2 << 27) | (2 << 24);
122 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
123 pci_write_config32(dev, D31F2_SDAT, reg32);
124
125 pci_write_config8(dev, D31F2_SIDX, 0x94);
126 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
127
128 pci_write_config8(dev, D31F2_SIDX, 0xa0);
129 reg32 = pci_read_config32(dev, D31F2_SDAT);
130 reg32 &= ~((7 << 3) | (7 << 0));
131 reg32 |= (3 << 3) | (3 << 0);
132 pci_write_config32(dev, D31F2_SDAT, reg32);
133
134 pci_write_config8(dev, D31F2_SIDX, 0xa8);
135 reg32 = pci_read_config32(dev, D31F2_SDAT);
136 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
137 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
138 pci_write_config32(dev, D31F2_SDAT, reg32);
139
140 pci_write_config8(dev, D31F2_SIDX, 0xac);
141 reg32 = pci_read_config32(dev, D31F2_SDAT);
142 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
143 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
144 pci_write_config32(dev, D31F2_SDAT, reg32);
145}
146
147static void sata_init(struct device *const dev)
148{
149 u16 reg16;
150
151 /* Get the chip configuration */
152 const config_t *const config = dev->chip_info;
153
154 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
155 const int is_mobile = (devid == 0x2928) || (devid == 0x2929);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200156 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100157
158 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
159
160 if (config == NULL) {
161 printk(BIOS_ERR, "i82801ix_sata: error: "
162 "device not in devicetree.cb!\n");
163 return;
164 }
165
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200166 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
167 /* Default to AHCI */
168 sata_mode = 0;
169
Patrick Georgie72a8a32012-11-06 11:05:09 +0100170 /*
171 * TODO: In contrast to ICH7 and PCH code we don't set
172 * timings, dma and IDE-I/O settings here. Looks like they
173 * became obsolete with the fading of real IDE ports.
174 * Maybe we can safely remove those settings from PCH code and
175 * even ICH7 code if it doesn't use the feature to combine the
176 * IDE and SATA controllers.
177 */
178
179 pci_write_config16(dev, PCI_COMMAND,
180 PCI_COMMAND_MASTER |
181 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
182 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200183 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100184 /* No AHCI: clear AHCI base */
185 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
186
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200187 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100188 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
189 } else {
190 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
191
192 /* Enable native mode on both primary and secondary. */
193 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
194 }
195
196 /* Looks like we should only enable decoding here. */
197 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
198 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
199
200 /* Port enable. For AHCI, it's managed in memory mapped space. */
201 reg16 = pci_read_config16(dev, 0x92);
202 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200203 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100204 pci_write_config16(dev, 0x92, reg16);
205
206 /* SATA clock settings */
207 u32 sclkcg = 0;
208 if (config->sata_clock_request &&
209 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
210 sclkcg |= 1 << 30; /* Enable SATA clock request. */
211 /* Disable unused ports. */
212 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
213 /* Must be programmed. */
214 sclkcg |= 0x193;
215 pci_write_config32(dev, 0x94, sclkcg);
216
217 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300218 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100219 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
220 >> 3) & 3) == 3) {
221 u8 reg8 = pci_read_config8(dev, 0x9c);
222 reg8 &= ~(0x1f << 2);
223 reg8 |= 3 << 2;
224 pci_write_config8(dev, 0x9c, reg8);
225 }
226 }
227
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200228 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100229 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
230
231 sata_program_indexed(dev, is_mobile);
232}
233
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200234static void sata_enable(struct device *dev)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100235{
236 /* Get the chip configuration */
237 const config_t *const config = dev->chip_info;
238
239 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200240 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100241
242 if (!config)
243 return;
244
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200245 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
246 /* Default to AHCI */
247 sata_mode = 0;
248
Patrick Georgie72a8a32012-11-06 11:05:09 +0100249 /*
250 * Set SATA controller mode early so the resource allocator can
251 * properly assign IO/Memory resources for the controller.
252 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200253 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100254 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
255
256 map |= (config->sata_port_map ^ 0x3f) << 8;
257
258 pci_write_config16(dev, 0x90, map);
259}
260
Elyes HAOUAS8aa50732018-05-13 13:34:58 +0200261static void sata_set_subsystem(struct device *dev, unsigned vendor,
262 unsigned device)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100263{
264 if (!vendor || !device) {
265 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
266 pci_read_config32(dev, PCI_VENDOR_ID));
267 } else {
268 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
269 ((device & 0xffff) << 16) | (vendor & 0xffff));
270 }
271}
272
273static struct pci_operations sata_pci_ops = {
274 .set_subsystem = sata_set_subsystem,
275};
276
277static struct device_operations sata_ops = {
278 .read_resources = pci_dev_read_resources,
279 .set_resources = pci_dev_set_resources,
280 .enable_resources = pci_dev_enable_resources,
281 .init = sata_init,
282 .enable = sata_enable,
283 .scan_bus = 0,
284 .ops_pci = &sata_pci_ops,
285};
286
287static const unsigned short pci_device_ids[] = {
288 0x2920, 0x2921, 0x2922, 0x2923,
289 0x2928, 0x2929,
290 0,
291};
292
293static const struct pci_driver pch_sata __pci_driver = {
294 .ops = &sata_ops,
295 .vendor = PCI_VENDOR_ID_INTEL,
296 .devices = pci_device_ids,
297};