serial.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * (C) Copyright 2000 - 2007
  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. DECLARE_GLOBAL_DATA_PTR;
  32. #if defined(CONFIG_PSC_CONSOLE)
  33. static void fifo_init (volatile psc512x_t *psc)
  34. {
  35. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  36. /* reset Rx & Tx fifo slice */
  37. psc->rfcmd = PSC_FIFO_RESET_SLICE;
  38. psc->tfcmd = PSC_FIFO_RESET_SLICE;
  39. /* disable Tx & Rx FIFO interrupts */
  40. psc->rfintmask = 0;
  41. psc->tfintmask = 0;
  42. psc->tfsize = CONSOLE_FIFO_TX_SIZE | (CONSOLE_FIFO_TX_ADDR << 16);
  43. psc->rfsize = CONSOLE_FIFO_RX_SIZE | (CONSOLE_FIFO_RX_ADDR << 16);
  44. /* enable Tx & Rx FIFO slice */
  45. psc->rfcmd = PSC_FIFO_ENABLE_SLICE;
  46. psc->tfcmd = PSC_FIFO_ENABLE_SLICE;
  47. im->fifoc.fifoc_cmd = FIFOC_DISABLE_CLOCK_GATE;
  48. __asm__ volatile ("sync");
  49. }
  50. int serial_init(void)
  51. {
  52. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  53. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  54. unsigned long baseclk;
  55. int div;
  56. fifo_init (psc);
  57. /* set MR register to point to MR1 */
  58. psc->command = PSC_SEL_MODE_REG_1;
  59. /* disable Tx/Rx */
  60. psc->command = PSC_TX_DISABLE | PSC_RX_DISABLE;
  61. /* choose the prescaler by 16 for the Tx/Rx clock generation */
  62. psc->psc_clock_select = 0xdd00;
  63. /* switch to UART mode */
  64. psc->sicr = 0;
  65. /* mode register points to mr1 */
  66. /* configure parity, bit length and so on in mode register 1*/
  67. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  68. /* now, mode register points to mr2 */
  69. psc->mode = PSC_MODE_1_STOPBIT;
  70. /* calculate dividor for setting PSC CTUR and CTLR registers */
  71. baseclk = (gd->ips_clk + 8) / 16;
  72. div = (baseclk + (gd->baudrate / 2)) / gd->baudrate;
  73. psc->ctur = (div >> 8) & 0xff;
  74. /* set baudrate */
  75. psc->ctlr = div & 0xff;
  76. /* disable all interrupts */
  77. psc->psc_imr = 0;
  78. /* reset and enable Rx/Tx */
  79. psc->command = PSC_RST_RX;
  80. psc->command = PSC_RST_TX;
  81. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  82. return 0;
  83. }
  84. void serial_putc (const char c)
  85. {
  86. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  87. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  88. if (c == '\n')
  89. serial_putc ('\r');
  90. /* Wait for last character to go. */
  91. while (!(psc->psc_status & PSC_SR_TXEMP))
  92. ;
  93. psc->tfdata_8 = c;
  94. }
  95. void serial_putc_raw (const char c)
  96. {
  97. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  98. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  99. /* Wait for last character to go. */
  100. while (!(psc->psc_status & PSC_SR_TXEMP))
  101. ;
  102. psc->tfdata_8 = c;
  103. }
  104. void serial_puts (const char *s)
  105. {
  106. while (*s) {
  107. serial_putc (*s++);
  108. }
  109. }
  110. int serial_getc (void)
  111. {
  112. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  113. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  114. /* Wait for a character to arrive. */
  115. while (psc->rfstat & PSC_FIFO_EMPTY)
  116. ;
  117. return psc->rfdata_8;
  118. }
  119. int serial_tstc (void)
  120. {
  121. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  122. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  123. return !(psc->rfstat & PSC_FIFO_EMPTY);
  124. }
  125. void serial_setbrg (void)
  126. {
  127. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  128. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  129. unsigned long baseclk, div;
  130. baseclk = (gd->csb_clk + 8) / 16;
  131. div = (baseclk + (gd->baudrate / 2)) / gd->baudrate;
  132. psc->ctur = (div >> 8) & 0xFF;
  133. psc->ctlr = div & 0xff; /* set baudrate */
  134. }
  135. void serial_setrts(int s)
  136. {
  137. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  138. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  139. if (s) {
  140. /* Assert RTS (become LOW) */
  141. psc->op1 = 0x1;
  142. }
  143. else {
  144. /* Negate RTS (become HIGH) */
  145. psc->op0 = 0x1;
  146. }
  147. }
  148. int serial_getcts(void)
  149. {
  150. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  151. volatile psc512x_t *psc = (psc512x_t *) &im->psc[CONFIG_PSC_CONSOLE];
  152. return (psc->ip & 0x1) ? 0 : 1;
  153. }
  154. #endif /* CONFIG_PSC_CONSOLE */