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);
204 #ifdef CONFIG_CONSOLE_POLL
205 static int serial_polled;
211 static void cpm_uart_int_rx(struct uart_port *port)
216 struct tty_struct *tty = port->info->port.tty;
217 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
222 pr_debug("CPM uart[%d]:RX INT\n", port->line);
224 /* Just loop through the closed BDs and copy the characters into
229 #ifdef CONFIG_CONSOLE_POLL
230 if (unlikely(serial_polled)) {
236 status = in_be16(&bdp->cbd_sc);
237 /* If this one is empty, return happy */
238 if (status & BD_SC_EMPTY)
241 /* get number of characters, and check spce in flip-buffer */
242 i = in_be16(&bdp->cbd_datlen);
244 /* If we have not enough room in tty flip buffer, then we try
245 * later, which will be the next rx-interrupt or a timeout
247 if(tty_buffer_request_room(tty, i) < i) {
248 printk(KERN_WARNING "No room in flip buffer\n");
253 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
255 /* loop through the buffer */
262 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
264 if (uart_handle_sysrq_char(port, ch))
266 #ifdef CONFIG_CONSOLE_POLL
267 if (unlikely(serial_polled)) {
273 tty_insert_flip_char(tty, ch, flg);
275 } /* End while (i--) */
277 /* This BD is ready to be used again. Clear status. get next */
278 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
279 BD_SC_OV | BD_SC_ID);
280 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
282 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
283 bdp = pinfo->rx_bd_base;
289 /* Write back buffer pointer */
292 /* activate BH processing */
293 tty_flip_buffer_push(tty);
297 /* Error processing */
301 if (status & BD_SC_BR)
303 if (status & BD_SC_PR)
304 port->icount.parity++;
305 if (status & BD_SC_FR)
306 port->icount.frame++;
307 if (status & BD_SC_OV)
308 port->icount.overrun++;
310 /* Mask out ignored conditions */
311 status &= port->read_status_mask;
313 /* Handle the remaining ones */
314 if (status & BD_SC_BR)
316 else if (status & BD_SC_PR)
318 else if (status & BD_SC_FR)
321 /* overrun does not affect the current character ! */
322 if (status & BD_SC_OV) {
325 /* We skip this buffer */
326 /* CHECK: Is really nothing senseful there */
327 /* ASSUMPTION: it contains nothing valid */
337 * Asynchron mode interrupt handler
339 static irqreturn_t cpm_uart_int(int irq, void *data)
342 struct uart_port *port = data;
343 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
344 smc_t __iomem *smcp = pinfo->smcp;
345 scc_t __iomem *sccp = pinfo->sccp;
347 pr_debug("CPM uart[%d]:IRQ\n", port->line);
350 events = in_8(&smcp->smc_smce);
351 out_8(&smcp->smc_smce, events);
352 if (events & SMCM_BRKE)
353 uart_handle_break(port);
354 if (events & SMCM_RX)
355 cpm_uart_int_rx(port);
356 if (events & SMCM_TX)
357 cpm_uart_int_tx(port);
359 events = in_be16(&sccp->scc_scce);
360 out_be16(&sccp->scc_scce, events);
361 if (events & UART_SCCM_BRKE)
362 uart_handle_break(port);
363 if (events & UART_SCCM_RX)
364 cpm_uart_int_rx(port);
365 if (events & UART_SCCM_TX)
366 cpm_uart_int_tx(port);
368 return (events) ? IRQ_HANDLED : IRQ_NONE;
371 static int cpm_uart_startup(struct uart_port *port)
374 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
376 pr_debug("CPM uart[%d]:startup\n", port->line);
378 /* Install interrupt handler. */
379 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
385 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
386 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
388 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
389 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
392 if (!(pinfo->flags & FLAG_CONSOLE))
393 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
397 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
399 set_current_state(TASK_UNINTERRUPTIBLE);
400 schedule_timeout(pinfo->wait_closing);
406 static void cpm_uart_shutdown(struct uart_port *port)
408 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
410 pr_debug("CPM uart[%d]:shutdown\n", port->line);
412 /* free interrupt handler */
413 free_irq(port->irq, port);
415 /* If the port is not the console, disable Rx and Tx. */
416 if (!(pinfo->flags & FLAG_CONSOLE)) {
417 /* Wait for all the BDs marked sent */
418 while(!cpm_uart_tx_empty(port)) {
419 set_current_state(TASK_UNINTERRUPTIBLE);
423 if (pinfo->wait_closing)
424 cpm_uart_wait_until_send(pinfo);
428 smc_t __iomem *smcp = pinfo->smcp;
429 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
430 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
432 scc_t __iomem *sccp = pinfo->sccp;
433 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
434 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
437 /* Shut them really down and reinit buffer descriptors */
439 out_be16(&pinfo->smcup->smc_brkcr, 0);
440 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
442 out_be16(&pinfo->sccup->scc_brkcr, 0);
443 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
446 cpm_uart_initbd(pinfo);
450 static void cpm_uart_set_termios(struct uart_port *port,
451 struct ktermios *termios,
452 struct ktermios *old)
456 u16 cval, scval, prev_mode;
458 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
459 smc_t __iomem *smcp = pinfo->smcp;
460 scc_t __iomem *sccp = pinfo->sccp;
462 pr_debug("CPM uart[%d]:set_termios\n", port->line);
464 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
466 /* Character length programmed into the mode register is the
467 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
468 * 1 or 2 stop bits, minus 1.
469 * The value 'bits' counts this for us.
475 switch (termios->c_cflag & CSIZE) {
488 /* Never happens, but GCC is too dumb to figure it out */
495 if (termios->c_cflag & CSTOPB) {
496 cval |= SMCMR_SL; /* Two stops */
497 scval |= SCU_PSMR_SL;
501 if (termios->c_cflag & PARENB) {
503 scval |= SCU_PSMR_PEN;
505 if (!(termios->c_cflag & PARODD)) {
506 cval |= SMCMR_PM_EVEN;
507 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
514 uart_update_timeout(port, termios->c_cflag, baud);
517 * Set up parity check flag
519 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
521 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
522 if (termios->c_iflag & INPCK)
523 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
524 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
525 port->read_status_mask |= BD_SC_BR;
528 * Characters to ignore
530 port->ignore_status_mask = 0;
531 if (termios->c_iflag & IGNPAR)
532 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
533 if (termios->c_iflag & IGNBRK) {
534 port->ignore_status_mask |= BD_SC_BR;
536 * If we're ignore parity and break indicators, ignore
537 * overruns too. (For real raw support).
539 if (termios->c_iflag & IGNPAR)
540 port->ignore_status_mask |= BD_SC_OV;
543 * !!! ignore all characters if CREAD is not set
545 if ((termios->c_cflag & CREAD) == 0)
546 port->read_status_mask &= ~BD_SC_EMPTY;
548 spin_lock_irqsave(&port->lock, flags);
550 /* Start bit has not been added (so don't, because we would just
551 * subtract it later), and we need to add one for the number of
552 * stops bits (there is always at least one).
556 /* Set the mode register. We want to keep a copy of the
557 * enables, because we want to put them back if they were
560 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
561 /* Output in *one* operation, so we don't interrupt RX/TX if they
562 * were already enabled. */
563 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
564 SMCMR_SM_UART | prev_mode);
566 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
569 cpm_set_brg(pinfo->brg - 1, baud);
570 spin_unlock_irqrestore(&port->lock, flags);
573 static const char *cpm_uart_type(struct uart_port *port)
575 pr_debug("CPM uart[%d]:uart_type\n", port->line);
577 return port->type == PORT_CPM ? "CPM UART" : NULL;
581 * verify the new serial_struct (for TIOCSSERIAL).
583 static int cpm_uart_verify_port(struct uart_port *port,
584 struct serial_struct *ser)
588 pr_debug("CPM uart[%d]:verify_port\n", port->line);
590 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
592 if (ser->irq < 0 || ser->irq >= NR_IRQS)
594 if (ser->baud_base < 9600)
600 * Transmit characters, refill buffer descriptor, if possible
602 static int cpm_uart_tx_pump(struct uart_port *port)
607 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
608 struct circ_buf *xmit = &port->info->xmit;
610 /* Handle xon/xoff */
612 /* Pick next descriptor and fill from buffer */
615 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
619 out_be16(&bdp->cbd_datlen, 1);
620 setbits16(&bdp->cbd_sc, BD_SC_READY);
622 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
623 bdp = pinfo->tx_bd_base;
633 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
634 cpm_uart_stop_tx(port);
638 /* Pick next descriptor and fill from buffer */
641 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
642 xmit->tail != xmit->head) {
644 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
645 while (count < pinfo->tx_fifosize) {
646 *p++ = xmit->buf[xmit->tail];
647 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
650 if (xmit->head == xmit->tail)
653 out_be16(&bdp->cbd_datlen, count);
654 setbits16(&bdp->cbd_sc, BD_SC_READY);
656 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
657 bdp = pinfo->tx_bd_base;
663 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
664 uart_write_wakeup(port);
666 if (uart_circ_empty(xmit)) {
667 cpm_uart_stop_tx(port);
675 * init buffer descriptors
677 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
683 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
685 /* Set the physical address of the host memory
686 * buffers in the buffer descriptors, and the
687 * virtual address for us to work with.
689 mem_addr = pinfo->mem_addr;
690 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
691 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
692 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
693 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
694 mem_addr += pinfo->rx_fifosize;
697 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
698 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
700 /* Set the physical address of the host memory
701 * buffers in the buffer descriptors, and the
702 * virtual address for us to work with.
704 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
705 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
706 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
707 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
708 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
709 mem_addr += pinfo->tx_fifosize;
712 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
713 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
716 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
719 scc_uart_t __iomem *sup;
721 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
727 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
728 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
729 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
730 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
732 /* Set up the uart parameters in the
736 cpm_set_scc_fcr(sup);
738 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
739 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
740 out_be16(&sup->scc_brkcr, 1);
741 out_be16(&sup->scc_parec, 0);
742 out_be16(&sup->scc_frmec, 0);
743 out_be16(&sup->scc_nosec, 0);
744 out_be16(&sup->scc_brkec, 0);
745 out_be16(&sup->scc_uaddr1, 0);
746 out_be16(&sup->scc_uaddr2, 0);
747 out_be16(&sup->scc_toseq, 0);
748 out_be16(&sup->scc_char1, 0x8000);
749 out_be16(&sup->scc_char2, 0x8000);
750 out_be16(&sup->scc_char3, 0x8000);
751 out_be16(&sup->scc_char4, 0x8000);
752 out_be16(&sup->scc_char5, 0x8000);
753 out_be16(&sup->scc_char6, 0x8000);
754 out_be16(&sup->scc_char7, 0x8000);
755 out_be16(&sup->scc_char8, 0x8000);
756 out_be16(&sup->scc_rccm, 0xc0ff);
758 /* Send the CPM an initialize command.
760 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
762 /* Set UART mode, 8 bit, no parity, one stop.
763 * Enable receive and transmit.
765 out_be32(&scp->scc_gsmrh, 0);
766 out_be32(&scp->scc_gsmrl,
767 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
769 /* Enable rx interrupts and clear all pending events. */
770 out_be16(&scp->scc_sccm, 0);
771 out_be16(&scp->scc_scce, 0xffff);
772 out_be16(&scp->scc_dsr, 0x7e7e);
773 out_be16(&scp->scc_psmr, 0x3000);
775 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
778 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
781 smc_uart_t __iomem *up;
783 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
789 out_be16(&pinfo->smcup->smc_rbase,
790 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
791 out_be16(&pinfo->smcup->smc_tbase,
792 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
795 * In case SMC1 is being relocated...
797 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
798 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
799 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
800 out_be32(&up->smc_rstate, 0);
801 out_be32(&up->smc_tstate, 0);
802 out_be16(&up->smc_brkcr, 1); /* number of break chars */
803 out_be16(&up->smc_brkec, 0);
806 /* Set up the uart parameters in the
811 /* Using idle charater time requires some additional tuning. */
812 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
813 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
814 out_be16(&up->smc_brklen, 0);
815 out_be16(&up->smc_brkec, 0);
816 out_be16(&up->smc_brkcr, 1);
818 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
820 /* Set UART mode, 8 bit, no parity, one stop.
821 * Enable receive and transmit.
823 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
825 /* Enable only rx interrupts clear all pending events. */
826 out_8(&sp->smc_smcm, 0);
827 out_8(&sp->smc_smce, 0xff);
829 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
833 * Initialize port. This is called from early_console stuff
834 * so we have to be careful here !
836 static int cpm_uart_request_port(struct uart_port *port)
838 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
841 pr_debug("CPM uart[%d]:request port\n", port->line);
843 if (pinfo->flags & FLAG_CONSOLE)
847 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
848 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
850 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
851 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
854 ret = cpm_uart_allocbuf(pinfo, 0);
859 cpm_uart_initbd(pinfo);
861 cpm_uart_init_smc(pinfo);
863 cpm_uart_init_scc(pinfo);
868 static void cpm_uart_release_port(struct uart_port *port)
870 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
872 if (!(pinfo->flags & FLAG_CONSOLE))
873 cpm_uart_freebuf(pinfo);
877 * Configure/autoconfigure the port.
879 static void cpm_uart_config_port(struct uart_port *port, int flags)
881 pr_debug("CPM uart[%d]:config_port\n", port->line);
883 if (flags & UART_CONFIG_TYPE) {
884 port->type = PORT_CPM;
885 cpm_uart_request_port(port);
889 #ifdef CONFIG_CONSOLE_POLL
890 /* Serial polling routines for writing and reading from the uart while
891 * in an interrupt or debug context.
894 #define GDB_BUF_SIZE 512 /* power of 2, please */
896 static char poll_buf[GDB_BUF_SIZE];
898 static int poll_chars;
900 static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
906 /* Get the address of the host memory buffer.
909 while (bdp->cbd_sc & BD_SC_EMPTY)
912 /* If the buffer address is in the CPM DPRAM, don't
915 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
918 i = c = bdp->cbd_datlen;
923 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
924 bdp->cbd_sc |= BD_SC_EMPTY;
926 if (bdp->cbd_sc & BD_SC_WRAP)
927 bdp = pinfo->rx_bd_base;
930 pinfo->rx_cur = (cbd_t *)bdp;
935 static int cpm_get_poll_char(struct uart_port *port)
937 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
939 if (!serial_polled) {
943 if (poll_chars <= 0) {
944 poll_chars = poll_wait_key(poll_buf, pinfo);
951 static void cpm_put_poll_char(struct uart_port *port,
954 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
958 cpm_uart_early_write(pinfo->port.line, ch, 1);
960 #endif /* CONFIG_CONSOLE_POLL */
962 static struct uart_ops cpm_uart_pops = {
963 .tx_empty = cpm_uart_tx_empty,
964 .set_mctrl = cpm_uart_set_mctrl,
965 .get_mctrl = cpm_uart_get_mctrl,
966 .stop_tx = cpm_uart_stop_tx,
967 .start_tx = cpm_uart_start_tx,
968 .stop_rx = cpm_uart_stop_rx,
969 .enable_ms = cpm_uart_enable_ms,
970 .break_ctl = cpm_uart_break_ctl,
971 .startup = cpm_uart_startup,
972 .shutdown = cpm_uart_shutdown,
973 .set_termios = cpm_uart_set_termios,
974 .type = cpm_uart_type,
975 .release_port = cpm_uart_release_port,
976 .request_port = cpm_uart_request_port,
977 .config_port = cpm_uart_config_port,
978 .verify_port = cpm_uart_verify_port,
979 #ifdef CONFIG_CONSOLE_POLL
980 .poll_get_char = cpm_get_poll_char,
981 .poll_put_char = cpm_put_poll_char,
985 struct uart_cpm_port cpm_uart_ports[UART_NR];
987 static int cpm_uart_init_port(struct device_node *np,
988 struct uart_cpm_port *pinfo)
991 void __iomem *mem, *pram;
995 data = of_get_property(np, "fsl,cpm-brg", &len);
996 if (!data || len != 4) {
997 printk(KERN_ERR "CPM UART %s has no/invalid "
998 "fsl,cpm-brg property.\n", np->name);
1003 data = of_get_property(np, "fsl,cpm-command", &len);
1004 if (!data || len != 4) {
1005 printk(KERN_ERR "CPM UART %s has no/invalid "
1006 "fsl,cpm-command property.\n", np->name);
1009 pinfo->command = *data;
1011 mem = of_iomap(np, 0);
1015 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1016 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1018 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1019 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1020 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1021 pinfo->flags |= FLAG_SMC;
1023 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1034 pinfo->tx_nrfifos = TX_NUM_FIFO;
1035 pinfo->tx_fifosize = TX_BUF_SIZE;
1036 pinfo->rx_nrfifos = RX_NUM_FIFO;
1037 pinfo->rx_fifosize = RX_BUF_SIZE;
1039 pinfo->port.uartclk = ppc_proc_freq;
1040 pinfo->port.mapbase = (unsigned long)mem;
1041 pinfo->port.type = PORT_CPM;
1042 pinfo->port.ops = &cpm_uart_pops,
1043 pinfo->port.iotype = UPIO_MEM;
1044 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1045 spin_lock_init(&pinfo->port.lock);
1047 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1048 if (pinfo->port.irq == NO_IRQ) {
1053 return cpm_uart_request_port(&pinfo->port);
1056 cpm_uart_unmap_pram(pinfo, pram);
1062 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1064 * Print a string to the serial port trying not to disturb
1065 * any possible real use of the port...
1067 * Note that this is called with interrupts already disabled
1069 static void cpm_uart_console_write(struct console *co, const char *s,
1072 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1074 cbd_t __iomem *bdp, *bdbase;
1076 unsigned long flags;
1077 int nolock = oops_in_progress;
1079 if (unlikely(nolock)) {
1080 local_irq_save(flags);
1082 spin_lock_irqsave(&pinfo->port.lock, flags);
1085 /* Get the address of the host memory buffer.
1087 bdp = pinfo->tx_cur;
1088 bdbase = pinfo->tx_bd_base;
1091 * Now, do each character. This is not as bad as it looks
1092 * since this is a holding FIFO and not a transmitting FIFO.
1093 * We could add the complexity of filling the entire transmit
1094 * buffer, but we would just wait longer between accesses......
1096 for (i = 0; i < count; i++, s++) {
1097 /* Wait for transmitter fifo to empty.
1098 * Ready indicates output is ready, and xmt is doing
1099 * that, not that it is ready for us to send.
1101 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1104 /* Send the character out.
1105 * If the buffer address is in the CPM DPRAM, don't
1108 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1111 out_be16(&bdp->cbd_datlen, 1);
1112 setbits16(&bdp->cbd_sc, BD_SC_READY);
1114 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1119 /* if a LF, also do CR... */
1121 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1124 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1127 out_be16(&bdp->cbd_datlen, 1);
1128 setbits16(&bdp->cbd_sc, BD_SC_READY);
1130 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1138 * Finally, Wait for transmitter & holding register to empty
1139 * and restore the IER
1141 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1144 pinfo->tx_cur = bdp;
1146 if (unlikely(nolock)) {
1147 local_irq_restore(flags);
1149 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1154 static int __init cpm_uart_console_setup(struct console *co, char *options)
1161 struct uart_cpm_port *pinfo;
1162 struct uart_port *port;
1164 struct device_node *np = NULL;
1167 if (co->index >= UART_NR) {
1168 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1174 np = of_find_node_by_type(np, "serial");
1178 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1179 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1180 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1181 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1183 } while (i++ != co->index);
1185 pinfo = &cpm_uart_ports[co->index];
1187 pinfo->flags |= FLAG_CONSOLE;
1188 port = &pinfo->port;
1190 ret = cpm_uart_init_port(np, pinfo);
1196 uart_parse_options(options, &baud, &parity, &bits, &flow);
1198 if ((baud = uart_baudrate()) == -1)
1202 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1206 if (IS_SMC(pinfo)) {
1207 out_be16(&pinfo->smcup->smc_brkcr, 0);
1208 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1209 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1210 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1212 out_be16(&pinfo->sccup->scc_brkcr, 0);
1213 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
1214 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1215 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1218 ret = cpm_uart_allocbuf(pinfo, 1);
1223 cpm_uart_initbd(pinfo);
1226 cpm_uart_init_smc(pinfo);
1228 cpm_uart_init_scc(pinfo);
1230 uart_set_options(port, co, baud, parity, bits, flow);
1231 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1236 static struct uart_driver cpm_reg;
1237 static struct console cpm_scc_uart_console = {
1239 .write = cpm_uart_console_write,
1240 .device = uart_console_device,
1241 .setup = cpm_uart_console_setup,
1242 .flags = CON_PRINTBUFFER,
1247 static int __init cpm_uart_console_init(void)
1249 register_console(&cpm_scc_uart_console);
1253 console_initcall(cpm_uart_console_init);
1255 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1257 #define CPM_UART_CONSOLE NULL
1260 static struct uart_driver cpm_reg = {
1261 .owner = THIS_MODULE,
1262 .driver_name = "ttyCPM",
1263 .dev_name = "ttyCPM",
1264 .major = SERIAL_CPM_MAJOR,
1265 .minor = SERIAL_CPM_MINOR,
1266 .cons = CPM_UART_CONSOLE,
1270 static int probe_index;
1272 static int __devinit cpm_uart_probe(struct of_device *ofdev,
1273 const struct of_device_id *match)
1275 int index = probe_index++;
1276 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1279 pinfo->port.line = index;
1281 if (index >= UART_NR)
1284 dev_set_drvdata(&ofdev->dev, pinfo);
1286 ret = cpm_uart_init_port(ofdev->node, pinfo);
1290 return uart_add_one_port(&cpm_reg, &pinfo->port);
1293 static int __devexit cpm_uart_remove(struct of_device *ofdev)
1295 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1296 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1299 static struct of_device_id cpm_uart_match[] = {
1301 .compatible = "fsl,cpm1-smc-uart",
1304 .compatible = "fsl,cpm1-scc-uart",
1307 .compatible = "fsl,cpm2-smc-uart",
1310 .compatible = "fsl,cpm2-scc-uart",
1315 static struct of_platform_driver cpm_uart_driver = {
1317 .match_table = cpm_uart_match,
1318 .probe = cpm_uart_probe,
1319 .remove = cpm_uart_remove,
1322 static int __init cpm_uart_init(void)
1324 int ret = uart_register_driver(&cpm_reg);
1328 ret = of_register_platform_driver(&cpm_uart_driver);
1330 uart_unregister_driver(&cpm_reg);
1335 static void __exit cpm_uart_exit(void)
1337 of_unregister_platform_driver(&cpm_uart_driver);
1338 uart_unregister_driver(&cpm_reg);
1341 module_init(cpm_uart_init);
1342 module_exit(cpm_uart_exit);
1344 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1345 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1346 MODULE_LICENSE("GPL");
1347 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);