Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH SCIF device driver. |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 3 | * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu |
| 4 | * Copyright (C) 2002 - 2008 Paul Mundt |
Wolfgang Denk | 0a5c214 | 2007-12-27 01:52:50 +0100 | [diff] [blame] | 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | b27a8e3 | 2009-01-11 16:35:16 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 11 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 12 | #include "serial_sh.h" |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 13 | #include <serial.h> |
| 14 | #include <linux/compiler.h> |
John Rigby | 0d21ed0 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 15 | |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 16 | #if defined(CONFIG_CONS_SCIF0) |
| 17 | # define SCIF_BASE SCIF0_BASE |
| 18 | #elif defined(CONFIG_CONS_SCIF1) |
| 19 | # define SCIF_BASE SCIF1_BASE |
| 20 | #elif defined(CONFIG_CONS_SCIF2) |
| 21 | # define SCIF_BASE SCIF2_BASE |
| 22 | #elif defined(CONFIG_CONS_SCIF3) |
| 23 | # define SCIF_BASE SCIF3_BASE |
| 24 | #elif defined(CONFIG_CONS_SCIF4) |
| 25 | # define SCIF_BASE SCIF4_BASE |
| 26 | #elif defined(CONFIG_CONS_SCIF5) |
| 27 | # define SCIF_BASE SCIF5_BASE |
Phil Edworthy | 04a6275 | 2012-05-15 22:15:51 +0000 | [diff] [blame] | 28 | #elif defined(CONFIG_CONS_SCIF6) |
| 29 | # define SCIF_BASE SCIF6_BASE |
| 30 | #elif defined(CONFIG_CONS_SCIF7) |
| 31 | # define SCIF_BASE SCIF7_BASE |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 32 | #else |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 33 | # error "Default SCIF doesn't set....." |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 34 | #endif |
| 35 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 36 | #if defined(CONFIG_SCIF_A) |
| 37 | #define SCIF_BASE_PORT PORT_SCIFA |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 38 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 39 | #define SCIF_BASE_PORT PORT_SCIF |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 40 | #endif |
| 41 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 42 | static struct uart_port sh_sci = { |
| 43 | .membase = (unsigned char*)SCIF_BASE, |
| 44 | .mapbase = SCIF_BASE, |
| 45 | .type = SCIF_BASE_PORT, |
| 46 | }; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 47 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 48 | static void sh_serial_setbrg(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 49 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 50 | DECLARE_GLOBAL_DATA_PTR; |
| 51 | sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 52 | } |
| 53 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 54 | static int sh_serial_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 55 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 56 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 57 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 58 | sci_out(&sh_sci, SCSMR, 0); |
| 59 | sci_out(&sh_sci, SCSMR, 0); |
| 60 | sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST); |
| 61 | sci_in(&sh_sci, SCFCR); |
| 62 | sci_out(&sh_sci, SCFCR, 0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 63 | |
| 64 | serial_setbrg(); |
| 65 | return 0; |
| 66 | } |
| 67 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 68 | #if defined(CONFIG_CPU_SH7760) || \ |
| 69 | defined(CONFIG_CPU_SH7780) || \ |
| 70 | defined(CONFIG_CPU_SH7785) || \ |
| 71 | defined(CONFIG_CPU_SH7786) |
| 72 | static int scif_rxfill(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 73 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 74 | return sci_in(port, SCRFDR) & 0xff; |
| 75 | } |
| 76 | #elif defined(CONFIG_CPU_SH7763) |
| 77 | static int scif_rxfill(struct uart_port *port) |
| 78 | { |
| 79 | if ((port->mapbase == 0xffe00000) || |
| 80 | (port->mapbase == 0xffe08000)) { |
| 81 | /* SCIF0/1*/ |
| 82 | return sci_in(port, SCRFDR) & 0xff; |
| 83 | } else { |
| 84 | /* SCIF2 */ |
| 85 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
| 86 | } |
| 87 | } |
| 88 | #elif defined(CONFIG_ARCH_SH7372) |
| 89 | static int scif_rxfill(struct uart_port *port) |
| 90 | { |
| 91 | if (port->type == PORT_SCIFA) |
| 92 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 93 | else |
| 94 | return sci_in(port, SCRFDR); |
| 95 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 96 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 97 | static int scif_rxfill(struct uart_port *port) |
| 98 | { |
| 99 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 100 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 101 | #endif |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 102 | |
| 103 | static int serial_rx_fifo_level(void) |
| 104 | { |
| 105 | return scif_rxfill(&sh_sci); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 108 | static void handle_error(void) |
| 109 | { |
| 110 | sci_in(&sh_sci, SCxSR); |
| 111 | sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci)); |
| 112 | sci_in(&sh_sci, SCLSR); |
| 113 | sci_out(&sh_sci, SCLSR, 0x00); |
| 114 | } |
| 115 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 116 | void serial_raw_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 117 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 118 | while (1) { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 119 | /* Tx fifo is empty */ |
| 120 | if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 121 | break; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 122 | } |
| 123 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 124 | sci_out(&sh_sci, SCxTDR, c); |
| 125 | sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 126 | } |
| 127 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 128 | static void sh_serial_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 129 | { |
| 130 | if (c == '\n') |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 131 | serial_raw_putc('\r'); |
| 132 | serial_raw_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 135 | static int sh_serial_tstc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 136 | { |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 137 | if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) { |
| 138 | handle_error(); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 142 | return serial_rx_fifo_level() ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 145 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 146 | int serial_getc_check(void) |
| 147 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 148 | unsigned short status; |
| 149 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 150 | status = sci_in(&sh_sci, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 151 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 152 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 153 | handle_error(); |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 154 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 155 | handle_error(); |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 156 | return status & (SCIF_DR | SCxSR_RDxF(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 159 | static int sh_serial_getc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 160 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 161 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 162 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 163 | |
| 164 | while (!serial_getc_check()) |
| 165 | ; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 166 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 167 | ch = sci_in(&sh_sci, SCxRDR); |
| 168 | status = sci_in(&sh_sci, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 169 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 170 | sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 171 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 172 | if (status & SCIF_ERRORS) |
| 173 | handle_error(); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 174 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 175 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 176 | handle_error(); |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 177 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 178 | } |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 179 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 180 | static struct serial_device sh_serial_drv = { |
| 181 | .name = "sh_serial", |
| 182 | .start = sh_serial_init, |
| 183 | .stop = NULL, |
| 184 | .setbrg = sh_serial_setbrg, |
| 185 | .putc = sh_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 186 | .puts = default_serial_puts, |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 187 | .getc = sh_serial_getc, |
| 188 | .tstc = sh_serial_tstc, |
| 189 | }; |
| 190 | |
| 191 | void sh_serial_initialize(void) |
| 192 | { |
| 193 | serial_register(&sh_serial_drv); |
| 194 | } |
| 195 | |
| 196 | __weak struct serial_device *default_serial_console(void) |
| 197 | { |
| 198 | return &sh_serial_drv; |
| 199 | } |