blob: 7a3914a9bd5a2ec11a316c270b896f920c38d001 [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"
Marc Jones956a58e2017-04-20 16:55:24 -060032#include <vboot/vbnv.h>
Philipp Deppenwiese30670122017-03-01 02:24:33 +010033#include "pci_devs.h"
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030034
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 */
42 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
43 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);
63 byte &= ~(1 << 0); /* 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 pci_write_config8(dev, 0x78, byte);
67
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020068 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030069 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
70 byte = pci_read_config8(dev, 0xBB);
71 byte |= 1 << 0 | 1 << 3;
72 pci_write_config8(dev, 0xBB, byte);
73
Gabe Black03abaee212014-04-30 21:31:44 -070074 cmos_check_update_date();
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030075
76 /* Initialize the real time clock.
77 * The 0 argument tells cmos_init not to
78 * update CMOS unless it is invalid.
79 * 1 tells cmos_init to always initialize the CMOS.
80 */
Marc Jones956a58e2017-04-20 16:55:24 -060081 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
82 init_vbnv_cmos(0);
83 else
84 cmos_init(0);
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060085
86 /* Initialize i8259 pic */
87 setup_i8259 ();
88
89 /* Initialize i8254 timers */
90 setup_i8254 ();
Marc Jones3eec9dd2017-04-09 18:00:40 -060091
Paul Menzel4c402292017-04-14 17:23:49 +020092 /* Set up SERIRQ, enable continuous mode */
Marc Jones3eec9dd2017-04-09 18:00:40 -060093 byte = (BIT(4) | BIT(7));
94 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
95 byte |= BIT(6);
96
97 pm_write8(PM_SERIRQ_CONF, byte);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030098}
99
100static void hudson_lpc_read_resources(device_t dev)
101{
102 struct resource *res;
103
104 /* Get the normal pci resources of this device */
105 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
106
107 /* Add an extra subtractive resource for both memory and I/O. */
108 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
109 res->base = 0;
110 res->size = 0x1000;
111 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
112 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
113
114 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
115 res->base = 0xff800000;
116 res->size = 0x00800000; /* 8 MB for flash */
117 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
118 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
119
120 /* Add a memory resource for the SPI BAR. */
121 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
122
123 res = new_resource(dev, 3); /* IOAPIC */
124 res->base = IO_APIC_ADDR;
125 res->size = 0x00001000;
126 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
127
128 compact_resources(dev);
129}
130
131static void hudson_lpc_set_resources(struct device *dev)
132{
133 struct resource *res;
Marc Jones6fcaaef2017-04-20 16:48:42 -0600134 u32 spi_enable_bits;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300135
Marc Jones6fcaaef2017-04-20 16:48:42 -0600136 /* Special case. The SpiRomEnable and other enables should STAY set. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300137 res = find_resource(dev, 2);
Marc Jones6fcaaef2017-04-20 16:48:42 -0600138 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
139 spi_enable_bits &= 0xF;
140 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300141
142 pci_dev_set_resources(dev);
143}
144
145/**
146 * @brief Enable resources for children devices
147 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000148 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300149 *
150 */
151static void hudson_lpc_enable_childrens_resources(device_t dev)
152{
153 struct bus *link;
154 u32 reg, reg_x;
155 int var_num = 0;
156 u16 reg_var[3];
157 u16 reg_size[1] = {512};
158 u8 wiosize = pci_read_config8(dev, 0x74);
159
160 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
161 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
162 * it must be 512 bytes to be able to allocate the fresh LPC window.
163 *
164 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
165 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
166 * The code tries to check if resource can fit into this region
167 */
168
169 reg = pci_read_config32(dev, 0x44);
170 reg_x = pci_read_config32(dev, 0x48);
171
172 /* check if ranges are free and not use them if entry is just already taken */
173 if (reg_x & (1 << 2))
174 var_num = 1;
175 /* just in case check if someone did not manually set other ranges too */
176 if (reg_x & (1 << 24))
177 var_num = 2;
178
179 if (reg_x & (1 << 25))
180 var_num = 3;
181
182 /* check AGESA region size */
183 if (wiosize & (1 << 0))
184 reg_size[0] = 16;
185
186 reg_var[2] = pci_read_config16(dev, 0x90);
187 reg_var[1] = pci_read_config16(dev, 0x66);
188 reg_var[0] = pci_read_config16(dev, 0x64);
189
190 for (link = dev->link_list; link; link = link->next) {
191 device_t child;
192 for (child = link->children; child;
193 child = child->sibling) {
194 if (child->enabled
195 && (child->path.type == DEVICE_PATH_PNP)) {
196 struct resource *res;
197 for (res = child->resource_list; res; res = res->next) {
198 u32 base, end; /* don't need long long */
199 u32 rsize, set = 0, set_x = 0;
200 if (!(res->flags & IORESOURCE_IO))
201 continue;
202 base = res->base;
203 end = resource_end(res);
204 /* find a resource size */
205 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
206 dev_path(child), base, end);
207 switch (base) {
208 case 0x60: /* KB */
209 case 0x64: /* MS */
210 set |= (1 << 29);
211 rsize = 1;
212 break;
213 case 0x3f8: /* COM1 */
214 set |= (1 << 6);
215 rsize = 8;
216 break;
217 case 0x2f8: /* COM2 */
218 set |= (1 << 7);
219 rsize = 8;
220 break;
221 case 0x378: /* Parallel 1 */
222 set |= (1 << 0);
223 set |= (1 << 1); /* + 0x778 for ECP */
224 rsize = 8;
225 break;
226 case 0x3f0: /* FD0 */
227 set |= (1 << 26);
228 rsize = 8;
229 break;
230 case 0x220: /* 0x220 - 0x227 */
231 set |= (1 << 8);
232 rsize = 8;
233 break;
234 case 0x228: /* 0x228 - 0x22f */
235 set |= (1 << 9);
236 rsize = 8;
237 break;
238 case 0x238: /* 0x238 - 0x23f */
239 set |= (1 << 10);
240 rsize = 8;
241 break;
242 case 0x300: /* 0x300 -0x301 */
243 set |= (1 << 18);
244 rsize = 2;
245 break;
246 case 0x400:
247 set_x |= (1 << 16);
248 rsize = 0x40;
249 break;
250 case 0x480:
251 set_x |= (1 << 17);
252 rsize = 0x40;
253 break;
254 case 0x500:
255 set_x |= (1 << 18);
256 rsize = 0x40;
257 break;
258 case 0x580:
259 set_x |= (1 << 19);
260 rsize = 0x40;
261 break;
262 case 0x4700:
263 set_x |= (1 << 22);
264 rsize = 0xc;
265 break;
266 case 0xfd60:
267 set_x |= (1 << 23);
268 rsize = 16;
269 break;
270 default:
271 rsize = 0;
272 /* try AGESA allocated region in region 0 */
273 if ((var_num > 0) && ((base >=reg_var[0]) &&
274 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
275 rsize = reg_size[0];
276 }
277 /* check if region found and matches the enable */
278 if (res->size <= rsize) {
279 reg |= set;
280 reg_x |= set_x;
281 /* check if we can fit resource in variable range */
282 } else if ((var_num < 3) &&
283 ((res->size <= 16) || (res->size == 512))) {
284 /* use variable ranges if pre-defined do not match */
285 switch (var_num) {
286 case 0:
287 reg_x |= (1 << 2);
288 if (res->size <= 16) {
289 wiosize |= (1 << 0);
290 }
291 break;
292 case 1:
293 reg_x |= (1 << 24);
294 if (res->size <= 16)
295 wiosize |= (1 << 2);
296 break;
297 case 2:
298 reg_x |= (1 << 25);
299 if (res->size <= 16)
300 wiosize |= (1 << 3);
301 break;
302 }
303 reg_var[var_num++] =
304 base & 0xffff;
305 } else {
306 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
307 dev_path(child), base, end);
308 }
309 }
310 }
311 }
312 }
313 pci_write_config32(dev, 0x44, reg);
314 pci_write_config32(dev, 0x48, reg_x);
315 /* Set WideIO for as many IOs found (fall through is on purpose) */
316 switch (var_num) {
317 case 3:
318 pci_write_config16(dev, 0x90, reg_var[2]);
319 /* fall through */
320 case 2:
321 pci_write_config16(dev, 0x66, reg_var[1]);
322 /* fall through */
323 case 1:
324 pci_write_config16(dev, 0x64, reg_var[0]);
325 break;
326 }
327 pci_write_config8(dev, 0x74, wiosize);
328}
329
330static void hudson_lpc_enable_resources(device_t dev)
331{
332 pci_dev_enable_resources(dev);
333 hudson_lpc_enable_childrens_resources(dev);
334}
335
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000336unsigned long acpi_fill_mcfg(unsigned long current)
337{
338 /* Just a dummy */
339 return current;
340}
341
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100342static const char *lpc_acpi_name(struct device *dev)
343{
344 if (dev->path.type != DEVICE_PATH_PCI)
345 return NULL;
346
347 if (dev->path.pci.devfn == LPC_DEVFN)
348 return "LIBR";
349
350 return NULL;
351}
352
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300353static struct pci_operations lops_pci = {
354 .set_subsystem = pci_dev_set_subsystem,
355};
356
357static struct device_operations lpc_ops = {
358 .read_resources = hudson_lpc_read_resources,
359 .set_resources = hudson_lpc_set_resources,
360 .enable_resources = hudson_lpc_enable_resources,
Vladimir Serbinenko83f81ca2014-11-09 13:30:50 +0100361#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Dave Frodin5c015f02015-01-27 07:19:04 -0700362 .write_acpi_tables = acpi_write_hpet,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200363#endif
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300364 .init = lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200365 .scan_bus = scan_lpc_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300366 .ops_pci = &lops_pci,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100367 .acpi_name = lpc_acpi_name,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300368};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800369
370static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200371 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800372 PCI_DEVICE_ID_AMD_CZ_LPC,
373 0
374};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300375static const struct pci_driver lpc_driver __pci_driver = {
376 .ops = &lpc_ops,
377 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800378 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300379};