blob: 10c8a2bd1586c92e01ae02c106b0d6d454599772 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Patrick Georgie72a8a32012-11-06 11:05:09 +010020 */
21
22#include <arch/io.h>
23#include <console/console.h>
24#include <device/device.h>
25#include <device/pci.h>
26#include <device/pci_ids.h>
27#include "i82801ix.h"
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +020028#include <pc80/mc146818rtc.h>
Patrick Georgie72a8a32012-11-06 11:05:09 +010029
30typedef struct southbridge_intel_i82801ix_config config_t;
31
32static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
33 const int is_mobile)
34{
35 int i;
36 u32 reg32;
37
38 /* Initialize AHCI memory-mapped space */
39 const u32 abar = pci_read_config32(dev, PCI_BASE_ADDRESS_5);
40 printk(BIOS_DEBUG, "ABAR: %08X\n", abar);
41
42 /* Set AHCI access mode.
43 No other ABAR registers should be accessed before this. */
44 reg32 = read32(abar + 0x04);
45 reg32 |= 1 << 31;
46 write32(abar + 0x04, reg32);
47
48 /* CAP (HBA Capabilities) : enable power management */
49 reg32 = read32(abar + 0x00);
50 /* CCCS must be set. */
51 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
52 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
53 write32(abar + 0x00, reg32);
54
55 /* PI (Ports implemented) */
56 write32(abar + 0x0c, port_map);
57 /* PCH code reads back twice, do we need it, too? */
58 (void) read32(abar + 0x0c); /* Read back 1 */
59 (void) read32(abar + 0x0c); /* Read back 2 */
60
61 /* VSP (Vendor Specific Register) */
62 reg32 = read32(abar + 0xa0);
63 reg32 &= ~0x00000001; /* clear SLPD */
64 write32(abar + 0xa0, reg32);
65
66 /* Lock R/WO bits in Port command registers. */
67 for (i = 0; i < 6; ++i) {
68 if (((i == 2) || (i == 3)) && is_mobile)
69 continue;
70 const u32 addr = abar + 0x118 + (i * 0x80);
71 write32(addr, read32(addr));
72 }
73}
74
75static void sata_program_indexed(struct device *const dev, const int is_mobile)
76{
77 u32 reg32;
78
79 pci_write_config8(dev, D31F2_SIDX, 0x18);
80 reg32 = pci_read_config32(dev, D31F2_SDAT);
81 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
82 reg32 |= (3 << 3) | (3 << 0);
83 pci_write_config32(dev, D31F2_SDAT, reg32);
84
85 pci_write_config8(dev, D31F2_SIDX, 0x28);
86 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
87
88 pci_write_config8(dev, D31F2_SIDX, 0x40);
89 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
90
91 pci_write_config8(dev, D31F2_SIDX, 0x78);
92 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
93
94 if (!is_mobile) {
95 pci_write_config8(dev, D31F2_SIDX, 0x84);
96 reg32 = pci_read_config32(dev, D31F2_SDAT);
97 reg32 &= ~((7 << 3) | (7 << 0));
98 reg32 |= (3 << 3) | (3 << 0);
99 pci_write_config32(dev, D31F2_SDAT, reg32);
100 }
101
102 pci_write_config8(dev, D31F2_SIDX, 0x88);
103 reg32 = pci_read_config32(dev, D31F2_SDAT);
104 if (!is_mobile)
105 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
106 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
107 if (!is_mobile)
108 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
109 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
110 pci_write_config32(dev, D31F2_SDAT, reg32);
111
112 pci_write_config8(dev, D31F2_SIDX, 0x8c);
113 reg32 = pci_read_config32(dev, D31F2_SDAT);
114 if (!is_mobile)
115 reg32 &= ~((7 << 27) | (7 << 24));
116 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
117 if (!is_mobile)
118 reg32 |= (2 << 27) | (2 << 24);
119 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
120 pci_write_config32(dev, D31F2_SDAT, reg32);
121
122 pci_write_config8(dev, D31F2_SIDX, 0x94);
123 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
124
125 pci_write_config8(dev, D31F2_SIDX, 0xa0);
126 reg32 = pci_read_config32(dev, D31F2_SDAT);
127 reg32 &= ~((7 << 3) | (7 << 0));
128 reg32 |= (3 << 3) | (3 << 0);
129 pci_write_config32(dev, D31F2_SDAT, reg32);
130
131 pci_write_config8(dev, D31F2_SIDX, 0xa8);
132 reg32 = pci_read_config32(dev, D31F2_SDAT);
133 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
134 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
135 pci_write_config32(dev, D31F2_SDAT, reg32);
136
137 pci_write_config8(dev, D31F2_SIDX, 0xac);
138 reg32 = pci_read_config32(dev, D31F2_SDAT);
139 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
140 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
141 pci_write_config32(dev, D31F2_SDAT, reg32);
142}
143
144static void sata_init(struct device *const dev)
145{
146 u16 reg16;
147
148 /* Get the chip configuration */
149 const config_t *const config = dev->chip_info;
150
151 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
152 const int is_mobile = (devid == 0x2928) || (devid == 0x2929);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200153 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100154
155 printk(BIOS_DEBUG, "i82801ix_sata: initializing...\n");
156
157 if (config == NULL) {
158 printk(BIOS_ERR, "i82801ix_sata: error: "
159 "device not in devicetree.cb!\n");
160 return;
161 }
162
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200163 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
164 /* Default to AHCI */
165 sata_mode = 0;
166
Patrick Georgie72a8a32012-11-06 11:05:09 +0100167 /*
168 * TODO: In contrast to ICH7 and PCH code we don't set
169 * timings, dma and IDE-I/O settings here. Looks like they
170 * became obsolete with the fading of real IDE ports.
171 * Maybe we can safely remove those settings from PCH code and
172 * even ICH7 code if it doesn't use the feature to combine the
173 * IDE and SATA controllers.
174 */
175
176 pci_write_config16(dev, PCI_COMMAND,
177 PCI_COMMAND_MASTER |
178 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
179 PCI_COMMAND_IO);
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200180 if (sata_mode != 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100181 /* No AHCI: clear AHCI base */
182 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
183
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200184 if (sata_mode == 0) {
Patrick Georgie72a8a32012-11-06 11:05:09 +0100185 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
186 } else {
187 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
188
189 /* Enable native mode on both primary and secondary. */
190 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
191 }
192
193 /* Looks like we should only enable decoding here. */
194 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
195 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
196
197 /* Port enable. For AHCI, it's managed in memory mapped space. */
198 reg16 = pci_read_config16(dev, 0x92);
199 reg16 &= ~0x3f;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200200 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
Patrick Georgie72a8a32012-11-06 11:05:09 +0100201 pci_write_config16(dev, 0x92, reg16);
202
203 /* SATA clock settings */
204 u32 sclkcg = 0;
205 if (config->sata_clock_request &&
206 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
207 sclkcg |= 1 << 30; /* Enable SATA clock request. */
208 /* Disable unused ports. */
209 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
210 /* Must be programmed. */
211 sclkcg |= 0x193;
212 pci_write_config32(dev, 0x94, sclkcg);
213
214 if (is_mobile && config->sata_traffic_monitor) {
215 const device_t lpc_dev = dev_find_slot(0, PCI_DEVFN(0x1f, 0));
216 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
217 >> 3) & 3) == 3) {
218 u8 reg8 = pci_read_config8(dev, 0x9c);
219 reg8 &= ~(0x1f << 2);
220 reg8 |= 3 << 2;
221 pci_write_config8(dev, 0x9c, reg8);
222 }
223 }
224
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200225 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100226 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
227
228 sata_program_indexed(dev, is_mobile);
229}
230
231static void sata_enable(device_t dev)
232{
233 /* Get the chip configuration */
234 const config_t *const config = dev->chip_info;
235
236 u16 map = 0;
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200237 u8 sata_mode;
Patrick Georgie72a8a32012-11-06 11:05:09 +0100238
239 if (!config)
240 return;
241
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200242 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
243 /* Default to AHCI */
244 sata_mode = 0;
245
Patrick Georgie72a8a32012-11-06 11:05:09 +0100246 /*
247 * Set SATA controller mode early so the resource allocator can
248 * properly assign IO/Memory resources for the controller.
249 */
Vladimir Serbinenko0dd5e432014-07-29 22:35:45 +0200250 if (sata_mode == 0)
Patrick Georgie72a8a32012-11-06 11:05:09 +0100251 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
252
253 map |= (config->sata_port_map ^ 0x3f) << 8;
254
255 pci_write_config16(dev, 0x90, map);
256}
257
258static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device)
259{
260 if (!vendor || !device) {
261 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
262 pci_read_config32(dev, PCI_VENDOR_ID));
263 } else {
264 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
265 ((device & 0xffff) << 16) | (vendor & 0xffff));
266 }
267}
268
269static struct pci_operations sata_pci_ops = {
270 .set_subsystem = sata_set_subsystem,
271};
272
273static struct device_operations sata_ops = {
274 .read_resources = pci_dev_read_resources,
275 .set_resources = pci_dev_set_resources,
276 .enable_resources = pci_dev_enable_resources,
277 .init = sata_init,
278 .enable = sata_enable,
279 .scan_bus = 0,
280 .ops_pci = &sata_pci_ops,
281};
282
283static const unsigned short pci_device_ids[] = {
284 0x2920, 0x2921, 0x2922, 0x2923,
285 0x2928, 0x2929,
286 0,
287};
288
289static const struct pci_driver pch_sata __pci_driver = {
290 .ops = &sata_ops,
291 .vendor = PCI_VENDOR_ID_INTEL,
292 .devices = pci_device_ids,
293};