Skip to contentSkip to conversation

RFC: Replace CONFIG_SYS_BAUDRATE_TABLE by board and UART driver rounding functions

RFC PATCH In review Pali Rohár 2 participants 5 messages Sep 25, 2021 – Apr 6, 2022

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 RFC: Replace CONFIG_SYS_BAUDRATE_TABLE by board and UART driver rounding functions

Add new functions which returns the nearest baudrate and use them instead of hardcoded and incomplete CONFIG_SYS_BAUDRATE_TABLE compile time option.

Add implementation of rounding function for serial_mvebu_a3700 driver and also for A3720 Espressobin board which has integrated pl2303 USB<->UART converter, which basically limits baudrates which can user set.

Completely remove CONFIG_SYS_BAUDRATE_TABLE defines from all A3720 boards as now with rounding functions it is not used anymore.

NOTE: This is just an example how to kill CONFIG_SYS_BAUDRATE_TABLE compile time definitions. I tested it that it works on A3720 Turris Mox board. I have not tested A3720 Espressobin board yet.

More discussion on this approach is required, so take this just as RFC change.

---
 board/Marvell/mvebu_armada-37xx/board.c | 28 ++++++++++
 drivers/serial/serial-uclass.c          | 71 +++++++++++++++++++++----
 drivers/serial/serial_mvebu_a3700.c     | 39 ++++++++++++++
 include/configs/mvebu_armada-37xx.h     |  8 ---
 include/configs/turris_mox.h            |  8 ---
 include/serial.h                        | 24 +++++++++
 6 files changed, 152 insertions(+), 26 deletions(-)
board/Marvell/mvebu_armada-37xx/board.c +28 -0
index fdc873b1952f..0cd9ea6a1515 100644
@@ -435,3 +435,31 @@ int ft_board_setup(void *blob, struct bd_info *bd)
 	return 0;
 }
 #endif
+
+int board_round_baudrate(int baudrate)
+{
+	/*
+	 * Espressobin has on-board pl2303 connected to A3720 UART.
+	 * So calculate the final real baudrate supported by pl2303.
+	 * Code from linux kernel function pl2303_encode_baud_rate_divisor()
+	 * Exact formula is: baudrate = 12M * 32 / (mantissa * 4^exponent)
+	 */
+	unsigned int baseline, mantissa, exponent;
+
+	baseline = 12000000 * 32;
+	mantissa = baseline / baudrate;
+	if (mantissa == 0)
+		mantissa = 1;
+	exponent = 0;
+	while (mantissa >= 512) {
+		if (exponent < 7) {
+			mantissa >>= 2;
+			exponent++;
+		} else {
+			mantissa = 511;
+			break;
+		}
+	}
+
+	return (baseline / mantissa) >> (exponent << 1);
+}
drivers/serial/serial-uclass.c +61 -10

99 lines not on this page · raw message

drivers/serial/serial_mvebu_a3700.c +39 -0
index 8dc4e5880e6b..d8bf3e97c486 100644
@@ -147,6 +147,44 @@ static int mvebu_serial_setbrg(struct udevice *dev, int baudrate)
 	return 0;
 }
 
