blob: 1f60bc4ed913d5bfee17623252c69bfbeeb40d81 [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.
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
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
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>
27#include <device/pci_def.h>
28#include <pc80/mc146818rtc.h>
29#include <pc80/isa-dma.h>
30#include <arch/io.h>
31#include <arch/ioapic.h>
32#include "hudson.h"
33
34static void lpc_init(device_t dev)
35{
36 u8 byte;
37 u32 dword;
38 device_t sm_dev;
39
40 /* Enable the LPC Controller */
41 sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
42 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
67 /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI rom */
68 /* 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
73 cmos_check_update_date(RTC_HAS_ALTCENTURY);
74
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 */
80 cmos_init(0);
81}
82
83static void hudson_lpc_read_resources(device_t dev)
84{
85 struct resource *res;
86
87 /* Get the normal pci resources of this device */
88 pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */
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 /* Add a memory resource for the SPI BAR. */
104 fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE);
105
106 res = new_resource(dev, 3); /* IOAPIC */
107 res->base = IO_APIC_ADDR;
108 res->size = 0x00001000;
109 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
110
111 compact_resources(dev);
112}
113
114static void hudson_lpc_set_resources(struct device *dev)
115{
116 struct resource *res;
117
118 /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */
119 res = find_resource(dev, 2);
120 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE);
121
122 pci_dev_set_resources(dev);
123}
124
125/**
126 * @brief Enable resources for children devices
127 *
128 * @param dev the device whos children's resources are to be enabled
129 *
130 */
131static void hudson_lpc_enable_childrens_resources(device_t dev)
132{
133 struct bus *link;
134 u32 reg, reg_x;
135 int var_num = 0;
136 u16 reg_var[3];
137 u16 reg_size[1] = {512};
138 u8 wiosize = pci_read_config8(dev, 0x74);
139
140 /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit,
141 * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes
142 * it must be 512 bytes to be able to allocate the fresh LPC window.
143 *
144 * AGESA likes to enable already one LPC region in wide port base 0x64-0x65,
145 * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size
146 * The code tries to check if resource can fit into this region
147 */
148
149 reg = pci_read_config32(dev, 0x44);
150 reg_x = pci_read_config32(dev, 0x48);
151
152 /* check if ranges are free and not use them if entry is just already taken */
153 if (reg_x & (1 << 2))
154 var_num = 1;
155 /* just in case check if someone did not manually set other ranges too */
156 if (reg_x & (1 << 24))
157 var_num = 2;
158
159 if (reg_x & (1 << 25))
160 var_num = 3;
161
162 /* check AGESA region size */
163 if (wiosize & (1 << 0))
164 reg_size[0] = 16;
165
166 reg_var[2] = pci_read_config16(dev, 0x90);
167 reg_var[1] = pci_read_config16(dev, 0x66);
168 reg_var[0] = pci_read_config16(dev, 0x64);
169
170 for (link = dev->link_list; link; link = link->next) {
171 device_t child;
172 for (child = link->children; child;
173 child = child->sibling) {
174 if (child->enabled
175 && (child->path.type == DEVICE_PATH_PNP)) {
176 struct resource *res;
177 for (res = child->resource_list; res; res = res->next) {
178 u32 base, end; /* don't need long long */
179 u32 rsize, set = 0, set_x = 0;
180 if (!(res->flags & IORESOURCE_IO))
181 continue;
182 base = res->base;
183 end = resource_end(res);
184 /* find a resource size */
185 printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n",
186 dev_path(child), base, end);
187 switch (base) {
188 case 0x60: /* KB */
189 case 0x64: /* MS */
190 set |= (1 << 29);
191 rsize = 1;
192 break;
193 case 0x3f8: /* COM1 */
194 set |= (1 << 6);
195 rsize = 8;
196 break;
197 case 0x2f8: /* COM2 */
198 set |= (1 << 7);
199 rsize = 8;
200 break;
201 case 0x378: /* Parallel 1 */
202 set |= (1 << 0);
203 set |= (1 << 1); /* + 0x778 for ECP */
204 rsize = 8;
205 break;
206 case 0x3f0: /* FD0 */
207 set |= (1 << 26);
208 rsize = 8;
209 break;
210 case 0x220: /* 0x220 - 0x227 */
211 set |= (1 << 8);
212 rsize = 8;
213 break;
214 case 0x228: /* 0x228 - 0x22f */
215 set |= (1 << 9);
216 rsize = 8;
217 break;
218 case 0x238: /* 0x238 - 0x23f */
219 set |= (1 << 10);
220 rsize = 8;
221 break;
222 case 0x300: /* 0x300 -0x301 */
223 set |= (1 << 18);
224 rsize = 2;
225 break;
226 case 0x400:
227 set_x |= (1 << 16);
228 rsize = 0x40;
229 break;
230 case 0x480:
231 set_x |= (1 << 17);
232 rsize = 0x40;
233 break;
234 case 0x500:
235 set_x |= (1 << 18);
236 rsize = 0x40;
237 break;
238 case 0x580:
239 set_x |= (1 << 19);
240 rsize = 0x40;
241 break;
242 case 0x4700:
243 set_x |= (1 << 22);
244 rsize = 0xc;
245 break;
246 case 0xfd60:
247 set_x |= (1 << 23);
248 rsize = 16;
249 break;
250 default:
251 rsize = 0;
252 /* try AGESA allocated region in region 0 */
253 if ((var_num > 0) && ((base >=reg_var[0]) &&
254 ((base + res->size) <= (reg_var[0] + reg_size[0]))))
255 rsize = reg_size[0];
256 }
257 /* check if region found and matches the enable */
258 if (res->size <= rsize) {
259 reg |= set;
260 reg_x |= set_x;
261 /* check if we can fit resource in variable range */
262 } else if ((var_num < 3) &&
263 ((res->size <= 16) || (res->size == 512))) {
264 /* use variable ranges if pre-defined do not match */
265 switch (var_num) {
266 case 0:
267 reg_x |= (1 << 2);
268 if (res->size <= 16) {
269 wiosize |= (1 << 0);
270 }
271 break;
272 case 1:
273 reg_x |= (1 << 24);
274 if (res->size <= 16)
275 wiosize |= (1 << 2);
276 break;
277 case 2:
278 reg_x |= (1 << 25);
279 if (res->size <= 16)
280 wiosize |= (1 << 3);
281 break;
282 }
283 reg_var[var_num++] =
284 base & 0xffff;
285 } else {
286 printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n",
287 dev_path(child), base, end);
288 }
289 }
290 }
291 }
292 }
293 pci_write_config32(dev, 0x44, reg);
294 pci_write_config32(dev, 0x48, reg_x);
295 /* Set WideIO for as many IOs found (fall through is on purpose) */
296 switch (var_num) {
297 case 3:
298 pci_write_config16(dev, 0x90, reg_var[2]);
299 /* fall through */
300 case 2:
301 pci_write_config16(dev, 0x66, reg_var[1]);
302 /* fall through */
303 case 1:
304 pci_write_config16(dev, 0x64, reg_var[0]);
305 break;
306 }
307 pci_write_config8(dev, 0x74, wiosize);
308}
309
310static void hudson_lpc_enable_resources(device_t dev)
311{
312 pci_dev_enable_resources(dev);
313 hudson_lpc_enable_childrens_resources(dev);
314}
315
316static struct pci_operations lops_pci = {
317 .set_subsystem = pci_dev_set_subsystem,
318};
319
320static struct device_operations lpc_ops = {
321 .read_resources = hudson_lpc_read_resources,
322 .set_resources = hudson_lpc_set_resources,
323 .enable_resources = hudson_lpc_enable_resources,
324 .init = lpc_init,
325 .scan_bus = scan_static_bus,
326 .ops_pci = &lops_pci,
327};
328static const struct pci_driver lpc_driver __pci_driver = {
329 .ops = &lpc_ops,
330 .vendor = PCI_VENDOR_ID_AMD,
331 .device = PCI_DEVICE_ID_ATI_SB900_LPC,
332};