blob: 239dda8e74fee6bdf0a5df9c567e1a7ec32b4ff0 [file] [log] [blame]
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03001/*
2 * This file is part of the coreboot project.
3 *
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030013 */
14
Michał Żygowskif3db2ae2019-11-24 13:26:10 +010015#include <amdblocks/acpimmio.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030016#include <console/console.h>
17#include <device/device.h>
18#include <device/pci.h>
19#include <device/pnp.h>
20#include <device/pci_ids.h>
21#include <device/pci_ops.h>
22#include <device/pci_def.h>
23#include <pc80/mc146818rtc.h>
24#include <pc80/isa-dma.h>
Kyösti Mälkkif3758b62019-10-08 19:25:57 +030025#include <arch/io.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030026#include <arch/ioapic.h>
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100027#include <arch/acpi.h>
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060028#include <pc80/i8254.h>
29#include <pc80/i8259.h>
Elyes HAOUASab89edb2019-05-15 21:10:44 +020030#include <types.h>
31
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030032#include "hudson.h"
Philipp Deppenwiese30670122017-03-01 02:24:33 +010033#include "pci_devs.h"
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030034
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020035static void lpc_init(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030036{
37 u8 byte;
38 u32 dword;
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020039 struct device *sm_dev;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030040
41 /* Enable the LPC Controller */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +030042 sm_dev = pcidev_on_root(0x14, 0);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030043 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 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060081 cmos_init(0);
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060082
83 /* Initialize i8259 pic */
84 setup_i8259 ();
85
86 /* Initialize i8254 timers */
87 setup_i8254 ();
Marc Jones3eec9dd2017-04-09 18:00:40 -060088
Paul Menzel4c402292017-04-14 17:23:49 +020089 /* Set up SERIRQ, enable continuous mode */
Marc Jones3eec9dd2017-04-09 18:00:40 -060090 byte = (BIT(4) | BIT(7));
Julius Wernercd49cce2019-03-05 16:53:33 -080091 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
Marc Jones3eec9dd2017-04-09 18:00:40 -060092 byte |= BIT(6);
93
94 pm_write8(PM_SERIRQ_CONF, byte);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030095}
96
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020097static void hudson_lpc_read_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030098{
99 struct resource *res;
100
101 /* Get the normal pci resources of this device */
102 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
103
104 /* Add an extra subtractive resource for both memory and I/O. */
105 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
106 res->base = 0;
107 res->size = 0x1000;
108 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
109 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
110
111 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
112 res->base = 0xff800000;
113 res->size = 0x00800000; /* 8 MB for flash */
114 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
115 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
116
117 /* Add a memory resource for the SPI BAR. */
118 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
119
120 res = new_resource(dev, 3); /* IOAPIC */
121 res->base = IO_APIC_ADDR;
122 res->size = 0x00001000;
123 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
124
125 compact_resources(dev);
126}
127
128static void hudson_lpc_set_resources(struct device *dev)
129{
130 struct resource *res;
Marc Jones6fcaaef2017-04-20 16:48:42 -0600131 u32 spi_enable_bits;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300132
Marc Jones6fcaaef2017-04-20 16:48:42 -0600133 /* Special case. The SpiRomEnable and other enables should STAY set. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300134 res = find_resource(dev, 2);
Marc Jones6fcaaef2017-04-20 16:48:42 -0600135 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
136 spi_enable_bits &= 0xF;
137 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300138
139 pci_dev_set_resources(dev);
140}
141
142/**
143 * @brief Enable resources for children devices
144 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000145 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300146 *
147 */
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200148static void hudson_lpc_enable_childrens_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300149{
150 struct bus *link;
151 u32 reg, reg_x;
152 int var_num = 0;
153 u16 reg_var[3];
154 u16 reg_size[1] = {512};
155 u8 wiosize = pci_read_config8(dev, 0x74);
156
157 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
158 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
159 * it must be 512 bytes to be able to allocate the fresh LPC window.
160 *
161 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
162 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
163 * The code tries to check if resource can fit into this region
164 */
165
166 reg = pci_read_config32(dev, 0x44);
167 reg_x = pci_read_config32(dev, 0x48);
168
169 /* check if ranges are free and not use them if entry is just already taken */
170 if (reg_x & (1 << 2))
171 var_num = 1;
172 /* just in case check if someone did not manually set other ranges too */
173 if (reg_x & (1 << 24))
174 var_num = 2;
175
176 if (reg_x & (1 << 25))
177 var_num = 3;
178
179 /* check AGESA region size */
180 if (wiosize & (1 << 0))
181 reg_size[0] = 16;
182
183 reg_var[2] = pci_read_config16(dev, 0x90);
184 reg_var[1] = pci_read_config16(dev, 0x66);
185 reg_var[0] = pci_read_config16(dev, 0x64);
186
187 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200188 struct device *child;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300189 for (child = link->children; child;
190 child = child->sibling) {
191 if (child->enabled
192 && (child->path.type == DEVICE_PATH_PNP)) {
193 struct resource *res;
194 for (res = child->resource_list; res; res = res->next) {
195 u32 base, end; /* don't need long long */
196 u32 rsize, set = 0, set_x = 0;
197 if (!(res->flags & IORESOURCE_IO))
198 continue;
199 base = res->base;
200 end = resource_end(res);
201 /* find a resource size */
202 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
203 dev_path(child), base, end);
204 switch (base) {
205 case 0x60: /* KB */
206 case 0x64: /* MS */
207 set |= (1 << 29);
208 rsize = 1;
209 break;
210 case 0x3f8: /* COM1 */
211 set |= (1 << 6);
212 rsize = 8;
213 break;
214 case 0x2f8: /* COM2 */
215 set |= (1 << 7);
216 rsize = 8;
217 break;
218 case 0x378: /* Parallel 1 */
219 set |= (1 << 0);
220 set |= (1 << 1); /* + 0x778 for ECP */
221 rsize = 8;
222 break;
223 case 0x3f0: /* FD0 */
224 set |= (1 << 26);
225 rsize = 8;
226 break;
227 case 0x220: /* 0x220 - 0x227 */
228 set |= (1 << 8);
229 rsize = 8;
230 break;
231 case 0x228: /* 0x228 - 0x22f */
232 set |= (1 << 9);
233 rsize = 8;
234 break;
235 case 0x238: /* 0x238 - 0x23f */
236 set |= (1 << 10);
237 rsize = 8;
238 break;
239 case 0x300: /* 0x300 -0x301 */
240 set |= (1 << 18);
241 rsize = 2;
242 break;
243 case 0x400:
244 set_x |= (1 << 16);
245 rsize = 0x40;
246 break;
247 case 0x480:
248 set_x |= (1 << 17);
249 rsize = 0x40;
250 break;
251 case 0x500:
252 set_x |= (1 << 18);
253 rsize = 0x40;
254 break;
255 case 0x580:
256 set_x |= (1 << 19);
257 rsize = 0x40;
258 break;
259 case 0x4700:
260 set_x |= (1 << 22);
261 rsize = 0xc;
262 break;
263 case 0xfd60:
264 set_x |= (1 << 23);
265 rsize = 16;
266 break;
267 default:
268 rsize = 0;
269 /* try AGESA allocated region in region 0 */
270 if ((var_num > 0) && ((base >=reg_var[0]) &&
271 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
272 rsize = reg_size[0];
273 }
274 /* check if region found and matches the enable */
275 if (res->size <= rsize) {
276 reg |= set;
277 reg_x |= set_x;
278 /* check if we can fit resource in variable range */
279 } else if ((var_num < 3) &&
280 ((res->size <= 16) || (res->size == 512))) {
281 /* use variable ranges if pre-defined do not match */
282 switch (var_num) {
283 case 0:
284 reg_x |= (1 << 2);
285 if (res->size <= 16) {
286 wiosize |= (1 << 0);
287 }
288 break;
289 case 1:
290 reg_x |= (1 << 24);
291 if (res->size <= 16)
292 wiosize |= (1 << 2);
293 break;
294 case 2:
295 reg_x |= (1 << 25);
296 if (res->size <= 16)
297 wiosize |= (1 << 3);
298 break;
299 }
300 reg_var[var_num++] =
301 base & 0xffff;
302 } else {
303 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
304 dev_path(child), base, end);
305 }
306 }
307 }
308 }
309 }
310 pci_write_config32(dev, 0x44, reg);
311 pci_write_config32(dev, 0x48, reg_x);
312 /* Set WideIO for as many IOs found (fall through is on purpose) */
313 switch (var_num) {
314 case 3:
315 pci_write_config16(dev, 0x90, reg_var[2]);
316 /* fall through */
317 case 2:
318 pci_write_config16(dev, 0x66, reg_var[1]);
319 /* fall through */
320 case 1:
321 pci_write_config16(dev, 0x64, reg_var[0]);
322 break;
323 }
324 pci_write_config8(dev, 0x74, wiosize);
325}
326
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200327static void hudson_lpc_enable_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300328{
329 pci_dev_enable_resources(dev);
330 hudson_lpc_enable_childrens_resources(dev);
331}
332
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000333unsigned long acpi_fill_mcfg(unsigned long current)
334{
Michał Żygowski033435b2019-09-06 19:18:24 +0200335 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
336 CONFIG_MMCONF_BASE_ADDRESS,
337 0,
338 0,
339 CONFIG_MMCONF_BUS_NUMBER);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000340 return current;
341}
342
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600343static const char *lpc_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100344{
345 if (dev->path.type != DEVICE_PATH_PCI)
346 return NULL;
347
348 if (dev->path.pci.devfn == LPC_DEVFN)
349 return "LIBR";
350
351 return NULL;
352}
353
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300354static void lpc_final(struct device *dev)
355{
356 if (!acpi_is_wakeup_s3()) {
357 if (CONFIG(HAVE_SMI_HANDLER))
358 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
359 else
360 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
361 }
362}
363
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300364static struct pci_operations lops_pci = {
365 .set_subsystem = pci_dev_set_subsystem,
366};
367
368static struct device_operations lpc_ops = {
369 .read_resources = hudson_lpc_read_resources,
370 .set_resources = hudson_lpc_set_resources,
371 .enable_resources = hudson_lpc_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800372#if CONFIG(HAVE_ACPI_TABLES)
Dave Frodin5c015f02015-01-27 07:19:04 -0700373 .write_acpi_tables = acpi_write_hpet,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200374#endif
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300375 .init = lpc_init,
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300376 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100377 .scan_bus = scan_static_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300378 .ops_pci = &lops_pci,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100379 .acpi_name = lpc_acpi_name,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300380};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800381
382static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200383 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800384 PCI_DEVICE_ID_AMD_CZ_LPC,
385 0
386};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300387static const struct pci_driver lpc_driver __pci_driver = {
388 .ops = &lpc_ops,
389 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800390 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300391};