Commit ec063899 authored by Alexander Shiyan's avatar Alexander Shiyan Committed by Greg Kroah-Hartman

serial: sccnxp: Implement polling mode

This patch adds support for polling work mode, i.e. system is perform
periodical check chip status when IRQ-line is not connected to CPU.
Signed-off-by: default avatarAlexander Shiyan <shc_work@mail.ru>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 496c9077
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <linux/io.h> #include <linux/io.h>
#include <linux/tty.h> #include <linux/tty.h>
#include <linux/tty_flip.h> #include <linux/tty_flip.h>
#include <linux/spinlock.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/platform_data/sccnxp.h> #include <linux/platform_data/sccnxp.h>
...@@ -106,6 +107,7 @@ enum { ...@@ -106,6 +107,7 @@ enum {
struct sccnxp_port { struct sccnxp_port {
struct uart_driver uart; struct uart_driver uart;
struct uart_port port[SCCNXP_MAX_UARTS]; struct uart_port port[SCCNXP_MAX_UARTS];
bool opened[SCCNXP_MAX_UARTS];
const char *name; const char *name;
int irq; int irq;
...@@ -122,7 +124,10 @@ struct sccnxp_port { ...@@ -122,7 +124,10 @@ struct sccnxp_port {
struct console console; struct console console;
#endif #endif
struct mutex sccnxp_mutex; spinlock_t lock;
bool poll;
struct timer_list timer;
struct sccnxp_pdata pdata; struct sccnxp_pdata pdata;
}; };
...@@ -371,31 +376,48 @@ static void sccnxp_handle_tx(struct uart_port *port) ...@@ -371,31 +376,48 @@ static void sccnxp_handle_tx(struct uart_port *port)
uart_write_wakeup(port); uart_write_wakeup(port);
} }
static irqreturn_t sccnxp_ist(int irq, void *dev_id) static void sccnxp_handle_events(struct sccnxp_port *s)
{ {
int i; int i;
u8 isr; u8 isr;
struct sccnxp_port *s = (struct sccnxp_port *)dev_id;
mutex_lock(&s->sccnxp_mutex);
for (;;) { do {
isr = sccnxp_read(&s->port[0], SCCNXP_ISR_REG); isr = sccnxp_read(&s->port[0], SCCNXP_ISR_REG);
isr &= s->imr; isr &= s->imr;
if (!isr) if (!isr)
break; break;
dev_dbg(s->port[0].dev, "IRQ status: 0x%02x\n", isr);
for (i = 0; i < s->uart.nr; i++) { for (i = 0; i < s->uart.nr; i++) {
if (isr & ISR_RXRDY(i)) if (s->opened[i] && (isr & ISR_RXRDY(i)))
sccnxp_handle_rx(&s->port[i]); sccnxp_handle_rx(&s->port[i]);
if (isr & ISR_TXRDY(i)) if (s->opened[i] && (isr & ISR_TXRDY(i)))
sccnxp_handle_tx(&s->port[i]); sccnxp_handle_tx(&s->port[i]);
} }
} } while (1);
}
static void sccnxp_timer(unsigned long data)
{
struct sccnxp_port *s = (struct sccnxp_port *)data;
unsigned long flags;
mutex_unlock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
sccnxp_handle_events(s);
spin_unlock_irqrestore(&s->lock, flags);
if (!timer_pending(&s->timer))
mod_timer(&s->timer, jiffies +
usecs_to_jiffies(s->pdata.poll_time_us));
}
static irqreturn_t sccnxp_ist(int irq, void *dev_id)
{
struct sccnxp_port *s = (struct sccnxp_port *)dev_id;
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
sccnxp_handle_events(s);
spin_unlock_irqrestore(&s->lock, flags);
return IRQ_HANDLED; return IRQ_HANDLED;
} }
...@@ -403,8 +425,9 @@ static irqreturn_t sccnxp_ist(int irq, void *dev_id) ...@@ -403,8 +425,9 @@ static irqreturn_t sccnxp_ist(int irq, void *dev_id)
static void sccnxp_start_tx(struct uart_port *port) static void sccnxp_start_tx(struct uart_port *port)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
/* Set direction to output */ /* Set direction to output */
if (s->flags & SCCNXP_HAVE_IO) if (s->flags & SCCNXP_HAVE_IO)
...@@ -412,7 +435,7 @@ static void sccnxp_start_tx(struct uart_port *port) ...@@ -412,7 +435,7 @@ static void sccnxp_start_tx(struct uart_port *port)
sccnxp_enable_irq(port, IMR_TXRDY); sccnxp_enable_irq(port, IMR_TXRDY);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static void sccnxp_stop_tx(struct uart_port *port) static void sccnxp_stop_tx(struct uart_port *port)
...@@ -423,20 +446,22 @@ static void sccnxp_stop_tx(struct uart_port *port) ...@@ -423,20 +446,22 @@ static void sccnxp_stop_tx(struct uart_port *port)
static void sccnxp_stop_rx(struct uart_port *port) static void sccnxp_stop_rx(struct uart_port *port)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE); sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static unsigned int sccnxp_tx_empty(struct uart_port *port) static unsigned int sccnxp_tx_empty(struct uart_port *port)
{ {
u8 val; u8 val;
unsigned long flags;
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
val = sccnxp_port_read(port, SCCNXP_SR_REG); val = sccnxp_port_read(port, SCCNXP_SR_REG);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
return (val & SR_TXEMT) ? TIOCSER_TEMT : 0; return (val & SR_TXEMT) ? TIOCSER_TEMT : 0;
} }
...@@ -449,28 +474,30 @@ static void sccnxp_enable_ms(struct uart_port *port) ...@@ -449,28 +474,30 @@ static void sccnxp_enable_ms(struct uart_port *port)
static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl) static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
if (!(s->flags & SCCNXP_HAVE_IO)) if (!(s->flags & SCCNXP_HAVE_IO))
return; return;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
sccnxp_set_bit(port, DTR_OP, mctrl & TIOCM_DTR); sccnxp_set_bit(port, DTR_OP, mctrl & TIOCM_DTR);
sccnxp_set_bit(port, RTS_OP, mctrl & TIOCM_RTS); sccnxp_set_bit(port, RTS_OP, mctrl & TIOCM_RTS);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static unsigned int sccnxp_get_mctrl(struct uart_port *port) static unsigned int sccnxp_get_mctrl(struct uart_port *port)
{ {
u8 bitmask, ipr; u8 bitmask, ipr;
unsigned long flags;
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
if (!(s->flags & SCCNXP_HAVE_IO)) if (!(s->flags & SCCNXP_HAVE_IO))
return mctrl; return mctrl;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
ipr = ~sccnxp_read(port, SCCNXP_IPCR_REG); ipr = ~sccnxp_read(port, SCCNXP_IPCR_REG);
...@@ -499,7 +526,7 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port) ...@@ -499,7 +526,7 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port)
mctrl |= (ipr & bitmask) ? TIOCM_RNG : 0; mctrl |= (ipr & bitmask) ? TIOCM_RNG : 0;
} }
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
return mctrl; return mctrl;
} }
...@@ -507,21 +534,23 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port) ...@@ -507,21 +534,23 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port)
static void sccnxp_break_ctl(struct uart_port *port, int break_state) static void sccnxp_break_ctl(struct uart_port *port, int break_state)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
sccnxp_port_write(port, SCCNXP_CR_REG, break_state ? sccnxp_port_write(port, SCCNXP_CR_REG, break_state ?
CR_CMD_START_BREAK : CR_CMD_STOP_BREAK); CR_CMD_START_BREAK : CR_CMD_STOP_BREAK);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static void sccnxp_set_termios(struct uart_port *port, static void sccnxp_set_termios(struct uart_port *port,
struct ktermios *termios, struct ktermios *old) struct ktermios *termios, struct ktermios *old)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
u8 mr1, mr2; u8 mr1, mr2;
int baud; int baud;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
/* Mask termios capabilities we don't support */ /* Mask termios capabilities we don't support */
termios->c_cflag &= ~CMSPAR; termios->c_cflag &= ~CMSPAR;
...@@ -588,20 +617,22 @@ static void sccnxp_set_termios(struct uart_port *port, ...@@ -588,20 +617,22 @@ static void sccnxp_set_termios(struct uart_port *port,
/* Update timeout according to new baud rate */ /* Update timeout according to new baud rate */
uart_update_timeout(port, termios->c_cflag, baud); uart_update_timeout(port, termios->c_cflag, baud);
/* Report actual baudrate back to core */
if (tty_termios_baud_rate(termios)) if (tty_termios_baud_rate(termios))
tty_termios_encode_baud_rate(termios, baud, baud); tty_termios_encode_baud_rate(termios, baud, baud);
/* Enable RX & TX */ /* Enable RX & TX */
sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE); sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static int sccnxp_startup(struct uart_port *port) static int sccnxp_startup(struct uart_port *port)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
if (s->flags & SCCNXP_HAVE_IO) { if (s->flags & SCCNXP_HAVE_IO) {
/* Outputs are controlled manually */ /* Outputs are controlled manually */
...@@ -620,7 +651,9 @@ static int sccnxp_startup(struct uart_port *port) ...@@ -620,7 +651,9 @@ static int sccnxp_startup(struct uart_port *port)
/* Enable RX interrupt */ /* Enable RX interrupt */
sccnxp_enable_irq(port, IMR_RXRDY); sccnxp_enable_irq(port, IMR_RXRDY);
mutex_unlock(&s->sccnxp_mutex); s->opened[port->line] = 1;
spin_unlock_irqrestore(&s->lock, flags);
return 0; return 0;
} }
...@@ -628,8 +661,11 @@ static int sccnxp_startup(struct uart_port *port) ...@@ -628,8 +661,11 @@ static int sccnxp_startup(struct uart_port *port)
static void sccnxp_shutdown(struct uart_port *port) static void sccnxp_shutdown(struct uart_port *port)
{ {
struct sccnxp_port *s = dev_get_drvdata(port->dev); struct sccnxp_port *s = dev_get_drvdata(port->dev);
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
mutex_lock(&s->sccnxp_mutex); s->opened[port->line] = 0;
/* Disable interrupts */ /* Disable interrupts */
sccnxp_disable_irq(port, IMR_TXRDY | IMR_RXRDY); sccnxp_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
...@@ -641,7 +677,7 @@ static void sccnxp_shutdown(struct uart_port *port) ...@@ -641,7 +677,7 @@ static void sccnxp_shutdown(struct uart_port *port)
if (s->flags & SCCNXP_HAVE_IO) if (s->flags & SCCNXP_HAVE_IO)
sccnxp_set_bit(port, DIR_OP, 0); sccnxp_set_bit(port, DIR_OP, 0);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static const char *sccnxp_type(struct uart_port *port) static const char *sccnxp_type(struct uart_port *port)
...@@ -715,10 +751,11 @@ static void sccnxp_console_write(struct console *co, const char *c, unsigned n) ...@@ -715,10 +751,11 @@ static void sccnxp_console_write(struct console *co, const char *c, unsigned n)
{ {
struct sccnxp_port *s = (struct sccnxp_port *)co->data; struct sccnxp_port *s = (struct sccnxp_port *)co->data;
struct uart_port *port = &s->port[co->index]; struct uart_port *port = &s->port[co->index];
unsigned long flags;
mutex_lock(&s->sccnxp_mutex); spin_lock_irqsave(&s->lock, flags);
uart_console_write(port, c, n, sccnxp_console_putchar); uart_console_write(port, c, n, sccnxp_console_putchar);
mutex_unlock(&s->sccnxp_mutex); spin_unlock_irqrestore(&s->lock, flags);
} }
static int sccnxp_console_setup(struct console *co, char *options) static int sccnxp_console_setup(struct console *co, char *options)
...@@ -757,7 +794,7 @@ static int sccnxp_probe(struct platform_device *pdev) ...@@ -757,7 +794,7 @@ static int sccnxp_probe(struct platform_device *pdev)
} }
platform_set_drvdata(pdev, s); platform_set_drvdata(pdev, s);
mutex_init(&s->sccnxp_mutex); spin_lock_init(&s->lock);
/* Individual chip settings */ /* Individual chip settings */
switch (chiptype) { switch (chiptype) {
...@@ -854,11 +891,19 @@ static int sccnxp_probe(struct platform_device *pdev) ...@@ -854,11 +891,19 @@ static int sccnxp_probe(struct platform_device *pdev)
} else } else
memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata)); memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata));
s->irq = platform_get_irq(pdev, 0); if (pdata->poll_time_us) {
if (s->irq <= 0) { dev_info(&pdev->dev, "Using poll mode, resolution %u usecs\n",
dev_err(&pdev->dev, "Missing irq resource data\n"); pdata->poll_time_us);
ret = -ENXIO; s->poll = 1;
goto err_out; }
if (!s->poll) {
s->irq = platform_get_irq(pdev, 0);
if (s->irq < 0) {
dev_err(&pdev->dev, "Missing irq resource data\n");
ret = -ENXIO;
goto err_out;
}
} }
/* Check input frequency */ /* Check input frequency */
...@@ -923,13 +968,23 @@ static int sccnxp_probe(struct platform_device *pdev) ...@@ -923,13 +968,23 @@ static int sccnxp_probe(struct platform_device *pdev)
if (s->pdata.init) if (s->pdata.init)
s->pdata.init(); s->pdata.init();
ret = devm_request_threaded_irq(&pdev->dev, s->irq, NULL, sccnxp_ist, if (!s->poll) {
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, ret = devm_request_threaded_irq(&pdev->dev, s->irq, NULL,
dev_name(&pdev->dev), s); sccnxp_ist,
if (!ret) IRQF_TRIGGER_FALLING |
IRQF_ONESHOT,
dev_name(&pdev->dev), s);
if (!ret)
return 0;
dev_err(&pdev->dev, "Unable to reguest IRQ %i\n", s->irq);
} else {
init_timer(&s->timer);
setup_timer(&s->timer, sccnxp_timer, (unsigned long)s);
mod_timer(&s->timer, jiffies +
usecs_to_jiffies(s->pdata.poll_time_us));
return 0; return 0;
}
dev_err(&pdev->dev, "Unable to reguest IRQ %i\n", s->irq);
err_out: err_out:
platform_set_drvdata(pdev, NULL); platform_set_drvdata(pdev, NULL);
...@@ -942,7 +997,10 @@ static int sccnxp_remove(struct platform_device *pdev) ...@@ -942,7 +997,10 @@ static int sccnxp_remove(struct platform_device *pdev)
int i; int i;
struct sccnxp_port *s = platform_get_drvdata(pdev); struct sccnxp_port *s = platform_get_drvdata(pdev);
devm_free_irq(&pdev->dev, s->irq, s); if (!s->poll)
devm_free_irq(&pdev->dev, s->irq, s);
else
del_timer_sync(&s->timer);
for (i = 0; i < s->uart.nr; i++) for (i = 0; i < s->uart.nr; i++)
uart_remove_one_port(&s->uart, &s->port[i]); uart_remove_one_port(&s->uart, &s->port[i]);
......
...@@ -84,6 +84,8 @@ struct sccnxp_pdata { ...@@ -84,6 +84,8 @@ struct sccnxp_pdata {
const u8 reg_shift; const u8 reg_shift;
/* Modem control lines configuration */ /* Modem control lines configuration */
const u32 mctrl_cfg[SCCNXP_MAX_UARTS]; const u32 mctrl_cfg[SCCNXP_MAX_UARTS];
/* Timer value for polling mode (usecs) */
const unsigned int poll_time_us;
/* Called during startup */ /* Called during startup */
void (*init)(void); void (*init)(void);
/* Called before finish */ /* Called before finish */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment