2 * Driver for CPM (SCC/SMC) serial ports; core driver
4 * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
5 * Based on ppc8xx.c by Thomas Gleixner
6 * Based on drivers/serial/amba.c by Russell King
8 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
9 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
11 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
12 * (C) 2004 Intracom, S.A.
13 * (C) 2005-2006 MontaVista Software, Inc.
14 * Vitaly Bordug <vbordug@ru.mvista.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/module.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/serial.h>
38 #include <linux/console.h>
39 #include <linux/sysrq.h>
40 #include <linux/device.h>
41 #include <linux/bootmem.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/fs_uart_pd.h>
44 #include <linux/of_address.h>
45 #include <linux/of_irq.h>
46 #include <linux/of_platform.h>
47 #include <linux/gpio.h>
48 #include <linux/of_gpio.h>
49 #include <linux/clk.h>
53 #include <asm/delay.h>
54 #include <asm/fs_pd.h>
57 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
61 #include <linux/serial_core.h>
62 #include <linux/kernel.h>
67 /**************************************************************/
69 static int cpm_uart_tx_pump(struct uart_port *port);
70 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
71 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
72 static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
74 /**************************************************************/
76 #define HW_BUF_SPD_THRESHOLD 2400
79 * Check, if transmit buffers are processed
81 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
83 struct uart_cpm_port *pinfo =
84 container_of(port, struct uart_cpm_port, port);
85 cbd_t __iomem *bdp = pinfo->tx_bd_base;
89 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
92 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
99 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
104 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
106 struct uart_cpm_port *pinfo =
107 container_of(port, struct uart_cpm_port, port);
109 if (pinfo->gpios[GPIO_RTS] >= 0)
110 gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
112 if (pinfo->gpios[GPIO_DTR] >= 0)
113 gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
116 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
118 struct uart_cpm_port *pinfo =
119 container_of(port, struct uart_cpm_port, port);
120 unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
122 if (pinfo->gpios[GPIO_CTS] >= 0) {
123 if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
127 if (pinfo->gpios[GPIO_DSR] >= 0) {
128 if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
132 if (pinfo->gpios[GPIO_DCD] >= 0) {
133 if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
137 if (pinfo->gpios[GPIO_RI] >= 0) {
138 if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
148 static void cpm_uart_stop_tx(struct uart_port *port)
150 struct uart_cpm_port *pinfo =
151 container_of(port, struct uart_cpm_port, port);
152 smc_t __iomem *smcp = pinfo->smcp;
153 scc_t __iomem *sccp = pinfo->sccp;
155 pr_debug("CPM uart[%d]:stop tx\n", port->line);
158 clrbits8(&smcp->smc_smcm, SMCM_TX);
160 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
166 static void cpm_uart_start_tx(struct uart_port *port)
168 struct uart_cpm_port *pinfo =
169 container_of(port, struct uart_cpm_port, port);
170 smc_t __iomem *smcp = pinfo->smcp;
171 scc_t __iomem *sccp = pinfo->sccp;
173 pr_debug("CPM uart[%d]:start tx\n", port->line);
176 if (in_8(&smcp->smc_smcm) & SMCM_TX)
179 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
183 if (cpm_uart_tx_pump(port) != 0) {
185 setbits8(&smcp->smc_smcm, SMCM_TX);
187 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
195 static void cpm_uart_stop_rx(struct uart_port *port)
197 struct uart_cpm_port *pinfo =
198 container_of(port, struct uart_cpm_port, port);
199 smc_t __iomem *smcp = pinfo->smcp;
200 scc_t __iomem *sccp = pinfo->sccp;
202 pr_debug("CPM uart[%d]:stop rx\n", port->line);
205 clrbits8(&smcp->smc_smcm, SMCM_RX);
207 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
213 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
215 struct uart_cpm_port *pinfo =
216 container_of(port, struct uart_cpm_port, port);
218 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
222 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
224 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
228 * Transmit characters, refill buffer descriptor, if possible
230 static void cpm_uart_int_tx(struct uart_port *port)
232 pr_debug("CPM uart[%d]:TX INT\n", port->line);
234 cpm_uart_tx_pump(port);
237 #ifdef CONFIG_CONSOLE_POLL
238 static int serial_polled;
244 static void cpm_uart_int_rx(struct uart_port *port)
249 struct tty_port *tport = &port->state->port;
250 struct uart_cpm_port *pinfo =
251 container_of(port, struct uart_cpm_port, port);
256 pr_debug("CPM uart[%d]:RX INT\n", port->line);
258 /* Just loop through the closed BDs and copy the characters into
263 #ifdef CONFIG_CONSOLE_POLL
264 if (unlikely(serial_polled)) {
270 status = in_be16(&bdp->cbd_sc);
271 /* If this one is empty, return happy */
272 if (status & BD_SC_EMPTY)
275 /* get number of characters, and check spce in flip-buffer */
276 i = in_be16(&bdp->cbd_datlen);
278 /* If we have not enough room in tty flip buffer, then we try
279 * later, which will be the next rx-interrupt or a timeout
281 if (tty_buffer_request_room(tport, i) < i) {
282 printk(KERN_WARNING "No room in flip buffer\n");
287 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
289 /* loop through the buffer */
296 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
298 if (uart_handle_sysrq_char(port, ch))
300 #ifdef CONFIG_CONSOLE_POLL
301 if (unlikely(serial_polled)) {
307 tty_insert_flip_char(tport, ch, flg);
309 } /* End while (i--) */
311 /* This BD is ready to be used again. Clear status. get next */
312 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
313 BD_SC_OV | BD_SC_ID);
314 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
316 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
317 bdp = pinfo->rx_bd_base;
323 /* Write back buffer pointer */
326 /* activate BH processing */
327 tty_flip_buffer_push(tport);
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)
376 struct uart_port *port = data;
377 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
378 smc_t __iomem *smcp = pinfo->smcp;
379 scc_t __iomem *sccp = pinfo->sccp;
381 pr_debug("CPM uart[%d]:IRQ\n", port->line);
384 events = in_8(&smcp->smc_smce);
385 out_8(&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);
390 if (events & SMCM_TX)
391 cpm_uart_int_tx(port);
393 events = in_be16(&sccp->scc_scce);
394 out_be16(&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);
399 if (events & UART_SCCM_TX)
400 cpm_uart_int_tx(port);
402 return (events) ? IRQ_HANDLED : IRQ_NONE;
405 static int cpm_uart_startup(struct uart_port *port)
408 struct uart_cpm_port *pinfo =
409 container_of(port, struct uart_cpm_port, port);
411 pr_debug("CPM uart[%d]:startup\n", port->line);
413 /* If the port is not the console, make sure rx is disabled. */
414 if (!(pinfo->flags & FLAG_CONSOLE)) {
415 /* Disable UART rx */
417 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);
418 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
420 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);
421 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
423 cpm_uart_initbd(pinfo);
424 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
426 /* Install interrupt handler. */
427 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
433 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
434 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
436 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
437 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
443 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
445 set_current_state(TASK_UNINTERRUPTIBLE);
446 schedule_timeout(pinfo->wait_closing);
452 static void cpm_uart_shutdown(struct uart_port *port)
454 struct uart_cpm_port *pinfo =
455 container_of(port, struct uart_cpm_port, port);
457 pr_debug("CPM uart[%d]:shutdown\n", port->line);
459 /* free interrupt handler */
460 free_irq(port->irq, port);
462 /* If the port is not the console, disable Rx and Tx. */
463 if (!(pinfo->flags & FLAG_CONSOLE)) {
464 /* Wait for all the BDs marked sent */
465 while(!cpm_uart_tx_empty(port)) {
466 set_current_state(TASK_UNINTERRUPTIBLE);
470 if (pinfo->wait_closing)
471 cpm_uart_wait_until_send(pinfo);
475 smc_t __iomem *smcp = pinfo->smcp;
476 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
477 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
479 scc_t __iomem *sccp = pinfo->sccp;
480 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
481 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
484 /* Shut them really down and reinit buffer descriptors */
486 out_be16(&pinfo->smcup->smc_brkcr, 0);
487 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
489 out_be16(&pinfo->sccup->scc_brkcr, 0);
490 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
493 cpm_uart_initbd(pinfo);
497 static void cpm_uart_set_termios(struct uart_port *port,
498 struct ktermios *termios,
499 struct ktermios *old)
503 u16 cval, scval, prev_mode;
505 struct uart_cpm_port *pinfo =
506 container_of(port, struct uart_cpm_port, port);
507 smc_t __iomem *smcp = pinfo->smcp;
508 scc_t __iomem *sccp = pinfo->sccp;
511 pr_debug("CPM uart[%d]:set_termios\n", port->line);
513 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
514 if (baud < HW_BUF_SPD_THRESHOLD ||
515 (pinfo->port.state && pinfo->port.state->port.low_latency))
516 pinfo->rx_fifosize = 1;
518 pinfo->rx_fifosize = RX_BUF_SIZE;
520 /* MAXIDL is the timeout after which a receive buffer is closed
521 * when not full if no more characters are received.
522 * We calculate it from the baudrate so that the duration is
523 * always the same at standard rates: about 4ms.
525 maxidl = baud / 2400;
531 /* Character length programmed into the mode register is the
532 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
533 * 1 or 2 stop bits, minus 1.
534 * The value 'bits' counts this for us.
540 switch (termios->c_cflag & CSIZE) {
553 /* Never happens, but GCC is too dumb to figure it out */
560 if (termios->c_cflag & CSTOPB) {
561 cval |= SMCMR_SL; /* Two stops */
562 scval |= SCU_PSMR_SL;
566 if (termios->c_cflag & PARENB) {
568 scval |= SCU_PSMR_PEN;
570 if (!(termios->c_cflag & PARODD)) {
571 cval |= SMCMR_PM_EVEN;
572 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
579 uart_update_timeout(port, termios->c_cflag, baud);
582 * Set up parity check flag
584 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
586 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
587 if (termios->c_iflag & INPCK)
588 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
589 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
590 port->read_status_mask |= BD_SC_BR;
593 * Characters to ignore
595 port->ignore_status_mask = 0;
596 if (termios->c_iflag & IGNPAR)
597 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
598 if (termios->c_iflag & IGNBRK) {
599 port->ignore_status_mask |= BD_SC_BR;
601 * If we're ignore parity and break indicators, ignore
602 * overruns too. (For real raw support).
604 if (termios->c_iflag & IGNPAR)
605 port->ignore_status_mask |= BD_SC_OV;
608 * !!! ignore all characters if CREAD is not set
610 if ((termios->c_cflag & CREAD) == 0)
611 port->read_status_mask &= ~BD_SC_EMPTY;
613 spin_lock_irqsave(&port->lock, flags);
615 /* Start bit has not been added (so don't, because we would just
616 * subtract it later), and we need to add one for the number of
617 * stops bits (there is always at least one).
622 * MRBLR can be changed while an SMC/SCC is operating only
623 * if it is done in a single bus cycle with one 16-bit move
624 * (not two 8-bit bus cycles back-to-back). This occurs when
625 * the cp shifts control to the next RxBD, so the change does
626 * not take effect immediately. To guarantee the exact RxBD
627 * on which the change occurs, change MRBLR only while the
628 * SMC/SCC receiver is disabled.
630 out_be16(&pinfo->smcup->smc_mrblr, pinfo->rx_fifosize);
631 out_be16(&pinfo->smcup->smc_maxidl, maxidl);
633 /* Set the mode register. We want to keep a copy of the
634 * enables, because we want to put them back if they were
637 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
638 /* Output in *one* operation, so we don't interrupt RX/TX if they
639 * were already enabled. */
640 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
641 SMCMR_SM_UART | prev_mode);
643 out_be16(&pinfo->sccup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
644 out_be16(&pinfo->sccup->scc_maxidl, maxidl);
645 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
649 clk_set_rate(pinfo->clk, baud);
651 cpm_set_brg(pinfo->brg - 1, baud);
652 spin_unlock_irqrestore(&port->lock, flags);
655 static const char *cpm_uart_type(struct uart_port *port)
657 pr_debug("CPM uart[%d]:uart_type\n", port->line);
659 return port->type == PORT_CPM ? "CPM UART" : NULL;
663 * verify the new serial_struct (for TIOCSSERIAL).
665 static int cpm_uart_verify_port(struct uart_port *port,
666 struct serial_struct *ser)
670 pr_debug("CPM uart[%d]:verify_port\n", port->line);
672 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
674 if (ser->irq < 0 || ser->irq >= nr_irqs)
676 if (ser->baud_base < 9600)
682 * Transmit characters, refill buffer descriptor, if possible
684 static int cpm_uart_tx_pump(struct uart_port *port)
689 struct uart_cpm_port *pinfo =
690 container_of(port, struct uart_cpm_port, port);
691 struct circ_buf *xmit = &port->state->xmit;
693 /* Handle xon/xoff */
695 /* Pick next descriptor and fill from buffer */
698 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
702 out_be16(&bdp->cbd_datlen, 1);
703 setbits16(&bdp->cbd_sc, BD_SC_READY);
705 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
706 bdp = pinfo->tx_bd_base;
716 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
717 cpm_uart_stop_tx(port);
721 /* Pick next descriptor and fill from buffer */
724 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
725 xmit->tail != xmit->head) {
727 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
728 while (count < pinfo->tx_fifosize) {
729 *p++ = xmit->buf[xmit->tail];
730 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
733 if (xmit->head == xmit->tail)
736 out_be16(&bdp->cbd_datlen, count);
737 setbits16(&bdp->cbd_sc, BD_SC_READY);
739 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
740 bdp = pinfo->tx_bd_base;
746 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
747 uart_write_wakeup(port);
749 if (uart_circ_empty(xmit)) {
750 cpm_uart_stop_tx(port);
758 * init buffer descriptors
760 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
766 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
768 /* Set the physical address of the host memory
769 * buffers in the buffer descriptors, and the
770 * virtual address for us to work with.
772 mem_addr = pinfo->mem_addr;
773 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
774 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
775 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
776 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
777 mem_addr += pinfo->rx_fifosize;
780 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
781 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
783 /* Set the physical address of the host memory
784 * buffers in the buffer descriptors, and the
785 * virtual address for us to work with.
787 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
788 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
789 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
790 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
791 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
792 mem_addr += pinfo->tx_fifosize;
795 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
796 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
799 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
802 scc_uart_t __iomem *sup;
804 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
810 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
811 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
812 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
813 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
815 /* Set up the uart parameters in the
819 cpm_set_scc_fcr(sup);
821 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
822 out_be16(&sup->scc_maxidl, 0x10);
823 out_be16(&sup->scc_brkcr, 1);
824 out_be16(&sup->scc_parec, 0);
825 out_be16(&sup->scc_frmec, 0);
826 out_be16(&sup->scc_nosec, 0);
827 out_be16(&sup->scc_brkec, 0);
828 out_be16(&sup->scc_uaddr1, 0);
829 out_be16(&sup->scc_uaddr2, 0);
830 out_be16(&sup->scc_toseq, 0);
831 out_be16(&sup->scc_char1, 0x8000);
832 out_be16(&sup->scc_char2, 0x8000);
833 out_be16(&sup->scc_char3, 0x8000);
834 out_be16(&sup->scc_char4, 0x8000);
835 out_be16(&sup->scc_char5, 0x8000);
836 out_be16(&sup->scc_char6, 0x8000);
837 out_be16(&sup->scc_char7, 0x8000);
838 out_be16(&sup->scc_char8, 0x8000);
839 out_be16(&sup->scc_rccm, 0xc0ff);
841 /* Send the CPM an initialize command.
843 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
845 /* Set UART mode, 8 bit, no parity, one stop.
846 * Enable receive and transmit.
848 out_be32(&scp->scc_gsmrh, 0);
849 out_be32(&scp->scc_gsmrl,
850 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
852 /* Enable rx interrupts and clear all pending events. */
853 out_be16(&scp->scc_sccm, 0);
854 out_be16(&scp->scc_scce, 0xffff);
855 out_be16(&scp->scc_dsr, 0x7e7e);
856 out_be16(&scp->scc_psmr, 0x3000);
858 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
861 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
864 smc_uart_t __iomem *up;
866 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
872 out_be16(&pinfo->smcup->smc_rbase,
873 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
874 out_be16(&pinfo->smcup->smc_tbase,
875 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
878 * In case SMC1 is being relocated...
880 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
881 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
882 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
883 out_be32(&up->smc_rstate, 0);
884 out_be32(&up->smc_tstate, 0);
885 out_be16(&up->smc_brkcr, 1); /* number of break chars */
886 out_be16(&up->smc_brkec, 0);
889 /* Set up the uart parameters in the
894 /* Using idle character time requires some additional tuning. */
895 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
896 out_be16(&up->smc_maxidl, 0x10);
897 out_be16(&up->smc_brklen, 0);
898 out_be16(&up->smc_brkec, 0);
899 out_be16(&up->smc_brkcr, 1);
901 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
903 /* Set UART mode, 8 bit, no parity, one stop.
904 * Enable receive and transmit.
906 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
908 /* Enable only rx interrupts clear all pending events. */
909 out_8(&sp->smc_smcm, 0);
910 out_8(&sp->smc_smce, 0xff);
912 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
916 * Initialize port. This is called from early_console stuff
917 * so we have to be careful here !
919 static int cpm_uart_request_port(struct uart_port *port)
921 struct uart_cpm_port *pinfo =
922 container_of(port, struct uart_cpm_port, port);
925 pr_debug("CPM uart[%d]:request port\n", port->line);
927 if (pinfo->flags & FLAG_CONSOLE)
931 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
932 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
934 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
935 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
938 ret = cpm_uart_allocbuf(pinfo, 0);
943 cpm_uart_initbd(pinfo);
945 cpm_uart_init_smc(pinfo);
947 cpm_uart_init_scc(pinfo);
952 static void cpm_uart_release_port(struct uart_port *port)
954 struct uart_cpm_port *pinfo =
955 container_of(port, struct uart_cpm_port, port);
957 if (!(pinfo->flags & FLAG_CONSOLE))
958 cpm_uart_freebuf(pinfo);
962 * Configure/autoconfigure the port.
964 static void cpm_uart_config_port(struct uart_port *port, int flags)
966 pr_debug("CPM uart[%d]:config_port\n", port->line);
968 if (flags & UART_CONFIG_TYPE) {
969 port->type = PORT_CPM;
970 cpm_uart_request_port(port);
974 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
976 * Write a string to the serial port
977 * Note that this is called with interrupts already disabled
979 static void cpm_uart_early_write(struct uart_cpm_port *pinfo,
980 const char *string, u_int count, bool handle_linefeed)
983 cbd_t __iomem *bdp, *bdbase;
984 unsigned char *cpm_outp_addr;
986 /* Get the address of the host memory buffer.
989 bdbase = pinfo->tx_bd_base;
992 * Now, do each character. This is not as bad as it looks
993 * since this is a holding FIFO and not a transmitting FIFO.
994 * We could add the complexity of filling the entire transmit
995 * buffer, but we would just wait longer between accesses......
997 for (i = 0; i < count; i++, string++) {
998 /* Wait for transmitter fifo to empty.
999 * Ready indicates output is ready, and xmt is doing
1000 * that, not that it is ready for us to send.
1002 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1005 /* Send the character out.
1006 * If the buffer address is in the CPM DPRAM, don't
1009 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
1011 *cpm_outp_addr = *string;
1013 out_be16(&bdp->cbd_datlen, 1);
1014 setbits16(&bdp->cbd_sc, BD_SC_READY);
1016 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1021 /* if a LF, also do CR... */
1022 if (handle_linefeed && *string == 10) {
1023 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1026 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
1028 *cpm_outp_addr = 13;
1030 out_be16(&bdp->cbd_datlen, 1);
1031 setbits16(&bdp->cbd_sc, BD_SC_READY);
1033 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1041 * Finally, Wait for transmitter & holding register to empty
1042 * and restore the IER
1044 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1047 pinfo->tx_cur = bdp;
1051 #ifdef CONFIG_CONSOLE_POLL
1052 /* Serial polling routines for writing and reading from the uart while
1053 * in an interrupt or debug context.
1056 #define GDB_BUF_SIZE 512 /* power of 2, please */
1058 static char poll_buf[GDB_BUF_SIZE];
1060 static int poll_chars;
1062 static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
1065 volatile cbd_t *bdp;
1068 /* Get the address of the host memory buffer.
1070 bdp = pinfo->rx_cur;
1071 while (bdp->cbd_sc & BD_SC_EMPTY)
1074 /* If the buffer address is in the CPM DPRAM, don't
1077 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1080 i = c = bdp->cbd_datlen;
1085 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
1086 bdp->cbd_sc |= BD_SC_EMPTY;
1088 if (bdp->cbd_sc & BD_SC_WRAP)
1089 bdp = pinfo->rx_bd_base;
1092 pinfo->rx_cur = (cbd_t *)bdp;
1097 static int cpm_get_poll_char(struct uart_port *port)
1099 struct uart_cpm_port *pinfo =
1100 container_of(port, struct uart_cpm_port, port);
1102 if (!serial_polled) {
1106 if (poll_chars <= 0) {
1107 poll_chars = poll_wait_key(poll_buf, pinfo);
1114 static void cpm_put_poll_char(struct uart_port *port,
1117 struct uart_cpm_port *pinfo =
1118 container_of(port, struct uart_cpm_port, port);
1122 cpm_uart_early_write(pinfo, ch, 1, false);
1124 #endif /* CONFIG_CONSOLE_POLL */
1126 static struct uart_ops cpm_uart_pops = {
1127 .tx_empty = cpm_uart_tx_empty,
1128 .set_mctrl = cpm_uart_set_mctrl,
1129 .get_mctrl = cpm_uart_get_mctrl,
1130 .stop_tx = cpm_uart_stop_tx,
1131 .start_tx = cpm_uart_start_tx,
1132 .stop_rx = cpm_uart_stop_rx,
1133 .break_ctl = cpm_uart_break_ctl,
1134 .startup = cpm_uart_startup,
1135 .shutdown = cpm_uart_shutdown,
1136 .set_termios = cpm_uart_set_termios,
1137 .type = cpm_uart_type,
1138 .release_port = cpm_uart_release_port,
1139 .request_port = cpm_uart_request_port,
1140 .config_port = cpm_uart_config_port,
1141 .verify_port = cpm_uart_verify_port,
1142 #ifdef CONFIG_CONSOLE_POLL
1143 .poll_get_char = cpm_get_poll_char,
1144 .poll_put_char = cpm_put_poll_char,
1148 struct uart_cpm_port cpm_uart_ports[UART_NR];
1150 static int cpm_uart_init_port(struct device_node *np,
1151 struct uart_cpm_port *pinfo)
1154 void __iomem *mem, *pram;
1159 data = of_get_property(np, "clock", NULL);
1161 struct clk *clk = clk_get(NULL, (const char*)data);
1166 data = of_get_property(np, "fsl,cpm-brg", &len);
1167 if (!data || len != 4) {
1168 printk(KERN_ERR "CPM UART %s has no/invalid "
1169 "fsl,cpm-brg property.\n", np->name);
1175 data = of_get_property(np, "fsl,cpm-command", &len);
1176 if (!data || len != 4) {
1177 printk(KERN_ERR "CPM UART %s has no/invalid "
1178 "fsl,cpm-command property.\n", np->name);
1181 pinfo->command = *data;
1183 mem = of_iomap(np, 0);
1187 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1188 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1190 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1191 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1192 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1193 pinfo->flags |= FLAG_SMC;
1195 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1206 pinfo->tx_nrfifos = TX_NUM_FIFO;
1207 pinfo->tx_fifosize = TX_BUF_SIZE;
1208 pinfo->rx_nrfifos = RX_NUM_FIFO;
1209 pinfo->rx_fifosize = RX_BUF_SIZE;
1211 pinfo->port.uartclk = ppc_proc_freq;
1212 pinfo->port.mapbase = (unsigned long)mem;
1213 pinfo->port.type = PORT_CPM;
1214 pinfo->port.ops = &cpm_uart_pops,
1215 pinfo->port.iotype = UPIO_MEM;
1216 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1217 spin_lock_init(&pinfo->port.lock);
1219 pinfo->port.irq = irq_of_parse_and_map(np, 0);
1220 if (pinfo->port.irq == NO_IRQ) {
1225 for (i = 0; i < NUM_GPIOS; i++) {
1228 pinfo->gpios[i] = -1;
1230 gpio = of_get_gpio(np, i);
1232 if (gpio_is_valid(gpio)) {
1233 ret = gpio_request(gpio, "cpm_uart");
1235 pr_err("can't request gpio #%d: %d\n", i, ret);
1238 if (i == GPIO_RTS || i == GPIO_DTR)
1239 ret = gpio_direction_output(gpio, 0);
1241 ret = gpio_direction_input(gpio);
1243 pr_err("can't set direction for gpio #%d: %d\n",
1248 pinfo->gpios[i] = gpio;
1252 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1256 return cpm_uart_request_port(&pinfo->port);
1259 cpm_uart_unmap_pram(pinfo, pram);
1265 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1267 * Print a string to the serial port trying not to disturb
1268 * any possible real use of the port...
1270 * Note that this is called with interrupts already disabled
1272 static void cpm_uart_console_write(struct console *co, const char *s,
1275 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1276 unsigned long flags;
1277 int nolock = oops_in_progress;
1279 if (unlikely(nolock)) {
1280 local_irq_save(flags);
1282 spin_lock_irqsave(&pinfo->port.lock, flags);
1285 cpm_uart_early_write(pinfo, s, count, true);
1287 if (unlikely(nolock)) {
1288 local_irq_restore(flags);
1290 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1295 static int __init cpm_uart_console_setup(struct console *co, char *options)
1302 struct uart_cpm_port *pinfo;
1303 struct uart_port *port;
1305 struct device_node *np = NULL;
1308 if (co->index >= UART_NR) {
1309 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1315 np = of_find_node_by_type(np, "serial");
1319 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1320 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1321 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1322 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1324 } while (i++ != co->index);
1326 pinfo = &cpm_uart_ports[co->index];
1328 pinfo->flags |= FLAG_CONSOLE;
1329 port = &pinfo->port;
1331 ret = cpm_uart_init_port(np, pinfo);
1337 uart_parse_options(options, &baud, &parity, &bits, &flow);
1339 if ((baud = uart_baudrate()) == -1)
1343 if (IS_SMC(pinfo)) {
1344 out_be16(&pinfo->smcup->smc_brkcr, 0);
1345 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1346 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1347 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1349 out_be16(&pinfo->sccup->scc_brkcr, 0);
1350 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
1351 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1352 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1355 ret = cpm_uart_allocbuf(pinfo, 1);
1360 cpm_uart_initbd(pinfo);
1363 cpm_uart_init_smc(pinfo);
1365 cpm_uart_init_scc(pinfo);
1367 uart_set_options(port, co, baud, parity, bits, flow);
1368 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1373 static struct uart_driver cpm_reg;
1374 static struct console cpm_scc_uart_console = {
1376 .write = cpm_uart_console_write,
1377 .device = uart_console_device,
1378 .setup = cpm_uart_console_setup,
1379 .flags = CON_PRINTBUFFER,
1384 static int __init cpm_uart_console_init(void)
1386 register_console(&cpm_scc_uart_console);
1390 console_initcall(cpm_uart_console_init);
1392 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1394 #define CPM_UART_CONSOLE NULL
1397 static struct uart_driver cpm_reg = {
1398 .owner = THIS_MODULE,
1399 .driver_name = "ttyCPM",
1400 .dev_name = "ttyCPM",
1401 .major = SERIAL_CPM_MAJOR,
1402 .minor = SERIAL_CPM_MINOR,
1403 .cons = CPM_UART_CONSOLE,
1407 static int probe_index;
1409 static int cpm_uart_probe(struct platform_device *ofdev)
1411 int index = probe_index++;
1412 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1415 pinfo->port.line = index;
1417 if (index >= UART_NR)
1420 platform_set_drvdata(ofdev, pinfo);
1422 /* initialize the device pointer for the port */
1423 pinfo->port.dev = &ofdev->dev;
1425 ret = cpm_uart_init_port(ofdev->dev.of_node, pinfo);
1429 return uart_add_one_port(&cpm_reg, &pinfo->port);
1432 static int cpm_uart_remove(struct platform_device *ofdev)
1434 struct uart_cpm_port *pinfo = platform_get_drvdata(ofdev);
1435 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1438 static struct of_device_id cpm_uart_match[] = {
1440 .compatible = "fsl,cpm1-smc-uart",
1443 .compatible = "fsl,cpm1-scc-uart",
1446 .compatible = "fsl,cpm2-smc-uart",
1449 .compatible = "fsl,cpm2-scc-uart",
1454 static struct platform_driver cpm_uart_driver = {
1457 .of_match_table = cpm_uart_match,
1459 .probe = cpm_uart_probe,
1460 .remove = cpm_uart_remove,
1463 static int __init cpm_uart_init(void)
1465 int ret = uart_register_driver(&cpm_reg);
1469 ret = platform_driver_register(&cpm_uart_driver);
1471 uart_unregister_driver(&cpm_reg);
1476 static void __exit cpm_uart_exit(void)
1478 platform_driver_unregister(&cpm_uart_driver);
1479 uart_unregister_driver(&cpm_reg);
1482 module_init(cpm_uart_init);
1483 module_exit(cpm_uart_exit);
1485 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1486 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1487 MODULE_LICENSE("GPL");
1488 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);