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 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +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) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 27 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 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) || |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 34 | (port->mapbase == 0xffe08000)) { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 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 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 50 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 51 | static int scif_rxfill(struct uart_port *port) |
| 52 | { |
| 53 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 54 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 55 | #endif |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 56 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 57 | static void sh_serial_init_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 58 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 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); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 68 | static void |
| 69 | sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate) |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 70 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 71 | if (port->clk_mode == EXT_CLK) { |
| 72 | unsigned short dl = DL_VALUE(baudrate, clk); |
| 73 | sci_out(port, DL, dl); |
Nobuhiro Iwamatsu | 1786175 | 2014-12-10 14:42:05 +0900 | [diff] [blame] | 74 | /* Need wait: Clock * 1/dl * 1/16 */ |
Nobuhiro Iwamatsu | 6d02035 | 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 | } |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 81 | static void handle_error(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 82 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 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; |
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 | sci_out(port, SCxTDR, c); |
| 96 | sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port)); |
| 97 | |
| 98 | return 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 101 | static int serial_rx_fifo_level(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 102 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 103 | return scif_rxfill(port); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 106 | static int sh_serial_tstc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 107 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 108 | if (sci_in(port, SCxSR) & SCIF_ERRORS) { |
| 109 | handle_error(port); |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 113 | return serial_rx_fifo_level(port) ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 116 | static int serial_getc_check(struct uart_port *port) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 117 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 118 | unsigned short status; |
| 119 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 120 | status = sci_in(port, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 121 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 122 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 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)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 129 | static int sh_serial_getc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 130 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 131 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 132 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 133 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 134 | if (!serial_getc_check(port)) |
| 135 | return -EAGAIN; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 136 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 137 | ch = sci_in(port, SCxRDR); |
| 138 | status = sci_in(port, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 139 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 140 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 141 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 142 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 143 | handle_error(port); |
| 144 | |
| 145 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 146 | handle_error(port); |
| 147 | |
| 148 | return ch; |
| 149 | } |
| 150 | |
Marek Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 151 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 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 | |
Marek Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 206 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 207 | static const struct udevice_id sh_serial_id[] ={ |
Yoshinori Sato | e5669a3 | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 208 | {.compatible = "renesas,sci", .data = PORT_SCI}, |
Yoshinori Sato | a6eed2b | 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); |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 217 | struct clk sh_serial_clk; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 218 | fdt_addr_t addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 219 | int ret; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 220 | |
Marek Vasut | 48db776 | 2018-01-17 22:36:37 +0100 | [diff] [blame] | 221 | addr = devfdt_get_addr(dev); |
| 222 | if (!addr) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 223 | return -EINVAL; |
| 224 | |
| 225 | plat->base = addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 226 | |
| 227 | ret = clk_get_by_name(dev, "fck", &sh_serial_clk); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 228 | if (!ret) { |
| 229 | ret = clk_enable(&sh_serial_clk); |
| 230 | if (!ret) |
| 231 | plat->clk = clk_get_rate(&sh_serial_clk); |
| 232 | } else { |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 233 | plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 234 | "clock", 1); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 235 | } |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 236 | |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 237 | plat->type = dev_get_driver_data(dev); |
| 238 | return 0; |
| 239 | } |
| 240 | #endif |
| 241 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 242 | U_BOOT_DRIVER(serial_sh) = { |
| 243 | .name = "serial_sh", |
| 244 | .id = UCLASS_SERIAL, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 245 | .of_match = of_match_ptr(sh_serial_id), |
| 246 | .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata), |
| 247 | .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata), |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 248 | .probe = sh_serial_probe, |
| 249 | .ops = &sh_serial_ops, |
| 250 | .flags = DM_FLAG_PRE_RELOC, |
| 251 | .priv_auto_alloc_size = sizeof(struct uart_port), |
| 252 | }; |
| 253 | |
| 254 | #else /* CONFIG_DM_SERIAL */ |
| 255 | |
| 256 | #if defined(CONFIG_CONS_SCIF0) |
| 257 | # define SCIF_BASE SCIF0_BASE |
| 258 | #elif defined(CONFIG_CONS_SCIF1) |
| 259 | # define SCIF_BASE SCIF1_BASE |
| 260 | #elif defined(CONFIG_CONS_SCIF2) |
| 261 | # define SCIF_BASE SCIF2_BASE |
| 262 | #elif defined(CONFIG_CONS_SCIF3) |
| 263 | # define SCIF_BASE SCIF3_BASE |
| 264 | #elif defined(CONFIG_CONS_SCIF4) |
| 265 | # define SCIF_BASE SCIF4_BASE |
| 266 | #elif defined(CONFIG_CONS_SCIF5) |
| 267 | # define SCIF_BASE SCIF5_BASE |
| 268 | #elif defined(CONFIG_CONS_SCIF6) |
| 269 | # define SCIF_BASE SCIF6_BASE |
| 270 | #elif defined(CONFIG_CONS_SCIF7) |
| 271 | # define SCIF_BASE SCIF7_BASE |
Marek Vasut | 1d9756b | 2018-04-12 15:23:46 +0200 | [diff] [blame] | 272 | #elif defined(CONFIG_CONS_SCIFA0) |
| 273 | # define SCIF_BASE SCIFA0_BASE |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 274 | #else |
| 275 | # error "Default SCIF doesn't set....." |
| 276 | #endif |
| 277 | |
| 278 | #if defined(CONFIG_SCIF_A) |
| 279 | #define SCIF_BASE_PORT PORT_SCIFA |
Yoshinori Sato | e5669a3 | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 280 | #elif defined(CONFIG_SCI) |
| 281 | #define SCIF_BASE_PORT PORT_SCI |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 282 | #else |
| 283 | #define SCIF_BASE_PORT PORT_SCIF |
| 284 | #endif |
| 285 | |
| 286 | static struct uart_port sh_sci = { |
| 287 | .membase = (unsigned char *)SCIF_BASE, |
| 288 | .mapbase = SCIF_BASE, |
| 289 | .type = SCIF_BASE_PORT, |
| 290 | #ifdef CONFIG_SCIF_USE_EXT_CLK |
| 291 | .clk_mode = EXT_CLK, |
| 292 | #endif |
| 293 | }; |
| 294 | |
| 295 | static void sh_serial_setbrg(void) |
| 296 | { |
| 297 | DECLARE_GLOBAL_DATA_PTR; |
| 298 | struct uart_port *port = &sh_sci; |
| 299 | |
| 300 | sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate); |
| 301 | } |
| 302 | |
| 303 | static int sh_serial_init(void) |
| 304 | { |
| 305 | struct uart_port *port = &sh_sci; |
| 306 | |
| 307 | sh_serial_init_generic(port); |
| 308 | serial_setbrg(); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void sh_serial_putc(const char c) |
| 314 | { |
| 315 | struct uart_port *port = &sh_sci; |
| 316 | |
| 317 | if (c == '\n') { |
| 318 | while (1) { |
| 319 | if (serial_raw_putc(port, '\r') != -EAGAIN) |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | while (1) { |
| 324 | if (serial_raw_putc(port, c) != -EAGAIN) |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static int sh_serial_tstc(void) |
| 330 | { |
| 331 | struct uart_port *port = &sh_sci; |
| 332 | |
| 333 | return sh_serial_tstc_generic(port); |
| 334 | } |
| 335 | |
| 336 | static int sh_serial_getc(void) |
| 337 | { |
| 338 | struct uart_port *port = &sh_sci; |
| 339 | int ch; |
| 340 | |
| 341 | while (1) { |
| 342 | ch = sh_serial_getc_generic(port); |
| 343 | if (ch != -EAGAIN) |
| 344 | break; |
| 345 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 346 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 347 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 348 | } |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 349 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 350 | static struct serial_device sh_serial_drv = { |
| 351 | .name = "sh_serial", |
| 352 | .start = sh_serial_init, |
| 353 | .stop = NULL, |
| 354 | .setbrg = sh_serial_setbrg, |
| 355 | .putc = sh_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 356 | .puts = default_serial_puts, |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 357 | .getc = sh_serial_getc, |
| 358 | .tstc = sh_serial_tstc, |
| 359 | }; |
| 360 | |
| 361 | void sh_serial_initialize(void) |
| 362 | { |
| 363 | serial_register(&sh_serial_drv); |
| 364 | } |
| 365 | |
| 366 | __weak struct serial_device *default_serial_console(void) |
| 367 | { |
| 368 | return &sh_serial_drv; |
| 369 | } |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 370 | #endif /* CONFIG_DM_SERIAL */ |