blob: 68e1fce9603def18e926e8b26481a6115bd4b4fa [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 Held390a2802021-10-21 03:13:42 +02003#include <arch/ioapic.h>
Marc Jones24484842017-05-04 21:17:45 -06004#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>
10#include <device/pci_def.h>
11#include <pc80/mc146818rtc.h>
12#include <pc80/isa-dma.h>
Marc Jones24484842017-05-04 21:17:45 -060013#include <arch/ioapic.h>
Marc Jones24484842017-05-04 21:17:45 -060014#include <pc80/i8254.h>
15#include <pc80/i8259.h>
Raul E Rangel0f3bc812021-02-10 16:36:33 -070016#include <amdblocks/acpi.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>
Felix Held390a2802021-10-21 03:13:42 +020019#include <amdblocks/ioapic.h>
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060020#include <amdblocks/lpc.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
Felix Held390a2802021-10-21 03:13:42 +020039static void fch_ioapic_init(void)
40{
41 fch_enable_ioapic_decode();
42 setup_ioapic(VIO_APIC_VADDR, FCH_IOAPIC_ID);
43}
44
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020045static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060046{
47 u8 byte;
Marc Jones24484842017-05-04 21:17:45 -060048
49 /* Initialize isa dma */
50 isa_dma_init();
51
52 /* Enable DMA transaction on the LPC bus */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060053 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070054 byte |= LEGACY_DMA_EN;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060055 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060056
57 /* Disable the timeout mechanism on LPC */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060058 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070059 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060060 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060061
62 /* Disable LPC MSI Capability */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060063 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070064 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060065 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070066
Marshall Dawson1bc04e32019-05-02 18:56:54 -060067 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060068
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070069 /*
Marshall Dawson6ab5ed32019-05-29 09:24:18 -060070 * Enable hand-instance of the pulse generator and SPI prefetch from
71 * host (earlier is recommended for boot speed).
Marshall Dawson4e101ad2017-06-15 12:17:38 -060072 */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060073 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070074 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060075 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060076
77 cmos_check_update_date();
78
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070079 /*
80 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060081 * The 0 argument tells cmos_init not to
82 * update CMOS unless it is invalid.
83 * 1 tells cmos_init to always initialize the CMOS.
84 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060085 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060086
87 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060088 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060089
90 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060091 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060092
Raul E Rangela91eb902021-02-24 16:26:34 -070093 setup_serirq();
Felix Held390a2802021-10-21 03:13:42 +020094
95 fch_ioapic_init();
96 fch_configure_hpet();
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. */
Kyösti Mälkki8b894242022-05-26 15:42:59 +0300120 mmio_range(dev, 2, SPI_BASE_ADDRESS, 1 * KiB);
Marc Jones24484842017-05-04 21:17:45 -0600121
122 res = new_resource(dev, 3); /* IOAPIC */
123 res->base = IO_APIC_ADDR;
124 res->size = 0x00001000;
125 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
126
127 compact_resources(dev);
128}
129
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600130static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600131{
132 struct resource *res;
133 u32 spi_enable_bits;
134
135 /* Special case. The SpiRomEnable and other enables should STAY set. */
136 res = find_resource(dev, 2);
Felix Held697fa742022-03-03 20:54:38 +0100137 spi_enable_bits = pci_read_config32(dev, SPI_BASE_ADDRESS_REGISTER);
Marshall Dawsoneceaa972019-05-05 18:35:12 -0600138 spi_enable_bits &= SPI_BASE_ALIGNMENT - 1;
Felix Held697fa742022-03-03 20:54:38 +0100139 pci_write_config32(dev, SPI_BASE_ADDRESS_REGISTER,
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600140 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600141
142 pci_dev_set_resources(dev);
143}
144
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700145static void configure_child_lpc_windows(struct device *dev, struct device *child)
Richard Spiegelaa183852017-10-05 18:53:31 -0700146{
147 struct resource *res;
148 u32 base, end;
149 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700150 int wideio_index;
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700151 u32 reg, reg_x;
152
153 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
154 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
155
Richard Spiegel7a39e022017-11-09 10:54:04 -0700156 /*
157 * Be a bit relaxed, tolerate that LPC region might be bigger than
158 * resource we try to fit, do it like this for all regions < 16 bytes.
159 * If there is a resource > 16 bytes it must be 512 bytes to be able
160 * to allocate the fresh LPC window.
161 *
162 * AGESA and early initialization can set a wide IO port. This code
163 * will verify if required region was previously set and will avoid
164 * setting a new wide IO resource if one is already set.
165 */
166
Richard Spiegelaa183852017-10-05 18:53:31 -0700167 for (res = child->resource_list; res; res = res->next) {
168 if (!(res->flags & IORESOURCE_IO))
169 continue;
170 base = res->base;
171 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700172 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700173 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
174 dev_path(child), base, end);
175 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700176 switch (base) {
177 case 0x60: /* KB */
178 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700179 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700180 rsize = 1;
181 break;
182 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700183 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700184 rsize = 8;
185 break;
186 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700187 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700188 rsize = 8;
189 break;
190 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700191 set |= DECODE_ENABLE_PARALLEL_PORT0;
192 /* enable 0x778 for ECP mode */
193 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700194 rsize = 8;
195 break;
196 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700197 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700198 rsize = 8;
199 break;
200 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700201 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700202 rsize = 8;
203 break;
204 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700205 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700206 rsize = 8;
207 break;
208 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700209 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700210 rsize = 8;
211 break;
212 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700213 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700214 rsize = 2;
215 break;
216 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700217 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700218 rsize = 0x40;
219 break;
220 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700221 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700222 rsize = 0x40;
223 break;
224 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700225 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700226 rsize = 0x40;
227 break;
228 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700229 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700230 rsize = 0x40;
231 break;
232 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700233 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700234 rsize = 0xc;
235 break;
236 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700237 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700238 rsize = 16;
239 break;
240 default:
241 rsize = 0;
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600242 wideio_index = lpc_find_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700243 if (wideio_index != WIDEIO_RANGE_ERROR) {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600244 rsize = lpc_wideio_size(wideio_index);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700245 printk(BIOS_DEBUG, "Covered by wideIO");
246 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700247 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700248 }
249 /* check if region found and matches the enable */
250 if (res->size <= rsize) {
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700251 reg |= set;
252 reg_x |= set_x;
Richard Spiegelaa183852017-10-05 18:53:31 -0700253 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700254 } else {
Marshall Dawson6ab5ed32019-05-29 09:24:18 -0600255 wideio_index = lpc_set_wideio_range(base, res->size);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700256 if (wideio_index != WIDEIO_RANGE_ERROR) {
257 /* preserve wide IO related bits. */
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700258 reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700259 LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelb5f96452017-11-22 15:28:25 -0700260 printk(BIOS_DEBUG,
261 "Range assigned to wide IO %d\n",
262 wideio_index);
263 } else {
264 printk(BIOS_ERR,
265 "cannot fit LPC decode region:");
266 printk(BIOS_ERR,
267 "%s, base = 0x%08x, end = 0x%08x\n",
268 dev_path(child), base, end);
269 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700270 }
271 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700272
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700273 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
274 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600275}
276
Furquan Shaikh511aa442020-05-04 23:42:46 -0700277static void configure_child_espi_windows(struct device *child)
278{
279 struct resource *res;
280
281 for (res = child->resource_list; res; res = res->next) {
282 if (res->flags & IORESOURCE_IO)
283 espi_open_io_window(res->base, res->size);
284 else if (res->flags & IORESOURCE_MEM)
285 espi_open_mmio_window(res->base, res->size);
286 }
287}
288
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700289static void lpc_enable_children_resources(struct device *dev)
290{
291 struct bus *link;
292 struct device *child;
293
294 for (link = dev->link_list; link; link = link->next) {
295 for (child = link->children; child; child = child->sibling) {
296 if (!child->enabled)
297 continue;
298 if (child->path.type != DEVICE_PATH_PNP)
299 continue;
Furquan Shaikh511aa442020-05-04 23:42:46 -0700300 if (CONFIG(SOC_AMD_COMMON_BLOCK_USE_ESPI))
301 configure_child_espi_windows(child);
302 else
303 configure_child_lpc_windows(dev, child);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700304 }
305 }
306}
307
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200308static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600309{
310 pci_dev_enable_resources(dev);
Furquan Shaikh1e279a52020-05-04 21:22:22 -0700311 lpc_enable_children_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600312}
313
Felix Held3e29ca92021-02-16 23:52:58 +0100314#if CONFIG(HAVE_ACPI_TABLES)
315static const char *lpc_acpi_name(const struct device *dev)
316{
317 return "LPCB";
318}
319#endif
320
Marc Jones24484842017-05-04 21:17:45 -0600321static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600322 .read_resources = lpc_read_resources,
323 .set_resources = lpc_set_resources,
324 .enable_resources = lpc_enable_resources,
Zheng Baobdd50312021-01-26 18:27:46 +0800325#if CONFIG(HAVE_ACPI_TABLES)
Felix Held3e29ca92021-02-16 23:52:58 +0100326 .acpi_name = lpc_acpi_name,
Marc Jones257db582017-06-18 17:33:30 -0600327 .write_acpi_tables = southbridge_write_acpi_tables,
Zheng Baobdd50312021-01-26 18:27:46 +0800328#endif
Marc Jones24484842017-05-04 21:17:45 -0600329 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100330 .scan_bus = scan_static_bus,
Furquan Shaikh40454b72020-05-04 20:52:08 -0700331 .ops_pci = &pci_dev_ops_pci,
Marc Jones24484842017-05-04 21:17:45 -0600332};
333
334static const unsigned short pci_device_ids[] = {
Felix Held43cf27d2021-10-27 18:31:16 +0200335 /* PCI device ID is used on all discrete FCHs and Family 16h Models 00h-3Fh */
Felix Singer43b7f412022-03-07 04:34:52 +0100336 PCI_DID_AMD_SB900_LPC,
Felix Held43cf27d2021-10-27 18:31:16 +0200337 /* PCI device ID is used on all integrated FCHs except Family 16h Models 00h-3Fh */
Felix Singer43b7f412022-03-07 04:34:52 +0100338 PCI_DID_AMD_CZ_LPC,
Marc Jones24484842017-05-04 21:17:45 -0600339 0
340};
341static const struct pci_driver lpc_driver __pci_driver = {
342 .ops = &lpc_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100343 .vendor = PCI_VID_AMD,
Marc Jones24484842017-05-04 21:17:45 -0600344 .devices = pci_device_ids,
345};