blob: 14e1bae5039221c1384b911499e086fe5e86d887 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
Paul Mundtf43dc232011-01-13 15:06:28 +09004 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09005 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090014 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090020#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#undef DEBUG
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090041#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090042#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/notifier.h>
Magnus Damm5e50d2d2011-04-19 10:38:25 +000044#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090046#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090047#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090048#include <linux/err.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090049#include <linux/dmaengine.h>
50#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090052
53#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundtce6738b2011-01-19 15:24:40 +090062 /* Platform configuration */
63 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090064
Paul Mundte108b2c2006-09-27 16:32:13 +090065 /* 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 Liakhovetski73a19e4c2010-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 Liakhovetski73a19e4c2010-03-02 11:39:15 +090083#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-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 Liakhovetski73a19e4c2010-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
300/*
301 * The "offset" here is rather misleading, in that it refers to an enum
302 * value relative to the port mapping rather than the fixed offset
303 * itself, which needs to be manually retrieved from the platform's
304 * register map for the given port.
305 */
306static unsigned int sci_serial_in(struct uart_port *p, int offset)
307{
308 struct sci_port *s = to_sci_port(p);
309 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + offset;
310
311 if (reg->size == 8)
312 return ioread8(p->membase + (reg->offset << p->regshift));
313 else if (reg->size == 16)
314 return ioread16(p->membase + (reg->offset << p->regshift));
315 else
316 WARN(1, "Invalid register access\n");
317
318 return 0;
319}
320
321static void sci_serial_out(struct uart_port *p, int offset, int value)
322{
323 struct sci_port *s = to_sci_port(p);
324 struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + offset;
325
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
Paul Mundt32351a22007-03-12 14:38:59 +0900436#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
437 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900438 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
439 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900440static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900441{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900442 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900443}
444
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900445static int scif_txroom(struct uart_port *port)
446{
447 return SCIF_TXROOM_MAX - scif_txfill(port);
448}
449
450static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900451{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900452 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900453}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900454#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900455static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900456{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900457 if (port->mapbase == 0xffe00000 ||
458 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900459 /* SCIF0/1*/
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900460 return sci_in(port, SCTFDR) & 0xff;
461 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900462 /* SCIF2 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900463 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900464}
465
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900466static int scif_txroom(struct uart_port *port)
467{
468 if (port->mapbase == 0xffe00000 ||
469 port->mapbase == 0xffe08000)
470 /* SCIF0/1*/
471 return SCIF_TXROOM_MAX - scif_txfill(port);
472 else
473 /* SCIF2 */
474 return SCIF2_TXROOM_MAX - scif_txfill(port);
475}
476
477static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900478{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900479 if ((port->mapbase == 0xffe00000) ||
480 (port->mapbase == 0xffe08000)) {
481 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900482 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900483 } else {
484 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900485 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900486 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900487}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000488#elif defined(CONFIG_ARCH_SH7372)
489static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900490{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000491 if (port->type == PORT_SCIFA)
492 return sci_in(port, SCFDR) >> 8;
493 else
494 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900495}
496
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000497static int scif_txroom(struct uart_port *port)
498{
499 return port->fifosize - scif_txfill(port);
500}
501
502static int scif_rxfill(struct uart_port *port)
503{
504 if (port->type == PORT_SCIFA)
505 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
506 else
507 return sci_in(port, SCRFDR);
508}
Paul Mundte108b2c2006-09-27 16:32:13 +0900509#else
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900510static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900511{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900512 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900513}
514
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900515static int scif_txroom(struct uart_port *port)
516{
517 return SCIF_TXROOM_MAX - scif_txfill(port);
518}
519
520static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900521{
522 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900526static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900527{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900528 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900529}
530
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900531static int sci_txroom(struct uart_port *port)
532{
533 return !sci_txfill(port);
534}
535
536static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900537{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900538 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900539}
540
Paul Mundt514820e2011-06-08 18:51:32 +0900541/*
542 * SCI helper for checking the state of the muxed port/RXD pins.
543 */
544static inline int sci_rxd_in(struct uart_port *port)
545{
546 struct sci_port *s = to_sci_port(port);
547
548 if (s->cfg->port_reg <= 0)
549 return 1;
550
551 return !!__raw_readb(s->cfg->port_reg);
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* ********************************************************************** *
555 * the interrupt related routines *
556 * ********************************************************************** */
557
558static void sci_transmit_chars(struct uart_port *port)
559{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700560 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 unsigned short status;
563 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900564 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 status = sci_in(port, SCxSR);
567 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900569 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900570 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900571 else
Paul Mundt8e698612009-06-24 19:44:32 +0900572 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return;
575 }
576
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900577 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900578 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900579 else
580 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 do {
583 unsigned char c;
584
585 if (port->x_char) {
586 c = port->x_char;
587 port->x_char = 0;
588 } else if (!uart_circ_empty(xmit) && !stopped) {
589 c = xmit->buf[xmit->tail];
590 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
591 } else {
592 break;
593 }
594
595 sci_out(port, SCxTDR, c);
596
597 port->icount.tx++;
598 } while (--count > 0);
599
600 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
601
602 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
603 uart_write_wakeup(port);
604 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100605 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ctrl = sci_in(port, SCSCR);
608
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900609 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sci_in(port, SCxSR); /* Dummy read */
611 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Paul Mundt8e698612009-06-24 19:44:32 +0900614 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617}
618
619/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900620#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900622static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900624 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700625 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int i, count, copied = 0;
627 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800628 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 status = sci_in(port, SCxSR);
631 if (!(status & SCxSR_RDxF(port)))
632 return;
633
634 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900635 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900636 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900637 else
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900638 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800641 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* If for any reason we can't copy more data, we're done! */
644 if (count == 0)
645 break;
646
647 if (port->type == PORT_SCI) {
648 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900649 if (uart_handle_sysrq_char(port, c) ||
650 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900652 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900653 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900655 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 char c = sci_in(port, SCxRDR);
657 status = sci_in(port, SCxSR);
658#if defined(CONFIG_CPU_SH3)
659 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900660 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if ((c == 0) &&
662 (status & SCxSR_FER(port))) {
663 count--; i--;
664 continue;
665 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900668 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900669 sci_port->break_flag = 0;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (STEPFN(c)) {
672 count--; i--;
673 continue;
674 }
675 }
676#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100677 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 count--; i--;
679 continue;
680 }
681
682 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900683 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800684 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900685 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900686 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800687 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900688 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800689 } else
690 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900691
Alan Cox33f0f882006-01-09 20:54:13 -0800692 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
695
696 sci_in(port, SCxSR); /* dummy read */
697 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 copied += count;
700 port->icount.rx += count;
701 }
702
703 if (copied) {
704 /* Tell the rest of the system the news. New characters! */
705 tty_flip_buffer_push(tty);
706 } else {
707 sci_in(port, SCxSR); /* dummy read */
708 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
709 }
710}
711
712#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900713
714/*
715 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * 1 per millisecond or so during the break period, for 9600 baud.
717 * So dont bother disabling interrupts.
718 * But dont want more than 1 break event.
719 * Use a kernel timer to periodically poll the rx line until
720 * the break is finished.
721 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900722static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900724 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/* Ensure that two consecutive samples find the break over. */
728static void sci_break_timer(unsigned long data)
729{
Paul Mundte108b2c2006-09-27 16:32:13 +0900730 struct sci_port *port = (struct sci_port *)data;
731
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000732 if (port->enable)
733 port->enable(&port->port);
734
Paul Mundte108b2c2006-09-27 16:32:13 +0900735 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900737 sci_schedule_break_timer(port);
738 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* break is over. */
740 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900741 sci_schedule_break_timer(port);
742 } else
743 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000744
745 if (port->disable)
746 port->disable(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900749static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int copied = 0;
752 unsigned short status = sci_in(port, SCxSR);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Paul Mundtdebf9502011-06-08 18:19:37 +0900756 /*
757 * Handle overruns, if supported.
758 */
759 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
760 if (status & (1 << s->cfg->overrun_bit)) {
761 /* overrun error */
762 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
763 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900764
Paul Mundtdebf9502011-06-08 18:19:37 +0900765 dev_notice(port->dev, "overrun error");
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Paul Mundte108b2c2006-09-27 16:32:13 +0900769 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (sci_rxd_in(port) == 0) {
771 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900772 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900773
774 if (!sci_port->break_flag) {
775 sci_port->break_flag = 1;
776 sci_schedule_break_timer(sci_port);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900779 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900781
782 dev_dbg(port->dev, "BREAK detected\n");
783
Paul Mundte108b2c2006-09-27 16:32:13 +0900784 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900785 copied++;
786 }
787
Paul Mundte108b2c2006-09-27 16:32:13 +0900788 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900790 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800791 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900792
793 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 }
796
Paul Mundte108b2c2006-09-27 16:32:13 +0900797 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900799 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
800 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900801
802 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Alan Cox33f0f882006-01-09 20:54:13 -0800805 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 return copied;
809}
810
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900811static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900812{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700813 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900814 struct sci_port *s = to_sci_port(port);
Paul Mundtd830fa42008-12-16 19:29:38 +0900815 int copied = 0;
816
Paul Mundtdebf9502011-06-08 18:19:37 +0900817 /*
818 * XXX: Technically not limited to non-SCIFs, it's simply the
819 * SCLSR check that is for the moment SCIF-specific. This
820 * probably wants to be revisited for SCIFA/B as well as for
821 * factoring in SCI overrun detection.
822 */
Paul Mundtd830fa42008-12-16 19:29:38 +0900823 if (port->type != PORT_SCIF)
824 return 0;
825
Paul Mundtdebf9502011-06-08 18:19:37 +0900826 if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
Paul Mundtd830fa42008-12-16 19:29:38 +0900827 sci_out(port, SCLSR, 0);
828
829 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
830 tty_flip_buffer_push(tty);
831
832 dev_notice(port->dev, "overrun error\n");
833 copied++;
834 }
835
836 return copied;
837}
838
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900839static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 int copied = 0;
842 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700843 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000844 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900846 if (uart_handle_break(port))
847 return 0;
848
Paul Mundtb7a76e42006-02-01 03:06:06 -0800849 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#if defined(CONFIG_CPU_SH3)
851 /* Debounce break */
852 s->break_flag = 1;
853#endif
854 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900855 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800856 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900857
858 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Alan Cox33f0f882006-01-09 20:54:13 -0800861 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900863
Paul Mundtd830fa42008-12-16 19:29:38 +0900864 copied += sci_handle_fifo_overrun(port);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return copied;
867}
868
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900869static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900871#ifdef CONFIG_SERIAL_SH_SCI_DMA
872 struct uart_port *port = ptr;
873 struct sci_port *s = to_sci_port(port);
874
875 if (s->chan_rx) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900876 u16 scr = sci_in(port, SCSCR);
877 u16 ssr = sci_in(port, SCxSR);
878
879 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000880 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000881 disable_irq_nosync(irq);
882 scr |= 0x4000;
883 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900884 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000885 }
886 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900887 /* Clear current interrupt */
888 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000889 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
890 jiffies, s->rx_timeout);
891 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900892
893 return IRQ_HANDLED;
894 }
895#endif
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* I think sci_receive_chars has to be called irrespective
898 * of whether the I_IXOFF is set, otherwise, how is the interrupt
899 * to be disabled?
900 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900901 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 return IRQ_HANDLED;
904}
905
David Howells7d12e782006-10-05 14:55:46 +0100906static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900909 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stuart Menefyfd78a762009-07-29 23:01:24 +0900911 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900913 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return IRQ_HANDLED;
916}
917
David Howells7d12e782006-10-05 14:55:46 +0100918static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 struct uart_port *port = ptr;
921
922 /* Handle errors */
923 if (port->type == PORT_SCI) {
924 if (sci_handle_errors(port)) {
925 /* discard character in rx buffer */
926 sci_in(port, SCxSR);
927 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
928 }
929 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900930 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100931 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
935
936 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100937 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return IRQ_HANDLED;
940}
941
David Howells7d12e782006-10-05 14:55:46 +0100942static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct uart_port *port = ptr;
945
946 /* Handle BREAKs */
947 sci_handle_breaks(port);
948 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
949
950 return IRQ_HANDLED;
951}
952
Paul Mundtf43dc232011-01-13 15:06:28 +0900953static inline unsigned long port_rx_irq_mask(struct uart_port *port)
954{
955 /*
956 * Not all ports (such as SCIFA) will support REIE. Rather than
957 * special-casing the port type, we check the port initialization
958 * IRQ enable mask to see whether the IRQ is desired at all. If
959 * it's unset, it's logically inferred that there's no point in
960 * testing for it.
961 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900962 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900963}
964
David Howells7d12e782006-10-05 14:55:46 +0100965static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Magnus Damm44e18e92009-07-03 08:39:34 +0000967 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900968 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900969 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900970 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900972 ssr_status = sci_in(port, SCxSR);
973 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900974 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900977 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900978 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900979 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900980
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900981 /*
982 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
983 * DR flags
984 */
985 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900986 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900987 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000990 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900991 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000994 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900995 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Michael Trimarchia8884e32008-10-31 16:10:23 +0900997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001001 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * ports' baud rate when the peripheral clock changes.
1003 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001004static int sci_notifier(struct notifier_block *self,
1005 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Magnus Damme552de22009-01-21 15:13:42 +00001007 struct sci_port *sci_port;
1008 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Paul Mundtd535a232011-01-19 17:19:35 +09001010 sci_port = container_of(self, struct sci_port, freq_transition);
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +00001013 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +09001014 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +09001015
Paul Mundtd535a232011-01-19 17:19:35 +09001016 spin_lock_irqsave(&port->lock, flags);
1017 port->uartclk = clk_get_rate(sci_port->iclk);
1018 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +00001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return NOTIFY_OK;
1022}
Magnus Damm501b8252009-01-21 15:14:30 +00001023
1024static void sci_clk_enable(struct uart_port *port)
1025{
1026 struct sci_port *sci_port = to_sci_port(port);
1027
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001028 pm_runtime_get_sync(port->dev);
1029
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001030 clk_enable(sci_port->iclk);
1031 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
1032 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +00001033}
1034
1035static void sci_clk_disable(struct uart_port *port)
1036{
1037 struct sci_port *sci_port = to_sci_port(port);
1038
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001039 clk_disable(sci_port->fclk);
1040 clk_disable(sci_port->iclk);
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001041
1042 pm_runtime_put_sync(port->dev);
Magnus Damm501b8252009-01-21 15:14:30 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045static int sci_request_irq(struct sci_port *port)
1046{
1047 int i;
David Howells7d12e782006-10-05 14:55:46 +01001048 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1050 sci_br_interrupt,
1051 };
1052 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
1053 "SCI Transmit Data Empty", "SCI Break" };
1054
Paul Mundtce6738b2011-01-19 15:24:40 +09001055 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
1056 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001058
Paul Mundtce6738b2011-01-19 15:24:40 +09001059 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +09001060 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +09001061 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ENODEV;
1063 }
1064 } else {
1065 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +09001066 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +09001068
Paul Mundtce6738b2011-01-19 15:24:40 +09001069 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +09001070 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +09001071 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -ENODEV;
1073 }
1074 }
1075 }
1076
1077 return 0;
1078}
1079
1080static void sci_free_irq(struct sci_port *port)
1081{
1082 int i;
1083
Paul Mundtce6738b2011-01-19 15:24:40 +09001084 if (port->cfg->irqs[0] == port->cfg->irqs[1])
1085 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +09001086 else {
Paul Mundtce6738b2011-01-19 15:24:40 +09001087 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
1088 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 continue;
1090
Paul Mundtce6738b2011-01-19 15:24:40 +09001091 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 }
1094}
1095
1096static unsigned int sci_tx_empty(struct uart_port *port)
1097{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +00001098 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001099 unsigned short in_tx_fifo = scif_txfill(port);
1100
1101 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1105{
1106 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
1107 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
1108 /* If you have signals for DTR and DCD, please implement here. */
1109}
1110
1111static unsigned int sci_get_mctrl(struct uart_port *port)
1112{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001113 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 and CTS/RTS */
1115
1116 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
1117}
1118
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001119#ifdef CONFIG_SERIAL_SH_SCI_DMA
1120static void sci_dma_tx_complete(void *arg)
1121{
1122 struct sci_port *s = arg;
1123 struct uart_port *port = &s->port;
1124 struct circ_buf *xmit = &port->state->xmit;
1125 unsigned long flags;
1126
1127 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1128
1129 spin_lock_irqsave(&port->lock, flags);
1130
Magnus Dammf354a382010-03-19 04:47:01 +00001131 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001132 xmit->tail &= UART_XMIT_SIZE - 1;
1133
Magnus Dammf354a382010-03-19 04:47:01 +00001134 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001135
1136 async_tx_ack(s->desc_tx);
1137 s->cookie_tx = -EINVAL;
1138 s->desc_tx = NULL;
1139
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001140 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1141 uart_write_wakeup(port);
1142
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001143 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001144 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001145 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001146 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001147 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001148 }
1149
1150 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001151}
1152
1153/* Locking: called with port lock held */
1154static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
1155 size_t count)
1156{
1157 struct uart_port *port = &s->port;
1158 int i, active, room;
1159
1160 room = tty_buffer_request_room(tty, count);
1161
1162 if (s->active_rx == s->cookie_rx[0]) {
1163 active = 0;
1164 } else if (s->active_rx == s->cookie_rx[1]) {
1165 active = 1;
1166 } else {
1167 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1168 return 0;
1169 }
1170
1171 if (room < count)
1172 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1173 count - room);
1174 if (!room)
1175 return room;
1176
1177 for (i = 0; i < room; i++)
1178 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1179 TTY_NORMAL);
1180
1181 port->icount.rx += room;
1182
1183 return room;
1184}
1185
1186static void sci_dma_rx_complete(void *arg)
1187{
1188 struct sci_port *s = arg;
1189 struct uart_port *port = &s->port;
1190 struct tty_struct *tty = port->state->port.tty;
1191 unsigned long flags;
1192 int count;
1193
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001194 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001195
1196 spin_lock_irqsave(&port->lock, flags);
1197
1198 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1199
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001200 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001201
1202 spin_unlock_irqrestore(&port->lock, flags);
1203
1204 if (count)
1205 tty_flip_buffer_push(tty);
1206
1207 schedule_work(&s->work_rx);
1208}
1209
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001210static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1211{
1212 struct dma_chan *chan = s->chan_rx;
1213 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001214
1215 s->chan_rx = NULL;
1216 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1217 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001218 if (sg_dma_address(&s->sg_rx[0]))
1219 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1220 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001221 if (enable_pio)
1222 sci_start_rx(port);
1223}
1224
1225static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1226{
1227 struct dma_chan *chan = s->chan_tx;
1228 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001229
1230 s->chan_tx = NULL;
1231 s->cookie_tx = -EINVAL;
1232 dma_release_channel(chan);
1233 if (enable_pio)
1234 sci_start_tx(port);
1235}
1236
1237static void sci_submit_rx(struct sci_port *s)
1238{
1239 struct dma_chan *chan = s->chan_rx;
1240 int i;
1241
1242 for (i = 0; i < 2; i++) {
1243 struct scatterlist *sg = &s->sg_rx[i];
1244 struct dma_async_tx_descriptor *desc;
1245
1246 desc = chan->device->device_prep_slave_sg(chan,
1247 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1248
1249 if (desc) {
1250 s->desc_rx[i] = desc;
1251 desc->callback = sci_dma_rx_complete;
1252 desc->callback_param = s;
1253 s->cookie_rx[i] = desc->tx_submit(desc);
1254 }
1255
1256 if (!desc || s->cookie_rx[i] < 0) {
1257 if (i) {
1258 async_tx_ack(s->desc_rx[0]);
1259 s->cookie_rx[0] = -EINVAL;
1260 }
1261 if (desc) {
1262 async_tx_ack(desc);
1263 s->cookie_rx[i] = -EINVAL;
1264 }
1265 dev_warn(s->port.dev,
1266 "failed to re-start DMA, using PIO\n");
1267 sci_rx_dma_release(s, true);
1268 return;
1269 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001270 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1271 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001272 }
1273
1274 s->active_rx = s->cookie_rx[0];
1275
1276 dma_async_issue_pending(chan);
1277}
1278
1279static void work_fn_rx(struct work_struct *work)
1280{
1281 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1282 struct uart_port *port = &s->port;
1283 struct dma_async_tx_descriptor *desc;
1284 int new;
1285
1286 if (s->active_rx == s->cookie_rx[0]) {
1287 new = 0;
1288 } else if (s->active_rx == s->cookie_rx[1]) {
1289 new = 1;
1290 } else {
1291 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1292 return;
1293 }
1294 desc = s->desc_rx[new];
1295
1296 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1297 DMA_SUCCESS) {
1298 /* Handle incomplete DMA receive */
1299 struct tty_struct *tty = port->state->port.tty;
1300 struct dma_chan *chan = s->chan_rx;
1301 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1302 async_tx);
1303 unsigned long flags;
1304 int count;
1305
Linus Walleij05827632010-05-17 16:30:42 -07001306 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001307 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1308 sh_desc->partial, sh_desc->cookie);
1309
1310 spin_lock_irqsave(&port->lock, flags);
1311 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1312 spin_unlock_irqrestore(&port->lock, flags);
1313
1314 if (count)
1315 tty_flip_buffer_push(tty);
1316
1317 sci_submit_rx(s);
1318
1319 return;
1320 }
1321
1322 s->cookie_rx[new] = desc->tx_submit(desc);
1323 if (s->cookie_rx[new] < 0) {
1324 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1325 sci_rx_dma_release(s, true);
1326 return;
1327 }
1328
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001329 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001330
1331 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1332 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001333}
1334
1335static void work_fn_tx(struct work_struct *work)
1336{
1337 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1338 struct dma_async_tx_descriptor *desc;
1339 struct dma_chan *chan = s->chan_tx;
1340 struct uart_port *port = &s->port;
1341 struct circ_buf *xmit = &port->state->xmit;
1342 struct scatterlist *sg = &s->sg_tx;
1343
1344 /*
1345 * DMA is idle now.
1346 * Port xmit buffer is already mapped, and it is one page... Just adjust
1347 * offsets and lengths. Since it is a circular buffer, we have to
1348 * transmit till the end, and then the rest. Take the port lock to get a
1349 * consistent xmit buffer state.
1350 */
1351 spin_lock_irq(&port->lock);
1352 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001353 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001354 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001355 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001356 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001357 spin_unlock_irq(&port->lock);
1358
Magnus Dammf354a382010-03-19 04:47:01 +00001359 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001360
1361 desc = chan->device->device_prep_slave_sg(chan,
1362 sg, s->sg_len_tx, DMA_TO_DEVICE,
1363 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1364 if (!desc) {
1365 /* switch to PIO */
1366 sci_tx_dma_release(s, true);
1367 return;
1368 }
1369
1370 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1371
1372 spin_lock_irq(&port->lock);
1373 s->desc_tx = desc;
1374 desc->callback = sci_dma_tx_complete;
1375 desc->callback_param = s;
1376 spin_unlock_irq(&port->lock);
1377 s->cookie_tx = desc->tx_submit(desc);
1378 if (s->cookie_tx < 0) {
1379 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1380 /* switch to PIO */
1381 sci_tx_dma_release(s, true);
1382 return;
1383 }
1384
1385 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1386 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1387
1388 dma_async_issue_pending(chan);
1389}
1390#endif
1391
Russell Kingb129a8c2005-08-31 10:12:14 +01001392static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001394 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001395 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001397#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001398 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001399 u16 new, scr = sci_in(port, SCSCR);
1400 if (s->chan_tx)
1401 new = scr | 0x8000;
1402 else
1403 new = scr & ~0x8000;
1404 if (new != scr)
1405 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001406 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001407
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001408 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1409 s->cookie_tx < 0)
1410 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001411#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001412
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001413 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001414 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1415 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001416 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Russell Kingb129a8c2005-08-31 10:12:14 +01001420static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 unsigned short ctrl;
1423
1424 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001426
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001427 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001428 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001429
Paul Mundt8e698612009-06-24 19:44:32 +09001430 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001435static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 unsigned short ctrl;
1438
Paul Mundtf43dc232011-01-13 15:06:28 +09001439 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1440
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001441 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001442 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447static void sci_stop_rx(struct uart_port *port)
1448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 unsigned short ctrl;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001452
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001453 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001454 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001455
1456 ctrl &= ~port_rx_irq_mask(port);
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461static void sci_enable_ms(struct uart_port *port)
1462{
1463 /* Nothing here yet .. */
1464}
1465
1466static void sci_break_ctl(struct uart_port *port, int break_state)
1467{
1468 /* Nothing here yet .. */
1469}
1470
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001471#ifdef CONFIG_SERIAL_SH_SCI_DMA
1472static bool filter(struct dma_chan *chan, void *slave)
1473{
1474 struct sh_dmae_slave *param = slave;
1475
1476 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1477 param->slave_id);
1478
1479 if (param->dma_dev == chan->device->dev) {
1480 chan->private = param;
1481 return true;
1482 } else {
1483 return false;
1484 }
1485}
1486
1487static void rx_timer_fn(unsigned long arg)
1488{
1489 struct sci_port *s = (struct sci_port *)arg;
1490 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001491 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001492
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001493 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001494 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001495 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001496 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001497 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001498 dev_dbg(port->dev, "DMA Rx timed out\n");
1499 schedule_work(&s->work_rx);
1500}
1501
1502static void sci_request_dma(struct uart_port *port)
1503{
1504 struct sci_port *s = to_sci_port(port);
1505 struct sh_dmae_slave *param;
1506 struct dma_chan *chan;
1507 dma_cap_mask_t mask;
1508 int nent;
1509
1510 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001511 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001512
Paul Mundtce6738b2011-01-19 15:24:40 +09001513 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001514 return;
1515
1516 dma_cap_zero(mask);
1517 dma_cap_set(DMA_SLAVE, mask);
1518
1519 param = &s->param_tx;
1520
1521 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001522 param->slave_id = s->cfg->dma_slave_tx;
1523 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001524
1525 s->cookie_tx = -EINVAL;
1526 chan = dma_request_channel(mask, filter, param);
1527 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1528 if (chan) {
1529 s->chan_tx = chan;
1530 sg_init_table(&s->sg_tx, 1);
1531 /* UART circular tx buffer is an aligned page. */
1532 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1533 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1534 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1535 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1536 if (!nent)
1537 sci_tx_dma_release(s, false);
1538 else
1539 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1540 sg_dma_len(&s->sg_tx),
1541 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1542
1543 s->sg_len_tx = nent;
1544
1545 INIT_WORK(&s->work_tx, work_fn_tx);
1546 }
1547
1548 param = &s->param_rx;
1549
1550 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001551 param->slave_id = s->cfg->dma_slave_rx;
1552 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001553
1554 chan = dma_request_channel(mask, filter, param);
1555 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1556 if (chan) {
1557 dma_addr_t dma[2];
1558 void *buf[2];
1559 int i;
1560
1561 s->chan_rx = chan;
1562
1563 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1564 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1565 &dma[0], GFP_KERNEL);
1566
1567 if (!buf[0]) {
1568 dev_warn(port->dev,
1569 "failed to allocate dma buffer, using PIO\n");
1570 sci_rx_dma_release(s, true);
1571 return;
1572 }
1573
1574 buf[1] = buf[0] + s->buf_len_rx;
1575 dma[1] = dma[0] + s->buf_len_rx;
1576
1577 for (i = 0; i < 2; i++) {
1578 struct scatterlist *sg = &s->sg_rx[i];
1579
1580 sg_init_table(sg, 1);
1581 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1582 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001583 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001584 }
1585
1586 INIT_WORK(&s->work_rx, work_fn_rx);
1587 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1588
1589 sci_submit_rx(s);
1590 }
1591}
1592
1593static void sci_free_dma(struct uart_port *port)
1594{
1595 struct sci_port *s = to_sci_port(port);
1596
Paul Mundtce6738b2011-01-19 15:24:40 +09001597 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001598 return;
1599
1600 if (s->chan_tx)
1601 sci_tx_dma_release(s, false);
1602 if (s->chan_rx)
1603 sci_rx_dma_release(s, false);
1604}
Paul Mundt27bd1072011-01-19 15:37:31 +09001605#else
1606static inline void sci_request_dma(struct uart_port *port)
1607{
1608}
1609
1610static inline void sci_free_dma(struct uart_port *port)
1611{
1612}
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001613#endif
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615static int sci_startup(struct uart_port *port)
1616{
Magnus Damma5660ad2009-01-21 15:14:38 +00001617 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001618 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001620 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1621
Paul Mundte108b2c2006-09-27 16:32:13 +09001622 if (s->enable)
1623 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Paul Mundt073e84c2011-01-19 17:30:53 +09001625 ret = sci_request_irq(s);
1626 if (unlikely(ret < 0))
1627 return ret;
1628
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001629 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001630
Yoshinori Satod6569012005-10-14 15:59:12 -07001631 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001632 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 return 0;
1635}
1636
1637static void sci_shutdown(struct uart_port *port)
1638{
Magnus Damma5660ad2009-01-21 15:14:38 +00001639 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001641 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001644 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001645
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001646 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 sci_free_irq(s);
1648
Paul Mundte108b2c2006-09-27 16:32:13 +09001649 if (s->disable)
1650 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Paul Mundt26c92f32009-06-24 18:23:52 +09001653static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1654 unsigned long freq)
1655{
1656 switch (algo_id) {
1657 case SCBRR_ALGO_1:
1658 return ((freq + 16 * bps) / (16 * bps) - 1);
1659 case SCBRR_ALGO_2:
1660 return ((freq + 16 * bps) / (32 * bps) - 1);
1661 case SCBRR_ALGO_3:
1662 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1663 case SCBRR_ALGO_4:
1664 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1665 case SCBRR_ALGO_5:
1666 return (((freq * 1000 / 32) / bps) - 1);
1667 }
1668
1669 /* Warn, but use a safe default */
1670 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001671
Paul Mundt26c92f32009-06-24 18:23:52 +09001672 return ((freq + 16 * bps) / (32 * bps) - 1);
1673}
1674
Alan Cox606d0992006-12-08 02:38:45 -08001675static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1676 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Paul Mundt00b9de92009-06-24 17:53:33 +09001678 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001679 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001680 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001681 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Magnus Damm154280f2009-12-22 03:37:28 +00001683 /*
1684 * earlyprintk comes here early on with port->uartclk set to zero.
1685 * the clock framework is not up and running at this point so here
1686 * we assume that 115200 is the maximum baud rate. please note that
1687 * the baud rate is not programmed during earlyprintk - it is assumed
1688 * that the previous boot loader has enabled required clocks and
1689 * setup the baud rate generator hardware for us already.
1690 */
1691 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1692
1693 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1694 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001695 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Alexandre Courbot36003382011-03-03 08:04:42 +00001697 if (s->enable)
1698 s->enable(port);
1699
Paul Mundte108b2c2006-09-27 16:32:13 +09001700 do {
1701 status = sci_in(port, SCxSR);
1702 } while (!(status & SCxSR_TEND(port)));
1703
1704 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1705
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001706 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001707 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001708
1709 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001710
Paul Mundte108b2c2006-09-27 16:32:13 +09001711 if ((termios->c_cflag & CSIZE) == CS7)
1712 smr_val |= 0x40;
1713 if (termios->c_cflag & PARENB)
1714 smr_val |= 0x20;
1715 if (termios->c_cflag & PARODD)
1716 smr_val |= 0x30;
1717 if (termios->c_cflag & CSTOPB)
1718 smr_val |= 0x08;
1719
1720 uart_update_timeout(port, termios->c_cflag, baud);
1721
1722 sci_out(port, SCSMR, smr_val);
1723
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001724 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001725 s->cfg->scscr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001728 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1730 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001731 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 sci_out(port, SCBRR, t);
1735 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1736 }
1737
Paul Mundtd5701642008-12-16 20:07:27 +09001738 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001739 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001740
Paul Mundtce6738b2011-01-19 15:24:40 +09001741 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001743#ifdef CONFIG_SERIAL_SH_SCI_DMA
1744 /*
1745 * Calculate delay for 1.5 DMA buffers: see
1746 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1747 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1748 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1749 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1750 * sizes), but it has been found out experimentally, that this is not
1751 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1752 * as a minimum seem to work perfectly.
1753 */
1754 if (s->chan_rx) {
1755 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1756 port->fifosize / 2;
1757 dev_dbg(port->dev,
1758 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1759 s->rx_timeout * 1000 / HZ, port->timeout);
1760 if (s->rx_timeout < msecs_to_jiffies(20))
1761 s->rx_timeout = msecs_to_jiffies(20);
1762 }
1763#endif
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001766 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001767
1768 if (s->disable)
1769 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772static const char *sci_type(struct uart_port *port)
1773{
1774 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001775 case PORT_IRDA:
1776 return "irda";
1777 case PORT_SCI:
1778 return "sci";
1779 case PORT_SCIF:
1780 return "scif";
1781 case PORT_SCIFA:
1782 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001783 case PORT_SCIFB:
1784 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
Paul Mundtfa439722008-09-04 18:53:58 +09001787 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Paul Mundte2651642011-01-20 21:24:03 +09001790static inline unsigned long sci_port_size(struct uart_port *port)
1791{
1792 /*
1793 * Pick an arbitrary size that encapsulates all of the base
1794 * registers by default. This can be optimized later, or derived
1795 * from platform resource data at such a time that ports begin to
1796 * behave more erratically.
1797 */
1798 return 64;
1799}
1800
Paul Mundtf6e94952011-01-21 15:25:36 +09001801static int sci_remap_port(struct uart_port *port)
1802{
1803 unsigned long size = sci_port_size(port);
1804
1805 /*
1806 * Nothing to do if there's already an established membase.
1807 */
1808 if (port->membase)
1809 return 0;
1810
1811 if (port->flags & UPF_IOREMAP) {
1812 port->membase = ioremap_nocache(port->mapbase, size);
1813 if (unlikely(!port->membase)) {
1814 dev_err(port->dev, "can't remap port#%d\n", port->line);
1815 return -ENXIO;
1816 }
1817 } else {
1818 /*
1819 * For the simple (and majority of) cases where we don't
1820 * need to do any remapping, just cast the cookie
1821 * directly.
1822 */
1823 port->membase = (void __iomem *)port->mapbase;
1824 }
1825
1826 return 0;
1827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static void sci_release_port(struct uart_port *port)
1830{
Paul Mundte2651642011-01-20 21:24:03 +09001831 if (port->flags & UPF_IOREMAP) {
1832 iounmap(port->membase);
1833 port->membase = NULL;
1834 }
1835
1836 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
1839static int sci_request_port(struct uart_port *port)
1840{
Paul Mundte2651642011-01-20 21:24:03 +09001841 unsigned long size = sci_port_size(port);
1842 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001843 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Paul Mundt10205202011-01-21 16:00:31 +09001845 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001846 if (unlikely(res == NULL))
1847 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Paul Mundtf6e94952011-01-21 15:25:36 +09001849 ret = sci_remap_port(port);
1850 if (unlikely(ret != 0)) {
1851 release_resource(res);
1852 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001853 }
Paul Mundte2651642011-01-20 21:24:03 +09001854
1855 return 0;
1856}
1857
1858static void sci_config_port(struct uart_port *port, int flags)
1859{
1860 if (flags & UART_CONFIG_TYPE) {
1861 struct sci_port *sport = to_sci_port(port);
1862
1863 port->type = sport->cfg->type;
1864 sci_request_port(port);
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1869{
Magnus Damma5660ad2009-01-21 15:14:38 +00001870 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Paul Mundtce6738b2011-01-19 15:24:40 +09001872 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return -EINVAL;
1874 if (ser->baud_base < 2400)
1875 /* No paper tape reader for Mitch.. */
1876 return -EINVAL;
1877
1878 return 0;
1879}
1880
1881static struct uart_ops sci_uart_ops = {
1882 .tx_empty = sci_tx_empty,
1883 .set_mctrl = sci_set_mctrl,
1884 .get_mctrl = sci_get_mctrl,
1885 .start_tx = sci_start_tx,
1886 .stop_tx = sci_stop_tx,
1887 .stop_rx = sci_stop_rx,
1888 .enable_ms = sci_enable_ms,
1889 .break_ctl = sci_break_ctl,
1890 .startup = sci_startup,
1891 .shutdown = sci_shutdown,
1892 .set_termios = sci_set_termios,
1893 .type = sci_type,
1894 .release_port = sci_release_port,
1895 .request_port = sci_request_port,
1896 .config_port = sci_config_port,
1897 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001898#ifdef CONFIG_CONSOLE_POLL
1899 .poll_get_char = sci_poll_get_char,
1900 .poll_put_char = sci_poll_put_char,
1901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902};
1903
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001904static int __devinit sci_init_single(struct platform_device *dev,
1905 struct sci_port *sci_port,
1906 unsigned int index,
1907 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001908{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001909 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001910
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001911 port->ops = &sci_uart_ops;
1912 port->iotype = UPIO_MEM;
1913 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001914
1915 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001916 case PORT_SCIFB:
1917 port->fifosize = 256;
1918 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001919 case PORT_SCIFA:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001920 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001921 break;
1922 case PORT_SCIF:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001923 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001924 break;
1925 default:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001926 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001927 break;
1928 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001929
Paul Mundt61a69762011-06-14 12:40:19 +09001930 if (p->regtype == SCIx_PROBE_REGTYPE)
1931 BUG_ON(sci_probe_regmap(p) != 0);
1932
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001933 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001934 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1935 if (IS_ERR(sci_port->iclk)) {
1936 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1937 if (IS_ERR(sci_port->iclk)) {
1938 dev_err(&dev->dev, "can't get iclk\n");
1939 return PTR_ERR(sci_port->iclk);
1940 }
1941 }
1942
1943 /*
1944 * The function clock is optional, ignore it if we can't
1945 * find it.
1946 */
1947 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1948 if (IS_ERR(sci_port->fclk))
1949 sci_port->fclk = NULL;
1950
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001951 sci_port->enable = sci_clk_enable;
1952 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001953 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001954
1955 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001956 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001957
Magnus Damm7ed7e072009-01-21 15:14:14 +00001958 sci_port->break_timer.data = (unsigned long)sci_port;
1959 sci_port->break_timer.function = sci_break_timer;
1960 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001961
Paul Mundtdebf9502011-06-08 18:19:37 +09001962 /*
1963 * Establish some sensible defaults for the error detection.
1964 */
1965 if (!p->error_mask)
1966 p->error_mask = (p->type == PORT_SCI) ?
1967 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1968
1969 /*
1970 * Establish sensible defaults for the overrun detection, unless
1971 * the part has explicitly disabled support for it.
1972 */
1973 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1974 if (p->type == PORT_SCI)
1975 p->overrun_bit = 5;
1976 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1977 p->overrun_bit = 9;
1978 else
1979 p->overrun_bit = 0;
1980
1981 /*
1982 * Make the error mask inclusive of overrun detection, if
1983 * supported.
1984 */
1985 p->error_mask |= (1 << p->overrun_bit);
1986 }
1987
Paul Mundtce6738b2011-01-19 15:24:40 +09001988 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001989
Paul Mundtce6738b2011-01-19 15:24:40 +09001990 port->mapbase = p->mapbase;
1991 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001992 port->flags = p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09001993 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09001994
1995 /*
Paul Mundt61a69762011-06-14 12:40:19 +09001996 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09001997 * for the multi-IRQ ports, which is where we are primarily
1998 * concerned with the shutdown path synchronization.
1999 *
2000 * For the muxed case there's nothing more to do.
2001 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00002002 port->irq = p->irqs[SCIx_RXI_IRQ];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002003
Paul Mundt61a69762011-06-14 12:40:19 +09002004 port->serial_in = sci_serial_in;
2005 port->serial_out = sci_serial_out;
2006
Paul Mundtce6738b2011-01-19 15:24:40 +09002007 if (p->dma_dev)
2008 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
2009 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002010
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002011 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002015static void serial_console_putchar(struct uart_port *port, int ch)
2016{
2017 sci_poll_put_char(port, ch);
2018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/*
2021 * Print a string to the serial port trying not to disturb
2022 * any possible real use of the port...
2023 */
2024static void serial_console_write(struct console *co, const char *s,
2025 unsigned count)
2026{
Paul Mundt906b17d2011-01-21 16:19:53 +09002027 struct sci_port *sci_port = &sci_ports[co->index];
2028 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09002029 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002030
Magnus Damm501b8252009-01-21 15:14:30 +00002031 if (sci_port->enable)
2032 sci_port->enable(port);
2033
2034 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002035
2036 /* wait until fifo is empty and last bit has been transmitted */
2037 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
2038 while ((sci_in(port, SCxSR) & bits) != bits)
2039 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00002040
Magnus Damm345e5a72009-11-05 14:34:57 +00002041 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00002042 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002045static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002047 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct uart_port *port;
2049 int baud = 115200;
2050 int bits = 8;
2051 int parity = 'n';
2052 int flow = 'n';
2053 int ret;
2054
Paul Mundte108b2c2006-09-27 16:32:13 +09002055 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002056 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002057 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002058 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002059 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002060
Paul Mundt906b17d2011-01-21 16:19:53 +09002061 sci_port = &sci_ports[co->index];
2062 port = &sci_port->port;
2063
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002064 /*
2065 * Refuse to handle uninitialized ports.
2066 */
2067 if (!port->ops)
2068 return -ENODEV;
2069
Paul Mundtf6e94952011-01-21 15:25:36 +09002070 ret = sci_remap_port(port);
2071 if (unlikely(ret != 0))
2072 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002073
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002074 if (sci_port->enable)
2075 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (options)
2078 uart_parse_options(options, &baud, &parity, &bits, &flow);
2079
Magnus Damm501b8252009-01-21 15:14:30 +00002080 /* TODO: disable clock */
Paul Mundtab7cfb52011-06-01 14:47:42 +09002081 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084static struct console serial_console = {
2085 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002086 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 .write = serial_console_write,
2088 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002089 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002091 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092};
2093
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002094static struct console early_serial_console = {
2095 .name = "early_ttySC",
2096 .write = serial_console_write,
2097 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002098 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002099};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002100
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002101static char early_serial_buf[32];
2102
Paul Mundtecdf8a42011-01-21 00:05:48 +09002103static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2104{
2105 struct plat_sci_port *cfg = pdev->dev.platform_data;
2106
2107 if (early_serial_console.data)
2108 return -EEXIST;
2109
2110 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002111
Paul Mundt906b17d2011-01-21 16:19:53 +09002112 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002113
2114 serial_console_setup(&early_serial_console, early_serial_buf);
2115
2116 if (!strstr(early_serial_buf, "keep"))
2117 early_serial_console.flags |= CON_BOOT;
2118
2119 register_console(&early_serial_console);
2120 return 0;
2121}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002122
2123#define SCI_CONSOLE (&serial_console)
2124
Paul Mundtecdf8a42011-01-21 00:05:48 +09002125#else
2126static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2127{
2128 return -EINVAL;
2129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002131#define SCI_CONSOLE NULL
2132
2133#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135static char banner[] __initdata =
2136 KERN_INFO "SuperH SCI(F) driver initialized\n";
2137
2138static struct uart_driver sci_uart_driver = {
2139 .owner = THIS_MODULE,
2140 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .dev_name = "ttySC",
2142 .major = SCI_MAJOR,
2143 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002144 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .cons = SCI_CONSOLE,
2146};
2147
Paul Mundt54507f62009-05-08 23:48:33 +09002148static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002149{
Paul Mundtd535a232011-01-19 17:19:35 +09002150 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002151
Paul Mundtd535a232011-01-19 17:19:35 +09002152 cpufreq_unregister_notifier(&port->freq_transition,
2153 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002154
Paul Mundtd535a232011-01-19 17:19:35 +09002155 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002156
Paul Mundtd535a232011-01-19 17:19:35 +09002157 clk_put(port->iclk);
2158 clk_put(port->fclk);
2159
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002160 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00002161 return 0;
2162}
2163
Magnus Damm0ee70712009-01-21 15:13:50 +00002164static int __devinit sci_probe_single(struct platform_device *dev,
2165 unsigned int index,
2166 struct plat_sci_port *p,
2167 struct sci_port *sciport)
2168{
Magnus Damm0ee70712009-01-21 15:13:50 +00002169 int ret;
2170
2171 /* Sanity check */
2172 if (unlikely(index >= SCI_NPORTS)) {
2173 dev_notice(&dev->dev, "Attempting to register port "
2174 "%d when only %d are available.\n",
2175 index+1, SCI_NPORTS);
2176 dev_notice(&dev->dev, "Consider bumping "
2177 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
2178 return 0;
2179 }
2180
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002181 ret = sci_init_single(dev, sciport, index, p);
2182 if (ret)
2183 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002184
Paul Mundtd535a232011-01-19 17:19:35 +09002185 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00002186}
2187
Paul Mundte108b2c2006-09-27 16:32:13 +09002188static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Paul Mundte108b2c2006-09-27 16:32:13 +09002190 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09002191 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002192 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002193
Paul Mundtecdf8a42011-01-21 00:05:48 +09002194 /*
2195 * If we've come here via earlyprintk initialization, head off to
2196 * the special early probe. We don't have sufficient device state
2197 * to make it beyond this yet.
2198 */
2199 if (is_early_platform_device(dev))
2200 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002201
Paul Mundtd535a232011-01-19 17:19:35 +09002202 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002203
Paul Mundt906b17d2011-01-21 16:19:53 +09002204 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002205 if (ret)
2206 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00002207
Paul Mundtd535a232011-01-19 17:19:35 +09002208 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Paul Mundtd535a232011-01-19 17:19:35 +09002210 ret = cpufreq_register_notifier(&sp->freq_transition,
2211 CPUFREQ_TRANSITION_NOTIFIER);
2212 if (unlikely(ret < 0))
2213 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215#ifdef CONFIG_SH_STANDARD_BIOS
2216 sh_bios_gdb_detach();
2217#endif
2218
Paul Mundte108b2c2006-09-27 16:32:13 +09002219 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002220
2221err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002222 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002223 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002224}
2225
Paul Mundt6daa79b2009-06-15 07:07:38 +09002226static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002227{
Paul Mundtd535a232011-01-19 17:19:35 +09002228 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002229
Paul Mundtd535a232011-01-19 17:19:35 +09002230 if (sport)
2231 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002232
2233 return 0;
2234}
2235
Paul Mundt6daa79b2009-06-15 07:07:38 +09002236static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002237{
Paul Mundtd535a232011-01-19 17:19:35 +09002238 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002239
Paul Mundtd535a232011-01-19 17:19:35 +09002240 if (sport)
2241 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002242
2243 return 0;
2244}
2245
Alexey Dobriyan47145212009-12-14 18:00:08 -08002246static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002247 .suspend = sci_suspend,
2248 .resume = sci_resume,
2249};
2250
Paul Mundte108b2c2006-09-27 16:32:13 +09002251static struct platform_driver sci_driver = {
2252 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002253 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002254 .driver = {
2255 .name = "sh-sci",
2256 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002257 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002258 },
2259};
2260
2261static int __init sci_init(void)
2262{
2263 int ret;
2264
2265 printk(banner);
2266
Paul Mundte108b2c2006-09-27 16:32:13 +09002267 ret = uart_register_driver(&sci_uart_driver);
2268 if (likely(ret == 0)) {
2269 ret = platform_driver_register(&sci_driver);
2270 if (unlikely(ret))
2271 uart_unregister_driver(&sci_uart_driver);
2272 }
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return ret;
2275}
2276
2277static void __exit sci_exit(void)
2278{
Paul Mundte108b2c2006-09-27 16:32:13 +09002279 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 uart_unregister_driver(&sci_uart_driver);
2281}
2282
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002283#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2284early_platform_init_buffer("earlyprintk", &sci_driver,
2285 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287module_init(sci_init);
2288module_exit(sci_exit);
2289
Paul Mundte108b2c2006-09-27 16:32:13 +09002290MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002291MODULE_ALIAS("platform:sh-sci");