[U-Boot] [PATCH v2 0/9] dm: Introduce driver model for Atmel at91

This series adds driver model support for serial and GPIO for the Atmel AT91 series. The at91sam9260-based Snapper 9260 is converted over as an example.
For both drivers the old behaviour is retained, so driver model becomes optional, not mandatory.
This series is available at u-boot-dm/atmel-working.
Changes in v2: - Use a text base of 0x21f00000 - Remove unnecessary port number check - Use GPIO_PER_BANK instead of 32 in more places - Use PA/PB/PC instead of A/B/C for GPIO port names - Rename header to atmel_serial.h and use atmel instead of at91 throughout - Update platform data and header #include for the new driver name - Add new patch to add myself as maintainer for snapper9260 - Add a patch to implement baud rate changes
Simon Glass (9): dm: at91: Correct text base for snapper9260 dm: at91: Move snapper9260 to generic baord dm: at91: Add driver model support for atmel GPIO driver dm: at91: Add platform data for GPIO on at91sam9260-based boards dm: at91: Refactor serial driver slightly for driver model dm: at91: Add driver model support for the serial driver dm: at91: Convert snapper9260 to use driver model dm: at91: Add myself as maintainer for snapper9260 dm: serial: Support changing the baud rate
arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c | 14 ++ arch/arm/include/asm/arch-at91/at91sam9260.h | 4 +- arch/arm/include/asm/arch-at91/atmel_serial.h | 15 ++ arch/arm/include/asm/arch-at91/gpio.h | 6 + board/bluewater/snapper9260/MAINTAINERS | 2 +- board/bluewater/snapper9260/snapper9260.c | 18 +- drivers/gpio/at91_gpio.c | 241 +++++++++++++++++----- drivers/serial/atmel_usart.c | 116 ++++++++++- drivers/serial/serial-uclass.c | 67 ++++++ include/configs/snapper9260.h | 12 +- 10 files changed, 426 insertions(+), 69 deletions(-) create mode 100644 arch/arm/include/asm/arch-at91/atmel_serial.h

The value should be 0x21f00000. Fix it.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Use a text base of 0x21f00000
include/configs/snapper9260.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index 1ebee71..6909e99 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -15,7 +15,7 @@ #include <asm/hardware.h> #include <linux/sizes.h>
-#define CONFIG_SYS_TEXT_BASE 0x20000000 +#define CONFIG_SYS_TEXT_BASE 0x21f00000
/* ARM asynchronous clock */ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */

On 29 October 2014 at 20:08, Simon Glass sjg@chromium.org wrote:
The value should be 0x21f00000. Fix it.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Use a text base of 0x21f00000
include/configs/snapper9260.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied to u-boot-dm.

This works correctly, so switch it over before the deadline.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: None
include/configs/snapper9260.h | 1 + 1 file changed, 1 insertion(+)
diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index 6909e99..e2e623e 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -20,6 +20,7 @@ /* ARM asynchronous clock */ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */ #define CONFIG_SYS_AT91_SLOW_CLOCK 32768 +#define CONFIG_SYS_GENERIC_BOARD
/* CPU */ #define CONFIG_ARCH_CPU_INIT

On 29 October 2014 at 20:08, Simon Glass sjg@chromium.org wrote:
This works correctly, so switch it over before the deadline.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2: None
include/configs/snapper9260.h | 1 + 1 file changed, 1 insertion(+)
Applied to u-boot-dm.

