blob: a0dc0727d9b3b5ca7f45f8dc4a600653236cc86f [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.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
zbao246e84b2012-07-13 18:47:03 +080018 */
19
20#include <console/console.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pnp.h>
24#include <device/pci_ids.h>
25#include <device/pci_ops.h>
Martin Roth3aef7b42012-12-05 15:50:32 -070026#include <device/pci_def.h>
zbao246e84b2012-07-13 18:47:03 +080027#include <pc80/mc146818rtc.h>
28#include <pc80/isa-dma.h>
zbao246e84b2012-07-13 18:47:03 +080029#include <arch/io.h>
30#include "hudson.h"
31
32static void lpc_init(device_t dev)
33{
34 u8 byte;
35 u32 dword;
36 device_t sm_dev;
37
38 /* Enable the LPC Controller */
39 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
40 dword = pci_read_config32(sm_dev, 0x64);
41 dword |= 1 << 20;
42 pci_write_config32(sm_dev, 0x64, dword);
43
44 /* Initialize isa dma */
45 isa_dma_init();
46
47 /* Enable DMA transaction on the LPC bus */
48 byte = pci_read_config8(dev, 0x40);
49 byte |= (1 << 2);
50 pci_write_config8(dev, 0x40, byte);
51
52 /* Disable the timeout mechanism on LPC */
53 byte = pci_read_config8(dev, 0x48);
54 byte &= ~(1 << 7);
55 pci_write_config8(dev, 0x48, byte);
56
57 /* Disable LPC MSI Capability */
58 byte = pci_read_config8(dev, 0x78);
59 byte &= ~(1 << 1);
60 byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going
61 on on LPC, it holds PCI grant, so no LPC slave cycle can
62 interrupt and visit LPC. */
63 pci_write_config8(dev, 0x78, byte);
64
65 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI rom */
66 /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */
67 byte = pci_read_config8(dev, 0xBB);
68 byte |= 1 << 0 | 1 << 3;
69 pci_write_config8(dev, 0xBB, byte);
zbaoef180e22012-08-02 19:03:44 +080070
71 rtc_check_update_cmos_date(RTC_HAS_ALTCENTURY);
Mike Loptiena96d24d2013-02-25 10:41:28 -070072
73 /* Initialize the real time clock.
74 * The 0 argument tells rtc_init not to
75 * update CMOS unless it is invalid.
76 * 1 tells rtc_init to always initialize the CMOS.
77 */
78 rtc_init(0);
zbao246e84b2012-07-13 18:47:03 +080079}
80
81static void hudson_lpc_read_resources(device_t dev)
82{
83 struct resource *res;
84
85 /* Get the normal pci resources of this device */
86 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
87
88 pci_get_resource(dev, 0xA0); /* SPI ROM base address */
89
90 /* Add an extra subtractive resource for both memory and I/O. */
91 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
92 res->base = 0;
93 res->size = 0x1000;
94 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
95 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
96
97 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
98 res->base = 0xff800000;
99 res->size = 0x00800000; /* 8 MB for flash */
100 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
101 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
102
103 //res = new_resource(dev, 3); /* IOAPIC */
104 //res->base = 0xfec00000;
105 //res->size = 0x00001000;
106 //res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
107
108 compact_resources(dev);
109}
110
111static void hudson_lpc_set_resources(struct device *dev)
112{
113 struct resource *res;
114
Martin Roth3aef7b42012-12-05 15:50:32 -0700115 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
116 res = find_resource(dev, SPIROM_BASE_ADDRESS_REGISTER);
117 res->base |= PCI_COMMAND_MEMORY;
118
zbao246e84b2012-07-13 18:47:03 +0800119 pci_dev_set_resources(dev);
120
zbao246e84b2012-07-13 18:47:03 +0800121
122}
123
124/**
125 * @brief Enable resources for children devices
126 *
127 * @param dev the device whos children's resources are to be enabled
128 *
129 */
130static void hudson_lpc_enable_childrens_resources(device_t dev)
131{
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100132 struct bus *link;
133 u32 reg, reg_x;
134 int var_num = 0;
135 u16 reg_var[3];
136 u16 reg_size[1] = {512};
137 u8 wiosize = pci_read_config8(dev, 0x74);
zbao246e84b2012-07-13 18:47:03 +0800138
Rudolf Marek6181e3d2013-12-07 22:29:36 +0100139 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
140 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
141 * it must be 512 bytes to be able to allocate the fresh LPC window.
142 *
143 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
144 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
145 * The code tries to check if resource can fit into this region
146 */
147
148 reg = pci_read_config32(dev, 0x44);
149 reg_x = pci_read_config32(dev, 0x48);
150
151 /* check if ranges are free and not use them if entry is just already taken */
152 if (reg_x & (1 << 2))
153 var_num = 1;
154 /* just in case check if someone did not manually set other ranges too */
155 if (reg_x & (1 << 24))
156 var_num = 2;
157
158 if (reg_x & (1 << 25))
159 var_num = 3;
160
161 /* check AGESA region size */
162 if (wiosize & (1 << 0))
163 reg_size[0] = 16;
164
165 reg_var[2] = pci_read_config16(dev, 0x90);
166 reg_var[1] = pci_read_config16(dev, 0x66);
167 reg_var[0] = pci_read_config16(dev, 0x64);
168
169 for (link = dev->link_list; link; link = link->next) {
170 device_t child;
171 for (child = link->children; child;
172 child = child->sibling) {
173 if (child->enabled
174 && (child->path.type == DEVICE_PATH_PNP)) {
175 struct resource *res;
176 for (res = child->resource_list; res; res = res->next) {
177 u32 base, end; /* don't need long long */
178 u32 rsize, set = 0, set_x = 0;
179 if (!(res->flags & IORESOURCE_IO))
180 continue;
181 base = res->base;
182 end = resource_end(res);
183 /* find a resource size */
184 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
185 dev_path(child), base, end);
186 switch (base) {
187 case 0x60: /* KB */
188 case 0x64: /* MS */
189 set |= (1 << 29);
190 rsize = 1;
191 break;
192 case 0x3f8: /* COM1 */
193 set |= (1 << 6);
194 rsize = 8;
195 break;
196 case 0x2f8: /* COM2 */
197 set |= (1 << 7);
198 rsize = 8;
199 break;
200 case 0x378: /* Parallel 1 */
201 set |= (1 << 0);
202 set |= (1 << 1); /* + 0x778 for ECP */
203 rsize = 8;
204 break;
205 case 0x3f0: /* FD0 */
206 set |= (1 << 26);
207 rsize = 8;
208 break;
209 case 0x220: /* 0x220 - 0x227 */
210 set |= (1 << 8);
211 rsize = 8;
212 break;
213 case 0x228: /* 0x228 - 0x22f */
214 set |= (1 << 9);
215 rsize = 8;
216 break;
217 case 0x238: /* 0x238 - 0x23f */
218 set |= (1 << 10);
219 rsize = 8;
220 break;
221 case 0x300: /* 0x300 -0x301 */
222 set |= (1 << 18);
223 rsize = 2;
224 break;
225 case 0x400:
226 set_x |= (1 << 16);
227 rsize = 0x40;
228 break;
229 case 0x480:
230 set_x |= (1 << 17);
231 rsize = 0x40;
232 case 0x500:
233 set_x |= (1 << 18);
234 rsize = 0x40;
235 break;
236 case 0x580:
237 set_x |= (1 << 19);
238 rsize = 0x40;
239 break;
240 case 0x4700:
241 set_x |= (1 << 22);
242 rsize = 0xc;
243 break;
244 case 0xfd60:
245 set_x |= (1 << 23);
246 rsize = 16;
247 break;
248 default:
249 rsize = 0;
250 /* try AGESA allocated region in region 0 */
251 if ((var_num > 0) && ((base >=reg_var[0]) &&
252 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
253 rsize = reg_size[0];
254 }
255 /* check if region found and matches the enable */
256 if (res->size <= rsize) {
257 reg |= set;
258 reg_x |= set_x;
259 /* check if we can fit resource in variable range */
260 } else if ((var_num < 3) &&
261 ((res->size <= 16) || (res->size == 512))) {
262 /* use variable ranges if pre-defined do not match */
263 switch (var_num) {
264 case 0:
265 reg_x |= (1 << 2);
266 if (res->size <= 16) {
267 wiosize |= (1 << 0);
268 }
269 break;
270 case 1:
271 reg_x |= (1 << 24);
272 if (res->size <= 16)
273 wiosize |= (1 << 2);
274 break;
275 case 2:
276 reg_x |= (1 << 25);
277 if (res->size <= 16)
278 wiosize |= (1 << 3);
279 break;
280 }
281 reg_var[var_num++] =
282 base & 0xffff;
283 } else {
284 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
285 dev_path(child), base, end);
286 }
287 }
288 }
289 }
290 }
291 pci_write_config32(dev, 0x44, reg);
292 pci_write_config32(dev, 0x48, reg_x);
293 /* Set WideIO for as many IOs found (fall through is on purpose) */
294 switch (var_num) {
295 case 3:
296 pci_write_config16(dev, 0x90, reg_var[2]);
297 /* fall through */
298 case 2:
299 pci_write_config16(dev, 0x66, reg_var[1]);
300 /* fall through */
301 case 1:
302 pci_write_config16(dev, 0x64, reg_var[0]);
303 break;
304 }
305 pci_write_config8(dev, 0x74, wiosize);
zbao246e84b2012-07-13 18:47:03 +0800306}
307
308static void hudson_lpc_enable_resources(device_t dev)
309{
310 pci_dev_enable_resources(dev);
311 hudson_lpc_enable_childrens_resources(dev);
312}
313
314static struct pci_operations lops_pci = {
315 .set_subsystem = pci_dev_set_subsystem,
316};
317
318static struct device_operations lpc_ops = {
319 .read_resources = hudson_lpc_read_resources,
320 .set_resources = hudson_lpc_set_resources,
321 .enable_resources = hudson_lpc_enable_resources,
322 .init = lpc_init,
323 .scan_bus = scan_static_bus,
324 .ops_pci = &lops_pci,
325};
326static const struct pci_driver lpc_driver __pci_driver = {
327 .ops = &lpc_ops,
328 .vendor = PCI_VENDOR_ID_AMD,
329 .device = PCI_DEVICE_ID_ATI_SB900_LPC,
330};