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