Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / serial / cpm_uart / cpm_uart_core.c
1 /*
2  *  linux/drivers/serial/cpm_uart.c
3  *
4  *  Driver for CPM (SCC/SMC) serial ports; core driver
5  *
6  *  Based on arch/ppc/cpm2_io/uart.c by Dan Malek
7  *  Based on ppc8xx.c by Thomas Gleixner
8  *  Based on drivers/serial/amba.c by Russell King
9  *
10  *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
11  *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
12  *
13  *  Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
14  *            (C) 2004 Intracom, S.A.
15  *            (C) 2005-2006 MontaVista Software, Inc.
16  *              Vitaly Bordug <vbordug@ru.mvista.com>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33
34 #include <linux/module.h>
35 #include <linux/tty.h>
36 #include <linux/ioport.h>
37 #include <linux/init.h>
38 #include <linux/serial.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/device.h>
42 #include <linux/bootmem.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/fs_uart_pd.h>
45 #include <linux/of_platform.h>
46
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/delay.h>
50 #include <asm/fs_pd.h>
51 #include <asm/udbg.h>
52
53 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54 #define SUPPORT_SYSRQ
55 #endif
56
57 #include <linux/serial_core.h>
58 #include <linux/kernel.h>
59
60 #include "cpm_uart.h"
61
62
63 /**************************************************************/
64
65 static int  cpm_uart_tx_pump(struct uart_port *port);
66 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
67 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
68 static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
69
70 /**************************************************************/
71
72 /*
73  * Check, if transmit buffers are processed
74 */
75 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
76 {
77         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
78         cbd_t __iomem *bdp = pinfo->tx_bd_base;
79         int ret = 0;
80
81         while (1) {
82                 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
83                         break;
84
85                 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
86                         ret = TIOCSER_TEMT;
87                         break;
88                 }
89                 bdp++;
90         }
91
92         pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
93
94         return ret;
95 }
96
97 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
98 {
99         /* Whee. Do nothing. */
100 }
101
102 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
103 {
104         /* Whee. Do nothing. */
105         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
106 }
107
108 /*
109  * Stop transmitter
110  */
111 static void cpm_uart_stop_tx(struct uart_port *port)
112 {
113         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114         smc_t __iomem *smcp = pinfo->smcp;
115         scc_t __iomem *sccp = pinfo->sccp;
116
117         pr_debug("CPM uart[%d]:stop tx\n", port->line);
118
119         if (IS_SMC(pinfo))
120                 clrbits8(&smcp->smc_smcm, SMCM_TX);
121         else
122                 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
123 }
124
125 /*
126  * Start transmitter
127  */
128 static void cpm_uart_start_tx(struct uart_port *port)
129 {
130         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
131         smc_t __iomem *smcp = pinfo->smcp;
132         scc_t __iomem *sccp = pinfo->sccp;
133
134         pr_debug("CPM uart[%d]:start tx\n", port->line);
135
136         if (IS_SMC(pinfo)) {
137                 if (in_8(&smcp->smc_smcm) & SMCM_TX)
138                         return;
139         } else {
140                 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
141                         return;
142         }
143
144         if (cpm_uart_tx_pump(port) != 0) {
145                 if (IS_SMC(pinfo)) {
146                         setbits8(&smcp->smc_smcm, SMCM_TX);
147                 } else {
148                         setbits16(&sccp->scc_sccm, UART_SCCM_TX);
149                 }
150         }
151 }
152
153 /*
154  * Stop receiver
155  */
156 static void cpm_uart_stop_rx(struct uart_port *port)
157 {
158         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
159         smc_t __iomem *smcp = pinfo->smcp;
160         scc_t __iomem *sccp = pinfo->sccp;
161
162         pr_debug("CPM uart[%d]:stop rx\n", port->line);
163
164         if (IS_SMC(pinfo))
165                 clrbits8(&smcp->smc_smcm, SMCM_RX);
166         else
167                 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
168 }
169
170 /*
171  * Enable Modem status interrupts
172  */
173 static void cpm_uart_enable_ms(struct uart_port *port)
174 {
175         pr_debug("CPM uart[%d]:enable ms\n", port->line);
176 }
177
178 /*
179  * Generate a break.
180  */
181 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
182 {
183         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
184
185         pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
186                 break_state);
187
188         if (break_state)
189                 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
190         else
191                 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
192 }
193
194 /*
195  * Transmit characters, refill buffer descriptor, if possible
196  */
197 static void cpm_uart_int_tx(struct uart_port *port)
198 {
199         pr_debug("CPM uart[%d]:TX INT\n", port->line);
200
201         cpm_uart_tx_pump(port);
202 }
203
204 /*
205  * Receive characters
206  */
207 static void cpm_uart_int_rx(struct uart_port *port)
208 {
209         int i;
210         unsigned char ch;
211         u8 *cp;
212         struct tty_struct *tty = port->info->port.tty;
213         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
214         cbd_t __iomem *bdp;
215         u16 status;
216         unsigned int flg;
217
218         pr_debug("CPM uart[%d]:RX INT\n", port->line);
219
220         /* Just loop through the closed BDs and copy the characters into
221          * the buffer.
222          */
223         bdp = pinfo->rx_cur;
224         for (;;) {
225                 /* get status */
226                 status = in_be16(&bdp->cbd_sc);
227                 /* If this one is empty, return happy */
228                 if (status & BD_SC_EMPTY)
229                         break;
230
231                 /* get number of characters, and check spce in flip-buffer */
232                 i = in_be16(&bdp->cbd_datlen);
233
234                 /* If we have not enough room in tty flip buffer, then we try
235                  * later, which will be the next rx-interrupt or a timeout
236                  */
237                 if(tty_buffer_request_room(tty, i) < i) {
238                         printk(KERN_WARNING "No room in flip buffer\n");
239                         return;
240                 }
241
242                 /* get pointer */
243                 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
244
245                 /* loop through the buffer */
246                 while (i-- > 0) {
247                         ch = *cp++;
248                         port->icount.rx++;
249                         flg = TTY_NORMAL;
250
251                         if (status &
252                             (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
253                                 goto handle_error;
254                         if (uart_handle_sysrq_char(port, ch))
255                                 continue;
256
257                       error_return:
258                         tty_insert_flip_char(tty, ch, flg);
259
260                 }               /* End while (i--) */
261
262                 /* This BD is ready to be used again. Clear status. get next */
263                 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
264                                         BD_SC_OV | BD_SC_ID);
265                 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
266
267                 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
268                         bdp = pinfo->rx_bd_base;
269                 else
270                         bdp++;
271
272         } /* End for (;;) */
273
274         /* Write back buffer pointer */
275         pinfo->rx_cur = bdp;
276
277         /* activate BH processing */
278         tty_flip_buffer_push(tty);
279
280         return;
281
282         /* Error processing */
283
284       handle_error:
285         /* Statistics */
286         if (status & BD_SC_BR)
287                 port->icount.brk++;
288         if (status & BD_SC_PR)
289                 port->icount.parity++;
290         if (status & BD_SC_FR)
291                 port->icount.frame++;
292         if (status & BD_SC_OV)
293                 port->icount.overrun++;
294
295         /* Mask out ignored conditions */
296         status &= port->read_status_mask;
297
298         /* Handle the remaining ones */
299         if (status & BD_SC_BR)
300                 flg = TTY_BREAK;
301         else if (status & BD_SC_PR)
302                 flg = TTY_PARITY;
303         else if (status & BD_SC_FR)
304                 flg = TTY_FRAME;
305
306         /* overrun does not affect the current character ! */
307         if (status & BD_SC_OV) {
308                 ch = 0;
309                 flg = TTY_OVERRUN;
310                 /* We skip this buffer */
311                 /* CHECK: Is really nothing senseful there */
312                 /* ASSUMPTION: it contains nothing valid */
313                 i = 0;
314         }
315 #ifdef SUPPORT_SYSRQ
316         port->sysrq = 0;
317 #endif
318         goto error_return;
319 }
320
321 /*
322  * Asynchron mode interrupt handler
323  */
324 static irqreturn_t cpm_uart_int(int irq, void *data)
325 {
326         u8 events;
327         struct uart_port *port = data;
328         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
329         smc_t __iomem *smcp = pinfo->smcp;
330         scc_t __iomem *sccp = pinfo->sccp;
331
332         pr_debug("CPM uart[%d]:IRQ\n", port->line);
333
334         if (IS_SMC(pinfo)) {
335                 events = in_8(&smcp->smc_smce);
336                 out_8(&smcp->smc_smce, events);
337                 if (events & SMCM_BRKE)
338                         uart_handle_break(port);
339                 if (events & SMCM_RX)
340                         cpm_uart_int_rx(port);
341                 if (events & SMCM_TX)
342                         cpm_uart_int_tx(port);
343         } else {
344                 events = in_be16(&sccp->scc_scce);
345                 out_be16(&sccp->scc_scce, events);
346                 if (events & UART_SCCM_BRKE)
347                         uart_handle_break(port);
348                 if (events & UART_SCCM_RX)
349                         cpm_uart_int_rx(port);
350                 if (events & UART_SCCM_TX)
351                         cpm_uart_int_tx(port);
352         }
353         return (events) ? IRQ_HANDLED : IRQ_NONE;
354 }
355
356 static int cpm_uart_startup(struct uart_port *port)
357 {
358         int retval;
359         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
360
361         pr_debug("CPM uart[%d]:startup\n", port->line);
362
363         /* Install interrupt handler. */
364         retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
365         if (retval)
366                 return retval;
367
368         /* Startup rx-int */
369         if (IS_SMC(pinfo)) {
370                 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
371                 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
372         } else {
373                 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
374                 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
375         }
376
377         if (!(pinfo->flags & FLAG_CONSOLE))
378                 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
379         return 0;
380 }
381
382 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
383 {
384         set_current_state(TASK_UNINTERRUPTIBLE);
385         schedule_timeout(pinfo->wait_closing);
386 }
387
388 /*
389  * Shutdown the uart
390  */
391 static void cpm_uart_shutdown(struct uart_port *port)
392 {
393         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
394
395         pr_debug("CPM uart[%d]:shutdown\n", port->line);
396
397         /* free interrupt handler */
398         free_irq(port->irq, port);
399
400         /* If the port is not the console, disable Rx and Tx. */
401         if (!(pinfo->flags & FLAG_CONSOLE)) {
402                 /* Wait for all the BDs marked sent */
403                 while(!cpm_uart_tx_empty(port)) {
404                         set_current_state(TASK_UNINTERRUPTIBLE);
405                         schedule_timeout(2);
406                 }
407
408                 if (pinfo->wait_closing)
409                         cpm_uart_wait_until_send(pinfo);
410
411                 /* Stop uarts */
412                 if (IS_SMC(pinfo)) {
413                         smc_t __iomem *smcp = pinfo->smcp;
414                         clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
415                         clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
416                 } else {
417                         scc_t __iomem *sccp = pinfo->sccp;
418                         clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
419                         clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
420                 }
421
422                 /* Shut them really down and reinit buffer descriptors */
423                 if (IS_SMC(pinfo))
424                         cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
425                 else
426                         cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
427
428                 cpm_uart_initbd(pinfo);
429         }
430 }
431
432 static void cpm_uart_set_termios(struct uart_port *port,
433                                  struct ktermios *termios,
434                                  struct ktermios *old)
435 {
436         int baud;
437         unsigned long flags;
438         u16 cval, scval, prev_mode;
439         int bits, sbits;
440         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
441         smc_t __iomem *smcp = pinfo->smcp;
442         scc_t __iomem *sccp = pinfo->sccp;
443
444         pr_debug("CPM uart[%d]:set_termios\n", port->line);
445
446         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
447
448         /* Character length programmed into the mode register is the
449          * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
450          * 1 or 2 stop bits, minus 1.
451          * The value 'bits' counts this for us.
452          */
453         cval = 0;
454         scval = 0;
455
456         /* byte size */
457         switch (termios->c_cflag & CSIZE) {
458         case CS5:
459                 bits = 5;
460                 break;
461         case CS6:
462                 bits = 6;
463                 break;
464         case CS7:
465                 bits = 7;
466                 break;
467         case CS8:
468                 bits = 8;
469                 break;
470                 /* Never happens, but GCC is too dumb to figure it out */
471         default:
472                 bits = 8;
473                 break;
474         }
475         sbits = bits - 5;
476
477         if (termios->c_cflag & CSTOPB) {
478                 cval |= SMCMR_SL;       /* Two stops */
479                 scval |= SCU_PSMR_SL;
480                 bits++;
481         }
482
483         if (termios->c_cflag & PARENB) {
484                 cval |= SMCMR_PEN;
485                 scval |= SCU_PSMR_PEN;
486                 bits++;
487                 if (!(termios->c_cflag & PARODD)) {
488                         cval |= SMCMR_PM_EVEN;
489                         scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
490                 }
491         }
492
493         /*
494          * Update the timeout
495          */
496         uart_update_timeout(port, termios->c_cflag, baud);
497
498         /*
499          * Set up parity check flag
500          */
501 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
502
503         port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
504         if (termios->c_iflag & INPCK)
505                 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
506         if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
507                 port->read_status_mask |= BD_SC_BR;
508
509         /*
510          * Characters to ignore
511          */
512         port->ignore_status_mask = 0;
513         if (termios->c_iflag & IGNPAR)
514                 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
515         if (termios->c_iflag & IGNBRK) {
516                 port->ignore_status_mask |= BD_SC_BR;
517                 /*
518                  * If we're ignore parity and break indicators, ignore
519                  * overruns too.  (For real raw support).
520                  */
521                 if (termios->c_iflag & IGNPAR)
522                         port->ignore_status_mask |= BD_SC_OV;
523         }
524         /*
525          * !!! ignore all characters if CREAD is not set
526          */
527         if ((termios->c_cflag & CREAD) == 0)
528                 port->read_status_mask &= ~BD_SC_EMPTY;
529
530         spin_lock_irqsave(&port->lock, flags);
531
532         /* Start bit has not been added (so don't, because we would just
533          * subtract it later), and we need to add one for the number of
534          * stops bits (there is always at least one).
535          */
536         bits++;
537         if (IS_SMC(pinfo)) {
538                 /* Set the mode register.  We want to keep a copy of the
539                  * enables, because we want to put them back if they were
540                  * present.
541                  */
542                 prev_mode = in_be16(&smcp->smc_smcmr);
543                 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval | SMCMR_SM_UART);
544                 setbits16(&smcp->smc_smcmr, (prev_mode & (SMCMR_REN | SMCMR_TEN)));
545         } else {
546                 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
547         }
548
549         cpm_set_brg(pinfo->brg - 1, baud);
550         spin_unlock_irqrestore(&port->lock, flags);
551 }
552
553 static const char *cpm_uart_type(struct uart_port *port)
554 {
555         pr_debug("CPM uart[%d]:uart_type\n", port->line);
556
557         return port->type == PORT_CPM ? "CPM UART" : NULL;
558 }
559
560 /*
561  * verify the new serial_struct (for TIOCSSERIAL).
562  */
563 static int cpm_uart_verify_port(struct uart_port *port,
564                                 struct serial_struct *ser)
565 {
566         int ret = 0;
567
568         pr_debug("CPM uart[%d]:verify_port\n", port->line);
569
570         if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
571                 ret = -EINVAL;
572         if (ser->irq < 0 || ser->irq >= NR_IRQS)
573                 ret = -EINVAL;
574         if (ser->baud_base < 9600)
575                 ret = -EINVAL;
576         return ret;
577 }
578
579 /*
580  * Transmit characters, refill buffer descriptor, if possible
581  */
582 static int cpm_uart_tx_pump(struct uart_port *port)
583 {
584         cbd_t __iomem *bdp;
585         u8 *p;
586         int count;
587         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
588         struct circ_buf *xmit = &port->info->xmit;
589
590         /* Handle xon/xoff */
591         if (port->x_char) {
592                 /* Pick next descriptor and fill from buffer */
593                 bdp = pinfo->tx_cur;
594
595                 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
596
597                 *p++ = port->x_char;
598
599                 out_be16(&bdp->cbd_datlen, 1);
600                 setbits16(&bdp->cbd_sc, BD_SC_READY);
601                 /* Get next BD. */
602                 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
603                         bdp = pinfo->tx_bd_base;
604                 else
605                         bdp++;
606                 pinfo->tx_cur = bdp;
607
608                 port->icount.tx++;
609                 port->x_char = 0;
610                 return 1;
611         }
612
613         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
614                 cpm_uart_stop_tx(port);
615                 return 0;
616         }
617
618         /* Pick next descriptor and fill from buffer */
619         bdp = pinfo->tx_cur;
620
621         while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
622                xmit->tail != xmit->head) {
623                 count = 0;
624                 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
625                 while (count < pinfo->tx_fifosize) {
626                         *p++ = xmit->buf[xmit->tail];
627                         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
628                         port->icount.tx++;
629                         count++;
630                         if (xmit->head == xmit->tail)
631                                 break;
632                 }
633                 out_be16(&bdp->cbd_datlen, count);
634                 setbits16(&bdp->cbd_sc, BD_SC_READY);
635                 /* Get next BD. */
636                 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
637                         bdp = pinfo->tx_bd_base;
638                 else
639                         bdp++;
640         }
641         pinfo->tx_cur = bdp;
642
643         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
644                 uart_write_wakeup(port);
645
646         if (uart_circ_empty(xmit)) {
647                 cpm_uart_stop_tx(port);
648                 return 0;
649         }
650
651         return 1;
652 }
653
654 /*
655  * init buffer descriptors
656  */
657 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
658 {
659         int i;
660         u8 *mem_addr;
661         cbd_t __iomem *bdp;
662
663         pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
664
665         /* Set the physical address of the host memory
666          * buffers in the buffer descriptors, and the
667          * virtual address for us to work with.
668          */
669         mem_addr = pinfo->mem_addr;
670         bdp = pinfo->rx_cur = pinfo->rx_bd_base;
671         for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
672                 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
673                 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
674                 mem_addr += pinfo->rx_fifosize;
675         }
676
677         out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
678         out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
679
680         /* Set the physical address of the host memory
681          * buffers in the buffer descriptors, and the
682          * virtual address for us to work with.
683          */
684         mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
685         bdp = pinfo->tx_cur = pinfo->tx_bd_base;
686         for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
687                 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
688                 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
689                 mem_addr += pinfo->tx_fifosize;
690         }
691
692         out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
693         out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
694 }
695
696 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
697 {
698         scc_t __iomem *scp;
699         scc_uart_t __iomem *sup;
700
701         pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
702
703         scp = pinfo->sccp;
704         sup = pinfo->sccup;
705
706         /* Store address */
707         out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
708                  (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
709         out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
710                  (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
711
712         /* Set up the uart parameters in the
713          * parameter ram.
714          */
715
716         cpm_set_scc_fcr(sup);
717
718         out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
719         out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
720         out_be16(&sup->scc_brkcr, 1);
721         out_be16(&sup->scc_parec, 0);
722         out_be16(&sup->scc_frmec, 0);
723         out_be16(&sup->scc_nosec, 0);
724         out_be16(&sup->scc_brkec, 0);
725         out_be16(&sup->scc_uaddr1, 0);
726         out_be16(&sup->scc_uaddr2, 0);
727         out_be16(&sup->scc_toseq, 0);
728         out_be16(&sup->scc_char1, 0x8000);
729         out_be16(&sup->scc_char2, 0x8000);
730         out_be16(&sup->scc_char3, 0x8000);
731         out_be16(&sup->scc_char4, 0x8000);
732         out_be16(&sup->scc_char5, 0x8000);
733         out_be16(&sup->scc_char6, 0x8000);
734         out_be16(&sup->scc_char7, 0x8000);
735         out_be16(&sup->scc_char8, 0x8000);
736         out_be16(&sup->scc_rccm, 0xc0ff);
737
738         /* Send the CPM an initialize command.
739          */
740         cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
741
742         /* Set UART mode, 8 bit, no parity, one stop.
743          * Enable receive and transmit.
744          */
745         out_be32(&scp->scc_gsmrh, 0);
746         out_be32(&scp->scc_gsmrl,
747                  SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
748
749         /* Enable rx interrupts  and clear all pending events.  */
750         out_be16(&scp->scc_sccm, 0);
751         out_be16(&scp->scc_scce, 0xffff);
752         out_be16(&scp->scc_dsr, 0x7e7e);
753         out_be16(&scp->scc_psmr, 0x3000);
754
755         setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
756 }
757
758 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
759 {
760         smc_t __iomem *sp;
761         smc_uart_t __iomem *up;
762
763         pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
764
765         sp = pinfo->smcp;
766         up = pinfo->smcup;
767
768         /* Store address */
769         out_be16(&pinfo->smcup->smc_rbase,
770                  (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
771         out_be16(&pinfo->smcup->smc_tbase,
772                  (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
773
774 /*
775  *  In case SMC1 is being relocated...
776  */
777 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
778         out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
779         out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
780         out_be32(&up->smc_rstate, 0);
781         out_be32(&up->smc_tstate, 0);
782         out_be16(&up->smc_brkcr, 1);              /* number of break chars */
783         out_be16(&up->smc_brkec, 0);
784 #endif
785
786         /* Set up the uart parameters in the
787          * parameter ram.
788          */
789         cpm_set_smc_fcr(up);
790
791         /* Using idle charater time requires some additional tuning.  */
792         out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
793         out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
794         out_be16(&up->smc_brklen, 0);
795         out_be16(&up->smc_brkec, 0);
796         out_be16(&up->smc_brkcr, 1);
797
798         cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
799
800         /* Set UART mode, 8 bit, no parity, one stop.
801          * Enable receive and transmit.
802          */
803         out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
804
805         /* Enable only rx interrupts clear all pending events. */
806         out_8(&sp->smc_smcm, 0);
807         out_8(&sp->smc_smce, 0xff);
808
809         setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
810 }
811
812 /*
813  * Initialize port. This is called from early_console stuff
814  * so we have to be careful here !
815  */
816 static int cpm_uart_request_port(struct uart_port *port)
817 {
818         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
819         int ret;
820
821         pr_debug("CPM uart[%d]:request port\n", port->line);
822
823         if (pinfo->flags & FLAG_CONSOLE)
824                 return 0;
825
826         if (IS_SMC(pinfo)) {
827                 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
828                 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
829         } else {
830                 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
831                 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
832         }
833
834         ret = cpm_uart_allocbuf(pinfo, 0);
835
836         if (ret)
837                 return ret;
838
839         cpm_uart_initbd(pinfo);
840         if (IS_SMC(pinfo))
841                 cpm_uart_init_smc(pinfo);
842         else
843                 cpm_uart_init_scc(pinfo);
844
845         return 0;
846 }
847
848 static void cpm_uart_release_port(struct uart_port *port)
849 {
850         struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
851
852         if (!(pinfo->flags & FLAG_CONSOLE))
853                 cpm_uart_freebuf(pinfo);
854 }
855
856 /*
857  * Configure/autoconfigure the port.
858  */
859 static void cpm_uart_config_port(struct uart_port *port, int flags)
860 {
861         pr_debug("CPM uart[%d]:config_port\n", port->line);
862
863         if (flags & UART_CONFIG_TYPE) {
864                 port->type = PORT_CPM;
865                 cpm_uart_request_port(port);
866         }
867 }
868 static struct uart_ops cpm_uart_pops = {
869         .tx_empty       = cpm_uart_tx_empty,
870         .set_mctrl      = cpm_uart_set_mctrl,
871         .get_mctrl      = cpm_uart_get_mctrl,
872         .stop_tx        = cpm_uart_stop_tx,
873         .start_tx       = cpm_uart_start_tx,
874         .stop_rx        = cpm_uart_stop_rx,
875         .enable_ms      = cpm_uart_enable_ms,
876         .break_ctl      = cpm_uart_break_ctl,
877         .startup        = cpm_uart_startup,
878         .shutdown       = cpm_uart_shutdown,
879         .set_termios    = cpm_uart_set_termios,
880         .type           = cpm_uart_type,
881         .release_port   = cpm_uart_release_port,
882         .request_port   = cpm_uart_request_port,
883         .config_port    = cpm_uart_config_port,
884         .verify_port    = cpm_uart_verify_port,
885 };
886
887 struct uart_cpm_port cpm_uart_ports[UART_NR];
888
889 static int cpm_uart_init_port(struct device_node *np,
890                               struct uart_cpm_port *pinfo)
891 {
892         const u32 *data;
893         void __iomem *mem, *pram;
894         int len;
895         int ret;
896
897         data = of_get_property(np, "fsl,cpm-brg", &len);
898         if (!data || len != 4) {
899                 printk(KERN_ERR "CPM UART %s has no/invalid "
900                                 "fsl,cpm-brg property.\n", np->name);
901                 return -EINVAL;
902         }
903         pinfo->brg = *data;
904
905         data = of_get_property(np, "fsl,cpm-command", &len);
906         if (!data || len != 4) {
907                 printk(KERN_ERR "CPM UART %s has no/invalid "
908                                 "fsl,cpm-command property.\n", np->name);
909                 return -EINVAL;
910         }
911         pinfo->command = *data;
912
913         mem = of_iomap(np, 0);
914         if (!mem)
915                 return -ENOMEM;
916
917         if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
918             of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
919                 pinfo->sccp = mem;
920                 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
921         } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
922                    of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
923                 pinfo->flags |= FLAG_SMC;
924                 pinfo->smcp = mem;
925                 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
926         } else {
927                 ret = -ENODEV;
928                 goto out_mem;
929         }
930
931         if (!pram) {
932                 ret = -ENOMEM;
933                 goto out_mem;
934         }
935
936         pinfo->tx_nrfifos = TX_NUM_FIFO;
937         pinfo->tx_fifosize = TX_BUF_SIZE;
938         pinfo->rx_nrfifos = RX_NUM_FIFO;
939         pinfo->rx_fifosize = RX_BUF_SIZE;
940
941         pinfo->port.uartclk = ppc_proc_freq;
942         pinfo->port.mapbase = (unsigned long)mem;
943         pinfo->port.type = PORT_CPM;
944         pinfo->port.ops = &cpm_uart_pops,
945         pinfo->port.iotype = UPIO_MEM;
946         pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
947         spin_lock_init(&pinfo->port.lock);
948
949         pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
950         if (pinfo->port.irq == NO_IRQ) {
951                 ret = -EINVAL;
952                 goto out_pram;
953         }
954
955         return cpm_uart_request_port(&pinfo->port);
956
957 out_pram:
958         cpm_uart_unmap_pram(pinfo, pram);
959 out_mem:
960         iounmap(mem);
961         return ret;
962 }
963
964 #ifdef CONFIG_SERIAL_CPM_CONSOLE
965 /*
966  *      Print a string to the serial port trying not to disturb
967  *      any possible real use of the port...
968  *
969  *      Note that this is called with interrupts already disabled
970  */
971 static void cpm_uart_console_write(struct console *co, const char *s,
972                                    u_int count)
973 {
974         struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
975         unsigned int i;
976         cbd_t __iomem *bdp, *bdbase;
977         unsigned char *cp;
978         unsigned long flags;
979         int nolock = oops_in_progress;
980
981         if (unlikely(nolock)) {
982                 local_irq_save(flags);
983         } else {
984                 spin_lock_irqsave(&pinfo->port.lock, flags);
985         }
986
987         /* Get the address of the host memory buffer.
988          */
989         bdp = pinfo->tx_cur;
990         bdbase = pinfo->tx_bd_base;
991
992         /*
993          * Now, do each character.  This is not as bad as it looks
994          * since this is a holding FIFO and not a transmitting FIFO.
995          * We could add the complexity of filling the entire transmit
996          * buffer, but we would just wait longer between accesses......
997          */
998         for (i = 0; i < count; i++, s++) {
999                 /* Wait for transmitter fifo to empty.
1000                  * Ready indicates output is ready, and xmt is doing
1001                  * that, not that it is ready for us to send.
1002                  */
1003                 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1004                         ;
1005
1006                 /* Send the character out.
1007                  * If the buffer address is in the CPM DPRAM, don't
1008                  * convert it.
1009                  */
1010                 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1011                 *cp = *s;
1012
1013                 out_be16(&bdp->cbd_datlen, 1);
1014                 setbits16(&bdp->cbd_sc, BD_SC_READY);
1015
1016                 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1017                         bdp = bdbase;
1018                 else
1019                         bdp++;
1020
1021                 /* if a LF, also do CR... */
1022                 if (*s == 10) {
1023                         while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1024                                 ;
1025
1026                         cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1027                         *cp = 13;
1028
1029                         out_be16(&bdp->cbd_datlen, 1);
1030                         setbits16(&bdp->cbd_sc, BD_SC_READY);
1031
1032                         if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1033                                 bdp = bdbase;
1034                         else
1035                                 bdp++;
1036                 }
1037         }
1038
1039         /*
1040          * Finally, Wait for transmitter & holding register to empty
1041          *  and restore the IER
1042          */
1043         while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1044                 ;
1045
1046         pinfo->tx_cur = bdp;
1047
1048         if (unlikely(nolock)) {
1049                 local_irq_restore(flags);
1050         } else {
1051                 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1052         }
1053 }
1054
1055
1056 static int __init cpm_uart_console_setup(struct console *co, char *options)
1057 {
1058         int baud = 38400;
1059         int bits = 8;
1060         int parity = 'n';
1061         int flow = 'n';
1062         int ret;
1063         struct uart_cpm_port *pinfo;
1064         struct uart_port *port;
1065
1066         struct device_node *np = NULL;
1067         int i = 0;
1068
1069         if (co->index >= UART_NR) {
1070                 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1071                        co->index);
1072                 return -ENODEV;
1073         }
1074
1075         do {
1076                 np = of_find_node_by_type(np, "serial");
1077                 if (!np)
1078                         return -ENODEV;
1079
1080                 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1081                     !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1082                     !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1083                     !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1084                         i--;
1085         } while (i++ != co->index);
1086
1087         pinfo = &cpm_uart_ports[co->index];
1088
1089         pinfo->flags |= FLAG_CONSOLE;
1090         port = &pinfo->port;
1091
1092         ret = cpm_uart_init_port(np, pinfo);
1093         of_node_put(np);
1094         if (ret)
1095                 return ret;
1096
1097         if (options) {
1098                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1099         } else {
1100                 if ((baud = uart_baudrate()) == -1)
1101                         baud = 9600;
1102         }
1103
1104 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1105         udbg_putc = NULL;
1106 #endif
1107
1108         cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1109
1110         if (IS_SMC(pinfo)) {
1111                 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1112                 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1113         } else {
1114                 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1115                 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1116         }
1117
1118         ret = cpm_uart_allocbuf(pinfo, 1);
1119
1120         if (ret)
1121                 return ret;
1122
1123         cpm_uart_initbd(pinfo);
1124
1125         if (IS_SMC(pinfo))
1126                 cpm_uart_init_smc(pinfo);
1127         else
1128                 cpm_uart_init_scc(pinfo);
1129
1130         uart_set_options(port, co, baud, parity, bits, flow);
1131         cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1132
1133         return 0;
1134 }
1135
1136 static struct uart_driver cpm_reg;
1137 static struct console cpm_scc_uart_console = {
1138         .name           = "ttyCPM",
1139         .write          = cpm_uart_console_write,
1140         .device         = uart_console_device,
1141         .setup          = cpm_uart_console_setup,
1142         .flags          = CON_PRINTBUFFER,
1143         .index          = -1,
1144         .data           = &cpm_reg,
1145 };
1146
1147 static int __init cpm_uart_console_init(void)
1148 {
1149         register_console(&cpm_scc_uart_console);
1150         return 0;
1151 }
1152
1153 console_initcall(cpm_uart_console_init);
1154
1155 #define CPM_UART_CONSOLE        &cpm_scc_uart_console
1156 #else
1157 #define CPM_UART_CONSOLE        NULL
1158 #endif
1159
1160 static struct uart_driver cpm_reg = {
1161         .owner          = THIS_MODULE,
1162         .driver_name    = "ttyCPM",
1163         .dev_name       = "ttyCPM",
1164         .major          = SERIAL_CPM_MAJOR,
1165         .minor          = SERIAL_CPM_MINOR,
1166         .cons           = CPM_UART_CONSOLE,
1167         .nr             = UART_NR,
1168 };
1169
1170 static int probe_index;
1171
1172 static int __devinit cpm_uart_probe(struct of_device *ofdev,
1173                                     const struct of_device_id *match)
1174 {
1175         int index = probe_index++;
1176         struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1177         int ret;
1178
1179         pinfo->port.line = index;
1180
1181         if (index >= UART_NR)
1182                 return -ENODEV;
1183
1184         dev_set_drvdata(&ofdev->dev, pinfo);
1185
1186         ret = cpm_uart_init_port(ofdev->node, pinfo);
1187         if (ret)
1188                 return ret;
1189
1190         return uart_add_one_port(&cpm_reg, &pinfo->port);
1191 }
1192
1193 static int __devexit cpm_uart_remove(struct of_device *ofdev)
1194 {
1195         struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1196         return uart_remove_one_port(&cpm_reg, &pinfo->port);
1197 }
1198
1199 static struct of_device_id cpm_uart_match[] = {
1200         {
1201                 .compatible = "fsl,cpm1-smc-uart",
1202         },
1203         {
1204                 .compatible = "fsl,cpm1-scc-uart",
1205         },
1206         {
1207                 .compatible = "fsl,cpm2-smc-uart",
1208         },
1209         {
1210                 .compatible = "fsl,cpm2-scc-uart",
1211         },
1212         {}
1213 };
1214
1215 static struct of_platform_driver cpm_uart_driver = {
1216         .name = "cpm_uart",
1217         .match_table = cpm_uart_match,
1218         .probe = cpm_uart_probe,
1219         .remove = cpm_uart_remove,
1220  };
1221
1222 static int __init cpm_uart_init(void)
1223 {
1224         int ret = uart_register_driver(&cpm_reg);
1225         if (ret)
1226                 return ret;
1227
1228         ret = of_register_platform_driver(&cpm_uart_driver);
1229         if (ret)
1230                 uart_unregister_driver(&cpm_reg);
1231
1232         return ret;
1233 }
1234
1235 static void __exit cpm_uart_exit(void)
1236 {
1237         of_unregister_platform_driver(&cpm_uart_driver);
1238         uart_unregister_driver(&cpm_reg);
1239 }
1240
1241 module_init(cpm_uart_init);
1242 module_exit(cpm_uart_exit);
1243
1244 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1245 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1246 MODULE_LICENSE("GPL");
1247 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);