serial.c 3.4 KB

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