ns9750_serial.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #define CONSOLE CONFIG_CONS_INDEX
  33. static unsigned int calcBitrateRegister( void );
  34. static unsigned int calcRxCharGapRegister( void );
  35. static char cCharsAvailable; /* Numbers of chars in unCharCache */
  36. static unsigned int unCharCache; /* unCharCache is only valid if
  37. * cCharsAvailable > 0 */
  38. /***********************************************************************
  39. * @Function: serial_init
  40. * @Return: 0
  41. * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
  42. ***********************************************************************/
  43. int serial_init( void )
  44. {
  45. unsigned int aunGPIOTxD[] = { 0, 8, 40, 44 };
  46. unsigned int aunGPIORxD[] = { 1, 9, 41, 45 };
  47. cCharsAvailable = 0;
  48. /* configure TxD and RxD pins for their special function */
  49. set_gpio_cfg_reg_val( aunGPIOTxD[ CONSOLE ],
  50. NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_OUTPUT );
  51. set_gpio_cfg_reg_val( aunGPIORxD[ CONSOLE ],
  52. NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_INPUT );
  53. /* configure serial engine */
  54. *get_ser_reg_addr_channel( NS9750_SER_CTRL_A, CONSOLE ) =
  55. NS9750_SER_CTRL_A_CE |
  56. NS9750_SER_CTRL_A_STOP |
  57. NS9750_SER_CTRL_A_WLS_8;
  58. serial_setbrg();
  59. *get_ser_reg_addr_channel( NS9750_SER_CTRL_B, CONSOLE ) =
  60. NS9750_SER_CTRL_B_RCGT;
  61. return 0;
  62. }
  63. /***********************************************************************
  64. * @Function: serial_putc
  65. * @Return: n/a
  66. * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
  67. ***********************************************************************/
  68. void serial_putc( const char c )
  69. {
  70. if (c == '\n')
  71. serial_putc( '\r' );
  72. while (!(*get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE) &
  73. NS9750_SER_STAT_A_TRDY ) ) {
  74. /* do nothing, wait for characters in FIFO sent */
  75. }
  76. *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO,
  77. CONSOLE) = c;
  78. }
  79. /***********************************************************************
  80. * @Function: serial_puts
  81. * @Return: n/a
  82. * @Descr: writes non-zero string to the FIFO.
  83. ***********************************************************************/
  84. void serial_puts( const char *s )
  85. {
  86. while (*s) {
  87. serial_putc( *s++ );
  88. }
  89. }
  90. /***********************************************************************
  91. * @Function: serial_getc
  92. * @Return: the character read
  93. * @Descr: performs only 8bit accesses to the FIFO. No error handling
  94. ***********************************************************************/
  95. int serial_getc( void )
  96. {
  97. int i;
  98. while (!serial_tstc() ) {
  99. /* do nothing, wait for incoming characters */
  100. }
  101. /* at least one character in unCharCache */
  102. i = (int) (unCharCache & 0xff);
  103. unCharCache >>= 8;
  104. cCharsAvailable--;
  105. return i;
  106. }
  107. /***********************************************************************
  108. * @Function: serial_tstc
  109. * @Return: 0 if no input available, otherwise != 0
  110. * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
  111. * unCharCache and the numbers of characters in cCharsAvailable
  112. ***********************************************************************/
  113. int serial_tstc( void )
  114. {
  115. unsigned int unRegCache;
  116. if ( cCharsAvailable )
  117. return 1;
  118. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,CONSOLE );
  119. if( unRegCache & NS9750_SER_STAT_A_RBC ) {
  120. *get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE ) =
  121. NS9750_SER_STAT_A_RBC;
  122. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,
  123. CONSOLE );
  124. }
  125. if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
  126. cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
  127. if ( !cCharsAvailable )
  128. cCharsAvailable = 4;
  129. unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
  130. CONSOLE );
  131. return 1;
  132. }
  133. /* no chars available */
  134. return 0;
  135. }
  136. void serial_setbrg( void )
  137. {
  138. *get_ser_reg_addr_channel( NS9750_SER_BITRATE, CONSOLE ) =
  139. calcBitrateRegister();
  140. *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER, CONSOLE ) =
  141. calcRxCharGapRegister();
  142. }
  143. /***********************************************************************
  144. * @Function: calcBitrateRegister
  145. * @Return: value for the serial bitrate register
  146. * @Descr: register value depends on clock frequency and baudrate
  147. ***********************************************************************/
  148. static unsigned int calcBitrateRegister( void )
  149. {
  150. DECLARE_GLOBAL_DATA_PTR;
  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. DECLARE_GLOBAL_DATA_PTR;
  169. return NS9750_SER_RX_CHAR_TIMER_TRUN;
  170. }
  171. #endif /* CFG_NS9750_UART */