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>
45 #include <linux/of_platform.h>
49 #include <asm/delay.h>
50 #include <asm/fs_pd.h>
53 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57 #include <linux/serial_core.h>
58 #include <linux/kernel.h>
63 /**************************************************************/
65 static int cpm_uart_tx_pump(struct uart_port *port);
66 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
67 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
68 static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
70 /**************************************************************/
73 * Check, if transmit buffers are processed
75 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
77 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
78 cbd_t __iomem *bdp = pinfo->tx_bd_base;
82 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
85 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
92 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
97 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
99 /* Whee. Do nothing. */
102 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
104 /* Whee. Do nothing. */
105 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
111 static void cpm_uart_stop_tx(struct uart_port *port)
113 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114 smc_t __iomem *smcp = pinfo->smcp;
115 scc_t __iomem *sccp = pinfo->sccp;
117 pr_debug("CPM uart[%d]:stop tx\n", port->line);
120 clrbits8(&smcp->smc_smcm, SMCM_TX);
122 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
128 static void cpm_uart_start_tx(struct uart_port *port)
130 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
131 smc_t __iomem *smcp = pinfo->smcp;
132 scc_t __iomem *sccp = pinfo->sccp;
134 pr_debug("CPM uart[%d]:start tx\n", port->line);
137 if (in_8(&smcp->smc_smcm) & SMCM_TX)
140 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
144 if (cpm_uart_tx_pump(port) != 0) {
146 setbits8(&smcp->smc_smcm, SMCM_TX);
148 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
156 static void cpm_uart_stop_rx(struct uart_port *port)
158 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
159 smc_t __iomem *smcp = pinfo->smcp;
160 scc_t __iomem *sccp = pinfo->sccp;
162 pr_debug("CPM uart[%d]:stop rx\n", port->line);
165 clrbits8(&smcp->smc_smcm, SMCM_RX);
167 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
171 * Enable Modem status interrupts
173 static void cpm_uart_enable_ms(struct uart_port *port)
175 pr_debug("CPM uart[%d]:enable ms\n", port->line);
181 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
183 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
185 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
189 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
191 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
195 * Transmit characters, refill buffer descriptor, if possible
197 static void cpm_uart_int_tx(struct uart_port *port)
199 pr_debug("CPM uart[%d]:TX INT\n", port->line);
201 cpm_uart_tx_pump(port);
207 static void cpm_uart_int_rx(struct uart_port *port)
212 struct tty_struct *tty = port->info->tty;
213 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
218 pr_debug("CPM uart[%d]:RX INT\n", port->line);
220 /* Just loop through the closed BDs and copy the characters into
226 status = in_be16(&bdp->cbd_sc);
227 /* If this one is empty, return happy */
228 if (status & BD_SC_EMPTY)
231 /* get number of characters, and check spce in flip-buffer */
232 i = in_be16(&bdp->cbd_datlen);
234 /* If we have not enough room in tty flip buffer, then we try
235 * later, which will be the next rx-interrupt or a timeout
237 if(tty_buffer_request_room(tty, i) < i) {
238 printk(KERN_WARNING "No room in flip buffer\n");
243 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
245 /* loop through the buffer */
252 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
254 if (uart_handle_sysrq_char(port, ch))
258 tty_insert_flip_char(tty, ch, flg);
260 } /* End while (i--) */
262 /* This BD is ready to be used again. Clear status. get next */
263 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
264 BD_SC_OV | BD_SC_ID);
265 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
267 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
268 bdp = pinfo->rx_bd_base;
274 /* Write back buffer pointer */
277 /* activate BH processing */
278 tty_flip_buffer_push(tty);
282 /* Error processing */
286 if (status & BD_SC_BR)
288 if (status & BD_SC_PR)
289 port->icount.parity++;
290 if (status & BD_SC_FR)
291 port->icount.frame++;
292 if (status & BD_SC_OV)
293 port->icount.overrun++;
295 /* Mask out ignored conditions */
296 status &= port->read_status_mask;
298 /* Handle the remaining ones */
299 if (status & BD_SC_BR)
301 else if (status & BD_SC_PR)
303 else if (status & BD_SC_FR)
306 /* overrun does not affect the current character ! */
307 if (status & BD_SC_OV) {
310 /* We skip this buffer */
311 /* CHECK: Is really nothing senseful there */
312 /* ASSUMPTION: it contains nothing valid */
322 * Asynchron mode interrupt handler
324 static irqreturn_t cpm_uart_int(int irq, void *data)
327 struct uart_port *port = data;
328 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
329 smc_t __iomem *smcp = pinfo->smcp;
330 scc_t __iomem *sccp = pinfo->sccp;
332 pr_debug("CPM uart[%d]:IRQ\n", port->line);
335 events = in_8(&smcp->smc_smce);
336 out_8(&smcp->smc_smce, events);
337 if (events & SMCM_BRKE)
338 uart_handle_break(port);
339 if (events & SMCM_RX)
340 cpm_uart_int_rx(port);
341 if (events & SMCM_TX)
342 cpm_uart_int_tx(port);
344 events = in_be16(&sccp->scc_scce);
345 out_be16(&sccp->scc_scce, events);
346 if (events & UART_SCCM_BRKE)
347 uart_handle_break(port);
348 if (events & UART_SCCM_RX)
349 cpm_uart_int_rx(port);
350 if (events & UART_SCCM_TX)
351 cpm_uart_int_tx(port);
353 return (events) ? IRQ_HANDLED : IRQ_NONE;
356 static int cpm_uart_startup(struct uart_port *port)
359 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
361 pr_debug("CPM uart[%d]:startup\n", port->line);
363 /* Install interrupt handler. */
364 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
370 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
371 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
373 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
374 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
377 if (!(pinfo->flags & FLAG_CONSOLE))
378 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
382 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
384 set_current_state(TASK_UNINTERRUPTIBLE);
385 schedule_timeout(pinfo->wait_closing);
391 static void cpm_uart_shutdown(struct uart_port *port)
393 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
395 pr_debug("CPM uart[%d]:shutdown\n", port->line);
397 /* free interrupt handler */
398 free_irq(port->irq, port);
400 /* If the port is not the console, disable Rx and Tx. */
401 if (!(pinfo->flags & FLAG_CONSOLE)) {
402 /* Wait for all the BDs marked sent */
403 while(!cpm_uart_tx_empty(port)) {
404 set_current_state(TASK_UNINTERRUPTIBLE);
408 if (pinfo->wait_closing)
409 cpm_uart_wait_until_send(pinfo);
413 smc_t __iomem *smcp = pinfo->smcp;
414 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
415 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
417 scc_t __iomem *sccp = pinfo->sccp;
418 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
419 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
422 /* Shut them really down and reinit buffer descriptors */
424 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
426 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
428 cpm_uart_initbd(pinfo);
432 static void cpm_uart_set_termios(struct uart_port *port,
433 struct ktermios *termios,
434 struct ktermios *old)
438 u16 cval, scval, prev_mode;
440 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
441 smc_t __iomem *smcp = pinfo->smcp;
442 scc_t __iomem *sccp = pinfo->sccp;
444 pr_debug("CPM uart[%d]:set_termios\n", port->line);
446 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
448 /* Character length programmed into the mode register is the
449 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
450 * 1 or 2 stop bits, minus 1.
451 * The value 'bits' counts this for us.
457 switch (termios->c_cflag & CSIZE) {
470 /* Never happens, but GCC is too dumb to figure it out */
477 if (termios->c_cflag & CSTOPB) {
478 cval |= SMCMR_SL; /* Two stops */
479 scval |= SCU_PSMR_SL;
483 if (termios->c_cflag & PARENB) {
485 scval |= SCU_PSMR_PEN;
487 if (!(termios->c_cflag & PARODD)) {
488 cval |= SMCMR_PM_EVEN;
489 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
494 * Set up parity check flag
496 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
498 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
499 if (termios->c_iflag & INPCK)
500 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
501 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
502 port->read_status_mask |= BD_SC_BR;
505 * Characters to ignore
507 port->ignore_status_mask = 0;
508 if (termios->c_iflag & IGNPAR)
509 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
510 if (termios->c_iflag & IGNBRK) {
511 port->ignore_status_mask |= BD_SC_BR;
513 * If we're ignore parity and break indicators, ignore
514 * overruns too. (For real raw support).
516 if (termios->c_iflag & IGNPAR)
517 port->ignore_status_mask |= BD_SC_OV;
520 * !!! ignore all characters if CREAD is not set
522 if ((termios->c_cflag & CREAD) == 0)
523 port->read_status_mask &= ~BD_SC_EMPTY;
525 spin_lock_irqsave(&port->lock, flags);
527 /* Start bit has not been added (so don't, because we would just
528 * subtract it later), and we need to add one for the number of
529 * stops bits (there is always at least one).
533 /* Set the mode register. We want to keep a copy of the
534 * enables, because we want to put them back if they were
537 prev_mode = in_be16(&smcp->smc_smcmr);
538 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval | SMCMR_SM_UART);
539 setbits16(&smcp->smc_smcmr, (prev_mode & (SMCMR_REN | SMCMR_TEN)));
541 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
544 cpm_set_brg(pinfo->brg - 1, baud);
545 spin_unlock_irqrestore(&port->lock, flags);
548 static const char *cpm_uart_type(struct uart_port *port)
550 pr_debug("CPM uart[%d]:uart_type\n", port->line);
552 return port->type == PORT_CPM ? "CPM UART" : NULL;
556 * verify the new serial_struct (for TIOCSSERIAL).
558 static int cpm_uart_verify_port(struct uart_port *port,
559 struct serial_struct *ser)
563 pr_debug("CPM uart[%d]:verify_port\n", port->line);
565 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
567 if (ser->irq < 0 || ser->irq >= NR_IRQS)
569 if (ser->baud_base < 9600)
575 * Transmit characters, refill buffer descriptor, if possible
577 static int cpm_uart_tx_pump(struct uart_port *port)
582 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
583 struct circ_buf *xmit = &port->info->xmit;
585 /* Handle xon/xoff */
587 /* Pick next descriptor and fill from buffer */
590 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
594 out_be16(&bdp->cbd_datlen, 1);
595 setbits16(&bdp->cbd_sc, BD_SC_READY);
597 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
598 bdp = pinfo->tx_bd_base;
608 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
609 cpm_uart_stop_tx(port);
613 /* Pick next descriptor and fill from buffer */
616 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
617 xmit->tail != xmit->head) {
619 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
620 while (count < pinfo->tx_fifosize) {
621 *p++ = xmit->buf[xmit->tail];
622 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
625 if (xmit->head == xmit->tail)
628 out_be16(&bdp->cbd_datlen, count);
629 setbits16(&bdp->cbd_sc, BD_SC_READY);
631 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
632 bdp = pinfo->tx_bd_base;
638 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
639 uart_write_wakeup(port);
641 if (uart_circ_empty(xmit)) {
642 cpm_uart_stop_tx(port);
650 * init buffer descriptors
652 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
658 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
660 /* Set the physical address of the host memory
661 * buffers in the buffer descriptors, and the
662 * virtual address for us to work with.
664 mem_addr = pinfo->mem_addr;
665 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
666 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
667 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
668 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
669 mem_addr += pinfo->rx_fifosize;
672 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
673 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
675 /* Set the physical address of the host memory
676 * buffers in the buffer descriptors, and the
677 * virtual address for us to work with.
679 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
680 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
681 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
682 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
683 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
684 mem_addr += pinfo->tx_fifosize;
687 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
688 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
691 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
694 scc_uart_t __iomem *sup;
696 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
702 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
703 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
704 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
705 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
707 /* Set up the uart parameters in the
711 cpm_set_scc_fcr(sup);
713 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
714 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
715 out_be16(&sup->scc_brkcr, 1);
716 out_be16(&sup->scc_parec, 0);
717 out_be16(&sup->scc_frmec, 0);
718 out_be16(&sup->scc_nosec, 0);
719 out_be16(&sup->scc_brkec, 0);
720 out_be16(&sup->scc_uaddr1, 0);
721 out_be16(&sup->scc_uaddr2, 0);
722 out_be16(&sup->scc_toseq, 0);
723 out_be16(&sup->scc_char1, 0x8000);
724 out_be16(&sup->scc_char2, 0x8000);
725 out_be16(&sup->scc_char3, 0x8000);
726 out_be16(&sup->scc_char4, 0x8000);
727 out_be16(&sup->scc_char5, 0x8000);
728 out_be16(&sup->scc_char6, 0x8000);
729 out_be16(&sup->scc_char7, 0x8000);
730 out_be16(&sup->scc_char8, 0x8000);
731 out_be16(&sup->scc_rccm, 0xc0ff);
733 /* Send the CPM an initialize command.
735 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
737 /* Set UART mode, 8 bit, no parity, one stop.
738 * Enable receive and transmit.
740 out_be32(&scp->scc_gsmrh, 0);
741 out_be32(&scp->scc_gsmrl,
742 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
744 /* Enable rx interrupts and clear all pending events. */
745 out_be16(&scp->scc_sccm, 0);
746 out_be16(&scp->scc_scce, 0xffff);
747 out_be16(&scp->scc_dsr, 0x7e7e);
748 out_be16(&scp->scc_psmr, 0x3000);
750 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
753 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
756 smc_uart_t __iomem *up;
758 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
764 out_be16(&pinfo->smcup->smc_rbase,
765 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
766 out_be16(&pinfo->smcup->smc_tbase,
767 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
770 * In case SMC1 is being relocated...
772 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
773 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
774 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
775 out_be32(&up->smc_rstate, 0);
776 out_be32(&up->smc_tstate, 0);
777 out_be16(&up->smc_brkcr, 1); /* number of break chars */
778 out_be16(&up->smc_brkec, 0);
781 /* Set up the uart parameters in the
786 /* Using idle charater time requires some additional tuning. */
787 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
788 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
789 out_be16(&up->smc_brklen, 0);
790 out_be16(&up->smc_brkec, 0);
791 out_be16(&up->smc_brkcr, 1);
793 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
795 /* Set UART mode, 8 bit, no parity, one stop.
796 * Enable receive and transmit.
798 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
800 /* Enable only rx interrupts clear all pending events. */
801 out_8(&sp->smc_smcm, 0);
802 out_8(&sp->smc_smce, 0xff);
804 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
808 * Initialize port. This is called from early_console stuff
809 * so we have to be careful here !
811 static int cpm_uart_request_port(struct uart_port *port)
813 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
816 pr_debug("CPM uart[%d]:request port\n", port->line);
818 if (pinfo->flags & FLAG_CONSOLE)
822 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
823 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
825 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
826 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
829 ret = cpm_uart_allocbuf(pinfo, 0);
834 cpm_uart_initbd(pinfo);
836 cpm_uart_init_smc(pinfo);
838 cpm_uart_init_scc(pinfo);
843 static void cpm_uart_release_port(struct uart_port *port)
845 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
847 if (!(pinfo->flags & FLAG_CONSOLE))
848 cpm_uart_freebuf(pinfo);
852 * Configure/autoconfigure the port.
854 static void cpm_uart_config_port(struct uart_port *port, int flags)
856 pr_debug("CPM uart[%d]:config_port\n", port->line);
858 if (flags & UART_CONFIG_TYPE) {
859 port->type = PORT_CPM;
860 cpm_uart_request_port(port);
863 static struct uart_ops cpm_uart_pops = {
864 .tx_empty = cpm_uart_tx_empty,
865 .set_mctrl = cpm_uart_set_mctrl,
866 .get_mctrl = cpm_uart_get_mctrl,
867 .stop_tx = cpm_uart_stop_tx,
868 .start_tx = cpm_uart_start_tx,
869 .stop_rx = cpm_uart_stop_rx,
870 .enable_ms = cpm_uart_enable_ms,
871 .break_ctl = cpm_uart_break_ctl,
872 .startup = cpm_uart_startup,
873 .shutdown = cpm_uart_shutdown,
874 .set_termios = cpm_uart_set_termios,
875 .type = cpm_uart_type,
876 .release_port = cpm_uart_release_port,
877 .request_port = cpm_uart_request_port,
878 .config_port = cpm_uart_config_port,
879 .verify_port = cpm_uart_verify_port,
882 struct uart_cpm_port cpm_uart_ports[UART_NR];
884 static int cpm_uart_init_port(struct device_node *np,
885 struct uart_cpm_port *pinfo)
888 void __iomem *mem, *pram;
892 data = of_get_property(np, "fsl,cpm-brg", &len);
893 if (!data || len != 4) {
894 printk(KERN_ERR "CPM UART %s has no/invalid "
895 "fsl,cpm-brg property.\n", np->name);
900 data = of_get_property(np, "fsl,cpm-command", &len);
901 if (!data || len != 4) {
902 printk(KERN_ERR "CPM UART %s has no/invalid "
903 "fsl,cpm-command property.\n", np->name);
906 pinfo->command = *data;
908 mem = of_iomap(np, 0);
912 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
913 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
915 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
916 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
917 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
918 pinfo->flags |= FLAG_SMC;
920 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
931 pinfo->tx_nrfifos = TX_NUM_FIFO;
932 pinfo->tx_fifosize = TX_BUF_SIZE;
933 pinfo->rx_nrfifos = RX_NUM_FIFO;
934 pinfo->rx_fifosize = RX_BUF_SIZE;
936 pinfo->port.uartclk = ppc_proc_freq;
937 pinfo->port.mapbase = (unsigned long)mem;
938 pinfo->port.type = PORT_CPM;
939 pinfo->port.ops = &cpm_uart_pops,
940 pinfo->port.iotype = UPIO_MEM;
941 spin_lock_init(&pinfo->port.lock);
943 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
944 if (pinfo->port.irq == NO_IRQ) {
949 return cpm_uart_request_port(&pinfo->port);
952 cpm_uart_unmap_pram(pinfo, pram);
958 #ifdef CONFIG_SERIAL_CPM_CONSOLE
960 * Print a string to the serial port trying not to disturb
961 * any possible real use of the port...
963 * Note that this is called with interrupts already disabled
965 static void cpm_uart_console_write(struct console *co, const char *s,
968 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
970 cbd_t __iomem *bdp, *bdbase;
973 /* Get the address of the host memory buffer.
976 bdbase = pinfo->tx_bd_base;
979 * Now, do each character. This is not as bad as it looks
980 * since this is a holding FIFO and not a transmitting FIFO.
981 * We could add the complexity of filling the entire transmit
982 * buffer, but we would just wait longer between accesses......
984 for (i = 0; i < count; i++, s++) {
985 /* Wait for transmitter fifo to empty.
986 * Ready indicates output is ready, and xmt is doing
987 * that, not that it is ready for us to send.
989 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
992 /* Send the character out.
993 * If the buffer address is in the CPM DPRAM, don't
996 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
999 out_be16(&bdp->cbd_datlen, 1);
1000 setbits16(&bdp->cbd_sc, BD_SC_READY);
1002 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1007 /* if a LF, also do CR... */
1009 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1012 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1015 out_be16(&bdp->cbd_datlen, 1);
1016 setbits16(&bdp->cbd_sc, BD_SC_READY);
1018 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1026 * Finally, Wait for transmitter & holding register to empty
1027 * and restore the IER
1029 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1032 pinfo->tx_cur = bdp;
1036 static int __init cpm_uart_console_setup(struct console *co, char *options)
1043 struct uart_cpm_port *pinfo;
1044 struct uart_port *port;
1046 struct device_node *np = NULL;
1049 if (co->index >= UART_NR) {
1050 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1056 np = of_find_node_by_type(np, "serial");
1060 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1061 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1062 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1063 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1065 } while (i++ != co->index);
1067 pinfo = &cpm_uart_ports[co->index];
1069 pinfo->flags |= FLAG_CONSOLE;
1070 port = &pinfo->port;
1072 ret = cpm_uart_init_port(np, pinfo);
1078 uart_parse_options(options, &baud, &parity, &bits, &flow);
1080 if ((baud = uart_baudrate()) == -1)
1084 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1088 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1090 if (IS_SMC(pinfo)) {
1091 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1092 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1094 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1095 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1098 ret = cpm_uart_allocbuf(pinfo, 1);
1103 cpm_uart_initbd(pinfo);
1106 cpm_uart_init_smc(pinfo);
1108 cpm_uart_init_scc(pinfo);
1110 uart_set_options(port, co, baud, parity, bits, flow);
1111 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1116 static struct uart_driver cpm_reg;
1117 static struct console cpm_scc_uart_console = {
1119 .write = cpm_uart_console_write,
1120 .device = uart_console_device,
1121 .setup = cpm_uart_console_setup,
1122 .flags = CON_PRINTBUFFER,
1127 static int __init cpm_uart_console_init(void)
1129 register_console(&cpm_scc_uart_console);
1133 console_initcall(cpm_uart_console_init);
1135 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1137 #define CPM_UART_CONSOLE NULL
1140 static struct uart_driver cpm_reg = {
1141 .owner = THIS_MODULE,
1142 .driver_name = "ttyCPM",
1143 .dev_name = "ttyCPM",
1144 .major = SERIAL_CPM_MAJOR,
1145 .minor = SERIAL_CPM_MINOR,
1146 .cons = CPM_UART_CONSOLE,
1150 static int probe_index;
1152 static int __devinit cpm_uart_probe(struct of_device *ofdev,
1153 const struct of_device_id *match)
1155 int index = probe_index++;
1156 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1159 pinfo->port.line = index;
1161 if (index >= UART_NR)
1164 dev_set_drvdata(&ofdev->dev, pinfo);
1166 ret = cpm_uart_init_port(ofdev->node, pinfo);
1170 return uart_add_one_port(&cpm_reg, &pinfo->port);
1173 static int __devexit cpm_uart_remove(struct of_device *ofdev)
1175 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1176 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1179 static struct of_device_id cpm_uart_match[] = {
1181 .compatible = "fsl,cpm1-smc-uart",
1184 .compatible = "fsl,cpm1-scc-uart",
1187 .compatible = "fsl,cpm2-smc-uart",
1190 .compatible = "fsl,cpm2-scc-uart",
1195 static struct of_platform_driver cpm_uart_driver = {
1197 .match_table = cpm_uart_match,
1198 .probe = cpm_uart_probe,
1199 .remove = cpm_uart_remove,
1202 static int __init cpm_uart_init(void)
1204 int ret = uart_register_driver(&cpm_reg);
1208 ret = of_register_platform_driver(&cpm_uart_driver);
1210 uart_unregister_driver(&cpm_reg);
1215 static void __exit cpm_uart_exit(void)
1217 of_unregister_platform_driver(&cpm_uart_driver);
1218 uart_unregister_driver(&cpm_reg);
1221 module_init(cpm_uart_init);
1222 module_exit(cpm_uart_exit);
1224 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1225 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1226 MODULE_LICENSE("GPL");
1227 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);