serial_sh.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #if defined(CONFIG_CONS_SCIF0)
  22. # define SCIF_BASE SCIF0_BASE
  23. #elif defined(CONFIG_CONS_SCIF1)
  24. # define SCIF_BASE SCIF1_BASE
  25. #elif defined(CONFIG_CONS_SCIF2)
  26. # define SCIF_BASE SCIF2_BASE
  27. #elif defined(CONFIG_CONS_SCIF3)
  28. # define SCIF_BASE SCIF3_BASE
  29. #elif defined(CONFIG_CONS_SCIF4)
  30. # define SCIF_BASE SCIF4_BASE
  31. #elif defined(CONFIG_CONS_SCIF5)
  32. # define SCIF_BASE SCIF5_BASE
  33. #else
  34. # error "Default SCIF doesn't set....."
  35. #endif
  36. /* Base register */
  37. #define SCSMR (vu_short *)(SCIF_BASE + 0x0)
  38. #define SCBRR (vu_char *)(SCIF_BASE + 0x4)
  39. #define SCSCR (vu_short *)(SCIF_BASE + 0x8)
  40. #define SCFCR (vu_short *)(SCIF_BASE + 0x18)
  41. #define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
  42. #if defined(CONFIG_CPU_SH7720) || \
  43. (defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A))
  44. # define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
  45. # define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
  46. # define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
  47. #else
  48. # define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
  49. # define SCFSR (vu_short *)(SCIF_BASE + 0x10)
  50. # define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
  51. #endif
  52. #if defined(CONFIG_CPU_SH7780) || \
  53. defined(CONFIG_CPU_SH7785)
  54. # define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
  55. # define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
  56. # define SCLSR (vu_short *)(SCIF_BASE + 0x28)
  57. # define SCRER (vu_short *)(SCIF_BASE + 0x2C)
  58. # define LSR_ORER 1
  59. # define FIFOLEVEL_MASK 0xFF
  60. #elif defined(CONFIG_CPU_SH7763)
  61. # if defined(CONFIG_CONS_SCIF2)
  62. # define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
  63. # define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  64. # define LSR_ORER 1
  65. # define FIFOLEVEL_MASK 0x1F
  66. # else
  67. # define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
  68. # define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
  69. # define SCLSR (vu_short *)(SCIF_BASE + 0x28)
  70. # define SCRER (vu_short *)(SCIF_BASE + 0x2C)
  71. # define LSR_ORER 1
  72. # define FIFOLEVEL_MASK 0xFF
  73. # endif
  74. #elif defined(CONFIG_CPU_SH7723)
  75. # if defined(CONFIG_SCIF_A)
  76. # define SCLSR SCFSR
  77. # define LSR_ORER 0x0200
  78. # define FIFOLEVEL_MASK 0x3F
  79. #else
  80. # define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  81. # define LSR_ORER 1
  82. # define FIFOLEVEL_MASK 0x1F
  83. #endif
  84. #elif defined(CONFIG_CPU_SH7750) || \
  85. defined(CONFIG_CPU_SH7751) || \
  86. defined(CONFIG_CPU_SH7722) || \
  87. defined(CONFIG_CPU_SH7203)
  88. # define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
  89. # define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  90. # define LSR_ORER 1
  91. # define FIFOLEVEL_MASK 0x1F
  92. #elif defined(CONFIG_CPU_SH7720)
  93. # define SCLSR (vu_short *)(SCIF_BASE + 0x24)
  94. # define LSR_ORER 0x0200
  95. # define FIFOLEVEL_MASK 0x1F
  96. #elif defined(CONFIG_CPU_SH7710) || \
  97. defined(CONFIG_CPU_SH7712)
  98. # define SCLSR SCFSR /* SCSSR */
  99. # define LSR_ORER 1
  100. # define FIFOLEVEL_MASK 0x1F
  101. #endif
  102. /* SCBRR register value setting */
  103. #if defined(CONFIG_CPU_SH7720)
  104. # define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
  105. #elif defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A)
  106. /* SH7723 SCIFA use bus clock. So clock *2 */
  107. # define SCBRR_VALUE(bps, clk) (((clk*2*2)+16*bps)/(32*bps)-1)
  108. #else /* Generic SuperH */
  109. # define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
  110. #endif
  111. #define SCR_RE (1 << 4)
  112. #define SCR_TE (1 << 5)
  113. #define FCR_RFRST (1 << 1) /* RFCL */
  114. #define FCR_TFRST (1 << 2) /* TFCL */
  115. #define FSR_DR (1 << 0)
  116. #define FSR_RDF (1 << 1)
  117. #define FSR_FER (1 << 3)
  118. #define FSR_BRK (1 << 4)
  119. #define FSR_FER (1 << 3)
  120. #define FSR_TEND (1 << 6)
  121. #define FSR_ER (1 << 7)
  122. /*----------------------------------------------------------------------*/
  123. void serial_setbrg(void)
  124. {
  125. DECLARE_GLOBAL_DATA_PTR;
  126. *SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ);
  127. }
  128. int serial_init(void)
  129. {
  130. *SCSCR = (SCR_RE | SCR_TE);
  131. *SCSMR = 0;
  132. *SCSMR = 0;
  133. *SCFCR = (FCR_RFRST | FCR_TFRST);
  134. *SCFCR;
  135. *SCFCR = 0;
  136. serial_setbrg();
  137. return 0;
  138. }
  139. static int serial_rx_fifo_level(void)
  140. {
  141. #if defined(SCRFDR)
  142. return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
  143. #else
  144. return (*SCFDR >> 0) & FIFOLEVEL_MASK;
  145. #endif
  146. }
  147. void serial_raw_putc(const char c)
  148. {
  149. unsigned int fsr_bits_to_clear;
  150. while (1) {
  151. if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
  152. fsr_bits_to_clear = FSR_TEND;
  153. break;
  154. }
  155. }
  156. *SCFTDR = c;
  157. if (fsr_bits_to_clear != 0)
  158. *SCFSR &= ~fsr_bits_to_clear;
  159. }
  160. void serial_putc(const char c)
  161. {
  162. if (c == '\n')
  163. serial_raw_putc('\r');
  164. serial_raw_putc(c);
  165. }
  166. void serial_puts(const char *s)
  167. {
  168. char c;
  169. while ((c = *s++) != 0)
  170. serial_putc(c);
  171. }
  172. int serial_tstc(void)
  173. {
  174. return serial_rx_fifo_level() ? 1 : 0;
  175. }
  176. #define FSR_ERR_CLEAR 0x0063
  177. #define RDRF_CLEAR 0x00fc
  178. void handle_error(void)
  179. {
  180. (void)*SCFSR;
  181. *SCFSR = FSR_ERR_CLEAR;
  182. (void)*SCLSR;
  183. *SCLSR = 0x00;
  184. }
  185. int serial_getc_check(void)
  186. {
  187. unsigned short status;
  188. status = *SCFSR;
  189. if (status & (FSR_FER | FSR_ER | FSR_BRK))
  190. handle_error();
  191. if (*SCLSR & LSR_ORER)
  192. handle_error();
  193. return status & (FSR_DR | FSR_RDF);
  194. }
  195. int serial_getc(void)
  196. {
  197. unsigned short status;
  198. char ch;
  199. while (!serial_getc_check())
  200. ;
  201. ch = *SCFRDR;
  202. status = *SCFSR;
  203. *SCFSR = RDRF_CLEAR;
  204. if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
  205. handle_error();
  206. if (*SCLSR & LSR_ORER)
  207. handle_error();
  208. return ch;
  209. }