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. |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 15 | */ |
| 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> |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 26 | #include <arch/ioapic.h> |
Edward O'Callaghan | 4565aea | 2015-06-07 19:42:47 +1000 | [diff] [blame] | 27 | #include <arch/acpi.h> |
Dave Frodin | 8d9a1bd | 2015-03-31 16:10:58 -0600 | [diff] [blame] | 28 | #include <pc80/i8254.h> |
| 29 | #include <pc80/i8259.h> |
Elyes HAOUAS | ab89edb | 2019-05-15 21:10:44 +0200 | [diff] [blame] | 30 | #include <types.h> |
| 31 | |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 32 | #include "hudson.h" |
Philipp Deppenwiese | 3067012 | 2017-03-01 02:24:33 +0100 | [diff] [blame] | 33 | #include "pci_devs.h" |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 34 | |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 35 | static void lpc_init(struct device *dev) |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 36 | { |
| 37 | u8 byte; |
| 38 | u32 dword; |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 39 | struct device *sm_dev; |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 40 | |
| 41 | /* Enable the LPC Controller */ |
Kyösti Mälkki | 33ff44c | 2018-05-22 01:15:22 +0300 | [diff] [blame] | 42 | sm_dev = pcidev_on_root(0x14, 0); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 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 | |
Elyes HAOUAS | 1bcd7fc | 2016-07-28 21:20:04 +0200 | [diff] [blame] | 68 | /* bit0: Enable prefetch a cacheline (64 bytes) when Host reads code from SPI ROM */ |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 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); |
| 73 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 74 | cmos_check_update_date(); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 75 | |
| 76 | /* Initialize the real time clock. |
| 77 | * The 0 argument tells cmos_init not to |
| 78 | * update CMOS unless it is invalid. |
| 79 | * 1 tells cmos_init to always initialize the CMOS. |
| 80 | */ |
Aaron Durbin | 9fde0d7 | 2017-09-15 11:01:17 -0600 | [diff] [blame] | 81 | cmos_init(0); |
Dave Frodin | 8d9a1bd | 2015-03-31 16:10:58 -0600 | [diff] [blame] | 82 | |
| 83 | /* Initialize i8259 pic */ |
| 84 | setup_i8259 (); |
| 85 | |
| 86 | /* Initialize i8254 timers */ |
| 87 | setup_i8254 (); |
Marc Jones | 3eec9dd | 2017-04-09 18:00:40 -0600 | [diff] [blame] | 88 | |
Paul Menzel | 4c40229 | 2017-04-14 17:23:49 +0200 | [diff] [blame] | 89 | /* Set up SERIRQ, enable continuous mode */ |
Marc Jones | 3eec9dd | 2017-04-09 18:00:40 -0600 | [diff] [blame] | 90 | byte = (BIT(4) | BIT(7)); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 91 | if (!CONFIG(SERIRQ_CONTINUOUS_MODE)) |
Marc Jones | 3eec9dd | 2017-04-09 18:00:40 -0600 | [diff] [blame] | 92 | byte |= BIT(6); |
| 93 | |
| 94 | pm_write8(PM_SERIRQ_CONF, byte); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 97 | static void hudson_lpc_read_resources(struct device *dev) |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 98 | { |
| 99 | struct resource *res; |
| 100 | |
| 101 | /* Get the normal pci resources of this device */ |
| 102 | pci_dev_read_resources(dev); /* We got one for APIC, or one more for TRAP */ |
| 103 | |
| 104 | /* Add an extra subtractive resource for both memory and I/O. */ |
| 105 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0)); |
| 106 | res->base = 0; |
| 107 | res->size = 0x1000; |
| 108 | res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | |
| 109 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 110 | |
| 111 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0)); |
| 112 | res->base = 0xff800000; |
| 113 | res->size = 0x00800000; /* 8 MB for flash */ |
| 114 | res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | |
| 115 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 116 | |
| 117 | /* Add a memory resource for the SPI BAR. */ |
| 118 | fixed_mem_resource(dev, 2, SPI_BASE_ADDRESS / 1024, 1, IORESOURCE_SUBTRACTIVE); |
| 119 | |
| 120 | res = new_resource(dev, 3); /* IOAPIC */ |
| 121 | res->base = IO_APIC_ADDR; |
| 122 | res->size = 0x00001000; |
| 123 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 124 | |
| 125 | compact_resources(dev); |
| 126 | } |
| 127 | |
| 128 | static void hudson_lpc_set_resources(struct device *dev) |
| 129 | { |
| 130 | struct resource *res; |
Marc Jones | 6fcaaef | 2017-04-20 16:48:42 -0600 | [diff] [blame] | 131 | u32 spi_enable_bits; |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 132 | |
Marc Jones | 6fcaaef | 2017-04-20 16:48:42 -0600 | [diff] [blame] | 133 | /* Special case. The SpiRomEnable and other enables should STAY set. */ |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 134 | res = find_resource(dev, 2); |
Marc Jones | 6fcaaef | 2017-04-20 16:48:42 -0600 | [diff] [blame] | 135 | spi_enable_bits = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER); |
| 136 | spi_enable_bits &= 0xF; |
| 137 | pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, res->base | spi_enable_bits); |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 138 | |
| 139 | pci_dev_set_resources(dev); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @brief Enable resources for children devices |
| 144 | * |
Edward O'Callaghan | 4565aea | 2015-06-07 19:42:47 +1000 | [diff] [blame] | 145 | * @param dev the device whose children's resources are to be enabled |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 146 | * |
| 147 | */ |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 148 | static void hudson_lpc_enable_childrens_resources(struct device *dev) |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 149 | { |
| 150 | struct bus *link; |
| 151 | u32 reg, reg_x; |
| 152 | int var_num = 0; |
| 153 | u16 reg_var[3]; |
| 154 | u16 reg_size[1] = {512}; |
| 155 | u8 wiosize = pci_read_config8(dev, 0x74); |
| 156 | |
| 157 | /* Be bit relaxed, tolerate that LPC region might be bigger than resource we try to fit, |
| 158 | * do it like this for all regions < 16 bytes. If there is a resource > 16 bytes |
| 159 | * it must be 512 bytes to be able to allocate the fresh LPC window. |
| 160 | * |
| 161 | * AGESA likes to enable already one LPC region in wide port base 0x64-0x65, |
| 162 | * using DFLT_SIO_PME_BASE_ADDRESS, 512 bytes size |
| 163 | * The code tries to check if resource can fit into this region |
| 164 | */ |
| 165 | |
| 166 | reg = pci_read_config32(dev, 0x44); |
| 167 | reg_x = pci_read_config32(dev, 0x48); |
| 168 | |
| 169 | /* check if ranges are free and not use them if entry is just already taken */ |
| 170 | if (reg_x & (1 << 2)) |
| 171 | var_num = 1; |
| 172 | /* just in case check if someone did not manually set other ranges too */ |
| 173 | if (reg_x & (1 << 24)) |
| 174 | var_num = 2; |
| 175 | |
| 176 | if (reg_x & (1 << 25)) |
| 177 | var_num = 3; |
| 178 | |
| 179 | /* check AGESA region size */ |
| 180 | if (wiosize & (1 << 0)) |
| 181 | reg_size[0] = 16; |
| 182 | |
| 183 | reg_var[2] = pci_read_config16(dev, 0x90); |
| 184 | reg_var[1] = pci_read_config16(dev, 0x66); |
| 185 | reg_var[0] = pci_read_config16(dev, 0x64); |
| 186 | |
| 187 | for (link = dev->link_list; link; link = link->next) { |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 188 | struct device *child; |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 189 | for (child = link->children; child; |
| 190 | child = child->sibling) { |
| 191 | if (child->enabled |
| 192 | && (child->path.type == DEVICE_PATH_PNP)) { |
| 193 | struct resource *res; |
| 194 | for (res = child->resource_list; res; res = res->next) { |
| 195 | u32 base, end; /* don't need long long */ |
| 196 | u32 rsize, set = 0, set_x = 0; |
| 197 | if (!(res->flags & IORESOURCE_IO)) |
| 198 | continue; |
| 199 | base = res->base; |
| 200 | end = resource_end(res); |
| 201 | /* find a resource size */ |
| 202 | printk(BIOS_DEBUG, "hudson lpc decode:%s, base=0x%08x, end=0x%08x\n", |
| 203 | dev_path(child), base, end); |
| 204 | switch (base) { |
| 205 | case 0x60: /* KB */ |
| 206 | case 0x64: /* MS */ |
| 207 | set |= (1 << 29); |
| 208 | rsize = 1; |
| 209 | break; |
| 210 | case 0x3f8: /* COM1 */ |
| 211 | set |= (1 << 6); |
| 212 | rsize = 8; |
| 213 | break; |
| 214 | case 0x2f8: /* COM2 */ |
| 215 | set |= (1 << 7); |
| 216 | rsize = 8; |
| 217 | break; |
| 218 | case 0x378: /* Parallel 1 */ |
| 219 | set |= (1 << 0); |
| 220 | set |= (1 << 1); /* + 0x778 for ECP */ |
| 221 | rsize = 8; |
| 222 | break; |
| 223 | case 0x3f0: /* FD0 */ |
| 224 | set |= (1 << 26); |
| 225 | rsize = 8; |
| 226 | break; |
| 227 | case 0x220: /* 0x220 - 0x227 */ |
| 228 | set |= (1 << 8); |
| 229 | rsize = 8; |
| 230 | break; |
| 231 | case 0x228: /* 0x228 - 0x22f */ |
| 232 | set |= (1 << 9); |
| 233 | rsize = 8; |
| 234 | break; |
| 235 | case 0x238: /* 0x238 - 0x23f */ |
| 236 | set |= (1 << 10); |
| 237 | rsize = 8; |
| 238 | break; |
| 239 | case 0x300: /* 0x300 -0x301 */ |
| 240 | set |= (1 << 18); |
| 241 | rsize = 2; |
| 242 | break; |
| 243 | case 0x400: |
| 244 | set_x |= (1 << 16); |
| 245 | rsize = 0x40; |
| 246 | break; |
| 247 | case 0x480: |
| 248 | set_x |= (1 << 17); |
| 249 | rsize = 0x40; |
| 250 | break; |
| 251 | case 0x500: |
| 252 | set_x |= (1 << 18); |
| 253 | rsize = 0x40; |
| 254 | break; |
| 255 | case 0x580: |
| 256 | set_x |= (1 << 19); |
| 257 | rsize = 0x40; |
| 258 | break; |
| 259 | case 0x4700: |
| 260 | set_x |= (1 << 22); |
| 261 | rsize = 0xc; |
| 262 | break; |
| 263 | case 0xfd60: |
| 264 | set_x |= (1 << 23); |
| 265 | rsize = 16; |
| 266 | break; |
| 267 | default: |
| 268 | rsize = 0; |
| 269 | /* try AGESA allocated region in region 0 */ |
| 270 | if ((var_num > 0) && ((base >=reg_var[0]) && |
| 271 | ((base + res->size) <= (reg_var[0] + reg_size[0])))) |
| 272 | rsize = reg_size[0]; |
| 273 | } |
| 274 | /* check if region found and matches the enable */ |
| 275 | if (res->size <= rsize) { |
| 276 | reg |= set; |
| 277 | reg_x |= set_x; |
| 278 | /* check if we can fit resource in variable range */ |
| 279 | } else if ((var_num < 3) && |
| 280 | ((res->size <= 16) || (res->size == 512))) { |
| 281 | /* use variable ranges if pre-defined do not match */ |
| 282 | switch (var_num) { |
| 283 | case 0: |
| 284 | reg_x |= (1 << 2); |
| 285 | if (res->size <= 16) { |
| 286 | wiosize |= (1 << 0); |
| 287 | } |
| 288 | break; |
| 289 | case 1: |
| 290 | reg_x |= (1 << 24); |
| 291 | if (res->size <= 16) |
| 292 | wiosize |= (1 << 2); |
| 293 | break; |
| 294 | case 2: |
| 295 | reg_x |= (1 << 25); |
| 296 | if (res->size <= 16) |
| 297 | wiosize |= (1 << 3); |
| 298 | break; |
| 299 | } |
| 300 | reg_var[var_num++] = |
| 301 | base & 0xffff; |
| 302 | } else { |
| 303 | printk(BIOS_ERR, "cannot fit LPC decode region:%s, base=0x%08x, end=0x%08x\n", |
| 304 | dev_path(child), base, end); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | pci_write_config32(dev, 0x44, reg); |
| 311 | pci_write_config32(dev, 0x48, reg_x); |
| 312 | /* Set WideIO for as many IOs found (fall through is on purpose) */ |
| 313 | switch (var_num) { |
| 314 | case 3: |
| 315 | pci_write_config16(dev, 0x90, reg_var[2]); |
| 316 | /* fall through */ |
| 317 | case 2: |
| 318 | pci_write_config16(dev, 0x66, reg_var[1]); |
| 319 | /* fall through */ |
| 320 | case 1: |
| 321 | pci_write_config16(dev, 0x64, reg_var[0]); |
| 322 | break; |
| 323 | } |
| 324 | pci_write_config8(dev, 0x74, wiosize); |
| 325 | } |
| 326 | |
Elyes HAOUAS | d9ef546 | 2018-05-19 17:08:23 +0200 | [diff] [blame] | 327 | static void hudson_lpc_enable_resources(struct device *dev) |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 328 | { |
| 329 | pci_dev_enable_resources(dev); |
| 330 | hudson_lpc_enable_childrens_resources(dev); |
| 331 | } |
| 332 | |
Edward O'Callaghan | 4565aea | 2015-06-07 19:42:47 +1000 | [diff] [blame] | 333 | unsigned long acpi_fill_mcfg(unsigned long current) |
| 334 | { |
Michał Żygowski | 033435b | 2019-09-06 19:18:24 +0200 | [diff] [blame] | 335 | current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current, |
| 336 | CONFIG_MMCONF_BASE_ADDRESS, |
| 337 | 0, |
| 338 | 0, |
| 339 | CONFIG_MMCONF_BUS_NUMBER); |
Edward O'Callaghan | 4565aea | 2015-06-07 19:42:47 +1000 | [diff] [blame] | 340 | return current; |
| 341 | } |
| 342 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 343 | static const char *lpc_acpi_name(const struct device *dev) |
Philipp Deppenwiese | 3067012 | 2017-03-01 02:24:33 +0100 | [diff] [blame] | 344 | { |
| 345 | if (dev->path.type != DEVICE_PATH_PCI) |
| 346 | return NULL; |
| 347 | |
| 348 | if (dev->path.pci.devfn == LPC_DEVFN) |
| 349 | return "LIBR"; |
| 350 | |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 354 | static struct pci_operations lops_pci = { |
| 355 | .set_subsystem = pci_dev_set_subsystem, |
| 356 | }; |
| 357 | |
| 358 | static struct device_operations lpc_ops = { |
| 359 | .read_resources = hudson_lpc_read_resources, |
| 360 | .set_resources = hudson_lpc_set_resources, |
| 361 | .enable_resources = hudson_lpc_enable_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 362 | #if CONFIG(HAVE_ACPI_TABLES) |
Dave Frodin | 5c015f0 | 2015-01-27 07:19:04 -0700 | [diff] [blame] | 363 | .write_acpi_tables = acpi_write_hpet, |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 364 | #endif |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 365 | .init = lpc_init, |
Nico Huber | 51b75ae | 2019-03-14 16:02:05 +0100 | [diff] [blame] | 366 | .scan_bus = scan_static_bus, |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 367 | .ops_pci = &lops_pci, |
Philipp Deppenwiese | 3067012 | 2017-03-01 02:24:33 +0100 | [diff] [blame] | 368 | .acpi_name = lpc_acpi_name, |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 369 | }; |
WANG Siyuan | f2dfef0 | 2015-05-20 14:41:01 +0800 | [diff] [blame] | 370 | |
| 371 | static const unsigned short pci_device_ids[] = { |
Kyösti Mälkki | 9d9a552 | 2016-11-19 22:14:59 +0200 | [diff] [blame] | 372 | PCI_DEVICE_ID_AMD_SB900_LPC, |
WANG Siyuan | f2dfef0 | 2015-05-20 14:41:01 +0800 | [diff] [blame] | 373 | PCI_DEVICE_ID_AMD_CZ_LPC, |
| 374 | 0 |
| 375 | }; |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 376 | static const struct pci_driver lpc_driver __pci_driver = { |
| 377 | .ops = &lpc_ops, |
| 378 | .vendor = PCI_VENDOR_ID_AMD, |
WANG Siyuan | f2dfef0 | 2015-05-20 14:41:01 +0800 | [diff] [blame] | 379 | .devices = pci_device_ids, |
Kyösti Mälkki | e8b4da2 | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 380 | }; |