blob: e3475eebcb644d40452fdf21c936d885c2af0e9d [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
Richard Spiegel7a39e022017-11-09 10:54:04 -07004 * Copyright (C) 2010-2017 Advanced Micro Devices, Inc.
Marc Jones24484842017-05-04 21:17:45 -06005 * Copyright (C) 2014 Sage Electronic Engineering, LLC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
Marc Jones257db582017-06-18 17:33:30 -060017#include <cbmem.h>
Marc Jones24484842017-05-04 21:17:45 -060018#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pnp.h>
22#include <device/pci_ids.h>
23#include <device/pci_ops.h>
24#include <device/pci_def.h>
25#include <pc80/mc146818rtc.h>
26#include <pc80/isa-dma.h>
Marc Jones24484842017-05-04 21:17:45 -060027#include <arch/ioapic.h>
28#include <arch/acpi.h>
29#include <pc80/i8254.h>
30#include <pc80/i8259.h>
Marc Jones257db582017-06-18 17:33:30 -060031#include <soc/acpi.h>
Marshall Dawson4e101ad2017-06-15 12:17:38 -060032#include <soc/pci_devs.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060033#include <soc/southbridge.h>
Marc Jones257db582017-06-18 17:33:30 -060034#include <soc/nvs.h>
Marc Jones24484842017-05-04 21:17:45 -060035
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020036static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060037{
38 u8 byte;
39 u32 dword;
Marc Jones24484842017-05-04 21:17:45 -060040
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070041 /*
42 * Enable the LPC Controller
43 * SMBus register 0x64 is not defined in public datasheet.
44 */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070045 dword = pci_read_config32(SOC_SMBUS_DEV, 0x64);
Marc Jones24484842017-05-04 21:17:45 -060046 dword |= 1 << 20;
Richard Spiegel41baf0c2018-10-22 13:57:18 -070047 pci_write_config32(SOC_SMBUS_DEV, 0x64, dword);
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 */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070053 byte = pci_read_config8(SOC_SMBUS_DEV, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070054 byte |= LEGACY_DMA_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -070055 pci_write_config8(SOC_SMBUS_DEV, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060056
57 /* Disable the timeout mechanism on LPC */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070058 byte = pci_read_config8(SOC_SMBUS_DEV, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070059 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Richard Spiegel41baf0c2018-10-22 13:57:18 -070060 pci_write_config8(SOC_SMBUS_DEV, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060061
62 /* Disable LPC MSI Capability */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070063 byte = pci_read_config8(SOC_SMBUS_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
67 /*
68 * Keep the old way. i.e., when bus master/DMA cycle is going
Marshall Dawson4e101ad2017-06-15 12:17:38 -060069 * on on LPC, it holds PCI grant, so no LPC slave cycle can
70 * interrupt and visit LPC.
71 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070072 byte &= ~LPC_NOHOG;
Richard Spiegel41baf0c2018-10-22 13:57:18 -070073 pci_write_config8(SOC_SMBUS_DEV, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060074
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070075 /*
Richard Spiegelee098782018-07-30 12:05:22 -070076 * Enable hand-instance of the pulse generator and SPI
77 * controller prefetch of flash.
Marshall Dawson4e101ad2017-06-15 12:17:38 -060078 */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070079 byte = pci_read_config8(SOC_SMBUS_DEV, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070080 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Richard Spiegel41baf0c2018-10-22 13:57:18 -070081 pci_write_config8(SOC_SMBUS_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
99 /* Set up SERIRQ, enable continuous mode */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700100 byte = (PM_SERIRQ_NUM_BITS_21 | PM_SERIRQ_ENABLE);
Julius Wernercd49cce2019-03-05 16:53:33 -0800101 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700102 byte |= PM_SERIRQ_MODE;
Marc Jones24484842017-05-04 21:17:45 -0600103
104 pm_write8(PM_SERIRQ_CONF, byte);
105}
106
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200107static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600108{
109 struct resource *res;
Marc Jones257db582017-06-18 17:33:30 -0600110 global_nvs_t *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600111
112 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600113 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600114
115 /* Add an extra subtractive resource for both memory and I/O. */
116 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
117 res->base = 0;
118 res->size = 0x1000;
119 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
120 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
121
122 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700123 res->base = FLASH_BASE_ADDR;
124 res->size = CONFIG_ROM_SIZE;
Marc Jones24484842017-05-04 21:17:45 -0600125 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
126 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
127
128 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600129 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
130 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600131
132 res = new_resource(dev, 3); /* IOAPIC */
133 res->base = IO_APIC_ADDR;
134 res->size = 0x00001000;
135 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
136
Chris Ching6fc39d42017-12-20 16:06:03 -0700137 /* I2C devices (all 4 devices) */
138 res = new_resource(dev, 4);
139 res->base = I2C_BASE_ADDRESS;
140 res->size = I2C_DEVICE_SIZE * I2C_DEVICE_COUNT;
141 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
142
Marc Jones24484842017-05-04 21:17:45 -0600143 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600144
145 /* Allocate ACPI NVS in CBMEM */
146 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700147 printk(BIOS_DEBUG, "ACPI GNVS at %p\n", gnvs);
Marc Jones24484842017-05-04 21:17:45 -0600148}
149
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600150static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600151{
152 struct resource *res;
153 u32 spi_enable_bits;
154
155 /* Special case. The SpiRomEnable and other enables should STAY set. */
156 res = find_resource(dev, 2);
157 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700158 spi_enable_bits &= SPI_PRESERVE_BITS;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600159 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
160 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600161
162 pci_dev_set_resources(dev);
163}
164
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200165static void set_child_resource(struct device *child,
Richard Spiegelaa183852017-10-05 18:53:31 -0700166 u32 *reg,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700167 u32 *reg_x)
Richard Spiegelaa183852017-10-05 18:53:31 -0700168{
169 struct resource *res;
170 u32 base, end;
171 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700172 int wideio_index;
Richard Spiegelaa183852017-10-05 18:53:31 -0700173
Richard Spiegel7a39e022017-11-09 10:54:04 -0700174 /*
175 * Be a bit relaxed, tolerate that LPC region might be bigger than
176 * resource we try to fit, do it like this for all regions < 16 bytes.
177 * If there is a resource > 16 bytes it must be 512 bytes to be able
178 * to allocate the fresh LPC window.
179 *
180 * AGESA and early initialization can set a wide IO port. This code
181 * will verify if required region was previously set and will avoid
182 * setting a new wide IO resource if one is already set.
183 */
184
Richard Spiegelaa183852017-10-05 18:53:31 -0700185 for (res = child->resource_list; res; res = res->next) {
186 if (!(res->flags & IORESOURCE_IO))
187 continue;
188 base = res->base;
189 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700190 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700191 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
192 dev_path(child), base, end);
193 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700194 switch (base) {
195 case 0x60: /* KB */
196 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700197 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700198 rsize = 1;
199 break;
200 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700201 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700202 rsize = 8;
203 break;
204 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700205 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700206 rsize = 8;
207 break;
208 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700209 set |= DECODE_ENABLE_PARALLEL_PORT0;
210 /* enable 0x778 for ECP mode */
211 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700212 rsize = 8;
213 break;
214 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700215 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700216 rsize = 8;
217 break;
218 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700219 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700220 rsize = 8;
221 break;
222 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700223 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700224 rsize = 8;
225 break;
226 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700227 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700228 rsize = 8;
229 break;
230 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700231 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700232 rsize = 2;
233 break;
234 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700235 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700236 rsize = 0x40;
237 break;
238 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700239 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700240 rsize = 0x40;
241 break;
242 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700243 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700244 rsize = 0x40;
245 break;
246 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700247 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700248 rsize = 0x40;
249 break;
250 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700251 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700252 rsize = 0xc;
253 break;
254 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700255 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700256 rsize = 16;
257 break;
258 default:
259 rsize = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700260 wideio_index = sb_find_wideio_range(base, res->size);
261 if (wideio_index != WIDEIO_RANGE_ERROR) {
262 rsize = sb_wideio_size(wideio_index);
263 printk(BIOS_DEBUG, "Covered by wideIO");
264 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700265 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700266 }
267 /* check if region found and matches the enable */
268 if (res->size <= rsize) {
269 *reg |= set;
270 *reg_x |= set_x;
271 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700272 } else {
Richard Spiegelb5f96452017-11-22 15:28:25 -0700273 wideio_index = sb_set_wideio_range(base, res->size);
274 if (wideio_index != WIDEIO_RANGE_ERROR) {
275 /* preserve wide IO related bits. */
276 *reg_x = pci_read_config32(SOC_LPC_DEV,
277 LPC_IO_OR_MEM_DECODE_ENABLE);
278
279 printk(BIOS_DEBUG,
280 "Range assigned to wide IO %d\n",
281 wideio_index);
282 } else {
283 printk(BIOS_ERR,
284 "cannot fit LPC decode region:");
285 printk(BIOS_ERR,
286 "%s, base = 0x%08x, end = 0x%08x\n",
287 dev_path(child), base, end);
288 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700289 }
290 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700291}
292
Marc Jones24484842017-05-04 21:17:45 -0600293/**
294 * @brief Enable resources for children devices
295 *
296 * @param dev the device whose children's resources are to be enabled
297 *
298 */
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200299static void lpc_enable_childrens_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600300{
301 struct bus *link;
302 u32 reg, reg_x;
Marc Jones24484842017-05-04 21:17:45 -0600303
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700304 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
305 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Marc Jones24484842017-05-04 21:17:45 -0600306
Richard Spiegelaa183852017-10-05 18:53:31 -0700307 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200308 struct device *child;
Marc Jones24484842017-05-04 21:17:45 -0600309 for (child = link->children; child;
310 child = child->sibling) {
311 if (child->enabled
312 && (child->path.type == DEVICE_PATH_PNP)) {
Richard Spiegel7a39e022017-11-09 10:54:04 -0700313 set_child_resource(child,
Richard Spiegelaa183852017-10-05 18:53:31 -0700314 &reg,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700315 &reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600316 }
317 }
318 }
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700319 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
320 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600321}
322
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200323static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600324{
325 pci_dev_enable_resources(dev);
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600326 lpc_enable_childrens_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600327}
328
329unsigned long acpi_fill_mcfg(unsigned long current)
330{
331 /* Just a dummy */
332 return current;
333}
334
335static struct pci_operations lops_pci = {
336 .set_subsystem = pci_dev_set_subsystem,
337};
338
339static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600340 .read_resources = lpc_read_resources,
341 .set_resources = lpc_set_resources,
342 .enable_resources = lpc_enable_resources,
Marc Jones257db582017-06-18 17:33:30 -0600343 .acpi_inject_dsdt_generator = southbridge_inject_dsdt,
344 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600345 .init = lpc_init,
346 .scan_bus = scan_lpc_bus,
347 .ops_pci = &lops_pci,
348};
349
350static const unsigned short pci_device_ids[] = {
351 PCI_DEVICE_ID_AMD_SB900_LPC,
352 PCI_DEVICE_ID_AMD_CZ_LPC,
353 0
354};
355static const struct pci_driver lpc_driver __pci_driver = {
356 .ops = &lpc_ops,
357 .vendor = PCI_VENDOR_ID_AMD,
358 .devices = pci_device_ids,
359};