blob: e9c6e233988412a995eb6bbba21d1ab348afd012 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
Paul Mundtf43dc232011-01-13 15:06:28 +09004 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09005 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090014 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090020#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#undef DEBUG
25
Paul Mundt85f094e2008-04-25 16:04:20 +090026#include <linux/clk.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010027#include <linux/console.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090028#include <linux/ctype.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010029#include <linux/cpufreq.h>
30#include <linux/delay.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090031#include <linux/dmaengine.h>
Magnus Damm5beabc72011-08-02 09:42:54 +000032#include <linux/dma-mapping.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010033#include <linux/err.h>
34#include <linux/errno.h>
Paul Mundt50f09592011-12-02 20:09:48 +090035#include <linux/gpio.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010036#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/ioport.h>
39#include <linux/major.h>
40#include <linux/module.h>
41#include <linux/mm.h>
42#include <linux/notifier.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/scatterlist.h>
46#include <linux/serial.h>
47#include <linux/serial_sci.h>
48#include <linux/sh_dma.h>
49#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/sysrq.h>
52#include <linux/timer.h>
53#include <linux/tty.h>
54#include <linux/tty_flip.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090055
56#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090057#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "sh-sci.h"
61
Paul Mundte108b2c2006-09-27 16:32:13 +090062struct sci_port {
63 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Paul Mundtce6738b2011-01-19 15:24:40 +090065 /* Platform configuration */
66 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090067
Paul Mundte108b2c2006-09-27 16:32:13 +090068 /* Break timer */
69 struct timer_list break_timer;
70 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090071
Magnus Damm501b8252009-01-21 15:14:30 +000072 /* Interface clock */
73 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090074 /* Function clock */
75 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090076
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +010077 int irqs[SCIx_NR_IRQS];
Paul Mundt9174fc82011-06-28 15:25:36 +090078 char *irqstr[SCIx_NR_IRQS];
Paul Mundt50f09592011-12-02 20:09:48 +090079 char *gpiostr[SCIx_NR_FNS];
Paul Mundt9174fc82011-06-28 15:25:36 +090080
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090081 struct dma_chan *chan_tx;
82 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090083
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090084#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090085 struct dma_async_tx_descriptor *desc_tx;
86 struct dma_async_tx_descriptor *desc_rx[2];
87 dma_cookie_t cookie_tx;
88 dma_cookie_t cookie_rx[2];
89 dma_cookie_t active_rx;
90 struct scatterlist sg_tx;
91 unsigned int sg_len_tx;
92 struct scatterlist sg_rx[2];
93 size_t buf_len_rx;
94 struct sh_dmae_slave param_tx;
95 struct sh_dmae_slave param_rx;
96 struct work_struct work_tx;
97 struct work_struct work_rx;
98 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +000099 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900100#endif
Magnus Damme552de22009-01-21 15:13:42 +0000101
Paul Mundtd535a232011-01-19 17:19:35 +0900102 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900106static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100107static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900108static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Paul Mundte108b2c2006-09-27 16:32:13 +0900110#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
111
112static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static struct uart_driver sci_uart_driver;
114
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900115static inline struct sci_port *
116to_sci_port(struct uart_port *uart)
117{
118 return container_of(uart, struct sci_port, port);
119}
120
Paul Mundt61a69762011-06-14 12:40:19 +0900121struct plat_sci_reg {
122 u8 offset, size;
123};
124
125/* Helper for invalidating specific entries of an inherited map. */
126#define sci_reg_invalid { .offset = 0, .size = 0 }
127
128static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
129 [SCIx_PROBE_REGTYPE] = {
130 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
131 },
132
133 /*
134 * Common SCI definitions, dependent on the port's regshift
135 * value.
136 */
137 [SCIx_SCI_REGTYPE] = {
138 [SCSMR] = { 0x00, 8 },
139 [SCBRR] = { 0x01, 8 },
140 [SCSCR] = { 0x02, 8 },
141 [SCxTDR] = { 0x03, 8 },
142 [SCxSR] = { 0x04, 8 },
143 [SCxRDR] = { 0x05, 8 },
144 [SCFCR] = sci_reg_invalid,
145 [SCFDR] = sci_reg_invalid,
146 [SCTFDR] = sci_reg_invalid,
147 [SCRFDR] = sci_reg_invalid,
148 [SCSPTR] = sci_reg_invalid,
149 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200150 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900151 },
152
153 /*
154 * Common definitions for legacy IrDA ports, dependent on
155 * regshift value.
156 */
157 [SCIx_IRDA_REGTYPE] = {
158 [SCSMR] = { 0x00, 8 },
159 [SCBRR] = { 0x01, 8 },
160 [SCSCR] = { 0x02, 8 },
161 [SCxTDR] = { 0x03, 8 },
162 [SCxSR] = { 0x04, 8 },
163 [SCxRDR] = { 0x05, 8 },
164 [SCFCR] = { 0x06, 8 },
165 [SCFDR] = { 0x07, 16 },
166 [SCTFDR] = sci_reg_invalid,
167 [SCRFDR] = sci_reg_invalid,
168 [SCSPTR] = sci_reg_invalid,
169 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200170 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900171 },
172
173 /*
174 * Common SCIFA definitions.
175 */
176 [SCIx_SCIFA_REGTYPE] = {
177 [SCSMR] = { 0x00, 16 },
178 [SCBRR] = { 0x04, 8 },
179 [SCSCR] = { 0x08, 16 },
180 [SCxTDR] = { 0x20, 8 },
181 [SCxSR] = { 0x14, 16 },
182 [SCxRDR] = { 0x24, 8 },
183 [SCFCR] = { 0x18, 16 },
184 [SCFDR] = { 0x1c, 16 },
185 [SCTFDR] = sci_reg_invalid,
186 [SCRFDR] = sci_reg_invalid,
187 [SCSPTR] = sci_reg_invalid,
188 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200189 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900190 },
191
192 /*
193 * Common SCIFB definitions.
194 */
195 [SCIx_SCIFB_REGTYPE] = {
196 [SCSMR] = { 0x00, 16 },
197 [SCBRR] = { 0x04, 8 },
198 [SCSCR] = { 0x08, 16 },
199 [SCxTDR] = { 0x40, 8 },
200 [SCxSR] = { 0x14, 16 },
201 [SCxRDR] = { 0x60, 8 },
202 [SCFCR] = { 0x18, 16 },
Takashi Yoshii8c66d6d2012-11-16 10:53:31 +0900203 [SCFDR] = sci_reg_invalid,
204 [SCTFDR] = { 0x38, 16 },
205 [SCRFDR] = { 0x3c, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900206 [SCSPTR] = sci_reg_invalid,
207 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200208 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900209 },
210
211 /*
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100212 * Common SH-2(A) SCIF definitions for ports with FIFO data
213 * count registers.
214 */
215 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
216 [SCSMR] = { 0x00, 16 },
217 [SCBRR] = { 0x04, 8 },
218 [SCSCR] = { 0x08, 16 },
219 [SCxTDR] = { 0x0c, 8 },
220 [SCxSR] = { 0x10, 16 },
221 [SCxRDR] = { 0x14, 8 },
222 [SCFCR] = { 0x18, 16 },
223 [SCFDR] = { 0x1c, 16 },
224 [SCTFDR] = sci_reg_invalid,
225 [SCRFDR] = sci_reg_invalid,
226 [SCSPTR] = { 0x20, 16 },
227 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200228 [HSSRR] = sci_reg_invalid,
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100229 },
230
231 /*
Paul Mundt61a69762011-06-14 12:40:19 +0900232 * Common SH-3 SCIF definitions.
233 */
234 [SCIx_SH3_SCIF_REGTYPE] = {
235 [SCSMR] = { 0x00, 8 },
236 [SCBRR] = { 0x02, 8 },
237 [SCSCR] = { 0x04, 8 },
238 [SCxTDR] = { 0x06, 8 },
239 [SCxSR] = { 0x08, 16 },
240 [SCxRDR] = { 0x0a, 8 },
241 [SCFCR] = { 0x0c, 8 },
242 [SCFDR] = { 0x0e, 16 },
243 [SCTFDR] = sci_reg_invalid,
244 [SCRFDR] = sci_reg_invalid,
245 [SCSPTR] = sci_reg_invalid,
246 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200247 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900248 },
249
250 /*
251 * Common SH-4(A) SCIF(B) definitions.
252 */
253 [SCIx_SH4_SCIF_REGTYPE] = {
254 [SCSMR] = { 0x00, 16 },
255 [SCBRR] = { 0x04, 8 },
256 [SCSCR] = { 0x08, 16 },
257 [SCxTDR] = { 0x0c, 8 },
258 [SCxSR] = { 0x10, 16 },
259 [SCxRDR] = { 0x14, 8 },
260 [SCFCR] = { 0x18, 16 },
261 [SCFDR] = { 0x1c, 16 },
262 [SCTFDR] = sci_reg_invalid,
263 [SCRFDR] = sci_reg_invalid,
264 [SCSPTR] = { 0x20, 16 },
265 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200266 [HSSRR] = sci_reg_invalid,
267 },
268
269 /*
270 * Common HSCIF definitions.
271 */
272 [SCIx_HSCIF_REGTYPE] = {
273 [SCSMR] = { 0x00, 16 },
274 [SCBRR] = { 0x04, 8 },
275 [SCSCR] = { 0x08, 16 },
276 [SCxTDR] = { 0x0c, 8 },
277 [SCxSR] = { 0x10, 16 },
278 [SCxRDR] = { 0x14, 8 },
279 [SCFCR] = { 0x18, 16 },
280 [SCFDR] = { 0x1c, 16 },
281 [SCTFDR] = sci_reg_invalid,
282 [SCRFDR] = sci_reg_invalid,
283 [SCSPTR] = { 0x20, 16 },
284 [SCLSR] = { 0x24, 16 },
285 [HSSRR] = { 0x40, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900286 },
287
288 /*
289 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
290 * register.
291 */
292 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
293 [SCSMR] = { 0x00, 16 },
294 [SCBRR] = { 0x04, 8 },
295 [SCSCR] = { 0x08, 16 },
296 [SCxTDR] = { 0x0c, 8 },
297 [SCxSR] = { 0x10, 16 },
298 [SCxRDR] = { 0x14, 8 },
299 [SCFCR] = { 0x18, 16 },
300 [SCFDR] = { 0x1c, 16 },
301 [SCTFDR] = sci_reg_invalid,
302 [SCRFDR] = sci_reg_invalid,
303 [SCSPTR] = sci_reg_invalid,
304 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200305 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900306 },
307
308 /*
309 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
310 * count registers.
311 */
312 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
313 [SCSMR] = { 0x00, 16 },
314 [SCBRR] = { 0x04, 8 },
315 [SCSCR] = { 0x08, 16 },
316 [SCxTDR] = { 0x0c, 8 },
317 [SCxSR] = { 0x10, 16 },
318 [SCxRDR] = { 0x14, 8 },
319 [SCFCR] = { 0x18, 16 },
320 [SCFDR] = { 0x1c, 16 },
321 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
322 [SCRFDR] = { 0x20, 16 },
323 [SCSPTR] = { 0x24, 16 },
324 [SCLSR] = { 0x28, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200325 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900326 },
327
328 /*
329 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
330 * registers.
331 */
332 [SCIx_SH7705_SCIF_REGTYPE] = {
333 [SCSMR] = { 0x00, 16 },
334 [SCBRR] = { 0x04, 8 },
335 [SCSCR] = { 0x08, 16 },
336 [SCxTDR] = { 0x20, 8 },
337 [SCxSR] = { 0x14, 16 },
338 [SCxRDR] = { 0x24, 8 },
339 [SCFCR] = { 0x18, 16 },
340 [SCFDR] = { 0x1c, 16 },
341 [SCTFDR] = sci_reg_invalid,
342 [SCRFDR] = sci_reg_invalid,
343 [SCSPTR] = sci_reg_invalid,
344 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200345 [HSSRR] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900346 },
347};
348
Paul Mundt72b294c2011-06-14 17:38:19 +0900349#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
350
Paul Mundt61a69762011-06-14 12:40:19 +0900351/*
352 * The "offset" here is rather misleading, in that it refers to an enum
353 * value relative to the port mapping rather than the fixed offset
354 * itself, which needs to be manually retrieved from the platform's
355 * register map for the given port.
356 */
357static unsigned int sci_serial_in(struct uart_port *p, int offset)
358{
Paul Mundt72b294c2011-06-14 17:38:19 +0900359 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900360
361 if (reg->size == 8)
362 return ioread8(p->membase + (reg->offset << p->regshift));
363 else if (reg->size == 16)
364 return ioread16(p->membase + (reg->offset << p->regshift));
365 else
366 WARN(1, "Invalid register access\n");
367
368 return 0;
369}
370
371static void sci_serial_out(struct uart_port *p, int offset, int value)
372{
Paul Mundt72b294c2011-06-14 17:38:19 +0900373 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900374
375 if (reg->size == 8)
376 iowrite8(value, p->membase + (reg->offset << p->regshift));
377 else if (reg->size == 16)
378 iowrite16(value, p->membase + (reg->offset << p->regshift));
379 else
380 WARN(1, "Invalid register access\n");
381}
382
Paul Mundt61a69762011-06-14 12:40:19 +0900383static int sci_probe_regmap(struct plat_sci_port *cfg)
384{
385 switch (cfg->type) {
386 case PORT_SCI:
387 cfg->regtype = SCIx_SCI_REGTYPE;
388 break;
389 case PORT_IRDA:
390 cfg->regtype = SCIx_IRDA_REGTYPE;
391 break;
392 case PORT_SCIFA:
393 cfg->regtype = SCIx_SCIFA_REGTYPE;
394 break;
395 case PORT_SCIFB:
396 cfg->regtype = SCIx_SCIFB_REGTYPE;
397 break;
398 case PORT_SCIF:
399 /*
400 * The SH-4 is a bit of a misnomer here, although that's
401 * where this particular port layout originated. This
402 * configuration (or some slight variation thereof)
403 * remains the dominant model for all SCIFs.
404 */
405 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
406 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +0200407 case PORT_HSCIF:
408 cfg->regtype = SCIx_HSCIF_REGTYPE;
409 break;
Paul Mundt61a69762011-06-14 12:40:19 +0900410 default:
411 printk(KERN_ERR "Can't probe register map for given port\n");
412 return -EINVAL;
413 }
414
415 return 0;
416}
417
Paul Mundt23241d42011-06-28 13:55:31 +0900418static void sci_port_enable(struct sci_port *sci_port)
419{
420 if (!sci_port->port.dev)
421 return;
422
423 pm_runtime_get_sync(sci_port->port.dev);
424
Laurent Pinchartb016b642013-11-28 18:11:46 +0100425 clk_prepare_enable(sci_port->iclk);
Paul Mundt23241d42011-06-28 13:55:31 +0900426 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
Laurent Pinchartb016b642013-11-28 18:11:46 +0100427 clk_prepare_enable(sci_port->fclk);
Paul Mundt23241d42011-06-28 13:55:31 +0900428}
429
430static void sci_port_disable(struct sci_port *sci_port)
431{
432 if (!sci_port->port.dev)
433 return;
434
Laurent Pinchartcaec7032013-11-28 18:11:45 +0100435 /* Cancel the break timer to ensure that the timer handler will not try
436 * to access the hardware with clocks and power disabled. Reset the
437 * break flag to make the break debouncing state machine ready for the
438 * next break.
439 */
440 del_timer_sync(&sci_port->break_timer);
441 sci_port->break_flag = 0;
442
Laurent Pinchartb016b642013-11-28 18:11:46 +0100443 clk_disable_unprepare(sci_port->fclk);
444 clk_disable_unprepare(sci_port->iclk);
Paul Mundt23241d42011-06-28 13:55:31 +0900445
446 pm_runtime_put_sync(sci_port->port.dev);
447}
448
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900449#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900450
451#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900452static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned short status;
455 int c;
456
Paul Mundte108b2c2006-09-27 16:32:13 +0900457 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900458 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (status & SCxSR_ERRORS(port)) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900460 serial_port_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 continue;
462 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500463 break;
464 } while (1);
465
466 if (!(status & SCxSR_RDxF(port)))
467 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900468
Paul Mundtb12bb292012-03-30 19:50:15 +0900469 c = serial_port_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900470
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900471 /* Dummy read */
Paul Mundtb12bb292012-03-30 19:50:15 +0900472 serial_port_in(port, SCxSR);
473 serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 return c;
476}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900479static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned short status;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900484 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } while (!(status & SCxSR_TDxE(port)));
486
Paul Mundtb12bb292012-03-30 19:50:15 +0900487 serial_port_out(port, SCxTDR, c);
488 serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900490#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Paul Mundt61a69762011-06-14 12:40:19 +0900492static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900493{
Paul Mundt61a69762011-06-14 12:40:19 +0900494 struct sci_port *s = to_sci_port(port);
495 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900496
Paul Mundt61a69762011-06-14 12:40:19 +0900497 /*
498 * Use port-specific handler if provided.
499 */
500 if (s->cfg->ops && s->cfg->ops->init_pins) {
501 s->cfg->ops->init_pins(port, cflag);
502 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Paul Mundt61a69762011-06-14 12:40:19 +0900505 /*
506 * For the generic path SCSPTR is necessary. Bail out if that's
507 * unavailable, too.
508 */
509 if (!reg->size)
510 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800511
Paul Mundtfaf02f82011-12-02 17:44:50 +0900512 if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
513 ((!(cflag & CRTSCTS)))) {
514 unsigned short status;
515
Paul Mundtb12bb292012-03-30 19:50:15 +0900516 status = serial_port_in(port, SCSPTR);
Paul Mundtfaf02f82011-12-02 17:44:50 +0900517 status &= ~SCSPTR_CTSIO;
518 status |= SCSPTR_RTSIO;
Paul Mundtb12bb292012-03-30 19:50:15 +0900519 serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
Paul Mundtfaf02f82011-12-02 17:44:50 +0900520 }
Paul Mundtd5701642008-12-16 20:07:27 +0900521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900523static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900524{
Paul Mundt72b294c2011-06-14 17:38:19 +0900525 struct plat_sci_reg *reg;
526
527 reg = sci_getreg(port, SCTFDR);
528 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900529 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900530
531 reg = sci_getreg(port, SCFDR);
532 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900533 return serial_port_in(port, SCFDR) >> 8;
Paul Mundt72b294c2011-06-14 17:38:19 +0900534
Paul Mundtb12bb292012-03-30 19:50:15 +0900535 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900536}
537
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900538static int sci_txroom(struct uart_port *port)
539{
Paul Mundt72b294c2011-06-14 17:38:19 +0900540 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900541}
542
543static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900544{
Paul Mundt72b294c2011-06-14 17:38:19 +0900545 struct plat_sci_reg *reg;
546
547 reg = sci_getreg(port, SCRFDR);
548 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900549 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900550
551 reg = sci_getreg(port, SCFDR);
552 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900553 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900554
Paul Mundtb12bb292012-03-30 19:50:15 +0900555 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900556}
557
Paul Mundt514820e2011-06-08 18:51:32 +0900558/*
559 * SCI helper for checking the state of the muxed port/RXD pins.
560 */
561static inline int sci_rxd_in(struct uart_port *port)
562{
563 struct sci_port *s = to_sci_port(port);
564
565 if (s->cfg->port_reg <= 0)
566 return 1;
567
Paul Mundt0dd4d5c2012-10-15 14:08:48 +0900568 /* Cast for ARM damage */
Laurent Pincharte2afca62013-12-11 13:40:31 +0100569 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
Paul Mundt514820e2011-06-08 18:51:32 +0900570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* ********************************************************************** *
573 * the interrupt related routines *
574 * ********************************************************************** */
575
576static void sci_transmit_chars(struct uart_port *port)
577{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700578 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned short status;
581 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900582 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Paul Mundtb12bb292012-03-30 19:50:15 +0900584 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!(status & SCxSR_TDxE(port))) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900586 ctrl = serial_port_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900587 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900588 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900589 else
Paul Mundt8e698612009-06-24 19:44:32 +0900590 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900591 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593 }
594
Paul Mundt72b294c2011-06-14 17:38:19 +0900595 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 do {
598 unsigned char c;
599
600 if (port->x_char) {
601 c = port->x_char;
602 port->x_char = 0;
603 } else if (!uart_circ_empty(xmit) && !stopped) {
604 c = xmit->buf[xmit->tail];
605 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
606 } else {
607 break;
608 }
609
Paul Mundtb12bb292012-03-30 19:50:15 +0900610 serial_port_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 port->icount.tx++;
613 } while (--count > 0);
614
Paul Mundtb12bb292012-03-30 19:50:15 +0900615 serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
618 uart_write_wakeup(port);
619 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100620 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900622 ctrl = serial_port_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900624 if (port->type != PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900625 serial_port_in(port, SCxSR); /* Dummy read */
626 serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Paul Mundt8e698612009-06-24 19:44:32 +0900629 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900630 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632}
633
634/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900635#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900637static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900639 struct sci_port *sci_port = to_sci_port(port);
Jiri Slaby227434f2013-01-03 15:53:01 +0100640 struct tty_port *tport = &port->state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int i, count, copied = 0;
642 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800643 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Paul Mundtb12bb292012-03-30 19:50:15 +0900645 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!(status & SCxSR_RDxF(port)))
647 return;
648
649 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* Don't copy more bytes than there is room for in the buffer */
Jiri Slaby227434f2013-01-03 15:53:01 +0100651 count = tty_buffer_request_room(tport, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* If for any reason we can't copy more data, we're done! */
654 if (count == 0)
655 break;
656
657 if (port->type == PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900658 char c = serial_port_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900659 if (uart_handle_sysrq_char(port, c) ||
660 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900662 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100663 tty_insert_flip_char(tport, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900665 for (i = 0; i < count; i++) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900666 char c = serial_port_in(port, SCxRDR);
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900667
Paul Mundtb12bb292012-03-30 19:50:15 +0900668 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#if defined(CONFIG_CPU_SH3)
670 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900671 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if ((c == 0) &&
673 (status & SCxSR_FER(port))) {
674 count--; i--;
675 continue;
676 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900679 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900680 sci_port->break_flag = 0;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (STEPFN(c)) {
683 count--; i--;
684 continue;
685 }
686 }
687#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100688 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 count--; i--;
690 continue;
691 }
692
693 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900694 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800695 flag = TTY_FRAME;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900696 port->icount.frame++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900697 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900698 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800699 flag = TTY_PARITY;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900700 port->icount.parity++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900701 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800702 } else
703 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900704
Jiri Slaby92a19f92013-01-03 15:53:03 +0100705 tty_insert_flip_char(tport, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 }
708
Paul Mundtb12bb292012-03-30 19:50:15 +0900709 serial_port_in(port, SCxSR); /* dummy read */
710 serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 copied += count;
713 port->icount.rx += count;
714 }
715
716 if (copied) {
717 /* Tell the rest of the system the news. New characters! */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100718 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900720 serial_port_in(port, SCxSR); /* dummy read */
721 serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723}
724
725#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900726
727/*
728 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * 1 per millisecond or so during the break period, for 9600 baud.
730 * So dont bother disabling interrupts.
731 * But dont want more than 1 break event.
732 * Use a kernel timer to periodically poll the rx line until
733 * the break is finished.
734 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900735static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900737 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* Ensure that two consecutive samples find the break over. */
741static void sci_break_timer(unsigned long data)
742{
Paul Mundte108b2c2006-09-27 16:32:13 +0900743 struct sci_port *port = (struct sci_port *)data;
744
745 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900747 sci_schedule_break_timer(port);
748 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* break is over. */
750 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900751 sci_schedule_break_timer(port);
752 } else
753 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900756static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900759 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100760 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900761 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Paul Mundtdebf9502011-06-08 18:19:37 +0900763 /*
764 * Handle overruns, if supported.
765 */
766 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
767 if (status & (1 << s->cfg->overrun_bit)) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900768 port->icount.overrun++;
769
Paul Mundtdebf9502011-06-08 18:19:37 +0900770 /* overrun error */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100771 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
Paul Mundtdebf9502011-06-08 18:19:37 +0900772 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900773
Paul Mundtdebf9502011-06-08 18:19:37 +0900774 dev_notice(port->dev, "overrun error");
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Paul Mundte108b2c2006-09-27 16:32:13 +0900778 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (sci_rxd_in(port) == 0) {
780 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900781 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900782
783 if (!sci_port->break_flag) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900784 port->icount.brk++;
785
Paul Mundte108b2c2006-09-27 16:32:13 +0900786 sci_port->break_flag = 1;
787 sci_schedule_break_timer(sci_port);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900790 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900792
793 dev_dbg(port->dev, "BREAK detected\n");
794
Jiri Slaby92a19f92013-01-03 15:53:03 +0100795 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900796 copied++;
797 }
798
Paul Mundte108b2c2006-09-27 16:32:13 +0900799 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* frame error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900801 port->icount.frame++;
802
Jiri Slaby92a19f92013-01-03 15:53:03 +0100803 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800804 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900805
806 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
809
Paul Mundte108b2c2006-09-27 16:32:13 +0900810 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* parity error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900812 port->icount.parity++;
813
Jiri Slaby92a19f92013-01-03 15:53:03 +0100814 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
Paul Mundte108b2c2006-09-27 16:32:13 +0900815 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900816
817 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
Alan Cox33f0f882006-01-09 20:54:13 -0800820 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100821 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 return copied;
824}
825
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900826static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900827{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100828 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900829 struct sci_port *s = to_sci_port(port);
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900830 struct plat_sci_reg *reg;
Paul Mundtd830fa42008-12-16 19:29:38 +0900831 int copied = 0;
832
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900833 reg = sci_getreg(port, SCLSR);
834 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +0900835 return 0;
836
Paul Mundtb12bb292012-03-30 19:50:15 +0900837 if ((serial_port_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
838 serial_port_out(port, SCLSR, 0);
Paul Mundtd830fa42008-12-16 19:29:38 +0900839
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900840 port->icount.overrun++;
841
Jiri Slaby92a19f92013-01-03 15:53:03 +0100842 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100843 tty_flip_buffer_push(tport);
Paul Mundtd830fa42008-12-16 19:29:38 +0900844
845 dev_notice(port->dev, "overrun error\n");
846 copied++;
847 }
848
849 return copied;
850}
851
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900852static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900855 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100856 struct tty_port *tport = &port->state->port;
Magnus Damma5660ad2009-01-21 15:14:38 +0000857 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900859 if (uart_handle_break(port))
860 return 0;
861
Paul Mundtb7a76e42006-02-01 03:06:06 -0800862 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#if defined(CONFIG_CPU_SH3)
864 /* Debounce break */
865 s->break_flag = 1;
866#endif
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900867
868 port->icount.brk++;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Notify of BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100871 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800872 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900873
874 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Alan Cox33f0f882006-01-09 20:54:13 -0800877 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100878 tty_flip_buffer_push(tport);
Paul Mundte108b2c2006-09-27 16:32:13 +0900879
Paul Mundtd830fa42008-12-16 19:29:38 +0900880 copied += sci_handle_fifo_overrun(port);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return copied;
883}
884
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900885static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900887#ifdef CONFIG_SERIAL_SH_SCI_DMA
888 struct uart_port *port = ptr;
889 struct sci_port *s = to_sci_port(port);
890
891 if (s->chan_rx) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900892 u16 scr = serial_port_in(port, SCSCR);
893 u16 ssr = serial_port_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900894
895 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000896 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000897 disable_irq_nosync(irq);
898 scr |= 0x4000;
899 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900900 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000901 }
Paul Mundtb12bb292012-03-30 19:50:15 +0900902 serial_port_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900903 /* Clear current interrupt */
Paul Mundtb12bb292012-03-30 19:50:15 +0900904 serial_port_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000905 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
906 jiffies, s->rx_timeout);
907 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900908
909 return IRQ_HANDLED;
910 }
911#endif
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* I think sci_receive_chars has to be called irrespective
914 * of whether the I_IXOFF is set, otherwise, how is the interrupt
915 * to be disabled?
916 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900917 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return IRQ_HANDLED;
920}
921
David Howells7d12e782006-10-05 14:55:46 +0100922static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900925 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stuart Menefyfd78a762009-07-29 23:01:24 +0900927 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900929 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 return IRQ_HANDLED;
932}
933
David Howells7d12e782006-10-05 14:55:46 +0100934static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct uart_port *port = ptr;
937
938 /* Handle errors */
939 if (port->type == PORT_SCI) {
940 if (sci_handle_errors(port)) {
941 /* discard character in rx buffer */
Paul Mundtb12bb292012-03-30 19:50:15 +0900942 serial_port_in(port, SCxSR);
943 serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900946 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100947 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Paul Mundtb12bb292012-03-30 19:50:15 +0900950 serial_port_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100953 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 return IRQ_HANDLED;
956}
957
David Howells7d12e782006-10-05 14:55:46 +0100958static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct uart_port *port = ptr;
961
962 /* Handle BREAKs */
963 sci_handle_breaks(port);
Paul Mundtb12bb292012-03-30 19:50:15 +0900964 serial_port_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 return IRQ_HANDLED;
967}
968
Paul Mundtf43dc232011-01-13 15:06:28 +0900969static inline unsigned long port_rx_irq_mask(struct uart_port *port)
970{
971 /*
972 * Not all ports (such as SCIFA) will support REIE. Rather than
973 * special-casing the port type, we check the port initialization
974 * IRQ enable mask to see whether the IRQ is desired at all. If
975 * it's unset, it's logically inferred that there's no point in
976 * testing for it.
977 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900978 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900979}
980
David Howells7d12e782006-10-05 14:55:46 +0100981static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Magnus Damm44e18e92009-07-03 08:39:34 +0000983 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900984 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900985 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900986 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Paul Mundtb12bb292012-03-30 19:50:15 +0900988 ssr_status = serial_port_in(port, SCxSR);
989 scr_status = serial_port_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900990 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900993 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900994 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900995 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900996
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900997 /*
998 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
999 * DR flags
1000 */
1001 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +09001002 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +09001003 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001006 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001007 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001010 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001011 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Michael Trimarchia8884e32008-10-31 16:10:23 +09001013 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001017 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * ports' baud rate when the peripheral clock changes.
1019 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001020static int sci_notifier(struct notifier_block *self,
1021 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Magnus Damme552de22009-01-21 15:13:42 +00001023 struct sci_port *sci_port;
1024 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Paul Mundtd535a232011-01-19 17:19:35 +09001026 sci_port = container_of(self, struct sci_port, freq_transition);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +00001029 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +09001030 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +09001031
Paul Mundtd535a232011-01-19 17:19:35 +09001032 spin_lock_irqsave(&port->lock, flags);
1033 port->uartclk = clk_get_rate(sci_port->iclk);
1034 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +00001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return NOTIFY_OK;
1038}
Magnus Damm501b8252009-01-21 15:14:30 +00001039
Paul Mundt9174fc82011-06-28 15:25:36 +09001040static struct sci_irq_desc {
1041 const char *desc;
1042 irq_handler_t handler;
1043} sci_irq_desc[] = {
1044 /*
1045 * Split out handlers, the default case.
1046 */
1047 [SCIx_ERI_IRQ] = {
1048 .desc = "rx err",
1049 .handler = sci_er_interrupt,
1050 },
1051
1052 [SCIx_RXI_IRQ] = {
1053 .desc = "rx full",
1054 .handler = sci_rx_interrupt,
1055 },
1056
1057 [SCIx_TXI_IRQ] = {
1058 .desc = "tx empty",
1059 .handler = sci_tx_interrupt,
1060 },
1061
1062 [SCIx_BRI_IRQ] = {
1063 .desc = "break",
1064 .handler = sci_br_interrupt,
1065 },
1066
1067 /*
1068 * Special muxed handler.
1069 */
1070 [SCIx_MUX_IRQ] = {
1071 .desc = "mux",
1072 .handler = sci_mpxed_interrupt,
1073 },
1074};
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076static int sci_request_irq(struct sci_port *port)
1077{
Paul Mundt9174fc82011-06-28 15:25:36 +09001078 struct uart_port *up = &port->port;
1079 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Paul Mundt9174fc82011-06-28 15:25:36 +09001081 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
1082 struct sci_irq_desc *desc;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001083 int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001084
Paul Mundt9174fc82011-06-28 15:25:36 +09001085 if (SCIx_IRQ_IS_MUXED(port)) {
1086 i = SCIx_MUX_IRQ;
1087 irq = up->irq;
Paul Mundt0e8963d2012-05-18 18:21:06 +09001088 } else {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001089 irq = port->irqs[i];
Paul Mundt9174fc82011-06-28 15:25:36 +09001090
Paul Mundt0e8963d2012-05-18 18:21:06 +09001091 /*
1092 * Certain port types won't support all of the
1093 * available interrupt sources.
1094 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001095 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001096 continue;
1097 }
1098
Paul Mundt9174fc82011-06-28 15:25:36 +09001099 desc = sci_irq_desc + i;
1100 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1101 dev_name(up->dev), desc->desc);
1102 if (!port->irqstr[j]) {
1103 dev_err(up->dev, "Failed to allocate %s IRQ string\n",
1104 desc->desc);
1105 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Paul Mundt762c69e2008-12-16 18:55:26 +09001107
Paul Mundt9174fc82011-06-28 15:25:36 +09001108 ret = request_irq(irq, desc->handler, up->irqflags,
1109 port->irqstr[j], port);
1110 if (unlikely(ret)) {
1111 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1112 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115
1116 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001117
1118out_noirq:
1119 while (--i >= 0)
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001120 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001121
1122out_nomem:
1123 while (--j >= 0)
1124 kfree(port->irqstr[j]);
1125
1126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129static void sci_free_irq(struct sci_port *port)
1130{
1131 int i;
1132
Paul Mundt9174fc82011-06-28 15:25:36 +09001133 /*
1134 * Intentionally in reverse order so we iterate over the muxed
1135 * IRQ first.
1136 */
1137 for (i = 0; i < SCIx_NR_IRQS; i++) {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001138 int irq = port->irqs[i];
Paul Mundt0e8963d2012-05-18 18:21:06 +09001139
1140 /*
1141 * Certain port types won't support all of the available
1142 * interrupt sources.
1143 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001144 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001145 continue;
1146
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001147 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001148 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Paul Mundt9174fc82011-06-28 15:25:36 +09001150 if (SCIx_IRQ_IS_MUXED(port)) {
1151 /* If there's only one IRQ, we're done. */
1152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 }
1155}
1156
Paul Mundt50f09592011-12-02 20:09:48 +09001157static const char *sci_gpio_names[SCIx_NR_FNS] = {
1158 "sck", "rxd", "txd", "cts", "rts",
1159};
1160
1161static const char *sci_gpio_str(unsigned int index)
1162{
1163 return sci_gpio_names[index];
1164}
1165
Bill Pemberton9671f092012-11-19 13:21:50 -05001166static void sci_init_gpios(struct sci_port *port)
Paul Mundt50f09592011-12-02 20:09:48 +09001167{
1168 struct uart_port *up = &port->port;
1169 int i;
1170
1171 if (!port->cfg)
1172 return;
1173
1174 for (i = 0; i < SCIx_NR_FNS; i++) {
1175 const char *desc;
1176 int ret;
1177
1178 if (!port->cfg->gpios[i])
1179 continue;
1180
1181 desc = sci_gpio_str(i);
1182
1183 port->gpiostr[i] = kasprintf(GFP_KERNEL, "%s:%s",
1184 dev_name(up->dev), desc);
1185
1186 /*
1187 * If we've failed the allocation, we can still continue
1188 * on with a NULL string.
1189 */
1190 if (!port->gpiostr[i])
1191 dev_notice(up->dev, "%s string allocation failure\n",
1192 desc);
1193
1194 ret = gpio_request(port->cfg->gpios[i], port->gpiostr[i]);
1195 if (unlikely(ret != 0)) {
1196 dev_notice(up->dev, "failed %s gpio request\n", desc);
1197
1198 /*
1199 * If we can't get the GPIO for whatever reason,
1200 * no point in keeping the verbose string around.
1201 */
1202 kfree(port->gpiostr[i]);
1203 }
1204 }
1205}
1206
1207static void sci_free_gpios(struct sci_port *port)
1208{
1209 int i;
1210
1211 for (i = 0; i < SCIx_NR_FNS; i++)
1212 if (port->cfg->gpios[i]) {
1213 gpio_free(port->cfg->gpios[i]);
1214 kfree(port->gpiostr[i]);
1215 }
1216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218static unsigned int sci_tx_empty(struct uart_port *port)
1219{
Paul Mundtb12bb292012-03-30 19:50:15 +09001220 unsigned short status = serial_port_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001221 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001222
1223 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Paul Mundtcdf7c422011-11-24 20:18:32 +09001226/*
1227 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1228 * CTS/RTS is supported in hardware by at least one port and controlled
1229 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1230 * handled via the ->init_pins() op, which is a bit of a one-way street,
1231 * lacking any ability to defer pin control -- this will later be
1232 * converted over to the GPIO framework).
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001233 *
1234 * Other modes (such as loopback) are supported generically on certain
1235 * port types, but not others. For these it's sufficient to test for the
1236 * existence of the support register and simply ignore the port type.
Paul Mundtcdf7c422011-11-24 20:18:32 +09001237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1239{
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001240 if (mctrl & TIOCM_LOOP) {
1241 struct plat_sci_reg *reg;
1242
1243 /*
1244 * Standard loopback mode for SCFCR ports.
1245 */
1246 reg = sci_getreg(port, SCFCR);
1247 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09001248 serial_port_out(port, SCFCR, serial_port_in(port, SCFCR) | 1);
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252static unsigned int sci_get_mctrl(struct uart_port *port)
1253{
Paul Mundtcdf7c422011-11-24 20:18:32 +09001254 /*
1255 * CTS/RTS is handled in hardware when supported, while nothing
1256 * else is wired up. Keep it simple and simply assert DSR/CAR.
1257 */
1258 return TIOCM_DSR | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001261#ifdef CONFIG_SERIAL_SH_SCI_DMA
1262static void sci_dma_tx_complete(void *arg)
1263{
1264 struct sci_port *s = arg;
1265 struct uart_port *port = &s->port;
1266 struct circ_buf *xmit = &port->state->xmit;
1267 unsigned long flags;
1268
1269 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1270
1271 spin_lock_irqsave(&port->lock, flags);
1272
Magnus Dammf354a382010-03-19 04:47:01 +00001273 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001274 xmit->tail &= UART_XMIT_SIZE - 1;
1275
Magnus Dammf354a382010-03-19 04:47:01 +00001276 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001277
1278 async_tx_ack(s->desc_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001279 s->desc_tx = NULL;
1280
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001281 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1282 uart_write_wakeup(port);
1283
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001284 if (!uart_circ_empty(xmit)) {
Yoshii Takashi49d4bca2012-03-14 16:14:43 +09001285 s->cookie_tx = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001286 schedule_work(&s->work_tx);
Yoshii Takashi49d4bca2012-03-14 16:14:43 +09001287 } else {
1288 s->cookie_tx = -EINVAL;
1289 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001290 u16 ctrl = serial_port_in(port, SCSCR);
1291 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Yoshii Takashi49d4bca2012-03-14 16:14:43 +09001292 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001293 }
1294
1295 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001296}
1297
1298/* Locking: called with port lock held */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001299static int sci_dma_rx_push(struct sci_port *s, size_t count)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001300{
1301 struct uart_port *port = &s->port;
Jiri Slaby227434f2013-01-03 15:53:01 +01001302 struct tty_port *tport = &port->state->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001303 int i, active, room;
1304
Jiri Slaby227434f2013-01-03 15:53:01 +01001305 room = tty_buffer_request_room(tport, count);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001306
1307 if (s->active_rx == s->cookie_rx[0]) {
1308 active = 0;
1309 } else if (s->active_rx == s->cookie_rx[1]) {
1310 active = 1;
1311 } else {
1312 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1313 return 0;
1314 }
1315
1316 if (room < count)
Laurent Pincharte2afca62013-12-11 13:40:31 +01001317 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001318 count - room);
1319 if (!room)
1320 return room;
1321
1322 for (i = 0; i < room; i++)
Jiri Slaby92a19f92013-01-03 15:53:03 +01001323 tty_insert_flip_char(tport, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001324 TTY_NORMAL);
1325
1326 port->icount.rx += room;
1327
1328 return room;
1329}
1330
1331static void sci_dma_rx_complete(void *arg)
1332{
1333 struct sci_port *s = arg;
1334 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001335 unsigned long flags;
1336 int count;
1337
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001338 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001339
1340 spin_lock_irqsave(&port->lock, flags);
1341
Jiri Slaby92a19f92013-01-03 15:53:03 +01001342 count = sci_dma_rx_push(s, s->buf_len_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001343
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001344 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001345
1346 spin_unlock_irqrestore(&port->lock, flags);
1347
1348 if (count)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001349 tty_flip_buffer_push(&port->state->port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001350
1351 schedule_work(&s->work_rx);
1352}
1353
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001354static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1355{
1356 struct dma_chan *chan = s->chan_rx;
1357 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001358
1359 s->chan_rx = NULL;
1360 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1361 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001362 if (sg_dma_address(&s->sg_rx[0]))
1363 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1364 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001365 if (enable_pio)
1366 sci_start_rx(port);
1367}
1368
1369static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1370{
1371 struct dma_chan *chan = s->chan_tx;
1372 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001373
1374 s->chan_tx = NULL;
1375 s->cookie_tx = -EINVAL;
1376 dma_release_channel(chan);
1377 if (enable_pio)
1378 sci_start_tx(port);
1379}
1380
1381static void sci_submit_rx(struct sci_port *s)
1382{
1383 struct dma_chan *chan = s->chan_rx;
1384 int i;
1385
1386 for (i = 0; i < 2; i++) {
1387 struct scatterlist *sg = &s->sg_rx[i];
1388 struct dma_async_tx_descriptor *desc;
1389
Alexandre Bounine16052822012-03-08 16:11:18 -05001390 desc = dmaengine_prep_slave_sg(chan,
Vinod Koula485df42011-10-14 10:47:38 +05301391 sg, 1, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001392
1393 if (desc) {
1394 s->desc_rx[i] = desc;
1395 desc->callback = sci_dma_rx_complete;
1396 desc->callback_param = s;
1397 s->cookie_rx[i] = desc->tx_submit(desc);
1398 }
1399
1400 if (!desc || s->cookie_rx[i] < 0) {
1401 if (i) {
1402 async_tx_ack(s->desc_rx[0]);
1403 s->cookie_rx[0] = -EINVAL;
1404 }
1405 if (desc) {
1406 async_tx_ack(desc);
1407 s->cookie_rx[i] = -EINVAL;
1408 }
1409 dev_warn(s->port.dev,
1410 "failed to re-start DMA, using PIO\n");
1411 sci_rx_dma_release(s, true);
1412 return;
1413 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001414 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1415 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001416 }
1417
1418 s->active_rx = s->cookie_rx[0];
1419
1420 dma_async_issue_pending(chan);
1421}
1422
1423static void work_fn_rx(struct work_struct *work)
1424{
1425 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1426 struct uart_port *port = &s->port;
1427 struct dma_async_tx_descriptor *desc;
1428 int new;
1429
1430 if (s->active_rx == s->cookie_rx[0]) {
1431 new = 0;
1432 } else if (s->active_rx == s->cookie_rx[1]) {
1433 new = 1;
1434 } else {
1435 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1436 return;
1437 }
1438 desc = s->desc_rx[new];
1439
1440 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
Vinod Koul0b3d7d32013-10-16 21:06:19 +05301441 DMA_COMPLETE) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001442 /* Handle incomplete DMA receive */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001443 struct dma_chan *chan = s->chan_rx;
Guennadi Liakhovetski4dc4c512012-07-30 21:28:47 +02001444 struct shdma_desc *sh_desc = container_of(desc,
1445 struct shdma_desc, async_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001446 unsigned long flags;
1447 int count;
1448
Linus Walleij05827632010-05-17 16:30:42 -07001449 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Laurent Pincharte2afca62013-12-11 13:40:31 +01001450 dev_dbg(port->dev, "Read %zu bytes with cookie %d\n",
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001451 sh_desc->partial, sh_desc->cookie);
1452
1453 spin_lock_irqsave(&port->lock, flags);
Jiri Slaby92a19f92013-01-03 15:53:03 +01001454 count = sci_dma_rx_push(s, sh_desc->partial);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001455 spin_unlock_irqrestore(&port->lock, flags);
1456
1457 if (count)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001458 tty_flip_buffer_push(&port->state->port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001459
1460 sci_submit_rx(s);
1461
1462 return;
1463 }
1464
1465 s->cookie_rx[new] = desc->tx_submit(desc);
1466 if (s->cookie_rx[new] < 0) {
1467 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1468 sci_rx_dma_release(s, true);
1469 return;
1470 }
1471
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001472 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001473
1474 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1475 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001476}
1477
1478static void work_fn_tx(struct work_struct *work)
1479{
1480 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1481 struct dma_async_tx_descriptor *desc;
1482 struct dma_chan *chan = s->chan_tx;
1483 struct uart_port *port = &s->port;
1484 struct circ_buf *xmit = &port->state->xmit;
1485 struct scatterlist *sg = &s->sg_tx;
1486
1487 /*
1488 * DMA is idle now.
1489 * Port xmit buffer is already mapped, and it is one page... Just adjust
1490 * offsets and lengths. Since it is a circular buffer, we have to
1491 * transmit till the end, and then the rest. Take the port lock to get a
1492 * consistent xmit buffer state.
1493 */
1494 spin_lock_irq(&port->lock);
1495 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001496 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001497 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001498 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001499 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001500 spin_unlock_irq(&port->lock);
1501
Magnus Dammf354a382010-03-19 04:47:01 +00001502 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001503
Alexandre Bounine16052822012-03-08 16:11:18 -05001504 desc = dmaengine_prep_slave_sg(chan,
Vinod Koula485df42011-10-14 10:47:38 +05301505 sg, s->sg_len_tx, DMA_MEM_TO_DEV,
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001506 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1507 if (!desc) {
1508 /* switch to PIO */
1509 sci_tx_dma_release(s, true);
1510 return;
1511 }
1512
1513 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1514
1515 spin_lock_irq(&port->lock);
1516 s->desc_tx = desc;
1517 desc->callback = sci_dma_tx_complete;
1518 desc->callback_param = s;
1519 spin_unlock_irq(&port->lock);
1520 s->cookie_tx = desc->tx_submit(desc);
1521 if (s->cookie_tx < 0) {
1522 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1523 /* switch to PIO */
1524 sci_tx_dma_release(s, true);
1525 return;
1526 }
1527
1528 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1529 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1530
1531 dma_async_issue_pending(chan);
1532}
1533#endif
1534
Russell Kingb129a8c2005-08-31 10:12:14 +01001535static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001537 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001538 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001540#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001541 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001542 u16 new, scr = serial_port_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001543 if (s->chan_tx)
1544 new = scr | 0x8000;
1545 else
1546 new = scr & ~0x8000;
1547 if (new != scr)
Paul Mundtb12bb292012-03-30 19:50:15 +09001548 serial_port_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001549 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001550
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001551 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
Yoshii Takashi49d4bca2012-03-14 16:14:43 +09001552 s->cookie_tx < 0) {
1553 s->cookie_tx = 0;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001554 schedule_work(&s->work_tx);
Yoshii Takashi49d4bca2012-03-14 16:14:43 +09001555 }
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001556#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001557
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001558 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001559 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
Paul Mundtb12bb292012-03-30 19:50:15 +09001560 ctrl = serial_port_in(port, SCSCR);
1561 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Russell Kingb129a8c2005-08-31 10:12:14 +01001565static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 unsigned short ctrl;
1568
1569 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Paul Mundtb12bb292012-03-30 19:50:15 +09001570 ctrl = serial_port_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001571
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001572 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001573 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001574
Paul Mundt8e698612009-06-24 19:44:32 +09001575 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001576
Paul Mundtb12bb292012-03-30 19:50:15 +09001577 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001580static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 unsigned short ctrl;
1583
Paul Mundtb12bb292012-03-30 19:50:15 +09001584 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
Paul Mundtf43dc232011-01-13 15:06:28 +09001585
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001586 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001587 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001588
Paul Mundtb12bb292012-03-30 19:50:15 +09001589 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592static void sci_stop_rx(struct uart_port *port)
1593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 unsigned short ctrl;
1595
Paul Mundtb12bb292012-03-30 19:50:15 +09001596 ctrl = serial_port_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001597
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001598 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001599 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001600
1601 ctrl &= ~port_rx_irq_mask(port);
1602
Paul Mundtb12bb292012-03-30 19:50:15 +09001603 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606static void sci_enable_ms(struct uart_port *port)
1607{
Paul Mundtd39ec6c2011-11-24 19:36:46 +09001608 /*
1609 * Not supported by hardware, always a nop.
1610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613static void sci_break_ctl(struct uart_port *port, int break_state)
1614{
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001615 struct sci_port *s = to_sci_port(port);
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001616 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001617 unsigned short scscr, scsptr;
1618
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001619 /* check wheter the port has SCSPTR */
1620 if (!reg->size) {
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001621 /*
1622 * Not supported by hardware. Most parts couple break and rx
1623 * interrupts together, with break detection always enabled.
1624 */
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001625 return;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001626 }
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001627
1628 scsptr = serial_port_in(port, SCSPTR);
1629 scscr = serial_port_in(port, SCSCR);
1630
1631 if (break_state == -1) {
1632 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1633 scscr &= ~SCSCR_TE;
1634 } else {
1635 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1636 scscr |= SCSCR_TE;
1637 }
1638
1639 serial_port_out(port, SCSPTR, scsptr);
1640 serial_port_out(port, SCSCR, scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001643#ifdef CONFIG_SERIAL_SH_SCI_DMA
1644static bool filter(struct dma_chan *chan, void *slave)
1645{
1646 struct sh_dmae_slave *param = slave;
1647
1648 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
Guennadi Liakhovetskid6fa5a42012-05-09 17:09:17 +02001649 param->shdma_slave.slave_id);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001650
Guennadi Liakhovetskid6fa5a42012-05-09 17:09:17 +02001651 chan->private = &param->shdma_slave;
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02001652 return true;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001653}
1654
1655static void rx_timer_fn(unsigned long arg)
1656{
1657 struct sci_port *s = (struct sci_port *)arg;
1658 struct uart_port *port = &s->port;
Paul Mundtb12bb292012-03-30 19:50:15 +09001659 u16 scr = serial_port_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001660
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001661 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001662 scr &= ~0x4000;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001663 enable_irq(s->irqs[SCIx_RXI_IRQ]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001664 }
Paul Mundtb12bb292012-03-30 19:50:15 +09001665 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001666 dev_dbg(port->dev, "DMA Rx timed out\n");
1667 schedule_work(&s->work_rx);
1668}
1669
1670static void sci_request_dma(struct uart_port *port)
1671{
1672 struct sci_port *s = to_sci_port(port);
1673 struct sh_dmae_slave *param;
1674 struct dma_chan *chan;
1675 dma_cap_mask_t mask;
1676 int nent;
1677
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02001678 dev_dbg(port->dev, "%s: port %d\n", __func__,
1679 port->line);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001680
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02001681 if (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001682 return;
1683
1684 dma_cap_zero(mask);
1685 dma_cap_set(DMA_SLAVE, mask);
1686
1687 param = &s->param_tx;
1688
1689 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Guennadi Liakhovetskid6fa5a42012-05-09 17:09:17 +02001690 param->shdma_slave.slave_id = s->cfg->dma_slave_tx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001691
1692 s->cookie_tx = -EINVAL;
1693 chan = dma_request_channel(mask, filter, param);
1694 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1695 if (chan) {
1696 s->chan_tx = chan;
1697 sg_init_table(&s->sg_tx, 1);
1698 /* UART circular tx buffer is an aligned page. */
Laurent Pincharte2afca62013-12-11 13:40:31 +01001699 BUG_ON((uintptr_t)port->state->xmit.buf & ~PAGE_MASK);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001700 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
Laurent Pincharte2afca62013-12-11 13:40:31 +01001701 UART_XMIT_SIZE,
1702 (uintptr_t)port->state->xmit.buf & ~PAGE_MASK);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001703 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1704 if (!nent)
1705 sci_tx_dma_release(s, false);
1706 else
Laurent Pincharte2afca62013-12-11 13:40:31 +01001707 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
1708 sg_dma_len(&s->sg_tx), port->state->xmit.buf,
1709 &sg_dma_address(&s->sg_tx));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001710
1711 s->sg_len_tx = nent;
1712
1713 INIT_WORK(&s->work_tx, work_fn_tx);
1714 }
1715
1716 param = &s->param_rx;
1717
1718 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Guennadi Liakhovetskid6fa5a42012-05-09 17:09:17 +02001719 param->shdma_slave.slave_id = s->cfg->dma_slave_rx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001720
1721 chan = dma_request_channel(mask, filter, param);
1722 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1723 if (chan) {
1724 dma_addr_t dma[2];
1725 void *buf[2];
1726 int i;
1727
1728 s->chan_rx = chan;
1729
1730 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1731 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1732 &dma[0], GFP_KERNEL);
1733
1734 if (!buf[0]) {
1735 dev_warn(port->dev,
1736 "failed to allocate dma buffer, using PIO\n");
1737 sci_rx_dma_release(s, true);
1738 return;
1739 }
1740
1741 buf[1] = buf[0] + s->buf_len_rx;
1742 dma[1] = dma[0] + s->buf_len_rx;
1743
1744 for (i = 0; i < 2; i++) {
1745 struct scatterlist *sg = &s->sg_rx[i];
1746
1747 sg_init_table(sg, 1);
1748 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
Laurent Pincharte2afca62013-12-11 13:40:31 +01001749 (uintptr_t)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001750 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001751 }
1752
1753 INIT_WORK(&s->work_rx, work_fn_rx);
1754 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1755
1756 sci_submit_rx(s);
1757 }
1758}
1759
1760static void sci_free_dma(struct uart_port *port)
1761{
1762 struct sci_port *s = to_sci_port(port);
1763
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001764 if (s->chan_tx)
1765 sci_tx_dma_release(s, false);
1766 if (s->chan_rx)
1767 sci_rx_dma_release(s, false);
1768}
Paul Mundt27bd1072011-01-19 15:37:31 +09001769#else
1770static inline void sci_request_dma(struct uart_port *port)
1771{
1772}
1773
1774static inline void sci_free_dma(struct uart_port *port)
1775{
1776}
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001777#endif
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779static int sci_startup(struct uart_port *port)
1780{
Magnus Damma5660ad2009-01-21 15:14:38 +00001781 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001782 unsigned long flags;
Paul Mundt073e84c2011-01-19 17:30:53 +09001783 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001785 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1786
Paul Mundt073e84c2011-01-19 17:30:53 +09001787 ret = sci_request_irq(s);
1788 if (unlikely(ret < 0))
1789 return ret;
1790
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001791 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001792
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001793 spin_lock_irqsave(&port->lock, flags);
Yoshinori Satod6569012005-10-14 15:59:12 -07001794 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001795 sci_start_rx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001796 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 return 0;
1799}
1800
1801static void sci_shutdown(struct uart_port *port)
1802{
Magnus Damma5660ad2009-01-21 15:14:38 +00001803 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001804 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001806 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1807
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001808 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001810 sci_stop_tx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001811 spin_unlock_irqrestore(&port->lock, flags);
Paul Mundt073e84c2011-01-19 17:30:53 +09001812
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001813 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 sci_free_irq(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Paul Mundt26c92f32009-06-24 18:23:52 +09001817static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1818 unsigned long freq)
1819{
1820 switch (algo_id) {
1821 case SCBRR_ALGO_1:
Laurent Pinchart6557b1f2013-12-06 10:59:12 +01001822 return freq / (16 * bps);
Paul Mundt26c92f32009-06-24 18:23:52 +09001823 case SCBRR_ALGO_2:
Laurent Pinchart6557b1f2013-12-06 10:59:12 +01001824 return DIV_ROUND_CLOSEST(freq, 32 * bps) - 1;
Paul Mundt26c92f32009-06-24 18:23:52 +09001825 case SCBRR_ALGO_3:
Laurent Pinchart6557b1f2013-12-06 10:59:12 +01001826 return freq / (8 * bps);
Paul Mundt26c92f32009-06-24 18:23:52 +09001827 case SCBRR_ALGO_4:
Laurent Pinchart6557b1f2013-12-06 10:59:12 +01001828 return DIV_ROUND_CLOSEST(freq, 16 * bps) - 1;
Paul Mundt26c92f32009-06-24 18:23:52 +09001829 }
1830
1831 /* Warn, but use a safe default */
1832 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001833
Paul Mundt26c92f32009-06-24 18:23:52 +09001834 return ((freq + 16 * bps) / (32 * bps) - 1);
1835}
1836
Ulrich Hechtf303b362013-05-31 17:57:01 +02001837/* calculate sample rate, BRR, and clock select for HSCIF */
1838static void sci_baud_calc_hscif(unsigned int bps, unsigned long freq,
1839 int *brr, unsigned int *srr,
1840 unsigned int *cks)
1841{
1842 int sr, c, br, err;
1843 int min_err = 1000; /* 100% */
1844
1845 /* Find the combination of sample rate and clock select with the
1846 smallest deviation from the desired baud rate. */
1847 for (sr = 8; sr <= 32; sr++) {
1848 for (c = 0; c <= 3; c++) {
1849 /* integerized formulas from HSCIF documentation */
1850 br = freq / (sr * (1 << (2 * c + 1)) * bps) - 1;
1851 if (br < 0 || br > 255)
1852 continue;
1853 err = freq / ((br + 1) * bps * sr *
1854 (1 << (2 * c + 1)) / 1000) - 1000;
1855 if (min_err > err) {
1856 min_err = err;
1857 *brr = br;
1858 *srr = sr - 1;
1859 *cks = c;
1860 }
1861 }
1862 }
1863
1864 if (min_err == 1000) {
1865 WARN_ON(1);
1866 /* use defaults */
1867 *brr = 255;
1868 *srr = 15;
1869 *cks = 0;
1870 }
1871}
1872
Magnus Damm1ba76222011-08-03 03:47:36 +00001873static void sci_reset(struct uart_port *port)
1874{
Paul Mundt0979e0e2011-11-24 18:35:49 +09001875 struct plat_sci_reg *reg;
Magnus Damm1ba76222011-08-03 03:47:36 +00001876 unsigned int status;
1877
1878 do {
Paul Mundtb12bb292012-03-30 19:50:15 +09001879 status = serial_port_in(port, SCxSR);
Magnus Damm1ba76222011-08-03 03:47:36 +00001880 } while (!(status & SCxSR_TEND(port)));
1881
Paul Mundtb12bb292012-03-30 19:50:15 +09001882 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
Magnus Damm1ba76222011-08-03 03:47:36 +00001883
Paul Mundt0979e0e2011-11-24 18:35:49 +09001884 reg = sci_getreg(port, SCFCR);
1885 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09001886 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Magnus Damm1ba76222011-08-03 03:47:36 +00001887}
1888
Alan Cox606d0992006-12-08 02:38:45 -08001889static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1890 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Paul Mundt00b9de92009-06-24 17:53:33 +09001892 struct sci_port *s = to_sci_port(port);
Paul Mundt0979e0e2011-11-24 18:35:49 +09001893 struct plat_sci_reg *reg;
Simon Hormand4759de2013-06-20 21:09:45 +09001894 unsigned int baud, smr_val, max_baud, cks = 0;
Paul Mundta2159b52008-10-02 19:09:13 +09001895 int t = -1;
Simon Hormand4759de2013-06-20 21:09:45 +09001896 unsigned int srr = 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Magnus Damm154280f2009-12-22 03:37:28 +00001898 /*
1899 * earlyprintk comes here early on with port->uartclk set to zero.
1900 * the clock framework is not up and running at this point so here
1901 * we assume that 115200 is the maximum baud rate. please note that
1902 * the baud rate is not programmed during earlyprintk - it is assumed
1903 * that the previous boot loader has enabled required clocks and
1904 * setup the baud rate generator hardware for us already.
1905 */
1906 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1907
1908 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
Ulrich Hechtf303b362013-05-31 17:57:01 +02001909 if (likely(baud && port->uartclk)) {
1910 if (s->cfg->scbrr_algo_id == SCBRR_ALGO_6) {
1911 sci_baud_calc_hscif(baud, port->uartclk, &t, &srr,
1912 &cks);
1913 } else {
1914 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud,
1915 port->uartclk);
1916 for (cks = 0; t >= 256 && cks <= 3; cks++)
1917 t >>= 2;
1918 }
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Paul Mundt23241d42011-06-28 13:55:31 +09001921 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00001922
Magnus Damm1ba76222011-08-03 03:47:36 +00001923 sci_reset(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001924
Paul Mundtb12bb292012-03-30 19:50:15 +09001925 smr_val = serial_port_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001926
Paul Mundte108b2c2006-09-27 16:32:13 +09001927 if ((termios->c_cflag & CSIZE) == CS7)
1928 smr_val |= 0x40;
1929 if (termios->c_cflag & PARENB)
1930 smr_val |= 0x20;
1931 if (termios->c_cflag & PARODD)
1932 smr_val |= 0x30;
1933 if (termios->c_cflag & CSTOPB)
1934 smr_val |= 0x08;
1935
1936 uart_update_timeout(port, termios->c_cflag, baud);
1937
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09001938 dev_dbg(port->dev, "%s: SMR %x, cks %x, t %x, SCSCR %x\n",
1939 __func__, smr_val, cks, t, s->cfg->scscr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001940
Takashi Yoshii4ffc3cd2012-11-16 10:52:22 +09001941 if (t >= 0) {
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09001942 serial_port_out(port, SCSMR, (smr_val & ~3) | cks);
Paul Mundtb12bb292012-03-30 19:50:15 +09001943 serial_port_out(port, SCBRR, t);
Ulrich Hechtf303b362013-05-31 17:57:01 +02001944 reg = sci_getreg(port, HSSRR);
1945 if (reg->size)
1946 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09001948 } else
1949 serial_port_out(port, SCSMR, smr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Paul Mundtd5701642008-12-16 20:07:27 +09001951 sci_init_pins(port, termios->c_cflag);
Paul Mundt0979e0e2011-11-24 18:35:49 +09001952
Paul Mundt73c3d532011-12-02 19:02:06 +09001953 reg = sci_getreg(port, SCFCR);
1954 if (reg->size) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001955 unsigned short ctrl = serial_port_in(port, SCFCR);
Paul Mundt0979e0e2011-11-24 18:35:49 +09001956
Paul Mundt73c3d532011-12-02 19:02:06 +09001957 if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
Paul Mundtfaf02f82011-12-02 17:44:50 +09001958 if (termios->c_cflag & CRTSCTS)
1959 ctrl |= SCFCR_MCE;
1960 else
1961 ctrl &= ~SCFCR_MCE;
Paul Mundtfaf02f82011-12-02 17:44:50 +09001962 }
Paul Mundt73c3d532011-12-02 19:02:06 +09001963
1964 /*
1965 * As we've done a sci_reset() above, ensure we don't
1966 * interfere with the FIFOs while toggling MCE. As the
1967 * reset values could still be set, simply mask them out.
1968 */
1969 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
1970
Paul Mundtb12bb292012-03-30 19:50:15 +09001971 serial_port_out(port, SCFCR, ctrl);
Paul Mundt0979e0e2011-11-24 18:35:49 +09001972 }
Paul Mundtb7a76e42006-02-01 03:06:06 -08001973
Paul Mundtb12bb292012-03-30 19:50:15 +09001974 serial_port_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001976#ifdef CONFIG_SERIAL_SH_SCI_DMA
1977 /*
1978 * Calculate delay for 1.5 DMA buffers: see
1979 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1980 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1981 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1982 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1983 * sizes), but it has been found out experimentally, that this is not
1984 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1985 * as a minimum seem to work perfectly.
1986 */
1987 if (s->chan_rx) {
1988 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1989 port->fifosize / 2;
1990 dev_dbg(port->dev,
1991 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1992 s->rx_timeout * 1000 / HZ, port->timeout);
1993 if (s->rx_timeout < msecs_to_jiffies(20))
1994 s->rx_timeout = msecs_to_jiffies(20);
1995 }
1996#endif
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001999 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00002000
Paul Mundt23241d42011-06-28 13:55:31 +09002001 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002004static void sci_pm(struct uart_port *port, unsigned int state,
2005 unsigned int oldstate)
2006{
2007 struct sci_port *sci_port = to_sci_port(port);
2008
2009 switch (state) {
2010 case 3:
2011 sci_port_disable(sci_port);
2012 break;
2013 default:
2014 sci_port_enable(sci_port);
2015 break;
2016 }
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static const char *sci_type(struct uart_port *port)
2020{
2021 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09002022 case PORT_IRDA:
2023 return "irda";
2024 case PORT_SCI:
2025 return "sci";
2026 case PORT_SCIF:
2027 return "scif";
2028 case PORT_SCIFA:
2029 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002030 case PORT_SCIFB:
2031 return "scifb";
Ulrich Hechtf303b362013-05-31 17:57:01 +02002032 case PORT_HSCIF:
2033 return "hscif";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Paul Mundtfa439722008-09-04 18:53:58 +09002036 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Paul Mundte2651642011-01-20 21:24:03 +09002039static inline unsigned long sci_port_size(struct uart_port *port)
2040{
2041 /*
2042 * Pick an arbitrary size that encapsulates all of the base
2043 * registers by default. This can be optimized later, or derived
2044 * from platform resource data at such a time that ports begin to
2045 * behave more erratically.
2046 */
Ulrich Hechtf303b362013-05-31 17:57:01 +02002047 if (port->type == PORT_HSCIF)
2048 return 96;
2049 else
2050 return 64;
Paul Mundte2651642011-01-20 21:24:03 +09002051}
2052
Paul Mundtf6e94952011-01-21 15:25:36 +09002053static int sci_remap_port(struct uart_port *port)
2054{
2055 unsigned long size = sci_port_size(port);
2056
2057 /*
2058 * Nothing to do if there's already an established membase.
2059 */
2060 if (port->membase)
2061 return 0;
2062
2063 if (port->flags & UPF_IOREMAP) {
2064 port->membase = ioremap_nocache(port->mapbase, size);
2065 if (unlikely(!port->membase)) {
2066 dev_err(port->dev, "can't remap port#%d\n", port->line);
2067 return -ENXIO;
2068 }
2069 } else {
2070 /*
2071 * For the simple (and majority of) cases where we don't
2072 * need to do any remapping, just cast the cookie
2073 * directly.
2074 */
2075 port->membase = (void __iomem *)port->mapbase;
2076 }
2077
2078 return 0;
2079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081static void sci_release_port(struct uart_port *port)
2082{
Paul Mundte2651642011-01-20 21:24:03 +09002083 if (port->flags & UPF_IOREMAP) {
2084 iounmap(port->membase);
2085 port->membase = NULL;
2086 }
2087
2088 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091static int sci_request_port(struct uart_port *port)
2092{
Paul Mundte2651642011-01-20 21:24:03 +09002093 unsigned long size = sci_port_size(port);
2094 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09002095 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Paul Mundt10205202011-01-21 16:00:31 +09002097 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09002098 if (unlikely(res == NULL))
2099 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Paul Mundtf6e94952011-01-21 15:25:36 +09002101 ret = sci_remap_port(port);
2102 if (unlikely(ret != 0)) {
2103 release_resource(res);
2104 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002105 }
Paul Mundte2651642011-01-20 21:24:03 +09002106
2107 return 0;
2108}
2109
2110static void sci_config_port(struct uart_port *port, int flags)
2111{
2112 if (flags & UART_CONFIG_TYPE) {
2113 struct sci_port *sport = to_sci_port(port);
2114
2115 port->type = sport->cfg->type;
2116 sci_request_port(port);
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (ser->baud_base < 2400)
2123 /* No paper tape reader for Mitch.. */
2124 return -EINVAL;
2125
2126 return 0;
2127}
2128
2129static struct uart_ops sci_uart_ops = {
2130 .tx_empty = sci_tx_empty,
2131 .set_mctrl = sci_set_mctrl,
2132 .get_mctrl = sci_get_mctrl,
2133 .start_tx = sci_start_tx,
2134 .stop_tx = sci_stop_tx,
2135 .stop_rx = sci_stop_rx,
2136 .enable_ms = sci_enable_ms,
2137 .break_ctl = sci_break_ctl,
2138 .startup = sci_startup,
2139 .shutdown = sci_shutdown,
2140 .set_termios = sci_set_termios,
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002141 .pm = sci_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 .type = sci_type,
2143 .release_port = sci_release_port,
2144 .request_port = sci_request_port,
2145 .config_port = sci_config_port,
2146 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002147#ifdef CONFIG_CONSOLE_POLL
2148 .poll_get_char = sci_poll_get_char,
2149 .poll_put_char = sci_poll_put_char,
2150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151};
2152
Bill Pemberton9671f092012-11-19 13:21:50 -05002153static int sci_init_single(struct platform_device *dev,
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002154 struct sci_port *sci_port, unsigned int index,
2155 struct plat_sci_port *p, bool early)
Paul Mundte108b2c2006-09-27 16:32:13 +09002156{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002157 struct uart_port *port = &sci_port->port;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002158 const struct resource *res;
2159 unsigned int i;
Paul Mundt3127c6b2011-06-28 13:44:37 +09002160 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002161
Paul Mundt50f09592011-12-02 20:09:48 +09002162 sci_port->cfg = p;
2163
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002164 port->ops = &sci_uart_ops;
2165 port->iotype = UPIO_MEM;
2166 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09002167
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002168 if (dev->num_resources) {
2169 /* Device has resources, use them. */
2170 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2171 if (res == NULL)
2172 return -ENOMEM;
2173
2174 port->mapbase = res->start;
2175
2176 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2177 sci_port->irqs[i] = platform_get_irq(dev, i);
2178
2179 /* The SCI generates several interrupts. They can be muxed
2180 * together or connected to different interrupt lines. In the
2181 * muxed case only one interrupt resource is specified. In the
2182 * non-muxed case three or four interrupt resources are
2183 * specified, as the BRI interrupt is optional.
2184 */
2185 if (sci_port->irqs[0] < 0)
2186 return -ENXIO;
2187
2188 if (sci_port->irqs[1] < 0) {
2189 sci_port->irqs[1] = sci_port->irqs[0];
2190 sci_port->irqs[2] = sci_port->irqs[0];
2191 sci_port->irqs[3] = sci_port->irqs[0];
2192 }
2193 } else {
2194 /* No resources, use old-style platform data. */
2195 port->mapbase = p->mapbase;
2196 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2197 sci_port->irqs[i] = p->irqs[i] ? p->irqs[i] : -ENXIO;
2198 }
2199
Markus Pietrek75136d42010-01-15 08:33:20 +09002200 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002201 case PORT_SCIFB:
2202 port->fifosize = 256;
2203 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002204 case PORT_HSCIF:
2205 port->fifosize = 128;
2206 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09002207 case PORT_SCIFA:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002208 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09002209 break;
2210 case PORT_SCIF:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002211 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09002212 break;
2213 default:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002214 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09002215 break;
2216 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002217
Paul Mundt3127c6b2011-06-28 13:44:37 +09002218 if (p->regtype == SCIx_PROBE_REGTYPE) {
2219 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02002220 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09002221 return ret;
2222 }
Paul Mundt61a69762011-06-14 12:40:19 +09002223
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002224 if (!early) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002225 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
2226 if (IS_ERR(sci_port->iclk)) {
2227 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
2228 if (IS_ERR(sci_port->iclk)) {
2229 dev_err(&dev->dev, "can't get iclk\n");
2230 return PTR_ERR(sci_port->iclk);
2231 }
2232 }
2233
2234 /*
2235 * The function clock is optional, ignore it if we can't
2236 * find it.
2237 */
2238 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
2239 if (IS_ERR(sci_port->fclk))
2240 sci_port->fclk = NULL;
2241
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002242 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002243
Paul Mundt50f09592011-12-02 20:09:48 +09002244 sci_init_gpios(sci_port);
2245
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002246 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002247 }
Paul Mundte108b2c2006-09-27 16:32:13 +09002248
Magnus Damm7ed7e072009-01-21 15:14:14 +00002249 sci_port->break_timer.data = (unsigned long)sci_port;
2250 sci_port->break_timer.function = sci_break_timer;
2251 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09002252
Paul Mundtdebf9502011-06-08 18:19:37 +09002253 /*
2254 * Establish some sensible defaults for the error detection.
2255 */
2256 if (!p->error_mask)
2257 p->error_mask = (p->type == PORT_SCI) ?
2258 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
2259
2260 /*
2261 * Establish sensible defaults for the overrun detection, unless
2262 * the part has explicitly disabled support for it.
2263 */
2264 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
2265 if (p->type == PORT_SCI)
2266 p->overrun_bit = 5;
2267 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
2268 p->overrun_bit = 9;
2269 else
2270 p->overrun_bit = 0;
2271
2272 /*
2273 * Make the error mask inclusive of overrun detection, if
2274 * supported.
2275 */
2276 p->error_mask |= (1 << p->overrun_bit);
2277 }
2278
Paul Mundtce6738b2011-01-19 15:24:40 +09002279 port->type = p->type;
Laurent Pinchartb6e4a3f2013-12-06 10:59:14 +01002280 port->flags = UPF_FIXED_PORT | p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09002281 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09002282
2283 /*
Paul Mundt61a69762011-06-14 12:40:19 +09002284 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09002285 * for the multi-IRQ ports, which is where we are primarily
2286 * concerned with the shutdown path synchronization.
2287 *
2288 * For the muxed case there's nothing more to do.
2289 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002290 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08002291 port->irqflags = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002292
Paul Mundt61a69762011-06-14 12:40:19 +09002293 port->serial_in = sci_serial_in;
2294 port->serial_out = sci_serial_out;
2295
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02002296 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2297 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2298 p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002299
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002300 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002301}
2302
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002303static void sci_cleanup_single(struct sci_port *port)
2304{
2305 sci_free_gpios(port);
2306
2307 clk_put(port->iclk);
2308 clk_put(port->fclk);
2309
2310 pm_runtime_disable(port->port.dev);
2311}
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002314static void serial_console_putchar(struct uart_port *port, int ch)
2315{
2316 sci_poll_put_char(port, ch);
2317}
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319/*
2320 * Print a string to the serial port trying not to disturb
2321 * any possible real use of the port...
2322 */
2323static void serial_console_write(struct console *co, const char *s,
2324 unsigned count)
2325{
Paul Mundt906b17d2011-01-21 16:19:53 +09002326 struct sci_port *sci_port = &sci_ports[co->index];
2327 struct uart_port *port = &sci_port->port;
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002328 unsigned short bits, ctrl;
2329 unsigned long flags;
2330 int locked = 1;
2331
2332 local_irq_save(flags);
2333 if (port->sysrq)
2334 locked = 0;
2335 else if (oops_in_progress)
2336 locked = spin_trylock(&port->lock);
2337 else
2338 spin_lock(&port->lock);
2339
2340 /* first save the SCSCR then disable the interrupts */
2341 ctrl = serial_port_in(port, SCSCR);
2342 serial_port_out(port, SCSCR, sci_port->cfg->scscr);
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002343
Magnus Damm501b8252009-01-21 15:14:30 +00002344 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002345
2346 /* wait until fifo is empty and last bit has been transmitted */
2347 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
Paul Mundtb12bb292012-03-30 19:50:15 +09002348 while ((serial_port_in(port, SCxSR) & bits) != bits)
Magnus Damm973e5d52009-02-24 15:57:12 +09002349 cpu_relax();
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002350
2351 /* restore the SCSCR */
2352 serial_port_out(port, SCSCR, ctrl);
2353
2354 if (locked)
2355 spin_unlock(&port->lock);
2356 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Bill Pemberton9671f092012-11-19 13:21:50 -05002359static int serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002361 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct uart_port *port;
2363 int baud = 115200;
2364 int bits = 8;
2365 int parity = 'n';
2366 int flow = 'n';
2367 int ret;
2368
Paul Mundte108b2c2006-09-27 16:32:13 +09002369 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002370 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002371 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002372 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002373 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002374
Paul Mundt906b17d2011-01-21 16:19:53 +09002375 sci_port = &sci_ports[co->index];
2376 port = &sci_port->port;
2377
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002378 /*
2379 * Refuse to handle uninitialized ports.
2380 */
2381 if (!port->ops)
2382 return -ENODEV;
2383
Paul Mundtf6e94952011-01-21 15:25:36 +09002384 ret = sci_remap_port(port);
2385 if (unlikely(ret != 0))
2386 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (options)
2389 uart_parse_options(options, &baud, &parity, &bits, &flow);
2390
Paul Mundtab7cfb52011-06-01 14:47:42 +09002391 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394static struct console serial_console = {
2395 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002396 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 .write = serial_console_write,
2398 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002399 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002401 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402};
2403
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002404static struct console early_serial_console = {
2405 .name = "early_ttySC",
2406 .write = serial_console_write,
2407 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002408 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002409};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002410
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002411static char early_serial_buf[32];
2412
Bill Pemberton9671f092012-11-19 13:21:50 -05002413static int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002414{
Jingoo Han574de552013-07-30 17:06:57 +09002415 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002416
2417 if (early_serial_console.data)
2418 return -EEXIST;
2419
2420 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002421
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002422 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002423
2424 serial_console_setup(&early_serial_console, early_serial_buf);
2425
2426 if (!strstr(early_serial_buf, "keep"))
2427 early_serial_console.flags |= CON_BOOT;
2428
2429 register_console(&early_serial_console);
2430 return 0;
2431}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002432
2433#define SCI_CONSOLE (&serial_console)
2434
Paul Mundtecdf8a42011-01-21 00:05:48 +09002435#else
Bill Pemberton9671f092012-11-19 13:21:50 -05002436static inline int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002437{
2438 return -EINVAL;
2439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002441#define SCI_CONSOLE NULL
2442
2443#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445static char banner[] __initdata =
Ulrich Hechtf303b362013-05-31 17:57:01 +02002446 KERN_INFO "SuperH (H)SCI(F) driver initialized\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448static struct uart_driver sci_uart_driver = {
2449 .owner = THIS_MODULE,
2450 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 .dev_name = "ttySC",
2452 .major = SCI_MAJOR,
2453 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002454 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .cons = SCI_CONSOLE,
2456};
2457
Paul Mundt54507f62009-05-08 23:48:33 +09002458static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002459{
Paul Mundtd535a232011-01-19 17:19:35 +09002460 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002461
Paul Mundtd535a232011-01-19 17:19:35 +09002462 cpufreq_unregister_notifier(&port->freq_transition,
2463 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002464
Paul Mundtd535a232011-01-19 17:19:35 +09002465 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002466
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002467 sci_cleanup_single(port);
Paul Mundtd535a232011-01-19 17:19:35 +09002468
Magnus Damme552de22009-01-21 15:13:42 +00002469 return 0;
2470}
2471
Bill Pemberton9671f092012-11-19 13:21:50 -05002472static int sci_probe_single(struct platform_device *dev,
Magnus Damm0ee70712009-01-21 15:13:50 +00002473 unsigned int index,
2474 struct plat_sci_port *p,
2475 struct sci_port *sciport)
2476{
Magnus Damm0ee70712009-01-21 15:13:50 +00002477 int ret;
2478
2479 /* Sanity check */
2480 if (unlikely(index >= SCI_NPORTS)) {
2481 dev_notice(&dev->dev, "Attempting to register port "
2482 "%d when only %d are available.\n",
2483 index+1, SCI_NPORTS);
2484 dev_notice(&dev->dev, "Consider bumping "
2485 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
Laurent Pinchartb6c5ef62012-06-13 00:28:24 +02002486 return -EINVAL;
Magnus Damm0ee70712009-01-21 15:13:50 +00002487 }
2488
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002489 ret = sci_init_single(dev, sciport, index, p, false);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002490 if (ret)
2491 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002492
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002493 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
2494 if (ret) {
2495 sci_cleanup_single(sciport);
2496 return ret;
2497 }
2498
2499 return 0;
Magnus Damm0ee70712009-01-21 15:13:50 +00002500}
2501
Bill Pemberton9671f092012-11-19 13:21:50 -05002502static int sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Jingoo Han3ba35ba2013-08-01 18:17:45 +09002504 struct plat_sci_port *p = dev_get_platdata(&dev->dev);
Paul Mundtd535a232011-01-19 17:19:35 +09002505 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002506 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002507
Paul Mundtecdf8a42011-01-21 00:05:48 +09002508 /*
2509 * If we've come here via earlyprintk initialization, head off to
2510 * the special early probe. We don't have sufficient device state
2511 * to make it beyond this yet.
2512 */
2513 if (is_early_platform_device(dev))
2514 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002515
Paul Mundtd535a232011-01-19 17:19:35 +09002516 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002517
Paul Mundt906b17d2011-01-21 16:19:53 +09002518 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002519 if (ret)
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002520 return ret;
Magnus Damme552de22009-01-21 15:13:42 +00002521
Paul Mundtd535a232011-01-19 17:19:35 +09002522 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Paul Mundtd535a232011-01-19 17:19:35 +09002524 ret = cpufreq_register_notifier(&sp->freq_transition,
2525 CPUFREQ_TRANSITION_NOTIFIER);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002526 if (unlikely(ret < 0)) {
2527 sci_cleanup_single(sp);
2528 return ret;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531#ifdef CONFIG_SH_STANDARD_BIOS
2532 sh_bios_gdb_detach();
2533#endif
2534
Paul Mundte108b2c2006-09-27 16:32:13 +09002535 return 0;
2536}
2537
Paul Mundt6daa79b2009-06-15 07:07:38 +09002538static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002539{
Paul Mundtd535a232011-01-19 17:19:35 +09002540 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002541
Paul Mundtd535a232011-01-19 17:19:35 +09002542 if (sport)
2543 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002544
2545 return 0;
2546}
2547
Paul Mundt6daa79b2009-06-15 07:07:38 +09002548static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002549{
Paul Mundtd535a232011-01-19 17:19:35 +09002550 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002551
Paul Mundtd535a232011-01-19 17:19:35 +09002552 if (sport)
2553 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002554
2555 return 0;
2556}
2557
Alexey Dobriyan47145212009-12-14 18:00:08 -08002558static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002559 .suspend = sci_suspend,
2560 .resume = sci_resume,
2561};
2562
Paul Mundte108b2c2006-09-27 16:32:13 +09002563static struct platform_driver sci_driver = {
2564 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002565 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002566 .driver = {
2567 .name = "sh-sci",
2568 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002569 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002570 },
2571};
2572
2573static int __init sci_init(void)
2574{
2575 int ret;
2576
2577 printk(banner);
2578
Paul Mundte108b2c2006-09-27 16:32:13 +09002579 ret = uart_register_driver(&sci_uart_driver);
2580 if (likely(ret == 0)) {
2581 ret = platform_driver_register(&sci_driver);
2582 if (unlikely(ret))
2583 uart_unregister_driver(&sci_uart_driver);
2584 }
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return ret;
2587}
2588
2589static void __exit sci_exit(void)
2590{
Paul Mundte108b2c2006-09-27 16:32:13 +09002591 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 uart_unregister_driver(&sci_uart_driver);
2593}
2594
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002595#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2596early_platform_init_buffer("earlyprintk", &sci_driver,
2597 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599module_init(sci_init);
2600module_exit(sci_exit);
2601
Paul Mundte108b2c2006-09-27 16:32:13 +09002602MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002603MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09002604MODULE_AUTHOR("Paul Mundt");
Ulrich Hechtf303b362013-05-31 17:57:01 +02002605MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");