serial.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * (C) Copyright 2003
  3. * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
  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,
  21. */
  22. /*
  23. * File: serial.c
  24. *
  25. * Discription: Serial interface driver for SCI1 and SCI2.
  26. * Since this code will be called from ROM use
  27. * only non-static local variables.
  28. *
  29. */
  30. #include <common.h>
  31. #include <watchdog.h>
  32. #include <command.h>
  33. #include <mpc5xx.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. /*
  36. * Local function prototypes
  37. */
  38. static int ready_to_send(void);
  39. /*
  40. * Minimal global serial functions needed to use one of the SCI modules.
  41. */
  42. int serial_init (void)
  43. {
  44. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  45. serial_setbrg();
  46. #if defined(CONFIG_5xx_CONS_SCI1)
  47. /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */
  48. immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10;
  49. immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE;
  50. #else
  51. immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10;
  52. immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE;
  53. #endif
  54. return 0;
  55. }
  56. void serial_putc(const char c)
  57. {
  58. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  59. /* Test for completition */
  60. if(ready_to_send()) {
  61. #if defined(CONFIG_5xx_CONS_SCI1)
  62. immr->im_qsmcm.qsmcm_sc1dr = (short)c;
  63. #else
  64. immr->im_qsmcm.qsmcm_sc2dr = (short)c;
  65. #endif
  66. if(c == '\n') {
  67. if(ready_to_send());
  68. #if defined(CONFIG_5xx_CONS_SCI1)
  69. immr->im_qsmcm.qsmcm_sc1dr = (short)'\r';
  70. #else
  71. immr->im_qsmcm.qsmcm_sc2dr = (short)'\r';
  72. #endif
  73. }
  74. }
  75. }
  76. int serial_getc(void)
  77. {
  78. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  79. volatile short status;
  80. unsigned char tmp;
  81. /* New data ? */
  82. do {
  83. #if defined(CONFIG_5xx_CONS_SCI1)
  84. status = immr->im_qsmcm.qsmcm_sc1sr;
  85. #else
  86. status = immr->im_qsmcm.qsmcm_sc2sr;
  87. #endif
  88. #if defined(CONFIG_WATCHDOG)
  89. reset_5xx_watchdog (immr);
  90. #endif
  91. } while ((status & SCI_RDRF) == 0);
  92. /* Read data */
  93. #if defined(CONFIG_5xx_CONS_SCI1)
  94. tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK);
  95. #else
  96. tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK);
  97. #endif
  98. return tmp;
  99. }
  100. int serial_tstc()
  101. {
  102. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  103. short status;
  104. /* New data character ? */
  105. #if defined(CONFIG_5xx_CONS_SCI1)
  106. status = immr->im_qsmcm.qsmcm_sc1sr;
  107. #else
  108. status = immr->im_qsmcm.qsmcm_sc2sr;
  109. #endif
  110. return (status & SCI_RDRF);
  111. }
  112. void serial_setbrg (void)
  113. {
  114. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  115. short scxbr;
  116. /* Set baudrate */
  117. scxbr = (gd->cpu_clk / (32 * gd->baudrate));
  118. #if defined(CONFIG_5xx_CONS_SCI1)
  119. immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK);
  120. #else
  121. immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK);
  122. #endif
  123. }
  124. void serial_puts (const char *s)
  125. {
  126. while (*s) {
  127. serial_putc(*s);
  128. ++s;
  129. }
  130. }
  131. int ready_to_send(void)
  132. {
  133. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  134. volatile short status;
  135. do {
  136. #if defined(CONFIG_5xx_CONS_SCI1)
  137. status = immr->im_qsmcm.qsmcm_sc1sr;
  138. #else
  139. status = immr->im_qsmcm.qsmcm_sc2sr;
  140. #endif
  141. #if defined(CONFIG_WATCHDOG)
  142. reset_5xx_watchdog (immr);
  143. #endif
  144. } while ((status & SCI_TDRE) == 0);
  145. return 1;
  146. }