serial_sh.c 5.1 KB

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