Modify this driver to support driver model, with platform data required to determine the GPIOs that it controls.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Remove unnecessary port number check - Use GPIO_PER_BANK instead of 32 in more places
arch/arm/include/asm/arch-at91/gpio.h | 6 + drivers/gpio/at91_gpio.c | 241 ++++++++++++++++++++++++++-------- 2 files changed, 193 insertions(+), 54 deletions(-)
diff --git a/arch/arm/include/asm/arch-at91/gpio.h b/arch/arm/include/asm/arch-at91/gpio.h index 7121388..6d2a7b7 100644 --- a/arch/arm/include/asm/arch-at91/gpio.h +++ b/arch/arm/include/asm/arch-at91/gpio.h @@ -253,4 +253,10 @@ static inline unsigned at91_gpio_to_pin(unsigned gpio) return gpio % 32; }
+/* Platform data for each GPIO port */ +struct at91_port_platdata { + uint32_t base_addr; + const char *bank_name; +}; + #endif /* __ASM_ARCH_AT91_GPIO_H */ diff --git a/drivers/gpio/at91_gpio.c b/drivers/gpio/at91_gpio.c index 6517af1..6129c02 100644 --- a/drivers/gpio/at91_gpio.c +++ b/drivers/gpio/at91_gpio.c @@ -10,11 +10,14 @@
#include <config.h> #include <common.h> +#include <dm.h> #include <asm/io.h> #include <linux/sizes.h> +#include <asm/gpio.h> #include <asm/arch/hardware.h> #include <asm/arch/at91_pio.h> -#include <asm/arch/gpio.h> + +#define GPIO_PER_BANK 32
static struct at91_port *at91_pio_get_port(unsigned port) { @@ -39,19 +42,25 @@ static struct at91_port *at91_pio_get_port(unsigned port) } }
+static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset, + int use_pullup) +{ + u32 mask; + + mask = 1 << offset; + if (use_pullup) + writel(mask, &at91_port->puer); + else + writel(mask, &at91_port->pudr); + writel(mask, &at91_port->per); +} + int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) { struct at91_port *at91_port = at91_pio_get_port(port); - u32 mask;
- if (at91_port && (pin < 32)) { - mask = 1 << pin; - if (use_pullup) - writel(1 << pin, &at91_port->puer); - else - writel(1 << pin, &at91_port->pudr); - writel(mask, &at91_port->per); - } + if (at91_port && (pin < GPIO_PER_BANK)) + at91_set_port_pullup(at91_port, pin, use_pullup);
return 0; } @@ -64,7 +73,7 @@ int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->idr); at91_set_pio_pullup(port, pin, use_pullup); @@ -82,7 +91,7 @@ int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->idr); at91_set_pio_pullup(port, pin, use_pullup); @@ -108,7 +117,7 @@ int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->idr); at91_set_pio_pullup(port, pin, use_pullup); @@ -135,7 +144,7 @@ int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->idr); at91_set_pio_pullup(port, pin, use_pullup); @@ -157,7 +166,7 @@ int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->idr); at91_set_pio_pullup(port, pin, use_pullup); @@ -172,6 +181,29 @@ int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) } #endif
+#ifdef CONFIG_DM_GPIO +static bool at91_get_port_output(struct at91_port *at91_port, int offset) +{ + u32 mask, val; + + mask = 1 << offset; + val = readl(&at91_port->osr); + return val & mask; +} +#endif + +static void at91_set_port_input(struct at91_port *at91_port, int offset, + int use_pullup) +{ + u32 mask; + + mask = 1 << offset; + writel(mask, &at91_port->idr); + at91_set_port_pullup(at91_port, offset, use_pullup); + writel(mask, &at91_port->odr); + writel(mask, &at91_port->per); +} + /* * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and * configure it for an input. @@ -179,19 +211,29 @@ int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) { struct at91_port *at91_port = at91_pio_get_port(port); - u32 mask;
- if (at91_port && (pin < 32)) { - mask = 1 << pin; - writel(mask, &at91_port->idr); - at91_set_pio_pullup(port, pin, use_pullup); - writel(mask, &at91_port->odr); - writel(mask, &at91_port->per); - } + if (at91_port && (pin < GPIO_PER_BANK)) + at91_set_port_input(at91_port, pin, use_pullup);
return 0; }
+static void at91_set_port_output(struct at91_port *at91_port, int offset, + int value) +{ + u32 mask; + + mask = 1 << offset; + writel(mask, &at91_port->idr); + writel(mask, &at91_port->pudr); + if (value) + writel(mask, &at91_port->sodr); + else + writel(mask, &at91_port->codr); + writel(mask, &at91_port->oer); + writel(mask, &at91_port->per); +} + /* * mux the pin to the gpio controller (instead of "A" or "B" peripheral), * and configure it for an output. @@ -199,19 +241,9 @@ int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) int at91_set_pio_output(unsigned port, u32 pin, int value) { struct at91_port *at91_port = at91_pio_get_port(port); - u32 mask;
- if (at91_port && (port < ATMEL_PIO_PORTS) && (pin < 32)) { - mask = 1 << pin; - writel(mask, &at91_port->idr); - writel(mask, &at91_port->pudr); - if (value) - writel(mask, &at91_port->sodr); - else - writel(mask, &at91_port->codr); - writel(mask, &at91_port->oer); - writel(mask, &at91_port->per); - } + if (at91_port && (pin < GPIO_PER_BANK)) + at91_set_port_output(at91_port, pin, value);
return 0; } @@ -224,7 +256,7 @@ int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; if (is_on) { #if defined(CPU_HAS_PIO3) @@ -248,7 +280,7 @@ int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; if (is_on) { writel(mask, &at91_port->ifscer); @@ -271,7 +303,7 @@ int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(mask, &at91_port->pudr); if (is_on) @@ -291,7 +323,7 @@ int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; writel(readl(&at91_port->schmitt) | mask, &at91_port->schmitt); @@ -310,7 +342,7 @@ int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) struct at91_port *at91_port = at91_pio_get_port(port); u32 mask;
- if (at91_port && (pin < 32)) { + if (at91_port && (pin < GPIO_PER_BANK)) { mask = 1 << pin; if (is_on) writel(mask, &at91_port->mder); @@ -321,41 +353,54 @@ int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) return 0; }
+static void at91_set_port_value(struct at91_port *at91_port, int offset, + int value) +{ + u32 mask; + + mask = 1 << offset; + if (value) + writel(mask, &at91_port->sodr); + else + writel(mask, &at91_port->codr); +} + /* * assuming the pin is muxed as a gpio output, set its value. */ int at91_set_pio_value(unsigned port, unsigned pin, int value) { struct at91_port *at91_port = at91_pio_get_port(port); - u32 mask;
- if (at91_port && (pin < 32)) { - mask = 1 << pin; - if (value) - writel(mask, &at91_port->sodr); - else - writel(mask, &at91_port->codr); - } + if (at91_port && (pin < GPIO_PER_BANK)) + at91_set_port_value(at91_port, pin, value);
return 0; }
+static int at91_get_port_value(struct at91_port *at91_port, int offset) +{ + u32 pdsr = 0, mask; + + mask = 1 << offset; + pdsr = readl(&at91_port->pdsr) & mask; + + return pdsr != 0; +} /* * read the pin's value (works even if it's not muxed as a gpio). */ int at91_get_pio_value(unsigned port, unsigned pin) { struct at91_port *at91_port = at91_pio_get_port(port); - u32 pdsr = 0, mask;
- if (at91_port && (pin < 32)) { - mask = 1 << pin; - pdsr = readl(&at91_port->pdsr) & mask; - } + if (at91_port && (pin < GPIO_PER_BANK)) + return at91_get_port_value(at91_port, pin);
- return pdsr != 0; + return 0; }
+#ifndef CONFIG_DM_GPIO /* Common GPIO API */
int gpio_request(unsigned gpio, const char *label) @@ -395,3 +440,91 @@ int gpio_set_value(unsigned gpio, int value)
return 0; } +#endif + +#ifdef CONFIG_DM_GPIO + +struct at91_port_priv { + struct at91_port *regs; +}; + +/* set GPIO pin 'gpio' as an input */ +static int at91_gpio_direction_input(struct udevice *dev, unsigned offset) +{ + struct at91_port_priv *port = dev_get_platdata(dev); + + at91_set_port_input(port->regs, offset, 0); + + return 0; +} + +/* set GPIO pin 'gpio' as an output, with polarity 'value' */ +static int at91_gpio_direction_output(struct udevice *dev, unsigned offset, + int value) +{ + struct at91_port_priv *port = dev_get_platdata(dev); + + at91_set_port_output(port->regs, offset, value); + + return 0; +} + +/* read GPIO IN value of pin 'gpio' */ +static int at91_gpio_get_value(struct udevice *dev, unsigned offset) +{ + struct at91_port_priv *port = dev_get_platdata(dev); + + return at91_get_port_value(port->regs, offset); +} + +/* write GPIO OUT value to pin 'gpio' */ +static int at91_gpio_set_value(struct udevice *dev, unsigned offset, + int value) +{ + struct at91_port_priv *port = dev_get_platdata(dev); + + at91_set_port_value(port->regs, offset, value); + + return 0; +} + +static int at91_gpio_get_function(struct udevice *dev, unsigned offset) +{ + struct at91_port_priv *port = dev_get_platdata(dev); + + /* GPIOF_FUNC is not implemented yet */ + if (at91_get_port_output(port->regs, offset)) + return GPIOF_OUTPUT; + else + return GPIOF_INPUT; +} + +static const struct dm_gpio_ops gpio_at91_ops = { + .direction_input = at91_gpio_direction_input, + .direction_output = at91_gpio_direction_output, + .get_value = at91_gpio_get_value, + .set_value = at91_gpio_set_value, + .get_function = at91_gpio_get_function, +}; + +static int at91_gpio_probe(struct udevice *dev) +{ + struct at91_port_priv *port = dev_get_priv(dev); + struct at91_port_platdata *plat = dev_get_platdata(dev); + struct gpio_dev_priv *uc_priv = dev->uclass_priv; + + uc_priv->bank_name = plat->bank_name; + uc_priv->gpio_count = GPIO_PER_BANK; + port->regs = (struct at91_port *)plat->base_addr; + + return 0; +} + +U_BOOT_DRIVER(gpio_at91) = { + .name = "gpio_at91", + .id = UCLASS_GPIO, + .ops = &gpio_at91_ops, + .probe = at91_gpio_probe, + .priv_auto_alloc_size = sizeof(struct at91_port_priv), +}; +#endif

