serial.c 9.0 KB

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