blob: 3a13e58e9c5d593ef661fc6a1ba7c18525c236e7 [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 Mundt00b9de92009-06-24 17:53:33 +090082 /* SCSCR initialization */
83 unsigned int scscr;
84
Paul Mundta2159b52008-10-02 19:09:13 +090085#ifdef CONFIG_HAVE_CLK
Magnus Damm501b8252009-01-21 15:14:30 +000086 /* Interface clock */
87 struct clk *iclk;
88 /* Data clock */
89 struct clk *dclk;
Paul Mundt005a3362007-04-26 11:45:32 +090090#endif
Magnus Damme552de22009-01-21 15:13:42 +000091 struct list_head node;
92};
93
94struct sh_sci_priv {
95 spinlock_t lock;
96 struct list_head ports;
97
98#ifdef CONFIG_HAVE_CLK
99 struct notifier_block clk_nb;
100#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100104static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Paul Mundte108b2c2006-09-27 16:32:13 +0900106#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
107
108static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct uart_driver sci_uart_driver;
110
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900111static inline struct sci_port *
112to_sci_port(struct uart_port *uart)
113{
114 return container_of(uart, struct sci_port, port);
115}
116
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900117#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900118
119#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900120static inline void handle_error(struct uart_port *port)
121{
122 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
124}
125
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900126static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned short status;
129 int c;
130
Paul Mundte108b2c2006-09-27 16:32:13 +0900131 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 status = sci_in(port, SCxSR);
133 if (status & SCxSR_ERRORS(port)) {
134 handle_error(port);
135 continue;
136 }
137 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900140
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900141 /* Dummy read */
142 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return c;
146}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900149static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned short status;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 do {
154 status = sci_in(port, SCxSR);
155 } while (!(status & SCxSR_TDxE(port)));
156
Paul Mundt272966c2008-11-13 17:46:06 +0900157 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000158 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900160#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162#if defined(__H8300S__)
163enum { sci_disable, sci_enable };
164
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900165static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900167 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int ch = (port->mapbase - SMR0) >> 3;
169 unsigned char mask = 1 << (ch+1);
170
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900171 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900173 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
Paul Mundte108b2c2006-09-27 16:32:13 +0900176
Magnus Damm501b8252009-01-21 15:14:30 +0000177static void h8300_sci_enable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900178{
179 h8300_sci_config(port, sci_enable);
180}
181
Magnus Damm501b8252009-01-21 15:14:30 +0000182static void h8300_sci_disable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900183{
184 h8300_sci_config(port, sci_disable);
185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187
Paul Mundt15c73aa2008-10-02 19:47:12 +0900188#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900189static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int ch = (port->mapbase - SMR0) >> 3;
192
193 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900194 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
195 h8300_sci_pins[ch].rx,
196 H8300_GPIO_INPUT);
197 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
198 h8300_sci_pins[ch].tx,
199 H8300_GPIO_OUTPUT);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* tx mark output*/
202 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
203}
Paul Mundtd5701642008-12-16 20:07:27 +0900204#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
205static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900206{
Paul Mundtd5701642008-12-16 20:07:27 +0900207 if (port->mapbase == 0xA4400000) {
208 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
209 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
210 } else if (port->mapbase == 0xA4410000)
211 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900212}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900213#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900214static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900215{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900216 unsigned short data;
217
218 if (cflag & CRTSCTS) {
219 /* enable RTS/CTS */
220 if (port->mapbase == 0xa4430000) { /* SCIF0 */
221 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900222 data = __raw_readw(PORT_PTCR);
223 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900224 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
225 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900226 data = __raw_readw(PORT_PVCR);
227 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900228 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900229 } else {
230 if (port->mapbase == 0xa4430000) { /* SCIF0 */
231 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900232 data = __raw_readw(PORT_PTCR);
233 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900234 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
235 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900236 data = __raw_readw(PORT_PVCR);
237 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900238 }
239 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900240}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800241#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900242/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900243static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800245 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Paul Mundtb7a76e42006-02-01 03:06:06 -0800247 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900248 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800249 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900250 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800251
Paul Mundtd5701642008-12-16 20:07:27 +0900252 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900254 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Clear out SCP7MD1,0, SCP4MD1,0,
256 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900257 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 data = ctrl_inb(SCPDR);
260 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800261 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Paul Mundt41504c32006-12-11 20:28:03 +0900264#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900265static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900266{
Magnus Damm346b7462008-04-23 21:25:29 +0900267 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900268
Magnus Damm346b7462008-04-23 21:25:29 +0900269 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900270 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900271 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900272 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900273 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900274
Paul Mundtd5701642008-12-16 20:07:27 +0900275 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900276 }
Paul Mundt41504c32006-12-11 20:28:03 +0900277}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900278#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
279 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900280 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900281 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900282 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900283static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
284{
285 if (!(cflag & CRTSCTS))
286 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
287}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900288#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900289static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
290{
291 if (!(cflag & CRTSCTS))
292 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
293}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800294#else
Paul Mundtd5701642008-12-16 20:07:27 +0900295static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
296{
297 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Paul Mundte108b2c2006-09-27 16:32:13 +0900299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Paul Mundt32351a22007-03-12 14:38:59 +0900301#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
302 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900303 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
304 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900305static inline int scif_txroom(struct uart_port *port)
306{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900307 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900308}
309
310static inline int scif_rxroom(struct uart_port *port)
311{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900312 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900313}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900314#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
315static inline int scif_txroom(struct uart_port *port)
316{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900317 if ((port->mapbase == 0xffe00000) ||
318 (port->mapbase == 0xffe08000)) {
319 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900320 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900321 } else {
322 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900323 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900324 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900325}
326
327static inline int scif_rxroom(struct uart_port *port)
328{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900329 if ((port->mapbase == 0xffe00000) ||
330 (port->mapbase == 0xffe08000)) {
331 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900332 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900333 } else {
334 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900335 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900336 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900337}
Paul Mundte108b2c2006-09-27 16:32:13 +0900338#else
339static inline int scif_txroom(struct uart_port *port)
340{
341 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
342}
343
344static inline int scif_rxroom(struct uart_port *port)
345{
346 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Paul Mundte108b2c2006-09-27 16:32:13 +0900350static inline int sci_txroom(struct uart_port *port)
351{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900352 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900353}
354
355static inline int sci_rxroom(struct uart_port *port)
356{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900357 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* ********************************************************************** *
361 * the interrupt related routines *
362 * ********************************************************************** */
363
364static void sci_transmit_chars(struct uart_port *port)
365{
366 struct circ_buf *xmit = &port->info->xmit;
367 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned short status;
369 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900370 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 status = sci_in(port, SCxSR);
373 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900375 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900377 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return;
381 }
382
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900383 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900384 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900385 else
386 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 do {
389 unsigned char c;
390
391 if (port->x_char) {
392 c = port->x_char;
393 port->x_char = 0;
394 } else if (!uart_circ_empty(xmit) && !stopped) {
395 c = xmit->buf[xmit->tail];
396 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
397 } else {
398 break;
399 }
400
401 sci_out(port, SCxTDR, c);
402
403 port->icount.tx++;
404 } while (--count > 0);
405
406 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
407
408 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
409 uart_write_wakeup(port);
410 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100411 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ctrl = sci_in(port, SCSCR);
414
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900415 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sci_in(port, SCxSR); /* Dummy read */
417 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 ctrl |= SCI_CTRL_FLAGS_TIE;
421 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423}
424
425/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900426#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David Howells7d12e782006-10-05 14:55:46 +0100428static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900430 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100431 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int i, count, copied = 0;
433 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800434 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 status = sci_in(port, SCxSR);
437 if (!(status & SCxSR_RDxF(port)))
438 return;
439
440 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900441 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900442 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900443 else
444 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800447 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* If for any reason we can't copy more data, we're done! */
450 if (count == 0)
451 break;
452
453 if (port->type == PORT_SCI) {
454 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900455 if (uart_handle_sysrq_char(port, c) ||
456 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900458 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900459 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900461 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char c = sci_in(port, SCxRDR);
463 status = sci_in(port, SCxSR);
464#if defined(CONFIG_CPU_SH3)
465 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900466 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if ((c == 0) &&
468 (status & SCxSR_FER(port))) {
469 count--; i--;
470 continue;
471 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900474 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900475 sci_port->break_flag = 0;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (STEPFN(c)) {
478 count--; i--;
479 continue;
480 }
481 }
482#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100483 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 count--; i--;
485 continue;
486 }
487
488 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800490 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900491 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800493 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900494 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800495 } else
496 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900497
Alan Cox33f0f882006-01-09 20:54:13 -0800498 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 }
501
502 sci_in(port, SCxSR); /* dummy read */
503 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 copied += count;
506 port->icount.rx += count;
507 }
508
509 if (copied) {
510 /* Tell the rest of the system the news. New characters! */
511 tty_flip_buffer_push(tty);
512 } else {
513 sci_in(port, SCxSR); /* dummy read */
514 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
515 }
516}
517
518#define SCI_BREAK_JIFFIES (HZ/20)
519/* The sci generates interrupts during the break,
520 * 1 per millisecond or so during the break period, for 9600 baud.
521 * So dont bother disabling interrupts.
522 * But dont want more than 1 break event.
523 * Use a kernel timer to periodically poll the rx line until
524 * the break is finished.
525 */
526static void sci_schedule_break_timer(struct sci_port *port)
527{
528 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
529 add_timer(&port->break_timer);
530}
531/* Ensure that two consecutive samples find the break over. */
532static void sci_break_timer(unsigned long data)
533{
Paul Mundte108b2c2006-09-27 16:32:13 +0900534 struct sci_port *port = (struct sci_port *)data;
535
536 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900538 sci_schedule_break_timer(port);
539 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* break is over. */
541 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900542 sci_schedule_break_timer(port);
543 } else
544 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static inline int sci_handle_errors(struct uart_port *port)
548{
549 int copied = 0;
550 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100551 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Paul Mundte108b2c2006-09-27 16:32:13 +0900553 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800556 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900557
558 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Paul Mundte108b2c2006-09-27 16:32:13 +0900561 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (sci_rxd_in(port) == 0) {
563 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900564 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900565
566 if (!sci_port->break_flag) {
567 sci_port->break_flag = 1;
568 sci_schedule_break_timer(sci_port);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900571 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900573
574 dev_dbg(port->dev, "BREAK detected\n");
575
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900577 copied++;
578 }
579
Paul Mundte108b2c2006-09-27 16:32:13 +0900580 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900582 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800583 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900584
585 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 }
588
Paul Mundte108b2c2006-09-27 16:32:13 +0900589 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900591 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
592 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900593
594 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Alan Cox33f0f882006-01-09 20:54:13 -0800597 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return copied;
601}
602
Paul Mundtd830fa42008-12-16 19:29:38 +0900603static inline int sci_handle_fifo_overrun(struct uart_port *port)
604{
605 struct tty_struct *tty = port->info->port.tty;
606 int copied = 0;
607
608 if (port->type != PORT_SCIF)
609 return 0;
610
611 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
612 sci_out(port, SCLSR, 0);
613
614 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
615 tty_flip_buffer_push(tty);
616
617 dev_notice(port->dev, "overrun error\n");
618 copied++;
619 }
620
621 return copied;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static inline int sci_handle_breaks(struct uart_port *port)
625{
626 int copied = 0;
627 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100628 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000629 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900631 if (uart_handle_break(port))
632 return 0;
633
Paul Mundtb7a76e42006-02-01 03:06:06 -0800634 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#if defined(CONFIG_CPU_SH3)
636 /* Debounce break */
637 s->break_flag = 1;
638#endif
639 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900640 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800641 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900642
643 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Alan Cox33f0f882006-01-09 20:54:13 -0800646 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900648
Paul Mundtd830fa42008-12-16 19:29:38 +0900649 copied += sci_handle_fifo_overrun(port);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return copied;
652}
653
David Howells7d12e782006-10-05 14:55:46 +0100654static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* I think sci_receive_chars has to be called irrespective
657 * of whether the I_IXOFF is set, otherwise, how is the interrupt
658 * to be disabled?
659 */
David Howells7d12e782006-10-05 14:55:46 +0100660 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return IRQ_HANDLED;
663}
664
David Howells7d12e782006-10-05 14:55:46 +0100665static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct uart_port *port = ptr;
668
Paul Mundte108b2c2006-09-27 16:32:13 +0900669 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900671 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 return IRQ_HANDLED;
674}
675
David Howells7d12e782006-10-05 14:55:46 +0100676static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct uart_port *port = ptr;
679
680 /* Handle errors */
681 if (port->type == PORT_SCI) {
682 if (sci_handle_errors(port)) {
683 /* discard character in rx buffer */
684 sci_in(port, SCxSR);
685 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
686 }
687 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900688 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100689 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
693
694 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100695 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 return IRQ_HANDLED;
698}
699
David Howells7d12e782006-10-05 14:55:46 +0100700static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct uart_port *port = ptr;
703
704 /* Handle BREAKs */
705 sci_handle_breaks(port);
706 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
707
708 return IRQ_HANDLED;
709}
710
David Howells7d12e782006-10-05 14:55:46 +0100711static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900713 unsigned short ssr_status, scr_status;
714 struct uart_port *port = ptr;
715 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900717 ssr_status = sci_in(port, SCxSR);
718 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900721 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
722 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900724 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
725 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900727 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
728 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900730 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
731 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Michael Trimarchia8884e32008-10-31 16:10:23 +0900733 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Paul Mundt027e6872008-12-16 18:36:16 +0900736#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
738 * Here we define a transistion notifier so that we can update all of our
739 * ports' baud rate when the peripheral clock changes.
740 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900741static int sci_notifier(struct notifier_block *self,
742 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Magnus Damme552de22009-01-21 15:13:42 +0000744 struct sh_sci_priv *priv = container_of(self,
745 struct sh_sci_priv, clk_nb);
746 struct sci_port *sci_port;
747 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000750 (phase == CPUFREQ_RESUMECHANGE)) {
751 spin_lock_irqsave(&priv->lock, flags);
752 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000753 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000754
755 spin_unlock_irqrestore(&priv->lock, flags);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return NOTIFY_OK;
759}
Magnus Damm501b8252009-01-21 15:14:30 +0000760
761static void sci_clk_enable(struct uart_port *port)
762{
763 struct sci_port *sci_port = to_sci_port(port);
764
765 clk_enable(sci_port->dclk);
766 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
767
768 if (sci_port->iclk)
769 clk_enable(sci_port->iclk);
770}
771
772static void sci_clk_disable(struct uart_port *port)
773{
774 struct sci_port *sci_port = to_sci_port(port);
775
776 if (sci_port->iclk)
777 clk_disable(sci_port->iclk);
778
779 clk_disable(sci_port->dclk);
780}
Paul Mundt027e6872008-12-16 18:36:16 +0900781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783static int sci_request_irq(struct sci_port *port)
784{
785 int i;
David Howells7d12e782006-10-05 14:55:46 +0100786 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
788 sci_br_interrupt,
789 };
790 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
791 "SCI Transmit Data Empty", "SCI Break" };
792
793 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900794 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900796
797 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900798 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900799 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -ENODEV;
801 }
802 } else {
803 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900804 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900806
Paul Mundte108b2c2006-09-27 16:32:13 +0900807 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900808 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900809 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -ENODEV;
811 }
812 }
813 }
814
815 return 0;
816}
817
818static void sci_free_irq(struct sci_port *port)
819{
820 int i;
821
Paul Mundt762c69e2008-12-16 18:55:26 +0900822 if (port->irqs[0] == port->irqs[1])
823 free_irq(port->irqs[0], port);
824 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
826 if (!port->irqs[i])
827 continue;
828
829 free_irq(port->irqs[i], port);
830 }
831 }
832}
833
834static unsigned int sci_tx_empty(struct uart_port *port)
835{
836 /* Can't detect */
837 return TIOCSER_TEMT;
838}
839
840static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
841{
842 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
843 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
844 /* If you have signals for DTR and DCD, please implement here. */
845}
846
847static unsigned int sci_get_mctrl(struct uart_port *port)
848{
849 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
850 and CTS/RTS */
851
852 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
853}
854
Russell Kingb129a8c2005-08-31 10:12:14 +0100855static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Paul Mundte108b2c2006-09-27 16:32:13 +0900857 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Paul Mundte108b2c2006-09-27 16:32:13 +0900859 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
860 ctrl = sci_in(port, SCSCR);
861 ctrl |= SCI_CTRL_FLAGS_TIE;
862 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Russell Kingb129a8c2005-08-31 10:12:14 +0100865static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned short ctrl;
868
869 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ctrl = sci_in(port, SCSCR);
871 ctrl &= ~SCI_CTRL_FLAGS_TIE;
872 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
876{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 unsigned short ctrl;
878
879 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ctrl = sci_in(port, SCSCR);
881 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
882 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static void sci_stop_rx(struct uart_port *port)
886{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 unsigned short ctrl;
888
889 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ctrl = sci_in(port, SCSCR);
891 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
892 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895static void sci_enable_ms(struct uart_port *port)
896{
897 /* Nothing here yet .. */
898}
899
900static void sci_break_ctl(struct uart_port *port, int break_state)
901{
902 /* Nothing here yet .. */
903}
904
905static int sci_startup(struct uart_port *port)
906{
Magnus Damma5660ad2009-01-21 15:14:38 +0000907 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Paul Mundte108b2c2006-09-27 16:32:13 +0900909 if (s->enable)
910 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700913 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sci_start_rx(port, 1);
915
916 return 0;
917}
918
919static void sci_shutdown(struct uart_port *port)
920{
Magnus Damma5660ad2009-01-21 15:14:38 +0000921 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100924 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 sci_free_irq(s);
926
Paul Mundte108b2c2006-09-27 16:32:13 +0900927 if (s->disable)
928 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Alan Cox606d0992006-12-08 02:38:45 -0800931static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
932 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Paul Mundt00b9de92009-06-24 17:53:33 +0900934 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900936 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900939 if (likely(baud))
940 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Paul Mundte108b2c2006-09-27 16:32:13 +0900942 do {
943 status = sci_in(port, SCxSR);
944 } while (!(status & SCxSR_TEND(port)));
945
946 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
947
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900948 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900949 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900950
951 smr_val = sci_in(port, SCSMR) & 3;
952 if ((termios->c_cflag & CSIZE) == CS7)
953 smr_val |= 0x40;
954 if (termios->c_cflag & PARENB)
955 smr_val |= 0x20;
956 if (termios->c_cflag & PARODD)
957 smr_val |= 0x30;
958 if (termios->c_cflag & CSTOPB)
959 smr_val |= 0x08;
960
961 uart_update_timeout(port, termios->c_cflag, baud);
962
963 sci_out(port, SCSMR, smr_val);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900966 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
968 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900969 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sci_out(port, SCBRR, t);
973 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
974 }
975
Paul Mundtd5701642008-12-16 20:07:27 +0900976 sci_init_pins(port, termios->c_cflag);
977 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800978
Paul Mundt00b9de92009-06-24 17:53:33 +0900979 sci_out(port, SCSCR, s->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900982 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static const char *sci_type(struct uart_port *port)
986{
987 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900988 case PORT_IRDA:
989 return "irda";
990 case PORT_SCI:
991 return "sci";
992 case PORT_SCIF:
993 return "scif";
994 case PORT_SCIFA:
995 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
Paul Mundtfa439722008-09-04 18:53:58 +0900998 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static void sci_release_port(struct uart_port *port)
1002{
1003 /* Nothing here yet .. */
1004}
1005
1006static int sci_request_port(struct uart_port *port)
1007{
1008 /* Nothing here yet .. */
1009 return 0;
1010}
1011
1012static void sci_config_port(struct uart_port *port, int flags)
1013{
Magnus Damma5660ad2009-01-21 15:14:38 +00001014 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 port->type = s->type;
1017
Magnus Damm08f8cb32009-01-21 15:14:22 +00001018 if (port->membase)
1019 return;
1020
1021 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001022 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001023
1024 if (IS_ERR(port->membase))
1025 dev_err(port->dev, "can't remap port#%d\n", port->line);
1026 } else {
1027 /*
1028 * For the simple (and majority of) cases where we don't
1029 * need to do any remapping, just cast the cookie
1030 * directly.
1031 */
1032 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1037{
Magnus Damma5660ad2009-01-21 15:14:38 +00001038 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Yinghai Lua62c4132008-08-19 20:49:55 -07001040 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
1042 if (ser->baud_base < 2400)
1043 /* No paper tape reader for Mitch.. */
1044 return -EINVAL;
1045
1046 return 0;
1047}
1048
1049static struct uart_ops sci_uart_ops = {
1050 .tx_empty = sci_tx_empty,
1051 .set_mctrl = sci_set_mctrl,
1052 .get_mctrl = sci_get_mctrl,
1053 .start_tx = sci_start_tx,
1054 .stop_tx = sci_stop_tx,
1055 .stop_rx = sci_stop_rx,
1056 .enable_ms = sci_enable_ms,
1057 .break_ctl = sci_break_ctl,
1058 .startup = sci_startup,
1059 .shutdown = sci_shutdown,
1060 .set_termios = sci_set_termios,
1061 .type = sci_type,
1062 .release_port = sci_release_port,
1063 .request_port = sci_request_port,
1064 .config_port = sci_config_port,
1065 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001066#ifdef CONFIG_CONSOLE_POLL
1067 .poll_get_char = sci_poll_get_char,
1068 .poll_put_char = sci_poll_put_char,
1069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070};
1071
Magnus Damm501b8252009-01-21 15:14:30 +00001072static void __devinit sci_init_single(struct platform_device *dev,
1073 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001074 unsigned int index,
1075 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001076{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001077 sci_port->port.ops = &sci_uart_ops;
1078 sci_port->port.iotype = UPIO_MEM;
1079 sci_port->port.line = index;
1080 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001081
1082#if defined(__H8300H__) || defined(__H8300S__)
1083#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001084 sci_port->enable = h8300_sci_enable;
1085 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001087 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001088#elif defined(CONFIG_HAVE_CLK)
Magnus Damm501b8252009-01-21 15:14:30 +00001089 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
Paul Mundtaf777ce2009-05-13 16:59:40 +09001090 sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
Magnus Damm501b8252009-01-21 15:14:30 +00001091 sci_port->enable = sci_clk_enable;
1092 sci_port->disable = sci_clk_disable;
Paul Mundta2159b52008-10-02 19:09:13 +09001093#else
1094#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001095#endif
1096
Magnus Damm7ed7e072009-01-21 15:14:14 +00001097 sci_port->break_timer.data = (unsigned long)sci_port;
1098 sci_port->break_timer.function = sci_break_timer;
1099 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001100
Magnus Damm7ed7e072009-01-21 15:14:14 +00001101 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001102 sci_port->port.membase = p->membase;
1103
Paul Mundt00b9de92009-06-24 17:53:33 +09001104 sci_port->scscr = p->scscr;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001105 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1106 sci_port->port.flags = p->flags;
Magnus Damm501b8252009-01-21 15:14:30 +00001107 sci_port->port.dev = &dev->dev;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001108 sci_port->type = sci_port->port.type = p->type;
1109
1110 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Magnus Damm501b8252009-01-21 15:14:30 +00001111
Paul Mundte108b2c2006-09-27 16:32:13 +09001112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001115static struct tty_driver *serial_console_device(struct console *co, int *index)
1116{
1117 struct uart_driver *p = &sci_uart_driver;
1118 *index = co->index;
1119 return p->tty_driver;
1120}
1121
1122static void serial_console_putchar(struct uart_port *port, int ch)
1123{
1124 sci_poll_put_char(port, ch);
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * Print a string to the serial port trying not to disturb
1129 * any possible real use of the port...
1130 */
1131static void serial_console_write(struct console *co, const char *s,
1132 unsigned count)
1133{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001134 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001135 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001136 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001137
Magnus Damm501b8252009-01-21 15:14:30 +00001138 if (sci_port->enable)
1139 sci_port->enable(port);
1140
1141 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001142
1143 /* wait until fifo is empty and last bit has been transmitted */
1144 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1145 while ((sci_in(port, SCxSR) & bits) != bits)
1146 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001147
1148 if (sci_port->disable);
1149 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152static int __init serial_console_setup(struct console *co, char *options)
1153{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001154 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct uart_port *port;
1156 int baud = 115200;
1157 int bits = 8;
1158 int parity = 'n';
1159 int flow = 'n';
1160 int ret;
1161
Paul Mundte108b2c2006-09-27 16:32:13 +09001162 /*
1163 * Check whether an invalid uart number has been specified, and
1164 * if so, search for the first available port that does have
1165 * console support.
1166 */
1167 if (co->index >= SCI_NPORTS)
1168 co->index = 0;
1169
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001170 sci_port = &sci_ports[co->index];
1171 port = &sci_port->port;
1172 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001175 * Also need to check port->type, we don't actually have any
1176 * UPIO_PORT ports, but uart_report_port() handily misreports
1177 * it anyways if we don't have a port available by the time this is
1178 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001180 if (!port->type)
1181 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001182
Magnus Damm08f8cb32009-01-21 15:14:22 +00001183 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001184
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001185 if (sci_port->enable)
1186 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (options)
1189 uart_parse_options(options, &baud, &parity, &bits, &flow);
1190
1191 ret = uart_set_options(port, co, baud, parity, bits, flow);
1192#if defined(__H8300H__) || defined(__H8300S__)
1193 /* disable rx interrupt */
1194 if (ret == 0)
1195 sci_stop_rx(port);
1196#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001197 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return ret;
1199}
1200
1201static struct console serial_console = {
1202 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001203 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 .write = serial_console_write,
1205 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001206 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208};
1209
1210static int __init sci_console_init(void)
1211{
1212 register_console(&serial_console);
1213 return 0;
1214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215console_initcall(sci_console_init);
1216#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1217
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001218#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001219#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001221#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222#endif
1223
1224static char banner[] __initdata =
1225 KERN_INFO "SuperH SCI(F) driver initialized\n";
1226
1227static struct uart_driver sci_uart_driver = {
1228 .owner = THIS_MODULE,
1229 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .dev_name = "ttySC",
1231 .major = SCI_MAJOR,
1232 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001233 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 .cons = SCI_CONSOLE,
1235};
1236
Magnus Damme552de22009-01-21 15:13:42 +00001237
Paul Mundt54507f62009-05-08 23:48:33 +09001238static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001239{
1240 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1241 struct sci_port *p;
1242 unsigned long flags;
1243
1244#ifdef CONFIG_HAVE_CLK
1245 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1246#endif
1247
1248 spin_lock_irqsave(&priv->lock, flags);
1249 list_for_each_entry(p, &priv->ports, node)
1250 uart_remove_one_port(&sci_uart_driver, &p->port);
1251
1252 spin_unlock_irqrestore(&priv->lock, flags);
1253
1254 kfree(priv);
1255 return 0;
1256}
1257
Magnus Damm0ee70712009-01-21 15:13:50 +00001258static int __devinit sci_probe_single(struct platform_device *dev,
1259 unsigned int index,
1260 struct plat_sci_port *p,
1261 struct sci_port *sciport)
1262{
1263 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1264 unsigned long flags;
1265 int ret;
1266
1267 /* Sanity check */
1268 if (unlikely(index >= SCI_NPORTS)) {
1269 dev_notice(&dev->dev, "Attempting to register port "
1270 "%d when only %d are available.\n",
1271 index+1, SCI_NPORTS);
1272 dev_notice(&dev->dev, "Consider bumping "
1273 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1274 return 0;
1275 }
1276
Magnus Damm501b8252009-01-21 15:14:30 +00001277 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001278
1279 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001280 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001281 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001282
1283 INIT_LIST_HEAD(&sciport->node);
1284
1285 spin_lock_irqsave(&priv->lock, flags);
1286 list_add(&sciport->node, &priv->ports);
1287 spin_unlock_irqrestore(&priv->lock, flags);
1288
1289 return 0;
1290}
1291
Paul Mundte108b2c2006-09-27 16:32:13 +09001292/*
1293 * Register a set of serial devices attached to a platform device. The
1294 * list is terminated with a zero flags entry, which means we expect
1295 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1296 * remapping (such as sh64) should also set UPF_IOREMAP.
1297 */
1298static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Paul Mundte108b2c2006-09-27 16:32:13 +09001300 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001301 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001302 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001303
1304 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1305 if (!priv)
1306 return -ENOMEM;
1307
1308 INIT_LIST_HEAD(&priv->ports);
1309 spin_lock_init(&priv->lock);
1310 platform_set_drvdata(dev, priv);
1311
1312#ifdef CONFIG_HAVE_CLK
1313 priv->clk_nb.notifier_call = sci_notifier;
1314 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Magnus Damm0ee70712009-01-21 15:13:50 +00001317 if (dev->id != -1) {
1318 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1319 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001320 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001321 } else {
1322 for (i = 0; p && p->flags != 0; p++, i++) {
1323 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1324 if (ret)
1325 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001326 }
Magnus Damme552de22009-01-21 15:13:42 +00001327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329#ifdef CONFIG_SH_STANDARD_BIOS
1330 sh_bios_gdb_detach();
1331#endif
1332
Paul Mundte108b2c2006-09-27 16:32:13 +09001333 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001334
1335err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001336 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001337 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001338}
1339
Paul Mundt6daa79b2009-06-15 07:07:38 +09001340static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001341{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001342 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001343 struct sci_port *p;
1344 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001345
Magnus Damme552de22009-01-21 15:13:42 +00001346 spin_lock_irqsave(&priv->lock, flags);
1347 list_for_each_entry(p, &priv->ports, node)
1348 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001349 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001350
1351 return 0;
1352}
1353
Paul Mundt6daa79b2009-06-15 07:07:38 +09001354static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001355{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001356 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001357 struct sci_port *p;
1358 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001359
Magnus Damme552de22009-01-21 15:13:42 +00001360 spin_lock_irqsave(&priv->lock, flags);
1361 list_for_each_entry(p, &priv->ports, node)
1362 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001363 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001364
1365 return 0;
1366}
1367
Paul Mundt6daa79b2009-06-15 07:07:38 +09001368static struct dev_pm_ops sci_dev_pm_ops = {
1369 .suspend = sci_suspend,
1370 .resume = sci_resume,
1371};
1372
Paul Mundte108b2c2006-09-27 16:32:13 +09001373static struct platform_driver sci_driver = {
1374 .probe = sci_probe,
1375 .remove = __devexit_p(sci_remove),
Paul Mundte108b2c2006-09-27 16:32:13 +09001376 .driver = {
1377 .name = "sh-sci",
1378 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001379 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001380 },
1381};
1382
1383static int __init sci_init(void)
1384{
1385 int ret;
1386
1387 printk(banner);
1388
Paul Mundte108b2c2006-09-27 16:32:13 +09001389 ret = uart_register_driver(&sci_uart_driver);
1390 if (likely(ret == 0)) {
1391 ret = platform_driver_register(&sci_driver);
1392 if (unlikely(ret))
1393 uart_unregister_driver(&sci_uart_driver);
1394 }
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return ret;
1397}
1398
1399static void __exit sci_exit(void)
1400{
Paul Mundte108b2c2006-09-27 16:32:13 +09001401 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 uart_unregister_driver(&sci_uart_driver);
1403}
1404
1405module_init(sci_init);
1406module_exit(sci_exit);
1407
Paul Mundte108b2c2006-09-27 16:32:13 +09001408MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001409MODULE_ALIAS("platform:sh-sci");