Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 1 | /* |
| 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame^] | 18 | * Foundation, Inc. |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 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> |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 31 | #include <arch/acpi.h> |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 32 | #include <arch/ioapic.h> |
Dave Frodin | 8d9a1bd | 2015-03-31 16:10:58 -0600 | [diff] [blame] | 33 | #include <pc80/i8254.h> |
| 34 | #include <pc80/i8259.h> |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 35 | #include "hudson.h" |
| 36 | |
| 37 | static void lpc_init(device_t dev) |
| 38 | { |
| 39 | u8 byte; |
| 40 | u32 dword; |
| 41 | device_t sm_dev; |
| 42 | |
| 43 | /* Enable the LPC Controller */ |
| 44 | sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0)); |
| 45 | dword = pci_read_config32(sm_dev, 0x64); |
| 46 | dword |= 1 << 20; |
| 47 | pci_write_config32(sm_dev, 0x64, dword); |
| 48 | |
| 49 | /* Initialize isa dma */ |
| 50 | isa_dma_init(); |
| 51 | |
| 52 | /* Enable DMA transaction on the LPC bus */ |
| 53 | byte = pci_read_config8(dev, 0x40); |
| 54 | byte |= (1 << 2); |
| 55 | pci_write_config8(dev, 0x40, byte); |
| 56 | |
| 57 | /* Disable the timeout mechanism on LPC */ |
| 58 | byte = pci_read_config8(dev, 0x48); |
| 59 | byte &= ~(1 << 7); |
| 60 | pci_write_config8(dev, 0x48, byte); |
| 61 | |
| 62 | /* Disable LPC MSI Capability */ |
| 63 | byte = pci_read_config8(dev, 0x78); |
| 64 | byte &= ~(1 << 1); |
| 65 | byte &= ~(1 << 0); /* Keep the old way. i.e., when bus master/DMA cycle is going |
| 66 | on on LPC, it holds PCI grant, so no LPC slave cycle can |
| 67 | interrupt and visit LPC. */ |
| 68 | pci_write_config8(dev, 0x78, byte); |
| 69 | |
| 70 | /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI rom */ |
| 71 | /* bit3: Fix SPI_CS# timing issue when running at 66M. TODO:A12. */ |
| 72 | byte = pci_read_config8(dev, 0xBB); |
| 73 | byte |= 1 << 0 | 1 << 3; |
| 74 | pci_write_config8(dev, 0xBB, byte); |
| 75 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 76 | cmos_check_update_date(); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 77 | |
| 78 | /* Initialize the real time clock. |
| 79 | * The 0 argument tells cmos_init not to |
| 80 | * update CMOS unless it is invalid. |
| 81 | * 1 tells cmos_init to always initialize the CMOS. |
| 82 | */ |
| 83 | cmos_init(0); |
Dave Frodin | 8d9a1bd | 2015-03-31 16:10:58 -0600 | [diff] [blame] | 84 | |
| 85 | /* Initialize i8259 pic */ |
| 86 | setup_i8259 (); |
| 87 | |
| 88 | /* Initialize i8254 timers */ |
| 89 | setup_i8254 (); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void hudson_lpc_read_resources(device_t dev) |
| 93 | { |
| 94 | struct resource *res; |
| 95 | |
| 96 | /* Get the normal pci resources of this device */ |
| 97 | pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */ |
| 98 | |
| 99 | /* Add an extra subtractive resource for both memory and I/O. */ |
| 100 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0)); |
| 101 | res->base = 0; |
| 102 | res->size = 0x1000; |
| 103 | res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | |
| 104 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 105 | |
| 106 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0)); |
| 107 | res->base = 0xff800000; |
| 108 | res->size = 0x00800000; /* 8 MB for flash */ |
| 109 | res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | |
| 110 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 111 | |
| 112 | /* Add a memory resource for the SPI BAR. */ |
| 113 | fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE); |
| 114 | |
| 115 | res = new_resource(dev, 3); /* IOAPIC */ |
| 116 | res->base = IO_APIC_ADDR; |
| 117 | res->size = 0x00001000; |
| 118 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 119 | |
| 120 | compact_resources(dev); |
| 121 | } |
| 122 | |
| 123 | static void hudson_lpc_set_resources(struct device *dev) |
| 124 | { |
| 125 | struct resource *res; |
| 126 | |
| 127 | /* Special case. SPI Base Address. The SpiRomEnable should STAY set. */ |
| 128 | res = find_resource(dev, 2); |
| 129 | pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | SPI_ROM_ENABLE); |
| 130 | |
| 131 | pci_dev_set_resources(dev); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief Enable resources for children devices |
| 136 | * |
| 137 | * @param dev the device whos children's resources are to be enabled |
| 138 | * |
| 139 | */ |
| 140 | static void hudson_lpc_enable_childrens_resources(device_t dev) |
| 141 | { |
| 142 | struct bus *link; |
| 143 | u32 reg, reg_x; |
| 144 | int var_num = 0; |
| 145 | u16 reg_var[3]; |
| 146 | u16 reg_size[1] = {512}; |
| 147 | u8 wiosize = pci_read_config8(dev, 0x74); |
| 148 | |
| 149 | /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit, |
| 150 | * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes |
| 151 | * it must be 512 bytes to be able to allocate the fresh LPC window. |
| 152 | * |
| 153 | * AGESA likes to enable already one LPC region in wide port base 0x64-0x65, |
| 154 | * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size |
| 155 | * The code tries to check if resource can fit into this region |
| 156 | */ |
| 157 | |
| 158 | reg = pci_read_config32(dev, 0x44); |
| 159 | reg_x = pci_read_config32(dev, 0x48); |
| 160 | |
| 161 | /* check if ranges are free and not use them if entry is just already taken */ |
| 162 | if (reg_x & (1 << 2)) |
| 163 | var_num = 1; |
| 164 | /* just in case check if someone did not manually set other ranges too */ |
| 165 | if (reg_x & (1 << 24)) |
| 166 | var_num = 2; |
| 167 | |
| 168 | if (reg_x & (1 << 25)) |
| 169 | var_num = 3; |
| 170 | |
| 171 | /* check AGESA region size */ |
| 172 | if (wiosize & (1 << 0)) |
| 173 | reg_size[0] = 16; |
| 174 | |
| 175 | reg_var[2] = pci_read_config16(dev, 0x90); |
| 176 | reg_var[1] = pci_read_config16(dev, 0x66); |
| 177 | reg_var[0] = pci_read_config16(dev, 0x64); |
| 178 | |
| 179 | for (link = dev->link_list; link; link = link->next) { |
| 180 | device_t child; |
| 181 | for (child = link->children; child; |
| 182 | child = child->sibling) { |
| 183 | if (child->enabled |
| 184 | && (child->path.type == DEVICE_PATH_PNP)) { |
| 185 | struct resource *res; |
| 186 | for (res = child->resource_list; res; res = res->next) { |
| 187 | u32 base, end; /* don't need long long */ |
| 188 | u32 rsize, set = 0, set_x = 0; |
| 189 | if (!(res->flags & IORESOURCE_IO)) |
| 190 | continue; |
| 191 | base = res->base; |
| 192 | end = resource_end(res); |
| 193 | /* find a resource size */ |
| 194 | printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n", |
| 195 | dev_path(child), base, end); |
| 196 | switch (base) { |
| 197 | case 0x60: /* KB */ |
| 198 | case 0x64: /* MS */ |
| 199 | set |= (1 << 29); |
| 200 | rsize = 1; |
| 201 | break; |
| 202 | case 0x3f8: /* COM1 */ |
| 203 | set |= (1 << 6); |
| 204 | rsize = 8; |
| 205 | break; |
| 206 | case 0x2f8: /* COM2 */ |
| 207 | set |= (1 << 7); |
| 208 | rsize = 8; |
| 209 | break; |
| 210 | case 0x378: /* Parallel 1 */ |
| 211 | set |= (1 << 0); |
| 212 | set |= (1 << 1); /* + 0x778 for ECP */ |
| 213 | rsize = 8; |
| 214 | break; |
| 215 | case 0x3f0: /* FD0 */ |
| 216 | set |= (1 << 26); |
| 217 | rsize = 8; |
| 218 | break; |
| 219 | case 0x220: /* 0x220 - 0x227 */ |
| 220 | set |= (1 << 8); |
| 221 | rsize = 8; |
| 222 | break; |
| 223 | case 0x228: /* 0x228 - 0x22f */ |
| 224 | set |= (1 << 9); |
| 225 | rsize = 8; |
| 226 | break; |
| 227 | case 0x238: /* 0x238 - 0x23f */ |
| 228 | set |= (1 << 10); |
| 229 | rsize = 8; |
| 230 | break; |
| 231 | case 0x300: /* 0x300 -0x301 */ |
| 232 | set |= (1 << 18); |
| 233 | rsize = 2; |
| 234 | break; |
| 235 | case 0x400: |
| 236 | set_x |= (1 << 16); |
| 237 | rsize = 0x40; |
| 238 | break; |
| 239 | case 0x480: |
| 240 | set_x |= (1 << 17); |
| 241 | rsize = 0x40; |
| 242 | break; |
| 243 | case 0x500: |
| 244 | set_x |= (1 << 18); |
| 245 | rsize = 0x40; |
| 246 | break; |
| 247 | case 0x580: |
| 248 | set_x |= (1 << 19); |
| 249 | rsize = 0x40; |
| 250 | break; |
| 251 | case 0x4700: |
| 252 | set_x |= (1 << 22); |
| 253 | rsize = 0xc; |
| 254 | break; |
| 255 | case 0xfd60: |
| 256 | set_x |= (1 << 23); |
| 257 | rsize = 16; |
| 258 | break; |
| 259 | default: |
| 260 | rsize = 0; |
| 261 | /* try AGESA allocated region in region 0 */ |
| 262 | if ((var_num > 0) && ((base >=reg_var[0]) && |
| 263 | ((base + res->size) <= (reg_var[0] + reg_size[0])))) |
| 264 | rsize = reg_size[0]; |
| 265 | } |
| 266 | /* check if region found and matches the enable */ |
| 267 | if (res->size <= rsize) { |
| 268 | reg |= set; |
| 269 | reg_x |= set_x; |
| 270 | /* check if we can fit resource in variable range */ |
| 271 | } else if ((var_num < 3) && |
| 272 | ((res->size <= 16) || (res->size == 512))) { |
| 273 | /* use variable ranges if pre-defined do not match */ |
| 274 | switch (var_num) { |
| 275 | case 0: |
| 276 | reg_x |= (1 << 2); |
| 277 | if (res->size <= 16) { |
| 278 | wiosize |= (1 << 0); |
| 279 | } |
| 280 | break; |
| 281 | case 1: |
| 282 | reg_x |= (1 << 24); |
| 283 | if (res->size <= 16) |
| 284 | wiosize |= (1 << 2); |
| 285 | break; |
| 286 | case 2: |
| 287 | reg_x |= (1 << 25); |
| 288 | if (res->size <= 16) |
| 289 | wiosize |= (1 << 3); |
| 290 | break; |
| 291 | } |
| 292 | reg_var[var_num++] = |
| 293 | base & 0xffff; |
| 294 | } else { |
| 295 | printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n", |
| 296 | dev_path(child), base, end); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | pci_write_config32(dev, 0x44, reg); |
| 303 | pci_write_config32(dev, 0x48, reg_x); |
| 304 | /* Set WideIO for as many IOs found (fall through is on purpose) */ |
| 305 | switch (var_num) { |
| 306 | case 3: |
| 307 | pci_write_config16(dev, 0x90, reg_var[2]); |
| 308 | /* fall through */ |
| 309 | case 2: |
| 310 | pci_write_config16(dev, 0x66, reg_var[1]); |
| 311 | /* fall through */ |
| 312 | case 1: |
| 313 | pci_write_config16(dev, 0x64, reg_var[0]); |
| 314 | break; |
| 315 | } |
| 316 | pci_write_config8(dev, 0x74, wiosize); |
| 317 | } |
| 318 | |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 319 | unsigned long acpi_fill_mcfg(unsigned long current) |
| 320 | { |
| 321 | /* Just a dummy */ |
| 322 | return current; |
| 323 | } |
| 324 | |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 325 | static void hudson_lpc_enable_resources(device_t dev) |
| 326 | { |
| 327 | pci_dev_enable_resources(dev); |
| 328 | hudson_lpc_enable_childrens_resources(dev); |
| 329 | } |
| 330 | |
| 331 | static struct pci_operations lops_pci = { |
| 332 | .set_subsystem = pci_dev_set_subsystem, |
| 333 | }; |
| 334 | |
| 335 | static struct device_operations lpc_ops = { |
| 336 | .read_resources = hudson_lpc_read_resources, |
| 337 | .set_resources = hudson_lpc_set_resources, |
| 338 | .enable_resources = hudson_lpc_enable_resources, |
Dave Frodin | 5c015f0 | 2015-01-27 07:19:04 -0700 | [diff] [blame] | 339 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) && IS_ENABLED(CONFIG_PER_DEVICE_ACPI_TABLES) |
| 340 | .write_acpi_tables = acpi_write_hpet, |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 341 | #endif |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 342 | .init = lpc_init, |
| 343 | .scan_bus = scan_static_bus, |
| 344 | .ops_pci = &lops_pci, |
| 345 | }; |
| 346 | static const struct pci_driver lpc_driver __pci_driver = { |
| 347 | .ops = &lpc_ops, |
| 348 | .vendor = PCI_VENDOR_ID_AMD, |
| 349 | .device = PCI_DEVICE_ID_ATI_SB900_LPC, |
| 350 | }; |