Subject: [PATCH V3 1/2] of: Add generic device tree DMA helpers

From: [email-masked] (Arnd Bergmann)
Date: Fri, 4 May 2012 15:56:38 +0000

On Monday 30 April 2012, Jon Hunter wrote:
> From: Jon Hunter <[email-masked]>
> 
> This is based upon the work by Benoit Cousson [1] and Nicolas Ferre [2]
> to add some basic helpers to retrieve a DMA controller device_node and the
> DMA request/channel information.

Thanks for picking this up again, I very much appreciate it as not
having the binding is blocking a number of other activities.

> Design of DMA helpers
> 
> 1. Supporting devices with multiple DMA controllers
> 
>    In the case of DMA controllers that are using DMA Engine, requesting a
>    channel is performed by calling the following function.
> 
> 	struct dma_chan *dma_request_channel(dma_cap_mask_t mask,
> 			dma_filter_fn filter_fn,
> 			void *filter_param);
> 
>    The mask variable is used to identify the device controller in a list of
>    controllers. The filter_fn and filter_param are used to identify the
>    required dma channel and return a handle to the dma channel of type
>    dma_chan. From the examples I have seen, the mask and filter_fn are constant
>    for a given DMA controller.

I believe this is not entirely correct: sometimes the filter_fn is provided
by the slave driver, sometimes by the master driver.

Also, the mask does not identify a specific controller, AFAICT it just
provides certain constraints. There are cases where a driver can pick
from a multitude of dma engine controllers, as long as the channel
the constraints are resolved.
This could be expressed in the device tree by listing multiple dma
request lines (one per controller) in the device using it, or the
author of the device tree can pick one.

>    Therefore, when registering a DMA controller with
>    device tree we can pass these parameters and store them so that a device can
>    request them when requesting a channel. Hence, based upon this our register
>    function for the DMA controller now looks like this.
> 
> 	int of_dma_controller_register(struct device_node *np,
> 		dma_cap_mask_t *mask, dma_filter_fn fn);

This changes the behavior for drivers that provide their own filter
functions, which may or may not be a problem.
For examples, have a look at some of these:

drivers/spi/spi-ep93xx.c
drivers/mmc/host/tmio_mmc_dma.c
drivers/usb/renesas_usbhs/fifo.c

> 2. Supporting legacy devices not using DMA Engine
> 
>    These devices present a problem, as there may not be a uniform way to easily
>    support them with regard to device tree. However, _IF_ legacy devices that
>    are not using DMA Engine, only have a single DMA controller, then this
>    problem is a lot simpler. For example, if we look at the previously proposed
>    API for registering a DMA controller (where we pass the mask and function
>    pointer to the DMA Engine filter function) we can simply pass NULL and hence,
>    a driver requesting the DMA channel information would receive NULL for the
>    DMA Engine specific parameters. Then for legacy devices we simply need a
>    means to return the channel information (more on this later). If there are
>    legacy devices that do have multiple DMA controllers, then maybe they need to
>    be converted to support DMA Engine. I am not sure if this is unreasonable???

I think it's even simpler: any device that uses another interface instead
of dmaengine will just have to parse the DT information itself. We should
make sure that it uses the same binding though, so we can later migrate
to the dmaengine API without having to change the device trees.

> 3. Representing and requesting channel information
> 
>    From a hardware perspective, a DMA channel could be represented as ...
> 
>    i. channel index/number
>    ii. channel transfer type (optional)
>    iii. DMA interrupt mapping (optional)
> 
>   Please note that the transfer type is used to indicate if the transfer is to
>   device from memory, to memory from device, to memory from memory, etc. This
>   can be useful when there is a device such as an MMC device that uses two DMA
>   channels, one for reading (RX) and one for writing (TX).
> 
>   Forgetting device tree for now, some drivers use strings to represent a
>   DMA channel instead of using an integer. I assume that these drivers then
>   employ some sort of look-up table to convert the string into a channel
>   number/index that the hardware understands. If this assumption is correct
>   then when moving to a device tree implementation having such look-up tables
>   in the driver should no longer be necessary as the device tree will provide
>   the mapping of channel index/number to the device. Furthermore, it makes
>   sense that device tree uses integers to represent channel as opposed to
>   strings to save the driver having to convert the string into a integer at
>   some later stage.
> 
>   Next we need to think about how the DMA controller and channels are described
>   in the device tree itself. The following device tree node example describes
>   the properties of the DMA controller that includes, the register address
>   range, number of interrupt supported, number of channels and number of request
>   signals. This has been enhanced from the previous versions by adding number of
>   channels and number of request signals.

I think you can actually use strings, too, as long as you use a fixed number
of cells.

Wouldn't something like this work:?

	dma-controller1: dma1 {
		compatible = "something-using-strings";
		#dma-cells = <1>;
	};

	dma-controller2: dma2 {
		compatible = "something-using-integers";
		#dma-cells = <3>
	};

	some-device {
		compatible = "something";
		dmas = <&dma-controller1>, "some-dma",
			<&dma-controller2 1 2 3>;
	}

The only hard requirement is that the format of the attributes is
what the binding specific to that controller understands.

>    A driver can now request the DMA channel information by calling the following
>    function.
> 
> 	int of_get_dma_channel_info(struct device_node *np, int type,
> 		       struct of_dma_channel_info *info)
> 
>    Where type represents the transfer-type (again the DMA Engine
>    dma_transfer_direction enumeration can be used here regardless of if DMA
>    Engine is used) and of_dma_channel_info is defined as follows.
> 
> 	struct of_dma_channel_info {
> 		int		dma_channel;
> 		dma_cap_mask_t	dma_cap;
> 		dma_filter_fn	dma_filter_func;
> 	};
> 
>    Here dma_channel will always be valid and the other fields are optional
>    depending on whether DMA Engine is used.

I think a better interface for the device driver would be something that combines
your of_get_dma_channel_info() function with the dma_request_channel() function,
like

	struct dma_chan *of_dma_request_channel(struct device_node *dn, int index);

When we have the device tree, the driver using that channel doesn't actually have
to care about the specific of how the channel is defined any more, it just needs
to say which one it's interested in, out of the list of channels defined for that
device node.

	Arnd