Skip to contentSkip to conversation

serial: sh-sci: Support for HSCIF RX sampling point adjustment

PATCH In review Ulrich Hecht 6 participants 18 messages Apr 4, 2018 – Apr 1, 2019 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 serial: sh-sci: Support for HSCIF RX sampling point adjustment

HSCIF has facilities that allow moving the RX sampling point by between -8 and 7 sampling cycles (one sampling cycles equals 1/15 of a bit by default) to improve the error margin in case of slightly mismatched bit rates between sender and receiver.

This patch tries to determine if shifting the sampling point can improve the error margin and will enable it if so.

---
This revision dumps the sysfs interface and works out the optimal shift on its own. It also moves setting of the HSSRR register back to its original location.

CU
Uli

 drivers/tty/serial/sh-sci.c | 65 +++++++++++++++++++++++++++++----------------
 drivers/tty/serial/sh-sci.h |  4 +++
 2 files changed, 46 insertions(+), 23 deletions(-)
drivers/tty/serial/sh-sci.c +42 -23
index fdbbff5..5d61654 100644
@@ -2390,6 +2390,27 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	uart_update_timeout(port, termios->c_cflag, baud);
 
+	/* byte size and parity */
+	switch (termios->c_cflag & CSIZE) {
+	case CS5:
+		bits = 7;
+		break;
+	case CS6:
+		bits = 8;
+		break;
+	case CS7:
+		bits = 9;
+		break;
+	default:
+		bits = 10;
+		break;
+	}
+
+	if (termios->c_cflag & CSTOPB)
+		bits++;
+	if (termios->c_cflag & PARENB)
+		bits++;
+
 	if (best_clk >= 0) {
 		if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
 			switch (srr + 1) {
@@ -2406,8 +2427,27 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 		serial_port_out(port, SCSCR, scr_val | s->hscif_tot);
 		serial_port_out(port, SCSMR, smr_val);
 		serial_port_out(port, SCBRR, brr);
-		if (sci_getreg(port, HSSRR)->size)
-			serial_port_out(port, HSSRR, srr | HSCIF_SRE);
+		if (sci_getreg(port, HSSRR)->size) {
+			unsigned int hssrr = srr | HSCIF_SRE;
+			/* Calculate deviation from intended rate at the
+			 * center of the last stop bit in sampling clocks.
+			 */
+			int last_stop = bits * 2 - 1;
+			int deviation = min_err * srr * last_stop / 2 / baud;
+
+			if (abs(deviation) >= 2) {
+				/* At least two sampling clocks off at the
+				 * last stop bit; we can increase the error
+				 * margin by shifting the sampling point.
+				 */
+				int shift = min(-8, max(7, deviation / 2));
+
+				hssrr |= (shift << HSCIF_SRHP_SHIFT) &
+					 HSCIF_SRHP_MASK;
+				hssrr |= HSCIF_SRDE;
+			}
+			serial_port_out(port, HSSRR, hssrr);
+		}
 
 		/* Wait one bit interval */
 		udelay((1000000 + (baud - 1)) / baud);
@@ -2474,27 +2514,6 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 	 * value obtained by this formula is too small. Therefore, if the value
 	 * is smaller than 20ms, use 20ms as the timeout value for DMA.
 	 */
-	/* byte size and parity */
-	switch (termios->c_cflag & CSIZE) {
-	case CS5:
-		bits = 7;
-		break;
-	case CS6:
-		bits = 8;
-		break;
-	case CS7:
-		bits = 9;
-		break;
-	default:
-		bits = 10;
-		break;
-	}
-
-	if (termios->c_cflag & CSTOPB)
-		bits++;
-	if (termios->c_cflag & PARENB)
-		bits++;
-
 	s->rx_frame = (10000 * bits) / (baud / 100);
 #ifdef CONFIG_SERIAL_SH_SCI_DMA
 	s->rx_timeout = s->buf_len_rx * 2 * s->rx_frame;
drivers/tty/serial/sh-sci.h +4 -0
index a5f792f..0b9e804 100644
@@ -130,6 +130,10 @@ enum {
 
 /* HSSRR HSCIF */
 #define HSCIF_SRE	BIT(15)	/* Sampling Rate Register Enable */
+#define HSCIF_SRDE	BIT(14) /* Sampling Point Register Enable */
+
+#define HSCIF_SRHP_SHIFT	8
+#define HSCIF_SRHP_MASK		0x0f00
 
 /* SCPCR (Serial Port Control Register), SCIFA/SCIFB only */
 #define SCPCR_RTSC	BIT(4)	/* Serial Port RTS# Pin / Output Pin */
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#1 lore raw

Hi Ulrich,

Quoted 5 lines · Ulrich Hecht

HSCIF has facilities that allow moving the RX sampling point by between -8 and 7 sampling cycles (one sampling cycles equals 1/15 of a bit by default) to improve the error margin in case of slightly mismatched bit rates between sender and receiver.

Quoted 8 lines

This patch tries to determine if shifting the sampling point can improve the error margin and will enable it if so.

---
This revision dumps the sysfs interface and works out the optimal shift on its own. It also moves setting of the HSSRR register back to its original location.

Thanks for the update!

One suggestion for improvement below.

Quoted 6 lines
drivers/tty/serial/sh-sci.c +0 -0
@@ -2406,8 +2427,27 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
                serial_port_out(port, SCSCR, scr_val | s->hscif_tot);
                serial_port_out(port, SCSMR, smr_val);

drivers/tty/serial/sh-sci.c static void sci_set_termios(struct uart_port *port, struct ktermios *termios, · line 2436

Quoted 10 lines
drivers/tty/serial/sh-sci.c +7 -2
                serial_port_out(port, SCBRR, brr);
-               if (sci_getreg(port, HSSRR)->size)
-                       serial_port_out(port, HSSRR, srr | HSCIF_SRE);
+               if (sci_getreg(port, HSSRR)->size) {
+                       unsigned int hssrr = srr | HSCIF_SRE;
+                       /* Calculate deviation from intended rate at the
+                        * center of the last stop bit in sampling clocks.
+                        */
+                       int last_stop = bits * 2 - 1;
+                       int deviation = min_err * srr * last_stop / 2 / baud;

DIV_ROUND_CLOSEST()?

Quoted 7 lines
drivers/tty/serial/sh-sci.c +7 -0
+
+                       if (abs(deviation) >= 2) {
+                               /* At least two sampling clocks off at the
+                                * last stop bit; we can increase the error
+                                * margin by shifting the sampling point.
+                                */
+                               int shift = min(-8, max(7, deviation / 2));

drivers/tty/serial/sh-sci.c static void sci_set_termios(struct uart_port *port, struct ktermios *termios, · line 2453

Quoted 10 lines
drivers/tty/serial/sh-sci.c +7 -0
+
+                               hssrr |= (shift << HSCIF_SRHP_SHIFT) &
+                                        HSCIF_SRHP_MASK;
+                               hssrr |= HSCIF_SRDE;
+                       }
+                       serial_port_out(port, HSSRR, hssrr);
+               }
 
                /* Wait one bit interval */
                udelay((1000000 + (baud - 1)) / baud);

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw
In reply to Ulrich Hecht

Dear Renesas Open Source team,

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/horms/renesas-bsp.git/tag/?h=rcar-3.9.3
[2] https://patchwork.kernel.org/patch/10322809/
[3] https://git.kernel.org/pub/scm/linux/kernel/git/horms/renesas-bsp.git/commit/?id=e395fc813539
[4] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=63ba1e00f178

Best regards,
Eugeniu.

#3 lore raw

Hi Eugeniu,

Quoted 10 lines

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Thank you for this report! True, the information is scarce, but we will see what we can do with it. If we find something, I'll report back.

Kind regards,

Wolfram

#4 lore raw
In reply to Eugeniu Rosca

Hi Eugeniu,

Quoted 15 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful questions to let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

    sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
    sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 1 line

+ int shift = min(-8, max(7, deviation / 2));

Oops, min and max are exchanged!

I guess using

    int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Thanks!

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#5 lore raw

Hi Geert,

Quoted 50 lines · Geert Uytterhoeven

Hi Eugeniu,

Quoted 15 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful questions to let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

     sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
     sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 1 line

+ int shift = min(-8, max(7, deviation / 2));

Quoted 7 lines

Oops, min and max are exchanged!

I guess using

     int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Thanks again!

Dirk

#6 lore raw
Quoted 58 lines · Dirk Behme

Hi Geert,

Quoted 55 lines · Geert Uytterhoeven

Hi Eugeniu,

Quoted 15 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after writing the below paragraphs, which are still useful questions to let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

     sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32 sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+                               int shift = min(-8, max(7, deviation / 2));

Oops, min and max are exchanged!

I guess using

Quoted 8 lines
Quoted 3 lines

     int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

Best regards

Dirk

From 32cde852a03fa7fbb450d5665785b1ff390d3867 Mon Sep 17 00:00:00 2001
From: Geert Uytterhoeven <[email protected]>
Date: Thu, 28 Mar 2019 12:10:58 +0100
Subject: [PATCH] serial: sh-sci: Fix HSCIF RX sampling point adjustment

In commit 63ba1e00f178 ("serial: sh-sci: Support for HSCIF RX sampling point adjustment") min and max are exchanged. Fix this.

Fixes 63ba1e00f178 ("serial: sh-sci: Support for HSCIF RX sampling point adjustment")
Cc <[email protected]> # v4.18+
---
 drivers/tty/serial/sh-sci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/sh-sci.c +1 -1
index eba08cd892e5..69896d586a29 100644
@@ -2446,7 +2446,7 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 				 * last stop bit; we can increase the error
 				 * margin by shifting the sampling point.
 				 */
-				int shift = min(-8, max(7, deviation / 2));
+				int shift = clamp(deviation / 2, -8, 7);
 
 				hssrr |= (shift << HSCIF_SRHP_SHIFT) &
 					 HSCIF_SRHP_MASK;
Signature · 1 line
2.20.0
#7 lore raw

Hi Geert,

Quoted 64 lines · Dirk Behme
Quoted 63 lines · Dirk Behme

Hi Geert,

Quoted 58 lines · Geert Uytterhoeven

Hi Eugeniu,

Quoted 15 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after writing the below paragraphs, which are still useful questions to let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

     sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32 sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+                               int shift = min(-8, max(7, deviation / 2));

Oops, min and max are exchanged!

I guess using

     int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Quoted 6 lines
Quoted 3 lines

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

* Testing the patch [5]

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

does *not* fix our issue. Or in other words: Testing was *not* successful.

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41, base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps

* We are talking about a custom r8a7796 board

Best regards

Dirk

[5] https://patchwork.kernel.org/patch/10322809/#22554931

[6]

[pid  4715] newfstatat(AT_FDCWD, "/dev/ttySC3", {st_mode=S_IFCHR|0777, st_rdev=makedev(204, 11), ...}, 0) = 0
[pid  4715] openat(AT_FDCWD, "/dev/ttySC3", O_RDWR) = 9
[pid  4715] ioctl(9, TCGETS, {B9600 opost isig icanon echo ...}) = 0
[pid  4715] ioctl(9, TCFLSH, TCIFLUSH)  = 0
[pid  4715] ioctl(9, SNDCTL_TMR_START or TCSETS, {B57600 -opost -isig -icanon -echo ...}) = 0
[pid  4715] ioctl(9, TIOCMGET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4715] ioctl(9, TIOCMSET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4715] ioctl(9, TIOCMGET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4715] ioctl(9, TIOCMSET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4715] nanosleep({0, 250000000}, NULL) = 0
[pid  4715] mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0xffffbc192000
[pid  4715] mprotect(0xffffbc192000, 4096, PROT_NONE) = 0
[pid  4715] clone(child_stack=0xffffbc990af0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xffffbc9912b0, tls=0xffffbc9918d0, child_tidptr=0xffffbc9912b0) = 4720
strace: Process 4720 attached
[pid  4715] futex(0xffff200082cc, FUTEX_WAIT_BITSET_PRIVATE, 1, {442, 798479296}, ffffffff <unfinished ...>
[pid  4720] set_robust_list(0xffffbc9912c0, 24) = 0
[pid  4720] prctl(PR_SET_NAME, "link_thread\0\0\0\0\0") = 0
[pid  4720] mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0xffff27800000
[pid  4720] mmap(0xffff24000000, 67108864, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0xffff1c000000
[pid  4720] mprotect(0xffff1c000000, 135168, PROT_READ|PROT_WRITE) = 0
[pid  4720] mprotect(0xffff27800000, 4096, PROT_NONE) = 0
[pid  4720] clone(strace: Process 4721 attached
child_stack=0xffff27ffeaf0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xffff27fff2b0, tls=0xffff27fff8d0, child_tidptr=0xffff27fff2b0) = 4721
[pid  4721] set_robust_list(0xffff27fff2c0, 24 <unfinished ...>
[pid  4720] ioctl(9, TIOCMGET <unfinished ...>
[pid  4721] <... set_robust_list resumed> ) = 0
[pid  4720] <... ioctl resumed> , [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4721] prctl(PR_SET_NAME, "link_reader\0\0\0\0\0" <unfinished ...>
[pid  4720] ioctl(9, TIOCMSET, [TIOCM_DTR|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR] <unfinished ...>
[pid  4721] <... prctl resumed> )       = 0
[pid  4720] <... ioctl resumed> )       = 0
[pid  4721] ppoll([{fd=9, events=POLLIN}], 1, {1, 0}, NULL, 0 <unfinished ...>
[pid  4720] nanosleep({0, 250000000}, NULL) = 0
[pid  4720] ioctl(9, TIOCMGET, [TIOCM_DTR|TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4720] ioctl(9, TIOCMSET, [TIOCM_CTS|TIOCM_CAR|TIOCM_DSR]) = 0
[pid  4720] nanosleep({0, 100000000}, NULL) = 0
[pid  4720] ioctl(9, TCGETS, {B57600 -opost -isig -icanon -echo ...}) = 0
[pid  4720] ioctl(9, TCFLSH, TCIFLUSH)  = 0
[pid  4720] ioctl(9, SNDCTL_TMR_START or TCSETS, {B57600 -opost -isig -icanon -echo ...}) = 0
[pid  4720] write(9, "\336\306\0\0\0\4\0\0\0\0\304\250", 12) = 12
[pid  4720] futex(0xffff2000824c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008248, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4716] <... futex resumed> )       = 0
[pid  4720] futex(0xffff20008d9c, FUTEX_WAIT_PRIVATE, 1, NULL <unfinished ...>
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 5, {383, 226656729}, ffffffff) = -1 ETIMEDOUT (Connection timed out)
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff20008d9c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008d98, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4720] <... futex resumed> )       = 0
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 7, {393, 151155511}, ffffffff <unfinished ...>
[pid  4720] futex(0xffff20008d60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4720] write(9, "\336\306\0\0\0\4\0\0\0\0\304\250", 12) = 12
[pid  4720] futex(0xffff2000824c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008248, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4720] futex(0xffff20008d9c, FUTEX_WAIT_PRIVATE, 3, NULL <unfinished ...>
[pid  4716] <... futex resumed> )       = 0
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 9, {383, 301617693}, ffffffff) = -1 ETIMEDOUT (Connection timed out)
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff20008d9c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008d98, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 11, {393, 150961530}, ffffffff <unfinished ...>
[pid  4720] <... futex resumed> )       = 0
[pid  4720] futex(0xffff20008d60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4720] write(9, "\336\306\0\0\0\4\0\0\0\0\304\250", 12) = 12
[pid  4720] futex(0xffff2000824c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008248, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4716] <... futex resumed> )       = 0
[pid  4720] futex(0xffff20008d9c, FUTEX_WAIT_PRIVATE, 5, NULL <unfinished ...>
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 13, {383, 377427852}, ffffffff) = -1 ETIMEDOUT (Connection timed out)
[pid  4716] futex(0xffff20008210, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  4716] futex(0xffff20008d9c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0xffff20008d98, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
[pid  4720] <... futex resumed> )       = 0
[pid  4716] futex(0xffff2000824c, FUTEX_WAIT_BITSET_PRIVATE, 15, {393, 150762113}, ffffffff <unfinished ...>
[pid  4720] futex(0xffff20008d60, FUTEX_WAKE_PRIVATE, 1) = 0

...

[7]

From 9a3c199f02cb66cb67e93e0824b03b622ab3b024 Mon Sep 17 00:00:00 2001
From: Dirk Behme <[email protected]>
Date: Fri, 29 Mar 2019 06:39:31 +0100
Subject: [PATCH] serial: sh-sci: Enable debug output

Enable some debug output as requested by Geert in

https://patchwork.kernel.org/patch/10322809/#22554727

Change-Id Icd2f97138516a0e40726fa1ccd50a69abb57cb76
---
 drivers/tty/serial/sh-sci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/sh-sci.c +2 -2
index eba08cd892e5..32210cf2413c 100644
@@ -2161,7 +2161,7 @@ static int sci_brg_calc(struct sci_port *s, unsigned int bps,
 			break;
 	}
 
-	dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
+	dev_info(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
 		min_err, *dlr, *srr + 1);
 	return min_err;
 }
@@ -2376,7 +2376,7 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 
 done:
 	if (best_clk >= 0)
-		dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
+		dev_info(port->dev, "Using clk %pC for %u%+d bps\n",
 			s->clks[best_clk], baud, min_err);
 
 	sci_port_enable(s);
Signature · 1 line
2.20.0
#8 lore raw
Quoted 10 lines · Dirk Behme

Hi Geert,

Quoted 1 line · Dirk Behme

On 28.03.2019 11:16, Dirk Behme wrote:

* Testing the patch [5]

Quoted 7 lines

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

does *not* fix our issue. Or in other words: Testing was *not* successful.

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

It should.

Quoted 6 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

Quoted 8 lines
[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41, 
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps

* We are talking about a custom r8a7796 board

Reviewing the code, I have found another potential issue: the SRR (sampling rate) value is not validated; only values from 7 to 31 are permissible, according to the datasheet. The values in the debug output above would be fine, though.

So, for clarification: Is there a problem at 9600/57600 bps, or does it only occur at 115200 bps?

CU
Uli

#9 lore raw

Hi Uli,

Quoted 1 line · Ulrich Hecht

Reviewing the code, I have found another potential issue: the SRR (sampling rate) value is not validated; only values from 7 to 31 are permissible, according to the datasheet. The values in the debug output above would be fine, though.

Isn't that handled by

                .sampling_rate_mask = SCI_SR_RANGE(8, 32),

?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#10 lore raw
Quoted 6 lines · Geert Uytterhoeven

Hi Uli,

Quoted 8 lines
Quoted 1 line · Ulrich Hecht

Reviewing the code, I have found another potential issue: the SRR (sampling rate) value is not validated; only values from 7 to 31 are permissible, according to the datasheet. The values in the debug output above would be fine, though.

Isn't that handled by

                .sampling_rate_mask = SCI_SR_RANGE(8, 32),

?

Having crawled through the related macros, I can confirm that it is.

CU
Uli

#11 lore raw
Quoted 13 lines · Ulrich Hecht
Quoted 11 lines · Geert Uytterhoeven

Hi Uli,

Quoted 1 line · Ulrich Hecht

Reviewing the code, I have found another potential issue: the SRR (sampling rate) value is not validated; only values from 7 to 31 are permissible, according to the datasheet. The values in the debug output above would be fine, though.

Quoted 7 lines
Quoted 5 lines

Isn't that handled by

                .sampling_rate_mask = SCI_SR_RANGE(8, 32),

?

Having crawled through the related macros, I can confirm that it is.

There is, however, another restriction that is not checked for: The sampling point shift must not exceed half of the sampling rate in either direction. This could happen ATM if the sampling rate is below 16 and the deviation from the desired bit rate is quite large.

That is not so in the case for which debug output has been provided, though, in which the bit rate error is so small that shifting isn't enabled to begin with. I would thus still like to know the numbers for 115200 bps.

CU
Uli

#12 lore raw
In reply to Ulrich Hecht
Quoted 33 lines · Ulrich Hecht
Quoted 10 lines · Dirk Behme

Hi Geert,

Quoted 1 line · Dirk Behme

On 28.03.2019 11:16, Dirk Behme wrote:

* Testing the patch [5]

Quoted 7 lines

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

does *not* fix our issue. Or in other words: Testing was *not* successful.

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

It should.

Quoted 13 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41,
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps
Quoted 5 lines
Quoted 1 line

* We are talking about a custom r8a7796 board

Reviewing the code, I have found another potential issue: the SRR (sampling rate) value is not validated; only values from 7 to 31 are permissible, according to the datasheet. The values in the debug output above would be fine, though.

So, for clarification: Is there a problem at 9600/57600 bps, or does it only occur at 115200 bps?

The 115200 bps in the initial report was wrong (sorry!). Looking at the strace output provided to my understanding the issue happens with 57600 bps (it somehow starts with 9600 bps and switches to 57600 bps, then). I have to think about 115200 bps as currently we have only a precompiled binary to test with (the one the strace is from).

Best regards

Dirk

#13 lore raw
In reply to Dirk Behme

Hi Dirk,

Quoted 77 lines · Dirk Behme
Quoted 66 lines · Dirk Behme
Quoted 65 lines · Dirk Behme
Quoted 62 lines · Geert Uytterhoeven
Quoted 19 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the
latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which
mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially
adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful
questions to
let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

     sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
     sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+ int shift = min(-8, max(7, deviation
/ 2));

Oops, min and max are exchanged!

I guess using

     int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Quoted 8 lines
Quoted 5 lines

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

Quoted 6 lines

* Testing the patch [5]

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

does *not* fix our issue. Or in other words: Testing was *not* successful.

I'm sorry to hear that.

Quoted 2 lines

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

Yes, submitted.

Quoted 5 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

Quoted 8 lines
[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41,
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps

* We are talking about a custom r8a7796 board

The above values match with what I see on Salvator-X, after disabling scif_clk in DTS.

If your board does have scif_clk populated, you want to describe that in DT, as it may improve the situation.

BTW, have you verified the actual clock rate used? It wouldn't be the first time that a crystal description in DTS has a typo in its clock-frequency property, breaking UART communication at "high" speeds.
A simple way to do that is outputting a square wave using:

    yes U | tr -d "\n" > /dev/ttySC3

This should give an (approximate) square wave of 28797 Hz.

Quoted 38 lines

From 9a3c199f02cb66cb67e93e0824b03b622ab3b024 Mon Sep 17 00:00:00 2001
From: Dirk Behme <[email protected]>
Date: Fri, 29 Mar 2019 06:39:31 +0100
Subject: [PATCH] serial: sh-sci: Enable debug output

Enable some debug output as requested by Geert in

https://patchwork.kernel.org/patch/10322809/#22554727

Change-Id Icd2f97138516a0e40726fa1ccd50a69abb57cb76
---
  drivers/tty/serial/sh-sci.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/sh-sci.c +1 -1
index eba08cd892e5..32210cf2413c 100644
@@ -2161,7 +2161,7 @@ static int sci_brg_calc(struct sci_port *s,
 unsigned int bps,
                        break;
        }
 
-       dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
+       dev_info(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
                min_err, *dlr, *srr + 1);
        return min_err;
  }
drivers/tty/serial/sh-sci.c +1 -1
@@ -2376,7 +2376,7 @@ static void sci_set_termios(struct uart_port
 *port, struct ktermios *termios,
 
  done:
        if (best_clk >= 0)
-               dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
+               dev_info(port->dev, "Using clk %pC for %u%+d bps\n",
                        s->clks[best_clk], baud, min_err);
 
Quoted 1 line
        sci_port_enable(s);

You way want to enable printing of the other debug lines that contain "+d bps", for comparison with SCK and BRR alternatives.
But in the absence of a suitable scif_clk, the clock rate achieved using the BRG is the best option.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#14 lore raw
Quoted 162 lines · Geert Uytterhoeven

Hi Dirk,

Quoted 77 lines · Dirk Behme
Quoted 66 lines · Dirk Behme
Quoted 65 lines · Dirk Behme
Quoted 62 lines · Geert Uytterhoeven
Quoted 19 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the
latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which
mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially
adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful
questions to
let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

      sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
      sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+ int shift = min(-8, max(7, deviation
/ 2));

Oops, min and max are exchanged!

I guess using

      int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Quoted 8 lines
Quoted 5 lines

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

Quoted 6 lines

* Testing the patch [5]

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

does *not* fix our issue. Or in other words: Testing was *not* successful.

I'm sorry to hear that.

Quoted 2 lines

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

Yes, submitted.

Quoted 5 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

Quoted 8 lines
[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41,
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps

* We are talking about a custom r8a7796 board

The above values match with what I see on Salvator-X, after disabling scif_clk in DTS.

If your board does have scif_clk populated, you want to describe that in DT, as it may improve the situation.

BTW, have you verified the actual clock rate used? It wouldn't be the first time that a crystal description in DTS has a typo in its clock-frequency property, breaking UART communication at "high" speeds.
A simple way to do that is outputting a square wave using:

     yes U | tr -d "\n" > /dev/ttySC3

This should give an (approximate) square wave of 28797 Hz.

Quoted 38 lines

From 9a3c199f02cb66cb67e93e0824b03b622ab3b024 Mon Sep 17 00:00:00 2001
From: Dirk Behme <[email protected]>
Date: Fri, 29 Mar 2019 06:39:31 +0100
Subject: [PATCH] serial: sh-sci: Enable debug output

Enable some debug output as requested by Geert in

https://patchwork.kernel.org/patch/10322809/#22554727

Change-Id Icd2f97138516a0e40726fa1ccd50a69abb57cb76
---
   drivers/tty/serial/sh-sci.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/sh-sci.c +1 -1
index eba08cd892e5..32210cf2413c 100644
@@ -2161,7 +2161,7 @@ static int sci_brg_calc(struct sci_port *s,
 unsigned int bps,
                         break;
         }
 
-       dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
+       dev_info(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
                 min_err, *dlr, *srr + 1);
         return min_err;
   }
drivers/tty/serial/sh-sci.c +1 -1
@@ -2376,7 +2376,7 @@ static void sci_set_termios(struct uart_port
 *port, struct ktermios *termios,
 
   done:
         if (best_clk >= 0)
-               dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
+               dev_info(port->dev, "Using clk %pC for %u%+d bps\n",
                         s->clks[best_clk], baud, min_err);
 
Quoted 6 lines
Quoted 1 line
         sci_port_enable(s);

You way want to enable printing of the other debug lines that contain "+d bps", for comparison with SCK and BRR alternatives.
But in the absence of a suitable scif_clk, the clock rate achieved using the BRG is the best option.

Do you have any idea what might be the difference between reverting "serial: sh-sci: Support for HSCIF RX sampling point adjustment" (works) and not reverting that (doesn't work for us), then?

Best regards

Dirk

P.S.: I'll check the other topics above, thanks!

#15 lore raw

Hi Dirk,

Quoted 105 lines · Dirk Behme
Quoted 83 lines · Geert Uytterhoeven
Quoted 82 lines · Dirk Behme
Quoted 66 lines · Dirk Behme
Quoted 65 lines · Dirk Behme
Quoted 62 lines · Geert Uytterhoeven
Quoted 19 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the
latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which
mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially
adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful
questions to
let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

      sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
      sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+ int shift = min(-8, max(7, deviation
/ 2));

Oops, min and max are exchanged!

I guess using

      int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Quoted 8 lines
Quoted 5 lines

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

* Testing the patch [5]

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

Quoted 20 lines
Quoted 1 line

does *not* fix our issue. Or in other words: Testing was *not* successful.

I'm sorry to hear that.

Quoted 2 lines

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

Yes, submitted.

Quoted 11 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41,
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps
Quoted 3 lines

Do you have any idea what might be the difference between reverting "serial: sh-sci: Support for HSCIF RX sampling point adjustment" (works) and not reverting that (doesn't work for us), then?

Before that commit, the RX sampling point was not shifted. After that commit, it was incorrectly shifted by -8[*].
With my fix, it is shifted by 7[*], to compensate for a clock rate that is slightly off.

[*] In units of cycles of the sampling clock, which runs at SR * 57595 =

    575950 Hz.

However, doing the above calculation shows that's something wrong with the formula used by the driver: with SR = 10, the default sampling point at the center is at SR / 2 = 5, so the shift must be within [-4, +4], which is exceeded by using a value of 7.

    deviation = min_err * srr * last_stop / 2 / baud;

With:

    min_err = -5
    srr = 9
    last_stop = 19
    baud = 57600

Note that srr and baud are unsigned. Hence the multiplication and divisions are done in unsigned arithmetic, and we get deviation = 37282 instead of 0. Oops...

Fixed by:

- int deviation = min_err * srr * last_stop / 2 / baud;
+ int deviation = (int)(min_err * srr * last_stop) / 2 /
+ (int)baud;

Before I sent a patch: Uli, shouldn't the formula use "(srr + 1)" instead of "srr", as the actual sampling rate factor is one more than the value programmed in HSSRR.SRCYC?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#16 lore raw
Quoted 11 lines · Geert Uytterhoeven

Fixed by:

(quoted diff) +2 -1
-                       int deviation = min_err * srr * last_stop / 2 / baud;
+                       int deviation = (int)(min_err * srr * last_stop) / 2 /
+                                       (int)baud;

Before I sent a patch: Uli, shouldn't the formula use "(srr + 1)" instead of "srr", as the actual sampling rate factor is one more than the value programmed in HSSRR.SRCYC?

Yes, it should.

CU
Uli

#17 lore raw
In reply to Geert Uytterhoeven

Hi Geert,

Quoted 144 lines · Geert Uytterhoeven

Hi Dirk,

Quoted 105 lines · Dirk Behme
Quoted 83 lines · Geert Uytterhoeven
Quoted 82 lines · Dirk Behme
Quoted 66 lines · Dirk Behme
Quoted 65 lines · Dirk Behme
Quoted 62 lines · Geert Uytterhoeven
Quoted 19 lines · Eugeniu Rosca

We've recently switched from rcar-3.7.x to rcar-3.9.x [1] kernel and the
latter contains this patch [2] by virtue of rcar-3.9.0 commit [3], which
mirrors v4.18-rc1 commit [4] in mainline.

JFYI, quite far away in the delivery chain, we've received below report:

Quoted 3 lines

With this patch [2-4] there are reports about broken data communication with 115200 baud with SXM module. Reverting this patch results in successful communication, again.

While this scarce information barely helps anybody, I thought that sharing it with you might be beneficial in case you collect several reports linked to this specific commit in future, meaning it potentially
adds a regression.

Quoted 4 lines

Also, if you are aware of any userland changes that might be required/assumed by this patch or in case you have any alternative ideas how to avoid reverting this patch, your feedback would be very appreciated.

Thanks for your report!

[TLDR: skip to the suggested fix below; I only noticed the bug after
writing the below paragraphs, which are still useful
questions to
let us reproduce the issue]

Which SoC are you using?
I assume this is on a custom board, as Salvator-X(S) and ULCB have external SCIF clock crystals, which allow to use a perfect 115200 bps, hence the affected code path is not exercised:

       sh-sci e6550000.serial: BRG: 115200+0 bps using DL 4 SR 32
       sh-sci e6550000.serial: Using clk scif for 115200+0 bps

Does your board have an external SCIF clock? Which frequency? Can you check the clock values and deviation for your configuration, by changing the calls to print the above information from dev_dbg() to dev_info()?

Does adding the DIV_ROUND_CLOSEST(), as suggested in my review of the posted patch, help?
Perhaps the sampling point shift is inverted? Does -shift work better?

[possible solution]

Quoted 2 lines

+ int shift = min(-8, max(7, deviation
/ 2));

Oops, min and max are exchanged!

I guess using

       int shift = clamp(deviation / 2, -8, 7)

instead fixes the issue?

Quoted 8 lines
Quoted 5 lines

Uh, that was fast :) Many thanks!

We will test this as fast as possible! But due to the long delivery chain Eugeniu mentioned this will take some time. I'll try my best to come back to you as fast as possible.

Just for the archives: We are testing the attached patch.

* Testing the patch [5]

- int shift = min(-8, max(7, deviation / 2));
+ int shift = clamp(deviation / 2, -8, 7);

Quoted 20 lines
Quoted 1 line

does *not* fix our issue. Or in other words: Testing was *not* successful.

I'm sorry to hear that.

Quoted 2 lines

* However, from review point of view we think that it fixes a serious bug. So maybe it should be applied, anyhow?

Yes, submitted.

Quoted 11 lines

* Using strace we managed to get some more information about the usage of the serial port [6]. With this, we are talking about 57600 and not 115200

* Switching to dev_info() [7] as requested above we get

[    0.553256] e6560000.serial: ttySC3 at MMIO 0xe6560000 (irq = 41,
base_baud = 0) is a hscif
[  161.418527] sh-sci e6560000.serial: BRG: 9600+0 bps using DL 1462 SR 19
[  161.418543] sh-sci e6560000.serial: Using clk s3d1 for 9600+0 bps
[  161.418813] sh-sci e6560000.serial: BRG: 57600-5 bps using DL 463 SR 10
[  161.418824] sh-sci e6560000.serial: Using clk s3d1 for 57600-5 bps
Quoted 3 lines

Do you have any idea what might be the difference between reverting "serial: sh-sci: Support for HSCIF RX sampling point adjustment" (works) and not reverting that (doesn't work for us), then?

Before that commit, the RX sampling point was not shifted. After that commit, it was incorrectly shifted by -8[*].
With my fix, it is shifted by 7[*], to compensate for a clock rate that is slightly off.

[*] In units of cycles of the sampling clock, which runs at SR * 57595 =

     575950 Hz.

However, doing the above calculation shows that's something wrong with the formula used by the driver: with SR = 10, the default sampling point at the center is at SR / 2 = 5, so the shift must be within [-4, +4], which is exceeded by using a value of 7.

     deviation = min_err * srr * last_stop / 2 / baud;

With:

     min_err = -5
     srr = 9
     last_stop = 19
     baud = 57600

Note that srr and baud are unsigned. Hence the multiplication and divisions are done in unsigned arithmetic, and we get deviation = 37282 instead of 0. Oops...

Fixed by:

(quoted diff) +2 -1
-                       int deviation = min_err * srr * last_stop / 2 / baud;
+                       int deviation = (int)(min_err * srr * last_stop) / 2 /
+                                       (int)baud;
Quoted 3 lines

Before I sent a patch: Uli, shouldn't the formula use "(srr + 1)" instead of "srr", as the actual sampling rate factor is one more than the value programmed in HSSRR.SRCYC?

Using both attached patches together our tests have been *successful*, now. :)

Many thanks!

Best regards

Dirk

From eac98a31c3f654ac2e7d6d77dcc648d4a504c1a5 Mon Sep 17 00:00:00 2001
From: Geert Uytterhoeven <[email protected]>
Date: Mon, 1 Apr 2019 06:30:04 +0200
Subject: [PATCH 2/2] serial: sh-sci: Fix HSCIF RX deviation calculation

Doing the deviation calculation manually shows that's something wrong with the formula used by the driver: with SR = 10, the default sampling point at the center is at SR / 2 = 5, so the shift must be within [-4, +4], which is exceeded by using a value of 7.

    deviation = min_err * srr * last_stop / 2 / baud;

With:

    min_err = -5
    srr = 9
    last_stop = 19
    baud = 57600

Note that srr and baud are unsigned. Hence the multiplication and divisions are done in unsigned arithmetic, and we get deviation = 37282 instead of 0. Fix this.

Additionally, use "(srr + 1)" instead of "srr", as the actual sampling rate factor is one more than the value programmed in HSSRR.SRCYC.

Fixes 63ba1e00f178a448 ("serial: sh-sci: Support for HSCIF RX sampling point adjustment")
---
 drivers/tty/serial/sh-sci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/sh-sci.c +2 -1
index 69896d586a29..971a2dee4d1e 100644
@@ -2439,7 +2439,8 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 			 * center of the last stop bit in sampling clocks.
 			 */
 			int last_stop = bits * 2 - 1;
-			int deviation = min_err * srr * last_stop / 2 / baud;
+			int deviation = (int)(min_err * (srr + 1) * last_stop) /
+					2 / (int)baud;
 
 			if (abs(deviation) >= 2) {
 				/* At least two sampling clocks off at the
Signature · 1 line
2.20.0

From a6d8b37dae6ee8621002804ca7841c913b54483b Mon Sep 17 00:00:00 2001
From: Geert Uytterhoeven <[email protected]>
Date: Fri, 29 Mar 2019 10:10:26 +0100
Subject: [PATCH 1/2] serial: sh-sci: Fix HSCIF RX sampling point adjustment

The calculation of the sampling point has min() and max() exchanged. Fix this by using the clamp() helper instead.

Fixes 63ba1e00f178a448 ("serial: sh-sci: Support for HSCIF RX sampling point adjustment")
---
 drivers/tty/serial/sh-sci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/sh-sci.c +1 -1
index eba08cd892e5..69896d586a29 100644
@@ -2446,7 +2446,7 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
 				 * last stop bit; we can increase the error
 				 * margin by shifting the sampling point.
 				 */
-				int shift = min(-8, max(7, deviation / 2));
+				int shift = clamp(deviation / 2, -8, 7);
 
 				hssrr |= (shift << HSCIF_SRHP_SHIFT) &
 					 HSCIF_SRHP_MASK;
Signature · 1 line
2.20.0