On 29 October 2014 at 20:08, Simon Glass sjg@chromium.org wrote:
Modify this driver to support driver model, with platform data required to determine the GPIOs that it controls.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Remove unnecessary port number check
- Use GPIO_PER_BANK instead of 32 in more places
arch/arm/include/asm/arch-at91/gpio.h | 6 + drivers/gpio/at91_gpio.c | 241 ++++++++++++++++++++++++++-------- 2 files changed, 193 insertions(+), 54 deletions(-)
Applied to u-boot-dm.

These boards all have the same GPIO arrangement, so add some common platform data that can be used by all boards. Remove the configs which are no longer required.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Use PA/PB/PC instead of A/B/C for GPIO port names
arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c | 14 ++++++++++++++ arch/arm/include/asm/arch-at91/at91sam9260.h | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c b/arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c index cae4abc..840edd5 100644 --- a/arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c +++ b/arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c @@ -7,6 +7,7 @@ */
#include <common.h> +#include <dm.h> #include <asm/io.h> #include <asm/arch/at91_common.h> #include <asm/arch/at91_pmc.h> @@ -207,3 +208,16 @@ void at91_mci_hw_init(void) #endif } #endif + +/* Platform data for the GPIOs */ +static const struct at91_port_platdata at91sam9260_plat[] = { + { ATMEL_BASE_PIOA, "PA" }, + { ATMEL_BASE_PIOB, "PB" }, + { ATMEL_BASE_PIOC, "PC" }, +}; + +U_BOOT_DEVICES(at91sam9260_gpios) = { + { "gpio_at91", &at91sam9260_plat[0] }, + { "gpio_at91", &at91sam9260_plat[1] }, + { "gpio_at91", &at91sam9260_plat[2] }, +}; diff --git a/arch/arm/include/asm/arch-at91/at91sam9260.h b/arch/arm/include/asm/arch-at91/at91sam9260.h index 2e902ee..4d46def 100644 --- a/arch/arm/include/asm/arch-at91/at91sam9260.h +++ b/arch/arm/include/asm/arch-at91/at91sam9260.h @@ -136,9 +136,11 @@ /* * Other misc defines */ +#ifndef CONFIG_DM_GPIO #define ATMEL_PIO_PORTS 3 /* these SoCs have 3 PIO */ -#define ATMEL_PMC_UHP AT91SAM926x_PMC_UHP #define ATMEL_BASE_PIO ATMEL_BASE_PIOA +#endif +#define ATMEL_PMC_UHP AT91SAM926x_PMC_UHP
/* * SoC specific defines

On 29 October 2014 at 20:08, Simon Glass sjg@chromium.org wrote:
These boards all have the same GPIO arrangement, so add some common platform data that can be used by all boards. Remove the configs which are no longer required.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Use PA/PB/PC instead of A/B/C for GPIO port names
arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c | 14 ++++++++++++++ arch/arm/include/asm/arch-at91/at91sam9260.h | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-)
Applied to u-boot-dm.

Before adding driver model support, split out a few of the functions so that they can be used by the driver model code.
Signed-off-by: Simon Glass sjg@chromium.org Acked-by: Andreas Bießmann andreas.devel@googlemail.com ---
Changes in v2: None
drivers/serial/atmel_usart.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/drivers/serial/atmel_usart.c b/drivers/serial/atmel_usart.c index 8f0e348..ce36e99 100644 --- a/drivers/serial/atmel_usart.c +++ b/drivers/serial/atmel_usart.c @@ -19,9 +19,9 @@
DECLARE_GLOBAL_DATA_PTR;
-static void atmel_serial_setbrg(void) +static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id, + int baudrate) { - atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; unsigned long divisor; unsigned long usart_hz;
@@ -30,15 +30,13 @@ static void atmel_serial_setbrg(void) * Baud Rate = -------------- * 16 * CD */ - usart_hz = get_usart_clk_rate(CONFIG_USART_ID); - divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate; + usart_hz = get_usart_clk_rate(id); + divisor = (usart_hz / 16 + baudrate / 2) / baudrate; writel(USART3_BF(CD, divisor), &usart->brgr); }
-static int atmel_serial_init(void) +static void atmel_serial_init_internal(atmel_usart3_t *usart) { - atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; - /* * Just in case: drain transmitter register * 1000us is enough for baudrate >= 9600 @@ -47,9 +45,10 @@ static int atmel_serial_init(void) __udelay(1000);
writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr); +}
- serial_setbrg(); - +static void atmel_serial_activate(atmel_usart3_t *usart) +{ writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) | USART3_BF(USCLKS, USART3_USCLKS_MCK) | USART3_BF(CHRL, USART3_CHRL_8) @@ -59,6 +58,21 @@ static int atmel_serial_init(void) writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr); /* 100us is enough for the new settings to be settled */ __udelay(100); +} + +static void atmel_serial_setbrg(void) +{ + atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE, + CONFIG_USART_ID, gd->baudrate); +} + +static int atmel_serial_init(void) +{ + atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; + + atmel_serial_init_internal(usart); + serial_setbrg(); + atmel_serial_activate(usart);
return 0; }

