SDIO驅動(16)使用DMA傳輸數據2

DMA控制器驅動框架中的第二個函數:

s3c24xx_dma_order_set(&s3c2440_dma_order);
參數s3c2440_dma_order是一個全局變量,抽象的是下圖物理channel和邏輯channel及其互相關係:

static struct s3c24xx_dma_order __initdata s3c2440_dma_order = {
	.channels	= {
		[DMACH_SDI]	= {
			.list	= {
				[0]	= 3 | DMA_CH_VALID,
				[1]	= 2 | DMA_CH_VALID,
				[2]	= 1 | DMA_CH_VALID,
				[3]	= 0 | DMA_CH_VALID,
			},
		},
		[DMACH_I2S_IN]	= {
			.list	= {
				[0]	= 1 | DMA_CH_VALID,
				[1]	= 2 | DMA_CH_VALID,
			},
		},
		......
	},
};
對於SDIO/MMC/SD,其DMA源有4個(上圖紅框),位於物理channel的0~3通道;同樣,I2SSDI的DMA源有2個,位於通道0、1等等。

再來看s3c24xx_dma_order_set()函數:

int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
{
	struct s3c24xx_dma_order *nord = dma_order;

	if (nord == NULL)
		nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);

	if (nord == NULL) {
		printk(KERN_ERR "no memory to store dma channel order\n");
		return -ENOMEM;
	}

	dma_order = nord;
	memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
	return 0;
}
還是很簡單:爲全局變量dma_order分配空間並把s3c2440_dma_order的內容copy過去,何必多此一舉?

最後一個函數:

s3c24xx_dma_init_map(&s3c2440_dma_sel); 

int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
{
	struct s3c24xx_dma_map *nmap;
	size_t map_sz = sizeof(*nmap) * sel->map_size;
	int ptr;

	nmap = kmalloc(map_sz, GFP_KERNEL);
	if (nmap == NULL)
		return -ENOMEM;

	memcpy(nmap, sel->map, map_sz);
	memcpy(&dma_sel, sel, sizeof(*sel));

	dma_sel.map = nmap;

	for (ptr = 0; ptr < sel->map_size; ptr++)
		s3c24xx_dma_check_entry(nmap+ptr, ptr);

	return 0;
}
s3c24xx_dma_order_set()函數如出一轍,這裏使用s3c2440_dma_sel變量初始化dma_sel。
這樣,DMA Provider把基本的準備工作做好了,下面看Consumer如何使用它。


2、DMA的使用

在SDIO驅動的probe函數中:

static int __devinit s3cmci_probe(struct platform_device *pdev)
{
	if (s3cmci_host_usedma(host)) {
		host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,
						host);
		if (host->dma < 0) {
			dev_err(&pdev->dev, "cannot get DMA channel.\n");
			if (!s3cmci_host_canpio()) {
				ret = -EBUSY;
				goto probe_free_gpio_wp;
			} else {
				dev_warn(&pdev->dev, "falling back to PIO.\n");
				host->dodma = 0;
			}
		}
	}
}
使用DMA傳輸數據,就從provider哪裏request:

static struct s3c2410_dma_client s3cmci_dma_client = {
	.name		= "s3c-mci",
};

s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client, host);
/*
 * get control of an dma channel
*/

int s3c2410_dma_request(unsigned int channel, struct s3c2410_dma_client *client, void *dev)
{
	struct s3c2410_dma_chan *chan;
	unsigned long flags;
	int err;

	pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
		 channel, client->name, dev);

	local_irq_save(flags);

	chan = s3c2410_dma_map_channel(channel);
	if (chan == NULL) {
		local_irq_restore(flags);
		return -EBUSY;
	}

	dbg_showchan(chan);

	chan->client = client;
	chan->in_use = 1;

	if (!chan->irq_claimed) {
		pr_debug("dma%d: %s : requesting irq %d\n",
			 channel, __func__, chan->irq);

		chan->irq_claimed = 1;
		local_irq_restore(flags);

		err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
				  client->name, (void *)chan);

		local_irq_save(flags);

		if (err) {
			chan->in_use = 0;
			chan->irq_claimed = 0;
			local_irq_restore(flags);

			printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
			       client->name, chan->irq, chan->number);
			return err;
		}

		chan->irq_enabled = 1;
	}

	local_irq_restore(flags);

	/* need to setup */

	pr_debug("%s: channel initialised, %p\n", __func__, chan);

	return chan->number | DMACH_LOW_LEVEL;
}
DMACH_SDI是邏輯channel,在使用之前肯定需要映射到實際的物理channel上去,這個功能由s3c2410_dma_map_channel()函數負責完成;映射成功後設置其in_use標誌,在30行。之後申請DMA對應的中斷處理函數。

至此,DMA是申請下來了,下面該使用它了。用法就是圍繞“二、DMA介紹”中提出的5點信息:

static struct mmc_host_ops s3cmci_ops = {
	.request	= s3cmci_request,
	....
};

