blob: b784fc40dc18860c52356d8b01ca3b73d9a8bb6b [file] [log] [blame]
zbao246e84b2012-07-13 18:47:03 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2010 Advanced Micro Devices, Inc.
Dave Frodin8ef20cf2014-06-05 14:21:11 -06005 * Copyright (C) 2014 Sage Electronic Engineering, LLC
zbao246e84b2012-07-13 18:47:03 +08006 *
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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
zbao246e84b2012-07-13 18:47:03 +080019 */
20
21#include <console/console.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pnp.h>
25#include <device/pci_ids.h>
26#include <device/pci_ops.h>
Martin Roth3aef7b42012-12-05 15:50:32 -070027#include <device/pci_def.h>
zbao246e84b2012-07-13 18:47:03 +080028#include <pc80/mc146818rtc.h>
29#include <pc80/isa-dma.h>
zbao246e84b2012-07-13 18:47:03 +080030#include <arch/io.h>
Dave Frodin8ef20cf2014-06-05 14:21:11 -060031#include <arch/ioapic.h>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020032#include <arch/acpi.h>
zbao246e84b2012-07-13 18:47:03 +080033#include "hudson.h"
34
35static void lpc_init(device_t dev)
36{
37 u8 byte;
38 u32 dword;
39 device_t sm_dev;
40
41 /* Enable the LPC Controller */
42 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
43 dword = pci_read_config32(sm_dev, 0x64);
44 dword |= 1 << 20;
45 pci_write_config32(sm_dev, 0x64, dword);
46
47 /* Initialize isa dma */
48 isa_dma_init();
49
50 /* Enable DMA transaction on the LPC bus */
51 byte = pci_read_config8(dev, 0x40);
52 byte |= (1 << 2);
53 pci_write_config8(dev, 0x40, byte);
54
55 /* Disable the timeout mechanism on LPC */
56 byte = pci_read_config8(dev, 0x48);
57 byte &= ~(1 << 7);
58 pci_write_config8(dev, 0x48, byte);
59
60 /* Disable LPC MSI Capability */
61 byte = pci_read_config8(dev, 0x78);
62 byte &= ~(1 << 1);
63 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
64 on on LPC, it holds PCI grant, so no LPC slave cycle can
65 interrupt and visit LPC. */
66 pci_write_config8(dev, 0x78, byte);
67
68 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI rom */
69 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
70 byte = pci_read_config8(dev, 0xBB);
71 byte |= 1 << 0 | 1 << 3;
72 pci_write_config8(dev, 0xBB, byte);
zbaoef180e22012-08-02 19:03:44 +080073
Gabe Black03abaee212014-04-30 21:31:44 -070074 cmos_check_update_date();
Mike Loptiena96d24d2013-02-25 10:41:28 -070075
76 /* Initialize the real time clock.
Gabe Blackb3f08c62014-04-30 17:12:25 -070077 * The 0 argument tells cmos_init not to
Mike Loptiena96d24d2013-02-25 10:41:28 -070078 * update CMOS unless it is invalid.
Gabe Blackb3f08c62014-04-30 17:12:25 -070079 * 1 tells cmos_init to always initialize the CMOS.
Mike Loptiena96d24d2013-02-25 10:41:28 -070080 */
Gabe Blackb3f08c62014-04-30 17:12:25 -070081 cmos_init(0);
zbao246e84b2012-07-13 18:47:03 +080082}
83
84static void hudson_lpc_read_resources(device_t dev)
85{
86 struct resource *res;
87
88 /* Get the normal pci resources of this device */
89 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
90
zbao246e84b2012-07-13 18:47:03 +080091 /* Add an extra subtractive resource for both memory and I/O. */
92 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
93 res->base = 0;
94 res->size = 0x1000;
95 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
96 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
97
98 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
99 res->base = 0xff800000;
100 res->size = 0x00800000; /* 8 MB for flash */
101 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
102 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
103
Dave Frodinac1b8752014-06-05 14:30:22 -0600104 /* Add a memory resource for the SPI BAR. */
105 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
106
Dave Frodin8ef20cf2014-06-05 14:21:11 -0600107 res = new_resource(dev, 3); /* IOAPIC */
108 res->base = IO_APIC_ADDR;
109 res->size = 0x00001000;
110 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
zbao246e84b2012-07-13 18:47:03 +0800111
112 compact_resources(dev);
113}
114
115static void hudson_lpc_set_resources(struct device *dev)
116{
117 struct resource *res;
118
Martin Roth3aef7b42012-12-05 15:50:32 -0700119 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
Dave Frodinac1b8752014-06-05 14:30:22 -0600120 res = find_resource(dev, 2);
121 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
Martin Roth3aef7b42012-12-05 15:50:32 -0700122
zbao246e84b2012-07-13 18:47:03 +0800123 pci_dev_set_resources(dev);
zbao246e84b2012-07-13 18:47:03 +0800124}
125
126/**
127 * @brief Enable resources for children devices
128 *
Martin Roth3c3a50c2014-12-16 20:50:26 -0700129 * @param dev the device whose children's resources are to be enabled
zbao246e84b2012-07-13 18:47:03 +0800130 *
131 */
132static void hudson_lpc_enable_childrens_resources(device_t dev)
133{
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100134 struct bus *link;
135 u32 reg, reg_x;
136 int var_num = 0;
137 u16 reg_var[3];
138 u16 reg_size[1] = {512};
139 u8 wiosize = pci_read_config8(dev, 0x74);
zbao246e84b2012-07-13 18:47:03 +0800140
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100141 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
142 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
143 * it must be 512 bytes to be able to allocate the fresh LPC window.
144 *
145 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
146 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
147 * The code tries to check if resource can fit into this region
148 */
149
150 reg = pci_read_config32(dev, 0x44);
151 reg_x = pci_read_config32(dev, 0x48);
152
153 /* check if ranges are free and not use them if entry is just already taken */
154 if (reg_x & (1 << 2))
155 var_num = 1;
156 /* just in case check if someone did not manually set other ranges too */
157 if (reg_x & (1 << 24))
158 var_num = 2;
159
160 if (reg_x & (1 << 25))
161 var_num = 3;
162
163 /* check AGESA region size */
164 if (wiosize & (1 << 0))
165 reg_size[0] = 16;
166
167 reg_var[2] = pci_read_config16(dev, 0x90);
168 reg_var[1] = pci_read_config16(dev, 0x66);
169 reg_var[0] = pci_read_config16(dev, 0x64);
170
171 for (link = dev->link_list; link; link = link->next) {
172 device_t child;
173 for (child = link->children; child;
174 child = child->sibling) {
175 if (child->enabled
176 && (child->path.type == DEVICE_PATH_PNP)) {
177 struct resource *res;
178 for (res = child->resource_list; res; res = res->next) {
179 u32 base, end; /* don't need long long */
180 u32 rsize, set = 0, set_x = 0;
181 if (!(res->flags & IORESOURCE_IO))
182 continue;
183 base = res->base;
184 end = resource_end(res);
185 /* find a resource size */
186 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
187 dev_path(child), base, end);
188 switch (base) {
189 case 0x60: /* KB */
190 case 0x64: /* MS */
191 set |= (1 << 29);
192 rsize = 1;
193 break;
194 case 0x3f8: /* COM1 */
195 set |= (1 << 6);
196 rsize = 8;
197 break;
198 case 0x2f8: /* COM2 */
199 set |= (1 << 7);
200 rsize = 8;
201 break;
202 case 0x378: /* Parallel 1 */
203 set |= (1 << 0);
204 set |= (1 << 1); /* + 0x778 for ECP */
205 rsize = 8;
206 break;
207 case 0x3f0: /* FD0 */
208 set |= (1 << 26);
209 rsize = 8;
210 break;
211 case 0x220: /* 0x220 - 0x227 */
212 set |= (1 << 8);
213 rsize = 8;
214 break;
215 case 0x228: /* 0x228 - 0x22f */
216 set |= (1 << 9);
217 rsize = 8;
218 break;
219 case 0x238: /* 0x238 - 0x23f */
220 set |= (1 << 10);
221 rsize = 8;
222 break;
223 case 0x300: /* 0x300 -0x301 */
224 set |= (1 << 18);
225 rsize = 2;
226 break;
227 case 0x400:
228 set_x |= (1 << 16);
229 rsize = 0x40;
230 break;
231 case 0x480:
232 set_x |= (1 << 17);
233 rsize = 0x40;
Daniele Forsib532b122014-07-26 10:32:34 +0200234 break;
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100235 case 0x500:
236 set_x |= (1 << 18);
237 rsize = 0x40;
238 break;
239 case 0x580:
240 set_x |= (1 << 19);
241 rsize = 0x40;
242 break;
243 case 0x4700:
244 set_x |= (1 << 22);
245 rsize = 0xc;
246 break;
247 case 0xfd60:
248 set_x |= (1 << 23);
249 rsize = 16;
250 break;
251 default:
252 rsize = 0;
253 /* try AGESA allocated region in region 0 */
254 if ((var_num > 0) && ((base >=reg_var[0]) &&
255 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
256 rsize = reg_size[0];
257 }
258 /* check if region found and matches the enable */
259 if (res->size <= rsize) {
260 reg |= set;
261 reg_x |= set_x;
262 /* check if we can fit resource in variable range */
263 } else if ((var_num < 3) &&
264 ((res->size <= 16) || (res->size == 512))) {
265 /* use variable ranges if pre-defined do not match */
266 switch (var_num) {
267 case 0:
268 reg_x |= (1 << 2);
269 if (res->size <= 16) {
270 wiosize |= (1 << 0);
271 }
272 break;
273 case 1:
274 reg_x |= (1 << 24);
275 if (res->size <= 16)
276 wiosize |= (1 << 2);
277 break;
278 case 2:
279 reg_x |= (1 << 25);
280 if (res->size <= 16)
281 wiosize |= (1 << 3);
282 break;
283 }
284 reg_var[var_num++] =
285 base & 0xffff;
286 } else {
287 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
288 dev_path(child), base, end);
289 }
290 }
291 }
292 }
293 }
294 pci_write_config32(dev, 0x44, reg);
295 pci_write_config32(dev, 0x48, reg_x);
296 /* Set WideIO for as many IOs found (fall through is on purpose) */
297 switch (var_num) {
298 case 3:
299 pci_write_config16(dev, 0x90, reg_var[2]);
300 /* fall through */
301 case 2:
302 pci_write_config16(dev, 0x66, reg_var[1]);
303 /* fall through */
304 case 1:
305 pci_write_config16(dev, 0x64, reg_var[0]);
306 break;
307 }
308 pci_write_config8(dev, 0x74, wiosize);
zbao246e84b2012-07-13 18:47:03 +0800309}
310
311static void hudson_lpc_enable_resources(device_t dev)
312{
313 pci_dev_enable_resources(dev);
314 hudson_lpc_enable_childrens_resources(dev);
315}
316
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200317unsigned long acpi_fill_mcfg(unsigned long current)
318{
319 /* Just a dummy */
320 return current;
321}
322
323
zbao246e84b2012-07-13 18:47:03 +0800324static struct pci_operations lops_pci = {
325 .set_subsystem = pci_dev_set_subsystem,
326};
327
328static struct device_operations lpc_ops = {
329 .read_resources = hudson_lpc_read_resources,
330 .set_resources = hudson_lpc_set_resources,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200331#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) && IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES)
332 .write_acpi_tables = acpi_write_hpet,
333#endif
zbao246e84b2012-07-13 18:47:03 +0800334 .enable_resources = hudson_lpc_enable_resources,
335 .init = lpc_init,
336 .scan_bus = scan_static_bus,
337 .ops_pci = &lops_pci,
338};
339static const struct pci_driver lpc_driver __pci_driver = {
340 .ops = &lpc_ops,
341 .vendor = PCI_VENDOR_ID_AMD,
342 .device = PCI_DEVICE_ID_ATI_SB900_LPC,
343};