au1x00_serial.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * AU1X00 UART support
  3. *
  4. * Hardcoded to UART 0 for now
  5. * Speed and options also hardcoded to 115200 8N1
  6. *
  7. * Copyright (c) 2003 Thomas.Lange@corelatus.se
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <mach/au1x00.h>
  14. #include <serial.h>
  15. #include <linux/compiler.h>
  16. /******************************************************************************
  17. *
  18. * serial_init - initialize a channel
  19. *
  20. * This routine initializes the number of data bits, parity
  21. * and set the selected baud rate. Interrupts are disabled.
  22. * Set the modem control signals if the option is selected.
  23. *
  24. * RETURNS: N/A
  25. */
  26. static int au1x00_serial_init(void)
  27. {
  28. volatile u32 *uart_fifoctl = (volatile u32*)(UART0_ADDR+UART_FCR);
  29. volatile u32 *uart_enable = (volatile u32*)(UART0_ADDR+UART_ENABLE);
  30. /* Enable clocks first */
  31. *uart_enable = UART_EN_CE;
  32. /* Then release reset */
  33. /* Must release reset before setting other regs */
  34. *uart_enable = UART_EN_CE|UART_EN_E;
  35. /* Activate fifos, reset tx and rx */
  36. /* Set tx trigger level to 12 */
  37. *uart_fifoctl = UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|
  38. UART_FCR_CLEAR_XMIT|UART_FCR_T_TRIGGER_12;
  39. serial_setbrg();
  40. return 0;
  41. }
  42. static void au1x00_serial_setbrg(void)
  43. {
  44. volatile u32 *uart_clk = (volatile u32*)(UART0_ADDR+UART_CLK);
  45. volatile u32 *uart_lcr = (volatile u32*)(UART0_ADDR+UART_LCR);
  46. volatile u32 *sys_powerctrl = (u32 *)SYS_POWERCTRL;
  47. int sd;
  48. int divisorx2;
  49. /* sd is system clock divisor */
  50. /* see section 10.4.5 in au1550 datasheet */
  51. sd = (*sys_powerctrl & 0x03) + 2;
  52. /* calulate 2x baudrate and round */
  53. divisorx2 = ((CONFIG_SYS_MIPS_TIMER_FREQ/(sd * 16 * CONFIG_BAUDRATE)));
  54. if (divisorx2 & 0x01)
  55. divisorx2 = divisorx2 + 1;
  56. *uart_clk = divisorx2 / 2;
  57. /* Set parity, stop bits and word length to 8N1 */
  58. *uart_lcr = UART_LCR_WLEN8;
  59. }
  60. static void au1x00_serial_putc(const char c)
  61. {
  62. volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
  63. volatile u32 *uart_tx = (volatile u32*)(UART0_ADDR+UART_TX);
  64. if (c == '\n')
  65. au1x00_serial_putc('\r');
  66. /* Wait for fifo to shift out some bytes */
  67. while((*uart_lsr&UART_LSR_THRE)==0);
  68. *uart_tx = (u32)c;
  69. }
  70. static int au1x00_serial_getc(void)
  71. {
  72. volatile u32 *uart_rx = (volatile u32*)(UART0_ADDR+UART_RX);
  73. char c;
  74. while (!serial_tstc());
  75. c = (*uart_rx&0xFF);
  76. return c;
  77. }
  78. static int au1x00_serial_tstc(void)
  79. {
  80. volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
  81. if(*uart_lsr&UART_LSR_DR){
  82. /* Data in rfifo */
  83. return(1);
  84. }
  85. return 0;
  86. }
  87. static struct serial_device au1x00_serial_drv = {
  88. .name = "au1x00_serial",
  89. .start = au1x00_serial_init,
  90. .stop = NULL,
  91. .setbrg = au1x00_serial_setbrg,
  92. .putc = au1x00_serial_putc,
  93. .puts = default_serial_puts,
  94. .getc = au1x00_serial_getc,
  95. .tstc = au1x00_serial_tstc,
  96. };
  97. void au1x00_serial_initialize(void)
  98. {
  99. serial_register(&au1x00_serial_drv);
  100. }
  101. __weak struct serial_device *default_serial_console(void)
  102. {
  103. return &au1x00_serial_drv;
  104. }