Skip to content

Commit

Permalink
imxrt_serial: use small lock in arch/arm/src/imxrt/imxrt_serial.c
Browse files Browse the repository at this point in the history
Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 authored and xiaoxiang781216 committed Dec 1, 2024
1 parent 0bdc9d2 commit 30940d9
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions arch/arm/src/imxrt/imxrt_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ struct imxrt_uart_s
struct uart_dev_s dev; /* Generic UART device */
uint32_t uartbase; /* Base address of UART registers */
uint32_t baud; /* Configured baud */
spinlock_t lock; /* Spinlock */
uint32_t ie; /* Saved enabled interrupts */
uint8_t irq; /* IRQ associated with this UART */
uint8_t parity; /* 0=none, 1=odd, 2=even */
Expand Down Expand Up @@ -1145,6 +1146,7 @@ static struct imxrt_uart_s g_lpuart1priv =

.uartbase = IMXRT_LPUART1_BASE,
.baud = CONFIG_LPUART1_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART1,
.parity = CONFIG_LPUART1_PARITY,
.bits = CONFIG_LPUART1_BITS,
Expand Down Expand Up @@ -1214,6 +1216,7 @@ static struct imxrt_uart_s g_lpuart2priv =

.uartbase = IMXRT_LPUART2_BASE,
.baud = CONFIG_LPUART2_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART2,
.parity = CONFIG_LPUART2_PARITY,
.bits = CONFIG_LPUART2_BITS,
Expand Down Expand Up @@ -1280,6 +1283,7 @@ static struct imxrt_uart_s g_lpuart3priv =

.uartbase = IMXRT_LPUART3_BASE,
.baud = CONFIG_LPUART3_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART3,
.parity = CONFIG_LPUART3_PARITY,
.bits = CONFIG_LPUART3_BITS,
Expand Down Expand Up @@ -1346,6 +1350,7 @@ static struct imxrt_uart_s g_lpuart4priv =

.uartbase = IMXRT_LPUART4_BASE,
.baud = CONFIG_LPUART4_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART4,
.parity = CONFIG_LPUART4_PARITY,
.bits = CONFIG_LPUART4_BITS,
Expand Down Expand Up @@ -1412,6 +1417,7 @@ static struct imxrt_uart_s g_lpuart5priv =

.uartbase = IMXRT_LPUART5_BASE,
.baud = CONFIG_LPUART5_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART5,
.parity = CONFIG_LPUART5_PARITY,
.bits = CONFIG_LPUART5_BITS,
Expand Down Expand Up @@ -1478,6 +1484,7 @@ static struct imxrt_uart_s g_lpuart6priv =

.uartbase = IMXRT_LPUART6_BASE,
.baud = CONFIG_LPUART6_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART6,
.parity = CONFIG_LPUART6_PARITY,
.bits = CONFIG_LPUART6_BITS,
Expand Down Expand Up @@ -1544,6 +1551,7 @@ static struct imxrt_uart_s g_lpuart7priv =

.uartbase = IMXRT_LPUART7_BASE,
.baud = CONFIG_LPUART7_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART7,
.parity = CONFIG_LPUART7_PARITY,
.bits = CONFIG_LPUART7_BITS,
Expand Down Expand Up @@ -1610,6 +1618,7 @@ static struct imxrt_uart_s g_lpuart8priv =

.uartbase = IMXRT_LPUART8_BASE,
.baud = CONFIG_LPUART8_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART8,
.parity = CONFIG_LPUART8_PARITY,
.bits = CONFIG_LPUART8_BITS,
Expand Down Expand Up @@ -1676,6 +1685,7 @@ static struct imxrt_uart_s g_lpuart9priv =

.uartbase = IMXRT_LPUART9_BASE,
.baud = CONFIG_LPUART9_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART9,
.parity = CONFIG_LPUART9_PARITY,
.bits = CONFIG_LPUART9_BITS,
Expand Down Expand Up @@ -1742,6 +1752,7 @@ static struct imxrt_uart_s g_lpuart10priv =

.uartbase = IMXRT_LPUART10_BASE,
.baud = CONFIG_LPUART10_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART10,
.parity = CONFIG_LPUART10_PARITY,
.bits = CONFIG_LPUART10_BITS,
Expand Down Expand Up @@ -1808,6 +1819,7 @@ static struct imxrt_uart_s g_lpuart11priv =

.uartbase = IMXRT_LPUART11_BASE,
.baud = CONFIG_LPUART11_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART11,
.parity = CONFIG_LPUART11_PARITY,
.bits = CONFIG_LPUART11_BITS,
Expand Down Expand Up @@ -1874,6 +1886,7 @@ static struct imxrt_uart_s g_lpuart12priv =

