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