blob: 8e55e0a2733a6fdc749d8c8ef0b9a413e507316d [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>
50#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090052
53#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundtce6738b2011-01-19 15:24:40 +090062 /* Platform configuration */
63 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090064
Paul Mundte108b2c2006-09-27 16:32:13 +090065 /* Port enable callback */
66 void (*enable)(struct uart_port *port);
67
68 /* Port disable callback */
69 void (*disable)(struct uart_port *port);
70
71 /* Break timer */
72 struct timer_list break_timer;
73 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090074
Magnus Damm501b8252009-01-21 15:14:30 +000075 /* Interface clock */
76 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090077 /* Function clock */
78 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090079
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090080 struct dma_chan *chan_tx;
81 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090082
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090083#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090084 struct dma_async_tx_descriptor *desc_tx;
85 struct dma_async_tx_descriptor *desc_rx[2];
86 dma_cookie_t cookie_tx;
87 dma_cookie_t cookie_rx[2];
88 dma_cookie_t active_rx;
89 struct scatterlist sg_tx;
90 unsigned int sg_len_tx;
91 struct scatterlist sg_rx[2];
92 size_t buf_len_rx;
93 struct sh_dmae_slave param_tx;
94 struct sh_dmae_slave param_rx;
95 struct work_struct work_tx;
96 struct work_struct work_rx;
97 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +000098 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090099#endif
Magnus Damme552de22009-01-21 15:13:42 +0000100
Paul Mundtd535a232011-01-19 17:19:35 +0900101 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900105static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100106static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900107static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Paul Mundte108b2c2006-09-27 16:32:13 +0900109#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
110
111static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct uart_driver sci_uart_driver;
113
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900114static inline struct sci_port *
115to_sci_port(struct uart_port *uart)
116{
117 return container_of(uart, struct sci_port, port);
118}
119
Paul Mundt61a69762011-06-14 12:40:19 +0900120struct plat_sci_reg {
121 u8 offset, size;
122};
123
124/* Helper for invalidating specific entries of an inherited map. */
125#define sci_reg_invalid { .offset = 0, .size = 0 }
126
127static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
128 [SCIx_PROBE_REGTYPE] = {
129 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
130 },
131
132 /*
133 * Common SCI definitions, dependent on the port's regshift
134 * value.
135 */
136 [SCIx_SCI_REGTYPE] = {
137 [SCSMR] = { 0x00, 8 },
138 [SCBRR] = { 0x01, 8 },
139 [SCSCR] = { 0x02, 8 },
140 [SCxTDR] = { 0x03, 8 },
141 [SCxSR] = { 0x04, 8 },
142 [SCxRDR] = { 0x05, 8 },
143 [SCFCR] = sci_reg_invalid,
144 [SCFDR] = sci_reg_invalid,
145 [SCTFDR] = sci_reg_invalid,
146 [SCRFDR] = sci_reg_invalid,
147 [SCSPTR] = sci_reg_invalid,
148 [SCLSR] = sci_reg_invalid,
149 },
150
151 /*
152 * Common definitions for legacy IrDA ports, dependent on
153 * regshift value.
154 */
155 [SCIx_IRDA_REGTYPE] = {
156 [SCSMR] = { 0x00, 8 },
157 [SCBRR] = { 0x01, 8 },
158 [SCSCR] = { 0x02, 8 },
159 [SCxTDR] = { 0x03, 8 },
160 [SCxSR] = { 0x04, 8 },
161 [SCxRDR] = { 0x05, 8 },
162 [SCFCR] = { 0x06, 8 },
163 [SCFDR] = { 0x07, 16 },
164 [SCTFDR] = sci_reg_invalid,
165 [SCRFDR] = sci_reg_invalid,
166 [SCSPTR] = sci_reg_invalid,
167 [SCLSR] = sci_reg_invalid,
168 },
169
170 /*
171 * Common SCIFA definitions.
172 */
173 [SCIx_SCIFA_REGTYPE] = {
174 [SCSMR] = { 0x00, 16 },
175 [SCBRR] = { 0x04, 8 },
176 [SCSCR] = { 0x08, 16 },
177 [SCxTDR] = { 0x20, 8 },
178 [SCxSR] = { 0x14, 16 },
179 [SCxRDR] = { 0x24, 8 },
180 [SCFCR] = { 0x18, 16 },
181 [SCFDR] = { 0x1c, 16 },
182 [SCTFDR] = sci_reg_invalid,
183 [SCRFDR] = sci_reg_invalid,
184 [SCSPTR] = sci_reg_invalid,
185 [SCLSR] = sci_reg_invalid,
186 },
187
188 /*
189 * Common SCIFB definitions.
190 */
191 [SCIx_SCIFB_REGTYPE] = {
192 [SCSMR] = { 0x00, 16 },
193 [SCBRR] = { 0x04, 8 },
194 [SCSCR] = { 0x08, 16 },
195 [SCxTDR] = { 0x40, 8 },
196 [SCxSR] = { 0x14, 16 },
197 [SCxRDR] = { 0x60, 8 },
198 [SCFCR] = { 0x18, 16 },
199 [SCFDR] = { 0x1c, 16 },
200 [SCTFDR] = sci_reg_invalid,
201 [SCRFDR] = sci_reg_invalid,
202 [SCSPTR] = sci_reg_invalid,
203 [SCLSR] = sci_reg_invalid,
204 },
205
206 /*
207 * Common SH-3 SCIF definitions.
208 */
209 [SCIx_SH3_SCIF_REGTYPE] = {
210 [SCSMR] = { 0x00, 8 },
211 [SCBRR] = { 0x02, 8 },
212 [SCSCR] = { 0x04, 8 },
213 [SCxTDR] = { 0x06, 8 },
214 [SCxSR] = { 0x08, 16 },
215 [SCxRDR] = { 0x0a, 8 },
216 [SCFCR] = { 0x0c, 8 },
217 [SCFDR] = { 0x0e, 16 },
218 [SCTFDR] = sci_reg_invalid,
219 [SCRFDR] = sci_reg_invalid,
220 [SCSPTR] = sci_reg_invalid,
221 [SCLSR] = sci_reg_invalid,
222 },
223
224 /*
225 * Common SH-4(A) SCIF(B) definitions.
226 */
227 [SCIx_SH4_SCIF_REGTYPE] = {
228 [SCSMR] = { 0x00, 16 },
229 [SCBRR] = { 0x04, 8 },
230 [SCSCR] = { 0x08, 16 },
231 [SCxTDR] = { 0x0c, 8 },
232 [SCxSR] = { 0x10, 16 },
233 [SCxRDR] = { 0x14, 8 },
234 [SCFCR] = { 0x18, 16 },
235 [SCFDR] = { 0x1c, 16 },
236 [SCTFDR] = sci_reg_invalid,
237 [SCRFDR] = sci_reg_invalid,
238 [SCSPTR] = { 0x20, 16 },
239 [SCLSR] = { 0x24, 16 },
240 },
241
242 /*
243 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
244 * register.
245 */
246 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
247 [SCSMR] = { 0x00, 16 },
248 [SCBRR] = { 0x04, 8 },
249 [SCSCR] = { 0x08, 16 },
250 [SCxTDR] = { 0x0c, 8 },
251 [SCxSR] = { 0x10, 16 },
252 [SCxRDR] = { 0x14, 8 },
253 [SCFCR] = { 0x18, 16 },
254 [SCFDR] = { 0x1c, 16 },
255 [SCTFDR] = sci_reg_invalid,
256 [SCRFDR] = sci_reg_invalid,
257 [SCSPTR] = sci_reg_invalid,
258 [SCLSR] = { 0x24, 16 },
259 },
260
261 /*
262 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
263 * count registers.
264 */
265 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
266 [SCSMR] = { 0x00, 16 },
267 [SCBRR] = { 0x04, 8 },
268 [SCSCR] = { 0x08, 16 },
269 [SCxTDR] = { 0x0c, 8 },
270 [SCxSR] = { 0x10, 16 },
271 [SCxRDR] = { 0x14, 8 },
272 [SCFCR] = { 0x18, 16 },
273 [SCFDR] = { 0x1c, 16 },
274 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
275 [SCRFDR] = { 0x20, 16 },
276 [SCSPTR] = { 0x24, 16 },
277 [SCLSR] = { 0x28, 16 },
278 },
279
280 /*
281 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
282 * registers.
283 */
284 [SCIx_SH7705_SCIF_REGTYPE] = {
285 [SCSMR] = { 0x00, 16 },
286 [SCBRR] = { 0x04, 8 },
287 [SCSCR] = { 0x08, 16 },
288 [SCxTDR] = { 0x20, 8 },
289 [SCxSR] = { 0x14, 16 },
290 [SCxRDR] = { 0x24, 8 },
291 [SCFCR] = { 0x18, 16 },
292 [SCFDR] = { 0x1c, 16 },
293 [SCTFDR] = sci_reg_invalid,
294 [SCRFDR] = sci_reg_invalid,
295 [SCSPTR] = sci_reg_invalid,
296 [SCLSR] = sci_reg_invalid,
297 },
298};
299
Paul Mundt72b294c2011-06-14 17:38:19 +0900300#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
301
Paul Mundt61a69762011-06-14 12:40:19 +0900302/*
303 * The "offset" here is rather misleading, in that it refers to an enum
304 * value relative to the port mapping rather than the fixed offset
305 * itself, which needs to be manually retrieved from the platform's
306 * register map for the given port.
307 */
308static unsigned int sci_serial_in(struct uart_port *p, int offset)
309{
Paul Mundt72b294c2011-06-14 17:38:19 +0900310 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900311
312 if (reg->size == 8)
313 return ioread8(p->membase + (reg->offset << p->regshift));
314 else if (reg->size == 16)
315 return ioread16(p->membase + (reg->offset << p->regshift));
316 else
317 WARN(1, "Invalid register access\n");
318
319 return 0;
320}
321
322static void sci_serial_out(struct uart_port *p, int offset, int value)
323{
Paul Mundt72b294c2011-06-14 17:38:19 +0900324 struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900325
326 if (reg->size == 8)
327 iowrite8(value, p->membase + (reg->offset << p->regshift));
328 else if (reg->size == 16)
329 iowrite16(value, p->membase + (reg->offset << p->regshift));
330 else
331 WARN(1, "Invalid register access\n");
332}
333
334#define sci_in(up, offset) (up->serial_in(up, offset))
335#define sci_out(up, offset, value) (up->serial_out(up, offset, value))
336
337static int sci_probe_regmap(struct plat_sci_port *cfg)
338{
339 switch (cfg->type) {
340 case PORT_SCI:
341 cfg->regtype = SCIx_SCI_REGTYPE;
342 break;
343 case PORT_IRDA:
344 cfg->regtype = SCIx_IRDA_REGTYPE;
345 break;
346 case PORT_SCIFA:
347 cfg->regtype = SCIx_SCIFA_REGTYPE;
348 break;
349 case PORT_SCIFB:
350 cfg->regtype = SCIx_SCIFB_REGTYPE;
351 break;
352 case PORT_SCIF:
353 /*
354 * The SH-4 is a bit of a misnomer here, although that's
355 * where this particular port layout originated. This
356 * configuration (or some slight variation thereof)
357 * remains the dominant model for all SCIFs.
358 */
359 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
360 break;
361 default:
362 printk(KERN_ERR "Can't probe register map for given port\n");
363 return -EINVAL;
364 }
365
366 return 0;
367}
368
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900369#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900370
371#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900372static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned short status;
375 int c;
376
Paul Mundte108b2c2006-09-27 16:32:13 +0900377 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 status = sci_in(port, SCxSR);
379 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900380 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 continue;
382 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500383 break;
384 } while (1);
385
386 if (!(status & SCxSR_RDxF(port)))
387 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900390
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900391 /* Dummy read */
392 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return c;
396}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900399static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned short status;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 do {
404 status = sci_in(port, SCxSR);
405 } while (!(status & SCxSR_TDxE(port)));
406
Paul Mundt272966c2008-11-13 17:46:06 +0900407 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000408 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900410#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Paul Mundt61a69762011-06-14 12:40:19 +0900412static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900413{
Paul Mundt61a69762011-06-14 12:40:19 +0900414 struct sci_port *s = to_sci_port(port);
415 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900416
Paul Mundt61a69762011-06-14 12:40:19 +0900417 /*
418 * Use port-specific handler if provided.
419 */
420 if (s->cfg->ops && s->cfg->ops->init_pins) {
421 s->cfg->ops->init_pins(port, cflag);
422 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Paul Mundt61a69762011-06-14 12:40:19 +0900425 /*
426 * For the generic path SCSPTR is necessary. Bail out if that's
427 * unavailable, too.
428 */
429 if (!reg->size)
430 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800431
Paul Mundtd5701642008-12-16 20:07:27 +0900432 if (!(cflag & CRTSCTS))
Paul Mundt61a69762011-06-14 12:40:19 +0900433 sci_out(port, SCSPTR, 0x0080); /* Set RTS = 1 */
Paul Mundtd5701642008-12-16 20:07:27 +0900434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900436static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900437{
Paul Mundt72b294c2011-06-14 17:38:19 +0900438 struct plat_sci_reg *reg;
439
440 reg = sci_getreg(port, SCTFDR);
441 if (reg->size)
442 return sci_in(port, SCTFDR) & 0xff;
443
444 reg = sci_getreg(port, SCFDR);
445 if (reg->size)
446 return sci_in(port, SCFDR) >> 8;
447
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900448 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900449}
450
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900451static int sci_txroom(struct uart_port *port)
452{
Paul Mundt72b294c2011-06-14 17:38:19 +0900453 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900454}
455
456static int sci_rxfill(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, SCRFDR);
461 if (reg->size)
462 return sci_in(port, SCRFDR) & 0xff;
463
464 reg = sci_getreg(port, SCFDR);
465 if (reg->size)
466 return sci_in(port, SCFDR) & ((port->fifosize << 1) - 1);
467
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900468 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900469}
470
Paul Mundt514820e2011-06-08 18:51:32 +0900471/*
472 * SCI helper for checking the state of the muxed port/RXD pins.
473 */
474static inline int sci_rxd_in(struct uart_port *port)
475{
476 struct sci_port *s = to_sci_port(port);
477
478 if (s->cfg->port_reg <= 0)
479 return 1;
480
481 return !!__raw_readb(s->cfg->port_reg);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* ********************************************************************** *
485 * the interrupt related routines *
486 * ********************************************************************** */
487
488static void sci_transmit_chars(struct uart_port *port)
489{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700490 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned short status;
493 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900494 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 status = sci_in(port, SCxSR);
497 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900499 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900500 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900501 else
Paul Mundt8e698612009-06-24 19:44:32 +0900502 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return;
505 }
506
Paul Mundt72b294c2011-06-14 17:38:19 +0900507 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 do {
510 unsigned char c;
511
512 if (port->x_char) {
513 c = port->x_char;
514 port->x_char = 0;
515 } else if (!uart_circ_empty(xmit) && !stopped) {
516 c = xmit->buf[xmit->tail];
517 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
518 } else {
519 break;
520 }
521
522 sci_out(port, SCxTDR, c);
523
524 port->icount.tx++;
525 } while (--count > 0);
526
527 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
528
529 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
530 uart_write_wakeup(port);
531 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100532 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ctrl = sci_in(port, SCSCR);
535
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900536 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sci_in(port, SCxSR); /* Dummy read */
538 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Paul Mundt8e698612009-06-24 19:44:32 +0900541 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544}
545
546/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900547#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900549static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900551 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700552 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int i, count, copied = 0;
554 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800555 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 status = sci_in(port, SCxSR);
558 if (!(status & SCxSR_RDxF(port)))
559 return;
560
561 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Don't copy more bytes than there is room for in the buffer */
Paul Mundt72b294c2011-06-14 17:38:19 +0900563 count = tty_buffer_request_room(tty, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* If for any reason we can't copy more data, we're done! */
566 if (count == 0)
567 break;
568
569 if (port->type == PORT_SCI) {
570 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900571 if (uart_handle_sysrq_char(port, c) ||
572 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900574 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900575 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900577 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 char c = sci_in(port, SCxRDR);
579 status = sci_in(port, SCxSR);
580#if defined(CONFIG_CPU_SH3)
581 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900582 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if ((c == 0) &&
584 (status & SCxSR_FER(port))) {
585 count--; i--;
586 continue;
587 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900590 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900591 sci_port->break_flag = 0;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (STEPFN(c)) {
594 count--; i--;
595 continue;
596 }
597 }
598#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100599 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 count--; i--;
601 continue;
602 }
603
604 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900605 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800606 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900607 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900608 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800609 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900610 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800611 } else
612 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900613
Alan Cox33f0f882006-01-09 20:54:13 -0800614 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 }
617
618 sci_in(port, SCxSR); /* dummy read */
619 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 copied += count;
622 port->icount.rx += count;
623 }
624
625 if (copied) {
626 /* Tell the rest of the system the news. New characters! */
627 tty_flip_buffer_push(tty);
628 } else {
629 sci_in(port, SCxSR); /* dummy read */
630 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
631 }
632}
633
634#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900635
636/*
637 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * 1 per millisecond or so during the break period, for 9600 baud.
639 * So dont bother disabling interrupts.
640 * But dont want more than 1 break event.
641 * Use a kernel timer to periodically poll the rx line until
642 * the break is finished.
643 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900644static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900646 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/* Ensure that two consecutive samples find the break over. */
650static void sci_break_timer(unsigned long data)
651{
Paul Mundte108b2c2006-09-27 16:32:13 +0900652 struct sci_port *port = (struct sci_port *)data;
653
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000654 if (port->enable)
655 port->enable(&port->port);
656
Paul Mundte108b2c2006-09-27 16:32:13 +0900657 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900659 sci_schedule_break_timer(port);
660 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* break is over. */
662 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900663 sci_schedule_break_timer(port);
664 } else
665 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000666
667 if (port->disable)
668 port->disable(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900671static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int copied = 0;
674 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700675 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900676 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Paul Mundtdebf9502011-06-08 18:19:37 +0900678 /*
679 * Handle overruns, if supported.
680 */
681 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
682 if (status & (1 << s->cfg->overrun_bit)) {
683 /* overrun error */
684 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
685 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900686
Paul Mundtdebf9502011-06-08 18:19:37 +0900687 dev_notice(port->dev, "overrun error");
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Paul Mundte108b2c2006-09-27 16:32:13 +0900691 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (sci_rxd_in(port) == 0) {
693 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900694 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900695
696 if (!sci_port->break_flag) {
697 sci_port->break_flag = 1;
698 sci_schedule_break_timer(sci_port);
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900701 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900703
704 dev_dbg(port->dev, "BREAK detected\n");
705
Paul Mundte108b2c2006-09-27 16:32:13 +0900706 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900707 copied++;
708 }
709
Paul Mundte108b2c2006-09-27 16:32:13 +0900710 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900712 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800713 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900714
715 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 }
718
Paul Mundte108b2c2006-09-27 16:32:13 +0900719 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900721 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
722 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900723
724 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Alan Cox33f0f882006-01-09 20:54:13 -0800727 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return copied;
731}
732
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900733static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900734{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700735 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900736 struct sci_port *s = to_sci_port(port);
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900737 struct plat_sci_reg *reg;
Paul Mundtd830fa42008-12-16 19:29:38 +0900738 int copied = 0;
739
Paul Mundt4b8c59a2011-06-14 17:53:34 +0900740 reg = sci_getreg(port, SCLSR);
741 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +0900742 return 0;
743
Paul Mundtdebf9502011-06-08 18:19:37 +0900744 if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
Paul Mundtd830fa42008-12-16 19:29:38 +0900745 sci_out(port, SCLSR, 0);
746
747 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
748 tty_flip_buffer_push(tty);
749
750 dev_notice(port->dev, "overrun error\n");
751 copied++;
752 }
753
754 return copied;
755}
756
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900757static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int copied = 0;
760 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700761 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000762 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900764 if (uart_handle_break(port))
765 return 0;
766
Paul Mundtb7a76e42006-02-01 03:06:06 -0800767 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#if defined(CONFIG_CPU_SH3)
769 /* Debounce break */
770 s->break_flag = 1;
771#endif
772 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900773 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800774 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900775
776 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
Alan Cox33f0f882006-01-09 20:54:13 -0800779 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900781
Paul Mundtd830fa42008-12-16 19:29:38 +0900782 copied += sci_handle_fifo_overrun(port);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return copied;
785}
786
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900787static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900789#ifdef CONFIG_SERIAL_SH_SCI_DMA
790 struct uart_port *port = ptr;
791 struct sci_port *s = to_sci_port(port);
792
793 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900794 u16 scr = sci_in(port, SCSCR);
795 u16 ssr = sci_in(port, SCxSR);
796
797 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000798 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000799 disable_irq_nosync(irq);
800 scr |= 0x4000;
801 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900802 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000803 }
804 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900805 /* Clear current interrupt */
806 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000807 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
808 jiffies, s->rx_timeout);
809 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900810
811 return IRQ_HANDLED;
812 }
813#endif
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* I think sci_receive_chars has to be called irrespective
816 * of whether the I_IXOFF is set, otherwise, how is the interrupt
817 * to be disabled?
818 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900819 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return IRQ_HANDLED;
822}
823
David Howells7d12e782006-10-05 14:55:46 +0100824static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900827 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Stuart Menefyfd78a762009-07-29 23:01:24 +0900829 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900831 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 return IRQ_HANDLED;
834}
835
David Howells7d12e782006-10-05 14:55:46 +0100836static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct uart_port *port = ptr;
839
840 /* Handle errors */
841 if (port->type == PORT_SCI) {
842 if (sci_handle_errors(port)) {
843 /* discard character in rx buffer */
844 sci_in(port, SCxSR);
845 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
846 }
847 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900848 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100849 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
852 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
853
854 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100855 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 return IRQ_HANDLED;
858}
859
David Howells7d12e782006-10-05 14:55:46 +0100860static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct uart_port *port = ptr;
863
864 /* Handle BREAKs */
865 sci_handle_breaks(port);
866 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
867
868 return IRQ_HANDLED;
869}
870
Paul Mundtf43dc232011-01-13 15:06:28 +0900871static inline unsigned long port_rx_irq_mask(struct uart_port *port)
872{
873 /*
874 * Not all ports (such as SCIFA) will support REIE. Rather than
875 * special-casing the port type, we check the port initialization
876 * IRQ enable mask to see whether the IRQ is desired at all. If
877 * it's unset, it's logically inferred that there's no point in
878 * testing for it.
879 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900880 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900881}
882
David Howells7d12e782006-10-05 14:55:46 +0100883static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Magnus Damm44e18e92009-07-03 08:39:34 +0000885 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900886 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900887 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900888 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900890 ssr_status = sci_in(port, SCxSR);
891 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900892 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900895 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900896 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900897 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900898
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900899 /*
900 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
901 * DR flags
902 */
903 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900904 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900905 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000908 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900909 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000912 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900913 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Michael Trimarchia8884e32008-10-31 16:10:23 +0900915 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300919 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * ports' baud rate when the peripheral clock changes.
921 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900922static int sci_notifier(struct notifier_block *self,
923 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Magnus Damme552de22009-01-21 15:13:42 +0000925 struct sci_port *sci_port;
926 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Paul Mundtd535a232011-01-19 17:19:35 +0900928 sci_port = container_of(self, struct sci_port, freq_transition);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000931 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900932 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900933
Paul Mundtd535a232011-01-19 17:19:35 +0900934 spin_lock_irqsave(&port->lock, flags);
935 port->uartclk = clk_get_rate(sci_port->iclk);
936 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return NOTIFY_OK;
940}
Magnus Damm501b8252009-01-21 15:14:30 +0000941
942static void sci_clk_enable(struct uart_port *port)
943{
944 struct sci_port *sci_port = to_sci_port(port);
945
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000946 pm_runtime_get_sync(port->dev);
947
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900948 clk_enable(sci_port->iclk);
949 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
950 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000951}
952
953static void sci_clk_disable(struct uart_port *port)
954{
955 struct sci_port *sci_port = to_sci_port(port);
956
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900957 clk_disable(sci_port->fclk);
958 clk_disable(sci_port->iclk);
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000959
960 pm_runtime_put_sync(port->dev);
Magnus Damm501b8252009-01-21 15:14:30 +0000961}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963static int sci_request_irq(struct sci_port *port)
964{
965 int i;
David Howells7d12e782006-10-05 14:55:46 +0100966 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
968 sci_br_interrupt,
969 };
970 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
971 "SCI Transmit Data Empty", "SCI Break" };
972
Paul Mundtce6738b2011-01-19 15:24:40 +0900973 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
974 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900976
Paul Mundtce6738b2011-01-19 15:24:40 +0900977 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900978 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900979 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENODEV;
981 }
982 } else {
983 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900984 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900986
Paul Mundtce6738b2011-01-19 15:24:40 +0900987 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900988 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900989 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -ENODEV;
991 }
992 }
993 }
994
995 return 0;
996}
997
998static void sci_free_irq(struct sci_port *port)
999{
1000 int i;
1001
Paul Mundtce6738b2011-01-19 15:24:40 +09001002 if (port->cfg->irqs[0] == port->cfg->irqs[1])
1003 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +09001004 else {
Paul Mundtce6738b2011-01-19 15:24:40 +09001005 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
1006 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 continue;
1008
Paul Mundtce6738b2011-01-19 15:24:40 +09001009 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 }
1012}
1013
1014static unsigned int sci_tx_empty(struct uart_port *port)
1015{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +00001016 unsigned short status = sci_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001017 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001018
1019 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1023{
1024 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
1025 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
1026 /* If you have signals for DTR and DCD, please implement here. */
1027}
1028
1029static unsigned int sci_get_mctrl(struct uart_port *port)
1030{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001031 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 and CTS/RTS */
1033
1034 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
1035}
1036
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001037#ifdef CONFIG_SERIAL_SH_SCI_DMA
1038static void sci_dma_tx_complete(void *arg)
1039{
1040 struct sci_port *s = arg;
1041 struct uart_port *port = &s->port;
1042 struct circ_buf *xmit = &port->state->xmit;
1043 unsigned long flags;
1044
1045 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1046
1047 spin_lock_irqsave(&port->lock, flags);
1048
Magnus Dammf354a382010-03-19 04:47:01 +00001049 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001050 xmit->tail &= UART_XMIT_SIZE - 1;
1051
Magnus Dammf354a382010-03-19 04:47:01 +00001052 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001053
1054 async_tx_ack(s->desc_tx);
1055 s->cookie_tx = -EINVAL;
1056 s->desc_tx = NULL;
1057
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001058 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1059 uart_write_wakeup(port);
1060
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001061 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001062 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001063 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001064 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001065 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001066 }
1067
1068 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001069}
1070
1071/* Locking: called with port lock held */
1072static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
1073 size_t count)
1074{
1075 struct uart_port *port = &s->port;
1076 int i, active, room;
1077
1078 room = tty_buffer_request_room(tty, count);
1079
1080 if (s->active_rx == s->cookie_rx[0]) {
1081 active = 0;
1082 } else if (s->active_rx == s->cookie_rx[1]) {
1083 active = 1;
1084 } else {
1085 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1086 return 0;
1087 }
1088
1089 if (room < count)
1090 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1091 count - room);
1092 if (!room)
1093 return room;
1094
1095 for (i = 0; i < room; i++)
1096 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1097 TTY_NORMAL);
1098
1099 port->icount.rx += room;
1100
1101 return room;
1102}
1103
1104static void sci_dma_rx_complete(void *arg)
1105{
1106 struct sci_port *s = arg;
1107 struct uart_port *port = &s->port;
1108 struct tty_struct *tty = port->state->port.tty;
1109 unsigned long flags;
1110 int count;
1111
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001112 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001113
1114 spin_lock_irqsave(&port->lock, flags);
1115
1116 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1117
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001118 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001119
1120 spin_unlock_irqrestore(&port->lock, flags);
1121
1122 if (count)
1123 tty_flip_buffer_push(tty);
1124
1125 schedule_work(&s->work_rx);
1126}
1127
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001128static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1129{
1130 struct dma_chan *chan = s->chan_rx;
1131 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001132
1133 s->chan_rx = NULL;
1134 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1135 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001136 if (sg_dma_address(&s->sg_rx[0]))
1137 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1138 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001139 if (enable_pio)
1140 sci_start_rx(port);
1141}
1142
1143static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1144{
1145 struct dma_chan *chan = s->chan_tx;
1146 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001147
1148 s->chan_tx = NULL;
1149 s->cookie_tx = -EINVAL;
1150 dma_release_channel(chan);
1151 if (enable_pio)
1152 sci_start_tx(port);
1153}
1154
1155static void sci_submit_rx(struct sci_port *s)
1156{
1157 struct dma_chan *chan = s->chan_rx;
1158 int i;
1159
1160 for (i = 0; i < 2; i++) {
1161 struct scatterlist *sg = &s->sg_rx[i];
1162 struct dma_async_tx_descriptor *desc;
1163
1164 desc = chan->device->device_prep_slave_sg(chan,
1165 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1166
1167 if (desc) {
1168 s->desc_rx[i] = desc;
1169 desc->callback = sci_dma_rx_complete;
1170 desc->callback_param = s;
1171 s->cookie_rx[i] = desc->tx_submit(desc);
1172 }
1173
1174 if (!desc || s->cookie_rx[i] < 0) {
1175 if (i) {
1176 async_tx_ack(s->desc_rx[0]);
1177 s->cookie_rx[0] = -EINVAL;
1178 }
1179 if (desc) {
1180 async_tx_ack(desc);
1181 s->cookie_rx[i] = -EINVAL;
1182 }
1183 dev_warn(s->port.dev,
1184 "failed to re-start DMA, using PIO\n");
1185 sci_rx_dma_release(s, true);
1186 return;
1187 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001188 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1189 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001190 }
1191
1192 s->active_rx = s->cookie_rx[0];
1193
1194 dma_async_issue_pending(chan);
1195}
1196
1197static void work_fn_rx(struct work_struct *work)
1198{
1199 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1200 struct uart_port *port = &s->port;
1201 struct dma_async_tx_descriptor *desc;
1202 int new;
1203
1204 if (s->active_rx == s->cookie_rx[0]) {
1205 new = 0;
1206 } else if (s->active_rx == s->cookie_rx[1]) {
1207 new = 1;
1208 } else {
1209 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1210 return;
1211 }
1212 desc = s->desc_rx[new];
1213
1214 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1215 DMA_SUCCESS) {
1216 /* Handle incomplete DMA receive */
1217 struct tty_struct *tty = port->state->port.tty;
1218 struct dma_chan *chan = s->chan_rx;
1219 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1220 async_tx);
1221 unsigned long flags;
1222 int count;
1223
Linus Walleij05827632010-05-17 16:30:42 -07001224 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001225 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1226 sh_desc->partial, sh_desc->cookie);
1227
1228 spin_lock_irqsave(&port->lock, flags);
1229 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1230 spin_unlock_irqrestore(&port->lock, flags);
1231
1232 if (count)
1233 tty_flip_buffer_push(tty);
1234
1235 sci_submit_rx(s);
1236
1237 return;
1238 }
1239
1240 s->cookie_rx[new] = desc->tx_submit(desc);
1241 if (s->cookie_rx[new] < 0) {
1242 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1243 sci_rx_dma_release(s, true);
1244 return;
1245 }
1246
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001247 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001248
1249 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1250 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001251}
1252
1253static void work_fn_tx(struct work_struct *work)
1254{
1255 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1256 struct dma_async_tx_descriptor *desc;
1257 struct dma_chan *chan = s->chan_tx;
1258 struct uart_port *port = &s->port;
1259 struct circ_buf *xmit = &port->state->xmit;
1260 struct scatterlist *sg = &s->sg_tx;
1261
1262 /*
1263 * DMA is idle now.
1264 * Port xmit buffer is already mapped, and it is one page... Just adjust
1265 * offsets and lengths. Since it is a circular buffer, we have to
1266 * transmit till the end, and then the rest. Take the port lock to get a
1267 * consistent xmit buffer state.
1268 */
1269 spin_lock_irq(&port->lock);
1270 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001271 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001272 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001273 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001274 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001275 spin_unlock_irq(&port->lock);
1276
Magnus Dammf354a382010-03-19 04:47:01 +00001277 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001278
1279 desc = chan->device->device_prep_slave_sg(chan,
1280 sg, s->sg_len_tx, DMA_TO_DEVICE,
1281 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1282 if (!desc) {
1283 /* switch to PIO */
1284 sci_tx_dma_release(s, true);
1285 return;
1286 }
1287
1288 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1289
1290 spin_lock_irq(&port->lock);
1291 s->desc_tx = desc;
1292 desc->callback = sci_dma_tx_complete;
1293 desc->callback_param = s;
1294 spin_unlock_irq(&port->lock);
1295 s->cookie_tx = desc->tx_submit(desc);
1296 if (s->cookie_tx < 0) {
1297 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1298 /* switch to PIO */
1299 sci_tx_dma_release(s, true);
1300 return;
1301 }
1302
1303 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1304 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1305
1306 dma_async_issue_pending(chan);
1307}
1308#endif
1309
Russell Kingb129a8c2005-08-31 10:12:14 +01001310static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001312 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001313 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001315#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001316 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001317 u16 new, scr = sci_in(port, SCSCR);
1318 if (s->chan_tx)
1319 new = scr | 0x8000;
1320 else
1321 new = scr & ~0x8000;
1322 if (new != scr)
1323 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001324 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001325
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001326 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1327 s->cookie_tx < 0)
1328 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001329#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001330
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001331 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001332 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1333 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001334 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Russell Kingb129a8c2005-08-31 10:12:14 +01001338static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 unsigned short ctrl;
1341
1342 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001344
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001345 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001346 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001347
Paul Mundt8e698612009-06-24 19:44:32 +09001348 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001353static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned short ctrl;
1356
Paul Mundtf43dc232011-01-13 15:06:28 +09001357 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1358
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001359 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001360 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
1365static void sci_stop_rx(struct uart_port *port)
1366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 unsigned short ctrl;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001370
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001371 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001372 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001373
1374 ctrl &= ~port_rx_irq_mask(port);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379static void sci_enable_ms(struct uart_port *port)
1380{
1381 /* Nothing here yet .. */
1382}
1383
1384static void sci_break_ctl(struct uart_port *port, int break_state)
1385{
1386 /* Nothing here yet .. */
1387}
1388
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001389#ifdef CONFIG_SERIAL_SH_SCI_DMA
1390static bool filter(struct dma_chan *chan, void *slave)
1391{
1392 struct sh_dmae_slave *param = slave;
1393
1394 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1395 param->slave_id);
1396
1397 if (param->dma_dev == chan->device->dev) {
1398 chan->private = param;
1399 return true;
1400 } else {
1401 return false;
1402 }
1403}
1404
1405static void rx_timer_fn(unsigned long arg)
1406{
1407 struct sci_port *s = (struct sci_port *)arg;
1408 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001409 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001410
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001411 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001412 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001413 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001414 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001415 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001416 dev_dbg(port->dev, "DMA Rx timed out\n");
1417 schedule_work(&s->work_rx);
1418}
1419
1420static void sci_request_dma(struct uart_port *port)
1421{
1422 struct sci_port *s = to_sci_port(port);
1423 struct sh_dmae_slave *param;
1424 struct dma_chan *chan;
1425 dma_cap_mask_t mask;
1426 int nent;
1427
1428 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001429 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001430
Paul Mundtce6738b2011-01-19 15:24:40 +09001431 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001432 return;
1433
1434 dma_cap_zero(mask);
1435 dma_cap_set(DMA_SLAVE, mask);
1436
1437 param = &s->param_tx;
1438
1439 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001440 param->slave_id = s->cfg->dma_slave_tx;
1441 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001442
1443 s->cookie_tx = -EINVAL;
1444 chan = dma_request_channel(mask, filter, param);
1445 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1446 if (chan) {
1447 s->chan_tx = chan;
1448 sg_init_table(&s->sg_tx, 1);
1449 /* UART circular tx buffer is an aligned page. */
1450 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1451 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1452 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1453 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1454 if (!nent)
1455 sci_tx_dma_release(s, false);
1456 else
1457 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1458 sg_dma_len(&s->sg_tx),
1459 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1460
1461 s->sg_len_tx = nent;
1462
1463 INIT_WORK(&s->work_tx, work_fn_tx);
1464 }
1465
1466 param = &s->param_rx;
1467
1468 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001469 param->slave_id = s->cfg->dma_slave_rx;
1470 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001471
1472 chan = dma_request_channel(mask, filter, param);
1473 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1474 if (chan) {
1475 dma_addr_t dma[2];
1476 void *buf[2];
1477 int i;
1478
1479 s->chan_rx = chan;
1480
1481 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1482 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1483 &dma[0], GFP_KERNEL);
1484
1485 if (!buf[0]) {
1486 dev_warn(port->dev,
1487 "failed to allocate dma buffer, using PIO\n");
1488 sci_rx_dma_release(s, true);
1489 return;
1490 }
1491
1492 buf[1] = buf[0] + s->buf_len_rx;
1493 dma[1] = dma[0] + s->buf_len_rx;
1494
1495 for (i = 0; i < 2; i++) {
1496 struct scatterlist *sg = &s->sg_rx[i];
1497
1498 sg_init_table(sg, 1);
1499 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1500 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001501 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001502 }
1503
1504 INIT_WORK(&s->work_rx, work_fn_rx);
1505 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1506
1507 sci_submit_rx(s);
1508 }
1509}
1510
1511static void sci_free_dma(struct uart_port *port)
1512{
1513 struct sci_port *s = to_sci_port(port);
1514
Paul Mundtce6738b2011-01-19 15:24:40 +09001515 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001516 return;
1517
1518 if (s->chan_tx)
1519 sci_tx_dma_release(s, false);
1520 if (s->chan_rx)
1521 sci_rx_dma_release(s, false);
1522}
Paul Mundt27bd1072011-01-19 15:37:31 +09001523#else
1524static inline void sci_request_dma(struct uart_port *port)
1525{
1526}
1527
1528static inline void sci_free_dma(struct uart_port *port)
1529{
1530}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001531#endif
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533static int sci_startup(struct uart_port *port)
1534{
Magnus Damma5660ad2009-01-21 15:14:38 +00001535 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001536 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001538 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1539
Paul Mundte108b2c2006-09-27 16:32:13 +09001540 if (s->enable)
1541 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Paul Mundt073e84c2011-01-19 17:30:53 +09001543 ret = sci_request_irq(s);
1544 if (unlikely(ret < 0))
1545 return ret;
1546
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001547 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001548
Yoshinori Satod6569012005-10-14 15:59:12 -07001549 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001550 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 return 0;
1553}
1554
1555static void sci_shutdown(struct uart_port *port)
1556{
Magnus Damma5660ad2009-01-21 15:14:38 +00001557 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001559 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001562 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001563
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001564 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 sci_free_irq(s);
1566
Paul Mundte108b2c2006-09-27 16:32:13 +09001567 if (s->disable)
1568 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Paul Mundt26c92f32009-06-24 18:23:52 +09001571static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1572 unsigned long freq)
1573{
1574 switch (algo_id) {
1575 case SCBRR_ALGO_1:
1576 return ((freq + 16 * bps) / (16 * bps) - 1);
1577 case SCBRR_ALGO_2:
1578 return ((freq + 16 * bps) / (32 * bps) - 1);
1579 case SCBRR_ALGO_3:
1580 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1581 case SCBRR_ALGO_4:
1582 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1583 case SCBRR_ALGO_5:
1584 return (((freq * 1000 / 32) / bps) - 1);
1585 }
1586
1587 /* Warn, but use a safe default */
1588 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001589
Paul Mundt26c92f32009-06-24 18:23:52 +09001590 return ((freq + 16 * bps) / (32 * bps) - 1);
1591}
1592
Alan Cox606d0992006-12-08 02:38:45 -08001593static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1594 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Paul Mundt00b9de92009-06-24 17:53:33 +09001596 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001597 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001598 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001599 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Magnus Damm154280f2009-12-22 03:37:28 +00001601 /*
1602 * earlyprintk comes here early on with port->uartclk set to zero.
1603 * the clock framework is not up and running at this point so here
1604 * we assume that 115200 is the maximum baud rate. please note that
1605 * the baud rate is not programmed during earlyprintk - it is assumed
1606 * that the previous boot loader has enabled required clocks and
1607 * setup the baud rate generator hardware for us already.
1608 */
1609 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1610
1611 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1612 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001613 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Alexandre Courbot36003382011-03-03 08:04:42 +00001615 if (s->enable)
1616 s->enable(port);
1617
Paul Mundte108b2c2006-09-27 16:32:13 +09001618 do {
1619 status = sci_in(port, SCxSR);
1620 } while (!(status & SCxSR_TEND(port)));
1621
1622 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1623
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001624 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001625 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001626
1627 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001628
Paul Mundte108b2c2006-09-27 16:32:13 +09001629 if ((termios->c_cflag & CSIZE) == CS7)
1630 smr_val |= 0x40;
1631 if (termios->c_cflag & PARENB)
1632 smr_val |= 0x20;
1633 if (termios->c_cflag & PARODD)
1634 smr_val |= 0x30;
1635 if (termios->c_cflag & CSTOPB)
1636 smr_val |= 0x08;
1637
1638 uart_update_timeout(port, termios->c_cflag, baud);
1639
1640 sci_out(port, SCSMR, smr_val);
1641
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001642 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001643 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001646 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1648 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001649 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 sci_out(port, SCBRR, t);
1653 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1654 }
1655
Paul Mundtd5701642008-12-16 20:07:27 +09001656 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001657 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001658
Paul Mundtce6738b2011-01-19 15:24:40 +09001659 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001661#ifdef CONFIG_SERIAL_SH_SCI_DMA
1662 /*
1663 * Calculate delay for 1.5 DMA buffers: see
1664 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1665 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1666 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1667 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1668 * sizes), but it has been found out experimentally, that this is not
1669 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1670 * as a minimum seem to work perfectly.
1671 */
1672 if (s->chan_rx) {
1673 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1674 port->fifosize / 2;
1675 dev_dbg(port->dev,
1676 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1677 s->rx_timeout * 1000 / HZ, port->timeout);
1678 if (s->rx_timeout < msecs_to_jiffies(20))
1679 s->rx_timeout = msecs_to_jiffies(20);
1680 }
1681#endif
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001684 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001685
1686 if (s->disable)
1687 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690static const char *sci_type(struct uart_port *port)
1691{
1692 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001693 case PORT_IRDA:
1694 return "irda";
1695 case PORT_SCI:
1696 return "sci";
1697 case PORT_SCIF:
1698 return "scif";
1699 case PORT_SCIFA:
1700 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001701 case PORT_SCIFB:
1702 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
Paul Mundtfa439722008-09-04 18:53:58 +09001705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Paul Mundte2651642011-01-20 21:24:03 +09001708static inline unsigned long sci_port_size(struct uart_port *port)
1709{
1710 /*
1711 * Pick an arbitrary size that encapsulates all of the base
1712 * registers by default. This can be optimized later, or derived
1713 * from platform resource data at such a time that ports begin to
1714 * behave more erratically.
1715 */
1716 return 64;
1717}
1718
Paul Mundtf6e94952011-01-21 15:25:36 +09001719static int sci_remap_port(struct uart_port *port)
1720{
1721 unsigned long size = sci_port_size(port);
1722
1723 /*
1724 * Nothing to do if there's already an established membase.
1725 */
1726 if (port->membase)
1727 return 0;
1728
1729 if (port->flags & UPF_IOREMAP) {
1730 port->membase = ioremap_nocache(port->mapbase, size);
1731 if (unlikely(!port->membase)) {
1732 dev_err(port->dev, "can't remap port#%d\n", port->line);
1733 return -ENXIO;
1734 }
1735 } else {
1736 /*
1737 * For the simple (and majority of) cases where we don't
1738 * need to do any remapping, just cast the cookie
1739 * directly.
1740 */
1741 port->membase = (void __iomem *)port->mapbase;
1742 }
1743
1744 return 0;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static void sci_release_port(struct uart_port *port)
1748{
Paul Mundte2651642011-01-20 21:24:03 +09001749 if (port->flags & UPF_IOREMAP) {
1750 iounmap(port->membase);
1751 port->membase = NULL;
1752 }
1753
1754 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757static int sci_request_port(struct uart_port *port)
1758{
Paul Mundte2651642011-01-20 21:24:03 +09001759 unsigned long size = sci_port_size(port);
1760 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001761 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Paul Mundt10205202011-01-21 16:00:31 +09001763 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001764 if (unlikely(res == NULL))
1765 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Paul Mundtf6e94952011-01-21 15:25:36 +09001767 ret = sci_remap_port(port);
1768 if (unlikely(ret != 0)) {
1769 release_resource(res);
1770 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001771 }
Paul Mundte2651642011-01-20 21:24:03 +09001772
1773 return 0;
1774}
1775
1776static void sci_config_port(struct uart_port *port, int flags)
1777{
1778 if (flags & UART_CONFIG_TYPE) {
1779 struct sci_port *sport = to_sci_port(port);
1780
1781 port->type = sport->cfg->type;
1782 sci_request_port(port);
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
1786static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1787{
Magnus Damma5660ad2009-01-21 15:14:38 +00001788 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Paul Mundtce6738b2011-01-19 15:24:40 +09001790 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -EINVAL;
1792 if (ser->baud_base < 2400)
1793 /* No paper tape reader for Mitch.. */
1794 return -EINVAL;
1795
1796 return 0;
1797}
1798
1799static struct uart_ops sci_uart_ops = {
1800 .tx_empty = sci_tx_empty,
1801 .set_mctrl = sci_set_mctrl,
1802 .get_mctrl = sci_get_mctrl,
1803 .start_tx = sci_start_tx,
1804 .stop_tx = sci_stop_tx,
1805 .stop_rx = sci_stop_rx,
1806 .enable_ms = sci_enable_ms,
1807 .break_ctl = sci_break_ctl,
1808 .startup = sci_startup,
1809 .shutdown = sci_shutdown,
1810 .set_termios = sci_set_termios,
1811 .type = sci_type,
1812 .release_port = sci_release_port,
1813 .request_port = sci_request_port,
1814 .config_port = sci_config_port,
1815 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001816#ifdef CONFIG_CONSOLE_POLL
1817 .poll_get_char = sci_poll_get_char,
1818 .poll_put_char = sci_poll_put_char,
1819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820};
1821
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001822static int __devinit sci_init_single(struct platform_device *dev,
1823 struct sci_port *sci_port,
1824 unsigned int index,
1825 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001826{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001827 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001828
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001829 port->ops = &sci_uart_ops;
1830 port->iotype = UPIO_MEM;
1831 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001832
1833 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001834 case PORT_SCIFB:
1835 port->fifosize = 256;
1836 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001837 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001838 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001839 break;
1840 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001841 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001842 break;
1843 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001844 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001845 break;
1846 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001847
Paul Mundt61a69762011-06-14 12:40:19 +09001848 if (p->regtype == SCIx_PROBE_REGTYPE)
1849 BUG_ON(sci_probe_regmap(p) != 0);
1850
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001851 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001852 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1853 if (IS_ERR(sci_port->iclk)) {
1854 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1855 if (IS_ERR(sci_port->iclk)) {
1856 dev_err(&dev->dev, "can't get iclk\n");
1857 return PTR_ERR(sci_port->iclk);
1858 }
1859 }
1860
1861 /*
1862 * The function clock is optional, ignore it if we can't
1863 * find it.
1864 */
1865 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1866 if (IS_ERR(sci_port->fclk))
1867 sci_port->fclk = NULL;
1868
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001869 sci_port->enable = sci_clk_enable;
1870 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001871 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001872
1873 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001874 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001875
Magnus Damm7ed7e072009-01-21 15:14:14 +00001876 sci_port->break_timer.data = (unsigned long)sci_port;
1877 sci_port->break_timer.function = sci_break_timer;
1878 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001879
Paul Mundtdebf9502011-06-08 18:19:37 +09001880 /*
1881 * Establish some sensible defaults for the error detection.
1882 */
1883 if (!p->error_mask)
1884 p->error_mask = (p->type == PORT_SCI) ?
1885 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1886
1887 /*
1888 * Establish sensible defaults for the overrun detection, unless
1889 * the part has explicitly disabled support for it.
1890 */
1891 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1892 if (p->type == PORT_SCI)
1893 p->overrun_bit = 5;
1894 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1895 p->overrun_bit = 9;
1896 else
1897 p->overrun_bit = 0;
1898
1899 /*
1900 * Make the error mask inclusive of overrun detection, if
1901 * supported.
1902 */
1903 p->error_mask |= (1 << p->overrun_bit);
1904 }
1905
Paul Mundtce6738b2011-01-19 15:24:40 +09001906 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001907
Paul Mundtce6738b2011-01-19 15:24:40 +09001908 port->mapbase = p->mapbase;
1909 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001910 port->flags = p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09001911 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09001912
1913 /*
Paul Mundt61a69762011-06-14 12:40:19 +09001914 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09001915 * for the multi-IRQ ports, which is where we are primarily
1916 * concerned with the shutdown path synchronization.
1917 *
1918 * For the muxed case there's nothing more to do.
1919 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00001920 port->irq = p->irqs[SCIx_RXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001921
Paul Mundt61a69762011-06-14 12:40:19 +09001922 port->serial_in = sci_serial_in;
1923 port->serial_out = sci_serial_out;
1924
Paul Mundtce6738b2011-01-19 15:24:40 +09001925 if (p->dma_dev)
1926 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1927 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001928
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001929 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001933static void serial_console_putchar(struct uart_port *port, int ch)
1934{
1935 sci_poll_put_char(port, ch);
1936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/*
1939 * Print a string to the serial port trying not to disturb
1940 * any possible real use of the port...
1941 */
1942static void serial_console_write(struct console *co, const char *s,
1943 unsigned count)
1944{
Paul Mundt906b17d2011-01-21 16:19:53 +09001945 struct sci_port *sci_port = &sci_ports[co->index];
1946 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001947 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001948
Magnus Damm501b8252009-01-21 15:14:30 +00001949 if (sci_port->enable)
1950 sci_port->enable(port);
1951
1952 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001953
1954 /* wait until fifo is empty and last bit has been transmitted */
1955 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1956 while ((sci_in(port, SCxSR) & bits) != bits)
1957 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001958
Magnus Damm345e5a72009-11-05 14:34:57 +00001959 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001960 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001963static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001965 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct uart_port *port;
1967 int baud = 115200;
1968 int bits = 8;
1969 int parity = 'n';
1970 int flow = 'n';
1971 int ret;
1972
Paul Mundte108b2c2006-09-27 16:32:13 +09001973 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09001974 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09001975 */
Paul Mundt906b17d2011-01-21 16:19:53 +09001976 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09001977 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001978
Paul Mundt906b17d2011-01-21 16:19:53 +09001979 sci_port = &sci_ports[co->index];
1980 port = &sci_port->port;
1981
Alexandre Courbotb2267a62011-02-09 03:18:46 +00001982 /*
1983 * Refuse to handle uninitialized ports.
1984 */
1985 if (!port->ops)
1986 return -ENODEV;
1987
Paul Mundtf6e94952011-01-21 15:25:36 +09001988 ret = sci_remap_port(port);
1989 if (unlikely(ret != 0))
1990 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001991
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001992 if (sci_port->enable)
1993 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (options)
1996 uart_parse_options(options, &baud, &parity, &bits, &flow);
1997
Magnus Damm501b8252009-01-21 15:14:30 +00001998 /* TODO: disable clock */
Paul Mundtab7cfb52011-06-01 14:47:42 +09001999 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002static struct console serial_console = {
2003 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002004 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 .write = serial_console_write,
2006 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002007 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002009 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010};
2011
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002012static struct console early_serial_console = {
2013 .name = "early_ttySC",
2014 .write = serial_console_write,
2015 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002016 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002017};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002018
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002019static char early_serial_buf[32];
2020
Paul Mundtecdf8a42011-01-21 00:05:48 +09002021static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2022{
2023 struct plat_sci_port *cfg = pdev->dev.platform_data;
2024
2025 if (early_serial_console.data)
2026 return -EEXIST;
2027
2028 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002029
Paul Mundt906b17d2011-01-21 16:19:53 +09002030 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002031
2032 serial_console_setup(&early_serial_console, early_serial_buf);
2033
2034 if (!strstr(early_serial_buf, "keep"))
2035 early_serial_console.flags |= CON_BOOT;
2036
2037 register_console(&early_serial_console);
2038 return 0;
2039}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002040
2041#define SCI_CONSOLE (&serial_console)
2042
Paul Mundtecdf8a42011-01-21 00:05:48 +09002043#else
2044static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2045{
2046 return -EINVAL;
2047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002049#define SCI_CONSOLE NULL
2050
2051#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053static char banner[] __initdata =
2054 KERN_INFO "SuperH SCI(F) driver initialized\n";
2055
2056static struct uart_driver sci_uart_driver = {
2057 .owner = THIS_MODULE,
2058 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 .dev_name = "ttySC",
2060 .major = SCI_MAJOR,
2061 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002062 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 .cons = SCI_CONSOLE,
2064};
2065
Paul Mundt54507f62009-05-08 23:48:33 +09002066static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002067{
Paul Mundtd535a232011-01-19 17:19:35 +09002068 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002069
Paul Mundtd535a232011-01-19 17:19:35 +09002070 cpufreq_unregister_notifier(&port->freq_transition,
2071 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002072
Paul Mundtd535a232011-01-19 17:19:35 +09002073 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002074
Paul Mundtd535a232011-01-19 17:19:35 +09002075 clk_put(port->iclk);
2076 clk_put(port->fclk);
2077
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002078 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00002079 return 0;
2080}
2081
Magnus Damm0ee70712009-01-21 15:13:50 +00002082static int __devinit sci_probe_single(struct platform_device *dev,
2083 unsigned int index,
2084 struct plat_sci_port *p,
2085 struct sci_port *sciport)
2086{
Magnus Damm0ee70712009-01-21 15:13:50 +00002087 int ret;
2088
2089 /* Sanity check */
2090 if (unlikely(index >= SCI_NPORTS)) {
2091 dev_notice(&dev->dev, "Attempting to register port "
2092 "%d when only %d are available.\n",
2093 index+1, SCI_NPORTS);
2094 dev_notice(&dev->dev, "Consider bumping "
2095 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
2096 return 0;
2097 }
2098
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002099 ret = sci_init_single(dev, sciport, index, p);
2100 if (ret)
2101 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002102
Paul Mundtd535a232011-01-19 17:19:35 +09002103 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00002104}
2105
Paul Mundte108b2c2006-09-27 16:32:13 +09002106static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Paul Mundte108b2c2006-09-27 16:32:13 +09002108 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09002109 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002110 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002111
Paul Mundtecdf8a42011-01-21 00:05:48 +09002112 /*
2113 * If we've come here via earlyprintk initialization, head off to
2114 * the special early probe. We don't have sufficient device state
2115 * to make it beyond this yet.
2116 */
2117 if (is_early_platform_device(dev))
2118 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002119
Paul Mundtd535a232011-01-19 17:19:35 +09002120 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002121
Paul Mundt906b17d2011-01-21 16:19:53 +09002122 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002123 if (ret)
2124 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00002125
Paul Mundtd535a232011-01-19 17:19:35 +09002126 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Paul Mundtd535a232011-01-19 17:19:35 +09002128 ret = cpufreq_register_notifier(&sp->freq_transition,
2129 CPUFREQ_TRANSITION_NOTIFIER);
2130 if (unlikely(ret < 0))
2131 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133#ifdef CONFIG_SH_STANDARD_BIOS
2134 sh_bios_gdb_detach();
2135#endif
2136
Paul Mundte108b2c2006-09-27 16:32:13 +09002137 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002138
2139err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002140 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002141 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002142}
2143
Paul Mundt6daa79b2009-06-15 07:07:38 +09002144static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002145{
Paul Mundtd535a232011-01-19 17:19:35 +09002146 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002147
Paul Mundtd535a232011-01-19 17:19:35 +09002148 if (sport)
2149 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002150
2151 return 0;
2152}
2153
Paul Mundt6daa79b2009-06-15 07:07:38 +09002154static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002155{
Paul Mundtd535a232011-01-19 17:19:35 +09002156 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002157
Paul Mundtd535a232011-01-19 17:19:35 +09002158 if (sport)
2159 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002160
2161 return 0;
2162}
2163
Alexey Dobriyan47145212009-12-14 18:00:08 -08002164static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002165 .suspend = sci_suspend,
2166 .resume = sci_resume,
2167};
2168
Paul Mundte108b2c2006-09-27 16:32:13 +09002169static struct platform_driver sci_driver = {
2170 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002171 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002172 .driver = {
2173 .name = "sh-sci",
2174 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002175 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002176 },
2177};
2178
2179static int __init sci_init(void)
2180{
2181 int ret;
2182
2183 printk(banner);
2184
Paul Mundte108b2c2006-09-27 16:32:13 +09002185 ret = uart_register_driver(&sci_uart_driver);
2186 if (likely(ret == 0)) {
2187 ret = platform_driver_register(&sci_driver);
2188 if (unlikely(ret))
2189 uart_unregister_driver(&sci_uart_driver);
2190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return ret;
2193}
2194
2195static void __exit sci_exit(void)
2196{
Paul Mundte108b2c2006-09-27 16:32:13 +09002197 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 uart_unregister_driver(&sci_uart_driver);
2199}
2200
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002201#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2202early_platform_init_buffer("earlyprintk", &sci_driver,
2203 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205module_init(sci_init);
2206module_exit(sci_exit);
2207
Paul Mundte108b2c2006-09-27 16:32:13 +09002208MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002209MODULE_ALIAS("platform:sh-sci");