Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * QEMU PC APM controller Emulation |
| 3 | * This is split out from acpi.c |
| 4 | * |
| 5 | * Copyright (c) 2006 Fabrice Bellard |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This library 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 GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 18 | * |
| 19 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 20 | * GNU GPL, version 2 or (at your option) any later version. |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 21 | */ |
| 22 | |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 23 | #include "hw/isa/apm.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 24 | #include "hw/hw.h" |
| 25 | #include "hw/pci/pci.h" |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 26 | |
| 27 | //#define DEBUG |
| 28 | |
Isaku Yamahata | 019ea97 | 2010-05-14 16:29:23 +0900 | [diff] [blame] | 29 | #ifdef DEBUG |
| 30 | # define APM_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
| 31 | #else |
| 32 | # define APM_DPRINTF(format, ...) do { } while (0) |
| 33 | #endif |
| 34 | |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 35 | /* fixed I/O location */ |
| 36 | #define APM_CNT_IOPORT 0xb2 |
| 37 | #define APM_STS_IOPORT 0xb3 |
| 38 | |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 39 | static void apm_ioport_writeb(void *opaque, hwaddr addr, uint64_t val, |
| 40 | unsigned size) |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 41 | { |
| 42 | APMState *apm = opaque; |
| 43 | addr &= 1; |
Isaku Yamahata | 019ea97 | 2010-05-14 16:29:23 +0900 | [diff] [blame] | 44 | APM_DPRINTF("apm_ioport_writeb addr=0x%x val=0x%02x\n", addr, val); |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 45 | if (addr == 0) { |
| 46 | apm->apmc = val; |
| 47 | |
| 48 | if (apm->callback) { |
| 49 | (apm->callback)(val, apm->arg); |
| 50 | } |
| 51 | } else { |
| 52 | apm->apms = val; |
| 53 | } |
| 54 | } |
| 55 | |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 56 | static uint64_t apm_ioport_readb(void *opaque, hwaddr addr, unsigned size) |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 57 | { |
| 58 | APMState *apm = opaque; |
| 59 | uint32_t val; |
| 60 | |
| 61 | addr &= 1; |
| 62 | if (addr == 0) { |
| 63 | val = apm->apmc; |
| 64 | } else { |
| 65 | val = apm->apms; |
| 66 | } |
Isaku Yamahata | 019ea97 | 2010-05-14 16:29:23 +0900 | [diff] [blame] | 67 | APM_DPRINTF("apm_ioport_readb addr=0x%x val=0x%02x\n", addr, val); |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 68 | return val; |
| 69 | } |
| 70 | |
| 71 | const VMStateDescription vmstate_apm = { |
| 72 | .name = "APM State", |
| 73 | .version_id = 1, |
| 74 | .minimum_version_id = 1, |
| 75 | .minimum_version_id_old = 1, |
| 76 | .fields = (VMStateField[]) { |
| 77 | VMSTATE_UINT8(apmc, APMState), |
| 78 | VMSTATE_UINT8(apms, APMState), |
| 79 | VMSTATE_END_OF_LIST() |
| 80 | } |
| 81 | }; |
| 82 | |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 83 | static const MemoryRegionOps apm_ops = { |
| 84 | .read = apm_ioport_readb, |
| 85 | .write = apm_ioport_writeb, |
| 86 | .impl = { |
| 87 | .min_access_size = 1, |
| 88 | .max_access_size = 1, |
| 89 | }, |
| 90 | }; |
| 91 | |
| 92 | void apm_init(PCIDevice *dev, APMState *apm, apm_ctrl_changed_t callback, |
| 93 | void *arg) |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 94 | { |
| 95 | apm->callback = callback; |
| 96 | apm->arg = arg; |
| 97 | |
| 98 | /* ioport 0xb2, 0xb3 */ |
Paolo Bonzini | 1437c94 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 99 | memory_region_init_io(&apm->io, OBJECT(dev), &apm_ops, apm, "apm-io", 2); |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 100 | memory_region_add_subregion(pci_address_space_io(dev), APM_CNT_IOPORT, |
| 101 | &apm->io); |
Isaku Yamahata | 0476284 | 2010-05-14 16:29:00 +0900 | [diff] [blame] | 102 | } |