blob: c8f37a470a27f6ece8f74e5ac4cb38992669f4a0 [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. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03003
Michał Żygowskif3db2ae2019-11-24 13:26:10 +01004#include <amdblocks/acpimmio.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03005#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>
11#include <device/pci_def.h>
12#include <pc80/mc146818rtc.h>
13#include <pc80/isa-dma.h>
Kyösti Mälkkif3758b62019-10-08 19:25:57 +030014#include <arch/io.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030015#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070016#include <acpi/acpi.h>
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060017#include <pc80/i8254.h>
18#include <pc80/i8259.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020019#include <types.h>
20
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030021#include "hudson.h"
Philipp Deppenwiese30670122017-03-01 02:24:33 +010022#include "pci_devs.h"
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030023
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020024static void lpc_init(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030025{
26 u8 byte;
27 u32 dword;
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020028 struct device *sm_dev;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030029
30 /* Enable the LPC Controller */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +030031 sm_dev = pcidev_on_root(0x14, 0);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030032 dword = pci_read_config32(sm_dev, 0x64);
33 dword |= 1 << 20;
34 pci_write_config32(sm_dev, 0x64, dword);
35
36 /* Initialize isa dma */
37 isa_dma_init();
38
39 /* Enable DMA transaction on the LPC bus */
40 byte = pci_read_config8(dev, 0x40);
41 byte |= (1 << 2);
42 pci_write_config8(dev, 0x40, byte);
43
44 /* Disable the timeout mechanism on LPC */
45 byte = pci_read_config8(dev, 0x48);
46 byte &= ~(1 << 7);
47 pci_write_config8(dev, 0x48, byte);
48
49 /* Disable LPC MSI Capability */
50 byte = pci_read_config8(dev, 0x78);
51 byte &= ~(1 << 1);
52 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
53 on on LPC, it holds PCI grant, so no LPC slave cycle can
54 interrupt and visit LPC. */
55 pci_write_config8(dev, 0x78, byte);
56
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020057 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030058 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
59 byte = pci_read_config8(dev, 0xBB);
60 byte |= 1 << 0 | 1 << 3;
61 pci_write_config8(dev, 0xBB, byte);
62
Gabe Black03abaee212014-04-30 21:31:44 -070063 cmos_check_update_date();
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030064
65 /* Initialize the real time clock.
66 * The 0 argument tells cmos_init not to
67 * update CMOS unless it is invalid.
68 * 1 tells cmos_init to always initialize the CMOS.
69 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060070 cmos_init(0);
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060071
72 /* Initialize i8259 pic */
73 setup_i8259 ();
74
75 /* Initialize i8254 timers */
76 setup_i8254 ();
Marc Jones3eec9dd2017-04-09 18:00:40 -060077
Paul Menzel4c402292017-04-14 17:23:49 +020078 /* Set up SERIRQ, enable continuous mode */
Marc Jones3eec9dd2017-04-09 18:00:40 -060079 byte = (BIT(4) | BIT(7));
Julius Wernercd49cce2019-03-05 16:53:33 -080080 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
Marc Jones3eec9dd2017-04-09 18:00:40 -060081 byte |= BIT(6);
82
83 pm_write8(PM_SERIRQ_CONF, byte);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030084}
85
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020086static void hudson_lpc_read_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030087{
88 struct resource *res;
89
90 /* Get the normal pci resources of this device */
91 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
92
93 /* Add an extra subtractive resource for both memory and I/O. */
94 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
95 res->base = 0;
96 res->size = 0x1000;
97 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
98 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
99
100 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
101 res->base = 0xff800000;
102 res->size = 0x00800000; /* 8 MB for flash */
103 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
104 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
105
106 /* Add a memory resource for the SPI BAR. */
107 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
108
109 res = new_resource(dev, 3); /* IOAPIC */
110 res->base = IO_APIC_ADDR;
111 res->size = 0x00001000;
112 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
113
114 compact_resources(dev);
115}
116
117static void hudson_lpc_set_resources(struct device *dev)
118{
119 struct resource *res;
Marc Jones6fcaaef2017-04-20 16:48:42 -0600120 u32 spi_enable_bits;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300121
Marc Jones6fcaaef2017-04-20 16:48:42 -0600122 /* Special case. The SpiRomEnable and other enables should STAY set. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300123 res = find_resource(dev, 2);
Marc Jones6fcaaef2017-04-20 16:48:42 -0600124 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
125 spi_enable_bits &= 0xF;
126 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300127
128 pci_dev_set_resources(dev);
129}
130
131/**
132 * @brief Enable resources for children devices
133 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000134 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300135 *
136 */
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200137static void hudson_lpc_enable_childrens_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300138{
139 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);
145
146 /* 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 HAOUASd9ef5462018-05-19 17:08:23 +0200177 struct device *child;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300178 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;
239 break;
240 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);
314}
315
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200316static void hudson_lpc_enable_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300317{
318 pci_dev_enable_resources(dev);
319 hudson_lpc_enable_childrens_resources(dev);
320}
321
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000322unsigned long acpi_fill_mcfg(unsigned long current)
323{
Michał Żygowski033435b2019-09-06 19:18:24 +0200324 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
325 CONFIG_MMCONF_BASE_ADDRESS,
326 0,
327 0,
328 CONFIG_MMCONF_BUS_NUMBER);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000329 return current;
330}
331
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600332static const char *lpc_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100333{
334 if (dev->path.type != DEVICE_PATH_PCI)
335 return NULL;
336
337 if (dev->path.pci.devfn == LPC_DEVFN)
338 return "LIBR";
339
340 return NULL;
341}
342
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300343static void lpc_final(struct device *dev)
344{
345 if (!acpi_is_wakeup_s3()) {
346 if (CONFIG(HAVE_SMI_HANDLER))
347 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
348 else
349 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
350 }
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,
Julius Wernercd49cce2019-03-05 16:53:33 -0800361#if 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älkkif3758b62019-10-08 19:25:57 +0300365 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100366 .scan_bus = scan_static_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300367 .ops_pci = &lops_pci,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100368 .acpi_name = lpc_acpi_name,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300369};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800370
371static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200372 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800373 PCI_DEVICE_ID_AMD_CZ_LPC,
374 0
375};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300376static const struct pci_driver lpc_driver __pci_driver = {
377 .ops = &lpc_ops,
378 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800379 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300380};