ns9750_serial.c 6.5 KB

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