blob: e65fd838b0436f140ea1413c0ad9898721b9cb65 [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>
Kyösti Mälkkif3758b62019-10-08 19:25:57 +030026#include <arch/io.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030027#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>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020031#include <types.h>
32
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030033#include "hudson.h"
Philipp Deppenwiese30670122017-03-01 02:24:33 +010034#include "pci_devs.h"
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030035
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020036static void lpc_init(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030037{
38 u8 byte;
39 u32 dword;
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020040 struct device *sm_dev;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030041
42 /* Enable the LPC Controller */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +030043 sm_dev = pcidev_on_root(0x14, 0);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030044 dword = pci_read_config32(sm_dev, 0x64);
45 dword |= 1 << 20;
46 pci_write_config32(sm_dev, 0x64, dword);
47
48 /* Initialize isa dma */
49 isa_dma_init();
50
51 /* Enable DMA transaction on the LPC bus */
52 byte = pci_read_config8(dev, 0x40);
53 byte |= (1 << 2);
54 pci_write_config8(dev, 0x40, byte);
55
56 /* Disable the timeout mechanism on LPC */
57 byte = pci_read_config8(dev, 0x48);
58 byte &= ~(1 << 7);
59 pci_write_config8(dev, 0x48, byte);
60
61 /* Disable LPC MSI Capability */
62 byte = pci_read_config8(dev, 0x78);
63 byte &= ~(1 << 1);
64 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
65 on on LPC, it holds PCI grant, so no LPC slave cycle can
66 interrupt and visit LPC. */
67 pci_write_config8(dev, 0x78, byte);
68
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020069 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030070 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
71 byte = pci_read_config8(dev, 0xBB);
72 byte |= 1 << 0 | 1 << 3;
73 pci_write_config8(dev, 0xBB, byte);
74
Gabe Black03abaee212014-04-30 21:31:44 -070075 cmos_check_update_date();
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030076
77 /* Initialize the real time clock.
78 * The 0 argument tells cmos_init not to
79 * update CMOS unless it is invalid.
80 * 1 tells cmos_init to always initialize the CMOS.
81 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060082 cmos_init(0);
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060083
84 /* Initialize i8259 pic */
85 setup_i8259 ();
86
87 /* Initialize i8254 timers */
88 setup_i8254 ();
Marc Jones3eec9dd2017-04-09 18:00:40 -060089
Paul Menzel4c402292017-04-14 17:23:49 +020090 /* Set up SERIRQ, enable continuous mode */
Marc Jones3eec9dd2017-04-09 18:00:40 -060091 byte = (BIT(4) | BIT(7));
Julius Wernercd49cce2019-03-05 16:53:33 -080092 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
Marc Jones3eec9dd2017-04-09 18:00:40 -060093 byte |= BIT(6);
94
95 pm_write8(PM_SERIRQ_CONF, byte);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030096}
97
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020098static void hudson_lpc_read_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030099{
100 struct resource *res;
101
102 /* Get the normal pci resources of this device */
103 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
104
105 /* Add an extra subtractive resource for both memory and I/O. */
106 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
107 res->base = 0;
108 res->size = 0x1000;
109 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
110 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
111
112 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
113 res->base = 0xff800000;
114 res->size = 0x00800000; /* 8 MB for flash */
115 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
116 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
117
118 /* Add a memory resource for the SPI BAR. */
119 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
120
121 res = new_resource(dev, 3); /* IOAPIC */
122 res->base = IO_APIC_ADDR;
123 res->size = 0x00001000;
124 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
125
126 compact_resources(dev);
127}
128
129static void hudson_lpc_set_resources(struct device *dev)
130{
131 struct resource *res;
Marc Jones6fcaaef2017-04-20 16:48:42 -0600132 u32 spi_enable_bits;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300133
Marc Jones6fcaaef2017-04-20 16:48:42 -0600134 /* Special case. The SpiRomEnable and other enables should STAY set. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300135 res = find_resource(dev, 2);
Marc Jones6fcaaef2017-04-20 16:48:42 -0600136 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
137 spi_enable_bits &= 0xF;
138 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300139
140 pci_dev_set_resources(dev);
141}
142
143/**
144 * @brief Enable resources for children devices
145 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000146 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300147 *
148 */
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200149static void hudson_lpc_enable_childrens_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300150{
151 struct bus *link;
152 u32 reg, reg_x;
153 int var_num = 0;
154 u16 reg_var[3];
155 u16 reg_size[1] = {512};
156 u8 wiosize = pci_read_config8(dev, 0x74);
157
158 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
159 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
160 * it must be 512 bytes to be able to allocate the fresh LPC window.
161 *
162 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
163 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
164 * The code tries to check if resource can fit into this region
165 */
166
167 reg = pci_read_config32(dev, 0x44);
168 reg_x = pci_read_config32(dev, 0x48);
169
170 /* check if ranges are free and not use them if entry is just already taken */
171 if (reg_x & (1 << 2))
172 var_num = 1;
173 /* just in case check if someone did not manually set other ranges too */
174 if (reg_x & (1 << 24))
175 var_num = 2;
176
177 if (reg_x & (1 << 25))
178 var_num = 3;
179
180 /* check AGESA region size */
181 if (wiosize & (1 << 0))
182 reg_size[0] = 16;
183
184 reg_var[2] = pci_read_config16(dev, 0x90);
185 reg_var[1] = pci_read_config16(dev, 0x66);
186 reg_var[0] = pci_read_config16(dev, 0x64);
187
188 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200189 struct device *child;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300190 for (child = link->children; child;
191 child = child->sibling) {
192 if (child->enabled
193 && (child->path.type == DEVICE_PATH_PNP)) {
194 struct resource *res;
195 for (res = child->resource_list; res; res = res->next) {
196 u32 base, end; /* don't need long long */
197 u32 rsize, set = 0, set_x = 0;
198 if (!(res->flags & IORESOURCE_IO))
199 continue;
200 base = res->base;
201 end = resource_end(res);
202 /* find a resource size */
203 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
204 dev_path(child), base, end);
205 switch (base) {
206 case 0x60: /* KB */
207 case 0x64: /* MS */
208 set |= (1 << 29);
209 rsize = 1;
210 break;
211 case 0x3f8: /* COM1 */
212 set |= (1 << 6);
213 rsize = 8;
214 break;
215 case 0x2f8: /* COM2 */
216 set |= (1 << 7);
217 rsize = 8;
218 break;
219 case 0x378: /* Parallel 1 */
220 set |= (1 << 0);
221 set |= (1 << 1); /* + 0x778 for ECP */
222 rsize = 8;
223 break;
224 case 0x3f0: /* FD0 */
225 set |= (1 << 26);
226 rsize = 8;
227 break;
228 case 0x220: /* 0x220 - 0x227 */
229 set |= (1 << 8);
230 rsize = 8;
231 break;
232 case 0x228: /* 0x228 - 0x22f */
233 set |= (1 << 9);
234 rsize = 8;
235 break;
236 case 0x238: /* 0x238 - 0x23f */
237 set |= (1 << 10);
238 rsize = 8;
239 break;
240 case 0x300: /* 0x300 -0x301 */
241 set |= (1 << 18);
242 rsize = 2;
243 break;
244 case 0x400:
245 set_x |= (1 << 16);
246 rsize = 0x40;
247 break;
248 case 0x480:
249 set_x |= (1 << 17);
250 rsize = 0x40;
251 break;
252 case 0x500:
253 set_x |= (1 << 18);
254 rsize = 0x40;
255 break;
256 case 0x580:
257 set_x |= (1 << 19);
258 rsize = 0x40;
259 break;
260 case 0x4700:
261 set_x |= (1 << 22);
262 rsize = 0xc;
263 break;
264 case 0xfd60:
265 set_x |= (1 << 23);
266 rsize = 16;
267 break;
268 default:
269 rsize = 0;
270 /* try AGESA allocated region in region 0 */
271 if ((var_num > 0) && ((base >=reg_var[0]) &&
272 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
273 rsize = reg_size[0];
274 }
275 /* check if region found and matches the enable */
276 if (res->size <= rsize) {
277 reg |= set;
278 reg_x |= set_x;
279 /* check if we can fit resource in variable range */
280 } else if ((var_num < 3) &&
281 ((res->size <= 16) || (res->size == 512))) {
282 /* use variable ranges if pre-defined do not match */
283 switch (var_num) {
284 case 0:
285 reg_x |= (1 << 2);
286 if (res->size <= 16) {
287 wiosize |= (1 << 0);
288 }
289 break;
290 case 1:
291 reg_x |= (1 << 24);
292 if (res->size <= 16)
293 wiosize |= (1 << 2);
294 break;
295 case 2:
296 reg_x |= (1 << 25);
297 if (res->size <= 16)
298 wiosize |= (1 << 3);
299 break;
300 }
301 reg_var[var_num++] =
302 base & 0xffff;
303 } else {
304 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
305 dev_path(child), base, end);
306 }
307 }
308 }
309 }
310 }
311 pci_write_config32(dev, 0x44, reg);
312 pci_write_config32(dev, 0x48, reg_x);
313 /* Set WideIO for as many IOs found (fall through is on purpose) */
314 switch (var_num) {
315 case 3:
316 pci_write_config16(dev, 0x90, reg_var[2]);
317 /* fall through */
318 case 2:
319 pci_write_config16(dev, 0x66, reg_var[1]);
320 /* fall through */
321 case 1:
322 pci_write_config16(dev, 0x64, reg_var[0]);
323 break;
324 }
325 pci_write_config8(dev, 0x74, wiosize);
326}
327
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200328static void hudson_lpc_enable_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300329{
330 pci_dev_enable_resources(dev);
331 hudson_lpc_enable_childrens_resources(dev);
332}
333
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000334unsigned long acpi_fill_mcfg(unsigned long current)
335{
Michał Żygowski033435b2019-09-06 19:18:24 +0200336 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
337 CONFIG_MMCONF_BASE_ADDRESS,
338 0,
339 0,
340 CONFIG_MMCONF_BUS_NUMBER);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000341 return current;
342}
343
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600344static const char *lpc_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100345{
346 if (dev->path.type != DEVICE_PATH_PCI)
347 return NULL;
348
349 if (dev->path.pci.devfn == LPC_DEVFN)
350 return "LIBR";
351
352 return NULL;
353}
354
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300355static void lpc_final(struct device *dev)
356{
357 if (!acpi_is_wakeup_s3()) {
358 if (CONFIG(HAVE_SMI_HANDLER))
359 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
360 else
361 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
362 }
363}
364
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300365static struct pci_operations lops_pci = {
366 .set_subsystem = pci_dev_set_subsystem,
367};
368
369static struct device_operations lpc_ops = {
370 .read_resources = hudson_lpc_read_resources,
371 .set_resources = hudson_lpc_set_resources,
372 .enable_resources = hudson_lpc_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800373#if CONFIG(HAVE_ACPI_TABLES)
Dave Frodin5c015f02015-01-27 07:19:04 -0700374 .write_acpi_tables = acpi_write_hpet,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200375#endif
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300376 .init = lpc_init,
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300377 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100378 .scan_bus = scan_static_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300379 .ops_pci = &lops_pci,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100380 .acpi_name = lpc_acpi_name,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300381};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800382
383static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200384 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800385 PCI_DEVICE_ID_AMD_CZ_LPC,
386 0
387};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300388static const struct pci_driver lpc_driver __pci_driver = {
389 .ops = &lpc_ops,
390 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800391 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300392};