On 29 October 2014 at 20:08, Simon Glass sjg@chromium.org wrote:
Before adding driver model support, split out a few of the functions so that they can be used by the driver model code.
Signed-off-by: Simon Glass sjg@chromium.org Acked-by: Andreas Bießmann andreas.devel@googlemail.com
Changes in v2: None
drivers/serial/atmel_usart.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)
Applied to u-boot-dm.

Add driver model support while retaining the existing legacy code. This allows the driver to support boards that have converted to driver model as well as those that have not.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Rename header to atmel_serial.h and use atmel instead of at91 throughout
arch/arm/include/asm/arch-at91/atmel_serial.h | 15 +++++ drivers/serial/atmel_usart.c | 84 +++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 arch/arm/include/asm/arch-at91/atmel_serial.h
diff --git a/arch/arm/include/asm/arch-at91/atmel_serial.h b/arch/arm/include/asm/arch-at91/atmel_serial.h new file mode 100644 index 0000000..5bc094b --- /dev/null +++ b/arch/arm/include/asm/arch-at91/atmel_serial.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2014 Google, Inc + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef _ATMEL_SERIAL_H +#define _ATMEL_SERIAL_H + +/* Information about a serial port */ +struct atmel_serial_platdata { + uint32_t base_addr; +}; + +#endif diff --git a/drivers/serial/atmel_usart.c b/drivers/serial/atmel_usart.c index ce36e99..4fe992b 100644 --- a/drivers/serial/atmel_usart.c +++ b/drivers/serial/atmel_usart.c @@ -7,11 +7,16 @@ * SPDX-License-Identifier: GPL-2.0+ */ #include <common.h> +#include <dm.h> +#include <errno.h> #include <watchdog.h> #include <serial.h> #include <linux/compiler.h>
#include <asm/io.h> +#ifdef CONFIG_DM_SERIAL +#include <asm/arch/atmel_serial.h> +#endif #include <asm/arch/clk.h> #include <asm/arch/hardware.h>
@@ -60,6 +65,7 @@ static void atmel_serial_activate(atmel_usart3_t *usart) __udelay(100); }
+#ifndef CONFIG_DM_SERIAL static void atmel_serial_setbrg(void) { atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE, @@ -123,3 +129,81 @@ __weak struct serial_device *default_serial_console(void) { return &atmel_serial_drv; } +#endif + +#ifdef CONFIG_DM_SERIAL + +struct atmel_serial_priv { + atmel_usart3_t *usart; +}; + +int atmel_serial_setbrg(struct udevice *dev, int baudrate) +{ + struct atmel_serial_priv *priv = dev_get_priv(dev); + + atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate); + atmel_serial_activate(priv->usart); + + return 0; +} + +static int atmel_serial_getc(struct udevice *dev) +{ + struct atmel_serial_priv *priv = dev_get_priv(dev); + + if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY))) + return -EAGAIN; + + return readl(&priv->usart->rhr); +} + +static int atmel_serial_putc(struct udevice *dev, const char ch) +{ + struct atmel_serial_priv *priv = dev_get_priv(dev); + + if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY))) + return -EAGAIN; + + writel(ch, &priv->usart->thr); + + return 0; +} + +static int atmel_serial_pending(struct udevice *dev, bool input) +{ + struct atmel_serial_priv *priv = dev_get_priv(dev); + uint32_t csr = readl(&priv->usart->csr); + + if (input) + return csr & USART3_BIT(RXRDY) ? 1 : 0; + else + return csr & USART3_BIT(TXEMPTY) ? 0 : 1; +} + +static const struct dm_serial_ops atmel_serial_ops = { + .putc = atmel_serial_putc, + .pending = atmel_serial_pending, + .getc = atmel_serial_getc, + .setbrg = atmel_serial_setbrg, +}; + +static int atmel_serial_probe(struct udevice *dev) +{ + struct atmel_serial_platdata *plat = dev->platdata; + struct atmel_serial_priv *priv = dev_get_priv(dev); + + priv->usart = (atmel_usart3_t *)plat->base_addr; + atmel_serial_init_internal(priv->usart); + + return 0; +} + +U_BOOT_DRIVER(serial_atmel) = { + .name = "serial_atmel", + .id = UCLASS_SERIAL, + .probe = atmel_serial_probe, + .ops = &atmel_serial_ops, + .flags = DM_FLAG_PRE_RELOC, + .priv_auto_alloc_size = sizeof(struct atmel_serial_priv), +}; +#endif

