Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Bit-Bang i2c emulation extracted from |
| 3 | * Marvell MV88W8618 / Freecom MusicPal emulation. |
| 4 | * |
| 5 | * Copyright (c) 2008 Jan Kiszka |
| 6 | * |
Matthew Fernandez | 8e31bf3 | 2011-06-26 12:21:35 +1000 | [diff] [blame] | 7 | * This code is licensed under the GNU GPL v2. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 8 | * |
| 9 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 10 | * GNU GPL, version 2 or (at your option) any later version. |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 11 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 12 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | 64552b6 | 2019-08-12 07:23:42 +0200 | [diff] [blame] | 14 | #include "hw/irq.h" |
BALATON Zoltan | d718b74 | 2019-06-20 12:55:23 +0200 | [diff] [blame] | 15 | #include "hw/i2c/bitbang_i2c.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 16 | #include "hw/sysbus.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 17 | #include "qemu/module.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 18 | #include "qom/object.h" |
Philippe Mathieu-Daudé | 1e5b189 | 2023-01-11 09:50:15 +0100 | [diff] [blame] | 19 | #include "trace.h" |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 20 | |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 21 | |
Philippe Mathieu-Daudé | 1e5b189 | 2023-01-11 09:50:15 +0100 | [diff] [blame] | 22 | /* bitbang_i2c_state enum to name */ |
| 23 | static const char * const sname[] = { |
| 24 | #define NAME(e) [e] = stringify(e) |
| 25 | NAME(STOPPED), |
| 26 | [SENDING_BIT7] = "SENDING_BIT7 (START)", |
| 27 | NAME(SENDING_BIT6), |
| 28 | NAME(SENDING_BIT5), |
| 29 | NAME(SENDING_BIT4), |
| 30 | NAME(SENDING_BIT3), |
| 31 | NAME(SENDING_BIT2), |
| 32 | NAME(SENDING_BIT1), |
| 33 | NAME(SENDING_BIT0), |
| 34 | NAME(WAITING_FOR_ACK), |
| 35 | [RECEIVING_BIT7] = "RECEIVING_BIT7 (ACK)", |
| 36 | NAME(RECEIVING_BIT6), |
| 37 | NAME(RECEIVING_BIT5), |
| 38 | NAME(RECEIVING_BIT4), |
| 39 | NAME(RECEIVING_BIT3), |
| 40 | NAME(RECEIVING_BIT2), |
| 41 | NAME(RECEIVING_BIT1), |
| 42 | NAME(RECEIVING_BIT0), |
| 43 | NAME(SENDING_ACK), |
| 44 | NAME(SENT_NACK) |
| 45 | #undef NAME |
| 46 | }; |
| 47 | |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 48 | static void bitbang_i2c_set_state(bitbang_i2c_interface *i2c, |
| 49 | bitbang_i2c_state state) |
| 50 | { |
Philippe Mathieu-Daudé | 1e5b189 | 2023-01-11 09:50:15 +0100 | [diff] [blame] | 51 | trace_bitbang_i2c_state(sname[i2c->state], sname[state]); |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 52 | i2c->state = state; |
| 53 | } |
| 54 | |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 55 | static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c) |
| 56 | { |
| 57 | if (i2c->current_addr >= 0) |
| 58 | i2c_end_transfer(i2c->bus); |
| 59 | i2c->current_addr = -1; |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 60 | bitbang_i2c_set_state(i2c, STOPPED); |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 63 | /* Set device data pin. */ |
| 64 | static int bitbang_i2c_ret(bitbang_i2c_interface *i2c, int level) |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 65 | { |
Philippe Mathieu-Daudé | c166e59 | 2023-01-11 09:50:16 +0100 | [diff] [blame] | 66 | trace_bitbang_i2c_data(i2c->last_clock, i2c->last_data, |
| 67 | i2c->device_out, level); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 68 | i2c->device_out = level; |
Philippe Mathieu-Daudé | c166e59 | 2023-01-11 09:50:16 +0100 | [diff] [blame] | 69 | |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 70 | return level & i2c->last_data; |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 71 | } |
| 72 | |
BALATON Zoltan | 8ada214 | 2023-07-28 02:45:24 +0200 | [diff] [blame] | 73 | /* Leave device data pin unmodified. */ |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 74 | static int bitbang_i2c_nop(bitbang_i2c_interface *i2c) |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 75 | { |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 76 | return bitbang_i2c_ret(i2c, i2c->device_out); |
| 77 | } |
| 78 | |
| 79 | /* Returns data line level. */ |
| 80 | int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level) |
| 81 | { |
| 82 | int data; |
| 83 | |
| 84 | if (level != 0 && level != 1) { |
| 85 | abort(); |
| 86 | } |
| 87 | |
| 88 | if (line == BITBANG_I2C_SDA) { |
| 89 | if (level == i2c->last_data) { |
| 90 | return bitbang_i2c_nop(i2c); |
| 91 | } |
| 92 | i2c->last_data = level; |
| 93 | if (i2c->last_clock == 0) { |
| 94 | return bitbang_i2c_nop(i2c); |
| 95 | } |
| 96 | if (level == 0) { |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 97 | /* START condition. */ |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 98 | bitbang_i2c_set_state(i2c, SENDING_BIT7); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 99 | i2c->current_addr = -1; |
| 100 | } else { |
| 101 | /* STOP condition. */ |
| 102 | bitbang_i2c_enter_stop(i2c); |
| 103 | } |
| 104 | return bitbang_i2c_ret(i2c, 1); |
| 105 | } |
| 106 | |
| 107 | data = i2c->last_data; |
| 108 | if (i2c->last_clock == level) { |
| 109 | return bitbang_i2c_nop(i2c); |
| 110 | } |
| 111 | i2c->last_clock = level; |
| 112 | if (level == 0) { |
| 113 | /* State is set/read at the start of the clock pulse. |
| 114 | release the data line at the end. */ |
| 115 | return bitbang_i2c_ret(i2c, 1); |
| 116 | } |
| 117 | switch (i2c->state) { |
| 118 | case STOPPED: |
Marcus Comstedt | 2eb9f24 | 2011-05-28 16:55:52 +0200 | [diff] [blame] | 119 | case SENT_NACK: |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 120 | return bitbang_i2c_ret(i2c, 1); |
| 121 | |
| 122 | case SENDING_BIT7 ... SENDING_BIT0: |
| 123 | i2c->buffer = (i2c->buffer << 1) | data; |
| 124 | /* will end up in WAITING_FOR_ACK */ |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 125 | bitbang_i2c_set_state(i2c, i2c->state + 1); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 126 | return bitbang_i2c_ret(i2c, 1); |
| 127 | |
| 128 | case WAITING_FOR_ACK: |
Peter Maydell | 9706e01 | 2016-10-24 19:12:29 +0100 | [diff] [blame] | 129 | { |
| 130 | int ret; |
| 131 | |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 132 | if (i2c->current_addr < 0) { |
| 133 | i2c->current_addr = i2c->buffer; |
Philippe Mathieu-Daudé | c166e59 | 2023-01-11 09:50:16 +0100 | [diff] [blame] | 134 | trace_bitbang_i2c_addr(i2c->current_addr); |
Peter Maydell | 9706e01 | 2016-10-24 19:12:29 +0100 | [diff] [blame] | 135 | ret = i2c_start_transfer(i2c->bus, i2c->current_addr >> 1, |
| 136 | i2c->current_addr & 1); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 137 | } else { |
Philippe Mathieu-Daudé | c166e59 | 2023-01-11 09:50:16 +0100 | [diff] [blame] | 138 | trace_bitbang_i2c_send(i2c->buffer); |
Peter Maydell | 9706e01 | 2016-10-24 19:12:29 +0100 | [diff] [blame] | 139 | ret = i2c_send(i2c->bus, i2c->buffer); |
| 140 | } |
| 141 | if (ret) { |
| 142 | /* NACK (either addressing a nonexistent device, or the |
| 143 | * device we were sending to decided to NACK us). |
| 144 | */ |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 145 | bitbang_i2c_set_state(i2c, SENT_NACK); |
Peter Maydell | 9706e01 | 2016-10-24 19:12:29 +0100 | [diff] [blame] | 146 | bitbang_i2c_enter_stop(i2c); |
| 147 | return bitbang_i2c_ret(i2c, 1); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 148 | } |
| 149 | if (i2c->current_addr & 1) { |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 150 | bitbang_i2c_set_state(i2c, RECEIVING_BIT7); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 151 | } else { |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 152 | bitbang_i2c_set_state(i2c, SENDING_BIT7); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 153 | } |
| 154 | return bitbang_i2c_ret(i2c, 0); |
Peter Maydell | 9706e01 | 2016-10-24 19:12:29 +0100 | [diff] [blame] | 155 | } |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 156 | case RECEIVING_BIT7: |
| 157 | i2c->buffer = i2c_recv(i2c->bus); |
Philippe Mathieu-Daudé | c166e59 | 2023-01-11 09:50:16 +0100 | [diff] [blame] | 158 | trace_bitbang_i2c_recv(i2c->buffer); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 159 | /* Fall through... */ |
| 160 | case RECEIVING_BIT6 ... RECEIVING_BIT0: |
| 161 | data = i2c->buffer >> 7; |
| 162 | /* will end up in SENDING_ACK */ |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 163 | bitbang_i2c_set_state(i2c, i2c->state + 1); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 164 | i2c->buffer <<= 1; |
| 165 | return bitbang_i2c_ret(i2c, data); |
| 166 | |
| 167 | case SENDING_ACK: |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 168 | if (data != 0) { |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 169 | bitbang_i2c_set_state(i2c, SENT_NACK); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 170 | i2c_nack(i2c->bus); |
| 171 | } else { |
Philippe Mathieu-Daudé | dc575b5 | 2023-01-11 09:50:14 +0100 | [diff] [blame] | 172 | bitbang_i2c_set_state(i2c, RECEIVING_BIT7); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 173 | } |
| 174 | return bitbang_i2c_ret(i2c, 1); |
| 175 | } |
| 176 | abort(); |
| 177 | } |
| 178 | |
Peter Maydell | 4174292 | 2019-07-02 17:38:44 +0100 | [diff] [blame] | 179 | void bitbang_i2c_init(bitbang_i2c_interface *s, I2CBus *bus) |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 180 | { |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 181 | s->bus = bus; |
| 182 | s->last_data = 1; |
| 183 | s->last_clock = 1; |
| 184 | s->device_out = 1; |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* GPIO interface. */ |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 188 | |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 189 | OBJECT_DECLARE_SIMPLE_TYPE(GPIOI2CState, GPIO_I2C) |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 190 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 191 | struct GPIOI2CState { |
Philippe Mathieu-Daudé | 2b9339d | 2023-01-11 09:50:13 +0100 | [diff] [blame] | 192 | /*< private >*/ |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 193 | SysBusDevice parent_obj; |
Philippe Mathieu-Daudé | 2b9339d | 2023-01-11 09:50:13 +0100 | [diff] [blame] | 194 | /*< public >*/ |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 195 | |
Peter Maydell | 4174292 | 2019-07-02 17:38:44 +0100 | [diff] [blame] | 196 | bitbang_i2c_interface bitbang; |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 197 | int last_level; |
| 198 | qemu_irq out; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 199 | }; |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 200 | |
| 201 | static void bitbang_i2c_gpio_set(void *opaque, int irq, int level) |
| 202 | { |
| 203 | GPIOI2CState *s = opaque; |
| 204 | |
Peter Maydell | 4174292 | 2019-07-02 17:38:44 +0100 | [diff] [blame] | 205 | level = bitbang_i2c_set(&s->bitbang, irq, level); |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 206 | if (level != s->last_level) { |
| 207 | s->last_level = level; |
| 208 | qemu_set_irq(s->out, level); |
| 209 | } |
| 210 | } |
| 211 | |
xiaoqiang zhao | 00b2f75 | 2016-06-14 15:59:13 +0100 | [diff] [blame] | 212 | static void gpio_i2c_init(Object *obj) |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 213 | { |
xiaoqiang zhao | 00b2f75 | 2016-06-14 15:59:13 +0100 | [diff] [blame] | 214 | DeviceState *dev = DEVICE(obj); |
| 215 | GPIOI2CState *s = GPIO_I2C(obj); |
Andreas Färber | a5c8285 | 2013-08-03 00:18:51 +0200 | [diff] [blame] | 216 | I2CBus *bus; |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 217 | |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 218 | bus = i2c_init_bus(dev, "i2c"); |
Peter Maydell | 4174292 | 2019-07-02 17:38:44 +0100 | [diff] [blame] | 219 | bitbang_i2c_init(&s->bitbang, bus); |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 220 | |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 221 | qdev_init_gpio_in(dev, bitbang_i2c_gpio_set, 2); |
| 222 | qdev_init_gpio_out(dev, &s->out, 1); |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 225 | static void gpio_i2c_class_init(ObjectClass *klass, void *data) |
| 226 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 227 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 228 | |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 229 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 230 | dc->desc = "Virtual GPIO to I2C bridge"; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 231 | } |
| 232 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 233 | static const TypeInfo gpio_i2c_info = { |
Andreas Färber | cc3c3b8 | 2013-07-26 18:13:46 +0200 | [diff] [blame] | 234 | .name = TYPE_GPIO_I2C, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 235 | .parent = TYPE_SYS_BUS_DEVICE, |
| 236 | .instance_size = sizeof(GPIOI2CState), |
xiaoqiang zhao | 00b2f75 | 2016-06-14 15:59:13 +0100 | [diff] [blame] | 237 | .instance_init = gpio_i2c_init, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 238 | .class_init = gpio_i2c_class_init, |
Paul Brook | 3cd035d | 2009-11-20 23:37:15 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 241 | static void bitbang_i2c_register_types(void) |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 242 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 243 | type_register_static(&gpio_i2c_info); |
Andrzej Zaborowski | 3ead03b | 2009-08-23 15:38:50 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 246 | type_init(bitbang_i2c_register_types) |