serial.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * (C) Copyright 2000 - 2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * Based ont the MPC5200 PSC driver.
  24. * Adapted for MPC512x by Jan Wrobel <wrr@semihalf.com>
  25. */
  26. /*
  27. * Minimal serial functions needed to use one of the PSC ports
  28. * as serial console interface.
  29. */
  30. #include <common.h>
  31. #include <asm/io.h>
  32. #include <asm/processor.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #if defined(CONFIG_PSC_CONSOLE)
  35. static void fifo_init (volatile psc512x_t *psc)
  36. {
  37. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  38. /* reset Rx & Tx fifo slice */
  39. out_be32(&psc->rfcmd, PSC_FIFO_RESET_SLICE);
  40. out_be32(&psc->tfcmd, PSC_FIFO_RESET_SLICE);
  41. /* disable Tx & Rx FIFO interrupts */
  42. out_be32(&psc->rfintmask, 0);
  43. out_be32(&psc->tfintmask, 0);
  44. out_be32(&psc->tfsize, CONSOLE_FIFO_TX_SIZE | (CONSOLE_FIFO_TX_ADDR << 16));
  45. out_be32(&psc->rfsize, CONSOLE_FIFO_RX_SIZE | (CONSOLE_FIFO_RX_ADDR << 16));
  46. /* enable Tx & Rx FIFO slice */
  47. out_be32(&psc->rfcmd, PSC_FIFO_ENABLE_SLICE);
  48. out_be32(&psc->tfcmd, PSC_FIFO_ENABLE_SLICE);
  49. out_be32(&im->fifoc.fifoc_cmd, FIFOC_DISABLE_CLOCK_GATE);
  50. __asm__ volatile ("sync");
  51. }
  52. int serial_init(void)
  53. {
  54. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  55. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  56. unsigned long baseclk;
  57. int div;
  58. fifo_init (psc);
  59. /* set MR register to point to MR1 */
  60. out_8(&psc->command, PSC_SEL_MODE_REG_1);
  61. /* disable Tx/Rx */
  62. out_8(&psc->command, PSC_TX_DISABLE | PSC_RX_DISABLE);
  63. /* choose the prescaler by 16 for the Tx/Rx clock generation */
  64. out_be16(&psc->psc_clock_select, 0xdd00);
  65. /* switch to UART mode */
  66. out_be32(&psc->sicr, 0);
  67. /* mode register points to mr1 */
  68. /* configure parity, bit length and so on in mode register 1*/
  69. out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
  70. /* now, mode register points to mr2 */
  71. out_8(&psc->mode, PSC_MODE_1_STOPBIT);
  72. /* calculate dividor for setting PSC CTUR and CTLR registers */
  73. baseclk = (gd->ips_clk + 8) / 16;
  74. div = (baseclk + (gd->baudrate / 2)) / gd->baudrate;
  75. out_8(&psc->ctur, (div >> 8) & 0xff);
  76. /* set baudrate */
  77. out_8(&psc->ctlr, div & 0xff);
  78. /* disable all interrupts */
  79. out_be16(&psc->psc_imr, 0);
  80. /* reset and enable Rx/Tx */
  81. out_8(&psc->command, PSC_RST_RX);
  82. out_8(&psc->command, PSC_RST_TX);
  83. out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
  84. return 0;
  85. }
  86. void serial_putc (const char c)
  87. {
  88. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  89. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  90. if (c == '\n')
  91. serial_putc ('\r');
  92. /* Wait for last character to go. */
  93. while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
  94. ;
  95. psc->tfdata_8 = c;
  96. }
  97. void serial_putc_raw (const char c)
  98. {
  99. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  100. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  101. /* Wait for last character to go. */
  102. while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP))
  103. ;
  104. psc->tfdata_8 = c;
  105. }
  106. void serial_puts (const char *s)
  107. {
  108. while (*s) {
  109. serial_putc (*s++);
  110. }
  111. }
  112. int serial_getc (void)
  113. {
  114. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  115. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  116. /* Wait for a character to arrive. */
  117. while (in_be32(&psc->rfstat) & PSC_FIFO_EMPTY)
  118. ;
  119. return psc->rfdata_8;
  120. }
  121. int serial_tstc (void)
  122. {
  123. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  124. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  125. return !(in_be32(&psc->rfstat) & PSC_FIFO_EMPTY);
  126. }
  127. void serial_setbrg (void)
  128. {
  129. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  130. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  131. unsigned long baseclk, div;
  132. baseclk = (gd->ips_clk + 8) / 16;
  133. div = (baseclk + (gd->baudrate / 2)) / gd->baudrate;
  134. out_8(&psc->ctur, (div >> 8) & 0xFF);
  135. out_8(&psc->ctlr, div & 0xff); /* set baudrate */
  136. }
  137. void serial_setrts(int s)
  138. {
  139. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  140. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  141. if (s) {
  142. /* Assert RTS (become LOW) */
  143. out_8(&psc->op1, 0x1);
  144. }
  145. else {
  146. /* Negate RTS (become HIGH) */
  147. out_8(&psc->op0, 0x1);
  148. }
  149. }
  150. int serial_getcts(void)
  151. {
  152. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  153. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  154. return (in_8(&psc->ip) & 0x1) ? 0 : 1;
  155. }
  156. #endif /* CONFIG_PSC_CONSOLE */