serial_sh.c 5.1 KB

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