+static int mvebu_serial_round_baudrate(struct udevice *dev, int baudrate)
+{
+	struct mvebu_plat *plat = dev_get_plat(dev);
+	u32 divider, d1, d2, m;
+
+	m = 16;
+	d1 = d2 = 1;
+	divider = DIV_ROUND_CLOSEST(plat->tbg_rate, baudrate * d1 * d2 * m);
+
+	if (divider < 1)
+		divider = 1;
+	else if (divider > 1023) {
+		d1 = 6;
+		divider = DIV_ROUND_CLOSEST(plat->tbg_rate,
+					    baudrate * d1 * d2 * m);
+		if (divider < 1)
+			divider = 1;
+		else if (divider > 1023) {
+			d2 = 6;
+			divider = DIV_ROUND_CLOSEST(plat->tbg_rate,
+						    baudrate * d1 * d2 * m);
+			if (divider < 1)
+				divider = 1;
+			else if (divider > 1023) {
+				m = 63;
+				divider = DIV_ROUND_CLOSEST(plat->tbg_rate,
+							    baudrate * d1 * d2 * m);
+				if (divider < 1)
+					divider = 1;
+				else if (divider > 1023)
+					divider = 1023;
+			}
+		}
+	}
+
+	return DIV_ROUND_CLOSEST(plat->tbg_rate, d1 * d2 * m * divider);
+}
+
 static int mvebu_serial_probe(struct udevice *dev)
 {
 	struct mvebu_plat *plat = dev_get_plat(dev);
@@ -335,6 +373,7 @@ static const struct dm_serial_ops mvebu_serial_ops = {
 	.pending = mvebu_serial_pending,
 	.getc = mvebu_serial_getc,
 	.setbrg = mvebu_serial_setbrg,
+	.round_baudrate = mvebu_serial_round_baudrate,
 };
 
 static const struct udevice_id mvebu_serial_ids[] = {
include/configs/mvebu_armada-37xx.h +0 -8
index c8c34d7d92dd..6f7279f2ae4a 100644
@@ -17,14 +17,6 @@
 
 #define CONFIG_SYS_BOOTM_LEN	SZ_64M /* Increase max gunzip size */
 
-#define CONFIG_SYS_BAUDRATE_TABLE	{ 300, 600, 1200, 1800, 2400, 4800, \
-					  9600, 19200, 38400, 57600, 115200, \
-					  230400, 460800, 500000, 576000, \
-					  921600, 1000000, 1152000, 1500000, \
-					  2000000, 2500000, 3000000, 3500000, \
-					  4000000, 4500000, 5000000, 5500000, \
-					  6000000 }
-
 /*
  * For booting Linux, the board info and command line data
  * have to be in the first 8 MB of memory, since this is
include/configs/turris_mox.h +0 -8
index 671283982356..03813613e193 100644
@@ -22,14 +22,6 @@
 
 /* auto boot */
 
-#define CONFIG_SYS_BAUDRATE_TABLE	{ 300, 600, 1200, 1800, 2400, 4800, \
-					  9600, 19200, 38400, 57600, 115200, \
-					  230400, 460800, 500000, 576000, \
-					  921600, 1000000, 1152000, 1500000, \
-					  2000000, 2500000, 3000000, 3500000, \
-					  4000000, 4500000, 5000000, 5500000, \
-					  6000000 }
-
 /*
  * For booting Linux, the board info and command line data
  * have to be in the first 8 MB of memory, since this is
include/serial.h +24 -0
index 6d1e62c6770c..5e8a7501b9a6 100644
@@ -176,6 +176,30 @@ struct dm_serial_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*setbrg)(struct udevice *dev, int baudrate);
+	/**
+	 * round_baudrate() - Return the nearest available baudrate
+	 *
+	 * Return the exact baudrate value which would be set by setbrg()
+	 * if is called with this @baudrate argument. So this function
+	 * should return the nearest available baudrate.
+	 *
+	 * This function must not change baudrate generator. Its purpose
+	 * is just to test if particular baudrate value is supported
+	 * and to calculate baudrate tolerance.
+	 *
+	 * Caller may specify number 0 to retrieve the smallest possible
+	 * supported baudrate and INT_MAX number to retrive the highest
+	 * possible supported baudrate.
+	 *
+	 * Returning zero value is same as if this function is not
+	 * implemented at all. Meaning that driver cannot predict what is
+	 * the real final baudrate value.
+	 *
+	 * @dev: Device pointer
+	 * @baudrate: Baudrate to round
+	 * @return real baudrate value (see above)
+	 */
+	int (*round_baudrate)(struct udevice *dev, int baudrate);
 	/**
 	 * getc() - Read a character and return it
 	 *
Signature · 1 line
2.20.1
#1 lore raw
Quoted 10 lines · Pali Rohár

Add new functions which returns the nearest baudrate and use them instead of hardcoded and incomplete CONFIG_SYS_BAUDRATE_TABLE compile time option.

Add implementation of rounding function for serial_mvebu_a3700 driver and also for A3720 Espressobin board which has integrated pl2303 USB<->UART converter, which basically limits baudrates which can user set.

Completely remove CONFIG_SYS_BAUDRATE_TABLE defines from all A3720 boards as now with rounding functions it is not used anymore.

Quoted 8 lines

NOTE: This is just an example how to kill CONFIG_SYS_BAUDRATE_TABLE compile time definitions. I tested it that it works on A3720 Turris Mox board. I have not tested A3720 Espressobin board yet.

More discussion on this approach is required, so take this just as RFC change.

Thanks for doing this. My first question is, is this really per-board? Or per SoC / UART chipset. I would hope for example that for plain old ns16550s this would be a generic function, perhaps with an optional board call-out for board design limitations. This does feel like a reasonable amount of code for platforms like this that were supporting what was the maximal rate table before.

Signature · 1 line
Tom
#2 lore raw
Quoted 16 lines · On Saturday 25 September 2021 09:51:08 Tom Rini wrote:
Quoted 14 lines · Pali Rohár

Add new functions which returns the nearest baudrate and use them instead of hardcoded and incomplete CONFIG_SYS_BAUDRATE_TABLE compile time option.

Add implementation of rounding function for serial_mvebu_a3700 driver and also for A3720 Espressobin board which has integrated pl2303 USB<->UART converter, which basically limits baudrates which can user set.

Completely remove CONFIG_SYS_BAUDRATE_TABLE defines from all A3720 boards as now with rounding functions it is not used anymore.

NOTE: This is just an example how to kill CONFIG_SYS_BAUDRATE_TABLE compile time definitions. I tested it that it works on A3720 Turris Mox board. I have not tested A3720 Espressobin board yet.

Quoted 6 lines
Quoted 4 lines

More discussion on this approach is required, so take this just as RFC change.

Thanks for doing this. My first question is, is this really per-board?

There is per UART chipset rounding / limitation and this should be implemented in UART driver (like I did it in serial_mvebu_a3700.c).

Then there can be per SoC limitation, e.g. SoC has unstable clock source for high baudrates. Meaning that UART chipset can support higher baudrates (as exposed by UART driver) but for some reasons they are not possible on SoC.

And then there can be really per-board limitations, like it is for Espressobin board. In most cases SoC has UART pins which people can connect to other UART device (e.g. to other SoC with UART or to some universal USB<->UART cables for "computers"). But Espressobin board has integrated pl2303 chip which is soldered directly to A3720 SoC UART pins. This pl2303 chip is USB<->UART converter and board itself has just micro-USB port (which is soldered to this pl2303). So at the end you can on this board use UART only via that integrated micro-USB port, which talks with standard USB protocol.

Therefore on this board you have two limitations for UART baudrates. First one is exposed by UART SoC chip (serial_mvebu_a3700.c) and second one is exposed by what that pl2303 supports. And this second limitation is board specific -- not A3720 SoC. Other boards with A3720 SoC, e.g. Turris Mox has exposed directly UART pins and you are free to connect any USB<->UART cable to these pins as you want.

And this design with soldered USB<->UART chip on the board is not rare. So U-Boot should have better support for it.

Note that lot of times clock source for these USB<->UART chips and SoC UART chip is different (because these USB based chips have its own clock source). So there are less common supported baudrates which can be used on the boards. And when talking about high speed baudrates (above 115200 bauds) then lot of times these "common supported baudrates" are just non-standard baudrate numbers (e.g. 3 125 000 bauds). Because you have to choose divisors both both chips to produce baudrate with only about 2-3% difference.

Quoted 8 lines

Or per SoC / UART chipset. I would hope for example that for plain old ns16550s this would be a generic function, perhaps with an optional board call-out for board design limitations. This does feel like a reasonable amount of code for platforms like this that were supporting what was the maximal rate table before.

Signature · 1 line
Tom
#3 lore raw

PING. Any progress in this direction? Any comments or objections in this approach?

Quoted 69 lines · On Saturday 25 September 2021 16:23:13 Pali Rohár wrote:
Quoted 16 lines · On Saturday 25 September 2021 09:51:08 Tom Rini wrote:
Quoted 14 lines · Pali Rohár

Add new functions which returns the nearest baudrate and use them instead of hardcoded and incomplete CONFIG_SYS_BAUDRATE_TABLE compile time option.

Add implementation of rounding function for serial_mvebu_a3700 driver and also for A3720 Espressobin board which has integrated pl2303 USB<->UART converter, which basically limits baudrates which can user set.

Completely remove CONFIG_SYS_BAUDRATE_TABLE defines from all A3720 boards as now with rounding functions it is not used anymore.

NOTE: This is just an example how to kill CONFIG_SYS_BAUDRATE_TABLE compile time definitions. I tested it that it works on A3720 Turris Mox board. I have not tested A3720 Espressobin board yet.

Quoted 6 lines
Quoted 4 lines

More discussion on this approach is required, so take this just as RFC change.

Thanks for doing this. My first question is, is this really per-board?

There is per UART chipset rounding / limitation and this should be implemented in UART driver (like I did it in serial_mvebu_a3700.c).

Then there can be per SoC limitation, e.g. SoC has unstable clock source for high baudrates. Meaning that UART chipset can support higher baudrates (as exposed by UART driver) but for some reasons they are not possible on SoC.

And then there can be really per-board limitations, like it is for Espressobin board. In most cases SoC has UART pins which people can connect to other UART device (e.g. to other SoC with UART or to some universal USB<->UART cables for "computers"). But Espressobin board has integrated pl2303 chip which is soldered directly to A3720 SoC UART pins. This pl2303 chip is USB<->UART converter and board itself has just micro-USB port (which is soldered to this pl2303). So at the end you can on this board use UART only via that integrated micro-USB port, which talks with standard USB protocol.

Therefore on this board you have two limitations for UART baudrates. First one is exposed by UART SoC chip (serial_mvebu_a3700.c) and second one is exposed by what that pl2303 supports. And this second limitation is board specific -- not A3720 SoC. Other boards with A3720 SoC, e.g. Turris Mox has exposed directly UART pins and you are free to connect any USB<->UART cable to these pins as you want.

And this design with soldered USB<->UART chip on the board is not rare. So U-Boot should have better support for it.

Note that lot of times clock source for these USB<->UART chips and SoC UART chip is different (because these USB based chips have its own clock source). So there are less common supported baudrates which can be used on the boards. And when talking about high speed baudrates (above 115200 bauds) then lot of times these "common supported baudrates" are just non-standard baudrate numbers (e.g. 3 125 000 bauds). Because you have to choose divisors both both chips to produce baudrate with only about 2-3% difference.

Quoted 8 lines

Or per SoC / UART chipset. I would hope for example that for plain old ns16550s this would be a generic function, perhaps with an optional board call-out for board design limitations. This does feel like a reasonable amount of code for platforms like this that were supporting what was the maximal rate table before.

Signature · 1 line
Tom
#4 lore raw
Quoted 1 line · Pali Rohár

PING. Any progress in this direction? Any comments or objections in this approach?

I keep meaning to reply to this. I think this is the right direction, I'd just like to see it implemented for some other UARTs as well, and this hasn't re-appeared at the top of my CONFIG migrate list yet.

Quoted 71 lines · On Saturday 25 September 2021 16:23:13 Pali Rohár wrote:
Quoted 69 lines · On Saturday 25 September 2021 09:51:08 Tom Rini wrote:
Quoted 16 lines · Pali Rohár
Quoted 15 lines

Add new functions which returns the nearest baudrate and use them instead of hardcoded and incomplete CONFIG_SYS_BAUDRATE_TABLE compile time option.

Add implementation of rounding function for serial_mvebu_a3700 driver and also for A3720 Espressobin board which has integrated pl2303 USB<->UART converter, which basically limits baudrates which can user set.

Completely remove CONFIG_SYS_BAUDRATE_TABLE defines from all A3720 boards as now with rounding functions it is not used anymore.

NOTE: This is just an example how to kill CONFIG_SYS_BAUDRATE_TABLE compile time definitions. I tested it that it works on A3720 Turris Mox board. I have not tested A3720 Espressobin board yet.

Quoted 6 lines
Quoted 4 lines

More discussion on this approach is required, so take this just as RFC change.

Thanks for doing this. My first question is, is this really per-board?

There is per UART chipset rounding / limitation and this should be implemented in UART driver (like I did it in serial_mvebu_a3700.c).

Then there can be per SoC limitation, e.g. SoC has unstable clock source for high baudrates. Meaning that UART chipset can support higher baudrates (as exposed by UART driver) but for some reasons they are not possible on SoC.

And then there can be really per-board limitations, like it is for Espressobin board. In most cases SoC has UART pins which people can connect to other UART device (e.g. to other SoC with UART or to some universal USB<->UART cables for "computers"). But Espressobin board has integrated pl2303 chip which is soldered directly to A3720 SoC UART pins. This pl2303 chip is USB<->UART converter and board itself has just micro-USB port (which is soldered to this pl2303). So at the end you can on this board use UART only via that integrated micro-USB port, which talks with standard USB protocol.

Therefore on this board you have two limitations for UART baudrates. First one is exposed by UART SoC chip (serial_mvebu_a3700.c) and second one is exposed by what that pl2303 supports. And this second limitation is board specific -- not A3720 SoC. Other boards with A3720 SoC, e.g. Turris Mox has exposed directly UART pins and you are free to connect any USB<->UART cable to these pins as you want.

And this design with soldered USB<->UART chip on the board is not rare. So U-Boot should have better support for it.

Note that lot of times clock source for these USB<->UART chips and SoC UART chip is different (because these USB based chips have its own clock source). So there are less common supported baudrates which can be used on the boards. And when talking about high speed baudrates (above 115200 bauds) then lot of times these "common supported baudrates" are just non-standard baudrate numbers (e.g. 3 125 000 bauds). Because you have to choose divisors both both chips to produce baudrate with only about 2-3% difference.

Quoted 8 lines

Or per SoC / UART chipset. I would hope for example that for plain old ns16550s this would be a generic function, perhaps with an optional board call-out for board design limitations. This does feel like a reasonable amount of code for platforms like this that were supporting what was the maximal rate table before.

Signature · 1 line
Tom
Signature · 1 line
Tom