lpc32xx_hsuart.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <common.h>
  20. #include <asm/arch/cpu.h>
  21. #include <asm/arch/clk.h>
  22. #include <asm/arch/uart.h>
  23. #include <asm/io.h>
  24. #include <serial.h>
  25. #include <linux/compiler.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE;
  28. static void lpc32xx_serial_setbrg(void)
  29. {
  30. u32 div;
  31. /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
  32. div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1;
  33. if (div > 255)
  34. div = 255;
  35. writel(div, &hsuart->rate);
  36. }
  37. static int lpc32xx_serial_getc(void)
  38. {
  39. while (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
  40. /* NOP */;
  41. return readl(&hsuart->rx) & HSUART_RX_DATA;
  42. }
  43. static void lpc32xx_serial_putc(const char c)
  44. {
  45. writel(c, &hsuart->tx);
  46. /* Wait for character to be sent */
  47. while (readl(&hsuart->level) & HSUART_LEVEL_TX)
  48. /* NOP */;
  49. }
  50. static int lpc32xx_serial_tstc(void)
  51. {
  52. if (readl(&hsuart->level) & HSUART_LEVEL_RX)
  53. return 1;
  54. return 0;
  55. }
  56. static int lpc32xx_serial_init(void)
  57. {
  58. lpc32xx_serial_setbrg();
  59. /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
  60. writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
  61. HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
  62. &hsuart->ctrl);
  63. return 0;
  64. }
  65. static void lpc32xx_serial_puts(const char *s)
  66. {
  67. while (*s)
  68. serial_putc(*s++);
  69. }
  70. #ifdef CONFIG_SERIAL_MULTI
  71. static struct serial_device lpc32xx_serial_drv = {
  72. .name = "lpc32xx_serial",
  73. .start = lpc32xx_serial_init,
  74. .stop = NULL,
  75. .setbrg = lpc32xx_serial_setbrg,
  76. .putc = lpc32xx_serial_putc,
  77. .puts = lpc32xx_serial_puts,
  78. .getc = lpc32xx_serial_getc,
  79. .tstc = lpc32xx_serial_tstc,
  80. };
  81. void lpc32xx_serial_initialize(void)
  82. {
  83. serial_register(&lpc32xx_serial_drv);
  84. }
  85. __weak struct serial_device *default_serial_console(void)
  86. {
  87. return &lpc32xx_serial_drv;
  88. }
  89. #else
  90. int serial_init(void)
  91. {
  92. return lpc32xx_serial_init();
  93. }
  94. void serial_setbrg(void)
  95. {
  96. lpc32xx_serial_setbrg();
  97. }
  98. void serial_putc(const char c)
  99. {
  100. lpc32xx_serial_putc(c);
  101. }
  102. void serial_puts(const char *s)
  103. {
  104. lpc32xx_serial_puts(s);
  105. }
  106. int serial_getc(void)
  107. {
  108. return lpc32xx_serial_getc();
  109. }
  110. int serial_tstc(void)
  111. {
  112. return lpc32xx_serial_tstc();
  113. }
  114. #endif