2 * linux/drivers/serial/cpm_uart.c
4 * Driver for CPM (SCC/SMC) serial ports; core driver
6 * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
7 * Based on ppc8xx.c by Thomas Gleixner
8 * Based on drivers/serial/amba.c by Russell King
10 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
11 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
13 * Copyright (C) 2004 Freescale Semiconductor, Inc.
14 * (C) 2004 Intracom, S.A.
15 * (C) 2005 MontaVista Software, Inc. by Vitaly Bordug <vbordug@ru.mvista.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/tty.h>
36 #include <linux/ioport.h>
37 #include <linux/init.h>
38 #include <linux/serial.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/device.h>
42 #include <linux/bootmem.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/fs_uart_pd.h>
48 #include <asm/delay.h>
50 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54 #include <linux/serial_core.h>
55 #include <linux/kernel.h>
59 /***********************************************************************/
61 /* Track which ports are configured as uarts */
62 int cpm_uart_port_map[UART_NR];
63 /* How many ports did we config as uarts */
66 /**************************************************************/
68 static int cpm_uart_tx_pump(struct uart_port *port);
69 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
70 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
71 static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
73 /**************************************************************/
76 /* Place-holder for board-specific stuff */
77 struct platform_device* __attribute__ ((weak)) __init
78 early_uart_get_pdev(int index)
84 void cpm_uart_count(void)
87 #ifdef CONFIG_SERIAL_CPM_SMC1
88 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
90 #ifdef CONFIG_SERIAL_CPM_SMC2
91 cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
93 #ifdef CONFIG_SERIAL_CPM_SCC1
94 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
96 #ifdef CONFIG_SERIAL_CPM_SCC2
97 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
99 #ifdef CONFIG_SERIAL_CPM_SCC3
100 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
102 #ifdef CONFIG_SERIAL_CPM_SCC4
103 cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
108 * Check, if transmit buffers are processed
110 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
112 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
113 volatile cbd_t *bdp = pinfo->tx_bd_base;
117 if (bdp->cbd_sc & BD_SC_READY)
120 if (bdp->cbd_sc & BD_SC_WRAP) {
127 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
132 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
134 /* Whee. Do nothing. */
137 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
139 /* Whee. Do nothing. */
140 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
146 static void cpm_uart_stop_tx(struct uart_port *port)
148 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
149 volatile smc_t *smcp = pinfo->smcp;
150 volatile scc_t *sccp = pinfo->sccp;
152 pr_debug("CPM uart[%d]:stop tx\n", port->line);
155 smcp->smc_smcm &= ~SMCM_TX;
157 sccp->scc_sccm &= ~UART_SCCM_TX;
163 static void cpm_uart_start_tx(struct uart_port *port)
165 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
166 volatile smc_t *smcp = pinfo->smcp;
167 volatile scc_t *sccp = pinfo->sccp;
169 pr_debug("CPM uart[%d]:start tx\n", port->line);
172 if (smcp->smc_smcm & SMCM_TX)
175 if (sccp->scc_sccm & UART_SCCM_TX)
179 if (cpm_uart_tx_pump(port) != 0) {
181 smcp->smc_smcm |= SMCM_TX;
182 smcp->smc_smcmr |= SMCMR_TEN;
184 sccp->scc_sccm |= UART_SCCM_TX;
185 pinfo->sccp->scc_gsmrl |= SCC_GSMRL_ENT;
193 static void cpm_uart_stop_rx(struct uart_port *port)
195 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
196 volatile smc_t *smcp = pinfo->smcp;
197 volatile scc_t *sccp = pinfo->sccp;
199 pr_debug("CPM uart[%d]:stop rx\n", port->line);
202 smcp->smc_smcm &= ~SMCM_RX;
204 sccp->scc_sccm &= ~UART_SCCM_RX;
208 * Enable Modem status interrupts
210 static void cpm_uart_enable_ms(struct uart_port *port)
212 pr_debug("CPM uart[%d]:enable ms\n", port->line);
218 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
220 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
221 int line = pinfo - cpm_uart_ports;
223 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
227 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
229 cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
233 * Transmit characters, refill buffer descriptor, if possible
235 static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
237 pr_debug("CPM uart[%d]:TX INT\n", port->line);
239 cpm_uart_tx_pump(port);
245 static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
248 unsigned char ch, *cp;
249 struct tty_struct *tty = port->info->tty;
250 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
255 pr_debug("CPM uart[%d]:RX INT\n", port->line);
257 /* Just loop through the closed BDs and copy the characters into
263 status = bdp->cbd_sc;
264 /* If this one is empty, return happy */
265 if (status & BD_SC_EMPTY)
268 /* get number of characters, and check spce in flip-buffer */
271 /* If we have not enough room in tty flip buffer, then we try
272 * later, which will be the next rx-interrupt or a timeout
274 if(tty_buffer_request_room(tty, i) < i) {
275 printk(KERN_WARNING "No room in flip buffer\n");
280 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
282 /* loop through the buffer */
289 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
291 if (uart_handle_sysrq_char(port, ch, regs))
295 tty_insert_flip_char(tty, ch, flg);
297 } /* End while (i--) */
299 /* This BD is ready to be used again. Clear status. get next */
300 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
301 bdp->cbd_sc |= BD_SC_EMPTY;
303 if (bdp->cbd_sc & BD_SC_WRAP)
304 bdp = pinfo->rx_bd_base;
310 /* Write back buffer pointer */
311 pinfo->rx_cur = (volatile cbd_t *) bdp;
313 /* activate BH processing */
314 tty_flip_buffer_push(tty);
318 /* Error processing */
322 if (status & BD_SC_BR)
324 if (status & BD_SC_PR)
325 port->icount.parity++;
326 if (status & BD_SC_FR)
327 port->icount.frame++;
328 if (status & BD_SC_OV)
329 port->icount.overrun++;
331 /* Mask out ignored conditions */
332 status &= port->read_status_mask;
334 /* Handle the remaining ones */
335 if (status & BD_SC_BR)
337 else if (status & BD_SC_PR)
339 else if (status & BD_SC_FR)
342 /* overrun does not affect the current character ! */
343 if (status & BD_SC_OV) {
346 /* We skip this buffer */
347 /* CHECK: Is really nothing senseful there */
348 /* ASSUMPTION: it contains nothing valid */
358 * Asynchron mode interrupt handler
360 static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
363 struct uart_port *port = (struct uart_port *)data;
364 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
365 volatile smc_t *smcp = pinfo->smcp;
366 volatile scc_t *sccp = pinfo->sccp;
368 pr_debug("CPM uart[%d]:IRQ\n", port->line);
371 events = smcp->smc_smce;
372 smcp->smc_smce = events;
373 if (events & SMCM_BRKE)
374 uart_handle_break(port);
375 if (events & SMCM_RX)
376 cpm_uart_int_rx(port, regs);
377 if (events & SMCM_TX)
378 cpm_uart_int_tx(port, regs);
380 events = sccp->scc_scce;
381 sccp->scc_scce = events;
382 if (events & UART_SCCM_BRKE)
383 uart_handle_break(port);
384 if (events & UART_SCCM_RX)
385 cpm_uart_int_rx(port, regs);
386 if (events & UART_SCCM_TX)
387 cpm_uart_int_tx(port, regs);
389 return (events) ? IRQ_HANDLED : IRQ_NONE;
392 static int cpm_uart_startup(struct uart_port *port)
395 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
396 int line = pinfo - cpm_uart_ports;
398 pr_debug("CPM uart[%d]:startup\n", port->line);
400 /* Install interrupt handler. */
401 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
407 pinfo->smcp->smc_smcm |= SMCM_RX;
408 pinfo->smcp->smc_smcmr |= SMCMR_REN;
410 pinfo->sccp->scc_sccm |= UART_SCCM_RX;
413 if (!(pinfo->flags & FLAG_CONSOLE))
414 cpm_line_cr_cmd(line,CPM_CR_INIT_TRX);
418 inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
420 set_current_state(TASK_UNINTERRUPTIBLE);
421 schedule_timeout(pinfo->wait_closing);
427 static void cpm_uart_shutdown(struct uart_port *port)
429 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
430 int line = pinfo - cpm_uart_ports;
432 pr_debug("CPM uart[%d]:shutdown\n", port->line);
434 /* free interrupt handler */
435 free_irq(port->irq, port);
437 /* If the port is not the console, disable Rx and Tx. */
438 if (!(pinfo->flags & FLAG_CONSOLE)) {
439 /* Wait for all the BDs marked sent */
440 while(!cpm_uart_tx_empty(port)) {
441 set_current_state(TASK_UNINTERRUPTIBLE);
445 if (pinfo->wait_closing)
446 cpm_uart_wait_until_send(pinfo);
450 volatile smc_t *smcp = pinfo->smcp;
451 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
452 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
454 volatile scc_t *sccp = pinfo->sccp;
455 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
456 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
459 /* Shut them really down and reinit buffer descriptors */
460 cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
461 cpm_uart_initbd(pinfo);
465 static void cpm_uart_set_termios(struct uart_port *port,
466 struct termios *termios, struct termios *old)
470 u16 cval, scval, prev_mode;
472 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
473 volatile smc_t *smcp = pinfo->smcp;
474 volatile scc_t *sccp = pinfo->sccp;
476 pr_debug("CPM uart[%d]:set_termios\n", port->line);
478 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
480 /* Character length programmed into the mode register is the
481 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
482 * 1 or 2 stop bits, minus 1.
483 * The value 'bits' counts this for us.
489 switch (termios->c_cflag & CSIZE) {
502 /* Never happens, but GCC is too dumb to figure it out */
509 if (termios->c_cflag & CSTOPB) {
510 cval |= SMCMR_SL; /* Two stops */
511 scval |= SCU_PSMR_SL;
515 if (termios->c_cflag & PARENB) {
517 scval |= SCU_PSMR_PEN;
519 if (!(termios->c_cflag & PARODD)) {
520 cval |= SMCMR_PM_EVEN;
521 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
526 * Set up parity check flag
528 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
530 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
531 if (termios->c_iflag & INPCK)
532 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
533 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
534 port->read_status_mask |= BD_SC_BR;
537 * Characters to ignore
539 port->ignore_status_mask = 0;
540 if (termios->c_iflag & IGNPAR)
541 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
542 if (termios->c_iflag & IGNBRK) {
543 port->ignore_status_mask |= BD_SC_BR;
545 * If we're ignore parity and break indicators, ignore
546 * overruns too. (For real raw support).
548 if (termios->c_iflag & IGNPAR)
549 port->ignore_status_mask |= BD_SC_OV;
552 * !!! ignore all characters if CREAD is not set
554 if ((termios->c_cflag & CREAD) == 0)
555 port->read_status_mask &= ~BD_SC_EMPTY;
557 spin_lock_irqsave(&port->lock, flags);
559 /* Start bit has not been added (so don't, because we would just
560 * subtract it later), and we need to add one for the number of
561 * stops bits (there is always at least one).
565 /* Set the mode register. We want to keep a copy of the
566 * enables, because we want to put them back if they were
569 prev_mode = smcp->smc_smcmr;
570 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
571 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
573 sccp->scc_psmr = (sbits << 12) | scval;
576 cpm_set_brg(pinfo->brg - 1, baud);
577 spin_unlock_irqrestore(&port->lock, flags);
581 static const char *cpm_uart_type(struct uart_port *port)
583 pr_debug("CPM uart[%d]:uart_type\n", port->line);
585 return port->type == PORT_CPM ? "CPM UART" : NULL;
589 * verify the new serial_struct (for TIOCSSERIAL).
591 static int cpm_uart_verify_port(struct uart_port *port,
592 struct serial_struct *ser)
596 pr_debug("CPM uart[%d]:verify_port\n", port->line);
598 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
600 if (ser->irq < 0 || ser->irq >= NR_IRQS)
602 if (ser->baud_base < 9600)
608 * Transmit characters, refill buffer descriptor, if possible
610 static int cpm_uart_tx_pump(struct uart_port *port)
615 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
616 struct circ_buf *xmit = &port->info->xmit;
618 /* Handle xon/xoff */
620 /* Pick next descriptor and fill from buffer */
623 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
627 bdp->cbd_sc |= BD_SC_READY;
629 if (bdp->cbd_sc & BD_SC_WRAP)
630 bdp = pinfo->tx_bd_base;
640 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
641 cpm_uart_stop_tx(port);
645 /* Pick next descriptor and fill from buffer */
648 while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
650 p = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
651 while (count < pinfo->tx_fifosize) {
652 *p++ = xmit->buf[xmit->tail];
653 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
656 if (xmit->head == xmit->tail)
659 bdp->cbd_datlen = count;
660 bdp->cbd_sc |= BD_SC_READY;
663 if (bdp->cbd_sc & BD_SC_WRAP)
664 bdp = pinfo->tx_bd_base;
670 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
671 uart_write_wakeup(port);
673 if (uart_circ_empty(xmit)) {
674 cpm_uart_stop_tx(port);
682 * init buffer descriptors
684 static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
690 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
692 /* Set the physical address of the host memory
693 * buffers in the buffer descriptors, and the
694 * virtual address for us to work with.
696 mem_addr = pinfo->mem_addr;
697 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
698 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
699 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
700 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
701 mem_addr += pinfo->rx_fifosize;
704 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
705 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
707 /* Set the physical address of the host memory
708 * buffers in the buffer descriptors, and the
709 * virtual address for us to work with.
711 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
712 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
713 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
714 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
715 bdp->cbd_sc = BD_SC_INTRPT;
716 mem_addr += pinfo->tx_fifosize;
719 bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr, pinfo);
720 bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
723 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
725 int line = pinfo - cpm_uart_ports;
727 volatile scc_uart_t *sup;
729 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
735 pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
736 pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
738 /* Set up the uart parameters in the
742 cpm_set_scc_fcr(sup);
744 sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
745 sup->scc_maxidl = pinfo->rx_fifosize;
754 sup->scc_char1 = 0x8000;
755 sup->scc_char2 = 0x8000;
756 sup->scc_char3 = 0x8000;
757 sup->scc_char4 = 0x8000;
758 sup->scc_char5 = 0x8000;
759 sup->scc_char6 = 0x8000;
760 sup->scc_char7 = 0x8000;
761 sup->scc_char8 = 0x8000;
762 sup->scc_rccm = 0xc0ff;
764 /* Send the CPM an initialize command.
766 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
768 /* Set UART mode, 8 bit, no parity, one stop.
769 * Enable receive and transmit.
773 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
775 /* Enable rx interrupts and clear all pending events. */
777 scp->scc_scce = 0xffff;
778 scp->scc_dsr = 0x7e7e;
779 scp->scc_psmr = 0x3000;
781 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
784 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
786 int line = pinfo - cpm_uart_ports;
788 volatile smc_uart_t *up;
790 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
796 pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
797 pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
800 * In case SMC1 is being relocated...
802 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
803 up->smc_rbptr = pinfo->smcup->smc_rbase;
804 up->smc_tbptr = pinfo->smcup->smc_tbase;
807 up->smc_brkcr = 1; /* number of break chars */
811 /* Set up the uart parameters in the
816 /* Using idle charater time requires some additional tuning. */
817 up->smc_mrblr = pinfo->rx_fifosize;
818 up->smc_maxidl = pinfo->rx_fifosize;
823 cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
825 /* Set UART mode, 8 bit, no parity, one stop.
826 * Enable receive and transmit.
828 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
830 /* Enable only rx interrupts clear all pending events. */
834 sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
838 * Initialize port. This is called from early_console stuff
839 * so we have to be careful here !
841 static int cpm_uart_request_port(struct uart_port *port)
843 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
846 pr_debug("CPM uart[%d]:request port\n", port->line);
848 if (pinfo->flags & FLAG_CONSOLE)
852 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
853 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
855 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
856 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
859 ret = cpm_uart_allocbuf(pinfo, 0);
864 cpm_uart_initbd(pinfo);
866 cpm_uart_init_smc(pinfo);
868 cpm_uart_init_scc(pinfo);
873 static void cpm_uart_release_port(struct uart_port *port)
875 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
877 if (!(pinfo->flags & FLAG_CONSOLE))
878 cpm_uart_freebuf(pinfo);
882 * Configure/autoconfigure the port.
884 static void cpm_uart_config_port(struct uart_port *port, int flags)
886 pr_debug("CPM uart[%d]:config_port\n", port->line);
888 if (flags & UART_CONFIG_TYPE) {
889 port->type = PORT_CPM;
890 cpm_uart_request_port(port);
893 static struct uart_ops cpm_uart_pops = {
894 .tx_empty = cpm_uart_tx_empty,
895 .set_mctrl = cpm_uart_set_mctrl,
896 .get_mctrl = cpm_uart_get_mctrl,
897 .stop_tx = cpm_uart_stop_tx,
898 .start_tx = cpm_uart_start_tx,
899 .stop_rx = cpm_uart_stop_rx,
900 .enable_ms = cpm_uart_enable_ms,
901 .break_ctl = cpm_uart_break_ctl,
902 .startup = cpm_uart_startup,
903 .shutdown = cpm_uart_shutdown,
904 .set_termios = cpm_uart_set_termios,
905 .type = cpm_uart_type,
906 .release_port = cpm_uart_release_port,
907 .request_port = cpm_uart_request_port,
908 .config_port = cpm_uart_config_port,
909 .verify_port = cpm_uart_verify_port,
912 struct uart_cpm_port cpm_uart_ports[UART_NR] = {
916 .ops = &cpm_uart_pops,
918 .lock = SPIN_LOCK_UNLOCKED,
921 .tx_nrfifos = TX_NUM_FIFO,
922 .tx_fifosize = TX_BUF_SIZE,
923 .rx_nrfifos = RX_NUM_FIFO,
924 .rx_fifosize = RX_BUF_SIZE,
925 .set_lineif = smc1_lineif,
930 .ops = &cpm_uart_pops,
932 .lock = SPIN_LOCK_UNLOCKED,
935 .tx_nrfifos = TX_NUM_FIFO,
936 .tx_fifosize = TX_BUF_SIZE,
937 .rx_nrfifos = RX_NUM_FIFO,
938 .rx_fifosize = RX_BUF_SIZE,
939 .set_lineif = smc2_lineif,
940 #ifdef CONFIG_SERIAL_CPM_ALT_SMC2
947 .ops = &cpm_uart_pops,
949 .lock = SPIN_LOCK_UNLOCKED,
951 .tx_nrfifos = TX_NUM_FIFO,
952 .tx_fifosize = TX_BUF_SIZE,
953 .rx_nrfifos = RX_NUM_FIFO,
954 .rx_fifosize = RX_BUF_SIZE,
955 .set_lineif = scc1_lineif,
956 .wait_closing = SCC_WAIT_CLOSING,
961 .ops = &cpm_uart_pops,
963 .lock = SPIN_LOCK_UNLOCKED,
965 .tx_nrfifos = TX_NUM_FIFO,
966 .tx_fifosize = TX_BUF_SIZE,
967 .rx_nrfifos = RX_NUM_FIFO,
968 .rx_fifosize = RX_BUF_SIZE,
969 .set_lineif = scc2_lineif,
970 .wait_closing = SCC_WAIT_CLOSING,
975 .ops = &cpm_uart_pops,
977 .lock = SPIN_LOCK_UNLOCKED,
979 .tx_nrfifos = TX_NUM_FIFO,
980 .tx_fifosize = TX_BUF_SIZE,
981 .rx_nrfifos = RX_NUM_FIFO,
982 .rx_fifosize = RX_BUF_SIZE,
983 .set_lineif = scc3_lineif,
984 .wait_closing = SCC_WAIT_CLOSING,
989 .ops = &cpm_uart_pops,
991 .lock = SPIN_LOCK_UNLOCKED,
993 .tx_nrfifos = TX_NUM_FIFO,
994 .tx_fifosize = TX_BUF_SIZE,
995 .rx_nrfifos = RX_NUM_FIFO,
996 .rx_fifosize = RX_BUF_SIZE,
997 .set_lineif = scc4_lineif,
998 .wait_closing = SCC_WAIT_CLOSING,
1002 int cpm_uart_drv_get_platform_data(struct platform_device *pdev, int is_con)
1005 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1006 int idx = pdata->fs_no; /* It is UART_SMCx or UART_SCCx index */
1007 struct uart_cpm_port *pinfo;
1011 for (line=0; line<UART_NR && cpm_uart_port_map[line]!=pdata->fs_no; line++);
1013 pinfo = (struct uart_cpm_port *) &cpm_uart_ports[idx];
1015 pinfo->brg = pdata->brg;
1018 pinfo->port.line = line;
1019 pinfo->port.flags = UPF_BOOT_AUTOCONF;
1022 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs")))
1026 if (!(r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram")))
1030 if(idx > fsid_smc2_uart) {
1031 pinfo->sccp = (scc_t *)mem;
1032 pinfo->sccup = (scc_uart_t *)pram;
1034 pinfo->smcp = (smc_t *)mem;
1035 pinfo->smcup = (smc_uart_t *)pram;
1037 pinfo->tx_nrfifos = pdata->tx_num_fifo;
1038 pinfo->tx_fifosize = pdata->tx_buf_size;
1040 pinfo->rx_nrfifos = pdata->rx_num_fifo;
1041 pinfo->rx_fifosize = pdata->rx_buf_size;
1043 pinfo->port.uartclk = pdata->uart_clk;
1044 pinfo->port.mapbase = (unsigned long)mem;
1045 pinfo->port.irq = platform_get_irq(pdev, 0);
1050 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1052 * Print a string to the serial port trying not to disturb
1053 * any possible real use of the port...
1055 * Note that this is called with interrupts already disabled
1057 static void cpm_uart_console_write(struct console *co, const char *s,
1060 struct uart_cpm_port *pinfo =
1061 &cpm_uart_ports[cpm_uart_port_map[co->index]];
1063 volatile cbd_t *bdp, *bdbase;
1064 volatile unsigned char *cp;
1066 /* Get the address of the host memory buffer.
1068 bdp = pinfo->tx_cur;
1069 bdbase = pinfo->tx_bd_base;
1072 * Now, do each character. This is not as bad as it looks
1073 * since this is a holding FIFO and not a transmitting FIFO.
1074 * We could add the complexity of filling the entire transmit
1075 * buffer, but we would just wait longer between accesses......
1077 for (i = 0; i < count; i++, s++) {
1078 /* Wait for transmitter fifo to empty.
1079 * Ready indicates output is ready, and xmt is doing
1080 * that, not that it is ready for us to send.
1082 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1085 /* Send the character out.
1086 * If the buffer address is in the CPM DPRAM, don't
1089 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1093 bdp->cbd_datlen = 1;
1094 bdp->cbd_sc |= BD_SC_READY;
1096 if (bdp->cbd_sc & BD_SC_WRAP)
1101 /* if a LF, also do CR... */
1103 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1106 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1109 bdp->cbd_datlen = 1;
1110 bdp->cbd_sc |= BD_SC_READY;
1112 if (bdp->cbd_sc & BD_SC_WRAP)
1120 * Finally, Wait for transmitter & holding register to empty
1121 * and restore the IER
1123 while ((bdp->cbd_sc & BD_SC_READY) != 0)
1126 pinfo->tx_cur = (volatile cbd_t *) bdp;
1130 static int __init cpm_uart_console_setup(struct console *co, char *options)
1132 struct uart_port *port;
1133 struct uart_cpm_port *pinfo;
1140 struct fs_uart_platform_info *pdata;
1141 struct platform_device* pdev = early_uart_get_pdev(co->index);
1144 (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
1145 pinfo = (struct uart_cpm_port *)port;
1147 pr_info("cpm_uart: console: compat mode\n");
1148 /* compatibility - will be cleaned up */
1149 cpm_uart_init_portdesc();
1151 if (pinfo->set_lineif)
1152 pinfo->set_lineif(pinfo);
1154 pdata = pdev->dev.platform_data;
1156 if (pdata->init_ioports)
1157 pdata->init_ioports();
1159 cpm_uart_drv_get_platform_data(pdev, 1);
1162 pinfo->flags |= FLAG_CONSOLE;
1165 uart_parse_options(options, &baud, &parity, &bits, &flow);
1167 bd_t *bd = (bd_t *) __res;
1169 if (bd->bi_baudrate)
1170 baud = bd->bi_baudrate;
1175 if (IS_SMC(pinfo)) {
1176 pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
1177 pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1179 pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
1180 pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1183 ret = cpm_uart_allocbuf(pinfo, 1);
1188 cpm_uart_initbd(pinfo);
1191 cpm_uart_init_smc(pinfo);
1193 cpm_uart_init_scc(pinfo);
1195 uart_set_options(port, co, baud, parity, bits, flow);
1200 static struct uart_driver cpm_reg;
1201 static struct console cpm_scc_uart_console = {
1203 .write = cpm_uart_console_write,
1204 .device = uart_console_device,
1205 .setup = cpm_uart_console_setup,
1206 .flags = CON_PRINTBUFFER,
1211 int __init cpm_uart_console_init(void)
1213 register_console(&cpm_scc_uart_console);
1217 console_initcall(cpm_uart_console_init);
1219 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1221 #define CPM_UART_CONSOLE NULL
1224 static struct uart_driver cpm_reg = {
1225 .owner = THIS_MODULE,
1226 .driver_name = "ttyCPM",
1227 .dev_name = "ttyCPM",
1228 .major = SERIAL_CPM_MAJOR,
1229 .minor = SERIAL_CPM_MINOR,
1230 .cons = CPM_UART_CONSOLE,
1232 static int cpm_uart_drv_probe(struct device *dev)
1234 struct platform_device *pdev = to_platform_device(dev);
1235 struct fs_uart_platform_info *pdata;
1239 printk(KERN_ERR"CPM UART: platform data missing!\n");
1243 pdata = pdev->dev.platform_data;
1244 pr_debug("cpm_uart_drv_probe: Adding CPM UART %d\n",
1245 cpm_uart_port_map[pdata->fs_no]);
1247 if ((ret = cpm_uart_drv_get_platform_data(pdev, 0)))
1250 if (pdata->init_ioports)
1251 pdata->init_ioports();
1253 ret = uart_add_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1258 static int cpm_uart_drv_remove(struct device *dev)
1260 struct platform_device *pdev = to_platform_device(dev);
1261 struct fs_uart_platform_info *pdata = pdev->dev.platform_data;
1263 pr_debug("cpm_uart_drv_remove: Removing CPM UART %d\n",
1264 cpm_uart_port_map[pdata->fs_no]);
1266 uart_remove_one_port(&cpm_reg, &cpm_uart_ports[pdata->fs_no].port);
1270 static struct device_driver cpm_smc_uart_driver = {
1271 .name = "fsl-cpm-smc:uart",
1272 .bus = &platform_bus_type,
1273 .probe = cpm_uart_drv_probe,
1274 .remove = cpm_uart_drv_remove,
1277 static struct device_driver cpm_scc_uart_driver = {
1278 .name = "fsl-cpm-scc:uart",
1279 .bus = &platform_bus_type,
1280 .probe = cpm_uart_drv_probe,
1281 .remove = cpm_uart_drv_remove,
1285 This is supposed to match uart devices on platform bus,
1287 static int match_is_uart (struct device* dev, void* data)
1289 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
1291 /* this was setfunc as uart */
1292 if(strstr(pdev->name,":uart")) {
1299 static int cpm_uart_init(void) {
1304 printk(KERN_INFO "Serial: CPM driver $Revision: 0.02 $\n");
1306 /* lookup the bus for uart devices */
1307 dev = bus_find_device(&platform_bus_type, NULL, 0, match_is_uart);
1309 /* There are devices on the bus - all should be OK */
1312 cpm_reg.nr = cpm_uart_nr;
1314 if (!(ret = uart_register_driver(&cpm_reg))) {
1315 if ((ret = driver_register(&cpm_smc_uart_driver))) {
1316 uart_unregister_driver(&cpm_reg);
1319 if ((ret = driver_register(&cpm_scc_uart_driver))) {
1320 driver_unregister(&cpm_scc_uart_driver);
1321 uart_unregister_driver(&cpm_reg);
1325 /* No capable platform devices found - falling back to legacy mode */
1326 pr_info("cpm_uart: WARNING: no UART devices found on platform bus!\n");
1328 "cpm_uart: the driver will guess configuration, but this mode is no longer supported.\n");
1329 #ifndef CONFIG_SERIAL_CPM_CONSOLE
1330 ret = cpm_uart_init_portdesc();
1335 cpm_reg.nr = cpm_uart_nr;
1336 ret = uart_register_driver(&cpm_reg);
1341 for (i = 0; i < cpm_uart_nr; i++) {
1342 int con = cpm_uart_port_map[i];
1343 cpm_uart_ports[con].port.line = i;
1344 cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
1345 uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
1352 static void __exit cpm_uart_exit(void)
1354 driver_unregister(&cpm_scc_uart_driver);
1355 driver_unregister(&cpm_smc_uart_driver);
1356 uart_unregister_driver(&cpm_reg);
1359 module_init(cpm_uart_init);
1360 module_exit(cpm_uart_exit);
1362 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1363 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1364 MODULE_LICENSE("GPL");
1365 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);