Merge branches 'release' and 'autoload' into release
[sfrench/cifs-2.6.git] / drivers / serial / atmel_serial.c
1 /*
2  *  linux/drivers/char/atmel_serial.c
3  *
4  *  Driver for Atmel AT91 / AT32 Serial ports
5  *  Copyright (C) 2003 Rick Bronson
6  *
7  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
8  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  */
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/serial.h>
31 #include <linux/clk.h>
32 #include <linux/console.h>
33 #include <linux/sysrq.h>
34 #include <linux/tty_flip.h>
35 #include <linux/platform_device.h>
36 #include <linux/atmel_pdc.h>
37 #include <linux/atmel_serial.h>
38
39 #include <asm/io.h>
40
41 #include <asm/mach/serial_at91.h>
42 #include <asm/arch/board.h>
43
44 #ifdef CONFIG_ARM
45 #include <asm/arch/cpu.h>
46 #include <asm/arch/gpio.h>
47 #endif
48
49 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50 #define SUPPORT_SYSRQ
51 #endif
52
53 #include <linux/serial_core.h>
54
55 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
56
57 /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
58  * should coexist with the 8250 driver, such as if we have an external 16C550
59  * UART. */
60 #define SERIAL_ATMEL_MAJOR      204
61 #define MINOR_START             154
62 #define ATMEL_DEVICENAME        "ttyAT"
63
64 #else
65
66 /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
67  * name, but it is legally reserved for the 8250 driver. */
68 #define SERIAL_ATMEL_MAJOR      TTY_MAJOR
69 #define MINOR_START             64
70 #define ATMEL_DEVICENAME        "ttyS"
71
72 #endif
73
74 #define ATMEL_ISR_PASS_LIMIT    256
75
76 #define UART_PUT_CR(port,v)     __raw_writel(v, (port)->membase + ATMEL_US_CR)
77 #define UART_GET_MR(port)       __raw_readl((port)->membase + ATMEL_US_MR)
78 #define UART_PUT_MR(port,v)     __raw_writel(v, (port)->membase + ATMEL_US_MR)
79 #define UART_PUT_IER(port,v)    __raw_writel(v, (port)->membase + ATMEL_US_IER)
80 #define UART_PUT_IDR(port,v)    __raw_writel(v, (port)->membase + ATMEL_US_IDR)
81 #define UART_GET_IMR(port)      __raw_readl((port)->membase + ATMEL_US_IMR)
82 #define UART_GET_CSR(port)      __raw_readl((port)->membase + ATMEL_US_CSR)
83 #define UART_GET_CHAR(port)     __raw_readl((port)->membase + ATMEL_US_RHR)
84 #define UART_PUT_CHAR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_THR)
85 #define UART_GET_BRGR(port)     __raw_readl((port)->membase + ATMEL_US_BRGR)
86 #define UART_PUT_BRGR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
87 #define UART_PUT_RTOR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
88
89 // #define UART_GET_CR(port)    __raw_readl((port)->membase + ATMEL_US_CR)              // is write-only
90
91  /* PDC registers */
92 #define UART_PUT_PTCR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
93 #define UART_GET_PTSR(port)     __raw_readl((port)->membase + ATMEL_PDC_PTSR)
94
95 #define UART_PUT_RPR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
96 #define UART_GET_RPR(port)      __raw_readl((port)->membase + ATMEL_PDC_RPR)
97 #define UART_PUT_RCR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
98 #define UART_PUT_RNPR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
99 #define UART_PUT_RNCR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
100
101 #define UART_PUT_TPR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
102 #define UART_PUT_TCR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
103 //#define UART_PUT_TNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNPR)
104 //#define UART_PUT_TNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNCR)
105
106 static int (*atmel_open_hook)(struct uart_port *);
107 static void (*atmel_close_hook)(struct uart_port *);
108
109 /*
110  * We wrap our port structure around the generic uart_port.
111  */
112 struct atmel_uart_port {
113         struct uart_port        uart;           /* uart */
114         struct clk              *clk;           /* uart clock */
115         unsigned short          suspended;      /* is port suspended? */
116         int                     break_active;   /* break being received */
117 };
118
119 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
120
121 #ifdef SUPPORT_SYSRQ
122 static struct console atmel_console;
123 #endif
124
125 /*
126  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
127  */
128 static u_int atmel_tx_empty(struct uart_port *port)
129 {
130         return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
131 }
132
133 /*
134  * Set state of the modem control output lines
135  */
136 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
137 {
138         unsigned int control = 0;
139         unsigned int mode;
140
141 #ifdef CONFIG_ARCH_AT91RM9200
142         if (cpu_is_at91rm9200()) {
143                 /*
144                  * AT91RM9200 Errata #39: RTS0 is not internally connected to PA21.
145                  *  We need to drive the pin manually.
146                  */
147                 if (port->mapbase == AT91RM9200_BASE_US0) {
148                         if (mctrl & TIOCM_RTS)
149                                 at91_set_gpio_value(AT91_PIN_PA21, 0);
150                         else
151                                 at91_set_gpio_value(AT91_PIN_PA21, 1);
152                 }
153         }
154 #endif
155
156         if (mctrl & TIOCM_RTS)
157                 control |= ATMEL_US_RTSEN;
158         else
159                 control |= ATMEL_US_RTSDIS;
160
161         if (mctrl & TIOCM_DTR)
162                 control |= ATMEL_US_DTREN;
163         else
164                 control |= ATMEL_US_DTRDIS;
165
166         UART_PUT_CR(port, control);
167
168         /* Local loopback mode? */
169         mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
170         if (mctrl & TIOCM_LOOP)
171                 mode |= ATMEL_US_CHMODE_LOC_LOOP;
172         else
173                 mode |= ATMEL_US_CHMODE_NORMAL;
174         UART_PUT_MR(port, mode);
175 }
176
177 /*
178  * Get state of the modem control input lines
179  */
180 static u_int atmel_get_mctrl(struct uart_port *port)
181 {
182         unsigned int status, ret = 0;
183
184         status = UART_GET_CSR(port);
185
186         /*
187          * The control signals are active low.
188          */
189         if (!(status & ATMEL_US_DCD))
190                 ret |= TIOCM_CD;
191         if (!(status & ATMEL_US_CTS))
192                 ret |= TIOCM_CTS;
193         if (!(status & ATMEL_US_DSR))
194                 ret |= TIOCM_DSR;
195         if (!(status & ATMEL_US_RI))
196                 ret |= TIOCM_RI;
197
198         return ret;
199 }
200
201 /*
202  * Stop transmitting.
203  */
204 static void atmel_stop_tx(struct uart_port *port)
205 {
206         UART_PUT_IDR(port, ATMEL_US_TXRDY);
207 }
208
209 /*
210  * Start transmitting.
211  */
212 static void atmel_start_tx(struct uart_port *port)
213 {
214         UART_PUT_IER(port, ATMEL_US_TXRDY);
215 }
216
217 /*
218  * Stop receiving - port is in process of being closed.
219  */
220 static void atmel_stop_rx(struct uart_port *port)
221 {
222         UART_PUT_IDR(port, ATMEL_US_RXRDY);
223 }
224
225 /*
226  * Enable modem status interrupts
227  */
228 static void atmel_enable_ms(struct uart_port *port)
229 {
230         UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
231 }
232
233 /*
234  * Control the transmission of a break signal
235  */
236 static void atmel_break_ctl(struct uart_port *port, int break_state)
237 {
238         if (break_state != 0)
239                 UART_PUT_CR(port, ATMEL_US_STTBRK);     /* start break */
240         else
241                 UART_PUT_CR(port, ATMEL_US_STPBRK);     /* stop break */
242 }
243
244 /*
245  * Characters received (called from interrupt handler)
246  */
247 static void atmel_rx_chars(struct uart_port *port)
248 {
249         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
250         struct tty_struct *tty = port->info->tty;
251         unsigned int status, ch, flg;
252
253         status = UART_GET_CSR(port);
254         while (status & ATMEL_US_RXRDY) {
255                 ch = UART_GET_CHAR(port);
256
257                 port->icount.rx++;
258
259                 flg = TTY_NORMAL;
260
261                 /*
262                  * note that the error handling code is
263                  * out of the main execution path
264                  */
265                 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
266                                        | ATMEL_US_OVRE | ATMEL_US_RXBRK)
267                              || atmel_port->break_active)) {
268                         UART_PUT_CR(port, ATMEL_US_RSTSTA);     /* clear error */
269                         if (status & ATMEL_US_RXBRK
270                             && !atmel_port->break_active) {
271                                 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);    /* ignore side-effect */
272                                 port->icount.brk++;
273                                 atmel_port->break_active = 1;
274                                 UART_PUT_IER(port, ATMEL_US_RXBRK);
275                                 if (uart_handle_break(port))
276                                         goto ignore_char;
277                         } else {
278                                 /*
279                                  * This is either the end-of-break
280                                  * condition or we've received at
281                                  * least one character without RXBRK
282                                  * being set. In both cases, the next
283                                  * RXBRK will indicate start-of-break.
284                                  */
285                                 UART_PUT_IDR(port, ATMEL_US_RXBRK);
286                                 status &= ~ATMEL_US_RXBRK;
287                                 atmel_port->break_active = 0;
288                         }
289                         if (status & ATMEL_US_PARE)
290                                 port->icount.parity++;
291                         if (status & ATMEL_US_FRAME)
292                                 port->icount.frame++;
293                         if (status & ATMEL_US_OVRE)
294                                 port->icount.overrun++;
295
296                         status &= port->read_status_mask;
297
298                         if (status & ATMEL_US_RXBRK)
299                                 flg = TTY_BREAK;
300                         else if (status & ATMEL_US_PARE)
301                                 flg = TTY_PARITY;
302                         else if (status & ATMEL_US_FRAME)
303                                 flg = TTY_FRAME;
304                 }
305
306                 if (uart_handle_sysrq_char(port, ch))
307                         goto ignore_char;
308
309                 uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
310
311         ignore_char:
312                 status = UART_GET_CSR(port);
313         }
314
315         tty_flip_buffer_push(tty);
316 }
317
318 /*
319  * Transmit characters (called from interrupt handler)
320  */
321 static void atmel_tx_chars(struct uart_port *port)
322 {
323         struct circ_buf *xmit = &port->info->xmit;
324
325         if (port->x_char) {
326                 UART_PUT_CHAR(port, port->x_char);
327                 port->icount.tx++;
328                 port->x_char = 0;
329                 return;
330         }
331         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
332                 atmel_stop_tx(port);
333                 return;
334         }
335
336         while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
337                 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
338                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
339                 port->icount.tx++;
340                 if (uart_circ_empty(xmit))
341                         break;
342         }
343
344         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
345                 uart_write_wakeup(port);
346
347         if (uart_circ_empty(xmit))
348                 atmel_stop_tx(port);
349 }
350
351 /*
352  * Interrupt handler
353  */
354 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
355 {
356         struct uart_port *port = dev_id;
357         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
358         unsigned int status, pending, pass_counter = 0;
359
360         status = UART_GET_CSR(port);
361         pending = status & UART_GET_IMR(port);
362         while (pending) {
363                 /* Interrupt receive */
364                 if (pending & ATMEL_US_RXRDY)
365                         atmel_rx_chars(port);
366                 else if (pending & ATMEL_US_RXBRK) {
367                         /*
368                          * End of break detected. If it came along
369                          * with a character, atmel_rx_chars will
370                          * handle it.
371                          */
372                         UART_PUT_CR(port, ATMEL_US_RSTSTA);
373                         UART_PUT_IDR(port, ATMEL_US_RXBRK);
374                         atmel_port->break_active = 0;
375                 }
376
377                 // TODO: All reads to CSR will clear these interrupts!
378                 if (pending & ATMEL_US_RIIC) port->icount.rng++;
379                 if (pending & ATMEL_US_DSRIC) port->icount.dsr++;
380                 if (pending & ATMEL_US_DCDIC)
381                         uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
382                 if (pending & ATMEL_US_CTSIC)
383                         uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
384                 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC))
385                         wake_up_interruptible(&port->info->delta_msr_wait);
386
387                 /* Interrupt transmit */
388                 if (pending & ATMEL_US_TXRDY)
389                         atmel_tx_chars(port);
390
391                 if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
392                         break;
393
394                 status = UART_GET_CSR(port);
395                 pending = status & UART_GET_IMR(port);
396         }
397         return IRQ_HANDLED;
398 }
399
400 /*
401  * Perform initialization and enable port for reception
402  */
403 static int atmel_startup(struct uart_port *port)
404 {
405         int retval;
406
407         /*
408          * Ensure that no interrupts are enabled otherwise when
409          * request_irq() is called we could get stuck trying to
410          * handle an unexpected interrupt
411          */
412         UART_PUT_IDR(port, -1);
413
414         /*
415          * Allocate the IRQ
416          */
417         retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, "atmel_serial", port);
418         if (retval) {
419                 printk("atmel_serial: atmel_startup - Can't get irq\n");
420                 return retval;
421         }
422
423         /*
424          * If there is a specific "open" function (to register
425          * control line interrupts)
426          */
427         if (atmel_open_hook) {
428                 retval = atmel_open_hook(port);
429                 if (retval) {
430                         free_irq(port->irq, port);
431                         return retval;
432                 }
433         }
434
435         /*
436          * Finally, enable the serial port
437          */
438         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
439         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);               /* enable xmit & rcvr */
440
441         UART_PUT_IER(port, ATMEL_US_RXRDY);             /* enable receive only */
442
443         return 0;
444 }
445
446 /*
447  * Disable the port
448  */
449 static void atmel_shutdown(struct uart_port *port)
450 {
451         /*
452          * Disable all interrupts, port and break condition.
453          */
454         UART_PUT_CR(port, ATMEL_US_RSTSTA);
455         UART_PUT_IDR(port, -1);
456
457         /*
458          * Free the interrupt
459          */
460         free_irq(port->irq, port);
461
462         /*
463          * If there is a specific "close" function (to unregister
464          * control line interrupts)
465          */
466         if (atmel_close_hook)
467                 atmel_close_hook(port);
468 }
469
470 /*
471  * Power / Clock management.
472  */
473 static void atmel_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
474 {
475         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
476
477         switch (state) {
478                 case 0:
479                         /*
480                          * Enable the peripheral clock for this serial port.
481                          * This is called on uart_open() or a resume event.
482                          */
483                         clk_enable(atmel_port->clk);
484                         break;
485                 case 3:
486                         /*
487                          * Disable the peripheral clock for this serial port.
488                          * This is called on uart_close() or a suspend event.
489                          */
490                         clk_disable(atmel_port->clk);
491                         break;
492                 default:
493                         printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
494         }
495 }
496
497 /*
498  * Change the port parameters
499  */
500 static void atmel_set_termios(struct uart_port *port, struct ktermios * termios, struct ktermios * old)
501 {
502         unsigned long flags;
503         unsigned int mode, imr, quot, baud;
504
505         /* Get current mode register */
506         mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
507
508         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
509         quot = uart_get_divisor(port, baud);
510
511         if (quot > 65535) {             /* BRGR is 16-bit, so switch to slower clock */
512                 quot /= 8;
513                 mode |= ATMEL_US_USCLKS_MCK_DIV8;
514         }
515
516         /* byte size */
517         switch (termios->c_cflag & CSIZE) {
518         case CS5:
519                 mode |= ATMEL_US_CHRL_5;
520                 break;
521         case CS6:
522                 mode |= ATMEL_US_CHRL_6;
523                 break;
524         case CS7:
525                 mode |= ATMEL_US_CHRL_7;
526                 break;
527         default:
528                 mode |= ATMEL_US_CHRL_8;
529                 break;
530         }
531
532         /* stop bits */
533         if (termios->c_cflag & CSTOPB)
534                 mode |= ATMEL_US_NBSTOP_2;
535
536         /* parity */
537         if (termios->c_cflag & PARENB) {
538                 if (termios->c_cflag & CMSPAR) {                        /* Mark or Space parity */
539                         if (termios->c_cflag & PARODD)
540                                 mode |= ATMEL_US_PAR_MARK;
541                         else
542                                 mode |= ATMEL_US_PAR_SPACE;
543                 }
544                 else if (termios->c_cflag & PARODD)
545                         mode |= ATMEL_US_PAR_ODD;
546                 else
547                         mode |= ATMEL_US_PAR_EVEN;
548         }
549         else
550                 mode |= ATMEL_US_PAR_NONE;
551
552         spin_lock_irqsave(&port->lock, flags);
553
554         port->read_status_mask = ATMEL_US_OVRE;
555         if (termios->c_iflag & INPCK)
556                 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
557         if (termios->c_iflag & (BRKINT | PARMRK))
558                 port->read_status_mask |= ATMEL_US_RXBRK;
559
560         /*
561          * Characters to ignore
562          */
563         port->ignore_status_mask = 0;
564         if (termios->c_iflag & IGNPAR)
565                 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
566         if (termios->c_iflag & IGNBRK) {
567                 port->ignore_status_mask |= ATMEL_US_RXBRK;
568                 /*
569                  * If we're ignoring parity and break indicators,
570                  * ignore overruns too (for real raw support).
571                  */
572                 if (termios->c_iflag & IGNPAR)
573                         port->ignore_status_mask |= ATMEL_US_OVRE;
574         }
575
576         // TODO: Ignore all characters if CREAD is set.
577
578         /* update the per-port timeout */
579         uart_update_timeout(port, termios->c_cflag, baud);
580
581         /* disable interrupts and drain transmitter */
582         imr = UART_GET_IMR(port);       /* get interrupt mask */
583         UART_PUT_IDR(port, -1);         /* disable all interrupts */
584         while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
585
586         /* disable receiver and transmitter */
587         UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
588
589         /* set the parity, stop bits and data size */
590         UART_PUT_MR(port, mode);
591
592         /* set the baud rate */
593         UART_PUT_BRGR(port, quot);
594         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
595         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
596
597         /* restore interrupts */
598         UART_PUT_IER(port, imr);
599
600         /* CTS flow-control and modem-status interrupts */
601         if (UART_ENABLE_MS(port, termios->c_cflag))
602                 port->ops->enable_ms(port);
603
604         spin_unlock_irqrestore(&port->lock, flags);
605 }
606
607 /*
608  * Return string describing the specified port
609  */
610 static const char *atmel_type(struct uart_port *port)
611 {
612         return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
613 }
614
615 /*
616  * Release the memory region(s) being used by 'port'.
617  */
618 static void atmel_release_port(struct uart_port *port)
619 {
620         struct platform_device *pdev = to_platform_device(port->dev);
621         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
622
623         release_mem_region(port->mapbase, size);
624
625         if (port->flags & UPF_IOREMAP) {
626                 iounmap(port->membase);
627                 port->membase = NULL;
628         }
629 }
630
631 /*
632  * Request the memory region(s) being used by 'port'.
633  */
634 static int atmel_request_port(struct uart_port *port)
635 {
636         struct platform_device *pdev = to_platform_device(port->dev);
637         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
638
639         if (!request_mem_region(port->mapbase, size, "atmel_serial"))
640                 return -EBUSY;
641
642         if (port->flags & UPF_IOREMAP) {
643                 port->membase = ioremap(port->mapbase, size);
644                 if (port->membase == NULL) {
645                         release_mem_region(port->mapbase, size);
646                         return -ENOMEM;
647                 }
648         }
649
650         return 0;
651 }
652
653 /*
654  * Configure/autoconfigure the port.
655  */
656 static void atmel_config_port(struct uart_port *port, int flags)
657 {
658         if (flags & UART_CONFIG_TYPE) {
659                 port->type = PORT_ATMEL;
660                 atmel_request_port(port);
661         }
662 }
663
664 /*
665  * Verify the new serial_struct (for TIOCSSERIAL).
666  */
667 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
668 {
669         int ret = 0;
670         if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
671                 ret = -EINVAL;
672         if (port->irq != ser->irq)
673                 ret = -EINVAL;
674         if (ser->io_type != SERIAL_IO_MEM)
675                 ret = -EINVAL;
676         if (port->uartclk / 16 != ser->baud_base)
677                 ret = -EINVAL;
678         if ((void *)port->mapbase != ser->iomem_base)
679                 ret = -EINVAL;
680         if (port->iobase != ser->port)
681                 ret = -EINVAL;
682         if (ser->hub6 != 0)
683                 ret = -EINVAL;
684         return ret;
685 }
686
687 static struct uart_ops atmel_pops = {
688         .tx_empty       = atmel_tx_empty,
689         .set_mctrl      = atmel_set_mctrl,
690         .get_mctrl      = atmel_get_mctrl,
691         .stop_tx        = atmel_stop_tx,
692         .start_tx       = atmel_start_tx,
693         .stop_rx        = atmel_stop_rx,
694         .enable_ms      = atmel_enable_ms,
695         .break_ctl      = atmel_break_ctl,
696         .startup        = atmel_startup,
697         .shutdown       = atmel_shutdown,
698         .set_termios    = atmel_set_termios,
699         .type           = atmel_type,
700         .release_port   = atmel_release_port,
701         .request_port   = atmel_request_port,
702         .config_port    = atmel_config_port,
703         .verify_port    = atmel_verify_port,
704         .pm             = atmel_serial_pm,
705 };
706
707 /*
708  * Configure the port from the platform device resource info.
709  */
710 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
711 {
712         struct uart_port *port = &atmel_port->uart;
713         struct atmel_uart_data *data = pdev->dev.platform_data;
714
715         port->iotype    = UPIO_MEM;
716         port->flags     = UPF_BOOT_AUTOCONF;
717         port->ops       = &atmel_pops;
718         port->fifosize  = 1;
719         port->line      = pdev->id;
720         port->dev       = &pdev->dev;
721
722         port->mapbase   = pdev->resource[0].start;
723         port->irq       = pdev->resource[1].start;
724
725         if (data->regs)
726                 /* Already mapped by setup code */
727                 port->membase = data->regs;
728         else {
729                 port->flags     |= UPF_IOREMAP;
730                 port->membase   = NULL;
731         }
732
733         if (!atmel_port->clk) {         /* for console, the clock could already be configured */
734                 atmel_port->clk = clk_get(&pdev->dev, "usart");
735                 clk_enable(atmel_port->clk);
736                 port->uartclk = clk_get_rate(atmel_port->clk);
737         }
738 }
739
740 /*
741  * Register board-specific modem-control line handlers.
742  */
743 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
744 {
745         if (fns->enable_ms)
746                 atmel_pops.enable_ms = fns->enable_ms;
747         if (fns->get_mctrl)
748                 atmel_pops.get_mctrl = fns->get_mctrl;
749         if (fns->set_mctrl)
750                 atmel_pops.set_mctrl = fns->set_mctrl;
751         atmel_open_hook         = fns->open;
752         atmel_close_hook        = fns->close;
753         atmel_pops.pm           = fns->pm;
754         atmel_pops.set_wake     = fns->set_wake;
755 }
756
757
758 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
759 static void atmel_console_putchar(struct uart_port *port, int ch)
760 {
761         while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
762                 barrier();
763         UART_PUT_CHAR(port, ch);
764 }
765
766 /*
767  * Interrupts are disabled on entering
768  */
769 static void atmel_console_write(struct console *co, const char *s, u_int count)
770 {
771         struct uart_port *port = &atmel_ports[co->index].uart;
772         unsigned int status, imr;
773
774         /*
775          *      First, save IMR and then disable interrupts
776          */
777         imr = UART_GET_IMR(port);       /* get interrupt mask */
778         UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
779
780         uart_console_write(port, s, count, atmel_console_putchar);
781
782         /*
783          *      Finally, wait for transmitter to become empty
784          *      and restore IMR
785          */
786         do {
787                 status = UART_GET_CSR(port);
788         } while (!(status & ATMEL_US_TXRDY));
789         UART_PUT_IER(port, imr);        /* set interrupts back the way they were */
790 }
791
792 /*
793  * If the port was already initialised (eg, by a boot loader), try to determine
794  * the current setup.
795  */
796 static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
797 {
798         unsigned int mr, quot;
799
800 // TODO: CR is a write-only register
801 //      unsigned int cr;
802 //
803 //      cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
804 //      if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
805 //              /* ok, the port was enabled */
806 //      }
807
808         mr = UART_GET_MR(port) & ATMEL_US_CHRL;
809         if (mr == ATMEL_US_CHRL_8)
810                 *bits = 8;
811         else
812                 *bits = 7;
813
814         mr = UART_GET_MR(port) & ATMEL_US_PAR;
815         if (mr == ATMEL_US_PAR_EVEN)
816                 *parity = 'e';
817         else if (mr == ATMEL_US_PAR_ODD)
818                 *parity = 'o';
819
820         /*
821          * The serial core only rounds down when matching this to a
822          * supported baud rate. Make sure we don't end up slightly
823          * lower than one of those, as it would make us fall through
824          * to a much lower baud rate than we really want.
825          */
826         quot = UART_GET_BRGR(port);
827         *baud = port->uartclk / (16 * (quot - 1));
828 }
829
830 static int __init atmel_console_setup(struct console *co, char *options)
831 {
832         struct uart_port *port = &atmel_ports[co->index].uart;
833         int baud = 115200;
834         int bits = 8;
835         int parity = 'n';
836         int flow = 'n';
837
838         if (port->membase == 0)         /* Port not initialized yet - delay setup */
839                 return -ENODEV;
840
841         UART_PUT_IDR(port, -1);                         /* disable interrupts */
842         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
843         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
844
845         if (options)
846                 uart_parse_options(options, &baud, &parity, &bits, &flow);
847         else
848                 atmel_console_get_options(port, &baud, &parity, &bits);
849
850         return uart_set_options(port, co, baud, parity, bits, flow);
851 }
852
853 static struct uart_driver atmel_uart;
854
855 static struct console atmel_console = {
856         .name           = ATMEL_DEVICENAME,
857         .write          = atmel_console_write,
858         .device         = uart_console_device,
859         .setup          = atmel_console_setup,
860         .flags          = CON_PRINTBUFFER,
861         .index          = -1,
862         .data           = &atmel_uart,
863 };
864
865 #define ATMEL_CONSOLE_DEVICE    &atmel_console
866
867 /*
868  * Early console initialization (before VM subsystem initialized).
869  */
870 static int __init atmel_console_init(void)
871 {
872         if (atmel_default_console_device) {
873                 add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
874                 atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
875                 register_console(&atmel_console);
876         }
877
878         return 0;
879 }
880 console_initcall(atmel_console_init);
881
882 /*
883  * Late console initialization.
884  */
885 static int __init atmel_late_console_init(void)
886 {
887         if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
888                 register_console(&atmel_console);
889
890         return 0;
891 }
892 core_initcall(atmel_late_console_init);
893
894 #else
895 #define ATMEL_CONSOLE_DEVICE    NULL
896 #endif
897
898 static struct uart_driver atmel_uart = {
899         .owner                  = THIS_MODULE,
900         .driver_name            = "atmel_serial",
901         .dev_name               = ATMEL_DEVICENAME,
902         .major                  = SERIAL_ATMEL_MAJOR,
903         .minor                  = MINOR_START,
904         .nr                     = ATMEL_MAX_UART,
905         .cons                   = ATMEL_CONSOLE_DEVICE,
906 };
907
908 #ifdef CONFIG_PM
909 static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
910 {
911         struct uart_port *port = platform_get_drvdata(pdev);
912         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
913
914         if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
915                 enable_irq_wake(port->irq);
916         else {
917                 uart_suspend_port(&atmel_uart, port);
918                 atmel_port->suspended = 1;
919         }
920
921         return 0;
922 }
923
924 static int atmel_serial_resume(struct platform_device *pdev)
925 {
926         struct uart_port *port = platform_get_drvdata(pdev);
927         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
928
929         if (atmel_port->suspended) {
930                 uart_resume_port(&atmel_uart, port);
931                 atmel_port->suspended = 0;
932         }
933         else
934                 disable_irq_wake(port->irq);
935
936         return 0;
937 }
938 #else
939 #define atmel_serial_suspend NULL
940 #define atmel_serial_resume NULL
941 #endif
942
943 static int __devinit atmel_serial_probe(struct platform_device *pdev)
944 {
945         struct atmel_uart_port *port;
946         int ret;
947
948         port = &atmel_ports[pdev->id];
949         atmel_init_port(port, pdev);
950
951         ret = uart_add_one_port(&atmel_uart, &port->uart);
952         if (!ret) {
953                 device_init_wakeup(&pdev->dev, 1);
954                 platform_set_drvdata(pdev, port);
955         }
956
957         return ret;
958 }
959
960 static int __devexit atmel_serial_remove(struct platform_device *pdev)
961 {
962         struct uart_port *port = platform_get_drvdata(pdev);
963         struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
964         int ret = 0;
965
966         clk_disable(atmel_port->clk);
967         clk_put(atmel_port->clk);
968
969         device_init_wakeup(&pdev->dev, 0);
970         platform_set_drvdata(pdev, NULL);
971
972         if (port) {
973                 ret = uart_remove_one_port(&atmel_uart, port);
974                 kfree(port);
975         }
976
977         return ret;
978 }
979
980 static struct platform_driver atmel_serial_driver = {
981         .probe          = atmel_serial_probe,
982         .remove         = __devexit_p(atmel_serial_remove),
983         .suspend        = atmel_serial_suspend,
984         .resume         = atmel_serial_resume,
985         .driver         = {
986                 .name   = "atmel_usart",
987                 .owner  = THIS_MODULE,
988         },
989 };
990
991 static int __init atmel_serial_init(void)
992 {
993         int ret;
994
995         ret = uart_register_driver(&atmel_uart);
996         if (ret)
997                 return ret;
998
999         ret = platform_driver_register(&atmel_serial_driver);
1000         if (ret)
1001                 uart_unregister_driver(&atmel_uart);
1002
1003         return ret;
1004 }
1005
1006 static void __exit atmel_serial_exit(void)
1007 {
1008         platform_driver_unregister(&atmel_serial_driver);
1009         uart_unregister_driver(&atmel_uart);
1010 }
1011
1012 module_init(atmel_serial_init);
1013 module_exit(atmel_serial_exit);
1014
1015 MODULE_AUTHOR("Rick Bronson");
1016 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1017 MODULE_LICENSE("GPL");