]> Git Repo - qemu.git/blob - hw/i2c/versatile_i2c.c
hw/i2c/versatile_i2c: Add SCL/SDA definitions
[qemu.git] / hw / i2c / versatile_i2c.c
1 /*
2  * ARM Versatile I2C controller
3  *
4  * Copyright (c) 2006-2007 CodeSourcery.
5  * Copyright (c) 2012 Oskar Andero <[email protected]>
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
24 #include "qemu/osdep.h"
25 #include "hw/sysbus.h"
26 #include "hw/i2c/bitbang_i2c.h"
27 #include "hw/registerfields.h"
28 #include "qemu/log.h"
29 #include "qemu/module.h"
30
31 #define TYPE_VERSATILE_I2C "versatile_i2c"
32 #define VERSATILE_I2C(obj) \
33     OBJECT_CHECK(VersatileI2CState, (obj), TYPE_VERSATILE_I2C)
34
35 typedef struct VersatileI2CState {
36     SysBusDevice parent_obj;
37
38     MemoryRegion iomem;
39     bitbang_i2c_interface bitbang;
40     int out;
41     int in;
42 } VersatileI2CState;
43
44 REG32(CONTROL_GET, 0)
45 REG32(CONTROL_SET, 0)
46 REG32(CONTROL_CLR, 4)
47
48 #define SCL BIT(0)
49 #define SDA BIT(1)
50
51 static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
52                                    unsigned size)
53 {
54     VersatileI2CState *s = (VersatileI2CState *)opaque;
55
56     switch (offset) {
57     case A_CONTROL_SET:
58         return (s->out & 1) | (s->in << 1);
59     default:
60         qemu_log_mask(LOG_GUEST_ERROR,
61                       "%s: Bad offset 0x%x\n", __func__, (int)offset);
62         return -1;
63     }
64 }
65
66 static void versatile_i2c_write(void *opaque, hwaddr offset,
67                                 uint64_t value, unsigned size)
68 {
69     VersatileI2CState *s = (VersatileI2CState *)opaque;
70
71     switch (offset) {
72     case A_CONTROL_SET:
73         s->out |= value & 3;
74         break;
75     case A_CONTROL_CLR:
76         s->out &= ~value;
77         break;
78     default:
79         qemu_log_mask(LOG_GUEST_ERROR,
80                       "%s: Bad offset 0x%x\n", __func__, (int)offset);
81     }
82     bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SCL, (s->out & SCL) != 0);
83     s->in = bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SDA, (s->out & SDA) != 0);
84 }
85
86 static const MemoryRegionOps versatile_i2c_ops = {
87     .read = versatile_i2c_read,
88     .write = versatile_i2c_write,
89     .endianness = DEVICE_NATIVE_ENDIAN,
90 };
91
92 static void versatile_i2c_init(Object *obj)
93 {
94     DeviceState *dev = DEVICE(obj);
95     VersatileI2CState *s = VERSATILE_I2C(obj);
96     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
97     I2CBus *bus;
98
99     bus = i2c_init_bus(dev, "i2c");
100     bitbang_i2c_init(&s->bitbang, bus);
101     memory_region_init_io(&s->iomem, obj, &versatile_i2c_ops, s,
102                           "versatile_i2c", 0x1000);
103     sysbus_init_mmio(sbd, &s->iomem);
104 }
105
106 static const TypeInfo versatile_i2c_info = {
107     .name          = TYPE_VERSATILE_I2C,
108     .parent        = TYPE_SYS_BUS_DEVICE,
109     .instance_size = sizeof(VersatileI2CState),
110     .instance_init = versatile_i2c_init,
111 };
112
113 static void versatile_i2c_register_types(void)
114 {
115     type_register_static(&versatile_i2c_info);
116 }
117
118 type_init(versatile_i2c_register_types)
This page took 0.030007 seconds and 4 git commands to generate.