blob: 3daf76725ac6fc0663e1d215149a244030857f63 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090051
52#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080053#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundte108b2c2006-09-27 16:32:13 +090062 /* Port type */
63 unsigned int type;
64
65 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090066 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090067
Paul Mundte108b2c2006-09-27 16:32:13 +090068 /* Port enable callback */
69 void (*enable)(struct uart_port *port);
70
71 /* Port disable callback */
72 void (*disable)(struct uart_port *port);
73
74 /* Break timer */
75 struct timer_list break_timer;
76 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090077
Paul Mundta2159b52008-10-02 19:09:13 +090078#ifdef CONFIG_HAVE_CLK
Magnus Damm501b8252009-01-21 15:14:30 +000079 /* Interface clock */
80 struct clk *iclk;
81 /* Data clock */
82 struct clk *dclk;
Paul Mundt005a3362007-04-26 11:45:32 +090083#endif
Magnus Damme552de22009-01-21 15:13:42 +000084 struct list_head node;
85};
86
87struct sh_sci_priv {
88 spinlock_t lock;
89 struct list_head ports;
90
91#ifdef CONFIG_HAVE_CLK
92 struct notifier_block clk_nb;
93#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090094};
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010097static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Paul Mundte108b2c2006-09-27 16:32:13 +090099#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
100
101static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct uart_driver sci_uart_driver;
103
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900104static inline struct sci_port *
105to_sci_port(struct uart_port *uart)
106{
107 return container_of(uart, struct sci_port, port);
108}
109
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900110#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900111
112#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900113static inline void handle_error(struct uart_port *port)
114{
115 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
117}
118
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900119static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned short status;
122 int c;
123
Paul Mundte108b2c2006-09-27 16:32:13 +0900124 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 status = sci_in(port, SCxSR);
126 if (status & SCxSR_ERRORS(port)) {
127 handle_error(port);
128 continue;
129 }
130 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900133
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900134 /* Dummy read */
135 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return c;
139}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900142static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned short status;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 do {
147 status = sci_in(port, SCxSR);
148 } while (!(status & SCxSR_TDxE(port)));
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sci_in(port, SCxSR); /* Dummy read */
Magnus Damm973e5d52009-02-24 15:57:12 +0900151 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900152 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900154#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#if defined(__H8300S__)
157enum { sci_disable, sci_enable };
158
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900159static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900161 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int ch = (port->mapbase - SMR0) >> 3;
163 unsigned char mask = 1 << (ch+1);
164
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900165 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900167 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Paul Mundte108b2c2006-09-27 16:32:13 +0900170
Magnus Damm501b8252009-01-21 15:14:30 +0000171static void h8300_sci_enable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900172{
173 h8300_sci_config(port, sci_enable);
174}
175
Magnus Damm501b8252009-01-21 15:14:30 +0000176static void h8300_sci_disable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900177{
178 h8300_sci_config(port, sci_disable);
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif
181
Paul Mundt15c73aa2008-10-02 19:47:12 +0900182#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900183static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int ch = (port->mapbase - SMR0) >> 3;
186
187 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900188 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
189 h8300_sci_pins[ch].rx,
190 H8300_GPIO_INPUT);
191 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
192 h8300_sci_pins[ch].tx,
193 H8300_GPIO_OUTPUT);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* tx mark output*/
196 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
197}
Paul Mundtd5701642008-12-16 20:07:27 +0900198#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
199static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900200{
Paul Mundtd5701642008-12-16 20:07:27 +0900201 if (port->mapbase == 0xA4400000) {
202 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
203 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
204 } else if (port->mapbase == 0xA4410000)
205 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900206}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900207#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900208static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900209{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900210 unsigned short data;
211
212 if (cflag & CRTSCTS) {
213 /* enable RTS/CTS */
214 if (port->mapbase == 0xa4430000) { /* SCIF0 */
215 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900216 data = __raw_readw(PORT_PTCR);
217 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
219 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900220 data = __raw_readw(PORT_PVCR);
221 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900222 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900223 } else {
224 if (port->mapbase == 0xa4430000) { /* SCIF0 */
225 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900226 data = __raw_readw(PORT_PTCR);
227 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900228 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
229 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900230 data = __raw_readw(PORT_PVCR);
231 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900232 }
233 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900234}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800235#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900236/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900237static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800239 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Paul Mundtb7a76e42006-02-01 03:06:06 -0800241 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900242 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800243 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900244 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800245
Paul Mundtd5701642008-12-16 20:07:27 +0900246 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900248 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Clear out SCP7MD1,0, SCP4MD1,0,
250 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900251 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 data = ctrl_inb(SCPDR);
254 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800255 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Paul Mundt41504c32006-12-11 20:28:03 +0900258#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900259static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900260{
Magnus Damm346b7462008-04-23 21:25:29 +0900261 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900262
Magnus Damm346b7462008-04-23 21:25:29 +0900263 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900264 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900265 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900266 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900267 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900268
Paul Mundtd5701642008-12-16 20:07:27 +0900269 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900270 }
Paul Mundt41504c32006-12-11 20:28:03 +0900271}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900272#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
273 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900274 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900275 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900276 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900277static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
278{
279 if (!(cflag & CRTSCTS))
280 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
281}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900282#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
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, SCSPTR2); /* Set RTS = 1 */
287}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800288#else
Paul Mundtd5701642008-12-16 20:07:27 +0900289static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
290{
291 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Paul Mundte108b2c2006-09-27 16:32:13 +0900293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Paul Mundt32351a22007-03-12 14:38:59 +0900295#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
296 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900297 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
298 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900299static inline int scif_txroom(struct uart_port *port)
300{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900301 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900302}
303
304static inline int scif_rxroom(struct uart_port *port)
305{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900306 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900307}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900308#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
309static inline int scif_txroom(struct uart_port *port)
310{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900311 if ((port->mapbase == 0xffe00000) ||
312 (port->mapbase == 0xffe08000)) {
313 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900314 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900315 } else {
316 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900318 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900319}
320
321static inline int scif_rxroom(struct uart_port *port)
322{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900323 if ((port->mapbase == 0xffe00000) ||
324 (port->mapbase == 0xffe08000)) {
325 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900326 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900327 } else {
328 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900329 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900330 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900331}
Paul Mundte108b2c2006-09-27 16:32:13 +0900332#else
333static inline int scif_txroom(struct uart_port *port)
334{
335 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
336}
337
338static inline int scif_rxroom(struct uart_port *port)
339{
340 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
341}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Paul Mundte108b2c2006-09-27 16:32:13 +0900344static inline int sci_txroom(struct uart_port *port)
345{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900346 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900347}
348
349static inline int sci_rxroom(struct uart_port *port)
350{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900351 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/* ********************************************************************** *
355 * the interrupt related routines *
356 * ********************************************************************** */
357
358static void sci_transmit_chars(struct uart_port *port)
359{
360 struct circ_buf *xmit = &port->info->xmit;
361 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned short status;
363 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900364 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 status = sci_in(port, SCxSR);
367 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900369 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900371 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return;
375 }
376
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900377 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900378 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900379 else
380 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 do {
383 unsigned char c;
384
385 if (port->x_char) {
386 c = port->x_char;
387 port->x_char = 0;
388 } else if (!uart_circ_empty(xmit) && !stopped) {
389 c = xmit->buf[xmit->tail];
390 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
391 } else {
392 break;
393 }
394
395 sci_out(port, SCxTDR, c);
396
397 port->icount.tx++;
398 } while (--count > 0);
399
400 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
401
402 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
403 uart_write_wakeup(port);
404 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100405 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ctrl = sci_in(port, SCSCR);
408
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900409 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 sci_in(port, SCxSR); /* Dummy read */
411 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 ctrl |= SCI_CTRL_FLAGS_TIE;
415 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417}
418
419/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900420#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Howells7d12e782006-10-05 14:55:46 +0100422static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900424 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100425 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int i, count, copied = 0;
427 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800428 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 status = sci_in(port, SCxSR);
431 if (!(status & SCxSR_RDxF(port)))
432 return;
433
434 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900435 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900436 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900437 else
438 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800441 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* If for any reason we can't copy more data, we're done! */
444 if (count == 0)
445 break;
446
447 if (port->type == PORT_SCI) {
448 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900449 if (uart_handle_sysrq_char(port, c) ||
450 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900452 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900453 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900455 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 char c = sci_in(port, SCxRDR);
457 status = sci_in(port, SCxSR);
458#if defined(CONFIG_CPU_SH3)
459 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900460 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if ((c == 0) &&
462 (status & SCxSR_FER(port))) {
463 count--; i--;
464 continue;
465 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900468 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900469 sci_port->break_flag = 0;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (STEPFN(c)) {
472 count--; i--;
473 continue;
474 }
475 }
476#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100477 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 count--; i--;
479 continue;
480 }
481
482 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800484 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900485 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800487 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900488 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800489 } else
490 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900491
Alan Cox33f0f882006-01-09 20:54:13 -0800492 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 }
495
496 sci_in(port, SCxSR); /* dummy read */
497 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 copied += count;
500 port->icount.rx += count;
501 }
502
503 if (copied) {
504 /* Tell the rest of the system the news. New characters! */
505 tty_flip_buffer_push(tty);
506 } else {
507 sci_in(port, SCxSR); /* dummy read */
508 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
509 }
510}
511
512#define SCI_BREAK_JIFFIES (HZ/20)
513/* The sci generates interrupts during the break,
514 * 1 per millisecond or so during the break period, for 9600 baud.
515 * So dont bother disabling interrupts.
516 * But dont want more than 1 break event.
517 * Use a kernel timer to periodically poll the rx line until
518 * the break is finished.
519 */
520static void sci_schedule_break_timer(struct sci_port *port)
521{
522 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
523 add_timer(&port->break_timer);
524}
525/* Ensure that two consecutive samples find the break over. */
526static void sci_break_timer(unsigned long data)
527{
Paul Mundte108b2c2006-09-27 16:32:13 +0900528 struct sci_port *port = (struct sci_port *)data;
529
530 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900532 sci_schedule_break_timer(port);
533 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* break is over. */
535 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900536 sci_schedule_break_timer(port);
537 } else
538 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static inline int sci_handle_errors(struct uart_port *port)
542{
543 int copied = 0;
544 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100545 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Paul Mundte108b2c2006-09-27 16:32:13 +0900547 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900549 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800550 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900551
552 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (sci_rxd_in(port) == 0) {
557 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900558 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900559
560 if (!sci_port->break_flag) {
561 sci_port->break_flag = 1;
562 sci_schedule_break_timer(sci_port);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900565 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900567
568 dev_dbg(port->dev, "BREAK detected\n");
569
Paul Mundte108b2c2006-09-27 16:32:13 +0900570 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900571 copied++;
572 }
573
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800577 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900578
579 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582
Paul Mundte108b2c2006-09-27 16:32:13 +0900583 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900585 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
586 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900587
588 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Alan Cox33f0f882006-01-09 20:54:13 -0800591 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return copied;
595}
596
Paul Mundtd830fa42008-12-16 19:29:38 +0900597static inline int sci_handle_fifo_overrun(struct uart_port *port)
598{
599 struct tty_struct *tty = port->info->port.tty;
600 int copied = 0;
601
602 if (port->type != PORT_SCIF)
603 return 0;
604
605 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
606 sci_out(port, SCLSR, 0);
607
608 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
609 tty_flip_buffer_push(tty);
610
611 dev_notice(port->dev, "overrun error\n");
612 copied++;
613 }
614
615 return copied;
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static inline int sci_handle_breaks(struct uart_port *port)
619{
620 int copied = 0;
621 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100622 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000623 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900625 if (uart_handle_break(port))
626 return 0;
627
Paul Mundtb7a76e42006-02-01 03:06:06 -0800628 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#if defined(CONFIG_CPU_SH3)
630 /* Debounce break */
631 s->break_flag = 1;
632#endif
633 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900634 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800635 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900636
637 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Alan Cox33f0f882006-01-09 20:54:13 -0800640 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900642
Paul Mundtd830fa42008-12-16 19:29:38 +0900643 copied += sci_handle_fifo_overrun(port);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return copied;
646}
647
David Howells7d12e782006-10-05 14:55:46 +0100648static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* I think sci_receive_chars has to be called irrespective
651 * of whether the I_IXOFF is set, otherwise, how is the interrupt
652 * to be disabled?
653 */
David Howells7d12e782006-10-05 14:55:46 +0100654 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return IRQ_HANDLED;
657}
658
David Howells7d12e782006-10-05 14:55:46 +0100659static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct uart_port *port = ptr;
662
Paul Mundte108b2c2006-09-27 16:32:13 +0900663 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900665 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return IRQ_HANDLED;
668}
669
David Howells7d12e782006-10-05 14:55:46 +0100670static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct uart_port *port = ptr;
673
674 /* Handle errors */
675 if (port->type == PORT_SCI) {
676 if (sci_handle_errors(port)) {
677 /* discard character in rx buffer */
678 sci_in(port, SCxSR);
679 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
680 }
681 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900682 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100683 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
687
688 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100689 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 return IRQ_HANDLED;
692}
693
David Howells7d12e782006-10-05 14:55:46 +0100694static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct uart_port *port = ptr;
697
698 /* Handle BREAKs */
699 sci_handle_breaks(port);
700 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
701
702 return IRQ_HANDLED;
703}
704
David Howells7d12e782006-10-05 14:55:46 +0100705static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900707 unsigned short ssr_status, scr_status;
708 struct uart_port *port = ptr;
709 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900711 ssr_status = sci_in(port, SCxSR);
712 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900715 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
716 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900718 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
719 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900721 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
722 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900724 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
725 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Michael Trimarchia8884e32008-10-31 16:10:23 +0900727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Paul Mundt027e6872008-12-16 18:36:16 +0900730#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Here we define a transistion notifier so that we can update all of our
733 * ports' baud rate when the peripheral clock changes.
734 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900735static int sci_notifier(struct notifier_block *self,
736 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Magnus Damme552de22009-01-21 15:13:42 +0000738 struct sh_sci_priv *priv = container_of(self,
739 struct sh_sci_priv, clk_nb);
740 struct sci_port *sci_port;
741 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000744 (phase == CPUFREQ_RESUMECHANGE)) {
745 spin_lock_irqsave(&priv->lock, flags);
746 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000747 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000748
749 spin_unlock_irqrestore(&priv->lock, flags);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return NOTIFY_OK;
753}
Magnus Damm501b8252009-01-21 15:14:30 +0000754
755static void sci_clk_enable(struct uart_port *port)
756{
757 struct sci_port *sci_port = to_sci_port(port);
758
759 clk_enable(sci_port->dclk);
760 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
761
762 if (sci_port->iclk)
763 clk_enable(sci_port->iclk);
764}
765
766static void sci_clk_disable(struct uart_port *port)
767{
768 struct sci_port *sci_port = to_sci_port(port);
769
770 if (sci_port->iclk)
771 clk_disable(sci_port->iclk);
772
773 clk_disable(sci_port->dclk);
774}
Paul Mundt027e6872008-12-16 18:36:16 +0900775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777static int sci_request_irq(struct sci_port *port)
778{
779 int i;
David Howells7d12e782006-10-05 14:55:46 +0100780 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
782 sci_br_interrupt,
783 };
784 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
785 "SCI Transmit Data Empty", "SCI Break" };
786
787 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900788 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900790
791 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900792 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900793 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -ENODEV;
795 }
796 } else {
797 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900798 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900800
Paul Mundte108b2c2006-09-27 16:32:13 +0900801 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900802 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900803 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ENODEV;
805 }
806 }
807 }
808
809 return 0;
810}
811
812static void sci_free_irq(struct sci_port *port)
813{
814 int i;
815
Paul Mundt762c69e2008-12-16 18:55:26 +0900816 if (port->irqs[0] == port->irqs[1])
817 free_irq(port->irqs[0], port);
818 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
820 if (!port->irqs[i])
821 continue;
822
823 free_irq(port->irqs[i], port);
824 }
825 }
826}
827
828static unsigned int sci_tx_empty(struct uart_port *port)
829{
830 /* Can't detect */
831 return TIOCSER_TEMT;
832}
833
834static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
835{
836 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
837 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
838 /* If you have signals for DTR and DCD, please implement here. */
839}
840
841static unsigned int sci_get_mctrl(struct uart_port *port)
842{
843 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
844 and CTS/RTS */
845
846 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
847}
848
Russell Kingb129a8c2005-08-31 10:12:14 +0100849static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Paul Mundte108b2c2006-09-27 16:32:13 +0900851 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Paul Mundte108b2c2006-09-27 16:32:13 +0900853 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
854 ctrl = sci_in(port, SCSCR);
855 ctrl |= SCI_CTRL_FLAGS_TIE;
856 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Russell Kingb129a8c2005-08-31 10:12:14 +0100859static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned short ctrl;
862
863 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ctrl = sci_in(port, SCSCR);
865 ctrl &= ~SCI_CTRL_FLAGS_TIE;
866 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned short ctrl;
872
873 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ctrl = sci_in(port, SCSCR);
875 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
876 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879static void sci_stop_rx(struct uart_port *port)
880{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned short ctrl;
882
883 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ctrl = sci_in(port, SCSCR);
885 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
886 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889static void sci_enable_ms(struct uart_port *port)
890{
891 /* Nothing here yet .. */
892}
893
894static void sci_break_ctl(struct uart_port *port, int break_state)
895{
896 /* Nothing here yet .. */
897}
898
899static int sci_startup(struct uart_port *port)
900{
Magnus Damma5660ad2009-01-21 15:14:38 +0000901 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Paul Mundte108b2c2006-09-27 16:32:13 +0900903 if (s->enable)
904 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700907 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sci_start_rx(port, 1);
909
910 return 0;
911}
912
913static void sci_shutdown(struct uart_port *port)
914{
Magnus Damma5660ad2009-01-21 15:14:38 +0000915 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100918 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sci_free_irq(s);
920
Paul Mundte108b2c2006-09-27 16:32:13 +0900921 if (s->disable)
922 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Alan Cox606d0992006-12-08 02:38:45 -0800925static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
926 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900929 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900932 if (likely(baud))
933 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Paul Mundte108b2c2006-09-27 16:32:13 +0900935 do {
936 status = sci_in(port, SCxSR);
937 } while (!(status & SCxSR_TEND(port)));
938
939 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
940
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900941 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900942 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900943
944 smr_val = sci_in(port, SCSMR) & 3;
945 if ((termios->c_cflag & CSIZE) == CS7)
946 smr_val |= 0x40;
947 if (termios->c_cflag & PARENB)
948 smr_val |= 0x20;
949 if (termios->c_cflag & PARODD)
950 smr_val |= 0x30;
951 if (termios->c_cflag & CSTOPB)
952 smr_val |= 0x08;
953
954 uart_update_timeout(port, termios->c_cflag, baud);
955
956 sci_out(port, SCSMR, smr_val);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900959 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
961 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900962 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sci_out(port, SCBRR, t);
966 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
967 }
968
Paul Mundtd5701642008-12-16 20:07:27 +0900969 sci_init_pins(port, termios->c_cflag);
970 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sci_out(port, SCSCR, SCSCR_INIT(port));
973
974 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900975 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978static const char *sci_type(struct uart_port *port)
979{
980 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900981 case PORT_IRDA:
982 return "irda";
983 case PORT_SCI:
984 return "sci";
985 case PORT_SCIF:
986 return "scif";
987 case PORT_SCIFA:
988 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Paul Mundtfa439722008-09-04 18:53:58 +0900991 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static void sci_release_port(struct uart_port *port)
995{
996 /* Nothing here yet .. */
997}
998
999static int sci_request_port(struct uart_port *port)
1000{
1001 /* Nothing here yet .. */
1002 return 0;
1003}
1004
1005static void sci_config_port(struct uart_port *port, int flags)
1006{
Magnus Damma5660ad2009-01-21 15:14:38 +00001007 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 port->type = s->type;
1010
Magnus Damm08f8cb32009-01-21 15:14:22 +00001011 if (port->membase)
1012 return;
1013
1014 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001015 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001016
1017 if (IS_ERR(port->membase))
1018 dev_err(port->dev, "can't remap port#%d\n", port->line);
1019 } else {
1020 /*
1021 * For the simple (and majority of) cases where we don't
1022 * need to do any remapping, just cast the cookie
1023 * directly.
1024 */
1025 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1030{
Magnus Damma5660ad2009-01-21 15:14:38 +00001031 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Yinghai Lua62c4132008-08-19 20:49:55 -07001033 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -EINVAL;
1035 if (ser->baud_base < 2400)
1036 /* No paper tape reader for Mitch.. */
1037 return -EINVAL;
1038
1039 return 0;
1040}
1041
1042static struct uart_ops sci_uart_ops = {
1043 .tx_empty = sci_tx_empty,
1044 .set_mctrl = sci_set_mctrl,
1045 .get_mctrl = sci_get_mctrl,
1046 .start_tx = sci_start_tx,
1047 .stop_tx = sci_stop_tx,
1048 .stop_rx = sci_stop_rx,
1049 .enable_ms = sci_enable_ms,
1050 .break_ctl = sci_break_ctl,
1051 .startup = sci_startup,
1052 .shutdown = sci_shutdown,
1053 .set_termios = sci_set_termios,
1054 .type = sci_type,
1055 .release_port = sci_release_port,
1056 .request_port = sci_request_port,
1057 .config_port = sci_config_port,
1058 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001059#ifdef CONFIG_CONSOLE_POLL
1060 .poll_get_char = sci_poll_get_char,
1061 .poll_put_char = sci_poll_put_char,
1062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063};
1064
Magnus Damm501b8252009-01-21 15:14:30 +00001065static void __devinit sci_init_single(struct platform_device *dev,
1066 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001067 unsigned int index,
1068 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001069{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001070 sci_port->port.ops = &sci_uart_ops;
1071 sci_port->port.iotype = UPIO_MEM;
1072 sci_port->port.line = index;
1073 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001074
1075#if defined(__H8300H__) || defined(__H8300S__)
1076#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001077 sci_port->enable = h8300_sci_enable;
1078 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001080 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001081#elif defined(CONFIG_HAVE_CLK)
Magnus Damm501b8252009-01-21 15:14:30 +00001082 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
1083 sci_port->dclk = clk_get(&dev->dev, "module_clk");
1084 sci_port->enable = sci_clk_enable;
1085 sci_port->disable = sci_clk_disable;
Paul Mundta2159b52008-10-02 19:09:13 +09001086#else
1087#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001088#endif
1089
Magnus Damm7ed7e072009-01-21 15:14:14 +00001090 sci_port->break_timer.data = (unsigned long)sci_port;
1091 sci_port->break_timer.function = sci_break_timer;
1092 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001093
Magnus Damm7ed7e072009-01-21 15:14:14 +00001094 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001095 sci_port->port.membase = p->membase;
1096
1097 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1098 sci_port->port.flags = p->flags;
Magnus Damm501b8252009-01-21 15:14:30 +00001099 sci_port->port.dev = &dev->dev;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001100 sci_port->type = sci_port->port.type = p->type;
1101
1102 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Magnus Damm501b8252009-01-21 15:14:30 +00001103
Paul Mundte108b2c2006-09-27 16:32:13 +09001104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001107static struct tty_driver *serial_console_device(struct console *co, int *index)
1108{
1109 struct uart_driver *p = &sci_uart_driver;
1110 *index = co->index;
1111 return p->tty_driver;
1112}
1113
1114static void serial_console_putchar(struct uart_port *port, int ch)
1115{
1116 sci_poll_put_char(port, ch);
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*
1120 * Print a string to the serial port trying not to disturb
1121 * any possible real use of the port...
1122 */
1123static void serial_console_write(struct console *co, const char *s,
1124 unsigned count)
1125{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001126 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001127 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001128 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001129
Magnus Damm501b8252009-01-21 15:14:30 +00001130 if (sci_port->enable)
1131 sci_port->enable(port);
1132
1133 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001134
1135 /* wait until fifo is empty and last bit has been transmitted */
1136 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1137 while ((sci_in(port, SCxSR) & bits) != bits)
1138 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001139
1140 if (sci_port->disable);
1141 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static int __init serial_console_setup(struct console *co, char *options)
1145{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001146 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct uart_port *port;
1148 int baud = 115200;
1149 int bits = 8;
1150 int parity = 'n';
1151 int flow = 'n';
1152 int ret;
1153
Paul Mundte108b2c2006-09-27 16:32:13 +09001154 /*
1155 * Check whether an invalid uart number has been specified, and
1156 * if so, search for the first available port that does have
1157 * console support.
1158 */
1159 if (co->index >= SCI_NPORTS)
1160 co->index = 0;
1161
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001162 sci_port = &sci_ports[co->index];
1163 port = &sci_port->port;
1164 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001167 * Also need to check port->type, we don't actually have any
1168 * UPIO_PORT ports, but uart_report_port() handily misreports
1169 * it anyways if we don't have a port available by the time this is
1170 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001172 if (!port->type)
1173 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001174
Magnus Damm08f8cb32009-01-21 15:14:22 +00001175 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001176
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001177 if (sci_port->enable)
1178 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (options)
1181 uart_parse_options(options, &baud, &parity, &bits, &flow);
1182
1183 ret = uart_set_options(port, co, baud, parity, bits, flow);
1184#if defined(__H8300H__) || defined(__H8300S__)
1185 /* disable rx interrupt */
1186 if (ret == 0)
1187 sci_stop_rx(port);
1188#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001189 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return ret;
1191}
1192
1193static struct console serial_console = {
1194 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001195 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 .write = serial_console_write,
1197 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001198 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200};
1201
1202static int __init sci_console_init(void)
1203{
1204 register_console(&serial_console);
1205 return 0;
1206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207console_initcall(sci_console_init);
1208#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1209
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001210#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001211#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001213#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#endif
1215
1216static char banner[] __initdata =
1217 KERN_INFO "SuperH SCI(F) driver initialized\n";
1218
1219static struct uart_driver sci_uart_driver = {
1220 .owner = THIS_MODULE,
1221 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 .dev_name = "ttySC",
1223 .major = SCI_MAJOR,
1224 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001225 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .cons = SCI_CONSOLE,
1227};
1228
Magnus Damme552de22009-01-21 15:13:42 +00001229
1230static int __devexit sci_remove(struct platform_device *dev)
1231{
1232 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1233 struct sci_port *p;
1234 unsigned long flags;
1235
1236#ifdef CONFIG_HAVE_CLK
1237 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1238#endif
1239
1240 spin_lock_irqsave(&priv->lock, flags);
1241 list_for_each_entry(p, &priv->ports, node)
1242 uart_remove_one_port(&sci_uart_driver, &p->port);
1243
1244 spin_unlock_irqrestore(&priv->lock, flags);
1245
1246 kfree(priv);
1247 return 0;
1248}
1249
Magnus Damm0ee70712009-01-21 15:13:50 +00001250static int __devinit sci_probe_single(struct platform_device *dev,
1251 unsigned int index,
1252 struct plat_sci_port *p,
1253 struct sci_port *sciport)
1254{
1255 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1256 unsigned long flags;
1257 int ret;
1258
1259 /* Sanity check */
1260 if (unlikely(index >= SCI_NPORTS)) {
1261 dev_notice(&dev->dev, "Attempting to register port "
1262 "%d when only %d are available.\n",
1263 index+1, SCI_NPORTS);
1264 dev_notice(&dev->dev, "Consider bumping "
1265 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1266 return 0;
1267 }
1268
Magnus Damm501b8252009-01-21 15:14:30 +00001269 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001270
1271 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001272 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001273 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001274
1275 INIT_LIST_HEAD(&sciport->node);
1276
1277 spin_lock_irqsave(&priv->lock, flags);
1278 list_add(&sciport->node, &priv->ports);
1279 spin_unlock_irqrestore(&priv->lock, flags);
1280
1281 return 0;
1282}
1283
Paul Mundte108b2c2006-09-27 16:32:13 +09001284/*
1285 * Register a set of serial devices attached to a platform device. The
1286 * list is terminated with a zero flags entry, which means we expect
1287 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1288 * remapping (such as sh64) should also set UPF_IOREMAP.
1289 */
1290static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Paul Mundte108b2c2006-09-27 16:32:13 +09001292 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001293 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001294 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001295
1296 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1297 if (!priv)
1298 return -ENOMEM;
1299
1300 INIT_LIST_HEAD(&priv->ports);
1301 spin_lock_init(&priv->lock);
1302 platform_set_drvdata(dev, priv);
1303
1304#ifdef CONFIG_HAVE_CLK
1305 priv->clk_nb.notifier_call = sci_notifier;
1306 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Magnus Damm0ee70712009-01-21 15:13:50 +00001309 if (dev->id != -1) {
1310 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1311 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001312 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001313 } else {
1314 for (i = 0; p && p->flags != 0; p++, i++) {
1315 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1316 if (ret)
1317 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001318 }
Magnus Damme552de22009-01-21 15:13:42 +00001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321#ifdef CONFIG_SH_STANDARD_BIOS
1322 sh_bios_gdb_detach();
1323#endif
1324
Paul Mundte108b2c2006-09-27 16:32:13 +09001325 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001326
1327err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001328 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001329 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001330}
1331
Paul Mundte108b2c2006-09-27 16:32:13 +09001332static int sci_suspend(struct platform_device *dev, pm_message_t state)
1333{
Magnus Damme552de22009-01-21 15:13:42 +00001334 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1335 struct sci_port *p;
1336 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001337
Magnus Damme552de22009-01-21 15:13:42 +00001338 spin_lock_irqsave(&priv->lock, flags);
1339 list_for_each_entry(p, &priv->ports, node)
1340 uart_suspend_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001341
Magnus Damme552de22009-01-21 15:13:42 +00001342 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001343
1344 return 0;
1345}
1346
1347static int sci_resume(struct platform_device *dev)
1348{
Magnus Damme552de22009-01-21 15:13:42 +00001349 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1350 struct sci_port *p;
1351 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001352
Magnus Damme552de22009-01-21 15:13:42 +00001353 spin_lock_irqsave(&priv->lock, flags);
1354 list_for_each_entry(p, &priv->ports, node)
1355 uart_resume_port(&sci_uart_driver, &p->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001356
Magnus Damme552de22009-01-21 15:13:42 +00001357 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001358
1359 return 0;
1360}
1361
1362static struct platform_driver sci_driver = {
1363 .probe = sci_probe,
1364 .remove = __devexit_p(sci_remove),
1365 .suspend = sci_suspend,
1366 .resume = sci_resume,
1367 .driver = {
1368 .name = "sh-sci",
1369 .owner = THIS_MODULE,
1370 },
1371};
1372
1373static int __init sci_init(void)
1374{
1375 int ret;
1376
1377 printk(banner);
1378
Paul Mundte108b2c2006-09-27 16:32:13 +09001379 ret = uart_register_driver(&sci_uart_driver);
1380 if (likely(ret == 0)) {
1381 ret = platform_driver_register(&sci_driver);
1382 if (unlikely(ret))
1383 uart_unregister_driver(&sci_uart_driver);
1384 }
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return ret;
1387}
1388
1389static void __exit sci_exit(void)
1390{
Paul Mundte108b2c2006-09-27 16:32:13 +09001391 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 uart_unregister_driver(&sci_uart_driver);
1393}
1394
1395module_init(sci_init);
1396module_exit(sci_exit);
1397
Paul Mundte108b2c2006-09-27 16:32:13 +09001398MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001399MODULE_ALIAS("platform:sh-sci");