1 // SPDX-License-Identifier: GPL-2.0
3 * MUSB OTG driver - support for Mentor's DMA controller
5 * Copyright 2005 Mentor Graphics Corporation
6 * Copyright (C) 2005-2007 by Texas Instruments
8 #include <linux/device.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include "musb_core.h"
14 #define MUSB_HSDMA_BASE 0x200
15 #define MUSB_HSDMA_INTR (MUSB_HSDMA_BASE + 0)
16 #define MUSB_HSDMA_CONTROL 0x4
17 #define MUSB_HSDMA_ADDRESS 0x8
18 #define MUSB_HSDMA_COUNT 0xc
20 #define MUSB_HSDMA_CHANNEL_OFFSET(_bchannel, _offset) \
21 (MUSB_HSDMA_BASE + (_bchannel << 4) + _offset)
23 #define musb_read_hsdma_addr(mbase, bchannel) \
25 MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS))
27 #define musb_write_hsdma_addr(mbase, bchannel, addr) \
29 MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS), \
32 #define musb_read_hsdma_count(mbase, bchannel) \
34 MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT))
36 #define musb_write_hsdma_count(mbase, bchannel, len) \
38 MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT), \
40 /* control register (16-bit): */
41 #define MUSB_HSDMA_ENABLE_SHIFT 0
42 #define MUSB_HSDMA_TRANSMIT_SHIFT 1
43 #define MUSB_HSDMA_MODE1_SHIFT 2
44 #define MUSB_HSDMA_IRQENABLE_SHIFT 3
45 #define MUSB_HSDMA_ENDPOINT_SHIFT 4
46 #define MUSB_HSDMA_BUSERROR_SHIFT 8
47 #define MUSB_HSDMA_BURSTMODE_SHIFT 9
48 #define MUSB_HSDMA_BURSTMODE (3 << MUSB_HSDMA_BURSTMODE_SHIFT)
49 #define MUSB_HSDMA_BURSTMODE_UNSPEC 0
50 #define MUSB_HSDMA_BURSTMODE_INCR4 1
51 #define MUSB_HSDMA_BURSTMODE_INCR8 2
52 #define MUSB_HSDMA_BURSTMODE_INCR16 3
54 #define MUSB_HSDMA_CHANNELS 8
56 struct musb_dma_controller
;
58 struct musb_dma_channel
{
59 struct dma_channel channel
;
60 struct musb_dma_controller
*controller
;
69 struct musb_dma_controller
{
70 struct dma_controller controller
;
71 struct musb_dma_channel channel
[MUSB_HSDMA_CHANNELS
];
79 static void dma_channel_release(struct dma_channel
*channel
);
81 static void dma_controller_stop(struct musb_dma_controller
*controller
)
83 struct musb
*musb
= controller
->private_data
;
84 struct dma_channel
*channel
;
87 if (controller
->used_channels
!= 0) {
88 dev_err(musb
->controller
,
89 "Stopping DMA controller while channel active\n");
91 for (bit
= 0; bit
< MUSB_HSDMA_CHANNELS
; bit
++) {
92 if (controller
->used_channels
& (1 << bit
)) {
93 channel
= &controller
->channel
[bit
].channel
;
94 dma_channel_release(channel
);
96 if (!controller
->used_channels
)
103 static struct dma_channel
*dma_channel_allocate(struct dma_controller
*c
,
104 struct musb_hw_ep
*hw_ep
, u8 transmit
)
106 struct musb_dma_controller
*controller
= container_of(c
,
107 struct musb_dma_controller
, controller
);
108 struct musb_dma_channel
*musb_channel
= NULL
;
109 struct dma_channel
*channel
= NULL
;
112 for (bit
= 0; bit
< MUSB_HSDMA_CHANNELS
; bit
++) {
113 if (!(controller
->used_channels
& (1 << bit
))) {
114 controller
->used_channels
|= (1 << bit
);
115 musb_channel
= &(controller
->channel
[bit
]);
116 musb_channel
->controller
= controller
;
117 musb_channel
->idx
= bit
;
118 musb_channel
->epnum
= hw_ep
->epnum
;
119 musb_channel
->transmit
= transmit
;
120 channel
= &(musb_channel
->channel
);
121 channel
->private_data
= musb_channel
;
122 channel
->status
= MUSB_DMA_STATUS_FREE
;
123 channel
->max_len
= 0x100000;
124 /* Tx => mode 1; Rx => mode 0 */
125 channel
->desired_mode
= transmit
;
126 channel
->actual_len
= 0;
134 static void dma_channel_release(struct dma_channel
*channel
)
136 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
138 channel
->actual_len
= 0;
139 musb_channel
->start_addr
= 0;
140 musb_channel
->len
= 0;
142 musb_channel
->controller
->used_channels
&=
143 ~(1 << musb_channel
->idx
);
145 channel
->status
= MUSB_DMA_STATUS_UNKNOWN
;
148 static void configure_channel(struct dma_channel
*channel
,
149 u16 packet_sz
, u8 mode
,
150 dma_addr_t dma_addr
, u32 len
)
152 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
153 struct musb_dma_controller
*controller
= musb_channel
->controller
;
154 struct musb
*musb
= controller
->private_data
;
155 void __iomem
*mbase
= controller
->base
;
156 u8 bchannel
= musb_channel
->idx
;
159 musb_dbg(musb
, "%p, pkt_sz %d, addr %pad, len %d, mode %d",
160 channel
, packet_sz
, &dma_addr
, len
, mode
);
163 csr
|= 1 << MUSB_HSDMA_MODE1_SHIFT
;
164 BUG_ON(len
< packet_sz
);
166 csr
|= MUSB_HSDMA_BURSTMODE_INCR16
167 << MUSB_HSDMA_BURSTMODE_SHIFT
;
169 csr
|= (musb_channel
->epnum
<< MUSB_HSDMA_ENDPOINT_SHIFT
)
170 | (1 << MUSB_HSDMA_ENABLE_SHIFT
)
171 | (1 << MUSB_HSDMA_IRQENABLE_SHIFT
)
172 | (musb_channel
->transmit
173 ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT
)
177 musb_write_hsdma_addr(mbase
, bchannel
, dma_addr
);
178 musb_write_hsdma_count(mbase
, bchannel
, len
);
180 /* control (this should start things) */
182 MUSB_HSDMA_CHANNEL_OFFSET(bchannel
, MUSB_HSDMA_CONTROL
),
186 static int dma_channel_program(struct dma_channel
*channel
,
187 u16 packet_sz
, u8 mode
,
188 dma_addr_t dma_addr
, u32 len
)
190 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
191 struct musb_dma_controller
*controller
= musb_channel
->controller
;
192 struct musb
*musb
= controller
->private_data
;
194 musb_dbg(musb
, "ep%d-%s pkt_sz %d, dma_addr %pad length %d, mode %d",
196 musb_channel
->transmit
? "Tx" : "Rx",
197 packet_sz
, &dma_addr
, len
, mode
);
199 BUG_ON(channel
->status
== MUSB_DMA_STATUS_UNKNOWN
||
200 channel
->status
== MUSB_DMA_STATUS_BUSY
);
203 * The DMA engine in RTL1.8 and above cannot handle
204 * DMA addresses that are not aligned to a 4 byte boundary.
205 * It ends up masking the last two bits of the address
206 * programmed in DMA_ADDR.
208 * Fail such DMA transfers, so that the backup PIO mode
209 * can carry out the transfer
211 if ((musb
->hwvers
>= MUSB_HWVERS_1800
) && (dma_addr
% 4))
214 channel
->actual_len
= 0;
215 musb_channel
->start_addr
= dma_addr
;
216 musb_channel
->len
= len
;
217 musb_channel
->max_packet_sz
= packet_sz
;
218 channel
->status
= MUSB_DMA_STATUS_BUSY
;
220 configure_channel(channel
, packet_sz
, mode
, dma_addr
, len
);
225 static int dma_channel_abort(struct dma_channel
*channel
)
227 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
228 void __iomem
*mbase
= musb_channel
->controller
->base
;
229 struct musb
*musb
= musb_channel
->controller
->private_data
;
231 u8 bchannel
= musb_channel
->idx
;
235 if (channel
->status
== MUSB_DMA_STATUS_BUSY
) {
236 if (musb_channel
->transmit
) {
237 offset
= musb
->io
.ep_offset(musb_channel
->epnum
,
241 * The programming guide says that we must clear
242 * the DMAENAB bit before the DMAMODE bit...
244 csr
= musb_readw(mbase
, offset
);
245 csr
&= ~(MUSB_TXCSR_AUTOSET
| MUSB_TXCSR_DMAENAB
);
246 musb_writew(mbase
, offset
, csr
);
247 csr
&= ~MUSB_TXCSR_DMAMODE
;
248 musb_writew(mbase
, offset
, csr
);
250 offset
= musb
->io
.ep_offset(musb_channel
->epnum
,
253 csr
= musb_readw(mbase
, offset
);
254 csr
&= ~(MUSB_RXCSR_AUTOCLEAR
|
257 musb_writew(mbase
, offset
, csr
);
261 MUSB_HSDMA_CHANNEL_OFFSET(bchannel
, MUSB_HSDMA_CONTROL
),
263 musb_write_hsdma_addr(mbase
, bchannel
, 0);
264 musb_write_hsdma_count(mbase
, bchannel
, 0);
265 channel
->status
= MUSB_DMA_STATUS_FREE
;
271 static irqreturn_t
dma_controller_irq(int irq
, void *private_data
)
273 struct musb_dma_controller
*controller
= private_data
;
274 struct musb
*musb
= controller
->private_data
;
275 struct musb_dma_channel
*musb_channel
;
276 struct dma_channel
*channel
;
278 void __iomem
*mbase
= controller
->base
;
280 irqreturn_t retval
= IRQ_NONE
;
290 spin_lock_irqsave(&musb
->lock
, flags
);
292 int_hsdma
= musb_readb(mbase
, MUSB_HSDMA_INTR
);
295 musb_dbg(musb
, "spurious DMA irq");
297 for (bchannel
= 0; bchannel
< MUSB_HSDMA_CHANNELS
; bchannel
++) {
298 musb_channel
= (struct musb_dma_channel
*)
299 &(controller
->channel
[bchannel
]);
300 channel
= &musb_channel
->channel
;
301 if (channel
->status
== MUSB_DMA_STATUS_BUSY
) {
302 count
= musb_read_hsdma_count(mbase
, bchannel
);
305 int_hsdma
|= (1 << bchannel
);
309 musb_dbg(musb
, "int_hsdma = 0x%x", int_hsdma
);
315 for (bchannel
= 0; bchannel
< MUSB_HSDMA_CHANNELS
; bchannel
++) {
316 if (int_hsdma
& (1 << bchannel
)) {
317 musb_channel
= (struct musb_dma_channel
*)
318 &(controller
->channel
[bchannel
]);
319 channel
= &musb_channel
->channel
;
321 csr
= musb_readw(mbase
,
322 MUSB_HSDMA_CHANNEL_OFFSET(bchannel
,
323 MUSB_HSDMA_CONTROL
));
325 if (csr
& (1 << MUSB_HSDMA_BUSERROR_SHIFT
)) {
326 musb_channel
->channel
.status
=
327 MUSB_DMA_STATUS_BUS_ABORT
;
331 addr
= musb_read_hsdma_addr(mbase
,
333 channel
->actual_len
= addr
334 - musb_channel
->start_addr
;
336 musb_dbg(musb
, "ch %p, 0x%x -> 0x%x (%zu / %d) %s",
337 channel
, musb_channel
->start_addr
,
338 addr
, channel
->actual_len
,
341 < musb_channel
->len
) ?
342 "=> reconfig 0" : "=> complete");
344 devctl
= musb_readb(mbase
, MUSB_DEVCTL
);
346 channel
->status
= MUSB_DMA_STATUS_FREE
;
349 if (musb_channel
->transmit
&&
350 (!channel
->desired_mode
||
351 (channel
->actual_len
%
352 musb_channel
->max_packet_sz
))) {
353 u8 epnum
= musb_channel
->epnum
;
354 int offset
= musb
->io
.ep_offset(epnum
,
359 * The programming guide says that we
360 * must clear DMAENAB before DMAMODE.
362 musb_ep_select(mbase
, epnum
);
363 txcsr
= musb_readw(mbase
, offset
);
364 if (channel
->desired_mode
== 1) {
365 txcsr
&= ~(MUSB_TXCSR_DMAENAB
366 | MUSB_TXCSR_AUTOSET
);
367 musb_writew(mbase
, offset
, txcsr
);
368 /* Send out the packet */
369 txcsr
&= ~MUSB_TXCSR_DMAMODE
;
370 txcsr
|= MUSB_TXCSR_DMAENAB
;
372 txcsr
|= MUSB_TXCSR_TXPKTRDY
;
373 musb_writew(mbase
, offset
, txcsr
);
375 musb_dma_completion(musb
, musb_channel
->epnum
,
376 musb_channel
->transmit
);
381 retval
= IRQ_HANDLED
;
383 spin_unlock_irqrestore(&musb
->lock
, flags
);
387 void musbhs_dma_controller_destroy(struct dma_controller
*c
)
389 struct musb_dma_controller
*controller
= container_of(c
,
390 struct musb_dma_controller
, controller
);
392 dma_controller_stop(controller
);
395 free_irq(controller
->irq
, c
);
399 EXPORT_SYMBOL_GPL(musbhs_dma_controller_destroy
);
401 struct dma_controller
*musbhs_dma_controller_create(struct musb
*musb
,
404 struct musb_dma_controller
*controller
;
405 struct device
*dev
= musb
->controller
;
406 struct platform_device
*pdev
= to_platform_device(dev
);
407 int irq
= platform_get_irq_byname(pdev
, "dma");
410 dev_err(dev
, "No DMA interrupt line!\n");
414 controller
= kzalloc(sizeof(*controller
), GFP_KERNEL
);
418 controller
->channel_count
= MUSB_HSDMA_CHANNELS
;
419 controller
->private_data
= musb
;
420 controller
->base
= base
;
422 controller
->controller
.channel_alloc
= dma_channel_allocate
;
423 controller
->controller
.channel_release
= dma_channel_release
;
424 controller
->controller
.channel_program
= dma_channel_program
;
425 controller
->controller
.channel_abort
= dma_channel_abort
;
427 if (request_irq(irq
, dma_controller_irq
, 0,
428 dev_name(musb
->controller
), &controller
->controller
)) {
429 dev_err(dev
, "request_irq %d failed!\n", irq
);
430 musb_dma_controller_destroy(&controller
->controller
);
435 controller
->irq
= irq
;
437 return &controller
->controller
;
439 EXPORT_SYMBOL_GPL(musbhs_dma_controller_create
);