serial_sh.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * SuperH SCIF device driver.
  3. * Copyright (c) 2007,2008 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. /* Base register */
  30. #define SCSMR (vu_short *)(SCIF_BASE + 0x0)
  31. #define SCBRR (vu_char *)(SCIF_BASE + 0x4)
  32. #define SCSCR (vu_short *)(SCIF_BASE + 0x8)
  33. #define SCFCR (vu_short *)(SCIF_BASE + 0x18)
  34. #define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
  35. #ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
  36. #define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
  37. #define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
  38. #define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
  39. #else
  40. #define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
  41. #define SCFSR (vu_short *)(SCIF_BASE + 0x10)
  42. #define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
  43. #endif
  44. #if defined(CONFIG_SH4A)
  45. #define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
  46. #define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
  47. #define SCLSR (vu_short *)(SCIF_BASE + 0x28)
  48. #define SCRER (vu_short *)(SCIF_BASE + 0x2C)
  49. #define LSR_ORER 1
  50. #elif defined (CONFIG_SH4)
  51. #define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
  52. #define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  53. #define LSR_ORER 1
  54. #elif defined (CONFIG_SH3)
  55. #ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
  56. #define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  57. #define LSR_ORER 0x0200
  58. #else
  59. #define SCLSR SCFSR /* SCSSR */
  60. #define LSR_ORER 1
  61. #endif
  62. #endif
  63. #if defined(CONFIG_CPU_SH7720)
  64. #define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
  65. #else /* Generic SuperH */
  66. #define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
  67. #endif
  68. #define SCR_RE (1 << 4)
  69. #define SCR_TE (1 << 5)
  70. #define FCR_RFRST (1 << 1) /* RFCL */
  71. #define FCR_TFRST (1 << 2) /* TFCL */
  72. #define FSR_DR (1 << 0)
  73. #define FSR_RDF (1 << 1)
  74. #define FSR_FER (1 << 3)
  75. #define FSR_BRK (1 << 4)
  76. #define FSR_FER (1 << 3)
  77. #define FSR_TEND (1 << 6)
  78. #define FSR_ER (1 << 7)
  79. /*----------------------------------------------------------------------*/
  80. void serial_setbrg (void)
  81. {
  82. DECLARE_GLOBAL_DATA_PTR;
  83. *SCBRR = SCBRR_VALUE(gd->baudrate,CONFIG_SYS_CLK_FREQ);
  84. }
  85. int serial_init (void)
  86. {
  87. *SCSCR = (SCR_RE | SCR_TE);
  88. *SCSMR = 0 ;
  89. *SCSMR = 0;
  90. *SCFCR = (FCR_RFRST | FCR_TFRST);
  91. *SCFCR;
  92. *SCFCR = 0;
  93. serial_setbrg();
  94. return 0;
  95. }
  96. static int serial_tx_fifo_level (void)
  97. {
  98. return (*SCFDR >> 8) & 0x1F;
  99. }
  100. static int serial_rx_fifo_level (void)
  101. {
  102. return (*SCFDR >> 0) & 0x1F;
  103. }
  104. void serial_raw_putc (const char c)
  105. {
  106. unsigned int fsr_bits_to_clear;
  107. while (1) {
  108. if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
  109. fsr_bits_to_clear = FSR_TEND;
  110. break;
  111. }
  112. }
  113. *SCFTDR = c;
  114. if (fsr_bits_to_clear != 0)
  115. *SCFSR &= ~fsr_bits_to_clear;
  116. }
  117. void serial_putc (const char c)
  118. {
  119. if (c == '\n')
  120. serial_raw_putc ('\r');
  121. serial_raw_putc (c);
  122. }
  123. void serial_puts (const char *s)
  124. {
  125. char c;
  126. while ((c = *s++) != 0)
  127. serial_putc (c);
  128. }
  129. int serial_tstc (void)
  130. {
  131. return serial_rx_fifo_level() ? 1 : 0;
  132. }
  133. #define FSR_ERR_CLEAR 0x0063
  134. #define RDRF_CLEAR 0x00fc
  135. void handle_error( void ){
  136. (void)*SCFSR ;
  137. *SCFSR = FSR_ERR_CLEAR ;
  138. (void)*SCLSR ;
  139. *SCLSR = 0x00 ;
  140. }
  141. int serial_getc_check( void ){
  142. unsigned short status;
  143. status = *SCFSR ;
  144. if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
  145. handle_error();
  146. if( *SCLSR & LSR_ORER )
  147. handle_error();
  148. return (status & ( FSR_DR | FSR_RDF ));
  149. }
  150. int serial_getc (void)
  151. {
  152. unsigned short status ;
  153. char ch;
  154. while(!serial_getc_check());
  155. ch = *SCFRDR;
  156. status = *SCFSR ;
  157. *SCFSR = RDRF_CLEAR ;
  158. if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
  159. handle_error();
  160. if( *SCLSR & LSR_ORER )
  161. handle_error();
  162. return ch ;
  163. }
  164. #endif /* CFG_SCIF_CONSOLE */