blob: 0a34fa9e07336082a2e8ce3f3924178ff256299b [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)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900287#if defined(CONFIG_CPU_SUBTYPE_SH7300)
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}
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900293#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
294static void sci_init_pins_scif(struct uart_port* port, unsigned int cflag)
295{
296 unsigned int fcr_val = 0;
297
298 set_sh771x_scif_pfc(port);
299 if (cflag & CRTSCTS) {
300 fcr_val |= SCFCR_MCE;
301 }
302 sci_out(port, SCFCR, fcr_val);
303}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800304#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900305/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
307{
308 unsigned int fcr_val = 0;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800309 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Paul Mundtb7a76e42006-02-01 03:06:06 -0800311 /* We need to set SCPCR to enable RTS/CTS */
312 data = ctrl_inw(SCPCR);
313 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
314 ctrl_outw(data & 0x0fcf, SCPCR);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (cflag & CRTSCTS)
317 fcr_val |= SCFCR_MCE;
318 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* We need to set SCPCR to enable RTS/CTS */
320 data = ctrl_inw(SCPCR);
321 /* Clear out SCP7MD1,0, SCP4MD1,0,
322 Set SCP6MD1,0 = {01} (output) */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800323 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 data = ctrl_inb(SCPDR);
326 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800327 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Paul Mundtb7a76e42006-02-01 03:06:06 -0800329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 sci_out(port, SCFCR, fcr_val);
331}
Paul Mundt41504c32006-12-11 20:28:03 +0900332#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
333static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
334{
335 unsigned int fcr_val = 0;
336
337 if (cflag & CRTSCTS) {
338 fcr_val |= SCFCR_MCE;
339
340 ctrl_outw(0x0000, PORT_PSCR);
341 } else {
342 unsigned short data;
343
344 data = ctrl_inw(PORT_PSCR);
345 data &= 0x033f;
346 data |= 0x0400;
347 ctrl_outw(data, PORT_PSCR);
348
349 ctrl_outw(ctrl_inw(SCSPTR0) & 0x17, SCSPTR0);
350 }
351
352 sci_out(port, SCFCR, fcr_val);
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* For SH7750 */
356static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
357{
358 unsigned int fcr_val = 0;
359
360 if (cflag & CRTSCTS) {
361 fcr_val |= SCFCR_MCE;
362 } else {
Paul Mundte108b2c2006-09-27 16:32:13 +0900363#ifdef CONFIG_CPU_SUBTYPE_SH7343
364 /* Nothing */
Paul Mundt32351a22007-03-12 14:38:59 +0900365#elif defined(CONFIG_CPU_SUBTYPE_SH7780) || defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundtb7a76e42006-02-01 03:06:06 -0800366 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
367#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 sci_out(port, SCFCR, fcr_val);
372}
Paul Mundte108b2c2006-09-27 16:32:13 +0900373#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Paul Mundt32351a22007-03-12 14:38:59 +0900375#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
376 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
377 defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundte108b2c2006-09-27 16:32:13 +0900378static inline int scif_txroom(struct uart_port *port)
379{
380 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0x7f);
381}
382
383static inline int scif_rxroom(struct uart_port *port)
384{
385 return sci_in(port, SCRFDR) & 0x7f;
386}
387#else
388static inline int scif_txroom(struct uart_port *port)
389{
390 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
391}
392
393static inline int scif_rxroom(struct uart_port *port)
394{
395 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
398#endif /* SCIF_ONLY || SCI_AND_SCIF */
399
Paul Mundte108b2c2006-09-27 16:32:13 +0900400static inline int sci_txroom(struct uart_port *port)
401{
402 return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
403}
404
405static inline int sci_rxroom(struct uart_port *port)
406{
407 return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* ********************************************************************** *
411 * the interrupt related routines *
412 * ********************************************************************** */
413
414static void sci_transmit_chars(struct uart_port *port)
415{
416 struct circ_buf *xmit = &port->info->xmit;
417 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned short status;
419 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900420 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 status = sci_in(port, SCxSR);
423 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ctrl = sci_in(port, SCSCR);
425 if (uart_circ_empty(xmit)) {
426 ctrl &= ~SCI_CTRL_FLAGS_TIE;
427 } else {
428 ctrl |= SCI_CTRL_FLAGS_TIE;
429 }
430 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return;
432 }
433
Paul Mundte108b2c2006-09-27 16:32:13 +0900434#ifndef SCI_ONLY
435 if (port->type == PORT_SCIF)
436 count = scif_txroom(port);
437 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800438#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900439 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 do {
442 unsigned char c;
443
444 if (port->x_char) {
445 c = port->x_char;
446 port->x_char = 0;
447 } else if (!uart_circ_empty(xmit) && !stopped) {
448 c = xmit->buf[xmit->tail];
449 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
450 } else {
451 break;
452 }
453
454 sci_out(port, SCxTDR, c);
455
456 port->icount.tx++;
457 } while (--count > 0);
458
459 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
460
461 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
462 uart_write_wakeup(port);
463 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100464 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ctrl = sci_in(port, SCSCR);
467
468#if !defined(SCI_ONLY)
469 if (port->type == PORT_SCIF) {
470 sci_in(port, SCxSR); /* Dummy read */
471 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
472 }
473#endif
474
475 ctrl |= SCI_CTRL_FLAGS_TIE;
476 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478}
479
480/* On SH3, SCIF may read end-of-break as a space->mark char */
481#define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
482
David Howells7d12e782006-10-05 14:55:46 +0100483static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Paul Mundte108b2c2006-09-27 16:32:13 +0900485 struct sci_port *sci_port = (struct sci_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct tty_struct *tty = port->info->tty;
487 int i, count, copied = 0;
488 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800489 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 status = sci_in(port, SCxSR);
492 if (!(status & SCxSR_RDxF(port)))
493 return;
494
495 while (1) {
496#if !defined(SCI_ONLY)
Paul Mundte108b2c2006-09-27 16:32:13 +0900497 if (port->type == PORT_SCIF)
498 count = scif_rxroom(port);
499 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800500#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900501 count = sci_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800504 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* If for any reason we can't copy more data, we're done! */
507 if (count == 0)
508 break;
509
510 if (port->type == PORT_SCI) {
511 char c = sci_in(port, SCxRDR);
David Howells7d12e782006-10-05 14:55:46 +0100512 if (uart_handle_sysrq_char(port, c) || sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 count = 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900514 else {
515 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 } else {
518 for (i=0; i<count; i++) {
519 char c = sci_in(port, SCxRDR);
520 status = sci_in(port, SCxSR);
521#if defined(CONFIG_CPU_SH3)
522 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900523 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if ((c == 0) &&
525 (status & SCxSR_FER(port))) {
526 count--; i--;
527 continue;
528 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Nonzero => end-of-break */
531 pr_debug("scif: debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900532 sci_port->break_flag = 0;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (STEPFN(c)) {
535 count--; i--;
536 continue;
537 }
538 }
539#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100540 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 count--; i--;
542 continue;
543 }
544
545 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800547 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 pr_debug("sci: frame error\n");
549 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800550 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pr_debug("sci: parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800552 } else
553 flag = TTY_NORMAL;
554 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 }
557
558 sci_in(port, SCxSR); /* dummy read */
559 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 copied += count;
562 port->icount.rx += count;
563 }
564
565 if (copied) {
566 /* Tell the rest of the system the news. New characters! */
567 tty_flip_buffer_push(tty);
568 } else {
569 sci_in(port, SCxSR); /* dummy read */
570 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
571 }
572}
573
574#define SCI_BREAK_JIFFIES (HZ/20)
575/* The sci generates interrupts during the break,
576 * 1 per millisecond or so during the break period, for 9600 baud.
577 * So dont bother disabling interrupts.
578 * But dont want more than 1 break event.
579 * Use a kernel timer to periodically poll the rx line until
580 * the break is finished.
581 */
582static void sci_schedule_break_timer(struct sci_port *port)
583{
584 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
585 add_timer(&port->break_timer);
586}
587/* Ensure that two consecutive samples find the break over. */
588static void sci_break_timer(unsigned long data)
589{
Paul Mundte108b2c2006-09-27 16:32:13 +0900590 struct sci_port *port = (struct sci_port *)data;
591
592 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900594 sci_schedule_break_timer(port);
595 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* break is over. */
597 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900598 sci_schedule_break_timer(port);
599 } else
600 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static inline int sci_handle_errors(struct uart_port *port)
604{
605 int copied = 0;
606 unsigned short status = sci_in(port, SCxSR);
607 struct tty_struct *tty = port->info->tty;
608
Paul Mundte108b2c2006-09-27 16:32:13 +0900609 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900611 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800612 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 pr_debug("sci: overrun error\n");
614 }
615
Paul Mundte108b2c2006-09-27 16:32:13 +0900616 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (sci_rxd_in(port) == 0) {
618 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900619 struct sci_port *sci_port = (struct sci_port *)port;
620
621 if (!sci_port->break_flag) {
622 sci_port->break_flag = 1;
623 sci_schedule_break_timer(sci_port);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900626 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 pr_debug("sci: BREAK detected\n");
Paul Mundte108b2c2006-09-27 16:32:13 +0900629 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800630 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900634 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800635 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pr_debug("sci: frame error\n");
637 }
638 }
639
Paul Mundte108b2c2006-09-27 16:32:13 +0900640 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900642 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
643 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 pr_debug("sci: parity error\n");
645 }
646
Alan Cox33f0f882006-01-09 20:54:13 -0800647 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return copied;
651}
652
653static inline int sci_handle_breaks(struct uart_port *port)
654{
655 int copied = 0;
656 unsigned short status = sci_in(port, SCxSR);
657 struct tty_struct *tty = port->info->tty;
658 struct sci_port *s = &sci_ports[port->line];
659
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900660 if (uart_handle_break(port))
661 return 0;
662
Paul Mundtb7a76e42006-02-01 03:06:06 -0800663 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#if defined(CONFIG_CPU_SH3)
665 /* Debounce break */
666 s->break_flag = 1;
667#endif
668 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900669 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800670 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 pr_debug("sci: BREAK detected\n");
672 }
673
674#if defined(SCIF_ORER)
675 /* XXX: Handle SCIF overrun error */
676 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
677 sci_out(port, SCLSR, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +0900678 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 pr_debug("sci: overrun error\n");
681 }
682 }
683#endif
684
Alan Cox33f0f882006-01-09 20:54:13 -0800685 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return copied;
689}
690
David Howells7d12e782006-10-05 14:55:46 +0100691static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* I think sci_receive_chars has to be called irrespective
694 * of whether the I_IXOFF is set, otherwise, how is the interrupt
695 * to be disabled?
696 */
David Howells7d12e782006-10-05 14:55:46 +0100697 sci_receive_chars(port);
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_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct uart_port *port = ptr;
705
Paul Mundte108b2c2006-09-27 16:32:13 +0900706 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900708 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return IRQ_HANDLED;
711}
712
David Howells7d12e782006-10-05 14:55:46 +0100713static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct uart_port *port = ptr;
716
717 /* Handle errors */
718 if (port->type == PORT_SCI) {
719 if (sci_handle_errors(port)) {
720 /* discard character in rx buffer */
721 sci_in(port, SCxSR);
722 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
723 }
724 } else {
725#if defined(SCIF_ORER)
726 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
727 struct tty_struct *tty = port->info->tty;
728
729 sci_out(port, SCLSR, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800730 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
731 tty_flip_buffer_push(tty);
732 pr_debug("scif: overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734#endif
David Howells7d12e782006-10-05 14:55:46 +0100735 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
739
740 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100741 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 return IRQ_HANDLED;
744}
745
David Howells7d12e782006-10-05 14:55:46 +0100746static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct uart_port *port = ptr;
749
750 /* Handle BREAKs */
751 sci_handle_breaks(port);
752 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
753
754 return IRQ_HANDLED;
755}
756
David Howells7d12e782006-10-05 14:55:46 +0100757static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 unsigned short ssr_status, scr_status;
760 struct uart_port *port = ptr;
761
762 ssr_status = sci_in(port,SCxSR);
763 scr_status = sci_in(port,SCSCR);
764
765 /* Tx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900766 if ((ssr_status & 0x0020) && (scr_status & 0x0080))
David Howells7d12e782006-10-05 14:55:46 +0100767 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* Rx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900769 if ((ssr_status & 0x0002) && (scr_status & 0x0040))
David Howells7d12e782006-10-05 14:55:46 +0100770 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Error Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900772 if ((ssr_status & 0x0080) && (scr_status & 0x0400))
David Howells7d12e782006-10-05 14:55:46 +0100773 sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Break Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900775 if ((ssr_status & 0x0010) && (scr_status & 0x0200))
David Howells7d12e782006-10-05 14:55:46 +0100776 sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return IRQ_HANDLED;
779}
780
781#ifdef CONFIG_CPU_FREQ
782/*
783 * Here we define a transistion notifier so that we can update all of our
784 * ports' baud rate when the peripheral clock changes.
785 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900786static int sci_notifier(struct notifier_block *self,
787 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct cpufreq_freqs *freqs = p;
790 int i;
791
792 if ((phase == CPUFREQ_POSTCHANGE) ||
793 (phase == CPUFREQ_RESUMECHANGE)){
794 for (i = 0; i < SCI_NPORTS; i++) {
795 struct uart_port *port = &sci_ports[i].port;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800796 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /*
799 * Update the uartclk per-port if frequency has
800 * changed, since it will no longer necessarily be
801 * consistent with the old frequency.
802 *
803 * Really we want to be able to do something like
804 * uart_change_speed() or something along those lines
805 * here to implicitly reset the per-port baud rate..
806 *
807 * Clean this up later..
808 */
Paul Mundt1d118562006-12-01 13:15:14 +0900809 clk = clk_get(NULL, "module_clk");
Paul Mundtb7a76e42006-02-01 03:06:06 -0800810 port->uartclk = clk_get_rate(clk) * 16;
811 clk_put(clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Paul Mundte108b2c2006-09-27 16:32:13 +0900814 printk(KERN_INFO "%s: got a postchange notification "
815 "for cpu %d (old %d, new %d)\n",
816 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
819 return NOTIFY_OK;
820}
821
822static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
823#endif /* CONFIG_CPU_FREQ */
824
825static int sci_request_irq(struct sci_port *port)
826{
827 int i;
David Howells7d12e782006-10-05 14:55:46 +0100828 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
830 sci_br_interrupt,
831 };
832 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
833 "SCI Transmit Data Empty", "SCI Break" };
834
835 if (port->irqs[0] == port->irqs[1]) {
836 if (!port->irqs[0]) {
837 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
838 return -ENODEV;
839 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900840
841 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900842 IRQF_DISABLED, "sci", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 printk(KERN_ERR "sci: Cannot allocate irq.\n");
844 return -ENODEV;
845 }
846 } else {
847 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
848 if (!port->irqs[i])
849 continue;
Paul Mundte108b2c2006-09-27 16:32:13 +0900850 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900851 IRQF_DISABLED, desc[i], port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 printk(KERN_ERR "sci: Cannot allocate irq.\n");
853 return -ENODEV;
854 }
855 }
856 }
857
858 return 0;
859}
860
861static void sci_free_irq(struct sci_port *port)
862{
863 int i;
864
865 if (port->irqs[0] == port->irqs[1]) {
866 if (!port->irqs[0])
867 printk("sci: sci_free_irq error\n");
868 else
869 free_irq(port->irqs[0], port);
870 } else {
871 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
872 if (!port->irqs[i])
873 continue;
874
875 free_irq(port->irqs[i], port);
876 }
877 }
878}
879
880static unsigned int sci_tx_empty(struct uart_port *port)
881{
882 /* Can't detect */
883 return TIOCSER_TEMT;
884}
885
886static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
887{
888 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
889 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
890 /* If you have signals for DTR and DCD, please implement here. */
891}
892
893static unsigned int sci_get_mctrl(struct uart_port *port)
894{
895 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
896 and CTS/RTS */
897
898 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
899}
900
Russell Kingb129a8c2005-08-31 10:12:14 +0100901static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Paul Mundte108b2c2006-09-27 16:32:13 +0900903 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Paul Mundte108b2c2006-09-27 16:32:13 +0900905 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
906 ctrl = sci_in(port, SCSCR);
907 ctrl |= SCI_CTRL_FLAGS_TIE;
908 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Russell Kingb129a8c2005-08-31 10:12:14 +0100911static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 unsigned short ctrl;
914
915 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ctrl = sci_in(port, SCSCR);
917 ctrl &= ~SCI_CTRL_FLAGS_TIE;
918 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned short ctrl;
924
925 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ctrl = sci_in(port, SCSCR);
927 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
928 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931static void sci_stop_rx(struct uart_port *port)
932{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned short ctrl;
934
935 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ctrl = sci_in(port, SCSCR);
937 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
938 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941static void sci_enable_ms(struct uart_port *port)
942{
943 /* Nothing here yet .. */
944}
945
946static void sci_break_ctl(struct uart_port *port, int break_state)
947{
948 /* Nothing here yet .. */
949}
950
951static int sci_startup(struct uart_port *port)
952{
953 struct sci_port *s = &sci_ports[port->line];
954
Paul Mundte108b2c2006-09-27 16:32:13 +0900955 if (s->enable)
956 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700959 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sci_start_rx(port, 1);
961
962 return 0;
963}
964
965static void sci_shutdown(struct uart_port *port)
966{
967 struct sci_port *s = &sci_ports[port->line];
968
969 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100970 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sci_free_irq(s);
972
Paul Mundte108b2c2006-09-27 16:32:13 +0900973 if (s->disable)
974 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Alan Cox606d0992006-12-08 02:38:45 -0800977static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
978 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct sci_port *s = &sci_ports[port->line];
981 unsigned int status, baud, smr_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int t;
983
984 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 switch (baud) {
Paul Mundtb7a76e42006-02-01 03:06:06 -0800987 case 0:
988 t = -1;
989 break;
990 default:
991 {
992#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
Paul Mundt1d118562006-12-01 13:15:14 +0900993 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundtb7a76e42006-02-01 03:06:06 -0800994 t = SCBRR_VALUE(baud, clk_get_rate(clk));
995 clk_put(clk);
996#else
997 t = SCBRR_VALUE(baud);
998#endif
Paul Mundtb7a76e42006-02-01 03:06:06 -0800999 break;
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
Paul Mundte108b2c2006-09-27 16:32:13 +09001003 do {
1004 status = sci_in(port, SCxSR);
1005 } while (!(status & SCxSR_TEND(port)));
1006
1007 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1008
1009#if !defined(SCI_ONLY)
1010 if (port->type == PORT_SCIF)
1011 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1012#endif
1013
1014 smr_val = sci_in(port, SCSMR) & 3;
1015 if ((termios->c_cflag & CSIZE) == CS7)
1016 smr_val |= 0x40;
1017 if (termios->c_cflag & PARENB)
1018 smr_val |= 0x20;
1019 if (termios->c_cflag & PARODD)
1020 smr_val |= 0x30;
1021 if (termios->c_cflag & CSTOPB)
1022 smr_val |= 0x08;
1023
1024 uart_update_timeout(port, termios->c_cflag, baud);
1025
1026 sci_out(port, SCSMR, smr_val);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (t > 0) {
1029 if(t >= 256) {
1030 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1031 t >>= 2;
1032 } else {
1033 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1034 }
1035 sci_out(port, SCBRR, t);
1036 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1037 }
1038
Paul Mundtb7a76e42006-02-01 03:06:06 -08001039 if (likely(s->init_pins))
1040 s->init_pins(port, termios->c_cflag);
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 sci_out(port, SCSCR, SCSCR_INIT(port));
1043
1044 if ((termios->c_cflag & CREAD) != 0)
1045 sci_start_rx(port,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048static const char *sci_type(struct uart_port *port)
1049{
1050 switch (port->type) {
1051 case PORT_SCI: return "sci";
1052 case PORT_SCIF: return "scif";
1053 case PORT_IRDA: return "irda";
1054 }
1055
1056 return 0;
1057}
1058
1059static void sci_release_port(struct uart_port *port)
1060{
1061 /* Nothing here yet .. */
1062}
1063
1064static int sci_request_port(struct uart_port *port)
1065{
1066 /* Nothing here yet .. */
1067 return 0;
1068}
1069
1070static void sci_config_port(struct uart_port *port, int flags)
1071{
1072 struct sci_port *s = &sci_ports[port->line];
1073
1074 port->type = s->type;
1075
Paul Mundte108b2c2006-09-27 16:32:13 +09001076 switch (port->type) {
1077 case PORT_SCI:
1078 s->init_pins = sci_init_pins_sci;
1079 break;
1080 case PORT_SCIF:
1081 s->init_pins = sci_init_pins_scif;
1082 break;
1083 case PORT_IRDA:
1084 s->init_pins = sci_init_pins_irda;
1085 break;
1086 }
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1089 if (port->mapbase == 0)
1090 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1091
Paul Mundte108b2c2006-09-27 16:32:13 +09001092 port->membase = (void __iomem *)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#endif
1094}
1095
1096static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1097{
1098 struct sci_port *s = &sci_ports[port->line];
1099
1100 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1101 return -EINVAL;
1102 if (ser->baud_base < 2400)
1103 /* No paper tape reader for Mitch.. */
1104 return -EINVAL;
1105
1106 return 0;
1107}
1108
1109static struct uart_ops sci_uart_ops = {
1110 .tx_empty = sci_tx_empty,
1111 .set_mctrl = sci_set_mctrl,
1112 .get_mctrl = sci_get_mctrl,
1113 .start_tx = sci_start_tx,
1114 .stop_tx = sci_stop_tx,
1115 .stop_rx = sci_stop_rx,
1116 .enable_ms = sci_enable_ms,
1117 .break_ctl = sci_break_ctl,
1118 .startup = sci_startup,
1119 .shutdown = sci_shutdown,
1120 .set_termios = sci_set_termios,
1121 .type = sci_type,
1122 .release_port = sci_release_port,
1123 .request_port = sci_request_port,
1124 .config_port = sci_config_port,
1125 .verify_port = sci_verify_port,
1126};
1127
Paul Mundte108b2c2006-09-27 16:32:13 +09001128static void __init sci_init_ports(void)
1129{
1130 static int first = 1;
1131 int i;
1132
1133 if (!first)
1134 return;
1135
1136 first = 0;
1137
1138 for (i = 0; i < SCI_NPORTS; i++) {
1139 sci_ports[i].port.ops = &sci_uart_ops;
1140 sci_ports[i].port.iotype = UPIO_MEM;
1141 sci_ports[i].port.line = i;
1142 sci_ports[i].port.fifosize = 1;
1143
1144#if defined(__H8300H__) || defined(__H8300S__)
1145#ifdef __H8300S__
1146 sci_ports[i].enable = h8300_sci_enable;
1147 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001149 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1150#elif defined(CONFIG_SUPERH64)
1151 sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
1152#else
1153 /*
1154 * XXX: We should use a proper SCI/SCIF clock
1155 */
1156 {
Paul Mundt1d118562006-12-01 13:15:14 +09001157 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundte108b2c2006-09-27 16:32:13 +09001158 sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
1159 clk_put(clk);
1160 }
1161#endif
1162
1163 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1164 sci_ports[i].break_timer.function = sci_break_timer;
1165
1166 init_timer(&sci_ports[i].break_timer);
1167 }
1168}
1169
1170int __init early_sci_setup(struct uart_port *port)
1171{
1172 if (unlikely(port->line > SCI_NPORTS))
1173 return -ENODEV;
1174
1175 sci_init_ports();
1176
1177 sci_ports[port->line].port.membase = port->membase;
1178 sci_ports[port->line].port.mapbase = port->mapbase;
1179 sci_ports[port->line].port.type = port->type;
1180
1181 return 0;
1182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1185/*
1186 * Print a string to the serial port trying not to disturb
1187 * any possible real use of the port...
1188 */
1189static void serial_console_write(struct console *co, const char *s,
1190 unsigned count)
1191{
1192 put_string(serial_console_port, s, count);
1193}
1194
1195static int __init serial_console_setup(struct console *co, char *options)
1196{
1197 struct uart_port *port;
1198 int baud = 115200;
1199 int bits = 8;
1200 int parity = 'n';
1201 int flow = 'n';
1202 int ret;
1203
Paul Mundte108b2c2006-09-27 16:32:13 +09001204 /*
1205 * Check whether an invalid uart number has been specified, and
1206 * if so, search for the first available port that does have
1207 * console support.
1208 */
1209 if (co->index >= SCI_NPORTS)
1210 co->index = 0;
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 serial_console_port = &sci_ports[co->index];
1213 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001216 * Also need to check port->type, we don't actually have any
1217 * UPIO_PORT ports, but uart_report_port() handily misreports
1218 * it anyways if we don't have a port available by the time this is
1219 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001221 if (!port->type)
1222 return -ENODEV;
1223 if (!port->membase || !port->mapbase)
1224 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001225
Paul Mundte108b2c2006-09-27 16:32:13 +09001226 port->type = serial_console_port->type;
1227
1228 if (port->flags & UPF_IOREMAP)
1229 sci_config_port(port, 0);
1230
1231 if (serial_console_port->enable)
1232 serial_console_port->enable(port);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (options)
1235 uart_parse_options(options, &baud, &parity, &bits, &flow);
1236
1237 ret = uart_set_options(port, co, baud, parity, bits, flow);
1238#if defined(__H8300H__) || defined(__H8300S__)
1239 /* disable rx interrupt */
1240 if (ret == 0)
1241 sci_stop_rx(port);
1242#endif
1243 return ret;
1244}
1245
1246static struct console serial_console = {
1247 .name = "ttySC",
1248 .device = uart_console_device,
1249 .write = serial_console_write,
1250 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001251 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .index = -1,
1253 .data = &sci_uart_driver,
1254};
1255
1256static int __init sci_console_init(void)
1257{
Paul Mundte108b2c2006-09-27 16:32:13 +09001258 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 register_console(&serial_console);
1260 return 0;
1261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262console_initcall(sci_console_init);
1263#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1264
1265#ifdef CONFIG_SH_KGDB
1266/*
1267 * FIXME: Most of this can go away.. at the moment, we rely on
1268 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1269 * most of that can easily be done here instead.
1270 *
1271 * For the time being, just accept the values that were parsed earlier..
1272 */
1273static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1274 int *parity, int *bits)
1275{
1276 *baud = kgdb_baud;
1277 *parity = tolower(kgdb_parity);
1278 *bits = kgdb_bits - '0';
1279}
1280
1281/*
1282 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1283 * care of the early-on initialization for kgdb, regardless of whether we
1284 * actually use kgdb as a console or not.
1285 *
1286 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1287 */
1288int __init kgdb_console_setup(struct console *co, char *options)
1289{
1290 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1291 int baud = 38400;
1292 int bits = 8;
1293 int parity = 'n';
1294 int flow = 'n';
1295
Paul Mundtb7a76e42006-02-01 03:06:06 -08001296 if (co->index != kgdb_portnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 co->index = kgdb_portnum;
1298
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001299 kgdb_sci_port = &sci_ports[co->index];
1300 port = &kgdb_sci_port->port;
1301
1302 /*
1303 * Also need to check port->type, we don't actually have any
1304 * UPIO_PORT ports, but uart_report_port() handily misreports
1305 * it anyways if we don't have a port available by the time this is
1306 * called.
1307 */
1308 if (!port->type)
1309 return -ENODEV;
1310 if (!port->membase || !port->mapbase)
1311 return -ENODEV;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (options)
1314 uart_parse_options(options, &baud, &parity, &bits, &flow);
1315 else
1316 kgdb_console_get_options(port, &baud, &parity, &bits);
1317
1318 kgdb_getchar = kgdb_sci_getchar;
1319 kgdb_putchar = kgdb_sci_putchar;
1320
1321 return uart_set_options(port, co, baud, parity, bits, flow);
1322}
1323#endif /* CONFIG_SH_KGDB */
1324
1325#ifdef CONFIG_SH_KGDB_CONSOLE
1326static struct console kgdb_console = {
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001327 .name = "ttySC",
1328 .device = uart_console_device,
1329 .write = kgdb_console_write,
1330 .setup = kgdb_console_setup,
1331 .flags = CON_PRINTBUFFER,
1332 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 .data = &sci_uart_driver,
1334};
1335
1336/* Register the KGDB console so we get messages (d'oh!) */
1337static int __init kgdb_console_init(void)
1338{
Paul Mundte108b2c2006-09-27 16:32:13 +09001339 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 register_console(&kgdb_console);
1341 return 0;
1342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343console_initcall(kgdb_console_init);
1344#endif /* CONFIG_SH_KGDB_CONSOLE */
1345
1346#if defined(CONFIG_SH_KGDB_CONSOLE)
1347#define SCI_CONSOLE &kgdb_console
1348#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1349#define SCI_CONSOLE &serial_console
1350#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001351#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#endif
1353
1354static char banner[] __initdata =
1355 KERN_INFO "SuperH SCI(F) driver initialized\n";
1356
1357static struct uart_driver sci_uart_driver = {
1358 .owner = THIS_MODULE,
1359 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 .dev_name = "ttySC",
1361 .major = SCI_MAJOR,
1362 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001363 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 .cons = SCI_CONSOLE,
1365};
1366
Paul Mundte108b2c2006-09-27 16:32:13 +09001367/*
1368 * Register a set of serial devices attached to a platform device. The
1369 * list is terminated with a zero flags entry, which means we expect
1370 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1371 * remapping (such as sh64) should also set UPF_IOREMAP.
1372 */
1373static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Paul Mundte108b2c2006-09-27 16:32:13 +09001375 struct plat_sci_port *p = dev->dev.platform_data;
1376 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Paul Mundt32351a22007-03-12 14:38:59 +09001378 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001379 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Paul Mundt32351a22007-03-12 14:38:59 +09001381 /* Sanity check */
1382 if (unlikely(i == SCI_NPORTS)) {
1383 dev_notice(&dev->dev, "Attempting to register port "
1384 "%d when only %d are available.\n",
1385 i+1, SCI_NPORTS);
1386 dev_notice(&dev->dev, "Consider bumping "
1387 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1388 break;
1389 }
1390
Paul Mundte108b2c2006-09-27 16:32:13 +09001391 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001392
Paul Mundte108b2c2006-09-27 16:32:13 +09001393 /*
1394 * For the simple (and majority of) cases where we don't need
1395 * to do any remapping, just cast the cookie directly.
1396 */
1397 if (p->mapbase && !p->membase && !(p->flags & UPF_IOREMAP))
1398 p->membase = (void __iomem *)p->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Paul Mundte108b2c2006-09-27 16:32:13 +09001400 sciport->port.membase = p->membase;
1401
1402 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1403 sciport->port.flags = p->flags;
1404 sciport->port.dev = &dev->dev;
1405
1406 sciport->type = sciport->port.type = p->type;
1407
1408 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1409
1410 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001413#if defined(CONFIG_SH_KGDB) && !defined(CONFIG_SH_KGDB_CONSOLE)
1414 kgdb_sci_port = &sci_ports[kgdb_portnum];
1415 kgdb_getchar = kgdb_sci_getchar;
1416 kgdb_putchar = kgdb_sci_putchar;
1417#endif
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419#ifdef CONFIG_CPU_FREQ
1420 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Paul Mundte108b2c2006-09-27 16:32:13 +09001421 dev_info(&dev->dev, "sci: CPU frequency notifier registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422#endif
1423
1424#ifdef CONFIG_SH_STANDARD_BIOS
1425 sh_bios_gdb_detach();
1426#endif
1427
Paul Mundte108b2c2006-09-27 16:32:13 +09001428 return 0;
1429}
1430
1431static int __devexit sci_remove(struct platform_device *dev)
1432{
1433 int i;
1434
1435 for (i = 0; i < SCI_NPORTS; i++)
1436 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1437
1438 return 0;
1439}
1440
1441static int sci_suspend(struct platform_device *dev, pm_message_t state)
1442{
1443 int i;
1444
1445 for (i = 0; i < SCI_NPORTS; i++) {
1446 struct sci_port *p = &sci_ports[i];
1447
1448 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1449 uart_suspend_port(&sci_uart_driver, &p->port);
1450 }
1451
1452 return 0;
1453}
1454
1455static int sci_resume(struct platform_device *dev)
1456{
1457 int i;
1458
1459 for (i = 0; i < SCI_NPORTS; i++) {
1460 struct sci_port *p = &sci_ports[i];
1461
1462 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1463 uart_resume_port(&sci_uart_driver, &p->port);
1464 }
1465
1466 return 0;
1467}
1468
1469static struct platform_driver sci_driver = {
1470 .probe = sci_probe,
1471 .remove = __devexit_p(sci_remove),
1472 .suspend = sci_suspend,
1473 .resume = sci_resume,
1474 .driver = {
1475 .name = "sh-sci",
1476 .owner = THIS_MODULE,
1477 },
1478};
1479
1480static int __init sci_init(void)
1481{
1482 int ret;
1483
1484 printk(banner);
1485
1486 sci_init_ports();
1487
1488 ret = uart_register_driver(&sci_uart_driver);
1489 if (likely(ret == 0)) {
1490 ret = platform_driver_register(&sci_driver);
1491 if (unlikely(ret))
1492 uart_unregister_driver(&sci_uart_driver);
1493 }
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return ret;
1496}
1497
1498static void __exit sci_exit(void)
1499{
Paul Mundte108b2c2006-09-27 16:32:13 +09001500 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 uart_unregister_driver(&sci_uart_driver);
1502}
1503
1504module_init(sci_init);
1505module_exit(sci_exit);
1506
Paul Mundte108b2c2006-09-27 16:32:13 +09001507MODULE_LICENSE("GPL");