serial.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <serial.h>
  35. #include <linux/compiler.h>
  36. DECLARE_GLOBAL_DATA_PTR;
  37. /*
  38. * Local functions
  39. */
  40. static int ready_to_send(void)
  41. {
  42. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  43. volatile short status;
  44. do {
  45. #if defined(CONFIG_5xx_CONS_SCI1)
  46. status = immr->im_qsmcm.qsmcm_sc1sr;
  47. #else
  48. status = immr->im_qsmcm.qsmcm_sc2sr;
  49. #endif
  50. #if defined(CONFIG_WATCHDOG)
  51. reset_5xx_watchdog (immr);
  52. #endif
  53. } while ((status & SCI_TDRE) == 0);
  54. return 1;
  55. }
  56. /*
  57. * Minimal global serial functions needed to use one of the SCI modules.
  58. */
  59. static int mpc5xx_serial_init(void)
  60. {
  61. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  62. serial_setbrg();
  63. #if defined(CONFIG_5xx_CONS_SCI1)
  64. /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */
  65. immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10;
  66. immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE;
  67. #else
  68. immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10;
  69. immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE;
  70. #endif
  71. return 0;
  72. }
  73. static void mpc5xx_serial_putc(const char c)
  74. {
  75. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  76. /* Test for completition */
  77. if(ready_to_send()) {
  78. #if defined(CONFIG_5xx_CONS_SCI1)
  79. immr->im_qsmcm.qsmcm_sc1dr = (short)c;
  80. #else
  81. immr->im_qsmcm.qsmcm_sc2dr = (short)c;
  82. #endif
  83. if(c == '\n') {
  84. if(ready_to_send());
  85. #if defined(CONFIG_5xx_CONS_SCI1)
  86. immr->im_qsmcm.qsmcm_sc1dr = (short)'\r';
  87. #else
  88. immr->im_qsmcm.qsmcm_sc2dr = (short)'\r';
  89. #endif
  90. }
  91. }
  92. }
  93. static int mpc5xx_serial_getc(void)
  94. {
  95. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  96. volatile short status;
  97. unsigned char tmp;
  98. /* New data ? */
  99. do {
  100. #if defined(CONFIG_5xx_CONS_SCI1)
  101. status = immr->im_qsmcm.qsmcm_sc1sr;
  102. #else
  103. status = immr->im_qsmcm.qsmcm_sc2sr;
  104. #endif
  105. #if defined(CONFIG_WATCHDOG)
  106. reset_5xx_watchdog (immr);
  107. #endif
  108. } while ((status & SCI_RDRF) == 0);
  109. /* Read data */
  110. #if defined(CONFIG_5xx_CONS_SCI1)
  111. tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK);
  112. #else
  113. tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK);
  114. #endif
  115. return tmp;
  116. }
  117. static int mpc5xx_serial_tstc(void)
  118. {
  119. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  120. short status;
  121. /* New data character ? */
  122. #if defined(CONFIG_5xx_CONS_SCI1)
  123. status = immr->im_qsmcm.qsmcm_sc1sr;
  124. #else
  125. status = immr->im_qsmcm.qsmcm_sc2sr;
  126. #endif
  127. return (status & SCI_RDRF);
  128. }
  129. static void mpc5xx_serial_setbrg(void)
  130. {
  131. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  132. short scxbr;
  133. /* Set baudrate */
  134. scxbr = (gd->cpu_clk / (32 * gd->baudrate));
  135. #if defined(CONFIG_5xx_CONS_SCI1)
  136. immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK);
  137. #else
  138. immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK);
  139. #endif
  140. }
  141. static void mpc5xx_serial_puts(const char *s)
  142. {
  143. while (*s) {
  144. serial_putc(*s);
  145. ++s;
  146. }
  147. }
  148. static struct serial_device mpc5xx_serial_drv = {
  149. .name = "mpc5xx_serial",
  150. .start = mpc5xx_serial_init,
  151. .stop = NULL,
  152. .setbrg = mpc5xx_serial_setbrg,
  153. .putc = mpc5xx_serial_putc,
  154. .puts = mpc5xx_serial_puts,
  155. .getc = mpc5xx_serial_getc,
  156. .tstc = mpc5xx_serial_tstc,
  157. };
  158. void mpc5xx_serial_initialize(void)
  159. {
  160. serial_register(&mpc5xx_serial_drv);
  161. }
  162. __weak struct serial_device *default_serial_console(void)
  163. {
  164. return &mpc5xx_serial_drv;
  165. }