Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * linux/drivers/mmc/tmio_mmc_dma.c
  3 *
  4 * Copyright (C) 2010-2011 Guennadi Liakhovetski
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 * DMA function for TMIO MMC implementations
 11 */
 12
 13#include <linux/device.h>
 14#include <linux/dma-mapping.h>
 15#include <linux/dmaengine.h>
 16#include <linux/mfd/tmio.h>
 17#include <linux/mmc/host.h>
 18#include <linux/mmc/tmio.h>
 19#include <linux/pagemap.h>
 20#include <linux/scatterlist.h>
 21
 22#include "tmio_mmc.h"
 23
 24#define TMIO_MMC_MIN_DMA_LEN 8
 25
 26void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
 27{
 28	if (!host->chan_tx || !host->chan_rx)
 29		return;
 30
 31#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
 32	/* Switch DMA mode on or off - SuperH specific? */
 33	sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
 34#endif
 
 
 
 
 
 
 
 
 
 
 35}
 36
 37static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
 38{
 39	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
 40	struct dma_async_tx_descriptor *desc = NULL;
 41	struct dma_chan *chan = host->chan_rx;
 42	struct tmio_mmc_data *pdata = host->pdata;
 43	dma_cookie_t cookie;
 44	int ret, i;
 45	bool aligned = true, multiple = true;
 46	unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
 47
 48	for_each_sg(sg, sg_tmp, host->sg_len, i) {
 49		if (sg_tmp->offset & align)
 50			aligned = false;
 51		if (sg_tmp->length & align) {
 52			multiple = false;
 53			break;
 54		}
 55	}
 56
 57	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
 58			  (align & PAGE_MASK))) || !multiple) {
 59		ret = -EINVAL;
 60		goto pio;
 61	}
 62
 63	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
 64		host->force_pio = true;
 65		return;
 66	}
 67
 68	tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
 69
 70	/* The only sg element can be unaligned, use our bounce buffer then */
 71	if (!aligned) {
 72		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
 73		host->sg_ptr = &host->bounce_sg;
 74		sg = host->sg_ptr;
 75	}
 76
 77	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
 78	if (ret > 0)
 79		desc = chan->device->device_prep_slave_sg(chan, sg, ret,
 80			DMA_FROM_DEVICE, DMA_CTRL_ACK);
 81
 82	if (desc) {
 83		cookie = dmaengine_submit(desc);
 84		if (cookie < 0) {
 85			desc = NULL;
 86			ret = cookie;
 87		}
 88	}
 89	dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
 90		__func__, host->sg_len, ret, cookie, host->mrq);
 91
 92pio:
 93	if (!desc) {
 94		/* DMA failed, fall back to PIO */
 
 95		if (ret >= 0)
 96			ret = -EIO;
 97		host->chan_rx = NULL;
 98		dma_release_channel(chan);
 99		/* Free the Tx channel too */
100		chan = host->chan_tx;
101		if (chan) {
102			host->chan_tx = NULL;
103			dma_release_channel(chan);
104		}
105		dev_warn(&host->pdev->dev,
106			 "DMA failed: %d, falling back to PIO\n", ret);
107		tmio_mmc_enable_dma(host, false);
108	}
109
110	dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
111		desc, cookie, host->sg_len);
112}
113
114static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
115{
116	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
117	struct dma_async_tx_descriptor *desc = NULL;
118	struct dma_chan *chan = host->chan_tx;
119	struct tmio_mmc_data *pdata = host->pdata;
120	dma_cookie_t cookie;
121	int ret, i;
122	bool aligned = true, multiple = true;
123	unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
124
125	for_each_sg(sg, sg_tmp, host->sg_len, i) {
126		if (sg_tmp->offset & align)
127			aligned = false;
128		if (sg_tmp->length & align) {
129			multiple = false;
130			break;
131		}
132	}
133
134	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
135			  (align & PAGE_MASK))) || !multiple) {
136		ret = -EINVAL;
137		goto pio;
138	}
139
140	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
141		host->force_pio = true;
142		return;
143	}
144
145	tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
146
147	/* The only sg element can be unaligned, use our bounce buffer then */
148	if (!aligned) {
149		unsigned long flags;
150		void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
151		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
152		memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
153		tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
154		host->sg_ptr = &host->bounce_sg;
155		sg = host->sg_ptr;
156	}
157
158	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
159	if (ret > 0)
160		desc = chan->device->device_prep_slave_sg(chan, sg, ret,
161			DMA_TO_DEVICE, DMA_CTRL_ACK);
162
163	if (desc) {
164		cookie = dmaengine_submit(desc);
165		if (cookie < 0) {
166			desc = NULL;
167			ret = cookie;
168		}
169	}
170	dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
171		__func__, host->sg_len, ret, cookie, host->mrq);
172
173pio:
174	if (!desc) {
175		/* DMA failed, fall back to PIO */
 
176		if (ret >= 0)
177			ret = -EIO;
178		host->chan_tx = NULL;
179		dma_release_channel(chan);
180		/* Free the Rx channel too */
181		chan = host->chan_rx;
182		if (chan) {
183			host->chan_rx = NULL;
184			dma_release_channel(chan);
185		}
186		dev_warn(&host->pdev->dev,
187			 "DMA failed: %d, falling back to PIO\n", ret);
188		tmio_mmc_enable_dma(host, false);
189	}
190
191	dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
192		desc, cookie);
193}
194
195void tmio_mmc_start_dma(struct tmio_mmc_host *host,
196			       struct mmc_data *data)
197{
198	if (data->flags & MMC_DATA_READ) {
199		if (host->chan_rx)
200			tmio_mmc_start_dma_rx(host);
201	} else {
202		if (host->chan_tx)
203			tmio_mmc_start_dma_tx(host);
204	}
205}
206
207static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
208{
209	struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
210	struct dma_chan *chan = NULL;
211
212	spin_lock_irq(&host->lock);
213
214	if (host && host->data) {
215		if (host->data->flags & MMC_DATA_READ)
216			chan = host->chan_rx;
217		else
218			chan = host->chan_tx;
219	}
220
221	spin_unlock_irq(&host->lock);
222
223	tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
224
225	if (chan)
226		dma_async_issue_pending(chan);
227}
228
229static void tmio_mmc_tasklet_fn(unsigned long arg)
230{
231	struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
232
233	spin_lock_irq(&host->lock);
234
235	if (!host->data)
236		goto out;
237
238	if (host->data->flags & MMC_DATA_READ)
239		dma_unmap_sg(host->chan_rx->device->dev,
240			     host->sg_ptr, host->sg_len,
241			     DMA_FROM_DEVICE);
242	else
243		dma_unmap_sg(host->chan_tx->device->dev,
244			     host->sg_ptr, host->sg_len,
245			     DMA_TO_DEVICE);
246
247	tmio_mmc_do_data_irq(host);
248out:
249	spin_unlock_irq(&host->lock);
250}
251
252/* It might be necessary to make filter MFD specific */
253static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
254{
255	dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
256	chan->private = arg;
257	return true;
258}
259
260void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
261{
262	/* We can only either use DMA for both Tx and Rx or not use it at all */
263	if (!pdata->dma)
 
264		return;
265
266	if (!host->chan_tx && !host->chan_rx) {
 
 
 
267		dma_cap_mask_t mask;
 
 
 
 
268
269		dma_cap_zero(mask);
270		dma_cap_set(DMA_SLAVE, mask);
271
272		host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
273						    pdata->dma->chan_priv_tx);
 
274		dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
275			host->chan_tx);
276
277		if (!host->chan_tx)
278			return;
279
280		host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
281						    pdata->dma->chan_priv_rx);
 
 
 
 
 
 
 
 
 
 
 
