1 /*
2  * Freescale STMP37XX/STMP378X DMA helper interface
3  *
4  * Embedded Alley Solutions, Inc <source@embeddedalley.com>
5  *
6  * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
7  * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8  */
9 
10 /*
11  * The code contained herein is licensed under the GNU General Public
12  * License. You may obtain a copy of the GNU General Public License
13  * Version 2 or later at the following locations:
14  *
15  * http://www.opensource.org/licenses/gpl-license.html
16  * http://www.gnu.org/copyleft/gpl.html
17  */
18 #ifndef __ASM_PLAT_STMP3XXX_DMA_H
19 #define __ASM_PLAT_STMP3XXX_DMA_H
20 
21 #include <linux/platform_device.h>
22 #include <linux/dmapool.h>
23 
24 #if !defined(MAX_PIO_WORDS)
25 #define MAX_PIO_WORDS   (15)
26 #endif
27 
28 #define STMP3XXX_BUS_APBH		0
29 #define STMP3XXX_BUS_APBX		1
30 #define STMP3XXX_DMA_MAX_CHANNEL	16
31 #define STMP3XXX_DMA_BUS(dma)		((dma) / 16)
32 #define STMP3XXX_DMA_CHANNEL(dma)	((dma) % 16)
33 #define STMP3XXX_DMA(channel, bus)	((bus) * 16 + (channel))
34 #define MAX_DMA_ADDRESS			0xffffffff
35 #define MAX_DMA_CHANNELS		32
36 
37 struct stmp3xxx_dma_command {
38 	u32 next;
39 	u32 cmd;
40 	union {
41 		u32 buf_ptr;
42 		u32 alternate;
43 	};
44 	u32 pio_words[MAX_PIO_WORDS];
45 };
46 
47 struct stmp3xxx_dma_descriptor {
48 	struct stmp3xxx_dma_command *command;
49 	dma_addr_t handle;
50 
51 	/* The virtual address of the buffer pointer */
52 	void *virtual_buf_ptr;
53 	/* The next descriptor in a the DMA chain (optional) */
54 	struct stmp3xxx_dma_descriptor *next_descr;
55 };
56 
57 struct stmp37xx_circ_dma_chain {
58 	unsigned total_count;
59 	struct stmp3xxx_dma_descriptor *chain;
60 
61 	unsigned free_index;
62 	unsigned free_count;
63 	unsigned active_index;
64 	unsigned active_count;
65 	unsigned cooked_index;
66 	unsigned cooked_count;
67 
68 	int bus;
69 	unsigned channel;
70 };
71 
72 static inline struct stmp3xxx_dma_descriptor
stmp3xxx_dma_circ_get_free_head(struct stmp37xx_circ_dma_chain * chain)73     *stmp3xxx_dma_circ_get_free_head(struct stmp37xx_circ_dma_chain *chain)
74 {
75 	return &(chain->chain[chain->free_index]);
76 }
77 
78 static inline struct stmp3xxx_dma_descriptor
stmp3xxx_dma_circ_get_cooked_head(struct stmp37xx_circ_dma_chain * chain)79     *stmp3xxx_dma_circ_get_cooked_head(struct stmp37xx_circ_dma_chain *chain)
80 {
81 	return &(chain->chain[chain->cooked_index]);
82 }
83 
84 int stmp3xxx_dma_request(int ch, struct device *dev, const char *name);
85 int stmp3xxx_dma_release(int ch);
86 int stmp3xxx_dma_allocate_command(int ch,
87 				  struct stmp3xxx_dma_descriptor *descriptor);
88 int stmp3xxx_dma_free_command(int ch,
89 			      struct stmp3xxx_dma_descriptor *descriptor);
90 void stmp3xxx_dma_continue(int channel, u32 semaphore);
91 void stmp3xxx_dma_go(int ch, struct stmp3xxx_dma_descriptor *head,
92 		     u32 semaphore);
93 int stmp3xxx_dma_running(int ch);
94 int stmp3xxx_dma_make_chain(int ch, struct stmp37xx_circ_dma_chain *chain,
95 			    struct stmp3xxx_dma_descriptor descriptors[],
96 			    unsigned items);
97 void stmp3xxx_dma_free_chain(struct stmp37xx_circ_dma_chain *chain);
98 void stmp37xx_circ_clear_chain(struct stmp37xx_circ_dma_chain *chain);
99 void stmp37xx_circ_advance_free(struct stmp37xx_circ_dma_chain *chain,
100 		unsigned count);
101 void stmp37xx_circ_advance_active(struct stmp37xx_circ_dma_chain *chain,
102 		unsigned count);
103 unsigned stmp37xx_circ_advance_cooked(struct stmp37xx_circ_dma_chain *chain);
104 int stmp3xxx_dma_read_semaphore(int ch);
105 void stmp3xxx_dma_init(void);
106 void stmp3xxx_dma_set_alt_target(int ch, int target);
107 void stmp3xxx_dma_suspend(void);
108 void stmp3xxx_dma_resume(void);
109 
110 /*
111  * STMP37xx and STMP378x have different DMA control
112  * registers layout
113  */
114 
115 void stmp3xxx_arch_dma_freeze(int ch);
116 void stmp3xxx_arch_dma_unfreeze(int ch);
117 void stmp3xxx_arch_dma_reset_channel(int ch);
118 void stmp3xxx_arch_dma_enable_interrupt(int ch);
119 void stmp3xxx_arch_dma_clear_interrupt(int ch);
120 int stmp3xxx_arch_dma_is_interrupt(int ch);
121 
stmp3xxx_dma_reset_channel(int ch)122 static inline void stmp3xxx_dma_reset_channel(int ch)
123 {
124 	stmp3xxx_arch_dma_reset_channel(ch);
125 }
126 
127 
stmp3xxx_dma_freeze(int ch)128 static inline void stmp3xxx_dma_freeze(int ch)
129 {
130 	stmp3xxx_arch_dma_freeze(ch);
131 }
132 
stmp3xxx_dma_unfreeze(int ch)133 static inline void stmp3xxx_dma_unfreeze(int ch)
134 {
135 	stmp3xxx_arch_dma_unfreeze(ch);
136 }
137 
stmp3xxx_dma_enable_interrupt(int ch)138 static inline void stmp3xxx_dma_enable_interrupt(int ch)
139 {
140 	stmp3xxx_arch_dma_enable_interrupt(ch);
141 }
142 
stmp3xxx_dma_clear_interrupt(int ch)143 static inline void stmp3xxx_dma_clear_interrupt(int ch)
144 {
145 	stmp3xxx_arch_dma_clear_interrupt(ch);
146 }
147 
stmp3xxx_dma_is_interrupt(int ch)148 static inline int stmp3xxx_dma_is_interrupt(int ch)
149 {
150 	return stmp3xxx_arch_dma_is_interrupt(ch);
151 }
152 
153 #endif /* __ASM_PLAT_STMP3XXX_DMA_H */
154