serial_sh.c 4.7 KB

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