blob: a838146c8466ce8ff95b6381bfaaf60bf4894423 [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>
Marc Jones24484842017-05-04 21:17:45 -060028#include <pc80/i8254.h>
29#include <pc80/i8259.h>
Marc Jones257db582017-06-18 17:33:30 -060030#include <soc/acpi.h>
Marshall Dawson4e101ad2017-06-15 12:17:38 -060031#include <soc/pci_devs.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060032#include <soc/southbridge.h>
Marc Jones257db582017-06-18 17:33:30 -060033#include <soc/nvs.h>
Marc Jones24484842017-05-04 21:17:45 -060034
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020035static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060036{
37 u8 byte;
Marc Jones24484842017-05-04 21:17:45 -060038
39 /* Initialize isa dma */
40 isa_dma_init();
41
42 /* Enable DMA transaction on the LPC bus */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060043 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070044 byte |= LEGACY_DMA_EN;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060045 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060046
47 /* Disable the timeout mechanism on LPC */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060048 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070049 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060050 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060051
52 /* Disable LPC MSI Capability */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060053 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070054 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060055 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070056
57 /*
58 * Keep the old way. i.e., when bus master/DMA cycle is going
Marshall Dawson4e101ad2017-06-15 12:17:38 -060059 * on on LPC, it holds PCI grant, so no LPC slave cycle can
60 * interrupt and visit LPC.
61 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070062 byte &= ~LPC_NOHOG;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060063 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060064
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070065 /*
Richard Spiegelee098782018-07-30 12:05:22 -070066 * Enable hand-instance of the pulse generator and SPI
67 * controller prefetch of flash.
Marshall Dawson4e101ad2017-06-15 12:17:38 -060068 */
Marshall Dawson1bc04e32019-05-02 18:56:54 -060069 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070070 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Marshall Dawson1bc04e32019-05-02 18:56:54 -060071 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060072
73 cmos_check_update_date();
74
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070075 /*
76 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060077 * The 0 argument tells cmos_init not to
78 * update CMOS unless it is invalid.
79 * 1 tells cmos_init to always initialize the CMOS.
80 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060081 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060082
83 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060084 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060085
86 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060087 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060088
89 /* Set up SERIRQ, enable continuous mode */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070090 byte = (PM_SERIRQ_NUM_BITS_21 | PM_SERIRQ_ENABLE);
Julius Wernercd49cce2019-03-05 16:53:33 -080091 if (!CONFIG(SERIRQ_CONTINUOUS_MODE))
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070092 byte |= PM_SERIRQ_MODE;
Marc Jones24484842017-05-04 21:17:45 -060093
94 pm_write8(PM_SERIRQ_CONF, byte);
95}
96
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020097static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060098{
99 struct resource *res;
Marc Jones257db582017-06-18 17:33:30 -0600100 global_nvs_t *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600101
102 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600103 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600104
105 /* Add an extra subtractive resource for both memory and I/O. */
106 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
107 res->base = 0;
108 res->size = 0x1000;
109 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
110 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
111
112 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700113 res->base = FLASH_BASE_ADDR;
114 res->size = CONFIG_ROM_SIZE;
Marc Jones24484842017-05-04 21:17:45 -0600115 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
116 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
117
118 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600119 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
120 IORESOURCE_SUBTRACTIVE);
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
Chris Ching6fc39d42017-12-20 16:06:03 -0700127 /* I2C devices (all 4 devices) */
128 res = new_resource(dev, 4);
129 res->base = I2C_BASE_ADDRESS;
130 res->size = I2C_DEVICE_SIZE * I2C_DEVICE_COUNT;
131 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
132
Marc Jones24484842017-05-04 21:17:45 -0600133 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600134
135 /* Allocate ACPI NVS in CBMEM */
136 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700137 printk(BIOS_DEBUG, "ACPI GNVS at %p\n", gnvs);
Marc Jones24484842017-05-04 21:17:45 -0600138}
139
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600140static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600141{
142 struct resource *res;
143 u32 spi_enable_bits;
144
145 /* Special case. The SpiRomEnable and other enables should STAY set. */
146 res = find_resource(dev, 2);
147 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700148 spi_enable_bits &= SPI_PRESERVE_BITS;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600149 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
150 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600151
152 pci_dev_set_resources(dev);
153}
154
Marshall Dawson1bc04e32019-05-02 18:56:54 -0600155static void set_child_resource(struct device *dev, struct device *child,
156 u32 *reg, u32 *reg_x)
Richard Spiegelaa183852017-10-05 18:53:31 -0700157{
158 struct resource *res;
159 u32 base, end;
160 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700161 int wideio_index;
Richard Spiegelaa183852017-10-05 18:53:31 -0700162
Richard Spiegel7a39e022017-11-09 10:54:04 -0700163 /*
164 * Be a bit relaxed, tolerate that LPC region might be bigger than
165 * resource we try to fit, do it like this for all regions < 16 bytes.
166 * If there is a resource > 16 bytes it must be 512 bytes to be able
167 * to allocate the fresh LPC window.
168 *
169 * AGESA and early initialization can set a wide IO port. This code
170 * will verify if required region was previously set and will avoid
171 * setting a new wide IO resource if one is already set.
172 */
173
Richard Spiegelaa183852017-10-05 18:53:31 -0700174 for (res = child->resource_list; res; res = res->next) {
175 if (!(res->flags & IORESOURCE_IO))
176 continue;
177 base = res->base;
178 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700179 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700180 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
181 dev_path(child), base, end);
182 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700183 switch (base) {
184 case 0x60: /* KB */
185 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700186 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700187 rsize = 1;
188 break;
189 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700190 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700191 rsize = 8;
192 break;
193 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700194 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700195 rsize = 8;
196 break;
197 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700198 set |= DECODE_ENABLE_PARALLEL_PORT0;
199 /* enable 0x778 for ECP mode */
200 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700201 rsize = 8;
202 break;
203 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700204 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700205 rsize = 8;
206 break;
207 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700208 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700209 rsize = 8;
210 break;
211 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700212 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700213 rsize = 8;
214 break;
215 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700216 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700217 rsize = 8;
218 break;
219 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700220 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700221 rsize = 2;
222 break;
223 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700224 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700225 rsize = 0x40;
226 break;
227 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700228 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700229 rsize = 0x40;
230 break;
231 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700232 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700233 rsize = 0x40;
234 break;
235 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700236 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700237 rsize = 0x40;
238 break;
239 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700240 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700241 rsize = 0xc;
242 break;
243 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700244 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700245 rsize = 16;
246 break;
247 default:
248 rsize = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700249 wideio_index = sb_find_wideio_range(base, res->size);
250 if (wideio_index != WIDEIO_RANGE_ERROR) {
251 rsize = sb_wideio_size(wideio_index);
252 printk(BIOS_DEBUG, "Covered by wideIO");
253 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700254 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700255 }
256 /* check if region found and matches the enable */
257 if (res->size <= rsize) {
258 *reg |= set;
259 *reg_x |= set_x;
260 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700261 } else {
Richard Spiegelb5f96452017-11-22 15:28:25 -0700262 wideio_index = sb_set_wideio_range(base, res->size);
263 if (wideio_index != WIDEIO_RANGE_ERROR) {
264 /* preserve wide IO related bits. */
Marshall Dawson1bc04e32019-05-02 18:56:54 -0600265 *reg_x = pci_read_config32(dev,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700266 LPC_IO_OR_MEM_DECODE_ENABLE);
267
268 printk(BIOS_DEBUG,
269 "Range assigned to wide IO %d\n",
270 wideio_index);
271 } else {
272 printk(BIOS_ERR,
273 "cannot fit LPC decode region:");
274 printk(BIOS_ERR,
275 "%s, base = 0x%08x, end = 0x%08x\n",
276 dev_path(child), base, end);
277 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700278 }
279 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700280}
281
Marc Jones24484842017-05-04 21:17:45 -0600282/**
283 * @brief Enable resources for children devices
284 *
285 * @param dev the device whose children's resources are to be enabled
286 *
287 */
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200288static void lpc_enable_childrens_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600289{
290 struct bus *link;
291 u32 reg, reg_x;
Marc Jones24484842017-05-04 21:17:45 -0600292
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700293 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
294 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Marc Jones24484842017-05-04 21:17:45 -0600295
Richard Spiegelaa183852017-10-05 18:53:31 -0700296 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200297 struct device *child;
Marc Jones24484842017-05-04 21:17:45 -0600298 for (child = link->children; child;
299 child = child->sibling) {
300 if (child->enabled
Marshall Dawson1bc04e32019-05-02 18:56:54 -0600301 && (child->path.type == DEVICE_PATH_PNP))
302 set_child_resource(dev, child, &reg, &reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600303 }
304 }
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700305 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
306 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600307}
308
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200309static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600310{
311 pci_dev_enable_resources(dev);
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600312 lpc_enable_childrens_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600313}
314
Marc Jones24484842017-05-04 21:17:45 -0600315static struct pci_operations lops_pci = {
316 .set_subsystem = pci_dev_set_subsystem,
317};
318
319static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600320 .read_resources = lpc_read_resources,
321 .set_resources = lpc_set_resources,
322 .enable_resources = lpc_enable_resources,
Marc Jones257db582017-06-18 17:33:30 -0600323 .acpi_inject_dsdt_generator = southbridge_inject_dsdt,
324 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600325 .init = lpc_init,
326 .scan_bus = scan_lpc_bus,
327 .ops_pci = &lops_pci,
328};
329
330static const unsigned short pci_device_ids[] = {
331 PCI_DEVICE_ID_AMD_SB900_LPC,
332 PCI_DEVICE_ID_AMD_CZ_LPC,
333 0
334};
335static const struct pci_driver lpc_driver __pci_driver = {
336 .ops = &lpc_ops,
337 .vendor = PCI_VENDOR_ID_AMD,
338 .devices = pci_device_ids,
339};