On 29 October 2014 at 20:09, Simon Glass sjg@chromium.org wrote:
Add driver model support while retaining the existing legacy code. This allows the driver to support boards that have converted to driver model as well as those that have not.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Rename header to atmel_serial.h and use atmel instead of at91 throughout
arch/arm/include/asm/arch-at91/atmel_serial.h | 15 +++++ drivers/serial/atmel_usart.c | 84 +++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 arch/arm/include/asm/arch-at91/atmel_serial.h
Applied to u-boot-dm.

Convert this at91sam9260-based board to use driver model. This should serve as an example for other similar boards. Serial and GPIO are supported so far.
Signed-off-by: Simon Glass sjg@chromium.org Acked-by: Andreas Bießmann andreas.devel@googlemail.com ---
Changes in v2: - Update platform data and header #include for the new driver name
board/bluewater/snapper9260/snapper9260.c | 18 ++++++++++++++++-- include/configs/snapper9260.h | 9 ++++++++- 2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/board/bluewater/snapper9260/snapper9260.c b/board/bluewater/snapper9260/snapper9260.c index bfde129..95633b0 100644 --- a/board/bluewater/snapper9260/snapper9260.c +++ b/board/bluewater/snapper9260/snapper9260.c @@ -9,12 +9,15 @@ */
#include <common.h> +#include <dm.h> #include <asm/io.h> +#include <asm/gpio.h> #include <asm/arch/at91sam9260_matrix.h> #include <asm/arch/at91sam9_smc.h> #include <asm/arch/at91_common.h> #include <asm/arch/at91_pmc.h> #include <asm/arch/gpio.h> +#include <asm/arch/atmel_serial.h> #include <net.h> #include <netdev.h> #include <i2c.h> @@ -95,10 +98,12 @@ static void nand_hw_init(void) &smc->cs[3].mode);
/* Configure RDY/BSY */ - at91_set_gpio_input(CONFIG_SYS_NAND_READY_PIN, 1); + gpio_request(CONFIG_SYS_NAND_READY_PIN, "nand_rdy"); + gpio_direction_input(CONFIG_SYS_NAND_READY_PIN);
/* Enable NandFlash */ - at91_set_gpio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1); + gpio_request(CONFIG_SYS_NAND_ENABLE_PIN, "nand_ce"); + gpio_direction_output(CONFIG_SYS_NAND_ENABLE_PIN, 1); }
int board_init(void) @@ -140,3 +145,12 @@ int dram_init(void) void reset_phy(void) { } + +static struct atmel_serial_platdata at91sam9260_serial_plat = { + .base_addr = ATMEL_BASE_DBGU, +}; + +U_BOOT_DEVICE(at91sam9260_serial) = { + .name = "serial_atmel", + .platdata = &at91sam9260_serial_plat, +}; diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index e2e623e..942af2e 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -21,6 +21,11 @@ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */ #define CONFIG_SYS_AT91_SLOW_CLOCK 32768 #define CONFIG_SYS_GENERIC_BOARD +#define CONFIG_DM +#define CONFIG_CMD_DM +#define CONFIG_DM_GPIO +#define CONFIG_DM_SERIAL +#define CONFIG_SYS_MALLOC_F_LEN (1 << 10)
/* CPU */ #define CONFIG_ARCH_CPU_INIT @@ -85,8 +90,10 @@
/* UARTs/Serial console */ #define CONFIG_ATMEL_USART +#ifndef CONFIG_DM_SERIAL #define CONFIG_USART_BASE ATMEL_BASE_DBGU #define CONFIG_USART_ID ATMEL_ID_SYS +#endif #define CONFIG_BAUDRATE 115200 #define CONFIG_SYS_PROMPT "Snapper> "
@@ -160,7 +167,7 @@ #define CONFIG_CMD_DHCP #define CONFIG_CMD_FAT #define CONFIG_CMD_I2C -#undef CONFIG_CMD_GPIO +#define CONFIG_CMD_GPIO #define CONFIG_CMD_USB #define CONFIG_CMD_MII #define CONFIG_CMD_NAND

