blob: 89c9a6b9a1eb3f41e982adac0fc85afd210cae87 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
zbao246e84b2012-07-13 18:47:03 +08002
Michał Żygowskif3db2ae2019-11-24 13:26:10 +01003#include <amdblocks/acpimmio.h>
zbao246e84b2012-07-13 18:47:03 +08004#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pnp.h>
8#include <device/pci_ids.h>
9#include <device/pci_ops.h>
Martin Roth3aef7b42012-12-05 15:50:32 -070010#include <device/pci_def.h>
zbao246e84b2012-07-13 18:47:03 +080011#include <pc80/mc146818rtc.h>
12#include <pc80/isa-dma.h>
Kyösti Mälkkif3758b62019-10-08 19:25:57 +030013#include <arch/io.h>
Dave Frodin8ef20cf2014-06-05 14:21:11 -060014#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070015#include <acpi/acpi.h>
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100016#include <pc80/i8254.h>
17#include <pc80/i8259.h>
zbao246e84b2012-07-13 18:47:03 +080018#include "hudson.h"
Kevin Cody-Littlec0984002018-05-09 14:25:44 -040019#include "pci_devs.h"
zbao246e84b2012-07-13 18:47:03 +080020
Elyes HAOUASa93e7542018-05-19 14:30:47 +020021static void lpc_init(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080022{
23 u8 byte;
24 u32 dword;
Elyes HAOUASa93e7542018-05-19 14:30:47 +020025 struct device *sm_dev;
zbao246e84b2012-07-13 18:47:03 +080026
27 /* Enable the LPC Controller */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +030028 sm_dev = pcidev_on_root(0x14, 0);
zbao246e84b2012-07-13 18:47:03 +080029 dword = pci_read_config32(sm_dev, 0x64);
30 dword |= 1 << 20;
31 pci_write_config32(sm_dev, 0x64, dword);
32
33 /* Initialize isa dma */
34 isa_dma_init();
35
36 /* Enable DMA transaction on the LPC bus */
37 byte = pci_read_config8(dev, 0x40);
38 byte |= (1 << 2);
39 pci_write_config8(dev, 0x40, byte);
40
41 /* Disable the timeout mechanism on LPC */
42 byte = pci_read_config8(dev, 0x48);
43 byte &= ~(1 << 7);
44 pci_write_config8(dev, 0x48, byte);
45
46 /* Disable LPC MSI Capability */
47 byte = pci_read_config8(dev, 0x78);
48 byte &= ~(1 << 1);
49 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
50 on on LPC, it holds PCI grant, so no LPC slave cycle can
51 interrupt and visit LPC. */
52 pci_write_config8(dev, 0x78, byte);
53
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020054 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
zbao246e84b2012-07-13 18:47:03 +080055 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
56 byte = pci_read_config8(dev, 0xBB);
57 byte |= 1 << 0 | 1 << 3;
58 pci_write_config8(dev, 0xBB, byte);
zbaoef180e22012-08-02 19:03:44 +080059
Gabe Black03abaee212014-04-30 21:31:44 -070060 cmos_check_update_date();
Mike Loptiena96d24d2013-02-25 10:41:28 -070061
62 /* Initialize the real time clock.
Gabe Blackb3f08c62014-04-30 17:12:25 -070063 * The 0 argument tells cmos_init not to
Mike Loptiena96d24d2013-02-25 10:41:28 -070064 * update CMOS unless it is invalid.
Gabe Blackb3f08c62014-04-30 17:12:25 -070065 * 1 tells cmos_init to always initialize the CMOS.
Mike Loptiena96d24d2013-02-25 10:41:28 -070066 */
Gabe Blackb3f08c62014-04-30 17:12:25 -070067 cmos_init(0);
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100068
69 /* Initialize i8259 pic */
70 setup_i8259 ();
71
72 /* Initialize i8254 timers */
73 setup_i8254 ();
zbao246e84b2012-07-13 18:47:03 +080074}
75
Elyes HAOUASa93e7542018-05-19 14:30:47 +020076static void hudson_lpc_read_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +080077{
78 struct resource *res;
79
80 /* Get the normal pci resources of this device */
81 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
82
zbao246e84b2012-07-13 18:47:03 +080083 /* Add an extra subtractive resource for both memory and I/O. */
84 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
85 res->base = 0;
86 res->size = 0x1000;
87 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
88 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
89
90 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
91 res->base = 0xff800000;
92 res->size = 0x00800000; /* 8 MB for flash */
93 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
94 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
95
Dave Frodinac1b8752014-06-05 14:30:22 -060096 /* Add a memory resource for the SPI BAR. */
97 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
98
Dave Frodin8ef20cf2014-06-05 14:21:11 -060099 res = new_resource(dev, 3); /* IOAPIC */
100 res->base = IO_APIC_ADDR;
101 res->size = 0x00001000;
102 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
zbao246e84b2012-07-13 18:47:03 +0800103
104 compact_resources(dev);
105}
106
107static void hudson_lpc_set_resources(struct device *dev)
108{
109 struct resource *res;
110
Martin Roth3aef7b42012-12-05 15:50:32 -0700111 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
Dave Frodinac1b8752014-06-05 14:30:22 -0600112 res = find_resource(dev, 2);
113 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
Martin Roth3aef7b42012-12-05 15:50:32 -0700114
zbao246e84b2012-07-13 18:47:03 +0800115 pci_dev_set_resources(dev);
zbao246e84b2012-07-13 18:47:03 +0800116}
117
118/**
119 * @brief Enable resources for children devices
120 *
Martin Roth3c3a50c2014-12-16 20:50:26 -0700121 * @param dev the device whose children's resources are to be enabled
zbao246e84b2012-07-13 18:47:03 +0800122 *
123 */
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200124static void hudson_lpc_enable_childrens_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800125{
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100126 struct bus *link;
127 u32 reg, reg_x;
128 int var_num = 0;
129 u16 reg_var[3];
130 u16 reg_size[1] = {512};
131 u8 wiosize = pci_read_config8(dev, 0x74);
zbao246e84b2012-07-13 18:47:03 +0800132
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100133 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
134 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
135 * it must be 512 bytes to be able to allocate the fresh LPC window.
136 *
137 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
138 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
139 * The code tries to check if resource can fit into this region
140 */
141
142 reg = pci_read_config32(dev, 0x44);
143 reg_x = pci_read_config32(dev, 0x48);
144
145 /* check if ranges are free and not use them if entry is just already taken */
146 if (reg_x & (1 << 2))
147 var_num = 1;
148 /* just in case check if someone did not manually set other ranges too */
149 if (reg_x & (1 << 24))
150 var_num = 2;
151
152 if (reg_x & (1 << 25))
153 var_num = 3;
154
155 /* check AGESA region size */
156 if (wiosize & (1 << 0))
157 reg_size[0] = 16;
158
159 reg_var[2] = pci_read_config16(dev, 0x90);
160 reg_var[1] = pci_read_config16(dev, 0x66);
161 reg_var[0] = pci_read_config16(dev, 0x64);
162
163 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200164 struct device *child;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100165 for (child = link->children; child;
166 child = child->sibling) {
167 if (child->enabled
168 && (child->path.type == DEVICE_PATH_PNP)) {
169 struct resource *res;
170 for (res = child->resource_list; res; res = res->next) {
171 u32 base, end; /* don't need long long */
172 u32 rsize, set = 0, set_x = 0;
173 if (!(res->flags & IORESOURCE_IO))
174 continue;
175 base = res->base;
176 end = resource_end(res);
177 /* find a resource size */
178 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
179 dev_path(child), base, end);
180 switch (base) {
181 case 0x60: /* KB */
182 case 0x64: /* MS */
183 set |= (1 << 29);
184 rsize = 1;
185 break;
186 case 0x3f8: /* COM1 */
187 set |= (1 << 6);
188 rsize = 8;
189 break;
190 case 0x2f8: /* COM2 */
191 set |= (1 << 7);
192 rsize = 8;
193 break;
194 case 0x378: /* Parallel 1 */
195 set |= (1 << 0);
196 set |= (1 << 1); /* + 0x778 for ECP */
197 rsize = 8;
198 break;
199 case 0x3f0: /* FD0 */
200 set |= (1 << 26);
201 rsize = 8;
202 break;
203 case 0x220: /* 0x220 - 0x227 */
204 set |= (1 << 8);
205 rsize = 8;
206 break;
207 case 0x228: /* 0x228 - 0x22f */
208 set |= (1 << 9);
209 rsize = 8;
210 break;
211 case 0x238: /* 0x238 - 0x23f */
212 set |= (1 << 10);
213 rsize = 8;
214 break;
215 case 0x300: /* 0x300 -0x301 */
216 set |= (1 << 18);
217 rsize = 2;
218 break;
219 case 0x400:
220 set_x |= (1 << 16);
221 rsize = 0x40;
222 break;
223 case 0x480:
224 set_x |= (1 << 17);
225 rsize = 0x40;
Daniele Forsib532b122014-07-26 10:32:34 +0200226 break;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100227 case 0x500:
228 set_x |= (1 << 18);
229 rsize = 0x40;
230 break;
231 case 0x580:
232 set_x |= (1 << 19);
233 rsize = 0x40;
234 break;
235 case 0x4700:
236 set_x |= (1 << 22);
237 rsize = 0xc;
238 break;
239 case 0xfd60:
240 set_x |= (1 << 23);
241 rsize = 16;
242 break;
243 default:
244 rsize = 0;
245 /* try AGESA allocated region in region 0 */
246 if ((var_num > 0) && ((base >=reg_var[0]) &&
247 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
248 rsize = reg_size[0];
249 }
250 /* check if region found and matches the enable */
251 if (res->size <= rsize) {
252 reg |= set;
253 reg_x |= set_x;
254 /* check if we can fit resource in variable range */
255 } else if ((var_num < 3) &&
256 ((res->size <= 16) || (res->size == 512))) {
257 /* use variable ranges if pre-defined do not match */
258 switch (var_num) {
259 case 0:
260 reg_x |= (1 << 2);
261 if (res->size <= 16) {
262 wiosize |= (1 << 0);
263 }
264 break;
265 case 1:
266 reg_x |= (1 << 24);
267 if (res->size <= 16)
268 wiosize |= (1 << 2);
269 break;
270 case 2:
271 reg_x |= (1 << 25);
272 if (res->size <= 16)
273 wiosize |= (1 << 3);
274 break;
275 }
276 reg_var[var_num++] =
277 base & 0xffff;
278 } else {
279 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
280 dev_path(child), base, end);
281 }
282 }
283 }
284 }
285 }
286 pci_write_config32(dev, 0x44, reg);
287 pci_write_config32(dev, 0x48, reg_x);
288 /* Set WideIO for as many IOs found (fall through is on purpose) */
289 switch (var_num) {
290 case 3:
291 pci_write_config16(dev, 0x90, reg_var[2]);
292 /* fall through */
293 case 2:
294 pci_write_config16(dev, 0x66, reg_var[1]);
295 /* fall through */
296 case 1:
297 pci_write_config16(dev, 0x64, reg_var[0]);
298 break;
299 }
300 pci_write_config8(dev, 0x74, wiosize);
zbao246e84b2012-07-13 18:47:03 +0800301}
302
Elyes HAOUASa93e7542018-05-19 14:30:47 +0200303static void hudson_lpc_enable_resources(struct device *dev)
zbao246e84b2012-07-13 18:47:03 +0800304{
305 pci_dev_enable_resources(dev);
306 hudson_lpc_enable_childrens_resources(dev);
307}
308
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400309static const char *lpc_acpi_name(const struct device *dev)
310{
311 if (dev->path.type != DEVICE_PATH_PCI)
312 return NULL;
313
314 if (dev->path.pci.devfn == LPC_DEVFN)
315 return "LIBR";
316
317 return NULL;
318}
319
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200320unsigned long acpi_fill_mcfg(unsigned long current)
321{
322 /* Just a dummy */
323 return current;
324}
325
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300326static void lpc_final(struct device *dev)
327{
328 if (!acpi_is_wakeup_s3()) {
329 if (CONFIG(HAVE_SMI_HANDLER))
330 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
331 else
332 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
333 }
334}
335
zbao246e84b2012-07-13 18:47:03 +0800336static struct device_operations lpc_ops = {
337 .read_resources = hudson_lpc_read_resources,
338 .set_resources = hudson_lpc_set_resources,
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000339 .enable_resources = hudson_lpc_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800340#if CONFIG(HAVE_ACPI_TABLES)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200341 .write_acpi_tables = acpi_write_hpet,
342#endif
zbao246e84b2012-07-13 18:47:03 +0800343 .init = lpc_init,
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300344 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100345 .scan_bus = scan_static_bus,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200346 .ops_pci = &pci_dev_ops_pci,
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400347 .acpi_name = lpc_acpi_name,
zbao246e84b2012-07-13 18:47:03 +0800348};
349static const struct pci_driver lpc_driver __pci_driver = {
350 .ops = &lpc_ops,
351 .vendor = PCI_VENDOR_ID_AMD,
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200352 .device = PCI_DEVICE_ID_AMD_SB900_LPC,
zbao246e84b2012-07-13 18:47:03 +0800353};