mpc_i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "hw/sysbus.h"
  23. #include "migration/vmstate.h"
  24. #include "qom/object.h"
  25. #include "trace.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 dfsrr;
  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. trace_mpc_i2c_read(addr, value);
  199. return (uint64_t)value;
  200. }
  201. static void mpc_i2c_write(void *opaque, hwaddr addr,
  202. uint64_t value, unsigned size)
  203. {
  204. MPCI2CState *s = opaque;
  205. trace_mpc_i2c_write(addr, value);
  206. switch (addr) {
  207. case MPC_I2C_ADR:
  208. s->adr = value & CADR_MASK;
  209. break;
  210. case MPC_I2C_FDR:
  211. s->fdr = value & CFDR_MASK;
  212. break;
  213. case MPC_I2C_CR:
  214. if (mpc_i2c_is_enabled(s) && ((value & CCR_MEN) == 0)) {
  215. mpc_i2c_soft_reset(s);
  216. break;
  217. }
  218. /* normal write */
  219. s->cr = value & CCR_MASK;
  220. if (mpc_i2c_is_master(s)) { /* master mode */
  221. /* set the bus to busy after master is set as per RM */
  222. s->sr |= CSR_MBB;
  223. } else {
  224. /* bus is not busy anymore */
  225. s->sr &= ~CSR_MBB;
  226. /* Reset the address for fresh write/read cycle */
  227. if (s->address != CYCLE_RESET) {
  228. i2c_end_transfer(s->bus);
  229. s->address = CYCLE_RESET;
  230. }
  231. }
  232. /* For restart end the onging transfer */
  233. if (s->cr & CCR_RSTA) {
  234. if (s->address != CYCLE_RESET) {
  235. s->address = CYCLE_RESET;
  236. i2c_end_transfer(s->bus);
  237. s->cr &= ~CCR_RSTA;
  238. }
  239. }
  240. break;
  241. case MPC_I2C_SR:
  242. s->sr = value & CSR_MASK;
  243. /* Lower the interrupt */
  244. if (!(s->sr & CSR_MIF) || !(s->sr & CSR_MAL)) {
  245. mpc_i2c_irq(s);
  246. }
  247. break;
  248. case MPC_I2C_DR:
  249. /* if the device is not enabled, nothing to do */
  250. if (!mpc_i2c_is_enabled(s)) {
  251. break;
  252. }
  253. s->dr = value & CDR_MASK;
  254. if (mpc_i2c_is_master(s)) { /* master mode */
  255. if (s->address == CYCLE_RESET) {
  256. mpc_i2c_address_send(s);
  257. } else {
  258. mpc_i2c_data_send(s);
  259. }
  260. }
  261. break;
  262. case MPC_I2C_DFSRR:
  263. s->dfsrr = value;
  264. break;
  265. default:
  266. DPRINTF("ERROR: Bad write addr 0x%x\n", (unsigned int)addr);
  267. break;
  268. }
  269. }
  270. static const MemoryRegionOps i2c_ops = {
  271. .read = mpc_i2c_read,
  272. .write = mpc_i2c_write,
  273. .valid.max_access_size = 1,
  274. .endianness = DEVICE_NATIVE_ENDIAN,
  275. };
  276. static const VMStateDescription mpc_i2c_vmstate = {
  277. .name = TYPE_MPC_I2C,
  278. .version_id = 1,
  279. .minimum_version_id = 1,
  280. .fields = (const VMStateField[]) {
  281. VMSTATE_UINT8(address, MPCI2CState),
  282. VMSTATE_UINT8(adr, MPCI2CState),
  283. VMSTATE_UINT8(fdr, MPCI2CState),
  284. VMSTATE_UINT8(cr, MPCI2CState),
  285. VMSTATE_UINT8(sr, MPCI2CState),
  286. VMSTATE_UINT8(dr, MPCI2CState),
  287. VMSTATE_UINT8(dfsrr, MPCI2CState),
  288. VMSTATE_END_OF_LIST()
  289. }
  290. };
  291. static void mpc_i2c_realize(DeviceState *dev, Error **errp)
  292. {
  293. MPCI2CState *i2c = MPC_I2C(dev);
  294. sysbus_init_irq(SYS_BUS_DEVICE(dev), &i2c->irq);
  295. memory_region_init_io(&i2c->iomem, OBJECT(i2c), &i2c_ops, i2c,
  296. "mpc-i2c", 0x15);
  297. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &i2c->iomem);
  298. i2c->bus = i2c_init_bus(dev, "i2c");
  299. }
  300. static void mpc_i2c_class_init(ObjectClass *klass, void *data)
  301. {
  302. DeviceClass *dc = DEVICE_CLASS(klass);
  303. dc->vmsd = &mpc_i2c_vmstate ;
  304. device_class_set_legacy_reset(dc, mpc_i2c_reset);
  305. dc->realize = mpc_i2c_realize;
  306. dc->desc = "MPC I2C Controller";
  307. }
  308. static const TypeInfo mpc_i2c_types[] = {
  309. {
  310. .name = TYPE_MPC_I2C,
  311. .parent = TYPE_SYS_BUS_DEVICE,
  312. .instance_size = sizeof(MPCI2CState),
  313. .class_init = mpc_i2c_class_init,
  314. },
  315. };
  316. DEFINE_TYPES(mpc_i2c_types)