serial_netarm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Serial Port stuff - taken from Linux
  3. *
  4. * (C) Copyright 2002
  5. * MAZeT GmbH <www.mazet.de>
  6. * Stephan Linz <linz@mazet.de>, <linz@li-pro.net>
  7. *
  8. * (c) 2004
  9. * IMMS gGmbH <www.imms.de>
  10. * Thomas Elste <info@elste.org>
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <common.h>
  31. #include <asm/arch/netarm_registers.h>
  32. #ifdef CONFIG_NETARM
  33. #define PORTA (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_PORTA))
  34. #define PORTB (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_PORTB))
  35. /* wait until transmitter is ready for another character */
  36. #define TXWAITRDY(registers) \
  37. { \
  38. ulong tmo = get_timer(0) + 1 * CFG_HZ; \
  39. while (((registers)->status_a & NETARM_SER_STATA_TX_RDY) == 0 ) { \
  40. if (get_timer(0) > tmo) \
  41. break; \
  42. } \
  43. }
  44. volatile netarm_serial_channel_t *serial_reg_ch1 = get_serial_channel(0);
  45. volatile netarm_serial_channel_t *serial_reg_ch2 = get_serial_channel(1);
  46. extern void _netarm_led_FAIL1(void);
  47. /*
  48. * Setup both serial i/f with given baudrate
  49. */
  50. void serial_setbrg (void)
  51. {
  52. /* get the gd pointer */
  53. DECLARE_GLOBAL_DATA_PTR;
  54. /* set 0 ... make sure pins are configured for serial */
  55. PORTA = PORTB =
  56. NETARM_GEN_PORT_MODE (0xef) | NETARM_GEN_PORT_DIR (0xe0);
  57. /* first turn em off */
  58. serial_reg_ch1->ctrl_a = serial_reg_ch2->ctrl_a = 0;
  59. /* clear match register, we don't need it */
  60. serial_reg_ch1->rx_match = serial_reg_ch2->rx_match = 0;
  61. /* setup bit rate generator and rx buffer gap timer (1 byte only) */
  62. if ((gd->baudrate >= MIN_BAUD_RATE)
  63. && (gd->baudrate <= MAX_BAUD_RATE)) {
  64. serial_reg_ch1->bitrate = serial_reg_ch2->bitrate =
  65. NETARM_SER_BR_X16 (gd->baudrate);
  66. serial_reg_ch1->rx_buf_timer = serial_reg_ch2->rx_buf_timer =
  67. 0;
  68. serial_reg_ch1->rx_char_timer = serial_reg_ch2->rx_char_timer =
  69. NETARM_SER_RXGAP (gd->baudrate);
  70. } else {
  71. hang ();
  72. }
  73. /* setup port mode */
  74. serial_reg_ch1->ctrl_b = serial_reg_ch2->ctrl_b =
  75. ( NETARM_SER_CTLB_RCGT_EN |
  76. NETARM_SER_CTLB_UART_MODE);
  77. serial_reg_ch1->ctrl_a = serial_reg_ch2->ctrl_a =
  78. ( NETARM_SER_CTLA_ENABLE |
  79. NETARM_SER_CTLA_P_NONE |
  80. /* see errata */
  81. NETARM_SER_CTLA_2STOP |
  82. NETARM_SER_CTLA_8BITS |
  83. NETARM_SER_CTLA_DTR_EN |
  84. NETARM_SER_CTLA_RTS_EN);
  85. }
  86. /*
  87. * Initialise the serial port with the given baudrate. The settings
  88. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  89. */
  90. int serial_init (void)
  91. {
  92. serial_setbrg ();
  93. return 0;
  94. }
  95. /*
  96. * Output a single byte to the serial port.
  97. */
  98. void serial_putc (const char c)
  99. {
  100. volatile unsigned char *fifo;
  101. /* If \n, also do \r */
  102. if (c == '\n')
  103. serial_putc ('\r');
  104. fifo = (volatile unsigned char *) &(serial_reg_ch1->fifo);
  105. TXWAITRDY (serial_reg_ch1);
  106. *fifo = c;
  107. }
  108. /*
  109. * Test of a single byte from the serial port. Returns 1 on success, 0
  110. * otherwise.
  111. */
  112. int serial_tstc(void)
  113. {
  114. return serial_reg_ch1->status_a & NETARM_SER_STATA_RX_RDY;
  115. }
  116. /*
  117. * Read a single byte from the serial port. Returns 1 on success, 0
  118. * otherwise.
  119. */
  120. int serial_getc (void)
  121. {
  122. unsigned int ch_uint;
  123. volatile unsigned int *fifo;
  124. volatile unsigned char *fifo_char = NULL;
  125. int buf_count = 0;
  126. while (!(serial_reg_ch1->status_a & NETARM_SER_STATA_RX_RDY))
  127. /* NOP */ ;
  128. fifo = (volatile unsigned int *) &(serial_reg_ch1->fifo);
  129. fifo_char = (unsigned char *) &ch_uint;
  130. ch_uint = *fifo;
  131. buf_count = NETARM_SER_STATA_RXFDB (serial_reg_ch1->status_a);
  132. switch (buf_count) {
  133. case NETARM_SER_STATA_RXFDB_4BYTES:
  134. buf_count = 4;
  135. break;
  136. case NETARM_SER_STATA_RXFDB_3BYTES:
  137. buf_count = 3;
  138. break;
  139. case NETARM_SER_STATA_RXFDB_2BYTES:
  140. buf_count = 2;
  141. break;
  142. case NETARM_SER_STATA_RXFDB_1BYTES:
  143. buf_count = 1;
  144. break;
  145. default:
  146. /* panic, be never here */
  147. }
  148. serial_reg_ch1->status_a |= NETARM_SER_STATA_RX_CLOSED;
  149. return ch_uint & 0xff;
  150. }
  151. void serial_puts (const char *s)
  152. {
  153. while (*s) {
  154. serial_putc (*s++);
  155. }
  156. }
  157. #endif /* CONFIG_NETARM */