blob: deadfa2a777a089d5928cb499bb6e0c3b095b7e9 [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
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +03003#include <acpi/acpi_gnvs.h>
Marc Jones257db582017-06-18 17:33:30 -06004#include <cbmem.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>
9#include <device/pci_ids.h>
10#include <device/pci_ops.h>
11#include <device/pci_def.h>
12#include <pc80/mc146818rtc.h>
13#include <pc80/isa-dma.h>
Marc Jones24484842017-05-04 21:17:45 -060014#include <arch/ioapic.h>
Marc Jones24484842017-05-04 21:17:45 -060015#include <pc80/i8254.h>
16#include <pc80/i8259.h>
Marshall Dawson69486ca2019-05-02 12:03:45 -060017#include <amdblocks/acpimmio.h>
Furquan Shaikh511aa442020-05-04 23:42:46 -070018#include <amdblocks/espi.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060019#include <amdblocks/lpc.h>
Marc Jones257db582017-06-18 17:33:30 -060020#include <soc/acpi.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060021#include <soc/southbridge.h>
Marc Jones257db582017-06-18 17:33:30 -060022#include <soc/nvs.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060023#include <soc/iomap.h>
24
25/* Most systems should have already enabled the bridge */
26void __weak soc_late_lpc_bridge_enable(void) { }
Marc Jones24484842017-05-04 21:17:45 -060027
Marshall Dawson8d9b8782020-06-29 17:56:02 -060028static void setup_serirq(void)
29{
30 u8 byte;
31
32 /* Set up SERIRQ, enable continuous mode */
33 byte = (PM_SERIRQ_NUM_BITS_21 | PM_SERIRQ_ENABLE);
34 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
Marshall Dawson8d9b8782020-06-29 17:56:02 -060096 if (!CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
97 setup_serirq();
Marc Jones24484842017-05-04 21:17:45 -060098}
99
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200100static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600101{
102 struct resource *res;
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300103 struct global_nvs *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600104
105 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600106 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600107
108 /* Add an extra subtractive resource for both memory and I/O. */
109 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
110 res->base = 0;
111 res->size = 0x1000;
112 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
113 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
114
115 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700116 res->base = FLASH_BASE_ADDR;
117 res->size = CONFIG_ROM_SIZE;
Marc Jones24484842017-05-04 21:17:45 -0600118 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
119 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
120
121 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600122 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
123 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600124
125 res = new_resource(dev, 3); /* IOAPIC */
126 res->base = IO_APIC_ADDR;
127 res->size = 0x00001000;
128 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
129
Martin Roth7e78e562019-11-03 23:29:02 -0700130 /* I2C devices */
Chris Ching6fc39d42017-12-20 16:06:03 -0700131 res = new_resource(dev, 4);
132 res->base = I2C_BASE_ADDRESS;
133 res->size = I2C_DEVICE_SIZE * I2C_DEVICE_COUNT;
134 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
135
Marc Jones24484842017-05-04 21:17:45 -0600136 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600137
138 /* Allocate ACPI NVS in CBMEM */
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300139 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(struct global_nvs));
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700140 printk(BIOS_DEBUG, "ACPI GNVS at %p\n", gnvs);
Marc Jones24484842017-05-04 21:17:45 -0600141}
142
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600143static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600144{
145 struct resource *res;
146 u32 spi_enable_bits;
147
148 /* Special case. The SpiRomEnable and other enables should STAY set. */
149 res = find_resource(dev, 2);
150 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600151 spi_enable_bits &= SPI_BASE_ALIGNMENT - 1;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600152 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
153 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600154
155 pci_dev_set_resources(dev);
156}
157
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700158static void configure_child_lpc_windows(struct device *dev, struct device *child)
Richard Spiegelaa183852017-10-05 18:53:31 -0700159{
160 struct resource *res;
161 u32 base, end;
162 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700163 int wideio_index;
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700164 u32 reg, reg_x;
165
166 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
167 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
168
Richard Spiegelaa183852017-10-05 18:53:31 -0700169
Richard Spiegel7a39e022017-11-09 10:54:04 -0700170 /*
171 * Be a bit relaxed, tolerate that LPC region might be bigger than
172 * resource we try to fit, do it like this for all regions < 16 bytes.
173 * If there is a resource > 16 bytes it must be 512 bytes to be able
174 * to allocate the fresh LPC window.
175 *
176 * AGESA and early initialization can set a wide IO port. This code
177 * will verify if required region was previously set and will avoid
178 * setting a new wide IO resource if one is already set.
179 */
180
Richard Spiegelaa183852017-10-05 18:53:31 -0700181 for (res = child->resource_list; res; res = res->next) {
182 if (!(res->flags & IORESOURCE_IO))
183 continue;
184 base = res->base;
185 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700186 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700187 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
188 dev_path(child), base, end);
189 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700190 switch (base) {
191 case 0x60: /* KB */
192 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700193 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700194 rsize = 1;
195 break;
196 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700197 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700198 rsize = 8;
199 break;
200 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700201 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700202 rsize = 8;
203 break;
204 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700205 set |= DECODE_ENABLE_PARALLEL_PORT0;
206 /* enable 0x778 for ECP mode */
207 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700208 rsize = 8;
209 break;
210 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700211 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700212 rsize = 8;
213 break;
214 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700215 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700216 rsize = 8;
217 break;
218 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700219 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700220 rsize = 8;
221 break;
222 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700223 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700224 rsize = 8;
225 break;
226 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700227 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700228 rsize = 2;
229 break;
230 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700231 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700232 rsize = 0x40;
233 break;
234 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700235 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700236 rsize = 0x40;
237 break;
238 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700239 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700240 rsize = 0x40;
241 break;
242 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700243 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700244 rsize = 0x40;
245 break;
246 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700247 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700248 rsize = 0xc;
249 break;
250 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700251 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700252 rsize = 16;
253 break;
254 default:
255 rsize = 0;
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600256 wideio_index = lpc_find_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700257 if (wideio_index != WIDEIO_RANGE_ERROR) {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600258 rsize = lpc_wideio_size(wideio_index);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700259 printk(BIOS_DEBUG, "Covered by wideIO");
260 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700261 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700262 }
263 /* check if region found and matches the enable */
264 if (res->size <= rsize) {
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700265 reg |= set;
266 reg_x |= set_x;
Richard Spiegelaa183852017-10-05 18:53:31 -0700267 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700268 } else {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600269 wideio_index = lpc_set_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700270 if (wideio_index != WIDEIO_RANGE_ERROR) {
271 /* preserve wide IO related bits. */
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700272 reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700273 LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700274 printk(BIOS_DEBUG,
275 "Range assigned to wide IO %d\n",
276 wideio_index);
277 } else {
278 printk(BIOS_ERR,
279 "cannot fit LPC decode region:");
280 printk(BIOS_ERR,
281 "%s, base = 0x%08x, end = 0x%08x\n",
282 dev_path(child), base, end);
283 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700284 }
285 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700286
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700287 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
288 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600289}
290
Furquan Shaikh511aa442020-05-04 23:42:46 -0700291static void configure_child_espi_windows(struct device *child)
292{
293 struct resource *res;
294
295 for (res = child->resource_list; res; res = res->next) {
296 if (res->flags & IORESOURCE_IO)
297 espi_open_io_window(res->base, res->size);
298 else if (res->flags & IORESOURCE_MEM)
299 espi_open_mmio_window(res->base, res->size);
300 }
301}
302
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700303static void lpc_enable_children_resources(struct device *dev)
304{
305 struct bus *link;
306 struct device *child;
307
308 for (link = dev->link_list; link; link = link->next) {
309 for (child = link->children; child; child = child->sibling) {
310 if (!child->enabled)
311 continue;
312 if (child->path.type != DEVICE_PATH_PNP)
313 continue;
Furquan Shaikh511aa442020-05-04 23:42:46 -0700314 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
315 configure_child_espi_windows(child);
316 else
317 configure_child_lpc_windows(dev, child);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700318 }
319 }
320}
321
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200322static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600323{
324 pci_dev_enable_resources(dev);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700325 lpc_enable_children_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600326}
327
Marc Jones24484842017-05-04 21:17:45 -0600328static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600329 .read_resources = lpc_read_resources,
330 .set_resources = lpc_set_resources,
331 .enable_resources = lpc_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200332 .acpi_inject_dsdt = southbridge_inject_dsdt,
Marc Jones257db582017-06-18 17:33:30 -0600333 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600334 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100335 .scan_bus = scan_static_bus,
Furquan Shaikh40454b72020-05-04 20:52:08 -0700336 .ops_pci = &pci_dev_ops_pci,
Marc Jones24484842017-05-04 21:17:45 -0600337};
338
339static const unsigned short pci_device_ids[] = {
340 PCI_DEVICE_ID_AMD_SB900_LPC,
341 PCI_DEVICE_ID_AMD_CZ_LPC,
Furquan Shaikha1cd7eb2020-04-15 23:58:22 -0700342 PCI_DEVICE_ID_AMD_FAM17H_LPC,
Marc Jones24484842017-05-04 21:17:45 -0600343 0
344};
345static const struct pci_driver lpc_driver __pci_driver = {
346 .ops = &lpc_ops,
347 .vendor = PCI_VENDOR_ID_AMD,
348 .devices = pci_device_ids,
349};