blob: f6e256bba1e1ea34ed1a0260aa80282140731879 [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
Paul Menzel4df6b262020-03-08 18:10:33 +010062 /*
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 */
Paul Menzel4fb23282020-03-08 18:14:16 +010071 setup_i8259();
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100072
73 /* Initialize i8254 timers */
Paul Menzel4fb23282020-03-08 18:14:16 +010074 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. */
Kyösti Mälkki27d62992022-05-24 20:25:58 +030098 fixed_mem_resource_kb(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
Dave Frodinac1b8752014-06-05 14:30:22 -060099
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]);
Arthur Heymansfff20212021-03-15 14:56:16 +0100293 __fallthrough;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100294 case 2:
295 pci_write_config16(dev, 0x66, reg_var[1]);
Arthur Heymansfff20212021-03-15 14:56:16 +0100296 __fallthrough;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100297 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
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300321static void lpc_final(struct device *dev)
322{
323 if (!acpi_is_wakeup_s3()) {
324 if (CONFIG(HAVE_SMI_HANDLER))
325 outl(0x0, ACPI_PM1_CNT_BLK); /* clear SCI_EN */
326 else
327 outl(0x1, ACPI_PM1_CNT_BLK); /* set SCI_EN */
328 }
329}
330
zbao246e84b2012-07-13 18:47:03 +0800331static struct device_operations lpc_ops = {
332 .read_resources = hudson_lpc_read_resources,
333 .set_resources = hudson_lpc_set_resources,
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000334 .enable_resources = hudson_lpc_enable_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800335#if CONFIG(HAVE_ACPI_TABLES)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200336 .write_acpi_tables = acpi_write_hpet,
337#endif
zbao246e84b2012-07-13 18:47:03 +0800338 .init = lpc_init,
Kyösti Mälkkif3758b62019-10-08 19:25:57 +0300339 .final = lpc_final,
Nico Huber51b75ae2019-03-14 16:02:05 +0100340 .scan_bus = scan_static_bus,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200341 .ops_pci = &pci_dev_ops_pci,
Kevin Cody-Littlec0984002018-05-09 14:25:44 -0400342 .acpi_name = lpc_acpi_name,
zbao246e84b2012-07-13 18:47:03 +0800343};
344static const struct pci_driver lpc_driver __pci_driver = {
345 .ops = &lpc_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100346 .vendor = PCI_VID_AMD,
347 .device = PCI_DID_AMD_SB900_LPC,
zbao246e84b2012-07-13 18:47:03 +0800348};