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