blob: 38a81ae9b7df9e34410215a18c76a132c9ae123b [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 Liakhovetski73a19e42010-03-02 11:39:15 +090049#include <linux/dmaengine.h>
Magnus Damm5beabc72011-08-02 09:42:54 +000050#include <linux/dma-mapping.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090051#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090053
54#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090055#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "sh-sci.h"
59
Paul Mundte108b2c2006-09-27 16:32:13 +090060struct sci_port {
61 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Paul Mundtce6738b2011-01-19 15:24:40 +090063 /* Platform configuration */
64 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090065
Paul Mundte108b2c2006-09-27 16:32:13 +090066 /* Break timer */
67 struct timer_list break_timer;
68 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090069
Magnus Damm501b8252009-01-21 15:14:30 +000070 /* Interface clock */
71 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090072 /* Function clock */
73 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090074
Paul Mundt9174fc82011-06-28 15:25:36 +090075 char *irqstr[SCIx_NR_IRQS];
76
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090077 struct dma_chan *chan_tx;
78 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090079
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090080#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090081 struct dma_async_tx_descriptor *desc_tx;
82 struct dma_async_tx_descriptor *desc_rx[2];
83 dma_cookie_t cookie_tx;
84 dma_cookie_t cookie_rx[2];
85 dma_cookie_t active_rx;
86 struct scatterlist sg_tx;
87 unsigned int sg_len_tx;
88 struct scatterlist sg_rx[2];
89 size_t buf_len_rx;
90 struct sh_dmae_slave param_tx;
91 struct sh_dmae_slave param_rx;
92 struct work_struct work_tx;
93 struct work_struct work_rx;
94 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +000095 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090096#endif
Magnus Damme552de22009-01-21 15:13:42 +000097
Paul Mundtd535a232011-01-19 17:19:35 +090098 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +090099};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900102static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100103static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900104static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Paul Mundte108b2c2006-09-27 16:32:13 +0900106#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
107
108static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct uart_driver sci_uart_driver;
110
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900111static inline struct sci_port *
112to_sci_port(struct uart_port *uart)
113{
114 return container_of(uart, struct sci_port, port);
115}
116
Paul Mundt61a69762011-06-14 12:40:19 +0900117struct plat_sci_reg {
118 u8 offset, size;
119};
120
121/* Helper for invalidating specific entries of an inherited map. */
122#define sci_reg_invalid { .offset = 0, .size = 0 }
123
124static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
125 [SCIx_PROBE_REGTYPE] = {
126 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
127 },
128
129 /*
130 * Common SCI definitions, dependent on the port's regshift
131 * value.
132 */
133 [SCIx_SCI_REGTYPE] = {
134 [SCSMR] = { 0x00, 8 },
135 [SCBRR] = { 0x01, 8 },
136 [SCSCR] = { 0x02, 8 },
137 [SCxTDR] = { 0x03, 8 },
138 [SCxSR] = { 0x04, 8 },
139 [SCxRDR] = { 0x05, 8 },
140 [SCFCR] = sci_reg_invalid,
141 [SCFDR] = sci_reg_invalid,
142 [SCTFDR] = sci_reg_invalid,
143 [SCRFDR] = sci_reg_invalid,
144 [SCSPTR] = sci_reg_invalid,
145 [SCLSR] = sci_reg_invalid,
146 },
147
148 /*
149 * Common definitions for legacy IrDA ports, dependent on
150 * regshift value.
151 */
152 [SCIx_IRDA_REGTYPE] = {
153 [SCSMR] = { 0x00, 8 },
154 [SCBRR] = { 0x01, 8 },
155 [SCSCR] = { 0x02, 8 },
156 [SCxTDR] = { 0x03, 8 },
157 [SCxSR] = { 0x04, 8 },
158 [SCxRDR] = { 0x05, 8 },
159 [SCFCR] = { 0x06, 8 },
160 [SCFDR] = { 0x07, 16 },
161 [SCTFDR] = sci_reg_invalid,
162 [SCRFDR] = sci_reg_invalid,
163 [SCSPTR] = sci_reg_invalid,
164 [SCLSR] = sci_reg_invalid,
165 },
166
167 /*
168 * Common SCIFA definitions.
169 */
170 [SCIx_SCIFA_REGTYPE] = {
171 [SCSMR] = { 0x00, 16 },
172 [SCBRR] = { 0x04, 8 },
173 [SCSCR] = { 0x08, 16 },
174 [SCxTDR] = { 0x20, 8 },
175 [SCxSR] = { 0x14, 16 },
176 [SCxRDR] = { 0x24, 8 },
177 [SCFCR] = { 0x18, 16 },
178 [SCFDR] = { 0x1c, 16 },
179 [SCTFDR] = sci_reg_invalid,
180 [SCRFDR] = sci_reg_invalid,
181 [SCSPTR] = sci_reg_invalid,
182 [SCLSR] = sci_reg_invalid,
183 },
184
185 /*
186 * Common SCIFB definitions.
187 */
188 [SCIx_SCIFB_REGTYPE] = {
189 [SCSMR] = { 0x00, 16 },
190 [SCBRR] = { 0x04, 8 },
191 [SCSCR] = { 0x08, 16 },
192 [SCxTDR] = { 0x40, 8 },
193 [SCxSR] = { 0x14, 16 },
194 [SCxRDR] = { 0x60, 8 },
195 [SCFCR] = { 0x18, 16 },
196 [SCFDR] = { 0x1c, 16 },
197 [SCTFDR] = sci_reg_invalid,
198 [SCRFDR] = sci_reg_invalid,
199 [SCSPTR] = sci_reg_invalid,
200 [SCLSR] = sci_reg_invalid,
201 },
202
203 /*
204 * Common SH-3 SCIF definitions.
205 */
206 [SCIx_SH3_SCIF_REGTYPE] = {
207 [SCSMR] = { 0x00, 8 },
208 [SCBRR] = { 0x02, 8 },
209 [SCSCR] = { 0x04, 8 },
210 [SCxTDR] = { 0x06, 8 },
211 [SCxSR] = { 0x08, 16 },
212 [SCxRDR] = { 0x0a, 8 },
213 [SCFCR] = { 0x0c, 8 },
214 [SCFDR] = { 0x0e, 16 },
215 [SCTFDR] = sci_reg_invalid,
216 [SCRFDR] = sci_reg_invalid,
217 [SCSPTR] = sci_reg_invalid,
218 [SCLSR] = sci_reg_invalid,
219 },
220
221 /*
222 * Common SH-4(A) SCIF(B) definitions.
223 */
224 [SCIx_SH4_SCIF_REGTYPE] = {
225 [SCSMR] = { 0x00, 16 },
226 [SCBRR] = { 0x04, 8 },
227 [SCSCR] = { 0x08, 16 },
228 [SCxTDR] = { 0x0c, 8 },
229 [SCxSR] = { 0x10, 16 },
230 [SCxRDR] = { 0x14, 8 },
231 [SCFCR] = { 0x18, 16 },
232 [SCFDR] = { 0x1c, 16 },
233 [SCTFDR] = sci_reg_invalid,
234 [SCRFDR] = sci_reg_invalid,
235 [SCSPTR] = { 0x20, 16 },
236 [SCLSR] = { 0x24, 16 },
237 },
238
239 /*
240 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
241 * register.
242 */
243 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
244 [SCSMR] = { 0x00, 16 },
245 [SCBRR] = { 0x04, 8 },
246 [SCSCR] = { 0x08, 16 },
247 [SCxTDR] = { 0x0c, 8 },
248 [SCxSR] = { 0x10, 16 },
249 [SCxRDR] = { 0x14, 8 },
250 [SCFCR] = { 0x18, 16 },
251 [SCFDR] = { 0x1c, 16 },
252 [SCTFDR] = sci_reg_invalid,
253 [SCRFDR] = sci_reg_invalid,
254 [SCSPTR] = sci_reg_invalid,
255 [SCLSR] = { 0x24, 16 },
256 },
257
258 /*
259 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
260 * count registers.
261 */
262 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
263 [SCSMR] = { 0x00, 16 },
264 [SCBRR] = { 0x04, 8 },
265 [SCSCR] = { 0x08, 16 },
266 [SCxTDR] = { 0x0c, 8 },
267 [SCxSR] = { 0x10, 16 },
268 [SCxRDR] = { 0x14, 8 },
269 [SCFCR] = { 0x18, 16 },
270 [SCFDR] = { 0x1c, 16 },
271 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
272 [SCRFDR] = { 0x20, 16 },
273 [SCSPTR] = { 0x24, 16 },
274 [SCLSR] = { 0x28, 16 },
275 },
276
277 /*
278 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
279 * registers.
280 */
281 [SCIx_SH7705_SCIF_REGTYPE] = {
282 [SCSMR] = { 0x00, 16 },
283 [SCBRR] = { 0x04, 8 },
284 [SCSCR] = { 0x08, 16 },
285 [SCxTDR] = { 0x20, 8 },
286 [SCxSR] = { 0x14, 16 },
287 [SCxRDR] = { 0x24, 8 },
288 [SCFCR] = { 0x18, 16 },
289 [SCFDR] = { 0x1c, 16 },
290 [SCTFDR] = sci_reg_invalid,
291 [SCRFDR] = sci_reg_invalid,
292 [SCSPTR] = sci_reg_invalid,
293 [SCLSR] = sci_reg_invalid,
294 },
295};
296
Paul Mundt72b294c2011-06-14 17:38:19 +0900297#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
298
Paul Mundt61a69762011-06-14 12:40:19 +0900299/*
300 * The "offset" here is rather misleading, in that it refers to an enum
301 * value relative to the port mapping rather than the fixed offset
302 * itself, which needs to be manually retrieved from the platform's
303 * register map for the given port.
304 */
305static unsigned int sci_serial_in(struct uart_port *p, int offset)
306{
Paul Mundt72b294c2011-06-14 17:38:19 +0900307 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900308
309 if (reg->size == 8)
310 return ioread8(p->membase + (reg->offset << p->regshift));
311 else if (reg->size == 16)
312 return ioread16(p->membase + (reg->offset << p->regshift));
313 else
314 WARN(1, "Invalid register access\n");
315
316 return 0;
317}
318
319static void sci_serial_out(struct uart_port *p, int offset, int value)
320{
Paul Mundt72b294c2011-06-14 17:38:19 +0900321 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900322
323 if (reg->size == 8)
324 iowrite8(value, p->membase + (reg->offset << p->regshift));
325 else if (reg->size == 16)
326 iowrite16(value, p->membase + (reg->offset << p->regshift));
327 else
328 WARN(1, "Invalid register access\n");
329}
330
331#define sci_in(up, offset) (up->serial_in(up, offset))
332#define sci_out(up, offset, value) (up->serial_out(up, offset, value))
333
334static int sci_probe_regmap(struct plat_sci_port *cfg)
335{
336 switch (cfg->type) {
337 case PORT_SCI:
338 cfg->regtype = SCIx_SCI_REGTYPE;
339 break;
340 case PORT_IRDA:
341 cfg->regtype = SCIx_IRDA_REGTYPE;
342 break;
343 case PORT_SCIFA:
344 cfg->regtype = SCIx_SCIFA_REGTYPE;
345 break;
346 case PORT_SCIFB:
347 cfg->regtype = SCIx_SCIFB_REGTYPE;
348 break;
349 case PORT_SCIF:
350 /*
351 * The SH-4 is a bit of a misnomer here, although that's
352 * where this particular port layout originated. This
353 * configuration (or some slight variation thereof)
354 * remains the dominant model for all SCIFs.
355 */
356 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
357 break;
358 default:
359 printk(KERN_ERR "Can't probe register map for given port\n");
360 return -EINVAL;
361 }
362
363 return 0;
364}
365
Paul Mundt23241d42011-06-28 13:55:31 +0900366static void sci_port_enable(struct sci_port *sci_port)
367{
368 if (!sci_port->port.dev)
369 return;
370
371 pm_runtime_get_sync(sci_port->port.dev);
372
373 clk_enable(sci_port->iclk);
374 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
375 clk_enable(sci_port->fclk);
376}
377
378static void sci_port_disable(struct sci_port *sci_port)
379{
380 if (!sci_port->port.dev)
381 return;
382
383 clk_disable(sci_port->fclk);
384 clk_disable(sci_port->iclk);
385
386 pm_runtime_put_sync(sci_port->port.dev);
387}
388
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900389#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900390
391#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900392static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned short status;
395 int c;
396
Paul Mundte108b2c2006-09-27 16:32:13 +0900397 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 status = sci_in(port, SCxSR);
399 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900400 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 continue;
402 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500403 break;
404 } while (1);
405
406 if (!(status & SCxSR_RDxF(port)))
407 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900410
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900411 /* Dummy read */
412 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return c;
416}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900417#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900419static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned short status;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 do {
424 status = sci_in(port, SCxSR);
425 } while (!(status & SCxSR_TDxE(port)));
426
Paul Mundt272966c2008-11-13 17:46:06 +0900427 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000428 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900430#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Paul Mundt61a69762011-06-14 12:40:19 +0900432static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900433{
Paul Mundt61a69762011-06-14 12:40:19 +0900434 struct sci_port *s = to_sci_port(port);
435 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900436
Paul Mundt61a69762011-06-14 12:40:19 +0900437 /*
438 * Use port-specific handler if provided.
439 */
440 if (s->cfg->ops && s->cfg->ops->init_pins) {
441 s->cfg->ops->init_pins(port, cflag);
442 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Paul Mundt61a69762011-06-14 12:40:19 +0900445 /*
446 * For the generic path SCSPTR is necessary. Bail out if that's
447 * unavailable, too.
448 */
449 if (!reg->size)
450 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800451
Paul Mundtd5701642008-12-16 20:07:27 +0900452 if (!(cflag & CRTSCTS))
Paul Mundt61a69762011-06-14 12:40:19 +0900453 sci_out(port, SCSPTR, 0x0080); /* Set RTS = 1 */
Paul Mundtd5701642008-12-16 20:07:27 +0900454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900456static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900457{
Paul Mundt72b294c2011-06-14 17:38:19 +0900458 struct plat_sci_reg *reg;
459
460 reg = sci_getreg(port, SCTFDR);
461 if (reg->size)
462 return sci_in(port, SCTFDR) & 0xff;
463
464 reg = sci_getreg(port, SCFDR);
465 if (reg->size)
466 return sci_in(port, SCFDR) >> 8;
467
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900468 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900469}
470
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900471static int sci_txroom(struct uart_port *port)
472{
Paul Mundt72b294c2011-06-14 17:38:19 +0900473 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900474}
475
476static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900477{
Paul Mundt72b294c2011-06-14 17:38:19 +0900478 struct plat_sci_reg *reg;
479
480 reg = sci_getreg(port, SCRFDR);
481 if (reg->size)
482 return sci_in(port, SCRFDR) & 0xff;
483
484 reg = sci_getreg(port, SCFDR);
485 if (reg->size)
486 return sci_in(port, SCFDR) & ((port->fifosize << 1) - 1);
487
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900488 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900489}
490
Paul Mundt514820e2011-06-08 18:51:32 +0900491/*
492 * SCI helper for checking the state of the muxed port/RXD pins.
493 */
494static inline int sci_rxd_in(struct uart_port *port)
495{
496 struct sci_port *s = to_sci_port(port);
497
498 if (s->cfg->port_reg <= 0)
499 return 1;
500
501 return !!__raw_readb(s->cfg->port_reg);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/* ********************************************************************** *
505 * the interrupt related routines *
506 * ********************************************************************** */
507
508static void sci_transmit_chars(struct uart_port *port)
509{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700510 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned short status;
513 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900514 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 status = sci_in(port, SCxSR);
517 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900519 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900520 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900521 else
Paul Mundt8e698612009-06-24 19:44:32 +0900522 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return;
525 }
526
Paul Mundt72b294c2011-06-14 17:38:19 +0900527 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 do {
530 unsigned char c;
531
532 if (port->x_char) {
533 c = port->x_char;
534 port->x_char = 0;
535 } else if (!uart_circ_empty(xmit) && !stopped) {
536 c = xmit->buf[xmit->tail];
537 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
538 } else {
539 break;
540 }
541
542 sci_out(port, SCxTDR, c);
543
544 port->icount.tx++;
545 } while (--count > 0);
546
547 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
548
549 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
550 uart_write_wakeup(port);
551 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100552 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ctrl = sci_in(port, SCSCR);
555
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900556 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 sci_in(port, SCxSR); /* Dummy read */
558 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Paul Mundt8e698612009-06-24 19:44:32 +0900561 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564}
565
566/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900567#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900569static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900571 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700572 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int i, count, copied = 0;
574 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800575 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 status = sci_in(port, SCxSR);
578 if (!(status & SCxSR_RDxF(port)))
579 return;
580
581 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Don't copy more bytes than there is room for in the buffer */
Paul Mundt72b294c2011-06-14 17:38:19 +0900583 count = tty_buffer_request_room(tty, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* If for any reason we can't copy more data, we're done! */
586 if (count == 0)
587 break;
588
589 if (port->type == PORT_SCI) {
590 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900591 if (uart_handle_sysrq_char(port, c) ||
592 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900594 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900595 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900597 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 char c = sci_in(port, SCxRDR);
599 status = sci_in(port, SCxSR);
600#if defined(CONFIG_CPU_SH3)
601 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900602 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if ((c == 0) &&
604 (status & SCxSR_FER(port))) {
605 count--; i--;
606 continue;
607 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900610 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900611 sci_port->break_flag = 0;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (STEPFN(c)) {
614 count--; i--;
615 continue;
616 }
617 }
618#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100619 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 count--; i--;
621 continue;
622 }
623
624 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900625 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800626 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900627 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900628 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800629 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900630 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800631 } else
632 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900633
Alan Cox33f0f882006-01-09 20:54:13 -0800634 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 }
637
638 sci_in(port, SCxSR); /* dummy read */
639 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 copied += count;
642 port->icount.rx += count;
643 }
644
645 if (copied) {
646 /* Tell the rest of the system the news. New characters! */
647 tty_flip_buffer_push(tty);
648 } else {
649 sci_in(port, SCxSR); /* dummy read */
650 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
651 }
652}
653
654#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900655
656/*
657 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * 1 per millisecond or so during the break period, for 9600 baud.
659 * So dont bother disabling interrupts.
660 * But dont want more than 1 break event.
661 * Use a kernel timer to periodically poll the rx line until
662 * the break is finished.
663 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900664static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900666 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/* Ensure that two consecutive samples find the break over. */
670static void sci_break_timer(unsigned long data)
671{
Paul Mundte108b2c2006-09-27 16:32:13 +0900672 struct sci_port *port = (struct sci_port *)data;
673
Paul Mundt23241d42011-06-28 13:55:31 +0900674 sci_port_enable(port);
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000675
Paul Mundte108b2c2006-09-27 16:32:13 +0900676 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900678 sci_schedule_break_timer(port);
679 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* break is over. */
681 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900682 sci_schedule_break_timer(port);
683 } else
684 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000685
Paul Mundt23241d42011-06-28 13:55:31 +0900686 sci_port_disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900689static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 int copied = 0;
692 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700693 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900694 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Paul Mundtdebf9502011-06-08 18:19:37 +0900696 /*
697 * Handle overruns, if supported.
698 */
699 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
700 if (status & (1 << s->cfg->overrun_bit)) {
701 /* overrun error */
702 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
703 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900704
Paul Mundtdebf9502011-06-08 18:19:37 +0900705 dev_notice(port->dev, "overrun error");
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
Paul Mundte108b2c2006-09-27 16:32:13 +0900709 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (sci_rxd_in(port) == 0) {
711 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900712 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900713
714 if (!sci_port->break_flag) {
715 sci_port->break_flag = 1;
716 sci_schedule_break_timer(sci_port);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900719 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900721
722 dev_dbg(port->dev, "BREAK detected\n");
723
Paul Mundte108b2c2006-09-27 16:32:13 +0900724 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900725 copied++;
726 }
727
Paul Mundte108b2c2006-09-27 16:32:13 +0900728 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900730 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800731 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900732
733 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 }
736
Paul Mundte108b2c2006-09-27 16:32:13 +0900737 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900739 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
740 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900741
742 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Alan Cox33f0f882006-01-09 20:54:13 -0800745 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 return copied;
749}
750
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900751static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900752{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700753 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900754 struct sci_port *s = to_sci_port(port);
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900755 struct plat_sci_reg *reg;
Paul Mundtd830fa42008-12-16 19:29:38 +0900756 int copied = 0;
757
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900758 reg = sci_getreg(port, SCLSR);
759 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +0900760 return 0;
761
Paul Mundtdebf9502011-06-08 18:19:37 +0900762 if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
Paul Mundtd830fa42008-12-16 19:29:38 +0900763 sci_out(port, SCLSR, 0);
764
765 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
766 tty_flip_buffer_push(tty);
767
768 dev_notice(port->dev, "overrun error\n");
769 copied++;
770 }
771
772 return copied;
773}
774
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900775static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 int copied = 0;
778 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700779 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000780 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900782 if (uart_handle_break(port))
783 return 0;
784
Paul Mundtb7a76e42006-02-01 03:06:06 -0800785 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#if defined(CONFIG_CPU_SH3)
787 /* Debounce break */
788 s->break_flag = 1;
789#endif
790 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900791 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800792 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900793
794 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Alan Cox33f0f882006-01-09 20:54:13 -0800797 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900799
Paul Mundtd830fa42008-12-16 19:29:38 +0900800 copied += sci_handle_fifo_overrun(port);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return copied;
803}
804
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900805static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900807#ifdef CONFIG_SERIAL_SH_SCI_DMA
808 struct uart_port *port = ptr;
809 struct sci_port *s = to_sci_port(port);
810
811 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900812 u16 scr = sci_in(port, SCSCR);
813 u16 ssr = sci_in(port, SCxSR);
814
815 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000816 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000817 disable_irq_nosync(irq);
818 scr |= 0x4000;
819 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900820 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000821 }
822 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900823 /* Clear current interrupt */
824 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000825 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
826 jiffies, s->rx_timeout);
827 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900828
829 return IRQ_HANDLED;
830 }
831#endif
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* I think sci_receive_chars has to be called irrespective
834 * of whether the I_IXOFF is set, otherwise, how is the interrupt
835 * to be disabled?
836 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900837 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 return IRQ_HANDLED;
840}
841
David Howells7d12e782006-10-05 14:55:46 +0100842static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900845 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Stuart Menefyfd78a762009-07-29 23:01:24 +0900847 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900849 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 return IRQ_HANDLED;
852}
853
David Howells7d12e782006-10-05 14:55:46 +0100854static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct uart_port *port = ptr;
857
858 /* Handle errors */
859 if (port->type == PORT_SCI) {
860 if (sci_handle_errors(port)) {
861 /* discard character in rx buffer */
862 sci_in(port, SCxSR);
863 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
864 }
865 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900866 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100867 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
871
872 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100873 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 return IRQ_HANDLED;
876}
877
David Howells7d12e782006-10-05 14:55:46 +0100878static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct uart_port *port = ptr;
881
882 /* Handle BREAKs */
883 sci_handle_breaks(port);
884 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
885
886 return IRQ_HANDLED;
887}
888
Paul Mundtf43dc232011-01-13 15:06:28 +0900889static inline unsigned long port_rx_irq_mask(struct uart_port *port)
890{
891 /*
892 * Not all ports (such as SCIFA) will support REIE. Rather than
893 * special-casing the port type, we check the port initialization
894 * IRQ enable mask to see whether the IRQ is desired at all. If
895 * it's unset, it's logically inferred that there's no point in
896 * testing for it.
897 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900898 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900899}
900
David Howells7d12e782006-10-05 14:55:46 +0100901static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Magnus Damm44e18e92009-07-03 08:39:34 +0000903 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900904 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900905 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900906 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900908 ssr_status = sci_in(port, SCxSR);
909 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900910 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900913 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900914 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900915 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900916
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900917 /*
918 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
919 * DR flags
920 */
921 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900922 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900923 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000926 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900927 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000930 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900931 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Michael Trimarchia8884e32008-10-31 16:10:23 +0900933 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300937 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * ports' baud rate when the peripheral clock changes.
939 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900940static int sci_notifier(struct notifier_block *self,
941 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Magnus Damme552de22009-01-21 15:13:42 +0000943 struct sci_port *sci_port;
944 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Paul Mundtd535a232011-01-19 17:19:35 +0900946 sci_port = container_of(self, struct sci_port, freq_transition);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000949 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900950 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900951
Paul Mundtd535a232011-01-19 17:19:35 +0900952 spin_lock_irqsave(&port->lock, flags);
953 port->uartclk = clk_get_rate(sci_port->iclk);
954 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return NOTIFY_OK;
958}
Magnus Damm501b8252009-01-21 15:14:30 +0000959
Paul Mundt9174fc82011-06-28 15:25:36 +0900960static struct sci_irq_desc {
961 const char *desc;
962 irq_handler_t handler;
963} sci_irq_desc[] = {
964 /*
965 * Split out handlers, the default case.
966 */
967 [SCIx_ERI_IRQ] = {
968 .desc = "rx err",
969 .handler = sci_er_interrupt,
970 },
971
972 [SCIx_RXI_IRQ] = {
973 .desc = "rx full",
974 .handler = sci_rx_interrupt,
975 },
976
977 [SCIx_TXI_IRQ] = {
978 .desc = "tx empty",
979 .handler = sci_tx_interrupt,
980 },
981
982 [SCIx_BRI_IRQ] = {
983 .desc = "break",
984 .handler = sci_br_interrupt,
985 },
986
987 /*
988 * Special muxed handler.
989 */
990 [SCIx_MUX_IRQ] = {
991 .desc = "mux",
992 .handler = sci_mpxed_interrupt,
993 },
994};
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static int sci_request_irq(struct sci_port *port)
997{
Paul Mundt9174fc82011-06-28 15:25:36 +0900998 struct uart_port *up = &port->port;
999 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Paul Mundt9174fc82011-06-28 15:25:36 +09001001 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
1002 struct sci_irq_desc *desc;
1003 unsigned int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001004
Paul Mundt9174fc82011-06-28 15:25:36 +09001005 if (SCIx_IRQ_IS_MUXED(port)) {
1006 i = SCIx_MUX_IRQ;
1007 irq = up->irq;
1008 } else
1009 irq = port->cfg->irqs[i];
1010
1011 desc = sci_irq_desc + i;
1012 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1013 dev_name(up->dev), desc->desc);
1014 if (!port->irqstr[j]) {
1015 dev_err(up->dev, "Failed to allocate %s IRQ string\n",
1016 desc->desc);
1017 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Paul Mundt762c69e2008-12-16 18:55:26 +09001019
Paul Mundt9174fc82011-06-28 15:25:36 +09001020 ret = request_irq(irq, desc->handler, up->irqflags,
1021 port->irqstr[j], port);
1022 if (unlikely(ret)) {
1023 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1024 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 }
1027
1028 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001029
1030out_noirq:
1031 while (--i >= 0)
1032 free_irq(port->cfg->irqs[i], port);
1033
1034out_nomem:
1035 while (--j >= 0)
1036 kfree(port->irqstr[j]);
1037
1038 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static void sci_free_irq(struct sci_port *port)
1042{
1043 int i;
1044
Paul Mundt9174fc82011-06-28 15:25:36 +09001045 /*
1046 * Intentionally in reverse order so we iterate over the muxed
1047 * IRQ first.
1048 */
1049 for (i = 0; i < SCIx_NR_IRQS; i++) {
1050 free_irq(port->cfg->irqs[i], port);
1051 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Paul Mundt9174fc82011-06-28 15:25:36 +09001053 if (SCIx_IRQ_IS_MUXED(port)) {
1054 /* If there's only one IRQ, we're done. */
1055 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 }
1058}
1059
1060static unsigned int sci_tx_empty(struct uart_port *port)
1061{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +00001062 unsigned short status = sci_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001063 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001064
1065 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1069{
1070 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
1071 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
1072 /* If you have signals for DTR and DCD, please implement here. */
1073}
1074
1075static unsigned int sci_get_mctrl(struct uart_port *port)
1076{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001077 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 and CTS/RTS */
1079
1080 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
1081}
1082
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001083#ifdef CONFIG_SERIAL_SH_SCI_DMA
1084static void sci_dma_tx_complete(void *arg)
1085{
1086 struct sci_port *s = arg;
1087 struct uart_port *port = &s->port;
1088 struct circ_buf *xmit = &port->state->xmit;
1089 unsigned long flags;
1090
1091 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1092
1093 spin_lock_irqsave(&port->lock, flags);
1094
Magnus Dammf354a382010-03-19 04:47:01 +00001095 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001096 xmit->tail &= UART_XMIT_SIZE - 1;
1097
Magnus Dammf354a382010-03-19 04:47:01 +00001098 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001099
1100 async_tx_ack(s->desc_tx);
1101 s->cookie_tx = -EINVAL;
1102 s->desc_tx = NULL;
1103
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001104 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1105 uart_write_wakeup(port);
1106
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001107 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001108 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001109 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001110 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001111 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001112 }
1113
1114 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001115}
1116
1117/* Locking: called with port lock held */
1118static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
1119 size_t count)
1120{
1121 struct uart_port *port = &s->port;
1122 int i, active, room;
1123
1124 room = tty_buffer_request_room(tty, count);
1125
1126 if (s->active_rx == s->cookie_rx[0]) {
1127 active = 0;
1128 } else if (s->active_rx == s->cookie_rx[1]) {
1129 active = 1;
1130 } else {
1131 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1132 return 0;
1133 }
1134
1135 if (room < count)
1136 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1137 count - room);
1138 if (!room)
1139 return room;
1140
1141 for (i = 0; i < room; i++)
1142 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1143 TTY_NORMAL);
1144
1145 port->icount.rx += room;
1146
1147 return room;
1148}
1149
1150static void sci_dma_rx_complete(void *arg)
1151{
1152 struct sci_port *s = arg;
1153 struct uart_port *port = &s->port;
1154 struct tty_struct *tty = port->state->port.tty;
1155 unsigned long flags;
1156 int count;
1157
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001158 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001159
1160 spin_lock_irqsave(&port->lock, flags);
1161
1162 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1163
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001164 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001165
1166 spin_unlock_irqrestore(&port->lock, flags);
1167
1168 if (count)
1169 tty_flip_buffer_push(tty);
1170
1171 schedule_work(&s->work_rx);
1172}
1173
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001174static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1175{
1176 struct dma_chan *chan = s->chan_rx;
1177 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001178
1179 s->chan_rx = NULL;
1180 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1181 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001182 if (sg_dma_address(&s->sg_rx[0]))
1183 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1184 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001185 if (enable_pio)
1186 sci_start_rx(port);
1187}
1188
1189static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1190{
1191 struct dma_chan *chan = s->chan_tx;
1192 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001193
1194 s->chan_tx = NULL;
1195 s->cookie_tx = -EINVAL;
1196 dma_release_channel(chan);
1197 if (enable_pio)
1198 sci_start_tx(port);
1199}
1200
1201static void sci_submit_rx(struct sci_port *s)
1202{
1203 struct dma_chan *chan = s->chan_rx;
1204 int i;
1205
1206 for (i = 0; i < 2; i++) {
1207 struct scatterlist *sg = &s->sg_rx[i];
1208 struct dma_async_tx_descriptor *desc;
1209
1210 desc = chan->device->device_prep_slave_sg(chan,
1211 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1212
1213 if (desc) {
1214 s->desc_rx[i] = desc;
1215 desc->callback = sci_dma_rx_complete;
1216 desc->callback_param = s;
1217 s->cookie_rx[i] = desc->tx_submit(desc);
1218 }
1219
1220 if (!desc || s->cookie_rx[i] < 0) {
1221 if (i) {
1222 async_tx_ack(s->desc_rx[0]);
1223 s->cookie_rx[0] = -EINVAL;
1224 }
1225 if (desc) {
1226 async_tx_ack(desc);
1227 s->cookie_rx[i] = -EINVAL;
1228 }
1229 dev_warn(s->port.dev,
1230 "failed to re-start DMA, using PIO\n");
1231 sci_rx_dma_release(s, true);
1232 return;
1233 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001234 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1235 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001236 }
1237
1238 s->active_rx = s->cookie_rx[0];
1239
1240 dma_async_issue_pending(chan);
1241}
1242
1243static void work_fn_rx(struct work_struct *work)
1244{
1245 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1246 struct uart_port *port = &s->port;
1247 struct dma_async_tx_descriptor *desc;
1248 int new;
1249
1250 if (s->active_rx == s->cookie_rx[0]) {
1251 new = 0;
1252 } else if (s->active_rx == s->cookie_rx[1]) {
1253 new = 1;
1254 } else {
1255 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1256 return;
1257 }
1258 desc = s->desc_rx[new];
1259
1260 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1261 DMA_SUCCESS) {
1262 /* Handle incomplete DMA receive */
1263 struct tty_struct *tty = port->state->port.tty;
1264 struct dma_chan *chan = s->chan_rx;
1265 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1266 async_tx);
1267 unsigned long flags;
1268 int count;
1269
Linus Walleij05827632010-05-17 16:30:42 -07001270 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001271 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1272 sh_desc->partial, sh_desc->cookie);
1273
1274 spin_lock_irqsave(&port->lock, flags);
1275 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1276 spin_unlock_irqrestore(&port->lock, flags);
1277
1278 if (count)
1279 tty_flip_buffer_push(tty);
1280
1281 sci_submit_rx(s);
1282
1283 return;
1284 }
1285
1286 s->cookie_rx[new] = desc->tx_submit(desc);
1287 if (s->cookie_rx[new] < 0) {
1288 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1289 sci_rx_dma_release(s, true);
1290 return;
1291 }
1292
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001293 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001294
1295 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1296 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001297}
1298
1299static void work_fn_tx(struct work_struct *work)
1300{
1301 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1302 struct dma_async_tx_descriptor *desc;
1303 struct dma_chan *chan = s->chan_tx;
1304 struct uart_port *port = &s->port;
1305 struct circ_buf *xmit = &port->state->xmit;
1306 struct scatterlist *sg = &s->sg_tx;
1307
1308 /*
1309 * DMA is idle now.
1310 * Port xmit buffer is already mapped, and it is one page... Just adjust
1311 * offsets and lengths. Since it is a circular buffer, we have to
1312 * transmit till the end, and then the rest. Take the port lock to get a
1313 * consistent xmit buffer state.
1314 */
1315 spin_lock_irq(&port->lock);
1316 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001317 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001318 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001319 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001320 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001321 spin_unlock_irq(&port->lock);
1322
Magnus Dammf354a382010-03-19 04:47:01 +00001323 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001324
1325 desc = chan->device->device_prep_slave_sg(chan,
1326 sg, s->sg_len_tx, DMA_TO_DEVICE,
1327 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1328 if (!desc) {
1329 /* switch to PIO */
1330 sci_tx_dma_release(s, true);
1331 return;
1332 }
1333
1334 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1335
1336 spin_lock_irq(&port->lock);
1337 s->desc_tx = desc;
1338 desc->callback = sci_dma_tx_complete;
1339 desc->callback_param = s;
1340 spin_unlock_irq(&port->lock);
1341 s->cookie_tx = desc->tx_submit(desc);
1342 if (s->cookie_tx < 0) {
1343 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1344 /* switch to PIO */
1345 sci_tx_dma_release(s, true);
1346 return;
1347 }
1348
1349 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1350 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1351
1352 dma_async_issue_pending(chan);
1353}
1354#endif
1355
Russell Kingb129a8c2005-08-31 10:12:14 +01001356static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001358 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001359 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001361#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001362 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001363 u16 new, scr = sci_in(port, SCSCR);
1364 if (s->chan_tx)
1365 new = scr | 0x8000;
1366 else
1367 new = scr & ~0x8000;
1368 if (new != scr)
1369 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001370 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001371
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001372 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1373 s->cookie_tx < 0)
1374 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001375#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001376
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001377 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001378 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1379 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001380 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Russell Kingb129a8c2005-08-31 10:12:14 +01001384static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 unsigned short ctrl;
1387
1388 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001390
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001391 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001392 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001393
Paul Mundt8e698612009-06-24 19:44:32 +09001394 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001399static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 unsigned short ctrl;
1402
Paul Mundtf43dc232011-01-13 15:06:28 +09001403 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1404
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001405 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001406 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411static void sci_stop_rx(struct uart_port *port)
1412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 unsigned short ctrl;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001416
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001417 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001418 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001419
1420 ctrl &= ~port_rx_irq_mask(port);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static void sci_enable_ms(struct uart_port *port)
1426{
1427 /* Nothing here yet .. */
1428}
1429
1430static void sci_break_ctl(struct uart_port *port, int break_state)
1431{
1432 /* Nothing here yet .. */
1433}
1434
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001435#ifdef CONFIG_SERIAL_SH_SCI_DMA
1436static bool filter(struct dma_chan *chan, void *slave)
1437{
1438 struct sh_dmae_slave *param = slave;
1439
1440 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1441 param->slave_id);
1442
1443 if (param->dma_dev == chan->device->dev) {
1444 chan->private = param;
1445 return true;
1446 } else {
1447 return false;
1448 }
1449}
1450
1451static void rx_timer_fn(unsigned long arg)
1452{
1453 struct sci_port *s = (struct sci_port *)arg;
1454 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001455 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001456
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001457 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001458 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001459 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001460 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001461 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001462 dev_dbg(port->dev, "DMA Rx timed out\n");
1463 schedule_work(&s->work_rx);
1464}
1465
1466static void sci_request_dma(struct uart_port *port)
1467{
1468 struct sci_port *s = to_sci_port(port);
1469 struct sh_dmae_slave *param;
1470 struct dma_chan *chan;
1471 dma_cap_mask_t mask;
1472 int nent;
1473
1474 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001475 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001476
Paul Mundtce6738b2011-01-19 15:24:40 +09001477 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001478 return;
1479
1480 dma_cap_zero(mask);
1481 dma_cap_set(DMA_SLAVE, mask);
1482
1483 param = &s->param_tx;
1484
1485 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001486 param->slave_id = s->cfg->dma_slave_tx;
1487 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001488
1489 s->cookie_tx = -EINVAL;
1490 chan = dma_request_channel(mask, filter, param);
1491 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1492 if (chan) {
1493 s->chan_tx = chan;
1494 sg_init_table(&s->sg_tx, 1);
1495 /* UART circular tx buffer is an aligned page. */
1496 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1497 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1498 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1499 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1500 if (!nent)
1501 sci_tx_dma_release(s, false);
1502 else
1503 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1504 sg_dma_len(&s->sg_tx),
1505 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1506
1507 s->sg_len_tx = nent;
1508
1509 INIT_WORK(&s->work_tx, work_fn_tx);
1510 }
1511
1512 param = &s->param_rx;
1513
1514 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001515 param->slave_id = s->cfg->dma_slave_rx;
1516 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001517
1518 chan = dma_request_channel(mask, filter, param);
1519 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1520 if (chan) {
1521 dma_addr_t dma[2];
1522 void *buf[2];
1523 int i;
1524
1525 s->chan_rx = chan;
1526
1527 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1528 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1529 &dma[0], GFP_KERNEL);
1530
1531 if (!buf[0]) {
1532 dev_warn(port->dev,
1533 "failed to allocate dma buffer, using PIO\n");
1534 sci_rx_dma_release(s, true);
1535 return;
1536 }
1537
1538 buf[1] = buf[0] + s->buf_len_rx;
1539 dma[1] = dma[0] + s->buf_len_rx;
1540
1541 for (i = 0; i < 2; i++) {
1542 struct scatterlist *sg = &s->sg_rx[i];
1543
1544 sg_init_table(sg, 1);
1545 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1546 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001547 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001548 }
1549
1550 INIT_WORK(&s->work_rx, work_fn_rx);
1551 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1552
1553 sci_submit_rx(s);
1554 }
1555}
1556
1557static void sci_free_dma(struct uart_port *port)
1558{
1559 struct sci_port *s = to_sci_port(port);
1560
Paul Mundtce6738b2011-01-19 15:24:40 +09001561 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001562 return;
1563
1564 if (s->chan_tx)
1565 sci_tx_dma_release(s, false);
1566 if (s->chan_rx)
1567 sci_rx_dma_release(s, false);
1568}
Paul Mundt27bd1072011-01-19 15:37:31 +09001569#else
1570static inline void sci_request_dma(struct uart_port *port)
1571{
1572}
1573
1574static inline void sci_free_dma(struct uart_port *port)
1575{
1576}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001577#endif
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static int sci_startup(struct uart_port *port)
1580{
Magnus Damma5660ad2009-01-21 15:14:38 +00001581 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001582 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001584 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1585
Paul Mundt23241d42011-06-28 13:55:31 +09001586 sci_port_enable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Paul Mundt073e84c2011-01-19 17:30:53 +09001588 ret = sci_request_irq(s);
1589 if (unlikely(ret < 0))
1590 return ret;
1591
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001592 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001593
Yoshinori Satod6569012005-10-14 15:59:12 -07001594 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001595 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 return 0;
1598}
1599
1600static void sci_shutdown(struct uart_port *port)
1601{
Magnus Damma5660ad2009-01-21 15:14:38 +00001602 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001604 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001607 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001608
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001609 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 sci_free_irq(s);
1611
Paul Mundt23241d42011-06-28 13:55:31 +09001612 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Paul Mundt26c92f32009-06-24 18:23:52 +09001615static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1616 unsigned long freq)
1617{
1618 switch (algo_id) {
1619 case SCBRR_ALGO_1:
1620 return ((freq + 16 * bps) / (16 * bps) - 1);
1621 case SCBRR_ALGO_2:
1622 return ((freq + 16 * bps) / (32 * bps) - 1);
1623 case SCBRR_ALGO_3:
1624 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1625 case SCBRR_ALGO_4:
1626 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1627 case SCBRR_ALGO_5:
1628 return (((freq * 1000 / 32) / bps) - 1);
1629 }
1630
1631 /* Warn, but use a safe default */
1632 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001633
Paul Mundt26c92f32009-06-24 18:23:52 +09001634 return ((freq + 16 * bps) / (32 * bps) - 1);
1635}
1636
Alan Cox606d0992006-12-08 02:38:45 -08001637static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1638 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Paul Mundt00b9de92009-06-24 17:53:33 +09001640 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001641 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001642 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001643 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Magnus Damm154280f2009-12-22 03:37:28 +00001645 /*
1646 * earlyprintk comes here early on with port->uartclk set to zero.
1647 * the clock framework is not up and running at this point so here
1648 * we assume that 115200 is the maximum baud rate. please note that
1649 * the baud rate is not programmed during earlyprintk - it is assumed
1650 * that the previous boot loader has enabled required clocks and
1651 * setup the baud rate generator hardware for us already.
1652 */
1653 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1654
1655 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1656 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001657 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Paul Mundt23241d42011-06-28 13:55:31 +09001659 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00001660
Paul Mundte108b2c2006-09-27 16:32:13 +09001661 do {
1662 status = sci_in(port, SCxSR);
1663 } while (!(status & SCxSR_TEND(port)));
1664
1665 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1666
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001667 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001668 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001669
1670 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001671
Paul Mundte108b2c2006-09-27 16:32:13 +09001672 if ((termios->c_cflag & CSIZE) == CS7)
1673 smr_val |= 0x40;
1674 if (termios->c_cflag & PARENB)
1675 smr_val |= 0x20;
1676 if (termios->c_cflag & PARODD)
1677 smr_val |= 0x30;
1678 if (termios->c_cflag & CSTOPB)
1679 smr_val |= 0x08;
1680
1681 uart_update_timeout(port, termios->c_cflag, baud);
1682
1683 sci_out(port, SCSMR, smr_val);
1684
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001685 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001686 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001689 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1691 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001692 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 sci_out(port, SCBRR, t);
1696 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1697 }
1698
Paul Mundtd5701642008-12-16 20:07:27 +09001699 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001700 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001701
Paul Mundtce6738b2011-01-19 15:24:40 +09001702 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001704#ifdef CONFIG_SERIAL_SH_SCI_DMA
1705 /*
1706 * Calculate delay for 1.5 DMA buffers: see
1707 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1708 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1709 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1710 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1711 * sizes), but it has been found out experimentally, that this is not
1712 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1713 * as a minimum seem to work perfectly.
1714 */
1715 if (s->chan_rx) {
1716 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1717 port->fifosize / 2;
1718 dev_dbg(port->dev,
1719 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1720 s->rx_timeout * 1000 / HZ, port->timeout);
1721 if (s->rx_timeout < msecs_to_jiffies(20))
1722 s->rx_timeout = msecs_to_jiffies(20);
1723 }
1724#endif
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001727 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001728
Paul Mundt23241d42011-06-28 13:55:31 +09001729 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732static const char *sci_type(struct uart_port *port)
1733{
1734 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001735 case PORT_IRDA:
1736 return "irda";
1737 case PORT_SCI:
1738 return "sci";
1739 case PORT_SCIF:
1740 return "scif";
1741 case PORT_SCIFA:
1742 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001743 case PORT_SCIFB:
1744 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
Paul Mundtfa439722008-09-04 18:53:58 +09001747 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Paul Mundte2651642011-01-20 21:24:03 +09001750static inline unsigned long sci_port_size(struct uart_port *port)
1751{
1752 /*
1753 * Pick an arbitrary size that encapsulates all of the base
1754 * registers by default. This can be optimized later, or derived
1755 * from platform resource data at such a time that ports begin to
1756 * behave more erratically.
1757 */
1758 return 64;
1759}
1760
Paul Mundtf6e94952011-01-21 15:25:36 +09001761static int sci_remap_port(struct uart_port *port)
1762{
1763 unsigned long size = sci_port_size(port);
1764
1765 /*
1766 * Nothing to do if there's already an established membase.
1767 */
1768 if (port->membase)
1769 return 0;
1770
1771 if (port->flags & UPF_IOREMAP) {
1772 port->membase = ioremap_nocache(port->mapbase, size);
1773 if (unlikely(!port->membase)) {
1774 dev_err(port->dev, "can't remap port#%d\n", port->line);
1775 return -ENXIO;
1776 }
1777 } else {
1778 /*
1779 * For the simple (and majority of) cases where we don't
1780 * need to do any remapping, just cast the cookie
1781 * directly.
1782 */
1783 port->membase = (void __iomem *)port->mapbase;
1784 }
1785
1786 return 0;
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void sci_release_port(struct uart_port *port)
1790{
Paul Mundte2651642011-01-20 21:24:03 +09001791 if (port->flags & UPF_IOREMAP) {
1792 iounmap(port->membase);
1793 port->membase = NULL;
1794 }
1795
1796 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799static int sci_request_port(struct uart_port *port)
1800{
Paul Mundte2651642011-01-20 21:24:03 +09001801 unsigned long size = sci_port_size(port);
1802 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001803 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Paul Mundt10205202011-01-21 16:00:31 +09001805 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001806 if (unlikely(res == NULL))
1807 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Paul Mundtf6e94952011-01-21 15:25:36 +09001809 ret = sci_remap_port(port);
1810 if (unlikely(ret != 0)) {
1811 release_resource(res);
1812 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001813 }
Paul Mundte2651642011-01-20 21:24:03 +09001814
1815 return 0;
1816}
1817
1818static void sci_config_port(struct uart_port *port, int flags)
1819{
1820 if (flags & UART_CONFIG_TYPE) {
1821 struct sci_port *sport = to_sci_port(port);
1822
1823 port->type = sport->cfg->type;
1824 sci_request_port(port);
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1829{
Magnus Damma5660ad2009-01-21 15:14:38 +00001830 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Paul Mundtce6738b2011-01-19 15:24:40 +09001832 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EINVAL;
1834 if (ser->baud_base < 2400)
1835 /* No paper tape reader for Mitch.. */
1836 return -EINVAL;
1837
1838 return 0;
1839}
1840
1841static struct uart_ops sci_uart_ops = {
1842 .tx_empty = sci_tx_empty,
1843 .set_mctrl = sci_set_mctrl,
1844 .get_mctrl = sci_get_mctrl,
1845 .start_tx = sci_start_tx,
1846 .stop_tx = sci_stop_tx,
1847 .stop_rx = sci_stop_rx,
1848 .enable_ms = sci_enable_ms,
1849 .break_ctl = sci_break_ctl,
1850 .startup = sci_startup,
1851 .shutdown = sci_shutdown,
1852 .set_termios = sci_set_termios,
1853 .type = sci_type,
1854 .release_port = sci_release_port,
1855 .request_port = sci_request_port,
1856 .config_port = sci_config_port,
1857 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001858#ifdef CONFIG_CONSOLE_POLL
1859 .poll_get_char = sci_poll_get_char,
1860 .poll_put_char = sci_poll_put_char,
1861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862};
1863
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001864static int __devinit sci_init_single(struct platform_device *dev,
1865 struct sci_port *sci_port,
1866 unsigned int index,
1867 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001868{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001869 struct uart_port *port = &sci_port->port;
Paul Mundt3127c6b2011-06-28 13:44:37 +09001870 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001871
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001872 port->ops = &sci_uart_ops;
1873 port->iotype = UPIO_MEM;
1874 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001875
1876 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001877 case PORT_SCIFB:
1878 port->fifosize = 256;
1879 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001880 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001881 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001882 break;
1883 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001884 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001885 break;
1886 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001887 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001888 break;
1889 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001890
Paul Mundt3127c6b2011-06-28 13:44:37 +09001891 if (p->regtype == SCIx_PROBE_REGTYPE) {
1892 ret = sci_probe_regmap(p);
Paul Mundtad75b882011-08-03 12:33:20 +09001893 if (unlikely(ret != 0))
Paul Mundt3127c6b2011-06-28 13:44:37 +09001894 return ret;
1895 }
Paul Mundt61a69762011-06-14 12:40:19 +09001896
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001897 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001898 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1899 if (IS_ERR(sci_port->iclk)) {
1900 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1901 if (IS_ERR(sci_port->iclk)) {
1902 dev_err(&dev->dev, "can't get iclk\n");
1903 return PTR_ERR(sci_port->iclk);
1904 }
1905 }
1906
1907 /*
1908 * The function clock is optional, ignore it if we can't
1909 * find it.
1910 */
1911 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1912 if (IS_ERR(sci_port->fclk))
1913 sci_port->fclk = NULL;
1914
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001915 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001916
1917 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001918 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001919
Magnus Damm7ed7e072009-01-21 15:14:14 +00001920 sci_port->break_timer.data = (unsigned long)sci_port;
1921 sci_port->break_timer.function = sci_break_timer;
1922 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001923
Paul Mundtdebf9502011-06-08 18:19:37 +09001924 /*
1925 * Establish some sensible defaults for the error detection.
1926 */
1927 if (!p->error_mask)
1928 p->error_mask = (p->type == PORT_SCI) ?
1929 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1930
1931 /*
1932 * Establish sensible defaults for the overrun detection, unless
1933 * the part has explicitly disabled support for it.
1934 */
1935 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1936 if (p->type == PORT_SCI)
1937 p->overrun_bit = 5;
1938 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1939 p->overrun_bit = 9;
1940 else
1941 p->overrun_bit = 0;
1942
1943 /*
1944 * Make the error mask inclusive of overrun detection, if
1945 * supported.
1946 */
1947 p->error_mask |= (1 << p->overrun_bit);
1948 }
1949
Paul Mundtce6738b2011-01-19 15:24:40 +09001950 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001951
Paul Mundtce6738b2011-01-19 15:24:40 +09001952 port->mapbase = p->mapbase;
1953 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001954 port->flags = p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09001955 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09001956
1957 /*
Paul Mundt61a69762011-06-14 12:40:19 +09001958 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09001959 * for the multi-IRQ ports, which is where we are primarily
1960 * concerned with the shutdown path synchronization.
1961 *
1962 * For the muxed case there's nothing more to do.
1963 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00001964 port->irq = p->irqs[SCIx_RXI_IRQ];
Paul Mundt9174fc82011-06-28 15:25:36 +09001965 port->irqflags = IRQF_DISABLED;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001966
Paul Mundt61a69762011-06-14 12:40:19 +09001967 port->serial_in = sci_serial_in;
1968 port->serial_out = sci_serial_out;
1969
Paul Mundtce6738b2011-01-19 15:24:40 +09001970 if (p->dma_dev)
1971 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1972 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001973
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001974 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001978static void serial_console_putchar(struct uart_port *port, int ch)
1979{
1980 sci_poll_put_char(port, ch);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
1984 * Print a string to the serial port trying not to disturb
1985 * any possible real use of the port...
1986 */
1987static void serial_console_write(struct console *co, const char *s,
1988 unsigned count)
1989{
Paul Mundt906b17d2011-01-21 16:19:53 +09001990 struct sci_port *sci_port = &sci_ports[co->index];
1991 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001992 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001993
Paul Mundt23241d42011-06-28 13:55:31 +09001994 sci_port_enable(sci_port);
Magnus Damm501b8252009-01-21 15:14:30 +00001995
1996 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001997
1998 /* wait until fifo is empty and last bit has been transmitted */
1999 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
2000 while ((sci_in(port, SCxSR) & bits) != bits)
2001 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00002002
Paul Mundt23241d42011-06-28 13:55:31 +09002003 sci_port_disable(sci_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002006static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002008 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct uart_port *port;
2010 int baud = 115200;
2011 int bits = 8;
2012 int parity = 'n';
2013 int flow = 'n';
2014 int ret;
2015
Paul Mundte108b2c2006-09-27 16:32:13 +09002016 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002017 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002018 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002019 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002020 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002021
Paul Mundt906b17d2011-01-21 16:19:53 +09002022 sci_port = &sci_ports[co->index];
2023 port = &sci_port->port;
2024
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002025 /*
2026 * Refuse to handle uninitialized ports.
2027 */
2028 if (!port->ops)
2029 return -ENODEV;
2030
Paul Mundtf6e94952011-01-21 15:25:36 +09002031 ret = sci_remap_port(port);
2032 if (unlikely(ret != 0))
2033 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002034
Paul Mundt23241d42011-06-28 13:55:31 +09002035 sci_port_enable(sci_port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (options)
2038 uart_parse_options(options, &baud, &parity, &bits, &flow);
2039
Magnus Damm501b8252009-01-21 15:14:30 +00002040 /* TODO: disable clock */
Paul Mundtab7cfb52011-06-01 14:47:42 +09002041 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
2044static struct console serial_console = {
2045 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002046 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 .write = serial_console_write,
2048 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002049 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002051 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052};
2053
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002054static struct console early_serial_console = {
2055 .name = "early_ttySC",
2056 .write = serial_console_write,
2057 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002058 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002059};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002060
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002061static char early_serial_buf[32];
2062
Paul Mundtecdf8a42011-01-21 00:05:48 +09002063static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2064{
2065 struct plat_sci_port *cfg = pdev->dev.platform_data;
2066
2067 if (early_serial_console.data)
2068 return -EEXIST;
2069
2070 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002071
Paul Mundt906b17d2011-01-21 16:19:53 +09002072 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002073
2074 serial_console_setup(&early_serial_console, early_serial_buf);
2075
2076 if (!strstr(early_serial_buf, "keep"))
2077 early_serial_console.flags |= CON_BOOT;
2078
2079 register_console(&early_serial_console);
2080 return 0;
2081}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002082
2083#define SCI_CONSOLE (&serial_console)
2084
Paul Mundtecdf8a42011-01-21 00:05:48 +09002085#else
2086static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2087{
2088 return -EINVAL;
2089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002091#define SCI_CONSOLE NULL
2092
2093#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095static char banner[] __initdata =
2096 KERN_INFO "SuperH SCI(F) driver initialized\n";
2097
2098static struct uart_driver sci_uart_driver = {
2099 .owner = THIS_MODULE,
2100 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 .dev_name = "ttySC",
2102 .major = SCI_MAJOR,
2103 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002104 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .cons = SCI_CONSOLE,
2106};
2107
Paul Mundt54507f62009-05-08 23:48:33 +09002108static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002109{
Paul Mundtd535a232011-01-19 17:19:35 +09002110 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002111
Paul Mundtd535a232011-01-19 17:19:35 +09002112 cpufreq_unregister_notifier(&port->freq_transition,
2113 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002114
Paul Mundtd535a232011-01-19 17:19:35 +09002115 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002116
Paul Mundtd535a232011-01-19 17:19:35 +09002117 clk_put(port->iclk);
2118 clk_put(port->fclk);
2119
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002120 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00002121 return 0;
2122}
2123
Magnus Damm0ee70712009-01-21 15:13:50 +00002124static int __devinit sci_probe_single(struct platform_device *dev,
2125 unsigned int index,
2126 struct plat_sci_port *p,
2127 struct sci_port *sciport)
2128{
Magnus Damm0ee70712009-01-21 15:13:50 +00002129 int ret;
2130
2131 /* Sanity check */
2132 if (unlikely(index >= SCI_NPORTS)) {
2133 dev_notice(&dev->dev, "Attempting to register port "
2134 "%d when only %d are available.\n",
2135 index+1, SCI_NPORTS);
2136 dev_notice(&dev->dev, "Consider bumping "
2137 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
2138 return 0;
2139 }
2140
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002141 ret = sci_init_single(dev, sciport, index, p);
2142 if (ret)
2143 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002144
Paul Mundtd535a232011-01-19 17:19:35 +09002145 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00002146}
2147
Paul Mundte108b2c2006-09-27 16:32:13 +09002148static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Paul Mundte108b2c2006-09-27 16:32:13 +09002150 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09002151 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002152 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002153
Paul Mundtecdf8a42011-01-21 00:05:48 +09002154 /*
2155 * If we've come here via earlyprintk initialization, head off to
2156 * the special early probe. We don't have sufficient device state
2157 * to make it beyond this yet.
2158 */
2159 if (is_early_platform_device(dev))
2160 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002161
Paul Mundtd535a232011-01-19 17:19:35 +09002162 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002163
Paul Mundt906b17d2011-01-21 16:19:53 +09002164 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002165 if (ret)
2166 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00002167
Paul Mundtd535a232011-01-19 17:19:35 +09002168 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Paul Mundtd535a232011-01-19 17:19:35 +09002170 ret = cpufreq_register_notifier(&sp->freq_transition,
2171 CPUFREQ_TRANSITION_NOTIFIER);
2172 if (unlikely(ret < 0))
2173 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175#ifdef CONFIG_SH_STANDARD_BIOS
2176 sh_bios_gdb_detach();
2177#endif
2178
Paul Mundte108b2c2006-09-27 16:32:13 +09002179 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002180
2181err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002182 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002183 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002184}
2185
Paul Mundt6daa79b2009-06-15 07:07:38 +09002186static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002187{
Paul Mundtd535a232011-01-19 17:19:35 +09002188 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002189
Paul Mundtd535a232011-01-19 17:19:35 +09002190 if (sport)
2191 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002192
2193 return 0;
2194}
2195
Paul Mundt6daa79b2009-06-15 07:07:38 +09002196static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002197{
Paul Mundtd535a232011-01-19 17:19:35 +09002198 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002199
Paul Mundtd535a232011-01-19 17:19:35 +09002200 if (sport)
2201 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002202
2203 return 0;
2204}
2205
Alexey Dobriyan47145212009-12-14 18:00:08 -08002206static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002207 .suspend = sci_suspend,
2208 .resume = sci_resume,
2209};
2210
Paul Mundte108b2c2006-09-27 16:32:13 +09002211static struct platform_driver sci_driver = {
2212 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002213 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002214 .driver = {
2215 .name = "sh-sci",
2216 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002217 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002218 },
2219};
2220
2221static int __init sci_init(void)
2222{
2223 int ret;
2224
2225 printk(banner);
2226
Paul Mundte108b2c2006-09-27 16:32:13 +09002227 ret = uart_register_driver(&sci_uart_driver);
2228 if (likely(ret == 0)) {
2229 ret = platform_driver_register(&sci_driver);
2230 if (unlikely(ret))
2231 uart_unregister_driver(&sci_uart_driver);
2232 }
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return ret;
2235}
2236
2237static void __exit sci_exit(void)
2238{
Paul Mundte108b2c2006-09-27 16:32:13 +09002239 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 uart_unregister_driver(&sci_uart_driver);
2241}
2242
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002243#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2244early_platform_init_buffer("earlyprintk", &sci_driver,
2245 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247module_init(sci_init);
2248module_exit(sci_exit);
2249
Paul Mundte108b2c2006-09-27 16:32:13 +09002250MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002251MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09002252MODULE_AUTHOR("Paul Mundt");
2253MODULE_DESCRIPTION("SuperH SCI(F) serial driver");