blob: 38c57331b2eb8ffbfd0b7a1037fd6920bec2aa4f [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
87 /* Setup SERIRQ, enable continuous mode */
88 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;
129
130 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
131 res = find_resource(dev, 2);
132 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
133
134 pci_dev_set_resources(dev);
135}
136
137/**
138 * @brief Enable resources for children devices
139 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000140 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300141 *
142 */
143static void hudson_lpc_enable_childrens_resources(device_t dev)
144{
145 struct bus *link;
146 u32 reg, reg_x;
147 int var_num = 0;
148 u16 reg_var[3];
149 u16 reg_size[1] = {512};
150 u8 wiosize = pci_read_config8(dev, 0x74);
151
152 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
153 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
154 * it must be 512 bytes to be able to allocate the fresh LPC window.
155 *
156 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
157 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
158 * The code tries to check if resource can fit into this region
159 */
160
161 reg = pci_read_config32(dev, 0x44);
162 reg_x = pci_read_config32(dev, 0x48);
163
164 /* check if ranges are free and not use them if entry is just already taken */
165 if (reg_x & (1 << 2))
166 var_num = 1;
167 /* just in case check if someone did not manually set other ranges too */
168 if (reg_x & (1 << 24))
169 var_num = 2;
170
171 if (reg_x & (1 << 25))
172 var_num = 3;
173
174 /* check AGESA region size */
175 if (wiosize & (1 << 0))
176 reg_size[0] = 16;
177
178 reg_var[2] = pci_read_config16(dev, 0x90);
179 reg_var[1] = pci_read_config16(dev, 0x66);
180 reg_var[0] = pci_read_config16(dev, 0x64);
181
182 for (link = dev->link_list; link; link = link->next) {
183 device_t child;
184 for (child = link->children; child;
185 child = child->sibling) {
186 if (child->enabled
187 && (child->path.type == DEVICE_PATH_PNP)) {
188 struct resource *res;
189 for (res = child->resource_list; res; res = res->next) {
190 u32 base, end; /* don't need long long */
191 u32 rsize, set = 0, set_x = 0;
192 if (!(res->flags & IORESOURCE_IO))
193 continue;
194 base = res->base;
195 end = resource_end(res);
196 /* find a resource size */
197 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
198 dev_path(child), base, end);
199 switch (base) {
200 case 0x60: /* KB */
201 case 0x64: /* MS */
202 set |= (1 << 29);
203 rsize = 1;
204 break;
205 case 0x3f8: /* COM1 */
206 set |= (1 << 6);
207 rsize = 8;
208 break;
209 case 0x2f8: /* COM2 */
210 set |= (1 << 7);
211 rsize = 8;
212 break;
213 case 0x378: /* Parallel 1 */
214 set |= (1 << 0);
215 set |= (1 << 1); /* + 0x778 for ECP */
216 rsize = 8;
217 break;
218 case 0x3f0: /* FD0 */
219 set |= (1 << 26);
220 rsize = 8;
221 break;
222 case 0x220: /* 0x220 - 0x227 */
223 set |= (1 << 8);
224 rsize = 8;
225 break;
226 case 0x228: /* 0x228 - 0x22f */
227 set |= (1 << 9);
228 rsize = 8;
229 break;
230 case 0x238: /* 0x238 - 0x23f */
231 set |= (1 << 10);
232 rsize = 8;
233 break;
234 case 0x300: /* 0x300 -0x301 */
235 set |= (1 << 18);
236 rsize = 2;
237 break;
238 case 0x400:
239 set_x |= (1 << 16);
240 rsize = 0x40;
241 break;
242 case 0x480:
243 set_x |= (1 << 17);
244 rsize = 0x40;
245 break;
246 case 0x500:
247 set_x |= (1 << 18);
248 rsize = 0x40;
249 break;
250 case 0x580:
251 set_x |= (1 << 19);
252 rsize = 0x40;
253 break;
254 case 0x4700:
255 set_x |= (1 << 22);
256 rsize = 0xc;
257 break;
258 case 0xfd60:
259 set_x |= (1 << 23);
260 rsize = 16;
261 break;
262 default:
263 rsize = 0;
264 /* try AGESA allocated region in region 0 */
265 if ((var_num > 0) && ((base >=reg_var[0]) &&
266 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
267 rsize = reg_size[0];
268 }
269 /* check if region found and matches the enable */
270 if (res->size <= rsize) {
271 reg |= set;
272 reg_x |= set_x;
273 /* check if we can fit resource in variable range */
274 } else if ((var_num < 3) &&
275 ((res->size <= 16) || (res->size == 512))) {
276 /* use variable ranges if pre-defined do not match */
277 switch (var_num) {
278 case 0:
279 reg_x |= (1 << 2);
280 if (res->size <= 16) {
281 wiosize |= (1 << 0);
282 }
283 break;
284 case 1:
285 reg_x |= (1 << 24);
286 if (res->size <= 16)
287 wiosize |= (1 << 2);
288 break;
289 case 2:
290 reg_x |= (1 << 25);
291 if (res->size <= 16)
292 wiosize |= (1 << 3);
293 break;
294 }
295 reg_var[var_num++] =
296 base & 0xffff;
297 } else {
298 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
299 dev_path(child), base, end);
300 }
301 }
302 }
303 }
304 }
305 pci_write_config32(dev, 0x44, reg);
306 pci_write_config32(dev, 0x48, reg_x);
307 /* Set WideIO for as many IOs found (fall through is on purpose) */
308 switch (var_num) {
309 case 3:
310 pci_write_config16(dev, 0x90, reg_var[2]);
311 /* fall through */
312 case 2:
313 pci_write_config16(dev, 0x66, reg_var[1]);
314 /* fall through */
315 case 1:
316 pci_write_config16(dev, 0x64, reg_var[0]);
317 break;
318 }
319 pci_write_config8(dev, 0x74, wiosize);
320}
321
322static void hudson_lpc_enable_resources(device_t dev)
323{
324 pci_dev_enable_resources(dev);
325 hudson_lpc_enable_childrens_resources(dev);
326}
327
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000328unsigned long acpi_fill_mcfg(unsigned long current)
329{
330 /* Just a dummy */
331 return current;
332}
333
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300334static struct pci_operations lops_pci = {
335 .set_subsystem = pci_dev_set_subsystem,
336};
337
338static struct device_operations lpc_ops = {
339 .read_resources = hudson_lpc_read_resources,
340 .set_resources = hudson_lpc_set_resources,
341 .enable_resources = hudson_lpc_enable_resources,
Vladimir Serbinenko83f81ca2014-11-09 13:30:50 +0100342#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Dave Frodin5c015f02015-01-27 07:19:04 -0700343 .write_acpi_tables = acpi_write_hpet,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200344#endif
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300345 .init = lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200346 .scan_bus = scan_lpc_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300347 .ops_pci = &lops_pci,
348};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800349
350static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200351 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800352 PCI_DEVICE_ID_AMD_CZ_LPC,
353 0
354};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300355static const struct pci_driver lpc_driver __pci_driver = {
356 .ops = &lpc_ops,
357 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800358 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300359};