lpc32xx_hsuart.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/arch/cpu.h>
  8. #include <asm/arch/clk.h>
  9. #include <asm/arch/uart.h>
  10. #include <asm/io.h>
  11. #include <serial.h>
  12. #include <linux/compiler.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE;
  15. static void lpc32xx_serial_setbrg(void)
  16. {
  17. u32 div;
  18. /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
  19. div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1;
  20. if (div > 255)
  21. div = 255;
  22. writel(div, &hsuart->rate);
  23. }
  24. static int lpc32xx_serial_getc(void)
  25. {
  26. while (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
  27. /* NOP */;
  28. return readl(&hsuart->rx) & HSUART_RX_DATA;
  29. }
  30. static void lpc32xx_serial_putc(const char c)
  31. {
  32. if (c == '\n')
  33. serial_putc('\r');
  34. writel(c, &hsuart->tx);
  35. /* Wait for character to be sent */
  36. while (readl(&hsuart->level) & HSUART_LEVEL_TX)
  37. /* NOP */;
  38. }
  39. static int lpc32xx_serial_tstc(void)
  40. {
  41. if (readl(&hsuart->level) & HSUART_LEVEL_RX)
  42. return 1;
  43. return 0;
  44. }
  45. static int lpc32xx_serial_init(void)
  46. {
  47. lpc32xx_serial_setbrg();
  48. /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
  49. writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
  50. HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
  51. &hsuart->ctrl);
  52. return 0;
  53. }
  54. static struct serial_device lpc32xx_serial_drv = {
  55. .name = "lpc32xx_serial",
  56. .start = lpc32xx_serial_init,
  57. .stop = NULL,
  58. .setbrg = lpc32xx_serial_setbrg,
  59. .putc = lpc32xx_serial_putc,
  60. .puts = default_serial_puts,
  61. .getc = lpc32xx_serial_getc,
  62. .tstc = lpc32xx_serial_tstc,
  63. };
  64. void lpc32xx_serial_initialize(void)
  65. {
  66. serial_register(&lpc32xx_serial_drv);
  67. }
  68. __weak struct serial_device *default_serial_console(void)
  69. {
  70. return &lpc32xx_serial_drv;
  71. }