blob: d45ef0a41645c8706e7b2c88efb8575716acf5b0 [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2010 Advanced Micro Devices, Inc.
5 * Copyright (C) 2014 Sage Electronic Engineering, LLC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
20#include <device/pnp.h>
21#include <device/pci_ids.h>
22#include <device/pci_ops.h>
23#include <device/pci_def.h>
24#include <pc80/mc146818rtc.h>
25#include <pc80/isa-dma.h>
26#include <arch/io.h>
27#include <arch/ioapic.h>
28#include <arch/acpi.h>
29#include <pc80/i8254.h>
30#include <pc80/i8259.h>
Marshall Dawson4e101ad2017-06-15 12:17:38 -060031#include <soc/pci_devs.h>
Marc Jones24484842017-05-04 21:17:45 -060032#include <soc/hudson.h>
33#include <vboot/vbnv.h>
34
35static void lpc_init(device_t dev)
36{
37 u8 byte;
38 u32 dword;
39 device_t sm_dev;
40
41 /* Enable the LPC Controller */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060042 sm_dev = dev_find_slot(0, PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC));
Marc Jones24484842017-05-04 21:17:45 -060043 dword = pci_read_config32(sm_dev, 0x64);
44 dword |= 1 << 20;
45 pci_write_config32(sm_dev, 0x64, dword);
46
47 /* Initialize isa dma */
48 isa_dma_init();
49
50 /* Enable DMA transaction on the LPC bus */
51 byte = pci_read_config8(dev, 0x40);
52 byte |= (1 << 2);
53 pci_write_config8(dev, 0x40, byte);
54
55 /* Disable the timeout mechanism on LPC */
56 byte = pci_read_config8(dev, 0x48);
57 byte &= ~(1 << 7);
58 pci_write_config8(dev, 0x48, byte);
59
60 /* Disable LPC MSI Capability */
61 byte = pci_read_config8(dev, 0x78);
62 byte &= ~(1 << 1);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060063 /* Keep the old way. i.e., when bus master/DMA cycle is going
64 * on on LPC, it holds PCI grant, so no LPC slave cycle can
65 * interrupt and visit LPC.
66 */
67 byte &= ~(1 << 0);
Marc Jones24484842017-05-04 21:17:45 -060068 pci_write_config8(dev, 0x78, byte);
69
Marshall Dawson4e101ad2017-06-15 12:17:38 -060070 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads
71 * code from SPI ROM
72 * bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12.
73 * todo: verify both these against BKDG
74 */
75 byte = pci_read_config8(dev, 0xbb);
Marc Jones24484842017-05-04 21:17:45 -060076 byte |= 1 << 0 | 1 << 3;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060077 pci_write_config8(dev, 0xbb, byte);
Marc Jones24484842017-05-04 21:17:45 -060078
79 cmos_check_update_date();
80
81 /* Initialize the real time clock.
82 * The 0 argument tells cmos_init not to
83 * update CMOS unless it is invalid.
84 * 1 tells cmos_init to always initialize the CMOS.
85 */
86 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
87 init_vbnv_cmos(0);
88 else
89 cmos_init(0);
90
91 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060092 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060093
94 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060095 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060096
97 /* Set up SERIRQ, enable continuous mode */
98 byte = (BIT(4) | BIT(7));
99 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
100 byte |= BIT(6);
101
102 pm_write8(PM_SERIRQ_CONF, byte);
103}
104
105static void hudson_lpc_read_resources(device_t dev)
106{
107 struct resource *res;
108
109 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600110 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600111
112 /* Add an extra subtractive resource for both memory and I/O. */
113 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
114 res->base = 0;
115 res->size = 0x1000;
116 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
117 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
118
119 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
120 res->base = 0xff800000;
121 res->size = 0x00800000; /* 8 MB for flash */
122 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
123 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
124
125 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600126 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
127 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600128
129 res = new_resource(dev, 3); /* IOAPIC */
130 res->base = IO_APIC_ADDR;
131 res->size = 0x00001000;
132 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
133
134 compact_resources(dev);
135}
136
137static void hudson_lpc_set_resources(struct device *dev)
138{
139 struct resource *res;
140 u32 spi_enable_bits;
141
142 /* Special case. The SpiRomEnable and other enables should STAY set. */
143 res = find_resource(dev, 2);
144 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600145 spi_enable_bits &= 0xf;
146 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
147 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600148
149 pci_dev_set_resources(dev);
150}
151
152/**
153 * @brief Enable resources for children devices
154 *
155 * @param dev the device whose children's resources are to be enabled
156 *
157 */
158static void hudson_lpc_enable_childrens_resources(device_t dev)
159{
160 struct bus *link;
161 u32 reg, reg_x;
162 int var_num = 0;
163 u16 reg_var[3];
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600164 u16 reg_size[1] = {512};
Marc Jones24484842017-05-04 21:17:45 -0600165 u8 wiosize = pci_read_config8(dev, 0x74);
166
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600167 /* Be a bit relaxed, tolerate that LPC region might be bigger than
168 * resource we try to fit, do it like this for all regions < 16 bytes.
169 * If there is a resource > 16 bytes it must be 512 bytes to be able
170 * to allocate the fresh LPC window.
Marc Jones24484842017-05-04 21:17:45 -0600171 *
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600172 * AGESA likes to enable already one LPC region in wide port base
173 * 0x64-0x65, using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
174 * The code tries to check if resource can fit into this region.
Marc Jones24484842017-05-04 21:17:45 -0600175 */
176
177 reg = pci_read_config32(dev, 0x44);
178 reg_x = pci_read_config32(dev, 0x48);
179
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600180 /* check if ranges are free and don't use them if already taken */
Marc Jones24484842017-05-04 21:17:45 -0600181 if (reg_x & (1 << 2))
182 var_num = 1;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600183 /* just in case check if someone did not manually set other ranges */
Marc Jones24484842017-05-04 21:17:45 -0600184 if (reg_x & (1 << 24))
185 var_num = 2;
186
187 if (reg_x & (1 << 25))
188 var_num = 3;
189
190 /* check AGESA region size */
191 if (wiosize & (1 << 0))
192 reg_size[0] = 16;
193
194 reg_var[2] = pci_read_config16(dev, 0x90);
195 reg_var[1] = pci_read_config16(dev, 0x66);
196 reg_var[0] = pci_read_config16(dev, 0x64);
197
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600198 /* todo: clean up the code style here */
199 for (link = dev->link_list ; link ; link = link->next) {
Marc Jones24484842017-05-04 21:17:45 -0600200 device_t child;
201 for (child = link->children; child;
202 child = child->sibling) {
203 if (child->enabled
204 && (child->path.type == DEVICE_PATH_PNP)) {
205 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600206 for (res = child->resource_list ; res ; res = res->next) {
Marc Jones24484842017-05-04 21:17:45 -0600207 u32 base, end; /* don't need long long */
208 u32 rsize, set = 0, set_x = 0;
209 if (!(res->flags & IORESOURCE_IO))
210 continue;
211 base = res->base;
212 end = resource_end(res);
213 /* find a resource size */
214 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
215 dev_path(child), base, end);
216 switch (base) {
217 case 0x60: /* KB */
218 case 0x64: /* MS */
219 set |= (1 << 29);
220 rsize = 1;
221 break;
222 case 0x3f8: /* COM1 */
223 set |= (1 << 6);
224 rsize = 8;
225 break;
226 case 0x2f8: /* COM2 */
227 set |= (1 << 7);
228 rsize = 8;
229 break;
230 case 0x378: /* Parallel 1 */
231 set |= (1 << 0);
232 set |= (1 << 1); /* + 0x778 for ECP */
233 rsize = 8;
234 break;
235 case 0x3f0: /* FD0 */
236 set |= (1 << 26);
237 rsize = 8;
238 break;
239 case 0x220: /* 0x220 - 0x227 */
240 set |= (1 << 8);
241 rsize = 8;
242 break;
243 case 0x228: /* 0x228 - 0x22f */
244 set |= (1 << 9);
245 rsize = 8;
246 break;
247 case 0x238: /* 0x238 - 0x23f */
248 set |= (1 << 10);
249 rsize = 8;
250 break;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600251 case 0x300: /* 0x300 - 0x301 */
Marc Jones24484842017-05-04 21:17:45 -0600252 set |= (1 << 18);
253 rsize = 2;
254 break;
255 case 0x400:
256 set_x |= (1 << 16);
257 rsize = 0x40;
258 break;
259 case 0x480:
260 set_x |= (1 << 17);
261 rsize = 0x40;
262 break;
263 case 0x500:
264 set_x |= (1 << 18);
265 rsize = 0x40;
266 break;
267 case 0x580:
268 set_x |= (1 << 19);
269 rsize = 0x40;
270 break;
271 case 0x4700:
272 set_x |= (1 << 22);
273 rsize = 0xc;
274 break;
275 case 0xfd60:
276 set_x |= (1 << 23);
277 rsize = 16;
278 break;
279 default:
280 rsize = 0;
281 /* try AGESA allocated region in region 0 */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600282 if ((var_num > 0) && ((base >= reg_var[0]) &&
Marc Jones24484842017-05-04 21:17:45 -0600283 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
284 rsize = reg_size[0];
285 }
286 /* check if region found and matches the enable */
287 if (res->size <= rsize) {
288 reg |= set;
289 reg_x |= set_x;
290 /* check if we can fit resource in variable range */
291 } else if ((var_num < 3) &&
292 ((res->size <= 16) || (res->size == 512))) {
293 /* use variable ranges if pre-defined do not match */
294 switch (var_num) {
295 case 0:
296 reg_x |= (1 << 2);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600297 if (res->size <= 16)
Marc Jones24484842017-05-04 21:17:45 -0600298 wiosize |= (1 << 0);
Marc Jones24484842017-05-04 21:17:45 -0600299 break;
300 case 1:
301 reg_x |= (1 << 24);
302 if (res->size <= 16)
303 wiosize |= (1 << 2);
304 break;
305 case 2:
306 reg_x |= (1 << 25);
307 if (res->size <= 16)
308 wiosize |= (1 << 3);
309 break;
310 }
311 reg_var[var_num++] =
312 base & 0xffff;
313 } else {
314 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
315 dev_path(child), base, end);
316 }
317 }
318 }
319 }
320 }
321 pci_write_config32(dev, 0x44, reg);
322 pci_write_config32(dev, 0x48, reg_x);
323 /* Set WideIO for as many IOs found (fall through is on purpose) */
324 switch (var_num) {
325 case 3:
326 pci_write_config16(dev, 0x90, reg_var[2]);
327 /* fall through */
328 case 2:
329 pci_write_config16(dev, 0x66, reg_var[1]);
330 /* fall through */
331 case 1:
332 pci_write_config16(dev, 0x64, reg_var[0]);
333 break;
334 }
335 pci_write_config8(dev, 0x74, wiosize);
336}
337
338static void hudson_lpc_enable_resources(device_t dev)
339{
340 pci_dev_enable_resources(dev);
341 hudson_lpc_enable_childrens_resources(dev);
342}
343
344unsigned long acpi_fill_mcfg(unsigned long current)
345{
346 /* Just a dummy */
347 return current;
348}
349
350static struct pci_operations lops_pci = {
351 .set_subsystem = pci_dev_set_subsystem,
352};
353
354static struct device_operations lpc_ops = {
355 .read_resources = hudson_lpc_read_resources,
356 .set_resources = hudson_lpc_set_resources,
357 .enable_resources = hudson_lpc_enable_resources,
358#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
359 .write_acpi_tables = acpi_write_hpet,
360#endif
361 .init = lpc_init,
362 .scan_bus = scan_lpc_bus,
363 .ops_pci = &lops_pci,
364};
365
366static const unsigned short pci_device_ids[] = {
367 PCI_DEVICE_ID_AMD_SB900_LPC,
368 PCI_DEVICE_ID_AMD_CZ_LPC,
369 0
370};
371static const struct pci_driver lpc_driver __pci_driver = {
372 .ops = &lpc_ops,
373 .vendor = PCI_VENDOR_ID_AMD,
374 .devices = pci_device_ids,
375};