2 * PPC4xx I2C controller emulation
4 * Copyright (c) 2007 Jocelyn Mayer
5 * Copyright (c) 2012 François Revol
6 * Copyright (c) 2016 BALATON Zoltan
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "qemu-common.h"
33 #include "hw/i2c/ppc4xx_i2c.h"
35 #define PPC4xx_I2C_MEM_SIZE 0x12
37 #define IIC_CNTL_PT (1 << 0)
38 #define IIC_CNTL_READ (1 << 1)
39 #define IIC_CNTL_CHT (1 << 2)
40 #define IIC_CNTL_RPST (1 << 3)
42 #define IIC_STS_PT (1 << 0)
43 #define IIC_STS_ERR (1 << 2)
44 #define IIC_STS_MDBS (1 << 5)
46 #define IIC_EXTSTS_XFRA (1 << 0)
48 #define IIC_XTCNTLSS_SRST (1 << 0)
50 static void ppc4xx_i2c_reset(DeviceState
*s
)
52 PPC4xxI2CState
*i2c
= PPC4xx_I2C(s
);
54 /* FIXME: Should also reset bus?
55 *if (s->address != ADDR_RESET) {
56 * i2c_end_transfer(s->bus);
74 i2c
->directcntl
= 0x0f;
78 static inline bool ppc4xx_i2c_is_master(PPC4xxI2CState
*i2c
)
83 static uint64_t ppc4xx_i2c_readb(void *opaque
, hwaddr addr
, unsigned int size
)
85 PPC4xxI2CState
*i2c
= PPC4xx_I2C(opaque
);
91 if (ppc4xx_i2c_is_master(i2c
)) {
94 if (!(i2c
->sts
& IIC_STS_MDBS
)) {
95 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Trying to read "
96 "without starting transfer\n",
97 TYPE_PPC4xx_I2C
, __func__
);
99 int pending
= (i2c
->cntl
>> 4) & 3;
101 /* get the next byte */
102 int byte
= i2c_recv(i2c
->bus
);
105 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: read failed "
106 "for device 0x%02x\n", TYPE_PPC4xx_I2C
,
107 __func__
, i2c
->lmadr
);
111 /* Raise interrupt if enabled */
112 /*ppc4xx_i2c_raise_interrupt(i2c)*/;
116 i2c
->sts
&= ~IIC_STS_MDBS
;
117 /*i2c_end_transfer(i2c->bus);*/
118 /*} else if (i2c->cntl & (IIC_CNTL_RPST | IIC_CNTL_CHT)) {*/
119 } else if (pending
) {
120 /* current smbus implementation doesn't like
121 multibyte xfer repeated start */
122 i2c_end_transfer(i2c
->bus
);
123 if (i2c_start_transfer(i2c
->bus
, i2c
->lmadr
>> 1, 1)) {
124 /* if non zero is returned, the adress is not valid */
125 i2c
->sts
&= ~IIC_STS_PT
;
126 i2c
->sts
|= IIC_STS_ERR
;
127 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
129 /*i2c->sts |= IIC_STS_PT;*/
130 i2c
->sts
|= IIC_STS_MDBS
;
131 i2c
->sts
&= ~IIC_STS_ERR
;
136 i2c
->cntl
= (i2c
->cntl
& 0xcf) | (pending
<< 4);
139 qemu_log_mask(LOG_UNIMP
, "[%s]%s: slave mode not implemented\n",
140 TYPE_PPC4xx_I2C
, __func__
);
183 ret
= i2c
->directcntl
;
189 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
190 HWADDR_PRIx
"\n", TYPE_PPC4xx_I2C
, __func__
, addr
);
198 static void ppc4xx_i2c_writeb(void *opaque
, hwaddr addr
, uint64_t value
,
201 PPC4xxI2CState
*i2c
= opaque
;
206 if (!i2c_bus_busy(i2c
->bus
)) {
207 /* assume we start a write transfer */
208 if (i2c_start_transfer(i2c
->bus
, i2c
->lmadr
>> 1, 0)) {
209 /* if non zero is returned, the adress is not valid */
210 i2c
->sts
&= ~IIC_STS_PT
;
211 i2c
->sts
|= IIC_STS_ERR
;
212 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
214 i2c
->sts
|= IIC_STS_PT
;
215 i2c
->sts
&= ~IIC_STS_ERR
;
219 if (i2c_bus_busy(i2c
->bus
)) {
220 if (i2c_send(i2c
->bus
, i2c
->mdata
)) {
221 /* if the target return non zero then end the transfer */
222 i2c
->sts
&= ~IIC_STS_PT
;
223 i2c
->sts
|= IIC_STS_ERR
;
224 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
225 i2c_end_transfer(i2c
->bus
);
234 if (i2c_bus_busy(i2c
->bus
)) {
235 i2c_end_transfer(i2c
->bus
);
243 if (i2c
->cntl
& IIC_CNTL_PT
) {
244 if (i2c
->cntl
& IIC_CNTL_READ
) {
245 if (i2c_bus_busy(i2c
->bus
)) {
246 /* end previous transfer */
247 i2c
->sts
&= ~IIC_STS_PT
;
248 i2c_end_transfer(i2c
->bus
);
250 if (i2c_start_transfer(i2c
->bus
, i2c
->lmadr
>> 1, 1)) {
251 /* if non zero is returned, the adress is not valid */
252 i2c
->sts
&= ~IIC_STS_PT
;
253 i2c
->sts
|= IIC_STS_ERR
;
254 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
256 /*i2c->sts |= IIC_STS_PT;*/
257 i2c
->sts
|= IIC_STS_MDBS
;
258 i2c
->sts
&= ~IIC_STS_ERR
;
262 /* we actually already did the write transfer... */
263 i2c
->sts
&= ~IIC_STS_PT
;
268 i2c
->mdcntl
= value
& 0xDF;
271 i2c
->sts
&= ~(value
& 0x0A);
274 i2c
->extsts
&= ~(value
& 0x8F);
278 /*i2c_set_slave_address(i2c->bus, i2c->lsadr);*/
287 i2c
->intrmsk
= value
;
290 i2c
->xfrcnt
= value
& 0x77;
293 if (value
& IIC_XTCNTLSS_SRST
) {
294 /* Is it actually a full reset? U-Boot sets some regs before */
295 ppc4xx_i2c_reset(DEVICE(i2c
));
298 i2c
->xtcntlss
= value
;
301 i2c
->directcntl
= value
& 0x7;
307 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
308 HWADDR_PRIx
"\n", TYPE_PPC4xx_I2C
, __func__
, addr
);
313 static const MemoryRegionOps ppc4xx_i2c_ops
= {
314 .read
= ppc4xx_i2c_readb
,
315 .write
= ppc4xx_i2c_writeb
,
316 .valid
.min_access_size
= 1,
317 .valid
.max_access_size
= 4,
318 .impl
.min_access_size
= 1,
319 .impl
.max_access_size
= 1,
320 .endianness
= DEVICE_NATIVE_ENDIAN
,
323 static void ppc4xx_i2c_init(Object
*o
)
325 PPC4xxI2CState
*s
= PPC4xx_I2C(o
);
327 memory_region_init_io(&s
->iomem
, OBJECT(s
), &ppc4xx_i2c_ops
, s
,
328 TYPE_PPC4xx_I2C
, PPC4xx_I2C_MEM_SIZE
);
329 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
330 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->irq
);
331 s
->bus
= i2c_init_bus(DEVICE(s
), "i2c");
334 static void ppc4xx_i2c_class_init(ObjectClass
*klass
, void *data
)
336 DeviceClass
*dc
= DEVICE_CLASS(klass
);
338 dc
->reset
= ppc4xx_i2c_reset
;
341 static const TypeInfo ppc4xx_i2c_type_info
= {
342 .name
= TYPE_PPC4xx_I2C
,
343 .parent
= TYPE_SYS_BUS_DEVICE
,
344 .instance_size
= sizeof(PPC4xxI2CState
),
345 .instance_init
= ppc4xx_i2c_init
,
346 .class_init
= ppc4xx_i2c_class_init
,
349 static void ppc4xx_i2c_register_types(void)
351 type_register_static(&ppc4xx_i2c_type_info
);
354 type_init(ppc4xx_i2c_register_types
)