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. /*
  35. * Local function prototypes
  36. */
  37. static int ready_to_send(void);
  38. /*
  39. * Minimal global serial functions needed to use one of the SCI modules.
  40. */
  41. int serial_init (void)
  42. {
  43. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  44. serial_setbrg();
  45. #if defined(CONFIG_5xx_CONS_SCI1)
  46. /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */
  47. immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10;
  48. immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE;
  49. #else
  50. immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10;
  51. immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE;
  52. #endif
  53. return 0;
  54. }
  55. void serial_putc(const char c)
  56. {
  57. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  58. /* Test for completition */
  59. if(ready_to_send()) {
  60. #if defined(CONFIG_5xx_CONS_SCI1)
  61. immr->im_qsmcm.qsmcm_sc1dr = (short)c;
  62. #else
  63. immr->im_qsmcm.qsmcm_sc2dr = (short)c;
  64. #endif
  65. if(c == '\n') {
  66. if(ready_to_send());
  67. #if defined(CONFIG_5xx_CONS_SCI1)
  68. immr->im_qsmcm.qsmcm_sc1dr = (short)'\r';
  69. #else
  70. immr->im_qsmcm.qsmcm_sc2dr = (short)'\r';
  71. #endif
  72. }
  73. }
  74. }
  75. int serial_getc(void)
  76. {
  77. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  78. volatile short status;
  79. unsigned char tmp;
  80. /* New data ? */
  81. do {
  82. #if defined(CONFIG_5xx_CONS_SCI1)
  83. status = immr->im_qsmcm.qsmcm_sc1sr;
  84. #else
  85. status = immr->im_qsmcm.qsmcm_sc2sr;
  86. #endif
  87. #if defined(CONFIG_WATCHDOG)
  88. reset_5xx_watchdog (immr);
  89. #endif
  90. } while ((status & SCI_RDRF) == 0);
  91. /* Read data */
  92. #if defined(CONFIG_5xx_CONS_SCI1)
  93. tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK);
  94. #else
  95. tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK);
  96. #endif
  97. return tmp;
  98. }
  99. int serial_tstc()
  100. {
  101. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  102. short status;
  103. /* New data character ? */
  104. #if defined(CONFIG_5xx_CONS_SCI1)
  105. status = immr->im_qsmcm.qsmcm_sc1sr;
  106. #else
  107. status = immr->im_qsmcm.qsmcm_sc2sr;
  108. #endif
  109. return (status & SCI_RDRF);
  110. }
  111. void serial_setbrg (void)
  112. {
  113. DECLARE_GLOBAL_DATA_PTR;
  114. volatile immap_t *immr = (immap_t *)CFG_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 *)CFG_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. }