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