blob: 403b01b382e268edb734a525c1322debd67822bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090051
52#ifdef CONFIG_SUPERH
Paul Mundtb7a76e42006-02-01 03:06:06 -080053#include <asm/clock.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Yoshinori Sato168f3622009-04-28 04:40:15 +000057#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "sh-sci.h"
62
Paul Mundte108b2c2006-09-27 16:32:13 +090063struct sci_port {
64 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Paul Mundte108b2c2006-09-27 16:32:13 +090066 /* Port type */
67 unsigned int type;
68
69 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090070 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090071
Paul Mundte108b2c2006-09-27 16:32:13 +090072 /* Port enable callback */
73 void (*enable)(struct uart_port *port);
74
75 /* Port disable callback */
76 void (*disable)(struct uart_port *port);
77
78 /* Break timer */
79 struct timer_list break_timer;
80 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090081
Paul Mundt00b9de92009-06-24 17:53:33 +090082 /* SCSCR initialization */
83 unsigned int scscr;
84
Paul Mundt26c92f32009-06-24 18:23:52 +090085 /* SCBRR calculation algo */
86 unsigned int scbrr_algo_id;
87
Paul Mundta2159b52008-10-02 19:09:13 +090088#ifdef CONFIG_HAVE_CLK
Magnus Damm501b8252009-01-21 15:14:30 +000089 /* Interface clock */
90 struct clk *iclk;
91 /* Data clock */
92 struct clk *dclk;
Paul Mundt005a3362007-04-26 11:45:32 +090093#endif
Magnus Damme552de22009-01-21 15:13:42 +000094 struct list_head node;
95};
96
97struct sh_sci_priv {
98 spinlock_t lock;
99 struct list_head ports;
100
101#ifdef CONFIG_HAVE_CLK
102 struct notifier_block clk_nb;
103#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100107static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Paul Mundte108b2c2006-09-27 16:32:13 +0900109#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
110
111static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct uart_driver sci_uart_driver;
113
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900114static inline struct sci_port *
115to_sci_port(struct uart_port *uart)
116{
117 return container_of(uart, struct sci_port, port);
118}
119
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900120#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900121
122#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900123static inline void handle_error(struct uart_port *port)
124{
125 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
127}
128
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900129static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned short status;
132 int c;
133
Paul Mundte108b2c2006-09-27 16:32:13 +0900134 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 status = sci_in(port, SCxSR);
136 if (status & SCxSR_ERRORS(port)) {
137 handle_error(port);
138 continue;
139 }
140 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900143
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900144 /* Dummy read */
145 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return c;
149}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900152static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 unsigned short status;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 do {
157 status = sci_in(port, SCxSR);
158 } while (!(status & SCxSR_TDxE(port)));
159
Paul Mundt272966c2008-11-13 17:46:06 +0900160 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000161 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900163#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165#if defined(__H8300S__)
166enum { sci_disable, sci_enable };
167
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900168static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900170 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int ch = (port->mapbase - SMR0) >> 3;
172 unsigned char mask = 1 << (ch+1);
173
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900174 if (ctrl == sci_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *mstpcrl |= mask;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900176 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 *mstpcrl &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Paul Mundte108b2c2006-09-27 16:32:13 +0900179
Magnus Damm501b8252009-01-21 15:14:30 +0000180static void h8300_sci_enable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900181{
182 h8300_sci_config(port, sci_enable);
183}
184
Magnus Damm501b8252009-01-21 15:14:30 +0000185static void h8300_sci_disable(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900186{
187 h8300_sci_config(port, sci_disable);
188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
Paul Mundt15c73aa2008-10-02 19:47:12 +0900191#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900192static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int ch = (port->mapbase - SMR0) >> 3;
195
196 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900197 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
198 h8300_sci_pins[ch].rx,
199 H8300_GPIO_INPUT);
200 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
201 h8300_sci_pins[ch].tx,
202 H8300_GPIO_OUTPUT);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* tx mark output*/
205 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
206}
Paul Mundtd5701642008-12-16 20:07:27 +0900207#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
208static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900209{
Paul Mundtd5701642008-12-16 20:07:27 +0900210 if (port->mapbase == 0xA4400000) {
211 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
212 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
213 } else if (port->mapbase == 0xA4410000)
214 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900215}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900216#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900217static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 unsigned short data;
220
221 if (cflag & CRTSCTS) {
222 /* enable RTS/CTS */
223 if (port->mapbase == 0xa4430000) { /* SCIF0 */
224 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900225 data = __raw_readw(PORT_PTCR);
226 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900227 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
228 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900229 data = __raw_readw(PORT_PVCR);
230 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900231 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900232 } else {
233 if (port->mapbase == 0xa4430000) { /* SCIF0 */
234 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900235 data = __raw_readw(PORT_PTCR);
236 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900237 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
238 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900239 data = __raw_readw(PORT_PVCR);
240 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900241 }
242 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900243}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800244#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900245/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900246static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800248 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Paul Mundtb7a76e42006-02-01 03:06:06 -0800250 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900251 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800252 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900253 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800254
Paul Mundtd5701642008-12-16 20:07:27 +0900255 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900257 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Clear out SCP7MD1,0, SCP4MD1,0,
259 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900260 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 data = ctrl_inb(SCPDR);
263 /* Set /RTS2 (bit6) = 0 */
Paul Mundtb7a76e42006-02-01 03:06:06 -0800264 ctrl_outb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Paul Mundt41504c32006-12-11 20:28:03 +0900267#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900268static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900269{
Magnus Damm346b7462008-04-23 21:25:29 +0900270 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900271
Magnus Damm346b7462008-04-23 21:25:29 +0900272 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900273 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900274 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900275 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900276 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900277
Paul Mundtd5701642008-12-16 20:07:27 +0900278 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900279 }
Paul Mundt41504c32006-12-11 20:28:03 +0900280}
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900281#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900283 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900284 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900285 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900286static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
287{
288 if (!(cflag & CRTSCTS))
289 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
290}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900291#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900292static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
293{
294 if (!(cflag & CRTSCTS))
295 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
296}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800297#else
Paul Mundtd5701642008-12-16 20:07:27 +0900298static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
299{
300 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Paul Mundte108b2c2006-09-27 16:32:13 +0900302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Paul Mundt32351a22007-03-12 14:38:59 +0900304#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
305 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900306 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
307 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900308static inline int scif_txroom(struct uart_port *port)
309{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900310 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900311}
312
313static inline int scif_rxroom(struct uart_port *port)
314{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900315 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900316}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
318static inline int scif_txroom(struct uart_port *port)
319{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900320 if ((port->mapbase == 0xffe00000) ||
321 (port->mapbase == 0xffe08000)) {
322 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900323 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900324 } else {
325 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900326 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900327 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900328}
329
330static inline int scif_rxroom(struct uart_port *port)
331{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900332 if ((port->mapbase == 0xffe00000) ||
333 (port->mapbase == 0xffe08000)) {
334 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900335 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900336 } else {
337 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900338 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900339 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900340}
Paul Mundte108b2c2006-09-27 16:32:13 +0900341#else
342static inline int scif_txroom(struct uart_port *port)
343{
344 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
345}
346
347static inline int scif_rxroom(struct uart_port *port)
348{
349 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Paul Mundte108b2c2006-09-27 16:32:13 +0900353static inline int sci_txroom(struct uart_port *port)
354{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900355 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900356}
357
358static inline int sci_rxroom(struct uart_port *port)
359{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900360 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* ********************************************************************** *
364 * the interrupt related routines *
365 * ********************************************************************** */
366
367static void sci_transmit_chars(struct uart_port *port)
368{
369 struct circ_buf *xmit = &port->info->xmit;
370 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned short status;
372 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900373 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 status = sci_in(port, SCxSR);
376 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900378 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900379 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900380 else
Paul Mundt8e698612009-06-24 19:44:32 +0900381 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return;
384 }
385
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900386 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900387 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900388 else
389 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 do {
392 unsigned char c;
393
394 if (port->x_char) {
395 c = port->x_char;
396 port->x_char = 0;
397 } else if (!uart_circ_empty(xmit) && !stopped) {
398 c = xmit->buf[xmit->tail];
399 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
400 } else {
401 break;
402 }
403
404 sci_out(port, SCxTDR, c);
405
406 port->icount.tx++;
407 } while (--count > 0);
408
409 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
410
411 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
412 uart_write_wakeup(port);
413 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100414 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ctrl = sci_in(port, SCSCR);
417
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900418 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sci_in(port, SCxSR); /* Dummy read */
420 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Paul Mundt8e698612009-06-24 19:44:32 +0900423 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426}
427
428/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900429#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David Howells7d12e782006-10-05 14:55:46 +0100431static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900433 struct sci_port *sci_port = to_sci_port(port);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100434 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int i, count, copied = 0;
436 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800437 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 status = sci_in(port, SCxSR);
440 if (!(status & SCxSR_RDxF(port)))
441 return;
442
443 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900444 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900445 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900446 else
447 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800450 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* If for any reason we can't copy more data, we're done! */
453 if (count == 0)
454 break;
455
456 if (port->type == PORT_SCI) {
457 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900458 if (uart_handle_sysrq_char(port, c) ||
459 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900461 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900462 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900464 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 char c = sci_in(port, SCxRDR);
466 status = sci_in(port, SCxSR);
467#if defined(CONFIG_CPU_SH3)
468 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900469 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if ((c == 0) &&
471 (status & SCxSR_FER(port))) {
472 count--; i--;
473 continue;
474 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900477 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900478 sci_port->break_flag = 0;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (STEPFN(c)) {
481 count--; i--;
482 continue;
483 }
484 }
485#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100486 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 count--; i--;
488 continue;
489 }
490
491 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800493 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900494 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800496 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900497 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800498 } else
499 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900500
Alan Cox33f0f882006-01-09 20:54:13 -0800501 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504
505 sci_in(port, SCxSR); /* dummy read */
506 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 copied += count;
509 port->icount.rx += count;
510 }
511
512 if (copied) {
513 /* Tell the rest of the system the news. New characters! */
514 tty_flip_buffer_push(tty);
515 } else {
516 sci_in(port, SCxSR); /* dummy read */
517 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
518 }
519}
520
521#define SCI_BREAK_JIFFIES (HZ/20)
522/* The sci generates interrupts during the break,
523 * 1 per millisecond or so during the break period, for 9600 baud.
524 * So dont bother disabling interrupts.
525 * But dont want more than 1 break event.
526 * Use a kernel timer to periodically poll the rx line until
527 * the break is finished.
528 */
529static void sci_schedule_break_timer(struct sci_port *port)
530{
531 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
532 add_timer(&port->break_timer);
533}
534/* Ensure that two consecutive samples find the break over. */
535static void sci_break_timer(unsigned long data)
536{
Paul Mundte108b2c2006-09-27 16:32:13 +0900537 struct sci_port *port = (struct sci_port *)data;
538
539 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900541 sci_schedule_break_timer(port);
542 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* break is over. */
544 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900545 sci_schedule_break_timer(port);
546 } else
547 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static inline int sci_handle_errors(struct uart_port *port)
551{
552 int copied = 0;
553 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100554 struct tty_struct *tty = port->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Paul Mundte108b2c2006-09-27 16:32:13 +0900556 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900558 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800559 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900560
561 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Paul Mundte108b2c2006-09-27 16:32:13 +0900564 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (sci_rxd_in(port) == 0) {
566 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900567 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900568
569 if (!sci_port->break_flag) {
570 sci_port->break_flag = 1;
571 sci_schedule_break_timer(sci_port);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900576
577 dev_dbg(port->dev, "BREAK detected\n");
578
Paul Mundte108b2c2006-09-27 16:32:13 +0900579 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900580 copied++;
581 }
582
Paul Mundte108b2c2006-09-27 16:32:13 +0900583 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900585 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800586 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900587
588 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 }
591
Paul Mundte108b2c2006-09-27 16:32:13 +0900592 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900594 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
595 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900596
597 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Alan Cox33f0f882006-01-09 20:54:13 -0800600 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 return copied;
604}
605
Paul Mundtd830fa42008-12-16 19:29:38 +0900606static inline int sci_handle_fifo_overrun(struct uart_port *port)
607{
608 struct tty_struct *tty = port->info->port.tty;
609 int copied = 0;
610
611 if (port->type != PORT_SCIF)
612 return 0;
613
614 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
615 sci_out(port, SCLSR, 0);
616
617 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
618 tty_flip_buffer_push(tty);
619
620 dev_notice(port->dev, "overrun error\n");
621 copied++;
622 }
623
624 return copied;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static inline int sci_handle_breaks(struct uart_port *port)
628{
629 int copied = 0;
630 unsigned short status = sci_in(port, SCxSR);
Takashi Iwaia88487c2008-07-16 21:54:42 +0100631 struct tty_struct *tty = port->info->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000632 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900634 if (uart_handle_break(port))
635 return 0;
636
Paul Mundtb7a76e42006-02-01 03:06:06 -0800637 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#if defined(CONFIG_CPU_SH3)
639 /* Debounce break */
640 s->break_flag = 1;
641#endif
642 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900643 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800644 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900645
646 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Alan Cox33f0f882006-01-09 20:54:13 -0800649 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900651
Paul Mundtd830fa42008-12-16 19:29:38 +0900652 copied += sci_handle_fifo_overrun(port);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return copied;
655}
656
David Howells7d12e782006-10-05 14:55:46 +0100657static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* I think sci_receive_chars has to be called irrespective
660 * of whether the I_IXOFF is set, otherwise, how is the interrupt
661 * to be disabled?
662 */
David Howells7d12e782006-10-05 14:55:46 +0100663 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return IRQ_HANDLED;
666}
667
David Howells7d12e782006-10-05 14:55:46 +0100668static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct uart_port *port = ptr;
671
Paul Mundte108b2c2006-09-27 16:32:13 +0900672 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 sci_transmit_chars(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900674 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return IRQ_HANDLED;
677}
678
David Howells7d12e782006-10-05 14:55:46 +0100679static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct uart_port *port = ptr;
682
683 /* Handle errors */
684 if (port->type == PORT_SCI) {
685 if (sci_handle_errors(port)) {
686 /* discard character in rx buffer */
687 sci_in(port, SCxSR);
688 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
689 }
690 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900691 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100692 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
695 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
696
697 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100698 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 return IRQ_HANDLED;
701}
702
David Howells7d12e782006-10-05 14:55:46 +0100703static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct uart_port *port = ptr;
706
707 /* Handle BREAKs */
708 sci_handle_breaks(port);
709 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
710
711 return IRQ_HANDLED;
712}
713
David Howells7d12e782006-10-05 14:55:46 +0100714static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Michael Trimarchia8884e32008-10-31 16:10:23 +0900716 unsigned short ssr_status, scr_status;
717 struct uart_port *port = ptr;
718 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900720 ssr_status = sci_in(port, SCxSR);
721 scr_status = sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* Tx Interrupt */
Paul Mundt8e698612009-06-24 19:44:32 +0900724 if ((ssr_status & 0x0020) && (scr_status & SCSCR_TIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900725 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Rx Interrupt */
Paul Mundt8e698612009-06-24 19:44:32 +0900727 if ((ssr_status & 0x0002) && (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900728 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Error Interrupt */
Paul Mundt8e698612009-06-24 19:44:32 +0900730 if ((ssr_status & 0x0080) && (scr_status & SCSCR_REIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900731 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* Break Interrupt */
Paul Mundt8e698612009-06-24 19:44:32 +0900733 if ((ssr_status & 0x0010) && (scr_status & SCSCR_REIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900734 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Michael Trimarchia8884e32008-10-31 16:10:23 +0900736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Paul Mundt027e6872008-12-16 18:36:16 +0900739#ifdef CONFIG_HAVE_CLK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
741 * Here we define a transistion notifier so that we can update all of our
742 * ports' baud rate when the peripheral clock changes.
743 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900744static int sci_notifier(struct notifier_block *self,
745 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Magnus Damme552de22009-01-21 15:13:42 +0000747 struct sh_sci_priv *priv = container_of(self,
748 struct sh_sci_priv, clk_nb);
749 struct sci_port *sci_port;
750 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000753 (phase == CPUFREQ_RESUMECHANGE)) {
754 spin_lock_irqsave(&priv->lock, flags);
755 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000756 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000757
758 spin_unlock_irqrestore(&priv->lock, flags);
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return NOTIFY_OK;
762}
Magnus Damm501b8252009-01-21 15:14:30 +0000763
764static void sci_clk_enable(struct uart_port *port)
765{
766 struct sci_port *sci_port = to_sci_port(port);
767
768 clk_enable(sci_port->dclk);
769 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
770
771 if (sci_port->iclk)
772 clk_enable(sci_port->iclk);
773}
774
775static void sci_clk_disable(struct uart_port *port)
776{
777 struct sci_port *sci_port = to_sci_port(port);
778
779 if (sci_port->iclk)
780 clk_disable(sci_port->iclk);
781
782 clk_disable(sci_port->dclk);
783}
Paul Mundt027e6872008-12-16 18:36:16 +0900784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786static int sci_request_irq(struct sci_port *port)
787{
788 int i;
David Howells7d12e782006-10-05 14:55:46 +0100789 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
791 sci_br_interrupt,
792 };
793 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
794 "SCI Transmit Data Empty", "SCI Break" };
795
796 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900797 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900799
800 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900801 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900802 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -ENODEV;
804 }
805 } else {
806 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900807 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900809
Paul Mundte108b2c2006-09-27 16:32:13 +0900810 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900811 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900812 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -ENODEV;
814 }
815 }
816 }
817
818 return 0;
819}
820
821static void sci_free_irq(struct sci_port *port)
822{
823 int i;
824
Paul Mundt762c69e2008-12-16 18:55:26 +0900825 if (port->irqs[0] == port->irqs[1])
826 free_irq(port->irqs[0], port);
827 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
829 if (!port->irqs[i])
830 continue;
831
832 free_irq(port->irqs[i], port);
833 }
834 }
835}
836
837static unsigned int sci_tx_empty(struct uart_port *port)
838{
839 /* Can't detect */
840 return TIOCSER_TEMT;
841}
842
843static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
844{
845 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
846 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
847 /* If you have signals for DTR and DCD, please implement here. */
848}
849
850static unsigned int sci_get_mctrl(struct uart_port *port)
851{
852 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
853 and CTS/RTS */
854
855 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
856}
857
Russell Kingb129a8c2005-08-31 10:12:14 +0100858static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Paul Mundte108b2c2006-09-27 16:32:13 +0900860 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Paul Mundte108b2c2006-09-27 16:32:13 +0900862 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
863 ctrl = sci_in(port, SCSCR);
Paul Mundt8e698612009-06-24 19:44:32 +0900864 ctrl |= SCSCR_TIE;
Paul Mundte108b2c2006-09-27 16:32:13 +0900865 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Russell Kingb129a8c2005-08-31 10:12:14 +0100868static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned short ctrl;
871
872 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ctrl = sci_in(port, SCSCR);
Paul Mundt8e698612009-06-24 19:44:32 +0900874 ctrl &= ~SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned short ctrl;
881
882 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ctrl = sci_in(port, SCSCR);
Paul Mundt8e698612009-06-24 19:44:32 +0900884 ctrl |= SCSCR_RIE | SCSCR_REIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static void sci_stop_rx(struct uart_port *port)
889{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 unsigned short ctrl;
891
892 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 ctrl = sci_in(port, SCSCR);
Paul Mundt8e698612009-06-24 19:44:32 +0900894 ctrl &= ~(SCSCR_RIE | SCSCR_REIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static void sci_enable_ms(struct uart_port *port)
899{
900 /* Nothing here yet .. */
901}
902
903static void sci_break_ctl(struct uart_port *port, int break_state)
904{
905 /* Nothing here yet .. */
906}
907
908static int sci_startup(struct uart_port *port)
909{
Magnus Damma5660ad2009-01-21 15:14:38 +0000910 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Paul Mundte108b2c2006-09-27 16:32:13 +0900912 if (s->enable)
913 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700916 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sci_start_rx(port, 1);
918
919 return 0;
920}
921
922static void sci_shutdown(struct uart_port *port)
923{
Magnus Damma5660ad2009-01-21 15:14:38 +0000924 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100927 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 sci_free_irq(s);
929
Paul Mundte108b2c2006-09-27 16:32:13 +0900930 if (s->disable)
931 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Paul Mundt26c92f32009-06-24 18:23:52 +0900934static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
935 unsigned long freq)
936{
937 switch (algo_id) {
938 case SCBRR_ALGO_1:
939 return ((freq + 16 * bps) / (16 * bps) - 1);
940 case SCBRR_ALGO_2:
941 return ((freq + 16 * bps) / (32 * bps) - 1);
942 case SCBRR_ALGO_3:
943 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
944 case SCBRR_ALGO_4:
945 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
946 case SCBRR_ALGO_5:
947 return (((freq * 1000 / 32) / bps) - 1);
948 }
949
950 /* Warn, but use a safe default */
951 WARN_ON(1);
952 return ((freq + 16 * bps) / (32 * bps) - 1);
953}
954
Alan Cox606d0992006-12-08 02:38:45 -0800955static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
956 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Paul Mundt00b9de92009-06-24 17:53:33 +0900958 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unsigned int status, baud, smr_val;
Paul Mundta2159b52008-10-02 19:09:13 +0900960 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Paul Mundta2159b52008-10-02 19:09:13 +0900963 if (likely(baud))
Paul Mundt26c92f32009-06-24 18:23:52 +0900964 t = sci_scbrr_calc(s->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Paul Mundte108b2c2006-09-27 16:32:13 +0900966 do {
967 status = sci_in(port, SCxSR);
968 } while (!(status & SCxSR_TEND(port)));
969
970 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
971
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900972 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900973 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900974
975 smr_val = sci_in(port, SCSMR) & 3;
976 if ((termios->c_cflag & CSIZE) == CS7)
977 smr_val |= 0x40;
978 if (termios->c_cflag & PARENB)
979 smr_val |= 0x20;
980 if (termios->c_cflag & PARODD)
981 smr_val |= 0x30;
982 if (termios->c_cflag & CSTOPB)
983 smr_val |= 0x08;
984
985 uart_update_timeout(port, termios->c_cflag, baud);
986
987 sci_out(port, SCSMR, smr_val);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900990 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
992 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900993 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sci_out(port, SCBRR, t);
997 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
998 }
999
Paul Mundtd5701642008-12-16 20:07:27 +09001000 sci_init_pins(port, termios->c_cflag);
1001 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -08001002
Paul Mundt00b9de92009-06-24 17:53:33 +09001003 sci_out(port, SCSCR, s->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001006 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static const char *sci_type(struct uart_port *port)
1010{
1011 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001012 case PORT_IRDA:
1013 return "irda";
1014 case PORT_SCI:
1015 return "sci";
1016 case PORT_SCIF:
1017 return "scif";
1018 case PORT_SCIFA:
1019 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Paul Mundtfa439722008-09-04 18:53:58 +09001022 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static void sci_release_port(struct uart_port *port)
1026{
1027 /* Nothing here yet .. */
1028}
1029
1030static int sci_request_port(struct uart_port *port)
1031{
1032 /* Nothing here yet .. */
1033 return 0;
1034}
1035
1036static void sci_config_port(struct uart_port *port, int flags)
1037{
Magnus Damma5660ad2009-01-21 15:14:38 +00001038 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 port->type = s->type;
1041
Magnus Damm08f8cb32009-01-21 15:14:22 +00001042 if (port->membase)
1043 return;
1044
1045 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001046 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001047
1048 if (IS_ERR(port->membase))
1049 dev_err(port->dev, "can't remap port#%d\n", port->line);
1050 } else {
1051 /*
1052 * For the simple (and majority of) cases where we don't
1053 * need to do any remapping, just cast the cookie
1054 * directly.
1055 */
1056 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1061{
Magnus Damma5660ad2009-01-21 15:14:38 +00001062 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Yinghai Lua62c4132008-08-19 20:49:55 -07001064 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -EINVAL;
1066 if (ser->baud_base < 2400)
1067 /* No paper tape reader for Mitch.. */
1068 return -EINVAL;
1069
1070 return 0;
1071}
1072
1073static struct uart_ops sci_uart_ops = {
1074 .tx_empty = sci_tx_empty,
1075 .set_mctrl = sci_set_mctrl,
1076 .get_mctrl = sci_get_mctrl,
1077 .start_tx = sci_start_tx,
1078 .stop_tx = sci_stop_tx,
1079 .stop_rx = sci_stop_rx,
1080 .enable_ms = sci_enable_ms,
1081 .break_ctl = sci_break_ctl,
1082 .startup = sci_startup,
1083 .shutdown = sci_shutdown,
1084 .set_termios = sci_set_termios,
1085 .type = sci_type,
1086 .release_port = sci_release_port,
1087 .request_port = sci_request_port,
1088 .config_port = sci_config_port,
1089 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001090#ifdef CONFIG_CONSOLE_POLL
1091 .poll_get_char = sci_poll_get_char,
1092 .poll_put_char = sci_poll_put_char,
1093#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094};
1095
Magnus Damm501b8252009-01-21 15:14:30 +00001096static void __devinit sci_init_single(struct platform_device *dev,
1097 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001098 unsigned int index,
1099 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001100{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001101 sci_port->port.ops = &sci_uart_ops;
1102 sci_port->port.iotype = UPIO_MEM;
1103 sci_port->port.line = index;
1104 sci_port->port.fifosize = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +09001105
1106#if defined(__H8300H__) || defined(__H8300S__)
1107#ifdef __H8300S__
Magnus Damm7ed7e072009-01-21 15:14:14 +00001108 sci_port->enable = h8300_sci_enable;
1109 sci_port->disable = h8300_sci_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001111 sci_port->port.uartclk = CONFIG_CPU_CLOCK;
Paul Mundta2159b52008-10-02 19:09:13 +09001112#elif defined(CONFIG_HAVE_CLK)
Magnus Damm501b8252009-01-21 15:14:30 +00001113 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
Paul Mundtaf777ce2009-05-13 16:59:40 +09001114 sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
Magnus Damm501b8252009-01-21 15:14:30 +00001115 sci_port->enable = sci_clk_enable;
1116 sci_port->disable = sci_clk_disable;
Paul Mundta2159b52008-10-02 19:09:13 +09001117#else
1118#error "Need a valid uartclk"
Paul Mundte108b2c2006-09-27 16:32:13 +09001119#endif
1120
Magnus Damm7ed7e072009-01-21 15:14:14 +00001121 sci_port->break_timer.data = (unsigned long)sci_port;
1122 sci_port->break_timer.function = sci_break_timer;
1123 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001124
Magnus Damm7ed7e072009-01-21 15:14:14 +00001125 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001126 sci_port->port.membase = p->membase;
1127
Paul Mundt00b9de92009-06-24 17:53:33 +09001128 sci_port->scscr = p->scscr;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001129 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1130 sci_port->port.flags = p->flags;
Magnus Damm501b8252009-01-21 15:14:30 +00001131 sci_port->port.dev = &dev->dev;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001132 sci_port->type = sci_port->port.type = p->type;
1133
1134 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundte108b2c2006-09-27 16:32:13 +09001135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001138static struct tty_driver *serial_console_device(struct console *co, int *index)
1139{
1140 struct uart_driver *p = &sci_uart_driver;
1141 *index = co->index;
1142 return p->tty_driver;
1143}
1144
1145static void serial_console_putchar(struct uart_port *port, int ch)
1146{
1147 sci_poll_put_char(port, ch);
1148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/*
1151 * Print a string to the serial port trying not to disturb
1152 * any possible real use of the port...
1153 */
1154static void serial_console_write(struct console *co, const char *s,
1155 unsigned count)
1156{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001157 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001158 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001159 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001160
Magnus Damm501b8252009-01-21 15:14:30 +00001161 if (sci_port->enable)
1162 sci_port->enable(port);
1163
1164 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001165
1166 /* wait until fifo is empty and last bit has been transmitted */
1167 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1168 while ((sci_in(port, SCxSR) & bits) != bits)
1169 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001170
1171 if (sci_port->disable);
1172 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175static int __init serial_console_setup(struct console *co, char *options)
1176{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001177 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct uart_port *port;
1179 int baud = 115200;
1180 int bits = 8;
1181 int parity = 'n';
1182 int flow = 'n';
1183 int ret;
1184
Paul Mundte108b2c2006-09-27 16:32:13 +09001185 /*
1186 * Check whether an invalid uart number has been specified, and
1187 * if so, search for the first available port that does have
1188 * console support.
1189 */
1190 if (co->index >= SCI_NPORTS)
1191 co->index = 0;
1192
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001193 sci_port = &sci_ports[co->index];
1194 port = &sci_port->port;
1195 co->data = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001198 * Also need to check port->type, we don't actually have any
1199 * UPIO_PORT ports, but uart_report_port() handily misreports
1200 * it anyways if we don't have a port available by the time this is
1201 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001203 if (!port->type)
1204 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001205
Magnus Damm08f8cb32009-01-21 15:14:22 +00001206 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001207
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001208 if (sci_port->enable)
1209 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (options)
1212 uart_parse_options(options, &baud, &parity, &bits, &flow);
1213
1214 ret = uart_set_options(port, co, baud, parity, bits, flow);
1215#if defined(__H8300H__) || defined(__H8300S__)
1216 /* disable rx interrupt */
1217 if (ret == 0)
1218 sci_stop_rx(port);
1219#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001220 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return ret;
1222}
1223
1224static struct console serial_console = {
1225 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001226 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 .write = serial_console_write,
1228 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001229 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231};
1232
1233static int __init sci_console_init(void)
1234{
1235 register_console(&serial_console);
1236 return 0;
1237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238console_initcall(sci_console_init);
1239#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1240
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001241#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001242#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001244#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#endif
1246
1247static char banner[] __initdata =
1248 KERN_INFO "SuperH SCI(F) driver initialized\n";
1249
1250static struct uart_driver sci_uart_driver = {
1251 .owner = THIS_MODULE,
1252 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .dev_name = "ttySC",
1254 .major = SCI_MAJOR,
1255 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001256 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 .cons = SCI_CONSOLE,
1258};
1259
Magnus Damme552de22009-01-21 15:13:42 +00001260
Paul Mundt54507f62009-05-08 23:48:33 +09001261static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001262{
1263 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1264 struct sci_port *p;
1265 unsigned long flags;
1266
1267#ifdef CONFIG_HAVE_CLK
1268 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1269#endif
1270
1271 spin_lock_irqsave(&priv->lock, flags);
1272 list_for_each_entry(p, &priv->ports, node)
1273 uart_remove_one_port(&sci_uart_driver, &p->port);
1274
1275 spin_unlock_irqrestore(&priv->lock, flags);
1276
1277 kfree(priv);
1278 return 0;
1279}
1280
Magnus Damm0ee70712009-01-21 15:13:50 +00001281static int __devinit sci_probe_single(struct platform_device *dev,
1282 unsigned int index,
1283 struct plat_sci_port *p,
1284 struct sci_port *sciport)
1285{
1286 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1287 unsigned long flags;
1288 int ret;
1289
1290 /* Sanity check */
1291 if (unlikely(index >= SCI_NPORTS)) {
1292 dev_notice(&dev->dev, "Attempting to register port "
1293 "%d when only %d are available.\n",
1294 index+1, SCI_NPORTS);
1295 dev_notice(&dev->dev, "Consider bumping "
1296 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1297 return 0;
1298 }
1299
Magnus Damm501b8252009-01-21 15:14:30 +00001300 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001301
1302 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001303 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001304 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001305
1306 INIT_LIST_HEAD(&sciport->node);
1307
1308 spin_lock_irqsave(&priv->lock, flags);
1309 list_add(&sciport->node, &priv->ports);
1310 spin_unlock_irqrestore(&priv->lock, flags);
1311
1312 return 0;
1313}
1314
Paul Mundte108b2c2006-09-27 16:32:13 +09001315/*
1316 * Register a set of serial devices attached to a platform device. The
1317 * list is terminated with a zero flags entry, which means we expect
1318 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1319 * remapping (such as sh64) should also set UPF_IOREMAP.
1320 */
1321static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Paul Mundte108b2c2006-09-27 16:32:13 +09001323 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001324 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001325 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001326
1327 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1328 if (!priv)
1329 return -ENOMEM;
1330
1331 INIT_LIST_HEAD(&priv->ports);
1332 spin_lock_init(&priv->lock);
1333 platform_set_drvdata(dev, priv);
1334
1335#ifdef CONFIG_HAVE_CLK
1336 priv->clk_nb.notifier_call = sci_notifier;
1337 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
1338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Magnus Damm0ee70712009-01-21 15:13:50 +00001340 if (dev->id != -1) {
1341 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1342 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001343 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001344 } else {
1345 for (i = 0; p && p->flags != 0; p++, i++) {
1346 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1347 if (ret)
1348 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001349 }
Magnus Damme552de22009-01-21 15:13:42 +00001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352#ifdef CONFIG_SH_STANDARD_BIOS
1353 sh_bios_gdb_detach();
1354#endif
1355
Paul Mundte108b2c2006-09-27 16:32:13 +09001356 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001357
1358err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001359 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001360 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001361}
1362
Paul Mundt6daa79b2009-06-15 07:07:38 +09001363static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001364{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001365 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001366 struct sci_port *p;
1367 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001368
Magnus Damme552de22009-01-21 15:13:42 +00001369 spin_lock_irqsave(&priv->lock, flags);
1370 list_for_each_entry(p, &priv->ports, node)
1371 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001372 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001373
1374 return 0;
1375}
1376
Paul Mundt6daa79b2009-06-15 07:07:38 +09001377static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001378{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001379 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001380 struct sci_port *p;
1381 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001382
Magnus Damme552de22009-01-21 15:13:42 +00001383 spin_lock_irqsave(&priv->lock, flags);
1384 list_for_each_entry(p, &priv->ports, node)
1385 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001386 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001387
1388 return 0;
1389}
1390
Paul Mundt6daa79b2009-06-15 07:07:38 +09001391static struct dev_pm_ops sci_dev_pm_ops = {
1392 .suspend = sci_suspend,
1393 .resume = sci_resume,
1394};
1395
Paul Mundte108b2c2006-09-27 16:32:13 +09001396static struct platform_driver sci_driver = {
1397 .probe = sci_probe,
1398 .remove = __devexit_p(sci_remove),
Paul Mundte108b2c2006-09-27 16:32:13 +09001399 .driver = {
1400 .name = "sh-sci",
1401 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001402 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001403 },
1404};
1405
1406static int __init sci_init(void)
1407{
1408 int ret;
1409
1410 printk(banner);
1411
Paul Mundte108b2c2006-09-27 16:32:13 +09001412 ret = uart_register_driver(&sci_uart_driver);
1413 if (likely(ret == 0)) {
1414 ret = platform_driver_register(&sci_driver);
1415 if (unlikely(ret))
1416 uart_unregister_driver(&sci_uart_driver);
1417 }
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return ret;
1420}
1421
1422static void __exit sci_exit(void)
1423{
Paul Mundte108b2c2006-09-27 16:32:13 +09001424 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 uart_unregister_driver(&sci_uart_driver);
1426}
1427
1428module_init(sci_init);
1429module_exit(sci_exit);
1430
Paul Mundte108b2c2006-09-27 16:32:13 +09001431MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001432MODULE_ALIAS("platform:sh-sci");