Re: [PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Sebastian Huber

On 02/04/2020 17:15, Christian Mauderer wrote:


On 02/04/2020 17:00, Sebastian Huber wrote:

On 02/04/2020 16:42, Christian Mauderer wrote:


diff --git a/freebsd/sys/dev/usb/usb_busdma.c
b/freebsd/sys/dev/usb/usb_busdma.c
index 9a70c687..c8000170 100644
--- a/freebsd/sys/dev/usb/usb_busdma.c
+++ b/freebsd/sys/dev/usb/usb_busdma.c
@@ -78,6 +78,52 @@ static void    usb_pc_common_mem_cb(void *,
bus_dma_segment_t *, int, int,
   uint8_t);
   #endif
   +#ifdef __rtems__
+#include 
+#if defined(LIBBSP_ARM_IMX_BSP_H)
+#define NEED_MISSALIGNED_COPY
+#endif

Could you please move changes in general file imported from FreeBSD to
separate commits.

OK. I'll split them.


I don't like this change here at all. I had a similar issue with
misaligned memcpy() to device memory here on the STM32H7 platform. Could
you first try this patch here:

Works fine too. The patch seems generic. Do you plan to add it soon as
an extra patch?

I will try to send a patch tomorrow.
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Christian Mauderer
On 02/04/2020 17:00, Sebastian Huber wrote:
> On 02/04/2020 16:42, Christian Mauderer wrote:
> 
>> diff --git a/freebsd/sys/dev/usb/usb_busdma.c
>> b/freebsd/sys/dev/usb/usb_busdma.c
>> index 9a70c687..c8000170 100644
>> --- a/freebsd/sys/dev/usb/usb_busdma.c
>> +++ b/freebsd/sys/dev/usb/usb_busdma.c
>> @@ -78,6 +78,52 @@ static void    usb_pc_common_mem_cb(void *,
>> bus_dma_segment_t *, int, int,
>>   uint8_t);
>>   #endif
>>   +#ifdef __rtems__
>> +#include 
>> +#if defined(LIBBSP_ARM_IMX_BSP_H)
>> +#define NEED_MISSALIGNED_COPY
>> +#endif
> 
> Could you please move changes in general file imported from FreeBSD to
> separate commits.

OK. I'll split them.

> 
> I don't like this change here at all. I had a similar issue with
> misaligned memcpy() to device memory here on the STM32H7 platform. Could
> you first try this patch here:

Works fine too. The patch seems generic. Do you plan to add it soon as
an extra patch?

> 
> diff --git a/freebsd/sys/dev/usb/usb_busdma.c
> b/freebsd/sys/dev/usb/usb_busdma.c
> index 9a70c687..973a3409 100644
> --- a/freebsd/sys/dev/usb/usb_busdma.c
> +++ b/freebsd/sys/dev/usb/usb_busdma.c
> @@ -584,7 +584,7 @@ usb_pc_alloc_mem(struct usb_page_cache *pc, struct
> usb_page *pg,
>     }
>     /* allocate memory */
>     if (bus_dmamem_alloc(
> -   utag->tag, , (BUS_DMA_WAITOK | BUS_DMA_COHERENT), )) {
> +   utag->tag, , BUS_DMA_WAITOK, )) {
>     goto error;
>     }
>     /* setup page cache */
> 

-- 

embedded brains GmbH
Herr Christian Mauderer
Dornierstr. 4
D-82178 Puchheim
Germany
email: christian.maude...@embedded-brains.de
Phone: +49-89-18 94 741 - 18
Fax:   +49-89-18 94 741 - 08
PGP: Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Sebastian Huber

On 02/04/2020 16:42, Christian Mauderer wrote:


diff --git a/freebsd/sys/dev/usb/usb_busdma.c b/freebsd/sys/dev/usb/usb_busdma.c
index 9a70c687..c8000170 100644
--- a/freebsd/sys/dev/usb/usb_busdma.c
+++ b/freebsd/sys/dev/usb/usb_busdma.c
@@ -78,6 +78,52 @@ static void  usb_pc_common_mem_cb(void *, bus_dma_segment_t 
*, int, int,
uint8_t);
  #endif
  
+#ifdef __rtems__

+#include 
+#if defined(LIBBSP_ARM_IMX_BSP_H)
+#define NEED_MISSALIGNED_COPY
+#endif


Could you please move changes in general file imported from FreeBSD to 
separate commits.


