blob: 2392d5f3e813ed6e31e3f83635d9cddf9709588e [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>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060020#include <soc/iomap.h>
Raul E Rangel466edb52021-02-09 11:24:13 -070021#include <soc/lpc.h>
22#include <soc/southbridge.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060023
Marshall Dawson8d9b8782020-06-29 17:56:02 -060024static void setup_serirq(void)
25{
26 u8 byte;
27
28 /* Set up SERIRQ, enable continuous mode */
Raul E Rangela91eb902021-02-24 16:26:34 -070029 byte = PM_SERIRQ_NUM_BITS_21;
30 if (!CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
31 byte |= PM_SERIRQ_ENABLE;
Marshall Dawson8d9b8782020-06-29 17:56:02 -060032 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
33 byte |= PM_SERIRQ_MODE;
34
35 pm_write8(PM_SERIRQ_CONF, byte);
36}
37
Kyösti Mälkkie742b682023-04-10 17:03:32 +030038void ioapic_get_sci_pin(u8 *gsi, u8 *irq, u8 *flags)
39{
40 *gsi = ACPI_SCI_IRQ;
41 *irq = ACPI_SCI_IRQ;
42 *flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW;
43}
44
Felix Held390a2802021-10-21 03:13:42 +020045static void fch_ioapic_init(void)
46{
47 fch_enable_ioapic_decode();
Felix Held0d192892024-02-06 16:55:29 +010048 register_new_ioapic_gsi0(IO_APIC_ADDR);
Felix Held390a2802021-10-21 03:13:42 +020049}
50
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020051static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060052{
53 u8 byte;
Marc Jones24484842017-05-04 21:17:45 -060054
55 /* Initialize isa dma */
56 isa_dma_init();
57
58 /* Enable DMA transaction on the LPC bus */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060059 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070060 byte |= LEGACY_DMA_EN;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060061 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060062
63 /* Disable the timeout mechanism on LPC */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060064 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070065 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060066 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060067
68 /* Disable LPC MSI Capability */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060069 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070070 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060071 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070072
Marshall Dawson1bc04e32019-05-02 18:56:54 -060073 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060074
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070075 /*
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060076 * Enable hand-instance of the pulse generator and SPI prefetch from
77 * host (earlier is recommended for boot speed).
Marshall Dawson4e101ad2017-06-15 12:17:38 -060078 */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060079 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070080 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060081 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060082
83 cmos_check_update_date();
84
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070085 /*
86 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060087 * The 0 argument tells cmos_init not to
88 * update CMOS unless it is invalid.
89 * 1 tells cmos_init to always initialize the CMOS.
90 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060091 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060092
93 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060094 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060095
96 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060097 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060098
Raul E Rangela91eb902021-02-24 16:26:34 -070099 setup_serirq();
Felix Held390a2802021-10-21 03:13:42 +0200100
101 fch_ioapic_init();
102 fch_configure_hpet();
Marc Jones24484842017-05-04 21:17:45 -0600103}
104
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200105static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600106{
Felix Held1591f842023-04-29 02:56:07 +0200107 unsigned long idx = 0;
Marc Jones24484842017-05-04 21:17:45 -0600108
109 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600110 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600111
Felix Held6e0390762023-08-09 19:20:15 +0200112 /* Add an extra subtractive resource for I/O. */
113 fixed_io_range_flags(dev, IOINDEX_SUBTRACTIVE(0, 0), 0, 0x1000,
114 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600115
Felix Held199b10f2022-08-13 00:29:23 +0200116 /* Only up to 16 MByte of the SPI flash can be mapped right below 4 GB */
Felix Held1591f842023-04-29 02:56:07 +0200117 mmio_range(dev, idx++, FLASH_BELOW_4GB_MAPPING_REGION_BASE,
Felix Held662d7af2023-04-28 17:30:59 +0200118 FLASH_BELOW_4GB_MAPPING_REGION_SIZE);
Marc Jones24484842017-05-04 21:17:45 -0600119
120 /* Add a memory resource for the SPI BAR. */
Felix Held1591f842023-04-29 02:56:07 +0200121 mmio_range(dev, idx++, SPI_BASE_ADDRESS, 4 * KiB);
Marc Jones24484842017-05-04 21:17:45 -0600122
Felix Held19d1c162023-04-29 02:02:48 +0200123 /* Add a memory resource for the eSPI MMIO */
Felix Held1591f842023-04-29 02:56:07 +0200124 mmio_range(dev, idx++, SPI_BASE_ADDRESS + ESPI_OFFSET_FROM_BAR, 4 * KiB);
Felix Held19d1c162023-04-29 02:02:48 +0200125
Felix Held4d70daf2023-04-29 02:04:07 +0200126 /* FCH IOAPIC */
Felix Held1591f842023-04-29 02:56:07 +0200127 mmio_range(dev, idx++, IO_APIC_ADDR, 4 * KiB);
Marc Jones24484842017-05-04 21:17:45 -0600128
Felix Heldbd9db8d2023-04-29 02:05:05 +0200129 /* HPET */
Felix Held1591f842023-04-29 02:56:07 +0200130 mmio_range(dev, idx++, HPET_BASE_ADDRESS, 4 * KiB);
Felix Heldbd9db8d2023-04-29 02:05:05 +0200131
Marc Jones24484842017-05-04 21:17:45 -0600132 compact_resources(dev);
133}
134
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700135static void configure_child_lpc_windows(struct device *dev, struct device *child)
Richard Spiegelaa183852017-10-05 18:53:31 -0700136{
137 struct resource *res;
138 u32 base, end;
139 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700140 int wideio_index;
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700141 u32 reg, reg_x;
142
143 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
144 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
145
Richard Spiegel7a39e022017-11-09 10:54:04 -0700146 /*
147 * Be a bit relaxed, tolerate that LPC region might be bigger than
148 * resource we try to fit, do it like this for all regions < 16 bytes.
149 * If there is a resource > 16 bytes it must be 512 bytes to be able
150 * to allocate the fresh LPC window.
151 *
152 * AGESA and early initialization can set a wide IO port. This code
153 * will verify if required region was previously set and will avoid
154 * setting a new wide IO resource if one is already set.
155 */
156
Richard Spiegelaa183852017-10-05 18:53:31 -0700157 for (res = child->resource_list; res; res = res->next) {
158 if (!(res->flags & IORESOURCE_IO))
159 continue;
160 base = res->base;
161 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700162 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700163 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
164 dev_path(child), base, end);
165 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700166 switch (base) {
167 case 0x60: /* KB */
168 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700169 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700170 rsize = 1;
171 break;
172 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700173 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700174 rsize = 8;
175 break;
176 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700177 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700178 rsize = 8;
179 break;
180 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700181 set |= DECODE_ENABLE_PARALLEL_PORT0;
182 /* enable 0x778 for ECP mode */
183 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700184 rsize = 8;
185 break;
186 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700187 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700188 rsize = 8;
189 break;
190 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700191 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700192 rsize = 8;
193 break;
194 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700195 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700196 rsize = 8;
197 break;
198 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700199 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700200 rsize = 8;
201 break;
202 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700203 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700204 rsize = 2;
205 break;
206 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700207 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700208 rsize = 0x40;
209 break;
210 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700211 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700212 rsize = 0x40;
213 break;
214 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700215 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700216 rsize = 0x40;
217 break;
218 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700219 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700220 rsize = 0x40;
221 break;
222 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700223 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700224 rsize = 0xc;
225 break;
226 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700227 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700228 rsize = 16;
229 break;
230 default:
231 rsize = 0;
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600232 wideio_index = lpc_find_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700233 if (wideio_index != WIDEIO_RANGE_ERROR) {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600234 rsize = lpc_wideio_size(wideio_index);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700235 printk(BIOS_DEBUG, "Covered by wideIO");
236 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700237 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700238 }
239 /* check if region found and matches the enable */
240 if (res->size <= rsize) {
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700241 reg |= set;
242 reg_x |= set_x;
Richard Spiegelaa183852017-10-05 18:53:31 -0700243 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700244 } else {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600245 wideio_index = lpc_set_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700246 if (wideio_index != WIDEIO_RANGE_ERROR) {
247 /* preserve wide IO related bits. */
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700248 reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700249 LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700250 printk(BIOS_DEBUG,
251 "Range assigned to wide IO %d\n",
252 wideio_index);
253 } else {
254 printk(BIOS_ERR,
255 "cannot fit LPC decode region:");
256 printk(BIOS_ERR,
257 "%s, base = 0x%08x, end = 0x%08x\n",
258 dev_path(child), base, end);
259 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700260 }
261 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700262
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700263 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
264 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600265}
266
Furquan Shaikh511aa442020-05-04 23:42:46 -0700267static void configure_child_espi_windows(struct device *child)
268{
269 struct resource *res;
270
271 for (res = child->resource_list; res; res = res->next) {
272 if (res->flags & IORESOURCE_IO)
273 espi_open_io_window(res->base, res->size);
274 else if (res->flags & IORESOURCE_MEM)
275 espi_open_mmio_window(res->base, res->size);
276 }
277}
278
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700279static void lpc_enable_children_resources(struct device *dev)
280{
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700281 struct device *child;
282
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200283 if (!dev->downstream)
Arthur Heymans80c79a52023-08-24 15:12:19 +0200284 return;
285
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200286 for (child = dev->downstream->children; child; child = child->sibling) {
Arthur Heymans80c79a52023-08-24 15:12:19 +0200287 if (!child->enabled)
288 continue;
289 if (child->path.type != DEVICE_PATH_PNP)
290 continue;
291 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
292 configure_child_espi_windows(child);
293 else
294 configure_child_lpc_windows(dev, child);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700295 }
296}
297
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200298static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600299{
300 pci_dev_enable_resources(dev);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700301 lpc_enable_children_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600302}
303
Felix Held3e29ca92021-02-16 23:52:58 +0100304#if CONFIG(HAVE_ACPI_TABLES)
305static const char *lpc_acpi_name(const struct device *dev)
306{
307 return "LPCB";
308}
309#endif
310
Arthur Heymansc6f029c2022-10-05 21:48:07 +0200311struct device_operations amd_lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600312 .read_resources = lpc_read_resources,
Felix Held6a41b992023-04-29 02:50:05 +0200313 .set_resources = pci_dev_set_resources,
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600314 .enable_resources = lpc_enable_resources,
Zheng Baobdd50312021-01-26 18:27:46 +0800315#if CONFIG(HAVE_ACPI_TABLES)
Felix Held3e29ca92021-02-16 23:52:58 +0100316 .acpi_name = lpc_acpi_name,
Marc Jones257db582017-06-18 17:33:30 -0600317 .write_acpi_tables = southbridge_write_acpi_tables,
Zheng Baobdd50312021-01-26 18:27:46 +0800318#endif
Marc Jones24484842017-05-04 21:17:45 -0600319 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100320 .scan_bus = scan_static_bus,
Furquan Shaikh40454b72020-05-04 20:52:08 -0700321 .ops_pci = &pci_dev_ops_pci,
Marc Jones24484842017-05-04 21:17:45 -0600322};