serial_sh.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SuperH SCIF device driver.
  3. * Copyright (c) 2007 Nobuhiro Iwamatsu
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <asm/processor.h>
  21. #ifdef CFG_SCIF_CONSOLE
  22. #if defined (CONFIG_CONS_SCIF0)
  23. #define SCIF_BASE SCIF0_BASE
  24. #elif defined (CONFIG_CONS_SCIF1)
  25. #define SCIF_BASE SCIF1_BASE
  26. #else
  27. #error "Default SCIF doesn't set....."
  28. #endif
  29. #define SCSMR (vu_short *)(SCIF_BASE + 0x0)
  30. #define SCBRR (vu_char *)(SCIF_BASE + 0x4)
  31. #define SCSCR (vu_short *)(SCIF_BASE + 0x8)
  32. #define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
  33. #define SCFSR (vu_short *)(SCIF_BASE + 0x10)
  34. #define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
  35. #define SCFCR (vu_short *)(SCIF_BASE + 0x18)
  36. #define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
  37. #if defined(CONFIG_SH4A)
  38. #define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
  39. #define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
  40. #define SCLSR (vu_short *)(SCIF_BASE + 0x28)
  41. #define SCRER (vu_short *)(SCIF_BASE + 0x2C)
  42. #elif defined (CONFIG_SH4)
  43. #define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
  44. #define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  45. #elif defined (CONFIG_SH3)
  46. #define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  47. #endif
  48. #define SCR_RE (1 << 4)
  49. #define SCR_TE (1 << 5)
  50. #define FCR_RFRST (1 << 1) /* RFCL */
  51. #define FCR_TFRST (1 << 2) /* TFCL */
  52. #define FSR_DR (1 << 0)
  53. #define FSR_RDF (1 << 1)
  54. #define FSR_FER (1 << 3)
  55. #define FSR_BRK (1 << 4)
  56. #define FSR_FER (1 << 3)
  57. #define FSR_TEND (1 << 6)
  58. #define FSR_ER (1 << 7)
  59. /*----------------------------------------------------------------------*/
  60. void serial_setbrg (void)
  61. {
  62. DECLARE_GLOBAL_DATA_PTR;
  63. int divisor = gd->baudrate * 32;
  64. *SCBRR = (CONFIG_SYS_CLK_FREQ + (divisor / 2)) /
  65. (gd->baudrate * 32) - 1;
  66. }
  67. int serial_init (void)
  68. {
  69. *SCSCR = (SCR_RE | SCR_TE);
  70. *SCSMR = 0 ;
  71. *SCSMR = 0;
  72. *SCFCR = (FCR_RFRST | FCR_TFRST);
  73. *SCFCR;
  74. *SCFCR = 0;
  75. serial_setbrg();
  76. return 0;
  77. }
  78. static int serial_tx_fifo_level (void)
  79. {
  80. return (*SCFDR >> 8) & 0x1F;
  81. }
  82. static int serial_rx_fifo_level (void)
  83. {
  84. return (*SCFDR >> 0) & 0x1F;
  85. }
  86. void serial_raw_putc (const char c)
  87. {
  88. unsigned int fsr_bits_to_clear;
  89. while (1) {
  90. if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
  91. fsr_bits_to_clear = FSR_TEND;
  92. break;
  93. }
  94. }
  95. *SCFTDR = c;
  96. if (fsr_bits_to_clear != 0)
  97. *SCFSR &= ~fsr_bits_to_clear;
  98. }
  99. void serial_putc (const char c)
  100. {
  101. if (c == '\n')
  102. serial_raw_putc ('\r');
  103. serial_raw_putc (c);
  104. }
  105. void serial_puts (const char *s)
  106. {
  107. char c;
  108. while ((c = *s++) != 0)
  109. serial_putc (c);
  110. }
  111. int serial_tstc (void)
  112. {
  113. return serial_rx_fifo_level() ? 1 : 0;
  114. }
  115. #define FSR_ERR_CLEAR 0x0063
  116. #define RDRF_CLEAR 0x00fc
  117. #define LSR_ORER 1
  118. void handle_error( void ){
  119. (void)*SCFSR ;
  120. *SCFSR = FSR_ERR_CLEAR ;
  121. (void)*SCLSR ;
  122. *SCLSR = 0x00 ;
  123. }
  124. int serial_getc_check( void ){
  125. unsigned short status;
  126. status = *SCFSR ;
  127. if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
  128. handle_error();
  129. if( *SCLSR & LSR_ORER )
  130. handle_error();
  131. return (status & ( FSR_DR | FSR_RDF ));
  132. }
  133. int serial_getc (void)
  134. {
  135. unsigned short status ;
  136. char ch;
  137. while(!serial_getc_check());
  138. ch = *SCFRDR;
  139. status = *SCFSR ;
  140. *SCFSR = RDRF_CLEAR ;
  141. if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
  142. handle_error();
  143. if( *SCLSR & LSR_ORER )
  144. handle_error();
  145. return ch ;
  146. }
  147. #endif /* CFG_SCIF_CONSOLE */