2
0

imx_serial.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * IMX31 UARTS
  3. *
  4. * Copyright (c) 2008 OKL
  5. * Originally Written by Hans Jiang
  6. * Copyright (c) 2011 NICTA Pty Ltd.
  7. * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. * This is a `bare-bones' implementation of the IMX series serial ports.
  13. * TODO:
  14. * -- implement FIFOs. The real hardware has 32 word transmit
  15. * and receive FIFOs; we currently use a 1-char buffer
  16. * -- implement DMA
  17. * -- implement BAUD-rate and modem lines, for when the backend
  18. * is a real serial device.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/char/imx_serial.h"
  22. #include "hw/irq.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/qdev-properties-system.h"
  25. #include "migration/vmstate.h"
  26. #include "qemu/log.h"
  27. #include "qemu/module.h"
  28. #include "qemu/fifo32.h"
  29. #include "trace.h"
  30. #ifndef DEBUG_IMX_UART
  31. #define DEBUG_IMX_UART 0
  32. #endif
  33. #define DPRINTF(fmt, args...) \
  34. do { \
  35. if (DEBUG_IMX_UART) { \
  36. fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_SERIAL, \
  37. __func__, ##args); \
  38. } \
  39. } while (0)
  40. static const VMStateDescription vmstate_imx_serial = {
  41. .name = TYPE_IMX_SERIAL,
  42. .version_id = 3,
  43. .minimum_version_id = 3,
  44. .fields = (const VMStateField[]) {
  45. VMSTATE_FIFO32(rx_fifo, IMXSerialState),
  46. VMSTATE_TIMER(ageing_timer, IMXSerialState),
  47. VMSTATE_UINT32(usr1, IMXSerialState),
  48. VMSTATE_UINT32(usr2, IMXSerialState),
  49. VMSTATE_UINT32(ucr1, IMXSerialState),
  50. VMSTATE_UINT32(uts1, IMXSerialState),
  51. VMSTATE_UINT32(onems, IMXSerialState),
  52. VMSTATE_UINT32(ufcr, IMXSerialState),
  53. VMSTATE_UINT32(ubmr, IMXSerialState),
  54. VMSTATE_UINT32(ubrc, IMXSerialState),
  55. VMSTATE_UINT32(ucr3, IMXSerialState),
  56. VMSTATE_UINT32(ucr4, IMXSerialState),
  57. VMSTATE_END_OF_LIST()
  58. },
  59. };
  60. static void imx_update(IMXSerialState *s)
  61. {
  62. uint32_t usr1;
  63. uint32_t usr2;
  64. uint32_t mask;
  65. /*
  66. * Lucky for us TRDY and RRDY has the same offset in both USR1 and
  67. * UCR1, so we can get away with something as simple as the
  68. * following:
  69. */
  70. usr1 = s->usr1 & s->ucr1 & (USR1_TRDY | USR1_RRDY);
  71. /*
  72. * Interrupt if AGTIM is set (ageing timer interrupt in RxFIFO)
  73. */
  74. usr1 |= (s->ucr2 & UCR2_ATEN) ? (s->usr1 & USR1_AGTIM) : 0;
  75. /*
  76. * Bits that we want in USR2 are not as conveniently laid out,
  77. * unfortunately.
  78. */
  79. mask = (s->ucr1 & UCR1_TXMPTYEN) ? USR2_TXFE : 0;
  80. /*
  81. * TCEN and TXDC are both bit 3
  82. * ORE and OREN are both bit 1
  83. * RDR and DREN are both bit 0
  84. */
  85. mask |= s->ucr4 & (UCR4_WKEN | UCR4_TCEN | UCR4_DREN | UCR4_OREN);
  86. usr2 = s->usr2 & mask;
  87. qemu_set_irq(s->irq, usr1 || usr2);
  88. }
  89. static void imx_serial_rx_fifo_push(IMXSerialState *s, uint32_t value)
  90. {
  91. uint32_t pushed_value = value;
  92. if (fifo32_is_full(&s->rx_fifo)) {
  93. /* Set ORE if FIFO is already full */
  94. s->usr2 |= USR2_ORE;
  95. } else {
  96. if (fifo32_num_used(&s->rx_fifo) == FIFO_SIZE - 1) {
  97. /* Set OVRRUN on 32nd character in FIFO */
  98. pushed_value |= URXD_ERR | URXD_OVRRUN;
  99. }
  100. fifo32_push(&s->rx_fifo, pushed_value);
  101. }
  102. }
  103. static uint32_t imx_serial_rx_fifo_pop(IMXSerialState *s)
  104. {
  105. if (fifo32_is_empty(&s->rx_fifo)) {
  106. return 0;
  107. }
  108. return fifo32_pop(&s->rx_fifo);
  109. }
  110. static void imx_serial_rx_fifo_ageing_timer_int(void *opaque)
  111. {
  112. IMXSerialState *s = (IMXSerialState *) opaque;
  113. s->usr1 |= USR1_AGTIM;
  114. imx_update(s);
  115. }
  116. static void imx_serial_rx_fifo_ageing_timer_restart(void *opaque)
  117. {
  118. /*
  119. * Ageing timer starts ticking when
  120. * RX FIFO is non empty and below trigger level.
  121. * Timer is reset if new character is received or
  122. * a FIFO read occurs.
  123. * Timer triggers an interrupt when duration of
  124. * 8 characters has passed (assuming 115200 baudrate).
  125. */
  126. IMXSerialState *s = (IMXSerialState *) opaque;
  127. if (!(s->usr1 & USR1_RRDY) && !(s->uts1 & UTS1_RXEMPTY)) {
  128. timer_mod_ns(&s->ageing_timer,
  129. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + AGE_DURATION_NS);
  130. } else {
  131. timer_del(&s->ageing_timer);
  132. }
  133. }
  134. static void imx_serial_reset(IMXSerialState *s)
  135. {
  136. s->usr1 = USR1_TRDY | USR1_RXDS;
  137. /*
  138. * Fake attachment of a terminal: assert RTS.
  139. */
  140. s->usr1 |= USR1_RTSS;
  141. s->usr2 = USR2_TXFE | USR2_TXDC | USR2_DCDIN;
  142. s->uts1 = UTS1_RXEMPTY | UTS1_TXEMPTY;
  143. s->ucr1 = 0;
  144. s->ucr2 = UCR2_SRST;
  145. s->ucr3 = 0x700;
  146. s->ubmr = 0;
  147. s->ubrc = 4;
  148. s->ufcr = BIT(11) | BIT(0);
  149. fifo32_reset(&s->rx_fifo);
  150. timer_del(&s->ageing_timer);
  151. }
  152. static void imx_serial_reset_at_boot(DeviceState *dev)
  153. {
  154. IMXSerialState *s = IMX_SERIAL(dev);
  155. imx_serial_reset(s);
  156. /*
  157. * enable the uart on boot, so messages from the linux decompressor
  158. * are visible. On real hardware this is done by the boot rom
  159. * before anything else is loaded.
  160. */
  161. s->ucr1 = UCR1_UARTEN;
  162. s->ucr2 = UCR2_TXEN;
  163. }
  164. static uint64_t imx_serial_read(void *opaque, hwaddr offset,
  165. unsigned size)
  166. {
  167. IMXSerialState *s = (IMXSerialState *)opaque;
  168. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  169. uint32_t c, rx_used;
  170. uint8_t rxtl = s->ufcr & TL_MASK;
  171. uint64_t value;
  172. switch (offset >> 2) {
  173. case 0x0: /* URXD */
  174. c = imx_serial_rx_fifo_pop(s);
  175. if (!(s->uts1 & UTS1_RXEMPTY)) {
  176. /* Character is valid */
  177. c |= URXD_CHARRDY;
  178. rx_used = fifo32_num_used(&s->rx_fifo);
  179. /* Clear RRDY if below threshold */
  180. if (rx_used < rxtl) {
  181. s->usr1 &= ~USR1_RRDY;
  182. }
  183. if (rx_used == 0) {
  184. s->usr2 &= ~USR2_RDR;
  185. s->uts1 |= UTS1_RXEMPTY;
  186. }
  187. imx_update(s);
  188. imx_serial_rx_fifo_ageing_timer_restart(s);
  189. qemu_chr_fe_accept_input(&s->chr);
  190. }
  191. value = c;
  192. break;
  193. case 0x20: /* UCR1 */
  194. value = s->ucr1;
  195. break;
  196. case 0x21: /* UCR2 */
  197. value = s->ucr2;
  198. break;
  199. case 0x25: /* USR1 */
  200. value = s->usr1;
  201. break;
  202. case 0x26: /* USR2 */
  203. value = s->usr2;
  204. break;
  205. case 0x2A: /* BRM Modulator */
  206. value = s->ubmr;
  207. break;
  208. case 0x2B: /* Baud Rate Count */
  209. value = s->ubrc;
  210. break;
  211. case 0x2d: /* Test register */
  212. value = s->uts1;
  213. break;
  214. case 0x24: /* UFCR */
  215. value = s->ufcr;
  216. break;
  217. case 0x2c:
  218. value = s->onems;
  219. break;
  220. case 0x22: /* UCR3 */
  221. value = s->ucr3;
  222. break;
  223. case 0x23: /* UCR4 */
  224. value = s->ucr4;
  225. break;
  226. case 0x29: /* BRM Incremental */
  227. value = 0x0; /* TODO */
  228. break;
  229. default:
  230. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  231. HWADDR_PRIx "\n", TYPE_IMX_SERIAL, __func__, offset);
  232. value = 0;
  233. break;
  234. }
  235. trace_imx_serial_read(chr ? chr->label : "NODEV", offset, value);
  236. return value;
  237. }
  238. static void imx_serial_write(void *opaque, hwaddr offset,
  239. uint64_t value, unsigned size)
  240. {
  241. IMXSerialState *s = (IMXSerialState *)opaque;
  242. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  243. unsigned char ch;
  244. trace_imx_serial_write(chr ? chr->label : "NODEV", offset, value);
  245. switch (offset >> 2) {
  246. case 0x10: /* UTXD */
  247. ch = value;
  248. if (s->ucr2 & UCR2_TXEN) {
  249. /* XXX this blocks entire thread. Rewrite to use
  250. * qemu_chr_fe_write and background I/O callbacks */
  251. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  252. s->usr1 &= ~USR1_TRDY;
  253. s->usr2 &= ~USR2_TXDC;
  254. imx_update(s);
  255. s->usr1 |= USR1_TRDY;
  256. s->usr2 |= USR2_TXDC;
  257. imx_update(s);
  258. }
  259. break;
  260. case 0x20: /* UCR1 */
  261. s->ucr1 = value & 0xffff;
  262. DPRINTF("write(ucr1=%x)\n", (unsigned int)value);
  263. imx_update(s);
  264. break;
  265. case 0x21: /* UCR2 */
  266. /*
  267. * Only a few bits in control register 2 are implemented as yet.
  268. * If it's intended to use a real serial device as a back-end, this
  269. * register will have to be implemented more fully.
  270. */
  271. if (!(value & UCR2_SRST)) {
  272. imx_serial_reset(s);
  273. imx_update(s);
  274. value |= UCR2_SRST;
  275. }
  276. if (value & UCR2_RXEN) {
  277. if (!(s->ucr2 & UCR2_RXEN)) {
  278. qemu_chr_fe_accept_input(&s->chr);
  279. }
  280. }
  281. s->ucr2 = value & 0xffff;
  282. break;
  283. case 0x25: /* USR1 */
  284. value &= USR1_AWAKE | USR1_AIRINT | USR1_DTRD | USR1_AGTIM |
  285. USR1_FRAMERR | USR1_ESCF | USR1_RTSD | USR1_PARTYER;
  286. s->usr1 &= ~value;
  287. break;
  288. case 0x26: /* USR2 */
  289. /*
  290. * Writing 1 to some bits clears them; all other
  291. * values are ignored
  292. */
  293. value &= USR2_ADET | USR2_DTRF | USR2_IDLE | USR2_ACST |
  294. USR2_RIDELT | USR2_IRINT | USR2_WAKE |
  295. USR2_DCDDELT | USR2_RTSF | USR2_BRCD | USR2_ORE;
  296. s->usr2 &= ~value;
  297. break;
  298. /*
  299. * Linux expects to see what it writes to these registers
  300. * We don't currently alter the baud rate
  301. */
  302. case 0x29: /* UBIR */
  303. s->ubrc = value & 0xffff;
  304. break;
  305. case 0x2a: /* UBMR */
  306. s->ubmr = value & 0xffff;
  307. break;
  308. case 0x2c: /* One ms reg */
  309. s->onems = value & 0xffff;
  310. break;
  311. case 0x24: /* FIFO control register */
  312. s->ufcr = value & 0xffff;
  313. break;
  314. case 0x22: /* UCR3 */
  315. s->ucr3 = value & 0xffff;
  316. break;
  317. case 0x23: /* UCR4 */
  318. s->ucr4 = value & 0xffff;
  319. imx_update(s);
  320. break;
  321. case 0x2d: /* UTS1 */
  322. qemu_log_mask(LOG_UNIMP, "[%s]%s: Unimplemented reg 0x%"
  323. HWADDR_PRIx "\n", TYPE_IMX_SERIAL, __func__, offset);
  324. /* TODO */
  325. break;
  326. default:
  327. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  328. HWADDR_PRIx "\n", TYPE_IMX_SERIAL, __func__, offset);
  329. }
  330. }
  331. static int imx_can_receive(void *opaque)
  332. {
  333. IMXSerialState *s = (IMXSerialState *)opaque;
  334. return s->ucr2 & UCR2_RXEN ? fifo32_num_free(&s->rx_fifo) : 0;
  335. }
  336. static void imx_put_data(void *opaque, uint32_t value)
  337. {
  338. IMXSerialState *s = (IMXSerialState *)opaque;
  339. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  340. uint8_t rxtl = s->ufcr & TL_MASK;
  341. trace_imx_serial_put_data(chr ? chr->label : "NODEV", value);
  342. imx_serial_rx_fifo_push(s, value);
  343. if (fifo32_num_used(&s->rx_fifo) >= rxtl) {
  344. s->usr1 |= USR1_RRDY;
  345. }
  346. s->usr2 |= USR2_RDR;
  347. s->uts1 &= ~UTS1_RXEMPTY;
  348. if (value & URXD_BRK) {
  349. s->usr2 |= USR2_BRCD;
  350. }
  351. imx_serial_rx_fifo_ageing_timer_restart(s);
  352. imx_update(s);
  353. }
  354. static void imx_receive(void *opaque, const uint8_t *buf, int size)
  355. {
  356. IMXSerialState *s = (IMXSerialState *)opaque;
  357. s->usr2 |= USR2_WAKE;
  358. for (int i = 0; i < size; i++) {
  359. imx_put_data(opaque, buf[i]);
  360. }
  361. }
  362. static void imx_event(void *opaque, QEMUChrEvent event)
  363. {
  364. if (event == CHR_EVENT_BREAK) {
  365. imx_put_data(opaque, URXD_BRK | URXD_FRMERR | URXD_ERR);
  366. }
  367. }
  368. static const struct MemoryRegionOps imx_serial_ops = {
  369. .read = imx_serial_read,
  370. .write = imx_serial_write,
  371. .endianness = DEVICE_NATIVE_ENDIAN,
  372. };
  373. static void imx_serial_realize(DeviceState *dev, Error **errp)
  374. {
  375. IMXSerialState *s = IMX_SERIAL(dev);
  376. fifo32_create(&s->rx_fifo, FIFO_SIZE);
  377. timer_init_ns(&s->ageing_timer, QEMU_CLOCK_VIRTUAL,
  378. imx_serial_rx_fifo_ageing_timer_int, s);
  379. DPRINTF("char dev for uart: %p\n", qemu_chr_fe_get_driver(&s->chr));
  380. qemu_chr_fe_set_handlers(&s->chr, imx_can_receive, imx_receive,
  381. imx_event, NULL, s, NULL, true);
  382. }
  383. static void imx_serial_init(Object *obj)
  384. {
  385. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  386. IMXSerialState *s = IMX_SERIAL(obj);
  387. memory_region_init_io(&s->iomem, obj, &imx_serial_ops, s,
  388. TYPE_IMX_SERIAL, 0x1000);
  389. sysbus_init_mmio(sbd, &s->iomem);
  390. sysbus_init_irq(sbd, &s->irq);
  391. }
  392. static const Property imx_serial_properties[] = {
  393. DEFINE_PROP_CHR("chardev", IMXSerialState, chr),
  394. };
  395. static void imx_serial_class_init(ObjectClass *klass, void *data)
  396. {
  397. DeviceClass *dc = DEVICE_CLASS(klass);
  398. dc->realize = imx_serial_realize;
  399. dc->vmsd = &vmstate_imx_serial;
  400. device_class_set_legacy_reset(dc, imx_serial_reset_at_boot);
  401. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  402. dc->desc = "i.MX series UART";
  403. device_class_set_props(dc, imx_serial_properties);
  404. }
  405. static const TypeInfo imx_serial_info = {
  406. .name = TYPE_IMX_SERIAL,
  407. .parent = TYPE_SYS_BUS_DEVICE,
  408. .instance_size = sizeof(IMXSerialState),
  409. .instance_init = imx_serial_init,
  410. .class_init = imx_serial_class_init,
  411. };
  412. static void imx_serial_register_types(void)
  413. {
  414. type_register_static(&imx_serial_info);
  415. }
  416. type_init(imx_serial_register_types)