serial_sh.c 5.9 KB

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