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