Linux下的DMA学习

       最近在忙一个竖屏转横屏项目,使用的是spi接口的竖屏(cpu不带dpu,横屏lcd比竖屏lcd成本高),使用的方案就是利用dma的特性,将横屏的数据进行一定规则的搬运(设置dma的搬运规则,从左下角搬运第一列作为第一行,搬完所有的列)转换成竖屏的数据,然后发送到lcd。

常见的通信接口i2c,spi,i2s有fifo,dma两种通信方式。fifo每次传输的数据量不大,dma能进行大规模的数据量传输,减轻cpu的负担。一般驱动里要使用dma的传输方式,只需在相应的控制器中使能dma的开关即可(如dma-mode = <1>),设备驱动调用底层的接口时,cpu厂商的控制器驱动已经配好了dma的传输方式。

拿sprd的代码来学习下,如下

kernel/drivers/spi/spi-sprd.c
kernel/drivers/dma/sprd_dma.c

1.probe函数配置相应的接口函数,注册dma

static int sprd_dma_probe(struct platform_device *pdev)
{
    sdev->dma_dev.device_alloc_chan_resources =sprd_dma_alloc_chan_resources;
    sdev->dma_dev.device_prep_dma_memcpy = sprd_dma_prep_dma_memcpy;
    sdev->dma_dev.device_prep_dma_sg = sprd_prep_dma_sg;
    sdev->dma_dev.device_config = sprd_dma_control;
    dma_async_device_register(&sdev->dma_dev);
    of_dma_controller_register(pdev->dev.of_node,sprd_dma_simple_xlate, &sprd_dma_info);
}

2.申请dma资源

sprd_spi_request_dma--->of_dma_request_slave_channel---->ofdma->of_dma_xlate---->sprd_dma_simple_xlate

struct dma_chan *sprd_dma_simple_xlate(struct of_phandle_args *dma_spec,struct of_dma *ofdma)
{
    return dma_request_channel(info->dma_cap, info->filter_fn,dma_spec);
}

struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,dma_filter_fn fn, void *fn_param)
{
    err = dma_chan_get(chan);
}
static int dma_chan_get(struct dma_chan *chan)
{
    chan->device->device_alloc_chan_resources(chan);   ---------------------mdesc->desc.tx_submit = sprd_desc_submit;
}

分配资源,主要是描述符初始化(tx_submit),注册链表用于管理数据(不同平台有差异)。

struct dma_async_tx_descriptor {
	dma_cookie_t cookie;
	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
	dma_addr_t phys;
	struct dma_chan *chan;
	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
	int (*desc_free)(struct dma_async_tx_descriptor *tx);
	dma_async_tx_callback callback;
	void *callback_param;
	struct dmaengine_unmap_data *unmap;
};

static int sprd_dma_alloc_chan_resources(struct dma_chan *chan)
{
    ...
	dma_async_tx_descriptor_init(&mdesc->desc, chan);
	mdesc->desc.flags = DMA_CTRL_ACK;
	mdesc->desc.tx_submit = sprd_desc_submit;

	mdesc->dma_chn_reg = &chn_reg[i];
	mdesc->done = 0;
	mdesc->cycle = 0;
	mdesc->dma_flags = 0;
	INIT_LIST_HEAD(&mdesc->node);
	INIT_LIST_HEAD(&mdesc->next_node);
	list_add_tail(&mdesc->node, &descs);
    ...
}

3.使用流式dma映射,刷新cache到内存,参考https://blog.csdn.net/jasonchen_gbd/article/details/79462064

sprd_spi_dma_buf_mapping--->t->tx_dma = dma_map_single(spi_chip->dev, (void *)t->tx_buf,t->len, DMA_TO_DEVICE)

4.配置dma传输方式,设置发送完成回掉函数,使能dma

sprd_spi_dma_tx_config--->sprd_spi_dma_config(spi_chip, &dma_config, 0)--->
dmaengine_slave_config(spi_chip->dma[rx],&(c->config))--->chan->device->device_config
dma_dev->device_prep_dma_memcpy( spi_chip->dma[rx], 0, 0, 0, DMA_CFG_FLAG|DMA_HARDWARE_FLAG)
dma_des->callback = spi_chip->callback[rx];
dmaengine_submit(dma_des)--->desc->tx_submit(desc);

所以,每次发送数据,会进行dma映射,配置dma模式,并使能dma。

发送完成后,硬件触发中断,调用回掉函数

dma_irq_handle
tasklet_schedule(&sdev->tasklet);//tasklet_init(&sdev->tasklet, sprd_dma_tasklet,(unsigned long)sdev);
sprd_dma_process_completed
desc->callback(desc->callback_param);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章