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 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>
50 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54 #include <linux/serial_core.h>
55 #include <linux/kernel.h>
59 /***********************************************************************/
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 */
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 /**************************************************************/
76 /* Place-holder for board-specific stuff */
77 struct platform_device* __attribute__ ((weak)) __init
78 early_uart_get_pdev(int index)
84 static void cpm_uart_count(void)
87 #ifdef CONFIG_SERIAL_CPM_SMC1
88 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
90 #ifdef CONFIG_SERIAL_CPM_SMC2
91 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
93 #ifdef CONFIG_SERIAL_CPM_SCC1
94 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
96 #ifdef CONFIG_SERIAL_CPM_SCC2
97 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
99 #ifdef CONFIG_SERIAL_CPM_SCC3
100 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
102 #ifdef CONFIG_SERIAL_CPM_SCC4
103 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
107 /* Get UART number by its id */
108 static int cpm_uart_id2nr(int id)
112 for (i=0; i<UART_NR; i++) {
113 if (cpm_uart_port_map[i] == id)
118 /* not found or invalid argument */
123 * Check, if transmit buffers are processed
125 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
127 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
128 volatile cbd_t *bdp = pinfo->tx_bd_base;
132 if (bdp->cbd_sc & BD_SC_READY)
135 if (bdp->cbd_sc & BD_SC_WRAP) {
142 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
147 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
149 /* Whee. Do nothing. */
152 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
154 /* Whee. Do nothing. */
155 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
161 static void cpm_uart_stop_tx(struct uart_port *port)
163 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
164 volatile smc_t *smcp = pinfo->smcp;
165 volatile scc_t *sccp = pinfo->sccp;
167 pr_debug("CPM uart[%d]:stop tx\n", port->line);
170 smcp->smc_smcm &= ~SMCM_TX;
172 sccp->scc_sccm &= ~UART_SCCM_TX;
178 static void cpm_uart_start_tx(struct uart_port *port)
180 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
181 volatile smc_t *smcp = pinfo->smcp;
182 volatile scc_t *sccp = pinfo->sccp;
184 pr_debug("CPM uart[%d]:start tx\n", port->line);
187 if (smcp->smc_smcm & SMCM_TX)
190 if (sccp->scc_sccm & UART_SCCM_TX)
194 if (cpm_uart_tx_pump(port) != 0) {
196 smcp->smc_smcm |= SMCM_TX;
197 smcp->smc_smcmr |= SMCMR_TEN;
199 sccp->scc_sccm |= UART_SCCM_TX;
200 pinfo->sccp->scc_gsmrl |= SCC_GSMRL_ENT;
208 static void cpm_uart_stop_rx(struct uart_port *port)
210 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
211 volatile smc_t *smcp = pinfo->smcp;
212 volatile scc_t *sccp = pinfo->sccp;
214 pr_debug("CPM uart[%d]:stop rx\n", port->line);
217 smcp->smc_smcm &= ~SMCM_RX;
219 sccp->scc_sccm &= ~UART_SCCM_RX;
223 * Enable Modem status interrupts
225 static void cpm_uart_enable_ms(struct uart_port *port)
227 pr_debug("CPM uart[%d]:enable ms\n", port->line);
233 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
235 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
236 int line = pinfo - cpm_uart_ports;
238 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
242 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
244 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
248 * Transmit characters, refill buffer descriptor, if possible
250 static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
252 pr_debug("CPM uart[%d]:TX INT\n", port->line);
254 cpm_uart_tx_pump(port);
260 static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
263 unsigned char ch, *cp;
264 struct tty_struct *tty = port->info->tty;
265 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
270 pr_debug("CPM uart[%d]:RX INT\n", port->line);
272 /* Just loop through the closed BDs and copy the characters into
278 status = bdp->cbd_sc;
279 /* If this one is empty, return happy */
280 if (status & BD_SC_EMPTY)
283 /* get number of characters, and check spce in flip-buffer */
286 /* If we have not enough room in tty flip buffer, then we try
287 * later, which will be the next rx-interrupt or a timeout
289 if(tty_buffer_request_room(tty, i) < i) {
290 printk(KERN_WARNING "No room in flip buffer\n");
295 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
297 /* loop through the buffer */
304 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
306 if (uart_handle_sysrq_char(port, ch, regs))
310 tty_insert_flip_char(tty, ch, flg);
312 } /* End while (i--) */
314 /* This BD is ready to be used again. Clear status. get next */
315 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
316 bdp->cbd_sc |= BD_SC_EMPTY;
318 if (bdp->cbd_sc & BD_SC_WRAP)
319 bdp = pinfo->rx_bd_base;
325 /* Write back buffer pointer */
326 pinfo->rx_cur = (volatile cbd_t *) bdp;
328 /* activate BH processing */
329 tty_flip_buffer_push(tty);
333 /* Error processing */
337 if (status & BD_SC_BR)
339 if (status & BD_SC_PR)
340 port->icount.parity++;
341 if (status & BD_SC_FR)
342 port->icount.frame++;
343 if (status & BD_SC_OV)
344 port->icount.overrun++;
346 /* Mask out ignored conditions */
347 status &= port->read_status_mask;
349 /* Handle the remaining ones */
350 if (status & BD_SC_BR)
352 else if (status & BD_SC_PR)
354 else if (status & BD_SC_FR)
357 /* overrun does not affect the current character ! */
358 if (status & BD_SC_OV) {
361 /* We skip this buffer */
362 /* CHECK: Is really nothing senseful there */
363 /* ASSUMPTION: it contains nothing valid */
373 * Asynchron mode interrupt handler
375 static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
378 struct uart_port *port = (struct uart_port *)data;
379 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
380 volatile smc_t *smcp = pinfo->smcp;
381 volatile scc_t *sccp = pinfo->sccp;
383 pr_debug("CPM uart[%d]:IRQ\n", port->line);
386 events = smcp->smc_smce;
387 smcp->smc_smce = events;
388 if (events & SMCM_BRKE)
389 uart_handle_break(port);
390 if (events & SMCM_RX)
391 cpm_uart_int_rx(port, regs);
392 if (events & SMCM_TX)
393 cpm_uart_int_tx(port, regs);
395 events = sccp->scc_scce;
396 sccp->scc_scce = events;
397 if (events & UART_SCCM_BRKE)
398 uart_handle_break(port);
399 if (events & UART_SCCM_RX)
400 cpm_uart_int_rx(port, regs);
401 if (events & UART_SCCM_TX)
402 cpm_uart_int_tx(port, regs);
404 return (events) ? IRQ_HANDLED : IRQ_NONE;
407 static int cpm_uart_startup(struct uart_port *port)
410 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
411 int line = pinfo - cpm_uart_ports;
413 pr_debug("CPM uart[%d]:startup\n", port->line);
415 /* Install interrupt handler. */
416 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
422 pinfo->smcp->smc_smcm |= SMCM_RX;
423 pinfo->smcp->smc_smcmr |= SMCMR_REN;
425 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
428 if (!(pinfo->flags & FLAG_CONSOLE))
429 cpm_line_cr_cmd(line,CPM_CR_INIT_TRX);
433 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
435 set_current_state(TASK_UNINTERRUPTIBLE);
436 schedule_timeout(pinfo->wait_closing);
442 static void cpm_uart_shutdown(struct uart_port *port)
444 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
445 int line = pinfo - cpm_uart_ports;
447 pr_debug("CPM uart[%d]:shutdown\n", port->line);
449 /* free interrupt handler */
450 free_irq(port->irq, port);
452 /* If the port is not the console, disable Rx and Tx. */
453 if (!(pinfo->flags & FLAG_CONSOLE)) {
454 /* Wait for all the BDs marked sent */
455 while(!cpm_uart_tx_empty(port)) {
456 set_current_state(TASK_UNINTERRUPTIBLE);
460 if (pinfo->wait_closing)
461 cpm_uart_wait_until_send(pinfo);
465 volatile smc_t *smcp = pinfo->smcp;
466 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
467 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
469 volatile scc_t *sccp = pinfo->sccp;
470 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
471 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
474 /* Shut them really down and reinit buffer descriptors */
476 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
478 cpm_line_cr_cmd(line, CPM_CR_GRA_STOP_TX);
480 cpm_uart_initbd(pinfo);
484 static void cpm_uart_set_termios(struct uart_port *port,
485 struct termios *termios, struct termios *old)
489 u16 cval, scval, prev_mode;
491 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
492 volatile smc_t *smcp = pinfo->smcp;
493 volatile scc_t *sccp = pinfo->sccp;
495 pr_debug("CPM uart[%d]:set_termios\n", port->line);
497 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
499 /* Character length programmed into the mode register is the
500 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
501 * 1 or 2 stop bits, minus 1.
502 * The value 'bits' counts this for us.
508 switch (termios->c_cflag & CSIZE) {
521 /* Never happens, but GCC is too dumb to figure it out */
528 if (termios->c_cflag & CSTOPB) {
529 cval |= SMCMR_SL; /* Two stops */
530 scval |= SCU_PSMR_SL;
534 if (termios->c_cflag & PARENB) {
536 scval |= SCU_PSMR_PEN;
538 if (!(termios->c_cflag & PARODD)) {
539 cval |= SMCMR_PM_EVEN;
540 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
545 * Set up parity check flag
547 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
549 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
550 if (termios->c_iflag & INPCK)
551 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
552 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
553 port->read_status_mask |= BD_SC_BR;
556 * Characters to ignore
558 port->ignore_status_mask = 0;
559 if (termios->c_iflag & IGNPAR)
560 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
561 if (termios->c_iflag & IGNBRK) {
562 port->ignore_status_mask |= BD_SC_BR;
564 * If we're ignore parity and break indicators, ignore
565 * overruns too. (For real raw support).
567 if (termios->c_iflag & IGNPAR)
568 port->ignore_status_mask |= BD_SC_OV;
571 * !!! ignore all characters if CREAD is not set
573 if ((termios->c_cflag & CREAD) == 0)
574 port->read_status_mask &= ~BD_SC_EMPTY;
576 spin_lock_irqsave(&port->lock, flags);
578 /* Start bit has not been added (so don't, because we would just
579 * subtract it later), and we need to add one for the number of
580 * stops bits (there is always at least one).
584 /* Set the mode register. We want to keep a copy of the
585 * enables, because we want to put them back if they were
588 prev_mode = smcp->smc_smcmr;
589 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
590 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
592 sccp->scc_psmr = (sbits << 12) | scval;
595 cpm_set_brg(pinfo->brg - 1, baud);
596 spin_unlock_irqrestore(&port->lock, flags);
600 static const char *cpm_uart_type(struct uart_port *port)
602 pr_debug("CPM uart[%d]:uart_type\n", port->line);
604 return port->type == PORT_CPM ? "CPM UART" : NULL;
608 * verify the new serial_struct (for TIOCSSERIAL).
610 static int cpm_uart_verify_port(struct uart_port *port,
611 struct serial_struct *ser)
615 pr_debug("CPM uart[%d]:verify_port\n", port->line);
617 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
619 if (ser->irq < 0 || ser->irq >= NR_IRQS)
621 if (ser->baud_base < 9600)
627 * Transmit characters, refill buffer descriptor, if possible
629 static int cpm_uart_tx_pump(struct uart_port *port)
634 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
635 struct circ_buf *xmit = &port->info->xmit;
637 /* Handle xon/xoff */
639 /* Pick next descriptor and fill from buffer */
642 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
646 bdp->cbd_sc |= BD_SC_READY;
648 if (bdp->cbd_sc & BD_SC_WRAP)
649 bdp = pinfo->tx_bd_base;
659 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
660 cpm_uart_stop_tx(port);
664 /* Pick next descriptor and fill from buffer */
667 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
669 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
670 while (count < pinfo->tx_fifosize) {
671 *p++ = xmit->buf[xmit->tail];
672 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
675 if (xmit->head == xmit->tail)
678 bdp->cbd_datlen = count;
679 bdp->cbd_sc |= BD_SC_READY;
682 if (bdp->cbd_sc & BD_SC_WRAP)
683 bdp = pinfo->tx_bd_base;
689 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
690 uart_write_wakeup(port);
692 if (uart_circ_empty(xmit)) {
693 cpm_uart_stop_tx(port);
701 * init buffer descriptors
703 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
709 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
711 /* Set the physical address of the host memory
712 * buffers in the buffer descriptors, and the
713 * virtual address for us to work with.
715 mem_addr = pinfo->mem_addr;
716 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
717 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
718 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
719 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
720 mem_addr += pinfo->rx_fifosize;
723 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
724 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
726 /* Set the physical address of the host memory
727 * buffers in the buffer descriptors, and the
728 * virtual address for us to work with.
730 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
731 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
732 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
733 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
734 bdp->cbd_sc = BD_SC_INTRPT;
735 mem_addr += pinfo->tx_fifosize;
738 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
739 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
742 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
744 int line = pinfo - cpm_uart_ports;
746 volatile scc_uart_t *sup;
748 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
754 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
755 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
757 /* Set up the uart parameters in the
761 cpm_set_scc_fcr(sup);
763 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
764 sup->scc_maxidl = pinfo->rx_fifosize;
773 sup->scc_char1 = 0x8000;
774 sup->scc_char2 = 0x8000;
775 sup->scc_char3 = 0x8000;
776 sup->scc_char4 = 0x8000;
777 sup->scc_char5 = 0x8000;
778 sup->scc_char6 = 0x8000;
779 sup->scc_char7 = 0x8000;
780 sup->scc_char8 = 0x8000;
781 sup->scc_rccm = 0xc0ff;
783 /* Send the CPM an initialize command.
785 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
787 /* Set UART mode, 8 bit, no parity, one stop.
788 * Enable receive and transmit.
792 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
794 /* Enable rx interrupts and clear all pending events. */
796 scp->scc_scce = 0xffff;
797 scp->scc_dsr = 0x7e7e;
798 scp->scc_psmr = 0x3000;
800 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
803 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
805 int line = pinfo - cpm_uart_ports;
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(line, 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 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
935 .ops = &cpm_uart_pops,
937 .lock = SPIN_LOCK_UNLOCKED,
940 .tx_nrfifos = TX_NUM_FIFO,
941 .tx_fifosize = TX_BUF_SIZE,
942 .rx_nrfifos = RX_NUM_FIFO,
943 .rx_fifosize = RX_BUF_SIZE,
944 .set_lineif = smc1_lineif,
949 .ops = &cpm_uart_pops,
951 .lock = SPIN_LOCK_UNLOCKED,
954 .tx_nrfifos = TX_NUM_FIFO,
955 .tx_fifosize = TX_BUF_SIZE,
956 .rx_nrfifos = RX_NUM_FIFO,
957 .rx_fifosize = RX_BUF_SIZE,
958 .set_lineif = smc2_lineif,
959 #ifdef CONFIG_SERIAL_CPM_ALT_SMC2
966 .ops = &cpm_uart_pops,
968 .lock = SPIN_LOCK_UNLOCKED,
970 .tx_nrfifos = TX_NUM_FIFO,
971 .tx_fifosize = TX_BUF_SIZE,
972 .rx_nrfifos = RX_NUM_FIFO,
973 .rx_fifosize = RX_BUF_SIZE,
974 .set_lineif = scc1_lineif,
975 .wait_closing = SCC_WAIT_CLOSING,
980 .ops = &cpm_uart_pops,
982 .lock = SPIN_LOCK_UNLOCKED,
984 .tx_nrfifos = TX_NUM_FIFO,
985 .tx_fifosize = TX_BUF_SIZE,
986 .rx_nrfifos = RX_NUM_FIFO,
987 .rx_fifosize = RX_BUF_SIZE,
988 .set_lineif = scc2_lineif,
989 .wait_closing = SCC_WAIT_CLOSING,
994 .ops = &cpm_uart_pops,
996 .lock = SPIN_LOCK_UNLOCKED,
998 .tx_nrfifos = TX_NUM_FIFO,
999 .tx_fifosize = TX_BUF_SIZE,
1000 .rx_nrfifos = RX_NUM_FIFO,
1001 .rx_fifosize = RX_BUF_SIZE,
1002 .set_lineif = scc3_lineif,
1003 .wait_closing = SCC_WAIT_CLOSING,
1008 .ops = &cpm_uart_pops,
1010 .lock = SPIN_LOCK_UNLOCKED,
1012 .tx_nrfifos = TX_NUM_FIFO,
1013 .tx_fifosize = TX_BUF_SIZE,
1014 .rx_nrfifos = RX_NUM_FIFO,
1015 .rx_fifosize = RX_BUF_SIZE,
1016 .set_lineif = scc4_lineif,
1017 .wait_closing = SCC_WAIT_CLOSING,
1021 int cpm_uart_drv_get_platform_data(struct platform_device *pdev, int is_con)
1024 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1025 int idx = pdata->fs_no; /* It is UART_SMCx or UART_SCCx index */
1026 struct uart_cpm_port *pinfo;
1030 line = cpm_uart_id2nr(idx);
1032 printk(KERN_ERR"%s(): port %d is not registered", __FUNCTION__, idx);
1036 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1038 pinfo->brg = pdata->brg;
1041 pinfo->port.line = line;
1042 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1045 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1049 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
1053 if(idx > fsid_smc2_uart) {
1054 pinfo->sccp = (scc_t *)mem;
1055 pinfo->sccup = (scc_uart_t *)pram;
1057 pinfo->smcp = (smc_t *)mem;
1058 pinfo->smcup = (smc_uart_t *)pram;
1060 pinfo->tx_nrfifos = pdata->tx_num_fifo;
1061 pinfo->tx_fifosize = pdata->tx_buf_size;
1063 pinfo->rx_nrfifos = pdata->rx_num_fifo;
1064 pinfo->rx_fifosize = pdata->rx_buf_size;
1066 pinfo->port.uartclk = pdata->uart_clk;
1067 pinfo->port.mapbase = (unsigned long)mem;
1068 pinfo->port.irq = platform_get_irq(pdev, 0);
1073 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1075 * Print a string to the serial port trying not to disturb
1076 * any possible real use of the port...
1078 * Note that this is called with interrupts already disabled
1080 static void cpm_uart_console_write(struct console *co, const char *s,
1083 struct uart_cpm_port *pinfo =
1084 &cpm_uart_ports[cpm_uart_port_map[co->index]];
1086 volatile cbd_t *bdp, *bdbase;
1087 volatile unsigned char *cp;
1089 /* Get the address of the host memory buffer.
1091 bdp = pinfo->tx_cur;
1092 bdbase = pinfo->tx_bd_base;
1095 * Now, do each character. This is not as bad as it looks
1096 * since this is a holding FIFO and not a transmitting FIFO.
1097 * We could add the complexity of filling the entire transmit
1098 * buffer, but we would just wait longer between accesses......
1100 for (i = 0; i < count; i++, s++) {
1101 /* Wait for transmitter fifo to empty.
1102 * Ready indicates output is ready, and xmt is doing
1103 * that, not that it is ready for us to send.
1105 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1108 /* Send the character out.
1109 * If the buffer address is in the CPM DPRAM, don't
1112 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1116 bdp->cbd_datlen = 1;
1117 bdp->cbd_sc |= BD_SC_READY;
1119 if (bdp->cbd_sc & BD_SC_WRAP)
1124 /* if a LF, also do CR... */
1126 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1129 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1132 bdp->cbd_datlen = 1;
1133 bdp->cbd_sc |= BD_SC_READY;
1135 if (bdp->cbd_sc & BD_SC_WRAP)
1143 * Finally, Wait for transmitter & holding register to empty
1144 * and restore the IER
1146 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1149 pinfo->tx_cur = (volatile cbd_t *) bdp;
1153 static int __init cpm_uart_console_setup(struct console *co, char *options)
1155 struct uart_port *port;
1156 struct uart_cpm_port *pinfo;
1163 struct fs_uart_platform_info *pdata;
1164 struct platform_device* pdev = early_uart_get_pdev(co->index);
1167 pr_info("cpm_uart: console: compat mode\n");
1168 /* compatibility - will be cleaned up */
1169 cpm_uart_init_portdesc();
1173 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1174 pinfo = (struct uart_cpm_port *)port;
1176 if (pinfo->set_lineif)
1177 pinfo->set_lineif(pinfo);
1179 pdata = pdev->dev.platform_data;
1181 if (pdata->init_ioports)
1182 pdata->init_ioports();
1184 cpm_uart_drv_get_platform_data(pdev, 1);
1187 pinfo->flags |= FLAG_CONSOLE;
1190 uart_parse_options(options, &baud, &parity, &bits, &flow);
1192 bd_t *bd = (bd_t *) __res;
1194 if (bd->bi_baudrate)
1195 baud = bd->bi_baudrate;
1200 if (IS_SMC(pinfo)) {
1201 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1202 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1204 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1205 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1208 ret = cpm_uart_allocbuf(pinfo, 1);
1213 cpm_uart_initbd(pinfo);
1216 cpm_uart_init_smc(pinfo);
1218 cpm_uart_init_scc(pinfo);
1220 uart_set_options(port, co, baud, parity, bits, flow);
1225 static struct uart_driver cpm_reg;
1226 static struct console cpm_scc_uart_console = {
1228 .write = cpm_uart_console_write,
1229 .device = uart_console_device,
1230 .setup = cpm_uart_console_setup,
1231 .flags = CON_PRINTBUFFER,
1236 int __init cpm_uart_console_init(void)
1238 register_console(&cpm_scc_uart_console);
1242 console_initcall(cpm_uart_console_init);
1244 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1246 #define CPM_UART_CONSOLE NULL
1249 static struct uart_driver cpm_reg = {
1250 .owner = THIS_MODULE,
1251 .driver_name = "ttyCPM",
1252 .dev_name = "ttyCPM",
1253 .major = SERIAL_CPM_MAJOR,
1254 .minor = SERIAL_CPM_MINOR,
1255 .cons = CPM_UART_CONSOLE,
1257 static int cpm_uart_drv_probe(struct device *dev)
1259 struct platform_device *pdev = to_platform_device(dev);
1260 struct fs_uart_platform_info *pdata;
1264 printk(KERN_ERR"CPM UART: platform data missing!\n");
1268 pdata = pdev->dev.platform_data;
1269 pr_debug("cpm_uart_drv_probe: Adding CPM UART %d\n", cpm_uart_id2nr(pdata->fs_no));
1271 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
1274 if (pdata->init_ioports)
1275 pdata->init_ioports();
1277 ret = uart_add_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1282 static int cpm_uart_drv_remove(struct device *dev)
1284 struct platform_device *pdev = to_platform_device(dev);
1285 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1287 pr_debug("cpm_uart_drv_remove: Removing CPM UART %d\n",
1288 cpm_uart_id2nr(pdata->fs_no));
1290 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1294 static struct device_driver cpm_smc_uart_driver = {
1295 .name = "fsl-cpm-smc:uart",
1296 .bus = &platform_bus_type,
1297 .probe = cpm_uart_drv_probe,
1298 .remove = cpm_uart_drv_remove,
1301 static struct device_driver cpm_scc_uart_driver = {
1302 .name = "fsl-cpm-scc:uart",
1303 .bus = &platform_bus_type,
1304 .probe = cpm_uart_drv_probe,
1305 .remove = cpm_uart_drv_remove,
1309 This is supposed to match uart devices on platform bus,
1311 static int match_is_uart (struct device* dev, void* data)
1313 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
1315 /* this was setfunc as uart */
1316 if(strstr(pdev->name,":uart")) {
1323 static int cpm_uart_init(void) {
1328 printk(KERN_INFO "Serial: CPM driver $Revision: 0.02 $\n");
1330 /* lookup the bus for uart devices */
1331 dev = bus_find_device(&platform_bus_type, NULL, 0, match_is_uart);
1333 /* There are devices on the bus - all should be OK */
1336 cpm_reg.nr = cpm_uart_nr;
1338 if (!(ret = uart_register_driver(&cpm_reg))) {
1339 if ((ret = driver_register(&cpm_smc_uart_driver))) {
1340 uart_unregister_driver(&cpm_reg);
1343 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1344 driver_unregister(&cpm_scc_uart_driver);
1345 uart_unregister_driver(&cpm_reg);
1349 /* No capable platform devices found - falling back to legacy mode */
1350 pr_info("cpm_uart: WARNING: no UART devices found on platform bus!\n");
1352 "cpm_uart: the driver will guess configuration, but this mode is no longer supported.\n");
1353 #ifndef CONFIG_SERIAL_CPM_CONSOLE
1354 ret = cpm_uart_init_portdesc();
1359 cpm_reg.nr = cpm_uart_nr;
1360 ret = uart_register_driver(&cpm_reg);
1365 for (i = 0; i < cpm_uart_nr; i++) {
1366 int con = cpm_uart_port_map[i];
1367 cpm_uart_ports[con].port.line = i;
1368 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1369 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1376 static void __exit cpm_uart_exit(void)
1378 driver_unregister(&cpm_scc_uart_driver);
1379 driver_unregister(&cpm_smc_uart_driver);
1380 uart_unregister_driver(&cpm_reg);
1383 module_init(cpm_uart_init);
1384 module_exit(cpm_uart_exit);
1386 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1387 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1388 MODULE_LICENSE("GPL");
1389 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);