blob: 9b8753a3fec191988ad34937f51b0b73c0131ef2 [file] [log] [blame]
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03001/*
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.
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030015 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
20#include <device/pnp.h>
21#include <device/pci_ids.h>
22#include <device/pci_ops.h>
23#include <device/pci_def.h>
24#include <pc80/mc146818rtc.h>
25#include <pc80/isa-dma.h>
26#include <arch/io.h>
27#include <arch/ioapic.h>
Edward O'Callaghan4565aea2015-06-07 19:42:47 +100028#include <arch/acpi.h>
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060029#include <pc80/i8254.h>
30#include <pc80/i8259.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030031#include "hudson.h"
Philipp Deppenwiese30670122017-03-01 02:24:33 +010032#include "pci_devs.h"
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030033
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020034static void lpc_init(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030035{
36 u8 byte;
37 u32 dword;
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020038 struct device *sm_dev;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030039
40 /* Enable the LPC Controller */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +030041 sm_dev = pcidev_on_root(0x14, 0);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030042 dword = pci_read_config32(sm_dev, 0x64);
43 dword |= 1 << 20;
44 pci_write_config32(sm_dev, 0x64, dword);
45
46 /* Initialize isa dma */
47 isa_dma_init();
48
49 /* Enable DMA transaction on the LPC bus */
50 byte = pci_read_config8(dev, 0x40);
51 byte |= (1 << 2);
52 pci_write_config8(dev, 0x40, byte);
53
54 /* Disable the timeout mechanism on LPC */
55 byte = pci_read_config8(dev, 0x48);
56 byte &= ~(1 << 7);
57 pci_write_config8(dev, 0x48, byte);
58
59 /* Disable LPC MSI Capability */
60 byte = pci_read_config8(dev, 0x78);
61 byte &= ~(1 << 1);
62 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
63 on on LPC, it holds PCI grant, so no LPC slave cycle can
64 interrupt and visit LPC. */
65 pci_write_config8(dev, 0x78, byte);
66
Elyes HAOUAS1bcd7fc2016-07-28 21:20:04 +020067 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030068 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
69 byte = pci_read_config8(dev, 0xBB);
70 byte |= 1 << 0 | 1 << 3;
71 pci_write_config8(dev, 0xBB, byte);
72
Gabe Black03abaee212014-04-30 21:31:44 -070073 cmos_check_update_date();
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030074
75 /* Initialize the real time clock.
76 * The 0 argument tells cmos_init not to
77 * update CMOS unless it is invalid.
78 * 1 tells cmos_init to always initialize the CMOS.
79 */
Aaron Durbin9fde0d72017-09-15 11:01:17 -060080 cmos_init(0);
Dave Frodin8d9a1bd2015-03-31 16:10:58 -060081
82 /* Initialize i8259 pic */
83 setup_i8259 ();
84
85 /* Initialize i8254 timers */
86 setup_i8254 ();
Marc Jones3eec9dd2017-04-09 18:00:40 -060087
Paul Menzel4c402292017-04-14 17:23:49 +020088 /* Set up SERIRQ, enable continuous mode */
Marc Jones3eec9dd2017-04-09 18:00:40 -060089 byte = (BIT(4) | BIT(7));
90 if (!IS_ENABLED(CONFIG_SERIRQ_CONTINUOUS_MODE))
91 byte |= BIT(6);
92
93 pm_write8(PM_SERIRQ_CONF, byte);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030094}
95
Elyes HAOUASd9ef5462018-05-19 17:08:23 +020096static void hudson_lpc_read_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030097{
98 struct resource *res;
99
100 /* Get the normal pci resources of this device */
101 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
102
103 /* Add an extra subtractive resource for both memory and I/O. */
104 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
105 res->base = 0;
106 res->size = 0x1000;
107 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
108 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
109
110 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
111 res->base = 0xff800000;
112 res->size = 0x00800000; /* 8 MB for flash */
113 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
114 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
115
116 /* Add a memory resource for the SPI BAR. */
117 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
118
119 res = new_resource(dev, 3); /* IOAPIC */
120 res->base = IO_APIC_ADDR;
121 res->size = 0x00001000;
122 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
123
124 compact_resources(dev);
125}
126
127static void hudson_lpc_set_resources(struct device *dev)
128{
129 struct resource *res;
Marc Jones6fcaaef2017-04-20 16:48:42 -0600130 u32 spi_enable_bits;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300131
Marc Jones6fcaaef2017-04-20 16:48:42 -0600132 /* Special case. The SpiRomEnable and other enables should STAY set. */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300133 res = find_resource(dev, 2);
Marc Jones6fcaaef2017-04-20 16:48:42 -0600134 spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
135 spi_enable_bits &= 0xF;
136 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300137
138 pci_dev_set_resources(dev);
139}
140
141/**
142 * @brief Enable resources for children devices
143 *
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000144 * @param dev the device whose children's resources are to be enabled
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300145 *
146 */
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200147static void hudson_lpc_enable_childrens_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300148{
149 struct bus *link;
150 u32 reg, reg_x;
151 int var_num = 0;
152 u16 reg_var[3];
153 u16 reg_size[1] = {512};
154 u8 wiosize = pci_read_config8(dev, 0x74);
155
156 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
157 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
158 * it must be 512 bytes to be able to allocate the fresh LPC window.
159 *
160 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
161 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
162 * The code tries to check if resource can fit into this region
163 */
164
165 reg = pci_read_config32(dev, 0x44);
166 reg_x = pci_read_config32(dev, 0x48);
167
168 /* check if ranges are free and not use them if entry is just already taken */
169 if (reg_x & (1 << 2))
170 var_num = 1;
171 /* just in case check if someone did not manually set other ranges too */
172 if (reg_x & (1 << 24))
173 var_num = 2;
174
175 if (reg_x & (1 << 25))
176 var_num = 3;
177
178 /* check AGESA region size */
179 if (wiosize & (1 << 0))
180 reg_size[0] = 16;
181
182 reg_var[2] = pci_read_config16(dev, 0x90);
183 reg_var[1] = pci_read_config16(dev, 0x66);
184 reg_var[0] = pci_read_config16(dev, 0x64);
185
186 for (link = dev->link_list; link; link = link->next) {
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200187 struct device *child;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300188 for (child = link->children; child;
189 child = child->sibling) {
190 if (child->enabled
191 && (child->path.type == DEVICE_PATH_PNP)) {
192 struct resource *res;
193 for (res = child->resource_list; res; res = res->next) {
194 u32 base, end; /* don't need long long */
195 u32 rsize, set = 0, set_x = 0;
196 if (!(res->flags & IORESOURCE_IO))
197 continue;
198 base = res->base;
199 end = resource_end(res);
200 /* find a resource size */
201 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
202 dev_path(child), base, end);
203 switch (base) {
204 case 0x60: /* KB */
205 case 0x64: /* MS */
206 set |= (1 << 29);
207 rsize = 1;
208 break;
209 case 0x3f8: /* COM1 */
210 set |= (1 << 6);
211 rsize = 8;
212 break;
213 case 0x2f8: /* COM2 */
214 set |= (1 << 7);
215 rsize = 8;
216 break;
217 case 0x378: /* Parallel 1 */
218 set |= (1 << 0);
219 set |= (1 << 1); /* + 0x778 for ECP */
220 rsize = 8;
221 break;
222 case 0x3f0: /* FD0 */
223 set |= (1 << 26);
224 rsize = 8;
225 break;
226 case 0x220: /* 0x220 - 0x227 */
227 set |= (1 << 8);
228 rsize = 8;
229 break;
230 case 0x228: /* 0x228 - 0x22f */
231 set |= (1 << 9);
232 rsize = 8;
233 break;
234 case 0x238: /* 0x238 - 0x23f */
235 set |= (1 << 10);
236 rsize = 8;
237 break;
238 case 0x300: /* 0x300 -0x301 */
239 set |= (1 << 18);
240 rsize = 2;
241 break;
242 case 0x400:
243 set_x |= (1 << 16);
244 rsize = 0x40;
245 break;
246 case 0x480:
247 set_x |= (1 << 17);
248 rsize = 0x40;
249 break;
250 case 0x500:
251 set_x |= (1 << 18);
252 rsize = 0x40;
253 break;
254 case 0x580:
255 set_x |= (1 << 19);
256 rsize = 0x40;
257 break;
258 case 0x4700:
259 set_x |= (1 << 22);
260 rsize = 0xc;
261 break;
262 case 0xfd60:
263 set_x |= (1 << 23);
264 rsize = 16;
265 break;
266 default:
267 rsize = 0;
268 /* try AGESA allocated region in region 0 */
269 if ((var_num > 0) && ((base >=reg_var[0]) &&
270 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
271 rsize = reg_size[0];
272 }
273 /* check if region found and matches the enable */
274 if (res->size <= rsize) {
275 reg |= set;
276 reg_x |= set_x;
277 /* check if we can fit resource in variable range */
278 } else if ((var_num < 3) &&
279 ((res->size <= 16) || (res->size == 512))) {
280 /* use variable ranges if pre-defined do not match */
281 switch (var_num) {
282 case 0:
283 reg_x |= (1 << 2);
284 if (res->size <= 16) {
285 wiosize |= (1 << 0);
286 }
287 break;
288 case 1:
289 reg_x |= (1 << 24);
290 if (res->size <= 16)
291 wiosize |= (1 << 2);
292 break;
293 case 2:
294 reg_x |= (1 << 25);
295 if (res->size <= 16)
296 wiosize |= (1 << 3);
297 break;
298 }
299 reg_var[var_num++] =
300 base & 0xffff;
301 } else {
302 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
303 dev_path(child), base, end);
304 }
305 }
306 }
307 }
308 }
309 pci_write_config32(dev, 0x44, reg);
310 pci_write_config32(dev, 0x48, reg_x);
311 /* Set WideIO for as many IOs found (fall through is on purpose) */
312 switch (var_num) {
313 case 3:
314 pci_write_config16(dev, 0x90, reg_var[2]);
315 /* fall through */
316 case 2:
317 pci_write_config16(dev, 0x66, reg_var[1]);
318 /* fall through */
319 case 1:
320 pci_write_config16(dev, 0x64, reg_var[0]);
321 break;
322 }
323 pci_write_config8(dev, 0x74, wiosize);
324}
325
Elyes HAOUASd9ef5462018-05-19 17:08:23 +0200326static void hudson_lpc_enable_resources(struct device *dev)
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300327{
328 pci_dev_enable_resources(dev);
329 hudson_lpc_enable_childrens_resources(dev);
330}
331
Edward O'Callaghan4565aea2015-06-07 19:42:47 +1000332unsigned long acpi_fill_mcfg(unsigned long current)
333{
334 /* Just a dummy */
335 return current;
336}
337
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600338static const char *lpc_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100339{
340 if (dev->path.type != DEVICE_PATH_PCI)
341 return NULL;
342
343 if (dev->path.pci.devfn == LPC_DEVFN)
344 return "LIBR";
345
346 return NULL;
347}
348
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300349static struct pci_operations lops_pci = {
350 .set_subsystem = pci_dev_set_subsystem,
351};
352
353static struct device_operations lpc_ops = {
354 .read_resources = hudson_lpc_read_resources,
355 .set_resources = hudson_lpc_set_resources,
356 .enable_resources = hudson_lpc_enable_resources,
Vladimir Serbinenko83f81ca2014-11-09 13:30:50 +0100357#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Dave Frodin5c015f02015-01-27 07:19:04 -0700358 .write_acpi_tables = acpi_write_hpet,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200359#endif
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300360 .init = lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200361 .scan_bus = scan_lpc_bus,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300362 .ops_pci = &lops_pci,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100363 .acpi_name = lpc_acpi_name,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300364};
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800365
366static const unsigned short pci_device_ids[] = {
Kyösti Mälkki9d9a5522016-11-19 22:14:59 +0200367 PCI_DEVICE_ID_AMD_SB900_LPC,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800368 PCI_DEVICE_ID_AMD_CZ_LPC,
369 0
370};
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300371static const struct pci_driver lpc_driver __pci_driver = {
372 .ops = &lpc_ops,
373 .vendor = PCI_VENDOR_ID_AMD,
WANG Siyuanf2dfef02015-05-20 14:41:01 +0800374 .devices = pci_device_ids,
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300375};