blob: af5da110d52bd52a63b80b894829c506cfc978ed [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundte108b2c2006-09-27 16:32:13 +090062 /* Port type */
63 unsigned int type;
64
65 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090066 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090067
Paul Mundte108b2c2006-09-27 16:32:13 +090068 /* Port enable callback */
69 void (*enable)(struct uart_port *port);
70
71 /* Port disable callback */
72 void (*disable)(struct uart_port *port);
73
74 /* Break timer */
75 struct timer_list break_timer;
76 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090077
Paul Mundta2159b52008-10-02 19:09:13 +090078#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +090079 /* Port clock */
80 struct clk *clk;
Paul Mundt005a3362007-04-26 11:45:32 +090081#endif
Magnus Damme552de22009-01-21 15:13:42 +000082 struct list_head node;
83};
84
85struct sh_sci_priv {
86 spinlock_t lock;
87 struct list_head ports;
88
89#ifdef CONFIG_HAVE_CLK
90 struct notifier_block clk_nb;
91#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090092};
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Paul Mundte108b2c2006-09-27 16:32:13 +090095static struct sci_port *serial_console_port;
96#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010099static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paul Mundte108b2c2006-09-27 16:32:13 +0900101#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
102
103static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static struct uart_driver sci_uart_driver;
105
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900106static inline struct sci_port *
107to_sci_port(struct uart_port *uart)
108{
109 return container_of(uart, struct sci_port, port);
110}
111
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900112#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900113
114#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900115static inline void handle_error(struct uart_port *port)
116{
117 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
119}
120
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900121static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned short status;
124 int c;
125
Paul Mundte108b2c2006-09-27 16:32:13 +0900126 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 status = sci_in(port, SCxSR);
128 if (status & SCxSR_ERRORS(port)) {
129 handle_error(port);
130 continue;
131 }
132 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900135
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900136 /* Dummy read */
137 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return c;
141}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900144static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 unsigned short status;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 do {
149 status = sci_in(port, SCxSR);
150 } while (!(status & SCxSR_TDxE(port)));
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sci_in(port, SCxSR); /* Dummy read */
Magnus Damm973e5d52009-02-24 15:57:12 +0900153 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900154 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900156#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#if defined(__H8300S__)
159enum { sci_disable, sci_enable };
160
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900161static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900163 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int ch = (port->mapbase - SMR0) >> 3;
165 unsigned char mask = 1 << (ch+1);
166
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900167 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900169 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Paul Mundte108b2c2006-09-27 16:32:13 +0900172
173static inline void h8300_sci_enable(struct uart_port *port)
174{
175 h8300_sci_config(port, sci_enable);
176}
177
178static inline void h8300_sci_disable(struct uart_port *port)
179{
180 h8300_sci_config(port, sci_disable);
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#endif
183
Paul Mundt15c73aa2008-10-02 19:47:12 +0900184#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900185static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int ch = (port->mapbase - SMR0) >> 3;
188
189 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900190 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
191 h8300_sci_pins[ch].rx,
192 H8300_GPIO_INPUT);
193 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
194 h8300_sci_pins[ch].tx,
195 H8300_GPIO_OUTPUT);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* tx mark output*/
198 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
199}
Paul Mundtd5701642008-12-16 20:07:27 +0900200#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
201static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900202{
Paul Mundtd5701642008-12-16 20:07:27 +0900203 if (port->mapbase == 0xA4400000) {
204 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
205 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
206 } else if (port->mapbase == 0xA4410000)
207 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900208}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900209#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900210static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900211{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900212 unsigned short data;
213
214 if (cflag & CRTSCTS) {
215 /* enable RTS/CTS */
216 if (port->mapbase == 0xa4430000) { /* SCIF0 */
217 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900218 data = __raw_readw(PORT_PTCR);
219 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900220 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
221 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900222 data = __raw_readw(PORT_PVCR);
223 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900224 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900225 } else {
226 if (port->mapbase == 0xa4430000) { /* SCIF0 */
227 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900228 data = __raw_readw(PORT_PTCR);
229 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900230 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
231 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900232 data = __raw_readw(PORT_PVCR);
233 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900234 }
235 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900236}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800237#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900238/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900239static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800241 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Paul Mundtb7a76e42006-02-01 03:06:06 -0800243 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900244 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800245 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900246 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800247
Paul Mundtd5701642008-12-16 20:07:27 +0900248 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900250 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Clear out SCP7MD1,0, SCP4MD1,0,
252 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900253 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 data = ctrl_inb(SCPDR);
256 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800257 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
Paul Mundt41504c32006-12-11 20:28:03 +0900260#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900261static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900262{
Magnus Damm346b7462008-04-23 21:25:29 +0900263 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900264
Magnus Damm346b7462008-04-23 21:25:29 +0900265 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900266 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900267 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900268 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900269 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900270
Paul Mundtd5701642008-12-16 20:07:27 +0900271 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900272 }
Paul Mundt41504c32006-12-11 20:28:03 +0900273}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900274#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
275 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900276 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900277 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900278 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900279static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
280{
281 if (!(cflag & CRTSCTS))
282 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
283}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900284#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900285static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
286{
287 if (!(cflag & CRTSCTS))
288 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
289}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800290#else
Paul Mundtd5701642008-12-16 20:07:27 +0900291static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
292{
293 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Paul Mundte108b2c2006-09-27 16:32:13 +0900295#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Paul Mundt32351a22007-03-12 14:38:59 +0900297#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
298 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900299 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
300 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900301static inline int scif_txroom(struct uart_port *port)
302{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900303 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900304}
305
306static inline int scif_rxroom(struct uart_port *port)
307{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900308 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900309}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900310#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
311static inline int scif_txroom(struct uart_port *port)
312{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900313 if ((port->mapbase == 0xffe00000) ||
314 (port->mapbase == 0xffe08000)) {
315 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900316 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900317 } else {
318 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900319 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900320 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900321}
322
323static inline int scif_rxroom(struct uart_port *port)
324{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900325 if ((port->mapbase == 0xffe00000) ||
326 (port->mapbase == 0xffe08000)) {
327 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900328 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900329 } else {
330 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900331 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900332 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900333}
Paul Mundte108b2c2006-09-27 16:32:13 +0900334#else
335static inline int scif_txroom(struct uart_port *port)
336{
337 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
338}
339
340static inline int scif_rxroom(struct uart_port *port)
341{
342 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Paul Mundte108b2c2006-09-27 16:32:13 +0900346static inline int sci_txroom(struct uart_port *port)
347{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900348 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900349}
350
351static inline int sci_rxroom(struct uart_port *port)
352{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900353 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* ********************************************************************** *
357 * the interrupt related routines *
358 * ********************************************************************** */
359
360static void sci_transmit_chars(struct uart_port *port)
361{
362 struct circ_buf *xmit = &port->info->xmit;
363 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned short status;
365 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900366 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 status = sci_in(port, SCxSR);
369 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900371 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900373 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return;
377 }
378
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900379 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900380 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900381 else
382 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 do {
385 unsigned char c;
386
387 if (port->x_char) {
388 c = port->x_char;
389 port->x_char = 0;
390 } else if (!uart_circ_empty(xmit) && !stopped) {
391 c = xmit->buf[xmit->tail];
392 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
393 } else {
394 break;
395 }
396
397 sci_out(port, SCxTDR, c);
398
399 port->icount.tx++;
400 } while (--count > 0);
401
402 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
403
404 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
405 uart_write_wakeup(port);
406 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100407 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ctrl = sci_in(port, SCSCR);
410
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900411 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sci_in(port, SCxSR); /* Dummy read */
413 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 ctrl |= SCI_CTRL_FLAGS_TIE;
417 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419}
420
421/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900422#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David Howells7d12e782006-10-05 14:55:46 +0100424static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900426 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100427 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int i, count, copied = 0;
429 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800430 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 status = sci_in(port, SCxSR);
433 if (!(status & SCxSR_RDxF(port)))
434 return;
435
436 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900437 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900438 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900439 else
440 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800443 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* If for any reason we can't copy more data, we're done! */
446 if (count == 0)
447 break;
448
449 if (port->type == PORT_SCI) {
450 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900451 if (uart_handle_sysrq_char(port, c) ||
452 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900454 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900455 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900457 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 char c = sci_in(port, SCxRDR);
459 status = sci_in(port, SCxSR);
460#if defined(CONFIG_CPU_SH3)
461 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900462 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if ((c == 0) &&
464 (status & SCxSR_FER(port))) {
465 count--; i--;
466 continue;
467 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900470 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900471 sci_port->break_flag = 0;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (STEPFN(c)) {
474 count--; i--;
475 continue;
476 }
477 }
478#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100479 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 count--; i--;
481 continue;
482 }
483
484 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800486 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900487 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800489 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900490 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800491 } else
492 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900493
Alan Cox33f0f882006-01-09 20:54:13 -0800494 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497
498 sci_in(port, SCxSR); /* dummy read */
499 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 copied += count;
502 port->icount.rx += count;
503 }
504
505 if (copied) {
506 /* Tell the rest of the system the news. New characters! */
507 tty_flip_buffer_push(tty);
508 } else {
509 sci_in(port, SCxSR); /* dummy read */
510 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
511 }
512}
513
514#define SCI_BREAK_JIFFIES (HZ/20)
515/* The sci generates interrupts during the break,
516 * 1 per millisecond or so during the break period, for 9600 baud.
517 * So dont bother disabling interrupts.
518 * But dont want more than 1 break event.
519 * Use a kernel timer to periodically poll the rx line until
520 * the break is finished.
521 */
522static void sci_schedule_break_timer(struct sci_port *port)
523{
524 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
525 add_timer(&port->break_timer);
526}
527/* Ensure that two consecutive samples find the break over. */
528static void sci_break_timer(unsigned long data)
529{
Paul Mundte108b2c2006-09-27 16:32:13 +0900530 struct sci_port *port = (struct sci_port *)data;
531
532 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900534 sci_schedule_break_timer(port);
535 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* break is over. */
537 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900538 sci_schedule_break_timer(port);
539 } else
540 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543static inline int sci_handle_errors(struct uart_port *port)
544{
545 int copied = 0;
546 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100547 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Paul Mundte108b2c2006-09-27 16:32:13 +0900549 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900551 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800552 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900553
554 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Paul Mundte108b2c2006-09-27 16:32:13 +0900557 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (sci_rxd_in(port) == 0) {
559 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900560 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900561
562 if (!sci_port->break_flag) {
563 sci_port->break_flag = 1;
564 sci_schedule_break_timer(sci_port);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900569
570 dev_dbg(port->dev, "BREAK detected\n");
571
Paul Mundte108b2c2006-09-27 16:32:13 +0900572 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900573 copied++;
574 }
575
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900578 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800579 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900580
581 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
584
Paul Mundte108b2c2006-09-27 16:32:13 +0900585 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900587 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
588 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900589
590 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Alan Cox33f0f882006-01-09 20:54:13 -0800593 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 return copied;
597}
598
Paul Mundtd830fa42008-12-16 19:29:38 +0900599static inline int sci_handle_fifo_overrun(struct uart_port *port)
600{
601 struct tty_struct *tty = port->info->port.tty;
602 int copied = 0;
603
604 if (port->type != PORT_SCIF)
605 return 0;
606
607 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
608 sci_out(port, SCLSR, 0);
609
610 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
611 tty_flip_buffer_push(tty);
612
613 dev_notice(port->dev, "overrun error\n");
614 copied++;
615 }
616
617 return copied;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static inline int sci_handle_breaks(struct uart_port *port)
621{
622 int copied = 0;
623 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100624 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct sci_port *s = &sci_ports[port->line];
626
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900627 if (uart_handle_break(port))
628 return 0;
629
Paul Mundtb7a76e42006-02-01 03:06:06 -0800630 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#if defined(CONFIG_CPU_SH3)
632 /* Debounce break */
633 s->break_flag = 1;
634#endif
635 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900636 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800637 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900638
639 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Alan Cox33f0f882006-01-09 20:54:13 -0800642 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900644
Paul Mundtd830fa42008-12-16 19:29:38 +0900645 copied += sci_handle_fifo_overrun(port);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return copied;
648}
649
David Howells7d12e782006-10-05 14:55:46 +0100650static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* I think sci_receive_chars has to be called irrespective
653 * of whether the I_IXOFF is set, otherwise, how is the interrupt
654 * to be disabled?
655 */
David Howells7d12e782006-10-05 14:55:46 +0100656 sci_receive_chars(port);
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_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct uart_port *port = ptr;
664
Paul Mundte108b2c2006-09-27 16:32:13 +0900665 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900667 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return IRQ_HANDLED;
670}
671
David Howells7d12e782006-10-05 14:55:46 +0100672static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct uart_port *port = ptr;
675
676 /* Handle errors */
677 if (port->type == PORT_SCI) {
678 if (sci_handle_errors(port)) {
679 /* discard character in rx buffer */
680 sci_in(port, SCxSR);
681 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
682 }
683 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900684 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100685 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
689
690 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100691 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return IRQ_HANDLED;
694}
695
David Howells7d12e782006-10-05 14:55:46 +0100696static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct uart_port *port = ptr;
699
700 /* Handle BREAKs */
701 sci_handle_breaks(port);
702 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
703
704 return IRQ_HANDLED;
705}
706
David Howells7d12e782006-10-05 14:55:46 +0100707static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900709 unsigned short ssr_status, scr_status;
710 struct uart_port *port = ptr;
711 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900713 ssr_status = sci_in(port, SCxSR);
714 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900717 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
718 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900720 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
721 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900723 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
724 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900726 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
727 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Michael Trimarchia8884e32008-10-31 16:10:23 +0900729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Paul Mundt027e6872008-12-16 18:36:16 +0900732#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * Here we define a transistion notifier so that we can update all of our
735 * ports' baud rate when the peripheral clock changes.
736 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900737static int sci_notifier(struct notifier_block *self,
738 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Magnus Damme552de22009-01-21 15:13:42 +0000740 struct sh_sci_priv *priv = container_of(self,
741 struct sh_sci_priv, clk_nb);
742 struct sci_port *sci_port;
743 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000746 (phase == CPUFREQ_RESUMECHANGE)) {
747 spin_lock_irqsave(&priv->lock, flags);
748 list_for_each_entry(sci_port, &priv->ports, node)
749 sci_port->port.uartclk = clk_get_rate(sci_port->clk);
750
751 spin_unlock_irqrestore(&priv->lock, flags);
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return NOTIFY_OK;
755}
Paul Mundt027e6872008-12-16 18:36:16 +0900756#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758static int sci_request_irq(struct sci_port *port)
759{
760 int i;
David Howells7d12e782006-10-05 14:55:46 +0100761 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
763 sci_br_interrupt,
764 };
765 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
766 "SCI Transmit Data Empty", "SCI Break" };
767
768 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900769 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900771
772 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900773 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900774 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -ENODEV;
776 }
777 } else {
778 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900779 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900781
Paul Mundte108b2c2006-09-27 16:32:13 +0900782 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900783 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900784 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -ENODEV;
786 }
787 }
788 }
789
790 return 0;
791}
792
793static void sci_free_irq(struct sci_port *port)
794{
795 int i;
796
Paul Mundt762c69e2008-12-16 18:55:26 +0900797 if (port->irqs[0] == port->irqs[1])
798 free_irq(port->irqs[0], port);
799 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
801 if (!port->irqs[i])
802 continue;
803
804 free_irq(port->irqs[i], port);
805 }
806 }
807}
808
809static unsigned int sci_tx_empty(struct uart_port *port)
810{
811 /* Can't detect */
812 return TIOCSER_TEMT;
813}
814
815static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
816{
817 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
818 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
819 /* If you have signals for DTR and DCD, please implement here. */
820}
821
822static unsigned int sci_get_mctrl(struct uart_port *port)
823{
824 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
825 and CTS/RTS */
826
827 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
828}
829
Russell Kingb129a8c2005-08-31 10:12:14 +0100830static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Paul Mundte108b2c2006-09-27 16:32:13 +0900832 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Paul Mundte108b2c2006-09-27 16:32:13 +0900834 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
835 ctrl = sci_in(port, SCSCR);
836 ctrl |= SCI_CTRL_FLAGS_TIE;
837 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Russell Kingb129a8c2005-08-31 10:12:14 +0100840static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned short ctrl;
843
844 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 ctrl = sci_in(port, SCSCR);
846 ctrl &= ~SCI_CTRL_FLAGS_TIE;
847 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned short ctrl;
853
854 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 ctrl = sci_in(port, SCSCR);
856 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
857 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860static void sci_stop_rx(struct uart_port *port)
861{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned short ctrl;
863
864 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ctrl = sci_in(port, SCSCR);
866 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
867 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static void sci_enable_ms(struct uart_port *port)
871{
872 /* Nothing here yet .. */
873}
874
875static void sci_break_ctl(struct uart_port *port, int break_state)
876{
877 /* Nothing here yet .. */
878}
879
880static int sci_startup(struct uart_port *port)
881{
882 struct sci_port *s = &sci_ports[port->line];
883
Paul Mundte108b2c2006-09-27 16:32:13 +0900884 if (s->enable)
885 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Paul Mundta2159b52008-10-02 19:09:13 +0900887#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900888 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +0900889#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700892 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 sci_start_rx(port, 1);
894
895 return 0;
896}
897
898static void sci_shutdown(struct uart_port *port)
899{
900 struct sci_port *s = &sci_ports[port->line];
901
902 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100903 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 sci_free_irq(s);
905
Paul Mundte108b2c2006-09-27 16:32:13 +0900906 if (s->disable)
907 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900908
Paul Mundta2159b52008-10-02 19:09:13 +0900909#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900910 clk_put(s->clk);
911 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +0900912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Alan Cox606d0992006-12-08 02:38:45 -0800915static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
916 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900919 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900922 if (likely(baud))
923 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Paul Mundte108b2c2006-09-27 16:32:13 +0900925 do {
926 status = sci_in(port, SCxSR);
927 } while (!(status & SCxSR_TEND(port)));
928
929 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
930
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900931 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900932 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900933
934 smr_val = sci_in(port, SCSMR) & 3;
935 if ((termios->c_cflag & CSIZE) == CS7)
936 smr_val |= 0x40;
937 if (termios->c_cflag & PARENB)
938 smr_val |= 0x20;
939 if (termios->c_cflag & PARODD)
940 smr_val |= 0x30;
941 if (termios->c_cflag & CSTOPB)
942 smr_val |= 0x08;
943
944 uart_update_timeout(port, termios->c_cflag, baud);
945
946 sci_out(port, SCSMR, smr_val);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900949 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
951 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900952 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sci_out(port, SCBRR, t);
956 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
957 }
958
Paul Mundtd5701642008-12-16 20:07:27 +0900959 sci_init_pins(port, termios->c_cflag);
960 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sci_out(port, SCSCR, SCSCR_INIT(port));
963
964 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900965 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968static const char *sci_type(struct uart_port *port)
969{
970 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900971 case PORT_IRDA:
972 return "irda";
973 case PORT_SCI:
974 return "sci";
975 case PORT_SCIF:
976 return "scif";
977 case PORT_SCIFA:
978 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Paul Mundtfa439722008-09-04 18:53:58 +0900981 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984static void sci_release_port(struct uart_port *port)
985{
986 /* Nothing here yet .. */
987}
988
989static int sci_request_port(struct uart_port *port)
990{
991 /* Nothing here yet .. */
992 return 0;
993}
994
995static void sci_config_port(struct uart_port *port, int flags)
996{
997 struct sci_port *s = &sci_ports[port->line];
998
999 port->type = s->type;
1000
Paul Mundt7ff731a2008-10-01 15:46:58 +09001001 if (port->flags & UPF_IOREMAP && !port->membase) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001002 port->membase = ioremap_nocache(port->mapbase, 0x40);
Paul Mundt762c69e2008-12-16 18:55:26 +09001003 dev_err(port->dev, "can't remap port#%d\n", port->line);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1008{
1009 struct sci_port *s = &sci_ports[port->line];
1010
Yinghai Lua62c4132008-08-19 20:49:55 -07001011 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -EINVAL;
1013 if (ser->baud_base < 2400)
1014 /* No paper tape reader for Mitch.. */
1015 return -EINVAL;
1016
1017 return 0;
1018}
1019
1020static struct uart_ops sci_uart_ops = {
1021 .tx_empty = sci_tx_empty,
1022 .set_mctrl = sci_set_mctrl,
1023 .get_mctrl = sci_get_mctrl,
1024 .start_tx = sci_start_tx,
1025 .stop_tx = sci_stop_tx,
1026 .stop_rx = sci_stop_rx,
1027 .enable_ms = sci_enable_ms,
1028 .break_ctl = sci_break_ctl,
1029 .startup = sci_startup,
1030 .shutdown = sci_shutdown,
1031 .set_termios = sci_set_termios,
1032 .type = sci_type,
1033 .release_port = sci_release_port,
1034 .request_port = sci_request_port,
1035 .config_port = sci_config_port,
1036 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001037#ifdef CONFIG_CONSOLE_POLL
1038 .poll_get_char = sci_poll_get_char,
1039 .poll_put_char = sci_poll_put_char,
1040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041};
1042
Paul Mundte108b2c2006-09-27 16:32:13 +09001043static void __init sci_init_ports(void)
1044{
1045 static int first = 1;
1046 int i;
1047
1048 if (!first)
1049 return;
1050
1051 first = 0;
1052
1053 for (i = 0; i < SCI_NPORTS; i++) {
1054 sci_ports[i].port.ops = &sci_uart_ops;
1055 sci_ports[i].port.iotype = UPIO_MEM;
1056 sci_ports[i].port.line = i;
1057 sci_ports[i].port.fifosize = 1;
1058
1059#if defined(__H8300H__) || defined(__H8300S__)
1060#ifdef __H8300S__
1061 sci_ports[i].enable = h8300_sci_enable;
1062 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001064 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001065#elif defined(CONFIG_HAVE_CLK)
Paul Mundte108b2c2006-09-27 16:32:13 +09001066 /*
1067 * XXX: We should use a proper SCI/SCIF clock
1068 */
1069 {
Paul Mundt1d118562006-12-01 13:15:14 +09001070 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundta2159b52008-10-02 19:09:13 +09001071 sci_ports[i].port.uartclk = clk_get_rate(clk);
Paul Mundte108b2c2006-09-27 16:32:13 +09001072 clk_put(clk);
1073 }
Paul Mundta2159b52008-10-02 19:09:13 +09001074#else
1075#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001076#endif
1077
1078 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1079 sci_ports[i].break_timer.function = sci_break_timer;
1080
1081 init_timer(&sci_ports[i].break_timer);
1082 }
1083}
1084
1085int __init early_sci_setup(struct uart_port *port)
1086{
1087 if (unlikely(port->line > SCI_NPORTS))
1088 return -ENODEV;
1089
1090 sci_init_ports();
1091
1092 sci_ports[port->line].port.membase = port->membase;
1093 sci_ports[port->line].port.mapbase = port->mapbase;
1094 sci_ports[port->line].port.type = port->type;
1095
1096 return 0;
1097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1100/*
1101 * Print a string to the serial port trying not to disturb
1102 * any possible real use of the port...
1103 */
1104static void serial_console_write(struct console *co, const char *s,
1105 unsigned count)
1106{
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001107 struct uart_port *port = &serial_console_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001108 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001109 int i;
1110
1111 for (i = 0; i < count; i++) {
1112 if (*s == 10)
1113 sci_poll_put_char(port, '\r');
1114
1115 sci_poll_put_char(port, *s++);
1116 }
Magnus Damm973e5d52009-02-24 15:57:12 +09001117
1118 /* wait until fifo is empty and last bit has been transmitted */
1119 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1120 while ((sci_in(port, SCxSR) & bits) != bits)
1121 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124static int __init serial_console_setup(struct console *co, char *options)
1125{
1126 struct uart_port *port;
1127 int baud = 115200;
1128 int bits = 8;
1129 int parity = 'n';
1130 int flow = 'n';
1131 int ret;
1132
Paul Mundte108b2c2006-09-27 16:32:13 +09001133 /*
1134 * Check whether an invalid uart number has been specified, and
1135 * if so, search for the first available port that does have
1136 * console support.
1137 */
1138 if (co->index >= SCI_NPORTS)
1139 co->index = 0;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 serial_console_port = &sci_ports[co->index];
1142 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001145 * Also need to check port->type, we don't actually have any
1146 * UPIO_PORT ports, but uart_report_port() handily misreports
1147 * it anyways if we don't have a port available by the time this is
1148 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001150 if (!port->type)
1151 return -ENODEV;
1152 if (!port->membase || !port->mapbase)
1153 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001154
Paul Mundte108b2c2006-09-27 16:32:13 +09001155 port->type = serial_console_port->type;
1156
Paul Mundta2159b52008-10-02 19:09:13 +09001157#ifdef CONFIG_HAVE_CLK
Paul Mundt005a3362007-04-26 11:45:32 +09001158 if (!serial_console_port->clk)
1159 serial_console_port->clk = clk_get(NULL, "module_clk");
1160#endif
1161
Paul Mundte108b2c2006-09-27 16:32:13 +09001162 if (port->flags & UPF_IOREMAP)
1163 sci_config_port(port, 0);
1164
1165 if (serial_console_port->enable)
1166 serial_console_port->enable(port);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (options)
1169 uart_parse_options(options, &baud, &parity, &bits, &flow);
1170
1171 ret = uart_set_options(port, co, baud, parity, bits, flow);
1172#if defined(__H8300H__) || defined(__H8300S__)
1173 /* disable rx interrupt */
1174 if (ret == 0)
1175 sci_stop_rx(port);
1176#endif
1177 return ret;
1178}
1179
1180static struct console serial_console = {
1181 .name = "ttySC",
1182 .device = uart_console_device,
1183 .write = serial_console_write,
1184 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001185 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 .index = -1,
1187 .data = &sci_uart_driver,
1188};
1189
1190static int __init sci_console_init(void)
1191{
Paul Mundte108b2c2006-09-27 16:32:13 +09001192 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 register_console(&serial_console);
1194 return 0;
1195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196console_initcall(sci_console_init);
1197#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1198
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001199#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001200#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001202#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#endif
1204
1205static char banner[] __initdata =
1206 KERN_INFO "SuperH SCI(F) driver initialized\n";
1207
1208static struct uart_driver sci_uart_driver = {
1209 .owner = THIS_MODULE,
1210 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 .dev_name = "ttySC",
1212 .major = SCI_MAJOR,
1213 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001214 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 .cons = SCI_CONSOLE,
1216};
1217
Magnus Damme552de22009-01-21 15:13:42 +00001218
1219static int __devexit sci_remove(struct platform_device *dev)
1220{
1221 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1222 struct sci_port *p;
1223 unsigned long flags;
1224
1225#ifdef CONFIG_HAVE_CLK
1226 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1227#endif
1228
1229 spin_lock_irqsave(&priv->lock, flags);
1230 list_for_each_entry(p, &priv->ports, node)
1231 uart_remove_one_port(&sci_uart_driver, &p->port);
1232
1233 spin_unlock_irqrestore(&priv->lock, flags);
1234
1235 kfree(priv);
1236 return 0;
1237}
1238
Paul Mundte108b2c2006-09-27 16:32:13 +09001239/*
1240 * Register a set of serial devices attached to a platform device. The
1241 * list is terminated with a zero flags entry, which means we expect
1242 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1243 * remapping (such as sh64) should also set UPF_IOREMAP.
1244 */
1245static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Paul Mundte108b2c2006-09-27 16:32:13 +09001247 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001248 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001249 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001250 unsigned long flags;
1251
1252 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1253 if (!priv)
1254 return -ENOMEM;
1255
1256 INIT_LIST_HEAD(&priv->ports);
1257 spin_lock_init(&priv->lock);
1258 platform_set_drvdata(dev, priv);
1259
1260#ifdef CONFIG_HAVE_CLK
1261 priv->clk_nb.notifier_call = sci_notifier;
1262 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Paul Mundt32351a22007-03-12 14:38:59 +09001265 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001266 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Paul Mundt32351a22007-03-12 14:38:59 +09001268 /* Sanity check */
1269 if (unlikely(i == SCI_NPORTS)) {
1270 dev_notice(&dev->dev, "Attempting to register port "
1271 "%d when only %d are available.\n",
1272 i+1, SCI_NPORTS);
1273 dev_notice(&dev->dev, "Consider bumping "
1274 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1275 break;
1276 }
1277
Paul Mundte108b2c2006-09-27 16:32:13 +09001278 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001279
Paul Mundt7ff731a2008-10-01 15:46:58 +09001280 if (p->mapbase && !p->membase) {
1281 if (p->flags & UPF_IOREMAP) {
1282 p->membase = ioremap_nocache(p->mapbase, 0x40);
1283 if (IS_ERR(p->membase)) {
1284 ret = PTR_ERR(p->membase);
1285 goto err_unreg;
1286 }
1287 } else {
1288 /*
1289 * For the simple (and majority of) cases
1290 * where we don't need to do any remapping,
1291 * just cast the cookie directly.
1292 */
1293 p->membase = (void __iomem *)p->mapbase;
1294 }
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Paul Mundte108b2c2006-09-27 16:32:13 +09001297 sciport->port.membase = p->membase;
1298
1299 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1300 sciport->port.flags = p->flags;
1301 sciport->port.dev = &dev->dev;
1302
1303 sciport->type = sciport->port.type = p->type;
1304
1305 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1306
Magnus Damme552de22009-01-21 15:13:42 +00001307 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Magnus Damme552de22009-01-21 15:13:42 +00001309 if (ret && (p->flags & UPF_IOREMAP)) {
1310 iounmap(p->membase);
1311 goto err_unreg;
1312 }
1313
1314 INIT_LIST_HEAD(&sciport->node);
1315
1316 spin_lock_irqsave(&priv->lock, flags);
1317 list_add(&sciport->node, &priv->ports);
1318 spin_unlock_irqrestore(&priv->lock, flags);
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321#ifdef CONFIG_SH_STANDARD_BIOS
1322 sh_bios_gdb_detach();
1323#endif
1324
Paul Mundte108b2c2006-09-27 16:32:13 +09001325 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001326
1327err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001328 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001329 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001330}
1331
Paul Mundte108b2c2006-09-27 16:32:13 +09001332static int sci_suspend(struct platform_device *dev, pm_message_t state)
1333{
Magnus Damme552de22009-01-21 15:13:42 +00001334 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1335 struct sci_port *p;
1336 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001337
Magnus Damme552de22009-01-21 15:13:42 +00001338 spin_lock_irqsave(&priv->lock, flags);
1339 list_for_each_entry(p, &priv->ports, node)
1340 uart_suspend_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001341
Magnus Damme552de22009-01-21 15:13:42 +00001342 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001343
1344 return 0;
1345}
1346
1347static int sci_resume(struct platform_device *dev)
1348{
Magnus Damme552de22009-01-21 15:13:42 +00001349 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1350 struct sci_port *p;
1351 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001352
Magnus Damme552de22009-01-21 15:13:42 +00001353 spin_lock_irqsave(&priv->lock, flags);
1354 list_for_each_entry(p, &priv->ports, node)
1355 uart_resume_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001356
Magnus Damme552de22009-01-21 15:13:42 +00001357 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001358
1359 return 0;
1360}
1361
1362static struct platform_driver sci_driver = {
1363 .probe = sci_probe,
1364 .remove = __devexit_p(sci_remove),
1365 .suspend = sci_suspend,
1366 .resume = sci_resume,
1367 .driver = {
1368 .name = "sh-sci",
1369 .owner = THIS_MODULE,
1370 },
1371};
1372
1373static int __init sci_init(void)
1374{
1375 int ret;
1376
1377 printk(banner);
1378
1379 sci_init_ports();
1380
1381 ret = uart_register_driver(&sci_uart_driver);
1382 if (likely(ret == 0)) {
1383 ret = platform_driver_register(&sci_driver);
1384 if (unlikely(ret))
1385 uart_unregister_driver(&sci_uart_driver);
1386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return ret;
1389}
1390
1391static void __exit sci_exit(void)
1392{
Paul Mundte108b2c2006-09-27 16:32:13 +09001393 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 uart_unregister_driver(&sci_uart_driver);
1395}
1396
1397module_init(sci_init);
1398module_exit(sci_exit);
1399
Paul Mundte108b2c2006-09-27 16:32:13 +09001400MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001401MODULE_ALIAS("platform:sh-sci");