Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
| 2 | #include <device/device.h> |
| 3 | #include <device/pci.h> |
| 4 | #include <device/pci_ids.h> |
| 5 | #include <device/pci_ops.h> |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 6 | #include <device/smbus.h> |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 7 | #include <pc80/mc146818rtc.h> |
Ronald G. Minnich | 5079a0d | 2012-11-27 11:32:38 -0800 | [diff] [blame] | 8 | #include <lib.h> |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 9 | #include <arch/io.h> |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 10 | #include "amd8111.h" |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 11 | #include "amd8111_smbus.h" |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 12 | |
| 13 | #define PREVIOUS_POWER_STATE 0x43 |
| 14 | #define MAINBOARD_POWER_OFF 0 |
| 15 | #define MAINBOARD_POWER_ON 1 |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 16 | #define SLOW_CPU_OFF 0 |
| 17 | #define SLOW_CPU__ON 1 |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 18 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 19 | #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL |
| 20 | #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 23 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 24 | static int lsmbus_recv_byte(device_t dev) |
| 25 | { |
| 26 | unsigned device; |
| 27 | struct resource *res; |
| 28 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 29 | device = dev->path.i2c.device; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 30 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 31 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 32 | return do_smbus_recv_byte(res->base, device); |
| 33 | } |
| 34 | |
| 35 | static int lsmbus_send_byte(device_t dev, uint8_t val) |
| 36 | { |
| 37 | unsigned device; |
| 38 | struct resource *res; |
| 39 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 40 | device = dev->path.i2c.device; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 41 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 42 | |
| 43 | return do_smbus_send_byte(res->base, device, val); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | static int lsmbus_read_byte(device_t dev, uint8_t address) |
| 48 | { |
| 49 | unsigned device; |
| 50 | struct resource *res; |
| 51 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 52 | device = dev->path.i2c.device; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 53 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 54 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 55 | return do_smbus_read_byte(res->base, device, address); |
| 56 | } |
| 57 | |
| 58 | static int lsmbus_write_byte(device_t dev, uint8_t address, uint8_t val) |
| 59 | { |
| 60 | unsigned device; |
| 61 | struct resource *res; |
| 62 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 63 | device = dev->path.i2c.device; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 64 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 65 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 66 | return do_smbus_write_byte(res->base, device, address, val); |
| 67 | } |
| 68 | |
Oskar Enoksson | 9bfa1c8 | 2011-10-14 02:16:48 +0200 | [diff] [blame] | 69 | static int lsmbus_block_read(device_t dev, uint8_t cmd, u8 bytes, u8 *buffer) |
| 70 | { |
| 71 | unsigned device; |
| 72 | struct resource *res; |
| 73 | |
| 74 | device = dev->path.i2c.device; |
| 75 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
| 76 | |
| 77 | return do_smbus_block_read(res->base, device, cmd, bytes, buffer); |
| 78 | } |
| 79 | |
| 80 | static int lsmbus_block_write(device_t dev, uint8_t cmd, u8 bytes, const u8 *buffer) |
| 81 | { |
| 82 | unsigned device; |
| 83 | struct resource *res; |
| 84 | |
| 85 | device = dev->path.i2c.device; |
| 86 | res = find_resource(get_pbus_smbus(dev)->dev, 0x58); |
| 87 | |
| 88 | return do_smbus_block_write(res->base, device, cmd, bytes, buffer); |
| 89 | } |
| 90 | |
| 91 | |
Vladimir Serbinenko | 822bc65 | 2014-01-03 15:55:40 +0100 | [diff] [blame] | 92 | #if CONFIG_HAVE_ACPI_TABLES |
Stefan Reinauer | f622d59 | 2005-11-26 16:56:05 +0000 | [diff] [blame] | 93 | unsigned pm_base; |
| 94 | #endif |
| 95 | |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 96 | static void acpi_init(struct device *dev) |
| 97 | { |
| 98 | uint8_t byte; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 99 | uint16_t pm10_bar; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 100 | int on; |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 101 | |
| 102 | #if 0 |
Stefan Reinauer | 50776fa | 2010-03-17 04:40:15 +0000 | [diff] [blame] | 103 | uint16_t word; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 104 | printk(BIOS_DEBUG, "ACPI: disabling NMI watchdog.. "); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 105 | byte = pci_read_config8(dev, 0x49); |
| 106 | pci_write_config8(dev, 0x49, byte | (1<<2)); |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 107 | |
| 108 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 109 | byte = pci_read_config8(dev, 0x41); |
| 110 | pci_write_config8(dev, 0x41, byte | (1<<6)|(1<<2)); |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 111 | |
| 112 | /* added from sourceforge */ |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 113 | byte = pci_read_config8(dev, 0x48); |
| 114 | pci_write_config8(dev, 0x48, byte | (1<<3)); |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 115 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 116 | printk(BIOS_DEBUG, "done.\n"); |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 117 | |
| 118 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 119 | printk(BIOS_DEBUG, "ACPI: Routing IRQ 12 to PS2 port.. "); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 120 | word = pci_read_config16(dev, 0x46); |
| 121 | pci_write_config16(dev, 0x46, word | (1<<9)); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 122 | printk(BIOS_DEBUG, "done.\n"); |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 123 | #endif |
Stefan Reinauer | 50776fa | 2010-03-17 04:40:15 +0000 | [diff] [blame] | 124 | |
Stefan Reinauer | 6cc0e08 | 2005-11-26 00:10:10 +0000 | [diff] [blame] | 125 | /* To enable the register 0xcf9 in the IO space |
| 126 | * bit [D5] is set in the amd8111 configuration register. |
| 127 | * The config. reg. is devBx41. Register 0xcf9 allows |
| 128 | * hard reset capability to the system. For the ACPI |
| 129 | * reset.reg values in fadt.c to work this register |
| 130 | * must be enabled. |
| 131 | */ |
| 132 | byte = pci_read_config8(dev, 0x41); |
| 133 | pci_write_config8(dev, 0x41, byte | (1<<6)|(1<<5)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 134 | |
Jason Schildt | ab327a3 | 2005-10-13 00:44:34 +0000 | [diff] [blame] | 135 | /* power on after power fail */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 136 | on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 137 | get_option(&on, "power_on_after_fail"); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 138 | byte = pci_read_config8(dev, PREVIOUS_POWER_STATE); |
| 139 | byte &= ~0x40; |
| 140 | if (!on) { |
| 141 | byte |= 0x40; |
| 142 | } |
| 143 | pci_write_config8(dev, PREVIOUS_POWER_STATE, byte); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 144 | printk(BIOS_INFO, "set power %s after power fail\n", on?"on":"off"); |
Jason Schildt | 043b409 | 2005-08-10 15:16:44 +0000 | [diff] [blame] | 145 | |
Stefan Reinauer | 96f8fb5 | 2006-03-23 19:26:40 +0000 | [diff] [blame] | 146 | /* switch serial irq logic from quiet mode to continuous |
| 147 | * mode for Winbond W83627HF Rev. 17 |
| 148 | */ |
| 149 | byte = pci_read_config8(dev, 0x4a); |
| 150 | pci_write_config8(dev, 0x4a, byte | (1<<6)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 151 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 152 | /* Throttle the CPU speed down for testing */ |
| 153 | on = SLOW_CPU_OFF; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 154 | get_option(&on, "slow_cpu"); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 155 | if(on) { |
| 156 | pm10_bar = (pci_read_config16(dev, 0x58)&0xff00); |
| 157 | outl(((on<<1)+0x10) ,(pm10_bar + 0x10)); |
Paul Menzel | 69813fe | 2014-03-01 12:49:57 +0100 | [diff] [blame] | 158 | inl(pm10_bar + 0x10); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 159 | on = 8-on; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 160 | printk(BIOS_DEBUG, "Throttling CPU %2d.%1.1d percent.\n", |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 161 | (on*12)+(on>>1),(on&1)*5); |
| 162 | } |
Stefan Reinauer | f622d59 | 2005-11-26 16:56:05 +0000 | [diff] [blame] | 163 | |
Vladimir Serbinenko | 822bc65 | 2014-01-03 15:55:40 +0100 | [diff] [blame] | 164 | #if CONFIG_HAVE_ACPI_TABLES |
Stefan Reinauer | f622d59 | 2005-11-26 16:56:05 +0000 | [diff] [blame] | 165 | pm_base = pci_read_config16(dev, 0x58) & 0xff00; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 166 | printk(BIOS_DEBUG, "pm_base: 0x%04x\n",pm_base); |
Stefan Reinauer | f622d59 | 2005-11-26 16:56:05 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void acpi_read_resources(device_t dev) |
| 172 | { |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 173 | struct resource *resource; |
| 174 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 175 | /* Handle the generic bars */ |
| 176 | pci_dev_read_resources(dev); |
| 177 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 178 | /* Add the ACPI/SMBUS bar */ |
| 179 | resource = new_resource(dev, 0x58); |
| 180 | resource->base = 0; |
| 181 | resource->size = 256; |
| 182 | resource->align = log2(256); |
| 183 | resource->gran = log2(256); |
| 184 | resource->limit = 65536; |
| 185 | resource->flags = IORESOURCE_IO; |
| 186 | resource->index = 0x58; |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 189 | static void acpi_enable_resources(device_t dev) |
| 190 | { |
| 191 | uint8_t byte; |
| 192 | /* Enable the generic pci resources */ |
| 193 | pci_dev_enable_resources(dev); |
| 194 | |
| 195 | /* Enable the ACPI/SMBUS Bar */ |
| 196 | byte = pci_read_config8(dev, 0x41); |
| 197 | byte |= (1 << 7); |
| 198 | pci_write_config8(dev, 0x41, byte); |
| 199 | |
| 200 | /* Set the class code */ |
| 201 | pci_write_config32(dev, 0x60, 0x06800000); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 202 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
| 206 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 207 | pci_write_config32(dev, 0x7c, |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 208 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 209 | } |
| 210 | |
| 211 | static struct smbus_bus_operations lops_smbus_bus = { |
| 212 | .recv_byte = lsmbus_recv_byte, |
| 213 | .send_byte = lsmbus_send_byte, |
| 214 | .read_byte = lsmbus_read_byte, |
| 215 | .write_byte = lsmbus_write_byte, |
Oskar Enoksson | 9bfa1c8 | 2011-10-14 02:16:48 +0200 | [diff] [blame] | 216 | .block_read = lsmbus_block_read, |
| 217 | .block_write= lsmbus_block_write, |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 218 | }; |
Yinghai Lu | 7213d0f | 2004-12-03 03:39:04 +0000 | [diff] [blame] | 219 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 220 | static struct pci_operations lops_pci = { |
| 221 | .set_subsystem = lpci_set_subsystem, |
| 222 | }; |
| 223 | |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 224 | static struct device_operations acpi_ops = { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 225 | .read_resources = acpi_read_resources, |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 226 | .set_resources = pci_dev_set_resources, |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 227 | .enable_resources = acpi_enable_resources, |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 228 | .init = acpi_init, |
Kyösti Mälkki | d0e212c | 2015-02-26 20:47:47 +0200 | [diff] [blame] | 229 | .scan_bus = scan_smbus, |
Jason Schildt | ab327a3 | 2005-10-13 00:44:34 +0000 | [diff] [blame] | 230 | /* We don't need amd8111_enable, chip ops takes care of it. |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 231 | * It could be useful if these devices were not |
Jason Schildt | ab327a3 | 2005-10-13 00:44:34 +0000 | [diff] [blame] | 232 | * enabled by default. |
| 233 | */ |
Jason Schildt | 043b409 | 2005-08-10 15:16:44 +0000 | [diff] [blame] | 234 | // .enable = amd8111_enable, |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 235 | .ops_pci = &lops_pci, |
| 236 | .ops_smbus_bus = &lops_smbus_bus, |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Stefan Reinauer | f1cf1f7 | 2007-10-24 09:08:58 +0000 | [diff] [blame] | 239 | static const struct pci_driver acpi_driver __pci_driver = { |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 240 | .ops = &acpi_ops, |
| 241 | .vendor = PCI_VENDOR_ID_AMD, |
| 242 | .device = PCI_DEVICE_ID_AMD_8111_ACPI, |
| 243 | }; |