serial_pic32.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (c) 2015 Paul Thacker <paul.thacker@microchip.com>
  4. *
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <serial.h>
  10. #include <wait_bit.h>
  11. #include <mach/pic32.h>
  12. #include <dt-bindings/clock/microchip,clock.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* UART Control Registers */
  15. #define U_MOD 0x00
  16. #define U_MODCLR (U_MOD + _CLR_OFFSET)
  17. #define U_MODSET (U_MOD + _SET_OFFSET)
  18. #define U_STA 0x10
  19. #define U_STACLR (U_STA + _CLR_OFFSET)
  20. #define U_STASET (U_STA + _SET_OFFSET)
  21. #define U_TXR 0x20
  22. #define U_RXR 0x30
  23. #define U_BRG 0x40
  24. /* U_MOD bits */
  25. #define UART_ENABLE BIT(15)
  26. /* U_STA bits */
  27. #define UART_RX_ENABLE BIT(12)
  28. #define UART_TX_BRK BIT(11)
  29. #define UART_TX_ENABLE BIT(10)
  30. #define UART_TX_FULL BIT(9)
  31. #define UART_TX_EMPTY BIT(8)
  32. #define UART_RX_OVER BIT(1)
  33. #define UART_RX_DATA_AVAIL BIT(0)
  34. struct pic32_uart_priv {
  35. void __iomem *base;
  36. ulong uartclk;
  37. };
  38. /*
  39. * Initialize the serial port with the given baudrate.
  40. * The settings are always 8 data bits, no parity, 1 stop bit, no start bits.
  41. */
  42. static int pic32_serial_init(void __iomem *base, ulong clk, u32 baudrate)
  43. {
  44. u32 div = DIV_ROUND_CLOSEST(clk, baudrate * 16);
  45. /* wait for TX FIFO to empty */
  46. wait_for_bit_le32(base + U_STA, UART_TX_EMPTY,
  47. true, CONFIG_SYS_HZ, false);
  48. /* send break */
  49. writel(UART_TX_BRK, base + U_STASET);
  50. /* disable and clear mode */
  51. writel(0, base + U_MOD);
  52. writel(0, base + U_STA);
  53. /* set baud rate generator */
  54. writel(div - 1, base + U_BRG);
  55. /* enable the UART for TX and RX */
  56. writel(UART_TX_ENABLE | UART_RX_ENABLE, base + U_STASET);
  57. /* enable the UART */
  58. writel(UART_ENABLE, base + U_MODSET);
  59. return 0;
  60. }
  61. /* Check whether any char pending in RX fifo */
  62. static int pic32_uart_pending_input(void __iomem *base)
  63. {
  64. /* check if rx buffer overrun error has occurred */
  65. if (readl(base + U_STA) & UART_RX_OVER) {
  66. readl(base + U_RXR);
  67. /* clear overrun error to keep receiving */
  68. writel(UART_RX_OVER, base + U_STACLR);
  69. }
  70. /* In PIC32 there is no way to know number of outstanding
  71. * chars in rx-fifo. Only it can be known whether there is any.
  72. */
  73. return readl(base + U_STA) & UART_RX_DATA_AVAIL;
  74. }
  75. static int pic32_uart_pending(struct udevice *dev, bool input)
  76. {
  77. struct pic32_uart_priv *priv = dev_get_priv(dev);
  78. if (input)
  79. return pic32_uart_pending_input(priv->base);
  80. return !(readl(priv->base + U_STA) & UART_TX_EMPTY);
  81. }
  82. static int pic32_uart_setbrg(struct udevice *dev, int baudrate)
  83. {
  84. struct pic32_uart_priv *priv = dev_get_priv(dev);
  85. return pic32_serial_init(priv->base, priv->uartclk, baudrate);
  86. }
  87. static int pic32_uart_putc(struct udevice *dev, const char ch)
  88. {
  89. struct pic32_uart_priv *priv = dev_get_priv(dev);
  90. /* Check if Tx FIFO is full */
  91. if (readl(priv->base + U_STA) & UART_TX_FULL)
  92. return -EAGAIN;
  93. /* pump the char to tx buffer */
  94. writel(ch, priv->base + U_TXR);
  95. return 0;
  96. }
  97. static int pic32_uart_getc(struct udevice *dev)
  98. {
  99. struct pic32_uart_priv *priv = dev_get_priv(dev);
  100. /* return error if RX fifo is empty */
  101. if (!pic32_uart_pending_input(priv->base))
  102. return -EAGAIN;
  103. /* read the character from rx buffer */
  104. return readl(priv->base + U_RXR) & 0xff;
  105. }
  106. static int pic32_uart_probe(struct udevice *dev)
  107. {
  108. struct pic32_uart_priv *priv = dev_get_priv(dev);
  109. struct clk clk;
  110. fdt_addr_t addr;
  111. fdt_size_t size;
  112. int ret;
  113. /* get address */
  114. addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
  115. &size);
  116. if (addr == FDT_ADDR_T_NONE)
  117. return -EINVAL;
  118. priv->base = ioremap(addr, size);
  119. /* get clock rate */
  120. ret = clk_get_by_index(dev, 0, &clk);
  121. if (ret < 0)
  122. return ret;
  123. priv->uartclk = clk_get_rate(&clk);
  124. clk_free(&clk);
  125. /* initialize serial */
  126. return pic32_serial_init(priv->base, priv->uartclk, CONFIG_BAUDRATE);
  127. }
  128. static const struct dm_serial_ops pic32_uart_ops = {
  129. .putc = pic32_uart_putc,
  130. .pending = pic32_uart_pending,
  131. .getc = pic32_uart_getc,
  132. .setbrg = pic32_uart_setbrg,
  133. };
  134. static const struct udevice_id pic32_uart_ids[] = {
  135. { .compatible = "microchip,pic32mzda-uart" },
  136. {}
  137. };
  138. U_BOOT_DRIVER(pic32_serial) = {
  139. .name = "pic32-uart",
  140. .id = UCLASS_SERIAL,
  141. .of_match = pic32_uart_ids,
  142. .probe = pic32_uart_probe,
  143. .ops = &pic32_uart_ops,
  144. .flags = DM_FLAG_PRE_RELOC,
  145. .priv_auto_alloc_size = sizeof(struct pic32_uart_priv),
  146. };
  147. #ifdef CONFIG_DEBUG_UART_PIC32
  148. #include <debug_uart.h>
  149. static inline void _debug_uart_init(void)
  150. {
  151. void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
  152. pic32_serial_init(base, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  153. }
  154. static inline void _debug_uart_putc(int ch)
  155. {
  156. writel(ch, CONFIG_DEBUG_UART_BASE + U_TXR);
  157. }
  158. DEBUG_UART_FUNCS
  159. #endif