Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * m32r_sio.c |
| 4 | * |
| 5 | * Driver for M32R serial ports |
| 6 | * |
| 7 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 8 | * Based on drivers/serial/8250.c. |
| 9 | * |
| 10 | * Copyright (C) 2001 Russell King. |
| 11 | * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * A note about mapbase / membase |
| 16 | * |
| 17 | * mapbase is the physical address of the IO port. Currently, we don't |
| 18 | * support this very well, and it may well be dropped from this driver |
| 19 | * in future. As such, mapbase should be NULL. |
| 20 | * |
| 21 | * membase is an 'ioremapped' cookie. This is compatible with the old |
| 22 | * serial.c driver, and is currently the preferred form. |
| 23 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 26 | #define SUPPORT_SYSRQ |
| 27 | #endif |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/tty.h> |
Jiri Slaby | ee160a3 | 2011-09-01 16:20:57 +0200 | [diff] [blame] | 30 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/ioport.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/console.h> |
| 34 | #include <linux/sysrq.h> |
| 35 | #include <linux/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/delay.h> |
| 37 | |
| 38 | #include <asm/m32r.h> |
| 39 | #include <asm/io.h> |
| 40 | #include <asm/irq.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define BAUD_RATE 115200 |
| 43 | |
| 44 | #include <linux/serial_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "m32r_sio_reg.h" |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define PASS_LIMIT 256 |
| 48 | |
Jiri Slaby | d4dbe37 | 2016-01-12 10:59:06 +0100 | [diff] [blame] | 49 | static const struct { |
Jiri Slaby | d4dbe37 | 2016-01-12 10:59:06 +0100 | [diff] [blame] | 50 | unsigned int port; |
| 51 | unsigned int irq; |
Jiri Slaby | d4dbe37 | 2016-01-12 10:59:06 +0100 | [diff] [blame] | 52 | } old_serial_port[] = { |
Jiri Slaby | c8e7d14 | 2016-01-12 10:59:07 +0100 | [diff] [blame] | 53 | #if defined(CONFIG_PLAT_USRV) |
| 54 | /* PORT IRQ FLAGS */ |
| 55 | { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */ |
| 56 | { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */ |
| 57 | #elif defined(CONFIG_SERIAL_M32R_PLDSIO) |
| 58 | { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */ |
| 59 | #else |
| 60 | { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */ |
| 61 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #define UART_NR ARRAY_SIZE(old_serial_port) |
| 65 | |
| 66 | struct uart_sio_port { |
| 67 | struct uart_port port; |
| 68 | struct timer_list timer; /* "no irq" timer */ |
| 69 | struct list_head list; /* ports on this IRQ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | unsigned char ier; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct irq_info { |
| 74 | spinlock_t lock; |
| 75 | struct list_head *head; |
| 76 | }; |
| 77 | |
| 78 | static struct irq_info irq_lists[NR_IRQS]; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_SERIAL_M32R_PLDSIO |
| 81 | |
| 82 | #define __sio_in(x) inw((unsigned long)(x)) |
| 83 | #define __sio_out(v,x) outw((v),(unsigned long)(x)) |
| 84 | |
| 85 | static inline void sio_set_baud_rate(unsigned long baud) |
| 86 | { |
| 87 | unsigned short sbaud; |
| 88 | sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1; |
| 89 | __sio_out(sbaud, PLD_ESIO0BAUR); |
| 90 | } |
| 91 | |
| 92 | static void sio_reset(void) |
| 93 | { |
| 94 | unsigned short tmp; |
| 95 | |
| 96 | tmp = __sio_in(PLD_ESIO0RXB); |
| 97 | tmp = __sio_in(PLD_ESIO0RXB); |
| 98 | tmp = __sio_in(PLD_ESIO0CR); |
| 99 | sio_set_baud_rate(BAUD_RATE); |
| 100 | __sio_out(0x0300, PLD_ESIO0CR); |
| 101 | __sio_out(0x0003, PLD_ESIO0CR); |
| 102 | } |
| 103 | |
| 104 | static void sio_init(void) |
| 105 | { |
| 106 | unsigned short tmp; |
| 107 | |
| 108 | tmp = __sio_in(PLD_ESIO0RXB); |
| 109 | tmp = __sio_in(PLD_ESIO0RXB); |
| 110 | tmp = __sio_in(PLD_ESIO0CR); |
| 111 | __sio_out(0x0300, PLD_ESIO0CR); |
| 112 | __sio_out(0x0003, PLD_ESIO0CR); |
| 113 | } |
| 114 | |
| 115 | static void sio_error(int *status) |
| 116 | { |
| 117 | printk("SIO0 error[%04x]\n", *status); |
| 118 | do { |
| 119 | sio_init(); |
| 120 | } while ((*status = __sio_in(PLD_ESIO0CR)) != 3); |
| 121 | } |
| 122 | |
| 123 | #else /* not CONFIG_SERIAL_M32R_PLDSIO */ |
| 124 | |
| 125 | #define __sio_in(x) inl(x) |
| 126 | #define __sio_out(v,x) outl((v),(x)) |
| 127 | |
| 128 | static inline void sio_set_baud_rate(unsigned long baud) |
| 129 | { |
| 130 | unsigned long i, j; |
| 131 | |
| 132 | i = boot_cpu_data.bus_clock / (baud * 16); |
| 133 | j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud; |
| 134 | i -= 1; |
| 135 | j = (j + 1) >> 1; |
| 136 | |
| 137 | __sio_out(i, M32R_SIO0_BAUR_PORTL); |
| 138 | __sio_out(j, M32R_SIO0_RBAUR_PORTL); |
| 139 | } |
| 140 | |
| 141 | static void sio_reset(void) |
| 142 | { |
| 143 | __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */ |
| 144 | __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */ |
| 145 | __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */ |
| 146 | sio_set_baud_rate(BAUD_RATE); |
| 147 | __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL); |
| 148 | __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */ |
| 149 | } |
| 150 | |
| 151 | static void sio_init(void) |
| 152 | { |
| 153 | unsigned int tmp; |
| 154 | |
| 155 | tmp = __sio_in(M32R_SIO0_RXB_PORTL); |
| 156 | tmp = __sio_in(M32R_SIO0_RXB_PORTL); |
| 157 | tmp = __sio_in(M32R_SIO0_STS_PORTL); |
| 158 | __sio_out(0x00000003, M32R_SIO0_CR_PORTL); |
| 159 | } |
| 160 | |
| 161 | static void sio_error(int *status) |
| 162 | { |
| 163 | printk("SIO0 error[%04x]\n", *status); |
| 164 | do { |
| 165 | sio_init(); |
| 166 | } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3); |
| 167 | } |
| 168 | |
| 169 | #endif /* CONFIG_SERIAL_M32R_PLDSIO */ |
| 170 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 171 | static unsigned int sio_in(struct uart_sio_port *up, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | return __sio_in(up->port.iobase + offset); |
| 174 | } |
| 175 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 176 | static void sio_out(struct uart_sio_port *up, int offset, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | __sio_out(value, up->port.iobase + offset); |
| 179 | } |
| 180 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 181 | static unsigned int serial_in(struct uart_sio_port *up, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | if (!offset) |
| 184 | return 0; |
| 185 | |
| 186 | return __sio_in(offset); |
| 187 | } |
| 188 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 189 | static void serial_out(struct uart_sio_port *up, int offset, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | if (!offset) |
| 192 | return; |
| 193 | |
| 194 | __sio_out(value, offset); |
| 195 | } |
| 196 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 197 | static void m32r_sio_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 199 | struct uart_sio_port *up = |
| 200 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | if (up->ier & UART_IER_THRI) { |
| 203 | up->ier &= ~UART_IER_THRI; |
| 204 | serial_out(up, UART_IER, up->ier); |
| 205 | } |
| 206 | } |
| 207 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 208 | static void m32r_sio_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | #ifdef CONFIG_SERIAL_M32R_PLDSIO |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 211 | struct uart_sio_port *up = |
| 212 | container_of(port, struct uart_sio_port, port); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 213 | struct circ_buf *xmit = &up->port.state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | if (!(up->ier & UART_IER_THRI)) { |
| 216 | up->ier |= UART_IER_THRI; |
| 217 | serial_out(up, UART_IER, up->ier); |
Peter Hurley | c557d39 | 2014-07-06 11:29:52 -0400 | [diff] [blame] | 218 | if (!uart_circ_empty(xmit)) { |
| 219 | serial_out(up, UART_TX, xmit->buf[xmit->tail]); |
| 220 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 221 | up->port.icount.tx++; |
| 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY); |
| 225 | #else |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 226 | struct uart_sio_port *up = |
| 227 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | if (!(up->ier & UART_IER_THRI)) { |
| 230 | up->ier |= UART_IER_THRI; |
| 231 | serial_out(up, UART_IER, up->ier); |
| 232 | } |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | static void m32r_sio_stop_rx(struct uart_port *port) |
| 237 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 238 | struct uart_sio_port *up = |
| 239 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | up->ier &= ~UART_IER_RLSI; |
| 242 | up->port.read_status_mask &= ~UART_LSR_DR; |
| 243 | serial_out(up, UART_IER, up->ier); |
| 244 | } |
| 245 | |
| 246 | static void m32r_sio_enable_ms(struct uart_port *port) |
| 247 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 248 | struct uart_sio_port *up = |
| 249 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | up->ier |= UART_IER_MSI; |
| 252 | serial_out(up, UART_IER, up->ier); |
| 253 | } |
| 254 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 255 | static void receive_chars(struct uart_sio_port *up, int *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 257 | struct tty_port *port = &up->port.state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | unsigned char ch; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 259 | unsigned char flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | int max_count = 256; |
| 261 | |
| 262 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ch = sio_in(up, SIORXB); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 264 | flag = TTY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | up->port.icount.rx++; |
| 266 | |
| 267 | if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE | |
| 268 | UART_LSR_FE | UART_LSR_OE))) { |
| 269 | /* |
| 270 | * For statistics only |
| 271 | */ |
| 272 | if (*status & UART_LSR_BI) { |
| 273 | *status &= ~(UART_LSR_FE | UART_LSR_PE); |
| 274 | up->port.icount.brk++; |
| 275 | /* |
| 276 | * We do the SysRQ and SAK checking |
| 277 | * here because otherwise the break |
| 278 | * may get masked by ignore_status_mask |
| 279 | * or read_status_mask. |
| 280 | */ |
| 281 | if (uart_handle_break(&up->port)) |
| 282 | goto ignore_char; |
| 283 | } else if (*status & UART_LSR_PE) |
| 284 | up->port.icount.parity++; |
| 285 | else if (*status & UART_LSR_FE) |
| 286 | up->port.icount.frame++; |
| 287 | if (*status & UART_LSR_OE) |
| 288 | up->port.icount.overrun++; |
| 289 | |
| 290 | /* |
| 291 | * Mask off conditions which should be ingored. |
| 292 | */ |
| 293 | *status &= up->port.read_status_mask; |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (*status & UART_LSR_BI) { |
Jiri Slaby | ad245aa | 2016-01-12 10:59:08 +0100 | [diff] [blame] | 296 | pr_debug("handling break....\n"); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 297 | flag = TTY_BREAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } else if (*status & UART_LSR_PE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 299 | flag = TTY_PARITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | else if (*status & UART_LSR_FE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 301 | flag = TTY_FRAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 303 | if (uart_handle_sysrq_char(&up->port, ch)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | goto ignore_char; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 305 | if ((*status & up->port.ignore_status_mask) == 0) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 306 | tty_insert_flip_char(port, ch, flag); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 307 | |
| 308 | if (*status & UART_LSR_OE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* |
| 310 | * Overrun is special, since it's reported |
| 311 | * immediately, and doesn't affect the current |
| 312 | * character. |
| 313 | */ |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 314 | tty_insert_flip_char(port, 0, TTY_OVERRUN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | ignore_char: |
| 317 | *status = serial_in(up, UART_LSR); |
| 318 | } while ((*status & UART_LSR_DR) && (max_count-- > 0)); |
Viresh Kumar | af89f83 | 2013-08-19 20:14:16 +0530 | [diff] [blame] | 319 | |
| 320 | spin_unlock(&up->port.lock); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 321 | tty_flip_buffer_push(port); |
Viresh Kumar | af89f83 | 2013-08-19 20:14:16 +0530 | [diff] [blame] | 322 | spin_lock(&up->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 325 | static void transmit_chars(struct uart_sio_port *up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 327 | struct circ_buf *xmit = &up->port.state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | int count; |
| 329 | |
| 330 | if (up->port.x_char) { |
| 331 | #ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */ |
| 332 | serial_out(up, UART_TX, up->port.x_char); |
| 333 | #endif |
| 334 | up->port.icount.tx++; |
| 335 | up->port.x_char = 0; |
| 336 | return; |
| 337 | } |
| 338 | if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 339 | m32r_sio_stop_tx(&up->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
| 343 | count = up->port.fifosize; |
| 344 | do { |
| 345 | serial_out(up, UART_TX, xmit->buf[xmit->tail]); |
| 346 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 347 | up->port.icount.tx++; |
| 348 | if (uart_circ_empty(xmit)) |
| 349 | break; |
Julia Lawall | 022d917 | 2008-03-04 14:29:19 -0800 | [diff] [blame] | 350 | while (!(serial_in(up, UART_LSR) & UART_LSR_THRE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | } while (--count > 0); |
| 353 | |
| 354 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 355 | uart_write_wakeup(&up->port); |
| 356 | |
Jiri Slaby | ad245aa | 2016-01-12 10:59:08 +0100 | [diff] [blame] | 357 | pr_debug("THRE...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | if (uart_circ_empty(xmit)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 360 | m32r_sio_stop_tx(&up->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * This handles the interrupt from one port. |
| 365 | */ |
| 366 | static inline void m32r_sio_handle_port(struct uart_sio_port *up, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 367 | unsigned int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Jiri Slaby | ad245aa | 2016-01-12 10:59:08 +0100 | [diff] [blame] | 369 | pr_debug("status = %x...\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | if (status & 0x04) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 372 | receive_chars(up, &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (status & 0x01) |
| 374 | transmit_chars(up); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * This is the serial driver's interrupt routine. |
| 379 | * |
| 380 | * Arjan thinks the old way was overly complex, so it got simplified. |
| 381 | * Alan disagrees, saying that need the complexity to handle the weird |
| 382 | * nature of ISA shared interrupts. (This is a special exception.) |
| 383 | * |
| 384 | * In order to handle ISA shared interrupts properly, we need to check |
| 385 | * that all ports have been serviced, and therefore the ISA interrupt |
| 386 | * line has been de-asserted. |
| 387 | * |
| 388 | * This means we need to loop through all ports. checking that they |
| 389 | * don't have an interrupt pending. |
| 390 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 391 | static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
| 393 | struct irq_info *i = dev_id; |
| 394 | struct list_head *l, *end = NULL; |
| 395 | int pass_counter = 0; |
| 396 | |
Jiri Slaby | ad245aa | 2016-01-12 10:59:08 +0100 | [diff] [blame] | 397 | pr_debug("m32r_sio_interrupt(%d)...\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | #ifdef CONFIG_SERIAL_M32R_PLDSIO |
| 400 | // if (irq == PLD_IRQ_SIO0_SND) |
| 401 | // irq = PLD_IRQ_SIO0_RCV; |
| 402 | #else |
| 403 | if (irq == M32R_IRQ_SIO0_S) |
| 404 | irq = M32R_IRQ_SIO0_R; |
| 405 | #endif |
| 406 | |
| 407 | spin_lock(&i->lock); |
| 408 | |
| 409 | l = i->head; |
| 410 | do { |
| 411 | struct uart_sio_port *up; |
| 412 | unsigned int sts; |
| 413 | |
| 414 | up = list_entry(l, struct uart_sio_port, list); |
| 415 | |
| 416 | sts = sio_in(up, SIOSTS); |
| 417 | if (sts & 0x5) { |
| 418 | spin_lock(&up->port.lock); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 419 | m32r_sio_handle_port(up, sts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | spin_unlock(&up->port.lock); |
| 421 | |
| 422 | end = NULL; |
| 423 | } else if (end == NULL) |
| 424 | end = l; |
| 425 | |
| 426 | l = l->next; |
| 427 | |
| 428 | if (l == i->head && pass_counter++ > PASS_LIMIT) { |
| 429 | if (sts & 0xe0) |
| 430 | sio_error(&sts); |
| 431 | break; |
| 432 | } |
| 433 | } while (l != end); |
| 434 | |
| 435 | spin_unlock(&i->lock); |
| 436 | |
Jiri Slaby | ad245aa | 2016-01-12 10:59:08 +0100 | [diff] [blame] | 437 | pr_debug("end.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | return IRQ_HANDLED; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * To support ISA shared interrupts, we need to have one interrupt |
| 444 | * handler that ensures that the IRQ line has been deasserted |
| 445 | * before returning. Failing to do this will result in the IRQ |
| 446 | * line being stuck active, and, since ISA irqs are edge triggered, |
| 447 | * no more IRQs will be seen. |
| 448 | */ |
| 449 | static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up) |
| 450 | { |
| 451 | spin_lock_irq(&i->lock); |
| 452 | |
| 453 | if (!list_empty(i->head)) { |
| 454 | if (i->head == &up->list) |
| 455 | i->head = i->head->next; |
| 456 | list_del(&up->list); |
| 457 | } else { |
| 458 | BUG_ON(i->head != &up->list); |
| 459 | i->head = NULL; |
| 460 | } |
| 461 | |
| 462 | spin_unlock_irq(&i->lock); |
| 463 | } |
| 464 | |
| 465 | static int serial_link_irq_chain(struct uart_sio_port *up) |
| 466 | { |
| 467 | struct irq_info *i = irq_lists + up->port.irq; |
Hirokazu Takata | dab8f49 | 2007-10-16 01:26:36 -0700 | [diff] [blame] | 468 | int ret, irq_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | spin_lock_irq(&i->lock); |
| 471 | |
| 472 | if (i->head) { |
| 473 | list_add(&up->list, i->head); |
| 474 | spin_unlock_irq(&i->lock); |
| 475 | |
| 476 | ret = 0; |
| 477 | } else { |
| 478 | INIT_LIST_HEAD(&up->list); |
| 479 | i->head = &up->list; |
| 480 | spin_unlock_irq(&i->lock); |
| 481 | |
| 482 | ret = request_irq(up->port.irq, m32r_sio_interrupt, |
| 483 | irq_flags, "SIO0-RX", i); |
| 484 | ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt, |
| 485 | irq_flags, "SIO0-TX", i); |
| 486 | if (ret < 0) |
| 487 | serial_do_unlink(i, up); |
| 488 | } |
| 489 | |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static void serial_unlink_irq_chain(struct uart_sio_port *up) |
| 494 | { |
| 495 | struct irq_info *i = irq_lists + up->port.irq; |
| 496 | |
| 497 | BUG_ON(i->head == NULL); |
| 498 | |
| 499 | if (list_empty(i->head)) { |
| 500 | free_irq(up->port.irq, i); |
| 501 | free_irq(up->port.irq + 1, i); |
| 502 | } |
| 503 | |
| 504 | serial_do_unlink(i, up); |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * This function is used to handle ports that do not have an interrupt. |
| 509 | */ |
Kees Cook | f0f62c6 | 2017-10-16 16:27:37 -0700 | [diff] [blame] | 510 | static void m32r_sio_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Kees Cook | f0f62c6 | 2017-10-16 16:27:37 -0700 | [diff] [blame] | 512 | struct uart_sio_port *up = from_timer(up, t, timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | unsigned int timeout; |
| 514 | unsigned int sts; |
| 515 | |
| 516 | sts = sio_in(up, SIOSTS); |
| 517 | if (sts & 0x5) { |
| 518 | spin_lock(&up->port.lock); |
Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 519 | m32r_sio_handle_port(up, sts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | spin_unlock(&up->port.lock); |
| 521 | } |
| 522 | |
| 523 | timeout = up->port.timeout; |
| 524 | timeout = timeout > 6 ? (timeout / 2 - 2) : 1; |
| 525 | mod_timer(&up->timer, jiffies + timeout); |
| 526 | } |
| 527 | |
| 528 | static unsigned int m32r_sio_tx_empty(struct uart_port *port) |
| 529 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 530 | struct uart_sio_port *up = |
| 531 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | unsigned long flags; |
| 533 | unsigned int ret; |
| 534 | |
| 535 | spin_lock_irqsave(&up->port.lock, flags); |
| 536 | ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0; |
| 537 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | static unsigned int m32r_sio_get_mctrl(struct uart_port *port) |
| 543 | { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 548 | { |
| 549 | |
| 550 | } |
| 551 | |
| 552 | static void m32r_sio_break_ctl(struct uart_port *port, int break_state) |
| 553 | { |
| 554 | |
| 555 | } |
| 556 | |
| 557 | static int m32r_sio_startup(struct uart_port *port) |
| 558 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 559 | struct uart_sio_port *up = |
| 560 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | int retval; |
| 562 | |
| 563 | sio_init(); |
| 564 | |
| 565 | /* |
| 566 | * If the "interrupt" for this port doesn't correspond with any |
| 567 | * hardware interrupt, we use a timer-based system. The original |
| 568 | * driver used to do this with IRQ0. |
| 569 | */ |
Alan Cox | d4e33fa | 2012-01-26 17:44:09 +0000 | [diff] [blame] | 570 | if (!up->port.irq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | unsigned int timeout = up->port.timeout; |
| 572 | |
| 573 | timeout = timeout > 6 ? (timeout / 2 - 2) : 1; |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | mod_timer(&up->timer, jiffies + timeout); |
| 576 | } else { |
| 577 | retval = serial_link_irq_chain(up); |
| 578 | if (retval) |
| 579 | return retval; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Finally, enable interrupts. Note: Modem status interrupts |
| 584 | * are set via set_termios(), which will be occurring imminently |
| 585 | * anyway, so we don't enable them here. |
| 586 | * - M32R_SIO: 0x0c |
| 587 | * - M32R_PLDSIO: 0x04 |
| 588 | */ |
| 589 | up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; |
| 590 | sio_out(up, SIOTRCR, up->ier); |
| 591 | |
| 592 | /* |
| 593 | * And clear the interrupt registers again for luck. |
| 594 | */ |
| 595 | sio_reset(); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static void m32r_sio_shutdown(struct uart_port *port) |
| 601 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 602 | struct uart_sio_port *up = |
| 603 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * Disable interrupts from this port |
| 607 | */ |
| 608 | up->ier = 0; |
| 609 | sio_out(up, SIOTRCR, 0); |
| 610 | |
| 611 | /* |
| 612 | * Disable break condition and FIFOs |
| 613 | */ |
| 614 | |
| 615 | sio_init(); |
| 616 | |
Alan Cox | d4e33fa | 2012-01-26 17:44:09 +0000 | [diff] [blame] | 617 | if (!up->port.irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | del_timer_sync(&up->timer); |
| 619 | else |
| 620 | serial_unlink_irq_chain(up); |
| 621 | } |
| 622 | |
| 623 | static unsigned int m32r_sio_get_divisor(struct uart_port *port, |
| 624 | unsigned int baud) |
| 625 | { |
| 626 | return uart_get_divisor(port, baud); |
| 627 | } |
| 628 | |
| 629 | static void m32r_sio_set_termios(struct uart_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 630 | struct ktermios *termios, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 632 | struct uart_sio_port *up = |
| 633 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | unsigned char cval = 0; |
| 635 | unsigned long flags; |
| 636 | unsigned int baud, quot; |
| 637 | |
| 638 | switch (termios->c_cflag & CSIZE) { |
| 639 | case CS5: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 640 | cval = UART_LCR_WLEN5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | break; |
| 642 | case CS6: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 643 | cval = UART_LCR_WLEN6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | break; |
| 645 | case CS7: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 646 | cval = UART_LCR_WLEN7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | break; |
| 648 | default: |
| 649 | case CS8: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 650 | cval = UART_LCR_WLEN8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | |
| 654 | if (termios->c_cflag & CSTOPB) |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 655 | cval |= UART_LCR_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | if (termios->c_cflag & PARENB) |
| 657 | cval |= UART_LCR_PARITY; |
| 658 | if (!(termios->c_cflag & PARODD)) |
| 659 | cval |= UART_LCR_EPAR; |
| 660 | #ifdef CMSPAR |
| 661 | if (termios->c_cflag & CMSPAR) |
| 662 | cval |= UART_LCR_SPAR; |
| 663 | #endif |
| 664 | |
| 665 | /* |
| 666 | * Ask the core to calculate the divisor for us. |
| 667 | */ |
| 668 | #ifdef CONFIG_SERIAL_M32R_PLDSIO |
| 669 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4); |
| 670 | #else |
| 671 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); |
| 672 | #endif |
| 673 | quot = m32r_sio_get_divisor(port, baud); |
| 674 | |
| 675 | /* |
| 676 | * Ok, we're now changing the port state. Do it with |
| 677 | * interrupts disabled. |
| 678 | */ |
| 679 | spin_lock_irqsave(&up->port.lock, flags); |
| 680 | |
| 681 | sio_set_baud_rate(baud); |
| 682 | |
| 683 | /* |
| 684 | * Update the per-port timeout. |
| 685 | */ |
| 686 | uart_update_timeout(port, termios->c_cflag, baud); |
| 687 | |
| 688 | up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; |
| 689 | if (termios->c_iflag & INPCK) |
| 690 | up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
Peter Hurley | ef8b9dd | 2014-06-16 08:10:41 -0400 | [diff] [blame] | 691 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | up->port.read_status_mask |= UART_LSR_BI; |
| 693 | |
| 694 | /* |
| 695 | * Characteres to ignore |
| 696 | */ |
| 697 | up->port.ignore_status_mask = 0; |
| 698 | if (termios->c_iflag & IGNPAR) |
| 699 | up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; |
| 700 | if (termios->c_iflag & IGNBRK) { |
| 701 | up->port.ignore_status_mask |= UART_LSR_BI; |
| 702 | /* |
| 703 | * If we're ignoring parity and break indicators, |
| 704 | * ignore overruns too (for real raw support). |
| 705 | */ |
| 706 | if (termios->c_iflag & IGNPAR) |
| 707 | up->port.ignore_status_mask |= UART_LSR_OE; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * ignore all characters if CREAD is not set |
| 712 | */ |
| 713 | if ((termios->c_cflag & CREAD) == 0) |
| 714 | up->port.ignore_status_mask |= UART_LSR_DR; |
| 715 | |
| 716 | /* |
| 717 | * CTS flow control flag and modem status interrupts |
| 718 | */ |
| 719 | up->ier &= ~UART_IER_MSI; |
| 720 | if (UART_ENABLE_MS(&up->port, termios->c_cflag)) |
| 721 | up->ier |= UART_IER_MSI; |
| 722 | |
| 723 | serial_out(up, UART_IER, up->ier); |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 726 | } |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Resource handling. This is complicated by the fact that resources |
| 730 | * depend on the port type. Maybe we should be claiming the standard |
| 731 | * 8250 ports, and then trying to get other resources as necessary? |
| 732 | */ |
| 733 | static int |
| 734 | m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res) |
| 735 | { |
| 736 | unsigned int size = 8 << up->port.regshift; |
| 737 | #ifndef CONFIG_SERIAL_M32R_PLDSIO |
| 738 | unsigned long start; |
| 739 | #endif |
| 740 | int ret = 0; |
| 741 | |
| 742 | switch (up->port.iotype) { |
| 743 | case UPIO_MEM: |
| 744 | if (up->port.mapbase) { |
| 745 | #ifdef CONFIG_SERIAL_M32R_PLDSIO |
| 746 | *res = request_mem_region(up->port.mapbase, size, "serial"); |
| 747 | #else |
| 748 | start = up->port.mapbase; |
| 749 | *res = request_mem_region(start, size, "serial"); |
| 750 | #endif |
| 751 | if (!*res) |
| 752 | ret = -EBUSY; |
| 753 | } |
| 754 | break; |
| 755 | |
| 756 | case UPIO_PORT: |
| 757 | *res = request_region(up->port.iobase, size, "serial"); |
| 758 | if (!*res) |
| 759 | ret = -EBUSY; |
| 760 | break; |
| 761 | } |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | static void m32r_sio_release_port(struct uart_port *port) |
| 766 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 767 | struct uart_sio_port *up = |
| 768 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | unsigned long start, offset = 0, size = 0; |
| 770 | |
| 771 | size <<= up->port.regshift; |
| 772 | |
| 773 | switch (up->port.iotype) { |
| 774 | case UPIO_MEM: |
| 775 | if (up->port.mapbase) { |
| 776 | /* |
| 777 | * Unmap the area. |
| 778 | */ |
| 779 | iounmap(up->port.membase); |
| 780 | up->port.membase = NULL; |
| 781 | |
| 782 | start = up->port.mapbase; |
| 783 | |
| 784 | if (size) |
| 785 | release_mem_region(start + offset, size); |
| 786 | release_mem_region(start, 8 << up->port.regshift); |
| 787 | } |
| 788 | break; |
| 789 | |
| 790 | case UPIO_PORT: |
| 791 | start = up->port.iobase; |
| 792 | |
| 793 | if (size) |
| 794 | release_region(start + offset, size); |
| 795 | release_region(start + offset, 8 << up->port.regshift); |
| 796 | break; |
| 797 | |
| 798 | default: |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static int m32r_sio_request_port(struct uart_port *port) |
| 804 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 805 | struct uart_sio_port *up = |
| 806 | container_of(port, struct uart_sio_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | struct resource *res = NULL; |
| 808 | int ret = 0; |
| 809 | |
| 810 | ret = m32r_sio_request_std_resource(up, &res); |
| 811 | |
| 812 | /* |
| 813 | * If we have a mapbase, then request that as well. |
| 814 | */ |
| 815 | if (ret == 0 && up->port.flags & UPF_IOREMAP) { |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 816 | int size = resource_size(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | up->port.membase = ioremap(up->port.mapbase, size); |
| 819 | if (!up->port.membase) |
| 820 | ret = -ENOMEM; |
| 821 | } |
| 822 | |
| 823 | if (ret < 0) { |
| 824 | if (res) |
| 825 | release_resource(res); |
| 826 | } |
| 827 | |
| 828 | return ret; |
| 829 | } |
| 830 | |
KOSAKI Motohiro | 1e806c5 | 2011-05-26 16:25:00 -0700 | [diff] [blame] | 831 | static void m32r_sio_config_port(struct uart_port *port, int unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 833 | struct uart_sio_port *up = |
| 834 | container_of(port, struct uart_sio_port, port); |
KOSAKI Motohiro | 1e806c5 | 2011-05-26 16:25:00 -0700 | [diff] [blame] | 835 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
| 837 | spin_lock_irqsave(&up->port.lock, flags); |
| 838 | |
Paul Gortmaker | b8ede90 | 2012-08-20 19:56:26 -0400 | [diff] [blame] | 839 | up->port.fifosize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 842 | } |
| 843 | |
| 844 | static int |
| 845 | m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 846 | { |
Paul Gortmaker | b8ede90 | 2012-08-20 19:56:26 -0400 | [diff] [blame] | 847 | if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | return -EINVAL; |
| 849 | return 0; |
| 850 | } |
| 851 | |
Julia Lawall | cdb9394 | 2017-08-13 08:21:48 +0200 | [diff] [blame] | 852 | static const struct uart_ops m32r_sio_pops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | .tx_empty = m32r_sio_tx_empty, |
| 854 | .set_mctrl = m32r_sio_set_mctrl, |
| 855 | .get_mctrl = m32r_sio_get_mctrl, |
| 856 | .stop_tx = m32r_sio_stop_tx, |
| 857 | .start_tx = m32r_sio_start_tx, |
| 858 | .stop_rx = m32r_sio_stop_rx, |
| 859 | .enable_ms = m32r_sio_enable_ms, |
| 860 | .break_ctl = m32r_sio_break_ctl, |
| 861 | .startup = m32r_sio_startup, |
| 862 | .shutdown = m32r_sio_shutdown, |
| 863 | .set_termios = m32r_sio_set_termios, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | .release_port = m32r_sio_release_port, |
| 865 | .request_port = m32r_sio_request_port, |
| 866 | .config_port = m32r_sio_config_port, |
| 867 | .verify_port = m32r_sio_verify_port, |
| 868 | }; |
| 869 | |
| 870 | static struct uart_sio_port m32r_sio_ports[UART_NR]; |
| 871 | |
| 872 | static void __init m32r_sio_init_ports(void) |
| 873 | { |
| 874 | struct uart_sio_port *up; |
| 875 | static int first = 1; |
| 876 | int i; |
| 877 | |
| 878 | if (!first) |
| 879 | return; |
| 880 | first = 0; |
| 881 | |
Jiri Slaby | c8e7d14 | 2016-01-12 10:59:07 +0100 | [diff] [blame] | 882 | for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | up->port.iobase = old_serial_port[i].port; |
| 884 | up->port.irq = irq_canonicalize(old_serial_port[i].irq); |
Jiri Slaby | c8e7d14 | 2016-01-12 10:59:07 +0100 | [diff] [blame] | 885 | up->port.uartclk = BAUD_RATE * 16; |
Jiri Slaby | 6137b7a | 2016-05-09 09:11:57 +0200 | [diff] [blame] | 886 | up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST; |
Jiri Slaby | c8e7d14 | 2016-01-12 10:59:07 +0100 | [diff] [blame] | 887 | up->port.membase = 0; |
| 888 | up->port.iotype = 0; |
| 889 | up->port.regshift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | up->port.ops = &m32r_sio_pops; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | static void __init m32r_sio_register_ports(struct uart_driver *drv) |
| 895 | { |
| 896 | int i; |
| 897 | |
| 898 | m32r_sio_init_ports(); |
| 899 | |
| 900 | for (i = 0; i < UART_NR; i++) { |
| 901 | struct uart_sio_port *up = &m32r_sio_ports[i]; |
| 902 | |
| 903 | up->port.line = i; |
| 904 | up->port.ops = &m32r_sio_pops; |
Kees Cook | f0f62c6 | 2017-10-16 16:27:37 -0700 | [diff] [blame] | 905 | timer_setup(&up->timer, m32r_sio_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | uart_add_one_port(drv, &up->port); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE |
| 912 | |
| 913 | /* |
| 914 | * Wait for transmitter & holding register to empty |
| 915 | */ |
Denys Vlasenko | 9cdb933 | 2015-10-27 18:46:37 +0100 | [diff] [blame] | 916 | static void wait_for_xmitr(struct uart_sio_port *up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | unsigned int status, tmout = 10000; |
| 919 | |
| 920 | /* Wait up to 10ms for the character(s) to be sent. */ |
| 921 | do { |
| 922 | status = sio_in(up, SIOSTS); |
| 923 | |
| 924 | if (--tmout == 0) |
| 925 | break; |
| 926 | udelay(1); |
| 927 | } while ((status & UART_EMPTY) != UART_EMPTY); |
| 928 | |
| 929 | /* Wait up to 1s for flow control if necessary */ |
| 930 | if (up->port.flags & UPF_CONS_FLOW) { |
| 931 | tmout = 1000000; |
| 932 | while (--tmout) |
| 933 | udelay(1); |
| 934 | } |
| 935 | } |
| 936 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 937 | static void m32r_sio_console_putchar(struct uart_port *port, int ch) |
| 938 | { |
Fabian Frederick | b6b30d6 | 2014-10-05 19:01:02 +0200 | [diff] [blame] | 939 | struct uart_sio_port *up = |
| 940 | container_of(port, struct uart_sio_port, port); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 941 | |
| 942 | wait_for_xmitr(up); |
| 943 | sio_out(up, SIOTXB, ch); |
| 944 | } |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | /* |
| 947 | * Print a string to the serial port trying not to disturb |
| 948 | * any possible real use of the port... |
| 949 | * |
| 950 | * The console_lock must be held when we get here. |
| 951 | */ |
| 952 | static void m32r_sio_console_write(struct console *co, const char *s, |
| 953 | unsigned int count) |
| 954 | { |
| 955 | struct uart_sio_port *up = &m32r_sio_ports[co->index]; |
| 956 | unsigned int ier; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | /* |
| 959 | * First save the UER then disable the interrupts |
| 960 | */ |
| 961 | ier = sio_in(up, SIOTRCR); |
| 962 | sio_out(up, SIOTRCR, 0); |
| 963 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 964 | uart_console_write(&up->port, s, count, m32r_sio_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
| 966 | /* |
| 967 | * Finally, wait for transmitter to become empty |
| 968 | * and restore the IER |
| 969 | */ |
| 970 | wait_for_xmitr(up); |
| 971 | sio_out(up, SIOTRCR, ier); |
| 972 | } |
| 973 | |
| 974 | static int __init m32r_sio_console_setup(struct console *co, char *options) |
| 975 | { |
| 976 | struct uart_port *port; |
| 977 | int baud = 9600; |
| 978 | int bits = 8; |
| 979 | int parity = 'n'; |
| 980 | int flow = 'n'; |
| 981 | |
| 982 | /* |
| 983 | * Check whether an invalid uart number has been specified, and |
| 984 | * if so, search for the first available port that does have |
| 985 | * console support. |
| 986 | */ |
| 987 | if (co->index >= UART_NR) |
| 988 | co->index = 0; |
| 989 | port = &m32r_sio_ports[co->index].port; |
| 990 | |
| 991 | /* |
| 992 | * Temporary fix. |
| 993 | */ |
| 994 | spin_lock_init(&port->lock); |
| 995 | |
| 996 | if (options) |
| 997 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 998 | |
| 999 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 1000 | } |
| 1001 | |
Al Viro | a828b8e | 2005-08-23 22:47:27 +0100 | [diff] [blame] | 1002 | static struct uart_driver m32r_sio_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | static struct console m32r_sio_console = { |
| 1004 | .name = "ttyS", |
| 1005 | .write = m32r_sio_console_write, |
| 1006 | .device = uart_console_device, |
| 1007 | .setup = m32r_sio_console_setup, |
| 1008 | .flags = CON_PRINTBUFFER, |
| 1009 | .index = -1, |
| 1010 | .data = &m32r_sio_reg, |
| 1011 | }; |
| 1012 | |
| 1013 | static int __init m32r_sio_console_init(void) |
| 1014 | { |
| 1015 | sio_reset(); |
| 1016 | sio_init(); |
| 1017 | m32r_sio_init_ports(); |
| 1018 | register_console(&m32r_sio_console); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | console_initcall(m32r_sio_console_init); |
| 1022 | |
| 1023 | #define M32R_SIO_CONSOLE &m32r_sio_console |
| 1024 | #else |
| 1025 | #define M32R_SIO_CONSOLE NULL |
| 1026 | #endif |
| 1027 | |
| 1028 | static struct uart_driver m32r_sio_reg = { |
| 1029 | .owner = THIS_MODULE, |
| 1030 | .driver_name = "sio", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | .dev_name = "ttyS", |
| 1032 | .major = TTY_MAJOR, |
| 1033 | .minor = 64, |
| 1034 | .nr = UART_NR, |
| 1035 | .cons = M32R_SIO_CONSOLE, |
| 1036 | }; |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | static int __init m32r_sio_init(void) |
| 1039 | { |
| 1040 | int ret, i; |
| 1041 | |
Adrian Bunk | d87a6d9 | 2008-07-16 21:53:31 +0100 | [diff] [blame] | 1042 | printk(KERN_INFO "Serial: M32R SIO driver\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 1044 | for (i = 0; i < nr_irqs; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | spin_lock_init(&irq_lists[i].lock); |
| 1046 | |
| 1047 | ret = uart_register_driver(&m32r_sio_reg); |
| 1048 | if (ret >= 0) |
| 1049 | m32r_sio_register_ports(&m32r_sio_reg); |
| 1050 | |
| 1051 | return ret; |
| 1052 | } |
Paul Gortmaker | a10aebe | 2016-06-20 18:55:05 -0400 | [diff] [blame] | 1053 | device_initcall(m32r_sio_init); |