serial.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * (C) Copyright 2000 - 2003
  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. * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with
  24. * changes based on the file arch/ppc/mbxboot/m8260_tty.c from the
  25. * Linux/PPC sources (m8260_tty.c had no copyright info in it).
  26. */
  27. /*
  28. * Minimal serial functions needed to use one of the PSC ports
  29. * as serial console interface.
  30. */
  31. #include <common.h>
  32. #include <mpc5xxx.h>
  33. #if defined(CONFIG_PSC_CONSOLE)
  34. #if CONFIG_PSC_CONSOLE == 1
  35. #define PSC_BASE MPC5XXX_PSC1
  36. #elif CONFIG_PSC_CONSOLE == 2
  37. #define PSC_BASE MPC5XXX_PSC2
  38. #elif CONFIG_PSC_CONSOLE == 3
  39. #define PSC_BASE MPC5XXX_PSC3
  40. #elif defined(CONFIG_MGT5100)
  41. #error CONFIG_PSC_CONSOLE must be in 1, 2 or 3
  42. #elif CONFIG_PSC_CONSOLE == 4
  43. #define PSC_BASE MPC5XXX_PSC4
  44. #elif CONFIG_PSC_CONSOLE == 5
  45. #define PSC_BASE MPC5XXX_PSC5
  46. #elif CONFIG_PSC_CONSOLE == 6
  47. #define PSC_BASE MPC5XXX_PSC6
  48. #else
  49. #error CONFIG_PSC_CONSOLE must be in 1 ... 6
  50. #endif
  51. int serial_init (void)
  52. {
  53. DECLARE_GLOBAL_DATA_PTR;
  54. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  55. unsigned long baseclk;
  56. int div;
  57. /* reset PSC */
  58. psc->command = PSC_SEL_MODE_REG_1;
  59. /* select clock sources */
  60. #if defined(CONFIG_MGT5100)
  61. psc->psc_clock_select = 0xdd00;
  62. baseclk = CFG_MPC5XXX_CLKIN / 32;
  63. #elif defined(CONFIG_MPC5200)
  64. psc->psc_clock_select = 0;
  65. baseclk = gd->ipb_clk / 32;
  66. #endif
  67. /* switch to UART mode */
  68. psc->sicr = 0;
  69. /* configure parity, bit length and so on */
  70. #if defined(CONFIG_MGT5100)
  71. psc->mode = PSC_MODE_ERR | PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  72. #elif defined(CONFIG_MPC5200)
  73. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  74. #endif
  75. psc->mode = PSC_MODE_ONE_STOP;
  76. /* set up UART divisor */
  77. div = baseclk / gd->baudrate;
  78. psc->ctur = div >> 8;
  79. psc->ctlr = div & 0xff;
  80. /* disable all interrupts */
  81. psc->psc_imr = 0;
  82. /* reset and enable Rx/Tx */
  83. psc->command = PSC_RST_RX;
  84. psc->command = PSC_RST_TX;
  85. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  86. return (0);
  87. }
  88. void
  89. serial_putc(const char c)
  90. {
  91. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  92. if (c == '\n')
  93. serial_putc('\r');
  94. /* Wait for last character to go. */
  95. while (!(psc->psc_status & PSC_SR_TXEMP))
  96. ;
  97. psc->psc_buffer_8 = c;
  98. }
  99. void
  100. serial_puts (const char *s)
  101. {
  102. while (*s) {
  103. serial_putc (*s++);
  104. }
  105. }
  106. int
  107. serial_getc(void)
  108. {
  109. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  110. /* Wait for a character to arrive. */
  111. while (!(psc->psc_status & PSC_SR_RXRDY))
  112. ;
  113. return psc->psc_buffer_8;
  114. }
  115. int
  116. serial_tstc(void)
  117. {
  118. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  119. return (psc->psc_status & PSC_SR_RXRDY);
  120. }
  121. void
  122. serial_setbrg(void)
  123. {
  124. DECLARE_GLOBAL_DATA_PTR;
  125. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  126. unsigned long baseclk, div;
  127. #if defined(CONFIG_MGT5100)
  128. baseclk = CFG_MPC5XXX_CLKIN / 32;
  129. #elif defined(CONFIG_MPC5200)
  130. baseclk = gd->ipb_clk / 32;
  131. #endif
  132. /* set up UART divisor */
  133. div = baseclk / gd->baudrate;
  134. psc->ctur = div >> 8;
  135. psc->ctlr = div & 0xff;
  136. }
  137. #endif /* CONFIG_PSC_CONSOLE */