2 * Allwinner I2C Bus Serial Interface Emulation
4 * Copyright (C) 2022 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
6 * This file is derived from IMX I2C controller,
7 * by Jean-Christophe DUBOIS .
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 * SPDX-License-Identifier: MIT
25 #include "qemu/osdep.h"
26 #include "hw/i2c/allwinner-i2c.h"
28 #include "migration/vmstate.h"
29 #include "hw/i2c/i2c.h"
32 #include "qemu/module.h"
34 /* Allwinner I2C memory map */
35 #define TWI_ADDR_REG 0x00 /* slave address register */
36 #define TWI_XADDR_REG 0x04 /* extended slave address register */
37 #define TWI_DATA_REG 0x08 /* data register */
38 #define TWI_CNTR_REG 0x0c /* control register */
39 #define TWI_STAT_REG 0x10 /* status register */
40 #define TWI_CCR_REG 0x14 /* clock control register */
41 #define TWI_SRST_REG 0x18 /* software reset register */
42 #define TWI_EFR_REG 0x1c /* enhance feature register */
43 #define TWI_LCR_REG 0x20 /* line control register */
45 /* Used only in slave mode, do not set */
46 #define TWI_ADDR_RESET 0
47 #define TWI_XADDR_RESET 0
50 #define TWI_DATA_MASK 0xFF
51 #define TWI_DATA_RESET 0
53 /* Control register */
54 #define TWI_CNTR_INT_EN (1 << 7)
55 #define TWI_CNTR_BUS_EN (1 << 6)
56 #define TWI_CNTR_M_STA (1 << 5)
57 #define TWI_CNTR_M_STP (1 << 4)
58 #define TWI_CNTR_INT_FLAG (1 << 3)
59 #define TWI_CNTR_A_ACK (1 << 2)
60 #define TWI_CNTR_MASK 0xFC
61 #define TWI_CNTR_RESET 0
64 #define TWI_STAT_MASK 0xF8
65 #define TWI_STAT_RESET 0xF8
68 #define TWI_CCR_CLK_M_MASK 0x78
69 #define TWI_CCR_CLK_N_MASK 0x07
70 #define TWI_CCR_MASK 0x7F
71 #define TWI_CCR_RESET 0
74 #define TWI_SRST_MASK 0x01
75 #define TWI_SRST_RESET 0
78 #define TWI_EFR_MASK 0x03
79 #define TWI_EFR_RESET 0
82 #define TWI_LCR_SCL_STATE (1 << 5)
83 #define TWI_LCR_SDA_STATE (1 << 4)
84 #define TWI_LCR_SCL_CTL (1 << 3)
85 #define TWI_LCR_SCL_CTL_EN (1 << 2)
86 #define TWI_LCR_SDA_CTL (1 << 1)
87 #define TWI_LCR_SDA_CTL_EN (1 << 0)
88 #define TWI_LCR_MASK 0x3F
89 #define TWI_LCR_RESET 0x3A
91 /* Status value in STAT register is shifted by 3 bits */
92 #define TWI_STAT_SHIFT 3
93 #define STAT_FROM_STA(x) ((x) << TWI_STAT_SHIFT)
94 #define STAT_TO_STA(x) ((x) >> TWI_STAT_SHIFT)
112 STAT_S_ARB_LOST_AW_ACK
,
114 STAT_S_ARB_LOST_GCA_ACK
,
115 STAT_S_DATA_RX_SA_ACK
,
116 STAT_S_DATA_RX_SA_NACK
,
117 STAT_S_DATA_RX_GCA_ACK
,
118 STAT_S_DATA_RX_GCA_NACK
,
121 STAT_S_ARB_LOST_AR_ACK
,
125 /* Master mode, 10-bit */
126 STAT_M_2ND_ADDR_WR_ACK
,
127 STAT_M_2ND_ADDR_WR_NACK
,
132 static const char *allwinner_i2c_get_regname(unsigned offset
)
158 static inline bool allwinner_i2c_is_reset(AWI2CState
*s
)
160 return s
->srst
& TWI_SRST_MASK
;
163 static inline bool allwinner_i2c_bus_is_enabled(AWI2CState
*s
)
165 return s
->cntr
& TWI_CNTR_BUS_EN
;
168 static inline bool allwinner_i2c_interrupt_is_enabled(AWI2CState
*s
)
170 return s
->cntr
& TWI_CNTR_INT_EN
;
173 static void allwinner_i2c_reset_hold(Object
*obj
)
175 AWI2CState
*s
= AW_I2C(obj
);
177 if (STAT_TO_STA(s
->stat
) != STAT_IDLE
) {
178 i2c_end_transfer(s
->bus
);
181 s
->addr
= TWI_ADDR_RESET
;
182 s
->xaddr
= TWI_XADDR_RESET
;
183 s
->data
= TWI_DATA_RESET
;
184 s
->cntr
= TWI_CNTR_RESET
;
185 s
->stat
= TWI_STAT_RESET
;
186 s
->ccr
= TWI_CCR_RESET
;
187 s
->srst
= TWI_SRST_RESET
;
188 s
->efr
= TWI_EFR_RESET
;
189 s
->lcr
= TWI_LCR_RESET
;
192 static inline void allwinner_i2c_raise_interrupt(AWI2CState
*s
)
195 * Raise an interrupt if the device is not reset and it is configured
196 * to generate some interrupts.
198 if (!allwinner_i2c_is_reset(s
) && allwinner_i2c_bus_is_enabled(s
)) {
199 if (STAT_TO_STA(s
->stat
) != STAT_IDLE
) {
200 s
->cntr
|= TWI_CNTR_INT_FLAG
;
201 if (allwinner_i2c_interrupt_is_enabled(s
)) {
202 qemu_irq_raise(s
->irq
);
208 static uint64_t allwinner_i2c_read(void *opaque
, hwaddr offset
,
212 AWI2CState
*s
= AW_I2C(opaque
);
222 if ((STAT_TO_STA(s
->stat
) == STAT_M_ADDR_RD_ACK
) ||
223 (STAT_TO_STA(s
->stat
) == STAT_M_DATA_RX_ACK
) ||
224 (STAT_TO_STA(s
->stat
) == STAT_M_DATA_RX_NACK
)) {
225 /* Get the next byte */
226 s
->data
= i2c_recv(s
->bus
);
228 if (s
->cntr
& TWI_CNTR_A_ACK
) {
229 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_ACK
);
231 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_NACK
);
233 allwinner_i2c_raise_interrupt(s
);
243 * If polling when reading then change state to indicate data
246 if (STAT_TO_STA(s
->stat
) == STAT_M_ADDR_RD_ACK
) {
247 if (s
->cntr
& TWI_CNTR_A_ACK
) {
248 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_ACK
);
250 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_NACK
);
252 allwinner_i2c_raise_interrupt(s
);
268 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
269 HWADDR_PRIx
"\n", TYPE_AW_I2C
, __func__
, offset
);
274 trace_allwinner_i2c_read(allwinner_i2c_get_regname(offset
), offset
, value
);
276 return (uint64_t)value
;
279 static void allwinner_i2c_write(void *opaque
, hwaddr offset
,
280 uint64_t value
, unsigned size
)
282 AWI2CState
*s
= AW_I2C(opaque
);
286 trace_allwinner_i2c_write(allwinner_i2c_get_regname(offset
), offset
, value
);
290 s
->addr
= (uint8_t)value
;
293 s
->xaddr
= (uint8_t)value
;
296 /* If the device is in reset or not enabled, nothing to do */
297 if (allwinner_i2c_is_reset(s
) || (!allwinner_i2c_bus_is_enabled(s
))) {
301 s
->data
= value
& TWI_DATA_MASK
;
303 switch (STAT_TO_STA(s
->stat
)) {
307 if (i2c_start_transfer(s
->bus
, extract32(s
->data
, 1, 7),
308 extract32(s
->data
, 0, 1))) {
309 /* If non zero is returned, the address is not valid */
310 s
->stat
= STAT_FROM_STA(STAT_M_ADDR_WR_NACK
);
312 /* Determine if read of write */
313 if (extract32(s
->data
, 0, 1)) {
314 s
->stat
= STAT_FROM_STA(STAT_M_ADDR_RD_ACK
);
316 s
->stat
= STAT_FROM_STA(STAT_M_ADDR_WR_ACK
);
318 allwinner_i2c_raise_interrupt(s
);
321 case STAT_M_ADDR_WR_ACK
:
322 case STAT_M_DATA_TX_ACK
:
323 if (i2c_send(s
->bus
, s
->data
)) {
324 /* If the target return non zero then end the transfer */
325 s
->stat
= STAT_FROM_STA(STAT_M_DATA_TX_NACK
);
326 i2c_end_transfer(s
->bus
);
328 s
->stat
= STAT_FROM_STA(STAT_M_DATA_TX_ACK
);
329 allwinner_i2c_raise_interrupt(s
);
337 if (!allwinner_i2c_is_reset(s
)) {
338 /* Do something only if not in software reset */
339 s
->cntr
= value
& TWI_CNTR_MASK
;
341 /* Check if start condition should be sent */
342 if (s
->cntr
& TWI_CNTR_M_STA
) {
344 if (STAT_TO_STA(s
->stat
) == STAT_IDLE
) {
345 /* Send start condition */
346 s
->stat
= STAT_FROM_STA(STAT_M_STA_TX
);
348 /* Send repeated start condition */
349 s
->stat
= STAT_FROM_STA(STAT_M_RSTA_TX
);
351 /* Clear start condition */
352 s
->cntr
&= ~TWI_CNTR_M_STA
;
354 if (s
->cntr
& TWI_CNTR_M_STP
) {
356 i2c_end_transfer(s
->bus
);
357 s
->stat
= STAT_FROM_STA(STAT_IDLE
);
358 s
->cntr
&= ~TWI_CNTR_M_STP
;
361 if (!s
->irq_clear_inverted
&& !(s
->cntr
& TWI_CNTR_INT_FLAG
)) {
362 /* Write 0 to clear this flag */
363 qemu_irq_lower(s
->irq
);
364 } else if (s
->irq_clear_inverted
&& (s
->cntr
& TWI_CNTR_INT_FLAG
)) {
365 /* Write 1 to clear this flag */
366 s
->cntr
&= ~TWI_CNTR_INT_FLAG
;
367 qemu_irq_lower(s
->irq
);
370 if ((s
->cntr
& TWI_CNTR_A_ACK
) == 0) {
371 if (STAT_TO_STA(s
->stat
) == STAT_M_DATA_RX_ACK
) {
372 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_NACK
);
375 if (STAT_TO_STA(s
->stat
) == STAT_M_DATA_RX_NACK
) {
376 s
->stat
= STAT_FROM_STA(STAT_M_DATA_RX_ACK
);
379 allwinner_i2c_raise_interrupt(s
);
384 s
->ccr
= value
& TWI_CCR_MASK
;
387 if (((value
& TWI_SRST_MASK
) == 0) && (s
->srst
& TWI_SRST_MASK
)) {
389 allwinner_i2c_reset_hold(OBJECT(s
));
391 s
->srst
= value
& TWI_SRST_MASK
;
394 s
->efr
= value
& TWI_EFR_MASK
;
397 s
->lcr
= value
& TWI_LCR_MASK
;
400 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
401 HWADDR_PRIx
"\n", TYPE_AW_I2C
, __func__
, offset
);
406 static const MemoryRegionOps allwinner_i2c_ops
= {
407 .read
= allwinner_i2c_read
,
408 .write
= allwinner_i2c_write
,
409 .valid
.min_access_size
= 1,
410 .valid
.max_access_size
= 4,
411 .endianness
= DEVICE_NATIVE_ENDIAN
,
414 static const VMStateDescription allwinner_i2c_vmstate
= {
417 .minimum_version_id
= 1,
418 .fields
= (VMStateField
[]) {
419 VMSTATE_UINT8(addr
, AWI2CState
),
420 VMSTATE_UINT8(xaddr
, AWI2CState
),
421 VMSTATE_UINT8(data
, AWI2CState
),
422 VMSTATE_UINT8(cntr
, AWI2CState
),
423 VMSTATE_UINT8(ccr
, AWI2CState
),
424 VMSTATE_UINT8(srst
, AWI2CState
),
425 VMSTATE_UINT8(efr
, AWI2CState
),
426 VMSTATE_UINT8(lcr
, AWI2CState
),
427 VMSTATE_END_OF_LIST()
431 static void allwinner_i2c_realize(DeviceState
*dev
, Error
**errp
)
433 AWI2CState
*s
= AW_I2C(dev
);
435 memory_region_init_io(&s
->iomem
, OBJECT(s
), &allwinner_i2c_ops
, s
,
436 TYPE_AW_I2C
, AW_I2C_MEM_SIZE
);
437 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
438 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq
);
439 s
->bus
= i2c_init_bus(dev
, "i2c");
442 static void allwinner_i2c_class_init(ObjectClass
*klass
, void *data
)
444 DeviceClass
*dc
= DEVICE_CLASS(klass
);
445 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
447 rc
->phases
.hold
= allwinner_i2c_reset_hold
;
448 dc
->vmsd
= &allwinner_i2c_vmstate
;
449 dc
->realize
= allwinner_i2c_realize
;
450 dc
->desc
= "Allwinner I2C Controller";
453 static const TypeInfo allwinner_i2c_type_info
= {
455 .parent
= TYPE_SYS_BUS_DEVICE
,
456 .instance_size
= sizeof(AWI2CState
),
457 .class_init
= allwinner_i2c_class_init
,
460 static void allwinner_i2c_sun6i_init(Object
*obj
)
462 AWI2CState
*s
= AW_I2C(obj
);
464 s
->irq_clear_inverted
= true;
467 static const TypeInfo allwinner_i2c_sun6i_type_info
= {
468 .name
= TYPE_AW_I2C_SUN6I
,
469 .parent
= TYPE_AW_I2C
,
470 .instance_init
= allwinner_i2c_sun6i_init
,
473 static void allwinner_i2c_register_types(void)
475 type_register_static(&allwinner_i2c_type_info
);
476 type_register_static(&allwinner_i2c_sun6i_type_info
);
479 type_init(allwinner_i2c_register_types
)