media: imx: imx7-media-csi: Sync frames to start of frame for MIPI

Message ID 20231017150854.270003-1-s.riedmueller@phytec.de (mailing list archive)
State New
Headers
Series media: imx: imx7-media-csi: Sync frames to start of frame for MIPI |

Commit Message

Stefan Riedmüller Oct. 17, 2023, 3:08 p.m. UTC
  The default behavior for a base address change is to do it automatically
after a DMA completion. This can lead to the situation, were one
corrupted frame, with less lines than configured, results in all
following frames being corrupted as well, due to a missing
re-synchronization to the beginning of the next frame.

Fix this by configuring the base address switch to be synced with the
start of frame event.

Currently this is already implemented for the parallel interface. To
have it with MIPI as well, simply configure it unconditionally.

Tested on i.MX 8MM.

Signed-off-by: Stefan Riedmüller <s.riedmueller@phytec.de>
---
 drivers/media/platform/nxp/imx7-media-csi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Fabio Estevam Oct. 17, 2023, 3:46 p.m. UTC | #1
Hi Stefan,

On Tue, Oct 17, 2023 at 12:09 PM Stefan Riedmüller
<s.riedmueller@phytec.de> wrote:
>
> The default behavior for a base address change is to do it automatically
> after a DMA completion. This can lead to the situation, were one
> corrupted frame, with less lines than configured, results in all
> following frames being corrupted as well, due to a missing
> re-synchronization to the beginning of the next frame.
>
> Fix this by configuring the base address switch to be synced with the
> start of frame event.
>
> Currently this is already implemented for the parallel interface. To
> have it with MIPI as well, simply configure it unconditionally.
>
> Tested on i.MX 8MM.

Should this contain a Fixes tag? Thanks
  
Stefan Riedmüller Oct. 18, 2023, 8:02 a.m. UTC | #2
Hi Fabio,

On Tue, 2023-10-17 at 12:46 -0300, Fabio Estevam wrote:
> Hi Stefan,
> 
> On Tue, Oct 17, 2023 at 12:09 PM Stefan Riedmüller
> <s.riedmueller@phytec.de> wrote:
> > 
> > The default behavior for a base address change is to do it automatically
> > after a DMA completion. This can lead to the situation, were one
> > corrupted frame, with less lines than configured, results in all
> > following frames being corrupted as well, due to a missing
> > re-synchronization to the beginning of the next frame.
> > 
> > Fix this by configuring the base address switch to be synced with the
> > start of frame event.
> > 
> > Currently this is already implemented for the parallel interface. To
> > have it with MIPI as well, simply configure it unconditionally.
> > 
> > Tested on i.MX 8MM.
> 
> Should this contain a Fixes tag? Thanks

Now that you mention it, I guess it could. I'm just not sure which commit to
select for the fixes tag. I think this issue is there since day one of the
driver, but I doubt that it would apply on every version of it. The fixes tag
is to trigger a stable fix, right? Or is it just meant as a note?

Do you have any suggestions?

Thanks,
Stefan
  
Laurent Pinchart Oct. 18, 2023, 11:48 a.m. UTC | #3
Hi Stefan,

Thank you for the patch.

On Tue, Oct 17, 2023 at 05:08:54PM +0200, Stefan Riedmüller wrote:
> The default behavior for a base address change is to do it automatically
> after a DMA completion. This can lead to the situation, were one
> corrupted frame, with less lines than configured, results in all
> following frames being corrupted as well, due to a missing
> re-synchronization to the beginning of the next frame.
> 
> Fix this by configuring the base address switch to be synced with the
> start of frame event.
> 
> Currently this is already implemented for the parallel interface. To
> have it with MIPI as well, simply configure it unconditionally.
> 
> Tested on i.MX 8MM.

I recall not doing this unconditionally as it didn't work on some of the
platforms I was testing, but I'm not sure of the details anymore. I'll
retest on i.MX7.

Do we have any buffer overflow protection in this mode ? If the sensor
happens to send more lines than expected, will extra lines be dropped,
or overflow the buffer ?

