lpc32xx_hsuart.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 struct serial_device lpc32xx_serial_drv = {
  66. .name = "lpc32xx_serial",
  67. .start = lpc32xx_serial_init,
  68. .stop = NULL,
  69. .setbrg = lpc32xx_serial_setbrg,
  70. .putc = lpc32xx_serial_putc,
  71. .puts = default_serial_puts,
  72. .getc = lpc32xx_serial_getc,
  73. .tstc = lpc32xx_serial_tstc,
  74. };
  75. void lpc32xx_serial_initialize(void)
  76. {
  77. serial_register(&lpc32xx_serial_drv);
  78. }
  79. __weak struct serial_device *default_serial_console(void)
  80. {
  81. return &lpc32xx_serial_drv;
  82. }