ns9750_serial.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /***********************************************************************
  2. *
  3. * Copyright (C) 2004 by FS Forth-Systeme GmbH.
  4. * All rights reserved.
  5. *
  6. * $Id: ns9750_serial.c,v 1.1 2004/02/16 10:37:20 mpietrek Exp $
  7. * @Author: Markus Pietrek
  8. * @Descr: Serial driver for the NS9750. Only one UART is supported yet.
  9. * @References: [1] NS9750 Hardware Reference/December 2003
  10. * @TODO: Implement Character GAP Timer when chip is fixed for PLL bypass
  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. ***********************************************************************/
  28. #include <common.h>
  29. #ifdef CFG_NS9750_UART
  30. #include "ns9750_bbus.h" /* for GPIOs */
  31. #include "ns9750_ser.h" /* for serial configuration */
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #define CONSOLE CONFIG_CONS_INDEX
  34. static unsigned int calcBitrateRegister( void );
  35. static unsigned int calcRxCharGapRegister( void );
  36. static char cCharsAvailable; /* Numbers of chars in unCharCache */
  37. static unsigned int unCharCache; /* unCharCache is only valid if
  38. * cCharsAvailable > 0 */
  39. /***********************************************************************
  40. * @Function: serial_init
  41. * @Return: 0
  42. * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
  43. ***********************************************************************/
  44. int serial_init( void )
  45. {
  46. unsigned int aunGPIOTxD[] = { 0, 8, 40, 44 };
  47. unsigned int aunGPIORxD[] = { 1, 9, 41, 45 };
  48. cCharsAvailable = 0;
  49. /* configure TxD and RxD pins for their special function */
  50. set_gpio_cfg_reg_val( aunGPIOTxD[ CONSOLE ],
  51. NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_OUTPUT );
  52. set_gpio_cfg_reg_val( aunGPIORxD[ CONSOLE ],
  53. NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_INPUT );
  54. /* configure serial engine */
  55. *get_ser_reg_addr_channel( NS9750_SER_CTRL_A, CONSOLE ) =
  56. NS9750_SER_CTRL_A_CE |
  57. NS9750_SER_CTRL_A_STOP |
  58. NS9750_SER_CTRL_A_WLS_8;
  59. serial_setbrg();
  60. *get_ser_reg_addr_channel( NS9750_SER_CTRL_B, CONSOLE ) =
  61. NS9750_SER_CTRL_B_RCGT;
  62. return 0;
  63. }
  64. /***********************************************************************
  65. * @Function: serial_putc
  66. * @Return: n/a
  67. * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
  68. ***********************************************************************/
  69. void serial_putc( const char c )
  70. {
  71. if (c == '\n')
  72. serial_putc( '\r' );
  73. while (!(*get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE) &
  74. NS9750_SER_STAT_A_TRDY ) ) {
  75. /* do nothing, wait for characters in FIFO sent */
  76. }
  77. *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO,
  78. CONSOLE) = c;
  79. }
  80. /***********************************************************************
  81. * @Function: serial_puts
  82. * @Return: n/a
  83. * @Descr: writes non-zero string to the FIFO.
  84. ***********************************************************************/
  85. void serial_puts( const char *s )
  86. {
  87. while (*s) {
  88. serial_putc( *s++ );
  89. }
  90. }
  91. /***********************************************************************
  92. * @Function: serial_getc
  93. * @Return: the character read
  94. * @Descr: performs only 8bit accesses to the FIFO. No error handling
  95. ***********************************************************************/
  96. int serial_getc( void )
  97. {
  98. int i;
  99. while (!serial_tstc() ) {
  100. /* do nothing, wait for incoming characters */
  101. }
  102. /* at least one character in unCharCache */
  103. i = (int) (unCharCache & 0xff);
  104. unCharCache >>= 8;
  105. cCharsAvailable--;
  106. return i;
  107. }
  108. /***********************************************************************
  109. * @Function: serial_tstc
  110. * @Return: 0 if no input available, otherwise != 0
  111. * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
  112. * unCharCache and the numbers of characters in cCharsAvailable
  113. ***********************************************************************/
  114. int serial_tstc( void )
  115. {
  116. unsigned int unRegCache;
  117. if ( cCharsAvailable )
  118. return 1;
  119. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,CONSOLE );
  120. if( unRegCache & NS9750_SER_STAT_A_RBC ) {
  121. *get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE ) =
  122. NS9750_SER_STAT_A_RBC;
  123. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,
  124. CONSOLE );
  125. }
  126. if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
  127. cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
  128. if ( !cCharsAvailable )
  129. cCharsAvailable = 4;
  130. unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
  131. CONSOLE );
  132. return 1;
  133. }
  134. /* no chars available */
  135. return 0;
  136. }
  137. void serial_setbrg( void )
  138. {
  139. *get_ser_reg_addr_channel( NS9750_SER_BITRATE, CONSOLE ) =
  140. calcBitrateRegister();
  141. *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER, CONSOLE ) =
  142. calcRxCharGapRegister();
  143. }
  144. /***********************************************************************
  145. * @Function: calcBitrateRegister
  146. * @Return: value for the serial bitrate register
  147. * @Descr: register value depends on clock frequency and baudrate
  148. ***********************************************************************/
  149. static unsigned int calcBitrateRegister( void )
  150. {
  151. return ( NS9750_SER_BITRATE_EBIT |
  152. NS9750_SER_BITRATE_CLKMUX_BCLK |
  153. NS9750_SER_BITRATE_TMODE |
  154. NS9750_SER_BITRATE_TCDR_16 |
  155. NS9750_SER_BITRATE_RCDR_16 |
  156. ( ( ( ( CONFIG_SYS_CLK_FREQ / 8 ) / /* BBUS clock,[1] Fig. 38 */
  157. ( gd->baudrate * 16 ) ) - 1 ) &
  158. NS9750_SER_BITRATE_N_MA ) );
  159. }
  160. /***********************************************************************
  161. * @Function: calcRxCharGapRegister
  162. * @Return: value for the character gap timer register
  163. * @Descr: register value depends on clock frequency and baudrate. Currently 0
  164. * is used as there is a bug with the gap timer in PLL bypass mode.
  165. ***********************************************************************/
  166. static unsigned int calcRxCharGapRegister( void )
  167. {
  168. return NS9750_SER_RX_CHAR_TIMER_TRUN;
  169. }
  170. #endif /* CFG_NS9750_UART */