1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sh/drivers/dma/dma-sh.c
5 * SuperH On-chip DMAC Support
7 * Copyright (C) 2000 Takashi YOSHII
8 * Copyright (C) 2003, 2004 Paul Mundt
9 * Copyright (C) 2005 Andriy Skulysh
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
15 #include <mach-dreamcast/mach/dma.h>
17 #include <asm/dma-register.h>
18 #include <cpu/dma-register.h>
22 * Define the default configuration for dual address memory-memory transfer.
23 * The 0x400 value represents auto-request, external->external.
25 #define RS_DUAL (DM_INC | SM_INC | RS_AUTO | TS_INDEX2VAL(XMIT_SZ_32BIT))
27 static unsigned long dma_find_base(unsigned int chan
)
29 unsigned long base
= SH_DMAC_BASE0
;
39 static unsigned long dma_base_addr(unsigned int chan
)
41 unsigned long base
= dma_find_base(chan
);
43 /* Normalize offset calculation */
49 return base
+ (chan
* 0x10);
52 #ifdef CONFIG_SH_DMA_IRQ_MULTI
53 static inline unsigned int get_dmte_irq(unsigned int chan
)
55 return chan
>= 6 ? DMTE6_IRQ
: DMTE0_IRQ
;
59 static unsigned int dmte_irq_map
[] = {
60 DMTE0_IRQ
, DMTE0_IRQ
+ 1, DMTE0_IRQ
+ 2, DMTE0_IRQ
+ 3,
63 DMTE4_IRQ
, DMTE4_IRQ
+ 1,
67 DMTE6_IRQ
, DMTE6_IRQ
+ 1,
71 DMTE8_IRQ
, DMTE9_IRQ
, DMTE10_IRQ
, DMTE11_IRQ
,
75 static inline unsigned int get_dmte_irq(unsigned int chan
)
77 return dmte_irq_map
[chan
];
82 * We determine the correct shift size based off of the CHCR transmit size
83 * for the given channel. Since we know that it will take:
85 * info->count >> ts_shift[transmit_size]
87 * iterations to complete the transfer.
89 static unsigned int ts_shift
[] = TS_SHIFT
;
91 static inline unsigned int calc_xmit_shift(struct dma_channel
*chan
)
93 u32 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
94 int cnt
= ((chcr
& CHCR_TS_LOW_MASK
) >> CHCR_TS_LOW_SHIFT
) |
95 ((chcr
& CHCR_TS_HIGH_MASK
) >> CHCR_TS_HIGH_SHIFT
);
101 * The transfer end interrupt must read the chcr register to end the
102 * hardware interrupt active condition.
103 * Besides that it needs to waken any waiting process, which should handle
104 * setting up the next transfer.
106 static irqreturn_t
dma_tei(int irq
, void *dev_id
)
108 struct dma_channel
*chan
= dev_id
;
111 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
113 if (!(chcr
& CHCR_TE
))
116 chcr
&= ~(CHCR_IE
| CHCR_DE
);
117 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
119 wake_up(&chan
->wait_queue
);
124 static int sh_dmac_request_dma(struct dma_channel
*chan
)
126 if (unlikely(!(chan
->flags
& DMA_TEI_CAPABLE
)))
129 return request_irq(get_dmte_irq(chan
->chan
), dma_tei
, IRQF_SHARED
,
133 static void sh_dmac_free_dma(struct dma_channel
*chan
)
135 free_irq(get_dmte_irq(chan
->chan
), chan
);
139 sh_dmac_configure_channel(struct dma_channel
*chan
, unsigned long chcr
)
142 chcr
= RS_DUAL
| CHCR_IE
;
144 if (chcr
& CHCR_IE
) {
146 chan
->flags
|= DMA_TEI_CAPABLE
;
148 chan
->flags
&= ~DMA_TEI_CAPABLE
;
151 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
153 chan
->flags
|= DMA_CONFIGURED
;
157 static void sh_dmac_enable_dma(struct dma_channel
*chan
)
162 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
165 if (chan
->flags
& DMA_TEI_CAPABLE
)
168 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
170 if (chan
->flags
& DMA_TEI_CAPABLE
) {
171 irq
= get_dmte_irq(chan
->chan
);
176 static void sh_dmac_disable_dma(struct dma_channel
*chan
)
181 if (chan
->flags
& DMA_TEI_CAPABLE
) {
182 irq
= get_dmte_irq(chan
->chan
);
186 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
187 chcr
&= ~(CHCR_DE
| CHCR_TE
| CHCR_IE
);
188 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
191 static int sh_dmac_xfer_dma(struct dma_channel
*chan
)
194 * If we haven't pre-configured the channel with special flags, use
197 if (unlikely(!(chan
->flags
& DMA_CONFIGURED
)))
198 sh_dmac_configure_channel(chan
, 0);
200 sh_dmac_disable_dma(chan
);
203 * Single-address mode usage note!
205 * It's important that we don't accidentally write any value to SAR/DAR
206 * (this includes 0) that hasn't been directly specified by the user if
207 * we're in single-address mode.
209 * In this case, only one address can be defined, anything else will
210 * result in a DMA address error interrupt (at least on the SH-4),
211 * which will subsequently halt the transfer.
213 * Channel 2 on the Dreamcast is a special case, as this is used for
214 * cascading to the PVR2 DMAC. In this case, we still need to write
215 * SAR and DAR, regardless of value, in order for cascading to work.
217 if (chan
->sar
|| (mach_is_dreamcast() &&
218 chan
->chan
== PVR2_CASCADE_CHAN
))
219 __raw_writel(chan
->sar
, (dma_base_addr(chan
->chan
) + SAR
));
220 if (chan
->dar
|| (mach_is_dreamcast() &&
221 chan
->chan
== PVR2_CASCADE_CHAN
))
222 __raw_writel(chan
->dar
, (dma_base_addr(chan
->chan
) + DAR
));
224 __raw_writel(chan
->count
>> calc_xmit_shift(chan
),
225 (dma_base_addr(chan
->chan
) + TCR
));
227 sh_dmac_enable_dma(chan
);
232 static int sh_dmac_get_dma_residue(struct dma_channel
*chan
)
234 if (!(__raw_readl(dma_base_addr(chan
->chan
) + CHCR
) & CHCR_DE
))
237 return __raw_readl(dma_base_addr(chan
->chan
) + TCR
)
238 << calc_xmit_shift(chan
);
244 #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
245 defined(CONFIG_CPU_SUBTYPE_SH7724) || \
246 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
247 defined(CONFIG_CPU_SUBTYPE_SH7785)
254 * DMAOR bases are broken out amongst channel groups. DMAOR0 manages
255 * channels 0 - 5, DMAOR1 6 - 11 (optional).
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)
260 static inline int dmaor_reset(int no
)
262 unsigned long dmaor
= dmaor_read_reg(no
);
264 /* Try to clear the error flags first, incase they are set */
265 dmaor
&= ~(DMAOR_NMIF
| DMAOR_AE
);
266 dmaor_write_reg(no
, dmaor
);
269 dmaor_write_reg(no
, dmaor
);
271 /* See if we got an error again */
272 if ((dmaor_read_reg(no
) & (DMAOR_AE
| DMAOR_NMIF
))) {
273 printk(KERN_ERR
"dma-sh: Can't initialize DMAOR.\n");
283 #ifdef CONFIG_CPU_SH4
285 #if defined(DMAE1_IRQ)
291 static const char *dmae_name
[] = {
292 "DMAC Address Error0",
293 "DMAC Address Error1"
296 #ifdef CONFIG_SH_DMA_IRQ_MULTI
297 static inline unsigned int get_dma_error_irq(int n
)
299 return get_dmte_irq(n
* 6);
303 static unsigned int dmae_irq_map
[] = {
311 static inline unsigned int get_dma_error_irq(int n
)
313 return dmae_irq_map
[n
];
317 static irqreturn_t
dma_err(int irq
, void *dummy
)
321 for (i
= 0; i
< NR_DMAOR
; i
++)
329 static int dmae_irq_init(void)
333 for (n
= 0; n
< NR_DMAE
; n
++) {
334 int i
= request_irq(get_dma_error_irq(n
), dma_err
,
335 IRQF_SHARED
, dmae_name
[n
], (void *)dmae_name
[n
]);
336 if (unlikely(i
< 0)) {
337 printk(KERN_ERR
"%s request_irq fail\n", dmae_name
[n
]);
345 static void dmae_irq_free(void)
349 for (n
= 0; n
< NR_DMAE
; n
++)
350 free_irq(get_dma_error_irq(n
), NULL
);
353 static inline int dmae_irq_init(void)
358 static void dmae_irq_free(void)
363 static struct dma_ops sh_dmac_ops
= {
364 .request
= sh_dmac_request_dma
,
365 .free
= sh_dmac_free_dma
,
366 .get_residue
= sh_dmac_get_dma_residue
,
367 .xfer
= sh_dmac_xfer_dma
,
368 .configure
= sh_dmac_configure_channel
,
371 static struct dma_info sh_dmac_info
= {
373 .nr_channels
= CONFIG_NR_ONCHIP_DMA_CHANNELS
,
375 .flags
= DMAC_CHANNELS_TEI_CAPABLE
,
378 static int __init
sh_dmac_init(void)
380 struct dma_info
*info
= &sh_dmac_info
;
384 * Initialize DMAE, for parts that support it.
386 rc
= dmae_irq_init();
387 if (unlikely(rc
!= 0))
391 * Initialize DMAOR, and clean up any error flags that may have
394 for (i
= 0; i
< NR_DMAOR
; i
++) {
396 if (unlikely(rc
!= 0))
400 return register_dmac(info
);
403 static void __exit
sh_dmac_exit(void)
406 unregister_dmac(&sh_dmac_info
);
409 subsys_initcall(sh_dmac_init
);
410 module_exit(sh_dmac_exit
);
412 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
413 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
414 MODULE_LICENSE("GPL v2");