blob: 3d5775fe3504e26d9e1452b5a2f041576f324424 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Marc Jones24484842017-05-04 21:17:45 -06002
Felix Heldbd9db8d2023-04-29 02:05:05 +02003#include <arch/hpet.h>
Felix Held390a2802021-10-21 03:13:42 +02004#include <arch/ioapic.h>
Marc Jones24484842017-05-04 21:17:45 -06005#include <console/console.h>
6#include <device/device.h>
7#include <device/pci.h>
8#include <device/pnp.h>
Marc Jones24484842017-05-04 21:17:45 -06009#include <device/pci_ops.h>
Marc Jones24484842017-05-04 21:17:45 -060010#include <pc80/mc146818rtc.h>
11#include <pc80/isa-dma.h>
Marc Jones24484842017-05-04 21:17:45 -060012#include <pc80/i8254.h>
13#include <pc80/i8259.h>
Raul E Rangel0f3bc812021-02-10 16:36:33 -070014#include <amdblocks/acpi.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060015#include <amdblocks/acpimmio.h>
Furquan Shaikh511aa442020-05-04 23:42:46 -070016#include <amdblocks/espi.h>
Felix Held390a2802021-10-21 03:13:42 +020017#include <amdblocks/ioapic.h>
Felix Held199b10f2022-08-13 00:29:23 +020018#include <amdblocks/iomap.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060019#include <amdblocks/lpc.h>
Kyösti Mälkkie742b682023-04-10 17:03:32 +030020#include <soc/acpi.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060021#include <soc/iomap.h>
Raul E Rangel466edb52021-02-09 11:24:13 -070022#include <soc/lpc.h>
23#include <soc/southbridge.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060024
Marshall Dawson8d9b8782020-06-29 17:56:02 -060025static void setup_serirq(void)
26{
27 u8 byte;
28
29 /* Set up SERIRQ, enable continuous mode */
Raul E Rangela91eb902021-02-24 16:26:34 -070030 byte = PM_SERIRQ_NUM_BITS_21;
31 if (!CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
32 byte |= PM_SERIRQ_ENABLE;
Marshall Dawson8d9b8782020-06-29 17:56:02 -060033 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
34 byte |= PM_SERIRQ_MODE;
35
36 pm_write8(PM_SERIRQ_CONF, byte);
37}
38
Kyösti Mälkkie742b682023-04-10 17:03:32 +030039void ioapic_get_sci_pin(u8 *gsi, u8 *irq, u8 *flags)
40{
41 *gsi = ACPI_SCI_IRQ;
42 *irq = ACPI_SCI_IRQ;
43 *flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW;
44}
45
Felix Held390a2802021-10-21 03:13:42 +020046static void fch_ioapic_init(void)
47{
48 fch_enable_ioapic_decode();
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030049 register_new_ioapic_gsi0(VIO_APIC_VADDR);
Felix Held390a2802021-10-21 03:13:42 +020050}
51
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020052static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060053{
54 u8 byte;
Marc Jones24484842017-05-04 21:17:45 -060055
56 /* Initialize isa dma */
57 isa_dma_init();
58
59 /* Enable DMA transaction on the LPC bus */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060060 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070061 byte |= LEGACY_DMA_EN;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060062 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060063
64 /* Disable the timeout mechanism on LPC */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060065 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070066 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060067 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060068
69 /* Disable LPC MSI Capability */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060070 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070071 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060072 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070073
Marshall Dawson1bc04e32019-05-02 18:56:54 -060074 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060075
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070076 /*
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060077 * Enable hand-instance of the pulse generator and SPI prefetch from
78 * host (earlier is recommended for boot speed).
Marshall Dawson4e101ad2017-06-15 12:17:38 -060079 */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060080 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070081 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060082 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060083
84 cmos_check_update_date();
85
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070086 /*
87 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060088 * The 0 argument tells cmos_init not to
89 * update CMOS unless it is invalid.
90 * 1 tells cmos_init to always initialize the CMOS.
91 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060092 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060093
94 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060095 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060096
97 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060098 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060099
Raul E Rangela91eb902021-02-24 16:26:34 -0700100 setup_serirq();
Felix Held390a2802021-10-21 03:13:42 +0200101
102 fch_ioapic_init();
103 fch_configure_hpet();
Marc Jones24484842017-05-04 21:17:45 -0600104}
105
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200106static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600107{
108 struct resource *res;
Felix Held1591f842023-04-29 02:56:07 +0200109 unsigned long idx = 0;
Marc Jones24484842017-05-04 21:17:45 -0600110
111 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600112 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600113
114 /* Add an extra subtractive resource for both memory and I/O. */
115 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
116 res->base = 0;
117 res->size = 0x1000;
118 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
119 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
120
Felix Held199b10f2022-08-13 00:29:23 +0200121 /* Only up to 16 MByte of the SPI flash can be mapped right below 4 GB */
Felix Held1591f842023-04-29 02:56:07 +0200122 mmio_range(dev, idx++, FLASH_BELOW_4GB_MAPPING_REGION_BASE,
Felix Held662d7af2023-04-28 17:30:59 +0200123 FLASH_BELOW_4GB_MAPPING_REGION_SIZE);
Marc Jones24484842017-05-04 21:17:45 -0600124
125 /* Add a memory resource for the SPI BAR. */
Felix Held1591f842023-04-29 02:56:07 +0200126 mmio_range(dev, idx++, SPI_BASE_ADDRESS, 4 * KiB);
Marc Jones24484842017-05-04 21:17:45 -0600127
Felix Held19d1c162023-04-29 02:02:48 +0200128 /* Add a memory resource for the eSPI MMIO */
Felix Held1591f842023-04-29 02:56:07 +0200129 mmio_range(dev, idx++, SPI_BASE_ADDRESS + ESPI_OFFSET_FROM_BAR, 4 * KiB);
Felix Held19d1c162023-04-29 02:02:48 +0200130
Felix Held4d70daf2023-04-29 02:04:07 +0200131 /* FCH IOAPIC */
Felix Held1591f842023-04-29 02:56:07 +0200132 mmio_range(dev, idx++, IO_APIC_ADDR, 4 * KiB);
Marc Jones24484842017-05-04 21:17:45 -0600133
Felix Heldbd9db8d2023-04-29 02:05:05 +0200134 /* HPET */
Felix Held1591f842023-04-29 02:56:07 +0200135 mmio_range(dev, idx++, HPET_BASE_ADDRESS, 4 * KiB);
Felix Heldbd9db8d2023-04-29 02:05:05 +0200136
Marc Jones24484842017-05-04 21:17:45 -0600137 compact_resources(dev);
138}
139
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700140static void configure_child_lpc_windows(struct device *dev, struct device *child)
Richard Spiegelaa183852017-10-05 18:53:31 -0700141{
142 struct resource *res;
143 u32 base, end;
144 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700145 int wideio_index;
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700146 u32 reg, reg_x;
147
148 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
149 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
150
Richard Spiegel7a39e022017-11-09 10:54:04 -0700151 /*
152 * Be a bit relaxed, tolerate that LPC region might be bigger than
153 * resource we try to fit, do it like this for all regions < 16 bytes.
154 * If there is a resource > 16 bytes it must be 512 bytes to be able
155 * to allocate the fresh LPC window.
156 *
157 * AGESA and early initialization can set a wide IO port. This code
158 * will verify if required region was previously set and will avoid
159 * setting a new wide IO resource if one is already set.
160 */
161
Richard Spiegelaa183852017-10-05 18:53:31 -0700162 for (res = child->resource_list; res; res = res->next) {
163 if (!(res->flags & IORESOURCE_IO))
164 continue;
165 base = res->base;
166 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700167 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700168 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
169 dev_path(child), base, end);
170 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700171 switch (base) {
172 case 0x60: /* KB */
173 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700174 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700175 rsize = 1;
176 break;
177 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700178 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700179 rsize = 8;
180 break;
181 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700182 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700183 rsize = 8;
184 break;
185 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700186 set |= DECODE_ENABLE_PARALLEL_PORT0;
187 /* enable 0x778 for ECP mode */
188 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700189 rsize = 8;
190 break;
191 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700192 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700193 rsize = 8;
194 break;
195 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700196 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700197 rsize = 8;
198 break;
199 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700200 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700201 rsize = 8;
202 break;
203 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700204 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700205 rsize = 8;
206 break;
207 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700208 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700209 rsize = 2;
210 break;
211 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700212 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700213 rsize = 0x40;
214 break;
215 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700216 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700217 rsize = 0x40;
218 break;
219 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700220 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700221 rsize = 0x40;
222 break;
223 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700224 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700225 rsize = 0x40;
226 break;
227 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700228 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700229 rsize = 0xc;
230 break;
231 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700232 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700233 rsize = 16;
234 break;
235 default:
236 rsize = 0;
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600237 wideio_index = lpc_find_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700238 if (wideio_index != WIDEIO_RANGE_ERROR) {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600239 rsize = lpc_wideio_size(wideio_index);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700240 printk(BIOS_DEBUG, "Covered by wideIO");
241 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700242 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700243 }
244 /* check if region found and matches the enable */
245 if (res->size <= rsize) {
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700246 reg |= set;
247 reg_x |= set_x;
Richard Spiegelaa183852017-10-05 18:53:31 -0700248 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700249 } else {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600250 wideio_index = lpc_set_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700251 if (wideio_index != WIDEIO_RANGE_ERROR) {
252 /* preserve wide IO related bits. */
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700253 reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700254 LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700255 printk(BIOS_DEBUG,
256 "Range assigned to wide IO %d\n",
257 wideio_index);
258 } else {
259 printk(BIOS_ERR,
260 "cannot fit LPC decode region:");
261 printk(BIOS_ERR,
262 "%s, base = 0x%08x, end = 0x%08x\n",
263 dev_path(child), base, end);
264 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700265 }
266 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700267
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700268 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
269 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600270}
271
Furquan Shaikh511aa442020-05-04 23:42:46 -0700272static void configure_child_espi_windows(struct device *child)
273{
274 struct resource *res;
275
276 for (res = child->resource_list; res; res = res->next) {
277 if (res->flags & IORESOURCE_IO)
278 espi_open_io_window(res->base, res->size);
279 else if (res->flags & IORESOURCE_MEM)
280 espi_open_mmio_window(res->base, res->size);
281 }
282}
283
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700284static void lpc_enable_children_resources(struct device *dev)
285{
286 struct bus *link;
287 struct device *child;
288
289 for (link = dev->link_list; link; link = link->next) {
290 for (child = link->children; child; child = child->sibling) {
291 if (!child->enabled)
292 continue;
293 if (child->path.type != DEVICE_PATH_PNP)
294 continue;
Furquan Shaikh511aa442020-05-04 23:42:46 -0700295 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
296 configure_child_espi_windows(child);
297 else
298 configure_child_lpc_windows(dev, child);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700299 }
300 }
301}
302
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200303static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600304{
305 pci_dev_enable_resources(dev);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700306 lpc_enable_children_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600307}
308
Felix Held3e29ca92021-02-16 23:52:58 +0100309#if CONFIG(HAVE_ACPI_TABLES)
310static const char *lpc_acpi_name(const struct device *dev)
311{
312 return "LPCB";
313}
314#endif
315
Arthur Heymansc6f029c2022-10-05 21:48:07 +0200316struct device_operations amd_lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600317 .read_resources = lpc_read_resources,
Felix Held6a41b992023-04-29 02:50:05 +0200318 .set_resources = pci_dev_set_resources,
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600319 .enable_resources = lpc_enable_resources,
Zheng Baobdd50312021-01-26 18:27:46 +0800320#if CONFIG(HAVE_ACPI_TABLES)
Felix Held3e29ca92021-02-16 23:52:58 +0100321 .acpi_name = lpc_acpi_name,
Marc Jones257db582017-06-18 17:33:30 -0600322 .write_acpi_tables = southbridge_write_acpi_tables,
Zheng Baobdd50312021-01-26 18:27:46 +0800323#endif
Marc Jones24484842017-05-04 21:17:45 -0600324 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100325 .scan_bus = scan_static_bus,
Furquan Shaikh40454b72020-05-04 20:52:08 -0700326 .ops_pci = &pci_dev_ops_pci,
Marc Jones24484842017-05-04 21:17:45 -0600327};