Skip to content

Commit e82e475

Browse files
zearglaubitz
authored andcommitted
sh: dma: Fix DMA channel offset calculation
Various SoCs of the SH3, SH4 and SH4A family, which use this driver, feature a differing number of DMA channels, which can be distributed between up to two DMAC modules. The existing implementation fails to correctly accommodate for all those variations, resulting in wrong channel offset calculations and leading to kernel panics. Rewrite dma_base_addr() in order to properly calculate channel offsets in a DMAC module. Fix dmaor_read_reg() and dmaor_write_reg(), so that the correct DMAC module base is selected for the DMAOR register. Fixes: 7f47c71 ("sh: dma: More legacy cpu dma chainsawing.") Signed-off-by: Artur Rojek <[email protected]> Reviewed-by: Geert Uytterhoeven <[email protected]> Reviewed-by: John Paul Adrian Glaubitz <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: John Paul Adrian Glaubitz <[email protected]>
1 parent 4bd04b2 commit e82e475

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

arch/sh/drivers/dma/dma-sh.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
#include <cpu/dma-register.h>
1919
#include <cpu/dma.h>
2020

21+
/*
22+
* Some of the SoCs feature two DMAC modules. In such a case, the channels are
23+
* distributed equally among them.
24+
*/
25+
#ifdef SH_DMAC_BASE1
26+
#define SH_DMAC_NR_MD_CH (CONFIG_NR_ONCHIP_DMA_CHANNELS / 2)
27+
#else
28+
#define SH_DMAC_NR_MD_CH CONFIG_NR_ONCHIP_DMA_CHANNELS
29+
#endif
30+
31+
#define SH_DMAC_CH_SZ 0x10
32+
2133
/*
2234
* Define the default configuration for dual address memory-memory transfer.
2335
* The 0x400 value represents auto-request, external->external.
@@ -29,7 +41,7 @@ static unsigned long dma_find_base(unsigned int chan)
2941
unsigned long base = SH_DMAC_BASE0;
3042

3143
#ifdef SH_DMAC_BASE1
32-
if (chan >= 6)
44+
if (chan >= SH_DMAC_NR_MD_CH)
3345
base = SH_DMAC_BASE1;
3446
#endif
3547

@@ -40,13 +52,13 @@ static unsigned long dma_base_addr(unsigned int chan)
4052
{
4153
unsigned long base = dma_find_base(chan);
4254

43-
/* Normalize offset calculation */
44-
if (chan >= 9)
45-
chan -= 6;
46-
if (chan >= 4)
47-
base += 0x10;
55+
chan = (chan % SH_DMAC_NR_MD_CH) * SH_DMAC_CH_SZ;
56+
57+
/* DMAOR is placed inside the channel register space. Step over it. */
58+
if (chan >= DMAOR)
59+
base += SH_DMAC_CH_SZ;
4860

49-
return base + (chan * 0x10);
61+
return base + chan;
5062
}
5163

5264
#ifdef CONFIG_SH_DMA_IRQ_MULTI
@@ -250,12 +262,11 @@ static int sh_dmac_get_dma_residue(struct dma_channel *chan)
250262
#define NR_DMAOR 1
251263
#endif
252264

253-
/*
254-
* DMAOR bases are broken out amongst channel groups. DMAOR0 manages
255-
* channels 0 - 5, DMAOR1 6 - 11 (optional).
256-
*/
257-
#define dmaor_read_reg(n) __raw_readw(dma_find_base((n)*6))
258-
#define dmaor_write_reg(n, data) __raw_writew(data, dma_find_base(n)*6)
265+
#define dmaor_read_reg(n) __raw_readw(dma_find_base((n) * \
266+
SH_DMAC_NR_MD_CH) + DMAOR)
267+
#define dmaor_write_reg(n, data) __raw_writew(data, \
268+
dma_find_base((n) * \
269+
SH_DMAC_NR_MD_CH) + DMAOR)
259270

260271
static inline int dmaor_reset(int no)
261272
{

0 commit comments

Comments
 (0)