mpc_i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Amit Tomar, <Amit.Tomar@freescale.com>
  5. *
  6. * Description:
  7. * This file is derived from IMX I2C controller,
  8. * by Jean-Christophe DUBOIS .
  9. *
  10. * Thanks to Scott Wood and Alexander Graf for their kind help on this.
  11. *
  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.
  15. *
  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/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/i2c/i2c.h"
  21. #include "hw/irq.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "hw/sysbus.h"
  25. #include "migration/vmstate.h"
  26. #include "qom/object.h"
  27. /* #define DEBUG_I2C */
  28. #ifdef DEBUG_I2C
  29. #define DPRINTF(fmt, ...) \
  30. do { fprintf(stderr, "mpc_i2c[%s]: " fmt, __func__, ## __VA_ARGS__); \
  31. } while (0)
  32. #else
  33. #define DPRINTF(fmt, ...) do {} while (0)
  34. #endif
  35. #define TYPE_MPC_I2C "mpc-i2c"
  36. OBJECT_DECLARE_SIMPLE_TYPE(MPCI2CState, MPC_I2C)
  37. #define MPC_I2C_ADR 0x00
  38. #define MPC_I2C_FDR 0x04
  39. #define MPC_I2C_CR 0x08
  40. #define MPC_I2C_SR 0x0c
  41. #define MPC_I2C_DR 0x10
  42. #define MPC_I2C_DFSRR 0x14
  43. #define CCR_MEN (1 << 7)
  44. #define CCR_MIEN (1 << 6)
  45. #define CCR_MSTA (1 << 5)
  46. #define CCR_MTX (1 << 4)
  47. #define CCR_TXAK (1 << 3)
  48. #define CCR_RSTA (1 << 2)
  49. #define CCR_BCST (1 << 0)
  50. #define CSR_MCF (1 << 7)
  51. #define CSR_MAAS (1 << 6)
  52. #define CSR_MBB (1 << 5)
  53. #define CSR_MAL (1 << 4)
  54. #define CSR_SRW (1 << 2)
  55. #define CSR_MIF (1 << 1)
  56. #define CSR_RXAK (1 << 0)
  57. #define CADR_MASK 0xFE
  58. #define CFDR_MASK 0x3F
  59. #define CCR_MASK 0xFC
  60. #define CSR_MASK 0xED
  61. #define CDR_MASK 0xFF
  62. #define CYCLE_RESET 0xFF
  63. struct MPCI2CState {
  64. SysBusDevice parent_obj;
  65. I2CBus *bus;
  66. qemu_irq irq;
  67. MemoryRegion iomem;
  68. uint8_t address;
  69. uint8_t adr;
  70. uint8_t fdr;
  71. uint8_t cr;
  72. uint8_t sr;
  73. uint8_t dr;
  74. uint8_t dfssr;
  75. };
  76. static bool mpc_i2c_is_enabled(MPCI2CState *s)
  77. {
  78. return s->cr & CCR_MEN;
  79. }
  80. static bool mpc_i2c_is_master(MPCI2CState *s)
  81. {
  82. return s->cr & CCR_MSTA;
  83. }
  84. static bool mpc_i2c_direction_is_tx(MPCI2CState *s)
  85. {
  86. return s->cr & CCR_MTX;
  87. }
  88. static bool mpc_i2c_irq_pending(MPCI2CState *s)
  89. {
  90. return s->sr & CSR_MIF;
  91. }
  92. static bool mpc_i2c_irq_is_enabled(MPCI2CState *s)
  93. {
  94. return s->cr & CCR_MIEN;
  95. }
  96. static void mpc_i2c_reset(DeviceState *dev)
  97. {
  98. MPCI2CState *i2c = MPC_I2C(dev);
  99. i2c->address = 0xFF;
  100. i2c->adr = 0x00;
  101. i2c->fdr = 0x00;
  102. i2c->cr = 0x00;
  103. i2c->sr = 0x81;
  104. i2c->dr = 0x00;
  105. }
  106. static void mpc_i2c_irq(MPCI2CState *s)
  107. {
  108. bool irq_active = false;
  109. if (mpc_i2c_is_enabled(s) && mpc_i2c_irq_is_enabled(s)
  110. && mpc_i2c_irq_pending(s)) {
  111. irq_active = true;
  112. }
  113. if (irq_active) {
  114. qemu_irq_raise(s->irq);
  115. } else {
  116. qemu_irq_lower(s->irq);
  117. }
  118. }
  119. static void mpc_i2c_soft_reset(MPCI2CState *s)
  120. {
  121. /* This is a soft reset. ADR is preserved during soft resets */
  122. uint8_t adr = s->adr;
  123. mpc_i2c_reset(DEVICE(s));
  124. s->adr = adr;
  125. }
  126. static void mpc_i2c_address_send(MPCI2CState *s)
  127. {
  128. /* if returns non zero slave address is not right */
  129. if (i2c_start_transfer(s->bus, s->dr >> 1, s->dr & (0x01))) {
  130. s->sr |= CSR_RXAK;
  131. } else {
  132. s->address = s->dr;
  133. s->sr &= ~CSR_RXAK;
  134. s->sr |= CSR_MCF; /* Set after Byte Transfer is completed */
  135. s->sr |= CSR_MIF; /* Set after Byte Transfer is completed */
  136. mpc_i2c_irq(s);
  137. }
  138. }
  139. static void mpc_i2c_data_send(MPCI2CState *s)
  140. {
  141. if (i2c_send(s->bus, s->dr)) {
  142. /* End of transfer */
  143. s->sr |= CSR_RXAK;
  144. i2c_end_transfer(s->bus);
  145. } else {
  146. s->sr &= ~CSR_RXAK;
  147. s->sr |= CSR_MCF; /* Set after Byte Transfer is completed */
  148. s->sr |= CSR_MIF; /* Set after Byte Transfer is completed */
  149. mpc_i2c_irq(s);
  150. }
  151. }
  152. static void mpc_i2c_data_recive(MPCI2CState *s)
  153. {
  154. int ret;
  155. /* get the next byte */
  156. ret = i2c_recv(s->bus);
  157. if (ret >= 0) {
  158. s->sr |= CSR_MCF; /* Set after Byte Transfer is completed */
  159. s->sr |= CSR_MIF; /* Set after Byte Transfer is completed */
  160. mpc_i2c_irq(s);
  161. } else {
  162. DPRINTF("read failed for device");
  163. ret = 0xff;
  164. }
  165. s->dr = ret;
  166. }
  167. static uint64_t mpc_i2c_read(void *opaque, hwaddr addr, unsigned size)
  168. {
  169. MPCI2CState *s = opaque;
  170. uint8_t value;
  171. switch (addr) {
  172. case MPC_I2C_ADR:
  173. value = s->adr;
  174. break;
  175. case MPC_I2C_FDR:
  176. value = s->fdr;
  177. break;
  178. case MPC_I2C_CR:
  179. value = s->cr;
  180. break;
  181. case MPC_I2C_SR:
  182. value = s->sr;
  183. break;
  184. case MPC_I2C_DR:
  185. value = s->dr;
  186. if (mpc_i2c_is_master(s)) { /* master mode */
  187. if (mpc_i2c_direction_is_tx(s)) {
  188. DPRINTF("MTX is set not in recv mode\n");
  189. } else {
  190. mpc_i2c_data_recive(s);
  191. }
  192. }
  193. break;
  194. default:
  195. value = 0;
  196. DPRINTF("ERROR: Bad read addr 0x%x\n", (unsigned int)addr);
  197. break;
  198. }
  199. DPRINTF("%s: addr " TARGET_FMT_plx " %02" PRIx32 "\n", __func__,
  200. addr, value);
  201. return (uint64_t)value;
  202. }
  203. static void mpc_i2c_write(void *opaque, hwaddr addr,
  204. uint64_t value, unsigned size)
  205. {
  206. MPCI2CState *s = opaque;
  207. DPRINTF("%s: addr " TARGET_FMT_plx " val %08" PRIx64 "\n", __func__,
  208. addr, value);
  209. switch (addr) {
  210. case MPC_I2C_ADR:
  211. s->adr = value & CADR_MASK;
  212. break;
  213. case MPC_I2C_FDR:
  214. s->fdr = value & CFDR_MASK;
  215. break;
  216. case MPC_I2C_CR:
  217. if (mpc_i2c_is_enabled(s) && ((value & CCR_MEN) == 0)) {
  218. mpc_i2c_soft_reset(s);
  219. break;
  220. }
  221. /* normal write */
  222. s->cr = value & CCR_MASK;
  223. if (mpc_i2c_is_master(s)) { /* master mode */
  224. /* set the bus to busy after master is set as per RM */
  225. s->sr |= CSR_MBB;
  226. } else {
  227. /* bus is not busy anymore */
  228. s->sr &= ~CSR_MBB;
  229. /* Reset the address for fresh write/read cycle */
  230. if (s->address != CYCLE_RESET) {
  231. i2c_end_transfer(s->bus);
  232. s->address = CYCLE_RESET;
  233. }
  234. }
  235. /* For restart end the onging transfer */
  236. if (s->cr & CCR_RSTA) {
  237. if (s->address != CYCLE_RESET) {
  238. s->address = CYCLE_RESET;
  239. i2c_end_transfer(s->bus);
  240. s->cr &= ~CCR_RSTA;
  241. }
  242. }
  243. break;
  244. case MPC_I2C_SR:
  245. s->sr = value & CSR_MASK;
  246. /* Lower the interrupt */
  247. if (!(s->sr & CSR_MIF) || !(s->sr & CSR_MAL)) {
  248. mpc_i2c_irq(s);
  249. }
  250. break;
  251. case MPC_I2C_DR:
  252. /* if the device is not enabled, nothing to do */
  253. if (!mpc_i2c_is_enabled(s)) {
  254. break;
  255. }
  256. s->dr = value & CDR_MASK;
  257. if (mpc_i2c_is_master(s)) { /* master mode */
  258. if (s->address == CYCLE_RESET) {
  259. mpc_i2c_address_send(s);
  260. } else {
  261. mpc_i2c_data_send(s);
  262. }
  263. }
  264. break;
  265. case MPC_I2C_DFSRR:
  266. s->dfssr = value;
  267. break;
  268. default:
  269. DPRINTF("ERROR: Bad write addr 0x%x\n", (unsigned int)addr);
  270. break;
  271. }
  272. }
  273. static const MemoryRegionOps i2c_ops = {
  274. .read = mpc_i2c_read,
  275. .write = mpc_i2c_write,
  276. .valid.max_access_size = 1,
  277. .endianness = DEVICE_NATIVE_ENDIAN,
  278. };
  279. static const VMStateDescription mpc_i2c_vmstate = {
  280. .name = TYPE_MPC_I2C,
  281. .version_id = 1,
  282. .minimum_version_id = 1,
  283. .fields = (VMStateField[]) {
  284. VMSTATE_UINT8(address, MPCI2CState),
  285. VMSTATE_UINT8(adr, MPCI2CState),
  286. VMSTATE_UINT8(fdr, MPCI2CState),
  287. VMSTATE_UINT8(cr, MPCI2CState),
  288. VMSTATE_UINT8(sr, MPCI2CState),
  289. VMSTATE_UINT8(dr, MPCI2CState),
  290. VMSTATE_UINT8(dfssr, MPCI2CState),
  291. VMSTATE_END_OF_LIST()
  292. }
  293. };
  294. static void mpc_i2c_realize(DeviceState *dev, Error **errp)
  295. {
  296. MPCI2CState *i2c = MPC_I2C(dev);
  297. sysbus_init_irq(SYS_BUS_DEVICE(dev), &i2c->irq);
  298. memory_region_init_io(&i2c->iomem, OBJECT(i2c), &i2c_ops, i2c,
  299. "mpc-i2c", 0x14);
  300. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &i2c->iomem);
  301. i2c->bus = i2c_init_bus(dev, "i2c");
  302. }
  303. static void mpc_i2c_class_init(ObjectClass *klass, void *data)
  304. {
  305. DeviceClass *dc = DEVICE_CLASS(klass);
  306. dc->vmsd = &mpc_i2c_vmstate ;
  307. dc->reset = mpc_i2c_reset;
  308. dc->realize = mpc_i2c_realize;
  309. dc->desc = "MPC I2C Controller";
  310. }
  311. static const TypeInfo mpc_i2c_type_info = {
  312. .name = TYPE_MPC_I2C,
  313. .parent = TYPE_SYS_BUS_DEVICE,
  314. .instance_size = sizeof(MPCI2CState),
  315. .class_init = mpc_i2c_class_init,
  316. };
  317. static void mpc_i2c_register_types(void)
  318. {
  319. type_register_static(&mpc_i2c_type_info);
  320. }
  321. type_init(mpc_i2c_register_types)