Hi Simon,
2014-10-30 4:09 GMT+09:00 Simon Glass sjg@chromium.org:
diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index e2e623e..942af2e 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -21,6 +21,11 @@ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */ #define CONFIG_SYS_AT91_SLOW_CLOCK 32768 #define CONFIG_SYS_GENERIC_BOARD +#define CONFIG_DM +#define CONFIG_CMD_DM +#define CONFIG_DM_GPIO +#define CONFIG_DM_SERIAL +#define CONFIG_SYS_MALLOC_F_LEN (1 << 10)
CONFIG_DM, CONFIG_DM_GPIO, CONFIG_DM_SERIAL are available in Kconfig. Could you add them into the defconfig, please?

Hi Masahiro,
On 1 November 2014 10:27, Masahiro YAMADA yamada.m@jp.panasonic.com wrote:
Hi Simon,
2014-10-30 4:09 GMT+09:00 Simon Glass sjg@chromium.org:
diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index e2e623e..942af2e 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -21,6 +21,11 @@ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */ #define CONFIG_SYS_AT91_SLOW_CLOCK 32768 #define CONFIG_SYS_GENERIC_BOARD +#define CONFIG_DM +#define CONFIG_CMD_DM +#define CONFIG_DM_GPIO +#define CONFIG_DM_SERIAL +#define CONFIG_SYS_MALLOC_F_LEN (1 << 10)
CONFIG_DM, CONFIG_DM_GPIO, CONFIG_DM_SERIAL are available in Kconfig. Could you add them into the defconfig, please?
Yes I need to do a full pass through all the boards I've added. Hopefully next week.
Regards, Simon

