blob: ac658a7a27becaaf253101a1d2176ce434237a80 [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 Mundt85f094e2008-04-25 16:04:20 +090082#ifdef CONFIG_SUPERH
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
Paul Mundte108b2c2006-09-27 16:32:13 +0900104#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
105 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
106static 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
112static int get_char(struct uart_port *port)
113{
114 unsigned long flags;
115 unsigned short status;
116 int c;
117
Paul Mundte108b2c2006-09-27 16:32:13 +0900118 spin_lock_irqsave(&port->lock, flags);
119 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 status = sci_in(port, SCxSR);
121 if (status & SCxSR_ERRORS(port)) {
122 handle_error(port);
123 continue;
124 }
125 } while (!(status & SCxSR_RDxF(port)));
126 c = sci_in(port, SCxRDR);
127 sci_in(port, SCxSR); /* Dummy read */
128 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Paul Mundte108b2c2006-09-27 16:32:13 +0900129 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return c;
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
134
Paul Mundte108b2c2006-09-27 16:32:13 +0900135#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void put_char(struct uart_port *port, char c)
137{
138 unsigned long flags;
139 unsigned short status;
140
Paul Mundte108b2c2006-09-27 16:32:13 +0900141 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 do {
144 status = sci_in(port, SCxSR);
145 } while (!(status & SCxSR_TDxE(port)));
146
147 sci_out(port, SCxTDR, c);
148 sci_in(port, SCxSR); /* Dummy read */
149 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
150
Paul Mundte108b2c2006-09-27 16:32:13 +0900151 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Paul Mundte108b2c2006-09-27 16:32:13 +0900153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Paul Mundte108b2c2006-09-27 16:32:13 +0900155#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void put_string(struct sci_port *sci_port, const char *buffer, int count)
157{
158 struct uart_port *port = &sci_port->port;
159 const unsigned char *p = buffer;
160 int i;
161
162#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
163 int checksum;
164 int usegdb=0;
165
166#ifdef CONFIG_SH_STANDARD_BIOS
Paul Mundtb7a76e42006-02-01 03:06:06 -0800167 /* This call only does a trap the first time it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * called, and so is safe to do here unconditionally
169 */
170 usegdb |= sh_bios_in_gdb_mode();
171#endif
172#ifdef CONFIG_SH_KGDB
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900173 usegdb |= (kgdb_in_gdb_mode && (sci_port == kgdb_sci_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
175
176 if (usegdb) {
177 /* $<packet info>#<checksum>. */
178 do {
179 unsigned char c;
180 put_char(port, '$');
181 put_char(port, 'O'); /* 'O'utput to console */
182 checksum = 'O';
183
184 for (i=0; i<count; i++) { /* Don't use run length encoding */
185 int h, l;
186
187 c = *p++;
Harvey Harrisonbfd3c7a2008-05-12 12:05:43 -0700188 h = hex_asc_hi(c);
189 l = hex_asc_lo(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 put_char(port, h);
191 put_char(port, l);
192 checksum += h + l;
193 }
194 put_char(port, '#');
Harvey Harrisonbfd3c7a2008-05-12 12:05:43 -0700195 put_char(port, hex_asc_hi(checksum));
196 put_char(port, hex_asc_lo(checksum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } while (get_char(port) != '+');
198 } else
199#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
200 for (i=0; i<count; i++) {
201 if (*p == 10)
202 put_char(port, '\r');
203 put_char(port, *p++);
204 }
205}
206#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#ifdef CONFIG_SH_KGDB
Paul Mundte108b2c2006-09-27 16:32:13 +0900209static int kgdb_sci_getchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int c;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Keep trying to read a character, this could be neater */
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900214 while ((c = get_char(&kgdb_sci_port->port)) < 0)
Paul Mundte108b2c2006-09-27 16:32:13 +0900215 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 return c;
218}
219
Paul Mundte108b2c2006-09-27 16:32:13 +0900220static inline void kgdb_sci_putchar(int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Paul Mundtfa5da2f2007-03-08 17:27:37 +0900222 put_char(&kgdb_sci_port->port, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#endif /* CONFIG_SH_KGDB */
225
226#if defined(__H8300S__)
227enum { sci_disable, sci_enable };
228
Paul Mundte108b2c2006-09-27 16:32:13 +0900229static void h8300_sci_config(struct uart_port* port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
232 int ch = (port->mapbase - SMR0) >> 3;
233 unsigned char mask = 1 << (ch+1);
234
235 if (ctrl == sci_disable) {
236 *mstpcrl |= mask;
237 } else {
238 *mstpcrl &= ~mask;
239 }
240}
Paul Mundte108b2c2006-09-27 16:32:13 +0900241
242static inline void h8300_sci_enable(struct uart_port *port)
243{
244 h8300_sci_config(port, sci_enable);
245}
246
247static inline void h8300_sci_disable(struct uart_port *port)
248{
249 h8300_sci_config(port, sci_disable);
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
252
Paul Mundte108b2c2006-09-27 16:32:13 +0900253#if defined(SCI_ONLY) || defined(SCI_AND_SCIF) && \
254 defined(__H8300H__) || defined(__H8300S__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
256{
257 int ch = (port->mapbase - SMR0) >> 3;
258
259 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900260 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
261 h8300_sci_pins[ch].rx,
262 H8300_GPIO_INPUT);
263 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
264 h8300_sci_pins[ch].tx,
265 H8300_GPIO_OUTPUT);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* tx mark output*/
268 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
269}
Paul Mundte108b2c2006-09-27 16:32:13 +0900270#else
271#define sci_init_pins_sci NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900273
274#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
275static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
276{
277 unsigned int fcr_val = 0;
278
279 if (cflag & CRTSCTS)
280 fcr_val |= SCFCR_MCE;
281
282 sci_out(port, SCFCR, fcr_val);
283}
284#else
285#define sci_init_pins_irda NULL
286#endif
287
288#ifdef SCI_ONLY
289#define sci_init_pins_scif NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#endif
291
292#if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
Magnus Dammd89ddd12007-07-25 11:42:56 +0900293#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900294static void sci_init_pins_scif(struct uart_port* port, unsigned int cflag)
295{
296 unsigned int fcr_val = 0;
297
298 set_sh771x_scif_pfc(port);
299 if (cflag & CRTSCTS) {
300 fcr_val |= SCFCR_MCE;
301 }
302 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{
428 if((port->mapbase == 0xffe00000) || (port->mapbase == 0xffe08000)) /* SCIF0/1*/
429 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
430 else /* SCIF2 */
431 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
432}
433
434static inline int scif_rxroom(struct uart_port *port)
435{
436 if((port->mapbase == 0xffe00000) || (port->mapbase == 0xffe08000)) /* SCIF0/1*/
437 return sci_in(port, SCRFDR) & 0xff;
438 else /* SCIF2 */
439 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
440}
Paul Mundte108b2c2006-09-27 16:32:13 +0900441#else
442static inline int scif_txroom(struct uart_port *port)
443{
444 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
445}
446
447static inline int scif_rxroom(struct uart_port *port)
448{
449 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
450}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#endif
452#endif /* SCIF_ONLY || SCI_AND_SCIF */
453
Paul Mundte108b2c2006-09-27 16:32:13 +0900454static inline int sci_txroom(struct uart_port *port)
455{
456 return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
457}
458
459static inline int sci_rxroom(struct uart_port *port)
460{
461 return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* ********************************************************************** *
465 * the interrupt related routines *
466 * ********************************************************************** */
467
468static void sci_transmit_chars(struct uart_port *port)
469{
470 struct circ_buf *xmit = &port->info->xmit;
471 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned short status;
473 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900474 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 status = sci_in(port, SCxSR);
477 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ctrl = sci_in(port, SCSCR);
479 if (uart_circ_empty(xmit)) {
480 ctrl &= ~SCI_CTRL_FLAGS_TIE;
481 } else {
482 ctrl |= SCI_CTRL_FLAGS_TIE;
483 }
484 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
486 }
487
Paul Mundte108b2c2006-09-27 16:32:13 +0900488#ifndef SCI_ONLY
489 if (port->type == PORT_SCIF)
490 count = scif_txroom(port);
491 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800492#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900493 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 do {
496 unsigned char c;
497
498 if (port->x_char) {
499 c = port->x_char;
500 port->x_char = 0;
501 } else if (!uart_circ_empty(xmit) && !stopped) {
502 c = xmit->buf[xmit->tail];
503 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
504 } else {
505 break;
506 }
507
508 sci_out(port, SCxTDR, c);
509
510 port->icount.tx++;
511 } while (--count > 0);
512
513 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
514
515 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
516 uart_write_wakeup(port);
517 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100518 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ctrl = sci_in(port, SCSCR);
521
522#if !defined(SCI_ONLY)
523 if (port->type == PORT_SCIF) {
524 sci_in(port, SCxSR); /* Dummy read */
525 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
526 }
527#endif
528
529 ctrl |= SCI_CTRL_FLAGS_TIE;
530 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532}
533
534/* On SH3, SCIF may read end-of-break as a space->mark char */
535#define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
536
David Howells7d12e782006-10-05 14:55:46 +0100537static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Paul Mundte108b2c2006-09-27 16:32:13 +0900539 struct sci_port *sci_port = (struct sci_port *)port;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100540 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int i, count, copied = 0;
542 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800543 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 status = sci_in(port, SCxSR);
546 if (!(status & SCxSR_RDxF(port)))
547 return;
548
549 while (1) {
550#if !defined(SCI_ONLY)
Paul Mundte108b2c2006-09-27 16:32:13 +0900551 if (port->type == PORT_SCIF)
552 count = scif_rxroom(port);
553 else
Paul Mundtb7a76e42006-02-01 03:06:06 -0800554#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 count = sci_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);
David Howells7d12e782006-10-05 14:55:46 +0100566 if (uart_handle_sysrq_char(port, c) || sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 count = 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900568 else {
569 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 } else {
572 for (i=0; i<count; i++) {
573 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 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900673 struct sci_port *sci_port = (struct sci_port *)port;
674
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 pr_debug("sci: BREAK detected\n");
Paul Mundte108b2c2006-09-27 16:32:13 +0900683 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800684 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900686 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900688 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800689 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 pr_debug("sci: frame error\n");
691 }
692 }
693
Paul Mundte108b2c2006-09-27 16:32:13 +0900694 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900696 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
697 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 pr_debug("sci: parity error\n");
699 }
700
Alan Cox33f0f882006-01-09 20:54:13 -0800701 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return copied;
705}
706
707static inline int sci_handle_breaks(struct uart_port *port)
708{
709 int copied = 0;
710 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100711 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct sci_port *s = &sci_ports[port->line];
713
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900714 if (uart_handle_break(port))
715 return 0;
716
Paul Mundtb7a76e42006-02-01 03:06:06 -0800717 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#if defined(CONFIG_CPU_SH3)
719 /* Debounce break */
720 s->break_flag = 1;
721#endif
722 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900723 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800724 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 pr_debug("sci: BREAK detected\n");
726 }
727
728#if defined(SCIF_ORER)
729 /* XXX: Handle SCIF overrun error */
730 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
731 sci_out(port, SCLSR, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +0900732 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 copied++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pr_debug("sci: overrun error\n");
735 }
736 }
737#endif
738
Alan Cox33f0f882006-01-09 20:54:13 -0800739 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return copied;
743}
744
David Howells7d12e782006-10-05 14:55:46 +0100745static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* I think sci_receive_chars has to be called irrespective
748 * of whether the I_IXOFF is set, otherwise, how is the interrupt
749 * to be disabled?
750 */
David Howells7d12e782006-10-05 14:55:46 +0100751 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 return IRQ_HANDLED;
754}
755
David Howells7d12e782006-10-05 14:55:46 +0100756static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct uart_port *port = ptr;
759
Paul Mundte108b2c2006-09-27 16:32:13 +0900760 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900762 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return IRQ_HANDLED;
765}
766
David Howells7d12e782006-10-05 14:55:46 +0100767static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct uart_port *port = ptr;
770
771 /* Handle errors */
772 if (port->type == PORT_SCI) {
773 if (sci_handle_errors(port)) {
774 /* discard character in rx buffer */
775 sci_in(port, SCxSR);
776 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
777 }
778 } else {
779#if defined(SCIF_ORER)
780 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100781 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 sci_out(port, SCLSR, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800784 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
785 tty_flip_buffer_push(tty);
786 pr_debug("scif: overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788#endif
David Howells7d12e782006-10-05 14:55:46 +0100789 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
793
794 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100795 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 return IRQ_HANDLED;
798}
799
David Howells7d12e782006-10-05 14:55:46 +0100800static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct uart_port *port = ptr;
803
804 /* Handle BREAKs */
805 sci_handle_breaks(port);
806 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
807
808 return IRQ_HANDLED;
809}
810
David Howells7d12e782006-10-05 14:55:46 +0100811static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 unsigned short ssr_status, scr_status;
814 struct uart_port *port = ptr;
815
816 ssr_status = sci_in(port,SCxSR);
817 scr_status = sci_in(port,SCSCR);
818
819 /* Tx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900820 if ((ssr_status & 0x0020) && (scr_status & 0x0080))
David Howells7d12e782006-10-05 14:55:46 +0100821 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Rx Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900823 if ((ssr_status & 0x0002) && (scr_status & 0x0040))
David Howells7d12e782006-10-05 14:55:46 +0100824 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* Error Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900826 if ((ssr_status & 0x0080) && (scr_status & 0x0400))
David Howells7d12e782006-10-05 14:55:46 +0100827 sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Break Interrupt */
Paul Mundte108b2c2006-09-27 16:32:13 +0900829 if ((ssr_status & 0x0010) && (scr_status & 0x0200))
David Howells7d12e782006-10-05 14:55:46 +0100830 sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return IRQ_HANDLED;
833}
834
835#ifdef CONFIG_CPU_FREQ
836/*
837 * Here we define a transistion notifier so that we can update all of our
838 * ports' baud rate when the peripheral clock changes.
839 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900840static int sci_notifier(struct notifier_block *self,
841 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct cpufreq_freqs *freqs = p;
844 int i;
845
846 if ((phase == CPUFREQ_POSTCHANGE) ||
847 (phase == CPUFREQ_RESUMECHANGE)){
848 for (i = 0; i < SCI_NPORTS; i++) {
849 struct uart_port *port = &sci_ports[i].port;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800850 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Update the uartclk per-port if frequency has
854 * changed, since it will no longer necessarily be
855 * consistent with the old frequency.
856 *
857 * Really we want to be able to do something like
858 * uart_change_speed() or something along those lines
859 * here to implicitly reset the per-port baud rate..
860 *
861 * Clean this up later..
862 */
Paul Mundt1d118562006-12-01 13:15:14 +0900863 clk = clk_get(NULL, "module_clk");
Paul Mundtb7a76e42006-02-01 03:06:06 -0800864 port->uartclk = clk_get_rate(clk) * 16;
865 clk_put(clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Paul Mundte108b2c2006-09-27 16:32:13 +0900868 printk(KERN_INFO "%s: got a postchange notification "
869 "for cpu %d (old %d, new %d)\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700870 __func__, freqs->cpu, freqs->old, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 return NOTIFY_OK;
874}
875
876static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
877#endif /* CONFIG_CPU_FREQ */
878
879static int sci_request_irq(struct sci_port *port)
880{
881 int i;
David Howells7d12e782006-10-05 14:55:46 +0100882 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
884 sci_br_interrupt,
885 };
886 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
887 "SCI Transmit Data Empty", "SCI Break" };
888
889 if (port->irqs[0] == port->irqs[1]) {
890 if (!port->irqs[0]) {
891 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
892 return -ENODEV;
893 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900894
895 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900896 IRQF_DISABLED, "sci", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 printk(KERN_ERR "sci: Cannot allocate irq.\n");
898 return -ENODEV;
899 }
900 } else {
901 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
902 if (!port->irqs[i])
903 continue;
Paul Mundte108b2c2006-09-27 16:32:13 +0900904 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900905 IRQF_DISABLED, desc[i], port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 printk(KERN_ERR "sci: Cannot allocate irq.\n");
907 return -ENODEV;
908 }
909 }
910 }
911
912 return 0;
913}
914
915static void sci_free_irq(struct sci_port *port)
916{
917 int i;
918
919 if (port->irqs[0] == port->irqs[1]) {
920 if (!port->irqs[0])
921 printk("sci: sci_free_irq error\n");
922 else
923 free_irq(port->irqs[0], port);
924 } else {
925 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
926 if (!port->irqs[i])
927 continue;
928
929 free_irq(port->irqs[i], port);
930 }
931 }
932}
933
934static unsigned int sci_tx_empty(struct uart_port *port)
935{
936 /* Can't detect */
937 return TIOCSER_TEMT;
938}
939
940static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
941{
942 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
943 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
944 /* If you have signals for DTR and DCD, please implement here. */
945}
946
947static unsigned int sci_get_mctrl(struct uart_port *port)
948{
949 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
950 and CTS/RTS */
951
952 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
953}
954
Russell Kingb129a8c2005-08-31 10:12:14 +0100955static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Paul Mundte108b2c2006-09-27 16:32:13 +0900957 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Paul Mundte108b2c2006-09-27 16:32:13 +0900959 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
960 ctrl = sci_in(port, SCSCR);
961 ctrl |= SCI_CTRL_FLAGS_TIE;
962 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Russell Kingb129a8c2005-08-31 10:12:14 +0100965static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 unsigned short ctrl;
968
969 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ctrl = sci_in(port, SCSCR);
971 ctrl &= ~SCI_CTRL_FLAGS_TIE;
972 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
976{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned short ctrl;
978
979 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ctrl = sci_in(port, SCSCR);
981 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
982 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static void sci_stop_rx(struct uart_port *port)
986{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned short ctrl;
988
989 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ctrl = sci_in(port, SCSCR);
991 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
992 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995static void sci_enable_ms(struct uart_port *port)
996{
997 /* Nothing here yet .. */
998}
999
1000static void sci_break_ctl(struct uart_port *port, int break_state)
1001{
1002 /* Nothing here yet .. */
1003}
1004
1005static int sci_startup(struct uart_port *port)
1006{
1007 struct sci_port *s = &sci_ports[port->line];
1008
Paul Mundte108b2c2006-09-27 16:32:13 +09001009 if (s->enable)
1010 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Paul Mundt005a3362007-04-26 11:45:32 +09001012#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001013 s->clk = clk_get(NULL, "module_clk");
Paul Mundt005a3362007-04-26 11:45:32 +09001014#endif
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -07001017 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sci_start_rx(port, 1);
1019
1020 return 0;
1021}
1022
1023static void sci_shutdown(struct uart_port *port)
1024{
1025 struct sci_port *s = &sci_ports[port->line];
1026
1027 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001028 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 sci_free_irq(s);
1030
Paul Mundte108b2c2006-09-27 16:32:13 +09001031 if (s->disable)
1032 s->disable(port);
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001033
Paul Mundt005a3362007-04-26 11:45:32 +09001034#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001035 clk_put(s->clk);
1036 s->clk = NULL;
Paul Mundt005a3362007-04-26 11:45:32 +09001037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Alan Cox606d0992006-12-08 02:38:45 -08001040static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1041 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct sci_port *s = &sci_ports[port->line];
1044 unsigned int status, baud, smr_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int t;
1046
1047 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 switch (baud) {
Paul Mundtb7a76e42006-02-01 03:06:06 -08001050 case 0:
1051 t = -1;
1052 break;
1053 default:
1054 {
1055#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
dmitry pervushin1534a3b2007-04-24 13:41:12 +09001056 t = SCBRR_VALUE(baud, clk_get_rate(s->clk));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001057#else
1058 t = SCBRR_VALUE(baud);
1059#endif
Paul Mundtb7a76e42006-02-01 03:06:06 -08001060 break;
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Paul Mundte108b2c2006-09-27 16:32:13 +09001064 do {
1065 status = sci_in(port, SCxSR);
1066 } while (!(status & SCxSR_TEND(port)));
1067
1068 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1069
1070#if !defined(SCI_ONLY)
1071 if (port->type == PORT_SCIF)
1072 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1073#endif
1074
1075 smr_val = sci_in(port, SCSMR) & 3;
1076 if ((termios->c_cflag & CSIZE) == CS7)
1077 smr_val |= 0x40;
1078 if (termios->c_cflag & PARENB)
1079 smr_val |= 0x20;
1080 if (termios->c_cflag & PARODD)
1081 smr_val |= 0x30;
1082 if (termios->c_cflag & CSTOPB)
1083 smr_val |= 0x08;
1084
1085 uart_update_timeout(port, termios->c_cflag, baud);
1086
1087 sci_out(port, SCSMR, smr_val);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (t > 0) {
1090 if(t >= 256) {
1091 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1092 t >>= 2;
1093 } else {
1094 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1095 }
1096 sci_out(port, SCBRR, t);
1097 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1098 }
1099
Paul Mundtb7a76e42006-02-01 03:06:06 -08001100 if (likely(s->init_pins))
1101 s->init_pins(port, termios->c_cflag);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sci_out(port, SCSCR, SCSCR_INIT(port));
1104
1105 if ((termios->c_cflag & CREAD) != 0)
1106 sci_start_rx(port,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109static const char *sci_type(struct uart_port *port)
1110{
1111 switch (port->type) {
1112 case PORT_SCI: return "sci";
1113 case PORT_SCIF: return "scif";
1114 case PORT_IRDA: return "irda";
1115 }
1116
Paul Mundtfa439722008-09-04 18:53:58 +09001117 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120static void sci_release_port(struct uart_port *port)
1121{
1122 /* Nothing here yet .. */
1123}
1124
1125static int sci_request_port(struct uart_port *port)
1126{
1127 /* Nothing here yet .. */
1128 return 0;
1129}
1130
1131static void sci_config_port(struct uart_port *port, int flags)
1132{
1133 struct sci_port *s = &sci_ports[port->line];
1134
1135 port->type = s->type;
1136
Paul Mundte108b2c2006-09-27 16:32:13 +09001137 switch (port->type) {
1138 case PORT_SCI:
1139 s->init_pins = sci_init_pins_sci;
1140 break;
1141 case PORT_SCIF:
1142 s->init_pins = sci_init_pins_scif;
1143 break;
1144 case PORT_IRDA:
1145 s->init_pins = sci_init_pins_irda;
1146 break;
1147 }
1148
Paul Mundt7ff731a2008-10-01 15:46:58 +09001149 if (port->flags & UPF_IOREMAP && !port->membase) {
1150#if defined(CONFIG_SUPERH64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
Paul Mundt7ff731a2008-10-01 15:46:58 +09001152 port->membase = (void __iomem *)port->mapbase;
1153#else
1154 port->membase = ioremap_nocache(port->mapbase, 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#endif
Paul Mundt7ff731a2008-10-01 15:46:58 +09001156
1157 printk(KERN_ERR "sci: can't remap port#%d\n", port->line);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1162{
1163 struct sci_port *s = &sci_ports[port->line];
1164
1165 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1166 return -EINVAL;
1167 if (ser->baud_base < 2400)
1168 /* No paper tape reader for Mitch.. */
1169 return -EINVAL;
1170
1171 return 0;
1172}
1173
1174static struct uart_ops sci_uart_ops = {
1175 .tx_empty = sci_tx_empty,
1176 .set_mctrl = sci_set_mctrl,
1177 .get_mctrl = sci_get_mctrl,
1178 .start_tx = sci_start_tx,
1179 .stop_tx = sci_stop_tx,
1180 .stop_rx = sci_stop_rx,
1181 .enable_ms = sci_enable_ms,
1182 .break_ctl = sci_break_ctl,
1183 .startup = sci_startup,
1184 .shutdown = sci_shutdown,
1185 .set_termios = sci_set_termios,
1186 .type = sci_type,
1187 .release_port = sci_release_port,
1188 .request_port = sci_request_port,
1189 .config_port = sci_config_port,
1190 .verify_port = sci_verify_port,
1191};
1192
Paul Mundte108b2c2006-09-27 16:32:13 +09001193static void __init sci_init_ports(void)
1194{
1195 static int first = 1;
1196 int i;
1197
1198 if (!first)
1199 return;
1200
1201 first = 0;
1202
1203 for (i = 0; i < SCI_NPORTS; i++) {
1204 sci_ports[i].port.ops = &sci_uart_ops;
1205 sci_ports[i].port.iotype = UPIO_MEM;
1206 sci_ports[i].port.line = i;
1207 sci_ports[i].port.fifosize = 1;
1208
1209#if defined(__H8300H__) || defined(__H8300S__)
1210#ifdef __H8300S__
1211 sci_ports[i].enable = h8300_sci_enable;
1212 sci_ports[i].disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#endif
Paul Mundte108b2c2006-09-27 16:32:13 +09001214 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1215#elif defined(CONFIG_SUPERH64)
1216 sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
1217#else
1218 /*
1219 * XXX: We should use a proper SCI/SCIF clock
1220 */
1221 {
Paul Mundt1d118562006-12-01 13:15:14 +09001222 struct clk *clk = clk_get(NULL, "module_clk");
Paul Mundte108b2c2006-09-27 16:32:13 +09001223 sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
1224 clk_put(clk);
1225 }
1226#endif
1227
1228 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1229 sci_ports[i].break_timer.function = sci_break_timer;
1230
1231 init_timer(&sci_ports[i].break_timer);
1232 }
1233}
1234
1235int __init early_sci_setup(struct uart_port *port)
1236{
1237 if (unlikely(port->line > SCI_NPORTS))
1238 return -ENODEV;
1239
1240 sci_init_ports();
1241
1242 sci_ports[port->line].port.membase = port->membase;
1243 sci_ports[port->line].port.mapbase = port->mapbase;
1244 sci_ports[port->line].port.type = port->type;
1245
1246 return 0;
1247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1250/*
1251 * Print a string to the serial port trying not to disturb
1252 * any possible real use of the port...
1253 */
1254static void serial_console_write(struct console *co, const char *s,
1255 unsigned count)
1256{
1257 put_string(serial_console_port, s, count);
1258}
1259
1260static int __init serial_console_setup(struct console *co, char *options)
1261{
1262 struct uart_port *port;
1263 int baud = 115200;
1264 int bits = 8;
1265 int parity = 'n';
1266 int flow = 'n';
1267 int ret;
1268
Paul Mundte108b2c2006-09-27 16:32:13 +09001269 /*
1270 * Check whether an invalid uart number has been specified, and
1271 * if so, search for the first available port that does have
1272 * console support.
1273 */
1274 if (co->index >= SCI_NPORTS)
1275 co->index = 0;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 serial_console_port = &sci_ports[co->index];
1278 port = &serial_console_port->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001281 * Also need to check port->type, we don't actually have any
1282 * UPIO_PORT ports, but uart_report_port() handily misreports
1283 * it anyways if we don't have a port available by the time this is
1284 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001286 if (!port->type)
1287 return -ENODEV;
1288 if (!port->membase || !port->mapbase)
1289 return -ENODEV;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001290
Paul Mundte108b2c2006-09-27 16:32:13 +09001291 port->type = serial_console_port->type;
1292
Paul Mundt005a3362007-04-26 11:45:32 +09001293#if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
1294 if (!serial_console_port->clk)
1295 serial_console_port->clk = clk_get(NULL, "module_clk");
1296#endif
1297
Paul Mundte108b2c2006-09-27 16:32:13 +09001298 if (port->flags & UPF_IOREMAP)
1299 sci_config_port(port, 0);
1300
1301 if (serial_console_port->enable)
1302 serial_console_port->enable(port);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (options)
1305 uart_parse_options(options, &baud, &parity, &bits, &flow);
1306
1307 ret = uart_set_options(port, co, baud, parity, bits, flow);
1308#if defined(__H8300H__) || defined(__H8300S__)
1309 /* disable rx interrupt */
1310 if (ret == 0)
1311 sci_stop_rx(port);
1312#endif
1313 return ret;
1314}
1315
1316static struct console serial_console = {
1317 .name = "ttySC",
1318 .device = uart_console_device,
1319 .write = serial_console_write,
1320 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001321 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 .index = -1,
1323 .data = &sci_uart_driver,
1324};
1325
1326static int __init sci_console_init(void)
1327{
Paul Mundte108b2c2006-09-27 16:32:13 +09001328 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 register_console(&serial_console);
1330 return 0;
1331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332console_initcall(sci_console_init);
1333#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1334
Paul Mundt68362e02007-09-11 15:27:29 +09001335#ifdef CONFIG_SH_KGDB_CONSOLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/*
1337 * FIXME: Most of this can go away.. at the moment, we rely on
1338 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1339 * most of that can easily be done here instead.
1340 *
1341 * For the time being, just accept the values that were parsed earlier..
1342 */
1343static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1344 int *parity, int *bits)
1345{
1346 *baud = kgdb_baud;
1347 *parity = tolower(kgdb_parity);
1348 *bits = kgdb_bits - '0';
1349}
1350
1351/*
1352 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1353 * care of the early-on initialization for kgdb, regardless of whether we
1354 * actually use kgdb as a console or not.
1355 *
1356 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1357 */
1358int __init kgdb_console_setup(struct console *co, char *options)
1359{
1360 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1361 int baud = 38400;
1362 int bits = 8;
1363 int parity = 'n';
1364 int flow = 'n';
1365
Paul Mundtb7a76e42006-02-01 03:06:06 -08001366 if (co->index != kgdb_portnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 co->index = kgdb_portnum;
1368
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001369 kgdb_sci_port = &sci_ports[co->index];
1370 port = &kgdb_sci_port->port;
1371
1372 /*
1373 * Also need to check port->type, we don't actually have any
1374 * UPIO_PORT ports, but uart_report_port() handily misreports
1375 * it anyways if we don't have a port available by the time this is
1376 * called.
1377 */
1378 if (!port->type)
1379 return -ENODEV;
1380 if (!port->membase || !port->mapbase)
1381 return -ENODEV;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (options)
1384 uart_parse_options(options, &baud, &parity, &bits, &flow);
1385 else
1386 kgdb_console_get_options(port, &baud, &parity, &bits);
1387
1388 kgdb_getchar = kgdb_sci_getchar;
1389 kgdb_putchar = kgdb_sci_putchar;
1390
1391 return uart_set_options(port, co, baud, parity, bits, flow);
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394static struct console kgdb_console = {
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001395 .name = "ttySC",
1396 .device = uart_console_device,
1397 .write = kgdb_console_write,
1398 .setup = kgdb_console_setup,
1399 .flags = CON_PRINTBUFFER,
1400 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 .data = &sci_uart_driver,
1402};
1403
1404/* Register the KGDB console so we get messages (d'oh!) */
1405static int __init kgdb_console_init(void)
1406{
Paul Mundte108b2c2006-09-27 16:32:13 +09001407 sci_init_ports();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 register_console(&kgdb_console);
1409 return 0;
1410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411console_initcall(kgdb_console_init);
1412#endif /* CONFIG_SH_KGDB_CONSOLE */
1413
1414#if defined(CONFIG_SH_KGDB_CONSOLE)
1415#define SCI_CONSOLE &kgdb_console
1416#elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1417#define SCI_CONSOLE &serial_console
1418#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001419#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420#endif
1421
1422static char banner[] __initdata =
1423 KERN_INFO "SuperH SCI(F) driver initialized\n";
1424
1425static struct uart_driver sci_uart_driver = {
1426 .owner = THIS_MODULE,
1427 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 .dev_name = "ttySC",
1429 .major = SCI_MAJOR,
1430 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001431 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 .cons = SCI_CONSOLE,
1433};
1434
Paul Mundte108b2c2006-09-27 16:32:13 +09001435/*
1436 * Register a set of serial devices attached to a platform device. The
1437 * list is terminated with a zero flags entry, which means we expect
1438 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1439 * remapping (such as sh64) should also set UPF_IOREMAP.
1440 */
1441static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Paul Mundte108b2c2006-09-27 16:32:13 +09001443 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001444 int i, ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Paul Mundt32351a22007-03-12 14:38:59 +09001446 for (i = 0; p && p->flags != 0; p++, i++) {
Paul Mundte108b2c2006-09-27 16:32:13 +09001447 struct sci_port *sciport = &sci_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Paul Mundt32351a22007-03-12 14:38:59 +09001449 /* Sanity check */
1450 if (unlikely(i == SCI_NPORTS)) {
1451 dev_notice(&dev->dev, "Attempting to register port "
1452 "%d when only %d are available.\n",
1453 i+1, SCI_NPORTS);
1454 dev_notice(&dev->dev, "Consider bumping "
1455 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1456 break;
1457 }
1458
Paul Mundte108b2c2006-09-27 16:32:13 +09001459 sciport->port.mapbase = p->mapbase;
Paul Mundtb7a76e42006-02-01 03:06:06 -08001460
Paul Mundt7ff731a2008-10-01 15:46:58 +09001461 if (p->mapbase && !p->membase) {
1462 if (p->flags & UPF_IOREMAP) {
1463 p->membase = ioremap_nocache(p->mapbase, 0x40);
1464 if (IS_ERR(p->membase)) {
1465 ret = PTR_ERR(p->membase);
1466 goto err_unreg;
1467 }
1468 } else {
1469 /*
1470 * For the simple (and majority of) cases
1471 * where we don't need to do any remapping,
1472 * just cast the cookie directly.
1473 */
1474 p->membase = (void __iomem *)p->mapbase;
1475 }
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Paul Mundte108b2c2006-09-27 16:32:13 +09001478 sciport->port.membase = p->membase;
1479
1480 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1481 sciport->port.flags = p->flags;
1482 sciport->port.dev = &dev->dev;
1483
1484 sciport->type = sciport->port.type = p->type;
1485
1486 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1487
1488 uart_add_one_port(&sci_uart_driver, &sciport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001491#if defined(CONFIG_SH_KGDB) && !defined(CONFIG_SH_KGDB_CONSOLE)
1492 kgdb_sci_port = &sci_ports[kgdb_portnum];
1493 kgdb_getchar = kgdb_sci_getchar;
1494 kgdb_putchar = kgdb_sci_putchar;
1495#endif
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497#ifdef CONFIG_CPU_FREQ
1498 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
Paul Mundte289fd92007-08-08 18:09:13 +09001499 dev_info(&dev->dev, "CPU frequency notifier registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#endif
1501
1502#ifdef CONFIG_SH_STANDARD_BIOS
1503 sh_bios_gdb_detach();
1504#endif
1505
Paul Mundte108b2c2006-09-27 16:32:13 +09001506 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001507
1508err_unreg:
1509 for (i = i - 1; i >= 0; i--)
1510 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1511
1512 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001513}
1514
1515static int __devexit sci_remove(struct platform_device *dev)
1516{
1517 int i;
1518
1519 for (i = 0; i < SCI_NPORTS; i++)
1520 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1521
1522 return 0;
1523}
1524
1525static int sci_suspend(struct platform_device *dev, pm_message_t state)
1526{
1527 int i;
1528
1529 for (i = 0; i < SCI_NPORTS; i++) {
1530 struct sci_port *p = &sci_ports[i];
1531
1532 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1533 uart_suspend_port(&sci_uart_driver, &p->port);
1534 }
1535
1536 return 0;
1537}
1538
1539static int sci_resume(struct platform_device *dev)
1540{
1541 int i;
1542
1543 for (i = 0; i < SCI_NPORTS; i++) {
1544 struct sci_port *p = &sci_ports[i];
1545
1546 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1547 uart_resume_port(&sci_uart_driver, &p->port);
1548 }
1549
1550 return 0;
1551}
1552
1553static struct platform_driver sci_driver = {
1554 .probe = sci_probe,
1555 .remove = __devexit_p(sci_remove),
1556 .suspend = sci_suspend,
1557 .resume = sci_resume,
1558 .driver = {
1559 .name = "sh-sci",
1560 .owner = THIS_MODULE,
1561 },
1562};
1563
1564static int __init sci_init(void)
1565{
1566 int ret;
1567
1568 printk(banner);
1569
1570 sci_init_ports();
1571
1572 ret = uart_register_driver(&sci_uart_driver);
1573 if (likely(ret == 0)) {
1574 ret = platform_driver_register(&sci_driver);
1575 if (unlikely(ret))
1576 uart_unregister_driver(&sci_uart_driver);
1577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return ret;
1580}
1581
1582static void __exit sci_exit(void)
1583{
Paul Mundte108b2c2006-09-27 16:32:13 +09001584 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 uart_unregister_driver(&sci_uart_driver);
1586}
1587
1588module_init(sci_init);
1589module_exit(sci_exit);
1590
Paul Mundte108b2c2006-09-27 16:32:13 +09001591MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001592MODULE_ALIAS("platform:sh-sci");