blob: 408624ae7fecb2ffd6209219813161c5f79143b8 [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/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010095static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Paul Mundte108b2c2006-09-27 16:32:13 +090097#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
98
99static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct uart_driver sci_uart_driver;
101
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900102static inline struct sci_port *
103to_sci_port(struct uart_port *uart)
104{
105 return container_of(uart, struct sci_port, port);
106}
107
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900108#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900109
110#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900111static inline void handle_error(struct uart_port *port)
112{
113 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
115}
116
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900117static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned short status;
120 int c;
121
Paul Mundte108b2c2006-09-27 16:32:13 +0900122 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 status = sci_in(port, SCxSR);
124 if (status & SCxSR_ERRORS(port)) {
125 handle_error(port);
126 continue;
127 }
128 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900131
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900132 /* Dummy read */
133 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 return c;
137}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900140static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned short status;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 do {
145 status = sci_in(port, SCxSR);
146 } while (!(status & SCxSR_TDxE(port)));
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sci_in(port, SCxSR); /* Dummy read */
Magnus Damm973e5d52009-02-24 15:57:12 +0900149 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900150 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900152#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#if defined(__H8300S__)
155enum { sci_disable, sci_enable };
156
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900157static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900159 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int ch = (port->mapbase - SMR0) >> 3;
161 unsigned char mask = 1 << (ch+1);
162
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900163 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900165 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Paul Mundte108b2c2006-09-27 16:32:13 +0900168
169static inline void h8300_sci_enable(struct uart_port *port)
170{
171 h8300_sci_config(port, sci_enable);
172}
173
174static inline void h8300_sci_disable(struct uart_port *port)
175{
176 h8300_sci_config(port, sci_disable);
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179
Paul Mundt15c73aa2008-10-02 19:47:12 +0900180#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900181static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 int ch = (port->mapbase - SMR0) >> 3;
184
185 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900186 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
187 h8300_sci_pins[ch].rx,
188 H8300_GPIO_INPUT);
189 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
190 h8300_sci_pins[ch].tx,
191 H8300_GPIO_OUTPUT);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* tx mark output*/
194 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
195}
Paul Mundtd5701642008-12-16 20:07:27 +0900196#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
197static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900198{
Paul Mundtd5701642008-12-16 20:07:27 +0900199 if (port->mapbase == 0xA4400000) {
200 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
201 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
202 } else if (port->mapbase == 0xA4410000)
203 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900204}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900205#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900206static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900207{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900208 unsigned short data;
209
210 if (cflag & CRTSCTS) {
211 /* enable RTS/CTS */
212 if (port->mapbase == 0xa4430000) { /* SCIF0 */
213 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900214 data = __raw_readw(PORT_PTCR);
215 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900216 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
217 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900218 data = __raw_readw(PORT_PVCR);
219 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900220 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900221 } else {
222 if (port->mapbase == 0xa4430000) { /* SCIF0 */
223 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900224 data = __raw_readw(PORT_PTCR);
225 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900226 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
227 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900228 data = __raw_readw(PORT_PVCR);
229 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900230 }
231 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900232}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800233#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900234/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900235static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800237 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paul Mundtb7a76e42006-02-01 03:06:06 -0800239 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900240 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800241 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900242 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800243
Paul Mundtd5701642008-12-16 20:07:27 +0900244 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900246 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Clear out SCP7MD1,0, SCP4MD1,0,
248 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900249 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 data = ctrl_inb(SCPDR);
252 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800253 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
Paul Mundt41504c32006-12-11 20:28:03 +0900256#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900257static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900258{
Magnus Damm346b7462008-04-23 21:25:29 +0900259 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900260
Magnus Damm346b7462008-04-23 21:25:29 +0900261 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900262 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900263 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900264 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900265 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900266
Paul Mundtd5701642008-12-16 20:07:27 +0900267 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900268 }
Paul Mundt41504c32006-12-11 20:28:03 +0900269}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900270#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
271 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900272 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900273 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900274 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900275static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
276{
277 if (!(cflag & CRTSCTS))
278 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
279}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900280#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
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, SCSPTR2); /* Set RTS = 1 */
285}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800286#else
Paul Mundtd5701642008-12-16 20:07:27 +0900287static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
288{
289 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Paul Mundte108b2c2006-09-27 16:32:13 +0900291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Paul Mundt32351a22007-03-12 14:38:59 +0900293#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
294 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900295 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
296 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900297static inline int scif_txroom(struct uart_port *port)
298{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900299 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900300}
301
302static inline int scif_rxroom(struct uart_port *port)
303{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900304 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900305}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900306#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
307static inline int scif_txroom(struct uart_port *port)
308{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900309 if ((port->mapbase == 0xffe00000) ||
310 (port->mapbase == 0xffe08000)) {
311 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900312 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900313 } else {
314 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900315 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900316 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317}
318
319static inline int scif_rxroom(struct uart_port *port)
320{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900321 if ((port->mapbase == 0xffe00000) ||
322 (port->mapbase == 0xffe08000)) {
323 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900324 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900325 } else {
326 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900327 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900328 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900329}
Paul Mundte108b2c2006-09-27 16:32:13 +0900330#else
331static inline int scif_txroom(struct uart_port *port)
332{
333 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
334}
335
336static inline int scif_rxroom(struct uart_port *port)
337{
338 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Paul Mundte108b2c2006-09-27 16:32:13 +0900342static inline int sci_txroom(struct uart_port *port)
343{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900344 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900345}
346
347static inline int sci_rxroom(struct uart_port *port)
348{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900349 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* ********************************************************************** *
353 * the interrupt related routines *
354 * ********************************************************************** */
355
356static void sci_transmit_chars(struct uart_port *port)
357{
358 struct circ_buf *xmit = &port->info->xmit;
359 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned short status;
361 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900362 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 status = sci_in(port, SCxSR);
365 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900367 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900369 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return;
373 }
374
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900375 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900376 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900377 else
378 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 do {
381 unsigned char c;
382
383 if (port->x_char) {
384 c = port->x_char;
385 port->x_char = 0;
386 } else if (!uart_circ_empty(xmit) && !stopped) {
387 c = xmit->buf[xmit->tail];
388 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
389 } else {
390 break;
391 }
392
393 sci_out(port, SCxTDR, c);
394
395 port->icount.tx++;
396 } while (--count > 0);
397
398 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
399
400 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
401 uart_write_wakeup(port);
402 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100403 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ctrl = sci_in(port, SCSCR);
406
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900407 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sci_in(port, SCxSR); /* Dummy read */
409 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 ctrl |= SCI_CTRL_FLAGS_TIE;
413 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415}
416
417/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900418#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howells7d12e782006-10-05 14:55:46 +0100420static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900422 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100423 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int i, count, copied = 0;
425 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800426 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 status = sci_in(port, SCxSR);
429 if (!(status & SCxSR_RDxF(port)))
430 return;
431
432 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900433 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900434 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900435 else
436 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800439 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* If for any reason we can't copy more data, we're done! */
442 if (count == 0)
443 break;
444
445 if (port->type == PORT_SCI) {
446 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900447 if (uart_handle_sysrq_char(port, c) ||
448 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900450 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900451 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900453 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 char c = sci_in(port, SCxRDR);
455 status = sci_in(port, SCxSR);
456#if defined(CONFIG_CPU_SH3)
457 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900458 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if ((c == 0) &&
460 (status & SCxSR_FER(port))) {
461 count--; i--;
462 continue;
463 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900466 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900467 sci_port->break_flag = 0;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (STEPFN(c)) {
470 count--; i--;
471 continue;
472 }
473 }
474#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100475 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 count--; i--;
477 continue;
478 }
479
480 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800482 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900483 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800485 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900486 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800487 } else
488 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900489
Alan Cox33f0f882006-01-09 20:54:13 -0800490 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
493
494 sci_in(port, SCxSR); /* dummy read */
495 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 copied += count;
498 port->icount.rx += count;
499 }
500
501 if (copied) {
502 /* Tell the rest of the system the news. New characters! */
503 tty_flip_buffer_push(tty);
504 } else {
505 sci_in(port, SCxSR); /* dummy read */
506 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
507 }
508}
509
510#define SCI_BREAK_JIFFIES (HZ/20)
511/* The sci generates interrupts during the break,
512 * 1 per millisecond or so during the break period, for 9600 baud.
513 * So dont bother disabling interrupts.
514 * But dont want more than 1 break event.
515 * Use a kernel timer to periodically poll the rx line until
516 * the break is finished.
517 */
518static void sci_schedule_break_timer(struct sci_port *port)
519{
520 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
521 add_timer(&port->break_timer);
522}
523/* Ensure that two consecutive samples find the break over. */
524static void sci_break_timer(unsigned long data)
525{
Paul Mundte108b2c2006-09-27 16:32:13 +0900526 struct sci_port *port = (struct sci_port *)data;
527
528 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900530 sci_schedule_break_timer(port);
531 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* break is over. */
533 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900534 sci_schedule_break_timer(port);
535 } else
536 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static inline int sci_handle_errors(struct uart_port *port)
540{
541 int copied = 0;
542 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100543 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Paul Mundte108b2c2006-09-27 16:32:13 +0900545 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900547 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800548 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900549
550 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
Paul Mundte108b2c2006-09-27 16:32:13 +0900553 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (sci_rxd_in(port) == 0) {
555 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900556 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900557
558 if (!sci_port->break_flag) {
559 sci_port->break_flag = 1;
560 sci_schedule_break_timer(sci_port);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900563 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900565
566 dev_dbg(port->dev, "BREAK detected\n");
567
Paul Mundte108b2c2006-09-27 16:32:13 +0900568 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900569 copied++;
570 }
571
Paul Mundte108b2c2006-09-27 16:32:13 +0900572 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800575 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900576
577 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 }
580
Paul Mundte108b2c2006-09-27 16:32:13 +0900581 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900583 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
584 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900585
586 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Alan Cox33f0f882006-01-09 20:54:13 -0800589 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return copied;
593}
594
Paul Mundtd830fa42008-12-16 19:29:38 +0900595static inline int sci_handle_fifo_overrun(struct uart_port *port)
596{
597 struct tty_struct *tty = port->info->port.tty;
598 int copied = 0;
599
600 if (port->type != PORT_SCIF)
601 return 0;
602
603 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
604 sci_out(port, SCLSR, 0);
605
606 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
607 tty_flip_buffer_push(tty);
608
609 dev_notice(port->dev, "overrun error\n");
610 copied++;
611 }
612
613 return copied;
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static inline int sci_handle_breaks(struct uart_port *port)
617{
618 int copied = 0;
619 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100620 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000621 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900623 if (uart_handle_break(port))
624 return 0;
625
Paul Mundtb7a76e42006-02-01 03:06:06 -0800626 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#if defined(CONFIG_CPU_SH3)
628 /* Debounce break */
629 s->break_flag = 1;
630#endif
631 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900632 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800633 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900634
635 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Alan Cox33f0f882006-01-09 20:54:13 -0800638 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900640
Paul Mundtd830fa42008-12-16 19:29:38 +0900641 copied += sci_handle_fifo_overrun(port);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return copied;
644}
645
David Howells7d12e782006-10-05 14:55:46 +0100646static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* I think sci_receive_chars has to be called irrespective
649 * of whether the I_IXOFF is set, otherwise, how is the interrupt
650 * to be disabled?
651 */
David Howells7d12e782006-10-05 14:55:46 +0100652 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 return IRQ_HANDLED;
655}
656
David Howells7d12e782006-10-05 14:55:46 +0100657static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct uart_port *port = ptr;
660
Paul Mundte108b2c2006-09-27 16:32:13 +0900661 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900663 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return IRQ_HANDLED;
666}
667
David Howells7d12e782006-10-05 14:55:46 +0100668static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct uart_port *port = ptr;
671
672 /* Handle errors */
673 if (port->type == PORT_SCI) {
674 if (sci_handle_errors(port)) {
675 /* discard character in rx buffer */
676 sci_in(port, SCxSR);
677 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
678 }
679 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900680 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100681 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
685
686 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100687 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return IRQ_HANDLED;
690}
691
David Howells7d12e782006-10-05 14:55:46 +0100692static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct uart_port *port = ptr;
695
696 /* Handle BREAKs */
697 sci_handle_breaks(port);
698 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
699
700 return IRQ_HANDLED;
701}
702
David Howells7d12e782006-10-05 14:55:46 +0100703static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900705 unsigned short ssr_status, scr_status;
706 struct uart_port *port = ptr;
707 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900709 ssr_status = sci_in(port, SCxSR);
710 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900713 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
714 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900716 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
717 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900719 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
720 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900722 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
723 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Michael Trimarchia8884e32008-10-31 16:10:23 +0900725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Paul Mundt027e6872008-12-16 18:36:16 +0900728#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Here we define a transistion notifier so that we can update all of our
731 * ports' baud rate when the peripheral clock changes.
732 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900733static int sci_notifier(struct notifier_block *self,
734 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Magnus Damme552de22009-01-21 15:13:42 +0000736 struct sh_sci_priv *priv = container_of(self,
737 struct sh_sci_priv, clk_nb);
738 struct sci_port *sci_port;
739 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000742 (phase == CPUFREQ_RESUMECHANGE)) {
743 spin_lock_irqsave(&priv->lock, flags);
744 list_for_each_entry(sci_port, &priv->ports, node)
745 sci_port->port.uartclk = clk_get_rate(sci_port->clk);
746
747 spin_unlock_irqrestore(&priv->lock, flags);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return NOTIFY_OK;
751}
Paul Mundt027e6872008-12-16 18:36:16 +0900752#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754static int sci_request_irq(struct sci_port *port)
755{
756 int i;
David Howells7d12e782006-10-05 14:55:46 +0100757 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
759 sci_br_interrupt,
760 };
761 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
762 "SCI Transmit Data Empty", "SCI Break" };
763
764 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900765 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900767
768 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900769 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900770 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENODEV;
772 }
773 } else {
774 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900775 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900777
Paul Mundte108b2c2006-09-27 16:32:13 +0900778 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900779 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900780 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -ENODEV;
782 }
783 }
784 }
785
786 return 0;
787}
788
789static void sci_free_irq(struct sci_port *port)
790{
791 int i;
792
Paul Mundt762c69e2008-12-16 18:55:26 +0900793 if (port->irqs[0] == port->irqs[1])
794 free_irq(port->irqs[0], port);
795 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
797 if (!port->irqs[i])
798 continue;
799
800 free_irq(port->irqs[i], port);
801 }
802 }
803}
804
805static unsigned int sci_tx_empty(struct uart_port *port)
806{
807 /* Can't detect */
808 return TIOCSER_TEMT;
809}
810
811static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
812{
813 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
814 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
815 /* If you have signals for DTR and DCD, please implement here. */
816}
817
818static unsigned int sci_get_mctrl(struct uart_port *port)
819{
820 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
821 and CTS/RTS */
822
823 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
824}
825
Russell Kingb129a8c2005-08-31 10:12:14 +0100826static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Paul Mundte108b2c2006-09-27 16:32:13 +0900828 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Paul Mundte108b2c2006-09-27 16:32:13 +0900830 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
831 ctrl = sci_in(port, SCSCR);
832 ctrl |= SCI_CTRL_FLAGS_TIE;
833 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Russell Kingb129a8c2005-08-31 10:12:14 +0100836static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 unsigned short ctrl;
839
840 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ctrl = sci_in(port, SCSCR);
842 ctrl &= ~SCI_CTRL_FLAGS_TIE;
843 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned short ctrl;
849
850 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ctrl = sci_in(port, SCSCR);
852 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
853 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static void sci_stop_rx(struct uart_port *port)
857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 unsigned short ctrl;
859
860 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ctrl = sci_in(port, SCSCR);
862 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
863 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866static void sci_enable_ms(struct uart_port *port)
867{
868 /* Nothing here yet .. */
869}
870
871static void sci_break_ctl(struct uart_port *port, int break_state)
872{
873 /* Nothing here yet .. */
874}
875
876static int sci_startup(struct uart_port *port)
877{
Magnus Damma5660ad2009-01-21 15:14:38 +0000878 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Paul Mundte108b2c2006-09-27 16:32:13 +0900880 if (s->enable)
881 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Paul Mundta2159b52008-10-02 19:09:13 +0900883#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900884 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +0900885#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700888 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sci_start_rx(port, 1);
890
891 return 0;
892}
893
894static void sci_shutdown(struct uart_port *port)
895{
Magnus Damma5660ad2009-01-21 15:14:38 +0000896 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100899 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sci_free_irq(s);
901
Paul Mundte108b2c2006-09-27 16:32:13 +0900902 if (s->disable)
903 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900904
Paul Mundta2159b52008-10-02 19:09:13 +0900905#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900906 clk_put(s->clk);
907 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +0900908#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Alan Cox606d0992006-12-08 02:38:45 -0800911static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
912 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900915 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900918 if (likely(baud))
919 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Paul Mundte108b2c2006-09-27 16:32:13 +0900921 do {
922 status = sci_in(port, SCxSR);
923 } while (!(status & SCxSR_TEND(port)));
924
925 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
926
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900927 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900928 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900929
930 smr_val = sci_in(port, SCSMR) & 3;
931 if ((termios->c_cflag & CSIZE) == CS7)
932 smr_val |= 0x40;
933 if (termios->c_cflag & PARENB)
934 smr_val |= 0x20;
935 if (termios->c_cflag & PARODD)
936 smr_val |= 0x30;
937 if (termios->c_cflag & CSTOPB)
938 smr_val |= 0x08;
939
940 uart_update_timeout(port, termios->c_cflag, baud);
941
942 sci_out(port, SCSMR, smr_val);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900945 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
947 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900948 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sci_out(port, SCBRR, t);
952 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
953 }
954
Paul Mundtd5701642008-12-16 20:07:27 +0900955 sci_init_pins(port, termios->c_cflag);
956 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 sci_out(port, SCSCR, SCSCR_INIT(port));
959
960 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900961 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964static const char *sci_type(struct uart_port *port)
965{
966 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900967 case PORT_IRDA:
968 return "irda";
969 case PORT_SCI:
970 return "sci";
971 case PORT_SCIF:
972 return "scif";
973 case PORT_SCIFA:
974 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Paul Mundtfa439722008-09-04 18:53:58 +0900977 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static void sci_release_port(struct uart_port *port)
981{
982 /* Nothing here yet .. */
983}
984
985static int sci_request_port(struct uart_port *port)
986{
987 /* Nothing here yet .. */
988 return 0;
989}
990
991static void sci_config_port(struct uart_port *port, int flags)
992{
Magnus Damma5660ad2009-01-21 15:14:38 +0000993 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 port->type = s->type;
996
Magnus Damm08f8cb32009-01-21 15:14:22 +0000997 if (port->membase)
998 return;
999
1000 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001001 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001002
1003 if (IS_ERR(port->membase))
1004 dev_err(port->dev, "can't remap port#%d\n", port->line);
1005 } else {
1006 /*
1007 * For the simple (and majority of) cases where we don't
1008 * need to do any remapping, just cast the cookie
1009 * directly.
1010 */
1011 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1016{
Magnus Damma5660ad2009-01-21 15:14:38 +00001017 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Yinghai Lua62c4132008-08-19 20:49:55 -07001019 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EINVAL;
1021 if (ser->baud_base < 2400)
1022 /* No paper tape reader for Mitch.. */
1023 return -EINVAL;
1024
1025 return 0;
1026}
1027
1028static struct uart_ops sci_uart_ops = {
1029 .tx_empty = sci_tx_empty,
1030 .set_mctrl = sci_set_mctrl,
1031 .get_mctrl = sci_get_mctrl,
1032 .start_tx = sci_start_tx,
1033 .stop_tx = sci_stop_tx,
1034 .stop_rx = sci_stop_rx,
1035 .enable_ms = sci_enable_ms,
1036 .break_ctl = sci_break_ctl,
1037 .startup = sci_startup,
1038 .shutdown = sci_shutdown,
1039 .set_termios = sci_set_termios,
1040 .type = sci_type,
1041 .release_port = sci_release_port,
1042 .request_port = sci_request_port,
1043 .config_port = sci_config_port,
1044 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001045#ifdef CONFIG_CONSOLE_POLL
1046 .poll_get_char = sci_poll_get_char,
1047 .poll_put_char = sci_poll_put_char,
1048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049};
1050
Magnus Damm08f8cb32009-01-21 15:14:22 +00001051static void __devinit sci_init_single(struct sci_port *sci_port,
1052 unsigned int index,
1053 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001054{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001055 sci_port->port.ops = &sci_uart_ops;
1056 sci_port->port.iotype = UPIO_MEM;
1057 sci_port->port.line = index;
1058 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001059
1060#if defined(__H8300H__) || defined(__H8300S__)
1061#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001062 sci_port->enable = h8300_sci_enable;
1063 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001065 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001066#elif defined(CONFIG_HAVE_CLK)
Magnus Damm7ed7e072009-01-21 15:14:14 +00001067 /*
1068 * XXX: We should use a proper SCI/SCIF clock
1069 */
1070 {
1071 struct clk *clk = clk_get(NULL, "module_clk");
1072 sci_port->port.uartclk = clk_get_rate(clk);
1073 clk_put(clk);
1074 }
Paul Mundta2159b52008-10-02 19:09:13 +09001075#else
1076#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001077#endif
1078
Magnus Damm7ed7e072009-01-21 15:14:14 +00001079 sci_port->break_timer.data = (unsigned long)sci_port;
1080 sci_port->break_timer.function = sci_break_timer;
1081 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001082
Magnus Damm7ed7e072009-01-21 15:14:14 +00001083 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001084 sci_port->port.membase = p->membase;
1085
1086 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1087 sci_port->port.flags = p->flags;
1088 sci_port->type = sci_port->port.type = p->type;
1089
1090 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundte108b2c2006-09-27 16:32:13 +09001091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001094static struct tty_driver *serial_console_device(struct console *co, int *index)
1095{
1096 struct uart_driver *p = &sci_uart_driver;
1097 *index = co->index;
1098 return p->tty_driver;
1099}
1100
1101static void serial_console_putchar(struct uart_port *port, int ch)
1102{
1103 sci_poll_put_char(port, ch);
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
1107 * Print a string to the serial port trying not to disturb
1108 * any possible real use of the port...
1109 */
1110static void serial_console_write(struct console *co, const char *s,
1111 unsigned count)
1112{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001113 struct uart_port *port = co->data;
Magnus Damm973e5d52009-02-24 15:57:12 +09001114 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001115
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001116 uart_console_write(co->data, s, count, serial_console_putchar);
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{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001126 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct uart_port *port;
1128 int baud = 115200;
1129 int bits = 8;
1130 int parity = 'n';
1131 int flow = 'n';
1132 int ret;
1133
Paul Mundte108b2c2006-09-27 16:32:13 +09001134 /*
1135 * Check whether an invalid uart number has been specified, and
1136 * if so, search for the first available port that does have
1137 * console support.
1138 */
1139 if (co->index >= SCI_NPORTS)
1140 co->index = 0;
1141
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001142 sci_port = &sci_ports[co->index];
1143 port = &sci_port->port;
1144 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001147 * Also need to check port->type, we don't actually have any
1148 * UPIO_PORT ports, but uart_report_port() handily misreports
1149 * it anyways if we don't have a port available by the time this is
1150 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001152 if (!port->type)
1153 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001154
Paul Mundta2159b52008-10-02 19:09:13 +09001155#ifdef CONFIG_HAVE_CLK
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001156 if (!sci_port->clk)
1157 sci_port->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +09001158#endif
1159
Magnus Damm08f8cb32009-01-21 15:14:22 +00001160 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001161
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001162 if (sci_port->enable)
1163 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (options)
1166 uart_parse_options(options, &baud, &parity, &bits, &flow);
1167
1168 ret = uart_set_options(port, co, baud, parity, bits, flow);
1169#if defined(__H8300H__) || defined(__H8300S__)
1170 /* disable rx interrupt */
1171 if (ret == 0)
1172 sci_stop_rx(port);
1173#endif
1174 return ret;
1175}
1176
1177static struct console serial_console = {
1178 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001179 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 .write = serial_console_write,
1181 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001182 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184};
1185
1186static int __init sci_console_init(void)
1187{
1188 register_console(&serial_console);
1189 return 0;
1190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191console_initcall(sci_console_init);
1192#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1193
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001194#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001195#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001197#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198#endif
1199
1200static char banner[] __initdata =
1201 KERN_INFO "SuperH SCI(F) driver initialized\n";
1202
1203static struct uart_driver sci_uart_driver = {
1204 .owner = THIS_MODULE,
1205 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .dev_name = "ttySC",
1207 .major = SCI_MAJOR,
1208 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001209 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 .cons = SCI_CONSOLE,
1211};
1212
Magnus Damme552de22009-01-21 15:13:42 +00001213
1214static int __devexit sci_remove(struct platform_device *dev)
1215{
1216 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1217 struct sci_port *p;
1218 unsigned long flags;
1219
1220#ifdef CONFIG_HAVE_CLK
1221 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1222#endif
1223
1224 spin_lock_irqsave(&priv->lock, flags);
1225 list_for_each_entry(p, &priv->ports, node)
1226 uart_remove_one_port(&sci_uart_driver, &p->port);
1227
1228 spin_unlock_irqrestore(&priv->lock, flags);
1229
1230 kfree(priv);
1231 return 0;
1232}
1233
Magnus Damm0ee70712009-01-21 15:13:50 +00001234static int __devinit sci_probe_single(struct platform_device *dev,
1235 unsigned int index,
1236 struct plat_sci_port *p,
1237 struct sci_port *sciport)
1238{
1239 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1240 unsigned long flags;
1241 int ret;
1242
1243 /* Sanity check */
1244 if (unlikely(index >= SCI_NPORTS)) {
1245 dev_notice(&dev->dev, "Attempting to register port "
1246 "%d when only %d are available.\n",
1247 index+1, SCI_NPORTS);
1248 dev_notice(&dev->dev, "Consider bumping "
1249 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1250 return 0;
1251 }
1252
Magnus Damm7ed7e072009-01-21 15:14:14 +00001253 sciport->port.dev = &dev->dev;
Magnus Damm08f8cb32009-01-21 15:14:22 +00001254 sci_init_single(sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001255
1256 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001257 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001258 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001259
1260 INIT_LIST_HEAD(&sciport->node);
1261
1262 spin_lock_irqsave(&priv->lock, flags);
1263 list_add(&sciport->node, &priv->ports);
1264 spin_unlock_irqrestore(&priv->lock, flags);
1265
1266 return 0;
1267}
1268
Paul Mundte108b2c2006-09-27 16:32:13 +09001269/*
1270 * Register a set of serial devices attached to a platform device. The
1271 * list is terminated with a zero flags entry, which means we expect
1272 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1273 * remapping (such as sh64) should also set UPF_IOREMAP.
1274 */
1275static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Paul Mundte108b2c2006-09-27 16:32:13 +09001277 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001278 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001279 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001280
1281 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1282 if (!priv)
1283 return -ENOMEM;
1284
1285 INIT_LIST_HEAD(&priv->ports);
1286 spin_lock_init(&priv->lock);
1287 platform_set_drvdata(dev, priv);
1288
1289#ifdef CONFIG_HAVE_CLK
1290 priv->clk_nb.notifier_call = sci_notifier;
1291 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Magnus Damm0ee70712009-01-21 15:13:50 +00001294 if (dev->id != -1) {
1295 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1296 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001297 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001298 } else {
1299 for (i = 0; p && p->flags != 0; p++, i++) {
1300 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1301 if (ret)
1302 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001303 }
Magnus Damme552de22009-01-21 15:13:42 +00001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306#ifdef CONFIG_SH_STANDARD_BIOS
1307 sh_bios_gdb_detach();
1308#endif
1309
Paul Mundte108b2c2006-09-27 16:32:13 +09001310 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001311
1312err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001313 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001314 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001315}
1316
Paul Mundte108b2c2006-09-27 16:32:13 +09001317static int sci_suspend(struct platform_device *dev, pm_message_t state)
1318{
Magnus Damme552de22009-01-21 15:13:42 +00001319 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1320 struct sci_port *p;
1321 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001322
Magnus Damme552de22009-01-21 15:13:42 +00001323 spin_lock_irqsave(&priv->lock, flags);
1324 list_for_each_entry(p, &priv->ports, node)
1325 uart_suspend_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001326
Magnus Damme552de22009-01-21 15:13:42 +00001327 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001328
1329 return 0;
1330}
1331
1332static int sci_resume(struct platform_device *dev)
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_resume_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 struct platform_driver sci_driver = {
1348 .probe = sci_probe,
1349 .remove = __devexit_p(sci_remove),
1350 .suspend = sci_suspend,
1351 .resume = sci_resume,
1352 .driver = {
1353 .name = "sh-sci",
1354 .owner = THIS_MODULE,
1355 },
1356};
1357
1358static int __init sci_init(void)
1359{
1360 int ret;
1361
1362 printk(banner);
1363
Paul Mundte108b2c2006-09-27 16:32:13 +09001364 ret = uart_register_driver(&sci_uart_driver);
1365 if (likely(ret == 0)) {
1366 ret = platform_driver_register(&sci_driver);
1367 if (unlikely(ret))
1368 uart_unregister_driver(&sci_uart_driver);
1369 }
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return ret;
1372}
1373
1374static void __exit sci_exit(void)
1375{
Paul Mundte108b2c2006-09-27 16:32:13 +09001376 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 uart_unregister_driver(&sci_uart_driver);
1378}
1379
1380module_init(sci_init);
1381module_exit(sci_exit);
1382
Paul Mundte108b2c2006-09-27 16:32:13 +09001383MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001384MODULE_ALIAS("platform:sh-sci");