2 * PPC4xx I2C controller emulation
4 * Copyright (c) 2007 Jocelyn Mayer
5 * Copyright (c) 2012 François Revol
6 * Copyright (c) 2016-2018 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 "qemu-common.h"
32 #include "hw/i2c/ppc4xx_i2c.h"
33 #include "bitbang_i2c.h"
35 #define PPC4xx_I2C_MEM_SIZE 18
56 #define IIC_CNTL_PT (1 << 0)
57 #define IIC_CNTL_READ (1 << 1)
58 #define IIC_CNTL_CHT (1 << 2)
59 #define IIC_CNTL_RPST (1 << 3)
60 #define IIC_CNTL_AMD (1 << 6)
61 #define IIC_CNTL_HMT (1 << 7)
63 #define IIC_MDCNTL_EINT (1 << 2)
64 #define IIC_MDCNTL_ESM (1 << 3)
65 #define IIC_MDCNTL_FMDB (1 << 6)
67 #define IIC_STS_PT (1 << 0)
68 #define IIC_STS_IRQA (1 << 1)
69 #define IIC_STS_ERR (1 << 2)
70 #define IIC_STS_MDBF (1 << 4)
71 #define IIC_STS_MDBS (1 << 5)
73 #define IIC_EXTSTS_XFRA (1 << 0)
74 #define IIC_EXTSTS_BCS_FREE (4 << 4)
75 #define IIC_EXTSTS_BCS_BUSY (5 << 4)
77 #define IIC_INTRMSK_EIMTC (1 << 0)
78 #define IIC_INTRMSK_EITA (1 << 1)
79 #define IIC_INTRMSK_EIIC (1 << 2)
80 #define IIC_INTRMSK_EIHE (1 << 3)
82 #define IIC_XTCNTLSS_SRST (1 << 0)
84 #define IIC_DIRECTCNTL_SDAC (1 << 3)
85 #define IIC_DIRECTCNTL_SCLC (1 << 2)
86 #define IIC_DIRECTCNTL_MSDA (1 << 1)
87 #define IIC_DIRECTCNTL_MSCL (1 << 0)
89 static void ppc4xx_i2c_reset(DeviceState
*s
)
91 PPC4xxI2CState
*i2c
= PPC4xx_I2C(s
);
94 memset(i2c
->mdata
, 0, ARRAY_SIZE(i2c
->mdata
));
95 /* [hl][ms]addr are not affected by reset */
99 i2c
->extsts
= IIC_EXTSTS_BCS_FREE
;
104 i2c
->directcntl
= 0xf; /* all non-reserved bits set */
107 static uint64_t ppc4xx_i2c_readb(void *opaque
, hwaddr addr
, unsigned int size
)
109 PPC4xxI2CState
*i2c
= PPC4xx_I2C(opaque
);
115 if (i2c
->mdidx
< 0) {
120 if (i2c
->mdidx
== 3) {
121 i2c
->sts
&= ~IIC_STS_MDBF
;
122 } else if (i2c
->mdidx
== 0) {
123 i2c
->sts
&= ~IIC_STS_MDBS
;
125 for (i
= 0; i
< i2c
->mdidx
; i
++) {
126 i2c
->mdata
[i
] = i2c
->mdata
[i
+ 1];
128 if (i2c
->mdidx
>= 0) {
148 ret
= i2c_bus_busy(i2c
->bus
) ?
149 IIC_EXTSTS_BCS_BUSY
: IIC_EXTSTS_BCS_FREE
;
170 ret
= i2c
->directcntl
;
173 if (addr
< PPC4xx_I2C_MEM_SIZE
) {
174 qemu_log_mask(LOG_UNIMP
, "%s: Unimplemented register 0x%"
175 HWADDR_PRIx
"\n", __func__
, addr
);
177 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address 0x%"
178 HWADDR_PRIx
"\n", __func__
, addr
);
186 static void ppc4xx_i2c_writeb(void *opaque
, hwaddr addr
, uint64_t value
,
189 PPC4xxI2CState
*i2c
= opaque
;
193 if (i2c
->mdidx
>= 3) {
196 i2c
->mdata
[++i2c
->mdidx
] = value
;
197 if (i2c
->mdidx
== 3) {
198 i2c
->sts
|= IIC_STS_MDBF
;
199 } else if (i2c
->mdidx
== 0) {
200 i2c
->sts
|= IIC_STS_MDBS
;
210 i2c
->cntl
= value
& ~IIC_CNTL_PT
;
211 if (value
& IIC_CNTL_AMD
) {
212 qemu_log_mask(LOG_UNIMP
, "%s: only 7 bit addresses supported\n",
215 if (value
& IIC_CNTL_HMT
&& i2c_bus_busy(i2c
->bus
)) {
216 i2c_end_transfer(i2c
->bus
);
217 if (i2c
->mdcntl
& IIC_MDCNTL_EINT
&&
218 i2c
->intrmsk
& IIC_INTRMSK_EIHE
) {
219 i2c
->sts
|= IIC_STS_IRQA
;
220 qemu_irq_raise(i2c
->irq
);
222 } else if (value
& IIC_CNTL_PT
) {
223 int recv
= (value
& IIC_CNTL_READ
) >> 1;
224 int tct
= value
>> 4 & 3;
227 if (recv
&& (i2c
->lmadr
>> 1) >= 0x50 && (i2c
->lmadr
>> 1) < 0x58) {
228 /* smbus emulation does not like multi byte reads w/o restart */
229 value
|= IIC_CNTL_RPST
;
232 for (i
= 0; i
<= tct
; i
++) {
233 if (!i2c_bus_busy(i2c
->bus
)) {
234 i2c
->extsts
= IIC_EXTSTS_BCS_FREE
;
235 if (i2c_start_transfer(i2c
->bus
, i2c
->lmadr
>> 1, recv
)) {
236 i2c
->sts
|= IIC_STS_ERR
;
237 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
240 i2c
->sts
&= ~IIC_STS_ERR
;
243 if (!(i2c
->sts
& IIC_STS_ERR
) &&
244 i2c_send_recv(i2c
->bus
, &i2c
->mdata
[i
], !recv
)) {
245 i2c
->sts
|= IIC_STS_ERR
;
246 i2c
->extsts
|= IIC_EXTSTS_XFRA
;
249 if (value
& IIC_CNTL_RPST
|| !(value
& IIC_CNTL_CHT
)) {
250 i2c_end_transfer(i2c
->bus
);
255 if (recv
&& i2c
->mdidx
>= 0) {
256 i2c
->sts
|= IIC_STS_MDBS
;
258 if (recv
&& i2c
->mdidx
== 3) {
259 i2c
->sts
|= IIC_STS_MDBF
;
261 if (i
&& i2c
->mdcntl
& IIC_MDCNTL_EINT
&&
262 i2c
->intrmsk
& IIC_INTRMSK_EIMTC
) {
263 i2c
->sts
|= IIC_STS_IRQA
;
264 qemu_irq_raise(i2c
->irq
);
269 i2c
->mdcntl
= value
& 0x3d;
270 if (value
& IIC_MDCNTL_ESM
) {
271 qemu_log_mask(LOG_UNIMP
, "%s: slave mode not implemented\n",
274 if (value
& IIC_MDCNTL_FMDB
) {
276 memset(i2c
->mdata
, 0, ARRAY_SIZE(i2c
->mdata
));
277 i2c
->sts
&= ~(IIC_STS_MDBF
| IIC_STS_MDBS
);
281 i2c
->sts
&= ~(value
& 0x0a);
282 if (value
& IIC_STS_IRQA
&& i2c
->mdcntl
& IIC_MDCNTL_EINT
) {
283 qemu_irq_lower(i2c
->irq
);
287 i2c
->extsts
&= ~(value
& 0x8f);
299 i2c
->intrmsk
= value
;
302 i2c
->xfrcnt
= value
& 0x77;
305 i2c
->xtcntlss
&= ~(value
& 0xf0);
306 if (value
& IIC_XTCNTLSS_SRST
) {
307 /* Is it actually a full reset? U-Boot sets some regs before */
308 ppc4xx_i2c_reset(DEVICE(i2c
));
313 i2c
->directcntl
= value
& (IIC_DIRECTCNTL_SDAC
& IIC_DIRECTCNTL_SCLC
);
314 i2c
->directcntl
|= (value
& IIC_DIRECTCNTL_SCLC
? 1 : 0);
315 bitbang_i2c_set(i2c
->bitbang
, BITBANG_I2C_SCL
,
316 i2c
->directcntl
& IIC_DIRECTCNTL_MSCL
);
317 i2c
->directcntl
|= bitbang_i2c_set(i2c
->bitbang
, BITBANG_I2C_SDA
,
318 (value
& IIC_DIRECTCNTL_SDAC
) != 0) << 1;
321 if (addr
< PPC4xx_I2C_MEM_SIZE
) {
322 qemu_log_mask(LOG_UNIMP
, "%s: Unimplemented register 0x%"
323 HWADDR_PRIx
"\n", __func__
, addr
);
325 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address 0x%"
326 HWADDR_PRIx
"\n", __func__
, addr
);
332 static const MemoryRegionOps ppc4xx_i2c_ops
= {
333 .read
= ppc4xx_i2c_readb
,
334 .write
= ppc4xx_i2c_writeb
,
335 .valid
.min_access_size
= 1,
336 .valid
.max_access_size
= 4,
337 .impl
.min_access_size
= 1,
338 .impl
.max_access_size
= 1,
339 .endianness
= DEVICE_NATIVE_ENDIAN
,
342 static void ppc4xx_i2c_init(Object
*o
)
344 PPC4xxI2CState
*s
= PPC4xx_I2C(o
);
346 memory_region_init_io(&s
->iomem
, OBJECT(s
), &ppc4xx_i2c_ops
, s
,
347 TYPE_PPC4xx_I2C
, PPC4xx_I2C_MEM_SIZE
);
348 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
349 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->irq
);
350 s
->bus
= i2c_init_bus(DEVICE(s
), "i2c");
351 s
->bitbang
= bitbang_i2c_init(s
->bus
);
354 static void ppc4xx_i2c_class_init(ObjectClass
*klass
, void *data
)
356 DeviceClass
*dc
= DEVICE_CLASS(klass
);
358 dc
->reset
= ppc4xx_i2c_reset
;
361 static const TypeInfo ppc4xx_i2c_type_info
= {
362 .name
= TYPE_PPC4xx_I2C
,
363 .parent
= TYPE_SYS_BUS_DEVICE
,
364 .instance_size
= sizeof(PPC4xxI2CState
),
365 .instance_init
= ppc4xx_i2c_init
,
366 .class_init
= ppc4xx_i2c_class_init
,
369 static void ppc4xx_i2c_register_types(void)
371 type_register_static(&ppc4xx_i2c_type_info
);
374 type_init(ppc4xx_i2c_register_types
)