xref: /openbmc/linux/drivers/usb/musb/musb_dma.h (revision d4255ac3)
16814c73fSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
2550a7375SFelipe Balbi /*
3550a7375SFelipe Balbi  * MUSB OTG driver DMA controller abstraction
4550a7375SFelipe Balbi  *
5550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
6550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
7550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
8550a7375SFelipe Balbi  */
9550a7375SFelipe Balbi 
10550a7375SFelipe Balbi #ifndef __MUSB_DMA_H__
11550a7375SFelipe Balbi #define __MUSB_DMA_H__
12550a7375SFelipe Balbi 
13550a7375SFelipe Balbi struct musb_hw_ep;
14550a7375SFelipe Balbi 
15550a7375SFelipe Balbi /*
16550a7375SFelipe Balbi  * DMA Controller Abstraction
17550a7375SFelipe Balbi  *
18550a7375SFelipe Balbi  * DMA Controllers are abstracted to allow use of a variety of different
19550a7375SFelipe Balbi  * implementations of DMA, as allowed by the Inventra USB cores.  On the
20550a7375SFelipe Balbi  * host side, usbcore sets up the DMA mappings and flushes caches; on the
21550a7375SFelipe Balbi  * peripheral side, the gadget controller driver does.  Responsibilities
22550a7375SFelipe Balbi  * of a DMA controller driver include:
23550a7375SFelipe Balbi  *
24550a7375SFelipe Balbi  *  - Handling the details of moving multiple USB packets
25550a7375SFelipe Balbi  *    in cooperation with the Inventra USB core, including especially
26550a7375SFelipe Balbi  *    the correct RX side treatment of short packets and buffer-full
27550a7375SFelipe Balbi  *    states (both of which terminate transfers).
28550a7375SFelipe Balbi  *
29550a7375SFelipe Balbi  *  - Knowing the correlation between dma channels and the
30550a7375SFelipe Balbi  *    Inventra core's local endpoint resources and data direction.
31550a7375SFelipe Balbi  *
32550a7375SFelipe Balbi  *  - Maintaining a list of allocated/available channels.
33550a7375SFelipe Balbi  *
34550a7375SFelipe Balbi  *  - Updating channel status on interrupts,
35550a7375SFelipe Balbi  *    whether shared with the Inventra core or separate.
36550a7375SFelipe Balbi  */
37550a7375SFelipe Balbi 
38edce6177SMin Guo #define MUSB_HSDMA_BASE		0x200
39edce6177SMin Guo #define MUSB_HSDMA_INTR		(MUSB_HSDMA_BASE + 0)
40edce6177SMin Guo #define MUSB_HSDMA_CONTROL	0x4
41edce6177SMin Guo #define MUSB_HSDMA_ADDRESS	0x8
42edce6177SMin Guo #define MUSB_HSDMA_COUNT	0xc
43edce6177SMin Guo 
44550a7375SFelipe Balbi #define	DMA_ADDR_INVALID	(~(dma_addr_t)0)
45550a7375SFelipe Balbi 
46260eba39SSebastian Andrzej Siewior #ifdef CONFIG_MUSB_PIO_ONLY
47550a7375SFelipe Balbi #define	is_dma_capable()	(0)
48260eba39SSebastian Andrzej Siewior #else
49260eba39SSebastian Andrzej Siewior #define	is_dma_capable()	(1)
50550a7375SFelipe Balbi #endif
51550a7375SFelipe Balbi 
52f8e9f34fSTony Lindgren #ifdef CONFIG_USB_UX500_DMA
53dc8fca6cSBin Liu #define musb_dma_ux500(musb)		(musb->ops->quirks & MUSB_DMA_UX500)
54550a7375SFelipe Balbi #else
55f8e9f34fSTony Lindgren #define musb_dma_ux500(musb)		0
56f8e9f34fSTony Lindgren #endif
57f8e9f34fSTony Lindgren 
58f8e9f34fSTony Lindgren #ifdef CONFIG_USB_TI_CPPI41_DMA
59dc8fca6cSBin Liu #define musb_dma_cppi41(musb)		(musb->ops->quirks & MUSB_DMA_CPPI41)
60f8e9f34fSTony Lindgren #else
61f8e9f34fSTony Lindgren #define musb_dma_cppi41(musb)		0
62f8e9f34fSTony Lindgren #endif
63f8e9f34fSTony Lindgren 
64550a7375SFelipe Balbi #ifdef CONFIG_USB_TUSB_OMAP_DMA
65dc8fca6cSBin Liu #define tusb_dma_omap(musb)		(musb->ops->quirks & MUSB_DMA_TUSB_OMAP)
66550a7375SFelipe Balbi #else
67f8e9f34fSTony Lindgren #define tusb_dma_omap(musb)		0
68f8e9f34fSTony Lindgren #endif
69f8e9f34fSTony Lindgren 
70f8e9f34fSTony Lindgren #ifdef CONFIG_USB_INVENTRA_DMA
71dc8fca6cSBin Liu #define musb_dma_inventra(musb)		(musb->ops->quirks & MUSB_DMA_INVENTRA)
72f8e9f34fSTony Lindgren #else
73f8e9f34fSTony Lindgren #define musb_dma_inventra(musb)		0
74f8e9f34fSTony Lindgren #endif
75f8e9f34fSTony Lindgren 
76*d119cd95SLukas Bulwahn #if defined(CONFIG_USB_TI_CPPI41_DMA)
77*d119cd95SLukas Bulwahn #define	is_cppi_enabled(musb)		musb_dma_cppi41(musb)
78f8e9f34fSTony Lindgren #else
79f8e9f34fSTony Lindgren #define	is_cppi_enabled(musb)		0
80550a7375SFelipe Balbi #endif
81550a7375SFelipe Balbi 
82550a7375SFelipe Balbi /*
83550a7375SFelipe Balbi  * DMA channel status ... updated by the dma controller driver whenever that
84550a7375SFelipe Balbi  * status changes, and protected by the overall controller spinlock.
85550a7375SFelipe Balbi  */
86550a7375SFelipe Balbi enum dma_channel_status {
87550a7375SFelipe Balbi 	/* unallocated */
88550a7375SFelipe Balbi 	MUSB_DMA_STATUS_UNKNOWN,
89550a7375SFelipe Balbi 	/* allocated ... but not busy, no errors */
90550a7375SFelipe Balbi 	MUSB_DMA_STATUS_FREE,
91550a7375SFelipe Balbi 	/* busy ... transactions are active */
92550a7375SFelipe Balbi 	MUSB_DMA_STATUS_BUSY,
93550a7375SFelipe Balbi 	/* transaction(s) aborted due to ... dma or memory bus error */
94550a7375SFelipe Balbi 	MUSB_DMA_STATUS_BUS_ABORT,
95550a7375SFelipe Balbi 	/* transaction(s) aborted due to ... core error or USB fault */
96550a7375SFelipe Balbi 	MUSB_DMA_STATUS_CORE_ABORT
97550a7375SFelipe Balbi };
98550a7375SFelipe Balbi 
99550a7375SFelipe Balbi struct dma_controller;
100550a7375SFelipe Balbi 
101550a7375SFelipe Balbi /**
102550a7375SFelipe Balbi  * struct dma_channel - A DMA channel.
103550a7375SFelipe Balbi  * @private_data: channel-private data
104550a7375SFelipe Balbi  * @max_len: the maximum number of bytes the channel can move in one
105550a7375SFelipe Balbi  *	transaction (typically representing many USB maximum-sized packets)
106550a7375SFelipe Balbi  * @actual_len: how many bytes have been transferred
107550a7375SFelipe Balbi  * @status: current channel status (updated e.g. on interrupt)
108550a7375SFelipe Balbi  * @desired_mode: true if mode 1 is desired; false if mode 0 is desired
109550a7375SFelipe Balbi  *
110550a7375SFelipe Balbi  * channels are associated with an endpoint for the duration of at least
111550a7375SFelipe Balbi  * one usb transfer.
112550a7375SFelipe Balbi  */
113550a7375SFelipe Balbi struct dma_channel {
114550a7375SFelipe Balbi 	void			*private_data;
115550a7375SFelipe Balbi 	/* FIXME not void* private_data, but a dma_controller * */
116550a7375SFelipe Balbi 	size_t			max_len;
117550a7375SFelipe Balbi 	size_t			actual_len;
118550a7375SFelipe Balbi 	enum dma_channel_status	status;
119550a7375SFelipe Balbi 	bool			desired_mode;
120ff3fcac9SDaniel Mack 	bool			rx_packet_done;
121550a7375SFelipe Balbi };
122550a7375SFelipe Balbi 
123550a7375SFelipe Balbi /*
124550a7375SFelipe Balbi  * dma_channel_status - return status of dma channel
125550a7375SFelipe Balbi  * @c: the channel
126550a7375SFelipe Balbi  *
127550a7375SFelipe Balbi  * Returns the software's view of the channel status.  If that status is BUSY
128550a7375SFelipe Balbi  * then it's possible that the hardware has completed (or aborted) a transfer,
129550a7375SFelipe Balbi  * so the driver needs to update that status.
130550a7375SFelipe Balbi  */
131550a7375SFelipe Balbi static inline enum dma_channel_status
dma_channel_status(struct dma_channel * c)132550a7375SFelipe Balbi dma_channel_status(struct dma_channel *c)
133550a7375SFelipe Balbi {
134550a7375SFelipe Balbi 	return (is_dma_capable() && c) ? c->status : MUSB_DMA_STATUS_UNKNOWN;
135550a7375SFelipe Balbi }
136550a7375SFelipe Balbi 
137550a7375SFelipe Balbi /**
138550a7375SFelipe Balbi  * struct dma_controller - A DMA Controller.
139050dc900SAlexandre Bailon  * @musb: the usb controller
140550a7375SFelipe Balbi  * @start: call this to start a DMA controller;
141550a7375SFelipe Balbi  *	return 0 on success, else negative errno
142550a7375SFelipe Balbi  * @stop: call this to stop a DMA controller
143550a7375SFelipe Balbi  *	return 0 on success, else negative errno
144550a7375SFelipe Balbi  * @channel_alloc: call this to allocate a DMA channel
145550a7375SFelipe Balbi  * @channel_release: call this to release a DMA channel
146550a7375SFelipe Balbi  * @channel_abort: call this to abort a pending DMA transaction,
147550a7375SFelipe Balbi  *	returning it to FREE (but allocated) state
148050dc900SAlexandre Bailon  * @dma_callback: invoked on DMA completion, useful to run platform
149050dc900SAlexandre Bailon  *	code such IRQ acknowledgment.
150550a7375SFelipe Balbi  *
151550a7375SFelipe Balbi  * Controllers manage dma channels.
152550a7375SFelipe Balbi  */
153550a7375SFelipe Balbi struct dma_controller {
154050dc900SAlexandre Bailon 	struct musb *musb;
155550a7375SFelipe Balbi 	struct dma_channel	*(*channel_alloc)(struct dma_controller *,
156550a7375SFelipe Balbi 					struct musb_hw_ep *, u8 is_tx);
157550a7375SFelipe Balbi 	void			(*channel_release)(struct dma_channel *);
158550a7375SFelipe Balbi 	int			(*channel_program)(struct dma_channel *channel,
159550a7375SFelipe Balbi 							u16 maxpacket, u8 mode,
160550a7375SFelipe Balbi 							dma_addr_t dma_addr,
161550a7375SFelipe Balbi 							u32 length);
162550a7375SFelipe Balbi 	int			(*channel_abort)(struct dma_channel *);
1635f5761cbSMian Yousaf Kaukab 	int			(*is_compatible)(struct dma_channel *channel,
1645f5761cbSMian Yousaf Kaukab 							u16 maxpacket,
1655f5761cbSMian Yousaf Kaukab 							void *buf, u32 length);
166050dc900SAlexandre Bailon 	void			(*dma_callback)(struct dma_controller *);
167550a7375SFelipe Balbi };
168550a7375SFelipe Balbi 
169550a7375SFelipe Balbi /* called after channel_program(), may indicate a fault */
170550a7375SFelipe Balbi extern void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit);
171550a7375SFelipe Balbi 
172a6a20885SSebastian Andrzej Siewior #ifdef CONFIG_MUSB_PIO_ONLY
1737f6283edSTony Lindgren static inline struct dma_controller *
musb_dma_controller_create(struct musb * m,void __iomem * io)1747f6283edSTony Lindgren musb_dma_controller_create(struct musb *m, void __iomem *io)
175a6a20885SSebastian Andrzej Siewior {
176a6a20885SSebastian Andrzej Siewior 	return NULL;
177a6a20885SSebastian Andrzej Siewior }
178a6a20885SSebastian Andrzej Siewior 
musb_dma_controller_destroy(struct dma_controller * d)1797f6283edSTony Lindgren static inline void musb_dma_controller_destroy(struct dma_controller *d) { }
180a6a20885SSebastian Andrzej Siewior 
181a6a20885SSebastian Andrzej Siewior #else
182550a7375SFelipe Balbi 
1837f6283edSTony Lindgren extern struct dma_controller *
1847f6283edSTony Lindgren (*musb_dma_controller_create)(struct musb *, void __iomem *);
185550a7375SFelipe Balbi 
1867f6283edSTony Lindgren extern void (*musb_dma_controller_destroy)(struct dma_controller *);
187a6a20885SSebastian Andrzej Siewior #endif
188550a7375SFelipe Balbi 
1897f6283edSTony Lindgren /* Platform specific DMA functions */
1907f6283edSTony Lindgren extern struct dma_controller *
1917f6283edSTony Lindgren musbhs_dma_controller_create(struct musb *musb, void __iomem *base);
1927f6283edSTony Lindgren extern void musbhs_dma_controller_destroy(struct dma_controller *c);
193edce6177SMin Guo extern struct dma_controller *
194edce6177SMin Guo musbhs_dma_controller_create_noirq(struct musb *musb, void __iomem *base);
195edce6177SMin Guo extern irqreturn_t dma_controller_irq(int irq, void *private_data);
1967f6283edSTony Lindgren 
1977f6283edSTony Lindgren extern struct dma_controller *
1987f6283edSTony Lindgren tusb_dma_controller_create(struct musb *musb, void __iomem *base);
1997f6283edSTony Lindgren extern void tusb_dma_controller_destroy(struct dma_controller *c);
2007f6283edSTony Lindgren 
2017f6283edSTony Lindgren extern struct dma_controller *
2027f6283edSTony Lindgren cppi41_dma_controller_create(struct musb *musb, void __iomem *base);
2037f6283edSTony Lindgren extern void cppi41_dma_controller_destroy(struct dma_controller *c);
2047f6283edSTony Lindgren 
2057f6283edSTony Lindgren extern struct dma_controller *
2067f6283edSTony Lindgren ux500_dma_controller_create(struct musb *musb, void __iomem *base);
2077f6283edSTony Lindgren extern void ux500_dma_controller_destroy(struct dma_controller *c);
2087f6283edSTony Lindgren 
209550a7375SFelipe Balbi #endif	/* __MUSB_DMA_H__ */
210