blob: 75cedfe47f56bb616e5eedfac7af1a85741434a1 [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#include <vboot/vbnv.h>
37
38static void lpc_init(device_t dev)
39{
40 u8 byte;
41 u32 dword;
42 device_t sm_dev;
43
44 /* Enable the LPC Controller */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060045 sm_dev = dev_find_slot(0, PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC));
Marc Jones24484842017-05-04 21:17:45 -060046 dword = pci_read_config32(sm_dev, 0x64);
47 dword |= 1 << 20;
48 pci_write_config32(sm_dev, 0x64, dword);
49
50 /* Initialize isa dma */
51 isa_dma_init();
52
53 /* Enable DMA transaction on the LPC bus */
54 byte = pci_read_config8(dev, 0x40);
55 byte |= (1 << 2);
56 pci_write_config8(dev, 0x40, byte);
57
58 /* Disable the timeout mechanism on LPC */
59 byte = pci_read_config8(dev, 0x48);
60 byte &= ~(1 << 7);
61 pci_write_config8(dev, 0x48, byte);
62
63 /* Disable LPC MSI Capability */
64 byte = pci_read_config8(dev, 0x78);
65 byte &= ~(1 << 1);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060066 /* Keep the old way. i.e., when bus master/DMA cycle is going
67 * on on LPC, it holds PCI grant, so no LPC slave cycle can
68 * interrupt and visit LPC.
69 */
70 byte &= ~(1 << 0);
Marc Jones24484842017-05-04 21:17:45 -060071 pci_write_config8(dev, 0x78, byte);
72
Marshall Dawson4e101ad2017-06-15 12:17:38 -060073 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads
74 * code from SPI ROM
75 * bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12.
76 * todo: verify both these against BKDG
77 */
78 byte = pci_read_config8(dev, 0xbb);
Marc Jones24484842017-05-04 21:17:45 -060079 byte |= 1 << 0 | 1 << 3;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060080 pci_write_config8(dev, 0xbb, byte);
Marc Jones24484842017-05-04 21:17:45 -060081
82 cmos_check_update_date();
83
84 /* Initialize the real time clock.
85 * The 0 argument tells cmos_init not to
86 * update CMOS unless it is invalid.
87 * 1 tells cmos_init to always initialize the CMOS.
88 */
89 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
90 init_vbnv_cmos(0);
91 else
92 cmos_init(0);
93
94 /* Initialize i8259 pic */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060095 setup_i8259();
Marc Jones24484842017-05-04 21:17:45 -060096
97 /* Initialize i8254 timers */
Marshall Dawson4e101ad2017-06-15 12:17:38 -060098 setup_i8254();
Marc Jones24484842017-05-04 21:17:45 -060099
100 /* Set up SERIRQ, enable continuous mode */
101 byte = (BIT(4) | BIT(7));
102 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
103 byte |= BIT(6);
104
105 pm_write8(PM_SERIRQ_CONF, byte);
106}
107
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600108static void lpc_read_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600109{
110 struct resource *res;
Marc Jones257db582017-06-18 17:33:30 -0600111 global_nvs_t *gnvs;
Marc Jones24484842017-05-04 21:17:45 -0600112
113 /* Get the normal pci resources of this device */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600114 pci_dev_read_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600115
116 /* Add an extra subtractive resource for both memory and I/O. */
117 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
118 res->base = 0;
119 res->size = 0x1000;
120 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
121 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
122
123 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
124 res->base = 0xff800000;
125 res->size = 0x00800000; /* 8 MB for flash */
126 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
127 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
128
129 /* Add a memory resource for the SPI BAR. */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600130 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1,
131 IORESOURCE_SUBTRACTIVE);
Marc Jones24484842017-05-04 21:17:45 -0600132
133 res = new_resource(dev, 3); /* IOAPIC */
134 res->base = IO_APIC_ADDR;
135 res->size = 0x00001000;
136 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
137
138 compact_resources(dev);
Marc Jones257db582017-06-18 17:33:30 -0600139
140 /* Allocate ACPI NVS in CBMEM */
141 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
Marc Jones24484842017-05-04 21:17:45 -0600142}
143
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600144static void lpc_set_resources(struct device *dev)
Marc Jones24484842017-05-04 21:17:45 -0600145{
146 struct resource *res;
147 u32 spi_enable_bits;
148
149 /* Special case. The SpiRomEnable and other enables should STAY set. */
150 res = find_resource(dev, 2);
151 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600152 spi_enable_bits &= 0xf;
153 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER,
154 res->base | spi_enable_bits);
Marc Jones24484842017-05-04 21:17:45 -0600155
156 pci_dev_set_resources(dev);
157}
158
159/**
160 * @brief Enable resources for children devices
161 *
162 * @param dev the device whose children's resources are to be enabled
163 *
164 */
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600165static void lpc_enable_childrens_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600166{
167 struct bus *link;
168 u32 reg, reg_x;
169 int var_num = 0;
170 u16 reg_var[3];
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600171 u16 reg_size[1] = {512};
Marc Jones24484842017-05-04 21:17:45 -0600172 u8 wiosize = pci_read_config8(dev, 0x74);
173
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600174 /* Be a bit relaxed, tolerate that LPC region might be bigger than
175 * resource we try to fit, do it like this for all regions < 16 bytes.
176 * If there is a resource > 16 bytes it must be 512 bytes to be able
177 * to allocate the fresh LPC window.
Marc Jones24484842017-05-04 21:17:45 -0600178 *
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600179 * AGESA likes to enable already one LPC region in wide port base
180 * 0x64-0x65, using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
181 * The code tries to check if resource can fit into this region.
Marc Jones24484842017-05-04 21:17:45 -0600182 */
183
184 reg = pci_read_config32(dev, 0x44);
185 reg_x = pci_read_config32(dev, 0x48);
186
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600187 /* check if ranges are free and don't use them if already taken */
Marc Jones24484842017-05-04 21:17:45 -0600188 if (reg_x & (1 << 2))
189 var_num = 1;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600190 /* just in case check if someone did not manually set other ranges */
Marc Jones24484842017-05-04 21:17:45 -0600191 if (reg_x & (1 << 24))
192 var_num = 2;
193
194 if (reg_x & (1 << 25))
195 var_num = 3;
196
197 /* check AGESA region size */
198 if (wiosize & (1 << 0))
199 reg_size[0] = 16;
200
201 reg_var[2] = pci_read_config16(dev, 0x90);
202 reg_var[1] = pci_read_config16(dev, 0x66);
203 reg_var[0] = pci_read_config16(dev, 0x64);
204
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600205 /* todo: clean up the code style here */
206 for (link = dev->link_list ; link ; link = link->next) {
Marc Jones24484842017-05-04 21:17:45 -0600207 device_t child;
208 for (child = link->children; child;
209 child = child->sibling) {
210 if (child->enabled
211 && (child->path.type == DEVICE_PATH_PNP)) {
212 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600213 for (res = child->resource_list ; res ; res = res->next) {
Marc Jones24484842017-05-04 21:17:45 -0600214 u32 base, end; /* don't need long long */
215 u32 rsize, set = 0, set_x = 0;
216 if (!(res->flags & IORESOURCE_IO))
217 continue;
218 base = res->base;
219 end = resource_end(res);
220 /* find a resource size */
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600221 printk(BIOS_DEBUG, "Southbridge LPC decode:%s, base=0x%08x, end=0x%08x\n",
Marc Jones24484842017-05-04 21:17:45 -0600222 dev_path(child), base, end);
223 switch (base) {
224 case 0x60: /* KB */
225 case 0x64: /* MS */
226 set |= (1 << 29);
227 rsize = 1;
228 break;
229 case 0x3f8: /* COM1 */
230 set |= (1 << 6);
231 rsize = 8;
232 break;
233 case 0x2f8: /* COM2 */
234 set |= (1 << 7);
235 rsize = 8;
236 break;
237 case 0x378: /* Parallel 1 */
238 set |= (1 << 0);
239 set |= (1 << 1); /* + 0x778 for ECP */
240 rsize = 8;
241 break;
242 case 0x3f0: /* FD0 */
243 set |= (1 << 26);
244 rsize = 8;
245 break;
246 case 0x220: /* 0x220 - 0x227 */
247 set |= (1 << 8);
248 rsize = 8;
249 break;
250 case 0x228: /* 0x228 - 0x22f */
251 set |= (1 << 9);
252 rsize = 8;
253 break;
254 case 0x238: /* 0x238 - 0x23f */
255 set |= (1 << 10);
256 rsize = 8;
257 break;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600258 case 0x300: /* 0x300 - 0x301 */
Marc Jones24484842017-05-04 21:17:45 -0600259 set |= (1 << 18);
260 rsize = 2;
261 break;
262 case 0x400:
263 set_x |= (1 << 16);
264 rsize = 0x40;
265 break;
266 case 0x480:
267 set_x |= (1 << 17);
268 rsize = 0x40;
269 break;
270 case 0x500:
271 set_x |= (1 << 18);
272 rsize = 0x40;
273 break;
274 case 0x580:
275 set_x |= (1 << 19);
276 rsize = 0x40;
277 break;
278 case 0x4700:
279 set_x |= (1 << 22);
280 rsize = 0xc;
281 break;
282 case 0xfd60:
283 set_x |= (1 << 23);
284 rsize = 16;
285 break;
286 default:
287 rsize = 0;
288 /* try AGESA allocated region in region 0 */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600289 if ((var_num > 0) && ((base >= reg_var[0]) &&
Marc Jones24484842017-05-04 21:17:45 -0600290 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
291 rsize = reg_size[0];
292 }
293 /* check if region found and matches the enable */
294 if (res->size <= rsize) {
295 reg |= set;
296 reg_x |= set_x;
297 /* check if we can fit resource in variable range */
298 } else if ((var_num < 3) &&
299 ((res->size <= 16) || (res->size == 512))) {
300 /* use variable ranges if pre-defined do not match */
301 switch (var_num) {
302 case 0:
303 reg_x |= (1 << 2);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600304 if (res->size <= 16)
Marc Jones24484842017-05-04 21:17:45 -0600305 wiosize |= (1 << 0);
Marc Jones24484842017-05-04 21:17:45 -0600306 break;
307 case 1:
308 reg_x |= (1 << 24);
309 if (res->size <= 16)
310 wiosize |= (1 << 2);
311 break;
312 case 2:
313 reg_x |= (1 << 25);
314 if (res->size <= 16)
315 wiosize |= (1 << 3);
316 break;
317 }
318 reg_var[var_num++] =
319 base & 0xffff;
320 } else {
321 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
322 dev_path(child), base, end);
323 }
324 }
325 }
326 }
327 }
328 pci_write_config32(dev, 0x44, reg);
329 pci_write_config32(dev, 0x48, reg_x);
330 /* Set WideIO for as many IOs found (fall through is on purpose) */
331 switch (var_num) {
332 case 3:
333 pci_write_config16(dev, 0x90, reg_var[2]);
334 /* fall through */
335 case 2:
336 pci_write_config16(dev, 0x66, reg_var[1]);
337 /* fall through */
338 case 1:
339 pci_write_config16(dev, 0x64, reg_var[0]);
340 break;
341 }
342 pci_write_config8(dev, 0x74, wiosize);
343}
344
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600345static void lpc_enable_resources(device_t dev)
Marc Jones24484842017-05-04 21:17:45 -0600346{
347 pci_dev_enable_resources(dev);
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600348 lpc_enable_childrens_resources(dev);
Marc Jones24484842017-05-04 21:17:45 -0600349}
350
351unsigned long acpi_fill_mcfg(unsigned long current)
352{
353 /* Just a dummy */
354 return current;
355}
356
357static struct pci_operations lops_pci = {
358 .set_subsystem = pci_dev_set_subsystem,
359};
360
361static struct device_operations lpc_ops = {
Marc Jonesdfeb1c42017-08-07 19:08:24 -0600362 .read_resources = lpc_read_resources,
363 .set_resources = lpc_set_resources,
364 .enable_resources = lpc_enable_resources,
Marc Jones257db582017-06-18 17:33:30 -0600365 .acpi_inject_dsdt_generator = southbridge_inject_dsdt,
366 .write_acpi_tables = southbridge_write_acpi_tables,
Marc Jones24484842017-05-04 21:17:45 -0600367 .init = lpc_init,
368 .scan_bus = scan_lpc_bus,
369 .ops_pci = &lops_pci,
370};
371
372static const unsigned short pci_device_ids[] = {
373 PCI_DEVICE_ID_AMD_SB900_LPC,
374 PCI_DEVICE_ID_AMD_CZ_LPC,
375 0
376};
377static const struct pci_driver lpc_driver __pci_driver = {
378 .ops = &lpc_ops,
379 .vendor = PCI_VENDOR_ID_AMD,
380 .devices = pci_device_ids,
381};