asc_serial.c 8.8 KB

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