282		dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
283			host->chan_rx);
284
285		if (!host->chan_rx)
286			goto ereqrx;
287
 
 
 
 
 
 
 
 
 
 
288		host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
289		if (!host->bounce_buf)
290			goto ebouncebuf;
291
292		tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
293		tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
294	}
295
296	tmio_mmc_enable_dma(host, true);
297
298	return;
299
300ebouncebuf:
 
301	dma_release_channel(host->chan_rx);
302	host->chan_rx = NULL;
303ereqrx:
 
304	dma_release_channel(host->chan_tx);
305	host->chan_tx = NULL;
306}
307
308void tmio_mmc_release_dma(struct tmio_mmc_host *host)
309{
310	if (host->chan_tx) {
311		struct dma_chan *chan = host->chan_tx;
312		host->chan_tx = NULL;
313		dma_release_channel(chan);
314	}
315	if (host->chan_rx) {
316		struct dma_chan *chan = host->chan_rx;
317		host->chan_rx = NULL;
318		dma_release_channel(chan);
319	}
320	if (host->bounce_buf) {
321		free_pages((unsigned long)host->bounce_buf, 0);
322		host->bounce_buf = NULL;
323	}
324}
v4.10.11
  1/*
  2 * linux/drivers/mmc/tmio_mmc_dma.c
  3 *
  4 * Copyright (C) 2010-2011 Guennadi Liakhovetski
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 * DMA function for TMIO MMC implementations
 11 */
 12
 13#include <linux/device.h>
 14#include <linux/dma-mapping.h>
 15#include <linux/dmaengine.h>
 16#include <linux/mfd/tmio.h>
 17#include <linux/mmc/host.h>
 
 18#include <linux/pagemap.h>
 19#include <linux/scatterlist.h>
 20
 21#include "tmio_mmc.h"
 22
 23#define TMIO_MMC_MIN_DMA_LEN 8
 24
 25void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
 26{
 27	if (!host->chan_tx || !host->chan_rx)
 28		return;
 29
 30	if (host->dma->enable)
 31		host->dma->enable(host, enable);
 32}
 33
 34void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
 35{
 36	tmio_mmc_enable_dma(host, false);
 37
 38	if (host->chan_rx)
 39		dmaengine_terminate_all(host->chan_rx);
 40	if (host->chan_tx)
 41		dmaengine_terminate_all(host->chan_tx);
 42
 43	tmio_mmc_enable_dma(host, true);
 44}
 45
 46static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
 47{
 48	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
 49	struct dma_async_tx_descriptor *desc = NULL;
 50	struct dma_chan *chan = host->chan_rx;
 
 51	dma_cookie_t cookie;
 52	int ret, i;
 53	bool aligned = true, multiple = true;
 54	unsigned int align = (1 << host->pdata->alignment_shift) - 1;
 55
 56	for_each_sg(sg, sg_tmp, host->sg_len, i) {
 57		if (sg_tmp->offset & align)
 58			aligned = false;
 59		if (sg_tmp->length & align) {
 60			multiple = false;
 61			break;
 62		}
 63	}
 64
 65	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
 66			  (align & PAGE_MASK))) || !multiple) {
 67		ret = -EINVAL;
 68		goto pio;
 69	}
 70
 71	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
 72		host->force_pio = true;
 73		return;
 74	}
 75
 76	tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_RXRDY);
 77
 78	/* The only sg element can be unaligned, use our bounce buffer then */
 79	if (!aligned) {
 80		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
 81		host->sg_ptr = &host->bounce_sg;
 82		sg = host->sg_ptr;
 83	}
 84
 85	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
 86	if (ret > 0)
 87		desc = dmaengine_prep_slave_sg(chan, sg, ret,
 88			DMA_DEV_TO_MEM, DMA_CTRL_ACK);
 89
 90	if (desc) {
 91		cookie = dmaengine_submit(desc);
 92		if (cookie < 0) {
 93			desc = NULL;
 94			ret = cookie;
 95		}
 96	}
 
 
 
 97pio:
 98	if (!desc) {
 99		/* DMA failed, fall back to PIO */
100		tmio_mmc_enable_dma(host, false);
101		if (ret >= 0)
102			ret = -EIO;
103		host->chan_rx = NULL;
104		dma_release_channel(chan);
105		/* Free the Tx channel too */
106		chan = host->chan_tx;
107		if (chan) {
108			host->chan_tx = NULL;
109			dma_release_channel(chan);
110		}
111		dev_warn(&host->pdev->dev,
112			 "DMA failed: %d, falling back to PIO\n", ret);
 
113	}
 
 
 
