serial: Use -EAGAIN in getc and putc
PATCH 1/1 serial: Use -EAGAIN in getc and putc
U-Boot serial code already handles -EAGAIN value from getc and putc callbacks. So change drivers code to return -EAGAIN when HW is busy instead of doing its own busy loop and waiting until HW is ready.
--- drivers/serial/serial_arc.c | 8 ++++---- drivers/serial/serial_lpuart.c | 8 ++++---- drivers/serial/serial_mvebu_a3700.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-)
drivers/serial/serial_arc.c +4 -4
@@ -53,8 +53,8 @@ static int arc_serial_putc(struct udevice *dev, const char c)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!(readb (®s->status) &UART_TXEMPTY )) - ; +if (!(readb (®s->status) &UART_TXEMPTY )) +return -EAGAIN ;writeb (c, ®s->data);
@@ -83,8 +83,8 @@ static int arc_serial_getc(struct udevice *dev)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!arc_serial_tstc (regs)) - ; +if (!arc_serial_tstc (regs)) +return -EAGAIN ;/* Check for overflow errors */ if (readb (®s->status) &UART_OVERFLOW_ERR )
drivers/serial/serial_lpuart.c +4 -4
@@ -168,8 +168,8 @@ static void _lpuart_serial_setbrg(struct udevice *dev,static int _lpuart_serial_getc (struct lpuart_serial_plat *plat) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) -schedule (); +if (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) +return -EAGAIN ;barrier ();
@@ -181,8 +181,8 @@ static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) &US1_TDRE )) -schedule (); +if (!(__raw_readb (&base->us1) &US1_TDRE )) +return -EAGAIN ;__raw_writeb (c, &base->ud); }
drivers/serial/serial_mvebu_a3700.c +4 -4
@@ -40,8 +40,8 @@ static int mvebu_serial_putc(struct udevice *dev, const char ch)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) - ; +if (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) +return -EAGAIN ;writel (ch, base +UART_TX_REG );
@@ -53,8 +53,8 @@ static int mvebu_serial_getc(struct udevice *dev)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) - ; +if (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) +return -EAGAIN ;return readl (base +UART_RX_REG ) &0xff ; }
Signature · 1 line
2.20.1
Quoted 11 lines · Pali Rohár
U-Boot serial code already handles
-EAGAINvalue from getc and putc callbacks. So change drivers code to return-EAGAINwhen HW is busy instead of doing its own busy loop and waiting until HW is ready.Signed-off-by Pali Rohár <[email protected]>--- drivers/serial/serial_arc.c | 8 ++++---- drivers/serial/serial_lpuart.c | 8 ++++---- drivers/serial/serial_mvebu_a3700.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-)
Quoted 5 lines · Pali Rohár
U-Boot serial code already handles
-EAGAINvalue from getc and putc callbacks. So change drivers code to return-EAGAINwhen HW is busy instead of doing its own busy loop and waiting until HW is ready.Signed-off-by Pali Rohár <[email protected]>
Thanks,
Stefan
Quoted 74 lines
--- drivers/serial/serial_arc.c | 8 ++++---- drivers/serial/serial_lpuart.c | 8 ++++---- drivers/serial/serial_mvebu_a3700.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-)drivers/serial/serial_arc.c +4
-4@@ -53,8 +53,8 @@ static int arc_serial_putc(struct udevice *dev, const char c)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!(readb (®s->status) &UART_TXEMPTY )) - ; +if (!(readb (®s->status) &UART_TXEMPTY )) +return -EAGAIN ;writeb (c, ®s->data);@@ -83,8 +83,8 @@ static int arc_serial_getc(struct udevice *dev)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!arc_serial_tstc (regs)) - ; +if (!arc_serial_tstc (regs)) +return -EAGAIN ;/* Check for overflow errors */ if (readb (®s->status) &UART_OVERFLOW_ERR )drivers/serial/serial_lpuart.c +4
-4@@ -168,8 +168,8 @@ static void _lpuart_serial_setbrg(struct udevice *dev,static int _lpuart_serial_getc (struct lpuart_serial_plat *plat) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) -schedule (); +if (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) +return -EAGAIN ;barrier ();@@ -181,8 +181,8 @@ static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) &US1_TDRE )) -schedule (); +if (!(__raw_readb (&base->us1) &US1_TDRE )) +return -EAGAIN ;__raw_writeb (c, &base->ud); }drivers/serial/serial_mvebu_a3700.c +2
-2@@ -40,8 +40,8 @@ static int mvebu_serial_putc(struct udevice *dev, const char ch)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) - ; +if (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) +return -EAGAIN ;writel (ch, base +UART_TX_REG );
drivers/serial/serial_mvebu_a3700.c static int mvebu_serial_getc(struct udevice *dev) · line 60
Quoted 10 lines
drivers/serial/serial_mvebu_a3700.c +2
-2@@ -53,8 +53,8 @@ static int mvebu_serial_getc(struct udevice *dev)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) - ; +if (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) +return -EAGAIN ;return readl (base +UART_RX_REG ) &0xff ; }
Viele Grüße,
Stefan Roese
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Erika Unter HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Quoted 7 lines · Pali Rohár
U-Boot serial code already handles
-EAGAINvalue from getc and putc callbacks. So change drivers code to return-EAGAINwhen HW is busy instead of doing its own busy loop and waiting until HW is ready.Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>Reviewed-by Stefan Roese <[email protected]>
[snip]
Quoted 16 lines
drivers/serial/serial_lpuart.c +2
-2@@ -168,8 +168,8 @@ static void _lpuart_serial_setbrg(struct udevice *dev,static int _lpuart_serial_getc (struct lpuart_serial_plat *plat) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) -schedule (); +if (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) +return -EAGAIN ;barrier ();
drivers/serial/serial_lpuart.c static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, · line 188
Quoted 10 lines
drivers/serial/serial_lpuart.c +2
-2@@ -181,8 +181,8 @@ static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) &US1_TDRE )) -schedule (); +if (!(__raw_readb (&base->us1) &US1_TDRE )) +return -EAGAIN ;__raw_writeb (c, &base->ud); }
This is non-trivially not right for this driver. ->putc here is set to lpuart_serial_putc which calls _lpuart_serial_putc OR _lpuart32_serial_putc, so there's the lpuart32 cases to address, and then lpuart_serial_putc needs to just return whatever one it called rather than always returning 0.
Signature · 1 line
Tom
Quoted 34 lines · On Thursday 08 December 2022 10:45:49 Tom Rini wrote:
Quoted 7 lines · Pali Rohár
U-Boot serial code already handles
-EAGAINvalue from getc and putc callbacks. So change drivers code to return-EAGAINwhen HW is busy instead of doing its own busy loop and waiting until HW is ready.Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>Reviewed-by Stefan Roese <[email protected]>[snip]
Quoted 24 lines
drivers/serial/serial_lpuart.c +4
-4@@ -168,8 +168,8 @@ static void _lpuart_serial_setbrg(struct udevice *dev,static int _lpuart_serial_getc (struct lpuart_serial_plat *plat) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) -schedule (); +if (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) +return -EAGAIN ;barrier ();@@ -181,8 +181,8 @@ static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) &US1_TDRE )) -schedule (); +if (!(__raw_readb (&base->us1) &US1_TDRE )) +return -EAGAIN ;
Quoted 11 lines
drivers/serial/serial_lpuart.c static void _lpuart_serial_putc(struct lpuart_serial_plat *plat, · line 188
Quoted 2 lines
drivers/serial/serial_lpuart.c +0
-0__raw_writeb (c, &base->ud); }This is non-trivially not right for this driver. ->putc here is set to
lpuart_serial_putcwhich calls_lpuart_serial_putcOR_lpuart32_serial_putc, so there's the lpuart32 cases to address, and thenlpuart_serial_putcneeds to just return whatever one it called rather than always returning 0.Signature · 1 line
Tom
Ou, you are right. This is really incomplete and does not work correctly. Thanks for spotting this issue, I will send fixed patch.
PATCH v2 1/1 serial: Use -EAGAIN in getc and putc
In reply to Pali RohárU-Boot serial code already handles -EAGAIN value from getc and putc callbacks. So change drivers code to return -EAGAIN when HW is busy instead of doing its own busy loop and waiting until HW is ready.
---
Changes in v2:
* Fix serial_lpuart.c code after Tom's review
* Add serial_mpc8xx.c change
To prevent merge conflicts, please apply this patch *after* this:
http://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
--- drivers/serial/serial_arc.c | 8 +++---- drivers/serial/serial_lpuart.c | 36 ++++++++++++----------------- drivers/serial/serial_mpc8xx.c | 12 ++++------ drivers/serial/serial_mvebu_a3700.c | 8 +++---- 4 files changed, 28 insertions(+), 36 deletions(-)
drivers/serial/serial_arc.c +4 -4
@@ -53,8 +53,8 @@ static int arc_serial_putc(struct udevice *dev, const char c)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!(readb (®s->status) &UART_TXEMPTY )) - ; +if (!(readb (®s->status) &UART_TXEMPTY )) +return -EAGAIN ;writeb (c, ®s->data);
@@ -83,8 +83,8 @@ static int arc_serial_getc(struct udevice *dev)struct arc_serial_plat *plat =dev_get_plat (dev);struct arc_serial_regs *const regs = plat->reg; -while (!arc_serial_tstc (regs)) - ; +if (!arc_serial_tstc (regs)) +return -EAGAIN ;/* Check for overflow errors */ if (readb (®s->status) &UART_OVERFLOW_ERR )
drivers/serial/serial_lpuart.c +15 -21
@@ -168,23 +168,24 @@ static void _lpuart_serial_setbrg(struct udevice *dev,static int _lpuart_serial_getc (struct lpuart_serial_plat *plat) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) -schedule (); +if (!(__raw_readb (&base->us1) & (US1_RDRF |US1_OR ))) +return -EAGAIN ;barrier ();return __raw_readb (&base->ud); } -static void _lpuart_serial_putc (struct lpuart_serial_plat *plat, +static int _lpuart_serial_putc (struct lpuart_serial_plat *plat,const char c) {struct lpuart_fsl *base = plat->reg; -while (!(__raw_readb (&base->us1) &US1_TDRE )) -schedule (); +if (!(__raw_readb (&base->us1) &US1_TDRE )) +return -EAGAIN ;__raw_writeb (c, &base->ud); +return 0 ; }/* Test whether a character is in the RX buffer */
@@ -328,10 +329,9 @@ static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)u32 stat, val;lpuart_read32 (plat->flags, &base->stat, &stat); -while ((stat &STAT_RDRF ) ==0 ) { +if ((stat &STAT_RDRF ) ==0 ) {lpuart_write32 (plat->flags, &base->stat,STAT_FLAGS ); -schedule (); -lpuart_read32 (plat->flags, &base->stat, &stat); +return -EAGAIN ; }lpuart_read32 (plat->flags, &base->data, &val);
@@ -343,22 +343,18 @@ static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)return val &0x3ff ; } -static void _lpuart32_serial_putc (struct lpuart_serial_plat *plat, +static int _lpuart32_serial_putc (struct lpuart_serial_plat *plat,const char c) {struct lpuart_fsl_reg32 *base = plat->reg;u32 stat; -while (true) { -lpuart_read32 (plat->flags, &base->stat, &stat); - -if ((stat &STAT_TDRE )) -break ; - -schedule (); - } +lpuart_read32 (plat->flags, &base->stat, &stat); +if (!(stat &STAT_TDRE )) +return -EAGAIN ;lpuart_write32 (plat->flags, &base->data, c); +return 0 ; }/* Test whether a character is in the RX buffer */
@@ -453,11 +449,9 @@ static int lpuart_serial_putc(struct udevice *dev, const char c)struct lpuart_serial_plat *plat =dev_get_plat (dev);if (is_lpuart32 (dev)) -_lpuart32_serial_putc (plat, c); -else -_lpuart_serial_putc (plat, c); +return _lpuart32_serial_putc (plat, c); -return 0 ; +return _lpuart_serial_putc (plat, c); }static int lpuart_serial_pending (struct udevice *dev,bool input)
drivers/serial/serial_mpc8xx.c +5 -7
@@ -178,14 +178,13 @@ static int serial_mpc8xx_putc(struct udevice *dev, const char c) rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE ]; -/* Wait for last character to go. */ +if (in_be16 (&rtx->txbd.cbd_sc) &BD_SC_READY ) +return -EAGAIN ; +out_8 (&rtx->txbuf, c);out_be16 (&rtx->txbd.cbd_datlen,1 );setbits_be16 (&rtx->txbd.cbd_sc,BD_SC_READY ); -while (in_be16 (&rtx->txbd.cbd_sc) &BD_SC_READY ) -schedule (); -return 0 ; }
@@ -199,9 +198,8 @@ static int serial_mpc8xx_getc(struct udevice *dev) rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE ]; -/* Wait for character to show up. */ -while (in_be16 (&rtx->rxbd.cbd_sc) &BD_SC_EMPTY ) -schedule (); +if (in_be16 (&rtx->rxbd.cbd_sc) &BD_SC_EMPTY ) +return -EAGAIN ;/* the characters are read one by one, * use the rxindex to know the next char to deliver
drivers/serial/serial_mvebu_a3700.c +4 -4
@@ -40,8 +40,8 @@ static int mvebu_serial_putc(struct udevice *dev, const char ch)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) - ; +if (readl (base +UART_STATUS_REG ) &UART_STATUS_TXFIFO_FULL ) +return -EAGAIN ;writel (ch, base +UART_TX_REG );
@@ -53,8 +53,8 @@ static int mvebu_serial_getc(struct udevice *dev)struct mvebu_plat *plat =dev_get_plat (dev);void __iomem *base = plat->base; -while (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) - ; +if (!(readl (base +UART_STATUS_REG ) &UART_STATUS_RX_RDY )) +return -EAGAIN ;return readl (base +UART_RX_REG ) &0xff ; }
Signature · 1 line
2.20.1
Quoted 5 lines · Pali Rohár
U-Boot serial code already handles
-EAGAINvalue from getc and putc callbacks. So change drivers code to return-EAGAINwhen HW is busy instead of doing its own busy loop and waiting until HW is ready.Signed-off-by Pali Rohár <[email protected]>
Applied to u-boot/next, thanks!
Signature · 1 line
Tom