blob: 4e3248d586020f78dba3fd0abade33a6cae9c5b2 [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>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090051
52#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080053#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Yoshinori Sato168f3622009-04-28 04:40:15 +000057#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "sh-sci.h"
62
Paul Mundte108b2c2006-09-27 16:32:13 +090063struct sci_port {
64 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Paul Mundte108b2c2006-09-27 16:32:13 +090066 /* Port type */
67 unsigned int type;
68
69 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090070 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090071
Paul Mundte108b2c2006-09-27 16:32:13 +090072 /* 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;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090081
Paul Mundta2159b52008-10-02 19:09:13 +090082#ifdef CONFIG_HAVE_CLK
Magnus Damm501b8252009-01-21 15:14:30 +000083 /* Interface clock */
84 struct clk *iclk;
85 /* Data clock */
86 struct clk *dclk;
Paul Mundt005a3362007-04-26 11:45:32 +090087#endif
Magnus Damme552de22009-01-21 15:13:42 +000088 struct list_head node;
89};
90
91struct sh_sci_priv {
92 spinlock_t lock;
93 struct list_head ports;
94
95#ifdef CONFIG_HAVE_CLK
96 struct notifier_block clk_nb;
97#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090098};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100101static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Paul Mundte108b2c2006-09-27 16:32:13 +0900103#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
104
105static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct uart_driver sci_uart_driver;
107
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900108static inline struct sci_port *
109to_sci_port(struct uart_port *uart)
110{
111 return container_of(uart, struct sci_port, port);
112}
113
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900114#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900115
116#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900117static inline void handle_error(struct uart_port *port)
118{
119 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
121}
122
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900123static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned short status;
126 int c;
127
Paul Mundte108b2c2006-09-27 16:32:13 +0900128 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 status = sci_in(port, SCxSR);
130 if (status & SCxSR_ERRORS(port)) {
131 handle_error(port);
132 continue;
133 }
134 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900137
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900138 /* Dummy read */
139 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return c;
143}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900146static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 unsigned short status;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 do {
151 status = sci_in(port, SCxSR);
152 } while (!(status & SCxSR_TDxE(port)));
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 sci_in(port, SCxSR); /* Dummy read */
Magnus Damm973e5d52009-02-24 15:57:12 +0900155 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900156 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900158#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#if defined(__H8300S__)
161enum { sci_disable, sci_enable };
162
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900163static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900165 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int ch = (port->mapbase - SMR0) >> 3;
167 unsigned char mask = 1 << (ch+1);
168
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900169 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900171 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Paul Mundte108b2c2006-09-27 16:32:13 +0900174
Magnus Damm501b8252009-01-21 15:14:30 +0000175static void h8300_sci_enable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900176{
177 h8300_sci_config(port, sci_enable);
178}
179
Magnus Damm501b8252009-01-21 15:14:30 +0000180static void h8300_sci_disable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900181{
182 h8300_sci_config(port, sci_disable);
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#endif
185
Paul Mundt15c73aa2008-10-02 19:47:12 +0900186#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900187static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int ch = (port->mapbase - SMR0) >> 3;
190
191 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900192 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
193 h8300_sci_pins[ch].rx,
194 H8300_GPIO_INPUT);
195 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
196 h8300_sci_pins[ch].tx,
197 H8300_GPIO_OUTPUT);
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* tx mark output*/
200 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
201}
Paul Mundtd5701642008-12-16 20:07:27 +0900202#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
203static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900204{
Paul Mundtd5701642008-12-16 20:07:27 +0900205 if (port->mapbase == 0xA4400000) {
206 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
207 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
208 } else if (port->mapbase == 0xA4410000)
209 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900210}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900211#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900212static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900213{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900214 unsigned short data;
215
216 if (cflag & CRTSCTS) {
217 /* enable RTS/CTS */
218 if (port->mapbase == 0xa4430000) { /* SCIF0 */
219 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900220 data = __raw_readw(PORT_PTCR);
221 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900222 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
223 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900224 data = __raw_readw(PORT_PVCR);
225 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900226 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900227 } else {
228 if (port->mapbase == 0xa4430000) { /* SCIF0 */
229 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900230 data = __raw_readw(PORT_PTCR);
231 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900232 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
233 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900234 data = __raw_readw(PORT_PVCR);
235 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900236 }
237 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900238}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800239#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900240/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900241static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800243 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Paul Mundtb7a76e42006-02-01 03:06:06 -0800245 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900246 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800247 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900248 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800249
Paul Mundtd5701642008-12-16 20:07:27 +0900250 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900252 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* Clear out SCP7MD1,0, SCP4MD1,0,
254 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900255 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 data = ctrl_inb(SCPDR);
258 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800259 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Paul Mundt41504c32006-12-11 20:28:03 +0900262#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900263static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900264{
Magnus Damm346b7462008-04-23 21:25:29 +0900265 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900266
Magnus Damm346b7462008-04-23 21:25:29 +0900267 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900268 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900269 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900270 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900271 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900272
Paul Mundtd5701642008-12-16 20:07:27 +0900273 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900274 }
Paul Mundt41504c32006-12-11 20:28:03 +0900275}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900276#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
277 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900278 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900279 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900280 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900281static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
282{
283 if (!(cflag & CRTSCTS))
284 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
285}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900286#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900287static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
288{
289 if (!(cflag & CRTSCTS))
290 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
291}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800292#else
Paul Mundtd5701642008-12-16 20:07:27 +0900293static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
294{
295 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Paul Mundte108b2c2006-09-27 16:32:13 +0900297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Paul Mundt32351a22007-03-12 14:38:59 +0900299#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
300 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900301 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
302 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900303static inline int scif_txroom(struct uart_port *port)
304{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900305 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900306}
307
308static inline int scif_rxroom(struct uart_port *port)
309{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900310 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900311}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900312#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
313static inline int scif_txroom(struct uart_port *port)
314{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900315 if ((port->mapbase == 0xffe00000) ||
316 (port->mapbase == 0xffe08000)) {
317 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900318 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900319 } else {
320 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900321 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900322 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900323}
324
325static inline int scif_rxroom(struct uart_port *port)
326{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900327 if ((port->mapbase == 0xffe00000) ||
328 (port->mapbase == 0xffe08000)) {
329 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900330 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900331 } else {
332 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900333 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900334 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900335}
Paul Mundte108b2c2006-09-27 16:32:13 +0900336#else
337static inline int scif_txroom(struct uart_port *port)
338{
339 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
340}
341
342static inline int scif_rxroom(struct uart_port *port)
343{
344 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
345}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Paul Mundte108b2c2006-09-27 16:32:13 +0900348static inline int sci_txroom(struct uart_port *port)
349{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900350 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900351}
352
353static inline int sci_rxroom(struct uart_port *port)
354{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900355 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* ********************************************************************** *
359 * the interrupt related routines *
360 * ********************************************************************** */
361
362static void sci_transmit_chars(struct uart_port *port)
363{
364 struct circ_buf *xmit = &port->info->xmit;
365 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned short status;
367 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900368 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 status = sci_in(port, SCxSR);
371 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900373 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900375 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379 }
380
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900381 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900382 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900383 else
384 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 do {
387 unsigned char c;
388
389 if (port->x_char) {
390 c = port->x_char;
391 port->x_char = 0;
392 } else if (!uart_circ_empty(xmit) && !stopped) {
393 c = xmit->buf[xmit->tail];
394 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
395 } else {
396 break;
397 }
398
399 sci_out(port, SCxTDR, c);
400
401 port->icount.tx++;
402 } while (--count > 0);
403
404 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
405
406 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
407 uart_write_wakeup(port);
408 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100409 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ctrl = sci_in(port, SCSCR);
412
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900413 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sci_in(port, SCxSR); /* Dummy read */
415 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 ctrl |= SCI_CTRL_FLAGS_TIE;
419 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421}
422
423/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900424#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howells7d12e782006-10-05 14:55:46 +0100426static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900428 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100429 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int i, count, copied = 0;
431 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800432 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 status = sci_in(port, SCxSR);
435 if (!(status & SCxSR_RDxF(port)))
436 return;
437
438 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900439 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900440 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900441 else
442 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800445 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* If for any reason we can't copy more data, we're done! */
448 if (count == 0)
449 break;
450
451 if (port->type == PORT_SCI) {
452 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900453 if (uart_handle_sysrq_char(port, c) ||
454 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900456 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900457 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900459 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 char c = sci_in(port, SCxRDR);
461 status = sci_in(port, SCxSR);
462#if defined(CONFIG_CPU_SH3)
463 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900464 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if ((c == 0) &&
466 (status & SCxSR_FER(port))) {
467 count--; i--;
468 continue;
469 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900472 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900473 sci_port->break_flag = 0;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (STEPFN(c)) {
476 count--; i--;
477 continue;
478 }
479 }
480#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100481 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 count--; i--;
483 continue;
484 }
485
486 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800488 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900489 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800491 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900492 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800493 } else
494 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900495
Alan Cox33f0f882006-01-09 20:54:13 -0800496 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
499
500 sci_in(port, SCxSR); /* dummy read */
501 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 copied += count;
504 port->icount.rx += count;
505 }
506
507 if (copied) {
508 /* Tell the rest of the system the news. New characters! */
509 tty_flip_buffer_push(tty);
510 } else {
511 sci_in(port, SCxSR); /* dummy read */
512 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
513 }
514}
515
516#define SCI_BREAK_JIFFIES (HZ/20)
517/* The sci generates interrupts during the break,
518 * 1 per millisecond or so during the break period, for 9600 baud.
519 * So dont bother disabling interrupts.
520 * But dont want more than 1 break event.
521 * Use a kernel timer to periodically poll the rx line until
522 * the break is finished.
523 */
524static void sci_schedule_break_timer(struct sci_port *port)
525{
526 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
527 add_timer(&port->break_timer);
528}
529/* Ensure that two consecutive samples find the break over. */
530static void sci_break_timer(unsigned long data)
531{
Paul Mundte108b2c2006-09-27 16:32:13 +0900532 struct sci_port *port = (struct sci_port *)data;
533
534 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900536 sci_schedule_break_timer(port);
537 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* break is over. */
539 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900540 sci_schedule_break_timer(port);
541 } else
542 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static inline int sci_handle_errors(struct uart_port *port)
546{
547 int copied = 0;
548 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100549 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Paul Mundte108b2c2006-09-27 16:32:13 +0900551 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900553 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800554 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900555
556 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Paul Mundte108b2c2006-09-27 16:32:13 +0900559 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (sci_rxd_in(port) == 0) {
561 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900562 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900563
564 if (!sci_port->break_flag) {
565 sci_port->break_flag = 1;
566 sci_schedule_break_timer(sci_port);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900569 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900571
572 dev_dbg(port->dev, "BREAK detected\n");
573
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900575 copied++;
576 }
577
Paul Mundte108b2c2006-09-27 16:32:13 +0900578 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900580 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800581 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900582
583 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 }
586
Paul Mundte108b2c2006-09-27 16:32:13 +0900587 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900589 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
590 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900591
592 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Alan Cox33f0f882006-01-09 20:54:13 -0800595 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 return copied;
599}
600
Paul Mundtd830fa42008-12-16 19:29:38 +0900601static inline int sci_handle_fifo_overrun(struct uart_port *port)
602{
603 struct tty_struct *tty = port->info->port.tty;
604 int copied = 0;
605
606 if (port->type != PORT_SCIF)
607 return 0;
608
609 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
610 sci_out(port, SCLSR, 0);
611
612 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
613 tty_flip_buffer_push(tty);
614
615 dev_notice(port->dev, "overrun error\n");
616 copied++;
617 }
618
619 return copied;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622static inline int sci_handle_breaks(struct uart_port *port)
623{
624 int copied = 0;
625 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100626 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000627 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900629 if (uart_handle_break(port))
630 return 0;
631
Paul Mundtb7a76e42006-02-01 03:06:06 -0800632 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633#if defined(CONFIG_CPU_SH3)
634 /* Debounce break */
635 s->break_flag = 1;
636#endif
637 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900638 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800639 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900640
641 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Alan Cox33f0f882006-01-09 20:54:13 -0800644 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900646
Paul Mundtd830fa42008-12-16 19:29:38 +0900647 copied += sci_handle_fifo_overrun(port);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return copied;
650}
651
David Howells7d12e782006-10-05 14:55:46 +0100652static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* I think sci_receive_chars has to be called irrespective
655 * of whether the I_IXOFF is set, otherwise, how is the interrupt
656 * to be disabled?
657 */
David Howells7d12e782006-10-05 14:55:46 +0100658 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return IRQ_HANDLED;
661}
662
David Howells7d12e782006-10-05 14:55:46 +0100663static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct uart_port *port = ptr;
666
Paul Mundte108b2c2006-09-27 16:32:13 +0900667 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900669 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 return IRQ_HANDLED;
672}
673
David Howells7d12e782006-10-05 14:55:46 +0100674static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct uart_port *port = ptr;
677
678 /* Handle errors */
679 if (port->type == PORT_SCI) {
680 if (sci_handle_errors(port)) {
681 /* discard character in rx buffer */
682 sci_in(port, SCxSR);
683 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
684 }
685 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900686 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100687 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
691
692 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100693 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return IRQ_HANDLED;
696}
697
David Howells7d12e782006-10-05 14:55:46 +0100698static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct uart_port *port = ptr;
701
702 /* Handle BREAKs */
703 sci_handle_breaks(port);
704 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
705
706 return IRQ_HANDLED;
707}
708
David Howells7d12e782006-10-05 14:55:46 +0100709static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900711 unsigned short ssr_status, scr_status;
712 struct uart_port *port = ptr;
713 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900715 ssr_status = sci_in(port, SCxSR);
716 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900719 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
720 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900722 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
723 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900725 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
726 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900728 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
729 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Michael Trimarchia8884e32008-10-31 16:10:23 +0900731 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Paul Mundt027e6872008-12-16 18:36:16 +0900734#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Here we define a transistion notifier so that we can update all of our
737 * ports' baud rate when the peripheral clock changes.
738 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900739static int sci_notifier(struct notifier_block *self,
740 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Magnus Damme552de22009-01-21 15:13:42 +0000742 struct sh_sci_priv *priv = container_of(self,
743 struct sh_sci_priv, clk_nb);
744 struct sci_port *sci_port;
745 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000748 (phase == CPUFREQ_RESUMECHANGE)) {
749 spin_lock_irqsave(&priv->lock, flags);
750 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000751 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000752
753 spin_unlock_irqrestore(&priv->lock, flags);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return NOTIFY_OK;
757}
Magnus Damm501b8252009-01-21 15:14:30 +0000758
759static void sci_clk_enable(struct uart_port *port)
760{
761 struct sci_port *sci_port = to_sci_port(port);
762
763 clk_enable(sci_port->dclk);
764 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
765
766 if (sci_port->iclk)
767 clk_enable(sci_port->iclk);
768}
769
770static void sci_clk_disable(struct uart_port *port)
771{
772 struct sci_port *sci_port = to_sci_port(port);
773
774 if (sci_port->iclk)
775 clk_disable(sci_port->iclk);
776
777 clk_disable(sci_port->dclk);
778}
Paul Mundt027e6872008-12-16 18:36:16 +0900779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781static int sci_request_irq(struct sci_port *port)
782{
783 int i;
David Howells7d12e782006-10-05 14:55:46 +0100784 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
786 sci_br_interrupt,
787 };
788 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
789 "SCI Transmit Data Empty", "SCI Break" };
790
791 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900792 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900794
795 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900796 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900797 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -ENODEV;
799 }
800 } else {
801 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900802 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900804
Paul Mundte108b2c2006-09-27 16:32:13 +0900805 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900806 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900807 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -ENODEV;
809 }
810 }
811 }
812
813 return 0;
814}
815
816static void sci_free_irq(struct sci_port *port)
817{
818 int i;
819
Paul Mundt762c69e2008-12-16 18:55:26 +0900820 if (port->irqs[0] == port->irqs[1])
821 free_irq(port->irqs[0], port);
822 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
824 if (!port->irqs[i])
825 continue;
826
827 free_irq(port->irqs[i], port);
828 }
829 }
830}
831
832static unsigned int sci_tx_empty(struct uart_port *port)
833{
834 /* Can't detect */
835 return TIOCSER_TEMT;
836}
837
838static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
839{
840 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
841 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
842 /* If you have signals for DTR and DCD, please implement here. */
843}
844
845static unsigned int sci_get_mctrl(struct uart_port *port)
846{
847 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
848 and CTS/RTS */
849
850 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
851}
852
Russell Kingb129a8c2005-08-31 10:12:14 +0100853static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Paul Mundte108b2c2006-09-27 16:32:13 +0900855 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Paul Mundte108b2c2006-09-27 16:32:13 +0900857 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
858 ctrl = sci_in(port, SCSCR);
859 ctrl |= SCI_CTRL_FLAGS_TIE;
860 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Russell Kingb129a8c2005-08-31 10:12:14 +0100863static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned short ctrl;
866
867 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 ctrl = sci_in(port, SCSCR);
869 ctrl &= ~SCI_CTRL_FLAGS_TIE;
870 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned short ctrl;
876
877 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ctrl = sci_in(port, SCSCR);
879 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
880 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void sci_stop_rx(struct uart_port *port)
884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned short ctrl;
886
887 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ctrl = sci_in(port, SCSCR);
889 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
890 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static void sci_enable_ms(struct uart_port *port)
894{
895 /* Nothing here yet .. */
896}
897
898static void sci_break_ctl(struct uart_port *port, int break_state)
899{
900 /* Nothing here yet .. */
901}
902
903static int sci_startup(struct uart_port *port)
904{
Magnus Damma5660ad2009-01-21 15:14:38 +0000905 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Paul Mundte108b2c2006-09-27 16:32:13 +0900907 if (s->enable)
908 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700911 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sci_start_rx(port, 1);
913
914 return 0;
915}
916
917static void sci_shutdown(struct uart_port *port)
918{
Magnus Damma5660ad2009-01-21 15:14:38 +0000919 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100922 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sci_free_irq(s);
924
Paul Mundte108b2c2006-09-27 16:32:13 +0900925 if (s->disable)
926 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Alan Cox606d0992006-12-08 02:38:45 -0800929static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
930 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900933 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900936 if (likely(baud))
937 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Paul Mundte108b2c2006-09-27 16:32:13 +0900939 do {
940 status = sci_in(port, SCxSR);
941 } while (!(status & SCxSR_TEND(port)));
942
943 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
944
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900945 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900946 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900947
948 smr_val = sci_in(port, SCSMR) & 3;
949 if ((termios->c_cflag & CSIZE) == CS7)
950 smr_val |= 0x40;
951 if (termios->c_cflag & PARENB)
952 smr_val |= 0x20;
953 if (termios->c_cflag & PARODD)
954 smr_val |= 0x30;
955 if (termios->c_cflag & CSTOPB)
956 smr_val |= 0x08;
957
958 uart_update_timeout(port, termios->c_cflag, baud);
959
960 sci_out(port, SCSMR, smr_val);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900963 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
965 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900966 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 sci_out(port, SCBRR, t);
970 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
971 }
972
Paul Mundtd5701642008-12-16 20:07:27 +0900973 sci_init_pins(port, termios->c_cflag);
974 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 sci_out(port, SCSCR, SCSCR_INIT(port));
977
978 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900979 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static const char *sci_type(struct uart_port *port)
983{
984 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900985 case PORT_IRDA:
986 return "irda";
987 case PORT_SCI:
988 return "sci";
989 case PORT_SCIF:
990 return "scif";
991 case PORT_SCIFA:
992 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
Paul Mundtfa439722008-09-04 18:53:58 +0900995 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998static void sci_release_port(struct uart_port *port)
999{
1000 /* Nothing here yet .. */
1001}
1002
1003static int sci_request_port(struct uart_port *port)
1004{
1005 /* Nothing here yet .. */
1006 return 0;
1007}
1008
1009static void sci_config_port(struct uart_port *port, int flags)
1010{
Magnus Damma5660ad2009-01-21 15:14:38 +00001011 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 port->type = s->type;
1014
Magnus Damm08f8cb32009-01-21 15:14:22 +00001015 if (port->membase)
1016 return;
1017
1018 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001019 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001020
1021 if (IS_ERR(port->membase))
1022 dev_err(port->dev, "can't remap port#%d\n", port->line);
1023 } else {
1024 /*
1025 * For the simple (and majority of) cases where we don't
1026 * need to do any remapping, just cast the cookie
1027 * directly.
1028 */
1029 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1034{
Magnus Damma5660ad2009-01-21 15:14:38 +00001035 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Yinghai Lua62c4132008-08-19 20:49:55 -07001037 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EINVAL;
1039 if (ser->baud_base < 2400)
1040 /* No paper tape reader for Mitch.. */
1041 return -EINVAL;
1042
1043 return 0;
1044}
1045
1046static struct uart_ops sci_uart_ops = {
1047 .tx_empty = sci_tx_empty,
1048 .set_mctrl = sci_set_mctrl,
1049 .get_mctrl = sci_get_mctrl,
1050 .start_tx = sci_start_tx,
1051 .stop_tx = sci_stop_tx,
1052 .stop_rx = sci_stop_rx,
1053 .enable_ms = sci_enable_ms,
1054 .break_ctl = sci_break_ctl,
1055 .startup = sci_startup,
1056 .shutdown = sci_shutdown,
1057 .set_termios = sci_set_termios,
1058 .type = sci_type,
1059 .release_port = sci_release_port,
1060 .request_port = sci_request_port,
1061 .config_port = sci_config_port,
1062 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001063#ifdef CONFIG_CONSOLE_POLL
1064 .poll_get_char = sci_poll_get_char,
1065 .poll_put_char = sci_poll_put_char,
1066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067};
1068
Magnus Damm501b8252009-01-21 15:14:30 +00001069static void __devinit sci_init_single(struct platform_device *dev,
1070 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001071 unsigned int index,
1072 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001073{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001074 sci_port->port.ops = &sci_uart_ops;
1075 sci_port->port.iotype = UPIO_MEM;
1076 sci_port->port.line = index;
1077 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001078
1079#if defined(__H8300H__) || defined(__H8300S__)
1080#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001081 sci_port->enable = h8300_sci_enable;
1082 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001084 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001085#elif defined(CONFIG_HAVE_CLK)
Magnus Damm501b8252009-01-21 15:14:30 +00001086 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
1087 sci_port->dclk = clk_get(&dev->dev, "module_clk");
1088 sci_port->enable = sci_clk_enable;
1089 sci_port->disable = sci_clk_disable;
Paul Mundta2159b52008-10-02 19:09:13 +09001090#else
1091#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001092#endif
1093
Magnus Damm7ed7e072009-01-21 15:14:14 +00001094 sci_port->break_timer.data = (unsigned long)sci_port;
1095 sci_port->break_timer.function = sci_break_timer;
1096 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001097
Magnus Damm7ed7e072009-01-21 15:14:14 +00001098 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001099 sci_port->port.membase = p->membase;
1100
1101 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1102 sci_port->port.flags = p->flags;
Magnus Damm501b8252009-01-21 15:14:30 +00001103 sci_port->port.dev = &dev->dev;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001104 sci_port->type = sci_port->port.type = p->type;
1105
1106 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Magnus Damm501b8252009-01-21 15:14:30 +00001107
Paul Mundte108b2c2006-09-27 16:32:13 +09001108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001111static struct tty_driver *serial_console_device(struct console *co, int *index)
1112{
1113 struct uart_driver *p = &sci_uart_driver;
1114 *index = co->index;
1115 return p->tty_driver;
1116}
1117
1118static void serial_console_putchar(struct uart_port *port, int ch)
1119{
1120 sci_poll_put_char(port, ch);
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
1124 * Print a string to the serial port trying not to disturb
1125 * any possible real use of the port...
1126 */
1127static void serial_console_write(struct console *co, const char *s,
1128 unsigned count)
1129{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001130 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001131 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001132 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001133
Magnus Damm501b8252009-01-21 15:14:30 +00001134 if (sci_port->enable)
1135 sci_port->enable(port);
1136
1137 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001138
1139 /* wait until fifo is empty and last bit has been transmitted */
1140 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1141 while ((sci_in(port, SCxSR) & bits) != bits)
1142 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001143
1144 if (sci_port->disable);
1145 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static int __init serial_console_setup(struct console *co, char *options)
1149{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001150 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct uart_port *port;
1152 int baud = 115200;
1153 int bits = 8;
1154 int parity = 'n';
1155 int flow = 'n';
1156 int ret;
1157
Paul Mundte108b2c2006-09-27 16:32:13 +09001158 /*
1159 * Check whether an invalid uart number has been specified, and
1160 * if so, search for the first available port that does have
1161 * console support.
1162 */
1163 if (co->index >= SCI_NPORTS)
1164 co->index = 0;
1165
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001166 sci_port = &sci_ports[co->index];
1167 port = &sci_port->port;
1168 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001171 * Also need to check port->type, we don't actually have any
1172 * UPIO_PORT ports, but uart_report_port() handily misreports
1173 * it anyways if we don't have a port available by the time this is
1174 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001176 if (!port->type)
1177 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001178
Magnus Damm08f8cb32009-01-21 15:14:22 +00001179 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001180
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001181 if (sci_port->enable)
1182 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (options)
1185 uart_parse_options(options, &baud, &parity, &bits, &flow);
1186
1187 ret = uart_set_options(port, co, baud, parity, bits, flow);
1188#if defined(__H8300H__) || defined(__H8300S__)
1189 /* disable rx interrupt */
1190 if (ret == 0)
1191 sci_stop_rx(port);
1192#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001193 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ret;
1195}
1196
1197static struct console serial_console = {
1198 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001199 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .write = serial_console_write,
1201 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001202 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204};
1205
1206static int __init sci_console_init(void)
1207{
1208 register_console(&serial_console);
1209 return 0;
1210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211console_initcall(sci_console_init);
1212#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1213
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001214#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001215#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001217#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#endif
1219
1220static char banner[] __initdata =
1221 KERN_INFO "SuperH SCI(F) driver initialized\n";
1222
1223static struct uart_driver sci_uart_driver = {
1224 .owner = THIS_MODULE,
1225 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .dev_name = "ttySC",
1227 .major = SCI_MAJOR,
1228 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001229 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .cons = SCI_CONSOLE,
1231};
1232
Magnus Damme552de22009-01-21 15:13:42 +00001233
Paul Mundt54507f62009-05-08 23:48:33 +09001234static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001235{
1236 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1237 struct sci_port *p;
1238 unsigned long flags;
1239
1240#ifdef CONFIG_HAVE_CLK
1241 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1242#endif
1243
1244 spin_lock_irqsave(&priv->lock, flags);
1245 list_for_each_entry(p, &priv->ports, node)
1246 uart_remove_one_port(&sci_uart_driver, &p->port);
1247
1248 spin_unlock_irqrestore(&priv->lock, flags);
1249
1250 kfree(priv);
1251 return 0;
1252}
1253
Magnus Damm0ee70712009-01-21 15:13:50 +00001254static int __devinit sci_probe_single(struct platform_device *dev,
1255 unsigned int index,
1256 struct plat_sci_port *p,
1257 struct sci_port *sciport)
1258{
1259 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1260 unsigned long flags;
1261 int ret;
1262
1263 /* Sanity check */
1264 if (unlikely(index >= SCI_NPORTS)) {
1265 dev_notice(&dev->dev, "Attempting to register port "
1266 "%d when only %d are available.\n",
1267 index+1, SCI_NPORTS);
1268 dev_notice(&dev->dev, "Consider bumping "
1269 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1270 return 0;
1271 }
1272
Magnus Damm501b8252009-01-21 15:14:30 +00001273 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001274
1275 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001276 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001277 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001278
1279 INIT_LIST_HEAD(&sciport->node);
1280
1281 spin_lock_irqsave(&priv->lock, flags);
1282 list_add(&sciport->node, &priv->ports);
1283 spin_unlock_irqrestore(&priv->lock, flags);
1284
1285 return 0;
1286}
1287
Paul Mundte108b2c2006-09-27 16:32:13 +09001288/*
1289 * Register a set of serial devices attached to a platform device. The
1290 * list is terminated with a zero flags entry, which means we expect
1291 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1292 * remapping (such as sh64) should also set UPF_IOREMAP.
1293 */
1294static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Paul Mundte108b2c2006-09-27 16:32:13 +09001296 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001297 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001298 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001299
1300 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1301 if (!priv)
1302 return -ENOMEM;
1303
1304 INIT_LIST_HEAD(&priv->ports);
1305 spin_lock_init(&priv->lock);
1306 platform_set_drvdata(dev, priv);
1307
1308#ifdef CONFIG_HAVE_CLK
1309 priv->clk_nb.notifier_call = sci_notifier;
1310 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1311#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Magnus Damm0ee70712009-01-21 15:13:50 +00001313 if (dev->id != -1) {
1314 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1315 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001316 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001317 } else {
1318 for (i = 0; p && p->flags != 0; p++, i++) {
1319 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1320 if (ret)
1321 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001322 }
Magnus Damme552de22009-01-21 15:13:42 +00001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325#ifdef CONFIG_SH_STANDARD_BIOS
1326 sh_bios_gdb_detach();
1327#endif
1328
Paul Mundte108b2c2006-09-27 16:32:13 +09001329 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001330
1331err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001332 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001333 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001334}
1335
Paul Mundte108b2c2006-09-27 16:32:13 +09001336static int sci_suspend(struct platform_device *dev, pm_message_t state)
1337{
Magnus Damme552de22009-01-21 15:13:42 +00001338 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1339 struct sci_port *p;
1340 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001341
Magnus Damme552de22009-01-21 15:13:42 +00001342 spin_lock_irqsave(&priv->lock, flags);
1343 list_for_each_entry(p, &priv->ports, node)
1344 uart_suspend_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001345
Magnus Damme552de22009-01-21 15:13:42 +00001346 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001347
1348 return 0;
1349}
1350
1351static int sci_resume(struct platform_device *dev)
1352{
Magnus Damme552de22009-01-21 15:13:42 +00001353 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1354 struct sci_port *p;
1355 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001356
Magnus Damme552de22009-01-21 15:13:42 +00001357 spin_lock_irqsave(&priv->lock, flags);
1358 list_for_each_entry(p, &priv->ports, node)
1359 uart_resume_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001360
Magnus Damme552de22009-01-21 15:13:42 +00001361 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001362
1363 return 0;
1364}
1365
1366static struct platform_driver sci_driver = {
1367 .probe = sci_probe,
1368 .remove = __devexit_p(sci_remove),
1369 .suspend = sci_suspend,
1370 .resume = sci_resume,
1371 .driver = {
1372 .name = "sh-sci",
1373 .owner = THIS_MODULE,
1374 },
1375};
1376
1377static int __init sci_init(void)
1378{
1379 int ret;
1380
1381 printk(banner);
1382
Paul Mundte108b2c2006-09-27 16:32:13 +09001383 ret = uart_register_driver(&sci_uart_driver);
1384 if (likely(ret == 0)) {
1385 ret = platform_driver_register(&sci_driver);
1386 if (unlikely(ret))
1387 uart_unregister_driver(&sci_uart_driver);
1388 }
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return ret;
1391}
1392
1393static void __exit sci_exit(void)
1394{
Paul Mundte108b2c2006-09-27 16:32:13 +09001395 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 uart_unregister_driver(&sci_uart_driver);
1397}
1398
1399module_init(sci_init);
1400module_exit(sci_exit);
1401
Paul Mundte108b2c2006-09-27 16:32:13 +09001402MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001403MODULE_ALIAS("platform:sh-sci");