au1x00_serial.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <config.h>
  28. #ifdef CONFIG_SOC_AU1X00
  29. #include <common.h>
  30. #include <asm/au1x00.h>
  31. /******************************************************************************
  32. *
  33. * serial_init - initialize a channel
  34. *
  35. * This routine initializes the number of data bits, parity
  36. * and set the selected baud rate. Interrupts are disabled.
  37. * Set the modem control signals if the option is selected.
  38. *
  39. * RETURNS: N/A
  40. */
  41. int serial_init (void)
  42. {
  43. volatile u32 *uart_fifoctl = (volatile u32*)(UART0_ADDR+UART_FCR);
  44. volatile u32 *uart_enable = (volatile u32*)(UART0_ADDR+UART_ENABLE);
  45. /* Enable clocks first */
  46. *uart_enable = UART_EN_CE;
  47. /* Then release reset */
  48. /* Must release reset before setting other regs */
  49. *uart_enable = UART_EN_CE|UART_EN_E;
  50. /* Activate fifos, reset tx and rx */
  51. /* Set tx trigger level to 12 */
  52. *uart_fifoctl = UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|
  53. UART_FCR_CLEAR_XMIT|UART_FCR_T_TRIGGER_12;
  54. serial_setbrg();
  55. return 0;
  56. }
  57. void serial_setbrg (void)
  58. {
  59. volatile u32 *uart_clk = (volatile u32*)(UART0_ADDR+UART_CLK);
  60. volatile u32 *uart_lcr = (volatile u32*)(UART0_ADDR+UART_LCR);
  61. volatile u32 *sys_powerctrl = (u32 *)SYS_POWERCTRL;
  62. int sd;
  63. int divisorx2;
  64. /* sd is system clock divisor */
  65. /* see section 10.4.5 in au1550 datasheet */
  66. sd = (*sys_powerctrl & 0x03) + 2;
  67. /* calulate 2x baudrate and round */
  68. divisorx2 = ((CFG_HZ/(sd * 16 * CONFIG_BAUDRATE)));
  69. if (divisorx2 & 0x01)
  70. divisorx2 = divisorx2 + 1;
  71. *uart_clk = divisorx2 / 2;
  72. /* Set parity, stop bits and word length to 8N1 */
  73. *uart_lcr = UART_LCR_WLEN8;
  74. }
  75. void serial_putc (const char c)
  76. {
  77. volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
  78. volatile u32 *uart_tx = (volatile u32*)(UART0_ADDR+UART_TX);
  79. if (c == '\n') serial_putc ('\r');
  80. /* Wait for fifo to shift out some bytes */
  81. while((*uart_lsr&UART_LSR_THRE)==0);
  82. *uart_tx = (u32)c;
  83. }
  84. void serial_puts (const char *s)
  85. {
  86. while (*s)
  87. {
  88. serial_putc (*s++);
  89. }
  90. }
  91. int serial_getc (void)
  92. {
  93. volatile u32 *uart_rx = (volatile u32*)(UART0_ADDR+UART_RX);
  94. char c;
  95. while (!serial_tstc());
  96. c = (*uart_rx&0xFF);
  97. return c;
  98. }
  99. int serial_tstc (void)
  100. {
  101. volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
  102. if(*uart_lsr&UART_LSR_DR){
  103. /* Data in rfifo */
  104. return(1);
  105. }
  106. return 0;
  107. }
  108. #endif /* CONFIG_SOC_AU1X00 */