blob: 0b579842a648cdf7c56b63bd620f3ba118236e6b [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
zbao246e84b2012-07-13 18:47:03 +08003
Michał Żygowskif3db2ae2019-11-24 13:26:10 +01004#include <amdblocks/acpimmio.h>
zbao246e84b2012-07-13 18:47:03 +08005#include <console/console.h>
6#include <device/device.h>
7#include <device/pci.h>
8#include <device/pnp.h>
9#include <device/pci_ids.h>
10#include <device/pci_ops.h>
Martin Roth3aef7b42012-12-05 15:50:32 -070011#include <device/pci_def.h>
zbao246e84b2012-07-13 18:47:03 +080012#include <pc80/mc146818rtc.h>
13#include <pc80/isa-dma.h>
Kyösti Mälkkif3758b62019-10-08 19:25:57 +030014#include <arch/io.h>
Dave Frodin8ef20cf2014-06-05 14:21:11 -060015#include <arch/ioapic.h>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020016#include <arch/acpi.h>
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100017#include <pc80/i8254.h>
18#include <pc80/i8259.h>
zbao246e84b2012-07-13 18:47:03 +080019#include "hudson.h"
Kevin Cody-Littlec0984002018-05-09 14:25:44 -040020#include "pci_devs.h"
zbao246e84b2012-07-13 18:47:03 +080021
Elyes HAOUASa93e7542018-05-19 14:30:47 +020022static void lpc_init(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080023{
24 u8 byte;
25 u32 dword;
Elyes HAOUASa93e7542018-05-19 14:30:47 +020026 struct device *sm_dev;
zbao246e84b2012-07-13 18:47:03 +080027
28 /* Enable the LPC Controller */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +030029 sm_dev = pcidev_on_root(0x14, 0);
zbao246e84b2012-07-13 18:47:03 +080030 dword = pci_read_config32(sm_dev, 0x64);
31 dword |= 1 << 20;
32 pci_write_config32(sm_dev, 0x64, dword);
33
34 /* Initialize isa dma */
35 isa_dma_init();
36
37 /* Enable DMA transaction on the LPC bus */
38 byte = pci_read_config8(dev, 0x40);
39 byte |= (1 << 2);
40 pci_write_config8(dev, 0x40, byte);
41
42 /* Disable the timeout mechanism on LPC */
43 byte = pci_read_config8(dev, 0x48);
44 byte &= ~(1 << 7);
45 pci_write_config8(dev, 0x48, byte);
46
47 /* Disable LPC MSI Capability */
48 byte = pci_read_config8(dev, 0x78);
49 byte &= ~(1 << 1);
50 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
51 on on LPC, it holds PCI grant, so no LPC slave cycle can
52 interrupt and visit LPC. */
53 pci_write_config8(dev, 0x78, byte);
54
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020055 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
zbao246e84b2012-07-13 18:47:03 +080056 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
57 byte = pci_read_config8(dev, 0xBB);
58 byte |= 1 << 0 | 1 << 3;
59 pci_write_config8(dev, 0xBB, byte);
zbaoef180e22012-08-02 19:03:44 +080060
Gabe Black03abaee212014-04-30 21:31:44 -070061 cmos_check_update_date();
Mike Loptiena96d24d2013-02-25 10:41:28 -070062
63 /* Initialize the real time clock.
Gabe Blackb3f08c62014-04-30 17:12:25 -070064 * The 0 argument tells cmos_init not to
Mike Loptiena96d24d2013-02-25 10:41:28 -070065 * update CMOS unless it is invalid.
Gabe Blackb3f08c62014-04-30 17:12:25 -070066 * 1 tells cmos_init to always initialize the CMOS.
Mike Loptiena96d24d2013-02-25 10:41:28 -070067 */
Gabe Blackb3f08c62014-04-30 17:12:25 -070068 cmos_init(0);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100069
70 /* Initialize i8259 pic */
71 setup_i8259 ();
72
73 /* Initialize i8254 timers */
74 setup_i8254 ();
zbao246e84b2012-07-13 18:47:03 +080075}
76
Elyes HAOUASa93e7542018-05-19 14:30:47 +020077static void hudson_lpc_read_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080078{
79 struct resource *res;
80
81 /* Get the normal pci resources of this device */
82 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
83
zbao246e84b2012-07-13 18:47:03 +080084 /* Add an extra subtractive resource for both memory and I/O. */
85 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
86 res->base = 0;
87 res->size = 0x1000;
88 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
89 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
90
91 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
92 res->base = 0xff800000;
93 res->size = 0x00800000; /* 8 MB for flash */
94 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
95 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
96
Dave Frodinac1b8752014-06-05 14:30:22 -060097 /* Add a memory resource for the SPI BAR. */
98 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
99
Dave Frodin8ef20cf2014-06-05 14:21:11 -0600100 res = new_resource(dev, 3); /* IOAPIC */
101 res->base = IO_APIC_ADDR;
102 res->size = 0x00001000;
103 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
zbao246e84b2012-07-13 18:47:03 +0800104
105 compact_resources(dev);
106}
107
108static void hudson_lpc_set_resources(struct device *dev)
109{
110 struct resource *res;
111
Martin Roth3aef7b42012-12-05 15:50:32 -0700112 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
Dave Frodinac1b8752014-06-05 14:30:22 -0600113 res = find_resource(dev, 2);
114 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
Martin Roth3aef7b42012-12-05 15:50:32 -0700115
zbao246e84b2012-07-13 18:47:03 +0800116 pci_dev_set_resources(dev);
zbao246e84b2012-07-13 18:47:03 +0800117}
118
119/**
120 * @brief Enable resources for children devices
121 *
Martin Roth3c3a50c2014-12-16 20:50:26 -0700122 * @param dev the device whose children's resources are to be enabled
zbao246e84b2012-07-13 18:47:03 +0800123 *
124 */
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200125static void hudson_lpc_enable_childrens_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800126{
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100127 struct bus *link;
128 u32 reg, reg_x;
129 int var_num = 0;
130 u16 reg_var[3];
131 u16 reg_size[1] = {512};
132 u8 wiosize = pci_read_config8(dev, 0x74);
zbao246e84b2012-07-13 18:47:03 +0800133
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100134 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
135 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
136 * it must be 512 bytes to be able to allocate the fresh LPC window.
137 *
138 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
139 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
140 * The code tries to check if resource can fit into this region
141 */
142
143 reg = pci_read_config32(dev, 0x44);
144 reg_x = pci_read_config32(dev, 0x48);
145
146 /* check if ranges are free and not use them if entry is just already taken */
147 if (reg_x & (1 << 2))
148 var_num = 1;
149 /* just in case check if someone did not manually set other ranges too */
150 if (reg_x & (1 << 24))
151 var_num = 2;
152
153 if (reg_x & (1 << 25))
154 var_num = 3;
155
156 /* check AGESA region size */
157 if (wiosize & (1 << 0))
158 reg_size[0] = 16;
159
160 reg_var[2] = pci_read_config16(dev, 0x90);
161 reg_var[1] = pci_read_config16(dev, 0x66);
162 reg_var[0] = pci_read_config16(dev, 0x64);
163
164 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200165 struct device *child;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100166 for (child = link->children; child;
167 child = child->sibling) {
168 if (child->enabled
169 && (child->path.type == DEVICE_PATH_PNP)) {
170 struct resource *res;
171 for (res = child->resource_list; res; res = res->next) {
172 u32 base, end; /* don't need long long */
173 u32 rsize, set = 0, set_x = 0;
174 if (!(res->flags & IORESOURCE_IO))
175 continue;
176 base = res->base;
177 end = resource_end(res);
178 /* find a resource size */
179 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
180 dev_path(child), base, end);
181 switch (base) {
182 case 0x60: /* KB */
183 case 0x64: /* MS */
184 set |= (1 << 29);
185 rsize = 1;
186 break;
187 case 0x3f8: /* COM1 */
188 set |= (1 << 6);
189 rsize = 8;
190 break;
191 case 0x2f8: /* COM2 */
192 set |= (1 << 7);
193 rsize = 8;
194 break;
195 case 0x378: /* Parallel 1 */
196 set |= (1 << 0);
197 set |= (1 << 1); /* + 0x778 for ECP */
198 rsize = 8;
199 break;
200 case 0x3f0: /* FD0 */
201 set |= (1 << 26);
202 rsize = 8;
203 break;
204 case 0x220: /* 0x220 - 0x227 */
205 set |= (1 << 8);
206 rsize = 8;
207 break;
208 case 0x228: /* 0x228 - 0x22f */
209 set |= (1 << 9);
210 rsize = 8;
211 break;
212 case 0x238: /* 0x238 - 0x23f */
213 set |= (1 << 10);
214 rsize = 8;
215 break;
216 case 0x300: /* 0x300 -0x301 */
217 set |= (1 << 18);
218 rsize = 2;
219 break;
220 case 0x400:
221 set_x |= (1 << 16);
222 rsize = 0x40;
223 break;
224 case 0x480:
225 set_x |= (1 << 17);
226 rsize = 0x40;
Daniele Forsib532b122014-07-26 10:32:34 +0200227 break;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100228 case 0x500:
229 set_x |= (1 << 18);
230 rsize = 0x40;
231 break;
232 case 0x580:
233 set_x |= (1 << 19);
234 rsize = 0x40;
235 break;
236 case 0x4700:
237 set_x |= (1 << 22);
238 rsize = 0xc;
239 break;
240 case 0xfd60:
241 set_x |= (1 << 23);
242 rsize = 16;
243 break;
244 default:
245 rsize = 0;
246 /* try AGESA allocated region in region 0 */
247 if ((var_num > 0) && ((base >=reg_var[0]) &&
248 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
249 rsize = reg_size[0];
250 }
251 /* check if region found and matches the enable */
252 if (res->size <= rsize) {
253 reg |= set;
254 reg_x |= set_x;
255 /* check if we can fit resource in variable range */
256 } else if ((var_num < 3) &&
257 ((res->size <= 16) || (res->size == 512))) {
258 /* use variable ranges if pre-defined do not match */
259 switch (var_num) {
260 case 0:
261 reg_x |= (1 << 2);
262 if (res->size <= 16) {
263 wiosize |= (1 << 0);
264 }
265 break;
266 case 1:
267 reg_x |= (1 << 24);
268 if (res->size <= 16)
269 wiosize |= (1 << 2);
270 break;
271 case 2:
272 reg_x |= (1 << 25);
273 if (res->size <= 16)
274 wiosize |= (1 << 3);
275 break;
276 }
277 reg_var[var_num++] =
278 base & 0xffff;
279 } else {
280 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
281 dev_path(child), base, end);
282 }
283 }
284 }
285 }
286 }
287 pci_write_config32(dev, 0x44, reg);
288 pci_write_config32(dev, 0x48, reg_x);
289 /* Set WideIO for as many IOs found (fall through is on purpose) */
290 switch (var_num) {
291 case 3:
292 pci_write_config16(dev, 0x90, reg_var[2]);
293 /* fall through */
294 case 2:
295 pci_write_config16(dev, 0x66, reg_var[1]);
296 /* fall through */
297 case 1:
298 pci_write_config16(dev, 0x64, reg_var[0]);
299 break;
300 }
301 pci_write_config8(dev, 0x74, wiosize);
zbao246e84b2012-07-13 18:47:03 +0800302}
303
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200304static void hudson_lpc_enable_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800305{
306 pci_dev_enable_resources(dev);
307 hudson_lpc_enable_childrens_resources(dev);
308}
309
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400310static const char *lpc_acpi_name(const struct device *dev)
311{
312 if (dev->path.type != DEVICE_PATH_PCI)
313 return NULL;
314
315 if (dev->path.pci.devfn == LPC_DEVFN)
316 return "LIBR";
317
318 return NULL;
319}
320
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200321unsigned long acpi_fill_mcfg(unsigned long current)
322{
323 /* Just a dummy */
324 return current;
325}
326
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300327static void lpc_final(struct device *dev)
328{
329 if (!acpi_is_wakeup_s3()) {
330 if (CONFIG(HAVE_SMI_HANDLER))
331 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
332 else
333 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
334 }
335}
336
zbao246e84b2012-07-13 18:47:03 +0800337static 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,
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000344 .enable_resources = hudson_lpc_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800345#if CONFIG(HAVE_ACPI_TABLES)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200346 .write_acpi_tables = acpi_write_hpet,
347#endif
zbao246e84b2012-07-13 18:47:03 +0800348 .init = lpc_init,
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300349 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100350 .scan_bus = scan_static_bus,
zbao246e84b2012-07-13 18:47:03 +0800351 .ops_pci = &lops_pci,
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400352 .acpi_name = lpc_acpi_name,
zbao246e84b2012-07-13 18:47:03 +0800353};
354static const struct pci_driver lpc_driver __pci_driver = {
355 .ops = &lpc_ops,
356 .vendor = PCI_VENDOR_ID_AMD,
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200357 .device = PCI_DEVICE_ID_AMD_SB900_LPC,
zbao246e84b2012-07-13 18:47:03 +0800358};