On 5 November 2014 at 03:54, Simon Glass sjg@chromium.org wrote:
Hi Masahiro,
On 1 November 2014 10:27, Masahiro YAMADA yamada.m@jp.panasonic.com wrote:
Hi Simon,
2014-10-30 4:09 GMT+09:00 Simon Glass sjg@chromium.org:
diff --git a/include/configs/snapper9260.h b/include/configs/snapper9260.h index e2e623e..942af2e 100644 --- a/include/configs/snapper9260.h +++ b/include/configs/snapper9260.h @@ -21,6 +21,11 @@ #define CONFIG_SYS_AT91_MAIN_CLOCK 18432000 /* External Crystal, in Hz */ #define CONFIG_SYS_AT91_SLOW_CLOCK 32768 #define CONFIG_SYS_GENERIC_BOARD +#define CONFIG_DM +#define CONFIG_CMD_DM +#define CONFIG_DM_GPIO +#define CONFIG_DM_SERIAL +#define CONFIG_SYS_MALLOC_F_LEN (1 << 10)
CONFIG_DM, CONFIG_DM_GPIO, CONFIG_DM_SERIAL are available in Kconfig. Could you add them into the defconfig, please?
Yes I need to do a full pass through all the boards I've added. Hopefully next week.
Applied to u-boot-dm.

The old maintainer has left, so take this over.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Add new patch to add myself as maintainer for snapper9260
board/bluewater/snapper9260/MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/board/bluewater/snapper9260/MAINTAINERS b/board/bluewater/snapper9260/MAINTAINERS index ff1e997..1f8f4d6 100644 --- a/board/bluewater/snapper9260/MAINTAINERS +++ b/board/bluewater/snapper9260/MAINTAINERS @@ -1,5 +1,5 @@ SNAPPER9260 BOARD -M: Ryan Mallon ryan@bluewatersys.com +M: Simon Glass sjg@chromium.org S: Maintained F: board/bluewater/snapper9260/ F: include/configs/snapper9260.h

On 29 October 2014 at 20:09, Simon Glass sjg@chromium.org wrote:
The old maintainer has left, so take this over.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Add new patch to add myself as maintainer for snapper9260
board/bluewater/snapper9260/MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied to u-boot-dm.