> Signed-off-by: Stefan Riedmüller <s.riedmueller@phytec.de>
> ---
>  drivers/media/platform/nxp/imx7-media-csi.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/media/platform/nxp/imx7-media-csi.c b/drivers/media/platform/nxp/imx7-media-csi.c
> index 15049c6aab37..0c9e3f01e96d 100644
> --- a/drivers/media/platform/nxp/imx7-media-csi.c
> +++ b/drivers/media/platform/nxp/imx7-media-csi.c
> @@ -529,13 +529,13 @@ static void imx7_csi_configure(struct imx7_csi *csi,
>  		stride = out_pix->width;
>  	}
>  
> +	cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
> +		BIT_BASEADDR_CHG_ERR_EN;
> +
>  	if (!csi->is_csi2) {
>  		cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | BIT_HSYNC_POL
>  		    | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
>  
> -		cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
> -			BIT_BASEADDR_CHG_ERR_EN;
> -
>  		if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
>  		    out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
>  			width *= 2;
  
Stefan Riedmüller Oct. 19, 2023, 7:44 a.m. UTC | #4
Hi Laurant,

On Wed, 2023-10-18 at 14:48 +0300, Laurent Pinchart wrote:
> Hi Stefan,
> 
> Thank you for the patch.
> 
> On Tue, Oct 17, 2023 at 05:08:54PM +0200, Stefan Riedmüller wrote:
> > The default behavior for a base address change is to do it automatically
> > after a DMA completion. This can lead to the situation, were one
> > corrupted frame, with less lines than configured, results in all
> > following frames being corrupted as well, due to a missing
> > re-synchronization to the beginning of the next frame.
> > 
> > Fix this by configuring the base address switch to be synced with the
> > start of frame event.
> > 
> > Currently this is already implemented for the parallel interface. To
> > have it with MIPI as well, simply configure it unconditionally.
> > 
> > Tested on i.MX 8MM.
> 
> I recall not doing this unconditionally as it didn't work on some of the
> platforms I was testing, but I'm not sure of the details anymore. I'll
> retest on i.MX7.

Thanks!

> Do we have any buffer overflow protection in this mode ? If the sensor
> happens to send more lines than expected, will extra lines be dropped,
> or overflow the buffer ?

As far as I understand the imx8mm reference manual, I think there is a buffer
overflow protection, in the form that the DMA will only write the amount of
data which was configured in CSI_IMAG_PARA. If additional data arrives on the
bus, without a new start of frame event, a new DMA transfer will be triggered
eventually. Since there was no base address change, due to the missing start
frame event, the new DMA transfer will use the same buffer as before and
corrupt that data. But all subsequent frames will be ok since a new frame will
trigger the start of frame event and thus a base address change.


Regards,
Stefan  

> 
> > Signed-off-by: Stefan Riedmüller <s.riedmueller@phytec.de>
> > ---
> >  drivers/media/platform/nxp/imx7-media-csi.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/media/platform/nxp/imx7-media-csi.c
> > b/drivers/media/platform/nxp/imx7-media-csi.c
> > index 15049c6aab37..0c9e3f01e96d 100644
> > --- a/drivers/media/platform/nxp/imx7-media-csi.c
> > +++ b/drivers/media/platform/nxp/imx7-media-csi.c
> > @@ -529,13 +529,13 @@ static void imx7_csi_configure(struct imx7_csi *csi,
> >  		stride = out_pix->width;
> >  	}
> >  
> > +	cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
> > +		BIT_BASEADDR_CHG_ERR_EN;
> > +
> >  	if (!csi->is_csi2) {
> >  		cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE |
> > BIT_HSYNC_POL
> >  		    | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
> >  
> > -		cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL
> > |
> > -			BIT_BASEADDR_CHG_ERR_EN;
> > -
> >  		if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
> >  		    out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
> >  			width *= 2;
>
  
Stefan Riedmüller Dec. 5, 2023, 12:14 p.m. UTC | #5
Hi Laurent,

On Thu, 2023-10-19 at 07:44 +0000, Stefan Riedmüller wrote:
> Hi Laurant,
> 
> On Wed, 2023-10-18 at 14:48 +0300, Laurent Pinchart wrote:
> > Hi Stefan,
> > 
> > Thank you for the patch.
> > 
> > On Tue, Oct 17, 2023 at 05:08:54PM +0200, Stefan Riedmüller wrote:
> > > The default behavior for a base address change is to do it automatically
> > > after a DMA completion. This can lead to the situation, were one
> > > corrupted frame, with less lines than configured, results in all
> > > following frames being corrupted as well, due to a missing
> > > re-synchronization to the beginning of the next frame.
> > > 
> > > Fix this by configuring the base address switch to be synced with the
> > > start of frame event.
> > > 
> > > Currently this is already implemented for the parallel interface. To
> > > have it with MIPI as well, simply configure it unconditionally.
> > > 
> > > Tested on i.MX 8MM.
> > 
> > I recall not doing this unconditionally as it didn't work on some of the
> > platforms I was testing, but I'm not sure of the details anymore. I'll
> > retest on i.MX7.
> 
> Thanks!

Any update yet on the i.MX7 test?

Regards,
Stefan

> 
> > Do we have any buffer overflow protection in this mode ? If the sensor
> > happens to send more lines than expected, will extra lines be dropped,
> > or overflow the buffer ?
> 
> As far as I understand the imx8mm reference manual, I think there is a
> buffer
> overflow protection, in the form that the DMA will only write the amount of
> data which was configured in CSI_IMAG_PARA. If additional data arrives on
> the
> bus, without a new start of frame event, a new DMA transfer will be
> triggered
> eventually. Since there was no base address change, due to the missing start
> frame event, the new DMA transfer will use the same buffer as before and
> corrupt that data. But all subsequent frames will be ok since a new frame
> will
> trigger the start of frame event and thus a base address change.
> 
> 
> Regards,
> Stefan  t
> 
> > 
> > > Signed-off-by: Stefan Riedmüller <s.riedmueller@phytec.de>
> > > ---
> > >  drivers/media/platform/nxp/imx7-media-csi.c | 6 +++---
> > >  1 file changed, 3 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/drivers/media/platform/nxp/imx7-media-csi.c
> > > b/drivers/media/platform/nxp/imx7-media-csi.c
> > > index 15049c6aab37..0c9e3f01e96d 100644
> > > --- a/drivers/media/platform/nxp/imx7-media-csi.c
> > > +++ b/drivers/media/platform/nxp/imx7-media-csi.c
> > > @@ -529,13 +529,13 @@ static void imx7_csi_configure(struct imx7_csi
> > > *csi,
> > >  		stride = out_pix->width;
> > >  	}
> > >  
> > > +	cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
> > > +		BIT_BASEADDR_CHG_ERR_EN;
> > > +
> > >  	if (!csi->is_csi2) {
> > >  		cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE |
> > > BIT_HSYNC_POL
> > >  		    | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
> > >  
> > > -		cr18 |= BIT_BASEADDR_SWITCH_EN |
> > > BIT_BASEADDR_SWITCH_SEL
> > > > 
> > > -			BIT_BASEADDR_CHG_ERR_EN;
> > > -
> > >  		if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
> > >  		    out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
> > >  			width *= 2;
> > 
> _______________________________________________
> upstream mailing list
> upstream@lists.phytec.de
> http://lists.phytec.de/cgi-bin/mailman/listinfo/upstream
  

Patch

diff --git a/drivers/media/platform/nxp/imx7-media-csi.c b/drivers/media/platform/nxp/imx7-media-csi.c
index 15049c6aab37..0c9e3f01e96d 100644
--- a/drivers/media/platform/nxp/imx7-media-csi.c
+++ b/drivers/media/platform/nxp/imx7-media-csi.c
@@ -529,13 +529,13 @@  static void imx7_csi_configure(struct imx7_csi *csi,
 		stride = out_pix->width;
 	}
 
+	cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
+		BIT_BASEADDR_CHG_ERR_EN;
+
 	if (!csi->is_csi2) {
 		cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | BIT_HSYNC_POL
 		    | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
 
-		cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
-			BIT_BASEADDR_CHG_ERR_EN;
-
 		if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
 		    out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
 			width *= 2;