1601 lines · 38.4 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* *  Driver for CPM (SCC/SMC) serial ports; core driver * *  Based on arch/ppc/cpm2_io/uart.c by Dan Malek *  Based on ppc8xx.c by Thomas Gleixner *  Based on drivers/serial/amba.c by Russell King * *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2) *              Pantelis Antoniou (panto@intracom.gr) (CPM1) * *  Copyright (C) 2004, 2007 Freescale Semiconductor, Inc. *            (C) 2004 Intracom, S.A. *            (C) 2005-2006 MontaVista Software, Inc. *		Vitaly Bordug <vbordug@ru.mvista.com> */ #include <linux/module.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/serial.h>#include <linux/console.h>#include <linux/sysrq.h>#include <linux/device.h>#include <linux/memblock.h>#include <linux/dma-mapping.h>#include <linux/of_address.h>#include <linux/of_platform.h>#include <linux/gpio/consumer.h>#include <linux/clk.h> #include <sysdev/fsl_soc.h> #include <asm/io.h>#include <asm/irq.h>#include <asm/delay.h>#include <asm/udbg.h> #include <linux/serial_core.h>#include <linux/kernel.h> #include "cpm_uart.h"  /**************************************************************/ static int  cpm_uart_tx_pump(struct uart_port *port);static void cpm_uart_initbd(struct uart_cpm_port *pinfo); /**************************************************************/ #define HW_BUF_SPD_THRESHOLD    2400 static void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd){	cpm_command(port->command, cmd);} /* * Check, if transmit buffers are processed*/static unsigned int cpm_uart_tx_empty(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	cbd_t __iomem *bdp = pinfo->tx_bd_base;	int ret = 0; 	while (1) {		if (in_be16(&bdp->cbd_sc) & BD_SC_READY)			break; 		if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {			ret = TIOCSER_TEMT;			break;		}		bdp++;	} 	pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret); 	return ret;} static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port); 	if (pinfo->gpios[GPIO_RTS])		gpiod_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS)); 	if (pinfo->gpios[GPIO_DTR])		gpiod_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));} static unsigned int cpm_uart_get_mctrl(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 	if (pinfo->gpios[GPIO_CTS]) {		if (gpiod_get_value(pinfo->gpios[GPIO_CTS]))			mctrl &= ~TIOCM_CTS;	} 	if (pinfo->gpios[GPIO_DSR]) {		if (gpiod_get_value(pinfo->gpios[GPIO_DSR]))			mctrl &= ~TIOCM_DSR;	} 	if (pinfo->gpios[GPIO_DCD]) {		if (gpiod_get_value(pinfo->gpios[GPIO_DCD]))			mctrl &= ~TIOCM_CAR;	} 	if (pinfo->gpios[GPIO_RI]) {		if (!gpiod_get_value(pinfo->gpios[GPIO_RI]))			mctrl |= TIOCM_RNG;	} 	return mctrl;} /* * Stop transmitter */static void cpm_uart_stop_tx(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	smc_t __iomem *smcp = pinfo->smcp;	scc_t __iomem *sccp = pinfo->sccp; 	pr_debug("CPM uart[%d]:stop tx\n", port->line); 	if (IS_SMC(pinfo))		clrbits8(&smcp->smc_smcm, SMCM_TX);	else		clrbits16(&sccp->scc_sccm, UART_SCCM_TX);} /* * Start transmitter */static void cpm_uart_start_tx(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	smc_t __iomem *smcp = pinfo->smcp;	scc_t __iomem *sccp = pinfo->sccp; 	pr_debug("CPM uart[%d]:start tx\n", port->line); 	if (IS_SMC(pinfo)) {		if (in_8(&smcp->smc_smcm) & SMCM_TX)			return;	} else {		if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)			return;	} 	if (cpm_uart_tx_pump(port) != 0) {		if (IS_SMC(pinfo)) {			setbits8(&smcp->smc_smcm, SMCM_TX);		} else {			setbits16(&sccp->scc_sccm, UART_SCCM_TX);		}	}} /* * Stop receiver */static void cpm_uart_stop_rx(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	smc_t __iomem *smcp = pinfo->smcp;	scc_t __iomem *sccp = pinfo->sccp; 	pr_debug("CPM uart[%d]:stop rx\n", port->line); 	if (IS_SMC(pinfo))		clrbits8(&smcp->smc_smcm, SMCM_RX);	else		clrbits16(&sccp->scc_sccm, UART_SCCM_RX);} /* * Generate a break. */static void cpm_uart_break_ctl(struct uart_port *port, int break_state){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port); 	pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,		break_state); 	if (break_state)		cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);	else		cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);} /* * Transmit characters, refill buffer descriptor, if possible */static void cpm_uart_int_tx(struct uart_port *port){	pr_debug("CPM uart[%d]:TX INT\n", port->line); 	cpm_uart_tx_pump(port);} #ifdef CONFIG_CONSOLE_POLLstatic int serial_polled;#endif /* * Receive characters */static void cpm_uart_int_rx(struct uart_port *port){	int i;	unsigned char ch;	u8 *cp;	struct tty_port *tport = &port->state->port;	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	cbd_t __iomem *bdp;	u16 status;	unsigned int flg; 	pr_debug("CPM uart[%d]:RX INT\n", port->line); 	/* Just loop through the closed BDs and copy the characters into	 * the buffer.	 */	bdp = pinfo->rx_cur;	for (;;) {#ifdef CONFIG_CONSOLE_POLL		if (unlikely(serial_polled)) {			serial_polled = 0;			return;		}#endif		/* get status */		status = in_be16(&bdp->cbd_sc);		/* If this one is empty, return happy */		if (status & BD_SC_EMPTY)			break; 		/* get number of characters, and check spce in flip-buffer */		i = in_be16(&bdp->cbd_datlen); 		/* If we have not enough room in tty flip buffer, then we try		 * later, which will be the next rx-interrupt or a timeout		 */		if (tty_buffer_request_room(tport, i) < i) {			printk(KERN_WARNING "No room in flip buffer\n");			return;		} 		/* get pointer */		cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo); 		/* loop through the buffer */		while (i-- > 0) {			ch = *cp++;			port->icount.rx++;			flg = TTY_NORMAL; 			if (status &			    (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))				goto handle_error;			if (uart_handle_sysrq_char(port, ch))				continue;#ifdef CONFIG_CONSOLE_POLL			if (unlikely(serial_polled)) {				serial_polled = 0;				return;			}#endif		      error_return:			tty_insert_flip_char(tport, ch, flg); 		}		/* End while (i--) */ 		/* This BD is ready to be used again. Clear status. get next */		clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |		                        BD_SC_OV | BD_SC_ID);		setbits16(&bdp->cbd_sc, BD_SC_EMPTY); 		if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)			bdp = pinfo->rx_bd_base;		else			bdp++; 	} /* End for (;;) */ 	/* Write back buffer pointer */	pinfo->rx_cur = bdp; 	/* activate BH processing */	tty_flip_buffer_push(tport); 	return; 	/* Error processing */       handle_error:	/* Statistics */	if (status & BD_SC_BR)		port->icount.brk++;	if (status & BD_SC_PR)		port->icount.parity++;	if (status & BD_SC_FR)		port->icount.frame++;	if (status & BD_SC_OV)		port->icount.overrun++; 	/* Mask out ignored conditions */	status &= port->read_status_mask; 	/* Handle the remaining ones */	if (status & BD_SC_BR)		flg = TTY_BREAK;	else if (status & BD_SC_PR)		flg = TTY_PARITY;	else if (status & BD_SC_FR)		flg = TTY_FRAME; 	/* overrun does not affect the current character ! */	if (status & BD_SC_OV) {		ch = 0;		flg = TTY_OVERRUN;		/* We skip this buffer */		/* CHECK: Is really nothing senseful there */		/* ASSUMPTION: it contains nothing valid */		i = 0;	}	port->sysrq = 0;	goto error_return;} /* * Asynchron mode interrupt handler */static irqreturn_t cpm_uart_int(int irq, void *data){	u8 events;	struct uart_port *port = data;	struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;	smc_t __iomem *smcp = pinfo->smcp;	scc_t __iomem *sccp = pinfo->sccp; 	pr_debug("CPM uart[%d]:IRQ\n", port->line); 	if (IS_SMC(pinfo)) {		events = in_8(&smcp->smc_smce);		out_8(&smcp->smc_smce, events);		if (events & SMCM_BRKE)			uart_handle_break(port);		if (events & SMCM_RX)			cpm_uart_int_rx(port);		if (events & SMCM_TX)			cpm_uart_int_tx(port);	} else {		events = in_be16(&sccp->scc_scce);		out_be16(&sccp->scc_scce, events);		if (events & UART_SCCM_BRKE)			uart_handle_break(port);		if (events & UART_SCCM_RX)			cpm_uart_int_rx(port);		if (events & UART_SCCM_TX)			cpm_uart_int_tx(port);	}	return (events) ? IRQ_HANDLED : IRQ_NONE;} static int cpm_uart_startup(struct uart_port *port){	int retval;	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port); 	pr_debug("CPM uart[%d]:startup\n", port->line); 	/* If the port is not the console, make sure rx is disabled. */	if (!(pinfo->flags & FLAG_CONSOLE)) {		/* Disable UART rx */		if (IS_SMC(pinfo)) {			clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);			clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);		} else {			clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);			clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);		}		cpm_uart_initbd(pinfo);		if (IS_SMC(pinfo)) {			out_be32(&pinfo->smcup->smc_rstate, 0);			out_be32(&pinfo->smcup->smc_tstate, 0);			out_be16(&pinfo->smcup->smc_rbptr,				 in_be16(&pinfo->smcup->smc_rbase));			out_be16(&pinfo->smcup->smc_tbptr,				 in_be16(&pinfo->smcup->smc_tbase));		} else {			cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);		}	}	/* Install interrupt handler. */	retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);	if (retval)		return retval; 	/* Startup rx-int */	if (IS_SMC(pinfo)) {		setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);		setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));	} else {		setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);		setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));	} 	return 0;} inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo){	set_current_state(TASK_UNINTERRUPTIBLE);	schedule_timeout(pinfo->wait_closing);} /* * Shutdown the uart */static void cpm_uart_shutdown(struct uart_port *port){	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port); 	pr_debug("CPM uart[%d]:shutdown\n", port->line); 	/* free interrupt handler */	free_irq(port->irq, port); 	/* If the port is not the console, disable Rx and Tx. */	if (!(pinfo->flags & FLAG_CONSOLE)) {		/* Wait for all the BDs marked sent */		while(!cpm_uart_tx_empty(port)) {			set_current_state(TASK_UNINTERRUPTIBLE);			schedule_timeout(2);		} 		if (pinfo->wait_closing)			cpm_uart_wait_until_send(pinfo); 		/* Stop uarts */		if (IS_SMC(pinfo)) {			smc_t __iomem *smcp = pinfo->smcp;			clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);			clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);		} else {			scc_t __iomem *sccp = pinfo->sccp;			clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);			clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);		} 		/* Shut them really down and reinit buffer descriptors */		if (IS_SMC(pinfo)) {			out_be16(&pinfo->smcup->smc_brkcr, 0);			cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);		} else {			out_be16(&pinfo->sccup->scc_brkcr, 0);			cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);		} 		cpm_uart_initbd(pinfo);	}} static void cpm_uart_set_termios(struct uart_port *port,                                 struct ktermios *termios,                                 const struct ktermios *old){	int baud;	unsigned long flags;	u16 cval, scval, prev_mode;	struct uart_cpm_port *pinfo =		container_of(port, struct uart_cpm_port, port);	smc_t __iomem *smcp = pinfo->smcp;	scc_t __iomem *sccp = pinfo->sccp;	int maxidl; 	pr_debug("CPM uart[%d]:set_termios\n", port->line);

Lines 1–500 of 1601.