2 * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Amit Tomar, <Amit.Tomar@freescale.com>
7 * This file is derived from IMX I2C controller,
8 * by Jean-Christophe DUBOIS .
10 * Thanks to Scott Wood and Alexander Graf for their kind help on this.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2 or later,
14 * as published by the Free Software Foundation.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "hw/i2c/i2c.h"
23 #include "qemu/module.h"
24 #include "hw/sysbus.h"
25 #include "migration/vmstate.h"
26 #include "qom/object.h"
28 /* #define DEBUG_I2C */
31 #define DPRINTF(fmt, ...) \
32 do { fprintf(stderr, "mpc_i2c[%s]: " fmt, __func__, ## __VA_ARGS__); \
35 #define DPRINTF(fmt, ...) do {} while (0)
38 #define TYPE_MPC_I2C "mpc-i2c"
39 OBJECT_DECLARE_SIMPLE_TYPE(MPCI2CState
, MPC_I2C
)
41 #define MPC_I2C_ADR 0x00
42 #define MPC_I2C_FDR 0x04
43 #define MPC_I2C_CR 0x08
44 #define MPC_I2C_SR 0x0c
45 #define MPC_I2C_DR 0x10
46 #define MPC_I2C_DFSRR 0x14
48 #define CCR_MEN (1 << 7)
49 #define CCR_MIEN (1 << 6)
50 #define CCR_MSTA (1 << 5)
51 #define CCR_MTX (1 << 4)
52 #define CCR_TXAK (1 << 3)
53 #define CCR_RSTA (1 << 2)
54 #define CCR_BCST (1 << 0)
56 #define CSR_MCF (1 << 7)
57 #define CSR_MAAS (1 << 6)
58 #define CSR_MBB (1 << 5)
59 #define CSR_MAL (1 << 4)
60 #define CSR_SRW (1 << 2)
61 #define CSR_MIF (1 << 1)
62 #define CSR_RXAK (1 << 0)
64 #define CADR_MASK 0xFE
65 #define CFDR_MASK 0x3F
70 #define CYCLE_RESET 0xFF
73 SysBusDevice parent_obj
;
88 static bool mpc_i2c_is_enabled(MPCI2CState
*s
)
90 return s
->cr
& CCR_MEN
;
93 static bool mpc_i2c_is_master(MPCI2CState
*s
)
95 return s
->cr
& CCR_MSTA
;
98 static bool mpc_i2c_direction_is_tx(MPCI2CState
*s
)
100 return s
->cr
& CCR_MTX
;
103 static bool mpc_i2c_irq_pending(MPCI2CState
*s
)
105 return s
->sr
& CSR_MIF
;
108 static bool mpc_i2c_irq_is_enabled(MPCI2CState
*s
)
110 return s
->cr
& CCR_MIEN
;
113 static void mpc_i2c_reset(DeviceState
*dev
)
115 MPCI2CState
*i2c
= MPC_I2C(dev
);
125 static void mpc_i2c_irq(MPCI2CState
*s
)
127 bool irq_active
= false;
129 if (mpc_i2c_is_enabled(s
) && mpc_i2c_irq_is_enabled(s
)
130 && mpc_i2c_irq_pending(s
)) {
135 qemu_irq_raise(s
->irq
);
137 qemu_irq_lower(s
->irq
);
141 static void mpc_i2c_soft_reset(MPCI2CState
*s
)
143 /* This is a soft reset. ADR is preserved during soft resets */
144 uint8_t adr
= s
->adr
;
145 mpc_i2c_reset(DEVICE(s
));
149 static void mpc_i2c_address_send(MPCI2CState
*s
)
151 /* if returns non zero slave address is not right */
152 if (i2c_start_transfer(s
->bus
, s
->dr
>> 1, s
->dr
& (0x01))) {
157 s
->sr
|= CSR_MCF
; /* Set after Byte Transfer is completed */
158 s
->sr
|= CSR_MIF
; /* Set after Byte Transfer is completed */
163 static void mpc_i2c_data_send(MPCI2CState
*s
)
165 if (i2c_send(s
->bus
, s
->dr
)) {
166 /* End of transfer */
168 i2c_end_transfer(s
->bus
);
171 s
->sr
|= CSR_MCF
; /* Set after Byte Transfer is completed */
172 s
->sr
|= CSR_MIF
; /* Set after Byte Transfer is completed */
177 static void mpc_i2c_data_recive(MPCI2CState
*s
)
180 /* get the next byte */
181 ret
= i2c_recv(s
->bus
);
183 s
->sr
|= CSR_MCF
; /* Set after Byte Transfer is completed */
184 s
->sr
|= CSR_MIF
; /* Set after Byte Transfer is completed */
187 DPRINTF("read failed for device");
193 static uint64_t mpc_i2c_read(void *opaque
, hwaddr addr
, unsigned size
)
195 MPCI2CState
*s
= opaque
;
213 if (mpc_i2c_is_master(s
)) { /* master mode */
214 if (mpc_i2c_direction_is_tx(s
)) {
215 DPRINTF("MTX is set not in recv mode\n");
217 mpc_i2c_data_recive(s
);
223 DPRINTF("ERROR: Bad read addr 0x%x\n", (unsigned int)addr
);
227 DPRINTF("%s: addr " HWADDR_FMT_plx
" %02" PRIx32
"\n", __func__
,
229 return (uint64_t)value
;
232 static void mpc_i2c_write(void *opaque
, hwaddr addr
,
233 uint64_t value
, unsigned size
)
235 MPCI2CState
*s
= opaque
;
237 DPRINTF("%s: addr " HWADDR_FMT_plx
" val %08" PRIx64
"\n", __func__
,
241 s
->adr
= value
& CADR_MASK
;
244 s
->fdr
= value
& CFDR_MASK
;
247 if (mpc_i2c_is_enabled(s
) && ((value
& CCR_MEN
) == 0)) {
248 mpc_i2c_soft_reset(s
);
252 s
->cr
= value
& CCR_MASK
;
253 if (mpc_i2c_is_master(s
)) { /* master mode */
254 /* set the bus to busy after master is set as per RM */
257 /* bus is not busy anymore */
259 /* Reset the address for fresh write/read cycle */
260 if (s
->address
!= CYCLE_RESET
) {
261 i2c_end_transfer(s
->bus
);
262 s
->address
= CYCLE_RESET
;
265 /* For restart end the onging transfer */
266 if (s
->cr
& CCR_RSTA
) {
267 if (s
->address
!= CYCLE_RESET
) {
268 s
->address
= CYCLE_RESET
;
269 i2c_end_transfer(s
->bus
);
275 s
->sr
= value
& CSR_MASK
;
276 /* Lower the interrupt */
277 if (!(s
->sr
& CSR_MIF
) || !(s
->sr
& CSR_MAL
)) {
282 /* if the device is not enabled, nothing to do */
283 if (!mpc_i2c_is_enabled(s
)) {
286 s
->dr
= value
& CDR_MASK
;
287 if (mpc_i2c_is_master(s
)) { /* master mode */
288 if (s
->address
== CYCLE_RESET
) {
289 mpc_i2c_address_send(s
);
291 mpc_i2c_data_send(s
);
299 DPRINTF("ERROR: Bad write addr 0x%x\n", (unsigned int)addr
);
304 static const MemoryRegionOps i2c_ops
= {
305 .read
= mpc_i2c_read
,
306 .write
= mpc_i2c_write
,
307 .valid
.max_access_size
= 1,
308 .endianness
= DEVICE_NATIVE_ENDIAN
,
311 static const VMStateDescription mpc_i2c_vmstate
= {
312 .name
= TYPE_MPC_I2C
,
314 .minimum_version_id
= 1,
315 .fields
= (const VMStateField
[]) {
316 VMSTATE_UINT8(address
, MPCI2CState
),
317 VMSTATE_UINT8(adr
, MPCI2CState
),
318 VMSTATE_UINT8(fdr
, MPCI2CState
),
319 VMSTATE_UINT8(cr
, MPCI2CState
),
320 VMSTATE_UINT8(sr
, MPCI2CState
),
321 VMSTATE_UINT8(dr
, MPCI2CState
),
322 VMSTATE_UINT8(dfsrr
, MPCI2CState
),
323 VMSTATE_END_OF_LIST()
327 static void mpc_i2c_realize(DeviceState
*dev
, Error
**errp
)
329 MPCI2CState
*i2c
= MPC_I2C(dev
);
330 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &i2c
->irq
);
331 memory_region_init_io(&i2c
->iomem
, OBJECT(i2c
), &i2c_ops
, i2c
,
333 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &i2c
->iomem
);
334 i2c
->bus
= i2c_init_bus(dev
, "i2c");
337 static void mpc_i2c_class_init(ObjectClass
*klass
, void *data
)
339 DeviceClass
*dc
= DEVICE_CLASS(klass
);
341 dc
->vmsd
= &mpc_i2c_vmstate
;
342 device_class_set_legacy_reset(dc
, mpc_i2c_reset
);
343 dc
->realize
= mpc_i2c_realize
;
344 dc
->desc
= "MPC I2C Controller";
347 static const TypeInfo mpc_i2c_type_info
= {
348 .name
= TYPE_MPC_I2C
,
349 .parent
= TYPE_SYS_BUS_DEVICE
,
350 .instance_size
= sizeof(MPCI2CState
),
351 .class_init
= mpc_i2c_class_init
,
354 static void mpc_i2c_register_types(void)
356 type_register_static(&mpc_i2c_type_info
);
359 type_init(mpc_i2c_register_types
)