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/ioport.h>
35 #include <linux/init.h>
36 #include <linux/serial.h>
37 #include <linux/console.h>
38 #include <linux/sysrq.h>
39 #include <linux/device.h>
40 #include <linux/bootmem.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/fs_uart_pd.h>
43 #include <linux/of_platform.h>
44 #include <linux/gpio.h>
45 #include <linux/of_gpio.h>
46 #include <linux/clk.h>
50 #include <asm/delay.h>
51 #include <asm/fs_pd.h>
54 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
58 #include <linux/serial_core.h>
59 #include <linux/kernel.h>
64 /**************************************************************/
66 static int cpm_uart_tx_pump(struct uart_port
*port
);
67 static void cpm_uart_init_smc(struct uart_cpm_port
*pinfo
);
68 static void cpm_uart_init_scc(struct uart_cpm_port
*pinfo
);
69 static void cpm_uart_initbd(struct uart_cpm_port
*pinfo
);
71 /**************************************************************/
73 #define HW_BUF_SPD_THRESHOLD 9600
76 * Check, if transmit buffers are processed
78 static unsigned int cpm_uart_tx_empty(struct uart_port
*port
)
80 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
81 cbd_t __iomem
*bdp
= pinfo
->tx_bd_base
;
85 if (in_be16(&bdp
->cbd_sc
) & BD_SC_READY
)
88 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
) {
95 pr_debug("CPM uart[%d]:tx_empty: %d\n", port
->line
, ret
);
100 static void cpm_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
102 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
104 if (pinfo
->gpios
[GPIO_RTS
] >= 0)
105 gpio_set_value(pinfo
->gpios
[GPIO_RTS
], !(mctrl
& TIOCM_RTS
));
107 if (pinfo
->gpios
[GPIO_DTR
] >= 0)
108 gpio_set_value(pinfo
->gpios
[GPIO_DTR
], !(mctrl
& TIOCM_DTR
));
111 static unsigned int cpm_uart_get_mctrl(struct uart_port
*port
)
113 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
114 unsigned int mctrl
= TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
116 if (pinfo
->gpios
[GPIO_CTS
] >= 0) {
117 if (gpio_get_value(pinfo
->gpios
[GPIO_CTS
]))
121 if (pinfo
->gpios
[GPIO_DSR
] >= 0) {
122 if (gpio_get_value(pinfo
->gpios
[GPIO_DSR
]))
126 if (pinfo
->gpios
[GPIO_DCD
] >= 0) {
127 if (gpio_get_value(pinfo
->gpios
[GPIO_DCD
]))
131 if (pinfo
->gpios
[GPIO_RI
] >= 0) {
132 if (!gpio_get_value(pinfo
->gpios
[GPIO_RI
]))
142 static void cpm_uart_stop_tx(struct uart_port
*port
)
144 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
145 smc_t __iomem
*smcp
= pinfo
->smcp
;
146 scc_t __iomem
*sccp
= pinfo
->sccp
;
148 pr_debug("CPM uart[%d]:stop tx\n", port
->line
);
151 clrbits8(&smcp
->smc_smcm
, SMCM_TX
);
153 clrbits16(&sccp
->scc_sccm
, UART_SCCM_TX
);
159 static void cpm_uart_start_tx(struct uart_port
*port
)
161 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
162 smc_t __iomem
*smcp
= pinfo
->smcp
;
163 scc_t __iomem
*sccp
= pinfo
->sccp
;
165 pr_debug("CPM uart[%d]:start tx\n", port
->line
);
168 if (in_8(&smcp
->smc_smcm
) & SMCM_TX
)
171 if (in_be16(&sccp
->scc_sccm
) & UART_SCCM_TX
)
175 if (cpm_uart_tx_pump(port
) != 0) {
177 setbits8(&smcp
->smc_smcm
, SMCM_TX
);
179 setbits16(&sccp
->scc_sccm
, UART_SCCM_TX
);
187 static void cpm_uart_stop_rx(struct uart_port
*port
)
189 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
190 smc_t __iomem
*smcp
= pinfo
->smcp
;
191 scc_t __iomem
*sccp
= pinfo
->sccp
;
193 pr_debug("CPM uart[%d]:stop rx\n", port
->line
);
196 clrbits8(&smcp
->smc_smcm
, SMCM_RX
);
198 clrbits16(&sccp
->scc_sccm
, UART_SCCM_RX
);
202 * Enable Modem status interrupts
204 static void cpm_uart_enable_ms(struct uart_port
*port
)
206 pr_debug("CPM uart[%d]:enable ms\n", port
->line
);
212 static void cpm_uart_break_ctl(struct uart_port
*port
, int break_state
)
214 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
216 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port
->line
,
220 cpm_line_cr_cmd(pinfo
, CPM_CR_STOP_TX
);
222 cpm_line_cr_cmd(pinfo
, CPM_CR_RESTART_TX
);
226 * Transmit characters, refill buffer descriptor, if possible
228 static void cpm_uart_int_tx(struct uart_port
*port
)
230 pr_debug("CPM uart[%d]:TX INT\n", port
->line
);
232 cpm_uart_tx_pump(port
);
235 #ifdef CONFIG_CONSOLE_POLL
236 static int serial_polled
;
242 static void cpm_uart_int_rx(struct uart_port
*port
)
247 struct tty_struct
*tty
= port
->state
->port
.tty
;
248 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
253 pr_debug("CPM uart[%d]:RX INT\n", port
->line
);
255 /* Just loop through the closed BDs and copy the characters into
260 #ifdef CONFIG_CONSOLE_POLL
261 if (unlikely(serial_polled
)) {
267 status
= in_be16(&bdp
->cbd_sc
);
268 /* If this one is empty, return happy */
269 if (status
& BD_SC_EMPTY
)
272 /* get number of characters, and check spce in flip-buffer */
273 i
= in_be16(&bdp
->cbd_datlen
);
275 /* If we have not enough room in tty flip buffer, then we try
276 * later, which will be the next rx-interrupt or a timeout
278 if(tty_buffer_request_room(tty
, i
) < i
) {
279 printk(KERN_WARNING
"No room in flip buffer\n");
284 cp
= cpm2cpu_addr(in_be32(&bdp
->cbd_bufaddr
), pinfo
);
286 /* loop through the buffer */
293 (BD_SC_BR
| BD_SC_FR
| BD_SC_PR
| BD_SC_OV
))
295 if (uart_handle_sysrq_char(port
, ch
))
297 #ifdef CONFIG_CONSOLE_POLL
298 if (unlikely(serial_polled
)) {
304 tty_insert_flip_char(tty
, ch
, flg
);
306 } /* End while (i--) */
308 /* This BD is ready to be used again. Clear status. get next */
309 clrbits16(&bdp
->cbd_sc
, BD_SC_BR
| BD_SC_FR
| BD_SC_PR
|
310 BD_SC_OV
| BD_SC_ID
);
311 setbits16(&bdp
->cbd_sc
, BD_SC_EMPTY
);
313 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
)
314 bdp
= pinfo
->rx_bd_base
;
320 /* Write back buffer pointer */
323 /* activate BH processing */
324 tty_flip_buffer_push(tty
);
328 /* Error processing */
332 if (status
& BD_SC_BR
)
334 if (status
& BD_SC_PR
)
335 port
->icount
.parity
++;
336 if (status
& BD_SC_FR
)
337 port
->icount
.frame
++;
338 if (status
& BD_SC_OV
)
339 port
->icount
.overrun
++;
341 /* Mask out ignored conditions */
342 status
&= port
->read_status_mask
;
344 /* Handle the remaining ones */
345 if (status
& BD_SC_BR
)
347 else if (status
& BD_SC_PR
)
349 else if (status
& BD_SC_FR
)
352 /* overrun does not affect the current character ! */
353 if (status
& BD_SC_OV
) {
356 /* We skip this buffer */
357 /* CHECK: Is really nothing senseful there */
358 /* ASSUMPTION: it contains nothing valid */
368 * Asynchron mode interrupt handler
370 static irqreturn_t
cpm_uart_int(int irq
, void *data
)
373 struct uart_port
*port
= data
;
374 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
375 smc_t __iomem
*smcp
= pinfo
->smcp
;
376 scc_t __iomem
*sccp
= pinfo
->sccp
;
378 pr_debug("CPM uart[%d]:IRQ\n", port
->line
);
381 events
= in_8(&smcp
->smc_smce
);
382 out_8(&smcp
->smc_smce
, events
);
383 if (events
& SMCM_BRKE
)
384 uart_handle_break(port
);
385 if (events
& SMCM_RX
)
386 cpm_uart_int_rx(port
);
387 if (events
& SMCM_TX
)
388 cpm_uart_int_tx(port
);
390 events
= in_be16(&sccp
->scc_scce
);
391 out_be16(&sccp
->scc_scce
, events
);
392 if (events
& UART_SCCM_BRKE
)
393 uart_handle_break(port
);
394 if (events
& UART_SCCM_RX
)
395 cpm_uart_int_rx(port
);
396 if (events
& UART_SCCM_TX
)
397 cpm_uart_int_tx(port
);
399 return (events
) ? IRQ_HANDLED
: IRQ_NONE
;
402 static int cpm_uart_startup(struct uart_port
*port
)
405 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
407 pr_debug("CPM uart[%d]:startup\n", port
->line
);
409 /* If the port is not the console, make sure rx is disabled. */
410 if (!(pinfo
->flags
& FLAG_CONSOLE
)) {
411 /* Disable UART rx */
413 clrbits16(&pinfo
->smcp
->smc_smcmr
, SMCMR_REN
);
414 clrbits8(&pinfo
->smcp
->smc_smcm
, SMCM_RX
);
416 clrbits32(&pinfo
->sccp
->scc_gsmrl
, SCC_GSMRL_ENR
);
417 clrbits16(&pinfo
->sccp
->scc_sccm
, UART_SCCM_RX
);
419 cpm_line_cr_cmd(pinfo
, CPM_CR_INIT_TRX
);
421 /* Install interrupt handler. */
422 retval
= request_irq(port
->irq
, cpm_uart_int
, 0, "cpm_uart", port
);
428 setbits8(&pinfo
->smcp
->smc_smcm
, SMCM_RX
);
429 setbits16(&pinfo
->smcp
->smc_smcmr
, (SMCMR_REN
| SMCMR_TEN
));
431 setbits16(&pinfo
->sccp
->scc_sccm
, UART_SCCM_RX
);
432 setbits32(&pinfo
->sccp
->scc_gsmrl
, (SCC_GSMRL_ENR
| SCC_GSMRL_ENT
));
438 inline void cpm_uart_wait_until_send(struct uart_cpm_port
*pinfo
)
440 set_current_state(TASK_UNINTERRUPTIBLE
);
441 schedule_timeout(pinfo
->wait_closing
);
447 static void cpm_uart_shutdown(struct uart_port
*port
)
449 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
451 pr_debug("CPM uart[%d]:shutdown\n", port
->line
);
453 /* free interrupt handler */
454 free_irq(port
->irq
, port
);
456 /* If the port is not the console, disable Rx and Tx. */
457 if (!(pinfo
->flags
& FLAG_CONSOLE
)) {
458 /* Wait for all the BDs marked sent */
459 while(!cpm_uart_tx_empty(port
)) {
460 set_current_state(TASK_UNINTERRUPTIBLE
);
464 if (pinfo
->wait_closing
)
465 cpm_uart_wait_until_send(pinfo
);
469 smc_t __iomem
*smcp
= pinfo
->smcp
;
470 clrbits16(&smcp
->smc_smcmr
, SMCMR_REN
| SMCMR_TEN
);
471 clrbits8(&smcp
->smc_smcm
, SMCM_RX
| SMCM_TX
);
473 scc_t __iomem
*sccp
= pinfo
->sccp
;
474 clrbits32(&sccp
->scc_gsmrl
, SCC_GSMRL_ENR
| SCC_GSMRL_ENT
);
475 clrbits16(&sccp
->scc_sccm
, UART_SCCM_TX
| UART_SCCM_RX
);
478 /* Shut them really down and reinit buffer descriptors */
480 out_be16(&pinfo
->smcup
->smc_brkcr
, 0);
481 cpm_line_cr_cmd(pinfo
, CPM_CR_STOP_TX
);
483 out_be16(&pinfo
->sccup
->scc_brkcr
, 0);
484 cpm_line_cr_cmd(pinfo
, CPM_CR_GRA_STOP_TX
);
487 cpm_uart_initbd(pinfo
);
491 static void cpm_uart_set_termios(struct uart_port
*port
,
492 struct ktermios
*termios
,
493 struct ktermios
*old
)
497 u16 cval
, scval
, prev_mode
;
499 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
500 smc_t __iomem
*smcp
= pinfo
->smcp
;
501 scc_t __iomem
*sccp
= pinfo
->sccp
;
503 pr_debug("CPM uart[%d]:set_termios\n", port
->line
);
505 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
506 if (baud
<= HW_BUF_SPD_THRESHOLD
||
507 (pinfo
->port
.state
&& pinfo
->port
.state
->port
.tty
->low_latency
))
508 pinfo
->rx_fifosize
= 1;
510 pinfo
->rx_fifosize
= RX_BUF_SIZE
;
512 /* Character length programmed into the mode register is the
513 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
514 * 1 or 2 stop bits, minus 1.
515 * The value 'bits' counts this for us.
521 switch (termios
->c_cflag
& CSIZE
) {
534 /* Never happens, but GCC is too dumb to figure it out */
541 if (termios
->c_cflag
& CSTOPB
) {
542 cval
|= SMCMR_SL
; /* Two stops */
543 scval
|= SCU_PSMR_SL
;
547 if (termios
->c_cflag
& PARENB
) {
549 scval
|= SCU_PSMR_PEN
;
551 if (!(termios
->c_cflag
& PARODD
)) {
552 cval
|= SMCMR_PM_EVEN
;
553 scval
|= (SCU_PSMR_REVP
| SCU_PSMR_TEVP
);
560 uart_update_timeout(port
, termios
->c_cflag
, baud
);
563 * Set up parity check flag
565 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
567 port
->read_status_mask
= (BD_SC_EMPTY
| BD_SC_OV
);
568 if (termios
->c_iflag
& INPCK
)
569 port
->read_status_mask
|= BD_SC_FR
| BD_SC_PR
;
570 if ((termios
->c_iflag
& BRKINT
) || (termios
->c_iflag
& PARMRK
))
571 port
->read_status_mask
|= BD_SC_BR
;
574 * Characters to ignore
576 port
->ignore_status_mask
= 0;
577 if (termios
->c_iflag
& IGNPAR
)
578 port
->ignore_status_mask
|= BD_SC_PR
| BD_SC_FR
;
579 if (termios
->c_iflag
& IGNBRK
) {
580 port
->ignore_status_mask
|= BD_SC_BR
;
582 * If we're ignore parity and break indicators, ignore
583 * overruns too. (For real raw support).
585 if (termios
->c_iflag
& IGNPAR
)
586 port
->ignore_status_mask
|= BD_SC_OV
;
589 * !!! ignore all characters if CREAD is not set
591 if ((termios
->c_cflag
& CREAD
) == 0)
592 port
->read_status_mask
&= ~BD_SC_EMPTY
;
594 spin_lock_irqsave(&port
->lock
, flags
);
596 /* Start bit has not been added (so don't, because we would just
597 * subtract it later), and we need to add one for the number of
598 * stops bits (there is always at least one).
603 * MRBLR can be changed while an SMC/SCC is operating only
604 * if it is done in a single bus cycle with one 16-bit move
605 * (not two 8-bit bus cycles back-to-back). This occurs when
606 * the cp shifts control to the next RxBD, so the change does
607 * not take effect immediately. To guarantee the exact RxBD
608 * on which the change occurs, change MRBLR only while the
609 * SMC/SCC receiver is disabled.
611 out_be16(&pinfo
->smcup
->smc_mrblr
, pinfo
->rx_fifosize
);
613 /* Set the mode register. We want to keep a copy of the
614 * enables, because we want to put them back if they were
617 prev_mode
= in_be16(&smcp
->smc_smcmr
) & (SMCMR_REN
| SMCMR_TEN
);
618 /* Output in *one* operation, so we don't interrupt RX/TX if they
619 * were already enabled. */
620 out_be16(&smcp
->smc_smcmr
, smcr_mk_clen(bits
) | cval
|
621 SMCMR_SM_UART
| prev_mode
);
623 out_be16(&pinfo
->sccup
->scc_genscc
.scc_mrblr
, pinfo
->rx_fifosize
);
624 out_be16(&sccp
->scc_psmr
, (sbits
<< 12) | scval
);
628 clk_set_rate(pinfo
->clk
, baud
);
630 cpm_set_brg(pinfo
->brg
- 1, baud
);
631 spin_unlock_irqrestore(&port
->lock
, flags
);
634 static const char *cpm_uart_type(struct uart_port
*port
)
636 pr_debug("CPM uart[%d]:uart_type\n", port
->line
);
638 return port
->type
== PORT_CPM
? "CPM UART" : NULL
;
642 * verify the new serial_struct (for TIOCSSERIAL).
644 static int cpm_uart_verify_port(struct uart_port
*port
,
645 struct serial_struct
*ser
)
649 pr_debug("CPM uart[%d]:verify_port\n", port
->line
);
651 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_CPM
)
653 if (ser
->irq
< 0 || ser
->irq
>= nr_irqs
)
655 if (ser
->baud_base
< 9600)
661 * Transmit characters, refill buffer descriptor, if possible
663 static int cpm_uart_tx_pump(struct uart_port
*port
)
668 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
669 struct circ_buf
*xmit
= &port
->state
->xmit
;
671 /* Handle xon/xoff */
673 /* Pick next descriptor and fill from buffer */
676 p
= cpm2cpu_addr(in_be32(&bdp
->cbd_bufaddr
), pinfo
);
680 out_be16(&bdp
->cbd_datlen
, 1);
681 setbits16(&bdp
->cbd_sc
, BD_SC_READY
);
683 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
)
684 bdp
= pinfo
->tx_bd_base
;
694 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
695 cpm_uart_stop_tx(port
);
699 /* Pick next descriptor and fill from buffer */
702 while (!(in_be16(&bdp
->cbd_sc
) & BD_SC_READY
) &&
703 xmit
->tail
!= xmit
->head
) {
705 p
= cpm2cpu_addr(in_be32(&bdp
->cbd_bufaddr
), pinfo
);
706 while (count
< pinfo
->tx_fifosize
) {
707 *p
++ = xmit
->buf
[xmit
->tail
];
708 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
711 if (xmit
->head
== xmit
->tail
)
714 out_be16(&bdp
->cbd_datlen
, count
);
715 setbits16(&bdp
->cbd_sc
, BD_SC_READY
);
717 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
)
718 bdp
= pinfo
->tx_bd_base
;
724 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
725 uart_write_wakeup(port
);
727 if (uart_circ_empty(xmit
)) {
728 cpm_uart_stop_tx(port
);
736 * init buffer descriptors
738 static void cpm_uart_initbd(struct uart_cpm_port
*pinfo
)
744 pr_debug("CPM uart[%d]:initbd\n", pinfo
->port
.line
);
746 /* Set the physical address of the host memory
747 * buffers in the buffer descriptors, and the
748 * virtual address for us to work with.
750 mem_addr
= pinfo
->mem_addr
;
751 bdp
= pinfo
->rx_cur
= pinfo
->rx_bd_base
;
752 for (i
= 0; i
< (pinfo
->rx_nrfifos
- 1); i
++, bdp
++) {
753 out_be32(&bdp
->cbd_bufaddr
, cpu2cpm_addr(mem_addr
, pinfo
));
754 out_be16(&bdp
->cbd_sc
, BD_SC_EMPTY
| BD_SC_INTRPT
);
755 mem_addr
+= pinfo
->rx_fifosize
;
758 out_be32(&bdp
->cbd_bufaddr
, cpu2cpm_addr(mem_addr
, pinfo
));
759 out_be16(&bdp
->cbd_sc
, BD_SC_WRAP
| BD_SC_EMPTY
| BD_SC_INTRPT
);
761 /* Set the physical address of the host memory
762 * buffers in the buffer descriptors, and the
763 * virtual address for us to work with.
765 mem_addr
= pinfo
->mem_addr
+ L1_CACHE_ALIGN(pinfo
->rx_nrfifos
* pinfo
->rx_fifosize
);
766 bdp
= pinfo
->tx_cur
= pinfo
->tx_bd_base
;
767 for (i
= 0; i
< (pinfo
->tx_nrfifos
- 1); i
++, bdp
++) {
768 out_be32(&bdp
->cbd_bufaddr
, cpu2cpm_addr(mem_addr
, pinfo
));
769 out_be16(&bdp
->cbd_sc
, BD_SC_INTRPT
);
770 mem_addr
+= pinfo
->tx_fifosize
;
773 out_be32(&bdp
->cbd_bufaddr
, cpu2cpm_addr(mem_addr
, pinfo
));
774 out_be16(&bdp
->cbd_sc
, BD_SC_WRAP
| BD_SC_INTRPT
);
777 static void cpm_uart_init_scc(struct uart_cpm_port
*pinfo
)
780 scc_uart_t __iomem
*sup
;
782 pr_debug("CPM uart[%d]:init_scc\n", pinfo
->port
.line
);
788 out_be16(&pinfo
->sccup
->scc_genscc
.scc_rbase
,
789 (u8 __iomem
*)pinfo
->rx_bd_base
- DPRAM_BASE
);
790 out_be16(&pinfo
->sccup
->scc_genscc
.scc_tbase
,
791 (u8 __iomem
*)pinfo
->tx_bd_base
- DPRAM_BASE
);
793 /* Set up the uart parameters in the
797 cpm_set_scc_fcr(sup
);
799 out_be16(&sup
->scc_genscc
.scc_mrblr
, pinfo
->rx_fifosize
);
800 out_be16(&sup
->scc_maxidl
, pinfo
->rx_fifosize
);
801 out_be16(&sup
->scc_brkcr
, 1);
802 out_be16(&sup
->scc_parec
, 0);
803 out_be16(&sup
->scc_frmec
, 0);
804 out_be16(&sup
->scc_nosec
, 0);
805 out_be16(&sup
->scc_brkec
, 0);
806 out_be16(&sup
->scc_uaddr1
, 0);
807 out_be16(&sup
->scc_uaddr2
, 0);
808 out_be16(&sup
->scc_toseq
, 0);
809 out_be16(&sup
->scc_char1
, 0x8000);
810 out_be16(&sup
->scc_char2
, 0x8000);
811 out_be16(&sup
->scc_char3
, 0x8000);
812 out_be16(&sup
->scc_char4
, 0x8000);
813 out_be16(&sup
->scc_char5
, 0x8000);
814 out_be16(&sup
->scc_char6
, 0x8000);
815 out_be16(&sup
->scc_char7
, 0x8000);
816 out_be16(&sup
->scc_char8
, 0x8000);
817 out_be16(&sup
->scc_rccm
, 0xc0ff);
819 /* Send the CPM an initialize command.
821 cpm_line_cr_cmd(pinfo
, CPM_CR_INIT_TRX
);
823 /* Set UART mode, 8 bit, no parity, one stop.
824 * Enable receive and transmit.
826 out_be32(&scp
->scc_gsmrh
, 0);
827 out_be32(&scp
->scc_gsmrl
,
828 SCC_GSMRL_MODE_UART
| SCC_GSMRL_TDCR_16
| SCC_GSMRL_RDCR_16
);
830 /* Enable rx interrupts and clear all pending events. */
831 out_be16(&scp
->scc_sccm
, 0);
832 out_be16(&scp
->scc_scce
, 0xffff);
833 out_be16(&scp
->scc_dsr
, 0x7e7e);
834 out_be16(&scp
->scc_psmr
, 0x3000);
836 setbits32(&scp
->scc_gsmrl
, SCC_GSMRL_ENR
| SCC_GSMRL_ENT
);
839 static void cpm_uart_init_smc(struct uart_cpm_port
*pinfo
)
842 smc_uart_t __iomem
*up
;
844 pr_debug("CPM uart[%d]:init_smc\n", pinfo
->port
.line
);
850 out_be16(&pinfo
->smcup
->smc_rbase
,
851 (u8 __iomem
*)pinfo
->rx_bd_base
- DPRAM_BASE
);
852 out_be16(&pinfo
->smcup
->smc_tbase
,
853 (u8 __iomem
*)pinfo
->tx_bd_base
- DPRAM_BASE
);
856 * In case SMC1 is being relocated...
858 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
859 out_be16(&up
->smc_rbptr
, in_be16(&pinfo
->smcup
->smc_rbase
));
860 out_be16(&up
->smc_tbptr
, in_be16(&pinfo
->smcup
->smc_tbase
));
861 out_be32(&up
->smc_rstate
, 0);
862 out_be32(&up
->smc_tstate
, 0);
863 out_be16(&up
->smc_brkcr
, 1); /* number of break chars */
864 out_be16(&up
->smc_brkec
, 0);
867 /* Set up the uart parameters in the
872 /* Using idle character time requires some additional tuning. */
873 out_be16(&up
->smc_mrblr
, pinfo
->rx_fifosize
);
874 out_be16(&up
->smc_maxidl
, pinfo
->rx_fifosize
);
875 out_be16(&up
->smc_brklen
, 0);
876 out_be16(&up
->smc_brkec
, 0);
877 out_be16(&up
->smc_brkcr
, 1);
879 cpm_line_cr_cmd(pinfo
, CPM_CR_INIT_TRX
);
881 /* Set UART mode, 8 bit, no parity, one stop.
882 * Enable receive and transmit.
884 out_be16(&sp
->smc_smcmr
, smcr_mk_clen(9) | SMCMR_SM_UART
);
886 /* Enable only rx interrupts clear all pending events. */
887 out_8(&sp
->smc_smcm
, 0);
888 out_8(&sp
->smc_smce
, 0xff);
890 setbits16(&sp
->smc_smcmr
, SMCMR_REN
| SMCMR_TEN
);
894 * Initialize port. This is called from early_console stuff
895 * so we have to be careful here !
897 static int cpm_uart_request_port(struct uart_port
*port
)
899 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
902 pr_debug("CPM uart[%d]:request port\n", port
->line
);
904 if (pinfo
->flags
& FLAG_CONSOLE
)
908 clrbits8(&pinfo
->smcp
->smc_smcm
, SMCM_RX
| SMCM_TX
);
909 clrbits16(&pinfo
->smcp
->smc_smcmr
, SMCMR_REN
| SMCMR_TEN
);
911 clrbits16(&pinfo
->sccp
->scc_sccm
, UART_SCCM_TX
| UART_SCCM_RX
);
912 clrbits32(&pinfo
->sccp
->scc_gsmrl
, SCC_GSMRL_ENR
| SCC_GSMRL_ENT
);
915 ret
= cpm_uart_allocbuf(pinfo
, 0);
920 cpm_uart_initbd(pinfo
);
922 cpm_uart_init_smc(pinfo
);
924 cpm_uart_init_scc(pinfo
);
929 static void cpm_uart_release_port(struct uart_port
*port
)
931 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
933 if (!(pinfo
->flags
& FLAG_CONSOLE
))
934 cpm_uart_freebuf(pinfo
);
938 * Configure/autoconfigure the port.
940 static void cpm_uart_config_port(struct uart_port
*port
, int flags
)
942 pr_debug("CPM uart[%d]:config_port\n", port
->line
);
944 if (flags
& UART_CONFIG_TYPE
) {
945 port
->type
= PORT_CPM
;
946 cpm_uart_request_port(port
);
950 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
952 * Write a string to the serial port
953 * Note that this is called with interrupts already disabled
955 static void cpm_uart_early_write(struct uart_cpm_port
*pinfo
,
956 const char *string
, u_int count
)
959 cbd_t __iomem
*bdp
, *bdbase
;
960 unsigned char *cpm_outp_addr
;
962 /* Get the address of the host memory buffer.
965 bdbase
= pinfo
->tx_bd_base
;
968 * Now, do each character. This is not as bad as it looks
969 * since this is a holding FIFO and not a transmitting FIFO.
970 * We could add the complexity of filling the entire transmit
971 * buffer, but we would just wait longer between accesses......
973 for (i
= 0; i
< count
; i
++, string
++) {
974 /* Wait for transmitter fifo to empty.
975 * Ready indicates output is ready, and xmt is doing
976 * that, not that it is ready for us to send.
978 while ((in_be16(&bdp
->cbd_sc
) & BD_SC_READY
) != 0)
981 /* Send the character out.
982 * If the buffer address is in the CPM DPRAM, don't
985 cpm_outp_addr
= cpm2cpu_addr(in_be32(&bdp
->cbd_bufaddr
),
987 *cpm_outp_addr
= *string
;
989 out_be16(&bdp
->cbd_datlen
, 1);
990 setbits16(&bdp
->cbd_sc
, BD_SC_READY
);
992 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
)
997 /* if a LF, also do CR... */
999 while ((in_be16(&bdp
->cbd_sc
) & BD_SC_READY
) != 0)
1002 cpm_outp_addr
= cpm2cpu_addr(in_be32(&bdp
->cbd_bufaddr
),
1004 *cpm_outp_addr
= 13;
1006 out_be16(&bdp
->cbd_datlen
, 1);
1007 setbits16(&bdp
->cbd_sc
, BD_SC_READY
);
1009 if (in_be16(&bdp
->cbd_sc
) & BD_SC_WRAP
)
1017 * Finally, Wait for transmitter & holding register to empty
1018 * and restore the IER
1020 while ((in_be16(&bdp
->cbd_sc
) & BD_SC_READY
) != 0)
1023 pinfo
->tx_cur
= bdp
;
1027 #ifdef CONFIG_CONSOLE_POLL
1028 /* Serial polling routines for writing and reading from the uart while
1029 * in an interrupt or debug context.
1032 #define GDB_BUF_SIZE 512 /* power of 2, please */
1034 static char poll_buf
[GDB_BUF_SIZE
];
1036 static int poll_chars
;
1038 static int poll_wait_key(char *obuf
, struct uart_cpm_port
*pinfo
)
1041 volatile cbd_t
*bdp
;
1044 /* Get the address of the host memory buffer.
1046 bdp
= pinfo
->rx_cur
;
1047 while (bdp
->cbd_sc
& BD_SC_EMPTY
)
1050 /* If the buffer address is in the CPM DPRAM, don't
1053 cp
= cpm2cpu_addr(bdp
->cbd_bufaddr
, pinfo
);
1056 i
= c
= bdp
->cbd_datlen
;
1061 bdp
->cbd_sc
&= ~(BD_SC_BR
| BD_SC_FR
| BD_SC_PR
| BD_SC_OV
| BD_SC_ID
);
1062 bdp
->cbd_sc
|= BD_SC_EMPTY
;
1064 if (bdp
->cbd_sc
& BD_SC_WRAP
)
1065 bdp
= pinfo
->rx_bd_base
;
1068 pinfo
->rx_cur
= (cbd_t
*)bdp
;
1073 static int cpm_get_poll_char(struct uart_port
*port
)
1075 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
1077 if (!serial_polled
) {
1081 if (poll_chars
<= 0) {
1082 poll_chars
= poll_wait_key(poll_buf
, pinfo
);
1089 static void cpm_put_poll_char(struct uart_port
*port
,
1092 struct uart_cpm_port
*pinfo
= (struct uart_cpm_port
*)port
;
1096 cpm_uart_early_write(pinfo
, ch
, 1);
1098 #endif /* CONFIG_CONSOLE_POLL */
1100 static struct uart_ops cpm_uart_pops
= {
1101 .tx_empty
= cpm_uart_tx_empty
,
1102 .set_mctrl
= cpm_uart_set_mctrl
,
1103 .get_mctrl
= cpm_uart_get_mctrl
,
1104 .stop_tx
= cpm_uart_stop_tx
,
1105 .start_tx
= cpm_uart_start_tx
,
1106 .stop_rx
= cpm_uart_stop_rx
,
1107 .enable_ms
= cpm_uart_enable_ms
,
1108 .break_ctl
= cpm_uart_break_ctl
,
1109 .startup
= cpm_uart_startup
,
1110 .shutdown
= cpm_uart_shutdown
,
1111 .set_termios
= cpm_uart_set_termios
,
1112 .type
= cpm_uart_type
,
1113 .release_port
= cpm_uart_release_port
,
1114 .request_port
= cpm_uart_request_port
,
1115 .config_port
= cpm_uart_config_port
,
1116 .verify_port
= cpm_uart_verify_port
,
1117 #ifdef CONFIG_CONSOLE_POLL
1118 .poll_get_char
= cpm_get_poll_char
,
1119 .poll_put_char
= cpm_put_poll_char
,
1123 struct uart_cpm_port cpm_uart_ports
[UART_NR
];
1125 static int cpm_uart_init_port(struct device_node
*np
,
1126 struct uart_cpm_port
*pinfo
)
1129 void __iomem
*mem
, *pram
;
1134 data
= of_get_property(np
, "clock", NULL
);
1136 struct clk
*clk
= clk_get(NULL
, (const char*)data
);
1141 data
= of_get_property(np
, "fsl,cpm-brg", &len
);
1142 if (!data
|| len
!= 4) {
1143 printk(KERN_ERR
"CPM UART %s has no/invalid "
1144 "fsl,cpm-brg property.\n", np
->name
);
1150 data
= of_get_property(np
, "fsl,cpm-command", &len
);
1151 if (!data
|| len
!= 4) {
1152 printk(KERN_ERR
"CPM UART %s has no/invalid "
1153 "fsl,cpm-command property.\n", np
->name
);
1156 pinfo
->command
= *data
;
1158 mem
= of_iomap(np
, 0);
1162 if (of_device_is_compatible(np
, "fsl,cpm1-scc-uart") ||
1163 of_device_is_compatible(np
, "fsl,cpm2-scc-uart")) {
1165 pinfo
->sccup
= pram
= cpm_uart_map_pram(pinfo
, np
);
1166 } else if (of_device_is_compatible(np
, "fsl,cpm1-smc-uart") ||
1167 of_device_is_compatible(np
, "fsl,cpm2-smc-uart")) {
1168 pinfo
->flags
|= FLAG_SMC
;
1170 pinfo
->smcup
= pram
= cpm_uart_map_pram(pinfo
, np
);
1181 pinfo
->tx_nrfifos
= TX_NUM_FIFO
;
1182 pinfo
->tx_fifosize
= TX_BUF_SIZE
;
1183 pinfo
->rx_nrfifos
= RX_NUM_FIFO
;
1184 pinfo
->rx_fifosize
= RX_BUF_SIZE
;
1186 pinfo
->port
.uartclk
= ppc_proc_freq
;
1187 pinfo
->port
.mapbase
= (unsigned long)mem
;
1188 pinfo
->port
.type
= PORT_CPM
;
1189 pinfo
->port
.ops
= &cpm_uart_pops
,
1190 pinfo
->port
.iotype
= UPIO_MEM
;
1191 pinfo
->port
.fifosize
= pinfo
->tx_nrfifos
* pinfo
->tx_fifosize
;
1192 spin_lock_init(&pinfo
->port
.lock
);
1194 pinfo
->port
.irq
= of_irq_to_resource(np
, 0, NULL
);
1195 if (pinfo
->port
.irq
== NO_IRQ
) {
1200 for (i
= 0; i
< NUM_GPIOS
; i
++)
1201 pinfo
->gpios
[i
] = of_get_gpio(np
, i
);
1203 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1207 return cpm_uart_request_port(&pinfo
->port
);
1210 cpm_uart_unmap_pram(pinfo
, pram
);
1216 #ifdef CONFIG_SERIAL_CPM_CONSOLE
1218 * Print a string to the serial port trying not to disturb
1219 * any possible real use of the port...
1221 * Note that this is called with interrupts already disabled
1223 static void cpm_uart_console_write(struct console
*co
, const char *s
,
1226 struct uart_cpm_port
*pinfo
= &cpm_uart_ports
[co
->index
];
1227 unsigned long flags
;
1228 int nolock
= oops_in_progress
;
1230 if (unlikely(nolock
)) {
1231 local_irq_save(flags
);
1233 spin_lock_irqsave(&pinfo
->port
.lock
, flags
);
1236 cpm_uart_early_write(pinfo
, s
, count
);
1238 if (unlikely(nolock
)) {
1239 local_irq_restore(flags
);
1241 spin_unlock_irqrestore(&pinfo
->port
.lock
, flags
);
1246 static int __init
cpm_uart_console_setup(struct console
*co
, char *options
)
1253 struct uart_cpm_port
*pinfo
;
1254 struct uart_port
*port
;
1256 struct device_node
*np
= NULL
;
1259 if (co
->index
>= UART_NR
) {
1260 printk(KERN_ERR
"cpm_uart: console index %d too high\n",
1266 np
= of_find_node_by_type(np
, "serial");
1270 if (!of_device_is_compatible(np
, "fsl,cpm1-smc-uart") &&
1271 !of_device_is_compatible(np
, "fsl,cpm1-scc-uart") &&
1272 !of_device_is_compatible(np
, "fsl,cpm2-smc-uart") &&
1273 !of_device_is_compatible(np
, "fsl,cpm2-scc-uart"))
1275 } while (i
++ != co
->index
);
1277 pinfo
= &cpm_uart_ports
[co
->index
];
1279 pinfo
->flags
|= FLAG_CONSOLE
;
1280 port
= &pinfo
->port
;
1282 ret
= cpm_uart_init_port(np
, pinfo
);
1288 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1290 if ((baud
= uart_baudrate()) == -1)
1294 if (IS_SMC(pinfo
)) {
1295 out_be16(&pinfo
->smcup
->smc_brkcr
, 0);
1296 cpm_line_cr_cmd(pinfo
, CPM_CR_STOP_TX
);
1297 clrbits8(&pinfo
->smcp
->smc_smcm
, SMCM_RX
| SMCM_TX
);
1298 clrbits16(&pinfo
->smcp
->smc_smcmr
, SMCMR_REN
| SMCMR_TEN
);
1300 out_be16(&pinfo
->sccup
->scc_brkcr
, 0);
1301 cpm_line_cr_cmd(pinfo
, CPM_CR_GRA_STOP_TX
);
1302 clrbits16(&pinfo
->sccp
->scc_sccm
, UART_SCCM_TX
| UART_SCCM_RX
);
1303 clrbits32(&pinfo
->sccp
->scc_gsmrl
, SCC_GSMRL_ENR
| SCC_GSMRL_ENT
);
1306 ret
= cpm_uart_allocbuf(pinfo
, 1);
1311 cpm_uart_initbd(pinfo
);
1314 cpm_uart_init_smc(pinfo
);
1316 cpm_uart_init_scc(pinfo
);
1318 uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1319 cpm_line_cr_cmd(pinfo
, CPM_CR_RESTART_TX
);
1324 static struct uart_driver cpm_reg
;
1325 static struct console cpm_scc_uart_console
= {
1327 .write
= cpm_uart_console_write
,
1328 .device
= uart_console_device
,
1329 .setup
= cpm_uart_console_setup
,
1330 .flags
= CON_PRINTBUFFER
,
1335 static int __init
cpm_uart_console_init(void)
1337 register_console(&cpm_scc_uart_console
);
1341 console_initcall(cpm_uart_console_init
);
1343 #define CPM_UART_CONSOLE &cpm_scc_uart_console
1345 #define CPM_UART_CONSOLE NULL
1348 static struct uart_driver cpm_reg
= {
1349 .owner
= THIS_MODULE
,
1350 .driver_name
= "ttyCPM",
1351 .dev_name
= "ttyCPM",
1352 .major
= SERIAL_CPM_MAJOR
,
1353 .minor
= SERIAL_CPM_MINOR
,
1354 .cons
= CPM_UART_CONSOLE
,
1358 static int probe_index
;
1360 static int __devinit
cpm_uart_probe(struct platform_device
*ofdev
)
1362 int index
= probe_index
++;
1363 struct uart_cpm_port
*pinfo
= &cpm_uart_ports
[index
];
1366 pinfo
->port
.line
= index
;
1368 if (index
>= UART_NR
)
1371 dev_set_drvdata(&ofdev
->dev
, pinfo
);
1373 /* initialize the device pointer for the port */
1374 pinfo
->port
.dev
= &ofdev
->dev
;
1376 ret
= cpm_uart_init_port(ofdev
->dev
.of_node
, pinfo
);
1380 return uart_add_one_port(&cpm_reg
, &pinfo
->port
);
1383 static int __devexit
cpm_uart_remove(struct platform_device
*ofdev
)
1385 struct uart_cpm_port
*pinfo
= dev_get_drvdata(&ofdev
->dev
);
1386 return uart_remove_one_port(&cpm_reg
, &pinfo
->port
);
1389 static struct of_device_id cpm_uart_match
[] = {
1391 .compatible
= "fsl,cpm1-smc-uart",
1394 .compatible
= "fsl,cpm1-scc-uart",
1397 .compatible
= "fsl,cpm2-smc-uart",
1400 .compatible
= "fsl,cpm2-scc-uart",
1405 static struct platform_driver cpm_uart_driver
= {
1408 .owner
= THIS_MODULE
,
1409 .of_match_table
= cpm_uart_match
,
1411 .probe
= cpm_uart_probe
,
1412 .remove
= cpm_uart_remove
,
1415 static int __init
cpm_uart_init(void)
1417 int ret
= uart_register_driver(&cpm_reg
);
1421 ret
= platform_driver_register(&cpm_uart_driver
);
1423 uart_unregister_driver(&cpm_reg
);
1428 static void __exit
cpm_uart_exit(void)
1430 platform_driver_unregister(&cpm_uart_driver
);
1431 uart_unregister_driver(&cpm_reg
);
1434 module_init(cpm_uart_init
);
1435 module_exit(cpm_uart_exit
);
1437 MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1438 MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1439 MODULE_LICENSE("GPL");
1440 MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR
, SERIAL_CPM_MINOR
);