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