blob: 31532e97fb96fc88aa1c86fe9f000db25bb07504 [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>
54#include <asm/kgdb.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
68 /* Port pin configuration */
69 void (*init_pins)(struct uart_port *port,
70 unsigned int cflag);
71
72 /* Port enable callback */
73 void (*enable)(struct uart_port *port);
74
75 /* Port disable callback */
76 void (*disable)(struct uart_port *port);
77
78 /* Break timer */
79 struct timer_list break_timer;
80 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090081
Paul Mundta2159b52008-10-02 19:09:13 +090082#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +090083 /* Port clock */
84 struct clk *clk;
Paul Mundt005a3362007-04-26 11:45:32 +090085#endif
Paul Mundte108b2c2006-09-27 16:32:13 +090086};
87
88#ifdef CONFIG_SH_KGDB
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct sci_port *kgdb_sci_port;
Paul Mundte108b2c2006-09-27 16:32:13 +090090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Paul Mundte108b2c2006-09-27 16:32:13 +090093static struct sci_port *serial_console_port;
94#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* 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 Mundte108b2c2006-09-27 16:32:13 +0900110#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
111 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
112static inline void handle_error(struct uart_port *port)
113{
114 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
116}
117
118static int get_char(struct uart_port *port)
119{
120 unsigned long flags;
121 unsigned short status;
122 int c;
123
Paul Mundte108b2c2006-09-27 16:32:13 +0900124 spin_lock_irqsave(&port->lock, flags);
125 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 status = sci_in(port, SCxSR);
127 if (status & SCxSR_ERRORS(port)) {
128 handle_error(port);
129 continue;
130 }
131 } while (!(status & SCxSR_RDxF(port)));
132 c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900133 /* Dummy read */
134 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Paul Mundte108b2c2006-09-27 16:32:13 +0900136 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return c;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
141
Paul Mundte108b2c2006-09-27 16:32:13 +0900142#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void put_char(struct uart_port *port, char c)
144{
145 unsigned long flags;
146 unsigned short status;
147
Paul Mundte108b2c2006-09-27 16:32:13 +0900148 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 do {
151 status = sci_in(port, SCxSR);
152 } while (!(status & SCxSR_TDxE(port)));
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 sci_in(port, SCxSR); /* Dummy read */
155 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
Paul Mundt272966c2008-11-13 17:46:06 +0900156 sci_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Paul Mundte108b2c2006-09-27 16:32:13 +0900158 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Paul Mundte108b2c2006-09-27 16:32:13 +0900160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Paul Mundte108b2c2006-09-27 16:32:13 +0900162#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void put_string(struct sci_port *sci_port, const char *buffer, int count)
164{
165 struct uart_port *port = &sci_port->port;
166 const unsigned char *p = buffer;
167 int i;
168
169#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
170 int checksum;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900171 int usegdb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtb7a76e42006-02-01 03:06:06 -0800174 /* This call only does a trap the first time it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * called, and so is safe to do here unconditionally
176 */
177 usegdb |= sh_bios_in_gdb_mode();
178#endif
179#ifdef CONFIG_SH_KGDB
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900180 usegdb |= (kgdb_in_gdb_mode && (sci_port == kgdb_sci_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif
182
183 if (usegdb) {
184 /* $<packet info>#<checksum>. */
185 do {
186 unsigned char c;
187 put_char(port, '$');
188 put_char(port, 'O'); /* 'O'utput to console */
189 checksum = 'O';
190
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900191 /* Don't use run length encoding */
192 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int h, l;
194
195 c = *p++;
Harvey Harrisonbfd3c7a2008-05-12 12:05:43 -0700196 h = hex_asc_hi(c);
197 l = hex_asc_lo(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 put_char(port, h);
199 put_char(port, l);
200 checksum += h + l;
201 }
202 put_char(port, '#');
Harvey Harrisonbfd3c7a2008-05-12 12:05:43 -0700203 put_char(port, hex_asc_hi(checksum));
204 put_char(port, hex_asc_lo(checksum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } while (get_char(port) != '+');
206 } else
207#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900208 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (*p == 10)
210 put_char(port, '\r');
211 put_char(port, *p++);
212 }
213}
214#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#ifdef CONFIG_SH_KGDB
Paul Mundte108b2c2006-09-27 16:32:13 +0900217static int kgdb_sci_getchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900219 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900221 /* Keep trying to read a character, this could be neater */
222 while ((c = get_char(&kgdb_sci_port->port)) < 0)
Paul Mundte108b2c2006-09-27 16:32:13 +0900223 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900225 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Paul Mundte108b2c2006-09-27 16:32:13 +0900228static inline void kgdb_sci_putchar(int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900230 put_char(&kgdb_sci_port->port, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#endif /* CONFIG_SH_KGDB */
233
234#if defined(__H8300S__)
235enum { sci_disable, sci_enable };
236
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900237static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900239 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int ch = (port->mapbase - SMR0) >> 3;
241 unsigned char mask = 1 << (ch+1);
242
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900243 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Paul Mundte108b2c2006-09-27 16:32:13 +0900248
249static inline void h8300_sci_enable(struct uart_port *port)
250{
251 h8300_sci_config(port, sci_enable);
252}
253
254static inline void h8300_sci_disable(struct uart_port *port)
255{
256 h8300_sci_config(port, sci_disable);
257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#endif
259
Paul Mundt15c73aa2008-10-02 19:47:12 +0900260#if defined(__H8300H__) || defined(__H8300S__)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900261static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int ch = (port->mapbase - SMR0) >> 3;
264
265 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900266 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
267 h8300_sci_pins[ch].rx,
268 H8300_GPIO_INPUT);
269 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
270 h8300_sci_pins[ch].tx,
271 H8300_GPIO_OUTPUT);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* tx mark output*/
274 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
275}
Paul Mundte108b2c2006-09-27 16:32:13 +0900276#else
277#define sci_init_pins_sci NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900279
280#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
281static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
282{
283 unsigned int fcr_val = 0;
284
285 if (cflag & CRTSCTS)
286 fcr_val |= SCFCR_MCE;
287
288 sci_out(port, SCFCR, fcr_val);
289}
290#else
291#define sci_init_pins_irda NULL
292#endif
293
Magnus Dammd89ddd12007-07-25 11:42:56 +0900294#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900295static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900296{
297 unsigned int fcr_val = 0;
298
299 set_sh771x_scif_pfc(port);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900300 if (cflag & CRTSCTS)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900301 fcr_val |= SCFCR_MCE;
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900302 sci_out(port, SCFCR, fcr_val);
303}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900304#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900305static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
306{
307 unsigned int fcr_val = 0;
308 unsigned short data;
309
310 if (cflag & CRTSCTS) {
311 /* enable RTS/CTS */
312 if (port->mapbase == 0xa4430000) { /* SCIF0 */
313 /* Clear PTCR bit 9-2; enable all scif pins but sck */
314 data = ctrl_inw(PORT_PTCR);
315 ctrl_outw((data & 0xfc03), PORT_PTCR);
316 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
317 /* Clear PVCR bit 9-2 */
318 data = ctrl_inw(PORT_PVCR);
319 ctrl_outw((data & 0xfc03), PORT_PVCR);
320 }
321 fcr_val |= SCFCR_MCE;
322 } else {
323 if (port->mapbase == 0xa4430000) { /* SCIF0 */
324 /* Clear PTCR bit 5-2; enable only tx and rx */
325 data = ctrl_inw(PORT_PTCR);
326 ctrl_outw((data & 0xffc3), PORT_PTCR);
327 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
328 /* Clear PVCR bit 5-2 */
329 data = ctrl_inw(PORT_PVCR);
330 ctrl_outw((data & 0xffc3), PORT_PVCR);
331 }
332 }
333 sci_out(port, SCFCR, fcr_val);
334}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800335#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900336/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
338{
339 unsigned int fcr_val = 0;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800340 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Paul Mundtb7a76e42006-02-01 03:06:06 -0800342 /* We need to set SCPCR to enable RTS/CTS */
343 data = ctrl_inw(SCPCR);
344 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
345 ctrl_outw(data & 0x0fcf, SCPCR);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (cflag & CRTSCTS)
348 fcr_val |= SCFCR_MCE;
349 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* We need to set SCPCR to enable RTS/CTS */
351 data = ctrl_inw(SCPCR);
352 /* Clear out SCP7MD1,0, SCP4MD1,0,
353 Set SCP6MD1,0 = {01} (output) */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800354 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 data = ctrl_inb(SCPDR);
357 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800358 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Paul Mundtb7a76e42006-02-01 03:06:06 -0800360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 sci_out(port, SCFCR, fcr_val);
362}
Paul Mundt41504c32006-12-11 20:28:03 +0900363#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
364static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
365{
366 unsigned int fcr_val = 0;
Magnus Damm346b7462008-04-23 21:25:29 +0900367 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900368
Magnus Damm346b7462008-04-23 21:25:29 +0900369 if (port->mapbase == 0xffe00000) {
370 data = ctrl_inw(PSCR);
371 data &= ~0x03cf;
372 if (cflag & CRTSCTS)
373 fcr_val |= SCFCR_MCE;
374 else
375 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900376
Magnus Damm346b7462008-04-23 21:25:29 +0900377 ctrl_outw(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900378 }
Magnus Damm346b7462008-04-23 21:25:29 +0900379 /* SCIF1 and SCIF2 should be setup by board code */
Paul Mundt41504c32006-12-11 20:28:03 +0900380
381 sci_out(port, SCFCR, fcr_val);
382}
Paul Mundt178dd0c2008-04-09 17:56:18 +0900383#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
384static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
385{
386 /* Nothing to do here.. */
387 sci_out(port, SCFCR, 0);
388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* For SH7750 */
391static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
392{
393 unsigned int fcr_val = 0;
394
395 if (cflag & CRTSCTS) {
396 fcr_val |= SCFCR_MCE;
397 } else {
Magnus Damm9109a302008-02-08 17:31:24 +0900398#if defined(CONFIG_CPU_SUBTYPE_SH7343) || defined(CONFIG_CPU_SUBTYPE_SH7366)
Paul Mundte108b2c2006-09-27 16:32:13 +0900399 /* Nothing */
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900400#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
401 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900402 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
403 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtb7a76e42006-02-01 03:06:06 -0800404 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
405#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 sci_out(port, SCFCR, fcr_val);
410}
Paul Mundte108b2c2006-09-27 16:32:13 +0900411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Paul Mundt32351a22007-03-12 14:38:59 +0900413#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
414 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
415 defined(CONFIG_CPU_SUBTYPE_SH7785)
Paul Mundte108b2c2006-09-27 16:32:13 +0900416static inline int scif_txroom(struct uart_port *port)
417{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900418 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900419}
420
421static inline int scif_rxroom(struct uart_port *port)
422{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900423 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900424}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900425#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
426static inline int scif_txroom(struct uart_port *port)
427{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900428 if ((port->mapbase == 0xffe00000) ||
429 (port->mapbase == 0xffe08000)) {
430 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900431 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900432 } else {
433 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900434 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900435 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900436}
437
438static inline int scif_rxroom(struct uart_port *port)
439{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900440 if ((port->mapbase == 0xffe00000) ||
441 (port->mapbase == 0xffe08000)) {
442 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900443 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900444 } else {
445 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900446 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900447 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900448}
Paul Mundte108b2c2006-09-27 16:32:13 +0900449#else
450static inline int scif_txroom(struct uart_port *port)
451{
452 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
453}
454
455static inline int scif_rxroom(struct uart_port *port)
456{
457 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Paul Mundte108b2c2006-09-27 16:32:13 +0900461static inline int sci_txroom(struct uart_port *port)
462{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900463 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900464}
465
466static inline int sci_rxroom(struct uart_port *port)
467{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900468 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/* ********************************************************************** *
472 * the interrupt related routines *
473 * ********************************************************************** */
474
475static void sci_transmit_chars(struct uart_port *port)
476{
477 struct circ_buf *xmit = &port->info->xmit;
478 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned short status;
480 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900481 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 status = sci_in(port, SCxSR);
484 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900486 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900488 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return;
492 }
493
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900494 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900495 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900496 else
497 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 do {
500 unsigned char c;
501
502 if (port->x_char) {
503 c = port->x_char;
504 port->x_char = 0;
505 } else if (!uart_circ_empty(xmit) && !stopped) {
506 c = xmit->buf[xmit->tail];
507 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
508 } else {
509 break;
510 }
511
512 sci_out(port, SCxTDR, c);
513
514 port->icount.tx++;
515 } while (--count > 0);
516
517 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
518
519 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
520 uart_write_wakeup(port);
521 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100522 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 ctrl = sci_in(port, SCSCR);
525
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900526 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sci_in(port, SCxSR); /* Dummy read */
528 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 ctrl |= SCI_CTRL_FLAGS_TIE;
532 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534}
535
536/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900537#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Howells7d12e782006-10-05 14:55:46 +0100539static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900541 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100542 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int i, count, copied = 0;
544 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800545 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 status = sci_in(port, SCxSR);
548 if (!(status & SCxSR_RDxF(port)))
549 return;
550
551 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900552 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900553 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900554 else
555 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800558 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* If for any reason we can't copy more data, we're done! */
561 if (count == 0)
562 break;
563
564 if (port->type == PORT_SCI) {
565 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900566 if (uart_handle_sysrq_char(port, c) ||
567 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900569 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900570 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900572 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 char c = sci_in(port, SCxRDR);
574 status = sci_in(port, SCxSR);
575#if defined(CONFIG_CPU_SH3)
576 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900577 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if ((c == 0) &&
579 (status & SCxSR_FER(port))) {
580 count--; i--;
581 continue;
582 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Nonzero => end-of-break */
585 pr_debug("scif: debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900586 sci_port->break_flag = 0;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (STEPFN(c)) {
589 count--; i--;
590 continue;
591 }
592 }
593#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100594 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 count--; i--;
596 continue;
597 }
598
599 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800601 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 pr_debug("sci: frame error\n");
603 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800604 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 pr_debug("sci: parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800606 } else
607 flag = TTY_NORMAL;
608 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 }
611
612 sci_in(port, SCxSR); /* dummy read */
613 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 copied += count;
616 port->icount.rx += count;
617 }
618
619 if (copied) {
620 /* Tell the rest of the system the news. New characters! */
621 tty_flip_buffer_push(tty);
622 } else {
623 sci_in(port, SCxSR); /* dummy read */
624 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
625 }
626}
627
628#define SCI_BREAK_JIFFIES (HZ/20)
629/* The sci generates interrupts during the break,
630 * 1 per millisecond or so during the break period, for 9600 baud.
631 * So dont bother disabling interrupts.
632 * But dont want more than 1 break event.
633 * Use a kernel timer to periodically poll the rx line until
634 * the break is finished.
635 */
636static void sci_schedule_break_timer(struct sci_port *port)
637{
638 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
639 add_timer(&port->break_timer);
640}
641/* Ensure that two consecutive samples find the break over. */
642static void sci_break_timer(unsigned long data)
643{
Paul Mundte108b2c2006-09-27 16:32:13 +0900644 struct sci_port *port = (struct sci_port *)data;
645
646 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900648 sci_schedule_break_timer(port);
649 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* break is over. */
651 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900652 sci_schedule_break_timer(port);
653 } else
654 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static inline int sci_handle_errors(struct uart_port *port)
658{
659 int copied = 0;
660 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100661 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Paul Mundte108b2c2006-09-27 16:32:13 +0900663 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900665 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800666 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 pr_debug("sci: overrun error\n");
668 }
669
Paul Mundte108b2c2006-09-27 16:32:13 +0900670 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (sci_rxd_in(port) == 0) {
672 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900673 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900674
675 if (!sci_port->break_flag) {
676 sci_port->break_flag = 1;
677 sci_schedule_break_timer(sci_port);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900680 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900682 pr_debug("sci: BREAK detected\n");
Paul Mundte108b2c2006-09-27 16:32:13 +0900683 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900684 copied++;
685 }
686
Paul Mundte108b2c2006-09-27 16:32:13 +0900687 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900689 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800690 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 pr_debug("sci: frame error\n");
692 }
693 }
694
Paul Mundte108b2c2006-09-27 16:32:13 +0900695 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900697 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
698 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 pr_debug("sci: parity error\n");
700 }
701
Alan Cox33f0f882006-01-09 20:54:13 -0800702 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return copied;
706}
707
708static inline int sci_handle_breaks(struct uart_port *port)
709{
710 int copied = 0;
711 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100712 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct sci_port *s = &sci_ports[port->line];
714
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900715 if (uart_handle_break(port))
716 return 0;
717
Paul Mundtb7a76e42006-02-01 03:06:06 -0800718 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#if defined(CONFIG_CPU_SH3)
720 /* Debounce break */
721 s->break_flag = 1;
722#endif
723 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900724 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800725 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 pr_debug("sci: BREAK detected\n");
727 }
728
729#if defined(SCIF_ORER)
730 /* XXX: Handle SCIF overrun error */
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900731 if (port->type != PORT_SCI && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 sci_out(port, SCLSR, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +0900733 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pr_debug("sci: overrun error\n");
736 }
737 }
738#endif
739
Alan Cox33f0f882006-01-09 20:54:13 -0800740 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return copied;
744}
745
David Howells7d12e782006-10-05 14:55:46 +0100746static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* I think sci_receive_chars has to be called irrespective
749 * of whether the I_IXOFF is set, otherwise, how is the interrupt
750 * to be disabled?
751 */
David Howells7d12e782006-10-05 14:55:46 +0100752 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 return IRQ_HANDLED;
755}
756
David Howells7d12e782006-10-05 14:55:46 +0100757static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct uart_port *port = ptr;
760
Paul Mundte108b2c2006-09-27 16:32:13 +0900761 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900763 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 return IRQ_HANDLED;
766}
767
David Howells7d12e782006-10-05 14:55:46 +0100768static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct uart_port *port = ptr;
771
772 /* Handle errors */
773 if (port->type == PORT_SCI) {
774 if (sci_handle_errors(port)) {
775 /* discard character in rx buffer */
776 sci_in(port, SCxSR);
777 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
778 }
779 } else {
780#if defined(SCIF_ORER)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900781 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100782 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 sci_out(port, SCLSR, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800785 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
786 tty_flip_buffer_push(tty);
787 pr_debug("scif: overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789#endif
David Howells7d12e782006-10-05 14:55:46 +0100790 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
794
795 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100796 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 return IRQ_HANDLED;
799}
800
David Howells7d12e782006-10-05 14:55:46 +0100801static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct uart_port *port = ptr;
804
805 /* Handle BREAKs */
806 sci_handle_breaks(port);
807 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
808
809 return IRQ_HANDLED;
810}
811
David Howells7d12e782006-10-05 14:55:46 +0100812static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900814 unsigned short ssr_status, scr_status;
815 struct uart_port *port = ptr;
816 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900818 ssr_status = sci_in(port, SCxSR);
819 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Tx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900822 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
823 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Rx Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900825 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
826 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* Error Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900828 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
829 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* Break Interrupt */
Michael Trimarchia8884e32008-10-31 16:10:23 +0900831 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
832 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Michael Trimarchia8884e32008-10-31 16:10:23 +0900834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Paul Mundta2159b52008-10-02 19:09:13 +0900837#if defined(CONFIG_CPU_FREQ) && defined(CONFIG_HAVE_CLK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
839 * Here we define a transistion notifier so that we can update all of our
840 * ports' baud rate when the peripheral clock changes.
841 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900842static int sci_notifier(struct notifier_block *self,
843 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct cpufreq_freqs *freqs = p;
846 int i;
847
848 if ((phase == CPUFREQ_POSTCHANGE) ||
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900849 (phase == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 for (i = 0; i < SCI_NPORTS; i++) {
851 struct uart_port *port = &sci_ports[i].port;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800852 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /*
855 * Update the uartclk per-port if frequency has
856 * changed, since it will no longer necessarily be
857 * consistent with the old frequency.
858 *
859 * Really we want to be able to do something like
860 * uart_change_speed() or something along those lines
861 * here to implicitly reset the per-port baud rate..
862 *
863 * Clean this up later..
864 */
Paul Mundt1d118562006-12-01 13:15:14 +0900865 clk = clk_get(NULL, "module_clk");
Paul Mundta2159b52008-10-02 19:09:13 +0900866 port->uartclk = clk_get_rate(clk);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800867 clk_put(clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Paul Mundte108b2c2006-09-27 16:32:13 +0900870 printk(KERN_INFO "%s: got a postchange notification "
871 "for cpu %d (old %d, new %d)\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700872 __func__, freqs->cpu, freqs->old, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 return NOTIFY_OK;
876}
877
878static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
Paul Mundta2159b52008-10-02 19:09:13 +0900879#endif /* CONFIG_CPU_FREQ && CONFIG_HAVE_CLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881static int sci_request_irq(struct sci_port *port)
882{
883 int i;
David Howells7d12e782006-10-05 14:55:46 +0100884 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
886 sci_br_interrupt,
887 };
888 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
889 "SCI Transmit Data Empty", "SCI Break" };
890
891 if (port->irqs[0] == port->irqs[1]) {
892 if (!port->irqs[0]) {
893 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
894 return -ENODEV;
895 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900896
897 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900898 IRQF_DISABLED, "sci", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printk(KERN_ERR "sci: Cannot allocate irq.\n");
900 return -ENODEV;
901 }
902 } else {
903 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
904 if (!port->irqs[i])
905 continue;
Paul Mundte108b2c2006-09-27 16:32:13 +0900906 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900907 IRQF_DISABLED, desc[i], port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 printk(KERN_ERR "sci: Cannot allocate irq.\n");
909 return -ENODEV;
910 }
911 }
912 }
913
914 return 0;
915}
916
917static void sci_free_irq(struct sci_port *port)
918{
919 int i;
920
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900921 if (port->irqs[0] == port->irqs[1]) {
922 if (!port->irqs[0])
923 printk(KERN_ERR "sci: sci_free_irq error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900925 free_irq(port->irqs[0], port);
926 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
928 if (!port->irqs[i])
929 continue;
930
931 free_irq(port->irqs[i], port);
932 }
933 }
934}
935
936static unsigned int sci_tx_empty(struct uart_port *port)
937{
938 /* Can't detect */
939 return TIOCSER_TEMT;
940}
941
942static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
943{
944 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
945 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
946 /* If you have signals for DTR and DCD, please implement here. */
947}
948
949static unsigned int sci_get_mctrl(struct uart_port *port)
950{
951 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
952 and CTS/RTS */
953
954 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
955}
956
Russell Kingb129a8c2005-08-31 10:12:14 +0100957static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Paul Mundte108b2c2006-09-27 16:32:13 +0900959 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Paul Mundte108b2c2006-09-27 16:32:13 +0900961 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
962 ctrl = sci_in(port, SCSCR);
963 ctrl |= SCI_CTRL_FLAGS_TIE;
964 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Russell Kingb129a8c2005-08-31 10:12:14 +0100967static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned short ctrl;
970
971 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ctrl = sci_in(port, SCSCR);
973 ctrl &= ~SCI_CTRL_FLAGS_TIE;
974 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
978{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 unsigned short ctrl;
980
981 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ctrl = sci_in(port, SCSCR);
983 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
984 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static void sci_stop_rx(struct uart_port *port)
988{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 unsigned short ctrl;
990
991 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ctrl = sci_in(port, SCSCR);
993 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
994 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void sci_enable_ms(struct uart_port *port)
998{
999 /* Nothing here yet .. */
1000}
1001
1002static void sci_break_ctl(struct uart_port *port, int break_state)
1003{
1004 /* Nothing here yet .. */
1005}
1006
1007static int sci_startup(struct uart_port *port)
1008{
1009 struct sci_port *s = &sci_ports[port->line];
1010
Paul Mundte108b2c2006-09-27 16:32:13 +09001011 if (s->enable)
1012 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Paul Mundta2159b52008-10-02 19:09:13 +09001014#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001015 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +09001016#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -07001019 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sci_start_rx(port, 1);
1021
1022 return 0;
1023}
1024
1025static void sci_shutdown(struct uart_port *port)
1026{
1027 struct sci_port *s = &sci_ports[port->line];
1028
1029 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001030 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sci_free_irq(s);
1032
Paul Mundte108b2c2006-09-27 16:32:13 +09001033 if (s->disable)
1034 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001035
Paul Mundta2159b52008-10-02 19:09:13 +09001036#ifdef CONFIG_HAVE_CLK
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001037 clk_put(s->clk);
1038 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +09001039#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Alan Cox606d0992006-12-08 02:38:45 -08001042static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1043 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct sci_port *s = &sci_ports[port->line];
1046 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +09001047 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +09001050 if (likely(baud))
1051 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Paul Mundte108b2c2006-09-27 16:32:13 +09001053 do {
1054 status = sci_in(port, SCxSR);
1055 } while (!(status & SCxSR_TEND(port)));
1056
1057 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1058
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001059 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +09001060 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001061
1062 smr_val = sci_in(port, SCSMR) & 3;
1063 if ((termios->c_cflag & CSIZE) == CS7)
1064 smr_val |= 0x40;
1065 if (termios->c_cflag & PARENB)
1066 smr_val |= 0x20;
1067 if (termios->c_cflag & PARODD)
1068 smr_val |= 0x30;
1069 if (termios->c_cflag & CSTOPB)
1070 smr_val |= 0x08;
1071
1072 uart_update_timeout(port, termios->c_cflag, baud);
1073
1074 sci_out(port, SCSMR, smr_val);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001077 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1079 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001080 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 sci_out(port, SCBRR, t);
1084 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1085 }
1086
Paul Mundtb7a76e42006-02-01 03:06:06 -08001087 if (likely(s->init_pins))
1088 s->init_pins(port, termios->c_cflag);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 sci_out(port, SCSCR, SCSCR_INIT(port));
1091
1092 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001093 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096static const char *sci_type(struct uart_port *port)
1097{
1098 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001099 case PORT_IRDA:
1100 return "irda";
1101 case PORT_SCI:
1102 return "sci";
1103 case PORT_SCIF:
1104 return "scif";
1105 case PORT_SCIFA:
1106 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Paul Mundtfa439722008-09-04 18:53:58 +09001109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static void sci_release_port(struct uart_port *port)
1113{
1114 /* Nothing here yet .. */
1115}
1116
1117static int sci_request_port(struct uart_port *port)
1118{
1119 /* Nothing here yet .. */
1120 return 0;
1121}
1122
1123static void sci_config_port(struct uart_port *port, int flags)
1124{
1125 struct sci_port *s = &sci_ports[port->line];
1126
1127 port->type = s->type;
1128
Paul Mundte108b2c2006-09-27 16:32:13 +09001129 switch (port->type) {
1130 case PORT_SCI:
1131 s->init_pins = sci_init_pins_sci;
1132 break;
1133 case PORT_SCIF:
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001134 case PORT_SCIFA:
Paul Mundte108b2c2006-09-27 16:32:13 +09001135 s->init_pins = sci_init_pins_scif;
1136 break;
1137 case PORT_IRDA:
1138 s->init_pins = sci_init_pins_irda;
1139 break;
1140 }
1141
Paul Mundt7ff731a2008-10-01 15:46:58 +09001142 if (port->flags & UPF_IOREMAP && !port->membase) {
1143#if defined(CONFIG_SUPERH64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
Paul Mundt7ff731a2008-10-01 15:46:58 +09001145 port->membase = (void __iomem *)port->mapbase;
1146#else
1147 port->membase = ioremap_nocache(port->mapbase, 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148#endif
Paul Mundt7ff731a2008-10-01 15:46:58 +09001149
1150 printk(KERN_ERR "sci: can't remap port#%d\n", port->line);
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1155{
1156 struct sci_port *s = &sci_ports[port->line];
1157
Yinghai Lua62c4132008-08-19 20:49:55 -07001158 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -EINVAL;
1160 if (ser->baud_base < 2400)
1161 /* No paper tape reader for Mitch.. */
1162 return -EINVAL;
1163
1164 return 0;
1165}
1166
1167static struct uart_ops sci_uart_ops = {
1168 .tx_empty = sci_tx_empty,
1169 .set_mctrl = sci_set_mctrl,
1170 .get_mctrl = sci_get_mctrl,
1171 .start_tx = sci_start_tx,
1172 .stop_tx = sci_stop_tx,
1173 .stop_rx = sci_stop_rx,
1174 .enable_ms = sci_enable_ms,
1175 .break_ctl = sci_break_ctl,
1176 .startup = sci_startup,
1177 .shutdown = sci_shutdown,
1178 .set_termios = sci_set_termios,
1179 .type = sci_type,
1180 .release_port = sci_release_port,
1181 .request_port = sci_request_port,
1182 .config_port = sci_config_port,
1183 .verify_port = sci_verify_port,
1184};
1185
Paul Mundte108b2c2006-09-27 16:32:13 +09001186static void __init sci_init_ports(void)
1187{
1188 static int first = 1;
1189 int i;
1190
1191 if (!first)
1192 return;
1193
1194 first = 0;
1195
1196 for (i = 0; i < SCI_NPORTS; i++) {
1197 sci_ports[i].port.ops = &sci_uart_ops;
1198 sci_ports[i].port.iotype = UPIO_MEM;
1199 sci_ports[i].port.line = i;
1200 sci_ports[i].port.fifosize = 1;
1201
1202#if defined(__H8300H__) || defined(__H8300S__)
1203#ifdef __H8300S__
1204 sci_ports[i].enable = h8300_sci_enable;
1205 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001207 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001208#elif defined(CONFIG_HAVE_CLK)
Paul Mundte108b2c2006-09-27 16:32:13 +09001209 /*
1210 * XXX: We should use a proper SCI/SCIF clock
1211 */
1212 {
Paul Mundt1d118562006-12-01 13:15:14 +09001213 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundta2159b52008-10-02 19:09:13 +09001214 sci_ports[i].port.uartclk = clk_get_rate(clk);
Paul Mundte108b2c2006-09-27 16:32:13 +09001215 clk_put(clk);
1216 }
Paul Mundta2159b52008-10-02 19:09:13 +09001217#else
1218#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001219#endif
1220
1221 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1222 sci_ports[i].break_timer.function = sci_break_timer;
1223
1224 init_timer(&sci_ports[i].break_timer);
1225 }
1226}
1227
1228int __init early_sci_setup(struct uart_port *port)
1229{
1230 if (unlikely(port->line > SCI_NPORTS))
1231 return -ENODEV;
1232
1233 sci_init_ports();
1234
1235 sci_ports[port->line].port.membase = port->membase;
1236 sci_ports[port->line].port.mapbase = port->mapbase;
1237 sci_ports[port->line].port.type = port->type;
1238
1239 return 0;
1240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1243/*
1244 * Print a string to the serial port trying not to disturb
1245 * any possible real use of the port...
1246 */
1247static void serial_console_write(struct console *co, const char *s,
1248 unsigned count)
1249{
1250 put_string(serial_console_port, s, count);
1251}
1252
1253static int __init serial_console_setup(struct console *co, char *options)
1254{
1255 struct uart_port *port;
1256 int baud = 115200;
1257 int bits = 8;
1258 int parity = 'n';
1259 int flow = 'n';
1260 int ret;
1261
Paul Mundte108b2c2006-09-27 16:32:13 +09001262 /*
1263 * Check whether an invalid uart number has been specified, and
1264 * if so, search for the first available port that does have
1265 * console support.
1266 */
1267 if (co->index >= SCI_NPORTS)
1268 co->index = 0;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 serial_console_port = &sci_ports[co->index];
1271 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001274 * Also need to check port->type, we don't actually have any
1275 * UPIO_PORT ports, but uart_report_port() handily misreports
1276 * it anyways if we don't have a port available by the time this is
1277 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001279 if (!port->type)
1280 return -ENODEV;
1281 if (!port->membase || !port->mapbase)
1282 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001283
Paul Mundte108b2c2006-09-27 16:32:13 +09001284 port->type = serial_console_port->type;
1285
Paul Mundta2159b52008-10-02 19:09:13 +09001286#ifdef CONFIG_HAVE_CLK
Paul Mundt005a3362007-04-26 11:45:32 +09001287 if (!serial_console_port->clk)
1288 serial_console_port->clk = clk_get(NULL, "module_clk");
1289#endif
1290
Paul Mundte108b2c2006-09-27 16:32:13 +09001291 if (port->flags & UPF_IOREMAP)
1292 sci_config_port(port, 0);
1293
1294 if (serial_console_port->enable)
1295 serial_console_port->enable(port);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (options)
1298 uart_parse_options(options, &baud, &parity, &bits, &flow);
1299
1300 ret = uart_set_options(port, co, baud, parity, bits, flow);
1301#if defined(__H8300H__) || defined(__H8300S__)
1302 /* disable rx interrupt */
1303 if (ret == 0)
1304 sci_stop_rx(port);
1305#endif
1306 return ret;
1307}
1308
1309static struct console serial_console = {
1310 .name = "ttySC",
1311 .device = uart_console_device,
1312 .write = serial_console_write,
1313 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001314 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 .index = -1,
1316 .data = &sci_uart_driver,
1317};
1318
1319static int __init sci_console_init(void)
1320{
Paul Mundte108b2c2006-09-27 16:32:13 +09001321 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 register_console(&serial_console);
1323 return 0;
1324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325console_initcall(sci_console_init);
1326#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1327
Paul Mundt68362e02007-09-11 15:27:29 +09001328#ifdef CONFIG_SH_KGDB_CONSOLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/*
1330 * FIXME: Most of this can go away.. at the moment, we rely on
1331 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1332 * most of that can easily be done here instead.
1333 *
1334 * For the time being, just accept the values that were parsed earlier..
1335 */
1336static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1337 int *parity, int *bits)
1338{
1339 *baud = kgdb_baud;
1340 *parity = tolower(kgdb_parity);
1341 *bits = kgdb_bits - '0';
1342}
1343
1344/*
1345 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1346 * care of the early-on initialization for kgdb, regardless of whether we
1347 * actually use kgdb as a console or not.
1348 *
1349 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1350 */
1351int __init kgdb_console_setup(struct console *co, char *options)
1352{
1353 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1354 int baud = 38400;
1355 int bits = 8;
1356 int parity = 'n';
1357 int flow = 'n';
1358
Paul Mundtb7a76e42006-02-01 03:06:06 -08001359 if (co->index != kgdb_portnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 co->index = kgdb_portnum;
1361
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001362 kgdb_sci_port = &sci_ports[co->index];
1363 port = &kgdb_sci_port->port;
1364
1365 /*
1366 * Also need to check port->type, we don't actually have any
1367 * UPIO_PORT ports, but uart_report_port() handily misreports
1368 * it anyways if we don't have a port available by the time this is
1369 * called.
1370 */
1371 if (!port->type)
1372 return -ENODEV;
1373 if (!port->membase || !port->mapbase)
1374 return -ENODEV;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (options)
1377 uart_parse_options(options, &baud, &parity, &bits, &flow);
1378 else
1379 kgdb_console_get_options(port, &baud, &parity, &bits);
1380
1381 kgdb_getchar = kgdb_sci_getchar;
1382 kgdb_putchar = kgdb_sci_putchar;
1383
1384 return uart_set_options(port, co, baud, parity, bits, flow);
1385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387static struct console kgdb_console = {
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001388 .name = "ttySC",
1389 .device = uart_console_device,
1390 .write = kgdb_console_write,
1391 .setup = kgdb_console_setup,
1392 .flags = CON_PRINTBUFFER,
1393 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 .data = &sci_uart_driver,
1395};
1396
1397/* Register the KGDB console so we get messages (d'oh!) */
1398static int __init kgdb_console_init(void)
1399{
Paul Mundte108b2c2006-09-27 16:32:13 +09001400 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 register_console(&kgdb_console);
1402 return 0;
1403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404console_initcall(kgdb_console_init);
1405#endif /* CONFIG_SH_KGDB_CONSOLE */
1406
1407#if defined(CONFIG_SH_KGDB_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001408#define SCI_CONSOLE (&kgdb_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001410#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001412#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413#endif
1414
1415static char banner[] __initdata =
1416 KERN_INFO "SuperH SCI(F) driver initialized\n";
1417
1418static struct uart_driver sci_uart_driver = {
1419 .owner = THIS_MODULE,
1420 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 .dev_name = "ttySC",
1422 .major = SCI_MAJOR,
1423 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001424 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .cons = SCI_CONSOLE,
1426};
1427
Paul Mundte108b2c2006-09-27 16:32:13 +09001428/*
1429 * Register a set of serial devices attached to a platform device. The
1430 * list is terminated with a zero flags entry, which means we expect
1431 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1432 * remapping (such as sh64) should also set UPF_IOREMAP.
1433 */
1434static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Paul Mundte108b2c2006-09-27 16:32:13 +09001436 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001437 int i, ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Paul Mundt32351a22007-03-12 14:38:59 +09001439 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001440 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Paul Mundt32351a22007-03-12 14:38:59 +09001442 /* Sanity check */
1443 if (unlikely(i == SCI_NPORTS)) {
1444 dev_notice(&dev->dev, "Attempting to register port "
1445 "%d when only %d are available.\n",
1446 i+1, SCI_NPORTS);
1447 dev_notice(&dev->dev, "Consider bumping "
1448 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1449 break;
1450 }
1451
Paul Mundte108b2c2006-09-27 16:32:13 +09001452 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001453
Paul Mundt7ff731a2008-10-01 15:46:58 +09001454 if (p->mapbase && !p->membase) {
1455 if (p->flags & UPF_IOREMAP) {
1456 p->membase = ioremap_nocache(p->mapbase, 0x40);
1457 if (IS_ERR(p->membase)) {
1458 ret = PTR_ERR(p->membase);
1459 goto err_unreg;
1460 }
1461 } else {
1462 /*
1463 * For the simple (and majority of) cases
1464 * where we don't need to do any remapping,
1465 * just cast the cookie directly.
1466 */
1467 p->membase = (void __iomem *)p->mapbase;
1468 }
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Paul Mundte108b2c2006-09-27 16:32:13 +09001471 sciport->port.membase = p->membase;
1472
1473 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1474 sciport->port.flags = p->flags;
1475 sciport->port.dev = &dev->dev;
1476
1477 sciport->type = sciport->port.type = p->type;
1478
1479 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1480
1481 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001484#if defined(CONFIG_SH_KGDB) && !defined(CONFIG_SH_KGDB_CONSOLE)
1485 kgdb_sci_port = &sci_ports[kgdb_portnum];
1486 kgdb_getchar = kgdb_sci_getchar;
1487 kgdb_putchar = kgdb_sci_putchar;
1488#endif
1489
Paul Mundta2159b52008-10-02 19:09:13 +09001490#if defined(CONFIG_CPU_FREQ) && defined(CONFIG_HAVE_CLK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Paul Mundte289fd92007-08-08 18:09:13 +09001492 dev_info(&dev->dev, "CPU frequency notifier registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493#endif
1494
1495#ifdef CONFIG_SH_STANDARD_BIOS
1496 sh_bios_gdb_detach();
1497#endif
1498
Paul Mundte108b2c2006-09-27 16:32:13 +09001499 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001500
1501err_unreg:
1502 for (i = i - 1; i >= 0; i--)
1503 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1504
1505 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001506}
1507
1508static int __devexit sci_remove(struct platform_device *dev)
1509{
1510 int i;
1511
1512 for (i = 0; i < SCI_NPORTS; i++)
1513 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1514
1515 return 0;
1516}
1517
1518static int sci_suspend(struct platform_device *dev, pm_message_t state)
1519{
1520 int i;
1521
1522 for (i = 0; i < SCI_NPORTS; i++) {
1523 struct sci_port *p = &sci_ports[i];
1524
1525 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1526 uart_suspend_port(&sci_uart_driver, &p->port);
1527 }
1528
1529 return 0;
1530}
1531
1532static int sci_resume(struct platform_device *dev)
1533{
1534 int i;
1535
1536 for (i = 0; i < SCI_NPORTS; i++) {
1537 struct sci_port *p = &sci_ports[i];
1538
1539 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1540 uart_resume_port(&sci_uart_driver, &p->port);
1541 }
1542
1543 return 0;
1544}
1545
1546static struct platform_driver sci_driver = {
1547 .probe = sci_probe,
1548 .remove = __devexit_p(sci_remove),
1549 .suspend = sci_suspend,
1550 .resume = sci_resume,
1551 .driver = {
1552 .name = "sh-sci",
1553 .owner = THIS_MODULE,
1554 },
1555};
1556
1557static int __init sci_init(void)
1558{
1559 int ret;
1560
1561 printk(banner);
1562
1563 sci_init_ports();
1564
1565 ret = uart_register_driver(&sci_uart_driver);
1566 if (likely(ret == 0)) {
1567 ret = platform_driver_register(&sci_driver);
1568 if (unlikely(ret))
1569 uart_unregister_driver(&sci_uart_driver);
1570 }
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return ret;
1573}
1574
1575static void __exit sci_exit(void)
1576{
Paul Mundte108b2c2006-09-27 16:32:13 +09001577 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 uart_unregister_driver(&sci_uart_driver);
1579}
1580
1581module_init(sci_init);
1582module_exit(sci_exit);
1583
Paul Mundte108b2c2006-09-27 16:32:13 +09001584MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001585MODULE_ALIAS("platform:sh-sci");