blob: 8d07e6487e4d9dbb87ef9ea301e30038791fb7d3 [file] [log] [blame]
zbao246e84b2012-07-13 18:47:03 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2010 Advanced Micro Devices, Inc.
Dave Frodin8ef20cf2014-06-05 14:21:11 -06005 * Copyright (C) 2014 Sage Electronic Engineering, LLC
zbao246e84b2012-07-13 18:47:03 +08006 *
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.
zbao246e84b2012-07-13 18:47:03 +080015 */
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>
Martin Roth3aef7b42012-12-05 15:50:32 -070023#include <device/pci_def.h>
zbao246e84b2012-07-13 18:47:03 +080024#include <pc80/mc146818rtc.h>
25#include <pc80/isa-dma.h>
zbao246e84b2012-07-13 18:47:03 +080026#include <arch/io.h>
Dave Frodin8ef20cf2014-06-05 14:21:11 -060027#include <arch/ioapic.h>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020028#include <arch/acpi.h>
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100029#include <pc80/i8254.h>
30#include <pc80/i8259.h>
zbao246e84b2012-07-13 18:47:03 +080031#include "hudson.h"
Kevin Cody-Littlec0984002018-05-09 14:25:44 -040032#include "pci_devs.h"
zbao246e84b2012-07-13 18:47:03 +080033
Elyes HAOUASa93e7542018-05-19 14:30:47 +020034static void lpc_init(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080035{
36 u8 byte;
37 u32 dword;
Elyes HAOUASa93e7542018-05-19 14:30:47 +020038 struct device *sm_dev;
zbao246e84b2012-07-13 18:47:03 +080039
40 /* Enable the LPC Controller */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +030041 sm_dev = pcidev_on_root(0x14, 0);
zbao246e84b2012-07-13 18:47:03 +080042 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
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020067 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
zbao246e84b2012-07-13 18:47:03 +080068 /* 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);
zbaoef180e22012-08-02 19:03:44 +080072
Gabe Black03abaee212014-04-30 21:31:44 -070073 cmos_check_update_date();
Mike Loptiena96d24d2013-02-25 10:41:28 -070074
75 /* Initialize the real time clock.
Gabe Blackb3f08c62014-04-30 17:12:25 -070076 * The 0 argument tells cmos_init not to
Mike Loptiena96d24d2013-02-25 10:41:28 -070077 * update CMOS unless it is invalid.
Gabe Blackb3f08c62014-04-30 17:12:25 -070078 * 1 tells cmos_init to always initialize the CMOS.
Mike Loptiena96d24d2013-02-25 10:41:28 -070079 */
Gabe Blackb3f08c62014-04-30 17:12:25 -070080 cmos_init(0);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100081
82 /* Initialize i8259 pic */
83 setup_i8259 ();
84
85 /* Initialize i8254 timers */
86 setup_i8254 ();
zbao246e84b2012-07-13 18:47:03 +080087}
88
Elyes HAOUASa93e7542018-05-19 14:30:47 +020089static void hudson_lpc_read_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080090{
91 struct resource *res;
92
93 /* Get the normal pci resources of this device */
94 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
95
zbao246e84b2012-07-13 18:47:03 +080096 /* Add an extra subtractive resource for both memory and I/O. */
97 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
98 res->base = 0;
99 res->size = 0x1000;
100 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
101 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
102
103 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
104 res->base = 0xff800000;
105 res->size = 0x00800000; /* 8 MB for flash */
106 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
107 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
108
Dave Frodinac1b8752014-06-05 14:30:22 -0600109 /* Add a memory resource for the SPI BAR. */
110 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
111
Dave Frodin8ef20cf2014-06-05 14:21:11 -0600112 res = new_resource(dev, 3); /* IOAPIC */
113 res->base = IO_APIC_ADDR;
114 res->size = 0x00001000;
115 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
zbao246e84b2012-07-13 18:47:03 +0800116
117 compact_resources(dev);
118}
119
120static void hudson_lpc_set_resources(struct device *dev)
121{
122 struct resource *res;
123
Martin Roth3aef7b42012-12-05 15:50:32 -0700124 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
Dave Frodinac1b8752014-06-05 14:30:22 -0600125 res = find_resource(dev, 2);
126 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
Martin Roth3aef7b42012-12-05 15:50:32 -0700127
zbao246e84b2012-07-13 18:47:03 +0800128 pci_dev_set_resources(dev);
zbao246e84b2012-07-13 18:47:03 +0800129}
130
131/**
132 * @brief Enable resources for children devices
133 *
Martin Roth3c3a50c2014-12-16 20:50:26 -0700134 * @param dev the device whose children's resources are to be enabled
zbao246e84b2012-07-13 18:47:03 +0800135 *
136 */
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200137static void hudson_lpc_enable_childrens_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800138{
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100139 struct bus *link;
140 u32 reg, reg_x;
141 int var_num = 0;
142 u16 reg_var[3];
143 u16 reg_size[1] = {512};
144 u8 wiosize = pci_read_config8(dev, 0x74);
zbao246e84b2012-07-13 18:47:03 +0800145
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100146 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
147 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
148 * it must be 512 bytes to be able to allocate the fresh LPC window.
149 *
150 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
151 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
152 * The code tries to check if resource can fit into this region
153 */
154
155 reg = pci_read_config32(dev, 0x44);
156 reg_x = pci_read_config32(dev, 0x48);
157
158 /* check if ranges are free and not use them if entry is just already taken */
159 if (reg_x & (1 << 2))
160 var_num = 1;
161 /* just in case check if someone did not manually set other ranges too */
162 if (reg_x & (1 << 24))
163 var_num = 2;
164
165 if (reg_x & (1 << 25))
166 var_num = 3;
167
168 /* check AGESA region size */
169 if (wiosize & (1 << 0))
170 reg_size[0] = 16;
171
172 reg_var[2] = pci_read_config16(dev, 0x90);
173 reg_var[1] = pci_read_config16(dev, 0x66);
174 reg_var[0] = pci_read_config16(dev, 0x64);
175
176 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200177 struct device *child;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100178 for (child = link->children; child;
179 child = child->sibling) {
180 if (child->enabled
181 && (child->path.type == DEVICE_PATH_PNP)) {
182 struct resource *res;
183 for (res = child->resource_list; res; res = res->next) {
184 u32 base, end; /* don't need long long */
185 u32 rsize, set = 0, set_x = 0;
186 if (!(res->flags & IORESOURCE_IO))
187 continue;
188 base = res->base;
189 end = resource_end(res);
190 /* find a resource size */
191 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
192 dev_path(child), base, end);
193 switch (base) {
194 case 0x60: /* KB */
195 case 0x64: /* MS */
196 set |= (1 << 29);
197 rsize = 1;
198 break;
199 case 0x3f8: /* COM1 */
200 set |= (1 << 6);
201 rsize = 8;
202 break;
203 case 0x2f8: /* COM2 */
204 set |= (1 << 7);
205 rsize = 8;
206 break;
207 case 0x378: /* Parallel 1 */
208 set |= (1 << 0);
209 set |= (1 << 1); /* + 0x778 for ECP */
210 rsize = 8;
211 break;
212 case 0x3f0: /* FD0 */
213 set |= (1 << 26);
214 rsize = 8;
215 break;
216 case 0x220: /* 0x220 - 0x227 */
217 set |= (1 << 8);
218 rsize = 8;
219 break;
220 case 0x228: /* 0x228 - 0x22f */
221 set |= (1 << 9);
222 rsize = 8;
223 break;
224 case 0x238: /* 0x238 - 0x23f */
225 set |= (1 << 10);
226 rsize = 8;
227 break;
228 case 0x300: /* 0x300 -0x301 */
229 set |= (1 << 18);
230 rsize = 2;
231 break;
232 case 0x400:
233 set_x |= (1 << 16);
234 rsize = 0x40;
235 break;
236 case 0x480:
237 set_x |= (1 << 17);
238 rsize = 0x40;
Daniele Forsib532b122014-07-26 10:32:34 +0200239 break;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100240 case 0x500:
241 set_x |= (1 << 18);
242 rsize = 0x40;
243 break;
244 case 0x580:
245 set_x |= (1 << 19);
246 rsize = 0x40;
247 break;
248 case 0x4700:
249 set_x |= (1 << 22);
250 rsize = 0xc;
251 break;
252 case 0xfd60:
253 set_x |= (1 << 23);
254 rsize = 16;
255 break;
256 default:
257 rsize = 0;
258 /* try AGESA allocated region in region 0 */
259 if ((var_num > 0) && ((base >=reg_var[0]) &&
260 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
261 rsize = reg_size[0];
262 }
263 /* check if region found and matches the enable */
264 if (res->size <= rsize) {
265 reg |= set;
266 reg_x |= set_x;
267 /* check if we can fit resource in variable range */
268 } else if ((var_num < 3) &&
269 ((res->size <= 16) || (res->size == 512))) {
270 /* use variable ranges if pre-defined do not match */
271 switch (var_num) {
272 case 0:
273 reg_x |= (1 << 2);
274 if (res->size <= 16) {
275 wiosize |= (1 << 0);
276 }
277 break;
278 case 1:
279 reg_x |= (1 << 24);
280 if (res->size <= 16)
281 wiosize |= (1 << 2);
282 break;
283 case 2:
284 reg_x |= (1 << 25);
285 if (res->size <= 16)
286 wiosize |= (1 << 3);
287 break;
288 }
289 reg_var[var_num++] =
290 base & 0xffff;
291 } else {
292 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
293 dev_path(child), base, end);
294 }
295 }
296 }
297 }
298 }
299 pci_write_config32(dev, 0x44, reg);
300 pci_write_config32(dev, 0x48, reg_x);
301 /* Set WideIO for as many IOs found (fall through is on purpose) */
302 switch (var_num) {
303 case 3:
304 pci_write_config16(dev, 0x90, reg_var[2]);
305 /* fall through */
306 case 2:
307 pci_write_config16(dev, 0x66, reg_var[1]);
308 /* fall through */
309 case 1:
310 pci_write_config16(dev, 0x64, reg_var[0]);
311 break;
312 }
313 pci_write_config8(dev, 0x74, wiosize);
zbao246e84b2012-07-13 18:47:03 +0800314}
315
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200316static void hudson_lpc_enable_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800317{
318 pci_dev_enable_resources(dev);
319 hudson_lpc_enable_childrens_resources(dev);
320}
321
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400322static const char *lpc_acpi_name(const struct device *dev)
323{
324 if (dev->path.type != DEVICE_PATH_PCI)
325 return NULL;
326
327 if (dev->path.pci.devfn == LPC_DEVFN)
328 return "LIBR";
329
330 return NULL;
331}
332
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200333unsigned long acpi_fill_mcfg(unsigned long current)
334{
335 /* Just a dummy */
336 return current;
337}
338
zbao246e84b2012-07-13 18:47:03 +0800339static struct pci_operations lops_pci = {
340 .set_subsystem = pci_dev_set_subsystem,
341};
342
343static struct device_operations lpc_ops = {
344 .read_resources = hudson_lpc_read_resources,
345 .set_resources = hudson_lpc_set_resources,
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000346 .enable_resources = hudson_lpc_enable_resources,
Vladimir Serbinenko83f81ca2014-11-09 13:30:50 +0100347#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200348 .write_acpi_tables = acpi_write_hpet,
349#endif
zbao246e84b2012-07-13 18:47:03 +0800350 .init = lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200351 .scan_bus = scan_lpc_bus,
zbao246e84b2012-07-13 18:47:03 +0800352 .ops_pci = &lops_pci,
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400353 .acpi_name = lpc_acpi_name,
zbao246e84b2012-07-13 18:47:03 +0800354};
355static const struct pci_driver lpc_driver __pci_driver = {
356 .ops = &lpc_ops,
357 .vendor = PCI_VENDOR_ID_AMD,
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200358 .device = PCI_DEVICE_ID_AMD_SB900_LPC,
zbao246e84b2012-07-13 18:47:03 +0800359};