I don't like this change here at all. I had a similar issue with 
misaligned memcpy() to device memory here on the STM32H7 platform. Could 
you first try this patch here:


diff --git a/freebsd/sys/dev/usb/usb_busdma.c 
b/freebsd/sys/dev/usb/usb_busdma.c

index 9a70c687..973a3409 100644
--- a/freebsd/sys/dev/usb/usb_busdma.c
+++ b/freebsd/sys/dev/usb/usb_busdma.c
@@ -584,7 +584,7 @@ usb_pc_alloc_mem(struct usb_page_cache *pc, struct 
usb_page *pg,

    }
    /* allocate memory */
    if (bus_dmamem_alloc(
-   utag->tag, , (BUS_DMA_WAITOK | BUS_DMA_COHERENT), )) {
+   utag->tag, , BUS_DMA_WAITOK, )) {
    goto error;
    }
    /* setup page cache */

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Sebastian Huber

On 02/04/2020 16:47, Christian Mauderer wrote:


Hello,

just noted: This patch now contains more from my code than from
Sebastian. I'll discuss with him whether we change the author before
commiting it.

Just change the author if it makes sense.
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Christian Mauderer
Hello,

just noted: This patch now contains more from my code than from
Sebastian. I'll discuss with him whether we change the author before
commiting it.

Best regards

Christian

