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 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
441 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
443 cpm_uart_initbd(pinfo);
447 static void cpm_uart_set_termios(struct uart_port *port,
448 struct ktermios *termios,
449 struct ktermios *old)
453 u16 cval, scval, prev_mode;
455 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
456 smc_t __iomem *smcp = pinfo->smcp;
457 scc_t __iomem *sccp = pinfo->sccp;
459 pr_debug("CPM uart[%d]:set_termios\n", port->line);
461 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
463 /* Character length programmed into the mode register is the
464 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
465 * 1 or 2 stop bits, minus 1.
466 * The value 'bits' counts this for us.
472 switch (termios->c_cflag & CSIZE) {
485 /* Never happens, but GCC is too dumb to figure it out */
492 if (termios->c_cflag & CSTOPB) {
493 cval |= SMCMR_SL; /* Two stops */
494 scval |= SCU_PSMR_SL;
498 if (termios->c_cflag & PARENB) {
500 scval |= SCU_PSMR_PEN;
502 if (!(termios->c_cflag & PARODD)) {
503 cval |= SMCMR_PM_EVEN;
504 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
511 uart_update_timeout(port, termios->c_cflag, baud);
514 * Set up parity check flag
516 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
518 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
519 if (termios->c_iflag & INPCK)
520 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
521 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
522 port->read_status_mask |= BD_SC_BR;
525 * Characters to ignore
527 port->ignore_status_mask = 0;
528 if (termios->c_iflag & IGNPAR)
529 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
530 if (termios->c_iflag & IGNBRK) {
531 port->ignore_status_mask |= BD_SC_BR;
533 * If we're ignore parity and break indicators, ignore
534 * overruns too. (For real raw support).
536 if (termios->c_iflag & IGNPAR)
537 port->ignore_status_mask |= BD_SC_OV;
540 * !!! ignore all characters if CREAD is not set
542 if ((termios->c_cflag & CREAD) == 0)
543 port->read_status_mask &= ~BD_SC_EMPTY;
545 spin_lock_irqsave(&port->lock, flags);
547 /* Start bit has not been added (so don't, because we would just
548 * subtract it later), and we need to add one for the number of
549 * stops bits (there is always at least one).
553 /* Set the mode register. We want to keep a copy of the
554 * enables, because we want to put them back if they were
557 prev_mode = in_be16(&smcp->smc_smcmr);
558 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval | SMCMR_SM_UART);
559 setbits16(&smcp->smc_smcmr, (prev_mode & (SMCMR_REN | SMCMR_TEN)));
561 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
564 cpm_set_brg(pinfo->brg - 1, baud);
565 spin_unlock_irqrestore(&port->lock, flags);
568 static const char *cpm_uart_type(struct uart_port *port)
570 pr_debug("CPM uart[%d]:uart_type\n", port->line);
572 return port->type == PORT_CPM ? "CPM UART" : NULL;
576 * verify the new serial_struct (for TIOCSSERIAL).
578 static int cpm_uart_verify_port(struct uart_port *port,
579 struct serial_struct *ser)
583 pr_debug("CPM uart[%d]:verify_port\n", port->line);
585 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
587 if (ser->irq < 0 || ser->irq >= NR_IRQS)
589 if (ser->baud_base < 9600)
595 * Transmit characters, refill buffer descriptor, if possible
597 static int cpm_uart_tx_pump(struct uart_port *port)
602 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
603 struct circ_buf *xmit = &port->info->xmit;
605 /* Handle xon/xoff */
607 /* Pick next descriptor and fill from buffer */
610 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
614 out_be16(&bdp->cbd_datlen, 1);
615 setbits16(&bdp->cbd_sc, BD_SC_READY);
617 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
618 bdp = pinfo->tx_bd_base;
628 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
629 cpm_uart_stop_tx(port);
633 /* Pick next descriptor and fill from buffer */
636 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
637 xmit->tail != xmit->head) {
639 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
640 while (count < pinfo->tx_fifosize) {
641 *p++ = xmit->buf[xmit->tail];
642 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
645 if (xmit->head == xmit->tail)
648 out_be16(&bdp->cbd_datlen, count);
649 setbits16(&bdp->cbd_sc, BD_SC_READY);
651 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
652 bdp = pinfo->tx_bd_base;
658 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
659 uart_write_wakeup(port);
661 if (uart_circ_empty(xmit)) {
662 cpm_uart_stop_tx(port);
670 * init buffer descriptors
672 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
678 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
680 /* Set the physical address of the host memory
681 * buffers in the buffer descriptors, and the
682 * virtual address for us to work with.
684 mem_addr = pinfo->mem_addr;
685 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
686 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
687 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
688 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
689 mem_addr += pinfo->rx_fifosize;
692 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
693 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
695 /* Set the physical address of the host memory
696 * buffers in the buffer descriptors, and the
697 * virtual address for us to work with.
699 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
700 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
701 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
702 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
703 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
704 mem_addr += pinfo->tx_fifosize;
707 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
708 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
711 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
714 scc_uart_t __iomem *sup;
716 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
722 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
723 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
724 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
725 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
727 /* Set up the uart parameters in the
731 cpm_set_scc_fcr(sup);
733 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
734 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
735 out_be16(&sup->scc_brkcr, 1);
736 out_be16(&sup->scc_parec, 0);
737 out_be16(&sup->scc_frmec, 0);
738 out_be16(&sup->scc_nosec, 0);
739 out_be16(&sup->scc_brkec, 0);
740 out_be16(&sup->scc_uaddr1, 0);
741 out_be16(&sup->scc_uaddr2, 0);
742 out_be16(&sup->scc_toseq, 0);
743 out_be16(&sup->scc_char1, 0x8000);
744 out_be16(&sup->scc_char2, 0x8000);
745 out_be16(&sup->scc_char3, 0x8000);
746 out_be16(&sup->scc_char4, 0x8000);
747 out_be16(&sup->scc_char5, 0x8000);
748 out_be16(&sup->scc_char6, 0x8000);
749 out_be16(&sup->scc_char7, 0x8000);
750 out_be16(&sup->scc_char8, 0x8000);
751 out_be16(&sup->scc_rccm, 0xc0ff);
753 /* Send the CPM an initialize command.
755 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
757 /* Set UART mode, 8 bit, no parity, one stop.
758 * Enable receive and transmit.
760 out_be32(&scp->scc_gsmrh, 0);
761 out_be32(&scp->scc_gsmrl,
762 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
764 /* Enable rx interrupts and clear all pending events. */
765 out_be16(&scp->scc_sccm, 0);
766 out_be16(&scp->scc_scce, 0xffff);
767 out_be16(&scp->scc_dsr, 0x7e7e);
768 out_be16(&scp->scc_psmr, 0x3000);
770 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
773 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
776 smc_uart_t __iomem *up;
778 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
784 out_be16(&pinfo->smcup->smc_rbase,
785 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
786 out_be16(&pinfo->smcup->smc_tbase,
787 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
790 * In case SMC1 is being relocated...
792 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
793 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
794 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
795 out_be32(&up->smc_rstate, 0);
796 out_be32(&up->smc_tstate, 0);
797 out_be16(&up->smc_brkcr, 1); /* number of break chars */
798 out_be16(&up->smc_brkec, 0);
801 /* Set up the uart parameters in the
806 /* Using idle charater time requires some additional tuning. */
807 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
808 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
809 out_be16(&up->smc_brklen, 0);
810 out_be16(&up->smc_brkec, 0);
811 out_be16(&up->smc_brkcr, 1);
813 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
815 /* Set UART mode, 8 bit, no parity, one stop.
816 * Enable receive and transmit.
818 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
820 /* Enable only rx interrupts clear all pending events. */
821 out_8(&sp->smc_smcm, 0);
822 out_8(&sp->smc_smce, 0xff);
824 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
828 * Initialize port. This is called from early_console stuff
829 * so we have to be careful here !
831 static int cpm_uart_request_port(struct uart_port *port)
833 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
836 pr_debug("CPM uart[%d]:request port\n", port->line);
838 if (pinfo->flags & FLAG_CONSOLE)
842 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
843 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
845 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
846 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
849 ret = cpm_uart_allocbuf(pinfo, 0);
854 cpm_uart_initbd(pinfo);
856 cpm_uart_init_smc(pinfo);
858 cpm_uart_init_scc(pinfo);
863 static void cpm_uart_release_port(struct uart_port *port)
865 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
867 if (!(pinfo->flags & FLAG_CONSOLE))
868 cpm_uart_freebuf(pinfo);
872 * Configure/autoconfigure the port.
874 static void cpm_uart_config_port(struct uart_port *port, int flags)
876 pr_debug("CPM uart[%d]:config_port\n", port->line);
878 if (flags & UART_CONFIG_TYPE) {
879 port->type = PORT_CPM;
880 cpm_uart_request_port(port);
884 #ifdef CONFIG_CONSOLE_POLL
885 /* Serial polling routines for writing and reading from the uart while
886 * in an interrupt or debug context.
889 #define GDB_BUF_SIZE 512 /* power of 2, please */
891 static char poll_buf[GDB_BUF_SIZE];
893 static int poll_chars;
895 static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
901 /* Get the address of the host memory buffer.
904 while (bdp->cbd_sc & BD_SC_EMPTY)
907 /* If the buffer address is in the CPM DPRAM, don't
910 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
913 i = c = bdp->cbd_datlen;
918 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
919 bdp->cbd_sc |= BD_SC_EMPTY;
921 if (bdp->cbd_sc & BD_SC_WRAP)
922 bdp = pinfo->rx_bd_base;
925 pinfo->rx_cur = (cbd_t *)bdp;
930 static int cpm_get_poll_char(struct uart_port *port)
932 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
934 if (!serial_polled) {
938 if (poll_chars <= 0) {
939 poll_chars = poll_wait_key(poll_buf, pinfo);
946 static void cpm_put_poll_char(struct uart_port *port,
949 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
953 cpm_uart_early_write(pinfo->port.line, ch, 1);
955 #endif /* CONFIG_CONSOLE_POLL */
957 static struct uart_ops cpm_uart_pops = {
958 .tx_empty = cpm_uart_tx_empty,
959 .set_mctrl = cpm_uart_set_mctrl,
960 .get_mctrl = cpm_uart_get_mctrl,
961 .stop_tx = cpm_uart_stop_tx,
962 .start_tx = cpm_uart_start_tx,
963 .stop_rx = cpm_uart_stop_rx,
964 .enable_ms = cpm_uart_enable_ms,
965 .break_ctl = cpm_uart_break_ctl,
966 .startup = cpm_uart_startup,
967 .shutdown = cpm_uart_shutdown,
968 .set_termios = cpm_uart_set_termios,
969 .type = cpm_uart_type,
970 .release_port = cpm_uart_release_port,
971 .request_port = cpm_uart_request_port,
972 .config_port = cpm_uart_config_port,
973 .verify_port = cpm_uart_verify_port,
974 #ifdef CONFIG_CONSOLE_POLL
975 .poll_get_char = cpm_get_poll_char,
976 .poll_put_char = cpm_put_poll_char,
980 struct uart_cpm_port cpm_uart_ports[UART_NR];
982 static int cpm_uart_init_port(struct device_node *np,
983 struct uart_cpm_port *pinfo)
986 void __iomem *mem, *pram;
990 data = of_get_property(np, "fsl,cpm-brg", &len);
991 if (!data || len != 4) {
992 printk(KERN_ERR "CPM UART %s has no/invalid "
993 "fsl,cpm-brg property.\n", np->name);
998 data = of_get_property(np, "fsl,cpm-command", &len);
999 if (!data || len != 4) {
1000 printk(KERN_ERR "CPM UART %s has no/invalid "
1001 "fsl,cpm-command property.\n", np->name);
1004 pinfo->command = *data;
1006 mem = of_iomap(np, 0);
1010 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1011 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1013 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1014 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1015 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1016 pinfo->flags |= FLAG_SMC;
1018 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1029 pinfo->tx_nrfifos = TX_NUM_FIFO;
1030 pinfo->tx_fifosize = TX_BUF_SIZE;
1031 pinfo->rx_nrfifos = RX_NUM_FIFO;
1032 pinfo->rx_fifosize = RX_BUF_SIZE;
1034 pinfo->port.uartclk = ppc_proc_freq;
1035 pinfo->port.mapbase = (unsigned long)mem;
1036 pinfo->port.type = PORT_CPM;
1037 pinfo->port.ops = &cpm_uart_pops,
1038 pinfo->port.iotype = UPIO_MEM;
1039 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1040 spin_lock_init(&pinfo->port.lock);
1042 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1043 if (pinfo->port.irq == NO_IRQ) {
1048 return cpm_uart_request_port(&pinfo->port);
1051 cpm_uart_unmap_pram(pinfo, pram);
1057 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1059 * Print a string to the serial port trying not to disturb
1060 * any possible real use of the port...
1062 * Note that this is called with interrupts already disabled
1064 static void cpm_uart_console_write(struct console *co, const char *s,
1067 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1069 cbd_t __iomem *bdp, *bdbase;
1071 unsigned long flags;
1072 int nolock = oops_in_progress;
1074 if (unlikely(nolock)) {
1075 local_irq_save(flags);
1077 spin_lock_irqsave(&pinfo->port.lock, flags);
1080 /* Get the address of the host memory buffer.
1082 bdp = pinfo->tx_cur;
1083 bdbase = pinfo->tx_bd_base;
1086 * Now, do each character. This is not as bad as it looks
1087 * since this is a holding FIFO and not a transmitting FIFO.
1088 * We could add the complexity of filling the entire transmit
1089 * buffer, but we would just wait longer between accesses......
1091 for (i = 0; i < count; i++, s++) {
1092 /* Wait for transmitter fifo to empty.
1093 * Ready indicates output is ready, and xmt is doing
1094 * that, not that it is ready for us to send.
1096 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1099 /* Send the character out.
1100 * If the buffer address is in the CPM DPRAM, don't
1103 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1106 out_be16(&bdp->cbd_datlen, 1);
1107 setbits16(&bdp->cbd_sc, BD_SC_READY);
1109 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1114 /* if a LF, also do CR... */
1116 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1119 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
1122 out_be16(&bdp->cbd_datlen, 1);
1123 setbits16(&bdp->cbd_sc, BD_SC_READY);
1125 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1133 * Finally, Wait for transmitter & holding register to empty
1134 * and restore the IER
1136 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1139 pinfo->tx_cur = bdp;
1141 if (unlikely(nolock)) {
1142 local_irq_restore(flags);
1144 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1149 static int __init cpm_uart_console_setup(struct console *co, char *options)
1156 struct uart_cpm_port *pinfo;
1157 struct uart_port *port;
1159 struct device_node *np = NULL;
1162 if (co->index >= UART_NR) {
1163 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1169 np = of_find_node_by_type(np, "serial");
1173 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1174 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1175 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1176 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1178 } while (i++ != co->index);
1180 pinfo = &cpm_uart_ports[co->index];
1182 pinfo->flags |= FLAG_CONSOLE;
1183 port = &pinfo->port;
1185 ret = cpm_uart_init_port(np, pinfo);
1191 uart_parse_options(options, &baud, &parity, &bits, &flow);
1193 if ((baud = uart_baudrate()) == -1)
1197 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1201 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1203 if (IS_SMC(pinfo)) {
1204 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1205 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1207 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1208 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1211 ret = cpm_uart_allocbuf(pinfo, 1);
1216 cpm_uart_initbd(pinfo);
1219 cpm_uart_init_smc(pinfo);
1221 cpm_uart_init_scc(pinfo);
1223 uart_set_options(port, co, baud, parity, bits, flow);
1224 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1229 static struct uart_driver cpm_reg;
1230 static struct console cpm_scc_uart_console = {
1232 .write = cpm_uart_console_write,
1233 .device = uart_console_device,
1234 .setup = cpm_uart_console_setup,
1235 .flags = CON_PRINTBUFFER,
1240 static int __init cpm_uart_console_init(void)
1242 register_console(&cpm_scc_uart_console);
1246 console_initcall(cpm_uart_console_init);
1248 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1250 #define CPM_UART_CONSOLE NULL
1253 static struct uart_driver cpm_reg = {
1254 .owner = THIS_MODULE,
1255 .driver_name = "ttyCPM",
1256 .dev_name = "ttyCPM",
1257 .major = SERIAL_CPM_MAJOR,
1258 .minor = SERIAL_CPM_MINOR,
1259 .cons = CPM_UART_CONSOLE,
1263 static int probe_index;
1265 static int __devinit cpm_uart_probe(struct of_device *ofdev,
1266 const struct of_device_id *match)
1268 int index = probe_index++;
1269 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1272 pinfo->port.line = index;
1274 if (index >= UART_NR)
1277 dev_set_drvdata(&ofdev->dev, pinfo);
1279 ret = cpm_uart_init_port(ofdev->node, pinfo);
1283 return uart_add_one_port(&cpm_reg, &pinfo->port);
1286 static int __devexit cpm_uart_remove(struct of_device *ofdev)
1288 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1289 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1292 static struct of_device_id cpm_uart_match[] = {
1294 .compatible = "fsl,cpm1-smc-uart",
1297 .compatible = "fsl,cpm1-scc-uart",
1300 .compatible = "fsl,cpm2-smc-uart",
1303 .compatible = "fsl,cpm2-scc-uart",
1308 static struct of_platform_driver cpm_uart_driver = {
1310 .match_table = cpm_uart_match,
1311 .probe = cpm_uart_probe,
1312 .remove = cpm_uart_remove,
1315 static int __init cpm_uart_init(void)
1317 int ret = uart_register_driver(&cpm_reg);
1321 ret = of_register_platform_driver(&cpm_uart_driver);
1323 uart_unregister_driver(&cpm_reg);
1328 static void __exit cpm_uart_exit(void)
1330 of_unregister_platform_driver(&cpm_uart_driver);
1331 uart_unregister_driver(&cpm_reg);
1334 module_init(cpm_uart_init);
1335 module_exit(cpm_uart_exit);
1337 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1338 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1339 MODULE_LICENSE("GPL");
1340 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);