ns9750_serial.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. static int ns9750_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. static void ns9750_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_getc
  84. * @Return: the character read
  85. * @Descr: performs only 8bit accesses to the FIFO. No error handling
  86. ***********************************************************************/
  87. static int ns9750_serial_getc(void)
  88. {
  89. int i;
  90. while (!serial_tstc() ) {
  91. /* do nothing, wait for incoming characters */
  92. }
  93. /* at least one character in unCharCache */
  94. i = (int) (unCharCache & 0xff);
  95. unCharCache >>= 8;
  96. cCharsAvailable--;
  97. return i;
  98. }
  99. /***********************************************************************
  100. * @Function: serial_tstc
  101. * @Return: 0 if no input available, otherwise != 0
  102. * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
  103. * unCharCache and the numbers of characters in cCharsAvailable
  104. ***********************************************************************/
  105. static int ns9750_serial_tstc(void)
  106. {
  107. unsigned int unRegCache;
  108. if ( cCharsAvailable )
  109. return 1;
  110. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,CONSOLE );
  111. if( unRegCache & NS9750_SER_STAT_A_RBC ) {
  112. *get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE ) =
  113. NS9750_SER_STAT_A_RBC;
  114. unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,
  115. CONSOLE );
  116. }
  117. if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
  118. cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
  119. if ( !cCharsAvailable )
  120. cCharsAvailable = 4;
  121. unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
  122. CONSOLE );
  123. return 1;
  124. }
  125. /* no chars available */
  126. return 0;
  127. }
  128. static void ns9750_serial_setbrg(void)
  129. {
  130. *get_ser_reg_addr_channel( NS9750_SER_BITRATE, CONSOLE ) =
  131. calcBitrateRegister();
  132. *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER, CONSOLE ) =
  133. calcRxCharGapRegister();
  134. }
  135. /***********************************************************************
  136. * @Function: calcBitrateRegister
  137. * @Return: value for the serial bitrate register
  138. * @Descr: register value depends on clock frequency and baudrate
  139. ***********************************************************************/
  140. static unsigned int calcBitrateRegister( void )
  141. {
  142. return ( NS9750_SER_BITRATE_EBIT |
  143. NS9750_SER_BITRATE_CLKMUX_BCLK |
  144. NS9750_SER_BITRATE_TMODE |
  145. NS9750_SER_BITRATE_TCDR_16 |
  146. NS9750_SER_BITRATE_RCDR_16 |
  147. ( ( ( ( CONFIG_SYS_CLK_FREQ / 8 ) / /* BBUS clock,[1] Fig. 38 */
  148. ( gd->baudrate * 16 ) ) - 1 ) &
  149. NS9750_SER_BITRATE_N_MA ) );
  150. }
  151. /***********************************************************************
  152. * @Function: calcRxCharGapRegister
  153. * @Return: value for the character gap timer register
  154. * @Descr: register value depends on clock frequency and baudrate. Currently 0
  155. * is used as there is a bug with the gap timer in PLL bypass mode.
  156. ***********************************************************************/
  157. static unsigned int calcRxCharGapRegister( void )
  158. {
  159. return NS9750_SER_RX_CHAR_TIMER_TRUN;
  160. }
  161. static struct serial_device ns9750_serial_drv = {
  162. .name = "ns9750_serial",
  163. .start = ns9750_serial_init,
  164. .stop = NULL,
  165. .setbrg = ns9750_serial_setbrg,
  166. .putc = ns9750_serial_putc,
  167. .puts = default_serial_puts,
  168. .getc = ns9750_serial_getc,
  169. .tstc = ns9750_serial_tstc,
  170. };
  171. void ns9750_serial_initialize(void)
  172. {
  173. serial_register(&ns9750_serial_drv);
  174. }
  175. __weak struct serial_device *default_serial_console(void)
  176. {
  177. return &ns9750_serial_drv;
  178. }