blob: 0251077693d13df22e347373f6b71c069314b9aa [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>
Paul Mundt85f094e2008-04-25 16:04:20 +090050
51#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080052#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090053#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "sh-sci.h"
57
Paul Mundte108b2c2006-09-27 16:32:13 +090058struct sci_port {
59 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Paul Mundte108b2c2006-09-27 16:32:13 +090061 /* Port type */
62 unsigned int type;
63
64 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090065 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090066
67 /* Port pin configuration */
68 void (*init_pins)(struct uart_port *port,
69 unsigned int cflag);
70
71 /* Port enable callback */
72 void (*enable)(struct uart_port *port);
73
74 /* Port disable callback */
75 void (*disable)(struct uart_port *port);
76
77 /* Break timer */
78 struct timer_list break_timer;
79 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090080
Paul Mundta2159b52008-10-02 19:09:13 +090081#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +090082 /* Port clock */
83 struct clk *clk;
Paul Mundt005a3362007-04-26 11:45:32 +090084#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Paul Mundte108b2c2006-09-27 16:32:13 +090088static struct sci_port *serial_console_port;
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010092static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Paul Mundte108b2c2006-09-27 16:32:13 +090094#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
95
96static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static struct uart_driver sci_uart_driver;
98
Michael Trimarchie7c98dc2008-11-13 18:18:35 +090099static inline struct sci_port *
100to_sci_port(struct uart_port *uart)
101{
102 return container_of(uart, struct sci_port, port);
103}
104
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900105#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundte108b2c2006-09-27 16:32:13 +0900106static inline void handle_error(struct uart_port *port)
107{
108 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
110}
111
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900112static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned short status;
115 int c;
116
Paul Mundte108b2c2006-09-27 16:32:13 +0900117 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 status = sci_in(port, SCxSR);
119 if (status & SCxSR_ERRORS(port)) {
120 handle_error(port);
121 continue;
122 }
123 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900126
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900127 /* Dummy read */
128 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return c;
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900134static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned short status;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 do {
139 status = sci_in(port, SCxSR);
140 } while (!(status & SCxSR_TDxE(port)));
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sci_in(port, SCxSR); /* Dummy read */
143 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900144 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900146#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#if defined(__H8300S__)
149enum { sci_disable, sci_enable };
150
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900151static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900153 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int ch = (port->mapbase - SMR0) >> 3;
155 unsigned char mask = 1 << (ch+1);
156
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900157 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900159 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Paul Mundte108b2c2006-09-27 16:32:13 +0900162
163static inline void h8300_sci_enable(struct uart_port *port)
164{
165 h8300_sci_config(port, sci_enable);
166}
167
168static inline void h8300_sci_disable(struct uart_port *port)
169{
170 h8300_sci_config(port, sci_disable);
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
Paul Mundt15c73aa2008-10-02 19:47:12 +0900174#if defined(__H8300H__) || defined(__H8300S__)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900175static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int ch = (port->mapbase - SMR0) >> 3;
178
179 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900180 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
181 h8300_sci_pins[ch].rx,
182 H8300_GPIO_INPUT);
183 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
184 h8300_sci_pins[ch].tx,
185 H8300_GPIO_OUTPUT);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* tx mark output*/
188 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
189}
Paul Mundte108b2c2006-09-27 16:32:13 +0900190#else
191#define sci_init_pins_sci NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900193
194#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
195static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
196{
197 unsigned int fcr_val = 0;
198
199 if (cflag & CRTSCTS)
200 fcr_val |= SCFCR_MCE;
201
202 sci_out(port, SCFCR, fcr_val);
203}
204#else
205#define sci_init_pins_irda NULL
206#endif
207
Magnus Dammd89ddd12007-07-25 11:42:56 +0900208#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900209static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900210{
211 unsigned int fcr_val = 0;
212
213 set_sh771x_scif_pfc(port);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900214 if (cflag & CRTSCTS)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900215 fcr_val |= SCFCR_MCE;
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900216 sci_out(port, SCFCR, fcr_val);
217}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900218#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
220{
221 unsigned int fcr_val = 0;
222 unsigned short data;
223
224 if (cflag & CRTSCTS) {
225 /* enable RTS/CTS */
226 if (port->mapbase == 0xa4430000) { /* SCIF0 */
227 /* Clear PTCR bit 9-2; enable all scif pins but sck */
228 data = ctrl_inw(PORT_PTCR);
229 ctrl_outw((data & 0xfc03), PORT_PTCR);
230 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
231 /* Clear PVCR bit 9-2 */
232 data = ctrl_inw(PORT_PVCR);
233 ctrl_outw((data & 0xfc03), PORT_PVCR);
234 }
235 fcr_val |= SCFCR_MCE;
236 } else {
237 if (port->mapbase == 0xa4430000) { /* SCIF0 */
238 /* Clear PTCR bit 5-2; enable only tx and rx */
239 data = ctrl_inw(PORT_PTCR);
240 ctrl_outw((data & 0xffc3), PORT_PTCR);
241 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
242 /* Clear PVCR bit 5-2 */
243 data = ctrl_inw(PORT_PVCR);
244 ctrl_outw((data & 0xffc3), PORT_PVCR);
245 }
246 }
247 sci_out(port, SCFCR, fcr_val);
248}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800249#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900250/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
252{
253 unsigned int fcr_val = 0;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800254 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Paul Mundtb7a76e42006-02-01 03:06:06 -0800256 /* We need to set SCPCR to enable RTS/CTS */
257 data = ctrl_inw(SCPCR);
258 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
259 ctrl_outw(data & 0x0fcf, SCPCR);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (cflag & CRTSCTS)
262 fcr_val |= SCFCR_MCE;
263 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* We need to set SCPCR to enable RTS/CTS */
265 data = ctrl_inw(SCPCR);
266 /* Clear out SCP7MD1,0, SCP4MD1,0,
267 Set SCP6MD1,0 = {01} (output) */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800268 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 data = ctrl_inb(SCPDR);
271 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800272 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Paul Mundtb7a76e42006-02-01 03:06:06 -0800274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 sci_out(port, SCFCR, fcr_val);
276}
Paul Mundt41504c32006-12-11 20:28:03 +0900277#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
278static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
279{
280 unsigned int fcr_val = 0;
Magnus Damm346b7462008-04-23 21:25:29 +0900281 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900282
Magnus Damm346b7462008-04-23 21:25:29 +0900283 if (port->mapbase == 0xffe00000) {
284 data = ctrl_inw(PSCR);
285 data &= ~0x03cf;
286 if (cflag & CRTSCTS)
287 fcr_val |= SCFCR_MCE;
288 else
289 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900290
Magnus Damm346b7462008-04-23 21:25:29 +0900291 ctrl_outw(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900292 }
Magnus Damm346b7462008-04-23 21:25:29 +0900293 /* SCIF1 and SCIF2 should be setup by board code */
Paul Mundt41504c32006-12-11 20:28:03 +0900294
295 sci_out(port, SCFCR, fcr_val);
296}
Paul Mundt178dd0c2008-04-09 17:56:18 +0900297#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
298static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
299{
300 /* Nothing to do here.. */
301 sci_out(port, SCFCR, 0);
302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/* For SH7750 */
305static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
306{
307 unsigned int fcr_val = 0;
308
309 if (cflag & CRTSCTS) {
310 fcr_val |= SCFCR_MCE;
311 } else {
Magnus Damm9109a302008-02-08 17:31:24 +0900312#if defined(CONFIG_CPU_SUBTYPE_SH7343) || defined(CONFIG_CPU_SUBTYPE_SH7366)
Paul Mundte108b2c2006-09-27 16:32:13 +0900313 /* Nothing */
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900314#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
315 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900316 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
317 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtb7a76e42006-02-01 03:06:06 -0800318 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
319#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 sci_out(port, SCFCR, fcr_val);
324}
Paul Mundte108b2c2006-09-27 16:32:13 +0900325#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Paul Mundt32351a22007-03-12 14:38:59 +0900327#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
328 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
329 defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundte108b2c2006-09-27 16:32:13 +0900330static inline int scif_txroom(struct uart_port *port)
331{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900332 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900333}
334
335static inline int scif_rxroom(struct uart_port *port)
336{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900337 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900338}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900339#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
340static inline int scif_txroom(struct uart_port *port)
341{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900342 if ((port->mapbase == 0xffe00000) ||
343 (port->mapbase == 0xffe08000)) {
344 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900345 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900346 } else {
347 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900348 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900349 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900350}
351
352static inline int scif_rxroom(struct uart_port *port)
353{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900354 if ((port->mapbase == 0xffe00000) ||
355 (port->mapbase == 0xffe08000)) {
356 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900357 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900358 } else {
359 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900360 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900361 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900362}
Paul Mundte108b2c2006-09-27 16:32:13 +0900363#else
364static inline int scif_txroom(struct uart_port *port)
365{
366 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
367}
368
369static inline int scif_rxroom(struct uart_port *port)
370{
371 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Paul Mundte108b2c2006-09-27 16:32:13 +0900375static inline int sci_txroom(struct uart_port *port)
376{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900377 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900378}
379
380static inline int sci_rxroom(struct uart_port *port)
381{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900382 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/* ********************************************************************** *
386 * the interrupt related routines *
387 * ********************************************************************** */
388
389static void sci_transmit_chars(struct uart_port *port)
390{
391 struct circ_buf *xmit = &port->info->xmit;
392 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned short status;
394 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900395 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 status = sci_in(port, SCxSR);
398 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900400 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900402 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return;
406 }
407
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900408 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900409 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900410 else
411 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 do {
414 unsigned char c;
415
416 if (port->x_char) {
417 c = port->x_char;
418 port->x_char = 0;
419 } else if (!uart_circ_empty(xmit) && !stopped) {
420 c = xmit->buf[xmit->tail];
421 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
422 } else {
423 break;
424 }
425
426 sci_out(port, SCxTDR, c);
427
428 port->icount.tx++;
429 } while (--count > 0);
430
431 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
432
433 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
434 uart_write_wakeup(port);
435 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100436 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ctrl = sci_in(port, SCSCR);
439
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900440 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sci_in(port, SCxSR); /* Dummy read */
442 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 ctrl |= SCI_CTRL_FLAGS_TIE;
446 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448}
449
450/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900451#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
David Howells7d12e782006-10-05 14:55:46 +0100453static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900455 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100456 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int i, count, copied = 0;
458 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800459 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 status = sci_in(port, SCxSR);
462 if (!(status & SCxSR_RDxF(port)))
463 return;
464
465 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900466 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900467 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900468 else
469 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800472 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* If for any reason we can't copy more data, we're done! */
475 if (count == 0)
476 break;
477
478 if (port->type == PORT_SCI) {
479 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900480 if (uart_handle_sysrq_char(port, c) ||
481 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900483 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900484 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900486 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 char c = sci_in(port, SCxRDR);
488 status = sci_in(port, SCxSR);
489#if defined(CONFIG_CPU_SH3)
490 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900491 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if ((c == 0) &&
493 (status & SCxSR_FER(port))) {
494 count--; i--;
495 continue;
496 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900499 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900500 sci_port->break_flag = 0;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (STEPFN(c)) {
503 count--; i--;
504 continue;
505 }
506 }
507#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100508 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 count--; i--;
510 continue;
511 }
512
513 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800515 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900516 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800518 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900519 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800520 } else
521 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900522
Alan Cox33f0f882006-01-09 20:54:13 -0800523 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
526
527 sci_in(port, SCxSR); /* dummy read */
528 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 copied += count;
531 port->icount.rx += count;
532 }
533
534 if (copied) {
535 /* Tell the rest of the system the news. New characters! */
536 tty_flip_buffer_push(tty);
537 } else {
538 sci_in(port, SCxSR); /* dummy read */
539 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
540 }
541}
542
543#define SCI_BREAK_JIFFIES (HZ/20)
544/* The sci generates interrupts during the break,
545 * 1 per millisecond or so during the break period, for 9600 baud.
546 * So dont bother disabling interrupts.
547 * But dont want more than 1 break event.
548 * Use a kernel timer to periodically poll the rx line until
549 * the break is finished.
550 */
551static void sci_schedule_break_timer(struct sci_port *port)
552{
553 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
554 add_timer(&port->break_timer);
555}
556/* Ensure that two consecutive samples find the break over. */
557static void sci_break_timer(unsigned long data)
558{
Paul Mundte108b2c2006-09-27 16:32:13 +0900559 struct sci_port *port = (struct sci_port *)data;
560
561 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900563 sci_schedule_break_timer(port);
564 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* break is over. */
566 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 sci_schedule_break_timer(port);
568 } else
569 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static inline int sci_handle_errors(struct uart_port *port)
573{
574 int copied = 0;
575 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100576 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Paul Mundte108b2c2006-09-27 16:32:13 +0900578 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900580 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800581 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900582
583 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Paul Mundte108b2c2006-09-27 16:32:13 +0900586 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (sci_rxd_in(port) == 0) {
588 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900589 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900590
591 if (!sci_port->break_flag) {
592 sci_port->break_flag = 1;
593 sci_schedule_break_timer(sci_port);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900596 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900598
599 dev_dbg(port->dev, "BREAK detected\n");
600
Paul Mundte108b2c2006-09-27 16:32:13 +0900601 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900602 copied++;
603 }
604
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900607 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800608 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900609
610 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612 }
613
Paul Mundte108b2c2006-09-27 16:32:13 +0900614 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900616 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
617 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900618
619 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Alan Cox33f0f882006-01-09 20:54:13 -0800622 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return copied;
626}
627
628static inline int sci_handle_breaks(struct uart_port *port)
629{
630 int copied = 0;
631 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100632 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct sci_port *s = &sci_ports[port->line];
634
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900635 if (uart_handle_break(port))
636 return 0;
637
Paul Mundtb7a76e42006-02-01 03:06:06 -0800638 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#if defined(CONFIG_CPU_SH3)
640 /* Debounce break */
641 s->break_flag = 1;
642#endif
643 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900644 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800645 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900646
647 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650#if defined(SCIF_ORER)
651 /* XXX: Handle SCIF overrun error */
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900652 if (port->type != PORT_SCI && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sci_out(port, SCLSR, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +0900654 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900656 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 }
659#endif
660
Alan Cox33f0f882006-01-09 20:54:13 -0800661 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return copied;
665}
666
David Howells7d12e782006-10-05 14:55:46 +0100667static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* I think sci_receive_chars has to be called irrespective
670 * of whether the I_IXOFF is set, otherwise, how is the interrupt
671 * to be disabled?
672 */
David Howells7d12e782006-10-05 14:55:46 +0100673 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 return IRQ_HANDLED;
676}
677
David Howells7d12e782006-10-05 14:55:46 +0100678static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct uart_port *port = ptr;
681
Paul Mundte108b2c2006-09-27 16:32:13 +0900682 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900684 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 return IRQ_HANDLED;
687}
688
David Howells7d12e782006-10-05 14:55:46 +0100689static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct uart_port *port = ptr;
692
693 /* Handle errors */
694 if (port->type == PORT_SCI) {
695 if (sci_handle_errors(port)) {
696 /* discard character in rx buffer */
697 sci_in(port, SCxSR);
698 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
699 }
700 } else {
701#if defined(SCIF_ORER)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900702 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100703 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 sci_out(port, SCLSR, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800706 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
707 tty_flip_buffer_push(tty);
Paul Mundt762c69e2008-12-16 18:55:26 +0900708 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710#endif
David Howells7d12e782006-10-05 14:55:46 +0100711 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
714 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
715
716 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100717 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return IRQ_HANDLED;
720}
721
David Howells7d12e782006-10-05 14:55:46 +0100722static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct uart_port *port = ptr;
725
726 /* Handle BREAKs */
727 sci_handle_breaks(port);
728 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
729
730 return IRQ_HANDLED;
731}
732
David Howells7d12e782006-10-05 14:55:46 +0100733static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900735 unsigned short ssr_status, scr_status;
736 struct uart_port *port = ptr;
737 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900739 ssr_status = sci_in(port, SCxSR);
740 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900743 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
744 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900746 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
747 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900749 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
750 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900752 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
753 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Michael Trimarchia8884e32008-10-31 16:10:23 +0900755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Paul Mundt027e6872008-12-16 18:36:16 +0900758#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * Here we define a transistion notifier so that we can update all of our
761 * ports' baud rate when the peripheral clock changes.
762 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900763static int sci_notifier(struct notifier_block *self,
764 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int i;
767
768 if ((phase == CPUFREQ_POSTCHANGE) ||
Paul Mundt027e6872008-12-16 18:36:16 +0900769 (phase == CPUFREQ_RESUMECHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 for (i = 0; i < SCI_NPORTS; i++) {
Paul Mundt027e6872008-12-16 18:36:16 +0900771 struct sci_port *s = &sci_ports[i];
772 s->port.uartclk = clk_get_rate(s->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return NOTIFY_OK;
776}
777
778static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
Paul Mundt027e6872008-12-16 18:36:16 +0900779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781static int sci_request_irq(struct sci_port *port)
782{
783 int i;
David Howells7d12e782006-10-05 14:55:46 +0100784 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
786 sci_br_interrupt,
787 };
788 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
789 "SCI Transmit Data Empty", "SCI Break" };
790
791 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900792 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900794
795 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900796 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900797 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -ENODEV;
799 }
800 } else {
801 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900802 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900804
Paul Mundte108b2c2006-09-27 16:32:13 +0900805 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900806 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900807 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -ENODEV;
809 }
810 }
811 }
812
813 return 0;
814}
815
816static void sci_free_irq(struct sci_port *port)
817{
818 int i;
819
Paul Mundt762c69e2008-12-16 18:55:26 +0900820 if (port->irqs[0] == port->irqs[1])
821 free_irq(port->irqs[0], port);
822 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
824 if (!port->irqs[i])
825 continue;
826
827 free_irq(port->irqs[i], port);
828 }
829 }
830}
831
832static unsigned int sci_tx_empty(struct uart_port *port)
833{
834 /* Can't detect */
835 return TIOCSER_TEMT;
836}
837
838static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
839{
840 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
841 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
842 /* If you have signals for DTR and DCD, please implement here. */
843}
844
845static unsigned int sci_get_mctrl(struct uart_port *port)
846{
847 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
848 and CTS/RTS */
849
850 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
851}
852
Russell Kingb129a8c2005-08-31 10:12:14 +0100853static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Paul Mundte108b2c2006-09-27 16:32:13 +0900855 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Paul Mundte108b2c2006-09-27 16:32:13 +0900857 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
858 ctrl = sci_in(port, SCSCR);
859 ctrl |= SCI_CTRL_FLAGS_TIE;
860 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Russell Kingb129a8c2005-08-31 10:12:14 +0100863static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned short ctrl;
866
867 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 ctrl = sci_in(port, SCSCR);
869 ctrl &= ~SCI_CTRL_FLAGS_TIE;
870 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned short ctrl;
876
877 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ctrl = sci_in(port, SCSCR);
879 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
880 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void sci_stop_rx(struct uart_port *port)
884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned short ctrl;
886
887 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ctrl = sci_in(port, SCSCR);
889 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
890 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static void sci_enable_ms(struct uart_port *port)
894{
895 /* Nothing here yet .. */
896}
897
898static void sci_break_ctl(struct uart_port *port, int break_state)
899{
900 /* Nothing here yet .. */
901}
902
903static int sci_startup(struct uart_port *port)
904{
905 struct sci_port *s = &sci_ports[port->line];
906
Paul Mundte108b2c2006-09-27 16:32:13 +0900907 if (s->enable)
908 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Paul Mundta2159b52008-10-02 19:09:13 +0900910#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900911 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +0900912#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700915 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sci_start_rx(port, 1);
917
918 return 0;
919}
920
921static void sci_shutdown(struct uart_port *port)
922{
923 struct sci_port *s = &sci_ports[port->line];
924
925 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100926 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 sci_free_irq(s);
928
Paul Mundte108b2c2006-09-27 16:32:13 +0900929 if (s->disable)
930 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900931
Paul Mundta2159b52008-10-02 19:09:13 +0900932#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900933 clk_put(s->clk);
934 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +0900935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Alan Cox606d0992006-12-08 02:38:45 -0800938static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
939 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct sci_port *s = &sci_ports[port->line];
942 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900943 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900946 if (likely(baud))
947 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Paul Mundte108b2c2006-09-27 16:32:13 +0900949 do {
950 status = sci_in(port, SCxSR);
951 } while (!(status & SCxSR_TEND(port)));
952
953 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
954
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900955 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900956 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900957
958 smr_val = sci_in(port, SCSMR) & 3;
959 if ((termios->c_cflag & CSIZE) == CS7)
960 smr_val |= 0x40;
961 if (termios->c_cflag & PARENB)
962 smr_val |= 0x20;
963 if (termios->c_cflag & PARODD)
964 smr_val |= 0x30;
965 if (termios->c_cflag & CSTOPB)
966 smr_val |= 0x08;
967
968 uart_update_timeout(port, termios->c_cflag, baud);
969
970 sci_out(port, SCSMR, smr_val);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900973 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
975 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900976 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sci_out(port, SCBRR, t);
980 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
981 }
982
Paul Mundtb7a76e42006-02-01 03:06:06 -0800983 if (likely(s->init_pins))
984 s->init_pins(port, termios->c_cflag);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sci_out(port, SCSCR, SCSCR_INIT(port));
987
988 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900989 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static const char *sci_type(struct uart_port *port)
993{
994 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900995 case PORT_IRDA:
996 return "irda";
997 case PORT_SCI:
998 return "sci";
999 case PORT_SCIF:
1000 return "scif";
1001 case PORT_SCIFA:
1002 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Paul Mundtfa439722008-09-04 18:53:58 +09001005 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static void sci_release_port(struct uart_port *port)
1009{
1010 /* Nothing here yet .. */
1011}
1012
1013static int sci_request_port(struct uart_port *port)
1014{
1015 /* Nothing here yet .. */
1016 return 0;
1017}
1018
1019static void sci_config_port(struct uart_port *port, int flags)
1020{
1021 struct sci_port *s = &sci_ports[port->line];
1022
1023 port->type = s->type;
1024
Paul Mundte108b2c2006-09-27 16:32:13 +09001025 switch (port->type) {
1026 case PORT_SCI:
1027 s->init_pins = sci_init_pins_sci;
1028 break;
1029 case PORT_SCIF:
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001030 case PORT_SCIFA:
Paul Mundte108b2c2006-09-27 16:32:13 +09001031 s->init_pins = sci_init_pins_scif;
1032 break;
1033 case PORT_IRDA:
1034 s->init_pins = sci_init_pins_irda;
1035 break;
1036 }
1037
Paul Mundt7ff731a2008-10-01 15:46:58 +09001038 if (port->flags & UPF_IOREMAP && !port->membase) {
1039#if defined(CONFIG_SUPERH64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
Paul Mundt7ff731a2008-10-01 15:46:58 +09001041 port->membase = (void __iomem *)port->mapbase;
1042#else
1043 port->membase = ioremap_nocache(port->mapbase, 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#endif
Paul Mundt7ff731a2008-10-01 15:46:58 +09001045
Paul Mundt762c69e2008-12-16 18:55:26 +09001046 dev_err(port->dev, "can't remap port#%d\n", port->line);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1051{
1052 struct sci_port *s = &sci_ports[port->line];
1053
Yinghai Lua62c4132008-08-19 20:49:55 -07001054 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -EINVAL;
1056 if (ser->baud_base < 2400)
1057 /* No paper tape reader for Mitch.. */
1058 return -EINVAL;
1059
1060 return 0;
1061}
1062
1063static struct uart_ops sci_uart_ops = {
1064 .tx_empty = sci_tx_empty,
1065 .set_mctrl = sci_set_mctrl,
1066 .get_mctrl = sci_get_mctrl,
1067 .start_tx = sci_start_tx,
1068 .stop_tx = sci_stop_tx,
1069 .stop_rx = sci_stop_rx,
1070 .enable_ms = sci_enable_ms,
1071 .break_ctl = sci_break_ctl,
1072 .startup = sci_startup,
1073 .shutdown = sci_shutdown,
1074 .set_termios = sci_set_termios,
1075 .type = sci_type,
1076 .release_port = sci_release_port,
1077 .request_port = sci_request_port,
1078 .config_port = sci_config_port,
1079 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001080#ifdef CONFIG_CONSOLE_POLL
1081 .poll_get_char = sci_poll_get_char,
1082 .poll_put_char = sci_poll_put_char,
1083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084};
1085
Paul Mundte108b2c2006-09-27 16:32:13 +09001086static void __init sci_init_ports(void)
1087{
1088 static int first = 1;
1089 int i;
1090
1091 if (!first)
1092 return;
1093
1094 first = 0;
1095
1096 for (i = 0; i < SCI_NPORTS; i++) {
1097 sci_ports[i].port.ops = &sci_uart_ops;
1098 sci_ports[i].port.iotype = UPIO_MEM;
1099 sci_ports[i].port.line = i;
1100 sci_ports[i].port.fifosize = 1;
1101
1102#if defined(__H8300H__) || defined(__H8300S__)
1103#ifdef __H8300S__
1104 sci_ports[i].enable = h8300_sci_enable;
1105 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001107 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001108#elif defined(CONFIG_HAVE_CLK)
Paul Mundte108b2c2006-09-27 16:32:13 +09001109 /*
1110 * XXX: We should use a proper SCI/SCIF clock
1111 */
1112 {
Paul Mundt1d118562006-12-01 13:15:14 +09001113 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundta2159b52008-10-02 19:09:13 +09001114 sci_ports[i].port.uartclk = clk_get_rate(clk);
Paul Mundte108b2c2006-09-27 16:32:13 +09001115 clk_put(clk);
1116 }
Paul Mundta2159b52008-10-02 19:09:13 +09001117#else
1118#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001119#endif
1120
1121 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1122 sci_ports[i].break_timer.function = sci_break_timer;
1123
1124 init_timer(&sci_ports[i].break_timer);
1125 }
1126}
1127
1128int __init early_sci_setup(struct uart_port *port)
1129{
1130 if (unlikely(port->line > SCI_NPORTS))
1131 return -ENODEV;
1132
1133 sci_init_ports();
1134
1135 sci_ports[port->line].port.membase = port->membase;
1136 sci_ports[port->line].port.mapbase = port->mapbase;
1137 sci_ports[port->line].port.type = port->type;
1138
1139 return 0;
1140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1143/*
1144 * Print a string to the serial port trying not to disturb
1145 * any possible real use of the port...
1146 */
1147static void serial_console_write(struct console *co, const char *s,
1148 unsigned count)
1149{
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001150 struct uart_port *port = &serial_console_port->port;
1151 int i;
1152
1153 for (i = 0; i < count; i++) {
1154 if (*s == 10)
1155 sci_poll_put_char(port, '\r');
1156
1157 sci_poll_put_char(port, *s++);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static int __init serial_console_setup(struct console *co, char *options)
1162{
1163 struct uart_port *port;
1164 int baud = 115200;
1165 int bits = 8;
1166 int parity = 'n';
1167 int flow = 'n';
1168 int ret;
1169
Paul Mundte108b2c2006-09-27 16:32:13 +09001170 /*
1171 * Check whether an invalid uart number has been specified, and
1172 * if so, search for the first available port that does have
1173 * console support.
1174 */
1175 if (co->index >= SCI_NPORTS)
1176 co->index = 0;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 serial_console_port = &sci_ports[co->index];
1179 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001182 * Also need to check port->type, we don't actually have any
1183 * UPIO_PORT ports, but uart_report_port() handily misreports
1184 * it anyways if we don't have a port available by the time this is
1185 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001187 if (!port->type)
1188 return -ENODEV;
1189 if (!port->membase || !port->mapbase)
1190 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001191
Paul Mundte108b2c2006-09-27 16:32:13 +09001192 port->type = serial_console_port->type;
1193
Paul Mundta2159b52008-10-02 19:09:13 +09001194#ifdef CONFIG_HAVE_CLK
Paul Mundt005a3362007-04-26 11:45:32 +09001195 if (!serial_console_port->clk)
1196 serial_console_port->clk = clk_get(NULL, "module_clk");
1197#endif
1198
Paul Mundte108b2c2006-09-27 16:32:13 +09001199 if (port->flags & UPF_IOREMAP)
1200 sci_config_port(port, 0);
1201
1202 if (serial_console_port->enable)
1203 serial_console_port->enable(port);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (options)
1206 uart_parse_options(options, &baud, &parity, &bits, &flow);
1207
1208 ret = uart_set_options(port, co, baud, parity, bits, flow);
1209#if defined(__H8300H__) || defined(__H8300S__)
1210 /* disable rx interrupt */
1211 if (ret == 0)
1212 sci_stop_rx(port);
1213#endif
1214 return ret;
1215}
1216
1217static struct console serial_console = {
1218 .name = "ttySC",
1219 .device = uart_console_device,
1220 .write = serial_console_write,
1221 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001222 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .index = -1,
1224 .data = &sci_uart_driver,
1225};
1226
1227static int __init sci_console_init(void)
1228{
Paul Mundte108b2c2006-09-27 16:32:13 +09001229 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 register_console(&serial_console);
1231 return 0;
1232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233console_initcall(sci_console_init);
1234#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1235
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001236#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001237#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001239#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240#endif
1241
1242static char banner[] __initdata =
1243 KERN_INFO "SuperH SCI(F) driver initialized\n";
1244
1245static struct uart_driver sci_uart_driver = {
1246 .owner = THIS_MODULE,
1247 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 .dev_name = "ttySC",
1249 .major = SCI_MAJOR,
1250 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001251 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .cons = SCI_CONSOLE,
1253};
1254
Paul Mundte108b2c2006-09-27 16:32:13 +09001255/*
1256 * Register a set of serial devices attached to a platform device. The
1257 * list is terminated with a zero flags entry, which means we expect
1258 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1259 * remapping (such as sh64) should also set UPF_IOREMAP.
1260 */
1261static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Paul Mundte108b2c2006-09-27 16:32:13 +09001263 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001264 int i, ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Paul Mundt32351a22007-03-12 14:38:59 +09001266 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001267 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Paul Mundt32351a22007-03-12 14:38:59 +09001269 /* Sanity check */
1270 if (unlikely(i == SCI_NPORTS)) {
1271 dev_notice(&dev->dev, "Attempting to register port "
1272 "%d when only %d are available.\n",
1273 i+1, SCI_NPORTS);
1274 dev_notice(&dev->dev, "Consider bumping "
1275 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1276 break;
1277 }
1278
Paul Mundte108b2c2006-09-27 16:32:13 +09001279 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001280
Paul Mundt7ff731a2008-10-01 15:46:58 +09001281 if (p->mapbase && !p->membase) {
1282 if (p->flags & UPF_IOREMAP) {
1283 p->membase = ioremap_nocache(p->mapbase, 0x40);
1284 if (IS_ERR(p->membase)) {
1285 ret = PTR_ERR(p->membase);
1286 goto err_unreg;
1287 }
1288 } else {
1289 /*
1290 * For the simple (and majority of) cases
1291 * where we don't need to do any remapping,
1292 * just cast the cookie directly.
1293 */
1294 p->membase = (void __iomem *)p->mapbase;
1295 }
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Paul Mundte108b2c2006-09-27 16:32:13 +09001298 sciport->port.membase = p->membase;
1299
1300 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1301 sciport->port.flags = p->flags;
1302 sciport->port.dev = &dev->dev;
1303
1304 sciport->type = sciport->port.type = p->type;
1305
1306 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1307
1308 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Paul Mundt027e6872008-12-16 18:36:16 +09001311#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#endif
1314
1315#ifdef CONFIG_SH_STANDARD_BIOS
1316 sh_bios_gdb_detach();
1317#endif
1318
Paul Mundte108b2c2006-09-27 16:32:13 +09001319 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001320
1321err_unreg:
1322 for (i = i - 1; i >= 0; i--)
1323 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1324
1325 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001326}
1327
1328static int __devexit sci_remove(struct platform_device *dev)
1329{
1330 int i;
1331
Paul Mundt027e6872008-12-16 18:36:16 +09001332#ifdef CONFIG_HAVE_CLK
1333 cpufreq_unregister_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1334#endif
1335
Paul Mundte108b2c2006-09-27 16:32:13 +09001336 for (i = 0; i < SCI_NPORTS; i++)
1337 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1338
1339 return 0;
1340}
1341
1342static int sci_suspend(struct platform_device *dev, pm_message_t state)
1343{
1344 int i;
1345
1346 for (i = 0; i < SCI_NPORTS; i++) {
1347 struct sci_port *p = &sci_ports[i];
1348
1349 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1350 uart_suspend_port(&sci_uart_driver, &p->port);
1351 }
1352
1353 return 0;
1354}
1355
1356static int sci_resume(struct platform_device *dev)
1357{
1358 int i;
1359
1360 for (i = 0; i < SCI_NPORTS; i++) {
1361 struct sci_port *p = &sci_ports[i];
1362
1363 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1364 uart_resume_port(&sci_uart_driver, &p->port);
1365 }
1366
1367 return 0;
1368}
1369
1370static struct platform_driver sci_driver = {
1371 .probe = sci_probe,
1372 .remove = __devexit_p(sci_remove),
1373 .suspend = sci_suspend,
1374 .resume = sci_resume,
1375 .driver = {
1376 .name = "sh-sci",
1377 .owner = THIS_MODULE,
1378 },
1379};
1380
1381static int __init sci_init(void)
1382{
1383 int ret;
1384
1385 printk(banner);
1386
1387 sci_init_ports();
1388
1389 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");