serial_sh.c 4.7 KB

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