serial_sh.c 4.4 KB

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