On 02/04/2020 16:42, Christian Mauderer wrote:
> From: Sebastian Huber 
> 
> Update #3869.
> ---
>  freebsd/sys/arm/freescale/imx/imx6_usbphy.c | 11 ++
>  freebsd/sys/dev/usb/controller/ehci_imx.c   | 12 +++
>  freebsd/sys/dev/usb/usb_busdma.c| 54 
> +
>  libbsd.py   |  4 +++
>  rtemsbsd/include/bsp/nexus-devices.h|  5 +++
>  5 files changed, 86 insertions(+)
> 
> diff --git a/freebsd/sys/arm/freescale/imx/imx6_usbphy.c 
> b/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
> index 8f47507b..ad545601 100644
> --- a/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
> +++ b/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
> @@ -91,6 +91,17 @@ usbphy_detach(device_t dev)
>   return (0);
>  }
>  
> +#ifdef __rtems__
> +#define BUS_SPACE_PHYSADDR(res, offs) \
> + ((u_int)(rman_get_start(res)+(offs)))
> +
> +void
> +imx6_anatop_write_4(bus_size_t offset, uint32_t value)
> +{
> +
> + bus_space_write_4(0, 0x20c8000, offset, value);
> +}
> +#endif /* __rtems__ */
>  static int
>  usbphy_attach(device_t dev)
>  {
> diff --git a/freebsd/sys/dev/usb/controller/ehci_imx.c 
> b/freebsd/sys/dev/usb/controller/ehci_imx.c
> index a872fb62..d158df1d 100644
> --- a/freebsd/sys/dev/usb/controller/ehci_imx.c
> +++ b/freebsd/sys/dev/usb/controller/ehci_imx.c
> @@ -303,6 +303,16 @@ imx_ehci_probe(device_t dev)
>   return (ENXIO);
>  
>   if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
> +#ifdef __rtems__
> + char dr_mode[24];
> +
> + if (OF_getprop(ofw_bus_get_node(dev), "dr_mode",
> + _mode, sizeof(dr_mode)) > 0 &&
> + strcasecmp(dr_mode, "host") != 0) {
> + return (ENXIO);
> + }
> +#endif /* __rtems__ */
> +
>   device_set_desc(dev, "Freescale i.MX integrated USB 
> controller");
>   return (BUS_PROBE_DEFAULT);
>   }
> @@ -437,8 +447,10 @@ imx_ehci_attach(device_t dev)
>   goto out;
>   }
>  
> +#ifndef __rtems__
>   /* Turn on clocks. */
>   imx_ccm_usb_enable(dev);
> +#endif /* __rtems__ */
>  
>   /* Disable overcurrent detection, if configured to do so. */
>   if (OF_hasprop(ofw_bus_get_node(sc->dev), "disable-over-current"))
> diff --git a/freebsd/sys/dev/usb/usb_busdma.c 
> b/freebsd/sys/dev/usb/usb_busdma.c
> index 9a70c687..c8000170 100644
> --- a/freebsd/sys/dev/usb/usb_busdma.c
> +++ b/freebsd/sys/dev/usb/usb_busdma.c
> @@ -78,6 +78,52 @@ static voidusb_pc_common_mem_cb(void *, 
> bus_dma_segment_t *, int, int,
>   uint8_t);
>  #endif
>  
> +#ifdef __rtems__
> +#include 
> +#if defined(LIBBSP_ARM_IMX_BSP_H)
> +#define NEED_MISSALIGNED_COPY
> +#endif
> +
> +#ifdef NEED_MISSALIGNED_COPY
> +/*
> + * Note: This functions are used instead of memcpy for regions where a
> + * misaligned access is not allowed (e.g. peripheral memory).
> + */
> +static void misaligned_do_copy(
> +  uint8_t *dst,
> +  const uint8_t *src,
> +  size_t n,
> +  bool aligned
> +)
> +{
> +  if (aligned) {
> +while (n > 3) {
> +  *(uint32_t *)dst = *(uint32_t *)src;
> +  dst += 4;
> +  src += 4;
> +  n -= 4;
> +}
> +  }
> +
> +  while (n > 0) {
> +*dst = *src;
> +++dst;
> +++src;
> +--n;
> +  }
> +}
> +
> +void misaligned_copy_to_io(void *dst, const void *src, size_t n)
> +{
> +  misaligned_do_copy(dst, src, n, ((uintptr_t)dst) % 4 == 0);
> +}
> +
> +void misaligned_copy_from_io(void *dst, const void *src, size_t n)
> +{
> +  misaligned_do_copy(dst, src, n, ((uintptr_t)src) % 4 == 0);
> +}
> +#endif
> +#endif /* __rtems__ */
>  /**
>   *  usbd_get_page - lookup DMA-able memory for the given offset
>   *
> @@ -182,7 +228,11 @@ usbd_copy_in(struct usb_page_cache *cache, 
> usb_frlength_t offset,
>   if (buf_res.length > len) {
>   buf_res.length = len;
>   }
> +#if defined(__rtems__) && defined(NEED_MISSALIGNED_COPY)
> + misaligned_copy_to_io(buf_res.buffer, ptr, buf_res.length);
> +#else /* __rtems__ */
>   memcpy(buf_res.buffer, ptr, buf_res.length);
> +#endif /* __rtems__ */
>  
>   offset += buf_res.length;
>   len -= buf_res.length;
> @@ -302,7 +352,11 @@ usbd_copy_out(struct usb_page_cache *cache, 
> usb_frlength_t offset,
>   if (res.length > len) {
>   res.length = len;
>   }
> +#if defined(__rtems__) && defined(NEED_MISSALIGNED_COPY)
> + misaligned_copy_from_io(ptr, res.buffer, res.length);
> +#else /* __rtems__ */
>   memcpy(ptr, res.buffer, res.length);
> +#endif /* __rtems__ */
>  
>   offset += 

[PATCH libbsd 09/11] ehci_imx.c: Port to RTEMS

2020-04-02 Thread Christian Mauderer
From: Sebastian Huber 

Update #3869.
---
 freebsd/sys/arm/freescale/imx/imx6_usbphy.c | 11 ++
 freebsd/sys/dev/usb/controller/ehci_imx.c   | 12 +++
 freebsd/sys/dev/usb/usb_busdma.c| 54 +
 libbsd.py   |  4 +++
 rtemsbsd/include/bsp/nexus-devices.h|  5 +++
 5 files changed, 86 insertions(+)

diff --git a/freebsd/sys/arm/freescale/imx/imx6_usbphy.c 
b/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
index 8f47507b..ad545601 100644
--- a/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
+++ b/freebsd/sys/arm/freescale/imx/imx6_usbphy.c
@@ -91,6 +91,17 @@ usbphy_detach(device_t dev)
return (0);
 }
 
