au1x00_serial.c 2.9 KB

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