[U-Boot] [PATCH v3 0/4] Replace serial setparity by setconfig

This series : - Replace setparity ops by more complete setconfig in serial uclass - Replace setparity by setconfig in STM32 serial driver _ Adds test for serial setconfig
Changes in v3: - Update SERIAL_PAR_MASK definition - Don't remove setparity ops declaration in patch 1 to not break bissectability - Replace u8 by uint for parity/bits/stop in stm32_serial_setconfig() - Add patch to remove obsolete setparity ops - Update serial test when wrong serial options are specified - Add SERIAL_CONFIG(par, bits, stop) macro to create serial mask
Changes in v2: - Update SERIAL_BITS_MASK and SERIAL_STOP_MASK - Update serial_setconfig prototype - Update stm32_serial_setconfig prototype - Add sandbox serial test
Patrice Chotard (4): dm: serial: Replace setparity by setconfig serial: stm32: Replace setparity by setconfig dm: serial: Remove setparity ops sandbox: Add serial test
drivers/serial/sandbox.c | 14 +++++++++++ drivers/serial/serial-uclass.c | 16 +++++++++++++ drivers/serial/serial_stm32.c | 21 +++++++++++----- include/common.h | 1 + include/serial.h | 48 ++++++++++++++++++++++++++++++++++--- test/dm/Makefile | 1 + test/dm/serial.c | 54 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 test/dm/serial.c

