2
0

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