blob: dbf5357a77b30e1678e268a485ae51e98c3eb7ce [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 Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090050
51#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080052#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090053#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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
Paul Mundte108b2c2006-09-27 16:32:13 +090067 /* Port enable callback */
68 void (*enable)(struct uart_port *port);
69
70 /* Port disable callback */
71 void (*disable)(struct uart_port *port);
72
73 /* Break timer */
74 struct timer_list break_timer;
75 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090076
Paul Mundta2159b52008-10-02 19:09:13 +090077#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +090078 /* Port clock */
79 struct clk *clk;
Paul Mundt005a3362007-04-26 11:45:32 +090080#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090081};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Paul Mundte108b2c2006-09-27 16:32:13 +090084static struct sci_port *serial_console_port;
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010088static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Paul Mundte108b2c2006-09-27 16:32:13 +090090#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
91
92static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static struct uart_driver sci_uart_driver;
94
Michael Trimarchie7c98dc2008-11-13 18:18:35 +090095static inline struct sci_port *
96to_sci_port(struct uart_port *uart)
97{
98 return container_of(uart, struct sci_port, port);
99}
100
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900101#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900102
103#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900104static inline void handle_error(struct uart_port *port)
105{
106 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
108}
109
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900110static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned short status;
113 int c;
114
Paul Mundte108b2c2006-09-27 16:32:13 +0900115 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 status = sci_in(port, SCxSR);
117 if (status & SCxSR_ERRORS(port)) {
118 handle_error(port);
119 continue;
120 }
121 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900124
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900125 /* Dummy read */
126 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return c;
130}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900133static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned short status;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 do {
138 status = sci_in(port, SCxSR);
139 } while (!(status & SCxSR_TDxE(port)));
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 sci_in(port, SCxSR); /* Dummy read */
Magnus Damm973e5d52009-02-24 15:57:12 +0900142 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900143 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900145#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#if defined(__H8300S__)
148enum { sci_disable, sci_enable };
149
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900150static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900152 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int ch = (port->mapbase - SMR0) >> 3;
154 unsigned char mask = 1 << (ch+1);
155
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900156 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900158 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Paul Mundte108b2c2006-09-27 16:32:13 +0900161
162static inline void h8300_sci_enable(struct uart_port *port)
163{
164 h8300_sci_config(port, sci_enable);
165}
166
167static inline void h8300_sci_disable(struct uart_port *port)
168{
169 h8300_sci_config(port, sci_disable);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#endif
172
Paul Mundt15c73aa2008-10-02 19:47:12 +0900173#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900174static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int ch = (port->mapbase - SMR0) >> 3;
177
178 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900179 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
180 h8300_sci_pins[ch].rx,
181 H8300_GPIO_INPUT);
182 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
183 h8300_sci_pins[ch].tx,
184 H8300_GPIO_OUTPUT);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* tx mark output*/
187 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
188}
Paul Mundtd5701642008-12-16 20:07:27 +0900189#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
190static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900191{
Paul Mundtd5701642008-12-16 20:07:27 +0900192 if (port->mapbase == 0xA4400000) {
193 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
194 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
195 } else if (port->mapbase == 0xA4410000)
196 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900197}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900198#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900199static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900200{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900201 unsigned short data;
202
203 if (cflag & CRTSCTS) {
204 /* enable RTS/CTS */
205 if (port->mapbase == 0xa4430000) { /* SCIF0 */
206 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900207 data = __raw_readw(PORT_PTCR);
208 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900209 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
210 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900211 data = __raw_readw(PORT_PVCR);
212 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900213 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900214 } else {
215 if (port->mapbase == 0xa4430000) { /* SCIF0 */
216 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900217 data = __raw_readw(PORT_PTCR);
218 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
220 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900221 data = __raw_readw(PORT_PVCR);
222 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900223 }
224 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900225}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800226#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900227/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900228static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800230 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Paul Mundtb7a76e42006-02-01 03:06:06 -0800232 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900233 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800234 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900235 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800236
Paul Mundtd5701642008-12-16 20:07:27 +0900237 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900239 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Clear out SCP7MD1,0, SCP4MD1,0,
241 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900242 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 data = ctrl_inb(SCPDR);
245 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800246 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Paul Mundt41504c32006-12-11 20:28:03 +0900249#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900250static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900251{
Magnus Damm346b7462008-04-23 21:25:29 +0900252 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900253
Magnus Damm346b7462008-04-23 21:25:29 +0900254 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900255 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900256 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900257 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900258 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900259
Paul Mundtd5701642008-12-16 20:07:27 +0900260 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900261 }
Paul Mundt41504c32006-12-11 20:28:03 +0900262}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900263#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
264 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900265 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900266 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900267 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900268static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
269{
270 if (!(cflag & CRTSCTS))
271 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
272}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900273#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900274static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
275{
276 if (!(cflag & CRTSCTS))
277 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
278}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800279#else
Paul Mundtd5701642008-12-16 20:07:27 +0900280static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
281{
282 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Paul Mundte108b2c2006-09-27 16:32:13 +0900284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Paul Mundt32351a22007-03-12 14:38:59 +0900286#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
287 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900288 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
289 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900290static inline int scif_txroom(struct uart_port *port)
291{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900292 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900293}
294
295static inline int scif_rxroom(struct uart_port *port)
296{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900297 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900298}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900299#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
300static inline int scif_txroom(struct uart_port *port)
301{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900302 if ((port->mapbase == 0xffe00000) ||
303 (port->mapbase == 0xffe08000)) {
304 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900305 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900306 } else {
307 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900308 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900309 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900310}
311
312static inline int scif_rxroom(struct uart_port *port)
313{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900314 if ((port->mapbase == 0xffe00000) ||
315 (port->mapbase == 0xffe08000)) {
316 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900318 } else {
319 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900320 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900321 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900322}
Paul Mundte108b2c2006-09-27 16:32:13 +0900323#else
324static inline int scif_txroom(struct uart_port *port)
325{
326 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
327}
328
329static inline int scif_rxroom(struct uart_port *port)
330{
331 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Paul Mundte108b2c2006-09-27 16:32:13 +0900335static inline int sci_txroom(struct uart_port *port)
336{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900337 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900338}
339
340static inline int sci_rxroom(struct uart_port *port)
341{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900342 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/* ********************************************************************** *
346 * the interrupt related routines *
347 * ********************************************************************** */
348
349static void sci_transmit_chars(struct uart_port *port)
350{
351 struct circ_buf *xmit = &port->info->xmit;
352 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned short status;
354 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900355 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 status = sci_in(port, SCxSR);
358 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900360 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900362 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return;
366 }
367
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900368 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900369 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900370 else
371 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 do {
374 unsigned char c;
375
376 if (port->x_char) {
377 c = port->x_char;
378 port->x_char = 0;
379 } else if (!uart_circ_empty(xmit) && !stopped) {
380 c = xmit->buf[xmit->tail];
381 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
382 } else {
383 break;
384 }
385
386 sci_out(port, SCxTDR, c);
387
388 port->icount.tx++;
389 } while (--count > 0);
390
391 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
392
393 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
394 uart_write_wakeup(port);
395 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100396 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ctrl = sci_in(port, SCSCR);
399
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900400 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sci_in(port, SCxSR); /* Dummy read */
402 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 ctrl |= SCI_CTRL_FLAGS_TIE;
406 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408}
409
410/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900411#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Howells7d12e782006-10-05 14:55:46 +0100413static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900415 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100416 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int i, count, copied = 0;
418 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800419 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 status = sci_in(port, SCxSR);
422 if (!(status & SCxSR_RDxF(port)))
423 return;
424
425 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900426 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900427 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900428 else
429 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800432 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* If for any reason we can't copy more data, we're done! */
435 if (count == 0)
436 break;
437
438 if (port->type == PORT_SCI) {
439 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900440 if (uart_handle_sysrq_char(port, c) ||
441 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900443 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900444 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900446 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 char c = sci_in(port, SCxRDR);
448 status = sci_in(port, SCxSR);
449#if defined(CONFIG_CPU_SH3)
450 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900451 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if ((c == 0) &&
453 (status & SCxSR_FER(port))) {
454 count--; i--;
455 continue;
456 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900459 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900460 sci_port->break_flag = 0;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (STEPFN(c)) {
463 count--; i--;
464 continue;
465 }
466 }
467#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100468 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 count--; i--;
470 continue;
471 }
472
473 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800475 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900476 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800478 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900479 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800480 } else
481 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900482
Alan Cox33f0f882006-01-09 20:54:13 -0800483 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 }
486
487 sci_in(port, SCxSR); /* dummy read */
488 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 copied += count;
491 port->icount.rx += count;
492 }
493
494 if (copied) {
495 /* Tell the rest of the system the news. New characters! */
496 tty_flip_buffer_push(tty);
497 } else {
498 sci_in(port, SCxSR); /* dummy read */
499 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
500 }
501}
502
503#define SCI_BREAK_JIFFIES (HZ/20)
504/* The sci generates interrupts during the break,
505 * 1 per millisecond or so during the break period, for 9600 baud.
506 * So dont bother disabling interrupts.
507 * But dont want more than 1 break event.
508 * Use a kernel timer to periodically poll the rx line until
509 * the break is finished.
510 */
511static void sci_schedule_break_timer(struct sci_port *port)
512{
513 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
514 add_timer(&port->break_timer);
515}
516/* Ensure that two consecutive samples find the break over. */
517static void sci_break_timer(unsigned long data)
518{
Paul Mundte108b2c2006-09-27 16:32:13 +0900519 struct sci_port *port = (struct sci_port *)data;
520
521 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900523 sci_schedule_break_timer(port);
524 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* break is over. */
526 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900527 sci_schedule_break_timer(port);
528 } else
529 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static inline int sci_handle_errors(struct uart_port *port)
533{
534 int copied = 0;
535 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100536 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Paul Mundte108b2c2006-09-27 16:32:13 +0900538 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900540 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800541 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900542
543 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Paul Mundte108b2c2006-09-27 16:32:13 +0900546 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (sci_rxd_in(port) == 0) {
548 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900549 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900550
551 if (!sci_port->break_flag) {
552 sci_port->break_flag = 1;
553 sci_schedule_break_timer(sci_port);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900556 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900558
559 dev_dbg(port->dev, "BREAK detected\n");
560
Paul Mundte108b2c2006-09-27 16:32:13 +0900561 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900562 copied++;
563 }
564
Paul Mundte108b2c2006-09-27 16:32:13 +0900565 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800568 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900569
570 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 }
573
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
577 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900578
579 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Alan Cox33f0f882006-01-09 20:54:13 -0800582 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return copied;
586}
587
Paul Mundtd830fa42008-12-16 19:29:38 +0900588static inline int sci_handle_fifo_overrun(struct uart_port *port)
589{
590 struct tty_struct *tty = port->info->port.tty;
591 int copied = 0;
592
593 if (port->type != PORT_SCIF)
594 return 0;
595
596 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
597 sci_out(port, SCLSR, 0);
598
599 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
600 tty_flip_buffer_push(tty);
601
602 dev_notice(port->dev, "overrun error\n");
603 copied++;
604 }
605
606 return copied;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static inline int sci_handle_breaks(struct uart_port *port)
610{
611 int copied = 0;
612 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100613 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct sci_port *s = &sci_ports[port->line];
615
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900616 if (uart_handle_break(port))
617 return 0;
618
Paul Mundtb7a76e42006-02-01 03:06:06 -0800619 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#if defined(CONFIG_CPU_SH3)
621 /* Debounce break */
622 s->break_flag = 1;
623#endif
624 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900625 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800626 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900627
628 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Alan Cox33f0f882006-01-09 20:54:13 -0800631 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900633
Paul Mundtd830fa42008-12-16 19:29:38 +0900634 copied += sci_handle_fifo_overrun(port);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return copied;
637}
638
David Howells7d12e782006-10-05 14:55:46 +0100639static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* I think sci_receive_chars has to be called irrespective
642 * of whether the I_IXOFF is set, otherwise, how is the interrupt
643 * to be disabled?
644 */
David Howells7d12e782006-10-05 14:55:46 +0100645 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 return IRQ_HANDLED;
648}
649
David Howells7d12e782006-10-05 14:55:46 +0100650static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct uart_port *port = ptr;
653
Paul Mundte108b2c2006-09-27 16:32:13 +0900654 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900656 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 return IRQ_HANDLED;
659}
660
David Howells7d12e782006-10-05 14:55:46 +0100661static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct uart_port *port = ptr;
664
665 /* Handle errors */
666 if (port->type == PORT_SCI) {
667 if (sci_handle_errors(port)) {
668 /* discard character in rx buffer */
669 sci_in(port, SCxSR);
670 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
671 }
672 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900673 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100674 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
678
679 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100680 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return IRQ_HANDLED;
683}
684
David Howells7d12e782006-10-05 14:55:46 +0100685static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct uart_port *port = ptr;
688
689 /* Handle BREAKs */
690 sci_handle_breaks(port);
691 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
692
693 return IRQ_HANDLED;
694}
695
David Howells7d12e782006-10-05 14:55:46 +0100696static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900698 unsigned short ssr_status, scr_status;
699 struct uart_port *port = ptr;
700 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900702 ssr_status = sci_in(port, SCxSR);
703 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900706 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
707 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900709 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
710 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900712 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
713 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900715 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
716 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Michael Trimarchia8884e32008-10-31 16:10:23 +0900718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Paul Mundt027e6872008-12-16 18:36:16 +0900721#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * Here we define a transistion notifier so that we can update all of our
724 * ports' baud rate when the peripheral clock changes.
725 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900726static int sci_notifier(struct notifier_block *self,
727 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int i;
730
731 if ((phase == CPUFREQ_POSTCHANGE) ||
Paul Mundt027e6872008-12-16 18:36:16 +0900732 (phase == CPUFREQ_RESUMECHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 for (i = 0; i < SCI_NPORTS; i++) {
Paul Mundt027e6872008-12-16 18:36:16 +0900734 struct sci_port *s = &sci_ports[i];
735 s->port.uartclk = clk_get_rate(s->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return NOTIFY_OK;
739}
740
741static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
Paul Mundt027e6872008-12-16 18:36:16 +0900742#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744static int sci_request_irq(struct sci_port *port)
745{
746 int i;
David Howells7d12e782006-10-05 14:55:46 +0100747 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
749 sci_br_interrupt,
750 };
751 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
752 "SCI Transmit Data Empty", "SCI Break" };
753
754 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900755 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900757
758 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900759 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900760 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -ENODEV;
762 }
763 } else {
764 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900765 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900767
Paul Mundte108b2c2006-09-27 16:32:13 +0900768 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900769 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900770 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENODEV;
772 }
773 }
774 }
775
776 return 0;
777}
778
779static void sci_free_irq(struct sci_port *port)
780{
781 int i;
782
Paul Mundt762c69e2008-12-16 18:55:26 +0900783 if (port->irqs[0] == port->irqs[1])
784 free_irq(port->irqs[0], port);
785 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
787 if (!port->irqs[i])
788 continue;
789
790 free_irq(port->irqs[i], port);
791 }
792 }
793}
794
795static unsigned int sci_tx_empty(struct uart_port *port)
796{
797 /* Can't detect */
798 return TIOCSER_TEMT;
799}
800
801static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
802{
803 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
804 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
805 /* If you have signals for DTR and DCD, please implement here. */
806}
807
808static unsigned int sci_get_mctrl(struct uart_port *port)
809{
810 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
811 and CTS/RTS */
812
813 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
814}
815
Russell Kingb129a8c2005-08-31 10:12:14 +0100816static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Paul Mundte108b2c2006-09-27 16:32:13 +0900818 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Paul Mundte108b2c2006-09-27 16:32:13 +0900820 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
821 ctrl = sci_in(port, SCSCR);
822 ctrl |= SCI_CTRL_FLAGS_TIE;
823 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Russell Kingb129a8c2005-08-31 10:12:14 +0100826static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned short ctrl;
829
830 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ctrl = sci_in(port, SCSCR);
832 ctrl &= ~SCI_CTRL_FLAGS_TIE;
833 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 unsigned short ctrl;
839
840 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ctrl = sci_in(port, SCSCR);
842 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
843 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846static void sci_stop_rx(struct uart_port *port)
847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned short ctrl;
849
850 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ctrl = sci_in(port, SCSCR);
852 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
853 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static void sci_enable_ms(struct uart_port *port)
857{
858 /* Nothing here yet .. */
859}
860
861static void sci_break_ctl(struct uart_port *port, int break_state)
862{
863 /* Nothing here yet .. */
864}
865
866static int sci_startup(struct uart_port *port)
867{
868 struct sci_port *s = &sci_ports[port->line];
869
Paul Mundte108b2c2006-09-27 16:32:13 +0900870 if (s->enable)
871 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Paul Mundta2159b52008-10-02 19:09:13 +0900873#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900874 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +0900875#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700878 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sci_start_rx(port, 1);
880
881 return 0;
882}
883
884static void sci_shutdown(struct uart_port *port)
885{
886 struct sci_port *s = &sci_ports[port->line];
887
888 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100889 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sci_free_irq(s);
891
Paul Mundte108b2c2006-09-27 16:32:13 +0900892 if (s->disable)
893 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900894
Paul Mundta2159b52008-10-02 19:09:13 +0900895#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900896 clk_put(s->clk);
897 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +0900898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Alan Cox606d0992006-12-08 02:38:45 -0800901static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
902 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900905 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900908 if (likely(baud))
909 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Paul Mundte108b2c2006-09-27 16:32:13 +0900911 do {
912 status = sci_in(port, SCxSR);
913 } while (!(status & SCxSR_TEND(port)));
914
915 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
916
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900917 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900918 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900919
920 smr_val = sci_in(port, SCSMR) & 3;
921 if ((termios->c_cflag & CSIZE) == CS7)
922 smr_val |= 0x40;
923 if (termios->c_cflag & PARENB)
924 smr_val |= 0x20;
925 if (termios->c_cflag & PARODD)
926 smr_val |= 0x30;
927 if (termios->c_cflag & CSTOPB)
928 smr_val |= 0x08;
929
930 uart_update_timeout(port, termios->c_cflag, baud);
931
932 sci_out(port, SCSMR, smr_val);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900935 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
937 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900938 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 sci_out(port, SCBRR, t);
942 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
943 }
944
Paul Mundtd5701642008-12-16 20:07:27 +0900945 sci_init_pins(port, termios->c_cflag);
946 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 sci_out(port, SCSCR, SCSCR_INIT(port));
949
950 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900951 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static const char *sci_type(struct uart_port *port)
955{
956 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900957 case PORT_IRDA:
958 return "irda";
959 case PORT_SCI:
960 return "sci";
961 case PORT_SCIF:
962 return "scif";
963 case PORT_SCIFA:
964 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Paul Mundtfa439722008-09-04 18:53:58 +0900967 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970static void sci_release_port(struct uart_port *port)
971{
972 /* Nothing here yet .. */
973}
974
975static int sci_request_port(struct uart_port *port)
976{
977 /* Nothing here yet .. */
978 return 0;
979}
980
981static void sci_config_port(struct uart_port *port, int flags)
982{
983 struct sci_port *s = &sci_ports[port->line];
984
985 port->type = s->type;
986
Paul Mundt7ff731a2008-10-01 15:46:58 +0900987 if (port->flags & UPF_IOREMAP && !port->membase) {
988#if defined(CONFIG_SUPERH64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
Paul Mundt7ff731a2008-10-01 15:46:58 +0900990 port->membase = (void __iomem *)port->mapbase;
991#else
992 port->membase = ioremap_nocache(port->mapbase, 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#endif
Paul Mundt7ff731a2008-10-01 15:46:58 +0900994
Paul Mundt762c69e2008-12-16 18:55:26 +0900995 dev_err(port->dev, "can't remap port#%d\n", port->line);
Paul Mundt7ff731a2008-10-01 15:46:58 +0900996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1000{
1001 struct sci_port *s = &sci_ports[port->line];
1002
Yinghai Lua62c4132008-08-19 20:49:55 -07001003 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EINVAL;
1005 if (ser->baud_base < 2400)
1006 /* No paper tape reader for Mitch.. */
1007 return -EINVAL;
1008
1009 return 0;
1010}
1011
1012static struct uart_ops sci_uart_ops = {
1013 .tx_empty = sci_tx_empty,
1014 .set_mctrl = sci_set_mctrl,
1015 .get_mctrl = sci_get_mctrl,
1016 .start_tx = sci_start_tx,
1017 .stop_tx = sci_stop_tx,
1018 .stop_rx = sci_stop_rx,
1019 .enable_ms = sci_enable_ms,
1020 .break_ctl = sci_break_ctl,
1021 .startup = sci_startup,
1022 .shutdown = sci_shutdown,
1023 .set_termios = sci_set_termios,
1024 .type = sci_type,
1025 .release_port = sci_release_port,
1026 .request_port = sci_request_port,
1027 .config_port = sci_config_port,
1028 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001029#ifdef CONFIG_CONSOLE_POLL
1030 .poll_get_char = sci_poll_get_char,
1031 .poll_put_char = sci_poll_put_char,
1032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033};
1034
Paul Mundte108b2c2006-09-27 16:32:13 +09001035static void __init sci_init_ports(void)
1036{
1037 static int first = 1;
1038 int i;
1039
1040 if (!first)
1041 return;
1042
1043 first = 0;
1044
1045 for (i = 0; i < SCI_NPORTS; i++) {
1046 sci_ports[i].port.ops = &sci_uart_ops;
1047 sci_ports[i].port.iotype = UPIO_MEM;
1048 sci_ports[i].port.line = i;
1049 sci_ports[i].port.fifosize = 1;
1050
1051#if defined(__H8300H__) || defined(__H8300S__)
1052#ifdef __H8300S__
1053 sci_ports[i].enable = h8300_sci_enable;
1054 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001056 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001057#elif defined(CONFIG_HAVE_CLK)
Paul Mundte108b2c2006-09-27 16:32:13 +09001058 /*
1059 * XXX: We should use a proper SCI/SCIF clock
1060 */
1061 {
Paul Mundt1d118562006-12-01 13:15:14 +09001062 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundta2159b52008-10-02 19:09:13 +09001063 sci_ports[i].port.uartclk = clk_get_rate(clk);
Paul Mundte108b2c2006-09-27 16:32:13 +09001064 clk_put(clk);
1065 }
Paul Mundta2159b52008-10-02 19:09:13 +09001066#else
1067#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001068#endif
1069
1070 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1071 sci_ports[i].break_timer.function = sci_break_timer;
1072
1073 init_timer(&sci_ports[i].break_timer);
1074 }
1075}
1076
1077int __init early_sci_setup(struct uart_port *port)
1078{
1079 if (unlikely(port->line > SCI_NPORTS))
1080 return -ENODEV;
1081
1082 sci_init_ports();
1083
1084 sci_ports[port->line].port.membase = port->membase;
1085 sci_ports[port->line].port.mapbase = port->mapbase;
1086 sci_ports[port->line].port.type = port->type;
1087
1088 return 0;
1089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1092/*
1093 * Print a string to the serial port trying not to disturb
1094 * any possible real use of the port...
1095 */
1096static void serial_console_write(struct console *co, const char *s,
1097 unsigned count)
1098{
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001099 struct uart_port *port = &serial_console_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001100 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001101 int i;
1102
1103 for (i = 0; i < count; i++) {
1104 if (*s == 10)
1105 sci_poll_put_char(port, '\r');
1106
1107 sci_poll_put_char(port, *s++);
1108 }
Magnus Damm973e5d52009-02-24 15:57:12 +09001109
1110 /* wait until fifo is empty and last bit has been transmitted */
1111 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1112 while ((sci_in(port, SCxSR) & bits) != bits)
1113 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116static int __init serial_console_setup(struct console *co, char *options)
1117{
1118 struct uart_port *port;
1119 int baud = 115200;
1120 int bits = 8;
1121 int parity = 'n';
1122 int flow = 'n';
1123 int ret;
1124
Paul Mundte108b2c2006-09-27 16:32:13 +09001125 /*
1126 * Check whether an invalid uart number has been specified, and
1127 * if so, search for the first available port that does have
1128 * console support.
1129 */
1130 if (co->index >= SCI_NPORTS)
1131 co->index = 0;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 serial_console_port = &sci_ports[co->index];
1134 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001137 * Also need to check port->type, we don't actually have any
1138 * UPIO_PORT ports, but uart_report_port() handily misreports
1139 * it anyways if we don't have a port available by the time this is
1140 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001142 if (!port->type)
1143 return -ENODEV;
1144 if (!port->membase || !port->mapbase)
1145 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001146
Paul Mundte108b2c2006-09-27 16:32:13 +09001147 port->type = serial_console_port->type;
1148
Paul Mundta2159b52008-10-02 19:09:13 +09001149#ifdef CONFIG_HAVE_CLK
Paul Mundt005a3362007-04-26 11:45:32 +09001150 if (!serial_console_port->clk)
1151 serial_console_port->clk = clk_get(NULL, "module_clk");
1152#endif
1153
Paul Mundte108b2c2006-09-27 16:32:13 +09001154 if (port->flags & UPF_IOREMAP)
1155 sci_config_port(port, 0);
1156
1157 if (serial_console_port->enable)
1158 serial_console_port->enable(port);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (options)
1161 uart_parse_options(options, &baud, &parity, &bits, &flow);
1162
1163 ret = uart_set_options(port, co, baud, parity, bits, flow);
1164#if defined(__H8300H__) || defined(__H8300S__)
1165 /* disable rx interrupt */
1166 if (ret == 0)
1167 sci_stop_rx(port);
1168#endif
1169 return ret;
1170}
1171
1172static struct console serial_console = {
1173 .name = "ttySC",
1174 .device = uart_console_device,
1175 .write = serial_console_write,
1176 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001177 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 .index = -1,
1179 .data = &sci_uart_driver,
1180};
1181
1182static int __init sci_console_init(void)
1183{
Paul Mundte108b2c2006-09-27 16:32:13 +09001184 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 register_console(&serial_console);
1186 return 0;
1187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188console_initcall(sci_console_init);
1189#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1190
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001191#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001192#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001194#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#endif
1196
1197static char banner[] __initdata =
1198 KERN_INFO "SuperH SCI(F) driver initialized\n";
1199
1200static struct uart_driver sci_uart_driver = {
1201 .owner = THIS_MODULE,
1202 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 .dev_name = "ttySC",
1204 .major = SCI_MAJOR,
1205 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001206 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .cons = SCI_CONSOLE,
1208};
1209
Paul Mundte108b2c2006-09-27 16:32:13 +09001210/*
1211 * Register a set of serial devices attached to a platform device. The
1212 * list is terminated with a zero flags entry, which means we expect
1213 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1214 * remapping (such as sh64) should also set UPF_IOREMAP.
1215 */
1216static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Paul Mundte108b2c2006-09-27 16:32:13 +09001218 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001219 int i, ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Paul Mundt32351a22007-03-12 14:38:59 +09001221 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001222 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Paul Mundt32351a22007-03-12 14:38:59 +09001224 /* Sanity check */
1225 if (unlikely(i == SCI_NPORTS)) {
1226 dev_notice(&dev->dev, "Attempting to register port "
1227 "%d when only %d are available.\n",
1228 i+1, SCI_NPORTS);
1229 dev_notice(&dev->dev, "Consider bumping "
1230 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1231 break;
1232 }
1233
Paul Mundte108b2c2006-09-27 16:32:13 +09001234 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001235
Paul Mundt7ff731a2008-10-01 15:46:58 +09001236 if (p->mapbase && !p->membase) {
1237 if (p->flags & UPF_IOREMAP) {
1238 p->membase = ioremap_nocache(p->mapbase, 0x40);
1239 if (IS_ERR(p->membase)) {
1240 ret = PTR_ERR(p->membase);
1241 goto err_unreg;
1242 }
1243 } else {
1244 /*
1245 * For the simple (and majority of) cases
1246 * where we don't need to do any remapping,
1247 * just cast the cookie directly.
1248 */
1249 p->membase = (void __iomem *)p->mapbase;
1250 }
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Paul Mundte108b2c2006-09-27 16:32:13 +09001253 sciport->port.membase = p->membase;
1254
1255 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1256 sciport->port.flags = p->flags;
1257 sciport->port.dev = &dev->dev;
1258
1259 sciport->type = sciport->port.type = p->type;
1260
1261 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1262
1263 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Paul Mundt027e6872008-12-16 18:36:16 +09001266#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif
1269
1270#ifdef CONFIG_SH_STANDARD_BIOS
1271 sh_bios_gdb_detach();
1272#endif
1273
Paul Mundte108b2c2006-09-27 16:32:13 +09001274 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001275
1276err_unreg:
1277 for (i = i - 1; i >= 0; i--)
1278 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1279
1280 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001281}
1282
1283static int __devexit sci_remove(struct platform_device *dev)
1284{
1285 int i;
1286
Paul Mundt027e6872008-12-16 18:36:16 +09001287#ifdef CONFIG_HAVE_CLK
1288 cpufreq_unregister_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1289#endif
1290
Paul Mundte108b2c2006-09-27 16:32:13 +09001291 for (i = 0; i < SCI_NPORTS; i++)
1292 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1293
1294 return 0;
1295}
1296
1297static int sci_suspend(struct platform_device *dev, pm_message_t state)
1298{
1299 int i;
1300
1301 for (i = 0; i < SCI_NPORTS; i++) {
1302 struct sci_port *p = &sci_ports[i];
1303
1304 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1305 uart_suspend_port(&sci_uart_driver, &p->port);
1306 }
1307
1308 return 0;
1309}
1310
1311static int sci_resume(struct platform_device *dev)
1312{
1313 int i;
1314
1315 for (i = 0; i < SCI_NPORTS; i++) {
1316 struct sci_port *p = &sci_ports[i];
1317
1318 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1319 uart_resume_port(&sci_uart_driver, &p->port);
1320 }
1321
1322 return 0;
1323}
1324
1325static struct platform_driver sci_driver = {
1326 .probe = sci_probe,
1327 .remove = __devexit_p(sci_remove),
1328 .suspend = sci_suspend,
1329 .resume = sci_resume,
1330 .driver = {
1331 .name = "sh-sci",
1332 .owner = THIS_MODULE,
1333 },
1334};
1335
1336static int __init sci_init(void)
1337{
1338 int ret;
1339
1340 printk(banner);
1341
1342 sci_init_ports();
1343
1344 ret = uart_register_driver(&sci_uart_driver);
1345 if (likely(ret == 0)) {
1346 ret = platform_driver_register(&sci_driver);
1347 if (unlikely(ret))
1348 uart_unregister_driver(&sci_uart_driver);
1349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
1352}
1353
1354static void __exit sci_exit(void)
1355{
Paul Mundte108b2c2006-09-27 16:32:13 +09001356 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 uart_unregister_driver(&sci_uart_driver);
1358}
1359
1360module_init(sci_init);
1361module_exit(sci_exit);
1362
Paul Mundte108b2c2006-09-27 16:32:13 +09001363MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001364MODULE_ALIAS("platform:sh-sci");