blob: ce8ae470a1a897f2dc28a87c45ef59fad49f4eb0 [file] [log] [blame]
Arthur Heymans7b9c1392017-04-09 20:40:39 +02001/*
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
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>
Arthur Heymans7b9c1392017-04-09 20:40:39 +020021#include <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020025#include <option.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020026#include <types.h>
27
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030028#include "chip.h"
Elyes HAOUASab89edb2019-05-15 21:10:44 +020029#include "i82801jx.h"
Arthur Heymans7b9c1392017-04-09 20:40:39 +020030
Arthur Heymans349e0852017-04-09 20:48:37 +020031typedef struct southbridge_intel_i82801jx_config config_t;
Arthur Heymans7b9c1392017-04-09 20:40:39 +020032
33static void sata_enable_ahci_mmap(struct device *const dev, const u8 port_map,
34 const int is_mobile)
35{
36 int i;
37 u32 reg32;
38
39 /* Initialize AHCI memory-mapped space */
40 u8 *abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
41 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
42
43 /* Set AHCI access mode.
44 No other ABAR registers should be accessed before this. */
45 reg32 = read32(abar + 0x04);
46 reg32 |= 1 << 31;
47 write32(abar + 0x04, reg32);
48
49 /* CAP (HBA Capabilities) : enable power management */
50 reg32 = read32(abar + 0x00);
51 /* CCCS must be set. */
52 reg32 |= 0x0c006080; /* set CCCS+PSC+SSC+SALP+SSS */
53 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
54 write32(abar + 0x00, reg32);
55
56 /* PI (Ports implemented) */
57 write32(abar + 0x0c, port_map);
58 /* PCH code reads back twice, do we need it, too? */
59 (void) read32(abar + 0x0c); /* Read back 1 */
60 (void) read32(abar + 0x0c); /* Read back 2 */
61
62 /* VSP (Vendor Specific Register) */
63 reg32 = read32(abar + 0xa0);
64 reg32 &= ~0x00000001; /* clear SLPD */
65 write32(abar + 0xa0, reg32);
66
67 /* Lock R/WO bits in Port command registers. */
68 for (i = 0; i < 6; ++i) {
69 if (((i == 2) || (i == 3)) && is_mobile)
70 continue;
71 u8 *addr = abar + 0x118 + (i * 0x80);
72 write32(addr, read32(addr));
73 }
74}
75
76static void sata_program_indexed(struct device *const dev, const int is_mobile)
77{
78 u32 reg32;
79
80 pci_write_config8(dev, D31F2_SIDX, 0x18);
81 reg32 = pci_read_config32(dev, D31F2_SDAT);
82 reg32 &= ~((7 << 6) | (7 << 3) | (7 << 0));
83 reg32 |= (3 << 3) | (3 << 0);
84 pci_write_config32(dev, D31F2_SDAT, reg32);
85
86 pci_write_config8(dev, D31F2_SIDX, 0x28);
87 pci_write_config32(dev, D31F2_SDAT, 0x00cc2080);
88
89 pci_write_config8(dev, D31F2_SIDX, 0x40);
90 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
91
92 pci_write_config8(dev, D31F2_SIDX, 0x78);
93 pci_write_config8(dev, D31F2_SDAT + 2, 0x22);
94
95 if (!is_mobile) {
96 pci_write_config8(dev, D31F2_SIDX, 0x84);
97 reg32 = pci_read_config32(dev, D31F2_SDAT);
98 reg32 &= ~((7 << 3) | (7 << 0));
99 reg32 |= (3 << 3) | (3 << 0);
100 pci_write_config32(dev, D31F2_SDAT, reg32);
101 }
102
103 pci_write_config8(dev, D31F2_SIDX, 0x88);
104 reg32 = pci_read_config32(dev, D31F2_SDAT);
105 if (!is_mobile)
106 reg32 &= ~((7 << 27) | (7 << 24) | (7 << 11) | (7 << 8));
107 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
108 if (!is_mobile)
109 reg32 |= (4 << 27) | (4 << 24) | (2 << 11) | (2 << 8);
110 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
111 pci_write_config32(dev, D31F2_SDAT, reg32);
112
113 pci_write_config8(dev, D31F2_SIDX, 0x8c);
114 reg32 = pci_read_config32(dev, D31F2_SDAT);
115 if (!is_mobile)
116 reg32 &= ~((7 << 27) | (7 << 24));
117 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
118 if (!is_mobile)
119 reg32 |= (2 << 27) | (2 << 24);
120 reg32 |= (2 << 19) | (2 << 16) | 0x00aa;
121 pci_write_config32(dev, D31F2_SDAT, reg32);
122
123 pci_write_config8(dev, D31F2_SIDX, 0x94);
124 pci_write_config32(dev, D31F2_SDAT, 0x00000022);
125
126 pci_write_config8(dev, D31F2_SIDX, 0xa0);
127 reg32 = pci_read_config32(dev, D31F2_SDAT);
128 reg32 &= ~((7 << 3) | (7 << 0));
129 reg32 |= (3 << 3) | (3 << 0);
130 pci_write_config32(dev, D31F2_SDAT, reg32);
131
132 pci_write_config8(dev, D31F2_SIDX, 0xa8);
133 reg32 = pci_read_config32(dev, D31F2_SDAT);
134 reg32 &= ~((7 << 19) | (7 << 16) | (7 << 3) | (7 << 0));
135 reg32 |= (4 << 19) | (4 << 16) | (2 << 3) | (2 << 0);
136 pci_write_config32(dev, D31F2_SDAT, reg32);
137
138 pci_write_config8(dev, D31F2_SIDX, 0xac);
139 reg32 = pci_read_config32(dev, D31F2_SDAT);
140 reg32 &= ~((7 << 19) | (7 << 16) | 0xffff);
141 reg32 |= (2 << 19) | (2 << 16) | 0x000a;
142 pci_write_config32(dev, D31F2_SDAT, reg32);
143}
144
145static void sata_init(struct device *const dev)
146{
147 u16 reg16;
148
149 /* Get the chip configuration */
150 const config_t *const config = dev->chip_info;
151
152 const u16 devid = pci_read_config16(dev, PCI_DEVICE_ID);
153 const int is_mobile = (devid == 0x2928) || (devid == 0x2929);
154 u8 sata_mode;
155
Arthur Heymans349e0852017-04-09 20:48:37 +0200156 printk(BIOS_DEBUG, "i82801jx_sata: initializing...\n");
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200157
158 if (config == NULL) {
Arthur Heymans349e0852017-04-09 20:48:37 +0200159 printk(BIOS_ERR, "i82801jx_sata: error: "
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200160 "device not in devicetree.cb!\n");
161 return;
162 }
163
164 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
165 /* Default to AHCI */
166 sata_mode = 0;
167
168 /*
169 * TODO: In contrast to ICH7 and PCH code we don't set
170 * timings, dma and IDE-I/O settings here. Looks like they
171 * became obsolete with the fading of real IDE ports.
172 * Maybe we can safely remove those settings from PCH code and
173 * even ICH7 code if it doesn't use the feature to combine the
174 * IDE and SATA controllers.
175 */
176
177 pci_write_config16(dev, PCI_COMMAND,
178 PCI_COMMAND_MASTER |
179 PCI_COMMAND_MEMORY | /* read-only in IDE modes */
180 PCI_COMMAND_IO);
181 if (sata_mode != 0)
182 /* No AHCI: clear AHCI base */
183 pci_write_config32(dev, PCI_BASE_ADDRESS_5, 0x00000000);
184
185 if (sata_mode == 0) {
186 printk(BIOS_DEBUG, "SATA controller in AHCI mode.\n");
187 } else {
188 printk(BIOS_DEBUG, "SATA controller in native mode.\n");
189
190 /* Enable native mode on both primary and secondary. */
191 pci_write_config8(dev, PCI_CLASS_PROG, 0x8f);
192 }
193
194 /* Looks like we should only enable decoding here. */
195 pci_write_config16(dev, D31F2_IDE_TIM_PRI, (1 << 15));
196 pci_write_config16(dev, D31F2_IDE_TIM_SEC, (1 << 15));
197
198 /* Port enable. For AHCI, it's managed in memory mapped space. */
199 reg16 = pci_read_config16(dev, 0x92);
200 reg16 &= ~0x3f;
201 reg16 |= (1 << 15) | ((sata_mode == 0) ? 0x3f : config->sata_port_map);
202 pci_write_config16(dev, 0x92, reg16);
203
204 /* SATA clock settings */
205 u32 sclkcg = 0;
206 if (config->sata_clock_request &&
207 !(inb(DEFAULT_GPIOBASE + 0x30) & (1 << (35 - 32))))
208 sclkcg |= 1 << 30; /* Enable SATA clock request. */
209 /* Disable unused ports. */
210 sclkcg |= ((~config->sata_port_map) & 0x3f) << 24;
211 /* Must be programmed. */
212 sclkcg |= 0x193;
213 pci_write_config32(dev, 0x94, sclkcg);
214
215 if (is_mobile && config->sata_traffic_monitor) {
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300216 struct device *const lpc_dev = pcidev_on_root(0x1f, 0);
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200217 if (((pci_read_config8(lpc_dev, D31F0_CxSTATE_CNF)
218 >> 3) & 3) == 3) {
219 u8 reg8 = pci_read_config8(dev, 0x9c);
220 reg8 &= ~(0x1f << 2);
221 reg8 |= 3 << 2;
222 pci_write_config8(dev, 0x9c, reg8);
223 }
224 }
225
226 if (sata_mode == 0)
227 sata_enable_ahci_mmap(dev, config->sata_port_map, is_mobile);
228
229 sata_program_indexed(dev, is_mobile);
230}
231
Elyes HAOUAS1a8c1df2018-05-13 13:36:44 +0200232static void sata_enable(struct device *dev)
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200233{
234 /* Get the chip configuration */
235 const config_t *const config = dev->chip_info;
236
237 u16 map = 0;
238 u8 sata_mode;
239
240 if (!config)
241 return;
242
243 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
244 /* Default to AHCI */
245 sata_mode = 0;
246
247 /*
248 * Set SATA controller mode early so the resource allocator can
249 * properly assign IO/Memory resources for the controller.
250 */
251 if (sata_mode == 0)
252 map = 0x0040 | 0x0020; /* SATA mode + all ports on D31:F2 */
253
254 map |= (config->sata_port_map ^ 0x3f) << 8;
255
256 pci_write_config16(dev, 0x90, map);
257}
258
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200259static struct pci_operations sata_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530260 .set_subsystem = pci_dev_set_subsystem,
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200261};
262
263static struct device_operations sata_ops = {
264 .read_resources = pci_dev_read_resources,
265 .set_resources = pci_dev_set_resources,
266 .enable_resources = pci_dev_enable_resources,
267 .init = sata_init,
268 .enable = sata_enable,
269 .scan_bus = 0,
270 .ops_pci = &sata_pci_ops,
271};
272
273static const unsigned short pci_device_ids[] = {
Arthur Heymans349e0852017-04-09 20:48:37 +0200274 0x3a00,
275 0x3a02,
276 0x3a05,
277 0x3a06,
278 0x3a20,
279 0x3a22,
280 0x3a25,
281 0x3a26,
Arthur Heymans7b9c1392017-04-09 20:40:39 +0200282 0,
283};
284
285static const struct pci_driver pch_sata __pci_driver = {
286 .ops = &sata_ops,
287 .vendor = PCI_VENDOR_ID_INTEL,
288 .devices = pci_device_ids,
289};