blob: 6833db6a8a74109fb8d5b5b2d00983177b0d3ff1 [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>
27#include <arch/io.h>
28#include <arch/ioapic.h>
29#include <arch/acpi.h>
30#include <pc80/i8254.h>
31#include <pc80/i8259.h>
Marc Jones257db582017-06-18 17:33:30 -060032#include <soc/acpi.h>
Marshall Dawson4e101ad2017-06-15 12:17:38 -060033#include <soc/pci_devs.h>
Marc Jonesdfeb1c42017-08-07 19:08:24 -060034#include <soc/southbridge.h>
Marc Jones257db582017-06-18 17:33:30 -060035#include <soc/nvs.h>
Marc Jones24484842017-05-04 21:17:45 -060036
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020037static void lpc_init(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -060038{
39 u8 byte;
40 u32 dword;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020041 struct device *sm_dev;
Marc Jones24484842017-05-04 21:17:45 -060042
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070043 /*
44 * Enable the LPC Controller
45 * SMBus register 0x64 is not defined in public datasheet.
46 */
Chris Ching6a35fab2017-10-19 11:45:30 -060047 sm_dev = dev_find_slot(0, SMBUS_DEVFN);
Marc Jones24484842017-05-04 21:17:45 -060048 dword = pci_read_config32(sm_dev, 0x64);
49 dword |= 1 << 20;
50 pci_write_config32(sm_dev, 0x64, dword);
51
52 /* Initialize isa dma */
53 isa_dma_init();
54
55 /* Enable DMA transaction on the LPC bus */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070056 byte = pci_read_config8(dev, LPC_PCI_CONTROL);
57 byte |= LEGACY_DMA_EN;
58 pci_write_config8(dev, LPC_PCI_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060059
60 /* Disable the timeout mechanism on LPC */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070061 byte = pci_read_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
62 byte &= ~LPC_SYNC_TIMEOUT_COUNT_ENABLE;
63 pci_write_config8(dev, LPC_IO_OR_MEM_DECODE_ENABLE, byte);
Marc Jones24484842017-05-04 21:17:45 -060064
65 /* Disable LPC MSI Capability */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070066 byte = pci_read_config8(dev, LPC_MISC_CONTROL_BITS);
67 /* BIT 1 is not defined in public datasheet. */
Marc Jones24484842017-05-04 21:17:45 -060068 byte &= ~(1 << 1);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070069
70 /*
71 * Keep the old way. i.e., when bus master/DMA cycle is going
Marshall Dawson4e101ad2017-06-15 12:17:38 -060072 * on on LPC, it holds PCI grant, so no LPC slave cycle can
73 * interrupt and visit LPC.
74 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070075 byte &= ~LPC_NOHOG;
76 pci_write_config8(dev, LPC_MISC_CONTROL_BITS, byte);
Marc Jones24484842017-05-04 21:17:45 -060077
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070078 /*
Richard Spiegelee098782018-07-30 12:05:22 -070079 * Enable hand-instance of the pulse generator and SPI
80 * controller prefetch of flash.
Marshall Dawson4e101ad2017-06-15 12:17:38 -060081 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070082 byte = pci_read_config8(dev, LPC_HOST_CONTROL);
Richard Spiegelee098782018-07-30 12:05:22 -070083 byte |= PREFETCH_EN_SPI_FROM_HOST | T_START_ENH;
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070084 pci_write_config8(dev, LPC_HOST_CONTROL, byte);
Marc Jones24484842017-05-04 21:17:45 -060085
86 cmos_check_update_date();
87
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -070088 /*
89 * Initialize the real time clock.
Marc Jones24484842017-05-04 21:17:45 -060090 * The 0 argument tells cmos_init not to
91 * update CMOS unless it is invalid.
92 * 1 tells cmos_init to always initialize the CMOS.
93 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060094 cmos_init(0);
Marc Jones24484842017-05-04 21:17:45 -060095
96 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060097 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060098
99 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600100 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -0600101
102 /* Set up SERIRQ, enable continuous mode */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700103 byte = (PM_SERIRQ_NUM_BITS_21 | PM_SERIRQ_ENABLE);
Marc Jones24484842017-05-04 21:17:45 -0600104 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700105 byte |= PM_SERIRQ_MODE;
Marc Jones24484842017-05-04 21:17:45 -0600106
107 pm_write8(PM_SERIRQ_CONF, byte);
108}
109
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200110static void lpc_read_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600111{
112 struct resource *res;
Marc Jones257db582017-06-18 17:33:30 -0600113 global_nvs_t *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600114
115 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600116 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600117
118 /* Add an extra subtractive resource for both memory and I/O. */
119 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
120 res->base = 0;
121 res->size = 0x1000;
122 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
123 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
124
125 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700126 res->base = FLASH_BASE_ADDR;
127 res->size = CONFIG_ROM_SIZE;
Marc Jones24484842017-05-04 21:17:45 -0600128 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
129 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
130
131 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600132 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
133 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600134
135 res = new_resource(dev, 3); /* IOAPIC */
136 res->base = IO_APIC_ADDR;
137 res->size = 0x00001000;
138 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
139
Chris Ching6fc39d42017-12-20 16:06:03 -0700140 /* I2C devices (all 4 devices) */
141 res = new_resource(dev, 4);
142 res->base = I2C_BASE_ADDRESS;
143 res->size = I2C_DEVICE_SIZE * I2C_DEVICE_COUNT;
144 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
145
Marc Jones24484842017-05-04 21:17:45 -0600146 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600147
148 /* Allocate ACPI NVS in CBMEM */
149 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
Marc Jones24484842017-05-04 21:17:45 -0600150}
151
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600152static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600153{
154 struct resource *res;
155 u32 spi_enable_bits;
156
157 /* Special case. The SpiRomEnable and other enables should STAY set. */
158 res = find_resource(dev, 2);
159 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700160 spi_enable_bits &= SPI_PRESERVE_BITS;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600161 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
162 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600163
164 pci_dev_set_resources(dev);
165}
166
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200167static void set_child_resource(struct device *child,
Richard Spiegelaa183852017-10-05 18:53:31 -0700168 u32 *reg,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700169 u32 *reg_x)
Richard Spiegelaa183852017-10-05 18:53:31 -0700170{
171 struct resource *res;
172 u32 base, end;
173 u32 rsize = 0, set = 0, set_x = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700174 int wideio_index;
Richard Spiegelaa183852017-10-05 18:53:31 -0700175
Richard Spiegel7a39e022017-11-09 10:54:04 -0700176 /*
177 * Be a bit relaxed, tolerate that LPC region might be bigger than
178 * resource we try to fit, do it like this for all regions < 16 bytes.
179 * If there is a resource > 16 bytes it must be 512 bytes to be able
180 * to allocate the fresh LPC window.
181 *
182 * AGESA and early initialization can set a wide IO port. This code
183 * will verify if required region was previously set and will avoid
184 * setting a new wide IO resource if one is already set.
185 */
186
Richard Spiegelaa183852017-10-05 18:53:31 -0700187 for (res = child->resource_list; res; res = res->next) {
188 if (!(res->flags & IORESOURCE_IO))
189 continue;
190 base = res->base;
191 end = resource_end(res);
Richard Spiegelaa183852017-10-05 18:53:31 -0700192 printk(BIOS_DEBUG,
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700193 "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
194 dev_path(child), base, end);
195 /* find a resource size */
Richard Spiegelaa183852017-10-05 18:53:31 -0700196 switch (base) {
197 case 0x60: /* KB */
198 case 0x64: /* MS */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700199 set |= DECODE_ENABLE_KBC_PORT;
Richard Spiegelaa183852017-10-05 18:53:31 -0700200 rsize = 1;
201 break;
202 case 0x3f8: /* COM1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700203 set |= DECODE_ENABLE_SERIAL_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700204 rsize = 8;
205 break;
206 case 0x2f8: /* COM2 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700207 set |= DECODE_ENABLE_SERIAL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700208 rsize = 8;
209 break;
210 case 0x378: /* Parallel 1 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700211 set |= DECODE_ENABLE_PARALLEL_PORT0;
212 /* enable 0x778 for ECP mode */
213 set |= DECODE_ENABLE_PARALLEL_PORT1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700214 rsize = 8;
215 break;
216 case 0x3f0: /* FD0 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700217 set |= DECODE_ENABLE_FDC_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700218 rsize = 8;
219 break;
220 case 0x220: /* 0x220 - 0x227 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700221 set |= DECODE_ENABLE_SERIAL_PORT2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700222 rsize = 8;
223 break;
224 case 0x228: /* 0x228 - 0x22f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700225 set |= DECODE_ENABLE_SERIAL_PORT3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700226 rsize = 8;
227 break;
228 case 0x238: /* 0x238 - 0x23f */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700229 set |= DECODE_ENABLE_SERIAL_PORT4;
Richard Spiegelaa183852017-10-05 18:53:31 -0700230 rsize = 8;
231 break;
232 case 0x300: /* 0x300 - 0x301 */
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700233 set |= DECODE_ENABLE_MIDI_PORT0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700234 rsize = 2;
235 break;
236 case 0x400:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700237 set_x |= DECODE_IO_PORT_ENABLE0;
Richard Spiegelaa183852017-10-05 18:53:31 -0700238 rsize = 0x40;
239 break;
240 case 0x480:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700241 set_x |= DECODE_IO_PORT_ENABLE1;
Richard Spiegelaa183852017-10-05 18:53:31 -0700242 rsize = 0x40;
243 break;
244 case 0x500:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700245 set_x |= DECODE_IO_PORT_ENABLE2;
Richard Spiegelaa183852017-10-05 18:53:31 -0700246 rsize = 0x40;
247 break;
248 case 0x580:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700249 set_x |= DECODE_IO_PORT_ENABLE3;
Richard Spiegelaa183852017-10-05 18:53:31 -0700250 rsize = 0x40;
251 break;
252 case 0x4700:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700253 set_x |= DECODE_IO_PORT_ENABLE5;
Richard Spiegelaa183852017-10-05 18:53:31 -0700254 rsize = 0xc;
255 break;
256 case 0xfd60:
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700257 set_x |= DECODE_IO_PORT_ENABLE6;
Richard Spiegelaa183852017-10-05 18:53:31 -0700258 rsize = 16;
259 break;
260 default:
261 rsize = 0;
Richard Spiegelb5f96452017-11-22 15:28:25 -0700262 wideio_index = sb_find_wideio_range(base, res->size);
263 if (wideio_index != WIDEIO_RANGE_ERROR) {
264 rsize = sb_wideio_size(wideio_index);
265 printk(BIOS_DEBUG, "Covered by wideIO");
266 printk(BIOS_DEBUG, " %d\n", wideio_index);
Richard Spiegel7a39e022017-11-09 10:54:04 -0700267 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700268 }
269 /* check if region found and matches the enable */
270 if (res->size <= rsize) {
271 *reg |= set;
272 *reg_x |= set_x;
273 /* check if we can fit resource in variable range */
Richard Spiegelaa183852017-10-05 18:53:31 -0700274 } else {
Richard Spiegelb5f96452017-11-22 15:28:25 -0700275 wideio_index = sb_set_wideio_range(base, res->size);
276 if (wideio_index != WIDEIO_RANGE_ERROR) {
277 /* preserve wide IO related bits. */
278 *reg_x = pci_read_config32(SOC_LPC_DEV,
279 LPC_IO_OR_MEM_DECODE_ENABLE);
280
281 printk(BIOS_DEBUG,
282 "Range assigned to wide IO %d\n",
283 wideio_index);
284 } else {
285 printk(BIOS_ERR,
286 "cannot fit LPC decode region:");
287 printk(BIOS_ERR,
288 "%s, base = 0x%08x, end = 0x%08x\n",
289 dev_path(child), base, end);
290 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700291 }
292 }
Richard Spiegelaa183852017-10-05 18:53:31 -0700293}
294
Marc Jones24484842017-05-04 21:17:45 -0600295/**
296 * @brief Enable resources for children devices
297 *
298 * @param dev the device whose children's resources are to be enabled
299 *
300 */
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200301static void lpc_enable_childrens_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600302{
303 struct bus *link;
304 u32 reg, reg_x;
Marc Jones24484842017-05-04 21:17:45 -0600305
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700306 reg = pci_read_config32(dev, LPC_IO_PORT_DECODE_ENABLE);
307 reg_x = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
Marc Jones24484842017-05-04 21:17:45 -0600308
Richard Spiegelaa183852017-10-05 18:53:31 -0700309 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200310 struct device *child;
Marc Jones24484842017-05-04 21:17:45 -0600311 for (child = link->children; child;
312 child = child->sibling) {
313 if (child->enabled
314 && (child->path.type == DEVICE_PATH_PNP)) {
Richard Spiegel7a39e022017-11-09 10:54:04 -0700315 set_child_resource(child,
Richard Spiegelaa183852017-10-05 18:53:31 -0700316 &reg,
Richard Spiegelb5f96452017-11-22 15:28:25 -0700317 &reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600318 }
319 }
320 }
Richard Spiegelc5ecd3e2017-09-29 10:05:35 -0700321 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, reg);
322 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, reg_x);
Marc Jones24484842017-05-04 21:17:45 -0600323}
324
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200325static void lpc_enable_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600326{
327 pci_dev_enable_resources(dev);
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600328 lpc_enable_childrens_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600329}
330
331unsigned long acpi_fill_mcfg(unsigned long current)
332{
333 /* Just a dummy */
334 return current;
335}
336
337static struct pci_operations lops_pci = {
338 .set_subsystem = pci_dev_set_subsystem,
339};
340
341static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600342 .read_resources = lpc_read_resources,
343 .set_resources = lpc_set_resources,
344 .enable_resources = lpc_enable_resources,
Marc Jones257db582017-06-18 17:33:30 -0600345 .acpi_inject_dsdt_generator = southbridge_inject_dsdt,
346 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600347 .init = lpc_init,
348 .scan_bus = scan_lpc_bus,
349 .ops_pci = &lops_pci,
350};
351
352static const unsigned short pci_device_ids[] = {
353 PCI_DEVICE_ID_AMD_SB900_LPC,
354 PCI_DEVICE_ID_AMD_CZ_LPC,
355 0
356};
357static const struct pci_driver lpc_driver __pci_driver = {
358 .ops = &lpc_ops,
359 .vendor = PCI_VENDOR_ID_AMD,
360 .devices = pci_device_ids,
361};