114}
115
116static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
117{
118	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
119	struct dma_async_tx_descriptor *desc = NULL;
120	struct dma_chan *chan = host->chan_tx;
 
121	dma_cookie_t cookie;
122	int ret, i;
123	bool aligned = true, multiple = true;
124	unsigned int align = (1 << host->pdata->alignment_shift) - 1;
125
126	for_each_sg(sg, sg_tmp, host->sg_len, i) {
127		if (sg_tmp->offset & align)
128			aligned = false;
129		if (sg_tmp->length & align) {
130			multiple = false;
131			break;
132		}
133	}
134
135	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
136			  (align & PAGE_MASK))) || !multiple) {
137		ret = -EINVAL;
138		goto pio;
139	}
140
141	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
142		host->force_pio = true;
143		return;
144	}
145
146	tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_TXRQ);
147
148	/* The only sg element can be unaligned, use our bounce buffer then */
149	if (!aligned) {
150		unsigned long flags;
151		void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
152		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
153		memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
154		tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
155		host->sg_ptr = &host->bounce_sg;
156		sg = host->sg_ptr;
157	}
158
159	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
160	if (ret > 0)
161		desc = dmaengine_prep_slave_sg(chan, sg, ret,
162			DMA_MEM_TO_DEV, DMA_CTRL_ACK);
163
164	if (desc) {
165		cookie = dmaengine_submit(desc);
166		if (cookie < 0) {
167			desc = NULL;
168			ret = cookie;
169		}
170	}
 
 
 
171pio:
172	if (!desc) {
173		/* DMA failed, fall back to PIO */
174		tmio_mmc_enable_dma(host, false);
175		if (ret >= 0)
176			ret = -EIO;
177		host->chan_tx = NULL;
178		dma_release_channel(chan);
179		/* Free the Rx channel too */
180		chan = host->chan_rx;
181		if (chan) {
182			host->chan_rx = NULL;
183			dma_release_channel(chan);
184		}
185		dev_warn(&host->pdev->dev,
186			 "DMA failed: %d, falling back to PIO\n", ret);
 
187	}
 
 
 
