blob: 2586ba95c5cab13d4fb8ed0f8fa297eff860c3c7 [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
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pnp.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
9#include <device/pci_def.h>
10#include <pc80/mc146818rtc.h>
11#include <pc80/isa-dma.h>
Marc Jones24484842017-05-04 21:17:45 -060012#include <arch/ioapic.h>
Marc Jones24484842017-05-04 21:17:45 -060013#include <pc80/i8254.h>
14#include <pc80/i8259.h>
Raul E Rangel0f3bc812021-02-10 16:36:33 -070015#include <amdblocks/acpi.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060016#include <amdblocks/acpimmio.h>
Furquan Shaikh511aa442020-05-04 23:42:46 -070017#include <amdblocks/espi.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060018#include <amdblocks/lpc.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060019#include <soc/iomap.h>
Raul E Rangel466edb52021-02-09 11:24:13 -070020#include <soc/lpc.h>
21#include <soc/southbridge.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060022
23/* Most systems should have already enabled the bridge */
24void __weak soc_late_lpc_bridge_enable(void) { }
Marc Jones24484842017-05-04 21:17:45 -060025
Marshall Dawson8d9b8782020-06-29 17:56:02 -060026static void setup_serirq(void)
27{
28 u8 byte;
29
30 /* Set up SERIRQ, enable continuous mode */
Raul E Rangela91eb902021-02-24 16:26:34 -070031 byte = PM_SERIRQ_NUM_BITS_21;
32 if (!CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
33 byte |= PM_SERIRQ_ENABLE;
Marshall Dawson8d9b8782020-06-29 17:56:02 -060034 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
35 byte |= PM_SERIRQ_MODE;
36
37 pm_write8(PM_SERIRQ_CONF, byte);
38}
39
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020040static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060041{
42 u8 byte;
Marc Jones24484842017-05-04 21:17:45 -060043
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060044 soc_late_lpc_bridge_enable();
45
Marc Jones24484842017-05-04 21:17:45 -060046 /* Initialize isa dma */
47 isa_dma_init();
48
49 /* Enable DMA transaction on the LPC bus */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060050 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070051 byte |= LEGACY_DMA_EN;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060052 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060053
54 /* Disable the timeout mechanism on LPC */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060055 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070056 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060057 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060058
59 /* Disable LPC MSI Capability */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060060 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070061 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060062 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070063
64 /*
65 * Keep the old way. i.e., when bus master/DMA cycle is going
Marshall Dawson4e101ad2017-06-15 12:17:38 -060066 * on on LPC, it holds PCI grant, so no LPC slave cycle can
67 * interrupt and visit LPC.
68 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070069 byte &= ~LPC_NOHOG;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060070 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060071
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070072 /*
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060073 * Enable hand-instance of the pulse generator and SPI prefetch from
74 * host (earlier is recommended for boot speed).
Marshall Dawson4e101ad2017-06-15 12:17:38 -060075 */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060076 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070077 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060078 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060079
80 cmos_check_update_date();
81
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070082 /*
83 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060084 * The 0 argument tells cmos_init not to
85 * update CMOS unless it is invalid.
86 * 1 tells cmos_init to always initialize the CMOS.
87 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060088 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060089
90 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060091 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060092
93 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060094 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060095
Raul E Rangela91eb902021-02-24 16:26:34 -070096 setup_serirq();
Marc Jones24484842017-05-04 21:17:45 -060097}
98
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020099static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600100{
101 struct resource *res;
102
103 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600104 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600105
106 /* Add an extra subtractive resource for both memory and I/O. */
107 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
108 res->base = 0;
109 res->size = 0x1000;
110 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
111 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
112
113 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700114 res->base = FLASH_BASE_ADDR;
115 res->size = CONFIG_ROM_SIZE;
Marc Jones24484842017-05-04 21:17:45 -0600116 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
117 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
118
119 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600120 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
121 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600122
123 res = new_resource(dev, 3); /* IOAPIC */
124 res->base = IO_APIC_ADDR;
125 res->size = 0x00001000;
126 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
127
Zheng Baodb11fa42021-01-27 13:49:26 +0800128#ifdef I2C_BASE_ADDRESS
Martin Roth7e78e562019-11-03 23:29:02 -0700129 /* I2C devices */
Chris Ching6fc39d42017-12-20 16:06:03 -0700130 res = new_resource(dev, 4);
131 res->base = I2C_BASE_ADDRESS;
132 res->size = I2C_DEVICE_SIZE * I2C_DEVICE_COUNT;
133 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Zheng Baodb11fa42021-01-27 13:49:26 +0800134#endif
Chris Ching6fc39d42017-12-20 16:06:03 -0700135
Marc Jones24484842017-05-04 21:17:45 -0600136 compact_resources(dev);
137}
138
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600139static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600140{
141 struct resource *res;
142 u32 spi_enable_bits;
143
144 /* Special case. The SpiRomEnable and other enables should STAY set. */
145 res = find_resource(dev, 2);
146 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600147 spi_enable_bits &= SPI_BASE_ALIGNMENT - 1;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600148 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
149 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600150
151 pci_dev_set_resources(dev);
152}
153
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700154static void configure_child_lpc_windows(struct device *dev, struct device *child)
Richard Spiegelaa183852017-10-05 18:53:31 -0700155{
156 struct resource *res;
157 u32 base, end;
158 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700159 int wideio_index;
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700160 u32 reg, reg_x;
161
162 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
163 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
164
Richard Spiegel7a39e022017-11-09 10:54:04 -0700165 /*
166 * Be a bit relaxed, tolerate that LPC region might be bigger than
167 * resource we try to fit, do it like this for all regions < 16 bytes.
168 * If there is a resource > 16 bytes it must be 512 bytes to be able
169 * to allocate the fresh LPC window.
170 *
171 * AGESA and early initialization can set a wide IO port. This code
172 * will verify if required region was previously set and will avoid
173 * setting a new wide IO resource if one is already set.
174 */
175
Richard Spiegelaa183852017-10-05 18:53:31 -0700176 for (res = child->resource_list; res; res = res->next) {
177 if (!(res->flags & IORESOURCE_IO))
178 continue;
179 base = res->base;
180 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700181 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700182 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
183 dev_path(child), base, end);
184 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700185 switch (base) {
186 case 0x60: /* KB */
187 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700188 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700189 rsize = 1;
190 break;
191 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700192 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700193 rsize = 8;
194 break;
195 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700196 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700197 rsize = 8;
198 break;
199 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700200 set |= DECODE_ENABLE_PARALLEL_PORT0;
201 /* enable 0x778 for ECP mode */
202 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700203 rsize = 8;
204 break;
205 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700206 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700207 rsize = 8;
208 break;
209 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700210 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700211 rsize = 8;
212 break;
213 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700214 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700215 rsize = 8;
216 break;
217 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700218 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700219 rsize = 8;
220 break;
221 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700222 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700223 rsize = 2;
224 break;
225 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700226 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700227 rsize = 0x40;
228 break;
229 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700230 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700231 rsize = 0x40;
232 break;
233 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700234 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700235 rsize = 0x40;
236 break;
237 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700238 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700239 rsize = 0x40;
240 break;
241 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700242 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700243 rsize = 0xc;
244 break;
245 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700246 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700247 rsize = 16;
248 break;
249 default:
250 rsize = 0;
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600251 wideio_index = lpc_find_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700252 if (wideio_index != WIDEIO_RANGE_ERROR) {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600253 rsize = lpc_wideio_size(wideio_index);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700254 printk(BIOS_DEBUG, "Covered by wideIO");
255 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700256 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700257 }
258 /* check if region found and matches the enable */
259 if (res->size <= rsize) {
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700260 reg |= set;
261 reg_x |= set_x;
Richard Spiegelaa183852017-10-05 18:53:31 -0700262 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700263 } else {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600264 wideio_index = lpc_set_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700265 if (wideio_index != WIDEIO_RANGE_ERROR) {
266 /* preserve wide IO related bits. */
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700267 reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700268 LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700269 printk(BIOS_DEBUG,
270 "Range assigned to wide IO %d\n",
271 wideio_index);
272 } else {
273 printk(BIOS_ERR,
274 "cannot fit LPC decode region:");
275 printk(BIOS_ERR,
276 "%s, base = 0x%08x, end = 0x%08x\n",
277 dev_path(child), base, end);
278 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700279 }
280 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700281
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700282 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
283 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600284}
285
Furquan Shaikh511aa442020-05-04 23:42:46 -0700286static void configure_child_espi_windows(struct device *child)
287{
288 struct resource *res;
289
290 for (res = child->resource_list; res; res = res->next) {
291 if (res->flags & IORESOURCE_IO)
292 espi_open_io_window(res->base, res->size);
293 else if (res->flags & IORESOURCE_MEM)
294 espi_open_mmio_window(res->base, res->size);
295 }
296}
297
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700298static void lpc_enable_children_resources(struct device *dev)
299{
300 struct bus *link;
301 struct device *child;
302
303 for (link = dev->link_list; link; link = link->next) {
304 for (child = link->children; child; child = child->sibling) {
305 if (!child->enabled)
306 continue;
307 if (child->path.type != DEVICE_PATH_PNP)
308 continue;
Furquan Shaikh511aa442020-05-04 23:42:46 -0700309 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
310 configure_child_espi_windows(child);
311 else
312 configure_child_lpc_windows(dev, child);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700313 }
314 }
315}
316
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200317static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600318{
319 pci_dev_enable_resources(dev);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700320 lpc_enable_children_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600321}
322
Felix Held3e29ca92021-02-16 23:52:58 +0100323#if CONFIG(HAVE_ACPI_TABLES)
324static const char *lpc_acpi_name(const struct device *dev)
325{
326 return "LPCB";
327}
328#endif
329
Marc Jones24484842017-05-04 21:17:45 -0600330static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600331 .read_resources = lpc_read_resources,
332 .set_resources = lpc_set_resources,
333 .enable_resources = lpc_enable_resources,
Zheng Baobdd50312021-01-26 18:27:46 +0800334#if CONFIG(HAVE_ACPI_TABLES)
Felix Held3e29ca92021-02-16 23:52:58 +0100335 .acpi_name = lpc_acpi_name,
Marc Jones257db582017-06-18 17:33:30 -0600336 .write_acpi_tables = southbridge_write_acpi_tables,
Zheng Baobdd50312021-01-26 18:27:46 +0800337#endif
Marc Jones24484842017-05-04 21:17:45 -0600338 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100339 .scan_bus = scan_static_bus,
Furquan Shaikh40454b72020-05-04 20:52:08 -0700340 .ops_pci = &pci_dev_ops_pci,
Marc Jones24484842017-05-04 21:17:45 -0600341};
342
343static const unsigned short pci_device_ids[] = {
344 PCI_DEVICE_ID_AMD_SB900_LPC,
345 PCI_DEVICE_ID_AMD_CZ_LPC,
Furquan Shaikha1cd7eb2020-04-15 23:58:22 -0700346 PCI_DEVICE_ID_AMD_FAM17H_LPC,
Marc Jones24484842017-05-04 21:17:45 -0600347 0
348};
349static const struct pci_driver lpc_driver __pci_driver = {
350 .ops = &lpc_ops,
351 .vendor = PCI_VENDOR_ID_AMD,
352 .devices = pci_device_ids,
353};