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 /**************************************************************/
75 static inline unsigned long cpu2cpm_addr(void *addr)
77 if ((unsigned long)addr >= CPM_ADDR)
78 return (unsigned long)addr;
79 return virt_to_bus(addr);
82 static inline void *cpm2cpu_addr(unsigned long addr)
86 return bus_to_virt(addr);
89 /* Place-holder for board-specific stuff */
90 struct platform_device* __attribute__ ((weak)) __init
91 early_uart_get_pdev(int index)
97 void cpm_uart_count(void)
100 #ifdef CONFIG_SERIAL_CPM_SMC1
101 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
103 #ifdef CONFIG_SERIAL_CPM_SMC2
104 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
106 #ifdef CONFIG_SERIAL_CPM_SCC1
107 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
109 #ifdef CONFIG_SERIAL_CPM_SCC2
110 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
112 #ifdef CONFIG_SERIAL_CPM_SCC3
113 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
115 #ifdef CONFIG_SERIAL_CPM_SCC4
116 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
121 * Check, if transmit buffers are processed
123 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
125 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
126 volatile cbd_t *bdp = pinfo->tx_bd_base;
130 if (bdp->cbd_sc & BD_SC_READY)
133 if (bdp->cbd_sc & BD_SC_WRAP) {
140 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
145 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
147 /* Whee. Do nothing. */
150 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
152 /* Whee. Do nothing. */
153 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
159 static void cpm_uart_stop_tx(struct uart_port *port)
161 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
162 volatile smc_t *smcp = pinfo->smcp;
163 volatile scc_t *sccp = pinfo->sccp;
165 pr_debug("CPM uart[%d]:stop tx\n", port->line);
168 smcp->smc_smcm &= ~SMCM_TX;
170 sccp->scc_sccm &= ~UART_SCCM_TX;
176 static void cpm_uart_start_tx(struct uart_port *port)
178 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
179 volatile smc_t *smcp = pinfo->smcp;
180 volatile scc_t *sccp = pinfo->sccp;
182 pr_debug("CPM uart[%d]:start tx\n", port->line);
185 if (smcp->smc_smcm & SMCM_TX)
188 if (sccp->scc_sccm & UART_SCCM_TX)
192 if (cpm_uart_tx_pump(port) != 0) {
194 smcp->smc_smcm |= SMCM_TX;
195 smcp->smc_smcmr |= SMCMR_TEN;
197 sccp->scc_sccm |= UART_SCCM_TX;
198 pinfo->sccp->scc_gsmrl |= SCC_GSMRL_ENT;
206 static void cpm_uart_stop_rx(struct uart_port *port)
208 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
209 volatile smc_t *smcp = pinfo->smcp;
210 volatile scc_t *sccp = pinfo->sccp;
212 pr_debug("CPM uart[%d]:stop rx\n", port->line);
215 smcp->smc_smcm &= ~SMCM_RX;
217 sccp->scc_sccm &= ~UART_SCCM_RX;
221 * Enable Modem status interrupts
223 static void cpm_uart_enable_ms(struct uart_port *port)
225 pr_debug("CPM uart[%d]:enable ms\n", port->line);
231 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
233 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
234 int line = pinfo - cpm_uart_ports;
236 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
240 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
242 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
246 * Transmit characters, refill buffer descriptor, if possible
248 static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
250 pr_debug("CPM uart[%d]:TX INT\n", port->line);
252 cpm_uart_tx_pump(port);
258 static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
261 unsigned char ch, *cp;
262 struct tty_struct *tty = port->info->tty;
263 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
268 pr_debug("CPM uart[%d]:RX INT\n", port->line);
270 /* Just loop through the closed BDs and copy the characters into
276 status = bdp->cbd_sc;
277 /* If this one is empty, return happy */
278 if (status & BD_SC_EMPTY)
281 /* get number of characters, and check spce in flip-buffer */
284 /* If we have not enough room in tty flip buffer, then we try
285 * later, which will be the next rx-interrupt or a timeout
287 if(tty_buffer_request_room(tty, i) < i) {
288 printk(KERN_WARNING "No room in flip buffer\n");
293 cp = cpm2cpu_addr(bdp->cbd_bufaddr);
295 /* loop through the buffer */
302 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
304 if (uart_handle_sysrq_char(port, ch, regs))
308 tty_insert_flip_char(tty, ch, flg);
310 } /* End while (i--) */
312 /* This BD is ready to be used again. Clear status. get next */
313 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
314 bdp->cbd_sc |= BD_SC_EMPTY;
316 if (bdp->cbd_sc & BD_SC_WRAP)
317 bdp = pinfo->rx_bd_base;
323 /* Write back buffer pointer */
324 pinfo->rx_cur = (volatile cbd_t *) bdp;
326 /* activate BH processing */
327 tty_flip_buffer_push(tty);
331 /* Error processing */
335 if (status & BD_SC_BR)
337 if (status & BD_SC_PR)
338 port->icount.parity++;
339 if (status & BD_SC_FR)
340 port->icount.frame++;
341 if (status & BD_SC_OV)
342 port->icount.overrun++;
344 /* Mask out ignored conditions */
345 status &= port->read_status_mask;
347 /* Handle the remaining ones */
348 if (status & BD_SC_BR)
350 else if (status & BD_SC_PR)
352 else if (status & BD_SC_FR)
355 /* overrun does not affect the current character ! */
356 if (status & BD_SC_OV) {
359 /* We skip this buffer */
360 /* CHECK: Is really nothing senseful there */
361 /* ASSUMPTION: it contains nothing valid */
371 * Asynchron mode interrupt handler
373 static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
376 struct uart_port *port = (struct uart_port *)data;
377 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
378 volatile smc_t *smcp = pinfo->smcp;
379 volatile scc_t *sccp = pinfo->sccp;
381 pr_debug("CPM uart[%d]:IRQ\n", port->line);
384 events = smcp->smc_smce;
385 smcp->smc_smce = events;
386 if (events & SMCM_BRKE)
387 uart_handle_break(port);
388 if (events & SMCM_RX)
389 cpm_uart_int_rx(port, regs);
390 if (events & SMCM_TX)
391 cpm_uart_int_tx(port, regs);
393 events = sccp->scc_scce;
394 sccp->scc_scce = events;
395 if (events & UART_SCCM_BRKE)
396 uart_handle_break(port);
397 if (events & UART_SCCM_RX)
398 cpm_uart_int_rx(port, regs);
399 if (events & UART_SCCM_TX)
400 cpm_uart_int_tx(port, regs);
402 return (events) ? IRQ_HANDLED : IRQ_NONE;
405 static int cpm_uart_startup(struct uart_port *port)
408 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
409 int line = pinfo - cpm_uart_ports;
411 pr_debug("CPM uart[%d]:startup\n", port->line);
413 /* Install interrupt handler. */
414 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
420 pinfo->smcp->smc_smcm |= SMCM_RX;
421 pinfo->smcp->smc_smcmr |= SMCMR_REN;
423 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
426 if (!(pinfo->flags & FLAG_CONSOLE))
427 cpm_line_cr_cmd(line,CPM_CR_INIT_TRX);
431 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
433 set_current_state(TASK_UNINTERRUPTIBLE);
434 schedule_timeout(pinfo->wait_closing);
440 static void cpm_uart_shutdown(struct uart_port *port)
442 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
443 int line = pinfo - cpm_uart_ports;
445 pr_debug("CPM uart[%d]:shutdown\n", port->line);
447 /* free interrupt handler */
448 free_irq(port->irq, port);
450 /* If the port is not the console, disable Rx and Tx. */
451 if (!(pinfo->flags & FLAG_CONSOLE)) {
452 /* Wait for all the BDs marked sent */
453 while(!cpm_uart_tx_empty(port)) {
454 set_current_state(TASK_UNINTERRUPTIBLE);
458 if (pinfo->wait_closing)
459 cpm_uart_wait_until_send(pinfo);
463 volatile smc_t *smcp = pinfo->smcp;
464 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
465 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
467 volatile scc_t *sccp = pinfo->sccp;
468 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
469 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
472 /* Shut them really down and reinit buffer descriptors */
473 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
474 cpm_uart_initbd(pinfo);
478 static void cpm_uart_set_termios(struct uart_port *port,
479 struct termios *termios, struct termios *old)
483 u16 cval, scval, prev_mode;
485 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
486 volatile smc_t *smcp = pinfo->smcp;
487 volatile scc_t *sccp = pinfo->sccp;
489 pr_debug("CPM uart[%d]:set_termios\n", port->line);
491 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
493 /* Character length programmed into the mode register is the
494 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
495 * 1 or 2 stop bits, minus 1.
496 * The value 'bits' counts this for us.
502 switch (termios->c_cflag & CSIZE) {
515 /* Never happens, but GCC is too dumb to figure it out */
522 if (termios->c_cflag & CSTOPB) {
523 cval |= SMCMR_SL; /* Two stops */
524 scval |= SCU_PSMR_SL;
528 if (termios->c_cflag & PARENB) {
530 scval |= SCU_PSMR_PEN;
532 if (!(termios->c_cflag & PARODD)) {
533 cval |= SMCMR_PM_EVEN;
534 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
539 * Set up parity check flag
541 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
543 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
544 if (termios->c_iflag & INPCK)
545 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
546 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
547 port->read_status_mask |= BD_SC_BR;
550 * Characters to ignore
552 port->ignore_status_mask = 0;
553 if (termios->c_iflag & IGNPAR)
554 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
555 if (termios->c_iflag & IGNBRK) {
556 port->ignore_status_mask |= BD_SC_BR;
558 * If we're ignore parity and break indicators, ignore
559 * overruns too. (For real raw support).
561 if (termios->c_iflag & IGNPAR)
562 port->ignore_status_mask |= BD_SC_OV;
565 * !!! ignore all characters if CREAD is not set
567 if ((termios->c_cflag & CREAD) == 0)
568 port->read_status_mask &= ~BD_SC_EMPTY;
570 spin_lock_irqsave(&port->lock, flags);
572 /* Start bit has not been added (so don't, because we would just
573 * subtract it later), and we need to add one for the number of
574 * stops bits (there is always at least one).
578 /* Set the mode register. We want to keep a copy of the
579 * enables, because we want to put them back if they were
582 prev_mode = smcp->smc_smcmr;
583 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
584 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
586 sccp->scc_psmr = (sbits << 12) | scval;
589 cpm_set_brg(pinfo->brg - 1, baud);
590 spin_unlock_irqrestore(&port->lock, flags);
594 static const char *cpm_uart_type(struct uart_port *port)
596 pr_debug("CPM uart[%d]:uart_type\n", port->line);
598 return port->type == PORT_CPM ? "CPM UART" : NULL;
602 * verify the new serial_struct (for TIOCSSERIAL).
604 static int cpm_uart_verify_port(struct uart_port *port,
605 struct serial_struct *ser)
609 pr_debug("CPM uart[%d]:verify_port\n", port->line);
611 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
613 if (ser->irq < 0 || ser->irq >= NR_IRQS)
615 if (ser->baud_base < 9600)
621 * Transmit characters, refill buffer descriptor, if possible
623 static int cpm_uart_tx_pump(struct uart_port *port)
628 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
629 struct circ_buf *xmit = &port->info->xmit;
631 /* Handle xon/xoff */
633 /* Pick next descriptor and fill from buffer */
636 p = cpm2cpu_addr(bdp->cbd_bufaddr);
640 bdp->cbd_sc |= BD_SC_READY;
642 if (bdp->cbd_sc & BD_SC_WRAP)
643 bdp = pinfo->tx_bd_base;
653 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
654 cpm_uart_stop_tx(port);
658 /* Pick next descriptor and fill from buffer */
661 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
663 p = cpm2cpu_addr(bdp->cbd_bufaddr);
664 while (count < pinfo->tx_fifosize) {
665 *p++ = xmit->buf[xmit->tail];
666 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
669 if (xmit->head == xmit->tail)
672 bdp->cbd_datlen = count;
673 bdp->cbd_sc |= BD_SC_READY;
676 if (bdp->cbd_sc & BD_SC_WRAP)
677 bdp = pinfo->tx_bd_base;
683 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
684 uart_write_wakeup(port);
686 if (uart_circ_empty(xmit)) {
687 cpm_uart_stop_tx(port);
695 * init buffer descriptors
697 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
703 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
705 /* Set the physical address of the host memory
706 * buffers in the buffer descriptors, and the
707 * virtual address for us to work with.
709 mem_addr = pinfo->mem_addr;
710 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
711 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
712 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
713 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
714 mem_addr += pinfo->rx_fifosize;
717 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
718 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
720 /* Set the physical address of the host memory
721 * buffers in the buffer descriptors, and the
722 * virtual address for us to work with.
724 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
725 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
726 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
727 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
728 bdp->cbd_sc = BD_SC_INTRPT;
729 mem_addr += pinfo->tx_fifosize;
732 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
733 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
736 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
738 int line = pinfo - cpm_uart_ports;
740 volatile scc_uart_t *sup;
742 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
748 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
749 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
751 /* Set up the uart parameters in the
755 cpm_set_scc_fcr(sup);
757 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
758 sup->scc_maxidl = pinfo->rx_fifosize;
767 sup->scc_char1 = 0x8000;
768 sup->scc_char2 = 0x8000;
769 sup->scc_char3 = 0x8000;
770 sup->scc_char4 = 0x8000;
771 sup->scc_char5 = 0x8000;
772 sup->scc_char6 = 0x8000;
773 sup->scc_char7 = 0x8000;
774 sup->scc_char8 = 0x8000;
775 sup->scc_rccm = 0xc0ff;
777 /* Send the CPM an initialize command.
779 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
781 /* Set UART mode, 8 bit, no parity, one stop.
782 * Enable receive and transmit.
786 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
788 /* Enable rx interrupts and clear all pending events. */
790 scp->scc_scce = 0xffff;
791 scp->scc_dsr = 0x7e7e;
792 scp->scc_psmr = 0x3000;
794 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
797 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
799 int line = pinfo - cpm_uart_ports;
801 volatile smc_uart_t *up;
803 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
809 pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
810 pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
813 * In case SMC1 is being relocated...
815 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
816 up->smc_rbptr = pinfo->smcup->smc_rbase;
817 up->smc_tbptr = pinfo->smcup->smc_tbase;
820 up->smc_brkcr = 1; /* number of break chars */
824 /* Set up the uart parameters in the
829 /* Using idle charater time requires some additional tuning. */
830 up->smc_mrblr = pinfo->rx_fifosize;
831 up->smc_maxidl = pinfo->rx_fifosize;
836 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
838 /* Set UART mode, 8 bit, no parity, one stop.
839 * Enable receive and transmit.
841 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
843 /* Enable only rx interrupts clear all pending events. */
847 sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
851 * Initialize port. This is called from early_console stuff
852 * so we have to be careful here !
854 static int cpm_uart_request_port(struct uart_port *port)
856 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
859 pr_debug("CPM uart[%d]:request port\n", port->line);
861 if (pinfo->flags & FLAG_CONSOLE)
865 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
866 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
868 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
869 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
872 ret = cpm_uart_allocbuf(pinfo, 0);
877 cpm_uart_initbd(pinfo);
879 cpm_uart_init_smc(pinfo);
881 cpm_uart_init_scc(pinfo);
886 static void cpm_uart_release_port(struct uart_port *port)
888 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
890 if (!(pinfo->flags & FLAG_CONSOLE))
891 cpm_uart_freebuf(pinfo);
895 * Configure/autoconfigure the port.
897 static void cpm_uart_config_port(struct uart_port *port, int flags)
899 pr_debug("CPM uart[%d]:config_port\n", port->line);
901 if (flags & UART_CONFIG_TYPE) {
902 port->type = PORT_CPM;
903 cpm_uart_request_port(port);
906 static struct uart_ops cpm_uart_pops = {
907 .tx_empty = cpm_uart_tx_empty,
908 .set_mctrl = cpm_uart_set_mctrl,
909 .get_mctrl = cpm_uart_get_mctrl,
910 .stop_tx = cpm_uart_stop_tx,
911 .start_tx = cpm_uart_start_tx,
912 .stop_rx = cpm_uart_stop_rx,
913 .enable_ms = cpm_uart_enable_ms,
914 .break_ctl = cpm_uart_break_ctl,
915 .startup = cpm_uart_startup,
916 .shutdown = cpm_uart_shutdown,
917 .set_termios = cpm_uart_set_termios,
918 .type = cpm_uart_type,
919 .release_port = cpm_uart_release_port,
920 .request_port = cpm_uart_request_port,
921 .config_port = cpm_uart_config_port,
922 .verify_port = cpm_uart_verify_port,
925 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
929 .ops = &cpm_uart_pops,
931 .lock = SPIN_LOCK_UNLOCKED,
934 .tx_nrfifos = TX_NUM_FIFO,
935 .tx_fifosize = TX_BUF_SIZE,
936 .rx_nrfifos = RX_NUM_FIFO,
937 .rx_fifosize = RX_BUF_SIZE,
938 .set_lineif = smc1_lineif,
943 .ops = &cpm_uart_pops,
945 .lock = SPIN_LOCK_UNLOCKED,
948 .tx_nrfifos = TX_NUM_FIFO,
949 .tx_fifosize = TX_BUF_SIZE,
950 .rx_nrfifos = RX_NUM_FIFO,
951 .rx_fifosize = RX_BUF_SIZE,
952 .set_lineif = smc2_lineif,
953 #ifdef CONFIG_SERIAL_CPM_ALT_SMC2
960 .ops = &cpm_uart_pops,
962 .lock = SPIN_LOCK_UNLOCKED,
964 .tx_nrfifos = TX_NUM_FIFO,
965 .tx_fifosize = TX_BUF_SIZE,
966 .rx_nrfifos = RX_NUM_FIFO,
967 .rx_fifosize = RX_BUF_SIZE,
968 .set_lineif = scc1_lineif,
969 .wait_closing = SCC_WAIT_CLOSING,
974 .ops = &cpm_uart_pops,
976 .lock = SPIN_LOCK_UNLOCKED,
978 .tx_nrfifos = TX_NUM_FIFO,
979 .tx_fifosize = TX_BUF_SIZE,
980 .rx_nrfifos = RX_NUM_FIFO,
981 .rx_fifosize = RX_BUF_SIZE,
982 .set_lineif = scc2_lineif,
983 .wait_closing = SCC_WAIT_CLOSING,
988 .ops = &cpm_uart_pops,
990 .lock = SPIN_LOCK_UNLOCKED,
992 .tx_nrfifos = TX_NUM_FIFO,
993 .tx_fifosize = TX_BUF_SIZE,
994 .rx_nrfifos = RX_NUM_FIFO,
995 .rx_fifosize = RX_BUF_SIZE,
996 .set_lineif = scc3_lineif,
997 .wait_closing = SCC_WAIT_CLOSING,
1002 .ops = &cpm_uart_pops,
1004 .lock = SPIN_LOCK_UNLOCKED,
1006 .tx_nrfifos = TX_NUM_FIFO,
1007 .tx_fifosize = TX_BUF_SIZE,
1008 .rx_nrfifos = RX_NUM_FIFO,
1009 .rx_fifosize = RX_BUF_SIZE,
1010 .set_lineif = scc4_lineif,
1011 .wait_closing = SCC_WAIT_CLOSING,
1015 int cpm_uart_drv_get_platform_data(struct platform_device *pdev, int is_con)
1018 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1019 int idx = pdata->fs_no; /* It is UART_SMCx or UART_SCCx index */
1020 struct uart_cpm_port *pinfo;
1024 for (line=0; line<UART_NR && cpm_uart_port_map[line]!=pdata->fs_no; line++);
1026 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1028 pinfo->brg = pdata->brg;
1031 pinfo->port.line = line;
1032 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1035 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1039 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
1043 if(idx > fsid_smc2_uart) {
1044 pinfo->sccp = (scc_t *)mem;
1045 pinfo->sccup = (scc_uart_t *)pram;
1047 pinfo->smcp = (smc_t *)mem;
1048 pinfo->smcup = (smc_uart_t *)pram;
1050 pinfo->tx_nrfifos = pdata->tx_num_fifo;
1051 pinfo->tx_fifosize = pdata->tx_buf_size;
1053 pinfo->rx_nrfifos = pdata->rx_num_fifo;
1054 pinfo->rx_fifosize = pdata->rx_buf_size;
1056 pinfo->port.uartclk = pdata->uart_clk;
1057 pinfo->port.mapbase = (unsigned long)mem;
1058 pinfo->port.irq = platform_get_irq(pdev, 0);
1063 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1065 * Print a string to the serial port trying not to disturb
1066 * any possible real use of the port...
1068 * Note that this is called with interrupts already disabled
1070 static void cpm_uart_console_write(struct console *co, const char *s,
1073 struct uart_cpm_port *pinfo =
1074 &cpm_uart_ports[cpm_uart_port_map[co->index]];
1076 volatile cbd_t *bdp, *bdbase;
1077 volatile unsigned char *cp;
1079 /* Get the address of the host memory buffer.
1081 bdp = pinfo->tx_cur;
1082 bdbase = pinfo->tx_bd_base;
1085 * Now, do each character. This is not as bad as it looks
1086 * since this is a holding FIFO and not a transmitting FIFO.
1087 * We could add the complexity of filling the entire transmit
1088 * buffer, but we would just wait longer between accesses......
1090 for (i = 0; i < count; i++, s++) {
1091 /* Wait for transmitter fifo to empty.
1092 * Ready indicates output is ready, and xmt is doing
1093 * that, not that it is ready for us to send.
1095 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1098 /* Send the character out.
1099 * If the buffer address is in the CPM DPRAM, don't
1102 cp = cpm2cpu_addr(bdp->cbd_bufaddr);
1106 bdp->cbd_datlen = 1;
1107 bdp->cbd_sc |= BD_SC_READY;
1109 if (bdp->cbd_sc & BD_SC_WRAP)
1114 /* if a LF, also do CR... */
1116 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1119 cp = cpm2cpu_addr(bdp->cbd_bufaddr);
1122 bdp->cbd_datlen = 1;
1123 bdp->cbd_sc |= BD_SC_READY;
1125 if (bdp->cbd_sc & BD_SC_WRAP)
1133 * Finally, Wait for transmitter & holding register to empty
1134 * and restore the IER
1136 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1139 pinfo->tx_cur = (volatile cbd_t *) bdp;
1143 static int __init cpm_uart_console_setup(struct console *co, char *options)
1145 struct uart_port *port;
1146 struct uart_cpm_port *pinfo;
1153 struct fs_uart_platform_info *pdata;
1154 struct platform_device* pdev = early_uart_get_pdev(co->index);
1157 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1158 pinfo = (struct uart_cpm_port *)port;
1160 pr_info("cpm_uart: console: compat mode\n");
1161 /* compatibility - will be cleaned up */
1162 cpm_uart_init_portdesc();
1164 if (pinfo->set_lineif)
1165 pinfo->set_lineif(pinfo);
1167 pdata = pdev->dev.platform_data;
1169 if (pdata->init_ioports)
1170 pdata->init_ioports();
1172 cpm_uart_drv_get_platform_data(pdev, 1);
1175 pinfo->flags |= FLAG_CONSOLE;
1178 uart_parse_options(options, &baud, &parity, &bits, &flow);
1180 bd_t *bd = (bd_t *) __res;
1182 if (bd->bi_baudrate)
1183 baud = bd->bi_baudrate;
1188 if (IS_SMC(pinfo)) {
1189 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1190 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1192 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1193 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1196 ret = cpm_uart_allocbuf(pinfo, 1);
1201 cpm_uart_initbd(pinfo);
1204 cpm_uart_init_smc(pinfo);
1206 cpm_uart_init_scc(pinfo);
1208 uart_set_options(port, co, baud, parity, bits, flow);
1213 static struct uart_driver cpm_reg;
1214 static struct console cpm_scc_uart_console = {
1216 .write = cpm_uart_console_write,
1217 .device = uart_console_device,
1218 .setup = cpm_uart_console_setup,
1219 .flags = CON_PRINTBUFFER,
1224 int __init cpm_uart_console_init(void)
1226 register_console(&cpm_scc_uart_console);
1230 console_initcall(cpm_uart_console_init);
1232 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1234 #define CPM_UART_CONSOLE NULL
1237 static struct uart_driver cpm_reg = {
1238 .owner = THIS_MODULE,
1239 .driver_name = "ttyCPM",
1240 .dev_name = "ttyCPM",
1241 .major = SERIAL_CPM_MAJOR,
1242 .minor = SERIAL_CPM_MINOR,
1243 .cons = CPM_UART_CONSOLE,
1245 static int cpm_uart_drv_probe(struct device *dev)
1247 struct platform_device *pdev = to_platform_device(dev);
1248 struct fs_uart_platform_info *pdata;
1252 printk(KERN_ERR"CPM UART: platform data missing!\n");
1256 pdata = pdev->dev.platform_data;
1257 pr_debug("cpm_uart_drv_probe: Adding CPM UART %d\n",
1258 cpm_uart_port_map[pdata->fs_no]);
1260 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
1263 if (pdata->init_ioports)
1264 pdata->init_ioports();
1266 ret = uart_add_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1271 static int cpm_uart_drv_remove(struct device *dev)
1273 struct platform_device *pdev = to_platform_device(dev);
1274 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1276 pr_debug("cpm_uart_drv_remove: Removing CPM UART %d\n",
1277 cpm_uart_port_map[pdata->fs_no]);
1279 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1283 static struct device_driver cpm_smc_uart_driver = {
1284 .name = "fsl-cpm-smc:uart",
1285 .bus = &platform_bus_type,
1286 .probe = cpm_uart_drv_probe,
1287 .remove = cpm_uart_drv_remove,
1290 static struct device_driver cpm_scc_uart_driver = {
1291 .name = "fsl-cpm-scc:uart",
1292 .bus = &platform_bus_type,
1293 .probe = cpm_uart_drv_probe,
1294 .remove = cpm_uart_drv_remove,
1298 This is supposed to match uart devices on platform bus,
1300 static int match_is_uart (struct device* dev, void* data)
1302 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
1304 /* this was setfunc as uart */
1305 if(strstr(pdev->name,":uart")) {
1312 static int cpm_uart_init(void) {
1317 printk(KERN_INFO "Serial: CPM driver $Revision: 0.02 $\n");
1319 /* lookup the bus for uart devices */
1320 dev = bus_find_device(&platform_bus_type, NULL, 0, match_is_uart);
1322 /* There are devices on the bus - all should be OK */
1325 cpm_reg.nr = cpm_uart_nr;
1327 if (!(ret = uart_register_driver(&cpm_reg))) {
1328 if ((ret = driver_register(&cpm_smc_uart_driver))) {
1329 uart_unregister_driver(&cpm_reg);
1332 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1333 driver_unregister(&cpm_scc_uart_driver);
1334 uart_unregister_driver(&cpm_reg);
1338 /* No capable platform devices found - falling back to legacy mode */
1339 pr_info("cpm_uart: WARNING: no UART devices found on platform bus!\n");
1341 "cpm_uart: the driver will guess configuration, but this mode is no longer supported.\n");
1342 #ifndef CONFIG_SERIAL_CPM_CONSOLE
1343 ret = cpm_uart_init_portdesc();
1348 cpm_reg.nr = cpm_uart_nr;
1349 ret = uart_register_driver(&cpm_reg);
1354 for (i = 0; i < cpm_uart_nr; i++) {
1355 int con = cpm_uart_port_map[i];
1356 cpm_uart_ports[con].port.line = i;
1357 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1358 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1365 static void __exit cpm_uart_exit(void)
1367 driver_unregister(&cpm_scc_uart_driver);
1368 driver_unregister(&cpm_smc_uart_driver);
1369 uart_unregister_driver(&cpm_reg);
1372 module_init(cpm_uart_init);
1373 module_exit(cpm_uart_exit);
1375 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1376 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1377 MODULE_LICENSE("GPL");
1378 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);