pl011.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Arm PrimeCell PL011 UART
  3. *
  4. * Copyright (c) 2006 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. /*
  10. * QEMU interface:
  11. * + sysbus MMIO region 0: device registers
  12. * + sysbus IRQ 0: UARTINTR (combined interrupt line)
  13. * + sysbus IRQ 1: UARTRXINTR (receive FIFO interrupt line)
  14. * + sysbus IRQ 2: UARTTXINTR (transmit FIFO interrupt line)
  15. * + sysbus IRQ 3: UARTRTINTR (receive timeout interrupt line)
  16. * + sysbus IRQ 4: UARTMSINTR (momem status interrupt line)
  17. * + sysbus IRQ 5: UARTEINTR (error interrupt line)
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #include "hw/char/pl011.h"
  22. #include "hw/irq.h"
  23. #include "hw/sysbus.h"
  24. #include "hw/qdev-clock.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/qdev-properties-system.h"
  27. #include "migration/vmstate.h"
  28. #include "chardev/char-fe.h"
  29. #include "chardev/char-serial.h"
  30. #include "qemu/log.h"
  31. #include "qemu/module.h"
  32. #include "trace.h"
  33. DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr)
  34. {
  35. DeviceState *dev;
  36. SysBusDevice *s;
  37. dev = qdev_new("pl011");
  38. s = SYS_BUS_DEVICE(dev);
  39. qdev_prop_set_chr(dev, "chardev", chr);
  40. sysbus_realize_and_unref(s, &error_fatal);
  41. sysbus_mmio_map(s, 0, addr);
  42. sysbus_connect_irq(s, 0, irq);
  43. return dev;
  44. }
  45. /* Flag Register, UARTFR */
  46. #define PL011_FLAG_RI 0x100
  47. #define PL011_FLAG_TXFE 0x80
  48. #define PL011_FLAG_RXFF 0x40
  49. #define PL011_FLAG_TXFF 0x20
  50. #define PL011_FLAG_RXFE 0x10
  51. #define PL011_FLAG_DCD 0x04
  52. #define PL011_FLAG_DSR 0x02
  53. #define PL011_FLAG_CTS 0x01
  54. /* Data Register, UARTDR */
  55. #define DR_BE (1 << 10)
  56. /* Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC */
  57. #define INT_OE (1 << 10)
  58. #define INT_BE (1 << 9)
  59. #define INT_PE (1 << 8)
  60. #define INT_FE (1 << 7)
  61. #define INT_RT (1 << 6)
  62. #define INT_TX (1 << 5)
  63. #define INT_RX (1 << 4)
  64. #define INT_DSR (1 << 3)
  65. #define INT_DCD (1 << 2)
  66. #define INT_CTS (1 << 1)
  67. #define INT_RI (1 << 0)
  68. #define INT_E (INT_OE | INT_BE | INT_PE | INT_FE)
  69. #define INT_MS (INT_RI | INT_DSR | INT_DCD | INT_CTS)
  70. /* Line Control Register, UARTLCR_H */
  71. #define LCR_FEN (1 << 4)
  72. #define LCR_BRK (1 << 0)
  73. /* Control Register, UARTCR */
  74. #define CR_OUT2 (1 << 13)
  75. #define CR_OUT1 (1 << 12)
  76. #define CR_RTS (1 << 11)
  77. #define CR_DTR (1 << 10)
  78. #define CR_RXE (1 << 9)
  79. #define CR_TXE (1 << 8)
  80. #define CR_LBE (1 << 7)
  81. #define CR_UARTEN (1 << 0)
  82. /* Integer Baud Rate Divider, UARTIBRD */
  83. #define IBRD_MASK 0xffff
  84. /* Fractional Baud Rate Divider, UARTFBRD */
  85. #define FBRD_MASK 0x3f
  86. static const unsigned char pl011_id_arm[8] =
  87. { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
  88. static const unsigned char pl011_id_luminary[8] =
  89. { 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
  90. static const char *pl011_regname(hwaddr offset)
  91. {
  92. static const char *const rname[] = {
  93. [0] = "DR", [1] = "RSR", [6] = "FR", [8] = "ILPR", [9] = "IBRD",
  94. [10] = "FBRD", [11] = "LCRH", [12] = "CR", [13] = "IFLS", [14] = "IMSC",
  95. [15] = "RIS", [16] = "MIS", [17] = "ICR", [18] = "DMACR",
  96. };
  97. unsigned idx = offset >> 2;
  98. if (idx < ARRAY_SIZE(rname) && rname[idx]) {
  99. return rname[idx];
  100. }
  101. if (idx >= 0x3f8 && idx <= 0x400) {
  102. return "ID";
  103. }
  104. return "UNKN";
  105. }
  106. /* Which bits in the interrupt status matter for each outbound IRQ line ? */
  107. static const uint32_t irqmask[] = {
  108. INT_E | INT_MS | INT_RT | INT_TX | INT_RX, /* combined IRQ */
  109. INT_RX,
  110. INT_TX,
  111. INT_RT,
  112. INT_MS,
  113. INT_E,
  114. };
  115. static void pl011_update(PL011State *s)
  116. {
  117. uint32_t flags;
  118. int i;
  119. flags = s->int_level & s->int_enabled;
  120. trace_pl011_irq_state(flags != 0);
  121. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  122. qemu_set_irq(s->irq[i], (flags & irqmask[i]) != 0);
  123. }
  124. }
  125. static bool pl011_loopback_enabled(PL011State *s)
  126. {
  127. return !!(s->cr & CR_LBE);
  128. }
  129. static bool pl011_is_fifo_enabled(PL011State *s)
  130. {
  131. return (s->lcr & LCR_FEN) != 0;
  132. }
  133. static inline unsigned pl011_get_fifo_depth(PL011State *s)
  134. {
  135. /* Note: FIFO depth is expected to be power-of-2 */
  136. return pl011_is_fifo_enabled(s) ? PL011_FIFO_DEPTH : 1;
  137. }
  138. static inline void pl011_reset_rx_fifo(PL011State *s)
  139. {
  140. s->read_count = 0;
  141. s->read_pos = 0;
  142. /* Reset FIFO flags */
  143. s->flags &= ~PL011_FLAG_RXFF;
  144. s->flags |= PL011_FLAG_RXFE;
  145. }
  146. static inline void pl011_reset_tx_fifo(PL011State *s)
  147. {
  148. /* Reset FIFO flags */
  149. s->flags &= ~PL011_FLAG_TXFF;
  150. s->flags |= PL011_FLAG_TXFE;
  151. }
  152. static void pl011_fifo_rx_put(void *opaque, uint32_t value)
  153. {
  154. PL011State *s = (PL011State *)opaque;
  155. int slot;
  156. unsigned pipe_depth;
  157. pipe_depth = pl011_get_fifo_depth(s);
  158. slot = (s->read_pos + s->read_count) & (pipe_depth - 1);
  159. s->read_fifo[slot] = value;
  160. s->read_count++;
  161. s->flags &= ~PL011_FLAG_RXFE;
  162. trace_pl011_fifo_rx_put(value, s->read_count, pipe_depth);
  163. if (s->read_count == pipe_depth) {
  164. trace_pl011_fifo_rx_full();
  165. s->flags |= PL011_FLAG_RXFF;
  166. }
  167. if (s->read_count == s->read_trigger) {
  168. s->int_level |= INT_RX;
  169. pl011_update(s);
  170. }
  171. }
  172. static void pl011_loopback_tx(PL011State *s, uint32_t value)
  173. {
  174. if (!pl011_loopback_enabled(s)) {
  175. return;
  176. }
  177. /*
  178. * Caveat:
  179. *
  180. * In real hardware, TX loopback happens at the serial-bit level
  181. * and then reassembled by the RX logics back into bytes and placed
  182. * into the RX fifo. That is, loopback happens after TX fifo.
  183. *
  184. * Because the real hardware TX fifo is time-drained at the frame
  185. * rate governed by the configured serial format, some loopback
  186. * bytes in TX fifo may still be able to get into the RX fifo
  187. * that could be full at times while being drained at software
  188. * pace.
  189. *
  190. * In such scenario, the RX draining pace is the major factor
  191. * deciding which loopback bytes get into the RX fifo, unless
  192. * hardware flow-control is enabled.
  193. *
  194. * For simplicity, the above described is not emulated.
  195. */
  196. pl011_fifo_rx_put(s, value);
  197. }
  198. static void pl011_write_txdata(PL011State *s, uint8_t data)
  199. {
  200. if (!(s->cr & CR_UARTEN)) {
  201. qemu_log_mask(LOG_GUEST_ERROR,
  202. "PL011 data written to disabled UART\n");
  203. }
  204. if (!(s->cr & CR_TXE)) {
  205. qemu_log_mask(LOG_GUEST_ERROR,
  206. "PL011 data written to disabled TX UART\n");
  207. }
  208. /*
  209. * XXX this blocks entire thread. Rewrite to use
  210. * qemu_chr_fe_write and background I/O callbacks
  211. */
  212. qemu_chr_fe_write_all(&s->chr, &data, 1);
  213. pl011_loopback_tx(s, data);
  214. s->int_level |= INT_TX;
  215. pl011_update(s);
  216. }
  217. static uint32_t pl011_read_rxdata(PL011State *s)
  218. {
  219. uint32_t c;
  220. unsigned fifo_depth = pl011_get_fifo_depth(s);
  221. s->flags &= ~PL011_FLAG_RXFF;
  222. c = s->read_fifo[s->read_pos];
  223. if (s->read_count > 0) {
  224. s->read_count--;
  225. s->read_pos = (s->read_pos + 1) & (fifo_depth - 1);
  226. }
  227. if (s->read_count == 0) {
  228. s->flags |= PL011_FLAG_RXFE;
  229. }
  230. if (s->read_count == s->read_trigger - 1) {
  231. s->int_level &= ~INT_RX;
  232. }
  233. trace_pl011_read_fifo(s->read_count, fifo_depth);
  234. s->rsr = c >> 8;
  235. pl011_update(s);
  236. qemu_chr_fe_accept_input(&s->chr);
  237. return c;
  238. }
  239. static uint64_t pl011_read(void *opaque, hwaddr offset,
  240. unsigned size)
  241. {
  242. PL011State *s = (PL011State *)opaque;
  243. uint64_t r;
  244. switch (offset >> 2) {
  245. case 0: /* UARTDR */
  246. r = pl011_read_rxdata(s);
  247. break;
  248. case 1: /* UARTRSR */
  249. r = s->rsr;
  250. break;
  251. case 6: /* UARTFR */
  252. r = s->flags;
  253. break;
  254. case 8: /* UARTILPR */
  255. r = s->ilpr;
  256. break;
  257. case 9: /* UARTIBRD */
  258. r = s->ibrd;
  259. break;
  260. case 10: /* UARTFBRD */
  261. r = s->fbrd;
  262. break;
  263. case 11: /* UARTLCR_H */
  264. r = s->lcr;
  265. break;
  266. case 12: /* UARTCR */
  267. r = s->cr;
  268. break;
  269. case 13: /* UARTIFLS */
  270. r = s->ifl;
  271. break;
  272. case 14: /* UARTIMSC */
  273. r = s->int_enabled;
  274. break;
  275. case 15: /* UARTRIS */
  276. r = s->int_level;
  277. break;
  278. case 16: /* UARTMIS */
  279. r = s->int_level & s->int_enabled;
  280. break;
  281. case 18: /* UARTDMACR */
  282. r = s->dmacr;
  283. break;
  284. case 0x3f8 ... 0x400:
  285. r = s->id[(offset - 0xfe0) >> 2];
  286. break;
  287. default:
  288. qemu_log_mask(LOG_GUEST_ERROR,
  289. "pl011_read: Bad offset 0x%x\n", (int)offset);
  290. r = 0;
  291. break;
  292. }
  293. trace_pl011_read(offset, r, pl011_regname(offset));
  294. return r;
  295. }
  296. static void pl011_set_read_trigger(PL011State *s)
  297. {
  298. #if 0
  299. /* The docs say the RX interrupt is triggered when the FIFO exceeds
  300. the threshold. However linux only reads the FIFO in response to an
  301. interrupt. Triggering the interrupt when the FIFO is non-empty seems
  302. to make things work. */
  303. if (s->lcr & LCR_FEN)
  304. s->read_trigger = (s->ifl >> 1) & 0x1c;
  305. else
  306. #endif
  307. s->read_trigger = 1;
  308. }
  309. static unsigned int pl011_get_baudrate(const PL011State *s)
  310. {
  311. uint64_t clk;
  312. if (s->ibrd == 0) {
  313. return 0;
  314. }
  315. clk = clock_get_hz(s->clk);
  316. return (clk / ((s->ibrd << 6) + s->fbrd)) << 2;
  317. }
  318. static void pl011_trace_baudrate_change(const PL011State *s)
  319. {
  320. trace_pl011_baudrate_change(pl011_get_baudrate(s),
  321. clock_get_hz(s->clk),
  322. s->ibrd, s->fbrd);
  323. }
  324. static void pl011_loopback_mdmctrl(PL011State *s)
  325. {
  326. uint32_t cr, fr, il;
  327. if (!pl011_loopback_enabled(s)) {
  328. return;
  329. }
  330. /*
  331. * Loopback software-driven modem control outputs to modem status inputs:
  332. * FR.RI <= CR.Out2
  333. * FR.DCD <= CR.Out1
  334. * FR.CTS <= CR.RTS
  335. * FR.DSR <= CR.DTR
  336. *
  337. * The loopback happens immediately even if this call is triggered
  338. * by setting only CR.LBE.
  339. *
  340. * CTS/RTS updates due to enabled hardware flow controls are not
  341. * dealt with here.
  342. */
  343. cr = s->cr;
  344. fr = s->flags & ~(PL011_FLAG_RI | PL011_FLAG_DCD |
  345. PL011_FLAG_DSR | PL011_FLAG_CTS);
  346. fr |= (cr & CR_OUT2) ? PL011_FLAG_RI : 0;
  347. fr |= (cr & CR_OUT1) ? PL011_FLAG_DCD : 0;
  348. fr |= (cr & CR_RTS) ? PL011_FLAG_CTS : 0;
  349. fr |= (cr & CR_DTR) ? PL011_FLAG_DSR : 0;
  350. /* Change interrupts based on updated FR */
  351. il = s->int_level & ~(INT_DSR | INT_DCD | INT_CTS | INT_RI);
  352. il |= (fr & PL011_FLAG_DSR) ? INT_DSR : 0;
  353. il |= (fr & PL011_FLAG_DCD) ? INT_DCD : 0;
  354. il |= (fr & PL011_FLAG_CTS) ? INT_CTS : 0;
  355. il |= (fr & PL011_FLAG_RI) ? INT_RI : 0;
  356. s->flags = fr;
  357. s->int_level = il;
  358. pl011_update(s);
  359. }
  360. static void pl011_loopback_break(PL011State *s, int brk_enable)
  361. {
  362. if (brk_enable) {
  363. pl011_loopback_tx(s, DR_BE);
  364. }
  365. }
  366. static void pl011_write(void *opaque, hwaddr offset,
  367. uint64_t value, unsigned size)
  368. {
  369. PL011State *s = (PL011State *)opaque;
  370. unsigned char ch;
  371. trace_pl011_write(offset, value, pl011_regname(offset));
  372. switch (offset >> 2) {
  373. case 0: /* UARTDR */
  374. ch = value;
  375. pl011_write_txdata(s, ch);
  376. break;
  377. case 1: /* UARTRSR/UARTECR */
  378. s->rsr = 0;
  379. break;
  380. case 6: /* UARTFR */
  381. /* Writes to Flag register are ignored. */
  382. break;
  383. case 8: /* UARTILPR */
  384. s->ilpr = value;
  385. break;
  386. case 9: /* UARTIBRD */
  387. s->ibrd = value & IBRD_MASK;
  388. pl011_trace_baudrate_change(s);
  389. break;
  390. case 10: /* UARTFBRD */
  391. s->fbrd = value & FBRD_MASK;
  392. pl011_trace_baudrate_change(s);
  393. break;
  394. case 11: /* UARTLCR_H */
  395. /* Reset the FIFO state on FIFO enable or disable */
  396. if ((s->lcr ^ value) & LCR_FEN) {
  397. pl011_reset_rx_fifo(s);
  398. pl011_reset_tx_fifo(s);
  399. }
  400. if ((s->lcr ^ value) & LCR_BRK) {
  401. int break_enable = value & LCR_BRK;
  402. qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
  403. &break_enable);
  404. pl011_loopback_break(s, break_enable);
  405. }
  406. s->lcr = value;
  407. pl011_set_read_trigger(s);
  408. break;
  409. case 12: /* UARTCR */
  410. /* ??? Need to implement the enable bit. */
  411. s->cr = value;
  412. pl011_loopback_mdmctrl(s);
  413. break;
  414. case 13: /* UARTIFS */
  415. s->ifl = value;
  416. pl011_set_read_trigger(s);
  417. break;
  418. case 14: /* UARTIMSC */
  419. s->int_enabled = value;
  420. pl011_update(s);
  421. break;
  422. case 17: /* UARTICR */
  423. s->int_level &= ~value;
  424. pl011_update(s);
  425. break;
  426. case 18: /* UARTDMACR */
  427. s->dmacr = value;
  428. if (value & 3) {
  429. qemu_log_mask(LOG_UNIMP, "pl011: DMA not implemented\n");
  430. }
  431. break;
  432. default:
  433. qemu_log_mask(LOG_GUEST_ERROR,
  434. "pl011_write: Bad offset 0x%x\n", (int)offset);
  435. }
  436. }
  437. static int pl011_can_receive(void *opaque)
  438. {
  439. PL011State *s = (PL011State *)opaque;
  440. unsigned fifo_depth = pl011_get_fifo_depth(s);
  441. unsigned fifo_available = fifo_depth - s->read_count;
  442. /*
  443. * In theory we should check the UART and RX enable bits here and
  444. * return 0 if they are not set (so the guest can't receive data
  445. * until you have enabled the UART). In practice we suspect there
  446. * is at least some guest code out there which has been tested only
  447. * on QEMU and which never bothers to enable the UART because we
  448. * historically never enforced that. So we effectively keep the
  449. * UART continuously enabled regardless of the enable bits.
  450. */
  451. trace_pl011_can_receive(s->lcr, s->read_count, fifo_depth, fifo_available);
  452. return fifo_available;
  453. }
  454. static void pl011_receive(void *opaque, const uint8_t *buf, int size)
  455. {
  456. trace_pl011_receive(size);
  457. /*
  458. * In loopback mode, the RX input signal is internally disconnected
  459. * from the entire receiving logics; thus, all inputs are ignored,
  460. * and BREAK detection on RX input signal is also not performed.
  461. */
  462. if (pl011_loopback_enabled(opaque)) {
  463. return;
  464. }
  465. for (int i = 0; i < size; i++) {
  466. pl011_fifo_rx_put(opaque, buf[i]);
  467. }
  468. }
  469. static void pl011_event(void *opaque, QEMUChrEvent event)
  470. {
  471. if (event == CHR_EVENT_BREAK && !pl011_loopback_enabled(opaque)) {
  472. pl011_fifo_rx_put(opaque, DR_BE);
  473. }
  474. }
  475. static void pl011_clock_update(void *opaque, ClockEvent event)
  476. {
  477. PL011State *s = PL011(opaque);
  478. pl011_trace_baudrate_change(s);
  479. }
  480. static const MemoryRegionOps pl011_ops = {
  481. .read = pl011_read,
  482. .write = pl011_write,
  483. .endianness = DEVICE_NATIVE_ENDIAN,
  484. .impl.min_access_size = 4,
  485. .impl.max_access_size = 4,
  486. };
  487. static bool pl011_clock_needed(void *opaque)
  488. {
  489. PL011State *s = PL011(opaque);
  490. return s->migrate_clk;
  491. }
  492. static const VMStateDescription vmstate_pl011_clock = {
  493. .name = "pl011/clock",
  494. .version_id = 1,
  495. .minimum_version_id = 1,
  496. .needed = pl011_clock_needed,
  497. .fields = (const VMStateField[]) {
  498. VMSTATE_CLOCK(clk, PL011State),
  499. VMSTATE_END_OF_LIST()
  500. }
  501. };
  502. static int pl011_post_load(void *opaque, int version_id)
  503. {
  504. PL011State* s = opaque;
  505. /* Sanity-check input state */
  506. if (s->read_pos >= ARRAY_SIZE(s->read_fifo) ||
  507. s->read_count > ARRAY_SIZE(s->read_fifo)) {
  508. return -1;
  509. }
  510. if (!pl011_is_fifo_enabled(s) && s->read_count > 0 && s->read_pos > 0) {
  511. /*
  512. * Older versions of PL011 didn't ensure that the single
  513. * character in the FIFO in FIFO-disabled mode is in
  514. * element 0 of the array; convert to follow the current
  515. * code's assumptions.
  516. */
  517. s->read_fifo[0] = s->read_fifo[s->read_pos];
  518. s->read_pos = 0;
  519. }
  520. s->ibrd &= IBRD_MASK;
  521. s->fbrd &= FBRD_MASK;
  522. return 0;
  523. }
  524. static const VMStateDescription vmstate_pl011 = {
  525. .name = "pl011",
  526. .version_id = 2,
  527. .minimum_version_id = 2,
  528. .post_load = pl011_post_load,
  529. .fields = (const VMStateField[]) {
  530. VMSTATE_UNUSED(sizeof(uint32_t)),
  531. VMSTATE_UINT32(flags, PL011State),
  532. VMSTATE_UINT32(lcr, PL011State),
  533. VMSTATE_UINT32(rsr, PL011State),
  534. VMSTATE_UINT32(cr, PL011State),
  535. VMSTATE_UINT32(dmacr, PL011State),
  536. VMSTATE_UINT32(int_enabled, PL011State),
  537. VMSTATE_UINT32(int_level, PL011State),
  538. VMSTATE_UINT32_ARRAY(read_fifo, PL011State, PL011_FIFO_DEPTH),
  539. VMSTATE_UINT32(ilpr, PL011State),
  540. VMSTATE_UINT32(ibrd, PL011State),
  541. VMSTATE_UINT32(fbrd, PL011State),
  542. VMSTATE_UINT32(ifl, PL011State),
  543. VMSTATE_INT32(read_pos, PL011State),
  544. VMSTATE_INT32(read_count, PL011State),
  545. VMSTATE_INT32(read_trigger, PL011State),
  546. VMSTATE_END_OF_LIST()
  547. },
  548. .subsections = (const VMStateDescription * const []) {
  549. &vmstate_pl011_clock,
  550. NULL
  551. }
  552. };
  553. static const Property pl011_properties[] = {
  554. DEFINE_PROP_CHR("chardev", PL011State, chr),
  555. DEFINE_PROP_BOOL("migrate-clk", PL011State, migrate_clk, true),
  556. };
  557. static void pl011_init(Object *obj)
  558. {
  559. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  560. PL011State *s = PL011(obj);
  561. int i;
  562. memory_region_init_io(&s->iomem, OBJECT(s), &pl011_ops, s, "pl011", 0x1000);
  563. sysbus_init_mmio(sbd, &s->iomem);
  564. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  565. sysbus_init_irq(sbd, &s->irq[i]);
  566. }
  567. s->clk = qdev_init_clock_in(DEVICE(obj), "clk", pl011_clock_update, s,
  568. ClockUpdate);
  569. s->id = pl011_id_arm;
  570. }
  571. static void pl011_realize(DeviceState *dev, Error **errp)
  572. {
  573. PL011State *s = PL011(dev);
  574. qemu_chr_fe_set_handlers(&s->chr, pl011_can_receive, pl011_receive,
  575. pl011_event, NULL, s, NULL, true);
  576. }
  577. static void pl011_reset(DeviceState *dev)
  578. {
  579. PL011State *s = PL011(dev);
  580. s->lcr = 0;
  581. s->rsr = 0;
  582. s->dmacr = 0;
  583. s->int_enabled = 0;
  584. s->int_level = 0;
  585. s->ilpr = 0;
  586. s->ibrd = 0;
  587. s->fbrd = 0;
  588. s->read_trigger = 1;
  589. s->ifl = 0x12;
  590. s->cr = 0x300;
  591. s->flags = 0;
  592. pl011_reset_rx_fifo(s);
  593. pl011_reset_tx_fifo(s);
  594. }
  595. static void pl011_class_init(ObjectClass *oc, void *data)
  596. {
  597. DeviceClass *dc = DEVICE_CLASS(oc);
  598. dc->realize = pl011_realize;
  599. device_class_set_legacy_reset(dc, pl011_reset);
  600. dc->vmsd = &vmstate_pl011;
  601. device_class_set_props(dc, pl011_properties);
  602. }
  603. static const TypeInfo pl011_arm_info = {
  604. .name = TYPE_PL011,
  605. .parent = TYPE_SYS_BUS_DEVICE,
  606. .instance_size = sizeof(PL011State),
  607. .instance_init = pl011_init,
  608. .class_init = pl011_class_init,
  609. };
  610. static void pl011_luminary_init(Object *obj)
  611. {
  612. PL011State *s = PL011(obj);
  613. s->id = pl011_id_luminary;
  614. }
  615. static const TypeInfo pl011_luminary_info = {
  616. .name = TYPE_PL011_LUMINARY,
  617. .parent = TYPE_PL011,
  618. .instance_init = pl011_luminary_init,
  619. };
  620. static void pl011_register_types(void)
  621. {
  622. type_register_static(&pl011_arm_info);
  623. type_register_static(&pl011_luminary_info);
  624. }
  625. type_init(pl011_register_types)