[POWERPC] cpm_uart: Be an of_platform device when CONFIG_PPC_CPM_NEW_BINDING is set.
[linux-2.6/linux-2.6-openrd.git] / drivers / serial / cpm_uart / cpm_uart_core.c
blob8564ba2f3ec2c31c701808b5a4d8d24868ea23c2
1 /*
2 * linux/drivers/serial/cpm_uart.c
4 * Driver for CPM (SCC/SMC) serial ports; core driver
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
10 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
11 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
13 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
14 * (C) 2004 Intracom, S.A.
15 * (C) 2005-2006 MontaVista Software, Inc.
16 * Vitaly Bordug <vbordug@ru.mvista.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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>
46 #include <asm/io.h>
47 #include <asm/irq.h>
48 #include <asm/delay.h>
49 #include <asm/fs_pd.h>
50 #include <asm/udbg.h>
52 #ifdef CONFIG_PPC_CPM_NEW_BINDING
53 #include <linux/of_platform.h>
54 #endif
56 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57 #define SUPPORT_SYSRQ
58 #endif
60 #include <linux/serial_core.h>
61 #include <linux/kernel.h>
63 #include "cpm_uart.h"
66 /**************************************************************/
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);
73 /**************************************************************/
75 #ifndef CONFIG_PPC_CPM_NEW_BINDING
76 /* Track which ports are configured as uarts */
77 int cpm_uart_port_map[UART_NR];
78 /* How many ports did we config as uarts */
79 int cpm_uart_nr;
81 /* Place-holder for board-specific stuff */
82 struct platform_device* __attribute__ ((weak)) __init
83 early_uart_get_pdev(int index)
85 return NULL;
89 static void cpm_uart_count(void)
91 cpm_uart_nr = 0;
92 #ifdef CONFIG_SERIAL_CPM_SMC1
93 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
94 #endif
95 #ifdef CONFIG_SERIAL_CPM_SMC2
96 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
97 #endif
98 #ifdef CONFIG_SERIAL_CPM_SCC1
99 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
100 #endif
101 #ifdef CONFIG_SERIAL_CPM_SCC2
102 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
103 #endif
104 #ifdef CONFIG_SERIAL_CPM_SCC3
105 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
106 #endif
107 #ifdef CONFIG_SERIAL_CPM_SCC4
108 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
109 #endif
112 /* Get UART number by its id */
113 static int cpm_uart_id2nr(int id)
115 int i;
116 if (id < UART_NR) {
117 for (i=0; i<UART_NR; i++) {
118 if (cpm_uart_port_map[i] == id)
119 return i;
123 /* not found or invalid argument */
124 return -1;
126 #endif
129 * Check, if transmit buffers are processed
131 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
133 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
134 volatile cbd_t *bdp = pinfo->tx_bd_base;
135 int ret = 0;
137 while (1) {
138 if (bdp->cbd_sc & BD_SC_READY)
139 break;
141 if (bdp->cbd_sc & BD_SC_WRAP) {
142 ret = TIOCSER_TEMT;
143 break;
145 bdp++;
148 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
150 return ret;
153 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
155 /* Whee. Do nothing. */
158 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
160 /* Whee. Do nothing. */
161 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
165 * Stop transmitter
167 static void cpm_uart_stop_tx(struct uart_port *port)
169 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
170 volatile smc_t *smcp = pinfo->smcp;
171 volatile scc_t *sccp = pinfo->sccp;
173 pr_debug("CPM uart[%d]:stop tx\n", port->line);
175 if (IS_SMC(pinfo))
176 smcp->smc_smcm &= ~SMCM_TX;
177 else
178 sccp->scc_sccm &= ~UART_SCCM_TX;
182 * Start transmitter
184 static void cpm_uart_start_tx(struct uart_port *port)
186 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
187 volatile smc_t *smcp = pinfo->smcp;
188 volatile scc_t *sccp = pinfo->sccp;
190 pr_debug("CPM uart[%d]:start tx\n", port->line);
192 if (IS_SMC(pinfo)) {
193 if (smcp->smc_smcm & SMCM_TX)
194 return;
195 } else {
196 if (sccp->scc_sccm & UART_SCCM_TX)
197 return;
200 if (cpm_uart_tx_pump(port) != 0) {
201 if (IS_SMC(pinfo)) {
202 smcp->smc_smcm |= SMCM_TX;
203 } else {
204 sccp->scc_sccm |= UART_SCCM_TX;
210 * Stop receiver
212 static void cpm_uart_stop_rx(struct uart_port *port)
214 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
215 volatile smc_t *smcp = pinfo->smcp;
216 volatile scc_t *sccp = pinfo->sccp;
218 pr_debug("CPM uart[%d]:stop rx\n", port->line);
220 if (IS_SMC(pinfo))
221 smcp->smc_smcm &= ~SMCM_RX;
222 else
223 sccp->scc_sccm &= ~UART_SCCM_RX;
227 * Enable Modem status interrupts
229 static void cpm_uart_enable_ms(struct uart_port *port)
231 pr_debug("CPM uart[%d]:enable ms\n", port->line);
235 * Generate a break.
237 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
239 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
241 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
242 break_state);
244 if (break_state)
245 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
246 else
247 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
251 * Transmit characters, refill buffer descriptor, if possible
253 static void cpm_uart_int_tx(struct uart_port *port)
255 pr_debug("CPM uart[%d]:TX INT\n", port->line);
257 cpm_uart_tx_pump(port);
261 * Receive characters
263 static void cpm_uart_int_rx(struct uart_port *port)
265 int i;
266 unsigned char ch, *cp;
267 struct tty_struct *tty = port->info->tty;
268 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
269 volatile cbd_t *bdp;
270 u16 status;
271 unsigned int flg;
273 pr_debug("CPM uart[%d]:RX INT\n", port->line);
275 /* Just loop through the closed BDs and copy the characters into
276 * the buffer.
278 bdp = pinfo->rx_cur;
279 for (;;) {
280 /* get status */
281 status = bdp->cbd_sc;
282 /* If this one is empty, return happy */
283 if (status & BD_SC_EMPTY)
284 break;
286 /* get number of characters, and check spce in flip-buffer */
287 i = bdp->cbd_datlen;
289 /* If we have not enough room in tty flip buffer, then we try
290 * later, which will be the next rx-interrupt or a timeout
292 if(tty_buffer_request_room(tty, i) < i) {
293 printk(KERN_WARNING "No room in flip buffer\n");
294 return;
297 /* get pointer */
298 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
300 /* loop through the buffer */
301 while (i-- > 0) {
302 ch = *cp++;
303 port->icount.rx++;
304 flg = TTY_NORMAL;
306 if (status &
307 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
308 goto handle_error;
309 if (uart_handle_sysrq_char(port, ch))
310 continue;
312 error_return:
313 tty_insert_flip_char(tty, ch, flg);
315 } /* End while (i--) */
317 /* This BD is ready to be used again. Clear status. get next */
318 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
319 bdp->cbd_sc |= BD_SC_EMPTY;
321 if (bdp->cbd_sc & BD_SC_WRAP)
322 bdp = pinfo->rx_bd_base;
323 else
324 bdp++;
326 } /* End for (;;) */
328 /* Write back buffer pointer */
329 pinfo->rx_cur = (volatile cbd_t *) bdp;
331 /* activate BH processing */
332 tty_flip_buffer_push(tty);
334 return;
336 /* Error processing */
338 handle_error:
339 /* Statistics */
340 if (status & BD_SC_BR)
341 port->icount.brk++;
342 if (status & BD_SC_PR)
343 port->icount.parity++;
344 if (status & BD_SC_FR)
345 port->icount.frame++;
346 if (status & BD_SC_OV)
347 port->icount.overrun++;
349 /* Mask out ignored conditions */
350 status &= port->read_status_mask;
352 /* Handle the remaining ones */
353 if (status & BD_SC_BR)
354 flg = TTY_BREAK;
355 else if (status & BD_SC_PR)
356 flg = TTY_PARITY;
357 else if (status & BD_SC_FR)
358 flg = TTY_FRAME;
360 /* overrun does not affect the current character ! */
361 if (status & BD_SC_OV) {
362 ch = 0;
363 flg = TTY_OVERRUN;
364 /* We skip this buffer */
365 /* CHECK: Is really nothing senseful there */
366 /* ASSUMPTION: it contains nothing valid */
367 i = 0;
369 #ifdef SUPPORT_SYSRQ
370 port->sysrq = 0;
371 #endif
372 goto error_return;
376 * Asynchron mode interrupt handler
378 static irqreturn_t cpm_uart_int(int irq, void *data)
380 u8 events;
381 struct uart_port *port = (struct uart_port *)data;
382 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
383 volatile smc_t *smcp = pinfo->smcp;
384 volatile scc_t *sccp = pinfo->sccp;
386 pr_debug("CPM uart[%d]:IRQ\n", port->line);
388 if (IS_SMC(pinfo)) {
389 events = smcp->smc_smce;
390 smcp->smc_smce = events;
391 if (events & SMCM_BRKE)
392 uart_handle_break(port);
393 if (events & SMCM_RX)
394 cpm_uart_int_rx(port);
395 if (events & SMCM_TX)
396 cpm_uart_int_tx(port);
397 } else {
398 events = sccp->scc_scce;
399 sccp->scc_scce = events;
400 if (events & UART_SCCM_BRKE)
401 uart_handle_break(port);
402 if (events & UART_SCCM_RX)
403 cpm_uart_int_rx(port);
404 if (events & UART_SCCM_TX)
405 cpm_uart_int_tx(port);
407 return (events) ? IRQ_HANDLED : IRQ_NONE;
410 static int cpm_uart_startup(struct uart_port *port)
412 int retval;
413 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
415 pr_debug("CPM uart[%d]:startup\n", port->line);
417 /* Install interrupt handler. */
418 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
419 if (retval)
420 return retval;
422 /* Startup rx-int */
423 if (IS_SMC(pinfo)) {
424 pinfo->smcp->smc_smcm |= SMCM_RX;
425 pinfo->smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
426 } else {
427 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
428 pinfo->sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
431 if (!(pinfo->flags & FLAG_CONSOLE))
432 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
433 return 0;
436 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
438 set_current_state(TASK_UNINTERRUPTIBLE);
439 schedule_timeout(pinfo->wait_closing);
443 * Shutdown the uart
445 static void cpm_uart_shutdown(struct uart_port *port)
447 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
449 pr_debug("CPM uart[%d]:shutdown\n", port->line);
451 /* free interrupt handler */
452 free_irq(port->irq, port);
454 /* If the port is not the console, disable Rx and Tx. */
455 if (!(pinfo->flags & FLAG_CONSOLE)) {
456 /* Wait for all the BDs marked sent */
457 while(!cpm_uart_tx_empty(port)) {
458 set_current_state(TASK_UNINTERRUPTIBLE);
459 schedule_timeout(2);
462 if (pinfo->wait_closing)
463 cpm_uart_wait_until_send(pinfo);
465 /* Stop uarts */
466 if (IS_SMC(pinfo)) {
467 volatile smc_t *smcp = pinfo->smcp;
468 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
469 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
470 } else {
471 volatile scc_t *sccp = pinfo->sccp;
472 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
473 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
476 /* Shut them really down and reinit buffer descriptors */
477 if (IS_SMC(pinfo))
478 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
479 else
480 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
482 cpm_uart_initbd(pinfo);
486 static void cpm_uart_set_termios(struct uart_port *port,
487 struct ktermios *termios,
488 struct ktermios *old)
490 int baud;
491 unsigned long flags;
492 u16 cval, scval, prev_mode;
493 int bits, sbits;
494 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
495 volatile smc_t *smcp = pinfo->smcp;
496 volatile scc_t *sccp = pinfo->sccp;
498 pr_debug("CPM uart[%d]:set_termios\n", port->line);
500 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
502 /* Character length programmed into the mode register is the
503 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
504 * 1 or 2 stop bits, minus 1.
505 * The value 'bits' counts this for us.
507 cval = 0;
508 scval = 0;
510 /* byte size */
511 switch (termios->c_cflag & CSIZE) {
512 case CS5:
513 bits = 5;
514 break;
515 case CS6:
516 bits = 6;
517 break;
518 case CS7:
519 bits = 7;
520 break;
521 case CS8:
522 bits = 8;
523 break;
524 /* Never happens, but GCC is too dumb to figure it out */
525 default:
526 bits = 8;
527 break;
529 sbits = bits - 5;
531 if (termios->c_cflag & CSTOPB) {
532 cval |= SMCMR_SL; /* Two stops */
533 scval |= SCU_PSMR_SL;
534 bits++;
537 if (termios->c_cflag & PARENB) {
538 cval |= SMCMR_PEN;
539 scval |= SCU_PSMR_PEN;
540 bits++;
541 if (!(termios->c_cflag & PARODD)) {
542 cval |= SMCMR_PM_EVEN;
543 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
548 * Set up parity check flag
550 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
552 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
553 if (termios->c_iflag & INPCK)
554 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
555 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
556 port->read_status_mask |= BD_SC_BR;
559 * Characters to ignore
561 port->ignore_status_mask = 0;
562 if (termios->c_iflag & IGNPAR)
563 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
564 if (termios->c_iflag & IGNBRK) {
565 port->ignore_status_mask |= BD_SC_BR;
567 * If we're ignore parity and break indicators, ignore
568 * overruns too. (For real raw support).
570 if (termios->c_iflag & IGNPAR)
571 port->ignore_status_mask |= BD_SC_OV;
574 * !!! ignore all characters if CREAD is not set
576 if ((termios->c_cflag & CREAD) == 0)
577 port->read_status_mask &= ~BD_SC_EMPTY;
579 spin_lock_irqsave(&port->lock, flags);
581 /* Start bit has not been added (so don't, because we would just
582 * subtract it later), and we need to add one for the number of
583 * stops bits (there is always at least one).
585 bits++;
586 if (IS_SMC(pinfo)) {
587 /* Set the mode register. We want to keep a copy of the
588 * enables, because we want to put them back if they were
589 * present.
591 prev_mode = smcp->smc_smcmr;
592 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
593 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
594 } else {
595 sccp->scc_psmr = (sbits << 12) | scval;
598 cpm_set_brg(pinfo->brg - 1, baud);
599 spin_unlock_irqrestore(&port->lock, flags);
602 static const char *cpm_uart_type(struct uart_port *port)
604 pr_debug("CPM uart[%d]:uart_type\n", port->line);
606 return port->type == PORT_CPM ? "CPM UART" : NULL;
610 * verify the new serial_struct (for TIOCSSERIAL).
612 static int cpm_uart_verify_port(struct uart_port *port,
613 struct serial_struct *ser)
615 int ret = 0;
617 pr_debug("CPM uart[%d]:verify_port\n", port->line);
619 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
620 ret = -EINVAL;
621 if (ser->irq < 0 || ser->irq >= NR_IRQS)
622 ret = -EINVAL;
623 if (ser->baud_base < 9600)
624 ret = -EINVAL;
625 return ret;
629 * Transmit characters, refill buffer descriptor, if possible
631 static int cpm_uart_tx_pump(struct uart_port *port)
633 volatile cbd_t *bdp;
634 unsigned char *p;
635 int count;
636 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
637 struct circ_buf *xmit = &port->info->xmit;
639 /* Handle xon/xoff */
640 if (port->x_char) {
641 /* Pick next descriptor and fill from buffer */
642 bdp = pinfo->tx_cur;
644 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
646 *p++ = port->x_char;
647 bdp->cbd_datlen = 1;
648 bdp->cbd_sc |= BD_SC_READY;
649 /* Get next BD. */
650 if (bdp->cbd_sc & BD_SC_WRAP)
651 bdp = pinfo->tx_bd_base;
652 else
653 bdp++;
654 pinfo->tx_cur = bdp;
656 port->icount.tx++;
657 port->x_char = 0;
658 return 1;
661 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
662 cpm_uart_stop_tx(port);
663 return 0;
666 /* Pick next descriptor and fill from buffer */
667 bdp = pinfo->tx_cur;
669 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
670 count = 0;
671 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
672 while (count < pinfo->tx_fifosize) {
673 *p++ = xmit->buf[xmit->tail];
674 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
675 port->icount.tx++;
676 count++;
677 if (xmit->head == xmit->tail)
678 break;
680 bdp->cbd_datlen = count;
681 bdp->cbd_sc |= BD_SC_READY;
682 eieio();
683 /* Get next BD. */
684 if (bdp->cbd_sc & BD_SC_WRAP)
685 bdp = pinfo->tx_bd_base;
686 else
687 bdp++;
689 pinfo->tx_cur = bdp;
691 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
692 uart_write_wakeup(port);
694 if (uart_circ_empty(xmit)) {
695 cpm_uart_stop_tx(port);
696 return 0;
699 return 1;
703 * init buffer descriptors
705 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
707 int i;
708 u8 *mem_addr;
709 volatile cbd_t *bdp;
711 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
713 /* Set the physical address of the host memory
714 * buffers in the buffer descriptors, and the
715 * virtual address for us to work with.
717 mem_addr = pinfo->mem_addr;
718 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
719 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
720 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
721 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
722 mem_addr += pinfo->rx_fifosize;
725 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
726 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
728 /* Set the physical address of the host memory
729 * buffers in the buffer descriptors, and the
730 * virtual address for us to work with.
732 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
733 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
734 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
735 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
736 bdp->cbd_sc = BD_SC_INTRPT;
737 mem_addr += pinfo->tx_fifosize;
740 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
741 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
744 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
746 volatile scc_t *scp;
747 volatile scc_uart_t *sup;
749 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
751 scp = pinfo->sccp;
752 sup = pinfo->sccup;
754 /* Store address */
755 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
756 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
758 /* Set up the uart parameters in the
759 * parameter ram.
762 cpm_set_scc_fcr(sup);
764 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
765 sup->scc_maxidl = pinfo->rx_fifosize;
766 sup->scc_brkcr = 1;
767 sup->scc_parec = 0;
768 sup->scc_frmec = 0;
769 sup->scc_nosec = 0;
770 sup->scc_brkec = 0;
771 sup->scc_uaddr1 = 0;
772 sup->scc_uaddr2 = 0;
773 sup->scc_toseq = 0;
774 sup->scc_char1 = 0x8000;
775 sup->scc_char2 = 0x8000;
776 sup->scc_char3 = 0x8000;
777 sup->scc_char4 = 0x8000;
778 sup->scc_char5 = 0x8000;
779 sup->scc_char6 = 0x8000;
780 sup->scc_char7 = 0x8000;
781 sup->scc_char8 = 0x8000;
782 sup->scc_rccm = 0xc0ff;
784 /* Send the CPM an initialize command.
786 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
788 /* Set UART mode, 8 bit, no parity, one stop.
789 * Enable receive and transmit.
791 scp->scc_gsmrh = 0;
792 scp->scc_gsmrl =
793 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
795 /* Enable rx interrupts and clear all pending events. */
796 scp->scc_sccm = 0;
797 scp->scc_scce = 0xffff;
798 scp->scc_dsr = 0x7e7e;
799 scp->scc_psmr = 0x3000;
801 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
804 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
806 volatile smc_t *sp;
807 volatile smc_uart_t *up;
809 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
811 sp = pinfo->smcp;
812 up = pinfo->smcup;
814 /* Store address */
815 pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
816 pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
819 * In case SMC1 is being relocated...
821 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
822 up->smc_rbptr = pinfo->smcup->smc_rbase;
823 up->smc_tbptr = pinfo->smcup->smc_tbase;
824 up->smc_rstate = 0;
825 up->smc_tstate = 0;
826 up->smc_brkcr = 1; /* number of break chars */
827 up->smc_brkec = 0;
828 #endif
830 /* Set up the uart parameters in the
831 * parameter ram.
833 cpm_set_smc_fcr(up);
835 /* Using idle charater time requires some additional tuning. */
836 up->smc_mrblr = pinfo->rx_fifosize;
837 up->smc_maxidl = pinfo->rx_fifosize;
838 up->smc_brklen = 0;
839 up->smc_brkec = 0;
840 up->smc_brkcr = 1;
842 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
844 /* Set UART mode, 8 bit, no parity, one stop.
845 * Enable receive and transmit.
847 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
849 /* Enable only rx interrupts clear all pending events. */
850 sp->smc_smcm = 0;
851 sp->smc_smce = 0xff;
853 sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
857 * Initialize port. This is called from early_console stuff
858 * so we have to be careful here !
860 static int cpm_uart_request_port(struct uart_port *port)
862 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
863 int ret;
865 pr_debug("CPM uart[%d]:request port\n", port->line);
867 if (pinfo->flags & FLAG_CONSOLE)
868 return 0;
870 if (IS_SMC(pinfo)) {
871 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
872 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
873 } else {
874 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
875 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
878 ret = cpm_uart_allocbuf(pinfo, 0);
880 if (ret)
881 return ret;
883 cpm_uart_initbd(pinfo);
884 if (IS_SMC(pinfo))
885 cpm_uart_init_smc(pinfo);
886 else
887 cpm_uart_init_scc(pinfo);
889 return 0;
892 static void cpm_uart_release_port(struct uart_port *port)
894 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
896 if (!(pinfo->flags & FLAG_CONSOLE))
897 cpm_uart_freebuf(pinfo);
901 * Configure/autoconfigure the port.
903 static void cpm_uart_config_port(struct uart_port *port, int flags)
905 pr_debug("CPM uart[%d]:config_port\n", port->line);
907 if (flags & UART_CONFIG_TYPE) {
908 port->type = PORT_CPM;
909 cpm_uart_request_port(port);
912 static struct uart_ops cpm_uart_pops = {
913 .tx_empty = cpm_uart_tx_empty,
914 .set_mctrl = cpm_uart_set_mctrl,
915 .get_mctrl = cpm_uart_get_mctrl,
916 .stop_tx = cpm_uart_stop_tx,
917 .start_tx = cpm_uart_start_tx,
918 .stop_rx = cpm_uart_stop_rx,
919 .enable_ms = cpm_uart_enable_ms,
920 .break_ctl = cpm_uart_break_ctl,
921 .startup = cpm_uart_startup,
922 .shutdown = cpm_uart_shutdown,
923 .set_termios = cpm_uart_set_termios,
924 .type = cpm_uart_type,
925 .release_port = cpm_uart_release_port,
926 .request_port = cpm_uart_request_port,
927 .config_port = cpm_uart_config_port,
928 .verify_port = cpm_uart_verify_port,
931 #ifdef CONFIG_PPC_CPM_NEW_BINDING
932 struct uart_cpm_port cpm_uart_ports[UART_NR];
934 int cpm_uart_init_port(struct device_node *np, struct uart_cpm_port *pinfo)
936 const u32 *data;
937 void __iomem *mem, __iomem *pram;
938 int len;
939 int ret;
941 data = of_get_property(np, "fsl,cpm-brg", &len);
942 if (!data || len != 4) {
943 printk(KERN_ERR "CPM UART %s has no/invalid "
944 "fsl,cpm-brg property.\n", np->name);
945 return -EINVAL;
947 pinfo->brg = *data;
949 data = of_get_property(np, "fsl,cpm-command", &len);
950 if (!data || len != 4) {
951 printk(KERN_ERR "CPM UART %s has no/invalid "
952 "fsl,cpm-command property.\n", np->name);
953 return -EINVAL;
955 pinfo->command = *data;
957 mem = of_iomap(np, 0);
958 if (!mem)
959 return -ENOMEM;
961 pram = of_iomap(np, 1);
962 if (!pram) {
963 ret = -ENOMEM;
964 goto out_mem;
967 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
968 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
969 pinfo->sccp = mem;
970 pinfo->sccup = pram;
971 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
972 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
973 pinfo->flags |= FLAG_SMC;
974 pinfo->smcp = mem;
975 pinfo->smcup = pram;
976 } else {
977 ret = -ENODEV;
978 goto out_pram;
981 pinfo->tx_nrfifos = TX_NUM_FIFO;
982 pinfo->tx_fifosize = TX_BUF_SIZE;
983 pinfo->rx_nrfifos = RX_NUM_FIFO;
984 pinfo->rx_fifosize = RX_BUF_SIZE;
986 pinfo->port.uartclk = ppc_proc_freq;
987 pinfo->port.mapbase = (unsigned long)mem;
988 pinfo->port.type = PORT_CPM;
989 pinfo->port.ops = &cpm_uart_pops,
990 pinfo->port.iotype = UPIO_MEM;
991 spin_lock_init(&pinfo->port.lock);
993 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
994 if (pinfo->port.irq == NO_IRQ) {
995 ret = -EINVAL;
996 goto out_pram;
999 return cpm_uart_request_port(&pinfo->port);
1001 out_pram:
1002 iounmap(pram);
1003 out_mem:
1004 iounmap(mem);
1005 return ret;
1008 #else
1010 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
1011 [UART_SMC1] = {
1012 .port = {
1013 .irq = SMC1_IRQ,
1014 .ops = &cpm_uart_pops,
1015 .iotype = UPIO_MEM,
1016 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SMC1].port.lock),
1018 .flags = FLAG_SMC,
1019 .tx_nrfifos = TX_NUM_FIFO,
1020 .tx_fifosize = TX_BUF_SIZE,
1021 .rx_nrfifos = RX_NUM_FIFO,
1022 .rx_fifosize = RX_BUF_SIZE,
1023 .set_lineif = smc1_lineif,
1025 [UART_SMC2] = {
1026 .port = {
1027 .irq = SMC2_IRQ,
1028 .ops = &cpm_uart_pops,
1029 .iotype = UPIO_MEM,
1030 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SMC2].port.lock),
1032 .flags = FLAG_SMC,
1033 .tx_nrfifos = TX_NUM_FIFO,
1034 .tx_fifosize = TX_BUF_SIZE,
1035 .rx_nrfifos = RX_NUM_FIFO,
1036 .rx_fifosize = RX_BUF_SIZE,
1037 .set_lineif = smc2_lineif,
1038 #ifdef CONFIG_SERIAL_CPM_ALT_SMC2
1039 .is_portb = 1,
1040 #endif
1042 [UART_SCC1] = {
1043 .port = {
1044 .irq = SCC1_IRQ,
1045 .ops = &cpm_uart_pops,
1046 .iotype = UPIO_MEM,
1047 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SCC1].port.lock),
1049 .tx_nrfifos = TX_NUM_FIFO,
1050 .tx_fifosize = TX_BUF_SIZE,
1051 .rx_nrfifos = RX_NUM_FIFO,
1052 .rx_fifosize = RX_BUF_SIZE,
1053 .set_lineif = scc1_lineif,
1054 .wait_closing = SCC_WAIT_CLOSING,
1056 [UART_SCC2] = {
1057 .port = {
1058 .irq = SCC2_IRQ,
1059 .ops = &cpm_uart_pops,
1060 .iotype = UPIO_MEM,
1061 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SCC2].port.lock),
1063 .tx_nrfifos = TX_NUM_FIFO,
1064 .tx_fifosize = TX_BUF_SIZE,
1065 .rx_nrfifos = RX_NUM_FIFO,
1066 .rx_fifosize = RX_BUF_SIZE,
1067 .set_lineif = scc2_lineif,
1068 .wait_closing = SCC_WAIT_CLOSING,
1070 [UART_SCC3] = {
1071 .port = {
1072 .irq = SCC3_IRQ,
1073 .ops = &cpm_uart_pops,
1074 .iotype = UPIO_MEM,
1075 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SCC3].port.lock),
1077 .tx_nrfifos = TX_NUM_FIFO,
1078 .tx_fifosize = TX_BUF_SIZE,
1079 .rx_nrfifos = RX_NUM_FIFO,
1080 .rx_fifosize = RX_BUF_SIZE,
1081 .set_lineif = scc3_lineif,
1082 .wait_closing = SCC_WAIT_CLOSING,
1084 [UART_SCC4] = {
1085 .port = {
1086 .irq = SCC4_IRQ,
1087 .ops = &cpm_uart_pops,
1088 .iotype = UPIO_MEM,
1089 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SCC4].port.lock),
1091 .tx_nrfifos = TX_NUM_FIFO,
1092 .tx_fifosize = TX_BUF_SIZE,
1093 .rx_nrfifos = RX_NUM_FIFO,
1094 .rx_fifosize = RX_BUF_SIZE,
1095 .set_lineif = scc4_lineif,
1096 .wait_closing = SCC_WAIT_CLOSING,
1100 int cpm_uart_drv_get_platform_data(struct platform_device *pdev, int is_con)
1102 struct resource *r;
1103 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1104 int idx; /* It is UART_SMCx or UART_SCCx index */
1105 struct uart_cpm_port *pinfo;
1106 int line;
1107 u32 mem, pram;
1109 idx = pdata->fs_no = fs_uart_get_id(pdata);
1111 line = cpm_uart_id2nr(idx);
1112 if(line < 0) {
1113 printk(KERN_ERR"%s(): port %d is not registered", __FUNCTION__, idx);
1114 return -EINVAL;
1117 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1119 pinfo->brg = pdata->brg;
1121 if (!is_con) {
1122 pinfo->port.line = line;
1123 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1126 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1127 return -EINVAL;
1128 mem = (u32)ioremap(r->start, r->end - r->start + 1);
1130 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
1131 return -EINVAL;
1132 pram = (u32)ioremap(r->start, r->end - r->start + 1);
1134 if(idx > fsid_smc2_uart) {
1135 pinfo->sccp = (scc_t *)mem;
1136 pinfo->sccup = (scc_uart_t *)pram;
1137 } else {
1138 pinfo->smcp = (smc_t *)mem;
1139 pinfo->smcup = (smc_uart_t *)pram;
1141 pinfo->tx_nrfifos = pdata->tx_num_fifo;
1142 pinfo->tx_fifosize = pdata->tx_buf_size;
1144 pinfo->rx_nrfifos = pdata->rx_num_fifo;
1145 pinfo->rx_fifosize = pdata->rx_buf_size;
1147 pinfo->port.uartclk = pdata->uart_clk;
1148 pinfo->port.mapbase = (unsigned long)mem;
1149 pinfo->port.irq = platform_get_irq(pdev, 0);
1151 return 0;
1153 #endif
1155 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1157 * Print a string to the serial port trying not to disturb
1158 * any possible real use of the port...
1160 * Note that this is called with interrupts already disabled
1162 static void cpm_uart_console_write(struct console *co, const char *s,
1163 u_int count)
1165 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1166 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1167 #else
1168 struct uart_cpm_port *pinfo =
1169 &cpm_uart_ports[cpm_uart_port_map[co->index]];
1170 #endif
1171 unsigned int i;
1172 volatile cbd_t *bdp, *bdbase;
1173 volatile unsigned char *cp;
1175 /* Get the address of the host memory buffer.
1177 bdp = pinfo->tx_cur;
1178 bdbase = pinfo->tx_bd_base;
1181 * Now, do each character. This is not as bad as it looks
1182 * since this is a holding FIFO and not a transmitting FIFO.
1183 * We could add the complexity of filling the entire transmit
1184 * buffer, but we would just wait longer between accesses......
1186 for (i = 0; i < count; i++, s++) {
1187 /* Wait for transmitter fifo to empty.
1188 * Ready indicates output is ready, and xmt is doing
1189 * that, not that it is ready for us to send.
1191 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1194 /* Send the character out.
1195 * If the buffer address is in the CPM DPRAM, don't
1196 * convert it.
1198 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1200 *cp = *s;
1202 bdp->cbd_datlen = 1;
1203 bdp->cbd_sc |= BD_SC_READY;
1205 if (bdp->cbd_sc & BD_SC_WRAP)
1206 bdp = bdbase;
1207 else
1208 bdp++;
1210 /* if a LF, also do CR... */
1211 if (*s == 10) {
1212 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1215 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1217 *cp = 13;
1218 bdp->cbd_datlen = 1;
1219 bdp->cbd_sc |= BD_SC_READY;
1221 if (bdp->cbd_sc & BD_SC_WRAP)
1222 bdp = bdbase;
1223 else
1224 bdp++;
1229 * Finally, Wait for transmitter & holding register to empty
1230 * and restore the IER
1232 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1235 pinfo->tx_cur = (volatile cbd_t *) bdp;
1239 static int __init cpm_uart_console_setup(struct console *co, char *options)
1241 int baud = 38400;
1242 int bits = 8;
1243 int parity = 'n';
1244 int flow = 'n';
1245 int ret;
1246 struct uart_cpm_port *pinfo;
1247 struct uart_port *port;
1249 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1250 struct device_node *np = NULL;
1251 int i = 0;
1253 if (co->index >= UART_NR) {
1254 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1255 co->index);
1256 return -ENODEV;
1259 do {
1260 np = of_find_node_by_type(np, "serial");
1261 if (!np)
1262 return -ENODEV;
1264 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1265 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1266 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1267 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1268 i--;
1269 } while (i++ != co->index);
1271 pinfo = &cpm_uart_ports[co->index];
1273 pinfo->flags |= FLAG_CONSOLE;
1274 port = &pinfo->port;
1276 ret = cpm_uart_init_port(np, pinfo);
1277 of_node_put(np);
1278 if (ret)
1279 return ret;
1281 #else
1283 struct fs_uart_platform_info *pdata;
1284 struct platform_device* pdev = early_uart_get_pdev(co->index);
1286 if (!pdev) {
1287 pr_info("cpm_uart: console: compat mode\n");
1288 /* compatibility - will be cleaned up */
1289 cpm_uart_init_portdesc();
1292 port =
1293 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1294 pinfo = (struct uart_cpm_port *)port;
1295 if (!pdev) {
1296 if (pinfo->set_lineif)
1297 pinfo->set_lineif(pinfo);
1298 } else {
1299 pdata = pdev->dev.platform_data;
1300 if (pdata)
1301 if (pdata->init_ioports)
1302 pdata->init_ioports(pdata);
1304 cpm_uart_drv_get_platform_data(pdev, 1);
1307 pinfo->flags |= FLAG_CONSOLE;
1308 #endif
1310 if (options) {
1311 uart_parse_options(options, &baud, &parity, &bits, &flow);
1312 } else {
1313 if ((baud = uart_baudrate()) == -1)
1314 baud = 9600;
1317 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1318 udbg_putc = NULL;
1319 #endif
1321 if (IS_SMC(pinfo)) {
1322 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1323 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1324 } else {
1325 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1326 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1329 ret = cpm_uart_allocbuf(pinfo, 1);
1331 if (ret)
1332 return ret;
1334 cpm_uart_initbd(pinfo);
1336 if (IS_SMC(pinfo))
1337 cpm_uart_init_smc(pinfo);
1338 else
1339 cpm_uart_init_scc(pinfo);
1341 uart_set_options(port, co, baud, parity, bits, flow);
1343 return 0;
1346 static struct uart_driver cpm_reg;
1347 static struct console cpm_scc_uart_console = {
1348 .name = "ttyCPM",
1349 .write = cpm_uart_console_write,
1350 .device = uart_console_device,
1351 .setup = cpm_uart_console_setup,
1352 .flags = CON_PRINTBUFFER,
1353 .index = -1,
1354 .data = &cpm_reg,
1357 int __init cpm_uart_console_init(void)
1359 register_console(&cpm_scc_uart_console);
1360 return 0;
1363 console_initcall(cpm_uart_console_init);
1365 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1366 #else
1367 #define CPM_UART_CONSOLE NULL
1368 #endif
1370 static struct uart_driver cpm_reg = {
1371 .owner = THIS_MODULE,
1372 .driver_name = "ttyCPM",
1373 .dev_name = "ttyCPM",
1374 .major = SERIAL_CPM_MAJOR,
1375 .minor = SERIAL_CPM_MINOR,
1376 .cons = CPM_UART_CONSOLE,
1377 .nr = UART_NR,
1380 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1381 static int probe_index;
1383 static int __devinit cpm_uart_probe(struct of_device *ofdev,
1384 const struct of_device_id *match)
1386 int index = probe_index++;
1387 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1388 int ret;
1390 pinfo->port.line = index;
1392 if (index >= UART_NR)
1393 return -ENODEV;
1395 dev_set_drvdata(&ofdev->dev, pinfo);
1397 ret = cpm_uart_init_port(ofdev->node, pinfo);
1398 if (ret)
1399 return ret;
1401 return uart_add_one_port(&cpm_reg, &pinfo->port);
1404 static int __devexit cpm_uart_remove(struct of_device *ofdev)
1406 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1407 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1410 static struct of_device_id cpm_uart_match[] = {
1412 .compatible = "fsl,cpm1-smc-uart",
1415 .compatible = "fsl,cpm1-scc-uart",
1418 .compatible = "fsl,cpm2-smc-uart",
1421 .compatible = "fsl,cpm2-scc-uart",
1426 static struct of_platform_driver cpm_uart_driver = {
1427 .name = "cpm_uart",
1428 .match_table = cpm_uart_match,
1429 .probe = cpm_uart_probe,
1430 .remove = cpm_uart_remove,
1433 static int __init cpm_uart_init(void)
1435 int ret = uart_register_driver(&cpm_reg);
1436 if (ret)
1437 return ret;
1439 ret = of_register_platform_driver(&cpm_uart_driver);
1440 if (ret)
1441 uart_unregister_driver(&cpm_reg);
1443 return ret;
1446 static void __exit cpm_uart_exit(void)
1448 of_unregister_platform_driver(&cpm_uart_driver);
1449 uart_unregister_driver(&cpm_reg);
1451 #else
1452 static int cpm_uart_drv_probe(struct device *dev)
1454 struct platform_device *pdev = to_platform_device(dev);
1455 struct fs_uart_platform_info *pdata;
1456 int ret = -ENODEV;
1458 if(!pdev) {
1459 printk(KERN_ERR"CPM UART: platform data missing!\n");
1460 return ret;
1463 pdata = pdev->dev.platform_data;
1465 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
1466 return ret;
1468 pr_debug("cpm_uart_drv_probe: Adding CPM UART %d\n", cpm_uart_id2nr(pdata->fs_no));
1470 if (pdata->init_ioports)
1471 pdata->init_ioports(pdata);
1473 ret = uart_add_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1475 return ret;
1478 static int cpm_uart_drv_remove(struct device *dev)
1480 struct platform_device *pdev = to_platform_device(dev);
1481 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1483 pr_debug("cpm_uart_drv_remove: Removing CPM UART %d\n",
1484 cpm_uart_id2nr(pdata->fs_no));
1486 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1487 return 0;
1490 static struct device_driver cpm_smc_uart_driver = {
1491 .name = "fsl-cpm-smc:uart",
1492 .bus = &platform_bus_type,
1493 .probe = cpm_uart_drv_probe,
1494 .remove = cpm_uart_drv_remove,
1497 static struct device_driver cpm_scc_uart_driver = {
1498 .name = "fsl-cpm-scc:uart",
1499 .bus = &platform_bus_type,
1500 .probe = cpm_uart_drv_probe,
1501 .remove = cpm_uart_drv_remove,
1505 This is supposed to match uart devices on platform bus,
1507 static int match_is_uart (struct device* dev, void* data)
1509 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
1510 int ret = 0;
1511 /* this was setfunc as uart */
1512 if(strstr(pdev->name,":uart")) {
1513 ret = 1;
1515 return ret;
1519 static int cpm_uart_init(void) {
1521 int ret;
1522 int i;
1523 struct device *dev;
1524 printk(KERN_INFO "Serial: CPM driver $Revision: 0.02 $\n");
1526 /* lookup the bus for uart devices */
1527 dev = bus_find_device(&platform_bus_type, NULL, 0, match_is_uart);
1529 /* There are devices on the bus - all should be OK */
1530 if (dev) {
1531 cpm_uart_count();
1532 cpm_reg.nr = cpm_uart_nr;
1534 if (!(ret = uart_register_driver(&cpm_reg))) {
1535 if ((ret = driver_register(&cpm_smc_uart_driver))) {
1536 uart_unregister_driver(&cpm_reg);
1537 return ret;
1539 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1540 driver_unregister(&cpm_scc_uart_driver);
1541 uart_unregister_driver(&cpm_reg);
1544 } else {
1545 /* No capable platform devices found - falling back to legacy mode */
1546 pr_info("cpm_uart: WARNING: no UART devices found on platform bus!\n");
1547 pr_info(
1548 "cpm_uart: the driver will guess configuration, but this mode is no longer supported.\n");
1550 /* Don't run this again, if the console driver did it already */
1551 if (cpm_uart_nr == 0)
1552 cpm_uart_init_portdesc();
1554 cpm_reg.nr = cpm_uart_nr;
1555 ret = uart_register_driver(&cpm_reg);
1557 if (ret)
1558 return ret;
1560 for (i = 0; i < cpm_uart_nr; i++) {
1561 int con = cpm_uart_port_map[i];
1562 cpm_uart_ports[con].port.line = i;
1563 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1564 if (cpm_uart_ports[con].set_lineif)
1565 cpm_uart_ports[con].set_lineif(&cpm_uart_ports[con]);
1566 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1570 return ret;
1573 static void __exit cpm_uart_exit(void)
1575 driver_unregister(&cpm_scc_uart_driver);
1576 driver_unregister(&cpm_smc_uart_driver);
1577 uart_unregister_driver(&cpm_reg);
1579 #endif
1581 module_init(cpm_uart_init);
1582 module_exit(cpm_uart_exit);
1584 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1585 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1586 MODULE_LICENSE("GPL");
1587 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);