blob: ca1a4c385d400e32e9435198aaab62b901edbb58 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090041#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090042#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/notifier.h>
Magnus Damm5e50d2d2011-04-19 10:38:25 +000044#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090046#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090047#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090048#include <linux/err.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090049#include <linux/dmaengine.h>
50#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090052
53#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundtce6738b2011-01-19 15:24:40 +090062 /* Platform configuration */
63 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090064
Paul Mundte108b2c2006-09-27 16:32:13 +090065 /* Break timer */
66 struct timer_list break_timer;
67 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090068
Magnus Damm501b8252009-01-21 15:14:30 +000069 /* Interface clock */
70 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090071 /* Function clock */
72 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090073
Paul Mundt9174fc82011-06-28 15:25:36 +090074 char *irqstr[SCIx_NR_IRQS];
75
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090076 struct dma_chan *chan_tx;
77 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090078
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090079#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090080 struct dma_async_tx_descriptor *desc_tx;
81 struct dma_async_tx_descriptor *desc_rx[2];
82 dma_cookie_t cookie_tx;
83 dma_cookie_t cookie_rx[2];
84 dma_cookie_t active_rx;
85 struct scatterlist sg_tx;
86 unsigned int sg_len_tx;
87 struct scatterlist sg_rx[2];
88 size_t buf_len_rx;
89 struct sh_dmae_slave param_tx;
90 struct sh_dmae_slave param_rx;
91 struct work_struct work_tx;
92 struct work_struct work_rx;
93 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +000094 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090095#endif
Magnus Damme552de22009-01-21 15:13:42 +000096
Paul Mundtd535a232011-01-19 17:19:35 +090097 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +090098};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900101static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100102static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900103static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Paul Mundte108b2c2006-09-27 16:32:13 +0900105#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
106
107static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct uart_driver sci_uart_driver;
109
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900110static inline struct sci_port *
111to_sci_port(struct uart_port *uart)
112{
113 return container_of(uart, struct sci_port, port);
114}
115
Paul Mundt61a69762011-06-14 12:40:19 +0900116struct plat_sci_reg {
117 u8 offset, size;
118};
119
120/* Helper for invalidating specific entries of an inherited map. */
121#define sci_reg_invalid { .offset = 0, .size = 0 }
122
123static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
124 [SCIx_PROBE_REGTYPE] = {
125 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
126 },
127
128 /*
129 * Common SCI definitions, dependent on the port's regshift
130 * value.
131 */
132 [SCIx_SCI_REGTYPE] = {
133 [SCSMR] = { 0x00, 8 },
134 [SCBRR] = { 0x01, 8 },
135 [SCSCR] = { 0x02, 8 },
136 [SCxTDR] = { 0x03, 8 },
137 [SCxSR] = { 0x04, 8 },
138 [SCxRDR] = { 0x05, 8 },
139 [SCFCR] = sci_reg_invalid,
140 [SCFDR] = sci_reg_invalid,
141 [SCTFDR] = sci_reg_invalid,
142 [SCRFDR] = sci_reg_invalid,
143 [SCSPTR] = sci_reg_invalid,
144 [SCLSR] = sci_reg_invalid,
145 },
146
147 /*
148 * Common definitions for legacy IrDA ports, dependent on
149 * regshift value.
150 */
151 [SCIx_IRDA_REGTYPE] = {
152 [SCSMR] = { 0x00, 8 },
153 [SCBRR] = { 0x01, 8 },
154 [SCSCR] = { 0x02, 8 },
155 [SCxTDR] = { 0x03, 8 },
156 [SCxSR] = { 0x04, 8 },
157 [SCxRDR] = { 0x05, 8 },
158 [SCFCR] = { 0x06, 8 },
159 [SCFDR] = { 0x07, 16 },
160 [SCTFDR] = sci_reg_invalid,
161 [SCRFDR] = sci_reg_invalid,
162 [SCSPTR] = sci_reg_invalid,
163 [SCLSR] = sci_reg_invalid,
164 },
165
166 /*
167 * Common SCIFA definitions.
168 */
169 [SCIx_SCIFA_REGTYPE] = {
170 [SCSMR] = { 0x00, 16 },
171 [SCBRR] = { 0x04, 8 },
172 [SCSCR] = { 0x08, 16 },
173 [SCxTDR] = { 0x20, 8 },
174 [SCxSR] = { 0x14, 16 },
175 [SCxRDR] = { 0x24, 8 },
176 [SCFCR] = { 0x18, 16 },
177 [SCFDR] = { 0x1c, 16 },
178 [SCTFDR] = sci_reg_invalid,
179 [SCRFDR] = sci_reg_invalid,
180 [SCSPTR] = sci_reg_invalid,
181 [SCLSR] = sci_reg_invalid,
182 },
183
184 /*
185 * Common SCIFB definitions.
186 */
187 [SCIx_SCIFB_REGTYPE] = {
188 [SCSMR] = { 0x00, 16 },
189 [SCBRR] = { 0x04, 8 },
190 [SCSCR] = { 0x08, 16 },
191 [SCxTDR] = { 0x40, 8 },
192 [SCxSR] = { 0x14, 16 },
193 [SCxRDR] = { 0x60, 8 },
194 [SCFCR] = { 0x18, 16 },
195 [SCFDR] = { 0x1c, 16 },
196 [SCTFDR] = sci_reg_invalid,
197 [SCRFDR] = sci_reg_invalid,
198 [SCSPTR] = sci_reg_invalid,
199 [SCLSR] = sci_reg_invalid,
200 },
201
202 /*
203 * Common SH-3 SCIF definitions.
204 */
205 [SCIx_SH3_SCIF_REGTYPE] = {
206 [SCSMR] = { 0x00, 8 },
207 [SCBRR] = { 0x02, 8 },
208 [SCSCR] = { 0x04, 8 },
209 [SCxTDR] = { 0x06, 8 },
210 [SCxSR] = { 0x08, 16 },
211 [SCxRDR] = { 0x0a, 8 },
212 [SCFCR] = { 0x0c, 8 },
213 [SCFDR] = { 0x0e, 16 },
214 [SCTFDR] = sci_reg_invalid,
215 [SCRFDR] = sci_reg_invalid,
216 [SCSPTR] = sci_reg_invalid,
217 [SCLSR] = sci_reg_invalid,
218 },
219
220 /*
221 * Common SH-4(A) SCIF(B) definitions.
222 */
223 [SCIx_SH4_SCIF_REGTYPE] = {
224 [SCSMR] = { 0x00, 16 },
225 [SCBRR] = { 0x04, 8 },
226 [SCSCR] = { 0x08, 16 },
227 [SCxTDR] = { 0x0c, 8 },
228 [SCxSR] = { 0x10, 16 },
229 [SCxRDR] = { 0x14, 8 },
230 [SCFCR] = { 0x18, 16 },
231 [SCFDR] = { 0x1c, 16 },
232 [SCTFDR] = sci_reg_invalid,
233 [SCRFDR] = sci_reg_invalid,
234 [SCSPTR] = { 0x20, 16 },
235 [SCLSR] = { 0x24, 16 },
236 },
237
238 /*
239 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
240 * register.
241 */
242 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
243 [SCSMR] = { 0x00, 16 },
244 [SCBRR] = { 0x04, 8 },
245 [SCSCR] = { 0x08, 16 },
246 [SCxTDR] = { 0x0c, 8 },
247 [SCxSR] = { 0x10, 16 },
248 [SCxRDR] = { 0x14, 8 },
249 [SCFCR] = { 0x18, 16 },
250 [SCFDR] = { 0x1c, 16 },
251 [SCTFDR] = sci_reg_invalid,
252 [SCRFDR] = sci_reg_invalid,
253 [SCSPTR] = sci_reg_invalid,
254 [SCLSR] = { 0x24, 16 },
255 },
256
257 /*
258 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
259 * count registers.
260 */
261 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
262 [SCSMR] = { 0x00, 16 },
263 [SCBRR] = { 0x04, 8 },
264 [SCSCR] = { 0x08, 16 },
265 [SCxTDR] = { 0x0c, 8 },
266 [SCxSR] = { 0x10, 16 },
267 [SCxRDR] = { 0x14, 8 },
268 [SCFCR] = { 0x18, 16 },
269 [SCFDR] = { 0x1c, 16 },
270 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
271 [SCRFDR] = { 0x20, 16 },
272 [SCSPTR] = { 0x24, 16 },
273 [SCLSR] = { 0x28, 16 },
274 },
275
276 /*
277 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
278 * registers.
279 */
280 [SCIx_SH7705_SCIF_REGTYPE] = {
281 [SCSMR] = { 0x00, 16 },
282 [SCBRR] = { 0x04, 8 },
283 [SCSCR] = { 0x08, 16 },
284 [SCxTDR] = { 0x20, 8 },
285 [SCxSR] = { 0x14, 16 },
286 [SCxRDR] = { 0x24, 8 },
287 [SCFCR] = { 0x18, 16 },
288 [SCFDR] = { 0x1c, 16 },
289 [SCTFDR] = sci_reg_invalid,
290 [SCRFDR] = sci_reg_invalid,
291 [SCSPTR] = sci_reg_invalid,
292 [SCLSR] = sci_reg_invalid,
293 },
294};
295
Paul Mundt72b294c2011-06-14 17:38:19 +0900296#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
297
Paul Mundt61a69762011-06-14 12:40:19 +0900298/*
299 * The "offset" here is rather misleading, in that it refers to an enum
300 * value relative to the port mapping rather than the fixed offset
301 * itself, which needs to be manually retrieved from the platform's
302 * register map for the given port.
303 */
304static unsigned int sci_serial_in(struct uart_port *p, int offset)
305{
Paul Mundt72b294c2011-06-14 17:38:19 +0900306 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900307
308 if (reg->size == 8)
309 return ioread8(p->membase + (reg->offset << p->regshift));
310 else if (reg->size == 16)
311 return ioread16(p->membase + (reg->offset << p->regshift));
312 else
313 WARN(1, "Invalid register access\n");
314
315 return 0;
316}
317
318static void sci_serial_out(struct uart_port *p, int offset, int value)
319{
Paul Mundt72b294c2011-06-14 17:38:19 +0900320 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900321
322 if (reg->size == 8)
323 iowrite8(value, p->membase + (reg->offset << p->regshift));
324 else if (reg->size == 16)
325 iowrite16(value, p->membase + (reg->offset << p->regshift));
326 else
327 WARN(1, "Invalid register access\n");
328}
329
330#define sci_in(up, offset) (up->serial_in(up, offset))
331#define sci_out(up, offset, value) (up->serial_out(up, offset, value))
332
333static int sci_probe_regmap(struct plat_sci_port *cfg)
334{
335 switch (cfg->type) {
336 case PORT_SCI:
337 cfg->regtype = SCIx_SCI_REGTYPE;
338 break;
339 case PORT_IRDA:
340 cfg->regtype = SCIx_IRDA_REGTYPE;
341 break;
342 case PORT_SCIFA:
343 cfg->regtype = SCIx_SCIFA_REGTYPE;
344 break;
345 case PORT_SCIFB:
346 cfg->regtype = SCIx_SCIFB_REGTYPE;
347 break;
348 case PORT_SCIF:
349 /*
350 * The SH-4 is a bit of a misnomer here, although that's
351 * where this particular port layout originated. This
352 * configuration (or some slight variation thereof)
353 * remains the dominant model for all SCIFs.
354 */
355 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
356 break;
357 default:
358 printk(KERN_ERR "Can't probe register map for given port\n");
359 return -EINVAL;
360 }
361
362 return 0;
363}
364
Paul Mundt23241d42011-06-28 13:55:31 +0900365static void sci_port_enable(struct sci_port *sci_port)
366{
367 if (!sci_port->port.dev)
368 return;
369
370 pm_runtime_get_sync(sci_port->port.dev);
371
372 clk_enable(sci_port->iclk);
373 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
374 clk_enable(sci_port->fclk);
375}
376
377static void sci_port_disable(struct sci_port *sci_port)
378{
379 if (!sci_port->port.dev)
380 return;
381
382 clk_disable(sci_port->fclk);
383 clk_disable(sci_port->iclk);
384
385 pm_runtime_put_sync(sci_port->port.dev);
386}
387
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900388#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900389
390#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900391static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned short status;
394 int c;
395
Paul Mundte108b2c2006-09-27 16:32:13 +0900396 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 status = sci_in(port, SCxSR);
398 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900399 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 continue;
401 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500402 break;
403 } while (1);
404
405 if (!(status & SCxSR_RDxF(port)))
406 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900409
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900410 /* Dummy read */
411 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return c;
415}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900416#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900418static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned short status;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 do {
423 status = sci_in(port, SCxSR);
424 } while (!(status & SCxSR_TDxE(port)));
425
Paul Mundt272966c2008-11-13 17:46:06 +0900426 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000427 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900429#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Paul Mundt61a69762011-06-14 12:40:19 +0900431static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900432{
Paul Mundt61a69762011-06-14 12:40:19 +0900433 struct sci_port *s = to_sci_port(port);
434 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900435
Paul Mundt61a69762011-06-14 12:40:19 +0900436 /*
437 * Use port-specific handler if provided.
438 */
439 if (s->cfg->ops && s->cfg->ops->init_pins) {
440 s->cfg->ops->init_pins(port, cflag);
441 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Paul Mundt61a69762011-06-14 12:40:19 +0900444 /*
445 * For the generic path SCSPTR is necessary. Bail out if that's
446 * unavailable, too.
447 */
448 if (!reg->size)
449 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800450
Paul Mundtd5701642008-12-16 20:07:27 +0900451 if (!(cflag & CRTSCTS))
Paul Mundt61a69762011-06-14 12:40:19 +0900452 sci_out(port, SCSPTR, 0x0080); /* Set RTS = 1 */
Paul Mundtd5701642008-12-16 20:07:27 +0900453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900455static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900456{
Paul Mundt72b294c2011-06-14 17:38:19 +0900457 struct plat_sci_reg *reg;
458
459 reg = sci_getreg(port, SCTFDR);
460 if (reg->size)
461 return sci_in(port, SCTFDR) & 0xff;
462
463 reg = sci_getreg(port, SCFDR);
464 if (reg->size)
465 return sci_in(port, SCFDR) >> 8;
466
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900467 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900468}
469
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900470static int sci_txroom(struct uart_port *port)
471{
Paul Mundt72b294c2011-06-14 17:38:19 +0900472 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900473}
474
475static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900476{
Paul Mundt72b294c2011-06-14 17:38:19 +0900477 struct plat_sci_reg *reg;
478
479 reg = sci_getreg(port, SCRFDR);
480 if (reg->size)
481 return sci_in(port, SCRFDR) & 0xff;
482
483 reg = sci_getreg(port, SCFDR);
484 if (reg->size)
485 return sci_in(port, SCFDR) & ((port->fifosize << 1) - 1);
486
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900487 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900488}
489
Paul Mundt514820e2011-06-08 18:51:32 +0900490/*
491 * SCI helper for checking the state of the muxed port/RXD pins.
492 */
493static inline int sci_rxd_in(struct uart_port *port)
494{
495 struct sci_port *s = to_sci_port(port);
496
497 if (s->cfg->port_reg <= 0)
498 return 1;
499
500 return !!__raw_readb(s->cfg->port_reg);
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* ********************************************************************** *
504 * the interrupt related routines *
505 * ********************************************************************** */
506
507static void sci_transmit_chars(struct uart_port *port)
508{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700509 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned short status;
512 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900513 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 status = sci_in(port, SCxSR);
516 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900518 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900519 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900520 else
Paul Mundt8e698612009-06-24 19:44:32 +0900521 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return;
524 }
525
Paul Mundt72b294c2011-06-14 17:38:19 +0900526 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 do {
529 unsigned char c;
530
531 if (port->x_char) {
532 c = port->x_char;
533 port->x_char = 0;
534 } else if (!uart_circ_empty(xmit) && !stopped) {
535 c = xmit->buf[xmit->tail];
536 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
537 } else {
538 break;
539 }
540
541 sci_out(port, SCxTDR, c);
542
543 port->icount.tx++;
544 } while (--count > 0);
545
546 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
547
548 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
549 uart_write_wakeup(port);
550 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100551 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ctrl = sci_in(port, SCSCR);
554
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900555 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 sci_in(port, SCxSR); /* Dummy read */
557 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Paul Mundt8e698612009-06-24 19:44:32 +0900560 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563}
564
565/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900566#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900568static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900570 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700571 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int i, count, copied = 0;
573 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800574 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 status = sci_in(port, SCxSR);
577 if (!(status & SCxSR_RDxF(port)))
578 return;
579
580 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Don't copy more bytes than there is room for in the buffer */
Paul Mundt72b294c2011-06-14 17:38:19 +0900582 count = tty_buffer_request_room(tty, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* If for any reason we can't copy more data, we're done! */
585 if (count == 0)
586 break;
587
588 if (port->type == PORT_SCI) {
589 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900590 if (uart_handle_sysrq_char(port, c) ||
591 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900593 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900594 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900596 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 char c = sci_in(port, SCxRDR);
598 status = sci_in(port, SCxSR);
599#if defined(CONFIG_CPU_SH3)
600 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900601 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if ((c == 0) &&
603 (status & SCxSR_FER(port))) {
604 count--; i--;
605 continue;
606 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900609 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900610 sci_port->break_flag = 0;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (STEPFN(c)) {
613 count--; i--;
614 continue;
615 }
616 }
617#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100618 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 count--; i--;
620 continue;
621 }
622
623 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900624 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800625 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900626 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900627 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800628 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900629 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800630 } else
631 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900632
Alan Cox33f0f882006-01-09 20:54:13 -0800633 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 }
636
637 sci_in(port, SCxSR); /* dummy read */
638 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 copied += count;
641 port->icount.rx += count;
642 }
643
644 if (copied) {
645 /* Tell the rest of the system the news. New characters! */
646 tty_flip_buffer_push(tty);
647 } else {
648 sci_in(port, SCxSR); /* dummy read */
649 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
650 }
651}
652
653#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900654
655/*
656 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * 1 per millisecond or so during the break period, for 9600 baud.
658 * So dont bother disabling interrupts.
659 * But dont want more than 1 break event.
660 * Use a kernel timer to periodically poll the rx line until
661 * the break is finished.
662 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900663static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900665 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/* Ensure that two consecutive samples find the break over. */
669static void sci_break_timer(unsigned long data)
670{
Paul Mundte108b2c2006-09-27 16:32:13 +0900671 struct sci_port *port = (struct sci_port *)data;
672
Paul Mundt23241d42011-06-28 13:55:31 +0900673 sci_port_enable(port);
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000674
Paul Mundte108b2c2006-09-27 16:32:13 +0900675 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900677 sci_schedule_break_timer(port);
678 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* break is over. */
680 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900681 sci_schedule_break_timer(port);
682 } else
683 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000684
Paul Mundt23241d42011-06-28 13:55:31 +0900685 sci_port_disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900688static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 int copied = 0;
691 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700692 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900693 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Paul Mundtdebf9502011-06-08 18:19:37 +0900695 /*
696 * Handle overruns, if supported.
697 */
698 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
699 if (status & (1 << s->cfg->overrun_bit)) {
700 /* overrun error */
701 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
702 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900703
Paul Mundtdebf9502011-06-08 18:19:37 +0900704 dev_notice(port->dev, "overrun error");
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Paul Mundte108b2c2006-09-27 16:32:13 +0900708 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (sci_rxd_in(port) == 0) {
710 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900711 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900712
713 if (!sci_port->break_flag) {
714 sci_port->break_flag = 1;
715 sci_schedule_break_timer(sci_port);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900718 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900720
721 dev_dbg(port->dev, "BREAK detected\n");
722
Paul Mundte108b2c2006-09-27 16:32:13 +0900723 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900724 copied++;
725 }
726
Paul Mundte108b2c2006-09-27 16:32:13 +0900727 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900729 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800730 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900731
732 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735
Paul Mundte108b2c2006-09-27 16:32:13 +0900736 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900738 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
739 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900740
741 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Alan Cox33f0f882006-01-09 20:54:13 -0800744 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 return copied;
748}
749
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900750static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900751{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700752 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900753 struct sci_port *s = to_sci_port(port);
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900754 struct plat_sci_reg *reg;
Paul Mundtd830fa42008-12-16 19:29:38 +0900755 int copied = 0;
756
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900757 reg = sci_getreg(port, SCLSR);
758 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +0900759 return 0;
760
Paul Mundtdebf9502011-06-08 18:19:37 +0900761 if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
Paul Mundtd830fa42008-12-16 19:29:38 +0900762 sci_out(port, SCLSR, 0);
763
764 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
765 tty_flip_buffer_push(tty);
766
767 dev_notice(port->dev, "overrun error\n");
768 copied++;
769 }
770
771 return copied;
772}
773
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900774static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 int copied = 0;
777 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700778 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000779 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900781 if (uart_handle_break(port))
782 return 0;
783
Paul Mundtb7a76e42006-02-01 03:06:06 -0800784 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#if defined(CONFIG_CPU_SH3)
786 /* Debounce break */
787 s->break_flag = 1;
788#endif
789 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900790 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800791 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900792
793 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Alan Cox33f0f882006-01-09 20:54:13 -0800796 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900798
Paul Mundtd830fa42008-12-16 19:29:38 +0900799 copied += sci_handle_fifo_overrun(port);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return copied;
802}
803
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900804static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900806#ifdef CONFIG_SERIAL_SH_SCI_DMA
807 struct uart_port *port = ptr;
808 struct sci_port *s = to_sci_port(port);
809
810 if (s->chan_rx) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900811 u16 scr = sci_in(port, SCSCR);
812 u16 ssr = sci_in(port, SCxSR);
813
814 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000815 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000816 disable_irq_nosync(irq);
817 scr |= 0x4000;
818 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900819 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000820 }
821 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900822 /* Clear current interrupt */
823 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000824 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
825 jiffies, s->rx_timeout);
826 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900827
828 return IRQ_HANDLED;
829 }
830#endif
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* I think sci_receive_chars has to be called irrespective
833 * of whether the I_IXOFF is set, otherwise, how is the interrupt
834 * to be disabled?
835 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900836 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 return IRQ_HANDLED;
839}
840
David Howells7d12e782006-10-05 14:55:46 +0100841static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900844 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Stuart Menefyfd78a762009-07-29 23:01:24 +0900846 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900848 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 return IRQ_HANDLED;
851}
852
David Howells7d12e782006-10-05 14:55:46 +0100853static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct uart_port *port = ptr;
856
857 /* Handle errors */
858 if (port->type == PORT_SCI) {
859 if (sci_handle_errors(port)) {
860 /* discard character in rx buffer */
861 sci_in(port, SCxSR);
862 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
863 }
864 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900865 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100866 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
870
871 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100872 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 return IRQ_HANDLED;
875}
876
David Howells7d12e782006-10-05 14:55:46 +0100877static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct uart_port *port = ptr;
880
881 /* Handle BREAKs */
882 sci_handle_breaks(port);
883 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
884
885 return IRQ_HANDLED;
886}
887
Paul Mundtf43dc232011-01-13 15:06:28 +0900888static inline unsigned long port_rx_irq_mask(struct uart_port *port)
889{
890 /*
891 * Not all ports (such as SCIFA) will support REIE. Rather than
892 * special-casing the port type, we check the port initialization
893 * IRQ enable mask to see whether the IRQ is desired at all. If
894 * it's unset, it's logically inferred that there's no point in
895 * testing for it.
896 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900897 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900898}
899
David Howells7d12e782006-10-05 14:55:46 +0100900static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Magnus Damm44e18e92009-07-03 08:39:34 +0000902 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900903 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900904 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900905 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900907 ssr_status = sci_in(port, SCxSR);
908 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900909 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900912 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900913 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900914 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900915
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900916 /*
917 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
918 * DR flags
919 */
920 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900921 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900922 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000925 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900926 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000929 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900930 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Michael Trimarchia8884e32008-10-31 16:10:23 +0900932 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300936 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * ports' baud rate when the peripheral clock changes.
938 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900939static int sci_notifier(struct notifier_block *self,
940 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Magnus Damme552de22009-01-21 15:13:42 +0000942 struct sci_port *sci_port;
943 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Paul Mundtd535a232011-01-19 17:19:35 +0900945 sci_port = container_of(self, struct sci_port, freq_transition);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000948 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900949 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900950
Paul Mundtd535a232011-01-19 17:19:35 +0900951 spin_lock_irqsave(&port->lock, flags);
952 port->uartclk = clk_get_rate(sci_port->iclk);
953 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return NOTIFY_OK;
957}
Magnus Damm501b8252009-01-21 15:14:30 +0000958
Paul Mundt9174fc82011-06-28 15:25:36 +0900959static struct sci_irq_desc {
960 const char *desc;
961 irq_handler_t handler;
962} sci_irq_desc[] = {
963 /*
964 * Split out handlers, the default case.
965 */
966 [SCIx_ERI_IRQ] = {
967 .desc = "rx err",
968 .handler = sci_er_interrupt,
969 },
970
971 [SCIx_RXI_IRQ] = {
972 .desc = "rx full",
973 .handler = sci_rx_interrupt,
974 },
975
976 [SCIx_TXI_IRQ] = {
977 .desc = "tx empty",
978 .handler = sci_tx_interrupt,
979 },
980
981 [SCIx_BRI_IRQ] = {
982 .desc = "break",
983 .handler = sci_br_interrupt,
984 },
985
986 /*
987 * Special muxed handler.
988 */
989 [SCIx_MUX_IRQ] = {
990 .desc = "mux",
991 .handler = sci_mpxed_interrupt,
992 },
993};
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static int sci_request_irq(struct sci_port *port)
996{
Paul Mundt9174fc82011-06-28 15:25:36 +0900997 struct uart_port *up = &port->port;
998 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Paul Mundt9174fc82011-06-28 15:25:36 +09001000 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
1001 struct sci_irq_desc *desc;
1002 unsigned int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001003
Paul Mundt9174fc82011-06-28 15:25:36 +09001004 if (SCIx_IRQ_IS_MUXED(port)) {
1005 i = SCIx_MUX_IRQ;
1006 irq = up->irq;
1007 } else
1008 irq = port->cfg->irqs[i];
1009
1010 desc = sci_irq_desc + i;
1011 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1012 dev_name(up->dev), desc->desc);
1013 if (!port->irqstr[j]) {
1014 dev_err(up->dev, "Failed to allocate %s IRQ string\n",
1015 desc->desc);
1016 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Paul Mundt762c69e2008-12-16 18:55:26 +09001018
Paul Mundt9174fc82011-06-28 15:25:36 +09001019 ret = request_irq(irq, desc->handler, up->irqflags,
1020 port->irqstr[j], port);
1021 if (unlikely(ret)) {
1022 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1023 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
1026
1027 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001028
1029out_noirq:
1030 while (--i >= 0)
1031 free_irq(port->cfg->irqs[i], port);
1032
1033out_nomem:
1034 while (--j >= 0)
1035 kfree(port->irqstr[j]);
1036
1037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static void sci_free_irq(struct sci_port *port)
1041{
1042 int i;
1043
Paul Mundt9174fc82011-06-28 15:25:36 +09001044 /*
1045 * Intentionally in reverse order so we iterate over the muxed
1046 * IRQ first.
1047 */
1048 for (i = 0; i < SCIx_NR_IRQS; i++) {
1049 free_irq(port->cfg->irqs[i], port);
1050 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Paul Mundt9174fc82011-06-28 15:25:36 +09001052 if (SCIx_IRQ_IS_MUXED(port)) {
1053 /* If there's only one IRQ, we're done. */
1054 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 }
1057}
1058
1059static unsigned int sci_tx_empty(struct uart_port *port)
1060{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +00001061 unsigned short status = sci_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001062 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001063
1064 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1068{
1069 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
1070 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
1071 /* If you have signals for DTR and DCD, please implement here. */
1072}
1073
1074static unsigned int sci_get_mctrl(struct uart_port *port)
1075{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001076 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 and CTS/RTS */
1078
1079 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
1080}
1081
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001082#ifdef CONFIG_SERIAL_SH_SCI_DMA
1083static void sci_dma_tx_complete(void *arg)
1084{
1085 struct sci_port *s = arg;
1086 struct uart_port *port = &s->port;
1087 struct circ_buf *xmit = &port->state->xmit;
1088 unsigned long flags;
1089
1090 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1091
1092 spin_lock_irqsave(&port->lock, flags);
1093
Magnus Dammf354a3812010-03-19 04:47:01 +00001094 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001095 xmit->tail &= UART_XMIT_SIZE - 1;
1096
Magnus Dammf354a3812010-03-19 04:47:01 +00001097 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001098
1099 async_tx_ack(s->desc_tx);
1100 s->cookie_tx = -EINVAL;
1101 s->desc_tx = NULL;
1102
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001103 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1104 uart_write_wakeup(port);
1105
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001106 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001107 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001108 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001109 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001110 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001111 }
1112
1113 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001114}
1115
1116/* Locking: called with port lock held */
1117static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
1118 size_t count)
1119{
1120 struct uart_port *port = &s->port;
1121 int i, active, room;
1122
1123 room = tty_buffer_request_room(tty, count);
1124
1125 if (s->active_rx == s->cookie_rx[0]) {
1126 active = 0;
1127 } else if (s->active_rx == s->cookie_rx[1]) {
1128 active = 1;
1129 } else {
1130 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1131 return 0;
1132 }
1133
1134 if (room < count)
1135 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1136 count - room);
1137 if (!room)
1138 return room;
1139
1140 for (i = 0; i < room; i++)
1141 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1142 TTY_NORMAL);
1143
1144 port->icount.rx += room;
1145
1146 return room;
1147}
1148
1149static void sci_dma_rx_complete(void *arg)
1150{
1151 struct sci_port *s = arg;
1152 struct uart_port *port = &s->port;
1153 struct tty_struct *tty = port->state->port.tty;
1154 unsigned long flags;
1155 int count;
1156
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001157 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001158
1159 spin_lock_irqsave(&port->lock, flags);
1160
1161 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1162
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001163 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001164
1165 spin_unlock_irqrestore(&port->lock, flags);
1166
1167 if (count)
1168 tty_flip_buffer_push(tty);
1169
1170 schedule_work(&s->work_rx);
1171}
1172
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001173static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1174{
1175 struct dma_chan *chan = s->chan_rx;
1176 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001177
1178 s->chan_rx = NULL;
1179 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1180 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001181 if (sg_dma_address(&s->sg_rx[0]))
1182 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1183 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001184 if (enable_pio)
1185 sci_start_rx(port);
1186}
1187
1188static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1189{
1190 struct dma_chan *chan = s->chan_tx;
1191 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001192
1193 s->chan_tx = NULL;
1194 s->cookie_tx = -EINVAL;
1195 dma_release_channel(chan);
1196 if (enable_pio)
1197 sci_start_tx(port);
1198}
1199
1200static void sci_submit_rx(struct sci_port *s)
1201{
1202 struct dma_chan *chan = s->chan_rx;
1203 int i;
1204
1205 for (i = 0; i < 2; i++) {
1206 struct scatterlist *sg = &s->sg_rx[i];
1207 struct dma_async_tx_descriptor *desc;
1208
1209 desc = chan->device->device_prep_slave_sg(chan,
1210 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1211
1212 if (desc) {
1213 s->desc_rx[i] = desc;
1214 desc->callback = sci_dma_rx_complete;
1215 desc->callback_param = s;
1216 s->cookie_rx[i] = desc->tx_submit(desc);
1217 }
1218
1219 if (!desc || s->cookie_rx[i] < 0) {
1220 if (i) {
1221 async_tx_ack(s->desc_rx[0]);
1222 s->cookie_rx[0] = -EINVAL;
1223 }
1224 if (desc) {
1225 async_tx_ack(desc);
1226 s->cookie_rx[i] = -EINVAL;
1227 }
1228 dev_warn(s->port.dev,
1229 "failed to re-start DMA, using PIO\n");
1230 sci_rx_dma_release(s, true);
1231 return;
1232 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001233 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1234 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001235 }
1236
1237 s->active_rx = s->cookie_rx[0];
1238
1239 dma_async_issue_pending(chan);
1240}
1241
1242static void work_fn_rx(struct work_struct *work)
1243{
1244 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1245 struct uart_port *port = &s->port;
1246 struct dma_async_tx_descriptor *desc;
1247 int new;
1248
1249 if (s->active_rx == s->cookie_rx[0]) {
1250 new = 0;
1251 } else if (s->active_rx == s->cookie_rx[1]) {
1252 new = 1;
1253 } else {
1254 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1255 return;
1256 }
1257 desc = s->desc_rx[new];
1258
1259 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1260 DMA_SUCCESS) {
1261 /* Handle incomplete DMA receive */
1262 struct tty_struct *tty = port->state->port.tty;
1263 struct dma_chan *chan = s->chan_rx;
1264 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1265 async_tx);
1266 unsigned long flags;
1267 int count;
1268
Linus Walleij05827632010-05-17 16:30:42 -07001269 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001270 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1271 sh_desc->partial, sh_desc->cookie);
1272
1273 spin_lock_irqsave(&port->lock, flags);
1274 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1275 spin_unlock_irqrestore(&port->lock, flags);
1276
1277 if (count)
1278 tty_flip_buffer_push(tty);
1279
1280 sci_submit_rx(s);
1281
1282 return;
1283 }
1284
1285 s->cookie_rx[new] = desc->tx_submit(desc);
1286 if (s->cookie_rx[new] < 0) {
1287 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1288 sci_rx_dma_release(s, true);
1289 return;
1290 }
1291
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001292 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001293
1294 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1295 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001296}
1297
1298static void work_fn_tx(struct work_struct *work)
1299{
1300 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1301 struct dma_async_tx_descriptor *desc;
1302 struct dma_chan *chan = s->chan_tx;
1303 struct uart_port *port = &s->port;
1304 struct circ_buf *xmit = &port->state->xmit;
1305 struct scatterlist *sg = &s->sg_tx;
1306
1307 /*
1308 * DMA is idle now.
1309 * Port xmit buffer is already mapped, and it is one page... Just adjust
1310 * offsets and lengths. Since it is a circular buffer, we have to
1311 * transmit till the end, and then the rest. Take the port lock to get a
1312 * consistent xmit buffer state.
1313 */
1314 spin_lock_irq(&port->lock);
1315 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a3812010-03-19 04:47:01 +00001316 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001317 sg->offset;
Magnus Dammf354a3812010-03-19 04:47:01 +00001318 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001319 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001320 spin_unlock_irq(&port->lock);
1321
Magnus Dammf354a3812010-03-19 04:47:01 +00001322 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001323
1324 desc = chan->device->device_prep_slave_sg(chan,
1325 sg, s->sg_len_tx, DMA_TO_DEVICE,
1326 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1327 if (!desc) {
1328 /* switch to PIO */
1329 sci_tx_dma_release(s, true);
1330 return;
1331 }
1332
1333 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1334
1335 spin_lock_irq(&port->lock);
1336 s->desc_tx = desc;
1337 desc->callback = sci_dma_tx_complete;
1338 desc->callback_param = s;
1339 spin_unlock_irq(&port->lock);
1340 s->cookie_tx = desc->tx_submit(desc);
1341 if (s->cookie_tx < 0) {
1342 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1343 /* switch to PIO */
1344 sci_tx_dma_release(s, true);
1345 return;
1346 }
1347
1348 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1349 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1350
1351 dma_async_issue_pending(chan);
1352}
1353#endif
1354
Russell Kingb129a8c2005-08-31 10:12:14 +01001355static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001357 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001358 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001360#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001361 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001362 u16 new, scr = sci_in(port, SCSCR);
1363 if (s->chan_tx)
1364 new = scr | 0x8000;
1365 else
1366 new = scr & ~0x8000;
1367 if (new != scr)
1368 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001369 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001370
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001371 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1372 s->cookie_tx < 0)
1373 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001374#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001375
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001376 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001377 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1378 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001379 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Russell Kingb129a8c2005-08-31 10:12:14 +01001383static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 unsigned short ctrl;
1386
1387 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001389
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001390 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001391 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001392
Paul Mundt8e698612009-06-24 19:44:32 +09001393 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001398static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 unsigned short ctrl;
1401
Paul Mundtf43dc232011-01-13 15:06:28 +09001402 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1403
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001404 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001405 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410static void sci_stop_rx(struct uart_port *port)
1411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 unsigned short ctrl;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001415
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001416 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001417 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001418
1419 ctrl &= ~port_rx_irq_mask(port);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static void sci_enable_ms(struct uart_port *port)
1425{
1426 /* Nothing here yet .. */
1427}
1428
1429static void sci_break_ctl(struct uart_port *port, int break_state)
1430{
1431 /* Nothing here yet .. */
1432}
1433
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001434#ifdef CONFIG_SERIAL_SH_SCI_DMA
1435static bool filter(struct dma_chan *chan, void *slave)
1436{
1437 struct sh_dmae_slave *param = slave;
1438
1439 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1440 param->slave_id);
1441
1442 if (param->dma_dev == chan->device->dev) {
1443 chan->private = param;
1444 return true;
1445 } else {
1446 return false;
1447 }
1448}
1449
1450static void rx_timer_fn(unsigned long arg)
1451{
1452 struct sci_port *s = (struct sci_port *)arg;
1453 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001454 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001455
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001456 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001457 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001458 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001459 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001460 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001461 dev_dbg(port->dev, "DMA Rx timed out\n");
1462 schedule_work(&s->work_rx);
1463}
1464
1465static void sci_request_dma(struct uart_port *port)
1466{
1467 struct sci_port *s = to_sci_port(port);
1468 struct sh_dmae_slave *param;
1469 struct dma_chan *chan;
1470 dma_cap_mask_t mask;
1471 int nent;
1472
1473 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001474 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001475
Paul Mundtce6738b2011-01-19 15:24:40 +09001476 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001477 return;
1478
1479 dma_cap_zero(mask);
1480 dma_cap_set(DMA_SLAVE, mask);
1481
1482 param = &s->param_tx;
1483
1484 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001485 param->slave_id = s->cfg->dma_slave_tx;
1486 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001487
1488 s->cookie_tx = -EINVAL;
1489 chan = dma_request_channel(mask, filter, param);
1490 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1491 if (chan) {
1492 s->chan_tx = chan;
1493 sg_init_table(&s->sg_tx, 1);
1494 /* UART circular tx buffer is an aligned page. */
1495 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1496 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1497 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1498 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1499 if (!nent)
1500 sci_tx_dma_release(s, false);
1501 else
1502 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1503 sg_dma_len(&s->sg_tx),
1504 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1505
1506 s->sg_len_tx = nent;
1507
1508 INIT_WORK(&s->work_tx, work_fn_tx);
1509 }
1510
1511 param = &s->param_rx;
1512
1513 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001514 param->slave_id = s->cfg->dma_slave_rx;
1515 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001516
1517 chan = dma_request_channel(mask, filter, param);
1518 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1519 if (chan) {
1520 dma_addr_t dma[2];
1521 void *buf[2];
1522 int i;
1523
1524 s->chan_rx = chan;
1525
1526 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1527 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1528 &dma[0], GFP_KERNEL);
1529
1530 if (!buf[0]) {
1531 dev_warn(port->dev,
1532 "failed to allocate dma buffer, using PIO\n");
1533 sci_rx_dma_release(s, true);
1534 return;
1535 }
1536
1537 buf[1] = buf[0] + s->buf_len_rx;
1538 dma[1] = dma[0] + s->buf_len_rx;
1539
1540 for (i = 0; i < 2; i++) {
1541 struct scatterlist *sg = &s->sg_rx[i];
1542
1543 sg_init_table(sg, 1);
1544 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1545 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a3812010-03-19 04:47:01 +00001546 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001547 }
1548
1549 INIT_WORK(&s->work_rx, work_fn_rx);
1550 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1551
1552 sci_submit_rx(s);
1553 }
1554}
1555
1556static void sci_free_dma(struct uart_port *port)
1557{
1558 struct sci_port *s = to_sci_port(port);
1559
Paul Mundtce6738b2011-01-19 15:24:40 +09001560 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001561 return;
1562
1563 if (s->chan_tx)
1564 sci_tx_dma_release(s, false);
1565 if (s->chan_rx)
1566 sci_rx_dma_release(s, false);
1567}
Paul Mundt27bd1072011-01-19 15:37:31 +09001568#else
1569static inline void sci_request_dma(struct uart_port *port)
1570{
1571}
1572
1573static inline void sci_free_dma(struct uart_port *port)
1574{
1575}
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001576#endif
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578static int sci_startup(struct uart_port *port)
1579{
Magnus Damma5660ad2009-01-21 15:14:38 +00001580 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001581 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001583 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1584
Paul Mundt23241d42011-06-28 13:55:31 +09001585 sci_port_enable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Paul Mundt073e84c2011-01-19 17:30:53 +09001587 ret = sci_request_irq(s);
1588 if (unlikely(ret < 0))
1589 return ret;
1590
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001591 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001592
Yoshinori Satod6569012005-10-14 15:59:12 -07001593 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001594 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 return 0;
1597}
1598
1599static void sci_shutdown(struct uart_port *port)
1600{
Magnus Damma5660ad2009-01-21 15:14:38 +00001601 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001603 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001606 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001607
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001608 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 sci_free_irq(s);
1610
Paul Mundt23241d42011-06-28 13:55:31 +09001611 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Paul Mundt26c92f32009-06-24 18:23:52 +09001614static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1615 unsigned long freq)
1616{
1617 switch (algo_id) {
1618 case SCBRR_ALGO_1:
1619 return ((freq + 16 * bps) / (16 * bps) - 1);
1620 case SCBRR_ALGO_2:
1621 return ((freq + 16 * bps) / (32 * bps) - 1);
1622 case SCBRR_ALGO_3:
1623 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1624 case SCBRR_ALGO_4:
1625 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1626 case SCBRR_ALGO_5:
1627 return (((freq * 1000 / 32) / bps) - 1);
1628 }
1629
1630 /* Warn, but use a safe default */
1631 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001632
Paul Mundt26c92f32009-06-24 18:23:52 +09001633 return ((freq + 16 * bps) / (32 * bps) - 1);
1634}
1635
Alan Cox606d0992006-12-08 02:38:45 -08001636static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1637 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Paul Mundt00b9de92009-06-24 17:53:33 +09001639 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001640 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001641 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001642 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Magnus Damm154280f2009-12-22 03:37:28 +00001644 /*
1645 * earlyprintk comes here early on with port->uartclk set to zero.
1646 * the clock framework is not up and running at this point so here
1647 * we assume that 115200 is the maximum baud rate. please note that
1648 * the baud rate is not programmed during earlyprintk - it is assumed
1649 * that the previous boot loader has enabled required clocks and
1650 * setup the baud rate generator hardware for us already.
1651 */
1652 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1653
1654 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1655 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001656 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Paul Mundt23241d42011-06-28 13:55:31 +09001658 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00001659
Paul Mundte108b2c2006-09-27 16:32:13 +09001660 do {
1661 status = sci_in(port, SCxSR);
1662 } while (!(status & SCxSR_TEND(port)));
1663
1664 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1665
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001666 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001667 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001668
1669 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001670
Paul Mundte108b2c2006-09-27 16:32:13 +09001671 if ((termios->c_cflag & CSIZE) == CS7)
1672 smr_val |= 0x40;
1673 if (termios->c_cflag & PARENB)
1674 smr_val |= 0x20;
1675 if (termios->c_cflag & PARODD)
1676 smr_val |= 0x30;
1677 if (termios->c_cflag & CSTOPB)
1678 smr_val |= 0x08;
1679
1680 uart_update_timeout(port, termios->c_cflag, baud);
1681
1682 sci_out(port, SCSMR, smr_val);
1683
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001684 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001685 s->cfg->scscr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001688 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1690 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001691 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 sci_out(port, SCBRR, t);
1695 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1696 }
1697
Paul Mundtd5701642008-12-16 20:07:27 +09001698 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001699 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001700
Paul Mundtce6738b2011-01-19 15:24:40 +09001701 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001703#ifdef CONFIG_SERIAL_SH_SCI_DMA
1704 /*
1705 * Calculate delay for 1.5 DMA buffers: see
1706 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1707 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1708 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1709 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1710 * sizes), but it has been found out experimentally, that this is not
1711 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1712 * as a minimum seem to work perfectly.
1713 */
1714 if (s->chan_rx) {
1715 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1716 port->fifosize / 2;
1717 dev_dbg(port->dev,
1718 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1719 s->rx_timeout * 1000 / HZ, port->timeout);
1720 if (s->rx_timeout < msecs_to_jiffies(20))
1721 s->rx_timeout = msecs_to_jiffies(20);
1722 }
1723#endif
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001726 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001727
Paul Mundt23241d42011-06-28 13:55:31 +09001728 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static const char *sci_type(struct uart_port *port)
1732{
1733 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001734 case PORT_IRDA:
1735 return "irda";
1736 case PORT_SCI:
1737 return "sci";
1738 case PORT_SCIF:
1739 return "scif";
1740 case PORT_SCIFA:
1741 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001742 case PORT_SCIFB:
1743 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Paul Mundtfa439722008-09-04 18:53:58 +09001746 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Paul Mundte2651642011-01-20 21:24:03 +09001749static inline unsigned long sci_port_size(struct uart_port *port)
1750{
1751 /*
1752 * Pick an arbitrary size that encapsulates all of the base
1753 * registers by default. This can be optimized later, or derived
1754 * from platform resource data at such a time that ports begin to
1755 * behave more erratically.
1756 */
1757 return 64;
1758}
1759
Paul Mundtf6e94952011-01-21 15:25:36 +09001760static int sci_remap_port(struct uart_port *port)
1761{
1762 unsigned long size = sci_port_size(port);
1763
1764 /*
1765 * Nothing to do if there's already an established membase.
1766 */
1767 if (port->membase)
1768 return 0;
1769
1770 if (port->flags & UPF_IOREMAP) {
1771 port->membase = ioremap_nocache(port->mapbase, size);
1772 if (unlikely(!port->membase)) {
1773 dev_err(port->dev, "can't remap port#%d\n", port->line);
1774 return -ENXIO;
1775 }
1776 } else {
1777 /*
1778 * For the simple (and majority of) cases where we don't
1779 * need to do any remapping, just cast the cookie
1780 * directly.
1781 */
1782 port->membase = (void __iomem *)port->mapbase;
1783 }
1784
1785 return 0;
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static void sci_release_port(struct uart_port *port)
1789{
Paul Mundte2651642011-01-20 21:24:03 +09001790 if (port->flags & UPF_IOREMAP) {
1791 iounmap(port->membase);
1792 port->membase = NULL;
1793 }
1794
1795 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798static int sci_request_port(struct uart_port *port)
1799{
Paul Mundte2651642011-01-20 21:24:03 +09001800 unsigned long size = sci_port_size(port);
1801 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001802 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Paul Mundt10205202011-01-21 16:00:31 +09001804 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001805 if (unlikely(res == NULL))
1806 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Paul Mundtf6e94952011-01-21 15:25:36 +09001808 ret = sci_remap_port(port);
1809 if (unlikely(ret != 0)) {
1810 release_resource(res);
1811 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001812 }
Paul Mundte2651642011-01-20 21:24:03 +09001813
1814 return 0;
1815}
1816
1817static void sci_config_port(struct uart_port *port, int flags)
1818{
1819 if (flags & UART_CONFIG_TYPE) {
1820 struct sci_port *sport = to_sci_port(port);
1821
1822 port->type = sport->cfg->type;
1823 sci_request_port(port);
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1828{
Magnus Damma5660ad2009-01-21 15:14:38 +00001829 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Paul Mundtce6738b2011-01-19 15:24:40 +09001831 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EINVAL;
1833 if (ser->baud_base < 2400)
1834 /* No paper tape reader for Mitch.. */
1835 return -EINVAL;
1836
1837 return 0;
1838}
1839
1840static struct uart_ops sci_uart_ops = {
1841 .tx_empty = sci_tx_empty,
1842 .set_mctrl = sci_set_mctrl,
1843 .get_mctrl = sci_get_mctrl,
1844 .start_tx = sci_start_tx,
1845 .stop_tx = sci_stop_tx,
1846 .stop_rx = sci_stop_rx,
1847 .enable_ms = sci_enable_ms,
1848 .break_ctl = sci_break_ctl,
1849 .startup = sci_startup,
1850 .shutdown = sci_shutdown,
1851 .set_termios = sci_set_termios,
1852 .type = sci_type,
1853 .release_port = sci_release_port,
1854 .request_port = sci_request_port,
1855 .config_port = sci_config_port,
1856 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001857#ifdef CONFIG_CONSOLE_POLL
1858 .poll_get_char = sci_poll_get_char,
1859 .poll_put_char = sci_poll_put_char,
1860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861};
1862
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001863static int __devinit sci_init_single(struct platform_device *dev,
1864 struct sci_port *sci_port,
1865 unsigned int index,
1866 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001867{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001868 struct uart_port *port = &sci_port->port;
Paul Mundt3127c6b2011-06-28 13:44:37 +09001869 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001870
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001871 port->ops = &sci_uart_ops;
1872 port->iotype = UPIO_MEM;
1873 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001874
1875 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001876 case PORT_SCIFB:
1877 port->fifosize = 256;
1878 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001879 case PORT_SCIFA:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001880 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001881 break;
1882 case PORT_SCIF:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001883 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001884 break;
1885 default:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001886 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001887 break;
1888 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001889
Paul Mundt3127c6b2011-06-28 13:44:37 +09001890 if (p->regtype == SCIx_PROBE_REGTYPE) {
1891 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02001892 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09001893 return ret;
1894 }
Paul Mundt61a69762011-06-14 12:40:19 +09001895
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001896 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001897 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1898 if (IS_ERR(sci_port->iclk)) {
1899 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1900 if (IS_ERR(sci_port->iclk)) {
1901 dev_err(&dev->dev, "can't get iclk\n");
1902 return PTR_ERR(sci_port->iclk);
1903 }
1904 }
1905
1906 /*
1907 * The function clock is optional, ignore it if we can't
1908 * find it.
1909 */
1910 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1911 if (IS_ERR(sci_port->fclk))
1912 sci_port->fclk = NULL;
1913
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001914 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001915
1916 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001917 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001918
Magnus Damm7ed7e072009-01-21 15:14:14 +00001919 sci_port->break_timer.data = (unsigned long)sci_port;
1920 sci_port->break_timer.function = sci_break_timer;
1921 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001922
Paul Mundtdebf9502011-06-08 18:19:37 +09001923 /*
1924 * Establish some sensible defaults for the error detection.
1925 */
1926 if (!p->error_mask)
1927 p->error_mask = (p->type == PORT_SCI) ?
1928 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1929
1930 /*
1931 * Establish sensible defaults for the overrun detection, unless
1932 * the part has explicitly disabled support for it.
1933 */
1934 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1935 if (p->type == PORT_SCI)
1936 p->overrun_bit = 5;
1937 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1938 p->overrun_bit = 9;
1939 else
1940 p->overrun_bit = 0;
1941
1942 /*
1943 * Make the error mask inclusive of overrun detection, if
1944 * supported.
1945 */
1946 p->error_mask |= (1 << p->overrun_bit);
1947 }
1948
Paul Mundtce6738b2011-01-19 15:24:40 +09001949 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001950
Paul Mundtce6738b2011-01-19 15:24:40 +09001951 port->mapbase = p->mapbase;
1952 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001953 port->flags = p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09001954 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09001955
1956 /*
Paul Mundt61a69762011-06-14 12:40:19 +09001957 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09001958 * for the multi-IRQ ports, which is where we are primarily
1959 * concerned with the shutdown path synchronization.
1960 *
1961 * For the muxed case there's nothing more to do.
1962 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00001963 port->irq = p->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001964 port->irqflags = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001965
Paul Mundt61a69762011-06-14 12:40:19 +09001966 port->serial_in = sci_serial_in;
1967 port->serial_out = sci_serial_out;
1968
Paul Mundtce6738b2011-01-19 15:24:40 +09001969 if (p->dma_dev)
1970 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1971 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001972
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001973 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001977static void serial_console_putchar(struct uart_port *port, int ch)
1978{
1979 sci_poll_put_char(port, ch);
1980}
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982/*
1983 * Print a string to the serial port trying not to disturb
1984 * any possible real use of the port...
1985 */
1986static void serial_console_write(struct console *co, const char *s,
1987 unsigned count)
1988{
Paul Mundt906b17d2011-01-21 16:19:53 +09001989 struct sci_port *sci_port = &sci_ports[co->index];
1990 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001991 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001992
Paul Mundt23241d42011-06-28 13:55:31 +09001993 sci_port_enable(sci_port);
Magnus Damm501b8252009-01-21 15:14:30 +00001994
1995 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001996
1997 /* wait until fifo is empty and last bit has been transmitted */
1998 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1999 while ((sci_in(port, SCxSR) & bits) != bits)
2000 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00002001
Paul Mundt23241d42011-06-28 13:55:31 +09002002 sci_port_disable(sci_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002005static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002007 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct uart_port *port;
2009 int baud = 115200;
2010 int bits = 8;
2011 int parity = 'n';
2012 int flow = 'n';
2013 int ret;
2014
Paul Mundte108b2c2006-09-27 16:32:13 +09002015 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002016 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002017 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002018 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002019 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002020
Paul Mundt906b17d2011-01-21 16:19:53 +09002021 sci_port = &sci_ports[co->index];
2022 port = &sci_port->port;
2023
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002024 /*
2025 * Refuse to handle uninitialized ports.
2026 */
2027 if (!port->ops)
2028 return -ENODEV;
2029
Paul Mundtf6e94952011-01-21 15:25:36 +09002030 ret = sci_remap_port(port);
2031 if (unlikely(ret != 0))
2032 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002033
Paul Mundt23241d42011-06-28 13:55:31 +09002034 sci_port_enable(sci_port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (options)
2037 uart_parse_options(options, &baud, &parity, &bits, &flow);
2038
Magnus Damm501b8252009-01-21 15:14:30 +00002039 /* TODO: disable clock */
Paul Mundtab7cfb52011-06-01 14:47:42 +09002040 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043static struct console serial_console = {
2044 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002045 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 .write = serial_console_write,
2047 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002048 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002050 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051};
2052
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002053static struct console early_serial_console = {
2054 .name = "early_ttySC",
2055 .write = serial_console_write,
2056 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002057 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002058};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002059
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002060static char early_serial_buf[32];
2061
Paul Mundtecdf8a42011-01-21 00:05:48 +09002062static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2063{
2064 struct plat_sci_port *cfg = pdev->dev.platform_data;
2065
2066 if (early_serial_console.data)
2067 return -EEXIST;
2068
2069 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002070
Paul Mundt906b17d2011-01-21 16:19:53 +09002071 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002072
2073 serial_console_setup(&early_serial_console, early_serial_buf);
2074
2075 if (!strstr(early_serial_buf, "keep"))
2076 early_serial_console.flags |= CON_BOOT;
2077
2078 register_console(&early_serial_console);
2079 return 0;
2080}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002081
2082#define SCI_CONSOLE (&serial_console)
2083
Paul Mundtecdf8a42011-01-21 00:05:48 +09002084#else
2085static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2086{
2087 return -EINVAL;
2088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002090#define SCI_CONSOLE NULL
2091
2092#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094static char banner[] __initdata =
2095 KERN_INFO "SuperH SCI(F) driver initialized\n";
2096
2097static struct uart_driver sci_uart_driver = {
2098 .owner = THIS_MODULE,
2099 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 .dev_name = "ttySC",
2101 .major = SCI_MAJOR,
2102 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002103 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 .cons = SCI_CONSOLE,
2105};
2106
Paul Mundt54507f62009-05-08 23:48:33 +09002107static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002108{
Paul Mundtd535a232011-01-19 17:19:35 +09002109 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002110
Paul Mundtd535a232011-01-19 17:19:35 +09002111 cpufreq_unregister_notifier(&port->freq_transition,
2112 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002113
Paul Mundtd535a232011-01-19 17:19:35 +09002114 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002115
Paul Mundtd535a232011-01-19 17:19:35 +09002116 clk_put(port->iclk);
2117 clk_put(port->fclk);
2118
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002119 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00002120 return 0;
2121}
2122
Magnus Damm0ee70712009-01-21 15:13:50 +00002123static int __devinit sci_probe_single(struct platform_device *dev,
2124 unsigned int index,
2125 struct plat_sci_port *p,
2126 struct sci_port *sciport)
2127{
Magnus Damm0ee70712009-01-21 15:13:50 +00002128 int ret;
2129
2130 /* Sanity check */
2131 if (unlikely(index >= SCI_NPORTS)) {
2132 dev_notice(&dev->dev, "Attempting to register port "
2133 "%d when only %d are available.\n",
2134 index+1, SCI_NPORTS);
2135 dev_notice(&dev->dev, "Consider bumping "
2136 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
2137 return 0;
2138 }
2139
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002140 ret = sci_init_single(dev, sciport, index, p);
2141 if (ret)
2142 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002143
Paul Mundtd535a232011-01-19 17:19:35 +09002144 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00002145}
2146
Paul Mundte108b2c2006-09-27 16:32:13 +09002147static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Paul Mundte108b2c2006-09-27 16:32:13 +09002149 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09002150 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002151 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002152
Paul Mundtecdf8a42011-01-21 00:05:48 +09002153 /*
2154 * If we've come here via earlyprintk initialization, head off to
2155 * the special early probe. We don't have sufficient device state
2156 * to make it beyond this yet.
2157 */
2158 if (is_early_platform_device(dev))
2159 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002160
Paul Mundtd535a232011-01-19 17:19:35 +09002161 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002162
Paul Mundt906b17d2011-01-21 16:19:53 +09002163 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002164 if (ret)
2165 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00002166
Paul Mundtd535a232011-01-19 17:19:35 +09002167 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Paul Mundtd535a232011-01-19 17:19:35 +09002169 ret = cpufreq_register_notifier(&sp->freq_transition,
2170 CPUFREQ_TRANSITION_NOTIFIER);
2171 if (unlikely(ret < 0))
2172 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174#ifdef CONFIG_SH_STANDARD_BIOS
2175 sh_bios_gdb_detach();
2176#endif
2177
Paul Mundte108b2c2006-09-27 16:32:13 +09002178 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002179
2180err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002181 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002182 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002183}
2184
Paul Mundt6daa79b2009-06-15 07:07:38 +09002185static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002186{
Paul Mundtd535a232011-01-19 17:19:35 +09002187 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002188
Paul Mundtd535a232011-01-19 17:19:35 +09002189 if (sport)
2190 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002191
2192 return 0;
2193}
2194
Paul Mundt6daa79b2009-06-15 07:07:38 +09002195static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002196{
Paul Mundtd535a232011-01-19 17:19:35 +09002197 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002198
Paul Mundtd535a232011-01-19 17:19:35 +09002199 if (sport)
2200 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002201
2202 return 0;
2203}
2204
Alexey Dobriyan47145212009-12-14 18:00:08 -08002205static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002206 .suspend = sci_suspend,
2207 .resume = sci_resume,
2208};
2209
Paul Mundte108b2c2006-09-27 16:32:13 +09002210static struct platform_driver sci_driver = {
2211 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002212 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002213 .driver = {
2214 .name = "sh-sci",
2215 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002216 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002217 },
2218};
2219
2220static int __init sci_init(void)
2221{
2222 int ret;
2223
2224 printk(banner);
2225
Paul Mundte108b2c2006-09-27 16:32:13 +09002226 ret = uart_register_driver(&sci_uart_driver);
2227 if (likely(ret == 0)) {
2228 ret = platform_driver_register(&sci_driver);
2229 if (unlikely(ret))
2230 uart_unregister_driver(&sci_uart_driver);
2231 }
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return ret;
2234}
2235
2236static void __exit sci_exit(void)
2237{
Paul Mundte108b2c2006-09-27 16:32:13 +09002238 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 uart_unregister_driver(&sci_uart_driver);
2240}
2241
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002242#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2243early_platform_init_buffer("earlyprintk", &sci_driver,
2244 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246module_init(sci_init);
2247module_exit(sci_exit);
2248
Paul Mundte108b2c2006-09-27 16:32:13 +09002249MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002250MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09002251MODULE_AUTHOR("Paul Mundt");
2252MODULE_DESCRIPTION("SuperH SCI(F) serial driver");