]>
Commit | Line | Data |
---|---|---|
d1157ca4 OA |
1 | /* |
2 | * ARM Versatile I2C controller | |
3 | * | |
4 | * Copyright (c) 2006-2007 CodeSourcery. | |
5 | * Copyright (c) 2012 Oskar Andero <oskar.andero@gmail.com> | |
6 | * | |
7 | * This file is derived from hw/realview.c by Paul Brook | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * as published by the Free Software Foundation; either version 2 | |
12 | * of the License, or (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
21 | * | |
22 | */ | |
23 | ||
83c9f4ca | 24 | #include "hw/sysbus.h" |
47b43a1f | 25 | #include "bitbang_i2c.h" |
d1157ca4 | 26 | |
93e7f5f4 AF |
27 | #define TYPE_VERSATILE_I2C "versatile_i2c" |
28 | #define VERSATILE_I2C(obj) \ | |
29 | OBJECT_CHECK(VersatileI2CState, (obj), TYPE_VERSATILE_I2C) | |
30 | ||
31 | typedef struct VersatileI2CState { | |
32 | SysBusDevice parent_obj; | |
33 | ||
d1157ca4 OA |
34 | MemoryRegion iomem; |
35 | bitbang_i2c_interface *bitbang; | |
36 | int out; | |
37 | int in; | |
38 | } VersatileI2CState; | |
39 | ||
a8170e5e | 40 | static uint64_t versatile_i2c_read(void *opaque, hwaddr offset, |
d1157ca4 OA |
41 | unsigned size) |
42 | { | |
43 | VersatileI2CState *s = (VersatileI2CState *)opaque; | |
44 | ||
45 | if (offset == 0) { | |
46 | return (s->out & 1) | (s->in << 1); | |
47 | } else { | |
5170d661 PM |
48 | qemu_log_mask(LOG_GUEST_ERROR, |
49 | "%s: Bad offset 0x%x\n", __func__, (int)offset); | |
d1157ca4 OA |
50 | return -1; |
51 | } | |
52 | } | |
53 | ||
a8170e5e | 54 | static void versatile_i2c_write(void *opaque, hwaddr offset, |
d1157ca4 OA |
55 | uint64_t value, unsigned size) |
56 | { | |
57 | VersatileI2CState *s = (VersatileI2CState *)opaque; | |
58 | ||
59 | switch (offset) { | |
60 | case 0: | |
61 | s->out |= value & 3; | |
62 | break; | |
63 | case 4: | |
64 | s->out &= ~value; | |
65 | break; | |
66 | default: | |
5170d661 PM |
67 | qemu_log_mask(LOG_GUEST_ERROR, |
68 | "%s: Bad offset 0x%x\n", __func__, (int)offset); | |
d1157ca4 OA |
69 | } |
70 | bitbang_i2c_set(s->bitbang, BITBANG_I2C_SCL, (s->out & 1) != 0); | |
71 | s->in = bitbang_i2c_set(s->bitbang, BITBANG_I2C_SDA, (s->out & 2) != 0); | |
72 | } | |
73 | ||
74 | static const MemoryRegionOps versatile_i2c_ops = { | |
75 | .read = versatile_i2c_read, | |
76 | .write = versatile_i2c_write, | |
77 | .endianness = DEVICE_NATIVE_ENDIAN, | |
78 | }; | |
79 | ||
93e7f5f4 | 80 | static int versatile_i2c_init(SysBusDevice *sbd) |
d1157ca4 | 81 | { |
93e7f5f4 AF |
82 | DeviceState *dev = DEVICE(sbd); |
83 | VersatileI2CState *s = VERSATILE_I2C(dev); | |
a5c82852 | 84 | I2CBus *bus; |
d1157ca4 | 85 | |
93e7f5f4 | 86 | bus = i2c_init_bus(dev, "i2c"); |
d1157ca4 | 87 | s->bitbang = bitbang_i2c_init(bus); |
1437c94b | 88 | memory_region_init_io(&s->iomem, OBJECT(s), &versatile_i2c_ops, s, |
d1157ca4 | 89 | "versatile_i2c", 0x1000); |
93e7f5f4 | 90 | sysbus_init_mmio(sbd, &s->iomem); |
d1157ca4 OA |
91 | return 0; |
92 | } | |
93 | ||
94 | static void versatile_i2c_class_init(ObjectClass *klass, void *data) | |
95 | { | |
96 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); | |
97 | ||
98 | k->init = versatile_i2c_init; | |
99 | } | |
100 | ||
101 | static const TypeInfo versatile_i2c_info = { | |
93e7f5f4 | 102 | .name = TYPE_VERSATILE_I2C, |
d1157ca4 OA |
103 | .parent = TYPE_SYS_BUS_DEVICE, |
104 | .instance_size = sizeof(VersatileI2CState), | |
105 | .class_init = versatile_i2c_class_init, | |
106 | }; | |
107 | ||
108 | static void versatile_i2c_register_types(void) | |
109 | { | |
110 | type_register_static(&versatile_i2c_info); | |
111 | } | |
112 | ||
113 | type_init(versatile_i2c_register_types) |