asc_serial.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * (INCA) ASC UART support
  3. */
  4. #include <config.h>
  5. #include <common.h>
  6. #include <asm/inca-ip.h>
  7. #include "asc_serial.h"
  8. #define SET_BIT(reg, mask) reg |= (mask)
  9. #define CLEAR_BIT(reg, mask) reg &= (~mask)
  10. #define CLEAR_BITS(reg, mask) CLEAR_BIT(reg, mask)
  11. #define SET_BITS(reg, mask) SET_BIT(reg, mask)
  12. #define SET_BITFIELD(reg, mask, off, val) {reg &= (~mask); reg |= (val << off);}
  13. extern uint incaip_get_fpiclk(void);
  14. static int serial_setopt (void);
  15. /* pointer to ASC register base address */
  16. static volatile incaAsc_t *pAsc = (incaAsc_t *)INCA_IP_ASC;
  17. /******************************************************************************
  18. *
  19. * serial_init - initialize a INCAASC channel
  20. *
  21. * This routine initializes the number of data bits, parity
  22. * and set the selected baud rate. Interrupts are disabled.
  23. * Set the modem control signals if the option is selected.
  24. *
  25. * RETURNS: N/A
  26. */
  27. int serial_init (void)
  28. {
  29. /* we have to set PMU.EN13 bit to enable an ASC device*/
  30. INCAASC_PMU_ENABLE(13);
  31. /* and we have to set CLC register*/
  32. CLEAR_BIT(pAsc->asc_clc, ASCCLC_DISS);
  33. SET_BITFIELD(pAsc->asc_clc, ASCCLC_RMCMASK, ASCCLC_RMCOFFSET, 0x0001);
  34. /* initialy we are in async mode */
  35. pAsc->asc_con = ASCCON_M_8ASYNC;
  36. /* select input port */
  37. pAsc->asc_pisel = (CONSOLE_TTY & 0x1);
  38. /* TXFIFO's filling level */
  39. SET_BITFIELD(pAsc->asc_txfcon, ASCTXFCON_TXFITLMASK,
  40. ASCTXFCON_TXFITLOFF, INCAASC_TXFIFO_FL);
  41. /* enable TXFIFO */
  42. SET_BIT(pAsc->asc_txfcon, ASCTXFCON_TXFEN);
  43. /* RXFIFO's filling level */
  44. SET_BITFIELD(pAsc->asc_txfcon, ASCRXFCON_RXFITLMASK,
  45. ASCRXFCON_RXFITLOFF, INCAASC_RXFIFO_FL);
  46. /* enable RXFIFO */
  47. SET_BIT(pAsc->asc_rxfcon, ASCRXFCON_RXFEN);
  48. /* enable error signals */
  49. SET_BIT(pAsc->asc_con, ASCCON_FEN);
  50. SET_BIT(pAsc->asc_con, ASCCON_OEN);
  51. /* acknowledge ASC interrupts */
  52. ASC_INTERRUPTS_CLEAR(INCAASC_IRQ_LINE_ALL);
  53. /* disable ASC interrupts */
  54. ASC_INTERRUPTS_DISABLE(INCAASC_IRQ_LINE_ALL);
  55. /* set FIFOs into the transparent mode */
  56. SET_BIT(pAsc->asc_txfcon, ASCTXFCON_TXTMEN);
  57. SET_BIT(pAsc->asc_rxfcon, ASCRXFCON_RXTMEN);
  58. /* set baud rate */
  59. serial_setbrg();
  60. /* set the options */
  61. serial_setopt();
  62. return 0;
  63. }
  64. void serial_setbrg (void)
  65. {
  66. ulong uiReloadValue, fdv;
  67. ulong f_ASC;
  68. f_ASC = incaip_get_fpiclk();
  69. #ifndef INCAASC_USE_FDV
  70. fdv = 2;
  71. uiReloadValue = (f_ASC / (fdv * 16 * CONFIG_BAUDRATE)) - 1;
  72. #else
  73. fdv = INCAASC_FDV_HIGH_BAUDRATE;
  74. uiReloadValue = (f_ASC / (8192 * CONFIG_BAUDRATE / fdv)) - 1;
  75. #endif /* INCAASC_USE_FDV */
  76. if ( (uiReloadValue < 0) || (uiReloadValue > 8191) )
  77. {
  78. #ifndef INCAASC_USE_FDV
  79. fdv = 3;
  80. uiReloadValue = (f_ASC / (fdv * 16 * CONFIG_BAUDRATE)) - 1;
  81. #else
  82. fdv = INCAASC_FDV_LOW_BAUDRATE;
  83. uiReloadValue = (f_ASC / (8192 * CONFIG_BAUDRATE / fdv)) - 1;
  84. #endif /* INCAASC_USE_FDV */
  85. if ( (uiReloadValue < 0) || (uiReloadValue > 8191) )
  86. {
  87. return; /* can't impossibly generate that baud rate */
  88. }
  89. }
  90. /* Disable Baud Rate Generator; BG should only be written when R=0 */
  91. CLEAR_BIT(pAsc->asc_con, ASCCON_R);
  92. #ifndef INCAASC_USE_FDV
  93. /*
  94. * Disable Fractional Divider (FDE)
  95. * Divide clock by reload-value + constant (BRS)
  96. */
  97. /* FDE = 0 */
  98. CLEAR_BIT(pAsc->asc_con, ASCCON_FDE);
  99. if ( fdv == 2 )
  100. CLEAR_BIT(pAsc->asc_con, ASCCON_BRS); /* BRS = 0 */
  101. else
  102. SET_BIT(pAsc->asc_con, ASCCON_BRS); /* BRS = 1 */
  103. #else /* INCAASC_USE_FDV */
  104. /* Enable Fractional Divider */
  105. SET_BIT(pAsc->asc_con, ASCCON_FDE); /* FDE = 1 */
  106. /* Set fractional divider value */
  107. pAsc->asc_fdv = fdv & ASCFDV_VALUE_MASK;
  108. #endif /* INCAASC_USE_FDV */
  109. /* Set reload value in BG */
  110. pAsc->asc_bg = uiReloadValue;
  111. /* Enable Baud Rate Generator */
  112. SET_BIT(pAsc->asc_con, ASCCON_R); /* R = 1 */
  113. }
  114. /*******************************************************************************
  115. *
  116. * serial_setopt - set the serial options
  117. *
  118. * Set the channel operating mode to that specified. Following options
  119. * are supported: CREAD, CSIZE, PARENB, and PARODD.
  120. *
  121. * Note, this routine disables the transmitter. The calling routine
  122. * may have to re-enable it.
  123. *
  124. * RETURNS:
  125. * Returns 0 to indicate success, otherwise -1 is returned
  126. */
  127. static int serial_setopt (void)
  128. {
  129. ulong con;
  130. switch ( ASC_OPTIONS & ASCOPT_CSIZE )
  131. {
  132. /* 7-bit-data */
  133. case ASCOPT_CS7:
  134. con = ASCCON_M_7ASYNCPAR; /* 7-bit-data and parity bit */
  135. break;
  136. /* 8-bit-data */
  137. case ASCOPT_CS8:
  138. if ( ASC_OPTIONS & ASCOPT_PARENB )
  139. con = ASCCON_M_8ASYNCPAR; /* 8-bit-data and parity bit */
  140. else
  141. con = ASCCON_M_8ASYNC; /* 8-bit-data no parity */
  142. break;
  143. /*
  144. * only 7 and 8-bit frames are supported
  145. * if we don't use IOCTL extensions
  146. */
  147. default:
  148. return -1;
  149. }
  150. if ( ASC_OPTIONS & ASCOPT_STOPB )
  151. SET_BIT(con, ASCCON_STP); /* 2 stop bits */
  152. else
  153. CLEAR_BIT(con, ASCCON_STP); /* 1 stop bit */
  154. if ( ASC_OPTIONS & ASCOPT_PARENB )
  155. SET_BIT(con, ASCCON_PEN); /* enable parity checking */
  156. else
  157. CLEAR_BIT(con, ASCCON_PEN); /* disable parity checking */
  158. if ( ASC_OPTIONS & ASCOPT_PARODD )
  159. SET_BIT(con, ASCCON_ODD); /* odd parity */
  160. else
  161. CLEAR_BIT(con, ASCCON_ODD); /* even parity */
  162. if ( ASC_OPTIONS & ASCOPT_CREAD )
  163. SET_BIT(pAsc->asc_whbcon, ASCWHBCON_SETREN); /* Receiver enable */
  164. pAsc->asc_con |= con;
  165. return 0;
  166. }
  167. void serial_putc (const char c)
  168. {
  169. uint txFl = 0;
  170. if (c == '\n') serial_putc ('\r');
  171. /* check do we have a free space in the TX FIFO */
  172. /* get current filling level */
  173. do
  174. {
  175. txFl = ( pAsc->asc_fstat & ASCFSTAT_TXFFLMASK ) >> ASCFSTAT_TXFFLOFF;
  176. }
  177. while ( txFl == INCAASC_TXFIFO_FULL );
  178. pAsc->asc_tbuf = c; /* write char to Transmit Buffer Register */
  179. /* check for errors */
  180. if ( pAsc->asc_con & ASCCON_OE )
  181. {
  182. SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLROE);
  183. return;
  184. }
  185. }
  186. void serial_puts (const char *s)
  187. {
  188. while (*s)
  189. {
  190. serial_putc (*s++);
  191. }
  192. }
  193. int serial_getc (void)
  194. {
  195. ulong symbol_mask;
  196. char c;
  197. while (!serial_tstc());
  198. symbol_mask =
  199. ((ASC_OPTIONS & ASCOPT_CSIZE) == ASCOPT_CS7) ? (0x7f) : (0xff);
  200. c = (char)(pAsc->asc_rbuf & symbol_mask);
  201. return c;
  202. }
  203. int serial_tstc (void)
  204. {
  205. int res = 1;
  206. if ( (pAsc->asc_fstat & ASCFSTAT_RXFFLMASK) == 0 )
  207. {
  208. res = 0;
  209. }
  210. else if ( pAsc->asc_con & ASCCON_FE )
  211. {
  212. SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLRFE);
  213. res = 0;
  214. }
  215. else if ( pAsc->asc_con & ASCCON_PE )
  216. {
  217. SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLRPE);
  218. res = 0;
  219. }
  220. else if ( pAsc->asc_con & ASCCON_OE )
  221. {
  222. SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLROE);
  223. res = 0;
  224. }
  225. return res;
  226. }