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>
48 #include <asm/delay.h>
49 #include <asm/fs_pd.h>
52 #ifdef CONFIG_PPC_CPM_NEW_BINDING
53 #include <linux/of_platform.h>
56 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
60 #include <linux/serial_core.h>
61 #include <linux/kernel.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 */
81 /* Place-holder for board-specific stuff */
82 struct platform_device* __attribute__ ((weak)) __init
83 early_uart_get_pdev(int index)
89 static void cpm_uart_count(void)
92 #ifdef CONFIG_SERIAL_CPM_SMC1
93 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
95 #ifdef CONFIG_SERIAL_CPM_SMC2
96 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
98 #ifdef CONFIG_SERIAL_CPM_SCC1
99 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
101 #ifdef CONFIG_SERIAL_CPM_SCC2
102 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
104 #ifdef CONFIG_SERIAL_CPM_SCC3
105 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
107 #ifdef CONFIG_SERIAL_CPM_SCC4
108 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
112 /* Get UART number by its id */
113 static int cpm_uart_id2nr(int id)
117 for (i=0; i<UART_NR; i++) {
118 if (cpm_uart_port_map[i] == id)
123 /* not found or invalid argument */
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;
138 if (bdp->cbd_sc & BD_SC_READY)
141 if (bdp->cbd_sc & BD_SC_WRAP) {
148 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, 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;
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);
176 smcp->smc_smcm &= ~SMCM_TX;
178 sccp->scc_sccm &= ~UART_SCCM_TX;
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);
193 if (smcp->smc_smcm & SMCM_TX)
196 if (sccp->scc_sccm & UART_SCCM_TX)
200 if (cpm_uart_tx_pump(port) != 0) {
202 smcp->smc_smcm |= SMCM_TX;
204 sccp->scc_sccm |= UART_SCCM_TX;
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);
221 smcp->smc_smcm &= ~SMCM_RX;
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);
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,
245 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
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);
263 static void cpm_uart_int_rx(struct uart_port *port)
266 unsigned char ch, *cp;
267 struct tty_struct *tty = port->info->tty;
268 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
273 pr_debug("CPM uart[%d]:RX INT\n", port->line);
275 /* Just loop through the closed BDs and copy the characters into
281 status = bdp->cbd_sc;
282 /* If this one is empty, return happy */
283 if (status & BD_SC_EMPTY)
286 /* get number of characters, and check spce in flip-buffer */
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");
298 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
300 /* loop through the buffer */
307 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
309 if (uart_handle_sysrq_char(port, ch))
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;
328 /* Write back buffer pointer */
329 pinfo->rx_cur = (volatile cbd_t *) bdp;
331 /* activate BH processing */
332 tty_flip_buffer_push(tty);
336 /* Error processing */
340 if (status & BD_SC_BR)
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)
355 else if (status & BD_SC_PR)
357 else if (status & BD_SC_FR)
360 /* overrun does not affect the current character ! */
361 if (status & BD_SC_OV) {
364 /* We skip this buffer */
365 /* CHECK: Is really nothing senseful there */
366 /* ASSUMPTION: it contains nothing valid */
376 * Asynchron mode interrupt handler
378 static irqreturn_t cpm_uart_int(int irq, void *data)
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);
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);
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)
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);
424 pinfo->smcp->smc_smcm |= SMCM_RX;
425 pinfo->smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
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);
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);
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);
462 if (pinfo->wait_closing)
463 cpm_uart_wait_until_send(pinfo);
467 volatile smc_t *smcp = pinfo->smcp;
468 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
469 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
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 */
478 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
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)
492 u16 cval, scval, prev_mode;
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.
511 switch (termios->c_cflag & CSIZE) {
524 /* Never happens, but GCC is too dumb to figure it out */
531 if (termios->c_cflag & CSTOPB) {
532 cval |= SMCMR_SL; /* Two stops */
533 scval |= SCU_PSMR_SL;
537 if (termios->c_cflag & PARENB) {
539 scval |= SCU_PSMR_PEN;
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).
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
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));
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)
617 pr_debug("CPM uart[%d]:verify_port\n", port->line);
619 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
621 if (ser->irq < 0 || ser->irq >= NR_IRQS)
623 if (ser->baud_base < 9600)
629 * Transmit characters, refill buffer descriptor, if possible
631 static int cpm_uart_tx_pump(struct uart_port *port)
636 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
637 struct circ_buf *xmit = &port->info->xmit;
639 /* Handle xon/xoff */
641 /* Pick next descriptor and fill from buffer */
644 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
648 bdp->cbd_sc |= BD_SC_READY;
650 if (bdp->cbd_sc & BD_SC_WRAP)
651 bdp = pinfo->tx_bd_base;
661 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
662 cpm_uart_stop_tx(port);
666 /* Pick next descriptor and fill from buffer */
669 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
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);
677 if (xmit->head == xmit->tail)
680 bdp->cbd_datlen = count;
681 bdp->cbd_sc |= BD_SC_READY;
684 if (bdp->cbd_sc & BD_SC_WRAP)
685 bdp = pinfo->tx_bd_base;
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);
703 * init buffer descriptors
705 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
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)
747 volatile scc_uart_t *sup;
749 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
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
762 cpm_set_scc_fcr(sup);
764 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
765 sup->scc_maxidl = pinfo->rx_fifosize;
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.
793 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
795 /* Enable rx interrupts and clear all pending events. */
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)
807 volatile smc_uart_t *up;
809 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
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;
826 up->smc_brkcr = 1; /* number of break chars */
830 /* Set up the uart parameters in the
835 /* Using idle charater time requires some additional tuning. */
836 up->smc_mrblr = pinfo->rx_fifosize;
837 up->smc_maxidl = pinfo->rx_fifosize;
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. */
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;
865 pr_debug("CPM uart[%d]:request port\n", port->line);
867 if (pinfo->flags & FLAG_CONSOLE)
871 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
872 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
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);
883 cpm_uart_initbd(pinfo);
885 cpm_uart_init_smc(pinfo);
887 cpm_uart_init_scc(pinfo);
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)
937 void __iomem *mem, __iomem *pram;
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);
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);
955 pinfo->command = *data;
957 mem = of_iomap(np, 0);
961 pram = of_iomap(np, 1);
967 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
968 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
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;
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) {
999 return cpm_uart_request_port(&pinfo->port);
1010 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
1014 .ops = &cpm_uart_pops,
1016 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SMC1].port.lock),
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,
1028 .ops = &cpm_uart_pops,
1030 .lock = __SPIN_LOCK_UNLOCKED(cpm_uart_ports[UART_SMC2].port.lock),
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
1045 .ops = &cpm_uart_pops,
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,
1059 .ops = &cpm_uart_pops,
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,
1073 .ops = &cpm_uart_pops,
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,
1087 .ops = &cpm_uart_pops,
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)
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;
1109 idx = pdata->fs_no = fs_uart_get_id(pdata);
1111 line = cpm_uart_id2nr(idx);
1113 printk(KERN_ERR"%s(): port %d is not registered", __FUNCTION__, idx);
1117 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1119 pinfo->brg = pdata->brg;
1122 pinfo->port.line = line;
1123 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1126 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1128 mem = (u32)ioremap(r->start, r->end - r->start + 1);
1130 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
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;
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);
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,
1165 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1166 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1168 struct uart_cpm_port *pinfo =
1169 &cpm_uart_ports[cpm_uart_port_map[co->index]];
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
1198 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1202 bdp->cbd_datlen = 1;
1203 bdp->cbd_sc |= BD_SC_READY;
1205 if (bdp->cbd_sc & BD_SC_WRAP)
1210 /* if a LF, also do CR... */
1212 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1215 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1218 bdp->cbd_datlen = 1;
1219 bdp->cbd_sc |= BD_SC_READY;
1221 if (bdp->cbd_sc & BD_SC_WRAP)
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)
1246 struct uart_cpm_port *pinfo;
1247 struct uart_port *port;
1249 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1250 struct device_node *np = NULL;
1253 if (co->index >= UART_NR) {
1254 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1260 np = of_find_node_by_type(np, "serial");
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"))
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);
1283 struct fs_uart_platform_info *pdata;
1284 struct platform_device* pdev = early_uart_get_pdev(co->index);
1287 pr_info("cpm_uart: console: compat mode\n");
1288 /* compatibility - will be cleaned up */
1289 cpm_uart_init_portdesc();
1293 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1294 pinfo = (struct uart_cpm_port *)port;
1296 if (pinfo->set_lineif)
1297 pinfo->set_lineif(pinfo);
1299 pdata = pdev->dev.platform_data;
1301 if (pdata->init_ioports)
1302 pdata->init_ioports(pdata);
1304 cpm_uart_drv_get_platform_data(pdev, 1);
1307 pinfo->flags |= FLAG_CONSOLE;
1311 uart_parse_options(options, &baud, &parity, &bits, &flow);
1313 if ((baud = uart_baudrate()) == -1)
1317 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1321 if (IS_SMC(pinfo)) {
1322 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1323 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
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);
1334 cpm_uart_initbd(pinfo);
1337 cpm_uart_init_smc(pinfo);
1339 cpm_uart_init_scc(pinfo);
1341 uart_set_options(port, co, baud, parity, bits, flow);
1346 static struct uart_driver cpm_reg;
1347 static struct console cpm_scc_uart_console = {
1349 .write = cpm_uart_console_write,
1350 .device = uart_console_device,
1351 .setup = cpm_uart_console_setup,
1352 .flags = CON_PRINTBUFFER,
1357 int __init cpm_uart_console_init(void)
1359 register_console(&cpm_scc_uart_console);
1363 console_initcall(cpm_uart_console_init);
1365 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1367 #define CPM_UART_CONSOLE NULL
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,
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];
1390 pinfo->port.line = index;
1392 if (index >= UART_NR)
1395 dev_set_drvdata(&ofdev->dev, pinfo);
1397 ret = cpm_uart_init_port(ofdev->node, pinfo);
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 = {
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);
1439 ret = of_register_platform_driver(&cpm_uart_driver);
1441 uart_unregister_driver(&cpm_reg);
1446 static void __exit cpm_uart_exit(void)
1448 of_unregister_platform_driver(&cpm_uart_driver);
1449 uart_unregister_driver(&cpm_reg);
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;
1459 printk(KERN_ERR"CPM UART: platform data missing!\n");
1463 pdata = pdev->dev.platform_data;
1465 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
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);
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);
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);
1511 /* this was setfunc as uart */
1512 if(strstr(pdev->name,":uart")) {
1519 static int cpm_uart_init(void) {
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 */
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);
1539 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1540 driver_unregister(&cpm_scc_uart_driver);
1541 uart_unregister_driver(&cpm_reg);
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");
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);
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);
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);
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);