Commit | Line | Data |
---|---|---|
8dcc8f72 MYK |
1 | /* |
2 | * drivers/usb/musb/ux500_dma.c | |
3 | * | |
4 | * U8500 and U5500 DMA support code | |
5 | * | |
6 | * Copyright (C) 2009 STMicroelectronics | |
7 | * Copyright (C) 2011 ST-Ericsson SA | |
8 | * Authors: | |
9 | * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com> | |
10 | * Praveena Nadahally <praveen.nadahally@stericsson.com> | |
11 | * Rajaram Regupathy <ragupathy.rajaram@stericsson.com> | |
12 | * | |
13 | * This program is free software: you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation, either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | */ | |
26 | ||
27 | #include <linux/device.h> | |
28 | #include <linux/interrupt.h> | |
29 | #include <linux/platform_device.h> | |
30 | #include <linux/dma-mapping.h> | |
31 | #include <linux/dmaengine.h> | |
32 | #include <linux/pfn.h> | |
33 | #include <mach/usb.h> | |
34 | #include "musb_core.h" | |
35 | ||
36 | struct ux500_dma_channel { | |
37 | struct dma_channel channel; | |
38 | struct ux500_dma_controller *controller; | |
39 | struct musb_hw_ep *hw_ep; | |
40 | struct work_struct channel_work; | |
41 | struct dma_chan *dma_chan; | |
42 | unsigned int cur_len; | |
43 | dma_cookie_t cookie; | |
44 | u8 ch_num; | |
45 | u8 is_tx; | |
46 | u8 is_allocated; | |
47 | }; | |
48 | ||
49 | struct ux500_dma_controller { | |
50 | struct dma_controller controller; | |
51 | struct ux500_dma_channel rx_channel[UX500_MUSB_DMA_NUM_RX_CHANNELS]; | |
52 | struct ux500_dma_channel tx_channel[UX500_MUSB_DMA_NUM_TX_CHANNELS]; | |
53 | u32 num_rx_channels; | |
54 | u32 num_tx_channels; | |
55 | void *private_data; | |
56 | dma_addr_t phy_base; | |
57 | }; | |
58 | ||
59 | /* Work function invoked from DMA callback to handle tx transfers. */ | |
60 | static void ux500_tx_work(struct work_struct *data) | |
61 | { | |
62 | struct ux500_dma_channel *ux500_channel = container_of(data, | |
63 | struct ux500_dma_channel, channel_work); | |
64 | struct musb_hw_ep *hw_ep = ux500_channel->hw_ep; | |
65 | struct musb *musb = hw_ep->musb; | |
66 | unsigned long flags; | |
67 | ||
68 | DBG(4, "DMA tx transfer done on hw_ep=%d\n", hw_ep->epnum); | |
69 | ||
70 | spin_lock_irqsave(&musb->lock, flags); | |
71 | ux500_channel->channel.actual_len = ux500_channel->cur_len; | |
72 | ux500_channel->channel.status = MUSB_DMA_STATUS_FREE; | |
73 | musb_dma_completion(musb, hw_ep->epnum, | |
74 | ux500_channel->is_tx); | |
75 | spin_unlock_irqrestore(&musb->lock, flags); | |
76 | } | |
77 | ||
78 | /* Work function invoked from DMA callback to handle rx transfers. */ | |
79 | static void ux500_rx_work(struct work_struct *data) | |
80 | { | |
81 | struct ux500_dma_channel *ux500_channel = container_of(data, | |
82 | struct ux500_dma_channel, channel_work); | |
83 | struct musb_hw_ep *hw_ep = ux500_channel->hw_ep; | |
84 | struct musb *musb = hw_ep->musb; | |
85 | unsigned long flags; | |
86 | ||
87 | DBG(4, "DMA rx transfer done on hw_ep=%d\n", hw_ep->epnum); | |
88 | ||
89 | spin_lock_irqsave(&musb->lock, flags); | |
90 | ux500_channel->channel.actual_len = ux500_channel->cur_len; | |
91 | ux500_channel->channel.status = MUSB_DMA_STATUS_FREE; | |
92 | musb_dma_completion(musb, hw_ep->epnum, | |
93 | ux500_channel->is_tx); | |
94 | spin_unlock_irqrestore(&musb->lock, flags); | |
95 | } | |
96 | ||
97 | void ux500_dma_callback(void *private_data) | |
98 | { | |
99 | struct dma_channel *channel = (struct dma_channel *)private_data; | |
100 | struct ux500_dma_channel *ux500_channel = channel->private_data; | |
101 | ||
102 | schedule_work(&ux500_channel->channel_work); | |
103 | } | |
104 | ||
105 | static bool ux500_configure_channel(struct dma_channel *channel, | |
106 | u16 packet_sz, u8 mode, | |
107 | dma_addr_t dma_addr, u32 len) | |
108 | { | |
109 | struct ux500_dma_channel *ux500_channel = channel->private_data; | |
110 | struct musb_hw_ep *hw_ep = ux500_channel->hw_ep; | |
111 | struct dma_chan *dma_chan = ux500_channel->dma_chan; | |
112 | struct dma_async_tx_descriptor *dma_desc; | |
113 | enum dma_data_direction direction; | |
114 | struct scatterlist sg; | |
115 | struct dma_slave_config slave_conf; | |
116 | enum dma_slave_buswidth addr_width; | |
117 | dma_addr_t usb_fifo_addr = (MUSB_FIFO_OFFSET(hw_ep->epnum) + | |
118 | ux500_channel->controller->phy_base); | |
119 | ||
120 | DBG(4, "packet_sz=%d, mode=%d, dma_addr=0x%x, len=%d is_tx=%d\n", | |
121 | packet_sz, mode, dma_addr, len, ux500_channel->is_tx); | |
122 | ||
123 | ux500_channel->cur_len = len; | |
124 | ||
125 | sg_init_table(&sg, 1); | |
126 | sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_addr)), len, | |
127 | offset_in_page(dma_addr)); | |
128 | sg_dma_address(&sg) = dma_addr; | |
129 | sg_dma_len(&sg) = len; | |
130 | ||
131 | direction = ux500_channel->is_tx ? DMA_TO_DEVICE : DMA_FROM_DEVICE; | |
132 | addr_width = (len & 0x3) ? DMA_SLAVE_BUSWIDTH_1_BYTE : | |
133 | DMA_SLAVE_BUSWIDTH_4_BYTES; | |
134 | ||
135 | slave_conf.direction = direction; | |
d366d39b PF |
136 | slave_conf.src_addr = usb_fifo_addr; |
137 | slave_conf.src_addr_width = addr_width; | |
138 | slave_conf.src_maxburst = 16; | |
139 | slave_conf.dst_addr = usb_fifo_addr; | |
140 | slave_conf.dst_addr_width = addr_width; | |
141 | slave_conf.dst_maxburst = 16; | |
142 | ||
8dcc8f72 MYK |
143 | dma_chan->device->device_control(dma_chan, DMA_SLAVE_CONFIG, |
144 | (unsigned long) &slave_conf); | |
145 | ||
146 | dma_desc = dma_chan->device-> | |
147 | device_prep_slave_sg(dma_chan, &sg, 1, direction, | |
148 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
149 | if (!dma_desc) | |
150 | return false; | |
151 | ||
152 | dma_desc->callback = ux500_dma_callback; | |
153 | dma_desc->callback_param = channel; | |
154 | ux500_channel->cookie = dma_desc->tx_submit(dma_desc); | |
155 | ||
156 | dma_async_issue_pending(dma_chan); | |
157 | ||
158 | return true; | |
159 | } | |
160 | ||
161 | static struct dma_channel *ux500_dma_channel_allocate(struct dma_controller *c, | |
162 | struct musb_hw_ep *hw_ep, u8 is_tx) | |
163 | { | |
164 | struct ux500_dma_controller *controller = container_of(c, | |
165 | struct ux500_dma_controller, controller); | |
166 | struct ux500_dma_channel *ux500_channel = NULL; | |
167 | u8 ch_num = hw_ep->epnum - 1; | |
168 | u32 max_ch; | |
169 | ||
170 | /* Max 8 DMA channels (0 - 7). Each DMA channel can only be allocated | |
171 | * to specified hw_ep. For example DMA channel 0 can only be allocated | |
172 | * to hw_ep 1 and 9. | |
173 | */ | |
174 | if (ch_num > 7) | |
175 | ch_num -= 8; | |
176 | ||
177 | max_ch = is_tx ? controller->num_tx_channels : | |
178 | controller->num_rx_channels; | |
179 | ||
180 | if (ch_num >= max_ch) | |
181 | return NULL; | |
182 | ||
183 | ux500_channel = is_tx ? &(controller->tx_channel[ch_num]) : | |
184 | &(controller->rx_channel[ch_num]) ; | |
185 | ||
186 | /* Check if channel is already used. */ | |
187 | if (ux500_channel->is_allocated) | |
188 | return NULL; | |
189 | ||
190 | ux500_channel->hw_ep = hw_ep; | |
191 | ux500_channel->is_allocated = 1; | |
192 | ||
193 | DBG(7, "hw_ep=%d, is_tx=0x%x, channel=%d\n", | |
194 | hw_ep->epnum, is_tx, ch_num); | |
195 | ||
196 | return &(ux500_channel->channel); | |
197 | } | |
198 | ||
199 | static void ux500_dma_channel_release(struct dma_channel *channel) | |
200 | { | |
201 | struct ux500_dma_channel *ux500_channel = channel->private_data; | |
202 | ||
203 | DBG(7, "channel=%d\n", ux500_channel->ch_num); | |
204 | ||
205 | if (ux500_channel->is_allocated) { | |
206 | ux500_channel->is_allocated = 0; | |
207 | channel->status = MUSB_DMA_STATUS_FREE; | |
208 | channel->actual_len = 0; | |
209 | } | |
210 | } | |
211 | ||
212 | static int ux500_dma_is_compatible(struct dma_channel *channel, | |
213 | u16 maxpacket, void *buf, u32 length) | |
214 | { | |
215 | if ((maxpacket & 0x3) || | |
216 | ((int)buf & 0x3) || | |
217 | (length < 512) || | |
218 | (length & 0x3)) | |
219 | return false; | |
220 | else | |
221 | return true; | |
222 | } | |
223 | ||
224 | static int ux500_dma_channel_program(struct dma_channel *channel, | |
225 | u16 packet_sz, u8 mode, | |
226 | dma_addr_t dma_addr, u32 len) | |
227 | { | |
228 | int ret; | |
229 | ||
230 | BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN || | |
231 | channel->status == MUSB_DMA_STATUS_BUSY); | |
232 | ||
233 | if (!ux500_dma_is_compatible(channel, packet_sz, (void *)dma_addr, len)) | |
234 | return false; | |
235 | ||
236 | channel->status = MUSB_DMA_STATUS_BUSY; | |
237 | channel->actual_len = 0; | |
238 | ret = ux500_configure_channel(channel, packet_sz, mode, dma_addr, len); | |
239 | if (!ret) | |
240 | channel->status = MUSB_DMA_STATUS_FREE; | |
241 | ||
242 | return ret; | |
243 | } | |
244 | ||
245 | static int ux500_dma_channel_abort(struct dma_channel *channel) | |
246 | { | |
247 | struct ux500_dma_channel *ux500_channel = channel->private_data; | |
248 | struct ux500_dma_controller *controller = ux500_channel->controller; | |
249 | struct musb *musb = controller->private_data; | |
250 | void __iomem *epio = musb->endpoints[ux500_channel->hw_ep->epnum].regs; | |
251 | u16 csr; | |
252 | ||
253 | DBG(4, "channel=%d, is_tx=%d\n", ux500_channel->ch_num, | |
254 | ux500_channel->is_tx); | |
255 | ||
256 | if (channel->status == MUSB_DMA_STATUS_BUSY) { | |
257 | if (ux500_channel->is_tx) { | |
258 | csr = musb_readw(epio, MUSB_TXCSR); | |
259 | csr &= ~(MUSB_TXCSR_AUTOSET | | |
260 | MUSB_TXCSR_DMAENAB | | |
261 | MUSB_TXCSR_DMAMODE); | |
262 | musb_writew(epio, MUSB_TXCSR, csr); | |
263 | } else { | |
264 | csr = musb_readw(epio, MUSB_RXCSR); | |
265 | csr &= ~(MUSB_RXCSR_AUTOCLEAR | | |
266 | MUSB_RXCSR_DMAENAB | | |
267 | MUSB_RXCSR_DMAMODE); | |
268 | musb_writew(epio, MUSB_RXCSR, csr); | |
269 | } | |
270 | ||
271 | ux500_channel->dma_chan->device-> | |
272 | device_control(ux500_channel->dma_chan, | |
273 | DMA_TERMINATE_ALL, 0); | |
274 | channel->status = MUSB_DMA_STATUS_FREE; | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | static int ux500_dma_controller_stop(struct dma_controller *c) | |
280 | { | |
281 | struct ux500_dma_controller *controller = container_of(c, | |
282 | struct ux500_dma_controller, controller); | |
283 | struct ux500_dma_channel *ux500_channel; | |
284 | struct dma_channel *channel; | |
285 | u8 ch_num; | |
286 | ||
287 | for (ch_num = 0; ch_num < controller->num_rx_channels; ch_num++) { | |
288 | channel = &controller->rx_channel[ch_num].channel; | |
289 | ux500_channel = channel->private_data; | |
290 | ||
291 | ux500_dma_channel_release(channel); | |
292 | ||
293 | if (ux500_channel->dma_chan) | |
294 | dma_release_channel(ux500_channel->dma_chan); | |
295 | } | |
296 | ||
297 | for (ch_num = 0; ch_num < controller->num_tx_channels; ch_num++) { | |
298 | channel = &controller->tx_channel[ch_num].channel; | |
299 | ux500_channel = channel->private_data; | |
300 | ||
301 | ux500_dma_channel_release(channel); | |
302 | ||
303 | if (ux500_channel->dma_chan) | |
304 | dma_release_channel(ux500_channel->dma_chan); | |
305 | } | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static int ux500_dma_controller_start(struct dma_controller *c) | |
311 | { | |
312 | struct ux500_dma_controller *controller = container_of(c, | |
313 | struct ux500_dma_controller, controller); | |
314 | struct ux500_dma_channel *ux500_channel = NULL; | |
315 | struct musb *musb = controller->private_data; | |
316 | struct device *dev = musb->controller; | |
317 | struct musb_hdrc_platform_data *plat = dev->platform_data; | |
318 | struct ux500_musb_board_data *data = plat->board_data; | |
319 | struct dma_channel *dma_channel = NULL; | |
320 | u32 ch_num; | |
321 | u8 dir; | |
322 | u8 is_tx = 0; | |
323 | ||
324 | void **param_array; | |
325 | struct ux500_dma_channel *channel_array; | |
326 | u32 ch_count; | |
327 | void (*musb_channel_work)(struct work_struct *); | |
328 | dma_cap_mask_t mask; | |
329 | ||
330 | if ((data->num_rx_channels > UX500_MUSB_DMA_NUM_RX_CHANNELS) || | |
331 | (data->num_tx_channels > UX500_MUSB_DMA_NUM_TX_CHANNELS)) | |
332 | return -EINVAL; | |
333 | ||
334 | controller->num_rx_channels = data->num_rx_channels; | |
335 | controller->num_tx_channels = data->num_tx_channels; | |
336 | ||
337 | dma_cap_zero(mask); | |
338 | dma_cap_set(DMA_SLAVE, mask); | |
339 | ||
340 | /* Prepare the loop for RX channels */ | |
341 | channel_array = controller->rx_channel; | |
342 | ch_count = data->num_rx_channels; | |
343 | param_array = data->dma_rx_param_array; | |
344 | musb_channel_work = ux500_rx_work; | |
345 | ||
346 | for (dir = 0; dir < 2; dir++) { | |
347 | for (ch_num = 0; ch_num < ch_count; ch_num++) { | |
348 | ux500_channel = &channel_array[ch_num]; | |
349 | ux500_channel->controller = controller; | |
350 | ux500_channel->ch_num = ch_num; | |
351 | ux500_channel->is_tx = is_tx; | |
352 | ||
353 | dma_channel = &(ux500_channel->channel); | |
354 | dma_channel->private_data = ux500_channel; | |
355 | dma_channel->status = MUSB_DMA_STATUS_FREE; | |
356 | dma_channel->max_len = SZ_16M; | |
357 | ||
358 | ux500_channel->dma_chan = dma_request_channel(mask, | |
359 | data->dma_filter, | |
360 | param_array[ch_num]); | |
361 | if (!ux500_channel->dma_chan) { | |
362 | ERR("Dma pipe allocation error dir=%d ch=%d\n", | |
363 | dir, ch_num); | |
364 | ||
365 | /* Release already allocated channels */ | |
366 | ux500_dma_controller_stop(c); | |
367 | ||
368 | return -EBUSY; | |
369 | } | |
370 | ||
371 | INIT_WORK(&ux500_channel->channel_work, | |
372 | musb_channel_work); | |
373 | } | |
374 | ||
375 | /* Prepare the loop for TX channels */ | |
376 | channel_array = controller->tx_channel; | |
377 | ch_count = data->num_tx_channels; | |
378 | param_array = data->dma_tx_param_array; | |
379 | musb_channel_work = ux500_tx_work; | |
380 | is_tx = 1; | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | void dma_controller_destroy(struct dma_controller *c) | |
387 | { | |
388 | struct ux500_dma_controller *controller = container_of(c, | |
389 | struct ux500_dma_controller, controller); | |
390 | ||
391 | kfree(controller); | |
392 | } | |
393 | ||
394 | struct dma_controller *__init | |
395 | dma_controller_create(struct musb *musb, void __iomem *base) | |
396 | { | |
397 | struct ux500_dma_controller *controller; | |
398 | struct platform_device *pdev = to_platform_device(musb->controller); | |
399 | struct resource *iomem; | |
400 | ||
401 | controller = kzalloc(sizeof(*controller), GFP_KERNEL); | |
402 | if (!controller) | |
403 | return NULL; | |
404 | ||
405 | controller->private_data = musb; | |
406 | ||
407 | /* Save physical address for DMA controller. */ | |
408 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
409 | controller->phy_base = (dma_addr_t) iomem->start; | |
410 | ||
411 | controller->controller.start = ux500_dma_controller_start; | |
412 | controller->controller.stop = ux500_dma_controller_stop; | |
413 | controller->controller.channel_alloc = ux500_dma_channel_allocate; | |
414 | controller->controller.channel_release = ux500_dma_channel_release; | |
415 | controller->controller.channel_program = ux500_dma_channel_program; | |
416 | controller->controller.channel_abort = ux500_dma_channel_abort; | |
417 | controller->controller.is_compatible = ux500_dma_is_compatible; | |
418 | ||
419 | return &controller->controller; | |
420 | } |