serial_sh.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * SuperH SCIF device driver.
  3. * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
  4. * Copyright (C) 2002 - 2008 Paul Mundt
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <asm/io.h>
  22. #include <asm/processor.h>
  23. #include "serial_sh.h"
  24. #if defined(CONFIG_CONS_SCIF0)
  25. # define SCIF_BASE SCIF0_BASE
  26. #elif defined(CONFIG_CONS_SCIF1)
  27. # define SCIF_BASE SCIF1_BASE
  28. #elif defined(CONFIG_CONS_SCIF2)
  29. # define SCIF_BASE SCIF2_BASE
  30. #elif defined(CONFIG_CONS_SCIF3)
  31. # define SCIF_BASE SCIF3_BASE
  32. #elif defined(CONFIG_CONS_SCIF4)
  33. # define SCIF_BASE SCIF4_BASE
  34. #elif defined(CONFIG_CONS_SCIF5)
  35. # define SCIF_BASE SCIF5_BASE
  36. #elif defined(CONFIG_CONS_SCIF6)
  37. # define SCIF_BASE SCIF6_BASE
  38. #elif defined(CONFIG_CONS_SCIF7)
  39. # define SCIF_BASE SCIF7_BASE
  40. #else
  41. # error "Default SCIF doesn't set....."
  42. #endif
  43. #if defined(CONFIG_SCIF_A)
  44. #define SCIF_BASE_PORT PORT_SCIFA
  45. #else
  46. #define SCIF_BASE_PORT PORT_SCIF
  47. #endif
  48. static struct uart_port sh_sci = {
  49. .membase = (unsigned char*)SCIF_BASE,
  50. .mapbase = SCIF_BASE,
  51. .type = SCIF_BASE_PORT,
  52. };
  53. void serial_setbrg(void)
  54. {
  55. DECLARE_GLOBAL_DATA_PTR;
  56. sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
  57. }
  58. int serial_init(void)
  59. {
  60. sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
  61. sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
  62. sci_out(&sh_sci, SCSMR, 0);
  63. sci_out(&sh_sci, SCSMR, 0);
  64. sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
  65. sci_in(&sh_sci, SCFCR);
  66. sci_out(&sh_sci, SCFCR, 0);
  67. serial_setbrg();
  68. return 0;
  69. }
  70. #if defined(CONFIG_CPU_SH7760) || \
  71. defined(CONFIG_CPU_SH7780) || \
  72. defined(CONFIG_CPU_SH7785) || \
  73. defined(CONFIG_CPU_SH7786)
  74. static int scif_rxfill(struct uart_port *port)
  75. {
  76. return sci_in(port, SCRFDR) & 0xff;
  77. }
  78. #elif defined(CONFIG_CPU_SH7763)
  79. static int scif_rxfill(struct uart_port *port)
  80. {
  81. if ((port->mapbase == 0xffe00000) ||
  82. (port->mapbase == 0xffe08000)) {
  83. /* SCIF0/1*/
  84. return sci_in(port, SCRFDR) & 0xff;
  85. } else {
  86. /* SCIF2 */
  87. return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
  88. }
  89. }
  90. #elif defined(CONFIG_ARCH_SH7372)
  91. static int scif_rxfill(struct uart_port *port)
  92. {
  93. if (port->type == PORT_SCIFA)
  94. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  95. else
  96. return sci_in(port, SCRFDR);
  97. }
  98. #else
  99. static int scif_rxfill(struct uart_port *port)
  100. {
  101. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  102. }
  103. #endif
  104. static int serial_rx_fifo_level(void)
  105. {
  106. return scif_rxfill(&sh_sci);
  107. }
  108. void serial_raw_putc(const char c)
  109. {
  110. while (1) {
  111. /* Tx fifo is empty */
  112. if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
  113. break;
  114. }
  115. sci_out(&sh_sci, SCxTDR, c);
  116. sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
  117. }
  118. void serial_putc(const char c)
  119. {
  120. if (c == '\n')
  121. serial_raw_putc('\r');
  122. serial_raw_putc(c);
  123. }
  124. void serial_puts(const char *s)
  125. {
  126. char c;
  127. while ((c = *s++) != 0)
  128. serial_putc(c);
  129. }
  130. int serial_tstc(void)
  131. {
  132. return serial_rx_fifo_level() ? 1 : 0;
  133. }
  134. void handle_error(void)
  135. {
  136. sci_in(&sh_sci, SCxSR);
  137. sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
  138. sci_in(&sh_sci, SCLSR);
  139. sci_out(&sh_sci, SCLSR, 0x00);
  140. }
  141. int serial_getc_check(void)
  142. {
  143. unsigned short status;
  144. status = sci_in(&sh_sci, SCxSR);
  145. if (status & SCIF_ERRORS)
  146. handle_error();
  147. if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
  148. handle_error();
  149. return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
  150. }
  151. int serial_getc(void)
  152. {
  153. unsigned short status;
  154. char ch;
  155. while (!serial_getc_check())
  156. ;
  157. ch = sci_in(&sh_sci, SCxRDR);
  158. status = sci_in(&sh_sci, SCxSR);
  159. sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
  160. if (status & SCIF_ERRORS)
  161. handle_error();
  162. if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
  163. handle_error();
  164. return ch;
  165. }