188}
189
190void tmio_mmc_start_dma(struct tmio_mmc_host *host,
191			       struct mmc_data *data)
192{
193	if (data->flags & MMC_DATA_READ) {
194		if (host->chan_rx)
195			tmio_mmc_start_dma_rx(host);
196	} else {
197		if (host->chan_tx)
198			tmio_mmc_start_dma_tx(host);
199	}
200}
201
202static void tmio_mmc_issue_tasklet_fn(unsigned long priv)
203{
204	struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
205	struct dma_chan *chan = NULL;
206
207	spin_lock_irq(&host->lock);
208
209	if (host && host->data) {
210		if (host->data->flags & MMC_DATA_READ)
211			chan = host->chan_rx;
212		else
213			chan = host->chan_tx;
214	}
215
216	spin_unlock_irq(&host->lock);
217
218	tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
219
220	if (chan)
221		dma_async_issue_pending(chan);
222}
223
224static void tmio_mmc_tasklet_fn(unsigned long arg)
225{
226	struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
227
228	spin_lock_irq(&host->lock);
229
230	if (!host->data)
231		goto out;
232
233	if (host->data->flags & MMC_DATA_READ)
234		dma_unmap_sg(host->chan_rx->device->dev,
235			     host->sg_ptr, host->sg_len,
236			     DMA_FROM_DEVICE);
237	else
238		dma_unmap_sg(host->chan_tx->device->dev,
239			     host->sg_ptr, host->sg_len,
240			     DMA_TO_DEVICE);
241
242	tmio_mmc_do_data_irq(host);
243out:
244	spin_unlock_irq(&host->lock);
245}
246
 
 
 
 
 
 
 
 
247void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
248{
249	/* We can only either use DMA for both Tx and Rx or not use it at all */
250	if (!host->dma || (!host->pdev->dev.of_node &&
251		(!pdata->chan_priv_tx || !pdata->chan_priv_rx)))
252		return;
253
254	if (!host->chan_tx && !host->chan_rx) {
255		struct resource *res = platform_get_resource(host->pdev,
256							     IORESOURCE_MEM, 0);
257		struct dma_slave_config cfg = {};
258		dma_cap_mask_t mask;
259		int ret;
260
261		if (!res)
262			return;
263
264		dma_cap_zero(mask);
265		dma_cap_set(DMA_SLAVE, mask);
266
267		host->chan_tx = dma_request_slave_channel_compat(mask,
268					host->dma->filter, pdata->chan_priv_tx,
269					&host->pdev->dev, "tx");
270		dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
271			host->chan_tx);
272
273		if (!host->chan_tx)
274			return;
275
276		cfg.direction = DMA_MEM_TO_DEV;
277		cfg.dst_addr = res->start + (CTL_SD_DATA_PORT << host->bus_shift);
278		cfg.dst_addr_width = host->dma->dma_buswidth;
279		if (!cfg.dst_addr_width)
280			cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
281		cfg.src_addr = 0;
282		ret = dmaengine_slave_config(host->chan_tx, &cfg);
283		if (ret < 0)
284			goto ecfgtx;
285
286		host->chan_rx = dma_request_slave_channel_compat(mask,
287					host->dma->filter, pdata->chan_priv_rx,
288					&host->pdev->dev, "rx");
289		dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
290			host->chan_rx);
291
292		if (!host->chan_rx)
293			goto ereqrx;
294
295		cfg.direction = DMA_DEV_TO_MEM;
296		cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
297		cfg.src_addr_width = host->dma->dma_buswidth;
298		if (!cfg.src_addr_width)
299			cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
300		cfg.dst_addr = 0;
301		ret = dmaengine_slave_config(host->chan_rx, &cfg);
302		if (ret < 0)
303			goto ecfgrx;
304
305		host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
306		if (!host->bounce_buf)
307			goto ebouncebuf;
308
309		tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
310		tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
311	}
312
313	tmio_mmc_enable_dma(host, true);
314
315	return;
316
317ebouncebuf:
318ecfgrx:
319	dma_release_channel(host->chan_rx);
320	host->chan_rx = NULL;
321ereqrx:
322ecfgtx:
323	dma_release_channel(host->chan_tx);
324	host->chan_tx = NULL;
325}
326
327void tmio_mmc_release_dma(struct tmio_mmc_host *host)
328{
329	if (host->chan_tx) {
330		struct dma_chan *chan = host->chan_tx;
331		host->chan_tx = NULL;
332		dma_release_channel(chan);
333	}
334	if (host->chan_rx) {
335		struct dma_chan *chan = host->chan_rx;
336		host->chan_rx = NULL;
337		dma_release_channel(chan);
338	}
339	if (host->bounce_buf) {
340		free_pages((unsigned long)host->bounce_buf, 0);
341		host->bounce_buf = NULL;
342	}
343}