blob: 94deebda6da0e61a15fe51d2bcb18d40ffc0501a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundte108b2c2006-09-27 16:32:13 +09006 * Copyright (C) 2002 - 2006 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * based off of the old drivers/char/sh-sci.c by:
9 *
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
15 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#ifdef CONFIG_CPU_FREQ
44#include <linux/notifier.h>
45#include <linux/cpufreq.h>
46#endif
47
Paul Mundtb7a76e42006-02-01 03:06:06 -080048#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
Paul Mundtfa5da2f2007-03-08 17:27:37 +090049#include <linux/ctype.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080050#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090051#include <asm/sh_bios.h>
52#include <asm/kgdb.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Paul Mundte108b2c2006-09-27 16:32:13 +090055#include <asm/sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "sh-sci.h"
57
Paul Mundte108b2c2006-09-27 16:32:13 +090058struct sci_port {
59 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Paul Mundte108b2c2006-09-27 16:32:13 +090061 /* Port type */
62 unsigned int type;
63
64 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090065 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090066
67 /* Port pin configuration */
68 void (*init_pins)(struct uart_port *port,
69 unsigned int cflag);
70
71 /* Port enable callback */
72 void (*enable)(struct uart_port *port);
73
74 /* Port disable callback */
75 void (*disable)(struct uart_port *port);
76
77 /* Break timer */
78 struct timer_list break_timer;
79 int break_flag;
80};
81
82#ifdef CONFIG_SH_KGDB
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct sci_port *kgdb_sci_port;
Paul Mundte108b2c2006-09-27 16:32:13 +090084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Paul Mundte108b2c2006-09-27 16:32:13 +090087static struct sci_port *serial_console_port;
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010091static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Paul Mundte108b2c2006-09-27 16:32:13 +090093#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
94
95static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct uart_driver sci_uart_driver;
97
Paul Mundte108b2c2006-09-27 16:32:13 +090098#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
99 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
100static inline void handle_error(struct uart_port *port)
101{
102 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
104}
105
106static int get_char(struct uart_port *port)
107{
108 unsigned long flags;
109 unsigned short status;
110 int c;
111
Paul Mundte108b2c2006-09-27 16:32:13 +0900112 spin_lock_irqsave(&port->lock, flags);
113 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 status = sci_in(port, SCxSR);
115 if (status & SCxSR_ERRORS(port)) {
116 handle_error(port);
117 continue;
118 }
119 } while (!(status & SCxSR_RDxF(port)));
120 c = sci_in(port, SCxRDR);
121 sci_in(port, SCxSR); /* Dummy read */
122 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Paul Mundte108b2c2006-09-27 16:32:13 +0900123 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return c;
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
128
Paul Mundte108b2c2006-09-27 16:32:13 +0900129#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void put_char(struct uart_port *port, char c)
131{
132 unsigned long flags;
133 unsigned short status;
134
Paul Mundte108b2c2006-09-27 16:32:13 +0900135 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 do {
138 status = sci_in(port, SCxSR);
139 } while (!(status & SCxSR_TDxE(port)));
140
141 sci_out(port, SCxTDR, c);
142 sci_in(port, SCxSR); /* Dummy read */
143 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
144
Paul Mundte108b2c2006-09-27 16:32:13 +0900145 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
Paul Mundte108b2c2006-09-27 16:32:13 +0900147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Paul Mundte108b2c2006-09-27 16:32:13 +0900149#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static void put_string(struct sci_port *sci_port, const char *buffer, int count)
151{
152 struct uart_port *port = &sci_port->port;
153 const unsigned char *p = buffer;
154 int i;
155
156#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
157 int checksum;
158 int usegdb=0;
159
160#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtb7a76e42006-02-01 03:06:06 -0800161 /* This call only does a trap the first time it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * called, and so is safe to do here unconditionally
163 */
164 usegdb |= sh_bios_in_gdb_mode();
165#endif
166#ifdef CONFIG_SH_KGDB
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900167 usegdb |= (kgdb_in_gdb_mode && (sci_port == kgdb_sci_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif
169
170 if (usegdb) {
171 /* $<packet info>#<checksum>. */
172 do {
173 unsigned char c;
174 put_char(port, '$');
175 put_char(port, 'O'); /* 'O'utput to console */
176 checksum = 'O';
177
178 for (i=0; i<count; i++) { /* Don't use run length encoding */
179 int h, l;
180
181 c = *p++;
182 h = highhex(c);
183 l = lowhex(c);
184 put_char(port, h);
185 put_char(port, l);
186 checksum += h + l;
187 }
188 put_char(port, '#');
189 put_char(port, highhex(checksum));
190 put_char(port, lowhex(checksum));
191 } while (get_char(port) != '+');
192 } else
193#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
194 for (i=0; i<count; i++) {
195 if (*p == 10)
196 put_char(port, '\r');
197 put_char(port, *p++);
198 }
199}
200#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#ifdef CONFIG_SH_KGDB
Paul Mundte108b2c2006-09-27 16:32:13 +0900203static int kgdb_sci_getchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int c;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Keep trying to read a character, this could be neater */
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900208 while ((c = get_char(&kgdb_sci_port->port)) < 0)
Paul Mundte108b2c2006-09-27 16:32:13 +0900209 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return c;
212}
213
Paul Mundte108b2c2006-09-27 16:32:13 +0900214static inline void kgdb_sci_putchar(int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900216 put_char(&kgdb_sci_port->port, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif /* CONFIG_SH_KGDB */
219
220#if defined(__H8300S__)
221enum { sci_disable, sci_enable };
222
Paul Mundte108b2c2006-09-27 16:32:13 +0900223static void h8300_sci_config(struct uart_port* port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
226 int ch = (port->mapbase - SMR0) >> 3;
227 unsigned char mask = 1 << (ch+1);
228
229 if (ctrl == sci_disable) {
230 *mstpcrl |= mask;
231 } else {
232 *mstpcrl &= ~mask;
233 }
234}
Paul Mundte108b2c2006-09-27 16:32:13 +0900235
236static inline void h8300_sci_enable(struct uart_port *port)
237{
238 h8300_sci_config(port, sci_enable);
239}
240
241static inline void h8300_sci_disable(struct uart_port *port)
242{
243 h8300_sci_config(port, sci_disable);
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
246
Paul Mundte108b2c2006-09-27 16:32:13 +0900247#if defined(SCI_ONLY) || defined(SCI_AND_SCIF) && \
248 defined(__H8300H__) || defined(__H8300S__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
250{
251 int ch = (port->mapbase - SMR0) >> 3;
252
253 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900254 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
255 h8300_sci_pins[ch].rx,
256 H8300_GPIO_INPUT);
257 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
258 h8300_sci_pins[ch].tx,
259 H8300_GPIO_OUTPUT);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* tx mark output*/
262 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
263}
Paul Mundte108b2c2006-09-27 16:32:13 +0900264#else
265#define sci_init_pins_sci NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900267
268#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
269static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
270{
271 unsigned int fcr_val = 0;
272
273 if (cflag & CRTSCTS)
274 fcr_val |= SCFCR_MCE;
275
276 sci_out(port, SCFCR, fcr_val);
277}
278#else
279#define sci_init_pins_irda NULL
280#endif
281
282#ifdef SCI_ONLY
283#define sci_init_pins_scif NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285
286#if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
Paul Mundte108b2c2006-09-27 16:32:13 +0900287#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7710)
Paul Mundtb7a76e42006-02-01 03:06:06 -0800288/* SH7300 doesn't use RTS/CTS */
289static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
290{
291 sci_out(port, SCFCR, 0);
292}
293#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900294/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
296{
297 unsigned int fcr_val = 0;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800298 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Paul Mundtb7a76e42006-02-01 03:06:06 -0800300 /* We need to set SCPCR to enable RTS/CTS */
301 data = ctrl_inw(SCPCR);
302 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
303 ctrl_outw(data & 0x0fcf, SCPCR);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (cflag & CRTSCTS)
306 fcr_val |= SCFCR_MCE;
307 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* We need to set SCPCR to enable RTS/CTS */
309 data = ctrl_inw(SCPCR);
310 /* Clear out SCP7MD1,0, SCP4MD1,0,
311 Set SCP6MD1,0 = {01} (output) */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800312 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 data = ctrl_inb(SCPDR);
315 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800316 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Paul Mundtb7a76e42006-02-01 03:06:06 -0800318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sci_out(port, SCFCR, fcr_val);
320}
Paul Mundt41504c32006-12-11 20:28:03 +0900321#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
322static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
323{
324 unsigned int fcr_val = 0;
325
326 if (cflag & CRTSCTS) {
327 fcr_val |= SCFCR_MCE;
328
329 ctrl_outw(0x0000, PORT_PSCR);
330 } else {
331 unsigned short data;
332
333 data = ctrl_inw(PORT_PSCR);
334 data &= 0x033f;
335 data |= 0x0400;
336 ctrl_outw(data, PORT_PSCR);
337
338 ctrl_outw(ctrl_inw(SCSPTR0) & 0x17, SCSPTR0);
339 }
340
341 sci_out(port, SCFCR, fcr_val);
342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* For SH7750 */
345static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
346{
347 unsigned int fcr_val = 0;
348
349 if (cflag & CRTSCTS) {
350 fcr_val |= SCFCR_MCE;
351 } else {
Paul Mundte108b2c2006-09-27 16:32:13 +0900352#ifdef CONFIG_CPU_SUBTYPE_SH7343
353 /* Nothing */
Paul Mundt32351a22007-03-12 14:38:59 +0900354#elif defined(CONFIG_CPU_SUBTYPE_SH7780) || defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundtb7a76e42006-02-01 03:06:06 -0800355 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
356#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 sci_out(port, SCFCR, fcr_val);
361}
Paul Mundte108b2c2006-09-27 16:32:13 +0900362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Paul Mundt32351a22007-03-12 14:38:59 +0900364#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
365 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
366 defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundte108b2c2006-09-27 16:32:13 +0900367static inline int scif_txroom(struct uart_port *port)
368{
369 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0x7f);
370}
371
372static inline int scif_rxroom(struct uart_port *port)
373{
374 return sci_in(port, SCRFDR) & 0x7f;
375}
376#else
377static inline int scif_txroom(struct uart_port *port)
378{
379 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
380}
381
382static inline int scif_rxroom(struct uart_port *port)
383{
384 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif
387#endif /* SCIF_ONLY || SCI_AND_SCIF */
388
Paul Mundte108b2c2006-09-27 16:32:13 +0900389static inline int sci_txroom(struct uart_port *port)
390{
391 return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
392}
393
394static inline int sci_rxroom(struct uart_port *port)
395{
396 return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/* ********************************************************************** *
400 * the interrupt related routines *
401 * ********************************************************************** */
402
403static void sci_transmit_chars(struct uart_port *port)
404{
405 struct circ_buf *xmit = &port->info->xmit;
406 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned short status;
408 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900409 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 status = sci_in(port, SCxSR);
412 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ctrl = sci_in(port, SCSCR);
414 if (uart_circ_empty(xmit)) {
415 ctrl &= ~SCI_CTRL_FLAGS_TIE;
416 } else {
417 ctrl |= SCI_CTRL_FLAGS_TIE;
418 }
419 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 }
422
Paul Mundte108b2c2006-09-27 16:32:13 +0900423#ifndef SCI_ONLY
424 if (port->type == PORT_SCIF)
425 count = scif_txroom(port);
426 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800427#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900428 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 do {
431 unsigned char c;
432
433 if (port->x_char) {
434 c = port->x_char;
435 port->x_char = 0;
436 } else if (!uart_circ_empty(xmit) && !stopped) {
437 c = xmit->buf[xmit->tail];
438 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
439 } else {
440 break;
441 }
442
443 sci_out(port, SCxTDR, c);
444
445 port->icount.tx++;
446 } while (--count > 0);
447
448 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
449
450 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
451 uart_write_wakeup(port);
452 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100453 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ctrl = sci_in(port, SCSCR);
456
457#if !defined(SCI_ONLY)
458 if (port->type == PORT_SCIF) {
459 sci_in(port, SCxSR); /* Dummy read */
460 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
461 }
462#endif
463
464 ctrl |= SCI_CTRL_FLAGS_TIE;
465 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
469/* On SH3, SCIF may read end-of-break as a space->mark char */
470#define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
471
David Howells7d12e782006-10-05 14:55:46 +0100472static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Paul Mundte108b2c2006-09-27 16:32:13 +0900474 struct sci_port *sci_port = (struct sci_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct tty_struct *tty = port->info->tty;
476 int i, count, copied = 0;
477 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800478 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 status = sci_in(port, SCxSR);
481 if (!(status & SCxSR_RDxF(port)))
482 return;
483
484 while (1) {
485#if !defined(SCI_ONLY)
Paul Mundte108b2c2006-09-27 16:32:13 +0900486 if (port->type == PORT_SCIF)
487 count = scif_rxroom(port);
488 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800489#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900490 count = sci_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800493 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* If for any reason we can't copy more data, we're done! */
496 if (count == 0)
497 break;
498
499 if (port->type == PORT_SCI) {
500 char c = sci_in(port, SCxRDR);
David Howells7d12e782006-10-05 14:55:46 +0100501 if (uart_handle_sysrq_char(port, c) || sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 count = 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900503 else {
504 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 } else {
507 for (i=0; i<count; i++) {
508 char c = sci_in(port, SCxRDR);
509 status = sci_in(port, SCxSR);
510#if defined(CONFIG_CPU_SH3)
511 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900512 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if ((c == 0) &&
514 (status & SCxSR_FER(port))) {
515 count--; i--;
516 continue;
517 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Nonzero => end-of-break */
520 pr_debug("scif: debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900521 sci_port->break_flag = 0;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (STEPFN(c)) {
524 count--; i--;
525 continue;
526 }
527 }
528#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100529 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 count--; i--;
531 continue;
532 }
533
534 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800536 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 pr_debug("sci: frame error\n");
538 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800539 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 pr_debug("sci: parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800541 } else
542 flag = TTY_NORMAL;
543 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
546
547 sci_in(port, SCxSR); /* dummy read */
548 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 copied += count;
551 port->icount.rx += count;
552 }
553
554 if (copied) {
555 /* Tell the rest of the system the news. New characters! */
556 tty_flip_buffer_push(tty);
557 } else {
558 sci_in(port, SCxSR); /* dummy read */
559 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
560 }
561}
562
563#define SCI_BREAK_JIFFIES (HZ/20)
564/* The sci generates interrupts during the break,
565 * 1 per millisecond or so during the break period, for 9600 baud.
566 * So dont bother disabling interrupts.
567 * But dont want more than 1 break event.
568 * Use a kernel timer to periodically poll the rx line until
569 * the break is finished.
570 */
571static void sci_schedule_break_timer(struct sci_port *port)
572{
573 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
574 add_timer(&port->break_timer);
575}
576/* Ensure that two consecutive samples find the break over. */
577static void sci_break_timer(unsigned long data)
578{
Paul Mundte108b2c2006-09-27 16:32:13 +0900579 struct sci_port *port = (struct sci_port *)data;
580
581 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900583 sci_schedule_break_timer(port);
584 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* break is over. */
586 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900587 sci_schedule_break_timer(port);
588 } else
589 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592static inline int sci_handle_errors(struct uart_port *port)
593{
594 int copied = 0;
595 unsigned short status = sci_in(port, SCxSR);
596 struct tty_struct *tty = port->info->tty;
597
Paul Mundte108b2c2006-09-27 16:32:13 +0900598 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900600 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800601 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 pr_debug("sci: overrun error\n");
603 }
604
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (sci_rxd_in(port) == 0) {
607 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900608 struct sci_port *sci_port = (struct sci_port *)port;
609
610 if (!sci_port->break_flag) {
611 sci_port->break_flag = 1;
612 sci_schedule_break_timer(sci_port);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900615 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 pr_debug("sci: BREAK detected\n");
Paul Mundte108b2c2006-09-27 16:32:13 +0900618 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800619 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900621 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900623 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800624 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 pr_debug("sci: frame error\n");
626 }
627 }
628
Paul Mundte108b2c2006-09-27 16:32:13 +0900629 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900631 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
632 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 pr_debug("sci: parity error\n");
634 }
635
Alan Cox33f0f882006-01-09 20:54:13 -0800636 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 return copied;
640}
641
642static inline int sci_handle_breaks(struct uart_port *port)
643{
644 int copied = 0;
645 unsigned short status = sci_in(port, SCxSR);
646 struct tty_struct *tty = port->info->tty;
647 struct sci_port *s = &sci_ports[port->line];
648
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900649 if (uart_handle_break(port))
650 return 0;
651
Paul Mundtb7a76e42006-02-01 03:06:06 -0800652 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#if defined(CONFIG_CPU_SH3)
654 /* Debounce break */
655 s->break_flag = 1;
656#endif
657 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900658 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800659 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 pr_debug("sci: BREAK detected\n");
661 }
662
663#if defined(SCIF_ORER)
664 /* XXX: Handle SCIF overrun error */
665 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
666 sci_out(port, SCLSR, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +0900667 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 pr_debug("sci: overrun error\n");
670 }
671 }
672#endif
673
Alan Cox33f0f882006-01-09 20:54:13 -0800674 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return copied;
678}
679
David Howells7d12e782006-10-05 14:55:46 +0100680static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* I think sci_receive_chars has to be called irrespective
683 * of whether the I_IXOFF is set, otherwise, how is the interrupt
684 * to be disabled?
685 */
David Howells7d12e782006-10-05 14:55:46 +0100686 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 return IRQ_HANDLED;
689}
690
David Howells7d12e782006-10-05 14:55:46 +0100691static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct uart_port *port = ptr;
694
Paul Mundte108b2c2006-09-27 16:32:13 +0900695 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900697 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return IRQ_HANDLED;
700}
701
David Howells7d12e782006-10-05 14:55:46 +0100702static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct uart_port *port = ptr;
705
706 /* Handle errors */
707 if (port->type == PORT_SCI) {
708 if (sci_handle_errors(port)) {
709 /* discard character in rx buffer */
710 sci_in(port, SCxSR);
711 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
712 }
713 } else {
714#if defined(SCIF_ORER)
715 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
716 struct tty_struct *tty = port->info->tty;
717
718 sci_out(port, SCLSR, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800719 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
720 tty_flip_buffer_push(tty);
721 pr_debug("scif: overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723#endif
David Howells7d12e782006-10-05 14:55:46 +0100724 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
728
729 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100730 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 return IRQ_HANDLED;
733}
734
David Howells7d12e782006-10-05 14:55:46 +0100735static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct uart_port *port = ptr;
738
739 /* Handle BREAKs */
740 sci_handle_breaks(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900741
742#ifdef CONFIG_SH_KGDB
743 /* Break into the debugger if a break is detected */
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900744 breakpoint();
Paul Mundte108b2c2006-09-27 16:32:13 +0900745#endif
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
748
749 return IRQ_HANDLED;
750}
751
David Howells7d12e782006-10-05 14:55:46 +0100752static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned short ssr_status, scr_status;
755 struct uart_port *port = ptr;
756
757 ssr_status = sci_in(port,SCxSR);
758 scr_status = sci_in(port,SCSCR);
759
760 /* Tx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900761 if ((ssr_status & 0x0020) && (scr_status & 0x0080))
David Howells7d12e782006-10-05 14:55:46 +0100762 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Rx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900764 if ((ssr_status & 0x0002) && (scr_status & 0x0040))
David Howells7d12e782006-10-05 14:55:46 +0100765 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* Error Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900767 if ((ssr_status & 0x0080) && (scr_status & 0x0400))
David Howells7d12e782006-10-05 14:55:46 +0100768 sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* Break Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900770 if ((ssr_status & 0x0010) && (scr_status & 0x0200))
David Howells7d12e782006-10-05 14:55:46 +0100771 sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return IRQ_HANDLED;
774}
775
776#ifdef CONFIG_CPU_FREQ
777/*
778 * Here we define a transistion notifier so that we can update all of our
779 * ports' baud rate when the peripheral clock changes.
780 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900781static int sci_notifier(struct notifier_block *self,
782 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct cpufreq_freqs *freqs = p;
785 int i;
786
787 if ((phase == CPUFREQ_POSTCHANGE) ||
788 (phase == CPUFREQ_RESUMECHANGE)){
789 for (i = 0; i < SCI_NPORTS; i++) {
790 struct uart_port *port = &sci_ports[i].port;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800791 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * Update the uartclk per-port if frequency has
795 * changed, since it will no longer necessarily be
796 * consistent with the old frequency.
797 *
798 * Really we want to be able to do something like
799 * uart_change_speed() or something along those lines
800 * here to implicitly reset the per-port baud rate..
801 *
802 * Clean this up later..
803 */
Paul Mundt1d118562006-12-01 13:15:14 +0900804 clk = clk_get(NULL, "module_clk");
Paul Mundtb7a76e42006-02-01 03:06:06 -0800805 port->uartclk = clk_get_rate(clk) * 16;
806 clk_put(clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Paul Mundte108b2c2006-09-27 16:32:13 +0900809 printk(KERN_INFO "%s: got a postchange notification "
810 "for cpu %d (old %d, new %d)\n",
811 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 return NOTIFY_OK;
815}
816
817static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
818#endif /* CONFIG_CPU_FREQ */
819
820static int sci_request_irq(struct sci_port *port)
821{
822 int i;
David Howells7d12e782006-10-05 14:55:46 +0100823 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
825 sci_br_interrupt,
826 };
827 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
828 "SCI Transmit Data Empty", "SCI Break" };
829
830 if (port->irqs[0] == port->irqs[1]) {
831 if (!port->irqs[0]) {
832 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
833 return -ENODEV;
834 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900835
836 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900837 IRQF_DISABLED, "sci", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 printk(KERN_ERR "sci: Cannot allocate irq.\n");
839 return -ENODEV;
840 }
841 } else {
842 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
843 if (!port->irqs[i])
844 continue;
Paul Mundte108b2c2006-09-27 16:32:13 +0900845 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900846 IRQF_DISABLED, desc[i], port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 printk(KERN_ERR "sci: Cannot allocate irq.\n");
848 return -ENODEV;
849 }
850 }
851 }
852
853 return 0;
854}
855
856static void sci_free_irq(struct sci_port *port)
857{
858 int i;
859
860 if (port->irqs[0] == port->irqs[1]) {
861 if (!port->irqs[0])
862 printk("sci: sci_free_irq error\n");
863 else
864 free_irq(port->irqs[0], port);
865 } else {
866 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
867 if (!port->irqs[i])
868 continue;
869
870 free_irq(port->irqs[i], port);
871 }
872 }
873}
874
875static unsigned int sci_tx_empty(struct uart_port *port)
876{
877 /* Can't detect */
878 return TIOCSER_TEMT;
879}
880
881static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
882{
883 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
884 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
885 /* If you have signals for DTR and DCD, please implement here. */
886}
887
888static unsigned int sci_get_mctrl(struct uart_port *port)
889{
890 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
891 and CTS/RTS */
892
893 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
894}
895
Russell Kingb129a8c2005-08-31 10:12:14 +0100896static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Paul Mundte108b2c2006-09-27 16:32:13 +0900898 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Paul Mundte108b2c2006-09-27 16:32:13 +0900900 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
901 ctrl = sci_in(port, SCSCR);
902 ctrl |= SCI_CTRL_FLAGS_TIE;
903 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Russell Kingb129a8c2005-08-31 10:12:14 +0100906static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned short ctrl;
909
910 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 ctrl = sci_in(port, SCSCR);
912 ctrl &= ~SCI_CTRL_FLAGS_TIE;
913 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 unsigned short ctrl;
919
920 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ctrl = sci_in(port, SCSCR);
922 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
923 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static void sci_stop_rx(struct uart_port *port)
927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned short ctrl;
929
930 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ctrl = sci_in(port, SCSCR);
932 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
933 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936static void sci_enable_ms(struct uart_port *port)
937{
938 /* Nothing here yet .. */
939}
940
941static void sci_break_ctl(struct uart_port *port, int break_state)
942{
943 /* Nothing here yet .. */
944}
945
946static int sci_startup(struct uart_port *port)
947{
948 struct sci_port *s = &sci_ports[port->line];
949
Paul Mundte108b2c2006-09-27 16:32:13 +0900950 if (s->enable)
951 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700954 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sci_start_rx(port, 1);
956
957 return 0;
958}
959
960static void sci_shutdown(struct uart_port *port)
961{
962 struct sci_port *s = &sci_ports[port->line];
963
964 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100965 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sci_free_irq(s);
967
Paul Mundte108b2c2006-09-27 16:32:13 +0900968 if (s->disable)
969 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Alan Cox606d0992006-12-08 02:38:45 -0800972static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
973 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct sci_port *s = &sci_ports[port->line];
976 unsigned int status, baud, smr_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int t;
978
979 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 switch (baud) {
Paul Mundtb7a76e42006-02-01 03:06:06 -0800982 case 0:
983 t = -1;
984 break;
985 default:
986 {
987#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
Paul Mundt1d118562006-12-01 13:15:14 +0900988 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundtb7a76e42006-02-01 03:06:06 -0800989 t = SCBRR_VALUE(baud, clk_get_rate(clk));
990 clk_put(clk);
991#else
992 t = SCBRR_VALUE(baud);
993#endif
Paul Mundtb7a76e42006-02-01 03:06:06 -0800994 break;
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
Paul Mundte108b2c2006-09-27 16:32:13 +0900998 do {
999 status = sci_in(port, SCxSR);
1000 } while (!(status & SCxSR_TEND(port)));
1001
1002 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1003
1004#if !defined(SCI_ONLY)
1005 if (port->type == PORT_SCIF)
1006 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1007#endif
1008
1009 smr_val = sci_in(port, SCSMR) & 3;
1010 if ((termios->c_cflag & CSIZE) == CS7)
1011 smr_val |= 0x40;
1012 if (termios->c_cflag & PARENB)
1013 smr_val |= 0x20;
1014 if (termios->c_cflag & PARODD)
1015 smr_val |= 0x30;
1016 if (termios->c_cflag & CSTOPB)
1017 smr_val |= 0x08;
1018
1019 uart_update_timeout(port, termios->c_cflag, baud);
1020
1021 sci_out(port, SCSMR, smr_val);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (t > 0) {
1024 if(t >= 256) {
1025 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1026 t >>= 2;
1027 } else {
1028 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1029 }
1030 sci_out(port, SCBRR, t);
1031 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1032 }
1033
Paul Mundtb7a76e42006-02-01 03:06:06 -08001034 if (likely(s->init_pins))
1035 s->init_pins(port, termios->c_cflag);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 sci_out(port, SCSCR, SCSCR_INIT(port));
1038
1039 if ((termios->c_cflag & CREAD) != 0)
1040 sci_start_rx(port,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static const char *sci_type(struct uart_port *port)
1044{
1045 switch (port->type) {
1046 case PORT_SCI: return "sci";
1047 case PORT_SCIF: return "scif";
1048 case PORT_IRDA: return "irda";
1049 }
1050
1051 return 0;
1052}
1053
1054static void sci_release_port(struct uart_port *port)
1055{
1056 /* Nothing here yet .. */
1057}
1058
1059static int sci_request_port(struct uart_port *port)
1060{
1061 /* Nothing here yet .. */
1062 return 0;
1063}
1064
1065static void sci_config_port(struct uart_port *port, int flags)
1066{
1067 struct sci_port *s = &sci_ports[port->line];
1068
1069 port->type = s->type;
1070
Paul Mundte108b2c2006-09-27 16:32:13 +09001071 switch (port->type) {
1072 case PORT_SCI:
1073 s->init_pins = sci_init_pins_sci;
1074 break;
1075 case PORT_SCIF:
1076 s->init_pins = sci_init_pins_scif;
1077 break;
1078 case PORT_IRDA:
1079 s->init_pins = sci_init_pins_irda;
1080 break;
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1084 if (port->mapbase == 0)
1085 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1086
Paul Mundte108b2c2006-09-27 16:32:13 +09001087 port->membase = (void __iomem *)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#endif
1089}
1090
1091static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1092{
1093 struct sci_port *s = &sci_ports[port->line];
1094
1095 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1096 return -EINVAL;
1097 if (ser->baud_base < 2400)
1098 /* No paper tape reader for Mitch.. */
1099 return -EINVAL;
1100
1101 return 0;
1102}
1103
1104static struct uart_ops sci_uart_ops = {
1105 .tx_empty = sci_tx_empty,
1106 .set_mctrl = sci_set_mctrl,
1107 .get_mctrl = sci_get_mctrl,
1108 .start_tx = sci_start_tx,
1109 .stop_tx = sci_stop_tx,
1110 .stop_rx = sci_stop_rx,
1111 .enable_ms = sci_enable_ms,
1112 .break_ctl = sci_break_ctl,
1113 .startup = sci_startup,
1114 .shutdown = sci_shutdown,
1115 .set_termios = sci_set_termios,
1116 .type = sci_type,
1117 .release_port = sci_release_port,
1118 .request_port = sci_request_port,
1119 .config_port = sci_config_port,
1120 .verify_port = sci_verify_port,
1121};
1122
Paul Mundte108b2c2006-09-27 16:32:13 +09001123static void __init sci_init_ports(void)
1124{
1125 static int first = 1;
1126 int i;
1127
1128 if (!first)
1129 return;
1130
1131 first = 0;
1132
1133 for (i = 0; i < SCI_NPORTS; i++) {
1134 sci_ports[i].port.ops = &sci_uart_ops;
1135 sci_ports[i].port.iotype = UPIO_MEM;
1136 sci_ports[i].port.line = i;
1137 sci_ports[i].port.fifosize = 1;
1138
1139#if defined(__H8300H__) || defined(__H8300S__)
1140#ifdef __H8300S__
1141 sci_ports[i].enable = h8300_sci_enable;
1142 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001144 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1145#elif defined(CONFIG_SUPERH64)
1146 sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
1147#else
1148 /*
1149 * XXX: We should use a proper SCI/SCIF clock
1150 */
1151 {
Paul Mundt1d118562006-12-01 13:15:14 +09001152 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundte108b2c2006-09-27 16:32:13 +09001153 sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
1154 clk_put(clk);
1155 }
1156#endif
1157
1158 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1159 sci_ports[i].break_timer.function = sci_break_timer;
1160
1161 init_timer(&sci_ports[i].break_timer);
1162 }
1163}
1164
1165int __init early_sci_setup(struct uart_port *port)
1166{
1167 if (unlikely(port->line > SCI_NPORTS))
1168 return -ENODEV;
1169
1170 sci_init_ports();
1171
1172 sci_ports[port->line].port.membase = port->membase;
1173 sci_ports[port->line].port.mapbase = port->mapbase;
1174 sci_ports[port->line].port.type = port->type;
1175
1176 return 0;
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1180/*
1181 * Print a string to the serial port trying not to disturb
1182 * any possible real use of the port...
1183 */
1184static void serial_console_write(struct console *co, const char *s,
1185 unsigned count)
1186{
1187 put_string(serial_console_port, s, count);
1188}
1189
1190static int __init serial_console_setup(struct console *co, char *options)
1191{
1192 struct uart_port *port;
1193 int baud = 115200;
1194 int bits = 8;
1195 int parity = 'n';
1196 int flow = 'n';
1197 int ret;
1198
Paul Mundte108b2c2006-09-27 16:32:13 +09001199 /*
1200 * Check whether an invalid uart number has been specified, and
1201 * if so, search for the first available port that does have
1202 * console support.
1203 */
1204 if (co->index >= SCI_NPORTS)
1205 co->index = 0;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 serial_console_port = &sci_ports[co->index];
1208 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001211 * Also need to check port->type, we don't actually have any
1212 * UPIO_PORT ports, but uart_report_port() handily misreports
1213 * it anyways if we don't have a port available by the time this is
1214 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001216 if (!port->type)
1217 return -ENODEV;
1218 if (!port->membase || !port->mapbase)
1219 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001220
Paul Mundte108b2c2006-09-27 16:32:13 +09001221 port->type = serial_console_port->type;
1222
1223 if (port->flags & UPF_IOREMAP)
1224 sci_config_port(port, 0);
1225
1226 if (serial_console_port->enable)
1227 serial_console_port->enable(port);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (options)
1230 uart_parse_options(options, &baud, &parity, &bits, &flow);
1231
1232 ret = uart_set_options(port, co, baud, parity, bits, flow);
1233#if defined(__H8300H__) || defined(__H8300S__)
1234 /* disable rx interrupt */
1235 if (ret == 0)
1236 sci_stop_rx(port);
1237#endif
1238 return ret;
1239}
1240
1241static struct console serial_console = {
1242 .name = "ttySC",
1243 .device = uart_console_device,
1244 .write = serial_console_write,
1245 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001246 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 .index = -1,
1248 .data = &sci_uart_driver,
1249};
1250
1251static int __init sci_console_init(void)
1252{
Paul Mundte108b2c2006-09-27 16:32:13 +09001253 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 register_console(&serial_console);
1255 return 0;
1256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257console_initcall(sci_console_init);
1258#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1259
1260#ifdef CONFIG_SH_KGDB
1261/*
1262 * FIXME: Most of this can go away.. at the moment, we rely on
1263 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1264 * most of that can easily be done here instead.
1265 *
1266 * For the time being, just accept the values that were parsed earlier..
1267 */
1268static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1269 int *parity, int *bits)
1270{
1271 *baud = kgdb_baud;
1272 *parity = tolower(kgdb_parity);
1273 *bits = kgdb_bits - '0';
1274}
1275
1276/*
1277 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1278 * care of the early-on initialization for kgdb, regardless of whether we
1279 * actually use kgdb as a console or not.
1280 *
1281 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1282 */
1283int __init kgdb_console_setup(struct console *co, char *options)
1284{
1285 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1286 int baud = 38400;
1287 int bits = 8;
1288 int parity = 'n';
1289 int flow = 'n';
1290
Paul Mundtb7a76e42006-02-01 03:06:06 -08001291 if (co->index != kgdb_portnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 co->index = kgdb_portnum;
1293
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001294 kgdb_sci_port = &sci_ports[co->index];
1295 port = &kgdb_sci_port->port;
1296
1297 /*
1298 * Also need to check port->type, we don't actually have any
1299 * UPIO_PORT ports, but uart_report_port() handily misreports
1300 * it anyways if we don't have a port available by the time this is
1301 * called.
1302 */
1303 if (!port->type)
1304 return -ENODEV;
1305 if (!port->membase || !port->mapbase)
1306 return -ENODEV;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (options)
1309 uart_parse_options(options, &baud, &parity, &bits, &flow);
1310 else
1311 kgdb_console_get_options(port, &baud, &parity, &bits);
1312
1313 kgdb_getchar = kgdb_sci_getchar;
1314 kgdb_putchar = kgdb_sci_putchar;
1315
1316 return uart_set_options(port, co, baud, parity, bits, flow);
1317}
1318#endif /* CONFIG_SH_KGDB */
1319
1320#ifdef CONFIG_SH_KGDB_CONSOLE
1321static struct console kgdb_console = {
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001322 .name = "ttySC",
1323 .device = uart_console_device,
1324 .write = kgdb_console_write,
1325 .setup = kgdb_console_setup,
1326 .flags = CON_PRINTBUFFER,
1327 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 .data = &sci_uart_driver,
1329};
1330
1331/* Register the KGDB console so we get messages (d'oh!) */
1332static int __init kgdb_console_init(void)
1333{
Paul Mundte108b2c2006-09-27 16:32:13 +09001334 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 register_console(&kgdb_console);
1336 return 0;
1337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338console_initcall(kgdb_console_init);
1339#endif /* CONFIG_SH_KGDB_CONSOLE */
1340
1341#if defined(CONFIG_SH_KGDB_CONSOLE)
1342#define SCI_CONSOLE &kgdb_console
1343#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1344#define SCI_CONSOLE &serial_console
1345#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001346#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#endif
1348
1349static char banner[] __initdata =
1350 KERN_INFO "SuperH SCI(F) driver initialized\n";
1351
1352static struct uart_driver sci_uart_driver = {
1353 .owner = THIS_MODULE,
1354 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 .dev_name = "ttySC",
1356 .major = SCI_MAJOR,
1357 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001358 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .cons = SCI_CONSOLE,
1360};
1361
Paul Mundte108b2c2006-09-27 16:32:13 +09001362/*
1363 * Register a set of serial devices attached to a platform device. The
1364 * list is terminated with a zero flags entry, which means we expect
1365 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1366 * remapping (such as sh64) should also set UPF_IOREMAP.
1367 */
1368static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Paul Mundte108b2c2006-09-27 16:32:13 +09001370 struct plat_sci_port *p = dev->dev.platform_data;
1371 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Paul Mundt32351a22007-03-12 14:38:59 +09001373 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001374 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Paul Mundt32351a22007-03-12 14:38:59 +09001376 /* Sanity check */
1377 if (unlikely(i == SCI_NPORTS)) {
1378 dev_notice(&dev->dev, "Attempting to register port "
1379 "%d when only %d are available.\n",
1380 i+1, SCI_NPORTS);
1381 dev_notice(&dev->dev, "Consider bumping "
1382 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1383 break;
1384 }
1385
Paul Mundte108b2c2006-09-27 16:32:13 +09001386 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001387
Paul Mundte108b2c2006-09-27 16:32:13 +09001388 /*
1389 * For the simple (and majority of) cases where we don't need
1390 * to do any remapping, just cast the cookie directly.
1391 */
1392 if (p->mapbase && !p->membase && !(p->flags & UPF_IOREMAP))
1393 p->membase = (void __iomem *)p->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Paul Mundte108b2c2006-09-27 16:32:13 +09001395 sciport->port.membase = p->membase;
1396
1397 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1398 sciport->port.flags = p->flags;
1399 sciport->port.dev = &dev->dev;
1400
1401 sciport->type = sciport->port.type = p->type;
1402
1403 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1404
1405 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001408#if defined(CONFIG_SH_KGDB) && !defined(CONFIG_SH_KGDB_CONSOLE)
1409 kgdb_sci_port = &sci_ports[kgdb_portnum];
1410 kgdb_getchar = kgdb_sci_getchar;
1411 kgdb_putchar = kgdb_sci_putchar;
1412#endif
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#ifdef CONFIG_CPU_FREQ
1415 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Paul Mundte108b2c2006-09-27 16:32:13 +09001416 dev_info(&dev->dev, "sci: CPU frequency notifier registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417#endif
1418
1419#ifdef CONFIG_SH_STANDARD_BIOS
1420 sh_bios_gdb_detach();
1421#endif
1422
Paul Mundte108b2c2006-09-27 16:32:13 +09001423 return 0;
1424}
1425
1426static int __devexit sci_remove(struct platform_device *dev)
1427{
1428 int i;
1429
1430 for (i = 0; i < SCI_NPORTS; i++)
1431 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1432
1433 return 0;
1434}
1435
1436static int sci_suspend(struct platform_device *dev, pm_message_t state)
1437{
1438 int i;
1439
1440 for (i = 0; i < SCI_NPORTS; i++) {
1441 struct sci_port *p = &sci_ports[i];
1442
1443 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1444 uart_suspend_port(&sci_uart_driver, &p->port);
1445 }
1446
1447 return 0;
1448}
1449
1450static int sci_resume(struct platform_device *dev)
1451{
1452 int i;
1453
1454 for (i = 0; i < SCI_NPORTS; i++) {
1455 struct sci_port *p = &sci_ports[i];
1456
1457 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1458 uart_resume_port(&sci_uart_driver, &p->port);
1459 }
1460
1461 return 0;
1462}
1463
1464static struct platform_driver sci_driver = {
1465 .probe = sci_probe,
1466 .remove = __devexit_p(sci_remove),
1467 .suspend = sci_suspend,
1468 .resume = sci_resume,
1469 .driver = {
1470 .name = "sh-sci",
1471 .owner = THIS_MODULE,
1472 },
1473};
1474
1475static int __init sci_init(void)
1476{
1477 int ret;
1478
1479 printk(banner);
1480
1481 sci_init_ports();
1482
1483 ret = uart_register_driver(&sci_uart_driver);
1484 if (likely(ret == 0)) {
1485 ret = platform_driver_register(&sci_driver);
1486 if (unlikely(ret))
1487 uart_unregister_driver(&sci_uart_driver);
1488 }
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return ret;
1491}
1492
1493static void __exit sci_exit(void)
1494{
Paul Mundte108b2c2006-09-27 16:32:13 +09001495 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 uart_unregister_driver(&sci_uart_driver);
1497}
1498
1499module_init(sci_init);
1500module_exit(sci_exit);
1501
Paul Mundte108b2c2006-09-27 16:32:13 +09001502MODULE_LICENSE("GPL");