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 MontaVista Software, Inc. by Vitaly Bordug <vbordug@ru.mvista.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/tty.h>
36 #include <linux/ioport.h>
37 #include <linux/init.h>
38 #include <linux/serial.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/device.h>
42 #include <linux/bootmem.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/fs_uart_pd.h>
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 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;
108 * Check, if transmit buffers are processed
110 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
112 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
113 volatile cbd_t *bdp = pinfo->tx_bd_base;
117 if (bdp->cbd_sc & BD_SC_READY)
120 if (bdp->cbd_sc & BD_SC_WRAP) {
127 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
132 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
134 /* Whee. Do nothing. */
137 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
139 /* Whee. Do nothing. */
140 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
146 static void cpm_uart_stop_tx(struct uart_port *port)
148 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
149 volatile smc_t *smcp = pinfo->smcp;
150 volatile scc_t *sccp = pinfo->sccp;
152 pr_debug("CPM uart[%d]:stop tx\n", port->line);
155 smcp->smc_smcm &= ~SMCM_TX;
157 sccp->scc_sccm &= ~UART_SCCM_TX;
163 static void cpm_uart_start_tx(struct uart_port *port)
165 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
166 volatile smc_t *smcp = pinfo->smcp;
167 volatile scc_t *sccp = pinfo->sccp;
169 pr_debug("CPM uart[%d]:start tx\n", port->line);
172 if (smcp->smc_smcm & SMCM_TX)
175 if (sccp->scc_sccm & UART_SCCM_TX)
179 if (cpm_uart_tx_pump(port) != 0) {
181 smcp->smc_smcm |= SMCM_TX;
182 smcp->smc_smcmr |= SMCMR_TEN;
184 sccp->scc_sccm |= UART_SCCM_TX;
185 pinfo->sccp->scc_gsmrl |= SCC_GSMRL_ENT;
193 static void cpm_uart_stop_rx(struct uart_port *port)
195 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
196 volatile smc_t *smcp = pinfo->smcp;
197 volatile scc_t *sccp = pinfo->sccp;
199 pr_debug("CPM uart[%d]:stop rx\n", port->line);
202 smcp->smc_smcm &= ~SMCM_RX;
204 sccp->scc_sccm &= ~UART_SCCM_RX;
208 * Enable Modem status interrupts
210 static void cpm_uart_enable_ms(struct uart_port *port)
212 pr_debug("CPM uart[%d]:enable ms\n", port->line);
218 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
220 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
221 int line = pinfo - cpm_uart_ports;
223 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
227 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
229 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
233 * Transmit characters, refill buffer descriptor, if possible
235 static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
237 pr_debug("CPM uart[%d]:TX INT\n", port->line);
239 cpm_uart_tx_pump(port);
245 static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
248 unsigned char ch, *cp;
249 struct tty_struct *tty = port->info->tty;
250 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
255 pr_debug("CPM uart[%d]:RX INT\n", port->line);
257 /* Just loop through the closed BDs and copy the characters into
263 status = bdp->cbd_sc;
264 /* If this one is empty, return happy */
265 if (status & BD_SC_EMPTY)
268 /* get number of characters, and check spce in flip-buffer */
271 /* If we have not enough room in tty flip buffer, then we try
272 * later, which will be the next rx-interrupt or a timeout
274 if(tty_buffer_request_room(tty, i) < i) {
275 printk(KERN_WARNING "No room in flip buffer\n");
280 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
282 /* loop through the buffer */
289 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
291 if (uart_handle_sysrq_char(port, ch, regs))
295 tty_insert_flip_char(tty, ch, flg);
297 } /* End while (i--) */
299 /* This BD is ready to be used again. Clear status. get next */
300 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
301 bdp->cbd_sc |= BD_SC_EMPTY;
303 if (bdp->cbd_sc & BD_SC_WRAP)
304 bdp = pinfo->rx_bd_base;
310 /* Write back buffer pointer */
311 pinfo->rx_cur = (volatile cbd_t *) bdp;
313 /* activate BH processing */
314 tty_flip_buffer_push(tty);
318 /* Error processing */
322 if (status & BD_SC_BR)
324 if (status & BD_SC_PR)
325 port->icount.parity++;
326 if (status & BD_SC_FR)
327 port->icount.frame++;
328 if (status & BD_SC_OV)
329 port->icount.overrun++;
331 /* Mask out ignored conditions */
332 status &= port->read_status_mask;
334 /* Handle the remaining ones */
335 if (status & BD_SC_BR)
337 else if (status & BD_SC_PR)
339 else if (status & BD_SC_FR)
342 /* overrun does not affect the current character ! */
343 if (status & BD_SC_OV) {
346 /* We skip this buffer */
347 /* CHECK: Is really nothing senseful there */
348 /* ASSUMPTION: it contains nothing valid */
358 * Asynchron mode interrupt handler
360 static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
363 struct uart_port *port = (struct uart_port *)data;
364 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
365 volatile smc_t *smcp = pinfo->smcp;
366 volatile scc_t *sccp = pinfo->sccp;
368 pr_debug("CPM uart[%d]:IRQ\n", port->line);
371 events = smcp->smc_smce;
372 smcp->smc_smce = events;
373 if (events & SMCM_BRKE)
374 uart_handle_break(port);
375 if (events & SMCM_RX)
376 cpm_uart_int_rx(port, regs);
377 if (events & SMCM_TX)
378 cpm_uart_int_tx(port, regs);
380 events = sccp->scc_scce;
381 sccp->scc_scce = events;
382 if (events & UART_SCCM_BRKE)
383 uart_handle_break(port);
384 if (events & UART_SCCM_RX)
385 cpm_uart_int_rx(port, regs);
386 if (events & UART_SCCM_TX)
387 cpm_uart_int_tx(port, regs);
389 return (events) ? IRQ_HANDLED : IRQ_NONE;
392 static int cpm_uart_startup(struct uart_port *port)
395 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
396 int line = pinfo - cpm_uart_ports;
398 pr_debug("CPM uart[%d]:startup\n", port->line);
400 /* Install interrupt handler. */
401 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
407 pinfo->smcp->smc_smcm |= SMCM_RX;
408 pinfo->smcp->smc_smcmr |= SMCMR_REN;
410 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
413 if (!(pinfo->flags & FLAG_CONSOLE))
414 cpm_line_cr_cmd(line,CPM_CR_INIT_TRX);
418 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
420 set_current_state(TASK_UNINTERRUPTIBLE);
421 schedule_timeout(pinfo->wait_closing);
427 static void cpm_uart_shutdown(struct uart_port *port)
429 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
430 int line = pinfo - cpm_uart_ports;
432 pr_debug("CPM uart[%d]:shutdown\n", port->line);
434 /* free interrupt handler */
435 free_irq(port->irq, port);
437 /* If the port is not the console, disable Rx and Tx. */
438 if (!(pinfo->flags & FLAG_CONSOLE)) {
439 /* Wait for all the BDs marked sent */
440 while(!cpm_uart_tx_empty(port)) {
441 set_current_state(TASK_UNINTERRUPTIBLE);
445 if (pinfo->wait_closing)
446 cpm_uart_wait_until_send(pinfo);
450 volatile smc_t *smcp = pinfo->smcp;
451 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
452 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
454 volatile scc_t *sccp = pinfo->sccp;
455 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
456 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
459 /* Shut them really down and reinit buffer descriptors */
461 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
463 cpm_line_cr_cmd(line, CPM_CR_GRA_STOP_TX);
465 cpm_uart_initbd(pinfo);
469 static void cpm_uart_set_termios(struct uart_port *port,
470 struct termios *termios, struct termios *old)
474 u16 cval, scval, prev_mode;
476 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
477 volatile smc_t *smcp = pinfo->smcp;
478 volatile scc_t *sccp = pinfo->sccp;
480 pr_debug("CPM uart[%d]:set_termios\n", port->line);
482 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
484 /* Character length programmed into the mode register is the
485 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
486 * 1 or 2 stop bits, minus 1.
487 * The value 'bits' counts this for us.
493 switch (termios->c_cflag & CSIZE) {
506 /* Never happens, but GCC is too dumb to figure it out */
513 if (termios->c_cflag & CSTOPB) {
514 cval |= SMCMR_SL; /* Two stops */
515 scval |= SCU_PSMR_SL;
519 if (termios->c_cflag & PARENB) {
521 scval |= SCU_PSMR_PEN;
523 if (!(termios->c_cflag & PARODD)) {
524 cval |= SMCMR_PM_EVEN;
525 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
530 * Set up parity check flag
532 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
534 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
535 if (termios->c_iflag & INPCK)
536 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
537 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
538 port->read_status_mask |= BD_SC_BR;
541 * Characters to ignore
543 port->ignore_status_mask = 0;
544 if (termios->c_iflag & IGNPAR)
545 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
546 if (termios->c_iflag & IGNBRK) {
547 port->ignore_status_mask |= BD_SC_BR;
549 * If we're ignore parity and break indicators, ignore
550 * overruns too. (For real raw support).
552 if (termios->c_iflag & IGNPAR)
553 port->ignore_status_mask |= BD_SC_OV;
556 * !!! ignore all characters if CREAD is not set
558 if ((termios->c_cflag & CREAD) == 0)
559 port->read_status_mask &= ~BD_SC_EMPTY;
561 spin_lock_irqsave(&port->lock, flags);
563 /* Start bit has not been added (so don't, because we would just
564 * subtract it later), and we need to add one for the number of
565 * stops bits (there is always at least one).
569 /* Set the mode register. We want to keep a copy of the
570 * enables, because we want to put them back if they were
573 prev_mode = smcp->smc_smcmr;
574 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
575 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
577 sccp->scc_psmr = (sbits << 12) | scval;
580 cpm_set_brg(pinfo->brg - 1, baud);
581 spin_unlock_irqrestore(&port->lock, flags);
585 static const char *cpm_uart_type(struct uart_port *port)
587 pr_debug("CPM uart[%d]:uart_type\n", port->line);
589 return port->type == PORT_CPM ? "CPM UART" : NULL;
593 * verify the new serial_struct (for TIOCSSERIAL).
595 static int cpm_uart_verify_port(struct uart_port *port,
596 struct serial_struct *ser)
600 pr_debug("CPM uart[%d]:verify_port\n", port->line);
602 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
604 if (ser->irq < 0 || ser->irq >= NR_IRQS)
606 if (ser->baud_base < 9600)
612 * Transmit characters, refill buffer descriptor, if possible
614 static int cpm_uart_tx_pump(struct uart_port *port)
619 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
620 struct circ_buf *xmit = &port->info->xmit;
622 /* Handle xon/xoff */
624 /* Pick next descriptor and fill from buffer */
627 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
631 bdp->cbd_sc |= BD_SC_READY;
633 if (bdp->cbd_sc & BD_SC_WRAP)
634 bdp = pinfo->tx_bd_base;
644 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
645 cpm_uart_stop_tx(port);
649 /* Pick next descriptor and fill from buffer */
652 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
654 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
655 while (count < pinfo->tx_fifosize) {
656 *p++ = xmit->buf[xmit->tail];
657 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
660 if (xmit->head == xmit->tail)
663 bdp->cbd_datlen = count;
664 bdp->cbd_sc |= BD_SC_READY;
667 if (bdp->cbd_sc & BD_SC_WRAP)
668 bdp = pinfo->tx_bd_base;
674 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
675 uart_write_wakeup(port);
677 if (uart_circ_empty(xmit)) {
678 cpm_uart_stop_tx(port);
686 * init buffer descriptors
688 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
694 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
696 /* Set the physical address of the host memory
697 * buffers in the buffer descriptors, and the
698 * virtual address for us to work with.
700 mem_addr = pinfo->mem_addr;
701 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
702 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
703 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
704 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
705 mem_addr += pinfo->rx_fifosize;
708 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
709 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
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 + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
716 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
717 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
718 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
719 bdp->cbd_sc = BD_SC_INTRPT;
720 mem_addr += pinfo->tx_fifosize;
723 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
724 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
727 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
729 int line = pinfo - cpm_uart_ports;
731 volatile scc_uart_t *sup;
733 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
739 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
740 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
742 /* Set up the uart parameters in the
746 cpm_set_scc_fcr(sup);
748 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
749 sup->scc_maxidl = pinfo->rx_fifosize;
758 sup->scc_char1 = 0x8000;
759 sup->scc_char2 = 0x8000;
760 sup->scc_char3 = 0x8000;
761 sup->scc_char4 = 0x8000;
762 sup->scc_char5 = 0x8000;
763 sup->scc_char6 = 0x8000;
764 sup->scc_char7 = 0x8000;
765 sup->scc_char8 = 0x8000;
766 sup->scc_rccm = 0xc0ff;
768 /* Send the CPM an initialize command.
770 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
772 /* Set UART mode, 8 bit, no parity, one stop.
773 * Enable receive and transmit.
777 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
779 /* Enable rx interrupts and clear all pending events. */
781 scp->scc_scce = 0xffff;
782 scp->scc_dsr = 0x7e7e;
783 scp->scc_psmr = 0x3000;
785 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
788 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
790 int line = pinfo - cpm_uart_ports;
792 volatile smc_uart_t *up;
794 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
800 pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
801 pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
804 * In case SMC1 is being relocated...
806 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
807 up->smc_rbptr = pinfo->smcup->smc_rbase;
808 up->smc_tbptr = pinfo->smcup->smc_tbase;
811 up->smc_brkcr = 1; /* number of break chars */
815 /* Set up the uart parameters in the
820 /* Using idle charater time requires some additional tuning. */
821 up->smc_mrblr = pinfo->rx_fifosize;
822 up->smc_maxidl = pinfo->rx_fifosize;
827 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
829 /* Set UART mode, 8 bit, no parity, one stop.
830 * Enable receive and transmit.
832 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
834 /* Enable only rx interrupts clear all pending events. */
838 sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
842 * Initialize port. This is called from early_console stuff
843 * so we have to be careful here !
845 static int cpm_uart_request_port(struct uart_port *port)
847 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
850 pr_debug("CPM uart[%d]:request port\n", port->line);
852 if (pinfo->flags & FLAG_CONSOLE)
856 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
857 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
859 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
860 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
863 ret = cpm_uart_allocbuf(pinfo, 0);
868 cpm_uart_initbd(pinfo);
870 cpm_uart_init_smc(pinfo);
872 cpm_uart_init_scc(pinfo);
877 static void cpm_uart_release_port(struct uart_port *port)
879 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
881 if (!(pinfo->flags & FLAG_CONSOLE))
882 cpm_uart_freebuf(pinfo);
886 * Configure/autoconfigure the port.
888 static void cpm_uart_config_port(struct uart_port *port, int flags)
890 pr_debug("CPM uart[%d]:config_port\n", port->line);
892 if (flags & UART_CONFIG_TYPE) {
893 port->type = PORT_CPM;
894 cpm_uart_request_port(port);
897 static struct uart_ops cpm_uart_pops = {
898 .tx_empty = cpm_uart_tx_empty,
899 .set_mctrl = cpm_uart_set_mctrl,
900 .get_mctrl = cpm_uart_get_mctrl,
901 .stop_tx = cpm_uart_stop_tx,
902 .start_tx = cpm_uart_start_tx,
903 .stop_rx = cpm_uart_stop_rx,
904 .enable_ms = cpm_uart_enable_ms,
905 .break_ctl = cpm_uart_break_ctl,
906 .startup = cpm_uart_startup,
907 .shutdown = cpm_uart_shutdown,
908 .set_termios = cpm_uart_set_termios,
909 .type = cpm_uart_type,
910 .release_port = cpm_uart_release_port,
911 .request_port = cpm_uart_request_port,
912 .config_port = cpm_uart_config_port,
913 .verify_port = cpm_uart_verify_port,
916 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
920 .ops = &cpm_uart_pops,
922 .lock = SPIN_LOCK_UNLOCKED,
925 .tx_nrfifos = TX_NUM_FIFO,
926 .tx_fifosize = TX_BUF_SIZE,
927 .rx_nrfifos = RX_NUM_FIFO,
928 .rx_fifosize = RX_BUF_SIZE,
929 .set_lineif = smc1_lineif,
934 .ops = &cpm_uart_pops,
936 .lock = SPIN_LOCK_UNLOCKED,
939 .tx_nrfifos = TX_NUM_FIFO,
940 .tx_fifosize = TX_BUF_SIZE,
941 .rx_nrfifos = RX_NUM_FIFO,
942 .rx_fifosize = RX_BUF_SIZE,
943 .set_lineif = smc2_lineif,
944 #ifdef CONFIG_SERIAL_CPM_ALT_SMC2
951 .ops = &cpm_uart_pops,
953 .lock = SPIN_LOCK_UNLOCKED,
955 .tx_nrfifos = TX_NUM_FIFO,
956 .tx_fifosize = TX_BUF_SIZE,
957 .rx_nrfifos = RX_NUM_FIFO,
958 .rx_fifosize = RX_BUF_SIZE,
959 .set_lineif = scc1_lineif,
960 .wait_closing = SCC_WAIT_CLOSING,
965 .ops = &cpm_uart_pops,
967 .lock = SPIN_LOCK_UNLOCKED,
969 .tx_nrfifos = TX_NUM_FIFO,
970 .tx_fifosize = TX_BUF_SIZE,
971 .rx_nrfifos = RX_NUM_FIFO,
972 .rx_fifosize = RX_BUF_SIZE,
973 .set_lineif = scc2_lineif,
974 .wait_closing = SCC_WAIT_CLOSING,
979 .ops = &cpm_uart_pops,
981 .lock = SPIN_LOCK_UNLOCKED,
983 .tx_nrfifos = TX_NUM_FIFO,
984 .tx_fifosize = TX_BUF_SIZE,
985 .rx_nrfifos = RX_NUM_FIFO,
986 .rx_fifosize = RX_BUF_SIZE,
987 .set_lineif = scc3_lineif,
988 .wait_closing = SCC_WAIT_CLOSING,
993 .ops = &cpm_uart_pops,
995 .lock = SPIN_LOCK_UNLOCKED,
997 .tx_nrfifos = TX_NUM_FIFO,
998 .tx_fifosize = TX_BUF_SIZE,
999 .rx_nrfifos = RX_NUM_FIFO,
1000 .rx_fifosize = RX_BUF_SIZE,
1001 .set_lineif = scc4_lineif,
1002 .wait_closing = SCC_WAIT_CLOSING,
1006 int cpm_uart_drv_get_platform_data(struct platform_device *pdev, int is_con)
1009 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1010 int idx = pdata->fs_no; /* It is UART_SMCx or UART_SCCx index */
1011 struct uart_cpm_port *pinfo;
1015 for (line=0; line<UART_NR && cpm_uart_port_map[line]!=pdata->fs_no; line++);
1017 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1019 pinfo->brg = pdata->brg;
1022 pinfo->port.line = line;
1023 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1026 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1030 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
1034 if(idx > fsid_smc2_uart) {
1035 pinfo->sccp = (scc_t *)mem;
1036 pinfo->sccup = (scc_uart_t *)pram;
1038 pinfo->smcp = (smc_t *)mem;
1039 pinfo->smcup = (smc_uart_t *)pram;
1041 pinfo->tx_nrfifos = pdata->tx_num_fifo;
1042 pinfo->tx_fifosize = pdata->tx_buf_size;
1044 pinfo->rx_nrfifos = pdata->rx_num_fifo;
1045 pinfo->rx_fifosize = pdata->rx_buf_size;
1047 pinfo->port.uartclk = pdata->uart_clk;
1048 pinfo->port.mapbase = (unsigned long)mem;
1049 pinfo->port.irq = platform_get_irq(pdev, 0);
1054 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1056 * Print a string to the serial port trying not to disturb
1057 * any possible real use of the port...
1059 * Note that this is called with interrupts already disabled
1061 static void cpm_uart_console_write(struct console *co, const char *s,
1064 struct uart_cpm_port *pinfo =
1065 &cpm_uart_ports[cpm_uart_port_map[co->index]];
1067 volatile cbd_t *bdp, *bdbase;
1068 volatile unsigned char *cp;
1070 /* Get the address of the host memory buffer.
1072 bdp = pinfo->tx_cur;
1073 bdbase = pinfo->tx_bd_base;
1076 * Now, do each character. This is not as bad as it looks
1077 * since this is a holding FIFO and not a transmitting FIFO.
1078 * We could add the complexity of filling the entire transmit
1079 * buffer, but we would just wait longer between accesses......
1081 for (i = 0; i < count; i++, s++) {
1082 /* Wait for transmitter fifo to empty.
1083 * Ready indicates output is ready, and xmt is doing
1084 * that, not that it is ready for us to send.
1086 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1089 /* Send the character out.
1090 * If the buffer address is in the CPM DPRAM, don't
1093 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1097 bdp->cbd_datlen = 1;
1098 bdp->cbd_sc |= BD_SC_READY;
1100 if (bdp->cbd_sc & BD_SC_WRAP)
1105 /* if a LF, also do CR... */
1107 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1110 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1113 bdp->cbd_datlen = 1;
1114 bdp->cbd_sc |= BD_SC_READY;
1116 if (bdp->cbd_sc & BD_SC_WRAP)
1124 * Finally, Wait for transmitter & holding register to empty
1125 * and restore the IER
1127 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1130 pinfo->tx_cur = (volatile cbd_t *) bdp;
1134 static int __init cpm_uart_console_setup(struct console *co, char *options)
1136 struct uart_port *port;
1137 struct uart_cpm_port *pinfo;
1144 struct fs_uart_platform_info *pdata;
1145 struct platform_device* pdev = early_uart_get_pdev(co->index);
1148 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1149 pinfo = (struct uart_cpm_port *)port;
1151 pr_info("cpm_uart: console: compat mode\n");
1152 /* compatibility - will be cleaned up */
1153 cpm_uart_init_portdesc();
1155 if (pinfo->set_lineif)
1156 pinfo->set_lineif(pinfo);
1158 pdata = pdev->dev.platform_data;
1160 if (pdata->init_ioports)
1161 pdata->init_ioports();
1163 cpm_uart_drv_get_platform_data(pdev, 1);
1166 pinfo->flags |= FLAG_CONSOLE;
1169 uart_parse_options(options, &baud, &parity, &bits, &flow);
1171 bd_t *bd = (bd_t *) __res;
1173 if (bd->bi_baudrate)
1174 baud = bd->bi_baudrate;
1179 if (IS_SMC(pinfo)) {
1180 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1181 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1183 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1184 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1187 ret = cpm_uart_allocbuf(pinfo, 1);
1192 cpm_uart_initbd(pinfo);
1195 cpm_uart_init_smc(pinfo);
1197 cpm_uart_init_scc(pinfo);
1199 uart_set_options(port, co, baud, parity, bits, flow);
1204 static struct uart_driver cpm_reg;
1205 static struct console cpm_scc_uart_console = {
1207 .write = cpm_uart_console_write,
1208 .device = uart_console_device,
1209 .setup = cpm_uart_console_setup,
1210 .flags = CON_PRINTBUFFER,
1215 int __init cpm_uart_console_init(void)
1217 register_console(&cpm_scc_uart_console);
1221 console_initcall(cpm_uart_console_init);
1223 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1225 #define CPM_UART_CONSOLE NULL
1228 static struct uart_driver cpm_reg = {
1229 .owner = THIS_MODULE,
1230 .driver_name = "ttyCPM",
1231 .dev_name = "ttyCPM",
1232 .major = SERIAL_CPM_MAJOR,
1233 .minor = SERIAL_CPM_MINOR,
1234 .cons = CPM_UART_CONSOLE,
1236 static int cpm_uart_drv_probe(struct device *dev)
1238 struct platform_device *pdev = to_platform_device(dev);
1239 struct fs_uart_platform_info *pdata;
1243 printk(KERN_ERR"CPM UART: platform data missing!\n");
1247 pdata = pdev->dev.platform_data;
1248 pr_debug("cpm_uart_drv_probe: Adding CPM UART %d\n",
1249 cpm_uart_port_map[pdata->fs_no]);
1251 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
1254 if (pdata->init_ioports)
1255 pdata->init_ioports();
1257 ret = uart_add_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1262 static int cpm_uart_drv_remove(struct device *dev)
1264 struct platform_device *pdev = to_platform_device(dev);
1265 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1267 pr_debug("cpm_uart_drv_remove: Removing CPM UART %d\n",
1268 cpm_uart_port_map[pdata->fs_no]);
1270 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1274 static struct device_driver cpm_smc_uart_driver = {
1275 .name = "fsl-cpm-smc:uart",
1276 .bus = &platform_bus_type,
1277 .probe = cpm_uart_drv_probe,
1278 .remove = cpm_uart_drv_remove,
1281 static struct device_driver cpm_scc_uart_driver = {
1282 .name = "fsl-cpm-scc:uart",
1283 .bus = &platform_bus_type,
1284 .probe = cpm_uart_drv_probe,
1285 .remove = cpm_uart_drv_remove,
1289 This is supposed to match uart devices on platform bus,
1291 static int match_is_uart (struct device* dev, void* data)
1293 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
1295 /* this was setfunc as uart */
1296 if(strstr(pdev->name,":uart")) {
1303 static int cpm_uart_init(void) {
1308 printk(KERN_INFO "Serial: CPM driver $Revision: 0.02 $\n");
1310 /* lookup the bus for uart devices */
1311 dev = bus_find_device(&platform_bus_type, NULL, 0, match_is_uart);
1313 /* There are devices on the bus - all should be OK */
1316 cpm_reg.nr = cpm_uart_nr;
1318 if (!(ret = uart_register_driver(&cpm_reg))) {
1319 if ((ret = driver_register(&cpm_smc_uart_driver))) {
1320 uart_unregister_driver(&cpm_reg);
1323 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1324 driver_unregister(&cpm_scc_uart_driver);
1325 uart_unregister_driver(&cpm_reg);
1329 /* No capable platform devices found - falling back to legacy mode */
1330 pr_info("cpm_uart: WARNING: no UART devices found on platform bus!\n");
1332 "cpm_uart: the driver will guess configuration, but this mode is no longer supported.\n");
1333 #ifndef CONFIG_SERIAL_CPM_CONSOLE
1334 ret = cpm_uart_init_portdesc();
1339 cpm_reg.nr = cpm_uart_nr;
1340 ret = uart_register_driver(&cpm_reg);
1345 for (i = 0; i < cpm_uart_nr; i++) {
1346 int con = cpm_uart_port_map[i];
1347 cpm_uart_ports[con].port.line = i;
1348 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1349 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1356 static void __exit cpm_uart_exit(void)
1358 driver_unregister(&cpm_scc_uart_driver);
1359 driver_unregister(&cpm_smc_uart_driver);
1360 uart_unregister_driver(&cpm_reg);
1363 module_init(cpm_uart_init);
1364 module_exit(cpm_uart_exit);
1366 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1367 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1368 MODULE_LICENSE("GPL");
1369 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);