Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/serial/sh-sci.c |
| 3 | * |
| 4 | * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO) |
| 5 | * |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 6 | * Copyright (C) 2002 - 2008 Paul Mundt |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 7 | * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Damm | d89ddd1 | 2007-07-25 11:42:56 +0900 | [diff] [blame] | 16 | * Removed SH7300 support (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 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 Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 22 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 23 | #define SUPPORT_SYSRQ |
| 24 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #undef DEBUG |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/ioport.h> |
| 39 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/init.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/console.h> |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 43 | #include <linux/platform_device.h> |
Paul Mundt | 96de1a8 | 2008-02-26 14:52:45 +0900 | [diff] [blame] | 44 | #include <linux/serial_sci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/notifier.h> |
| 46 | #include <linux/cpufreq.h> |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 47 | #include <linux/clk.h> |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 48 | #include <linux/ctype.h> |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 49 | #include <linux/err.h> |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 50 | #include <linux/list.h> |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 51 | |
| 52 | #ifdef CONFIG_SUPERH |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 53 | #include <asm/sh_bios.h> |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 54 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Yoshinori Sato | 168f362 | 2009-04-28 04:40:15 +0000 | [diff] [blame] | 56 | #ifdef CONFIG_H8300 |
| 57 | #include <asm/gpio.h> |
| 58 | #endif |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include "sh-sci.h" |
| 61 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 62 | struct sci_port { |
| 63 | struct uart_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 65 | /* Port type */ |
| 66 | unsigned int type; |
| 67 | |
| 68 | /* Port IRQs: ERI, RXI, TXI, BRI (optional) */ |
Paul Mundt | 32351a2 | 2007-03-12 14:38:59 +0900 | [diff] [blame] | 69 | unsigned int irqs[SCIx_NR_IRQS]; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 70 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 71 | /* Port enable callback */ |
| 72 | void (*enable)(struct uart_port *port); |
| 73 | |
| 74 | /* Port disable callback */ |
| 75 | void (*disable)(struct uart_port *port); |
| 76 | |
| 77 | /* Break timer */ |
| 78 | struct timer_list break_timer; |
| 79 | int break_flag; |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 80 | |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 81 | /* Interface clock */ |
| 82 | struct clk *iclk; |
| 83 | /* Data clock */ |
| 84 | struct clk *dclk; |
Paul Mundt | edad1f2 | 2009-11-25 16:23:35 +0900 | [diff] [blame] | 85 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 86 | struct list_head node; |
| 87 | }; |
| 88 | |
| 89 | struct sh_sci_priv { |
| 90 | spinlock_t lock; |
| 91 | struct list_head ports; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 92 | struct notifier_block clk_nb; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 93 | }; |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* Function prototypes */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 96 | static void sci_stop_tx(struct uart_port *port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 98 | #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS |
| 99 | |
| 100 | static struct sci_port sci_ports[SCI_NPORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | static struct uart_driver sci_uart_driver; |
| 102 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 103 | static inline struct sci_port * |
| 104 | to_sci_port(struct uart_port *uart) |
| 105 | { |
| 106 | return container_of(uart, struct sci_port, port); |
| 107 | } |
| 108 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 109 | #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 110 | |
| 111 | #ifdef CONFIG_CONSOLE_POLL |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 112 | static inline void handle_error(struct uart_port *port) |
| 113 | { |
| 114 | /* Clear error flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 116 | } |
| 117 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 118 | static int sci_poll_get_char(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | unsigned short status; |
| 121 | int c; |
| 122 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 123 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | status = sci_in(port, SCxSR); |
| 125 | if (status & SCxSR_ERRORS(port)) { |
| 126 | handle_error(port); |
| 127 | continue; |
| 128 | } |
| 129 | } while (!(status & SCxSR_RDxF(port))); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | c = sci_in(port, SCxRDR); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 132 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 133 | /* Dummy read */ |
| 134 | sci_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | return c; |
| 138 | } |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 139 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 141 | static void sci_poll_put_char(struct uart_port *port, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | unsigned short status; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | do { |
| 146 | status = sci_in(port, SCxSR); |
| 147 | } while (!(status & SCxSR_TDxE(port))); |
| 148 | |
Paul Mundt | 272966c | 2008-11-13 17:46:06 +0900 | [diff] [blame] | 149 | sci_out(port, SCxTDR, c); |
SUGIOKA Toshinobu | dd0a3e7 | 2009-06-01 03:53:41 +0000 | [diff] [blame] | 150 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 152 | #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Paul Mundt | 15c73aa | 2008-10-02 19:47:12 +0900 | [diff] [blame] | 154 | #if defined(__H8300H__) || defined(__H8300S__) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 155 | static void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | int ch = (port->mapbase - SMR0) >> 3; |
| 158 | |
| 159 | /* set DDR regs */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 160 | H8300_GPIO_DDR(h8300_sci_pins[ch].port, |
| 161 | h8300_sci_pins[ch].rx, |
| 162 | H8300_GPIO_INPUT); |
| 163 | H8300_GPIO_DDR(h8300_sci_pins[ch].port, |
| 164 | h8300_sci_pins[ch].tx, |
| 165 | H8300_GPIO_OUTPUT); |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* tx mark output*/ |
| 168 | H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx; |
| 169 | } |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 170 | #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712) |
| 171 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 172 | { |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 173 | if (port->mapbase == 0xA4400000) { |
| 174 | __raw_writew(__raw_readw(PACR) & 0xffc0, PACR); |
| 175 | __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR); |
| 176 | } else if (port->mapbase == 0xA4410000) |
| 177 | __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR); |
Nobuhiro Iwamatsu | 9465a54 | 2007-03-27 18:13:51 +0900 | [diff] [blame] | 178 | } |
Yoshihiro Shimoda | 31a49c4 | 2007-12-26 11:45:06 +0900 | [diff] [blame] | 179 | #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 180 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 181 | { |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 182 | unsigned short data; |
| 183 | |
| 184 | if (cflag & CRTSCTS) { |
| 185 | /* enable RTS/CTS */ |
| 186 | if (port->mapbase == 0xa4430000) { /* SCIF0 */ |
| 187 | /* Clear PTCR bit 9-2; enable all scif pins but sck */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 188 | data = __raw_readw(PORT_PTCR); |
| 189 | __raw_writew((data & 0xfc03), PORT_PTCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 190 | } else if (port->mapbase == 0xa4438000) { /* SCIF1 */ |
| 191 | /* Clear PVCR bit 9-2 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 192 | data = __raw_readw(PORT_PVCR); |
| 193 | __raw_writew((data & 0xfc03), PORT_PVCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 194 | } |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 195 | } else { |
| 196 | if (port->mapbase == 0xa4430000) { /* SCIF0 */ |
| 197 | /* Clear PTCR bit 5-2; enable only tx and rx */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 198 | data = __raw_readw(PORT_PTCR); |
| 199 | __raw_writew((data & 0xffc3), PORT_PTCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 200 | } else if (port->mapbase == 0xa4438000) { /* SCIF1 */ |
| 201 | /* Clear PVCR bit 5-2 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 202 | data = __raw_readw(PORT_PVCR); |
| 203 | __raw_writew((data & 0xffc3), PORT_PVCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 204 | } |
| 205 | } |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 206 | } |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 207 | #elif defined(CONFIG_CPU_SH3) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 208 | /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 209 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 211 | unsigned short data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 213 | /* We need to set SCPCR to enable RTS/CTS */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 214 | data = __raw_readw(SCPCR); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 215 | /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 216 | __raw_writew(data & 0x0fcf, SCPCR); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 217 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 218 | if (!(cflag & CRTSCTS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* We need to set SCPCR to enable RTS/CTS */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 220 | data = __raw_readw(SCPCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* Clear out SCP7MD1,0, SCP4MD1,0, |
| 222 | Set SCP6MD1,0 = {01} (output) */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 223 | __raw_writew((data & 0x0fcf) | 0x1000, SCPCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Paul Mundt | 32b5307 | 2009-12-24 14:52:43 +0900 | [diff] [blame] | 225 | data = __raw_readb(SCPDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* Set /RTS2 (bit6) = 0 */ |
Paul Mundt | 32b5307 | 2009-12-24 14:52:43 +0900 | [diff] [blame] | 227 | __raw_writeb(data & 0xbf, SCPDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 230 | #elif defined(CONFIG_CPU_SUBTYPE_SH7722) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 231 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 232 | { |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 233 | unsigned short data; |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 234 | |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 235 | if (port->mapbase == 0xffe00000) { |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 236 | data = __raw_readw(PSCR); |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 237 | data &= ~0x03cf; |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 238 | if (!(cflag & CRTSCTS)) |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 239 | data |= 0x0340; |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 240 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 241 | __raw_writew(data, PSCR); |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 242 | } |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 243 | } |
Yoshihiro Shimoda | c01f0f1 | 2009-08-21 16:30:28 +0900 | [diff] [blame] | 244 | #elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \ |
| 245 | defined(CONFIG_CPU_SUBTYPE_SH7763) || \ |
Yoshihiro Shimoda | 7d740a0 | 2008-01-07 14:40:07 +0900 | [diff] [blame] | 246 | defined(CONFIG_CPU_SUBTYPE_SH7780) || \ |
Paul Mundt | 2b1bd1a | 2007-06-20 18:27:10 +0900 | [diff] [blame] | 247 | defined(CONFIG_CPU_SUBTYPE_SH7785) || \ |
Kuninori Morimoto | 55ba99e | 2009-03-03 15:40:25 +0900 | [diff] [blame] | 248 | defined(CONFIG_CPU_SUBTYPE_SH7786) || \ |
Paul Mundt | 2b1bd1a | 2007-06-20 18:27:10 +0900 | [diff] [blame] | 249 | defined(CONFIG_CPU_SUBTYPE_SHX3) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 250 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 251 | { |
| 252 | if (!(cflag & CRTSCTS)) |
| 253 | __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */ |
| 254 | } |
Paul Mundt | b0c50ad | 2008-12-22 03:40:10 +0900 | [diff] [blame] | 255 | #elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 256 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 257 | { |
| 258 | if (!(cflag & CRTSCTS)) |
| 259 | __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */ |
| 260 | } |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 261 | #else |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 262 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 263 | { |
| 264 | /* Nothing to do */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 266 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Paul Mundt | 32351a2 | 2007-03-12 14:38:59 +0900 | [diff] [blame] | 268 | #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \ |
| 269 | defined(CONFIG_CPU_SUBTYPE_SH7780) || \ |
Kuninori Morimoto | 55ba99e | 2009-03-03 15:40:25 +0900 | [diff] [blame] | 270 | defined(CONFIG_CPU_SUBTYPE_SH7785) || \ |
| 271 | defined(CONFIG_CPU_SUBTYPE_SH7786) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 272 | static inline int scif_txroom(struct uart_port *port) |
| 273 | { |
Yutaro Ebihara | cae167d | 2008-03-11 13:58:50 +0900 | [diff] [blame] | 274 | return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static inline int scif_rxroom(struct uart_port *port) |
| 278 | { |
Yutaro Ebihara | cae167d | 2008-03-11 13:58:50 +0900 | [diff] [blame] | 279 | return sci_in(port, SCRFDR) & 0xff; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 280 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 281 | #elif defined(CONFIG_CPU_SUBTYPE_SH7763) |
| 282 | static inline int scif_txroom(struct uart_port *port) |
| 283 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 284 | if ((port->mapbase == 0xffe00000) || |
| 285 | (port->mapbase == 0xffe08000)) { |
| 286 | /* SCIF0/1*/ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 287 | return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 288 | } else { |
| 289 | /* SCIF2 */ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 290 | return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 291 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static inline int scif_rxroom(struct uart_port *port) |
| 295 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 296 | if ((port->mapbase == 0xffe00000) || |
| 297 | (port->mapbase == 0xffe08000)) { |
| 298 | /* SCIF0/1*/ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 299 | return sci_in(port, SCRFDR) & 0xff; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 300 | } else { |
| 301 | /* SCIF2 */ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 302 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 303 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 304 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 305 | #else |
| 306 | static inline int scif_txroom(struct uart_port *port) |
| 307 | { |
| 308 | return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8); |
| 309 | } |
| 310 | |
| 311 | static inline int scif_rxroom(struct uart_port *port) |
| 312 | { |
| 313 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 317 | static inline int sci_txroom(struct uart_port *port) |
| 318 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 319 | return (sci_in(port, SCxSR) & SCI_TDRE) != 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static inline int sci_rxroom(struct uart_port *port) |
| 323 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 324 | return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | /* ********************************************************************** * |
| 328 | * the interrupt related routines * |
| 329 | * ********************************************************************** */ |
| 330 | |
| 331 | static void sci_transmit_chars(struct uart_port *port) |
| 332 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 333 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | unsigned int stopped = uart_tx_stopped(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | unsigned short status; |
| 336 | unsigned short ctrl; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 337 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | status = sci_in(port, SCxSR); |
| 340 | if (!(status & SCxSR_TDxE(port))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | ctrl = sci_in(port, SCSCR); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 342 | if (uart_circ_empty(xmit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | ctrl &= ~SCI_CTRL_FLAGS_TIE; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 344 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | ctrl |= SCI_CTRL_FLAGS_TIE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 350 | if (port->type == PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 351 | count = sci_txroom(port); |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 352 | else |
| 353 | count = scif_txroom(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | do { |
| 356 | unsigned char c; |
| 357 | |
| 358 | if (port->x_char) { |
| 359 | c = port->x_char; |
| 360 | port->x_char = 0; |
| 361 | } else if (!uart_circ_empty(xmit) && !stopped) { |
| 362 | c = xmit->buf[xmit->tail]; |
| 363 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 364 | } else { |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | sci_out(port, SCxTDR, c); |
| 369 | |
| 370 | port->icount.tx++; |
| 371 | } while (--count > 0); |
| 372 | |
| 373 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port)); |
| 374 | |
| 375 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 376 | uart_write_wakeup(port); |
| 377 | if (uart_circ_empty(xmit)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 378 | sci_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | ctrl = sci_in(port, SCSCR); |
| 381 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 382 | if (port->type != PORT_SCI) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | sci_in(port, SCxSR); /* Dummy read */ |
| 384 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port)); |
| 385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | ctrl |= SCI_CTRL_FLAGS_TIE; |
| 388 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | /* On SH3, SCIF may read end-of-break as a space->mark char */ |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 393 | #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 395 | static inline void sci_receive_chars(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 397 | struct sci_port *sci_port = to_sci_port(port); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 398 | struct tty_struct *tty = port->state->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | int i, count, copied = 0; |
| 400 | unsigned short status; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 401 | unsigned char flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | status = sci_in(port, SCxSR); |
| 404 | if (!(status & SCxSR_RDxF(port))) |
| 405 | return; |
| 406 | |
| 407 | while (1) { |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 408 | if (port->type == PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 409 | count = sci_rxroom(port); |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 410 | else |
| 411 | count = scif_rxroom(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | /* Don't copy more bytes than there is room for in the buffer */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 414 | count = tty_buffer_request_room(tty, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | /* If for any reason we can't copy more data, we're done! */ |
| 417 | if (count == 0) |
| 418 | break; |
| 419 | |
| 420 | if (port->type == PORT_SCI) { |
| 421 | char c = sci_in(port, SCxRDR); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 422 | if (uart_handle_sysrq_char(port, c) || |
| 423 | sci_port->break_flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | count = 0; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 425 | else |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 426 | tty_insert_flip_char(tty, c, TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } else { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 428 | for (i = 0; i < count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | char c = sci_in(port, SCxRDR); |
| 430 | status = sci_in(port, SCxSR); |
| 431 | #if defined(CONFIG_CPU_SH3) |
| 432 | /* Skip "chars" during break */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 433 | if (sci_port->break_flag) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if ((c == 0) && |
| 435 | (status & SCxSR_FER(port))) { |
| 436 | count--; i--; |
| 437 | continue; |
| 438 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | /* Nonzero => end-of-break */ |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 441 | dev_dbg(port->dev, "debounce<%02x>\n", c); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 442 | sci_port->break_flag = 0; |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | if (STEPFN(c)) { |
| 445 | count--; i--; |
| 446 | continue; |
| 447 | } |
| 448 | } |
| 449 | #endif /* CONFIG_CPU_SH3 */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 450 | if (uart_handle_sysrq_char(port, c)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | count--; i--; |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | /* Store data and status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (status&SCxSR_FER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 457 | flag = TTY_FRAME; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 458 | dev_notice(port->dev, "frame error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } else if (status&SCxSR_PER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 460 | flag = TTY_PARITY; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 461 | dev_notice(port->dev, "parity error\n"); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 462 | } else |
| 463 | flag = TTY_NORMAL; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 464 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 465 | tty_insert_flip_char(tty, c, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | sci_in(port, SCxSR); /* dummy read */ |
| 470 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | copied += count; |
| 473 | port->icount.rx += count; |
| 474 | } |
| 475 | |
| 476 | if (copied) { |
| 477 | /* Tell the rest of the system the news. New characters! */ |
| 478 | tty_flip_buffer_push(tty); |
| 479 | } else { |
| 480 | sci_in(port, SCxSR); /* dummy read */ |
| 481 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | #define SCI_BREAK_JIFFIES (HZ/20) |
| 486 | /* The sci generates interrupts during the break, |
| 487 | * 1 per millisecond or so during the break period, for 9600 baud. |
| 488 | * So dont bother disabling interrupts. |
| 489 | * But dont want more than 1 break event. |
| 490 | * Use a kernel timer to periodically poll the rx line until |
| 491 | * the break is finished. |
| 492 | */ |
| 493 | static void sci_schedule_break_timer(struct sci_port *port) |
| 494 | { |
| 495 | port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES; |
| 496 | add_timer(&port->break_timer); |
| 497 | } |
| 498 | /* Ensure that two consecutive samples find the break over. */ |
| 499 | static void sci_break_timer(unsigned long data) |
| 500 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 501 | struct sci_port *port = (struct sci_port *)data; |
| 502 | |
| 503 | if (sci_rxd_in(&port->port) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | port->break_flag = 1; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 505 | sci_schedule_break_timer(port); |
| 506 | } else if (port->break_flag == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | /* break is over. */ |
| 508 | port->break_flag = 2; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 509 | sci_schedule_break_timer(port); |
| 510 | } else |
| 511 | port->break_flag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static inline int sci_handle_errors(struct uart_port *port) |
| 515 | { |
| 516 | int copied = 0; |
| 517 | unsigned short status = sci_in(port, SCxSR); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 518 | struct tty_struct *tty = port->state->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 520 | if (status & SCxSR_ORER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* overrun error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 522 | if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 523 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 524 | |
| 525 | dev_notice(port->dev, "overrun error"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 528 | if (status & SCxSR_FER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (sci_rxd_in(port) == 0) { |
| 530 | /* Notify of BREAK */ |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 531 | struct sci_port *sci_port = to_sci_port(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 532 | |
| 533 | if (!sci_port->break_flag) { |
| 534 | sci_port->break_flag = 1; |
| 535 | sci_schedule_break_timer(sci_port); |
| 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | /* Do sysrq handling. */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 538 | if (uart_handle_break(port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return 0; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 540 | |
| 541 | dev_dbg(port->dev, "BREAK detected\n"); |
| 542 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 543 | if (tty_insert_flip_char(tty, 0, TTY_BREAK)) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 544 | copied++; |
| 545 | } |
| 546 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 547 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | /* frame error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 549 | if (tty_insert_flip_char(tty, 0, TTY_FRAME)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 550 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 551 | |
| 552 | dev_notice(port->dev, "frame error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 556 | if (status & SCxSR_PER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /* parity error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 558 | if (tty_insert_flip_char(tty, 0, TTY_PARITY)) |
| 559 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 560 | |
| 561 | dev_notice(port->dev, "parity error"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 564 | if (copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 567 | return copied; |
| 568 | } |
| 569 | |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 570 | static inline int sci_handle_fifo_overrun(struct uart_port *port) |
| 571 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 572 | struct tty_struct *tty = port->state->port.tty; |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 573 | int copied = 0; |
| 574 | |
| 575 | if (port->type != PORT_SCIF) |
| 576 | return 0; |
| 577 | |
| 578 | if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) { |
| 579 | sci_out(port, SCLSR, 0); |
| 580 | |
| 581 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 582 | tty_flip_buffer_push(tty); |
| 583 | |
| 584 | dev_notice(port->dev, "overrun error\n"); |
| 585 | copied++; |
| 586 | } |
| 587 | |
| 588 | return copied; |
| 589 | } |
| 590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | static inline int sci_handle_breaks(struct uart_port *port) |
| 592 | { |
| 593 | int copied = 0; |
| 594 | unsigned short status = sci_in(port, SCxSR); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 595 | struct tty_struct *tty = port->state->port.tty; |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 596 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Paul Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 598 | if (uart_handle_break(port)) |
| 599 | return 0; |
| 600 | |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 601 | if (!s->break_flag && status & SCxSR_BRK(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | #if defined(CONFIG_CPU_SH3) |
| 603 | /* Debounce break */ |
| 604 | s->break_flag = 1; |
| 605 | #endif |
| 606 | /* Notify of BREAK */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 607 | if (tty_insert_flip_char(tty, 0, TTY_BREAK)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 608 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 609 | |
| 610 | dev_dbg(port->dev, "BREAK detected\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 613 | if (copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | tty_flip_buffer_push(tty); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 615 | |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 616 | copied += sci_handle_fifo_overrun(port); |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return copied; |
| 619 | } |
| 620 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 621 | static irqreturn_t sci_rx_interrupt(int irq, void *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | /* I think sci_receive_chars has to be called irrespective |
| 624 | * of whether the I_IXOFF is set, otherwise, how is the interrupt |
| 625 | * to be disabled? |
| 626 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 627 | sci_receive_chars(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | return IRQ_HANDLED; |
| 630 | } |
| 631 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 632 | static irqreturn_t sci_tx_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
| 634 | struct uart_port *port = ptr; |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 635 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 637 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | sci_transmit_chars(port); |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 639 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | return IRQ_HANDLED; |
| 642 | } |
| 643 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 644 | static irqreturn_t sci_er_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
| 646 | struct uart_port *port = ptr; |
| 647 | |
| 648 | /* Handle errors */ |
| 649 | if (port->type == PORT_SCI) { |
| 650 | if (sci_handle_errors(port)) { |
| 651 | /* discard character in rx buffer */ |
| 652 | sci_in(port, SCxSR); |
| 653 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 654 | } |
| 655 | } else { |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 656 | sci_handle_fifo_overrun(port); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 657 | sci_rx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 661 | |
| 662 | /* Kick the transmission */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 663 | sci_tx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | return IRQ_HANDLED; |
| 666 | } |
| 667 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 668 | static irqreturn_t sci_br_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
| 670 | struct uart_port *port = ptr; |
| 671 | |
| 672 | /* Handle BREAKs */ |
| 673 | sci_handle_breaks(port); |
| 674 | sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port)); |
| 675 | |
| 676 | return IRQ_HANDLED; |
| 677 | } |
| 678 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 679 | static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
Magnus Damm | 44e18e9 | 2009-07-03 08:39:34 +0000 | [diff] [blame] | 681 | unsigned short ssr_status, scr_status, err_enabled; |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 682 | struct uart_port *port = ptr; |
| 683 | irqreturn_t ret = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 685 | ssr_status = sci_in(port, SCxSR); |
| 686 | scr_status = sci_in(port, SCSCR); |
Magnus Damm | 44e18e9 | 2009-07-03 08:39:34 +0000 | [diff] [blame] | 687 | err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | /* Tx Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 690 | if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE)) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 691 | ret = sci_tx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* Rx Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 693 | if ((ssr_status & SCxSR_RDxF(port)) && (scr_status & SCI_CTRL_FLAGS_RIE)) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 694 | ret = sci_rx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | /* Error Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 696 | if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 697 | ret = sci_er_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* Break Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 699 | if ((ssr_status & SCxSR_BRK(port)) && err_enabled) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 700 | ret = sci_br_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 702 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | /* |
| 706 | * Here we define a transistion notifier so that we can update all of our |
| 707 | * ports' baud rate when the peripheral clock changes. |
| 708 | */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 709 | static int sci_notifier(struct notifier_block *self, |
| 710 | unsigned long phase, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 712 | struct sh_sci_priv *priv = container_of(self, |
| 713 | struct sh_sci_priv, clk_nb); |
| 714 | struct sci_port *sci_port; |
| 715 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | if ((phase == CPUFREQ_POSTCHANGE) || |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 718 | (phase == CPUFREQ_RESUMECHANGE)) { |
| 719 | spin_lock_irqsave(&priv->lock, flags); |
| 720 | list_for_each_entry(sci_port, &priv->ports, node) |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 721 | sci_port->port.uartclk = clk_get_rate(sci_port->dclk); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 722 | spin_unlock_irqrestore(&priv->lock, flags); |
| 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return NOTIFY_OK; |
| 726 | } |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 727 | |
| 728 | static void sci_clk_enable(struct uart_port *port) |
| 729 | { |
| 730 | struct sci_port *sci_port = to_sci_port(port); |
| 731 | |
| 732 | clk_enable(sci_port->dclk); |
| 733 | sci_port->port.uartclk = clk_get_rate(sci_port->dclk); |
| 734 | |
| 735 | if (sci_port->iclk) |
| 736 | clk_enable(sci_port->iclk); |
| 737 | } |
| 738 | |
| 739 | static void sci_clk_disable(struct uart_port *port) |
| 740 | { |
| 741 | struct sci_port *sci_port = to_sci_port(port); |
| 742 | |
| 743 | if (sci_port->iclk) |
| 744 | clk_disable(sci_port->iclk); |
| 745 | |
| 746 | clk_disable(sci_port->dclk); |
| 747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | static int sci_request_irq(struct sci_port *port) |
| 750 | { |
| 751 | int i; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 752 | irqreturn_t (*handlers[4])(int irq, void *ptr) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt, |
| 754 | sci_br_interrupt, |
| 755 | }; |
| 756 | const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full", |
| 757 | "SCI Transmit Data Empty", "SCI Break" }; |
| 758 | |
| 759 | if (port->irqs[0] == port->irqs[1]) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 760 | if (unlikely(!port->irqs[0])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return -ENODEV; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 762 | |
| 763 | if (request_irq(port->irqs[0], sci_mpxed_interrupt, |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 764 | IRQF_DISABLED, "sci", port)) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 765 | dev_err(port->port.dev, "Can't allocate IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return -ENODEV; |
| 767 | } |
| 768 | } else { |
| 769 | for (i = 0; i < ARRAY_SIZE(handlers); i++) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 770 | if (unlikely(!port->irqs[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | continue; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 772 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 773 | if (request_irq(port->irqs[i], handlers[i], |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 774 | IRQF_DISABLED, desc[i], port)) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 775 | dev_err(port->port.dev, "Can't allocate IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return -ENODEV; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static void sci_free_irq(struct sci_port *port) |
| 785 | { |
| 786 | int i; |
| 787 | |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 788 | if (port->irqs[0] == port->irqs[1]) |
| 789 | free_irq(port->irqs[0], port); |
| 790 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | for (i = 0; i < ARRAY_SIZE(port->irqs); i++) { |
| 792 | if (!port->irqs[i]) |
| 793 | continue; |
| 794 | |
| 795 | free_irq(port->irqs[i], port); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | static unsigned int sci_tx_empty(struct uart_port *port) |
| 801 | { |
Guennadi Liakhovetski | b151680 | 2009-12-01 09:54:46 +0000 | [diff] [blame] | 802 | unsigned short status = sci_in(port, SCxSR); |
| 803 | return status & SCxSR_TEND(port) ? TIOCSER_TEMT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 807 | { |
| 808 | /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */ |
| 809 | /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */ |
| 810 | /* If you have signals for DTR and DCD, please implement here. */ |
| 811 | } |
| 812 | |
| 813 | static unsigned int sci_get_mctrl(struct uart_port *port) |
| 814 | { |
| 815 | /* This routine is used for geting signals of: DTR, DCD, DSR, RI, |
| 816 | and CTS/RTS */ |
| 817 | |
| 818 | return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR; |
| 819 | } |
| 820 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 821 | static void sci_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 823 | unsigned short ctrl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 825 | /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */ |
| 826 | ctrl = sci_in(port, SCSCR); |
| 827 | ctrl |= SCI_CTRL_FLAGS_TIE; |
| 828 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 831 | static void sci_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | unsigned short ctrl; |
| 834 | |
| 835 | /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | ctrl = sci_in(port, SCSCR); |
| 837 | ctrl &= ~SCI_CTRL_FLAGS_TIE; |
| 838 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void sci_start_rx(struct uart_port *port, unsigned int tty_start) |
| 842 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | unsigned short ctrl; |
| 844 | |
| 845 | /* Set RIE (Receive Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | ctrl = sci_in(port, SCSCR); |
| 847 | ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE; |
| 848 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void sci_stop_rx(struct uart_port *port) |
| 852 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | unsigned short ctrl; |
| 854 | |
| 855 | /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | ctrl = sci_in(port, SCSCR); |
| 857 | ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE); |
| 858 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static void sci_enable_ms(struct uart_port *port) |
| 862 | { |
| 863 | /* Nothing here yet .. */ |
| 864 | } |
| 865 | |
| 866 | static void sci_break_ctl(struct uart_port *port, int break_state) |
| 867 | { |
| 868 | /* Nothing here yet .. */ |
| 869 | } |
| 870 | |
| 871 | static int sci_startup(struct uart_port *port) |
| 872 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 873 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 875 | if (s->enable) |
| 876 | s->enable(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | sci_request_irq(s); |
Yoshinori Sato | d656901 | 2005-10-14 15:59:12 -0700 | [diff] [blame] | 879 | sci_start_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | sci_start_rx(port, 1); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static void sci_shutdown(struct uart_port *port) |
| 886 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 887 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | sci_stop_rx(port); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 890 | sci_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | sci_free_irq(s); |
| 892 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 893 | if (s->disable) |
| 894 | s->disable(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 897 | static void sci_set_termios(struct uart_port *port, struct ktermios *termios, |
| 898 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Magnus Damm | 154280f | 2009-12-22 03:37:28 +0000 | [diff] [blame] | 900 | unsigned int status, baud, smr_val, max_baud; |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 901 | int t = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Magnus Damm | 154280f | 2009-12-22 03:37:28 +0000 | [diff] [blame] | 903 | /* |
| 904 | * earlyprintk comes here early on with port->uartclk set to zero. |
| 905 | * the clock framework is not up and running at this point so here |
| 906 | * we assume that 115200 is the maximum baud rate. please note that |
| 907 | * the baud rate is not programmed during earlyprintk - it is assumed |
| 908 | * that the previous boot loader has enabled required clocks and |
| 909 | * setup the baud rate generator hardware for us already. |
| 910 | */ |
| 911 | max_baud = port->uartclk ? port->uartclk / 16 : 115200; |
| 912 | |
| 913 | baud = uart_get_baud_rate(port, termios, old, 0, max_baud); |
| 914 | if (likely(baud && port->uartclk)) |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 915 | t = SCBRR_VALUE(baud, port->uartclk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 917 | do { |
| 918 | status = sci_in(port, SCxSR); |
| 919 | } while (!(status & SCxSR_TEND(port))); |
| 920 | |
| 921 | sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */ |
| 922 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 923 | if (port->type != PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 924 | sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 925 | |
| 926 | smr_val = sci_in(port, SCSMR) & 3; |
| 927 | if ((termios->c_cflag & CSIZE) == CS7) |
| 928 | smr_val |= 0x40; |
| 929 | if (termios->c_cflag & PARENB) |
| 930 | smr_val |= 0x20; |
| 931 | if (termios->c_cflag & PARODD) |
| 932 | smr_val |= 0x30; |
| 933 | if (termios->c_cflag & CSTOPB) |
| 934 | smr_val |= 0x08; |
| 935 | |
| 936 | uart_update_timeout(port, termios->c_cflag, baud); |
| 937 | |
| 938 | sci_out(port, SCSMR, smr_val); |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (t > 0) { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 941 | if (t >= 256) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1); |
| 943 | t >>= 2; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 944 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | sci_out(port, SCBRR, t); |
| 948 | udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */ |
| 949 | } |
| 950 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 951 | sci_init_pins(port, termios->c_cflag); |
| 952 | sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | sci_out(port, SCSCR, SCSCR_INIT(port)); |
| 955 | |
| 956 | if ((termios->c_cflag & CREAD) != 0) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 957 | sci_start_rx(port, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | static const char *sci_type(struct uart_port *port) |
| 961 | { |
| 962 | switch (port->type) { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 963 | case PORT_IRDA: |
| 964 | return "irda"; |
| 965 | case PORT_SCI: |
| 966 | return "sci"; |
| 967 | case PORT_SCIF: |
| 968 | return "scif"; |
| 969 | case PORT_SCIFA: |
| 970 | return "scifa"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 973 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static void sci_release_port(struct uart_port *port) |
| 977 | { |
| 978 | /* Nothing here yet .. */ |
| 979 | } |
| 980 | |
| 981 | static int sci_request_port(struct uart_port *port) |
| 982 | { |
| 983 | /* Nothing here yet .. */ |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static void sci_config_port(struct uart_port *port, int flags) |
| 988 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 989 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | port->type = s->type; |
| 992 | |
Magnus Damm | 08f8cb3 | 2009-01-21 15:14:22 +0000 | [diff] [blame] | 993 | if (port->membase) |
| 994 | return; |
| 995 | |
| 996 | if (port->flags & UPF_IOREMAP) { |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 997 | port->membase = ioremap_nocache(port->mapbase, 0x40); |
Magnus Damm | 08f8cb3 | 2009-01-21 15:14:22 +0000 | [diff] [blame] | 998 | |
| 999 | if (IS_ERR(port->membase)) |
| 1000 | dev_err(port->dev, "can't remap port#%d\n", port->line); |
| 1001 | } else { |
| 1002 | /* |
| 1003 | * For the simple (and majority of) cases where we don't |
| 1004 | * need to do any remapping, just cast the cookie |
| 1005 | * directly. |
| 1006 | */ |
| 1007 | port->membase = (void __iomem *)port->mapbase; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static int sci_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 1012 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 1013 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 1015 | if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | return -EINVAL; |
| 1017 | if (ser->baud_base < 2400) |
| 1018 | /* No paper tape reader for Mitch.. */ |
| 1019 | return -EINVAL; |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static struct uart_ops sci_uart_ops = { |
| 1025 | .tx_empty = sci_tx_empty, |
| 1026 | .set_mctrl = sci_set_mctrl, |
| 1027 | .get_mctrl = sci_get_mctrl, |
| 1028 | .start_tx = sci_start_tx, |
| 1029 | .stop_tx = sci_stop_tx, |
| 1030 | .stop_rx = sci_stop_rx, |
| 1031 | .enable_ms = sci_enable_ms, |
| 1032 | .break_ctl = sci_break_ctl, |
| 1033 | .startup = sci_startup, |
| 1034 | .shutdown = sci_shutdown, |
| 1035 | .set_termios = sci_set_termios, |
| 1036 | .type = sci_type, |
| 1037 | .release_port = sci_release_port, |
| 1038 | .request_port = sci_request_port, |
| 1039 | .config_port = sci_config_port, |
| 1040 | .verify_port = sci_verify_port, |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1041 | #ifdef CONFIG_CONSOLE_POLL |
| 1042 | .poll_get_char = sci_poll_get_char, |
| 1043 | .poll_put_char = sci_poll_put_char, |
| 1044 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | }; |
| 1046 | |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1047 | static void __devinit sci_init_single(struct platform_device *dev, |
| 1048 | struct sci_port *sci_port, |
Magnus Damm | 08f8cb3 | 2009-01-21 15:14:22 +0000 | [diff] [blame] | 1049 | unsigned int index, |
| 1050 | struct plat_sci_port *p) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1051 | { |
Magnus Damm | 7ed7e07 | 2009-01-21 15:14:14 +0000 | [diff] [blame] | 1052 | sci_port->port.ops = &sci_uart_ops; |
| 1053 | sci_port->port.iotype = UPIO_MEM; |
| 1054 | sci_port->port.line = index; |
Markus Pietrek | 75136d4 | 2010-01-15 08:33:20 +0900 | [diff] [blame] | 1055 | |
| 1056 | switch (p->type) { |
| 1057 | case PORT_SCIFA: |
| 1058 | sci_port->port.fifosize = 64; |
| 1059 | break; |
| 1060 | case PORT_SCIF: |
| 1061 | sci_port->port.fifosize = 16; |
| 1062 | break; |
| 1063 | default: |
| 1064 | sci_port->port.fifosize = 1; |
| 1065 | break; |
| 1066 | } |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (dev) { |
| 1069 | sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL; |
| 1070 | sci_port->dclk = clk_get(&dev->dev, "peripheral_clk"); |
| 1071 | sci_port->enable = sci_clk_enable; |
| 1072 | sci_port->disable = sci_clk_disable; |
| 1073 | sci_port->port.dev = &dev->dev; |
| 1074 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1075 | |
Magnus Damm | 7ed7e07 | 2009-01-21 15:14:14 +0000 | [diff] [blame] | 1076 | sci_port->break_timer.data = (unsigned long)sci_port; |
| 1077 | sci_port->break_timer.function = sci_break_timer; |
| 1078 | init_timer(&sci_port->break_timer); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1079 | |
Magnus Damm | 7ed7e07 | 2009-01-21 15:14:14 +0000 | [diff] [blame] | 1080 | sci_port->port.mapbase = p->mapbase; |
Magnus Damm | 7ed7e07 | 2009-01-21 15:14:14 +0000 | [diff] [blame] | 1081 | sci_port->port.membase = p->membase; |
| 1082 | |
| 1083 | sci_port->port.irq = p->irqs[SCIx_TXI_IRQ]; |
| 1084 | sci_port->port.flags = p->flags; |
| 1085 | sci_port->type = sci_port->port.type = p->type; |
| 1086 | |
| 1087 | memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs)); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1088 | } |
| 1089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1091 | static struct tty_driver *serial_console_device(struct console *co, int *index) |
| 1092 | { |
| 1093 | struct uart_driver *p = &sci_uart_driver; |
| 1094 | *index = co->index; |
| 1095 | return p->tty_driver; |
| 1096 | } |
| 1097 | |
| 1098 | static void serial_console_putchar(struct uart_port *port, int ch) |
| 1099 | { |
| 1100 | sci_poll_put_char(port, ch); |
| 1101 | } |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Print a string to the serial port trying not to disturb |
| 1105 | * any possible real use of the port... |
| 1106 | */ |
| 1107 | static void serial_console_write(struct console *co, const char *s, |
| 1108 | unsigned count) |
| 1109 | { |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1110 | struct uart_port *port = co->data; |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1111 | struct sci_port *sci_port = to_sci_port(port); |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 1112 | unsigned short bits; |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1113 | |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1114 | if (sci_port->enable) |
| 1115 | sci_port->enable(port); |
| 1116 | |
| 1117 | uart_console_write(port, s, count, serial_console_putchar); |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 1118 | |
| 1119 | /* wait until fifo is empty and last bit has been transmitted */ |
| 1120 | bits = SCxSR_TDxE(port) | SCxSR_TEND(port); |
| 1121 | while ((sci_in(port, SCxSR) & bits) != bits) |
| 1122 | cpu_relax(); |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1123 | |
Magnus Damm | 345e5a7 | 2009-11-05 14:34:57 +0000 | [diff] [blame] | 1124 | if (sci_port->disable) |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1125 | sci_port->disable(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1128 | static int __devinit serial_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | { |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1130 | struct sci_port *sci_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | struct uart_port *port; |
| 1132 | int baud = 115200; |
| 1133 | int bits = 8; |
| 1134 | int parity = 'n'; |
| 1135 | int flow = 'n'; |
| 1136 | int ret; |
| 1137 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1138 | /* |
| 1139 | * Check whether an invalid uart number has been specified, and |
| 1140 | * if so, search for the first available port that does have |
| 1141 | * console support. |
| 1142 | */ |
| 1143 | if (co->index >= SCI_NPORTS) |
| 1144 | co->index = 0; |
| 1145 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1146 | if (co->data) { |
| 1147 | port = co->data; |
| 1148 | sci_port = to_sci_port(port); |
| 1149 | } else { |
| 1150 | sci_port = &sci_ports[co->index]; |
| 1151 | port = &sci_port->port; |
| 1152 | co->data = port; |
| 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1156 | * Also need to check port->type, we don't actually have any |
| 1157 | * UPIO_PORT ports, but uart_report_port() handily misreports |
| 1158 | * it anyways if we don't have a port available by the time this is |
| 1159 | * called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1161 | if (!port->type) |
| 1162 | return -ENODEV; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1163 | |
Magnus Damm | 08f8cb3 | 2009-01-21 15:14:22 +0000 | [diff] [blame] | 1164 | sci_config_port(port, 0); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1165 | |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1166 | if (sci_port->enable) |
| 1167 | sci_port->enable(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (options) |
| 1170 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1171 | |
| 1172 | ret = uart_set_options(port, co, baud, parity, bits, flow); |
| 1173 | #if defined(__H8300H__) || defined(__H8300S__) |
| 1174 | /* disable rx interrupt */ |
| 1175 | if (ret == 0) |
| 1176 | sci_stop_rx(port); |
| 1177 | #endif |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1178 | /* TODO: disable clock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | static struct console serial_console = { |
| 1183 | .name = "ttySC", |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1184 | .device = serial_console_device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | .write = serial_console_write, |
| 1186 | .setup = serial_console_setup, |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 1187 | .flags = CON_PRINTBUFFER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | .index = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | }; |
| 1190 | |
| 1191 | static int __init sci_console_init(void) |
| 1192 | { |
| 1193 | register_console(&serial_console); |
| 1194 | return 0; |
| 1195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | console_initcall(sci_console_init); |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1197 | |
| 1198 | static struct sci_port early_serial_port; |
| 1199 | static struct console early_serial_console = { |
| 1200 | .name = "early_ttySC", |
| 1201 | .write = serial_console_write, |
| 1202 | .flags = CON_PRINTBUFFER, |
| 1203 | }; |
| 1204 | static char early_serial_buf[32]; |
| 1205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */ |
| 1207 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1208 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 1209 | #define SCI_CONSOLE (&serial_console) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | #else |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 1211 | #define SCI_CONSOLE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | #endif |
| 1213 | |
| 1214 | static char banner[] __initdata = |
| 1215 | KERN_INFO "SuperH SCI(F) driver initialized\n"; |
| 1216 | |
| 1217 | static struct uart_driver sci_uart_driver = { |
| 1218 | .owner = THIS_MODULE, |
| 1219 | .driver_name = "sci", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | .dev_name = "ttySC", |
| 1221 | .major = SCI_MAJOR, |
| 1222 | .minor = SCI_MINOR_START, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1223 | .nr = SCI_NPORTS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | .cons = SCI_CONSOLE, |
| 1225 | }; |
| 1226 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1227 | |
Paul Mundt | 54507f6 | 2009-05-08 23:48:33 +0900 | [diff] [blame] | 1228 | static int sci_remove(struct platform_device *dev) |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1229 | { |
| 1230 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1231 | struct sci_port *p; |
| 1232 | unsigned long flags; |
| 1233 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1234 | cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1235 | |
| 1236 | spin_lock_irqsave(&priv->lock, flags); |
| 1237 | list_for_each_entry(p, &priv->ports, node) |
| 1238 | uart_remove_one_port(&sci_uart_driver, &p->port); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1239 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1240 | |
| 1241 | kfree(priv); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1245 | static int __devinit sci_probe_single(struct platform_device *dev, |
| 1246 | unsigned int index, |
| 1247 | struct plat_sci_port *p, |
| 1248 | struct sci_port *sciport) |
| 1249 | { |
| 1250 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1251 | unsigned long flags; |
| 1252 | int ret; |
| 1253 | |
| 1254 | /* Sanity check */ |
| 1255 | if (unlikely(index >= SCI_NPORTS)) { |
| 1256 | dev_notice(&dev->dev, "Attempting to register port " |
| 1257 | "%d when only %d are available.\n", |
| 1258 | index+1, SCI_NPORTS); |
| 1259 | dev_notice(&dev->dev, "Consider bumping " |
| 1260 | "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n"); |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 1264 | sci_init_single(dev, sciport, index, p); |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1265 | |
| 1266 | ret = uart_add_one_port(&sci_uart_driver, &sciport->port); |
Magnus Damm | 08f8cb3 | 2009-01-21 15:14:22 +0000 | [diff] [blame] | 1267 | if (ret) |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1268 | return ret; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1269 | |
| 1270 | INIT_LIST_HEAD(&sciport->node); |
| 1271 | |
| 1272 | spin_lock_irqsave(&priv->lock, flags); |
| 1273 | list_add(&sciport->node, &priv->ports); |
| 1274 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1275 | |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1279 | /* |
| 1280 | * Register a set of serial devices attached to a platform device. The |
| 1281 | * list is terminated with a zero flags entry, which means we expect |
| 1282 | * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need |
| 1283 | * remapping (such as sh64) should also set UPF_IOREMAP. |
| 1284 | */ |
| 1285 | static int __devinit sci_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1287 | struct plat_sci_port *p = dev->dev.platform_data; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1288 | struct sh_sci_priv *priv; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1289 | int i, ret = -EINVAL; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1290 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1291 | #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE |
| 1292 | if (is_early_platform_device(dev)) { |
| 1293 | if (dev->id == -1) |
| 1294 | return -ENOTSUPP; |
| 1295 | early_serial_console.index = dev->id; |
| 1296 | early_serial_console.data = &early_serial_port.port; |
| 1297 | sci_init_single(NULL, &early_serial_port, dev->id, p); |
| 1298 | serial_console_setup(&early_serial_console, early_serial_buf); |
| 1299 | if (!strstr(early_serial_buf, "keep")) |
| 1300 | early_serial_console.flags |= CON_BOOT; |
| 1301 | register_console(&early_serial_console); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | #endif |
| 1305 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1306 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 1307 | if (!priv) |
| 1308 | return -ENOMEM; |
| 1309 | |
| 1310 | INIT_LIST_HEAD(&priv->ports); |
| 1311 | spin_lock_init(&priv->lock); |
| 1312 | platform_set_drvdata(dev, priv); |
| 1313 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1314 | priv->clk_nb.notifier_call = sci_notifier; |
| 1315 | cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1317 | if (dev->id != -1) { |
| 1318 | ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]); |
| 1319 | if (ret) |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1320 | goto err_unreg; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 1321 | } else { |
| 1322 | for (i = 0; p && p->flags != 0; p++, i++) { |
| 1323 | ret = sci_probe_single(dev, i, p, &sci_ports[i]); |
| 1324 | if (ret) |
| 1325 | goto err_unreg; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1326 | } |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
| 1329 | #ifdef CONFIG_SH_STANDARD_BIOS |
| 1330 | sh_bios_gdb_detach(); |
| 1331 | #endif |
| 1332 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1333 | return 0; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1334 | |
| 1335 | err_unreg: |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1336 | sci_remove(dev); |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1337 | return ret; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1338 | } |
| 1339 | |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1340 | static int sci_suspend(struct device *dev) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1341 | { |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1342 | struct sh_sci_priv *priv = dev_get_drvdata(dev); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1343 | struct sci_port *p; |
| 1344 | unsigned long flags; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1345 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1346 | spin_lock_irqsave(&priv->lock, flags); |
| 1347 | list_for_each_entry(p, &priv->ports, node) |
| 1348 | uart_suspend_port(&sci_uart_driver, &p->port); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1349 | spin_unlock_irqrestore(&priv->lock, flags); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1354 | static int sci_resume(struct device *dev) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1355 | { |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1356 | struct sh_sci_priv *priv = dev_get_drvdata(dev); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1357 | struct sci_port *p; |
| 1358 | unsigned long flags; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1359 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1360 | spin_lock_irqsave(&priv->lock, flags); |
| 1361 | list_for_each_entry(p, &priv->ports, node) |
| 1362 | uart_resume_port(&sci_uart_driver, &p->port); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1363 | spin_unlock_irqrestore(&priv->lock, flags); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1364 | |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1368 | static const struct dev_pm_ops sci_dev_pm_ops = { |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1369 | .suspend = sci_suspend, |
| 1370 | .resume = sci_resume, |
| 1371 | }; |
| 1372 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1373 | static struct platform_driver sci_driver = { |
| 1374 | .probe = sci_probe, |
Uwe Kleine-König | b9e39c8 | 2009-11-24 22:07:32 +0100 | [diff] [blame] | 1375 | .remove = sci_remove, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1376 | .driver = { |
| 1377 | .name = "sh-sci", |
| 1378 | .owner = THIS_MODULE, |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 1379 | .pm = &sci_dev_pm_ops, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1380 | }, |
| 1381 | }; |
| 1382 | |
| 1383 | static int __init sci_init(void) |
| 1384 | { |
| 1385 | int ret; |
| 1386 | |
| 1387 | printk(banner); |
| 1388 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1389 | ret = uart_register_driver(&sci_uart_driver); |
| 1390 | if (likely(ret == 0)) { |
| 1391 | ret = platform_driver_register(&sci_driver); |
| 1392 | if (unlikely(ret)) |
| 1393 | uart_unregister_driver(&sci_uart_driver); |
| 1394 | } |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | return ret; |
| 1397 | } |
| 1398 | |
| 1399 | static void __exit sci_exit(void) |
| 1400 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1401 | platform_driver_unregister(&sci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | uart_unregister_driver(&sci_uart_driver); |
| 1403 | } |
| 1404 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 1405 | #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE |
| 1406 | early_platform_init_buffer("earlyprintk", &sci_driver, |
| 1407 | early_serial_buf, ARRAY_SIZE(early_serial_buf)); |
| 1408 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | module_init(sci_init); |
| 1410 | module_exit(sci_exit); |
| 1411 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1412 | MODULE_LICENSE("GPL"); |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 1413 | MODULE_ALIAS("platform:sh-sci"); |