mpc_i2c.c 9.0 KB

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