blob: 78864337913dbbcf9db00498ecb0343cd93d5313 [file] [log] [blame]
Marc Jones24484842017-05-04 21:17:45 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2010 Advanced Micro Devices, Inc.
5 * 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
37static void lpc_init(device_t dev)
38{
39 u8 byte;
40 u32 dword;
41 device_t sm_dev;
42
43 /* Enable the LPC Controller */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060044 sm_dev = dev_find_slot(0, PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC));
Marc Jones24484842017-05-04 21:17:45 -060045 dword = pci_read_config32(sm_dev, 0x64);
46 dword |= 1 << 20;
47 pci_write_config32(sm_dev, 0x64, dword);
48
49 /* Initialize isa dma */
50 isa_dma_init();
51
52 /* Enable DMA transaction on the LPC bus */
53 byte = pci_read_config8(dev, 0x40);
54 byte |= (1 << 2);
55 pci_write_config8(dev, 0x40, byte);
56
57 /* Disable the timeout mechanism on LPC */
58 byte = pci_read_config8(dev, 0x48);
59 byte &= ~(1 << 7);
60 pci_write_config8(dev, 0x48, byte);
61
62 /* Disable LPC MSI Capability */
63 byte = pci_read_config8(dev, 0x78);
64 byte &= ~(1 << 1);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060065 /* Keep the old way. i.e., when bus master/DMA cycle is going
66 * on on LPC, it holds PCI grant, so no LPC slave cycle can
67 * interrupt and visit LPC.
68 */
69 byte &= ~(1 << 0);
Marc Jones24484842017-05-04 21:17:45 -060070 pci_write_config8(dev, 0x78, byte);
71
Marshall Dawson4e101ad2017-06-15 12:17:38 -060072 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads
73 * code from SPI ROM
74 * bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12.
75 * todo: verify both these against BKDG
76 */
77 byte = pci_read_config8(dev, 0xbb);
Marc Jones24484842017-05-04 21:17:45 -060078 byte |= 1 << 0 | 1 << 3;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060079 pci_write_config8(dev, 0xbb, byte);
Marc Jones24484842017-05-04 21:17:45 -060080
81 cmos_check_update_date();
82
83 /* Initialize the real time clock.
84 * 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
96 /* Set up SERIRQ, enable continuous mode */
97 byte = (BIT(4) | BIT(7));
98 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
99 byte |= BIT(6);
100
101 pm_write8(PM_SERIRQ_CONF, byte);
102}
103
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600104static void lpc_read_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600105{
106 struct resource *res;
Marc Jones257db582017-06-18 17:33:30 -0600107 global_nvs_t *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600108
109 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600110 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600111
112 /* Add an extra subtractive resource for both memory and I/O. */
113 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
114 res->base = 0;
115 res->size = 0x1000;
116 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
117 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
118
119 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
120 res->base = 0xff800000;
121 res->size = 0x00800000; /* 8 MB for flash */
122 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
123 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
124
125 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600126 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
127 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600128
129 res = new_resource(dev, 3); /* IOAPIC */
130 res->base = IO_APIC_ADDR;
131 res->size = 0x00001000;
132 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
133
134 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600135
136 /* Allocate ACPI NVS in CBMEM */
137 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
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);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600148 spi_enable_bits &= 0xf;
149 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
155/**
156 * @brief Enable resources for children devices
157 *
158 * @param dev the device whose children's resources are to be enabled
159 *
160 */
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600161static void lpc_enable_childrens_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600162{
163 struct bus *link;
164 u32 reg, reg_x;
165 int var_num = 0;
166 u16 reg_var[3];
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600167 u16 reg_size[1] = {512};
Marc Jones24484842017-05-04 21:17:45 -0600168 u8 wiosize = pci_read_config8(dev, 0x74);
169
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600170 /* Be a bit relaxed, tolerate that LPC region might be bigger than
171 * resource we try to fit, do it like this for all regions < 16 bytes.
172 * If there is a resource > 16 bytes it must be 512 bytes to be able
173 * to allocate the fresh LPC window.
Marc Jones24484842017-05-04 21:17:45 -0600174 *
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600175 * AGESA likes to enable already one LPC region in wide port base
176 * 0x64-0x65, using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
177 * The code tries to check if resource can fit into this region.
Marc Jones24484842017-05-04 21:17:45 -0600178 */
179
180 reg = pci_read_config32(dev, 0x44);
181 reg_x = pci_read_config32(dev, 0x48);
182
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600183 /* check if ranges are free and don't use them if already taken */
Marc Jones24484842017-05-04 21:17:45 -0600184 if (reg_x & (1 << 2))
185 var_num = 1;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600186 /* just in case check if someone did not manually set other ranges */
Marc Jones24484842017-05-04 21:17:45 -0600187 if (reg_x & (1 << 24))
188 var_num = 2;
189
190 if (reg_x & (1 << 25))
191 var_num = 3;
192
193 /* check AGESA region size */
194 if (wiosize & (1 << 0))
195 reg_size[0] = 16;
196
197 reg_var[2] = pci_read_config16(dev, 0x90);
198 reg_var[1] = pci_read_config16(dev, 0x66);
199 reg_var[0] = pci_read_config16(dev, 0x64);
200
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600201 /* todo: clean up the code style here */
202 for (link = dev->link_list ; link ; link = link->next) {
Marc Jones24484842017-05-04 21:17:45 -0600203 device_t child;
204 for (child = link->children; child;
205 child = child->sibling) {
206 if (child->enabled
207 && (child->path.type == DEVICE_PATH_PNP)) {
208 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600209 for (res = child->resource_list ; res ; res = res->next) {
Marc Jones24484842017-05-04 21:17:45 -0600210 u32 base, end; /* don't need long long */
211 u32 rsize, set = 0, set_x = 0;
212 if (!(res->flags & IORESOURCE_IO))
213 continue;
214 base = res->base;
215 end = resource_end(res);
216 /* find a resource size */
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600217 printk(BIOS_DEBUG, "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
Marc Jones24484842017-05-04 21:17:45 -0600218 dev_path(child), base, end);
219 switch (base) {
220 case 0x60: /* KB */
221 case 0x64: /* MS */
222 set |= (1 << 29);
223 rsize = 1;
224 break;
225 case 0x3f8: /* COM1 */
226 set |= (1 << 6);
227 rsize = 8;
228 break;
229 case 0x2f8: /* COM2 */
230 set |= (1 << 7);
231 rsize = 8;
232 break;
233 case 0x378: /* Parallel 1 */
234 set |= (1 << 0);
235 set |= (1 << 1); /* + 0x778 for ECP */
236 rsize = 8;
237 break;
238 case 0x3f0: /* FD0 */
239 set |= (1 << 26);
240 rsize = 8;
241 break;
242 case 0x220: /* 0x220 - 0x227 */
243 set |= (1 << 8);
244 rsize = 8;
245 break;
246 case 0x228: /* 0x228 - 0x22f */
247 set |= (1 << 9);
248 rsize = 8;
249 break;
250 case 0x238: /* 0x238 - 0x23f */
251 set |= (1 << 10);
252 rsize = 8;
253 break;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600254 case 0x300: /* 0x300 - 0x301 */
Marc Jones24484842017-05-04 21:17:45 -0600255 set |= (1 << 18);
256 rsize = 2;
257 break;
258 case 0x400:
259 set_x |= (1 << 16);
260 rsize = 0x40;
261 break;
262 case 0x480:
263 set_x |= (1 << 17);
264 rsize = 0x40;
265 break;
266 case 0x500:
267 set_x |= (1 << 18);
268 rsize = 0x40;
269 break;
270 case 0x580:
271 set_x |= (1 << 19);
272 rsize = 0x40;
273 break;
274 case 0x4700:
275 set_x |= (1 << 22);
276 rsize = 0xc;
277 break;
278 case 0xfd60:
279 set_x |= (1 << 23);
280 rsize = 16;
281 break;
282 default:
283 rsize = 0;
284 /* try AGESA allocated region in region 0 */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600285 if ((var_num > 0) && ((base >= reg_var[0]) &&
Marc Jones24484842017-05-04 21:17:45 -0600286 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
287 rsize = reg_size[0];
288 }
289 /* check if region found and matches the enable */
290 if (res->size <= rsize) {
291 reg |= set;
292 reg_x |= set_x;
293 /* check if we can fit resource in variable range */
294 } else if ((var_num < 3) &&
295 ((res->size <= 16) || (res->size == 512))) {
296 /* use variable ranges if pre-defined do not match */
297 switch (var_num) {
298 case 0:
299 reg_x |= (1 << 2);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600300 if (res->size <= 16)
Marc Jones24484842017-05-04 21:17:45 -0600301 wiosize |= (1 << 0);
Marc Jones24484842017-05-04 21:17:45 -0600302 break;
303 case 1:
304 reg_x |= (1 << 24);
305 if (res->size <= 16)
306 wiosize |= (1 << 2);
307 break;
308 case 2:
309 reg_x |= (1 << 25);
310 if (res->size <= 16)
311 wiosize |= (1 << 3);
312 break;
313 }
314 reg_var[var_num++] =
315 base & 0xffff;
316 } else {
317 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
318 dev_path(child), base, end);
319 }
320 }
321 }
322 }
323 }
324 pci_write_config32(dev, 0x44, reg);
325 pci_write_config32(dev, 0x48, reg_x);
326 /* Set WideIO for as many IOs found (fall through is on purpose) */
327 switch (var_num) {
328 case 3:
329 pci_write_config16(dev, 0x90, reg_var[2]);
330 /* fall through */
331 case 2:
332 pci_write_config16(dev, 0x66, reg_var[1]);
333 /* fall through */
334 case 1:
335 pci_write_config16(dev, 0x64, reg_var[0]);
336 break;
337 }
338 pci_write_config8(dev, 0x74, wiosize);
339}
340
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600341static void lpc_enable_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600342{
343 pci_dev_enable_resources(dev);
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600344 lpc_enable_childrens_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600345}
346
347unsigned long acpi_fill_mcfg(unsigned long current)
348{
349 /* Just a dummy */
350 return current;
351}
352
353static struct pci_operations lops_pci = {
354 .set_subsystem = pci_dev_set_subsystem,
355};
356
357static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600358 .read_resources = lpc_read_resources,
359 .set_resources = lpc_set_resources,
360 .enable_resources = lpc_enable_resources,
Marc Jones257db582017-06-18 17:33:30 -0600361 .acpi_inject_dsdt_generator = southbridge_inject_dsdt,
362 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600363 .init = lpc_init,
364 .scan_bus = scan_lpc_bus,
365 .ops_pci = &lops_pci,
366};
367
368static const unsigned short pci_device_ids[] = {
369 PCI_DEVICE_ID_AMD_SB900_LPC,
370 PCI_DEVICE_ID_AMD_CZ_LPC,
371 0
372};
373static const struct pci_driver lpc_driver __pci_driver = {
374 .ops = &lpc_ops,
375 .vendor = PCI_VENDOR_ID_AMD,
376 .devices = pci_device_ids,
377};