serial_sh.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static void handle_error(void)
  111. {
  112. sci_in(&sh_sci, SCxSR);
  113. sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
  114. sci_in(&sh_sci, SCLSR);
  115. sci_out(&sh_sci, SCLSR, 0x00);
  116. }
  117. void serial_raw_putc(const char c)
  118. {
  119. while (1) {
  120. /* Tx fifo is empty */
  121. if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
  122. break;
  123. }
  124. sci_out(&sh_sci, SCxTDR, c);
  125. sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
  126. }
  127. static void sh_serial_putc(const char c)
  128. {
  129. if (c == '\n')
  130. serial_raw_putc('\r');
  131. serial_raw_putc(c);
  132. }
  133. static int sh_serial_tstc(void)
  134. {
  135. if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
  136. handle_error();
  137. return 0;
  138. }
  139. return serial_rx_fifo_level() ? 1 : 0;
  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. static int sh_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. }
  166. static struct serial_device sh_serial_drv = {
  167. .name = "sh_serial",
  168. .start = sh_serial_init,
  169. .stop = NULL,
  170. .setbrg = sh_serial_setbrg,
  171. .putc = sh_serial_putc,
  172. .puts = default_serial_puts,
  173. .getc = sh_serial_getc,
  174. .tstc = sh_serial_tstc,
  175. };
  176. void sh_serial_initialize(void)
  177. {
  178. serial_register(&sh_serial_drv);
  179. }
  180. __weak struct serial_device *default_serial_console(void)
  181. {
  182. return &sh_serial_drv;
  183. }