exynos4210_i2c.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/timer.h"
  24. #include "hw/sysbus.h"
  25. #include "hw/i2c/i2c.h"
  26. #ifndef EXYNOS4_I2C_DEBUG
  27. #define EXYNOS4_I2C_DEBUG 0
  28. #endif
  29. #define TYPE_EXYNOS4_I2C "exynos4210.i2c"
  30. #define EXYNOS4_I2C(obj) \
  31. OBJECT_CHECK(Exynos4210I2CState, (obj), TYPE_EXYNOS4_I2C)
  32. /* Exynos4210 I2C memory map */
  33. #define EXYNOS4_I2C_MEM_SIZE 0x14
  34. #define I2CCON_ADDR 0x00 /* control register */
  35. #define I2CSTAT_ADDR 0x04 /* control/status register */
  36. #define I2CADD_ADDR 0x08 /* address register */
  37. #define I2CDS_ADDR 0x0c /* data shift register */
  38. #define I2CLC_ADDR 0x10 /* line control register */
  39. #define I2CCON_ACK_GEN (1 << 7)
  40. #define I2CCON_INTRS_EN (1 << 5)
  41. #define I2CCON_INT_PEND (1 << 4)
  42. #define EXYNOS4_I2C_MODE(reg) (((reg) >> 6) & 3)
  43. #define I2C_IN_MASTER_MODE(reg) (((reg) >> 6) & 2)
  44. #define I2CMODE_MASTER_Rx 0x2
  45. #define I2CMODE_MASTER_Tx 0x3
  46. #define I2CSTAT_LAST_BIT (1 << 0)
  47. #define I2CSTAT_OUTPUT_EN (1 << 4)
  48. #define I2CSTAT_START_BUSY (1 << 5)
  49. #if EXYNOS4_I2C_DEBUG
  50. #define DPRINT(fmt, args...) \
  51. do { fprintf(stderr, "QEMU I2C: "fmt, ## args); } while (0)
  52. static const char *exynos4_i2c_get_regname(unsigned offset)
  53. {
  54. switch (offset) {
  55. case I2CCON_ADDR:
  56. return "I2CCON";
  57. case I2CSTAT_ADDR:
  58. return "I2CSTAT";
  59. case I2CADD_ADDR:
  60. return "I2CADD";
  61. case I2CDS_ADDR:
  62. return "I2CDS";
  63. case I2CLC_ADDR:
  64. return "I2CLC";
  65. default:
  66. return "[?]";
  67. }
  68. }
  69. #else
  70. #define DPRINT(fmt, args...) do { } while (0)
  71. #endif
  72. typedef struct Exynos4210I2CState {
  73. SysBusDevice parent_obj;
  74. MemoryRegion iomem;
  75. I2CBus *bus;
  76. qemu_irq irq;
  77. uint8_t i2ccon;
  78. uint8_t i2cstat;
  79. uint8_t i2cadd;
  80. uint8_t i2cds;
  81. uint8_t i2clc;
  82. bool scl_free;
  83. } Exynos4210I2CState;
  84. static inline void exynos4210_i2c_raise_interrupt(Exynos4210I2CState *s)
  85. {
  86. if (s->i2ccon & I2CCON_INTRS_EN) {
  87. s->i2ccon |= I2CCON_INT_PEND;
  88. qemu_irq_raise(s->irq);
  89. }
  90. }
  91. static void exynos4210_i2c_data_receive(void *opaque)
  92. {
  93. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  94. int ret;
  95. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  96. s->scl_free = false;
  97. ret = i2c_recv(s->bus);
  98. if (ret < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
  99. s->i2cstat |= I2CSTAT_LAST_BIT; /* Data is not acknowledged */
  100. } else {
  101. s->i2cds = ret;
  102. }
  103. exynos4210_i2c_raise_interrupt(s);
  104. }
  105. static void exynos4210_i2c_data_send(void *opaque)
  106. {
  107. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  108. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  109. s->scl_free = false;
  110. if (i2c_send(s->bus, s->i2cds) < 0 && (s->i2ccon & I2CCON_ACK_GEN)) {
  111. s->i2cstat |= I2CSTAT_LAST_BIT;
  112. }
  113. exynos4210_i2c_raise_interrupt(s);
  114. }
  115. static uint64_t exynos4210_i2c_read(void *opaque, hwaddr offset,
  116. unsigned size)
  117. {
  118. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  119. uint8_t value;
  120. switch (offset) {
  121. case I2CCON_ADDR:
  122. value = s->i2ccon;
  123. break;
  124. case I2CSTAT_ADDR:
  125. value = s->i2cstat;
  126. break;
  127. case I2CADD_ADDR:
  128. value = s->i2cadd;
  129. break;
  130. case I2CDS_ADDR:
  131. value = s->i2cds;
  132. s->scl_free = true;
  133. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Rx &&
  134. (s->i2cstat & I2CSTAT_START_BUSY) &&
  135. !(s->i2ccon & I2CCON_INT_PEND)) {
  136. exynos4210_i2c_data_receive(s);
  137. }
  138. break;
  139. case I2CLC_ADDR:
  140. value = s->i2clc;
  141. break;
  142. default:
  143. value = 0;
  144. DPRINT("ERROR: Bad read offset 0x%x\n", (unsigned int)offset);
  145. break;
  146. }
  147. DPRINT("read %s [0x%02x] -> 0x%02x\n", exynos4_i2c_get_regname(offset),
  148. (unsigned int)offset, value);
  149. return value;
  150. }
  151. static void exynos4210_i2c_write(void *opaque, hwaddr offset,
  152. uint64_t value, unsigned size)
  153. {
  154. Exynos4210I2CState *s = (Exynos4210I2CState *)opaque;
  155. uint8_t v = value & 0xff;
  156. DPRINT("write %s [0x%02x] <- 0x%02x\n", exynos4_i2c_get_regname(offset),
  157. (unsigned int)offset, v);
  158. switch (offset) {
  159. case I2CCON_ADDR:
  160. s->i2ccon = (v & ~I2CCON_INT_PEND) | (s->i2ccon & I2CCON_INT_PEND);
  161. if ((s->i2ccon & I2CCON_INT_PEND) && !(v & I2CCON_INT_PEND)) {
  162. s->i2ccon &= ~I2CCON_INT_PEND;
  163. qemu_irq_lower(s->irq);
  164. if (!(s->i2ccon & I2CCON_INTRS_EN)) {
  165. s->i2cstat &= ~I2CSTAT_START_BUSY;
  166. }
  167. if (s->i2cstat & I2CSTAT_START_BUSY) {
  168. if (s->scl_free) {
  169. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Tx) {
  170. exynos4210_i2c_data_send(s);
  171. } else if (EXYNOS4_I2C_MODE(s->i2cstat) ==
  172. I2CMODE_MASTER_Rx) {
  173. exynos4210_i2c_data_receive(s);
  174. }
  175. } else {
  176. s->i2ccon |= I2CCON_INT_PEND;
  177. qemu_irq_raise(s->irq);
  178. }
  179. }
  180. }
  181. break;
  182. case I2CSTAT_ADDR:
  183. s->i2cstat =
  184. (s->i2cstat & I2CSTAT_START_BUSY) | (v & ~I2CSTAT_START_BUSY);
  185. if (!(s->i2cstat & I2CSTAT_OUTPUT_EN)) {
  186. s->i2cstat &= ~I2CSTAT_START_BUSY;
  187. s->scl_free = true;
  188. qemu_irq_lower(s->irq);
  189. break;
  190. }
  191. /* Nothing to do if in i2c slave mode */
  192. if (!I2C_IN_MASTER_MODE(s->i2cstat)) {
  193. break;
  194. }
  195. if (v & I2CSTAT_START_BUSY) {
  196. s->i2cstat &= ~I2CSTAT_LAST_BIT;
  197. s->i2cstat |= I2CSTAT_START_BUSY; /* Line is busy */
  198. s->scl_free = false;
  199. /* Generate start bit and send slave address */
  200. if (i2c_start_transfer(s->bus, s->i2cds >> 1, s->i2cds & 0x1) &&
  201. (s->i2ccon & I2CCON_ACK_GEN)) {
  202. s->i2cstat |= I2CSTAT_LAST_BIT;
  203. } else if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Rx) {
  204. exynos4210_i2c_data_receive(s);
  205. }
  206. exynos4210_i2c_raise_interrupt(s);
  207. } else {
  208. i2c_end_transfer(s->bus);
  209. if (!(s->i2ccon & I2CCON_INT_PEND)) {
  210. s->i2cstat &= ~I2CSTAT_START_BUSY;
  211. }
  212. s->scl_free = true;
  213. }
  214. break;
  215. case I2CADD_ADDR:
  216. if ((s->i2cstat & I2CSTAT_OUTPUT_EN) == 0) {
  217. s->i2cadd = v;
  218. }
  219. break;
  220. case I2CDS_ADDR:
  221. if (s->i2cstat & I2CSTAT_OUTPUT_EN) {
  222. s->i2cds = v;
  223. s->scl_free = true;
  224. if (EXYNOS4_I2C_MODE(s->i2cstat) == I2CMODE_MASTER_Tx &&
  225. (s->i2cstat & I2CSTAT_START_BUSY) &&
  226. !(s->i2ccon & I2CCON_INT_PEND)) {
  227. exynos4210_i2c_data_send(s);
  228. }
  229. }
  230. break;
  231. case I2CLC_ADDR:
  232. s->i2clc = v;
  233. break;
  234. default:
  235. DPRINT("ERROR: Bad write offset 0x%x\n", (unsigned int)offset);
  236. break;
  237. }
  238. }
  239. static const MemoryRegionOps exynos4210_i2c_ops = {
  240. .read = exynos4210_i2c_read,
  241. .write = exynos4210_i2c_write,
  242. .endianness = DEVICE_NATIVE_ENDIAN,
  243. };
  244. static const VMStateDescription exynos4210_i2c_vmstate = {
  245. .name = "exynos4210.i2c",
  246. .version_id = 1,
  247. .minimum_version_id = 1,
  248. .fields = (VMStateField[]) {
  249. VMSTATE_UINT8(i2ccon, Exynos4210I2CState),
  250. VMSTATE_UINT8(i2cstat, Exynos4210I2CState),
  251. VMSTATE_UINT8(i2cds, Exynos4210I2CState),
  252. VMSTATE_UINT8(i2cadd, Exynos4210I2CState),
  253. VMSTATE_UINT8(i2clc, Exynos4210I2CState),
  254. VMSTATE_BOOL(scl_free, Exynos4210I2CState),
  255. VMSTATE_END_OF_LIST()
  256. }
  257. };
  258. static void exynos4210_i2c_reset(DeviceState *d)
  259. {
  260. Exynos4210I2CState *s = EXYNOS4_I2C(d);
  261. s->i2ccon = 0x00;
  262. s->i2cstat = 0x00;
  263. s->i2cds = 0xFF;
  264. s->i2clc = 0x00;
  265. s->i2cadd = 0xFF;
  266. s->scl_free = true;
  267. }
  268. static void exynos4210_i2c_init(Object *obj)
  269. {
  270. DeviceState *dev = DEVICE(obj);
  271. Exynos4210I2CState *s = EXYNOS4_I2C(obj);
  272. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  273. memory_region_init_io(&s->iomem, obj, &exynos4210_i2c_ops, s,
  274. TYPE_EXYNOS4_I2C, EXYNOS4_I2C_MEM_SIZE);
  275. sysbus_init_mmio(sbd, &s->iomem);
  276. sysbus_init_irq(sbd, &s->irq);
  277. s->bus = i2c_init_bus(dev, "i2c");
  278. }
  279. static void exynos4210_i2c_class_init(ObjectClass *klass, void *data)
  280. {
  281. DeviceClass *dc = DEVICE_CLASS(klass);
  282. dc->vmsd = &exynos4210_i2c_vmstate;
  283. dc->reset = exynos4210_i2c_reset;
  284. }
  285. static const TypeInfo exynos4210_i2c_type_info = {
  286. .name = TYPE_EXYNOS4_I2C,
  287. .parent = TYPE_SYS_BUS_DEVICE,
  288. .instance_size = sizeof(Exynos4210I2CState),
  289. .instance_init = exynos4210_i2c_init,
  290. .class_init = exynos4210_i2c_class_init,
  291. };
  292. static void exynos4210_i2c_register_types(void)
  293. {
  294. type_register_static(&exynos4210_i2c_type_info);
  295. }
  296. type_init(exynos4210_i2c_register_types)