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