.uartbase = IMXRT_LPUART12_BASE,
.baud = CONFIG_LPUART12_BAUD,
.lock = SP_UNLOCKED,
.irq = IMXRT_IRQ_LPUART12,
.parity = CONFIG_LPUART12_PARITY,
.bits = CONFIG_LPUART12_BITS,
Expand Down Expand Up @@ -1973,7 +1986,7 @@ static inline void imxrt_disableuartint(struct imxrt_uart_s *priv,
irqstate_t flags;
uint32_t regval;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
regval = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);

/* Return the current Rx and Tx interrupt state */
Expand All @@ -1985,7 +1998,7 @@ static inline void imxrt_disableuartint(struct imxrt_uart_s *priv,

regval &= ~LPUART_ALL_INTS;
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
#endif

Expand All @@ -2004,12 +2017,12 @@ static inline void imxrt_restoreuartint(struct imxrt_uart_s *priv,
* enabled/disabled.
*/

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
regval = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);
regval &= ~LPUART_ALL_INTS;
regval |= ie;
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
#endif

Expand Down Expand Up @@ -2631,15 +2644,15 @@ static int imxrt_ioctl(struct file *filep, int cmd, unsigned long arg)
* implement TCSADRAIN / TCSAFLUSH
*/

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
imxrt_disableuartint(priv, &ie);
ret = dev->ops->setup(dev);

/* Restore the interrupt state */

imxrt_restoreuartint(priv, ie);
priv->ie = ie;
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
}
break;
Expand All @@ -2652,7 +2665,7 @@ static int imxrt_ioctl(struct file *filep, int cmd, unsigned long arg)
irqstate_t flags;
struct imxrt_uart_s *priv = (struct imxrt_uart_s *)dev;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
regval = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);

if ((arg & SER_SINGLEWIRE_ENABLED) != 0)
Expand Down Expand Up @@ -2686,7 +2699,7 @@ static int imxrt_ioctl(struct file *filep, int cmd, unsigned long arg)

imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
break;
#endif
Expand All @@ -2700,7 +2713,7 @@ static int imxrt_ioctl(struct file *filep, int cmd, unsigned long arg)
irqstate_t flags;
struct imxrt_uart_s *priv = (struct imxrt_uart_s *)dev;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
ctrl = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);
stat = imxrt_serialin(priv, IMXRT_LPUART_STAT_OFFSET);
regval = ctrl;
Expand Down Expand Up @@ -2736,7 +2749,7 @@ static int imxrt_ioctl(struct file *filep, int cmd, unsigned long arg)
imxrt_serialout(priv, IMXRT_LPUART_STAT_OFFSET, stat);
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, ctrl);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
break;
#endif
Expand Down Expand Up @@ -2790,7 +2803,7 @@ static void imxrt_rxint(struct uart_dev_s *dev, bool enable)

/* Enable interrupts for data available at Rx */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
if (enable)
{
#ifndef CONFIG_SUPPRESS_SERIAL_INTS
Expand All @@ -2806,7 +2819,7 @@ static void imxrt_rxint(struct uart_dev_s *dev, bool enable)
regval &= ~LPUART_ALL_INTS;
regval |= priv->ie;
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
#endif

Expand Down Expand Up @@ -3258,12 +3271,12 @@ static void imxrt_singlewire_send(struct uart_dev_s *dev, int ch)
uint32_t regval;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
regval = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);
regval &= ~(LPUART_CTRL_RSRC);
regval |= (LPUART_CTRL_TXDIR);
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);

imxrt_serialout(priv, IMXRT_LPUART_DATA_OFFSET, (uint32_t)ch);
}
Expand Down Expand Up @@ -3323,7 +3336,7 @@ static void imxrt_txint(struct uart_dev_s *dev, bool enable)

/* Enable interrupt for TX complete */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
if (enable)
{
#ifndef CONFIG_SUPPRESS_SERIAL_INTS
Expand All @@ -3339,7 +3352,7 @@ static void imxrt_txint(struct uart_dev_s *dev, bool enable)
regval &= ~LPUART_ALL_INTS;
regval |= priv->ie;
imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
#endif

Expand All @@ -3360,7 +3373,7 @@ static void imxrt_singlewire_txint(struct uart_dev_s *dev, bool enable)

/* Enable interrupt for TX complete */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);
regval = imxrt_serialin(priv, IMXRT_LPUART_CTRL_OFFSET);
if (enable)
{
Expand All @@ -3384,7 +3397,7 @@ static void imxrt_singlewire_txint(struct uart_dev_s *dev, bool enable)
regval |= priv->ie;

imxrt_serialout(priv, IMXRT_LPUART_CTRL_OFFSET, regval);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
#endif

Expand Down

0 comments on commit 30940d9

Please sign in to comment.