Replace setparity by more generic setconfig ops to allow uart parity, bits word length and stop bits number change.
Adds SERIAL_GET_PARITY/BITS/STOP macros.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com ---
Changes in v3: - Update SERIAL_PAR_MASK definition - Don't remove setparity ops declaration in patch 1 to not break bissectability
Changes in v2: - Update SERIAL_BITS_MASK and SERIAL_STOP_MASK - Update serial_setconfig prototype
drivers/serial/serial-uclass.c | 16 ++++++++++++++ include/serial.h | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c index 321d23ee93bf..a7556c9b7bc3 100644 --- a/drivers/serial/serial-uclass.c +++ b/drivers/serial/serial-uclass.c @@ -287,6 +287,20 @@ void serial_setbrg(void) ops->setbrg(gd->cur_serial_dev, gd->baudrate); }
+int serial_setconfig(uint config) +{ + struct dm_serial_ops *ops; + + if (!gd->cur_serial_dev) + return 0; + + ops = serial_get_ops(gd->cur_serial_dev); + if (ops->setconfig) + return ops->setconfig(gd->cur_serial_dev, config); + + return 0; +} + void serial_stdio_init(void) { } @@ -398,6 +412,8 @@ static int serial_post_probe(struct udevice *dev) ops->pending += gd->reloc_off; if (ops->clear) ops->clear += gd->reloc_off; + if (ops->setconfig) + ops->setconfig += gd->reloc_off; #if CONFIG_POST & CONFIG_SYS_POST_UART if (ops->loop) ops->loop += gd->reloc_off diff --git a/include/serial.h b/include/serial.h index b9ef6d91c9c5..ed9c70af74ed 100644 --- a/include/serial.h +++ b/include/serial.h @@ -73,6 +73,39 @@ enum serial_par { SERIAL_PAR_EVEN };
+#define SERIAL_PAR_SHIFT 0 +#define SERIAL_PAR_MASK (0x03 << SERIAL_PAR_SHIFT) +#define SERIAL_GET_PARITY(config) \ + ((config & SERIAL_PAR_MASK) >> SERIAL_PAR_SHIFT) + +enum serial_bits { + SERIAL_5_BITS, + SERIAL_6_BITS, + SERIAL_7_BITS, + SERIAL_8_BITS +}; + +#define SERIAL_BITS_SHIFT 2 +#define SERIAL_BITS_MASK (0x3 << SERIAL_BITS_SHIFT) +#define SERIAL_GET_BITS(config) \ + ((config & SERIAL_BITS_MASK) >> SERIAL_BITS_SHIFT) + +enum serial_stop { + SERIAL_HALF_STOP, /* 0.5 stop bit */ + SERIAL_ONE_STOP, /* 1 stop bit */ + SERIAL_ONE_HALF_STOP, /* 1.5 stop bit */ + SERIAL_TWO_STOP /* 2 stop bit */ +}; + +#define SERIAL_STOP_SHIFT 4 +#define SERIAL_STOP_MASK (0x3 << SERIAL_STOP_SHIFT) +#define SERIAL_GET_STOP(config) \ + ((config & SERIAL_STOP_MASK) >> SERIAL_STOP_SHIFT) + +#define SERIAL_DEFAULT_CONFIG SERIAL_PAR_NONE << SERIAL_PAR_SHIFT | \ + SERIAL_8_BITS << SERIAL_BITS_SHIFT | \ + SERIAL_ONE_STOP << SERIAL_STOP_SHIFT + /** * struct struct dm_serial_ops - Driver model serial operations * @@ -159,6 +192,20 @@ struct dm_serial_ops { * @return 0 if OK, -ve on error */ int (*setparity)(struct udevice *dev, enum serial_par parity); + + /** + * setconfig() - Set up the uart configuration + * (parity, 5/6/7/8 bits word length, stop bits) + * + * Set up a new config for this device. + * + * @dev: Device pointer + * @parity: parity to use + * @bits: bits number to use + * @stop: stop bits number to use + * @return 0 if OK, -ve on error + */ + int (*setconfig)(struct udevice *dev, uint serial_config); };
/**

On 3 August 2018 at 07:07, Patrice Chotard patrice.chotard@st.com wrote:
Replace setparity by more generic setconfig ops to allow uart parity, bits word length and stop bits number change.
Adds SERIAL_GET_PARITY/BITS/STOP macros.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com
Changes in v3:
- Update SERIAL_PAR_MASK definition
- Don't remove setparity ops declaration in patch 1 to not break bissectability
Changes in v2:
- Update SERIAL_BITS_MASK and SERIAL_STOP_MASK
- Update serial_setconfig prototype
drivers/serial/serial-uclass.c | 16 ++++++++++++++ include/serial.h | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
Reviewed-by: Simon Glass sjg@chromium.org

On Fri, Aug 03, 2018 at 03:07:38PM +0200, Patrice Chotard wrote:
Replace setparity by more generic setconfig ops to allow uart parity, bits word length and stop bits number change.
Adds SERIAL_GET_PARITY/BITS/STOP macros.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!

Replace stm32_serial_setparity by stm32_serial_setconfig which allows to set serial bits number, parity and stop bits number. Only parity setting is implemented.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com ---
Changes in v3: - Replace u8 by uint for parity/bits/stop in stm32_serial_setconfig()
Changes in v2: - Update stm32_serial_setconfig prototype
drivers/serial/serial_stm32.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/drivers/serial/serial_stm32.c b/drivers/serial/serial_stm32.c index f26234549c3e..66e02d5689d0 100644 --- a/drivers/serial/serial_stm32.c +++ b/drivers/serial/serial_stm32.c @@ -47,20 +47,28 @@ static int stm32_serial_setbrg(struct udevice *dev, int baudrate) return 0; }
-static int stm32_serial_setparity(struct udevice *dev, enum serial_par parity) +static int stm32_serial_setconfig(struct udevice *dev, uint serial_config) { struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); bool stm32f4 = plat->uart_info->stm32f4; u8 uart_enable_bit = plat->uart_info->uart_enable_bit; u32 cr1 = plat->base + CR1_OFFSET(stm32f4); u32 config = 0; - - if (stm32f4) - return -EINVAL; /* not supported in driver*/ + uint parity = SERIAL_GET_PARITY(serial_config); + uint bits = SERIAL_GET_BITS(serial_config); + uint stop = SERIAL_GET_STOP(serial_config); + + /* + * only parity config is implemented, check if other serial settings + * are the default one. + * (STM32F4 serial IP didn't support parity setting) + */ + if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4) + return -ENOTSUPP; /* not supported in driver*/
clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit)); /* update usart configuration (uart need to be disable) - * PCE: parity check control + * PCE: parity check enable * PS : '0' : Even / '1' : Odd * M[1:0] = '00' : 8 Data bits * M[1:0] = '01' : 9 Data bits with parity @@ -77,6 +85,7 @@ static int stm32_serial_setparity(struct udevice *dev, enum serial_par parity) config = USART_CR1_PCE | USART_CR1_M0; break; } + clrsetbits_le32(cr1, USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 | USART_CR1_M0, @@ -210,7 +219,7 @@ static const struct dm_serial_ops stm32_serial_ops = { .pending = stm32_serial_pending, .getc = stm32_serial_getc, .setbrg = stm32_serial_setbrg, - .setparity = stm32_serial_setparity + .setconfig = stm32_serial_setconfig };
U_BOOT_DRIVER(serial_stm32) = {

On 3 August 2018 at 07:07, Patrice Chotard patrice.chotard@st.com wrote:
Replace stm32_serial_setparity by stm32_serial_setconfig which allows to set serial bits number, parity and stop bits number. Only parity setting is implemented.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com
Changes in v3:
- Replace u8 by uint for parity/bits/stop in stm32_serial_setconfig()
Changes in v2:
- Update stm32_serial_setconfig prototype
drivers/serial/serial_stm32.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
Reviewed-by: Simon Glass sjg@chromium.org

On Fri, Aug 03, 2018 at 03:07:39PM +0200, Patrice Chotard wrote:
Replace stm32_serial_setparity by stm32_serial_setconfig which allows to set serial bits number, parity and stop bits number. Only parity setting is implemented.
Signed-off-by: Patrick Delaunay patrick.delaunay@st.com Signed-off-by: Patrice Chotard patrice.chotard@st.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!

setparity users has been updated to use new setconfig ops, so we can safely remove setparity ops
Signed-off-by: Patrice Chotard patrice.chotard@st.com ---
Changes in v3: - Add patch to remove obsolete setparity ops
Changes in v2: None
include/serial.h | 10 ---------- 1 file changed, 10 deletions(-)
diff --git a/include/serial.h b/include/serial.h index ed9c70af74ed..9ae4b3879085 100644 --- a/include/serial.h +++ b/include/serial.h @@ -182,16 +182,6 @@ struct dm_serial_ops { */ int (*loop)(struct udevice *dev, int on); #endif - /** - * setparity() - Set up the parity - * - * Set up a new parity for this device. - * - * @dev: Device pointer - * @parity: parity to use - * @return 0 if OK, -ve on error - */ - int (*setparity)(struct udevice *dev, enum serial_par parity);
/** * setconfig() - Set up the uart configuration

On 3 August 2018 at 07:07, Patrice Chotard patrice.chotard@st.com wrote:
setparity users has been updated to use new setconfig ops, so we can safely remove setparity ops
Signed-off-by: Patrice Chotard patrice.chotard@st.com
Changes in v3:
- Add patch to remove obsolete setparity ops
Changes in v2: None
include/serial.h | 10 ---------- 1 file changed, 10 deletions(-)
Reviewed-by: Simon Glass sjg@chromium.org

On Fri, Aug 03, 2018 at 03:07:40PM +0200, Patrice Chotard wrote:
setparity users has been updated to use new setconfig ops, so we can safely remove setparity ops
Signed-off-by: Patrice Chotard patrice.chotard@st.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!

Signed-off-by: Patrice Chotard patrice.chotard@st.com ---
Changes in v3: - Update serial test when wrong serial options are specified - Add SERIAL_CONFIG(par, bits, stop) macro to create serial mask
Changes in v2: - Add sandbox serial test
drivers/serial/sandbox.c | 14 +++++++++++++ include/common.h | 1 + include/serial.h | 5 +++++ test/dm/Makefile | 1 + test/dm/serial.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 test/dm/serial.c
diff --git a/drivers/serial/sandbox.c b/drivers/serial/sandbox.c index a60dabe58835..94b4fdfb1714 100644 --- a/drivers/serial/sandbox.c +++ b/drivers/serial/sandbox.c @@ -143,6 +143,19 @@ static int sandbox_serial_getc(struct udevice *dev) return result; }
+static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config) +{ + u8 parity = SERIAL_GET_PARITY(serial_config); + u8 bits = SERIAL_GET_BITS(serial_config); + u8 stop = SERIAL_GET_STOP(serial_config); + + if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || + parity != SERIAL_PAR_NONE) + return -ENOTSUPP; /* not supported in driver*/ + + return 0; +} + static const char * const ansi_colour[] = { "black", "red", "green", "yellow", "blue", "megenta", "cyan", "white", @@ -173,6 +186,7 @@ static const struct dm_serial_ops sandbox_serial_ops = { .putc = sandbox_serial_putc, .pending = sandbox_serial_pending, .getc = sandbox_serial_getc, + .setconfig = sandbox_serial_setconfig, };
static const struct udevice_id sandbox_serial_ids[] = { diff --git a/include/common.h b/include/common.h index 940161f1758b..5c952af5e319 100644 --- a/include/common.h +++ b/include/common.h @@ -359,6 +359,7 @@ void serial_putc_raw(const char); void serial_puts (const char *); int serial_getc (void); int serial_tstc (void); +int serial_setconfig(uint config);
/* $(CPU)/speed.c */ int get_clocks (void); diff --git a/include/serial.h b/include/serial.h index 9ae4b3879085..79f512fc98f3 100644 --- a/include/serial.h +++ b/include/serial.h @@ -102,6 +102,11 @@ enum serial_stop { #define SERIAL_GET_STOP(config) \ ((config & SERIAL_STOP_MASK) >> SERIAL_STOP_SHIFT)
+#define SERIAL_CONFIG(par, bits, stop) \ + (par << SERIAL_PAR_SHIFT | \ + bits << SERIAL_BITS_SHIFT | \ + stop << SERIAL_STOP_SHIFT) + #define SERIAL_DEFAULT_CONFIG SERIAL_PAR_NONE << SERIAL_PAR_SHIFT | \ SERIAL_8_BITS << SERIAL_BITS_SHIFT | \ SERIAL_ONE_STOP << SERIAL_STOP_SHIFT diff --git a/test/dm/Makefile b/test/dm/Makefile index d2ed96c61533..97517c7f825e 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -44,4 +44,5 @@ obj-$(CONFIG_DM_VIDEO) += video.o obj-$(CONFIG_ADC) += adc.o obj-$(CONFIG_SPMI) += spmi.o obj-$(CONFIG_WDT) += wdt.o +obj-$(CONFIG_DM_SERIAL) += serial.o endif diff --git a/test/dm/serial.c b/test/dm/serial.c new file mode 100644 index 000000000000..e1636868d7fe --- /dev/null +++ b/test/dm/serial.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2018, STMicroelectronics + */ + +#include <common.h> +#include <serial.h> +#include <dm.h> +#include <dm/test.h> +#include <test/ut.h> + +static int dm_test_serial(struct unit_test_state *uts) +{ + struct udevice *dev_serial; + + ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial", + &dev_serial)); + + ut_assertok(serial_tstc()); + /* + * test with default config which is the only one supported by + * sandbox_serial driver + */ + ut_assertok(serial_setconfig(SERIAL_DEFAULT_CONFIG)); + /* + * test with a serial config which is not supported by + * sandbox_serial driver: test with wrong parity + */ + ut_asserteq(-ENOTSUPP, + setconfig(SERIAL_CONFIG(SERIAL_PAR_ODD, + SERIAL_8_BITS, + SERIAL_ONE_STOP))); + /* + * test with a serial config which is not supported by + * sandbox_serial driver: test with wrong bits number + */ + ut_asserteq(-ENOTSUPP, + serial_setconfig(SERIAL_CONFIG(SERIAL_PAR_NONE, + SERIAL_6_BITS, + SERIAL_ONE_STOP))); + + /* + * test with a serial config which is not supported by + * sandbox_serial driver: test with wrong stop bits number + */ + ut_asserteq(-ENOTSUPP, + serial_setconfig(SERIAL_CONFIG(SERIAL_PAR_NONE, + SERIAL_8_BITS, + SERIAL_TWO_STOP))); + + return 0; +} + +DM_TEST(dm_test_serial, DM_TESTF_SCAN_FDT);

On 3 August 2018 at 07:07, Patrice Chotard patrice.chotard@st.com wrote:
Signed-off-by: Patrice Chotard patrice.chotard@st.com
Changes in v3:
- Update serial test when wrong serial options are specified
- Add SERIAL_CONFIG(par, bits, stop) macro to create serial mask
Changes in v2:
- Add sandbox serial test
drivers/serial/sandbox.c | 14 +++++++++++++ include/common.h | 1 + include/serial.h | 5 +++++ test/dm/Makefile | 1 + test/dm/serial.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 test/dm/serial.c
Reviewed-by: Simon Glass sjg@chromium.org

On Fri, Aug 03, 2018 at 03:07:41PM +0200, Patrice Chotard wrote:
Signed-off-by: Patrice Chotard patrice.chotard@st.com Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!
participants (3)
-
Patrice Chotard
-
Simon Glass
-
Tom Rini