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