blob: b2ee84399112221add17da9340ce94e4fc247146 [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>
31#include <soc/hudson.h>
32#include <vboot/vbnv.h>
33
34static void lpc_init(device_t dev)
35{
36 u8 byte;
37 u32 dword;
38 device_t sm_dev;
39
40 /* Enable the LPC Controller */
41 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
42 dword = pci_read_config32(sm_dev, 0x64);
43 dword |= 1 << 20;
44 pci_write_config32(sm_dev, 0x64, dword);
45
46 /* Initialize isa dma */
47 isa_dma_init();
48
49 /* Enable DMA transaction on the LPC bus */
50 byte = pci_read_config8(dev, 0x40);
51 byte |= (1 << 2);
52 pci_write_config8(dev, 0x40, byte);
53
54 /* Disable the timeout mechanism on LPC */
55 byte = pci_read_config8(dev, 0x48);
56 byte &= ~(1 << 7);
57 pci_write_config8(dev, 0x48, byte);
58
59 /* Disable LPC MSI Capability */
60 byte = pci_read_config8(dev, 0x78);
61 byte &= ~(1 << 1);
62 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
63 on on LPC, it holds PCI grant, so no LPC slave cycle can
64 interrupt and visit LPC. */
65 pci_write_config8(dev, 0x78, byte);
66
67 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
68 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
69 byte = pci_read_config8(dev, 0xBB);
70 byte |= 1 << 0 | 1 << 3;
71 pci_write_config8(dev, 0xBB, byte);
72
73 cmos_check_update_date();
74
75 /* Initialize the real time clock.
76 * The 0 argument tells cmos_init not to
77 * update CMOS unless it is invalid.
78 * 1 tells cmos_init to always initialize the CMOS.
79 */
80 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
81 init_vbnv_cmos(0);
82 else
83 cmos_init(0);
84
85 /* Initialize i8259 pic */
86 setup_i8259 ();
87
88 /* Initialize i8254 timers */
89 setup_i8254 ();
90
91 /* Set up SERIRQ, enable continuous mode */
92 byte = (BIT(4) | BIT(7));
93 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
94 byte |= BIT(6);
95
96 pm_write8(PM_SERIRQ_CONF, byte);
97}
98
99static void hudson_lpc_read_resources(device_t dev)
100{
101 struct resource *res;
102
103 /* Get the normal pci resources of this device */
104 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
105
106 /* Add an extra subtractive resource for both memory and I/O. */
107 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
108 res->base = 0;
109 res->size = 0x1000;
110 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
111 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
112
113 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
114 res->base = 0xff800000;
115 res->size = 0x00800000; /* 8 MB for flash */
116 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
117 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
118
119 /* Add a memory resource for the SPI BAR. */
120 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
121
122 res = new_resource(dev, 3); /* IOAPIC */
123 res->base = IO_APIC_ADDR;
124 res->size = 0x00001000;
125 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
126
127 compact_resources(dev);
128}
129
130static void hudson_lpc_set_resources(struct device *dev)
131{
132 struct resource *res;
133 u32 spi_enable_bits;
134
135 /* Special case. The SpiRomEnable and other enables should STAY set. */
136 res = find_resource(dev, 2);
137 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
138 spi_enable_bits &= 0xF;
139 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
140
141 pci_dev_set_resources(dev);
142}
143
144/**
145 * @brief Enable resources for children devices
146 *
147 * @param dev the device whose children's resources are to be enabled
148 *
149 */
150static void hudson_lpc_enable_childrens_resources(device_t dev)
151{
152 struct bus *link;
153 u32 reg, reg_x;
154 int var_num = 0;
155 u16 reg_var[3];
156 u16 reg_size[1] = {512};
157 u8 wiosize = pci_read_config8(dev, 0x74);
158
159 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
160 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
161 * it must be 512 bytes to be able to allocate the fresh LPC window.
162 *
163 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
164 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
165 * The code tries to check if resource can fit into this region
166 */
167
168 reg = pci_read_config32(dev, 0x44);
169 reg_x = pci_read_config32(dev, 0x48);
170
171 /* check if ranges are free and not use them if entry is just already taken */
172 if (reg_x & (1 << 2))
173 var_num = 1;
174 /* just in case check if someone did not manually set other ranges too */
175 if (reg_x & (1 << 24))
176 var_num = 2;
177
178 if (reg_x & (1 << 25))
179 var_num = 3;
180
181 /* check AGESA region size */
182 if (wiosize & (1 << 0))
183 reg_size[0] = 16;
184
185 reg_var[2] = pci_read_config16(dev, 0x90);
186 reg_var[1] = pci_read_config16(dev, 0x66);
187 reg_var[0] = pci_read_config16(dev, 0x64);
188
189 for (link = dev->link_list; link; link = link->next) {
190 device_t child;
191 for (child = link->children; child;
192 child = child->sibling) {
193 if (child->enabled
194 && (child->path.type == DEVICE_PATH_PNP)) {
195 struct resource *res;
196 for (res = child->resource_list; res; res = res->next) {
197 u32 base, end; /* don't need long long */
198 u32 rsize, set = 0, set_x = 0;
199 if (!(res->flags & IORESOURCE_IO))
200 continue;
201 base = res->base;
202 end = resource_end(res);
203 /* find a resource size */
204 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
205 dev_path(child), base, end);
206 switch (base) {
207 case 0x60: /* KB */
208 case 0x64: /* MS */
209 set |= (1 << 29);
210 rsize = 1;
211 break;
212 case 0x3f8: /* COM1 */
213 set |= (1 << 6);
214 rsize = 8;
215 break;
216 case 0x2f8: /* COM2 */
217 set |= (1 << 7);
218 rsize = 8;
219 break;
220 case 0x378: /* Parallel 1 */
221 set |= (1 << 0);
222 set |= (1 << 1); /* + 0x778 for ECP */
223 rsize = 8;
224 break;
225 case 0x3f0: /* FD0 */
226 set |= (1 << 26);
227 rsize = 8;
228 break;
229 case 0x220: /* 0x220 - 0x227 */
230 set |= (1 << 8);
231 rsize = 8;
232 break;
233 case 0x228: /* 0x228 - 0x22f */
234 set |= (1 << 9);
235 rsize = 8;
236 break;
237 case 0x238: /* 0x238 - 0x23f */
238 set |= (1 << 10);
239 rsize = 8;
240 break;
241 case 0x300: /* 0x300 -0x301 */
242 set |= (1 << 18);
243 rsize = 2;
244 break;
245 case 0x400:
246 set_x |= (1 << 16);
247 rsize = 0x40;
248 break;
249 case 0x480:
250 set_x |= (1 << 17);
251 rsize = 0x40;
252 break;
253 case 0x500:
254 set_x |= (1 << 18);
255 rsize = 0x40;
256 break;
257 case 0x580:
258 set_x |= (1 << 19);
259 rsize = 0x40;
260 break;
261 case 0x4700:
262 set_x |= (1 << 22);
263 rsize = 0xc;
264 break;
265 case 0xfd60:
266 set_x |= (1 << 23);
267 rsize = 16;
268 break;
269 default:
270 rsize = 0;
271 /* try AGESA allocated region in region 0 */
272 if ((var_num > 0) && ((base >=reg_var[0]) &&
273 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
274 rsize = reg_size[0];
275 }
276 /* check if region found and matches the enable */
277 if (res->size <= rsize) {
278 reg |= set;
279 reg_x |= set_x;
280 /* check if we can fit resource in variable range */
281 } else if ((var_num < 3) &&
282 ((res->size <= 16) || (res->size == 512))) {
283 /* use variable ranges if pre-defined do not match */
284 switch (var_num) {
285 case 0:
286 reg_x |= (1 << 2);
287 if (res->size <= 16) {
288 wiosize |= (1 << 0);
289 }
290 break;
291 case 1:
292 reg_x |= (1 << 24);
293 if (res->size <= 16)
294 wiosize |= (1 << 2);
295 break;
296 case 2:
297 reg_x |= (1 << 25);
298 if (res->size <= 16)
299 wiosize |= (1 << 3);
300 break;
301 }
302 reg_var[var_num++] =
303 base & 0xffff;
304 } else {
305 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
306 dev_path(child), base, end);
307 }
308 }
309 }
310 }
311 }
312 pci_write_config32(dev, 0x44, reg);
313 pci_write_config32(dev, 0x48, reg_x);
314 /* Set WideIO for as many IOs found (fall through is on purpose) */
315 switch (var_num) {
316 case 3:
317 pci_write_config16(dev, 0x90, reg_var[2]);
318 /* fall through */
319 case 2:
320 pci_write_config16(dev, 0x66, reg_var[1]);
321 /* fall through */
322 case 1:
323 pci_write_config16(dev, 0x64, reg_var[0]);
324 break;
325 }
326 pci_write_config8(dev, 0x74, wiosize);
327}
328
329static void hudson_lpc_enable_resources(device_t dev)
330{
331 pci_dev_enable_resources(dev);
332 hudson_lpc_enable_childrens_resources(dev);
333}
334
335unsigned long acpi_fill_mcfg(unsigned long current)
336{
337 /* Just a dummy */
338 return current;
339}
340
341static struct pci_operations lops_pci = {
342 .set_subsystem = pci_dev_set_subsystem,
343};
344
345static struct device_operations lpc_ops = {
346 .read_resources = hudson_lpc_read_resources,
347 .set_resources = hudson_lpc_set_resources,
348 .enable_resources = hudson_lpc_enable_resources,
349#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
350 .write_acpi_tables = acpi_write_hpet,
351#endif
352 .init = lpc_init,
353 .scan_bus = scan_lpc_bus,
354 .ops_pci = &lops_pci,
355};
356
357static const unsigned short pci_device_ids[] = {
358 PCI_DEVICE_ID_AMD_SB900_LPC,
359 PCI_DEVICE_ID_AMD_CZ_LPC,
360 0
361};
362static const struct pci_driver lpc_driver __pci_driver = {
363 .ops = &lpc_ops,
364 .vendor = PCI_VENDOR_ID_AMD,
365 .devices = pci_device_ids,
366};