static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
	s3cmci_send_request(mmc);
}

static void s3cmci_send_request(struct mmc_host *mmc)
{
	if (s3cmci_host_usedma(host))
		res = s3cmci_prepare_dma(host, cmd->data);
	else
		res = s3cmci_prepare_pio(host, cmd->data);
}
以上都是準備工作,幹活的是:

static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
{
	int dma_len, i;
	int rw = data->flags & MMC_DATA_WRITE;

	BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);

	s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
	s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);

	dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
			     rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);

	if (dma_len == 0)
		return -ENOMEM;

	host->dma_complete = 0;
	host->dmatogo = dma_len;

	for (i = 0; i < dma_len; i++) {
		int res;

		dbg(host, dbg_dma, "enqueue %i: %08x@%u\n", i,
		    sg_dma_address(&data->sg[i]),
		    sg_dma_len(&data->sg[i]));

		res = s3c2410_dma_enqueue(host->dma, host,
					  sg_dma_address(&data->sg[i]),
					  sg_dma_len(&data->sg[i]));

		if (res) {
			s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
			return -EBUSY;
		}
	}

	s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);

	return 0;
}
s3cmci_prepare_dma()函數第8行,先確定數據傳輸方向:寫數據,方向是mem->dev;讀數據,反向是dev->mem,然後作爲參數傳進s3cmci_dma_setup()函數:

static void s3cmci_dma_setup(struct s3cmci_host *host,
			     enum s3c2410_dmasrc source)
{
	static enum s3c2410_dmasrc last_source = -1;
	static int setup_ok;

	if (last_source == source)
		return;

	last_source = source;

	s3c2410_dma_devconfig(host->dma, source,
			      host->mem->start + host->sdidata);

	if (!setup_ok) {
		s3c2410_dma_config(host->dma, 4);
		s3c2410_dma_set_buffdone_fn(host->dma,
					    s3cmci_dma_done_callback);
		s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
		setup_ok = 1;
	}
}

s3c2410_dma_devconfig()函數配置DMA的源/目的寄存器:

/* 
 * configure the dma source/destination hardware type and address
 * source:    S3C2410_DMASRC_HW: source is hardware
 *            S3C2410_DMASRC_MEM: source is memory
 * devaddr:   physical address of the source
*/
int s3c2410_dma_devconfig(unsigned int channel,
			  enum s3c2410_dmasrc source,
			  unsigned long devaddr)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	chan->source = source;
	chan->dev_addr = devaddr;

	switch (source) {
	case S3C2410_DMASRC_HW:
		/* source is hardware */
		dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
		dma_wrreg(chan, S3C2410_DMA_DISRC,  devaddr);
		dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));

		chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
		break;

	case S3C2410_DMASRC_MEM:
		/* source is memory */
		dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
		dma_wrreg(chan, S3C2410_DMA_DIDST,  devaddr);
		dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);

		chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
		break;
	}
	return 0;
}

s3cmci_dma_setup()函數用來配置DMA通道的屬性,第15行,setup_ok是一個static變量,意味着下次進入該函數其值爲上次設置的值;首次進入自然爲0,s3c2410_dma_config()函數配置DMA的控制寄存器(DMA CONTROL REGISTER),如傳輸數據個數(transfer count)、傳輸完成是否產生中斷等。 s3c2410_dma_set_buffdone_fn()函數設置 DMA通道對應的回調函數,這裏就是s3cmci_dma_done_callback()。s3c2410_dma_setflags()函數設置通道S3C2410_DMAF_AUTOSTART標誌,意味着如果緩存取隊列不爲空的情況下,DMA控制器將自動發起一次DMA傳輸。


s3cmci_prepare_dma()函數第9行,控制DMA的開啓、停止、暫停等:

int s3c2410_dma_ctrl(unsigned int channel, enum s3c2410_chan_op op)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	switch (op) {
	case S3C2410_DMAOP_START:
		return s3c2410_dma_start(chan);

	case S3C2410_DMAOP_STOP:
		return s3c2410_dma_dostop(chan);

	case S3C2410_DMAOP_PAUSE:
	case S3C2410_DMAOP_RESUME:
		return -ENOENT;

	case S3C2410_DMAOP_FLUSH:
		return s3c2410_dma_flush(chan);

	case S3C2410_DMAOP_STARTED:
		return s3c2410_dma_started(chan);

	case S3C2410_DMAOP_TIMEOUT:
		return 0;

	}

	return -ENOENT;      /* unknown, don't bother */
}
20~35行把數據buf入隊列,然後37行發起DMA傳輸。如果設置了S3C2410_DMAF_AUTOSTART標誌,在DMA空閒狀態下傳輸自動進行。
經過以上設置,數據傳輸方向、傳輸使用的channel、傳輸的數據大小/位寬、傳輸控制、傳輸狀態、DMA傳輸情況的通知等都具備,DMA就開始歡快的工作了。
發佈了168 篇原創文章 · 獲贊 150 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章