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; |
Nobuhiro Iwamatsu | e9900db | 2013-08-20 10:31:53 +0900 | [diff] [blame] | 51 | |
| 52 | sci_out(&sh_sci, SCBRR, |
| 53 | SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 56 | static int sh_serial_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 57 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 58 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 59 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 60 | sci_out(&sh_sci, SCSMR, 0); |
| 61 | sci_out(&sh_sci, SCSMR, 0); |
| 62 | sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST); |
| 63 | sci_in(&sh_sci, SCFCR); |
| 64 | sci_out(&sh_sci, SCFCR, 0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 65 | |
| 66 | serial_setbrg(); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 70 | #if defined(CONFIG_CPU_SH7760) || \ |
| 71 | defined(CONFIG_CPU_SH7780) || \ |
| 72 | defined(CONFIG_CPU_SH7785) || \ |
| 73 | defined(CONFIG_CPU_SH7786) |
| 74 | static int scif_rxfill(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 75 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 76 | return sci_in(port, SCRFDR) & 0xff; |
| 77 | } |
| 78 | #elif defined(CONFIG_CPU_SH7763) |
| 79 | static int scif_rxfill(struct uart_port *port) |
| 80 | { |
| 81 | if ((port->mapbase == 0xffe00000) || |
| 82 | (port->mapbase == 0xffe08000)) { |
| 83 | /* SCIF0/1*/ |
| 84 | return sci_in(port, SCRFDR) & 0xff; |
| 85 | } else { |
| 86 | /* SCIF2 */ |
| 87 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
| 88 | } |
| 89 | } |
| 90 | #elif defined(CONFIG_ARCH_SH7372) |
| 91 | static int scif_rxfill(struct uart_port *port) |
| 92 | { |
| 93 | if (port->type == PORT_SCIFA) |
| 94 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 95 | else |
| 96 | return sci_in(port, SCRFDR); |
| 97 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 98 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 99 | static int scif_rxfill(struct uart_port *port) |
| 100 | { |
| 101 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 102 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 103 | #endif |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 104 | |
| 105 | static int serial_rx_fifo_level(void) |
| 106 | { |
| 107 | return scif_rxfill(&sh_sci); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 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 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 118 | void serial_raw_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 119 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 120 | while (1) { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 121 | /* Tx fifo is empty */ |
| 122 | if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 123 | break; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 126 | sci_out(&sh_sci, SCxTDR, c); |
| 127 | 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] | 128 | } |
| 129 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 130 | static void sh_serial_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 131 | { |
| 132 | if (c == '\n') |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 133 | serial_raw_putc('\r'); |
| 134 | serial_raw_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 137 | static int sh_serial_tstc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 138 | { |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 139 | if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) { |
| 140 | handle_error(); |
| 141 | return 0; |
| 142 | } |
| 143 | |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 144 | return serial_rx_fifo_level() ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 147 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 148 | int serial_getc_check(void) |
| 149 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 150 | unsigned short status; |
| 151 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 152 | status = sci_in(&sh_sci, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 153 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 154 | if (status & SCIF_ERRORS) |
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 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 157 | handle_error(); |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 158 | return status & (SCIF_DR | SCxSR_RDxF(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 159 | } |
| 160 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 161 | static int sh_serial_getc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 162 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 163 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 164 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 165 | |
| 166 | while (!serial_getc_check()) |
| 167 | ; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 168 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 169 | ch = sci_in(&sh_sci, SCxRDR); |
| 170 | status = sci_in(&sh_sci, SCxSR); |
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 | sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 173 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 174 | if (status & SCIF_ERRORS) |
| 175 | handle_error(); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 176 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 177 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 178 | handle_error(); |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 179 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 180 | } |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 181 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 182 | static struct serial_device sh_serial_drv = { |
| 183 | .name = "sh_serial", |
| 184 | .start = sh_serial_init, |
| 185 | .stop = NULL, |
| 186 | .setbrg = sh_serial_setbrg, |
| 187 | .putc = sh_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 188 | .puts = default_serial_puts, |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 189 | .getc = sh_serial_getc, |
| 190 | .tstc = sh_serial_tstc, |
| 191 | }; |
| 192 | |
| 193 | void sh_serial_initialize(void) |
| 194 | { |
| 195 | serial_register(&sh_serial_drv); |
| 196 | } |
| 197 | |
| 198 | __weak struct serial_device *default_serial_console(void) |
| 199 | { |
| 200 | return &sh_serial_drv; |
| 201 | } |