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 | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 4 | * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 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> |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <dm.h> |
Jean-Christophe PLAGNIOL-VILLARD | fc83c92 | 2009-01-11 16:35:16 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 14 | #include <asm/processor.h> |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 15 | #include <serial.h> |
| 16 | #include <linux/compiler.h> |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 17 | #include <dm/platform_data/serial_sh.h> |
| 18 | #include "serial_sh.h" |
| 19 | |
Yoshinori Sato | 359787c | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 22 | #if defined(CONFIG_CPU_SH7760) || \ |
| 23 | defined(CONFIG_CPU_SH7780) || \ |
| 24 | defined(CONFIG_CPU_SH7785) || \ |
| 25 | defined(CONFIG_CPU_SH7786) |
| 26 | static int scif_rxfill(struct uart_port *port) |
| 27 | { |
| 28 | return sci_in(port, SCRFDR) & 0xff; |
| 29 | } |
| 30 | #elif defined(CONFIG_CPU_SH7763) |
| 31 | static int scif_rxfill(struct uart_port *port) |
| 32 | { |
| 33 | if ((port->mapbase == 0xffe00000) || |
| 34 | (port->mapbase == 0xffe08000)) { |
| 35 | /* SCIF0/1*/ |
| 36 | return sci_in(port, SCRFDR) & 0xff; |
| 37 | } else { |
| 38 | /* SCIF2 */ |
| 39 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
| 40 | } |
| 41 | } |
| 42 | #elif defined(CONFIG_ARCH_SH7372) |
| 43 | static int scif_rxfill(struct uart_port *port) |
| 44 | { |
| 45 | if (port->type == PORT_SCIFA) |
| 46 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 47 | else |
| 48 | return sci_in(port, SCRFDR); |
| 49 | } |
| 50 | #else |
| 51 | static int scif_rxfill(struct uart_port *port) |
| 52 | { |
| 53 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | static void sh_serial_init_generic(struct uart_port *port) |
| 58 | { |
| 59 | sci_out(port, SCSCR , SCSCR_INIT(port)); |
| 60 | sci_out(port, SCSCR , SCSCR_INIT(port)); |
| 61 | sci_out(port, SCSMR, 0); |
| 62 | sci_out(port, SCSMR, 0); |
| 63 | sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST); |
| 64 | sci_in(port, SCFCR); |
| 65 | sci_out(port, SCFCR, 0); |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate) |
| 70 | { |
| 71 | if (port->clk_mode == EXT_CLK) { |
| 72 | unsigned short dl = DL_VALUE(baudrate, clk); |
| 73 | sci_out(port, DL, dl); |
Nobuhiro Iwamatsu | 89f99a6 | 2014-12-10 14:42:05 +0900 | [diff] [blame] | 74 | /* Need wait: Clock * 1/dl * 1/16 */ |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 75 | udelay((1000000 * dl * 16 / clk) * 1000 + 1); |
| 76 | } else { |
| 77 | sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void handle_error(struct uart_port *port) |
| 82 | { |
| 83 | sci_in(port, SCxSR); |
| 84 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 85 | sci_in(port, SCLSR); |
| 86 | sci_out(port, SCLSR, 0x00); |
| 87 | } |
| 88 | |
| 89 | static int serial_raw_putc(struct uart_port *port, const char c) |
| 90 | { |
| 91 | /* Tx fifo is empty */ |
| 92 | if (!(sci_in(port, SCxSR) & SCxSR_TEND(port))) |
| 93 | return -EAGAIN; |
| 94 | |
| 95 | sci_out(port, SCxTDR, c); |
| 96 | sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port)); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int serial_rx_fifo_level(struct uart_port *port) |
| 102 | { |
| 103 | return scif_rxfill(port); |
| 104 | } |
| 105 | |
| 106 | static int sh_serial_tstc_generic(struct uart_port *port) |
| 107 | { |
| 108 | if (sci_in(port, SCxSR) & SCIF_ERRORS) { |
| 109 | handle_error(port); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | return serial_rx_fifo_level(port) ? 1 : 0; |
| 114 | } |
| 115 | |
| 116 | static int serial_getc_check(struct uart_port *port) |
| 117 | { |
| 118 | unsigned short status; |
| 119 | |
| 120 | status = sci_in(port, SCxSR); |
| 121 | |
| 122 | if (status & SCIF_ERRORS) |
| 123 | handle_error(port); |
| 124 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 125 | handle_error(port); |
| 126 | return status & (SCIF_DR | SCxSR_RDxF(port)); |
| 127 | } |
| 128 | |
| 129 | static int sh_serial_getc_generic(struct uart_port *port) |
| 130 | { |
| 131 | unsigned short status; |
| 132 | char ch; |
| 133 | |
| 134 | if (!serial_getc_check(port)) |
| 135 | return -EAGAIN; |
| 136 | |
| 137 | ch = sci_in(port, SCxRDR); |
| 138 | status = sci_in(port, SCxSR); |
| 139 | |
| 140 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 141 | |
| 142 | if (status & SCIF_ERRORS) |
| 143 | handle_error(port); |
| 144 | |
| 145 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 146 | handle_error(port); |
| 147 | |
| 148 | return ch; |
| 149 | } |
| 150 | |
| 151 | #ifdef CONFIG_DM_SERIAL |
| 152 | |
| 153 | static int sh_serial_pending(struct udevice *dev, bool input) |
| 154 | { |
| 155 | struct uart_port *priv = dev_get_priv(dev); |
| 156 | |
| 157 | return sh_serial_tstc_generic(priv); |
| 158 | } |
| 159 | |
| 160 | static int sh_serial_putc(struct udevice *dev, const char ch) |
| 161 | { |
| 162 | struct uart_port *priv = dev_get_priv(dev); |
| 163 | |
| 164 | return serial_raw_putc(priv, ch); |
| 165 | } |
| 166 | |
| 167 | static int sh_serial_getc(struct udevice *dev) |
| 168 | { |
| 169 | struct uart_port *priv = dev_get_priv(dev); |
| 170 | |
| 171 | return sh_serial_getc_generic(priv); |
| 172 | } |
| 173 | |
| 174 | static int sh_serial_setbrg(struct udevice *dev, int baudrate) |
| 175 | { |
| 176 | struct sh_serial_platdata *plat = dev_get_platdata(dev); |
| 177 | struct uart_port *priv = dev_get_priv(dev); |
| 178 | |
| 179 | sh_serial_setbrg_generic(priv, plat->clk, baudrate); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int sh_serial_probe(struct udevice *dev) |
| 185 | { |
| 186 | struct sh_serial_platdata *plat = dev_get_platdata(dev); |
| 187 | struct uart_port *priv = dev_get_priv(dev); |
| 188 | |
| 189 | priv->membase = (unsigned char *)plat->base; |
| 190 | priv->mapbase = plat->base; |
| 191 | priv->type = plat->type; |
| 192 | priv->clk_mode = plat->clk_mode; |
| 193 | |
| 194 | sh_serial_init_generic(priv); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static const struct dm_serial_ops sh_serial_ops = { |
| 200 | .putc = sh_serial_putc, |
| 201 | .pending = sh_serial_pending, |
| 202 | .getc = sh_serial_getc, |
| 203 | .setbrg = sh_serial_setbrg, |
| 204 | }; |
| 205 | |
Yoshinori Sato | 359787c | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 206 | #ifdef CONFIG_OF_CONTROL |
| 207 | static const struct udevice_id sh_serial_id[] ={ |
Yoshinori Sato | 747431b | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 208 | {.compatible = "renesas,sci", .data = PORT_SCI}, |
Yoshinori Sato | 359787c | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 209 | {.compatible = "renesas,scif", .data = PORT_SCIF}, |
| 210 | {.compatible = "renesas,scifa", .data = PORT_SCIFA}, |
| 211 | {} |
| 212 | }; |
| 213 | |
| 214 | static int sh_serial_ofdata_to_platdata(struct udevice *dev) |
| 215 | { |
| 216 | struct sh_serial_platdata *plat = dev_get_platdata(dev); |
| 217 | fdt_addr_t addr; |
| 218 | |
| 219 | addr = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); |
| 220 | if (addr == FDT_ADDR_T_NONE) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | plat->base = addr; |
| 224 | plat->clk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "clock", 1); |
| 225 | plat->type = dev_get_driver_data(dev); |
| 226 | return 0; |
| 227 | } |
| 228 | #endif |
| 229 | |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 230 | U_BOOT_DRIVER(serial_sh) = { |
| 231 | .name = "serial_sh", |
| 232 | .id = UCLASS_SERIAL, |
Yoshinori Sato | 359787c | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 233 | .of_match = of_match_ptr(sh_serial_id), |
| 234 | .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata), |
| 235 | .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata), |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 236 | .probe = sh_serial_probe, |
| 237 | .ops = &sh_serial_ops, |
| 238 | .flags = DM_FLAG_PRE_RELOC, |
| 239 | .priv_auto_alloc_size = sizeof(struct uart_port), |
| 240 | }; |
| 241 | |
| 242 | #else /* CONFIG_DM_SERIAL */ |
John Rigby | 2956532 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 243 | |
Nobuhiro Iwamatsu | ab09f43 | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 244 | #if defined(CONFIG_CONS_SCIF0) |
| 245 | # define SCIF_BASE SCIF0_BASE |
| 246 | #elif defined(CONFIG_CONS_SCIF1) |
| 247 | # define SCIF_BASE SCIF1_BASE |
| 248 | #elif defined(CONFIG_CONS_SCIF2) |
| 249 | # define SCIF_BASE SCIF2_BASE |
| 250 | #elif defined(CONFIG_CONS_SCIF3) |
| 251 | # define SCIF_BASE SCIF3_BASE |
| 252 | #elif defined(CONFIG_CONS_SCIF4) |
| 253 | # define SCIF_BASE SCIF4_BASE |
| 254 | #elif defined(CONFIG_CONS_SCIF5) |
| 255 | # define SCIF_BASE SCIF5_BASE |
Phil Edworthy | 99744b7 | 2012-05-15 22:15:51 +0000 | [diff] [blame] | 256 | #elif defined(CONFIG_CONS_SCIF6) |
| 257 | # define SCIF_BASE SCIF6_BASE |
| 258 | #elif defined(CONFIG_CONS_SCIF7) |
| 259 | # define SCIF_BASE SCIF7_BASE |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 260 | #else |
Nobuhiro Iwamatsu | ab09f43 | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 261 | # error "Default SCIF doesn't set....." |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 262 | #endif |
| 263 | |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 264 | #if defined(CONFIG_SCIF_A) |
| 265 | #define SCIF_BASE_PORT PORT_SCIFA |
Yoshinori Sato | 747431b | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 266 | #elif defined(CONFIG_SCI) |
| 267 | #define SCIF_BASE_PORT PORT_SCI |
Yoshihiro Shimoda | 7c10c57 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 268 | #else |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 269 | #define SCIF_BASE_PORT PORT_SCIF |
Yoshihiro Shimoda | 7c10c57 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 270 | #endif |
| 271 | |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 272 | static struct uart_port sh_sci = { |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 273 | .membase = (unsigned char *)SCIF_BASE, |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 274 | .mapbase = SCIF_BASE, |
| 275 | .type = SCIF_BASE_PORT, |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 276 | #ifdef CONFIG_SCIF_USE_EXT_CLK |
| 277 | .clk_mode = EXT_CLK, |
| 278 | #endif |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 279 | }; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 280 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 281 | static void sh_serial_setbrg(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 282 | { |
Nobuhiro Iwamatsu | 3f6c8e3 | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 283 | DECLARE_GLOBAL_DATA_PTR; |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 284 | struct uart_port *port = &sh_sci; |
| 285 | |
| 286 | sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 289 | static int sh_serial_init(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 290 | { |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 291 | struct uart_port *port = &sh_sci; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 292 | |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 293 | sh_serial_init_generic(port); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 294 | serial_setbrg(); |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 295 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 299 | static void sh_serial_putc(const char c) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 300 | { |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 301 | struct uart_port *port = &sh_sci; |
| 302 | |
| 303 | if (c == '\n') { |
| 304 | while (1) { |
| 305 | if (serial_raw_putc(port, '\r') != -EAGAIN) |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | while (1) { |
| 310 | if (serial_raw_putc(port, c) != -EAGAIN) |
| 311 | break; |
| 312 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 313 | } |
| 314 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 315 | static int sh_serial_tstc(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 316 | { |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 317 | struct uart_port *port = &sh_sci; |
Tetsuyuki Kobayashi | 7c791b3 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 318 | |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 319 | return sh_serial_tstc_generic(port); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 320 | } |
| 321 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 322 | static int sh_serial_getc(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 323 | { |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 324 | struct uart_port *port = &sh_sci; |
| 325 | int ch; |
Nobuhiro Iwamatsu | ab09f43 | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 326 | |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 327 | while (1) { |
| 328 | ch = sh_serial_getc_generic(port); |
| 329 | if (ch != -EAGAIN) |
| 330 | break; |
| 331 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 332 | |
Nobuhiro Iwamatsu | 08c5fab | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 333 | return ch; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 334 | } |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 335 | |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 336 | static struct serial_device sh_serial_drv = { |
| 337 | .name = "sh_serial", |
| 338 | .start = sh_serial_init, |
| 339 | .stop = NULL, |
| 340 | .setbrg = sh_serial_setbrg, |
| 341 | .putc = sh_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 342 | .puts = default_serial_puts, |
Marek Vasut | 8bdd7ef | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 343 | .getc = sh_serial_getc, |
| 344 | .tstc = sh_serial_tstc, |
| 345 | }; |
| 346 | |
| 347 | void sh_serial_initialize(void) |
| 348 | { |
| 349 | serial_register(&sh_serial_drv); |
| 350 | } |
| 351 | |
| 352 | __weak struct serial_device *default_serial_console(void) |
| 353 | { |
| 354 | return &sh_serial_drv; |
| 355 | } |
Nobuhiro Iwamatsu | 59088e4 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 356 | #endif /* CONFIG_DM_SERIAL */ |