serial_sh.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <serial.h>
  25. #include <linux/compiler.h>
  26. #if defined(CONFIG_CONS_SCIF0)
  27. # define SCIF_BASE SCIF0_BASE
  28. #elif defined(CONFIG_CONS_SCIF1)
  29. # define SCIF_BASE SCIF1_BASE
  30. #elif defined(CONFIG_CONS_SCIF2)
  31. # define SCIF_BASE SCIF2_BASE
  32. #elif defined(CONFIG_CONS_SCIF3)
  33. # define SCIF_BASE SCIF3_BASE
  34. #elif defined(CONFIG_CONS_SCIF4)
  35. # define SCIF_BASE SCIF4_BASE
  36. #elif defined(CONFIG_CONS_SCIF5)
  37. # define SCIF_BASE SCIF5_BASE
  38. #elif defined(CONFIG_CONS_SCIF6)
  39. # define SCIF_BASE SCIF6_BASE
  40. #elif defined(CONFIG_CONS_SCIF7)
  41. # define SCIF_BASE SCIF7_BASE
  42. #else
  43. # error "Default SCIF doesn't set....."
  44. #endif
  45. #if defined(CONFIG_SCIF_A)
  46. #define SCIF_BASE_PORT PORT_SCIFA
  47. #else
  48. #define SCIF_BASE_PORT PORT_SCIF
  49. #endif
  50. static struct uart_port sh_sci = {
  51. .membase = (unsigned char*)SCIF_BASE,
  52. .mapbase = SCIF_BASE,
  53. .type = SCIF_BASE_PORT,
  54. };
  55. static void sh_serial_setbrg(void)
  56. {
  57. DECLARE_GLOBAL_DATA_PTR;
  58. sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
  59. }
  60. static int sh_serial_init(void)
  61. {
  62. sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
  63. sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
  64. sci_out(&sh_sci, SCSMR, 0);
  65. sci_out(&sh_sci, SCSMR, 0);
  66. sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
  67. sci_in(&sh_sci, SCFCR);
  68. sci_out(&sh_sci, SCFCR, 0);
  69. serial_setbrg();
  70. return 0;
  71. }
  72. #if defined(CONFIG_CPU_SH7760) || \
  73. defined(CONFIG_CPU_SH7780) || \
  74. defined(CONFIG_CPU_SH7785) || \
  75. defined(CONFIG_CPU_SH7786)
  76. static int scif_rxfill(struct uart_port *port)
  77. {
  78. return sci_in(port, SCRFDR) & 0xff;
  79. }
  80. #elif defined(CONFIG_CPU_SH7763)
  81. static int scif_rxfill(struct uart_port *port)
  82. {
  83. if ((port->mapbase == 0xffe00000) ||
  84. (port->mapbase == 0xffe08000)) {
  85. /* SCIF0/1*/
  86. return sci_in(port, SCRFDR) & 0xff;
  87. } else {
  88. /* SCIF2 */
  89. return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
  90. }
  91. }
  92. #elif defined(CONFIG_ARCH_SH7372)
  93. static int scif_rxfill(struct uart_port *port)
  94. {
  95. if (port->type == PORT_SCIFA)
  96. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  97. else
  98. return sci_in(port, SCRFDR);
  99. }
  100. #else
  101. static int scif_rxfill(struct uart_port *port)
  102. {
  103. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  104. }
  105. #endif
  106. static int serial_rx_fifo_level(void)
  107. {
  108. return scif_rxfill(&sh_sci);
  109. }
  110. void serial_raw_putc(const char c)
  111. {
  112. while (1) {
  113. /* Tx fifo is empty */
  114. if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
  115. break;
  116. }
  117. sci_out(&sh_sci, SCxTDR, c);
  118. sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
  119. }
  120. static void sh_serial_putc(const char c)
  121. {
  122. if (c == '\n')
  123. serial_raw_putc('\r');
  124. serial_raw_putc(c);
  125. }
  126. static int sh_serial_tstc(void)
  127. {
  128. return serial_rx_fifo_level() ? 1 : 0;
  129. }
  130. void handle_error(void)
  131. {
  132. sci_in(&sh_sci, SCxSR);
  133. sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
  134. sci_in(&sh_sci, SCLSR);
  135. sci_out(&sh_sci, SCLSR, 0x00);
  136. }
  137. int serial_getc_check(void)
  138. {
  139. unsigned short status;
  140. status = sci_in(&sh_sci, SCxSR);
  141. if (status & SCIF_ERRORS)
  142. handle_error();
  143. if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
  144. handle_error();
  145. return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
  146. }
  147. static int sh_serial_getc(void)
  148. {
  149. unsigned short status;
  150. char ch;
  151. while (!serial_getc_check())
  152. ;
  153. ch = sci_in(&sh_sci, SCxRDR);
  154. status = sci_in(&sh_sci, SCxSR);
  155. sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
  156. if (status & SCIF_ERRORS)
  157. handle_error();
  158. if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
  159. handle_error();
  160. return ch;
  161. }
  162. static struct serial_device sh_serial_drv = {
  163. .name = "sh_serial",
  164. .start = sh_serial_init,
  165. .stop = NULL,
  166. .setbrg = sh_serial_setbrg,
  167. .putc = sh_serial_putc,
  168. .puts = default_serial_puts,
  169. .getc = sh_serial_getc,
  170. .tstc = sh_serial_tstc,
  171. };
  172. void sh_serial_initialize(void)
  173. {
  174. serial_register(&sh_serial_drv);
  175. }
  176. __weak struct serial_device *default_serial_console(void)
  177. {
  178. return &sh_serial_drv;
  179. }