lpc32xx_hsuart.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. DECLARE_GLOBAL_DATA_PTR;
  25. static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE;
  26. static void lpc32xx_hsuart_set_baudrate(void)
  27. {
  28. u32 div;
  29. /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
  30. div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1;
  31. if (div > 255)
  32. div = 255;
  33. writel(div, &hsuart->rate);
  34. }
  35. static int lpc32xx_hsuart_getc(void)
  36. {
  37. while (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
  38. /* NOP */;
  39. return readl(&hsuart->rx) & HSUART_RX_DATA;
  40. }
  41. static void lpc32xx_hsuart_putc(const char c)
  42. {
  43. writel(c, &hsuart->tx);
  44. /* Wait for character to be sent */
  45. while (readl(&hsuart->level) & HSUART_LEVEL_TX)
  46. /* NOP */;
  47. }
  48. static int lpc32xx_hsuart_tstc(void)
  49. {
  50. if (readl(&hsuart->level) & HSUART_LEVEL_RX)
  51. return 1;
  52. return 0;
  53. }
  54. static void lpc32xx_hsuart_init(void)
  55. {
  56. lpc32xx_hsuart_set_baudrate();
  57. /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
  58. writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
  59. HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
  60. &hsuart->ctrl);
  61. }
  62. void serial_setbrg(void)
  63. {
  64. return lpc32xx_hsuart_set_baudrate();
  65. }
  66. void serial_putc(const char c)
  67. {
  68. lpc32xx_hsuart_putc(c);
  69. /* If \n, also do \r */
  70. if (c == '\n')
  71. lpc32xx_hsuart_putc('\r');
  72. }
  73. int serial_getc(void)
  74. {
  75. return lpc32xx_hsuart_getc();
  76. }
  77. void serial_puts(const char *s)
  78. {
  79. while (*s)
  80. serial_putc(*s++);
  81. }
  82. int serial_tstc(void)
  83. {
  84. return lpc32xx_hsuart_tstc();
  85. }
  86. int serial_init(void)
  87. {
  88. lpc32xx_hsuart_init();
  89. return 0;
  90. }