blob: a4cf1079b3129e4892fb711b5f95276055552458 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090051
52#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080053#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Yoshinori Sato168f3622009-04-28 04:40:15 +000057#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "sh-sci.h"
62
Paul Mundte108b2c2006-09-27 16:32:13 +090063struct sci_port {
64 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Paul Mundte108b2c2006-09-27 16:32:13 +090066 /* Port type */
67 unsigned int type;
68
69 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090070 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090071
Paul Mundte108b2c2006-09-27 16:32:13 +090072 /* Port enable callback */
73 void (*enable)(struct uart_port *port);
74
75 /* Port disable callback */
76 void (*disable)(struct uart_port *port);
77
78 /* Break timer */
79 struct timer_list break_timer;
80 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090081
Paul Mundta2159b52008-10-02 19:09:13 +090082#ifdef CONFIG_HAVE_CLK
Magnus Damm501b8252009-01-21 15:14:30 +000083 /* Interface clock */
84 struct clk *iclk;
85 /* Data clock */
86 struct clk *dclk;
Paul Mundt005a3362007-04-26 11:45:32 +090087#endif
Magnus Damme552de22009-01-21 15:13:42 +000088 struct list_head node;
89};
90
91struct sh_sci_priv {
92 spinlock_t lock;
93 struct list_head ports;
94
95#ifdef CONFIG_HAVE_CLK
96 struct notifier_block clk_nb;
97#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090098};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100101static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Paul Mundte108b2c2006-09-27 16:32:13 +0900103#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
104
105static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct uart_driver sci_uart_driver;
107
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900108static inline struct sci_port *
109to_sci_port(struct uart_port *uart)
110{
111 return container_of(uart, struct sci_port, port);
112}
113
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900114#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900115
116#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900117static inline void handle_error(struct uart_port *port)
118{
119 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
121}
122
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900123static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned short status;
126 int c;
127
Paul Mundte108b2c2006-09-27 16:32:13 +0900128 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 status = sci_in(port, SCxSR);
130 if (status & SCxSR_ERRORS(port)) {
131 handle_error(port);
132 continue;
133 }
134 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900137
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900138 /* Dummy read */
139 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return c;
143}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900146static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 unsigned short status;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 do {
151 status = sci_in(port, SCxSR);
152 } while (!(status & SCxSR_TDxE(port)));
153
Paul Mundt272966c2008-11-13 17:46:06 +0900154 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000155 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900157#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159#if defined(__H8300S__)
160enum { sci_disable, sci_enable };
161
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900162static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900164 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int ch = (port->mapbase - SMR0) >> 3;
166 unsigned char mask = 1 << (ch+1);
167
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900168 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900170 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
Paul Mundte108b2c2006-09-27 16:32:13 +0900173
Magnus Damm501b8252009-01-21 15:14:30 +0000174static void h8300_sci_enable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900175{
176 h8300_sci_config(port, sci_enable);
177}
178
Magnus Damm501b8252009-01-21 15:14:30 +0000179static void h8300_sci_disable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900180{
181 h8300_sci_config(port, sci_disable);
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
184
Paul Mundt15c73aa2008-10-02 19:47:12 +0900185#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900186static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int ch = (port->mapbase - SMR0) >> 3;
189
190 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900191 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
192 h8300_sci_pins[ch].rx,
193 H8300_GPIO_INPUT);
194 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
195 h8300_sci_pins[ch].tx,
196 H8300_GPIO_OUTPUT);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* tx mark output*/
199 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
200}
Paul Mundtd5701642008-12-16 20:07:27 +0900201#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
202static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900203{
Paul Mundtd5701642008-12-16 20:07:27 +0900204 if (port->mapbase == 0xA4400000) {
205 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
206 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
207 } else if (port->mapbase == 0xA4410000)
208 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900209}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900210#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900211static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900212{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900213 unsigned short data;
214
215 if (cflag & CRTSCTS) {
216 /* enable RTS/CTS */
217 if (port->mapbase == 0xa4430000) { /* SCIF0 */
218 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900219 data = __raw_readw(PORT_PTCR);
220 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900221 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
222 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900223 data = __raw_readw(PORT_PVCR);
224 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900225 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900226 } else {
227 if (port->mapbase == 0xa4430000) { /* SCIF0 */
228 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900229 data = __raw_readw(PORT_PTCR);
230 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900231 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
232 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900233 data = __raw_readw(PORT_PVCR);
234 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900235 }
236 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900237}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800238#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900239/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900240static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800242 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Paul Mundtb7a76e42006-02-01 03:06:06 -0800244 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900245 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800246 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900247 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800248
Paul Mundtd5701642008-12-16 20:07:27 +0900249 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900251 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Clear out SCP7MD1,0, SCP4MD1,0,
253 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900254 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 data = ctrl_inb(SCPDR);
257 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800258 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Paul Mundt41504c32006-12-11 20:28:03 +0900261#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900262static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900263{
Magnus Damm346b7462008-04-23 21:25:29 +0900264 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900265
Magnus Damm346b7462008-04-23 21:25:29 +0900266 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900267 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900268 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900269 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900270 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900271
Paul Mundtd5701642008-12-16 20:07:27 +0900272 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900273 }
Paul Mundt41504c32006-12-11 20:28:03 +0900274}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900275#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
276 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900277 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900278 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900279 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900280static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
281{
282 if (!(cflag & CRTSCTS))
283 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
284}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900285#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900286static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
287{
288 if (!(cflag & CRTSCTS))
289 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
290}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800291#else
Paul Mundtd5701642008-12-16 20:07:27 +0900292static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
293{
294 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Paul Mundte108b2c2006-09-27 16:32:13 +0900296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Paul Mundt32351a22007-03-12 14:38:59 +0900298#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
299 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900300 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
301 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900302static inline int scif_txroom(struct uart_port *port)
303{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900304 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900305}
306
307static inline int scif_rxroom(struct uart_port *port)
308{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900309 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900310}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900311#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
312static inline int scif_txroom(struct uart_port *port)
313{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900314 if ((port->mapbase == 0xffe00000) ||
315 (port->mapbase == 0xffe08000)) {
316 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900318 } else {
319 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900320 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900321 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900322}
323
324static inline int scif_rxroom(struct uart_port *port)
325{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900326 if ((port->mapbase == 0xffe00000) ||
327 (port->mapbase == 0xffe08000)) {
328 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900329 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900330 } else {
331 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900332 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900333 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900334}
Paul Mundte108b2c2006-09-27 16:32:13 +0900335#else
336static inline int scif_txroom(struct uart_port *port)
337{
338 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
339}
340
341static inline int scif_rxroom(struct uart_port *port)
342{
343 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Paul Mundte108b2c2006-09-27 16:32:13 +0900347static inline int sci_txroom(struct uart_port *port)
348{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900349 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900350}
351
352static inline int sci_rxroom(struct uart_port *port)
353{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900354 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* ********************************************************************** *
358 * the interrupt related routines *
359 * ********************************************************************** */
360
361static void sci_transmit_chars(struct uart_port *port)
362{
363 struct circ_buf *xmit = &port->info->xmit;
364 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned short status;
366 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900367 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 status = sci_in(port, SCxSR);
370 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900372 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900374 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378 }
379
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900380 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900381 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900382 else
383 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 do {
386 unsigned char c;
387
388 if (port->x_char) {
389 c = port->x_char;
390 port->x_char = 0;
391 } else if (!uart_circ_empty(xmit) && !stopped) {
392 c = xmit->buf[xmit->tail];
393 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
394 } else {
395 break;
396 }
397
398 sci_out(port, SCxTDR, c);
399
400 port->icount.tx++;
401 } while (--count > 0);
402
403 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
404
405 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
406 uart_write_wakeup(port);
407 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100408 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ctrl = sci_in(port, SCSCR);
411
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900412 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sci_in(port, SCxSR); /* Dummy read */
414 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 ctrl |= SCI_CTRL_FLAGS_TIE;
418 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420}
421
422/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900423#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Howells7d12e782006-10-05 14:55:46 +0100425static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900427 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100428 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int i, count, copied = 0;
430 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800431 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 status = sci_in(port, SCxSR);
434 if (!(status & SCxSR_RDxF(port)))
435 return;
436
437 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900438 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900439 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900440 else
441 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800444 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* If for any reason we can't copy more data, we're done! */
447 if (count == 0)
448 break;
449
450 if (port->type == PORT_SCI) {
451 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900452 if (uart_handle_sysrq_char(port, c) ||
453 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900455 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900456 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900458 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 char c = sci_in(port, SCxRDR);
460 status = sci_in(port, SCxSR);
461#if defined(CONFIG_CPU_SH3)
462 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900463 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if ((c == 0) &&
465 (status & SCxSR_FER(port))) {
466 count--; i--;
467 continue;
468 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900471 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900472 sci_port->break_flag = 0;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (STEPFN(c)) {
475 count--; i--;
476 continue;
477 }
478 }
479#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100480 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 count--; i--;
482 continue;
483 }
484
485 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800487 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900488 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800490 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900491 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800492 } else
493 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900494
Alan Cox33f0f882006-01-09 20:54:13 -0800495 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498
499 sci_in(port, SCxSR); /* dummy read */
500 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 copied += count;
503 port->icount.rx += count;
504 }
505
506 if (copied) {
507 /* Tell the rest of the system the news. New characters! */
508 tty_flip_buffer_push(tty);
509 } else {
510 sci_in(port, SCxSR); /* dummy read */
511 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
512 }
513}
514
515#define SCI_BREAK_JIFFIES (HZ/20)
516/* The sci generates interrupts during the break,
517 * 1 per millisecond or so during the break period, for 9600 baud.
518 * So dont bother disabling interrupts.
519 * But dont want more than 1 break event.
520 * Use a kernel timer to periodically poll the rx line until
521 * the break is finished.
522 */
523static void sci_schedule_break_timer(struct sci_port *port)
524{
525 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
526 add_timer(&port->break_timer);
527}
528/* Ensure that two consecutive samples find the break over. */
529static void sci_break_timer(unsigned long data)
530{
Paul Mundte108b2c2006-09-27 16:32:13 +0900531 struct sci_port *port = (struct sci_port *)data;
532
533 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900535 sci_schedule_break_timer(port);
536 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* break is over. */
538 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900539 sci_schedule_break_timer(port);
540 } else
541 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static inline int sci_handle_errors(struct uart_port *port)
545{
546 int copied = 0;
547 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100548 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Paul Mundte108b2c2006-09-27 16:32:13 +0900550 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900552 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800553 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900554
555 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Paul Mundte108b2c2006-09-27 16:32:13 +0900558 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (sci_rxd_in(port) == 0) {
560 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900561 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900562
563 if (!sci_port->break_flag) {
564 sci_port->break_flag = 1;
565 sci_schedule_break_timer(sci_port);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900568 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900570
571 dev_dbg(port->dev, "BREAK detected\n");
572
Paul Mundte108b2c2006-09-27 16:32:13 +0900573 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900574 copied++;
575 }
576
Paul Mundte108b2c2006-09-27 16:32:13 +0900577 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900579 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800580 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900581
582 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
585
Paul Mundte108b2c2006-09-27 16:32:13 +0900586 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900588 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
589 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900590
591 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Alan Cox33f0f882006-01-09 20:54:13 -0800594 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 return copied;
598}
599
Paul Mundtd830fa42008-12-16 19:29:38 +0900600static inline int sci_handle_fifo_overrun(struct uart_port *port)
601{
602 struct tty_struct *tty = port->info->port.tty;
603 int copied = 0;
604
605 if (port->type != PORT_SCIF)
606 return 0;
607
608 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
609 sci_out(port, SCLSR, 0);
610
611 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
612 tty_flip_buffer_push(tty);
613
614 dev_notice(port->dev, "overrun error\n");
615 copied++;
616 }
617
618 return copied;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static inline int sci_handle_breaks(struct uart_port *port)
622{
623 int copied = 0;
624 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100625 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000626 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900628 if (uart_handle_break(port))
629 return 0;
630
Paul Mundtb7a76e42006-02-01 03:06:06 -0800631 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#if defined(CONFIG_CPU_SH3)
633 /* Debounce break */
634 s->break_flag = 1;
635#endif
636 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900637 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800638 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900639
640 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Alan Cox33f0f882006-01-09 20:54:13 -0800643 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900645
Paul Mundtd830fa42008-12-16 19:29:38 +0900646 copied += sci_handle_fifo_overrun(port);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return copied;
649}
650
David Howells7d12e782006-10-05 14:55:46 +0100651static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* I think sci_receive_chars has to be called irrespective
654 * of whether the I_IXOFF is set, otherwise, how is the interrupt
655 * to be disabled?
656 */
David Howells7d12e782006-10-05 14:55:46 +0100657 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 return IRQ_HANDLED;
660}
661
David Howells7d12e782006-10-05 14:55:46 +0100662static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct uart_port *port = ptr;
665
Paul Mundte108b2c2006-09-27 16:32:13 +0900666 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900668 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return IRQ_HANDLED;
671}
672
David Howells7d12e782006-10-05 14:55:46 +0100673static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct uart_port *port = ptr;
676
677 /* Handle errors */
678 if (port->type == PORT_SCI) {
679 if (sci_handle_errors(port)) {
680 /* discard character in rx buffer */
681 sci_in(port, SCxSR);
682 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
683 }
684 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900685 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100686 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
690
691 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100692 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return IRQ_HANDLED;
695}
696
David Howells7d12e782006-10-05 14:55:46 +0100697static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct uart_port *port = ptr;
700
701 /* Handle BREAKs */
702 sci_handle_breaks(port);
703 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
704
705 return IRQ_HANDLED;
706}
707
David Howells7d12e782006-10-05 14:55:46 +0100708static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900710 unsigned short ssr_status, scr_status;
711 struct uart_port *port = ptr;
712 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900714 ssr_status = sci_in(port, SCxSR);
715 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900718 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
719 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900721 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
722 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900724 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
725 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900727 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
728 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Michael Trimarchia8884e32008-10-31 16:10:23 +0900730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Paul Mundt027e6872008-12-16 18:36:16 +0900733#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Here we define a transistion notifier so that we can update all of our
736 * ports' baud rate when the peripheral clock changes.
737 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900738static int sci_notifier(struct notifier_block *self,
739 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Magnus Damme552de22009-01-21 15:13:42 +0000741 struct sh_sci_priv *priv = container_of(self,
742 struct sh_sci_priv, clk_nb);
743 struct sci_port *sci_port;
744 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000747 (phase == CPUFREQ_RESUMECHANGE)) {
748 spin_lock_irqsave(&priv->lock, flags);
749 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000750 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000751
752 spin_unlock_irqrestore(&priv->lock, flags);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return NOTIFY_OK;
756}
Magnus Damm501b8252009-01-21 15:14:30 +0000757
758static void sci_clk_enable(struct uart_port *port)
759{
760 struct sci_port *sci_port = to_sci_port(port);
761
762 clk_enable(sci_port->dclk);
763 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
764
765 if (sci_port->iclk)
766 clk_enable(sci_port->iclk);
767}
768
769static void sci_clk_disable(struct uart_port *port)
770{
771 struct sci_port *sci_port = to_sci_port(port);
772
773 if (sci_port->iclk)
774 clk_disable(sci_port->iclk);
775
776 clk_disable(sci_port->dclk);
777}
Paul Mundt027e6872008-12-16 18:36:16 +0900778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780static int sci_request_irq(struct sci_port *port)
781{
782 int i;
David Howells7d12e782006-10-05 14:55:46 +0100783 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
785 sci_br_interrupt,
786 };
787 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
788 "SCI Transmit Data Empty", "SCI Break" };
789
790 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900791 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900793
794 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900795 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900796 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -ENODEV;
798 }
799 } else {
800 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900801 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900803
Paul Mundte108b2c2006-09-27 16:32:13 +0900804 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900805 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900806 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -ENODEV;
808 }
809 }
810 }
811
812 return 0;
813}
814
815static void sci_free_irq(struct sci_port *port)
816{
817 int i;
818
Paul Mundt762c69e2008-12-16 18:55:26 +0900819 if (port->irqs[0] == port->irqs[1])
820 free_irq(port->irqs[0], port);
821 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
823 if (!port->irqs[i])
824 continue;
825
826 free_irq(port->irqs[i], port);
827 }
828 }
829}
830
831static unsigned int sci_tx_empty(struct uart_port *port)
832{
833 /* Can't detect */
834 return TIOCSER_TEMT;
835}
836
837static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
838{
839 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
840 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
841 /* If you have signals for DTR and DCD, please implement here. */
842}
843
844static unsigned int sci_get_mctrl(struct uart_port *port)
845{
846 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
847 and CTS/RTS */
848
849 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
850}
851
Russell Kingb129a8c2005-08-31 10:12:14 +0100852static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Paul Mundte108b2c2006-09-27 16:32:13 +0900854 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Paul Mundte108b2c2006-09-27 16:32:13 +0900856 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
857 ctrl = sci_in(port, SCSCR);
858 ctrl |= SCI_CTRL_FLAGS_TIE;
859 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Russell Kingb129a8c2005-08-31 10:12:14 +0100862static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned short ctrl;
865
866 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ctrl = sci_in(port, SCSCR);
868 ctrl &= ~SCI_CTRL_FLAGS_TIE;
869 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned short ctrl;
875
876 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ctrl = sci_in(port, SCSCR);
878 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
879 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static void sci_stop_rx(struct uart_port *port)
883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned short ctrl;
885
886 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ctrl = sci_in(port, SCSCR);
888 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
889 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static void sci_enable_ms(struct uart_port *port)
893{
894 /* Nothing here yet .. */
895}
896
897static void sci_break_ctl(struct uart_port *port, int break_state)
898{
899 /* Nothing here yet .. */
900}
901
902static int sci_startup(struct uart_port *port)
903{
Magnus Damma5660ad2009-01-21 15:14:38 +0000904 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Paul Mundte108b2c2006-09-27 16:32:13 +0900906 if (s->enable)
907 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700910 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 sci_start_rx(port, 1);
912
913 return 0;
914}
915
916static void sci_shutdown(struct uart_port *port)
917{
Magnus Damma5660ad2009-01-21 15:14:38 +0000918 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100921 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 sci_free_irq(s);
923
Paul Mundte108b2c2006-09-27 16:32:13 +0900924 if (s->disable)
925 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Alan Cox606d0992006-12-08 02:38:45 -0800928static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
929 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900932 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900935 if (likely(baud))
936 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Paul Mundte108b2c2006-09-27 16:32:13 +0900938 do {
939 status = sci_in(port, SCxSR);
940 } while (!(status & SCxSR_TEND(port)));
941
942 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
943
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900944 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900945 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900946
947 smr_val = sci_in(port, SCSMR) & 3;
948 if ((termios->c_cflag & CSIZE) == CS7)
949 smr_val |= 0x40;
950 if (termios->c_cflag & PARENB)
951 smr_val |= 0x20;
952 if (termios->c_cflag & PARODD)
953 smr_val |= 0x30;
954 if (termios->c_cflag & CSTOPB)
955 smr_val |= 0x08;
956
957 uart_update_timeout(port, termios->c_cflag, baud);
958
959 sci_out(port, SCSMR, smr_val);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900962 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
964 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900965 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sci_out(port, SCBRR, t);
969 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
970 }
971
Paul Mundtd5701642008-12-16 20:07:27 +0900972 sci_init_pins(port, termios->c_cflag);
973 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 sci_out(port, SCSCR, SCSCR_INIT(port));
976
977 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900978 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static const char *sci_type(struct uart_port *port)
982{
983 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900984 case PORT_IRDA:
985 return "irda";
986 case PORT_SCI:
987 return "sci";
988 case PORT_SCIF:
989 return "scif";
990 case PORT_SCIFA:
991 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
Paul Mundtfa439722008-09-04 18:53:58 +0900994 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void sci_release_port(struct uart_port *port)
998{
999 /* Nothing here yet .. */
1000}
1001
1002static int sci_request_port(struct uart_port *port)
1003{
1004 /* Nothing here yet .. */
1005 return 0;
1006}
1007
1008static void sci_config_port(struct uart_port *port, int flags)
1009{
Magnus Damma5660ad2009-01-21 15:14:38 +00001010 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 port->type = s->type;
1013
Magnus Damm08f8cb32009-01-21 15:14:22 +00001014 if (port->membase)
1015 return;
1016
1017 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001018 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001019
1020 if (IS_ERR(port->membase))
1021 dev_err(port->dev, "can't remap port#%d\n", port->line);
1022 } else {
1023 /*
1024 * For the simple (and majority of) cases where we don't
1025 * need to do any remapping, just cast the cookie
1026 * directly.
1027 */
1028 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1033{
Magnus Damma5660ad2009-01-21 15:14:38 +00001034 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Yinghai Lua62c4132008-08-19 20:49:55 -07001036 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -EINVAL;
1038 if (ser->baud_base < 2400)
1039 /* No paper tape reader for Mitch.. */
1040 return -EINVAL;
1041
1042 return 0;
1043}
1044
1045static struct uart_ops sci_uart_ops = {
1046 .tx_empty = sci_tx_empty,
1047 .set_mctrl = sci_set_mctrl,
1048 .get_mctrl = sci_get_mctrl,
1049 .start_tx = sci_start_tx,
1050 .stop_tx = sci_stop_tx,
1051 .stop_rx = sci_stop_rx,
1052 .enable_ms = sci_enable_ms,
1053 .break_ctl = sci_break_ctl,
1054 .startup = sci_startup,
1055 .shutdown = sci_shutdown,
1056 .set_termios = sci_set_termios,
1057 .type = sci_type,
1058 .release_port = sci_release_port,
1059 .request_port = sci_request_port,
1060 .config_port = sci_config_port,
1061 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001062#ifdef CONFIG_CONSOLE_POLL
1063 .poll_get_char = sci_poll_get_char,
1064 .poll_put_char = sci_poll_put_char,
1065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066};
1067
Magnus Damm501b8252009-01-21 15:14:30 +00001068static void __devinit sci_init_single(struct platform_device *dev,
1069 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001070 unsigned int index,
1071 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001072{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001073 sci_port->port.ops = &sci_uart_ops;
1074 sci_port->port.iotype = UPIO_MEM;
1075 sci_port->port.line = index;
1076 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001077
1078#if defined(__H8300H__) || defined(__H8300S__)
1079#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001080 sci_port->enable = h8300_sci_enable;
1081 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001083 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001084#elif defined(CONFIG_HAVE_CLK)
Magnus Damm501b8252009-01-21 15:14:30 +00001085 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
Paul Mundtaf777ce2009-05-13 16:59:40 +09001086 sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
Magnus Damm501b8252009-01-21 15:14:30 +00001087 sci_port->enable = sci_clk_enable;
1088 sci_port->disable = sci_clk_disable;
Paul Mundta2159b52008-10-02 19:09:13 +09001089#else
1090#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001091#endif
1092
Magnus Damm7ed7e072009-01-21 15:14:14 +00001093 sci_port->break_timer.data = (unsigned long)sci_port;
1094 sci_port->break_timer.function = sci_break_timer;
1095 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001096
Magnus Damm7ed7e072009-01-21 15:14:14 +00001097 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001098 sci_port->port.membase = p->membase;
1099
1100 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1101 sci_port->port.flags = p->flags;
Magnus Damm501b8252009-01-21 15:14:30 +00001102 sci_port->port.dev = &dev->dev;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001103 sci_port->type = sci_port->port.type = p->type;
1104
1105 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Magnus Damm501b8252009-01-21 15:14:30 +00001106
Paul Mundte108b2c2006-09-27 16:32:13 +09001107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001110static struct tty_driver *serial_console_device(struct console *co, int *index)
1111{
1112 struct uart_driver *p = &sci_uart_driver;
1113 *index = co->index;
1114 return p->tty_driver;
1115}
1116
1117static void serial_console_putchar(struct uart_port *port, int ch)
1118{
1119 sci_poll_put_char(port, ch);
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * Print a string to the serial port trying not to disturb
1124 * any possible real use of the port...
1125 */
1126static void serial_console_write(struct console *co, const char *s,
1127 unsigned count)
1128{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001129 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001130 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001131 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001132
Magnus Damm501b8252009-01-21 15:14:30 +00001133 if (sci_port->enable)
1134 sci_port->enable(port);
1135
1136 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001137
1138 /* wait until fifo is empty and last bit has been transmitted */
1139 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1140 while ((sci_in(port, SCxSR) & bits) != bits)
1141 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001142
1143 if (sci_port->disable);
1144 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147static int __init serial_console_setup(struct console *co, char *options)
1148{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001149 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct uart_port *port;
1151 int baud = 115200;
1152 int bits = 8;
1153 int parity = 'n';
1154 int flow = 'n';
1155 int ret;
1156
Paul Mundte108b2c2006-09-27 16:32:13 +09001157 /*
1158 * Check whether an invalid uart number has been specified, and
1159 * if so, search for the first available port that does have
1160 * console support.
1161 */
1162 if (co->index >= SCI_NPORTS)
1163 co->index = 0;
1164
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001165 sci_port = &sci_ports[co->index];
1166 port = &sci_port->port;
1167 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001170 * Also need to check port->type, we don't actually have any
1171 * UPIO_PORT ports, but uart_report_port() handily misreports
1172 * it anyways if we don't have a port available by the time this is
1173 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001175 if (!port->type)
1176 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001177
Magnus Damm08f8cb32009-01-21 15:14:22 +00001178 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001179
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001180 if (sci_port->enable)
1181 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (options)
1184 uart_parse_options(options, &baud, &parity, &bits, &flow);
1185
1186 ret = uart_set_options(port, co, baud, parity, bits, flow);
1187#if defined(__H8300H__) || defined(__H8300S__)
1188 /* disable rx interrupt */
1189 if (ret == 0)
1190 sci_stop_rx(port);
1191#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001192 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return ret;
1194}
1195
1196static struct console serial_console = {
1197 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001198 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 .write = serial_console_write,
1200 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001201 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203};
1204
1205static int __init sci_console_init(void)
1206{
1207 register_console(&serial_console);
1208 return 0;
1209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210console_initcall(sci_console_init);
1211#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1212
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001213#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001214#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001216#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217#endif
1218
1219static char banner[] __initdata =
1220 KERN_INFO "SuperH SCI(F) driver initialized\n";
1221
1222static struct uart_driver sci_uart_driver = {
1223 .owner = THIS_MODULE,
1224 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 .dev_name = "ttySC",
1226 .major = SCI_MAJOR,
1227 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001228 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 .cons = SCI_CONSOLE,
1230};
1231
Magnus Damme552de22009-01-21 15:13:42 +00001232
Paul Mundt54507f62009-05-08 23:48:33 +09001233static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001234{
1235 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1236 struct sci_port *p;
1237 unsigned long flags;
1238
1239#ifdef CONFIG_HAVE_CLK
1240 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1241#endif
1242
1243 spin_lock_irqsave(&priv->lock, flags);
1244 list_for_each_entry(p, &priv->ports, node)
1245 uart_remove_one_port(&sci_uart_driver, &p->port);
1246
1247 spin_unlock_irqrestore(&priv->lock, flags);
1248
1249 kfree(priv);
1250 return 0;
1251}
1252
Magnus Damm0ee70712009-01-21 15:13:50 +00001253static int __devinit sci_probe_single(struct platform_device *dev,
1254 unsigned int index,
1255 struct plat_sci_port *p,
1256 struct sci_port *sciport)
1257{
1258 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1259 unsigned long flags;
1260 int ret;
1261
1262 /* Sanity check */
1263 if (unlikely(index >= SCI_NPORTS)) {
1264 dev_notice(&dev->dev, "Attempting to register port "
1265 "%d when only %d are available.\n",
1266 index+1, SCI_NPORTS);
1267 dev_notice(&dev->dev, "Consider bumping "
1268 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1269 return 0;
1270 }
1271
Magnus Damm501b8252009-01-21 15:14:30 +00001272 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001273
1274 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001275 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001276 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001277
1278 INIT_LIST_HEAD(&sciport->node);
1279
1280 spin_lock_irqsave(&priv->lock, flags);
1281 list_add(&sciport->node, &priv->ports);
1282 spin_unlock_irqrestore(&priv->lock, flags);
1283
1284 return 0;
1285}
1286
Paul Mundte108b2c2006-09-27 16:32:13 +09001287/*
1288 * Register a set of serial devices attached to a platform device. The
1289 * list is terminated with a zero flags entry, which means we expect
1290 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1291 * remapping (such as sh64) should also set UPF_IOREMAP.
1292 */
1293static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Paul Mundte108b2c2006-09-27 16:32:13 +09001295 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001296 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001297 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001298
1299 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1300 if (!priv)
1301 return -ENOMEM;
1302
1303 INIT_LIST_HEAD(&priv->ports);
1304 spin_lock_init(&priv->lock);
1305 platform_set_drvdata(dev, priv);
1306
1307#ifdef CONFIG_HAVE_CLK
1308 priv->clk_nb.notifier_call = sci_notifier;
1309 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Magnus Damm0ee70712009-01-21 15:13:50 +00001312 if (dev->id != -1) {
1313 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1314 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001315 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001316 } else {
1317 for (i = 0; p && p->flags != 0; p++, i++) {
1318 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1319 if (ret)
1320 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001321 }
Magnus Damme552de22009-01-21 15:13:42 +00001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324#ifdef CONFIG_SH_STANDARD_BIOS
1325 sh_bios_gdb_detach();
1326#endif
1327
Paul Mundte108b2c2006-09-27 16:32:13 +09001328 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001329
1330err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001331 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001332 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001333}
1334
Paul Mundte108b2c2006-09-27 16:32:13 +09001335static int sci_suspend(struct platform_device *dev, pm_message_t state)
1336{
Magnus Damme552de22009-01-21 15:13:42 +00001337 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1338 struct sci_port *p;
1339 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001340
Magnus Damme552de22009-01-21 15:13:42 +00001341 spin_lock_irqsave(&priv->lock, flags);
1342 list_for_each_entry(p, &priv->ports, node)
1343 uart_suspend_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001344
Magnus Damme552de22009-01-21 15:13:42 +00001345 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001346
1347 return 0;
1348}
1349
1350static int sci_resume(struct platform_device *dev)
1351{
Magnus Damme552de22009-01-21 15:13:42 +00001352 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1353 struct sci_port *p;
1354 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001355
Magnus Damme552de22009-01-21 15:13:42 +00001356 spin_lock_irqsave(&priv->lock, flags);
1357 list_for_each_entry(p, &priv->ports, node)
1358 uart_resume_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001359
Magnus Damme552de22009-01-21 15:13:42 +00001360 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001361
1362 return 0;
1363}
1364
1365static struct platform_driver sci_driver = {
1366 .probe = sci_probe,
1367 .remove = __devexit_p(sci_remove),
1368 .suspend = sci_suspend,
1369 .resume = sci_resume,
1370 .driver = {
1371 .name = "sh-sci",
1372 .owner = THIS_MODULE,
1373 },
1374};
1375
1376static int __init sci_init(void)
1377{
1378 int ret;
1379
1380 printk(banner);
1381
Paul Mundte108b2c2006-09-27 16:32:13 +09001382 ret = uart_register_driver(&sci_uart_driver);
1383 if (likely(ret == 0)) {
1384 ret = platform_driver_register(&sci_driver);
1385 if (unlikely(ret))
1386 uart_unregister_driver(&sci_uart_driver);
1387 }
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return ret;
1390}
1391
1392static void __exit sci_exit(void)
1393{
Paul Mundte108b2c2006-09-27 16:32:13 +09001394 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 uart_unregister_driver(&sci_uart_driver);
1396}
1397
1398module_init(sci_init);
1399module_exit(sci_exit);
1400
Paul Mundte108b2c2006-09-27 16:32:13 +09001401MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001402MODULE_ALIAS("platform:sh-sci");