Implement this feature in the uclass so that the baudrate can be changed with 'setenv baudrate <rate>'.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v2: - Add a patch to implement baud rate changes
drivers/serial/serial-uclass.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c index 71f1a5c..632933f 100644 --- a/drivers/serial/serial-uclass.c +++ b/drivers/serial/serial-uclass.c @@ -6,6 +6,7 @@
#include <common.h> #include <dm.h> +#include <environment.h> #include <errno.h> #include <fdtdec.h> #include <os.h> @@ -22,6 +23,11 @@ DECLARE_GLOBAL_DATA_PTR; /* The currently-selected console serial device */ struct udevice *cur_dev __attribute__ ((section(".data")));
+/* + * Table with supported baudrates (defined in config_xyz.h) + */ +static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; + #ifndef CONFIG_SYS_MALLOC_F_LEN #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work" #endif @@ -177,6 +183,67 @@ int serial_stub_tstc(struct stdio_dev *sdev) return _serial_tstc(sdev->priv); }
+/** + * on_baudrate() - Update the actual baudrate when the env var changes + * + * This will check for a valid baudrate and only apply it if valid. + */ +static int on_baudrate(const char *name, const char *value, enum env_op op, + int flags) +{ + int i; + int baudrate; + + switch (op) { + case env_op_create: + case env_op_overwrite: + /* + * Switch to new baudrate if new baudrate is supported + */ + baudrate = simple_strtoul(value, NULL, 10); + + /* Not actually changing */ + if (gd->baudrate == baudrate) + return 0; + + for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { + if (baudrate == baudrate_table[i]) + break; + } + if (i == ARRAY_SIZE(baudrate_table)) { + if ((flags & H_FORCE) == 0) + printf("## Baudrate %d bps not supported\n", + baudrate); + return 1; + } + if ((flags & H_INTERACTIVE) != 0) { + printf("## Switch baudrate to %d bps and press ENTER ...\n", + baudrate); + udelay(50000); + } + + gd->baudrate = baudrate; + + serial_setbrg(); + + udelay(50000); + + if ((flags & H_INTERACTIVE) != 0) + while (1) { + if (getc() == '\r') + break; + } + + return 0; + case env_op_delete: + printf("## Baudrate may not be deleted\n"); + return 1; + default: + return 0; + } +} +U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); + static int serial_post_probe(struct udevice *dev) { struct stdio_dev sdev;

On 29 October 2014 at 20:09, Simon Glass sjg@chromium.org wrote:
Implement this feature in the uclass so that the baudrate can be changed with 'setenv baudrate <rate>'.
Signed-off-by: Simon Glass sjg@chromium.org
Changes in v2:
- Add a patch to implement baud rate changes
drivers/serial/serial-uclass.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
Applied to u-boot-dm.

Hi,
On 29 October 2014 13:08, Simon Glass sjg@chromium.org wrote:
This series adds driver model support for serial and GPIO for the Atmel AT91 series. The at91sam9260-based Snapper 9260 is converted over as an example.
For both drivers the old behaviour is retained, so driver model becomes optional, not mandatory.
This series is available at u-boot-dm/atmel-working.
Are there any comments on this version please? Otherwise I would like to pull it into u-boot-dm and start sorting out the Kconfig things (i.e. get rid of the CONFIG options for the 7-8 boards I have touched).
Changes in v2:
- Use a text base of 0x21f00000
- Remove unnecessary port number check
- Use GPIO_PER_BANK instead of 32 in more places
- Use PA/PB/PC instead of A/B/C for GPIO port names
- Rename header to atmel_serial.h and use atmel instead of at91 throughout
- Update platform data and header #include for the new driver name
- Add new patch to add myself as maintainer for snapper9260
- Add a patch to implement baud rate changes
Simon Glass (9): dm: at91: Correct text base for snapper9260 dm: at91: Move snapper9260 to generic baord dm: at91: Add driver model support for atmel GPIO driver dm: at91: Add platform data for GPIO on at91sam9260-based boards dm: at91: Refactor serial driver slightly for driver model dm: at91: Add driver model support for the serial driver dm: at91: Convert snapper9260 to use driver model dm: at91: Add myself as maintainer for snapper9260 dm: serial: Support changing the baud rate
arch/arm/cpu/arm926ejs/at91/at91sam9260_devices.c | 14 ++ arch/arm/include/asm/arch-at91/at91sam9260.h | 4 +- arch/arm/include/asm/arch-at91/atmel_serial.h | 15 ++ arch/arm/include/asm/arch-at91/gpio.h | 6 + board/bluewater/snapper9260/MAINTAINERS | 2 +- board/bluewater/snapper9260/snapper9260.c | 18 +- drivers/gpio/at91_gpio.c | 241 +++++++++++++++++----- drivers/serial/atmel_usart.c | 116 ++++++++++- drivers/serial/serial-uclass.c | 67 ++++++ include/configs/snapper9260.h | 12 +- 10 files changed, 426 insertions(+), 69 deletions(-) create mode 100644 arch/arm/include/asm/arch-at91/atmel_serial.h
-- 2.1.0.rc2.206.gedb03e5
Regards, Simon
participants (2)
-
Masahiro YAMADA
-
Simon Glass