+#ifdef __rtems__
+#define BUS_SPACE_PHYSADDR(res, offs) \
+   ((u_int)(rman_get_start(res)+(offs)))
+
+void
+imx6_anatop_write_4(bus_size_t offset, uint32_t value)
+{
+
+   bus_space_write_4(0, 0x20c8000, offset, value);
+}
+#endif /* __rtems__ */
 static int
 usbphy_attach(device_t dev)
 {
diff --git a/freebsd/sys/dev/usb/controller/ehci_imx.c 
b/freebsd/sys/dev/usb/controller/ehci_imx.c
index a872fb62..d158df1d 100644
--- a/freebsd/sys/dev/usb/controller/ehci_imx.c
+++ b/freebsd/sys/dev/usb/controller/ehci_imx.c
@@ -303,6 +303,16 @@ imx_ehci_probe(device_t dev)
return (ENXIO);
 
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
+#ifdef __rtems__
+   char dr_mode[24];
+
+   if (OF_getprop(ofw_bus_get_node(dev), "dr_mode",
+   _mode, sizeof(dr_mode)) > 0 &&
+   strcasecmp(dr_mode, "host") != 0) {
+   return (ENXIO);
+   }
+#endif /* __rtems__ */
+
device_set_desc(dev, "Freescale i.MX integrated USB 
controller");
return (BUS_PROBE_DEFAULT);
}
@@ -437,8 +447,10 @@ imx_ehci_attach(device_t dev)
goto out;
}
 
+#ifndef __rtems__
/* Turn on clocks. */
imx_ccm_usb_enable(dev);
+#endif /* __rtems__ */
 
/* Disable overcurrent detection, if configured to do so. */
if (OF_hasprop(ofw_bus_get_node(sc->dev), "disable-over-current"))
diff --git a/freebsd/sys/dev/usb/usb_busdma.c b/freebsd/sys/dev/usb/usb_busdma.c
index 9a70c687..c8000170 100644
--- a/freebsd/sys/dev/usb/usb_busdma.c
+++ b/freebsd/sys/dev/usb/usb_busdma.c
@@ -78,6 +78,52 @@ static void  usb_pc_common_mem_cb(void *, bus_dma_segment_t 
*, int, int,
uint8_t);
 #endif
 
+#ifdef __rtems__
+#include 
+#if defined(LIBBSP_ARM_IMX_BSP_H)
+#define NEED_MISSALIGNED_COPY
+#endif
+
+#ifdef NEED_MISSALIGNED_COPY
+/*
+ * Note: This functions are used instead of memcpy for regions where a
+ * misaligned access is not allowed (e.g. peripheral memory).
+ */
+static void misaligned_do_copy(
+  uint8_t *dst,
+  const uint8_t *src,
+  size_t n,
+  bool aligned
+)
+{
+  if (aligned) {
+while (n > 3) {
+  *(uint32_t *)dst = *(uint32_t *)src;
+  dst += 4;
+  src += 4;
+  n -= 4;
+}
+  }
+
+  while (n > 0) {
+*dst = *src;
+++dst;
+++src;
+--n;
+  }
+}
+
+void misaligned_copy_to_io(void *dst, const void *src, size_t n)
+{
+  misaligned_do_copy(dst, src, n, ((uintptr_t)dst) % 4 == 0);
+}
+
+void misaligned_copy_from_io(void *dst, const void *src, size_t n)
+{
+  misaligned_do_copy(dst, src, n, ((uintptr_t)src) % 4 == 0);
+}
+#endif
+#endif /* __rtems__ */
 /**
  *  usbd_get_page - lookup DMA-able memory for the given offset
  *
@@ -182,7 +228,11 @@ usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t 
offset,
if (buf_res.length > len) {
buf_res.length = len;
}
+#if defined(__rtems__) && defined(NEED_MISSALIGNED_COPY)
+   misaligned_copy_to_io(buf_res.buffer, ptr, buf_res.length);
+#else /* __rtems__ */
memcpy(buf_res.buffer, ptr, buf_res.length);
+#endif /* __rtems__ */
 
offset += buf_res.length;
len -= buf_res.length;
@@ -302,7 +352,11 @@ usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t 
offset,
if (res.length > len) {
res.length = len;
}
+#if defined(__rtems__) && defined(NEED_MISSALIGNED_COPY)
+   misaligned_copy_from_io(ptr, res.buffer, res.length);
+#else /* __rtems__ */
memcpy(ptr, res.buffer, res.length);
+#endif /* __rtems__ */
 
offset += res.length;
len -= res.length;
diff --git a/libbsd.py b/libbsd.py
index 6eaca5ad..ed6493b2 100644
--- a/libbsd.py
+++ b/libbsd.py
@@ -5018,6 +5018,8 @@ class imx(builder.Module):
 mm = self.manager
 self.addKernelSpaceHeaderFiles(
 [
+'sys/arm/freescale/imx/imx6_anatopreg.h',
+'sys/arm/freescale/imx/imx6_anatopvar.h',
 'sys/arm/freescale/imx/imx6_ccmreg.h',