[PATCH v3 2/2] bsp/raspberrypi: Mini UART driver

Alan Cudmore alan.cudmore at gmail.com
Sun Feb 16 17:35:01 UTC 2020


Awesome! The Pi Zero W is a bonus too! I think we can run RTEMS on all
models except the Pi 4 now.

On Sun, Feb 16, 2020 at 12:30 PM G. S. Niteesh <gsnb.gn at gmail.com> wrote:
>
> Hii Alan,
>
> I tested it on my Raspberry Pi 3 model b v1.2 and it works.
> U-Boot is not required anymore :)
>
> Thanks,
> Niteesh.
>
> On Sun, Feb 16, 2020 at 10:51 PM Alan Cudmore <alan.cudmore at gmail.com> wrote:
>>
>> I'm definitely using the kernel_address, but letting the firmware
>> decide what image to boot. For me kernel.img is the single core ARM
>> images. kernel7.img is used for the raspberry pi 2 and 3. With these
>> names, I can have both images on the same SD card to test in all of my
>> Pis.
>>
>>
>> my config.txt:
>> enable_uart=1
>> kernel_address=0x200000
>>
>>
>> On Sun, Feb 16, 2020 at 11:44 AM Christian Mauderer <list at c-mauderer.de> wrote:
>> >
>> > On 16/02/2020 17:31, G. S. Niteesh wrote:
>> > > Can you please share your config.txt for Pi3.
>> >
>> > +1
>> >
>> > It's interesting that it seems to work without an adapted config.txt. It
>> > should be at least necessary to define the start address.
>> >
>> > >
>> > > Thanks,
>> > > Niteesh
>> > >
>> > > On Sun, Feb 16, 2020 at 9:58 PM Alan Cudmore <alan.cudmore at gmail.com
>> > > <mailto:alan.cudmore at gmail.com>> wrote:
>> > >
>> > >     I tried running my raspberrypi BSP image that worked on the Pi Zero
>> > >     and W, but it did not work on the Pi 3.
>> > >
>> > >     I built the raspberrypi2 BSP, used objcopy to copy the binary to my sd
>> > >     card as kernel7.img, and it works on my Pi 3 and Pi3 A+ ( without
>> > >     ethernet) . Note that my config.txt file does not specify the kernel
>> > >     image, so the firmware defaults to kernel.img for the single core
>> > >     models and kernel7.img for the 2 and 3.
>> > >
>> > >     I probably wont get a chance to try u-boot until later today or
>> > >     tomorrow.
>> > >
>> > >     But these patches are great for enabling the Pi Zero W and Pi 3 models!
>> > >     Thanks,
>> > >     Alan
>> > >
>> > >     On Sun, Feb 16, 2020 at 10:58 AM G. S. Niteesh <gsnb.gn at gmail.com
>> > >     <mailto:gsnb.gn at gmail.com>> wrote:
>> > >     >
>> > >     > On Sun, Feb 16, 2020 at 8:37 PM Alan Cudmore
>> > >     <alan.cudmore at gmail.com <mailto:alan.cudmore at gmail.com>> wrote:
>> > >     >>
>> > >     >> Hi Niteesh,
>> > >     >> I can try this patch on my Pi 3. Which BSP should I use? Is there any
>> > >     >> special setup or BSP configuration needed?
>> > >     >> Thanks,
>> > >     >> Alan
>> > >     >
>> > >     > Hi Alan,
>> > >     > First of all, thank you for taking this initiative.
>> > >     > I have tried using the default bootloader in Pi3 to load RTEMS but
>> > >     it doesn't work,
>> > >     > Can you try it?
>> > >     > The currently use U-Boot in 32bit mode as the bootloader.
>> > >     > You can use the below instructions to build U-Boot in 32bit mode
>> > >     >
>> > >     > git clone https://github.com/u-boot/u-boot.git u-boot
>> > >     > cd u-boot
>> > >     > git revert 995eab8b5b580b67394312b1621c60a71042cd18
>> > >     > make CROSS_COMPILE=arm-linux-gnueabi- rpi_3_32b_defconfig
>> > >     > make CROSS_COMPILE=arm-linux-gnueabi-
>> > >     >
>> > >     > This one uses an old version of U-Boot and I don't know if there is
>> > >     > a new way to build in 32bit mode.
>> > >     >
>> > >     > To build the U-Boot image of RTEMS you can use the below instructions
>> > >     >
>> > >     > arm-rtems5-objcopy -Obinary hello.exe kernel.img
>> > >     > mkimage -A arm -O linux -T kernel -a 0x200000 -e 0x200080 -d
>> > >     kernel.img -C none rtems.img
>> > >     >
>> > >     > To load and run in U-Boot
>> > >     >
>> > >     > fatload mmc 0 0x200000 rtems.img
>> > >     > fatload mmc 0 0x1000 bcm2710-rpi-3-b.dtb
>> > >     > bootm 0x200000 - 0x1000
>> > >     >
>> > >     > And to make your U-Boot build process simpler I have uploaded my
>> > >     u-boot.bin
>> > >     > to my github, please use it :)
>> > >     > https://github.com/gs-niteesh/rpi3_RTEMS
>> > >     >
>> > >     > And sorry for the delay, I tested all these instructions again
>> > >     before sending them.
>> > >     >
>> > >     > Thanks
>> > >     > Niteesh.
>> > >     >
>> > >     >   On Sun, Feb 16, 2020 at 2:35 AM G. S. Niteesh <gsnb.gn at gmail.com
>> > >     <mailto:gsnb.gn at gmail.com>> wrote:
>> > >     >>
>> > >     >> >
>> > >     >> > Hii,
>> > >     >> > This is a reminder message since this patch has been unnoticed
>> > >     for a while.
>> > >     >> > I have tested it on Raspberry Pi 3 and it works fine. There is
>> > >     also another patch
>> > >     >> >
>> > >     https://lists.rtems.org/pipermail/devel/2020-February/057194.html,
>> > >     please
>> > >     >> > do have a look at these.
>> > >     >> >
>> > >     >> > Thank you.
>> > >     >> > Niteesh
>> > >     >> > On Mon, Feb 10, 2020 at 12:52 AM G S Niteesh <gsnb.gn at gmail.com
>> > >     <mailto:gsnb.gn at gmail.com>> wrote:
>> > >     >> >>
>> > >     >> >> This patch adds driver for Mini UART present in Raspberry Pi 3
>> > >     >> >> and above, this UART is currently used as the primary UART in
>> > >     >> >> these models.
>> > >     >> >> The Mini UART is similar to ns16550, this driver is built
>> > >     >> >> upon libchip/ns16550.
>> > >     >> >> ---
>> > >     >> >>  bsps/arm/raspberrypi/console/console-config.c | 118
>> > >     ++++++++++++++++--
>> > >     >> >>  bsps/arm/raspberrypi/include/bsp/usart.h      |   1 +
>> > >     >> >>  2 files changed, 110 insertions(+), 9 deletions(-)
>> > >     >> >>
>> > >     >> >> diff --git a/bsps/arm/raspberrypi/console/console-config.c
>> > >     b/bsps/arm/raspberrypi/console/console-config.c
>> > >     >> >> index 48c4c6a3ec..62196786dd 100644
>> > >     >> >> --- a/bsps/arm/raspberrypi/console/console-config.c
>> > >     >> >> +++ b/bsps/arm/raspberrypi/console/console-config.c
>> > >     >> >> @@ -24,6 +24,7 @@
>> > >     >> >>
>> > >     >> >>  #include <libchip/serial.h>
>> > >     >> >>  #include <libfdt.h>
>> > >     >> >> +#include <libchip/ns16550.h>
>> > >     >> >>
>> > >     >> >>  #include <bspopts.h>
>> > >     >> >>  #include <bsp/usart.h>
>> > >     >> >> @@ -34,35 +35,103 @@
>> > >     >> >>  #include <bsp/console-termios.h>
>> > >     >> >>  #include <bsp/fdt.h>
>> > >     >> >>  #include <bsp/fatal.h>
>> > >     >> >> +#include <bsp/gpio.h>
>> > >     >> >> +#include <bsp/rpi-gpio.h>
>> > >     >> >>
>> > >     >> >> -
>> > >     >> >> -#define UART0     "/dev/ttyS0"
>> > >     >> >> +/**
>> > >     >> >> + * UART0 - PL011
>> > >     >> >> + * UART1 - Mini UART
>> > >     >> >> + */
>> > >     >> >> +#define PL011     "/dev/ttyAMA0"
>> > >     >> >> +#define MINIUART  "/dev/ttyS0"
>> > >     >> >>  #define FBCONS    "/dev/fbcons"
>> > >     >> >>
>> > >     >> >>  arm_pl011_context pl011_context;
>> > >     >> >> +ns16550_context mini_uart_context;
>> > >     >> >>
>> > >     >> >>  rpi_fb_context fb_context;
>> > >     >> >>
>> > >     >> >> -static void output_char_serial(char c)
>> > >     >> >> +static void output_char_pl011(char c)
>> > >     >> >>  {
>> > >     >> >>    arm_pl011_write_polled(&pl011_context.base, c);
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >> +static void output_char_mini_uart(char c)
>> > >     >> >> +{
>> > >     >> >> +  ns16550_polled_putchar(&mini_uart_context.base, c);
>> > >     >> >> +}
>> > >     >> >> +
>> > >     >> >>  void output_char_fb(char c)
>> > >     >> >>  {
>> > >     >> >>    fbcons_write_polled(&fb_context.base, c);
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >> +static uint8_t mini_uart_get_reg(uintptr_t port, uint8_t index)
>> > >     >> >> +{
>> > >     >> >> +  volatile uint32_t *val = (volatile uint32_t *)port + index;
>> > >     >> >> +  return (uint8_t) *val;
>> > >     >> >> +}
>> > >     >> >> +
>> > >     >> >> +static void mini_uart_set_reg(uintptr_t port, uint8_t index,
>> > >     uint8_t val)
>> > >     >> >> +{
>> > >     >> >> +  volatile uint32_t *reg = (volatile uint32_t *)port + index;
>> > >     >> >> +  *reg = val;
>> > >     >> >> +}
>> > >     >> >> +
>> > >     >> >>  static void init_ctx_arm_pl011(
>> > >     >> >>    const void *fdt,
>> > >     >> >>    int node
>> > >     >> >>  )
>> > >     >> >>  {
>> > >     >> >>    arm_pl011_context *ctx = &pl011_context;
>> > >     >> >> -  rtems_termios_device_context_initialize(&ctx->base, "UART");
>> > >     >> >> +  rtems_termios_device_context_initialize(&ctx->base,
>> > >     "PL011UART");
>> > >     >> >>    ctx->regs = raspberrypi_get_reg_of_node(fdt, node);
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >> +static uint32_t calculate_baud_divisor(
>> > >     >> >> +  ns16550_context *ctx,
>> > >     >> >> +  uint32_t baud
>> > >     >> >> +)
>> > >     >> >> +{
>> > >     >> >> +  uint32_t baudDivisor = (ctx->clock / (8 * baud)) - 1;
>> > >     >> >> +  return baudDivisor;
>> > >     >> >> +}
>> > >     >> >> +
>> > >     >> >> +static void init_ctx_mini_uart(
>> > >     >> >> +  const void *fdt,
>> > >     >> >> +  int node
>> > >     >> >> +)
>> > >     >> >> +{
>> > >     >> >> +  const char *status;
>> > >     >> >> +  int len;
>> > >     >> >> +  ns16550_context *ctx;
>> > >     >> >> +
>> > >     >> >> +  memset(&mini_uart_context, 0, sizeof(mini_uart_context));
>> > >     >> >> +  ctx = &mini_uart_context;
>> > >     >> >> +
>> > >     >> >> +  rtems_termios_device_context_initialize(&ctx->base,
>> > >     "MiniUART");
>> > >     >> >> +
>> > >     >> >> +  status = fdt_getprop(fdt, node, "status", &len);
>> > >     >> >> +  if ( status == NULL || strcmp(status, "disabled" ) == 0){
>> > >     >> >> +    return ;
>> > >     >> >> +  }
>> > >     >> >> +
>> > >     >> >> +  ctx->port = (uintptr_t) raspberrypi_get_reg_of_node(fdt, node);
>> > >     >> >> +  ctx->initial_baud = MINI_UART_DEFAULT_BAUD;
>> > >     >> >> +  ctx->clock = BCM2835_CLOCK_FREQ;
>> > >     >> >> +  ctx->calculate_baud_divisor = calculate_baud_divisor;
>> > >     >> >> +  ctx->get_reg = mini_uart_get_reg;
>> > >     >> >> +  ctx->set_reg = mini_uart_set_reg;
>> > >     >> >> +
>> > >     >> >> +  rtems_gpio_bsp_select_specific_io(0, 14, RPI_ALT_FUNC_5, NULL);
>> > >     >> >> +  rtems_gpio_bsp_select_specific_io(0, 15, RPI_ALT_FUNC_5, NULL);
>> > >     >> >> +  rtems_gpio_bsp_set_resistor_mode(0, 14, NO_PULL_RESISTOR);
>> > >     >> >> +  rtems_gpio_bsp_set_resistor_mode(0, 15, NO_PULL_RESISTOR);
>> > >     >> >> +
>> > >     >> >> +  BCM2835_REG(AUX_ENABLES) |= 0x1;
>> > >     >> >> +  ns16550_probe(&ctx->base);
>> > >     >> >> +}
>> > >     >> >> +
>> > >     >> >>  static void register_fb( void )
>> > >     >> >>  {
>> > >     >> >>    if (fbcons_probe(&fb_context.base) == true) {
>> > >     >> >> @@ -87,16 +156,28 @@ static void console_select( void )
>> > >     >> >>          link(FBCONS, CONSOLE_DEVICE_NAME);
>> > >     >> >>          return ;
>> > >     >> >>        }
>> > >     >> >> +    } else if ( strncmp( opt, MINIUART, sizeof(MINIUART) - 1
>> > >     ) == 0) {
>> > >     >> >> +      BSP_output_char = output_char_mini_uart;
>> > >     >> >> +      link(MINIUART, CONSOLE_DEVICE_NAME);
>> > >     >> >> +    } else if ( strncmp( opt, PL011, sizeof(PL011) - 1 ) == 0) {
>> > >     >> >> +      BSP_output_char = output_char_pl011;
>> > >     >> >> +      link(PL011, CONSOLE_DEVICE_NAME);
>> > >     >> >>      }
>> > >     >> >> +  }else {
>> > >     >> >> +    /**
>> > >     >> >> +     * If no command line option was given, default to PL011.
>> > >     >> >> +     */
>> > >     >> >> +    BSP_output_char = output_char_pl011;
>> > >     >> >> +    link(PL011, CONSOLE_DEVICE_NAME);
>> > >     >> >>    }
>> > >     >> >> -  BSP_output_char = output_char_serial;
>> > >     >> >> -  link(UART0, CONSOLE_DEVICE_NAME);
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >>  static void uart_probe(void)
>> > >     >> >>  {
>> > >     >> >>    static bool initialized = false;
>> > >     >> >>    const void *fdt;
>> > >     >> >> +  const char *console;
>> > >     >> >> +  int len;
>> > >     >> >>    int node;
>> > >     >> >>
>> > >     >> >>    if ( initialized ) {
>> > >     >> >> @@ -104,17 +185,29 @@ static void uart_probe(void)
>> > >     >> >>    }
>> > >     >> >>
>> > >     >> >>    fdt = bsp_fdt_get();
>> > >     >> >> -  node = fdt_node_offset_by_compatible(fdt, -1,
>> > >     "brcm,bcm2835-pl011");
>> > >     >> >>
>> > >     >> >> +  node = fdt_node_offset_by_compatible(fdt, -1,
>> > >     "brcm,bcm2835-pl011");
>> > >     >> >>    init_ctx_arm_pl011(fdt, node);
>> > >     >> >>
>> > >     >> >> +  node = fdt_node_offset_by_compatible(fdt, -1,
>> > >     "brcm,bcm2835-aux-uart");
>> > >     >> >> +  init_ctx_mini_uart(fdt, node);
>> > >     >> >> +
>> > >     >> >> +  node = fdt_path_offset(fdt, "/aliases");
>> > >     >> >> +  console = fdt_getprop(fdt, node, "serial0", &len);
>> > >     >> >> +
>> > >     >> >> +  if ( strcmp(console, "/soc/serial at 7e215040" ) == 0) {
>> > >     >> >> +    BSP_output_char = output_char_mini_uart;
>> > >     >> >> +  }else {
>> > >     >> >> +    BSP_output_char = output_char_pl011;
>> > >     >> >> +  }
>> > >     >> >> +
>> > >     >> >>    initialized = true;
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >>  static void output_char(char c)
>> > >     >> >>  {
>> > >     >> >>    uart_probe();
>> > >     >> >> -  output_char_serial(c);
>> > >     >> >> +  (*BSP_output_char)(c);
>> > >     >> >>  }
>> > >     >> >>
>> > >     >> >>  rtems_status_code console_initialize(
>> > >     >> >> @@ -127,12 +220,19 @@ rtems_status_code console_initialize(
>> > >     >> >>
>> > >     >> >>    uart_probe();
>> > >     >> >>    rtems_termios_device_install(
>> > >     >> >> -    UART0,
>> > >     >> >> +    PL011,
>> > >     >> >>      &arm_pl011_fns,
>> > >     >> >>      NULL,
>> > >     >> >>      &pl011_context.base
>> > >     >> >>    );
>> > >     >> >>
>> > >     >> >> +  rtems_termios_device_install(
>> > >     >> >> +    MINIUART,
>> > >     >> >> +    &ns16550_handler_polled,
>> > >     >> >> +    NULL,
>> > >     >> >> +    &mini_uart_context.base
>> > >     >> >> +  );
>> > >     >> >> +
>> > >     >> >>    register_fb();
>> > >     >> >>
>> > >     >> >>    console_select();
>> > >     >> >> diff --git a/bsps/arm/raspberrypi/include/bsp/usart.h
>> > >     b/bsps/arm/raspberrypi/include/bsp/usart.h
>> > >     >> >> index abbf53626c..6af1844b3c 100644
>> > >     >> >> --- a/bsps/arm/raspberrypi/include/bsp/usart.h
>> > >     >> >> +++ b/bsps/arm/raspberrypi/include/bsp/usart.h
>> > >     >> >> @@ -33,6 +33,7 @@ extern "C" {
>> > >     >> >>  #endif /* __cplusplus */
>> > >     >> >>
>> > >     >> >>  #define PL011_DEFAULT_BAUD 115000
>> > >     >> >> +#define MINI_UART_DEFAULT_BAUD   115200
>> > >     >> >>  #define BCM2835_PL011_BASE (RPI_PERIPHERAL_BASE + 0x201000)
>> > >     >> >>
>> > >     >> >>  #ifdef __cplusplus
>> > >     >> >> --
>> > >     >> >> 2.17.1
>> > >     >> >>
>> > >     >> > _______________________________________________
>> > >     >> > devel mailing list
>> > >     >> > devel at rtems.org <mailto:devel at rtems.org>
>> > >     >> > http://lists.rtems.org/mailman/listinfo/devel
>> > >
>> > >
>> > > _______________________________________________
>> > > devel mailing list
>> > > devel at rtems.org
>> > > http://lists.rtems.org/mailman/listinfo/devel
>> > >


More information about the devel mailing list