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