]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/serial/sh-sci.c
IRQ: Typedef the IRQ handler function type
[mirror_ubuntu-bionic-kernel.git] / drivers / serial / sh-sci.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
e108b2ca 6 * Copyright (C) 2002 - 2006 Paul Mundt
1da177e4
LT
7 *
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 */
20
21#undef DEBUG
22
1da177e4
LT
23#include <linux/module.h>
24#include <linux/errno.h>
1da177e4
LT
25#include <linux/timer.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/major.h>
31#include <linux/string.h>
32#include <linux/sysrq.h>
1da177e4
LT
33#include <linux/ioport.h>
34#include <linux/mm.h>
1da177e4
LT
35#include <linux/init.h>
36#include <linux/delay.h>
37#include <linux/console.h>
e108b2ca 38#include <linux/platform_device.h>
1da177e4
LT
39
40#ifdef CONFIG_CPU_FREQ
41#include <linux/notifier.h>
42#include <linux/cpufreq.h>
43#endif
44
b7a76e4b
PM
45#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
46#include <asm/clock.h>
1da177e4 47#include <asm/sh_bios.h>
e108b2ca 48#include <asm/kgdb.h>
1da177e4
LT
49#endif
50
e108b2ca
PM
51#include <asm/sci.h>
52
1da177e4
LT
53#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54#define SUPPORT_SYSRQ
55#endif
56
57#include "sh-sci.h"
58
e108b2ca
PM
59struct sci_port {
60 struct uart_port port;
61
62 /* Port type */
63 unsigned int type;
64
65 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
66 unsigned int irqs[SCIx_NR_IRQS];
67
68 /* Port pin configuration */
69 void (*init_pins)(struct uart_port *port,
70 unsigned int cflag);
1da177e4 71
e108b2ca
PM
72 /* Port enable callback */
73 void (*enable)(struct uart_port *port);
74
75 /* Port disable callback */
76 void (*disable)(struct uart_port *port);
77
78 /* Break timer */
79 struct timer_list break_timer;
80 int break_flag;
81};
82
83#ifdef CONFIG_SH_KGDB
1da177e4 84static struct sci_port *kgdb_sci_port;
e108b2ca 85#endif
1da177e4
LT
86
87#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
e108b2ca
PM
88static struct sci_port *serial_console_port;
89#endif
1da177e4
LT
90
91/* Function prototypes */
b129a8cc 92static void sci_stop_tx(struct uart_port *port);
1da177e4 93
e108b2ca 94#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
b7a76e4b 95
e108b2ca
PM
96static struct sci_port sci_ports[SCI_NPORTS];
97static struct uart_driver sci_uart_driver;
1da177e4 98
e108b2ca
PM
99#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
100 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
101static inline void handle_error(struct uart_port *port)
102{
103 /* Clear error flags */
1da177e4
LT
104 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
105}
106
107static int get_char(struct uart_port *port)
108{
109 unsigned long flags;
110 unsigned short status;
111 int c;
112
e108b2ca
PM
113 spin_lock_irqsave(&port->lock, flags);
114 do {
1da177e4
LT
115 status = sci_in(port, SCxSR);
116 if (status & SCxSR_ERRORS(port)) {
117 handle_error(port);
118 continue;
119 }
120 } while (!(status & SCxSR_RDxF(port)));
121 c = sci_in(port, SCxRDR);
122 sci_in(port, SCxSR); /* Dummy read */
123 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
e108b2ca 124 spin_unlock_irqrestore(&port->lock, flags);
1da177e4
LT
125
126 return c;
127}
1da177e4
LT
128#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
129
e108b2ca 130#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
1da177e4
LT
131static void put_char(struct uart_port *port, char c)
132{
133 unsigned long flags;
134 unsigned short status;
135
e108b2ca 136 spin_lock_irqsave(&port->lock, flags);
1da177e4
LT
137
138 do {
139 status = sci_in(port, SCxSR);
140 } while (!(status & SCxSR_TDxE(port)));
141
142 sci_out(port, SCxTDR, c);
143 sci_in(port, SCxSR); /* Dummy read */
144 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
145
e108b2ca 146 spin_unlock_irqrestore(&port->lock, flags);
1da177e4 147}
e108b2ca 148#endif
1da177e4 149
e108b2ca 150#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1da177e4
LT
151static void put_string(struct sci_port *sci_port, const char *buffer, int count)
152{
153 struct uart_port *port = &sci_port->port;
154 const unsigned char *p = buffer;
155 int i;
156
157#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
158 int checksum;
159 int usegdb=0;
160
161#ifdef CONFIG_SH_STANDARD_BIOS
b7a76e4b 162 /* This call only does a trap the first time it is
1da177e4
LT
163 * called, and so is safe to do here unconditionally
164 */
165 usegdb |= sh_bios_in_gdb_mode();
166#endif
167#ifdef CONFIG_SH_KGDB
168 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
169#endif
170
171 if (usegdb) {
172 /* $<packet info>#<checksum>. */
173 do {
174 unsigned char c;
175 put_char(port, '$');
176 put_char(port, 'O'); /* 'O'utput to console */
177 checksum = 'O';
178
179 for (i=0; i<count; i++) { /* Don't use run length encoding */
180 int h, l;
181
182 c = *p++;
183 h = highhex(c);
184 l = lowhex(c);
185 put_char(port, h);
186 put_char(port, l);
187 checksum += h + l;
188 }
189 put_char(port, '#');
190 put_char(port, highhex(checksum));
191 put_char(port, lowhex(checksum));
192 } while (get_char(port) != '+');
193 } else
194#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
195 for (i=0; i<count; i++) {
196 if (*p == 10)
197 put_char(port, '\r');
198 put_char(port, *p++);
199 }
200}
201#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
202
1da177e4 203#ifdef CONFIG_SH_KGDB
1da177e4
LT
204static int kgdb_sci_getchar(void)
205{
e108b2ca 206 int c;
1da177e4
LT
207
208 /* Keep trying to read a character, this could be neater */
e108b2ca
PM
209 while ((c = get_char(kgdb_sci_port)) < 0)
210 cpu_relax();
1da177e4
LT
211
212 return c;
213}
214
e108b2ca 215static inline void kgdb_sci_putchar(int c)
1da177e4 216{
e108b2ca 217 put_char(kgdb_sci_port, c);
1da177e4 218}
1da177e4
LT
219#endif /* CONFIG_SH_KGDB */
220
221#if defined(__H8300S__)
222enum { sci_disable, sci_enable };
223
e108b2ca 224static void h8300_sci_config(struct uart_port* port, unsigned int ctrl)
1da177e4
LT
225{
226 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
227 int ch = (port->mapbase - SMR0) >> 3;
228 unsigned char mask = 1 << (ch+1);
229
230 if (ctrl == sci_disable) {
231 *mstpcrl |= mask;
232 } else {
233 *mstpcrl &= ~mask;
234 }
235}
e108b2ca
PM
236
237static inline void h8300_sci_enable(struct uart_port *port)
238{
239 h8300_sci_config(port, sci_enable);
240}
241
242static inline void h8300_sci_disable(struct uart_port *port)
243{
244 h8300_sci_config(port, sci_disable);
245}
1da177e4
LT
246#endif
247
e108b2ca
PM
248#if defined(SCI_ONLY) || defined(SCI_AND_SCIF) && \
249 defined(__H8300H__) || defined(__H8300S__)
1da177e4
LT
250static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
251{
252 int ch = (port->mapbase - SMR0) >> 3;
253
254 /* set DDR regs */
e108b2ca
PM
255 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
256 h8300_sci_pins[ch].rx,
257 H8300_GPIO_INPUT);
258 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
259 h8300_sci_pins[ch].tx,
260 H8300_GPIO_OUTPUT);
261
1da177e4
LT
262 /* tx mark output*/
263 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
264}
e108b2ca
PM
265#else
266#define sci_init_pins_sci NULL
267#endif
268
269#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
270static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
271{
272 unsigned int fcr_val = 0;
273
274 if (cflag & CRTSCTS)
275 fcr_val |= SCFCR_MCE;
276
277 sci_out(port, SCFCR, fcr_val);
278}
279#else
280#define sci_init_pins_irda NULL
1da177e4 281#endif
e108b2ca
PM
282
283#ifdef SCI_ONLY
284#define sci_init_pins_scif NULL
1da177e4
LT
285#endif
286
287#if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
e108b2ca 288#if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7710)
b7a76e4b
PM
289/* SH7300 doesn't use RTS/CTS */
290static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
291{
292 sci_out(port, SCFCR, 0);
293}
294#elif defined(CONFIG_CPU_SH3)
e108b2ca 295/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
1da177e4
LT
296static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
297{
298 unsigned int fcr_val = 0;
b7a76e4b
PM
299 unsigned short data;
300
301 /* We need to set SCPCR to enable RTS/CTS */
302 data = ctrl_inw(SCPCR);
303 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
304 ctrl_outw(data & 0x0fcf, SCPCR);
1da177e4 305
1da177e4
LT
306 if (cflag & CRTSCTS)
307 fcr_val |= SCFCR_MCE;
308 else {
1da177e4
LT
309 /* We need to set SCPCR to enable RTS/CTS */
310 data = ctrl_inw(SCPCR);
311 /* Clear out SCP7MD1,0, SCP4MD1,0,
312 Set SCP6MD1,0 = {01} (output) */
b7a76e4b 313 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
1da177e4
LT
314
315 data = ctrl_inb(SCPDR);
316 /* Set /RTS2 (bit6) = 0 */
b7a76e4b 317 ctrl_outb(data & 0xbf, SCPDR);
1da177e4 318 }
b7a76e4b 319
1da177e4
LT
320 sci_out(port, SCFCR, fcr_val);
321}
1da177e4 322#else
1da177e4
LT
323/* For SH7750 */
324static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
325{
326 unsigned int fcr_val = 0;
327
328 if (cflag & CRTSCTS) {
329 fcr_val |= SCFCR_MCE;
330 } else {
e108b2ca
PM
331#ifdef CONFIG_CPU_SUBTYPE_SH7343
332 /* Nothing */
333#elif defined(CONFIG_CPU_SUBTYPE_SH7780)
b7a76e4b
PM
334 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
335#else
1da177e4 336 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
b7a76e4b 337#endif
1da177e4
LT
338 }
339 sci_out(port, SCFCR, fcr_val);
340}
e108b2ca
PM
341#endif
342
343#if defined(CONFIG_CPU_SUBTYPE_SH7760) || defined(CONFIG_CPU_SUBTYPE_SH7780)
344static inline int scif_txroom(struct uart_port *port)
345{
346 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0x7f);
347}
348
349static inline int scif_rxroom(struct uart_port *port)
350{
351 return sci_in(port, SCRFDR) & 0x7f;
352}
353#else
354static inline int scif_txroom(struct uart_port *port)
355{
356 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
357}
1da177e4 358
e108b2ca
PM
359static inline int scif_rxroom(struct uart_port *port)
360{
361 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
362}
1da177e4
LT
363#endif
364#endif /* SCIF_ONLY || SCI_AND_SCIF */
365
e108b2ca
PM
366static inline int sci_txroom(struct uart_port *port)
367{
368 return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
369}
370
371static inline int sci_rxroom(struct uart_port *port)
372{
373 return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
374}
375
1da177e4
LT
376/* ********************************************************************** *
377 * the interrupt related routines *
378 * ********************************************************************** */
379
380static void sci_transmit_chars(struct uart_port *port)
381{
382 struct circ_buf *xmit = &port->info->xmit;
383 unsigned int stopped = uart_tx_stopped(port);
1da177e4
LT
384 unsigned short status;
385 unsigned short ctrl;
e108b2ca 386 int count;
1da177e4
LT
387
388 status = sci_in(port, SCxSR);
389 if (!(status & SCxSR_TDxE(port))) {
1da177e4
LT
390 ctrl = sci_in(port, SCSCR);
391 if (uart_circ_empty(xmit)) {
392 ctrl &= ~SCI_CTRL_FLAGS_TIE;
393 } else {
394 ctrl |= SCI_CTRL_FLAGS_TIE;
395 }
396 sci_out(port, SCSCR, ctrl);
1da177e4
LT
397 return;
398 }
399
e108b2ca
PM
400#ifndef SCI_ONLY
401 if (port->type == PORT_SCIF)
402 count = scif_txroom(port);
403 else
1da177e4 404#endif
e108b2ca 405 count = sci_txroom(port);
1da177e4
LT
406
407 do {
408 unsigned char c;
409
410 if (port->x_char) {
411 c = port->x_char;
412 port->x_char = 0;
413 } else if (!uart_circ_empty(xmit) && !stopped) {
414 c = xmit->buf[xmit->tail];
415 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
416 } else {
417 break;
418 }
419
420 sci_out(port, SCxTDR, c);
421
422 port->icount.tx++;
423 } while (--count > 0);
424
425 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
426
427 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
428 uart_write_wakeup(port);
429 if (uart_circ_empty(xmit)) {
b129a8cc 430 sci_stop_tx(port);
1da177e4 431 } else {
1da177e4
LT
432 ctrl = sci_in(port, SCSCR);
433
434#if !defined(SCI_ONLY)
435 if (port->type == PORT_SCIF) {
436 sci_in(port, SCxSR); /* Dummy read */
437 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
438 }
439#endif
440
441 ctrl |= SCI_CTRL_FLAGS_TIE;
442 sci_out(port, SCSCR, ctrl);
1da177e4
LT
443 }
444}
445
446/* On SH3, SCIF may read end-of-break as a space->mark char */
447#define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
448
449static inline void sci_receive_chars(struct uart_port *port,
450 struct pt_regs *regs)
451{
e108b2ca 452 struct sci_port *sci_port = (struct sci_port *)port;
1da177e4
LT
453 struct tty_struct *tty = port->info->tty;
454 int i, count, copied = 0;
455 unsigned short status;
33f0f88f 456 unsigned char flag;
1da177e4
LT
457
458 status = sci_in(port, SCxSR);
459 if (!(status & SCxSR_RDxF(port)))
460 return;
461
462 while (1) {
463#if !defined(SCI_ONLY)
e108b2ca
PM
464 if (port->type == PORT_SCIF)
465 count = scif_rxroom(port);
466 else
1da177e4 467#endif
e108b2ca 468 count = sci_rxroom(port);
1da177e4
LT
469
470 /* Don't copy more bytes than there is room for in the buffer */
33f0f88f 471 count = tty_buffer_request_room(tty, count);
1da177e4
LT
472
473 /* If for any reason we can't copy more data, we're done! */
474 if (count == 0)
475 break;
476
477 if (port->type == PORT_SCI) {
478 char c = sci_in(port, SCxRDR);
e108b2ca 479 if (uart_handle_sysrq_char(port, c, regs) || sci_port->break_flag)
1da177e4 480 count = 0;
e108b2ca
PM
481 else {
482 tty_insert_flip_char(tty, c, TTY_NORMAL);
1da177e4
LT
483 }
484 } else {
485 for (i=0; i<count; i++) {
486 char c = sci_in(port, SCxRDR);
487 status = sci_in(port, SCxSR);
488#if defined(CONFIG_CPU_SH3)
489 /* Skip "chars" during break */
e108b2ca 490 if (sci_port->break_flag) {
1da177e4
LT
491 if ((c == 0) &&
492 (status & SCxSR_FER(port))) {
493 count--; i--;
494 continue;
495 }
e108b2ca 496
1da177e4
LT
497 /* Nonzero => end-of-break */
498 pr_debug("scif: debounce<%02x>\n", c);
e108b2ca
PM
499 sci_port->break_flag = 0;
500
1da177e4
LT
501 if (STEPFN(c)) {
502 count--; i--;
503 continue;
504 }
505 }
506#endif /* CONFIG_CPU_SH3 */
507 if (uart_handle_sysrq_char(port, c, regs)) {
508 count--; i--;
509 continue;
510 }
511
512 /* Store data and status */
1da177e4 513 if (status&SCxSR_FER(port)) {
33f0f88f 514 flag = TTY_FRAME;
1da177e4
LT
515 pr_debug("sci: frame error\n");
516 } else if (status&SCxSR_PER(port)) {
33f0f88f 517 flag = TTY_PARITY;
1da177e4 518 pr_debug("sci: parity error\n");
33f0f88f
AC
519 } else
520 flag = TTY_NORMAL;
521 tty_insert_flip_char(tty, c, flag);
1da177e4
LT
522 }
523 }
524
525 sci_in(port, SCxSR); /* dummy read */
526 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
527
1da177e4
LT
528 copied += count;
529 port->icount.rx += count;
530 }
531
532 if (copied) {
533 /* Tell the rest of the system the news. New characters! */
534 tty_flip_buffer_push(tty);
535 } else {
536 sci_in(port, SCxSR); /* dummy read */
537 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
538 }
539}
540
541#define SCI_BREAK_JIFFIES (HZ/20)
542/* The sci generates interrupts during the break,
543 * 1 per millisecond or so during the break period, for 9600 baud.
544 * So dont bother disabling interrupts.
545 * But dont want more than 1 break event.
546 * Use a kernel timer to periodically poll the rx line until
547 * the break is finished.
548 */
549static void sci_schedule_break_timer(struct sci_port *port)
550{
551 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
552 add_timer(&port->break_timer);
553}
554/* Ensure that two consecutive samples find the break over. */
555static void sci_break_timer(unsigned long data)
556{
e108b2ca
PM
557 struct sci_port *port = (struct sci_port *)data;
558
559 if (sci_rxd_in(&port->port) == 0) {
1da177e4 560 port->break_flag = 1;
e108b2ca
PM
561 sci_schedule_break_timer(port);
562 } else if (port->break_flag == 1) {
1da177e4
LT
563 /* break is over. */
564 port->break_flag = 2;
e108b2ca
PM
565 sci_schedule_break_timer(port);
566 } else
567 port->break_flag = 0;
1da177e4
LT
568}
569
570static inline int sci_handle_errors(struct uart_port *port)
571{
572 int copied = 0;
573 unsigned short status = sci_in(port, SCxSR);
574 struct tty_struct *tty = port->info->tty;
575
e108b2ca 576 if (status & SCxSR_ORER(port)) {
1da177e4 577 /* overrun error */
e108b2ca 578 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
33f0f88f 579 copied++;
1da177e4
LT
580 pr_debug("sci: overrun error\n");
581 }
582
e108b2ca 583 if (status & SCxSR_FER(port)) {
1da177e4
LT
584 if (sci_rxd_in(port) == 0) {
585 /* Notify of BREAK */
e108b2ca
PM
586 struct sci_port *sci_port = (struct sci_port *)port;
587
588 if (!sci_port->break_flag) {
589 sci_port->break_flag = 1;
590 sci_schedule_break_timer(sci_port);
591
1da177e4 592 /* Do sysrq handling. */
e108b2ca 593 if (uart_handle_break(port))
1da177e4 594 return 0;
1da177e4 595 pr_debug("sci: BREAK detected\n");
e108b2ca 596 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
33f0f88f 597 copied++;
1da177e4 598 }
e108b2ca 599 } else {
1da177e4 600 /* frame error */
e108b2ca 601 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
33f0f88f 602 copied++;
1da177e4
LT
603 pr_debug("sci: frame error\n");
604 }
605 }
606
e108b2ca 607 if (status & SCxSR_PER(port)) {
1da177e4 608 /* parity error */
e108b2ca
PM
609 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
610 copied++;
1da177e4
LT
611 pr_debug("sci: parity error\n");
612 }
613
33f0f88f 614 if (copied)
1da177e4 615 tty_flip_buffer_push(tty);
1da177e4
LT
616
617 return copied;
618}
619
620static inline int sci_handle_breaks(struct uart_port *port)
621{
622 int copied = 0;
623 unsigned short status = sci_in(port, SCxSR);
624 struct tty_struct *tty = port->info->tty;
625 struct sci_port *s = &sci_ports[port->line];
626
b7a76e4b 627 if (!s->break_flag && status & SCxSR_BRK(port)) {
1da177e4
LT
628#if defined(CONFIG_CPU_SH3)
629 /* Debounce break */
630 s->break_flag = 1;
631#endif
632 /* Notify of BREAK */
e108b2ca 633 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
33f0f88f 634 copied++;
1da177e4
LT
635 pr_debug("sci: BREAK detected\n");
636 }
637
638#if defined(SCIF_ORER)
639 /* XXX: Handle SCIF overrun error */
640 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
641 sci_out(port, SCLSR, 0);
e108b2ca 642 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
1da177e4 643 copied++;
1da177e4
LT
644 pr_debug("sci: overrun error\n");
645 }
646 }
647#endif
648
33f0f88f 649 if (copied)
1da177e4 650 tty_flip_buffer_push(tty);
e108b2ca 651
1da177e4
LT
652 return copied;
653}
654
e108b2ca 655static irqreturn_t sci_rx_interrupt(int irq, void *port, struct pt_regs *regs)
1da177e4 656{
1da177e4
LT
657 /* I think sci_receive_chars has to be called irrespective
658 * of whether the I_IXOFF is set, otherwise, how is the interrupt
659 * to be disabled?
660 */
661 sci_receive_chars(port, regs);
662
663 return IRQ_HANDLED;
664}
665
666static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
667{
668 struct uart_port *port = ptr;
669
e108b2ca 670 spin_lock_irq(&port->lock);
1da177e4 671 sci_transmit_chars(port);
e108b2ca 672 spin_unlock_irq(&port->lock);
1da177e4
LT
673
674 return IRQ_HANDLED;
675}
676
677static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
678{
679 struct uart_port *port = ptr;
680
681 /* Handle errors */
682 if (port->type == PORT_SCI) {
683 if (sci_handle_errors(port)) {
684 /* discard character in rx buffer */
685 sci_in(port, SCxSR);
686 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
687 }
688 } else {
689#if defined(SCIF_ORER)
690 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
691 struct tty_struct *tty = port->info->tty;
692
693 sci_out(port, SCLSR, 0);
33f0f88f
AC
694 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
695 tty_flip_buffer_push(tty);
696 pr_debug("scif: overrun error\n");
1da177e4
LT
697 }
698#endif
699 sci_rx_interrupt(irq, ptr, regs);
700 }
701
702 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
703
704 /* Kick the transmission */
705 sci_tx_interrupt(irq, ptr, regs);
706
707 return IRQ_HANDLED;
708}
709
710static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
711{
712 struct uart_port *port = ptr;
713
714 /* Handle BREAKs */
715 sci_handle_breaks(port);
e108b2ca
PM
716
717#ifdef CONFIG_SH_KGDB
718 /* Break into the debugger if a break is detected */
719 BREAKPOINT();
720#endif
721
1da177e4
LT
722 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
723
724 return IRQ_HANDLED;
725}
726
727static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
728{
729 unsigned short ssr_status, scr_status;
730 struct uart_port *port = ptr;
731
732 ssr_status = sci_in(port,SCxSR);
733 scr_status = sci_in(port,SCSCR);
734
735 /* Tx Interrupt */
e108b2ca 736 if ((ssr_status & 0x0020) && (scr_status & 0x0080))
1da177e4
LT
737 sci_tx_interrupt(irq, ptr, regs);
738 /* Rx Interrupt */
e108b2ca 739 if ((ssr_status & 0x0002) && (scr_status & 0x0040))
1da177e4
LT
740 sci_rx_interrupt(irq, ptr, regs);
741 /* Error Interrupt */
e108b2ca 742 if ((ssr_status & 0x0080) && (scr_status & 0x0400))
1da177e4
LT
743 sci_er_interrupt(irq, ptr, regs);
744 /* Break Interrupt */
e108b2ca 745 if ((ssr_status & 0x0010) && (scr_status & 0x0200))
1da177e4
LT
746 sci_br_interrupt(irq, ptr, regs);
747
748 return IRQ_HANDLED;
749}
750
751#ifdef CONFIG_CPU_FREQ
752/*
753 * Here we define a transistion notifier so that we can update all of our
754 * ports' baud rate when the peripheral clock changes.
755 */
e108b2ca
PM
756static int sci_notifier(struct notifier_block *self,
757 unsigned long phase, void *p)
1da177e4
LT
758{
759 struct cpufreq_freqs *freqs = p;
760 int i;
761
762 if ((phase == CPUFREQ_POSTCHANGE) ||
763 (phase == CPUFREQ_RESUMECHANGE)){
764 for (i = 0; i < SCI_NPORTS; i++) {
765 struct uart_port *port = &sci_ports[i].port;
b7a76e4b 766 struct clk *clk;
1da177e4
LT
767
768 /*
769 * Update the uartclk per-port if frequency has
770 * changed, since it will no longer necessarily be
771 * consistent with the old frequency.
772 *
773 * Really we want to be able to do something like
774 * uart_change_speed() or something along those lines
775 * here to implicitly reset the per-port baud rate..
776 *
777 * Clean this up later..
778 */
b7a76e4b
PM
779 clk = clk_get("module_clk");
780 port->uartclk = clk_get_rate(clk) * 16;
781 clk_put(clk);
1da177e4
LT
782 }
783
e108b2ca
PM
784 printk(KERN_INFO "%s: got a postchange notification "
785 "for cpu %d (old %d, new %d)\n",
786 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
1da177e4
LT
787 }
788
789 return NOTIFY_OK;
790}
791
792static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
793#endif /* CONFIG_CPU_FREQ */
794
795static int sci_request_irq(struct sci_port *port)
796{
797 int i;
798 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
799 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
800 sci_br_interrupt,
801 };
802 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
803 "SCI Transmit Data Empty", "SCI Break" };
804
805 if (port->irqs[0] == port->irqs[1]) {
806 if (!port->irqs[0]) {
807 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
808 return -ENODEV;
809 }
e108b2ca
PM
810
811 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
812 SA_INTERRUPT, "sci", port)) {
1da177e4
LT
813 printk(KERN_ERR "sci: Cannot allocate irq.\n");
814 return -ENODEV;
815 }
816 } else {
817 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
818 if (!port->irqs[i])
819 continue;
e108b2ca
PM
820 if (request_irq(port->irqs[i], handlers[i],
821 SA_INTERRUPT, desc[i], port)) {
1da177e4
LT
822 printk(KERN_ERR "sci: Cannot allocate irq.\n");
823 return -ENODEV;
824 }
825 }
826 }
827
828 return 0;
829}
830
831static void sci_free_irq(struct sci_port *port)
832{
833 int i;
834
835 if (port->irqs[0] == port->irqs[1]) {
836 if (!port->irqs[0])
837 printk("sci: sci_free_irq error\n");
838 else
839 free_irq(port->irqs[0], port);
840 } else {
841 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
842 if (!port->irqs[i])
843 continue;
844
845 free_irq(port->irqs[i], port);
846 }
847 }
848}
849
850static unsigned int sci_tx_empty(struct uart_port *port)
851{
852 /* Can't detect */
853 return TIOCSER_TEMT;
854}
855
856static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
857{
858 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
859 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
860 /* If you have signals for DTR and DCD, please implement here. */
861}
862
863static unsigned int sci_get_mctrl(struct uart_port *port)
864{
865 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
866 and CTS/RTS */
867
868 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
869}
870
b129a8cc 871static void sci_start_tx(struct uart_port *port)
1da177e4 872{
e108b2ca 873 unsigned short ctrl;
1da177e4 874
e108b2ca
PM
875 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
876 ctrl = sci_in(port, SCSCR);
877 ctrl |= SCI_CTRL_FLAGS_TIE;
878 sci_out(port, SCSCR, ctrl);
1da177e4
LT
879}
880
b129a8cc 881static void sci_stop_tx(struct uart_port *port)
1da177e4 882{
1da177e4
LT
883 unsigned short ctrl;
884
885 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1da177e4
LT
886 ctrl = sci_in(port, SCSCR);
887 ctrl &= ~SCI_CTRL_FLAGS_TIE;
888 sci_out(port, SCSCR, ctrl);
1da177e4
LT
889}
890
891static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
892{
1da177e4
LT
893 unsigned short ctrl;
894
895 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
1da177e4
LT
896 ctrl = sci_in(port, SCSCR);
897 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
898 sci_out(port, SCSCR, ctrl);
1da177e4
LT
899}
900
901static void sci_stop_rx(struct uart_port *port)
902{
1da177e4
LT
903 unsigned short ctrl;
904
905 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
1da177e4
LT
906 ctrl = sci_in(port, SCSCR);
907 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
908 sci_out(port, SCSCR, ctrl);
1da177e4
LT
909}
910
911static void sci_enable_ms(struct uart_port *port)
912{
913 /* Nothing here yet .. */
914}
915
916static void sci_break_ctl(struct uart_port *port, int break_state)
917{
918 /* Nothing here yet .. */
919}
920
921static int sci_startup(struct uart_port *port)
922{
923 struct sci_port *s = &sci_ports[port->line];
924
e108b2ca
PM
925 if (s->enable)
926 s->enable(port);
1da177e4
LT
927
928 sci_request_irq(s);
d656901b 929 sci_start_tx(port);
1da177e4
LT
930 sci_start_rx(port, 1);
931
932 return 0;
933}
934
935static void sci_shutdown(struct uart_port *port)
936{
937 struct sci_port *s = &sci_ports[port->line];
938
939 sci_stop_rx(port);
b129a8cc 940 sci_stop_tx(port);
1da177e4
LT
941 sci_free_irq(s);
942
e108b2ca
PM
943 if (s->disable)
944 s->disable(port);
1da177e4
LT
945}
946
947static void sci_set_termios(struct uart_port *port, struct termios *termios,
948 struct termios *old)
949{
950 struct sci_port *s = &sci_ports[port->line];
951 unsigned int status, baud, smr_val;
952 unsigned long flags;
953 int t;
954
955 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
956
e108b2ca
PM
957 switch (baud) {
958 case 0:
959 t = -1;
960 break;
961 default:
962 {
963#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
964 struct clk *clk = clk_get("module_clk");
965 t = SCBRR_VALUE(baud, clk_get_rate(clk));
966 clk_put(clk);
967#else
968 t = SCBRR_VALUE(baud);
969#endif
970 }
971 break;
972 }
973
1da177e4
LT
974 spin_lock_irqsave(&port->lock, flags);
975
976 do {
977 status = sci_in(port, SCxSR);
978 } while (!(status & SCxSR_TEND(port)));
979
980 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
981
982#if !defined(SCI_ONLY)
e108b2ca 983 if (port->type == PORT_SCIF)
1da177e4 984 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1da177e4
LT
985#endif
986
987 smr_val = sci_in(port, SCSMR) & 3;
988 if ((termios->c_cflag & CSIZE) == CS7)
989 smr_val |= 0x40;
990 if (termios->c_cflag & PARENB)
991 smr_val |= 0x20;
992 if (termios->c_cflag & PARODD)
993 smr_val |= 0x30;
994 if (termios->c_cflag & CSTOPB)
995 smr_val |= 0x08;
996
997 uart_update_timeout(port, termios->c_cflag, baud);
998
999 sci_out(port, SCSMR, smr_val);
1000
1da177e4
LT
1001 if (t > 0) {
1002 if(t >= 256) {
1003 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1004 t >>= 2;
1005 } else {
1006 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1007 }
1008 sci_out(port, SCBRR, t);
1009 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1010 }
1011
b7a76e4b
PM
1012 if (likely(s->init_pins))
1013 s->init_pins(port, termios->c_cflag);
1014
1da177e4
LT
1015 sci_out(port, SCSCR, SCSCR_INIT(port));
1016
1017 if ((termios->c_cflag & CREAD) != 0)
1018 sci_start_rx(port,0);
1019
1020 spin_unlock_irqrestore(&port->lock, flags);
1021}
1022
1023static const char *sci_type(struct uart_port *port)
1024{
1025 switch (port->type) {
1026 case PORT_SCI: return "sci";
1027 case PORT_SCIF: return "scif";
1028 case PORT_IRDA: return "irda";
1029 }
1030
1031 return 0;
1032}
1033
1034static void sci_release_port(struct uart_port *port)
1035{
1036 /* Nothing here yet .. */
1037}
1038
1039static int sci_request_port(struct uart_port *port)
1040{
1041 /* Nothing here yet .. */
1042 return 0;
1043}
1044
1045static void sci_config_port(struct uart_port *port, int flags)
1046{
1047 struct sci_port *s = &sci_ports[port->line];
1048
1049 port->type = s->type;
1050
e108b2ca
PM
1051 switch (port->type) {
1052 case PORT_SCI:
1053 s->init_pins = sci_init_pins_sci;
1054 break;
1055 case PORT_SCIF:
1056 s->init_pins = sci_init_pins_scif;
1057 break;
1058 case PORT_IRDA:
1059 s->init_pins = sci_init_pins_irda;
1060 break;
1061 }
1062
1da177e4
LT
1063#if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1064 if (port->mapbase == 0)
1065 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1066
e108b2ca 1067 port->membase = (void __iomem *)port->mapbase;
1da177e4
LT
1068#endif
1069}
1070
1071static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1072{
1073 struct sci_port *s = &sci_ports[port->line];
1074
1075 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1076 return -EINVAL;
1077 if (ser->baud_base < 2400)
1078 /* No paper tape reader for Mitch.. */
1079 return -EINVAL;
1080
1081 return 0;
1082}
1083
1084static struct uart_ops sci_uart_ops = {
1085 .tx_empty = sci_tx_empty,
1086 .set_mctrl = sci_set_mctrl,
1087 .get_mctrl = sci_get_mctrl,
1088 .start_tx = sci_start_tx,
1089 .stop_tx = sci_stop_tx,
1090 .stop_rx = sci_stop_rx,
1091 .enable_ms = sci_enable_ms,
1092 .break_ctl = sci_break_ctl,
1093 .startup = sci_startup,
1094 .shutdown = sci_shutdown,
1095 .set_termios = sci_set_termios,
1096 .type = sci_type,
1097 .release_port = sci_release_port,
1098 .request_port = sci_request_port,
1099 .config_port = sci_config_port,
1100 .verify_port = sci_verify_port,
1101};
1102
e108b2ca
PM
1103static void __init sci_init_ports(void)
1104{
1105 static int first = 1;
1106 int i;
1107
1108 if (!first)
1109 return;
1110
1111 first = 0;
1112
1113 for (i = 0; i < SCI_NPORTS; i++) {
1114 sci_ports[i].port.ops = &sci_uart_ops;
1115 sci_ports[i].port.iotype = UPIO_MEM;
1116 sci_ports[i].port.line = i;
1117 sci_ports[i].port.fifosize = 1;
1118
1119#if defined(__H8300H__) || defined(__H8300S__)
1120#ifdef __H8300S__
1121 sci_ports[i].enable = h8300_sci_enable;
1122 sci_ports[i].disable = h8300_sci_disable;
1123#endif
1124 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1125#elif defined(CONFIG_SUPERH64)
1126 sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
1da177e4 1127#else
e108b2ca
PM
1128 /*
1129 * XXX: We should use a proper SCI/SCIF clock
1130 */
1131 {
1132 struct clk *clk = clk_get("module_clk");
1133 sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
1134 clk_put(clk);
1135 }
1da177e4 1136#endif
e108b2ca
PM
1137
1138 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1139 sci_ports[i].break_timer.function = sci_break_timer;
1140
1141 init_timer(&sci_ports[i].break_timer);
1142 }
1143}
1144
1145int __init early_sci_setup(struct uart_port *port)
1146{
1147 if (unlikely(port->line > SCI_NPORTS))
1148 return -ENODEV;
1149
1150 sci_init_ports();
1151
1152 sci_ports[port->line].port.membase = port->membase;
1153 sci_ports[port->line].port.mapbase = port->mapbase;
1154 sci_ports[port->line].port.type = port->type;
1155
1156 return 0;
1157}
1da177e4
LT
1158
1159#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1160/*
1161 * Print a string to the serial port trying not to disturb
1162 * any possible real use of the port...
1163 */
1164static void serial_console_write(struct console *co, const char *s,
1165 unsigned count)
1166{
1167 put_string(serial_console_port, s, count);
1168}
1169
1170static int __init serial_console_setup(struct console *co, char *options)
1171{
1172 struct uart_port *port;
1173 int baud = 115200;
1174 int bits = 8;
1175 int parity = 'n';
1176 int flow = 'n';
1177 int ret;
1178
e108b2ca
PM
1179 /*
1180 * Check whether an invalid uart number has been specified, and
1181 * if so, search for the first available port that does have
1182 * console support.
1183 */
1184 if (co->index >= SCI_NPORTS)
1185 co->index = 0;
1186
1da177e4
LT
1187 serial_console_port = &sci_ports[co->index];
1188 port = &serial_console_port->port;
1da177e4
LT
1189
1190 /*
e108b2ca
PM
1191 * Also need to check port->type, we don't actually have any
1192 * UPIO_PORT ports, but uart_report_port() handily misreports
1193 * it anyways if we don't have a port available by the time this is
1194 * called.
1da177e4 1195 */
e108b2ca
PM
1196 if (!port->type)
1197 return -ENODEV;
1198 if (!port->membase || !port->mapbase)
1199 return -ENODEV;
1200
1201 spin_lock_init(&port->lock);
1202
1203 port->type = serial_console_port->type;
1204
1205 if (port->flags & UPF_IOREMAP)
1206 sci_config_port(port, 0);
1207
1208 if (serial_console_port->enable)
1209 serial_console_port->enable(port);
b7a76e4b 1210
1da177e4
LT
1211 if (options)
1212 uart_parse_options(options, &baud, &parity, &bits, &flow);
1213
1214 ret = uart_set_options(port, co, baud, parity, bits, flow);
1215#if defined(__H8300H__) || defined(__H8300S__)
1216 /* disable rx interrupt */
1217 if (ret == 0)
1218 sci_stop_rx(port);
1219#endif
1220 return ret;
1221}
1222
1223static struct console serial_console = {
1224 .name = "ttySC",
1225 .device = uart_console_device,
1226 .write = serial_console_write,
1227 .setup = serial_console_setup,
e108b2ca 1228 .flags = CON_PRINTBUFFER,
1da177e4
LT
1229 .index = -1,
1230 .data = &sci_uart_driver,
1231};
1232
1233static int __init sci_console_init(void)
1234{
e108b2ca 1235 sci_init_ports();
1da177e4
LT
1236 register_console(&serial_console);
1237 return 0;
1238}
1da177e4
LT
1239console_initcall(sci_console_init);
1240#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1241
1242#ifdef CONFIG_SH_KGDB
1243/*
1244 * FIXME: Most of this can go away.. at the moment, we rely on
1245 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1246 * most of that can easily be done here instead.
1247 *
1248 * For the time being, just accept the values that were parsed earlier..
1249 */
1250static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1251 int *parity, int *bits)
1252{
1253 *baud = kgdb_baud;
1254 *parity = tolower(kgdb_parity);
1255 *bits = kgdb_bits - '0';
1256}
1257
1258/*
1259 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1260 * care of the early-on initialization for kgdb, regardless of whether we
1261 * actually use kgdb as a console or not.
1262 *
1263 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1264 */
1265int __init kgdb_console_setup(struct console *co, char *options)
1266{
1267 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1268 int baud = 38400;
1269 int bits = 8;
1270 int parity = 'n';
1271 int flow = 'n';
1272
e108b2ca
PM
1273 spin_lock_init(&port->lock);
1274
b7a76e4b 1275 if (co->index != kgdb_portnum)
1da177e4
LT
1276 co->index = kgdb_portnum;
1277
1278 if (options)
1279 uart_parse_options(options, &baud, &parity, &bits, &flow);
1280 else
1281 kgdb_console_get_options(port, &baud, &parity, &bits);
1282
1283 kgdb_getchar = kgdb_sci_getchar;
1284 kgdb_putchar = kgdb_sci_putchar;
1285
1286 return uart_set_options(port, co, baud, parity, bits, flow);
1287}
1288#endif /* CONFIG_SH_KGDB */
1289
1290#ifdef CONFIG_SH_KGDB_CONSOLE
1291static struct console kgdb_console = {
1292 .name = "ttySC",
1293 .write = kgdb_console_write,
1294 .setup = kgdb_console_setup,
1295 .flags = CON_PRINTBUFFER | CON_ENABLED,
1296 .index = -1,
1297 .data = &sci_uart_driver,
1298};
1299
1300/* Register the KGDB console so we get messages (d'oh!) */
1301static int __init kgdb_console_init(void)
1302{
e108b2ca 1303 sci_init_ports();
1da177e4
LT
1304 register_console(&kgdb_console);
1305 return 0;
1306}
1da177e4
LT
1307console_initcall(kgdb_console_init);
1308#endif /* CONFIG_SH_KGDB_CONSOLE */
1309
1310#if defined(CONFIG_SH_KGDB_CONSOLE)
1311#define SCI_CONSOLE &kgdb_console
1312#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1313#define SCI_CONSOLE &serial_console
1314#else
b7a76e4b 1315#define SCI_CONSOLE 0
1da177e4
LT
1316#endif
1317
1318static char banner[] __initdata =
1319 KERN_INFO "SuperH SCI(F) driver initialized\n";
1320
1321static struct uart_driver sci_uart_driver = {
1322 .owner = THIS_MODULE,
1323 .driver_name = "sci",
1da177e4
LT
1324 .dev_name = "ttySC",
1325 .major = SCI_MAJOR,
1326 .minor = SCI_MINOR_START,
e108b2ca 1327 .nr = SCI_NPORTS,
1da177e4
LT
1328 .cons = SCI_CONSOLE,
1329};
1330
e108b2ca
PM
1331/*
1332 * Register a set of serial devices attached to a platform device. The
1333 * list is terminated with a zero flags entry, which means we expect
1334 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1335 * remapping (such as sh64) should also set UPF_IOREMAP.
1336 */
1337static int __devinit sci_probe(struct platform_device *dev)
1da177e4 1338{
e108b2ca
PM
1339 struct plat_sci_port *p = dev->dev.platform_data;
1340 int i;
1da177e4 1341
e108b2ca
PM
1342 for (i = 0; p && p->flags != 0 && i < SCI_NPORTS; p++, i++) {
1343 struct sci_port *sciport = &sci_ports[i];
1da177e4 1344
e108b2ca 1345 sciport->port.mapbase = p->mapbase;
b7a76e4b 1346
e108b2ca
PM
1347 /*
1348 * For the simple (and majority of) cases where we don't need
1349 * to do any remapping, just cast the cookie directly.
1350 */
1351 if (p->mapbase && !p->membase && !(p->flags & UPF_IOREMAP))
1352 p->membase = (void __iomem *)p->mapbase;
1da177e4 1353
e108b2ca
PM
1354 sciport->port.membase = p->membase;
1355
1356 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1357 sciport->port.flags = p->flags;
1358 sciport->port.dev = &dev->dev;
1359
1360 sciport->type = sciport->port.type = p->type;
1361
1362 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1363
1364 uart_add_one_port(&sci_uart_driver, &sciport->port);
1da177e4
LT
1365 }
1366
1367#ifdef CONFIG_CPU_FREQ
1368 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
e108b2ca 1369 dev_info(&dev->dev, "sci: CPU frequency notifier registered\n");
1da177e4
LT
1370#endif
1371
1372#ifdef CONFIG_SH_STANDARD_BIOS
1373 sh_bios_gdb_detach();
1374#endif
1375
e108b2ca 1376 return 0;
1da177e4
LT
1377}
1378
e108b2ca
PM
1379static int __devexit sci_remove(struct platform_device *dev)
1380{
1381 int i;
1382
1383 for (i = 0; i < SCI_NPORTS; i++)
1384 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1385
1386 return 0;
1387}
1388
1389static int sci_suspend(struct platform_device *dev, pm_message_t state)
1da177e4 1390{
e108b2ca
PM
1391 int i;
1392
1393 for (i = 0; i < SCI_NPORTS; i++) {
1394 struct sci_port *p = &sci_ports[i];
1395
1396 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1397 uart_suspend_port(&sci_uart_driver, &p->port);
1398 }
1da177e4 1399
e108b2ca
PM
1400 return 0;
1401}
1da177e4 1402
e108b2ca
PM
1403static int sci_resume(struct platform_device *dev)
1404{
1405 int i;
1406
1407 for (i = 0; i < SCI_NPORTS; i++) {
1408 struct sci_port *p = &sci_ports[i];
1409
1410 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1411 uart_resume_port(&sci_uart_driver, &p->port);
1412 }
1413
1414 return 0;
1415}
1416
1417static struct platform_driver sci_driver = {
1418 .probe = sci_probe,
1419 .remove = __devexit_p(sci_remove),
1420 .suspend = sci_suspend,
1421 .resume = sci_resume,
1422 .driver = {
1423 .name = "sh-sci",
1424 .owner = THIS_MODULE,
1425 },
1426};
1427
1428static int __init sci_init(void)
1429{
1430 int ret;
1431
1432 printk(banner);
1433
1434 sci_init_ports();
1435
1436 ret = uart_register_driver(&sci_uart_driver);
1437 if (likely(ret == 0)) {
1438 ret = platform_driver_register(&sci_driver);
1439 if (unlikely(ret))
1440 uart_unregister_driver(&sci_uart_driver);
1441 }
1442
1443 return ret;
1444}
1445
1446static void __exit sci_exit(void)
1447{
1448 platform_driver_unregister(&sci_driver);
1da177e4
LT
1449 uart_unregister_driver(&sci_uart_driver);
1450}
1451
1452module_init(sci_init);
1453module_exit(sci_exit);
1454
e108b2ca 1455MODULE_LICENSE("GPL");