exynos4210_i2c.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Exynos4210 I2C Bus Serial Interface Emulation
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co Ltd.
  5. * Maksim Kozlov, <m.kozlov@samsung.com>
  6. * Igor Mitsyanko, <i.mitsyanko@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/module.h"
  24. #include "qemu/timer.h"
  25. #include "hw/sysbus.h"
  26. #include "migration/vmstate.h"
  27. #include "hw/i2c/i2c.h"
  28. #include "hw/irq.h"
  29. #include "qom/object.h"
  30. #ifndef EXYNOS4_I2C_DEBUG
  31. #define EXYNOS4_I2C_DEBUG 0
  32. #endif
  33. #define TYPE_EXYNOS4_I2C "exynos4210.i2c"
  34. OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210I2CState, EXYNOS4_I2C)
  35. /* Exynos4210 I2C memory map */
  36. #define EXYNOS4_I2C_MEM_SIZE 0x14
  37. #define I2CCON_ADDR 0x00 /* control register */
  38. #define I2CSTAT_ADDR 0x04 /* control/status register */
  39. #define I2CADD_ADDR 0x08 /* address register */
  40. #define I2CDS_ADDR 0x0c /* data shift register */
  41. #define I2CLC_ADDR 0x10 /* line control register */
  42. #define I2CCON_ACK_GEN (1 << 7)
  43. #define I2CCON_INTRS_EN (1 << 5)
  44. #define I2CCON_INT_PEND (1 << 4)
  45. #define EXYNOS4_I2C_MODE(reg) (((reg) >> 6) & 3)
  46. #define I2C_IN_MASTER_MODE(reg) (((reg) >> 6) & 2)
  47. #define I2CMODE_MASTER_Rx 0x2
  48. #define I2CMODE_MASTER_Tx 0x3
  49. #define I2CSTAT_LAST_BIT (1 << 0)
  50. #define I2CSTAT_OUTPUT_EN (1 << 4)
  51. #define I2CSTAT_START_BUSY (1 << 5)
  52. #if EXYNOS4_I2C_DEBUG
  53. #define DPRINT(fmt, args...) \
  54. do { fprintf(stderr, "QEMU I2C: "fmt, ## args); } while (0)
  55. static const char *exynos4_i2c_get_regname(unsigned offset)
  56. {
  57. switch (offset) {
  58. case I2CCON_ADDR:
  59. return "I2CCON";
  60. case I2CSTAT_ADDR:
  61. return "I2CSTAT";
  62. case I2CADD_ADDR:
  63. return "I2CADD";
  64. case I2CDS_ADDR:
  65. return "I2CDS";
  66. case I2CLC_ADDR:
  67. return "I2CLC";
  68. default:
  69. return "[?]";
  70. }
  71. }
  72. #else
  73. #define DPRINT(fmt, args...) do { } while (0)
  74. #endif
  75. struct Exynos4210I2CState {
  76. SysBusDevice parent_obj;
  77. MemoryRegion iomem;
  78. I2CBus *bus;
  79. qemu_irq irq;
  80. uint8_t i2ccon;
  81. uint8_t i2cstat;
  82. uint8_t i2cadd;
  83. uint8_t i2cds;
  84. uint8_t i2clc;
  85. bool scl_free;
  86. };
  87. static inline void exynos4210_i2c_raise_interrupt(Exynos4210I2CState *s)
  88. {
  89. if (s->i2ccon & I2CCON_INTRS_EN) {
  90. s->i2ccon |= I2CCON_INT_PEND;
  91. qemu_irq_raise(s->irq);
  92. }
  93. }
  94. static void exynos4210_i2c_data_receive(void *opaque)
  95. {
  96. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  97. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  98. s->scl_free = false;
  99. s->i2cds = i2c_recv(s->bus);
  100. exynos4210_i2c_raise_interrupt(s);
  101. }
  102. static void exynos4210_i2c_data_send(void *opaque)
  103. {
  104. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  105. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  106. s->scl_free = false;
  107. if (i2c_send(s->bus, s->i2cds) < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
  108. s->i2cstat |= I2CSTAT_LAST_BIT;
  109. }
  110. exynos4210_i2c_raise_interrupt(s);
  111. }
  112. static uint64_t exynos4210_i2c_read(void *opaque, hwaddr offset,
  113. unsigned size)
  114. {
  115. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  116. uint8_t value;
  117. switch (offset) {
  118. case I2CCON_ADDR:
  119. value = s->i2ccon;
  120. break;
  121. case I2CSTAT_ADDR:
  122. value = s->i2cstat;
  123. break;
  124. case I2CADD_ADDR:
  125. value = s->i2cadd;
  126. break;
  127. case I2CDS_ADDR:
  128. value = s->i2cds;
  129. s->scl_free = true;
  130. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Rx &&
  131. (s->i2cstat & I2CSTAT_START_BUSY) &&
  132. !(s->i2ccon & I2CCON_INT_PEND)) {
  133. exynos4210_i2c_data_receive(s);
  134. }
  135. break;
  136. case I2CLC_ADDR:
  137. value = s->i2clc;
  138. break;
  139. default:
  140. value = 0;
  141. DPRINT("ERROR: Bad read offset 0x%x\n", (unsigned int)offset);
  142. break;
  143. }
  144. DPRINT("read %s [0x%02x] -> 0x%02x\n", exynos4_i2c_get_regname(offset),
  145. (unsigned int)offset, value);
  146. return value;
  147. }
  148. static void exynos4210_i2c_write(void *opaque, hwaddr offset,
  149. uint64_t value, unsigned size)
  150. {
  151. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  152. uint8_t v = value & 0xff;
  153. DPRINT("write %s [0x%02x] <- 0x%02x\n", exynos4_i2c_get_regname(offset),
  154. (unsigned int)offset, v);
  155. switch (offset) {
  156. case I2CCON_ADDR:
  157. s->i2ccon = (v & ~I2CCON_INT_PEND) | (s->i2ccon & I2CCON_INT_PEND);
  158. if ((s->i2ccon & I2CCON_INT_PEND) && !(v & I2CCON_INT_PEND)) {
  159. s->i2ccon &= ~I2CCON_INT_PEND;
  160. qemu_irq_lower(s->irq);
  161. if (!(s->i2ccon & I2CCON_INTRS_EN)) {
  162. s->i2cstat &= ~I2CSTAT_START_BUSY;
  163. }
  164. if (s->i2cstat & I2CSTAT_START_BUSY) {
  165. if (s->scl_free) {
  166. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Tx) {
  167. exynos4210_i2c_data_send(s);
  168. } else if (EXYNOS4_I2C_MODE(s->i2cstat) ==
  169. I2CMODE_MASTER_Rx) {
  170. exynos4210_i2c_data_receive(s);
  171. }
  172. } else {
  173. s->i2ccon |= I2CCON_INT_PEND;
  174. qemu_irq_raise(s->irq);
  175. }
  176. }
  177. }
  178. break;
  179. case I2CSTAT_ADDR:
  180. s->i2cstat =
  181. (s->i2cstat & I2CSTAT_START_BUSY) | (v & ~I2CSTAT_START_BUSY);
  182. if (!(s->i2cstat & I2CSTAT_OUTPUT_EN)) {
  183. s->i2cstat &= ~I2CSTAT_START_BUSY;
  184. s->scl_free = true;
  185. qemu_irq_lower(s->irq);
  186. break;
  187. }
  188. /* Nothing to do if in i2c slave mode */
  189. if (!I2C_IN_MASTER_MODE(s->i2cstat)) {
  190. break;
  191. }
  192. if (v & I2CSTAT_START_BUSY) {
  193. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  194. s->i2cstat |= I2CSTAT_START_BUSY; /* Line is busy */
  195. s->scl_free = false;
  196. /* Generate start bit and send slave address */
  197. if (i2c_start_transfer(s->bus, s->i2cds >> 1, s->i2cds & 0x1) &&
  198. (s->i2ccon & I2CCON_ACK_GEN)) {
  199. s->i2cstat |= I2CSTAT_LAST_BIT;
  200. } else if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Rx) {
  201. exynos4210_i2c_data_receive(s);
  202. }
  203. exynos4210_i2c_raise_interrupt(s);
  204. } else {
  205. i2c_end_transfer(s->bus);
  206. if (!(s->i2ccon & I2CCON_INT_PEND)) {
  207. s->i2cstat &= ~I2CSTAT_START_BUSY;
  208. }
  209. s->scl_free = true;
  210. }
  211. break;
  212. case I2CADD_ADDR:
  213. if ((s->i2cstat & I2CSTAT_OUTPUT_EN) == 0) {
  214. s->i2cadd = v;
  215. }
  216. break;
  217. case I2CDS_ADDR:
  218. if (s->i2cstat & I2CSTAT_OUTPUT_EN) {
  219. s->i2cds = v;
  220. s->scl_free = true;
  221. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Tx &&
  222. (s->i2cstat & I2CSTAT_START_BUSY) &&
  223. !(s->i2ccon & I2CCON_INT_PEND)) {
  224. exynos4210_i2c_data_send(s);
  225. }
  226. }
  227. break;
  228. case I2CLC_ADDR:
  229. s->i2clc = v;
  230. break;
  231. default:
  232. DPRINT("ERROR: Bad write offset 0x%x\n", (unsigned int)offset);
  233. break;
  234. }
  235. }
  236. static const MemoryRegionOps exynos4210_i2c_ops = {
  237. .read = exynos4210_i2c_read,
  238. .write = exynos4210_i2c_write,
  239. .endianness = DEVICE_NATIVE_ENDIAN,
  240. };
  241. static const VMStateDescription exynos4210_i2c_vmstate = {
  242. .name = "exynos4210.i2c",
  243. .version_id = 1,
  244. .minimum_version_id = 1,
  245. .fields = (const VMStateField[]) {
  246. VMSTATE_UINT8(i2ccon, Exynos4210I2CState),
  247. VMSTATE_UINT8(i2cstat, Exynos4210I2CState),
  248. VMSTATE_UINT8(i2cds, Exynos4210I2CState),
  249. VMSTATE_UINT8(i2cadd, Exynos4210I2CState),
  250. VMSTATE_UINT8(i2clc, Exynos4210I2CState),
  251. VMSTATE_BOOL(scl_free, Exynos4210I2CState),
  252. VMSTATE_END_OF_LIST()
  253. }
  254. };
  255. static void exynos4210_i2c_reset(DeviceState *d)
  256. {
  257. Exynos4210I2CState *s = EXYNOS4_I2C(d);
  258. s->i2ccon = 0x00;
  259. s->i2cstat = 0x00;
  260. s->i2cds = 0xFF;
  261. s->i2clc = 0x00;
  262. s->i2cadd = 0xFF;
  263. s->scl_free = true;
  264. }
  265. static void exynos4210_i2c_init(Object *obj)
  266. {
  267. DeviceState *dev = DEVICE(obj);
  268. Exynos4210I2CState *s = EXYNOS4_I2C(obj);
  269. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  270. memory_region_init_io(&s->iomem, obj, &exynos4210_i2c_ops, s,
  271. TYPE_EXYNOS4_I2C, EXYNOS4_I2C_MEM_SIZE);
  272. sysbus_init_mmio(sbd, &s->iomem);
  273. sysbus_init_irq(sbd, &s->irq);
  274. s->bus = i2c_init_bus(dev, "i2c");
  275. }
  276. static void exynos4210_i2c_class_init(ObjectClass *klass, void *data)
  277. {
  278. DeviceClass *dc = DEVICE_CLASS(klass);
  279. dc->vmsd = &exynos4210_i2c_vmstate;
  280. device_class_set_legacy_reset(dc, exynos4210_i2c_reset);
  281. }
  282. static const TypeInfo exynos4210_i2c_type_info = {
  283. .name = TYPE_EXYNOS4_I2C,
  284. .parent = TYPE_SYS_BUS_DEVICE,
  285. .instance_size = sizeof(Exynos4210I2CState),
  286. .instance_init = exynos4210_i2c_init,
  287. .class_init = exynos4210_i2c_class_init,
  288. };
  289. static void exynos4210_i2c_register_types(void)
  290. {
  291. type_register_static(&exynos4210_i2c_type_info);
  292. }
  293. type_init(exynos4210_i2c_register_types)