160-dmaengine-add-sun4i-driver.patch 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. From 1a28c76f3965775854ed6f6229de457c3d0674ab Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Emilio=20L=C3=B3pez?= <emilio@elopez.com.ar>
  3. Date: Sat, 4 Apr 2015 11:37:24 +0200
  4. Subject: [PATCH] dma: sun4i: Add support for the DMA engine on sun[457]i SoCs
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This patch adds support for the DMA engine present on Allwinner A10,
  9. A13, A10S and A20 SoCs. This engine has two kinds of channels: normal
  10. and dedicated. The main difference is in the mode of operation;
  11. while a single normal channel may be operating at any given time,
  12. dedicated channels may operate simultaneously provided there is no
  13. overlap of source or destination.
  14. Hardware documentation can be found on A10 User Manual (section 12), A13
  15. User Manual (section 14) and A20 User Manual (section 1.12)
  16. Signed-off-by: Emilio López <emilio@elopez.com.ar>
  17. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  18. ---
  19. Changes from v4:
  20. * Fix for interrupt triggering after freeing a dma-channel, this fixed
  21. the problems with jack
  22. * Adjust to recent kernel dma API changes
  23. Changes from v3:
  24. * Drop threaded IRQ to get lower latency
  25. * Drop chancnt
  26. * Fix crash on first use when using a DMA-aware bootloader (eg., one
  27. that supports NAND)
  28. Changes from v2:
  29. * Faster memcpy
  30. * Quicker cyclic transfers
  31. * Address some stylistic and locking comments from Maxime
  32. * probably some more stuff I'm forgetting
  33. Changes from v1:
  34. * address comments from Chen-Yu and Maxime
  35. * fix issue converting bus width
  36. * switch to using a threaded IRQ instead of a tasklet on
  37. recommendation from Maxime
  38. * fix issue setting magic timing parameter for SPI transfers
  39. * fix an issue with list handling reported by the kbuild 0-DAY robot (thanks!)
  40. * drop a lot of unused #define
  41. * probably some more stuff I'm forgetting
  42. ---
  43. .../devicetree/bindings/dma/sun4i-dma.txt | 46 +
  44. drivers/dma/Kconfig | 11 +
  45. drivers/dma/Makefile | 1 +
  46. drivers/dma/sun4i-dma.c | 1235 ++++++++++++++++++++
  47. 4 files changed, 1293 insertions(+)
  48. create mode 100644 Documentation/devicetree/bindings/dma/sun4i-dma.txt
  49. create mode 100644 drivers/dma/sun4i-dma.c
  50. --- /dev/null
  51. +++ b/Documentation/devicetree/bindings/dma/sun4i-dma.txt
  52. @@ -0,0 +1,46 @@
  53. +Allwinner A10 DMA Controller
  54. +
  55. +This driver follows the generic DMA bindings defined in dma.txt.
  56. +
  57. +Required properties:
  58. +
  59. +- compatible: Must be "allwinner,sun4i-a10-dma"
  60. +- reg: Should contain the registers base address and length
  61. +- interrupts: Should contain a reference to the interrupt used by this device
  62. +- clocks: Should contain a reference to the parent AHB clock
  63. +- #dma-cells : Should be 2, first cell denoting normal or dedicated dma,
  64. + second cell holding the request line number.
  65. +
  66. +Example:
  67. + dma: dma-controller@01c02000 {
  68. + compatible = "allwinner,sun4i-a10-dma";
  69. + reg = <0x01c02000 0x1000>;
  70. + interrupts = <27>;
  71. + clocks = <&ahb_gates 6>;
  72. + #dma-cells = <2>;
  73. + };
  74. +
  75. +Clients:
  76. +
  77. +DMA clients connected to the Allwinner A10 DMA controller must use the
  78. +format described in the dma.txt file, using a three-cell specifier for
  79. +each channel: a phandle plus two integer cells.
  80. +The three cells in order are:
  81. +
  82. +1. A phandle pointing to the DMA controller.
  83. +2. Whether it is using normal (0) or dedicated (1) channels
  84. +3. The port ID as specified in the datasheet
  85. +
  86. +Example:
  87. + spi2: spi@01c17000 {
  88. + compatible = "allwinner,sun4i-a10-spi";
  89. + reg = <0x01c17000 0x1000>;
  90. + interrupts = <0 12 4>;
  91. + clocks = <&ahb_gates 22>, <&spi2_clk>;
  92. + clock-names = "ahb", "mod";
  93. + dmas = <&dma 1 29>, <&dma 1 28>;
  94. + dma-names = "rx", "tx";
  95. + status = "disabled";
  96. + #address-cells = <1>;
  97. + #size-cells = <0>;
  98. + };
  99. --- a/drivers/dma/Kconfig
  100. +++ b/drivers/dma/Kconfig
  101. @@ -444,6 +444,17 @@ config XGENE_DMA
  102. help
  103. Enable support for the APM X-Gene SoC DMA engine.
  104. +config SUN4I_DMA
  105. + tristate "Allwinner A10 DMA support"
  106. + depends on (MACH_SUN4I || MACH_SUN5I || MACH_SUN7I || (COMPILE_TEST && OF && ARM))
  107. + default (MACH_SUN4I || MACH_SUN5I || MACH_SUN7I)
  108. + select DMA_ENGINE
  109. + select DMA_OF
  110. + select DMA_VIRTUAL_CHANNELS
  111. + help
  112. + Enable support for the DMA controller present in the sun4i,
  113. + sun5i and sun7i Allwinner ARM SoCs.
  114. +
  115. config DMA_ENGINE
  116. bool
  117. --- a/drivers/dma/Makefile
  118. +++ b/drivers/dma/Makefile
  119. @@ -54,3 +54,4 @@ obj-$(CONFIG_NBPFAXI_DMA) += nbpfaxi.o
  120. obj-$(CONFIG_DMA_SUN6I) += sun6i-dma.o
  121. obj-$(CONFIG_IMG_MDC_DMA) += img-mdc-dma.o
  122. obj-$(CONFIG_XGENE_DMA) += xgene-dma.o
  123. +obj-$(CONFIG_SUN4I_DMA) += sun4i-dma.o
  124. --- /dev/null
  125. +++ b/drivers/dma/sun4i-dma.c
  126. @@ -0,0 +1,1235 @@
  127. +/*
  128. + * Copyright (C) 2014 Emilio López
  129. + * Emilio López <emilio@elopez.com.ar>
  130. + *
  131. + * This program is free software; you can redistribute it and/or modify
  132. + * it under the terms of the GNU General Public License as published by
  133. + * the Free Software Foundation; either version 2 of the License, or
  134. + * (at your option) any later version.
  135. + */
  136. +
  137. +#include <linux/bitmap.h>
  138. +#include <linux/bitops.h>
  139. +#include <linux/clk.h>
  140. +#include <linux/dmaengine.h>
  141. +#include <linux/dmapool.h>
  142. +#include <linux/interrupt.h>
  143. +#include <linux/module.h>
  144. +#include <linux/of_dma.h>
  145. +#include <linux/platform_device.h>
  146. +#include <linux/slab.h>
  147. +#include <linux/spinlock.h>
  148. +
  149. +#include "virt-dma.h"
  150. +
  151. +/** Normal DMA register values **/
  152. +
  153. +/* Normal DMA source/destination data request type values */
  154. +#define NDMA_DRQ_TYPE_SDRAM 0x16
  155. +#define NDMA_DRQ_TYPE_LIMIT (0x1F + 1)
  156. +
  157. +/** Normal DMA register layout **/
  158. +
  159. +/* Normal DMA configuration register layout */
  160. +#define NDMA_CFG_LOADING BIT(31)
  161. +#define NDMA_CFG_CONT_MODE BIT(30)
  162. +#define NDMA_CFG_WAIT_STATE(n) ((n) << 27)
  163. +#define NDMA_CFG_DEST_DATA_WIDTH(width) ((width) << 25)
  164. +#define NDMA_CFG_DEST_BURST_LENGTH(len) ((len) << 23)
  165. +#define NDMA_CFG_DEST_NON_SECURE BIT(22)
  166. +#define NDMA_CFG_DEST_FIXED_ADDR BIT(21)
  167. +#define NDMA_CFG_DEST_DRQ_TYPE(type) ((type) << 16)
  168. +#define NDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
  169. +#define NDMA_CFG_SRC_DATA_WIDTH(width) ((width) << 9)
  170. +#define NDMA_CFG_SRC_BURST_LENGTH(len) ((len) << 7)
  171. +#define NDMA_CFG_SRC_NON_SECURE BIT(6)
  172. +#define NDMA_CFG_SRC_FIXED_ADDR BIT(5)
  173. +#define NDMA_CFG_SRC_DRQ_TYPE(type) ((type) << 0)
  174. +
  175. +/** Dedicated DMA register values **/
  176. +
  177. +/* Dedicated DMA source/destination address mode values */
  178. +#define DDMA_ADDR_MODE_LINEAR 0
  179. +#define DDMA_ADDR_MODE_IO 1
  180. +#define DDMA_ADDR_MODE_HORIZONTAL_PAGE 2
  181. +#define DDMA_ADDR_MODE_VERTICAL_PAGE 3
  182. +
  183. +/* Dedicated DMA source/destination data request type values */
  184. +#define DDMA_DRQ_TYPE_SDRAM 0x1
  185. +#define DDMA_DRQ_TYPE_LIMIT (0x1F + 1)
  186. +
  187. +/** Dedicated DMA register layout **/
  188. +
  189. +/* Dedicated DMA configuration register layout */
  190. +#define DDMA_CFG_LOADING BIT(31)
  191. +#define DDMA_CFG_BUSY BIT(30)
  192. +#define DDMA_CFG_CONT_MODE BIT(29)
  193. +#define DDMA_CFG_DEST_NON_SECURE BIT(28)
  194. +#define DDMA_CFG_DEST_DATA_WIDTH(width) ((width) << 25)
  195. +#define DDMA_CFG_DEST_BURST_LENGTH(len) ((len) << 23)
  196. +#define DDMA_CFG_DEST_ADDR_MODE(mode) ((mode) << 21)
  197. +#define DDMA_CFG_DEST_DRQ_TYPE(type) ((type) << 16)
  198. +#define DDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
  199. +#define DDMA_CFG_SRC_NON_SECURE BIT(12)
  200. +#define DDMA_CFG_SRC_DATA_WIDTH(width) ((width) << 9)
  201. +#define DDMA_CFG_SRC_BURST_LENGTH(len) ((len) << 7)
  202. +#define DDMA_CFG_SRC_ADDR_MODE(mode) ((mode) << 5)
  203. +#define DDMA_CFG_SRC_DRQ_TYPE(type) ((type) << 0)
  204. +
  205. +/* Dedicated DMA parameter register layout */
  206. +#define DDMA_PARA_DEST_DATA_BLK_SIZE(n) (((n) - 1) << 24)
  207. +#define DDMA_PARA_DEST_WAIT_CYCLES(n) (((n) - 1) << 16)
  208. +#define DDMA_PARA_SRC_DATA_BLK_SIZE(n) (((n) - 1) << 8)
  209. +#define DDMA_PARA_SRC_WAIT_CYCLES(n) (((n) - 1) << 0)
  210. +
  211. +/** DMA register offsets **/
  212. +
  213. +/* General register offsets */
  214. +#define DMA_IRQ_ENABLE_REG 0x0
  215. +#define DMA_IRQ_PENDING_STATUS_REG 0x4
  216. +
  217. +/* Normal DMA register offsets */
  218. +#define NDMA_CHANNEL_REG_BASE(n) (0x100 + (n) * 0x20)
  219. +#define NDMA_CFG_REG 0x0
  220. +#define NDMA_SRC_ADDR_REG 0x4
  221. +#define NDMA_DEST_ADDR_REG 0x8
  222. +#define NDMA_BYTE_COUNT_REG 0xC
  223. +
  224. +/* Dedicated DMA register offsets */
  225. +#define DDMA_CHANNEL_REG_BASE(n) (0x300 + (n) * 0x20)
  226. +#define DDMA_CFG_REG 0x0
  227. +#define DDMA_SRC_ADDR_REG 0x4
  228. +#define DDMA_DEST_ADDR_REG 0x8
  229. +#define DDMA_BYTE_COUNT_REG 0xC
  230. +#define DDMA_PARA_REG 0x18
  231. +
  232. +/** DMA Driver **/
  233. +
  234. +/*
  235. + * Normal DMA has 8 channels, and Dedicated DMA has another 8, so that's
  236. + * 16 channels. As for endpoints, there's 29 and 21 respectively. Given
  237. + * that the Normal DMA endpoints (other than SDRAM) can be used as tx/rx,
  238. + * we need 78 vchans in total
  239. + */
  240. +#define NDMA_NR_MAX_CHANNELS 8
  241. +#define DDMA_NR_MAX_CHANNELS 8
  242. +#define DMA_NR_MAX_CHANNELS (NDMA_NR_MAX_CHANNELS + DDMA_NR_MAX_CHANNELS)
  243. +#define NDMA_NR_MAX_VCHANS (29 * 2 - 1)
  244. +#define DDMA_NR_MAX_VCHANS 21
  245. +#define DMA_NR_MAX_VCHANS (NDMA_NR_MAX_VCHANS + DDMA_NR_MAX_VCHANS)
  246. +
  247. +/* This set of DDMA timing parameters were found experimentally while
  248. + * working with the SPI driver and seem to make it behave correctly */
  249. +#define DDMA_MAGIC_SPI_PARAMETERS (DDMA_PARA_DEST_DATA_BLK_SIZE(1) | \
  250. + DDMA_PARA_SRC_DATA_BLK_SIZE(1) | \
  251. + DDMA_PARA_DEST_WAIT_CYCLES(2) | \
  252. + DDMA_PARA_SRC_WAIT_CYCLES(2))
  253. +
  254. +struct sun4i_dma_pchan {
  255. + /* Register base of channel */
  256. + void __iomem *base;
  257. + /* vchan currently being serviced */
  258. + struct sun4i_dma_vchan *vchan;
  259. + /* Is this a dedicated pchan? */
  260. + int is_dedicated;
  261. +};
  262. +
  263. +struct sun4i_dma_vchan {
  264. + struct virt_dma_chan vc;
  265. + struct dma_slave_config cfg;
  266. + struct sun4i_dma_pchan *pchan;
  267. + struct sun4i_dma_promise *processing;
  268. + struct sun4i_dma_contract *contract;
  269. + u8 endpoint;
  270. + int is_dedicated;
  271. +};
  272. +
  273. +struct sun4i_dma_promise {
  274. + u32 cfg;
  275. + u32 para;
  276. + dma_addr_t src;
  277. + dma_addr_t dst;
  278. + size_t len;
  279. + struct list_head list;
  280. +};
  281. +
  282. +/* A contract is a set of promises */
  283. +struct sun4i_dma_contract {
  284. + struct virt_dma_desc vd;
  285. + struct list_head demands;
  286. + struct list_head completed_demands;
  287. + int is_cyclic;
  288. +};
  289. +
  290. +struct sun4i_dma_dev {
  291. + DECLARE_BITMAP(pchans_used, DMA_NR_MAX_CHANNELS);
  292. + struct dma_device slave;
  293. + struct sun4i_dma_pchan *pchans;
  294. + struct sun4i_dma_vchan *vchans;
  295. + void __iomem *base;
  296. + struct clk *clk;
  297. + int irq;
  298. + spinlock_t lock;
  299. +};
  300. +
  301. +static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
  302. +{
  303. + return container_of(dev, struct sun4i_dma_dev, slave);
  304. +}
  305. +
  306. +static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
  307. +{
  308. + return container_of(chan, struct sun4i_dma_vchan, vc.chan);
  309. +}
  310. +
  311. +static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
  312. +{
  313. + return container_of(vd, struct sun4i_dma_contract, vd);
  314. +}
  315. +
  316. +static struct device *chan2dev(struct dma_chan *chan)
  317. +{
  318. + return &chan->dev->device;
  319. +}
  320. +
  321. +static int convert_burst(u32 maxburst)
  322. +{
  323. + if (maxburst > 8)
  324. + return -EINVAL;
  325. +
  326. + /* 1 -> 0, 4 -> 1, 8 -> 2 */
  327. + return (maxburst >> 2);
  328. +}
  329. +
  330. +static int convert_buswidth(enum dma_slave_buswidth addr_width)
  331. +{
  332. + if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
  333. + return -EINVAL;
  334. +
  335. + /* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
  336. + return (addr_width >> 1);
  337. +}
  338. +
  339. +static int choose_optimal_buswidth(dma_addr_t addr)
  340. +{
  341. + /* On 32 bit aligned addresses, we can use a 32 bit bus width */
  342. + if (addr % 4 == 0)
  343. + return DMA_SLAVE_BUSWIDTH_4_BYTES;
  344. + /* On 16 bit aligned addresses, we can use a 16 bit bus width */
  345. + else if (addr % 2 == 0)
  346. + return DMA_SLAVE_BUSWIDTH_2_BYTES;
  347. +
  348. + /* Worst-case scenario, we need to do byte aligned reads */
  349. + return DMA_SLAVE_BUSWIDTH_1_BYTE;
  350. +}
  351. +
  352. +static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
  353. +{
  354. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  355. +
  356. + vchan_free_chan_resources(&vchan->vc);
  357. +}
  358. +
  359. +static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
  360. + struct sun4i_dma_vchan *vchan)
  361. +{
  362. + struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
  363. + unsigned long flags;
  364. + int i, max;
  365. +
  366. + /*
  367. + * pchans 0-NDMA_NR_MAX_CHANNELS are normal, and
  368. + * NDMA_NR_MAX_CHANNELS+ are dedicated ones
  369. + */
  370. + if (vchan->is_dedicated) {
  371. + i = NDMA_NR_MAX_CHANNELS;
  372. + max = DMA_NR_MAX_CHANNELS;
  373. + } else {
  374. + i = 0;
  375. + max = NDMA_NR_MAX_CHANNELS;
  376. + }
  377. +
  378. + spin_lock_irqsave(&priv->lock, flags);
  379. + for_each_clear_bit_from(i, &priv->pchans_used, max) {
  380. + pchan = &pchans[i];
  381. + pchan->vchan = vchan;
  382. + set_bit(i, priv->pchans_used);
  383. + break;
  384. + }
  385. + spin_unlock_irqrestore(&priv->lock, flags);
  386. +
  387. + return pchan;
  388. +}
  389. +
  390. +static void release_pchan(struct sun4i_dma_dev *priv,
  391. + struct sun4i_dma_pchan *pchan)
  392. +{
  393. + unsigned long flags;
  394. + int nr = pchan - priv->pchans;
  395. +
  396. + spin_lock_irqsave(&priv->lock, flags);
  397. +
  398. + pchan->vchan = NULL;
  399. + clear_bit(nr, priv->pchans_used);
  400. +
  401. + spin_unlock_irqrestore(&priv->lock, flags);
  402. +}
  403. +
  404. +static void configure_pchan(struct sun4i_dma_pchan *pchan,
  405. + struct sun4i_dma_promise *d)
  406. +{
  407. + /*
  408. + * Configure addresses and misc parameters depending on type
  409. + * DDMA has an extra field with timing parameters
  410. + */
  411. + if (pchan->is_dedicated) {
  412. + writel_relaxed(d->src, pchan->base + DDMA_SRC_ADDR_REG);
  413. + writel_relaxed(d->dst, pchan->base + DDMA_DEST_ADDR_REG);
  414. + writel_relaxed(d->len, pchan->base + DDMA_BYTE_COUNT_REG);
  415. + writel_relaxed(d->para, pchan->base + DDMA_PARA_REG);
  416. + writel_relaxed(d->cfg, pchan->base + DDMA_CFG_REG);
  417. + } else {
  418. + writel_relaxed(d->src, pchan->base + NDMA_SRC_ADDR_REG);
  419. + writel_relaxed(d->dst, pchan->base + NDMA_DEST_ADDR_REG);
  420. + writel_relaxed(d->len, pchan->base + NDMA_BYTE_COUNT_REG);
  421. + writel_relaxed(d->cfg, pchan->base + NDMA_CFG_REG);
  422. + }
  423. +}
  424. +
  425. +static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
  426. + struct sun4i_dma_pchan *pchan,
  427. + int half, int end)
  428. +{
  429. + u32 reg;
  430. + int pchan_number = pchan - priv->pchans;
  431. + unsigned long flags;
  432. +
  433. + spin_lock_irqsave(&priv->lock, flags);
  434. +
  435. + reg = readl_relaxed(priv->base + DMA_IRQ_ENABLE_REG);
  436. +
  437. + if (half)
  438. + reg |= BIT(pchan_number * 2);
  439. + else
  440. + reg &= ~BIT(pchan_number * 2);
  441. +
  442. + if (end)
  443. + reg |= BIT(pchan_number * 2 + 1);
  444. + else
  445. + reg &= ~BIT(pchan_number * 2 + 1);
  446. +
  447. + writel_relaxed(reg, priv->base + DMA_IRQ_ENABLE_REG);
  448. +
  449. + spin_unlock_irqrestore(&priv->lock, flags);
  450. +}
  451. +
  452. +/**
  453. + * Execute pending operations on a vchan
  454. + *
  455. + * When given a vchan, this function will try to acquire a suitable
  456. + * pchan and, if successful, will configure it to fulfill a promise
  457. + * from the next pending contract.
  458. + *
  459. + * This function must be called with &vchan->vc.lock held.
  460. + */
  461. +static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
  462. + struct sun4i_dma_vchan *vchan)
  463. +{
  464. + struct sun4i_dma_promise *promise = NULL;
  465. + struct sun4i_dma_contract *contract = NULL;
  466. + struct sun4i_dma_pchan *pchan;
  467. + struct virt_dma_desc *vd;
  468. + int ret;
  469. +
  470. + lockdep_assert_held(&vchan->vc.lock);
  471. +
  472. + /* We need a pchan to do anything, so secure one if available */
  473. + pchan = find_and_use_pchan(priv, vchan);
  474. + if (!pchan)
  475. + return -EBUSY;
  476. +
  477. + /*
  478. + * Channel endpoints must not be repeated, so if this vchan
  479. + * has already submitted some work, we can't do anything else
  480. + */
  481. + if (vchan->processing) {
  482. + dev_dbg(chan2dev(&vchan->vc.chan),
  483. + "processing something to this endpoint already\n");
  484. + ret = -EBUSY;
  485. + goto release_pchan;
  486. + }
  487. +
  488. + do {
  489. + /* Figure out which contract we're working with today */
  490. + vd = vchan_next_desc(&vchan->vc);
  491. + if (!vd) {
  492. + dev_dbg(chan2dev(&vchan->vc.chan),
  493. + "No pending contract found");
  494. + ret = 0;
  495. + goto release_pchan;
  496. + }
  497. +
  498. + contract = to_sun4i_dma_contract(vd);
  499. + if (list_empty(&contract->demands)) {
  500. + /* The contract has been completed so mark it as such */
  501. + list_del(&contract->vd.node);
  502. + vchan_cookie_complete(&contract->vd);
  503. + dev_dbg(chan2dev(&vchan->vc.chan),
  504. + "Empty contract found and marked complete");
  505. + }
  506. + } while (list_empty(&contract->demands));
  507. +
  508. + /* Now find out what we need to do */
  509. + promise = list_first_entry(&contract->demands,
  510. + struct sun4i_dma_promise, list);
  511. + vchan->processing = promise;
  512. +
  513. + /* ... and make it reality */
  514. + if (promise) {
  515. + vchan->contract = contract;
  516. + vchan->pchan = pchan;
  517. + set_pchan_interrupt(priv, pchan, contract->is_cyclic, 1);
  518. + configure_pchan(pchan, promise);
  519. + }
  520. +
  521. + return 0;
  522. +
  523. +release_pchan:
  524. + release_pchan(priv, pchan);
  525. + return ret;
  526. +}
  527. +
  528. +/**
  529. + * Generate a promise, to be used in a normal DMA contract.
  530. + *
  531. + * A NDMA promise contains all the information required to program the
  532. + * normal part of the DMA Engine and get data copied. A non-executed
  533. + * promise will live in the demands list on a contract. Once it has been
  534. + * completed, it will be moved to the completed demands list for later freeing.
  535. + * All linked promises will be freed when the corresponding contract is freed
  536. + */
  537. +static struct sun4i_dma_promise *
  538. +generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
  539. + size_t len, struct dma_slave_config *sconfig)
  540. +{
  541. + struct sun4i_dma_promise *promise;
  542. + int ret;
  543. +
  544. + promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
  545. + if (!promise)
  546. + return NULL;
  547. +
  548. + promise->src = src;
  549. + promise->dst = dest;
  550. + promise->len = len;
  551. + promise->cfg = NDMA_CFG_LOADING | NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
  552. +
  553. + /* Use sensible default values if client is using undefined ones */
  554. + if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  555. + sconfig->src_addr_width = sconfig->dst_addr_width;
  556. + if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  557. + sconfig->dst_addr_width = sconfig->src_addr_width;
  558. + if (sconfig->src_maxburst == 0)
  559. + sconfig->src_maxburst = sconfig->dst_maxburst;
  560. + if (sconfig->dst_maxburst == 0)
  561. + sconfig->dst_maxburst = sconfig->src_maxburst;
  562. +
  563. + dev_dbg(chan2dev(chan),
  564. + "src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
  565. + sconfig->src_maxburst, sconfig->dst_maxburst,
  566. + sconfig->src_addr_width, sconfig->dst_addr_width);
  567. +
  568. + /* Source burst */
  569. + ret = convert_burst(sconfig->src_maxburst);
  570. + if (IS_ERR_VALUE(ret))
  571. + goto fail;
  572. + promise->cfg |= NDMA_CFG_SRC_BURST_LENGTH(ret);
  573. +
  574. + /* Destination burst */
  575. + ret = convert_burst(sconfig->dst_maxburst);
  576. + if (IS_ERR_VALUE(ret))
  577. + goto fail;
  578. + promise->cfg |= NDMA_CFG_DEST_BURST_LENGTH(ret);
  579. +
  580. + /* Source bus width */
  581. + ret = convert_buswidth(sconfig->src_addr_width);
  582. + if (IS_ERR_VALUE(ret))
  583. + goto fail;
  584. + promise->cfg |= NDMA_CFG_SRC_DATA_WIDTH(ret);
  585. +
  586. + /* Destination bus width */
  587. + ret = convert_buswidth(sconfig->dst_addr_width);
  588. + if (IS_ERR_VALUE(ret))
  589. + goto fail;
  590. + promise->cfg |= NDMA_CFG_DEST_DATA_WIDTH(ret);
  591. +
  592. + return promise;
  593. +
  594. +fail:
  595. + kfree(promise);
  596. + return NULL;
  597. +}
  598. +
  599. +/**
  600. + * Generate a promise, to be used in a dedicated DMA contract.
  601. + *
  602. + * A DDMA promise contains all the information required to program the
  603. + * Dedicated part of the DMA Engine and get data copied. A non-executed
  604. + * promise will live in the demands list on a contract. Once it has been
  605. + * completed, it will be moved to the completed demands list for later freeing.
  606. + * All linked promises will be freed when the corresponding contract is freed
  607. + */
  608. +static struct sun4i_dma_promise *
  609. +generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
  610. + size_t len, struct dma_slave_config *sconfig)
  611. +{
  612. + struct sun4i_dma_promise *promise;
  613. + int ret;
  614. +
  615. + promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
  616. + if (!promise)
  617. + return NULL;
  618. +
  619. + promise->src = src;
  620. + promise->dst = dest;
  621. + promise->len = len;
  622. + promise->cfg = DDMA_CFG_LOADING | DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
  623. +
  624. + /* Source burst */
  625. + ret = convert_burst(sconfig->src_maxburst);
  626. + if (IS_ERR_VALUE(ret))
  627. + goto fail;
  628. + promise->cfg |= DDMA_CFG_SRC_BURST_LENGTH(ret);
  629. +
  630. + /* Destination burst */
  631. + ret = convert_burst(sconfig->dst_maxburst);
  632. + if (IS_ERR_VALUE(ret))
  633. + goto fail;
  634. + promise->cfg |= DDMA_CFG_DEST_BURST_LENGTH(ret);
  635. +
  636. + /* Source bus width */
  637. + ret = convert_buswidth(sconfig->src_addr_width);
  638. + if (IS_ERR_VALUE(ret))
  639. + goto fail;
  640. + promise->cfg |= DDMA_CFG_SRC_DATA_WIDTH(ret);
  641. +
  642. + /* Destination bus width */
  643. + ret = convert_buswidth(sconfig->dst_addr_width);
  644. + if (IS_ERR_VALUE(ret))
  645. + goto fail;
  646. + promise->cfg |= DDMA_CFG_DEST_DATA_WIDTH(ret);
  647. +
  648. + return promise;
  649. +
  650. +fail:
  651. + kfree(promise);
  652. + return NULL;
  653. +}
  654. +
  655. +/**
  656. + * Generate a contract
  657. + *
  658. + * Contracts function as DMA descriptors. As our hardware does not support
  659. + * linked lists, we need to implement SG via software. We use a contract
  660. + * to hold all the pieces of the request and process them serially one
  661. + * after another. Each piece is represented as a promise.
  662. + */
  663. +static struct sun4i_dma_contract *generate_dma_contract(void)
  664. +{
  665. + struct sun4i_dma_contract *contract;
  666. +
  667. + contract = kzalloc(sizeof(*contract), GFP_NOWAIT);
  668. + if (!contract)
  669. + return NULL;
  670. +
  671. + INIT_LIST_HEAD(&contract->demands);
  672. + INIT_LIST_HEAD(&contract->completed_demands);
  673. +
  674. + return contract;
  675. +}
  676. +
  677. +/**
  678. + * Get next promise on a cyclic transfer
  679. + *
  680. + * Cyclic contracts contain a series of promises which are executed on a
  681. + * loop. This function returns the next promise from a cyclic contract,
  682. + * so it can be programmed into the hardware.
  683. + */
  684. +static struct sun4i_dma_promise *
  685. +get_next_cyclic_promise(struct sun4i_dma_contract *contract)
  686. +{
  687. + struct sun4i_dma_promise *promise;
  688. +
  689. + promise = list_first_entry_or_null(&contract->demands,
  690. + struct sun4i_dma_promise, list);
  691. + if (!promise) {
  692. + list_splice_init(&contract->completed_demands,
  693. + &contract->demands);
  694. + promise = list_first_entry(&contract->demands,
  695. + struct sun4i_dma_promise, list);
  696. + }
  697. +
  698. + return promise;
  699. +}
  700. +
  701. +/**
  702. + * Free a contract and all its associated promises
  703. + */
  704. +static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
  705. +{
  706. + struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
  707. + struct sun4i_dma_promise *promise;
  708. +
  709. + /* Free all the demands and completed demands */
  710. + list_for_each_entry(promise, &contract->demands, list)
  711. + kfree(promise);
  712. +
  713. + list_for_each_entry(promise, &contract->completed_demands, list)
  714. + kfree(promise);
  715. +
  716. + kfree(contract);
  717. +}
  718. +
  719. +static struct dma_async_tx_descriptor *
  720. +sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
  721. + dma_addr_t src, size_t len, unsigned long flags)
  722. +{
  723. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  724. + struct dma_slave_config *sconfig = &vchan->cfg;
  725. + struct sun4i_dma_promise *promise;
  726. + struct sun4i_dma_contract *contract;
  727. +
  728. + contract = generate_dma_contract();
  729. + if (!contract)
  730. + return NULL;
  731. +
  732. + /*
  733. + * We can only do the copy to bus aligned addresses, so
  734. + * choose the best one so we get decent performance. We also
  735. + * maximize the burst size for this same reason.
  736. + */
  737. + sconfig->src_addr_width = choose_optimal_buswidth(src);
  738. + sconfig->dst_addr_width = choose_optimal_buswidth(dest);
  739. + sconfig->src_maxburst = 8;
  740. + sconfig->dst_maxburst = 8;
  741. +
  742. + if (vchan->is_dedicated)
  743. + promise = generate_ddma_promise(chan, src, dest, len, sconfig);
  744. + else
  745. + promise = generate_ndma_promise(chan, src, dest, len, sconfig);
  746. +
  747. + if (!promise) {
  748. + kfree(contract);
  749. + return NULL;
  750. + }
  751. +
  752. + /* Configure memcpy mode */
  753. + if (vchan->is_dedicated) {
  754. + promise->cfg |= DDMA_CFG_SRC_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
  755. + DDMA_CFG_DEST_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM);
  756. + } else {
  757. + promise->cfg |= NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
  758. + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
  759. + }
  760. +
  761. + /* Fill the contract with our only promise */
  762. + list_add_tail(&promise->list, &contract->demands);
  763. +
  764. + /* And add it to the vchan */
  765. + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  766. +}
  767. +
  768. +static struct dma_async_tx_descriptor *
  769. +sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
  770. + size_t period_len, enum dma_transfer_direction dir,
  771. + unsigned long flags)
  772. +{
  773. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  774. + struct dma_slave_config *sconfig = &vchan->cfg;
  775. + struct sun4i_dma_promise *promise;
  776. + struct sun4i_dma_contract *contract;
  777. + dma_addr_t src, dest;
  778. + u32 endpoints;
  779. + int nr_periods, offset, plength, i;
  780. +
  781. + if (!is_slave_direction(dir)) {
  782. + dev_err(chan2dev(chan), "Invalid DMA direction\n");
  783. + return NULL;
  784. + }
  785. +
  786. + if (vchan->is_dedicated) {
  787. + /*
  788. + * As we are using this just for audio data, we need to use
  789. + * normal DMA. There is nothing stopping us from supporting
  790. + * dedicated DMA here as well, so if a client comes up and
  791. + * requires it, it will be simple to implement it.
  792. + */
  793. + dev_err(chan2dev(chan),
  794. + "Cyclic transfers are only supported on Normal DMA\n");
  795. + return NULL;
  796. + }
  797. +
  798. + contract = generate_dma_contract();
  799. + if (!contract)
  800. + return NULL;
  801. +
  802. + contract->is_cyclic = 1;
  803. +
  804. + /* Figure out the endpoints and the address we need */
  805. + if (dir == DMA_MEM_TO_DEV) {
  806. + src = buf;
  807. + dest = sconfig->dst_addr;
  808. + endpoints = NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
  809. + NDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
  810. + NDMA_CFG_DEST_FIXED_ADDR;
  811. + } else {
  812. + src = sconfig->src_addr;
  813. + dest = buf;
  814. + endpoints = NDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
  815. + NDMA_CFG_SRC_FIXED_ADDR |
  816. + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
  817. + }
  818. +
  819. + /*
  820. + * We will be using half done interrupts to make two periods
  821. + * out of a promise, so we need to program the DMA engine less
  822. + * often
  823. + */
  824. + nr_periods = DIV_ROUND_UP(len / period_len, 2);
  825. + for (i = 0; i < nr_periods; i++) {
  826. + /* Calculate the offset in the buffer and the length needed */
  827. + offset = i * period_len * 2;
  828. + plength = min((len - offset), (period_len * 2));
  829. + if (dir == DMA_MEM_TO_DEV)
  830. + src = buf + offset;
  831. + else
  832. + dest = buf + offset;
  833. +
  834. + /* Make the promise */
  835. + promise = generate_ndma_promise(chan, src, dest,
  836. + plength, sconfig);
  837. + if (!promise) {
  838. + /* TODO: should we free everything? */
  839. + return NULL;
  840. + }
  841. + promise->cfg |= endpoints;
  842. +
  843. + /* Then add it to the contract */
  844. + list_add_tail(&promise->list, &contract->demands);
  845. + }
  846. +
  847. + /* And add it to the vchan */
  848. + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  849. +}
  850. +
  851. +static struct dma_async_tx_descriptor *
  852. +sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  853. + unsigned int sg_len, enum dma_transfer_direction dir,
  854. + unsigned long flags, void *context)
  855. +{
  856. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  857. + struct dma_slave_config *sconfig = &vchan->cfg;
  858. + struct sun4i_dma_promise *promise;
  859. + struct sun4i_dma_contract *contract;
  860. + struct scatterlist *sg;
  861. + dma_addr_t srcaddr, dstaddr;
  862. + u32 endpoints, para;
  863. + int i;
  864. +
  865. + if (!sgl)
  866. + return NULL;
  867. +
  868. + if (!is_slave_direction(dir)) {
  869. + dev_err(chan2dev(chan), "Invalid DMA direction\n");
  870. + return NULL;
  871. + }
  872. +
  873. + contract = generate_dma_contract();
  874. + if (!contract)
  875. + return NULL;
  876. +
  877. + /* Figure out endpoints */
  878. + if (vchan->is_dedicated && dir == DMA_MEM_TO_DEV) {
  879. + endpoints = DDMA_CFG_SRC_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
  880. + DDMA_CFG_SRC_ADDR_MODE(DDMA_ADDR_MODE_LINEAR) |
  881. + DDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
  882. + DDMA_CFG_DEST_ADDR_MODE(DDMA_ADDR_MODE_IO);
  883. + } else if (!vchan->is_dedicated && dir == DMA_MEM_TO_DEV) {
  884. + endpoints = NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
  885. + NDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
  886. + NDMA_CFG_DEST_FIXED_ADDR;
  887. + } else if (vchan->is_dedicated) {
  888. + endpoints = DDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
  889. + DDMA_CFG_SRC_ADDR_MODE(DDMA_ADDR_MODE_IO) |
  890. + DDMA_CFG_DEST_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
  891. + DDMA_CFG_DEST_ADDR_MODE(DDMA_ADDR_MODE_LINEAR);
  892. + } else {
  893. + endpoints = NDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
  894. + NDMA_CFG_SRC_FIXED_ADDR |
  895. + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
  896. + }
  897. +
  898. + for_each_sg(sgl, sg, sg_len, i) {
  899. + /* Figure out addresses */
  900. + if (dir == DMA_MEM_TO_DEV) {
  901. + srcaddr = sg_dma_address(sg);
  902. + dstaddr = sconfig->dst_addr;
  903. + } else {
  904. + srcaddr = sconfig->src_addr;
  905. + dstaddr = sg_dma_address(sg);
  906. + }
  907. +
  908. + /*
  909. + * These are the magic DMA engine timings that keep SPI going.
  910. + * I haven't seen any interface on DMAEngine to configure
  911. + * timings, and so far they seem to work for everything we
  912. + * support, so I've kept them here. I don't know if other
  913. + * devices need different timings because, as usual, we only
  914. + * have the "para" bitfield meanings, but no comment on what
  915. + * the values should be when doing a certain operation :|
  916. + */
  917. + para = DDMA_MAGIC_SPI_PARAMETERS;
  918. +
  919. + /* And make a suitable promise */
  920. + if (vchan->is_dedicated)
  921. + promise = generate_ddma_promise(chan, srcaddr, dstaddr,
  922. + sg_dma_len(sg), sconfig);
  923. + else
  924. + promise = generate_ndma_promise(chan, srcaddr, dstaddr,
  925. + sg_dma_len(sg), sconfig);
  926. +
  927. + if (!promise)
  928. + return NULL; /* TODO: should we free everything? */
  929. +
  930. + promise->cfg |= endpoints;
  931. + promise->para = para;
  932. +
  933. + /* Then add it to the contract */
  934. + list_add_tail(&promise->list, &contract->demands);
  935. + }
  936. +
  937. + /*
  938. + * Once we've got all the promises ready, add the contract
  939. + * to the pending list on the vchan
  940. + */
  941. + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  942. +}
  943. +
  944. +static int sun4i_dma_terminate_all(struct dma_chan *chan)
  945. +{
  946. + struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
  947. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  948. + struct sun4i_dma_pchan *pchan = vchan->pchan;
  949. + LIST_HEAD(head);
  950. + unsigned long flags;
  951. +
  952. + spin_lock_irqsave(&vchan->vc.lock, flags);
  953. + vchan_get_all_descriptors(&vchan->vc, &head);
  954. + spin_unlock_irqrestore(&vchan->vc.lock, flags);
  955. +
  956. + /*
  957. + * Clearing the configuration register will halt the pchan. Interrupts
  958. + * may still trigger, so don't forget to disable them.
  959. + */
  960. + if (pchan) {
  961. + if (pchan->is_dedicated)
  962. + writel(0, pchan->base + DDMA_CFG_REG);
  963. + else
  964. + writel(0, pchan->base + NDMA_CFG_REG);
  965. + set_pchan_interrupt(priv, pchan, 0, 0);
  966. + release_pchan(priv, pchan);
  967. + }
  968. +
  969. + spin_lock_irqsave(&vchan->vc.lock, flags);
  970. + vchan_dma_desc_free_list(&vchan->vc, &head);
  971. + /* Clear these so the vchan is usable again */
  972. + vchan->processing = NULL;
  973. + vchan->pchan = NULL;
  974. + spin_unlock_irqrestore(&vchan->vc.lock, flags);
  975. +
  976. + return 0;
  977. +}
  978. +
  979. +static int sun4i_dma_config(struct dma_chan *chan,
  980. + struct dma_slave_config *config)
  981. +{
  982. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  983. +
  984. + memcpy(&vchan->cfg, config, sizeof(*config));
  985. +
  986. + return 0;
  987. +}
  988. +
  989. +static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
  990. + struct of_dma *ofdma)
  991. +{
  992. + struct sun4i_dma_dev *priv = ofdma->of_dma_data;
  993. + struct sun4i_dma_vchan *vchan;
  994. + struct dma_chan *chan;
  995. + u8 is_dedicated = dma_spec->args[0];
  996. + u8 endpoint = dma_spec->args[1];
  997. +
  998. + /* Check if type is Normal or Dedicated */
  999. + if (is_dedicated != 0 && is_dedicated != 1)
  1000. + return NULL;
  1001. +
  1002. + /* Make sure the endpoint looks sane */
  1003. + if ((is_dedicated && endpoint >= DDMA_DRQ_TYPE_LIMIT) ||
  1004. + (!is_dedicated && endpoint >= NDMA_DRQ_TYPE_LIMIT))
  1005. + return NULL;
  1006. +
  1007. + chan = dma_get_any_slave_channel(&priv->slave);
  1008. + if (!chan)
  1009. + return NULL;
  1010. +
  1011. + /* Assign the endpoint to the vchan */
  1012. + vchan = to_sun4i_dma_vchan(chan);
  1013. + vchan->is_dedicated = is_dedicated;
  1014. + vchan->endpoint = endpoint;
  1015. +
  1016. + return chan;
  1017. +}
  1018. +
  1019. +static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
  1020. + dma_cookie_t cookie,
  1021. + struct dma_tx_state *state)
  1022. +{
  1023. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  1024. + struct sun4i_dma_pchan *pchan = vchan->pchan;
  1025. + struct sun4i_dma_contract *contract;
  1026. + struct sun4i_dma_promise *promise;
  1027. + struct virt_dma_desc *vd;
  1028. + unsigned long flags;
  1029. + enum dma_status ret;
  1030. + size_t bytes = 0;
  1031. +
  1032. + ret = dma_cookie_status(chan, cookie, state);
  1033. + if (ret == DMA_COMPLETE)
  1034. + return ret;
  1035. +
  1036. + spin_lock_irqsave(&vchan->vc.lock, flags);
  1037. + vd = vchan_find_desc(&vchan->vc, cookie);
  1038. + if (!vd)
  1039. + goto exit;
  1040. + contract = to_sun4i_dma_contract(vd);
  1041. +
  1042. + list_for_each_entry(promise, &contract->demands, list)
  1043. + bytes += promise->len;
  1044. +
  1045. + /*
  1046. + * The hardware is configured to return the remaining byte
  1047. + * quantity. If possible, replace the first listed element's
  1048. + * full size with the actual remaining amount
  1049. + */
  1050. + promise = list_first_entry_or_null(&contract->demands,
  1051. + struct sun4i_dma_promise, list);
  1052. + if (promise && pchan) {
  1053. + bytes -= promise->len;
  1054. + if (pchan->is_dedicated)
  1055. + bytes += readl(pchan->base + DDMA_BYTE_COUNT_REG);
  1056. + else
  1057. + bytes += readl(pchan->base + NDMA_BYTE_COUNT_REG);
  1058. + }
  1059. +
  1060. +exit:
  1061. +
  1062. + dma_set_residue(state, bytes);
  1063. + spin_unlock_irqrestore(&vchan->vc.lock, flags);
  1064. +
  1065. + return ret;
  1066. +}
  1067. +
  1068. +static void sun4i_dma_issue_pending(struct dma_chan *chan)
  1069. +{
  1070. + struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
  1071. + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  1072. + unsigned long flags;
  1073. +
  1074. + spin_lock_irqsave(&vchan->vc.lock, flags);
  1075. +
  1076. + /*
  1077. + * If there are pending transactions for this vchan, push one of
  1078. + * them into the engine to get the ball rolling.
  1079. + */
  1080. + if (vchan_issue_pending(&vchan->vc))
  1081. + __execute_vchan_pending(priv, vchan);
  1082. +
  1083. + spin_unlock_irqrestore(&vchan->vc.lock, flags);
  1084. +}
  1085. +
  1086. +static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
  1087. +{
  1088. + struct sun4i_dma_dev *priv = dev_id;
  1089. + struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
  1090. + struct sun4i_dma_vchan *vchan;
  1091. + struct sun4i_dma_contract *contract;
  1092. + struct sun4i_dma_promise *promise;
  1093. + unsigned long pendirq, irqs, disableirqs;
  1094. + int bit, i, free_room, allow_mitigation = 1;
  1095. +
  1096. + pendirq = readl_relaxed(priv->base + DMA_IRQ_PENDING_STATUS_REG);
  1097. +
  1098. +handle_pending:
  1099. +
  1100. + disableirqs = 0;
  1101. + free_room = 0;
  1102. +
  1103. + for_each_set_bit(bit, &pendirq, 32) {
  1104. + pchan = &pchans[bit >> 1];
  1105. + vchan = pchan->vchan;
  1106. + if (!vchan) /* a terminated channel may still interrupt */
  1107. + continue;
  1108. + contract = vchan->contract;
  1109. +
  1110. + /*
  1111. + * Disable the IRQ and free the pchan if it's an end
  1112. + * interrupt (odd bit)
  1113. + */
  1114. + if (bit & 1) {
  1115. + spin_lock(&vchan->vc.lock);
  1116. +
  1117. + /*
  1118. + * Move the promise into the completed list now that
  1119. + * we're done with it
  1120. + */
  1121. + list_del(&vchan->processing->list);
  1122. + list_add_tail(&vchan->processing->list,
  1123. + &contract->completed_demands);
  1124. +
  1125. + /*
  1126. + * Cyclic DMA transfers are special:
  1127. + * - There's always something we can dispatch
  1128. + * - We need to run the callback
  1129. + * - Latency is very important, as this is used by audio
  1130. + * We therefore just cycle through the list and dispatch
  1131. + * whatever we have here, reusing the pchan. There's
  1132. + * no need to run the thread after this.
  1133. + *
  1134. + * For non-cyclic transfers we need to look around,
  1135. + * so we can program some more work, or notify the
  1136. + * client that their transfers have been completed.
  1137. + */
  1138. + if (contract->is_cyclic) {
  1139. + promise = get_next_cyclic_promise(contract);
  1140. + vchan->processing = promise;
  1141. + configure_pchan(pchan, promise);
  1142. + vchan_cyclic_callback(&contract->vd);
  1143. + } else {
  1144. + vchan->processing = NULL;
  1145. + vchan->pchan = NULL;
  1146. +
  1147. + free_room = 1;
  1148. + disableirqs |= BIT(bit);
  1149. + release_pchan(priv, pchan);
  1150. + }
  1151. +
  1152. + spin_unlock(&vchan->vc.lock);
  1153. + } else {
  1154. + /* Half done interrupt */
  1155. + if (contract->is_cyclic)
  1156. + vchan_cyclic_callback(&contract->vd);
  1157. + else
  1158. + disableirqs |= BIT(bit);
  1159. + }
  1160. + }
  1161. +
  1162. + /* Disable the IRQs for events we handled */
  1163. + spin_lock(&priv->lock);
  1164. + irqs = readl_relaxed(priv->base + DMA_IRQ_ENABLE_REG);
  1165. + writel_relaxed(irqs & ~disableirqs, priv->base + DMA_IRQ_ENABLE_REG);
  1166. + spin_unlock(&priv->lock);
  1167. +
  1168. + /* Writing 1 to the pending field will clear the pending interrupt */
  1169. + writel_relaxed(pendirq, priv->base + DMA_IRQ_PENDING_STATUS_REG);
  1170. +
  1171. + /*
  1172. + * If a pchan was freed, we may be able to schedule something else,
  1173. + * so have a look around
  1174. + */
  1175. + if (free_room) {
  1176. + for (i = 0; i < DMA_NR_MAX_VCHANS; i++) {
  1177. + vchan = &priv->vchans[i];
  1178. + spin_lock(&vchan->vc.lock);
  1179. + __execute_vchan_pending(priv, vchan);
  1180. + spin_unlock(&vchan->vc.lock);
  1181. + }
  1182. + }
  1183. +
  1184. + /*
  1185. + * Handle newer interrupts if some showed up, but only do it once
  1186. + * to avoid a too long a loop
  1187. + */
  1188. + if (allow_mitigation) {
  1189. + pendirq = readl_relaxed(priv->base + DMA_IRQ_PENDING_STATUS_REG);
  1190. + if (pendirq) {
  1191. + allow_mitigation = 0;
  1192. + goto handle_pending;
  1193. + }
  1194. + }
  1195. +
  1196. + return IRQ_HANDLED;
  1197. +}
  1198. +
  1199. +static int sun4i_dma_probe(struct platform_device *pdev)
  1200. +{
  1201. + struct sun4i_dma_dev *priv;
  1202. + struct resource *res;
  1203. + int i, j, ret;
  1204. +
  1205. + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  1206. + if (!priv)
  1207. + return -ENOMEM;
  1208. +
  1209. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1210. + priv->base = devm_ioremap_resource(&pdev->dev, res);
  1211. + if (IS_ERR(priv->base))
  1212. + return PTR_ERR(priv->base);
  1213. +
  1214. + priv->irq = platform_get_irq(pdev, 0);
  1215. + if (priv->irq < 0) {
  1216. + dev_err(&pdev->dev, "Cannot claim IRQ\n");
  1217. + return priv->irq;
  1218. + }
  1219. +
  1220. + priv->clk = devm_clk_get(&pdev->dev, NULL);
  1221. + if (IS_ERR(priv->clk)) {
  1222. + dev_err(&pdev->dev, "No clock specified\n");
  1223. + return PTR_ERR(priv->clk);
  1224. + }
  1225. +
  1226. + platform_set_drvdata(pdev, priv);
  1227. + spin_lock_init(&priv->lock);
  1228. +
  1229. + dma_cap_zero(priv->slave.cap_mask);
  1230. + dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
  1231. + dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
  1232. + dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
  1233. + dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
  1234. +
  1235. + INIT_LIST_HEAD(&priv->slave.channels);
  1236. + priv->slave.device_free_chan_resources = sun4i_dma_free_chan_resources;
  1237. + priv->slave.device_tx_status = sun4i_dma_tx_status;
  1238. + priv->slave.device_issue_pending = sun4i_dma_issue_pending;
  1239. + priv->slave.device_prep_slave_sg = sun4i_dma_prep_slave_sg;
  1240. + priv->slave.device_prep_dma_memcpy = sun4i_dma_prep_dma_memcpy;
  1241. + priv->slave.device_prep_dma_cyclic = sun4i_dma_prep_dma_cyclic;
  1242. + priv->slave.device_config = sun4i_dma_config;
  1243. + priv->slave.device_terminate_all = sun4i_dma_terminate_all;
  1244. + priv->slave.copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1245. + priv->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1246. + BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1247. + BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1248. + priv->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1249. + BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1250. + BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1251. + priv->slave.directions = BIT(DMA_DEV_TO_MEM) |
  1252. + BIT(DMA_MEM_TO_DEV);
  1253. + priv->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1254. +
  1255. + priv->slave.dev = &pdev->dev;
  1256. +
  1257. + priv->pchans = devm_kcalloc(&pdev->dev, DMA_NR_MAX_CHANNELS,
  1258. + sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
  1259. + priv->vchans = devm_kcalloc(&pdev->dev, DMA_NR_MAX_VCHANS,
  1260. + sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
  1261. + if (!priv->vchans || !priv->pchans)
  1262. + return -ENOMEM;
  1263. +
  1264. + /*
  1265. + * [0..NDMA_NR_MAX_CHANNELS) are normal pchans, and
  1266. + * [NDMA_NR_MAX_CHANNELS..DMA_NR_MAX_CHANNELS) are dedicated ones
  1267. + */
  1268. + for (i = 0; i < NDMA_NR_MAX_CHANNELS; i++)
  1269. + priv->pchans[i].base = priv->base + NDMA_CHANNEL_REG_BASE(i);
  1270. +
  1271. + for (j = 0; i < DMA_NR_MAX_CHANNELS; i++, j++) {
  1272. + priv->pchans[i].base = priv->base + DDMA_CHANNEL_REG_BASE(j);
  1273. + priv->pchans[i].is_dedicated = 1;
  1274. + }
  1275. +
  1276. + for (i = 0; i < DMA_NR_MAX_VCHANS; i++) {
  1277. + struct sun4i_dma_vchan *vchan = &priv->vchans[i];
  1278. +
  1279. + spin_lock_init(&vchan->vc.lock);
  1280. + vchan->vc.desc_free = sun4i_dma_free_contract;
  1281. + vchan_init(&vchan->vc, &priv->slave);
  1282. + }
  1283. +
  1284. + ret = clk_prepare_enable(priv->clk);
  1285. + if (ret) {
  1286. + dev_err(&pdev->dev, "Couldn't enable the clock\n");
  1287. + return ret;
  1288. + }
  1289. +
  1290. + /*
  1291. + * Make sure the IRQs are all disabled and accounted for. The bootloader
  1292. + * likes to leave these dirty
  1293. + */
  1294. + writel(0, priv->base + DMA_IRQ_ENABLE_REG);
  1295. + writel(0xFFFFFFFF, priv->base + DMA_IRQ_PENDING_STATUS_REG);
  1296. +
  1297. + ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
  1298. + 0, dev_name(&pdev->dev), priv);
  1299. + if (ret) {
  1300. + dev_err(&pdev->dev, "Cannot request IRQ\n");
  1301. + goto err_clk_disable;
  1302. + }
  1303. +
  1304. + ret = dma_async_device_register(&priv->slave);
  1305. + if (ret) {
  1306. + dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  1307. + goto err_clk_disable;
  1308. + }
  1309. +
  1310. + ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
  1311. + priv);
  1312. + if (ret) {
  1313. + dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  1314. + goto err_dma_unregister;
  1315. + }
  1316. +
  1317. + dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
  1318. +
  1319. + return 0;
  1320. +
  1321. +err_dma_unregister:
  1322. + dma_async_device_unregister(&priv->slave);
  1323. +err_clk_disable:
  1324. + clk_disable_unprepare(priv->clk);
  1325. + return ret;
  1326. +}
  1327. +
  1328. +static int sun4i_dma_remove(struct platform_device *pdev)
  1329. +{
  1330. + struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
  1331. +
  1332. + /* Disable IRQ so no more work is scheduled */
  1333. + disable_irq(priv->irq);
  1334. +
  1335. + of_dma_controller_free(pdev->dev.of_node);
  1336. + dma_async_device_unregister(&priv->slave);
  1337. +
  1338. + clk_disable_unprepare(priv->clk);
  1339. +
  1340. + return 0;
  1341. +}
  1342. +
  1343. +static struct of_device_id sun4i_dma_match[] = {
  1344. + { .compatible = "allwinner,sun4i-a10-dma" },
  1345. + { /* sentinel */ },
  1346. +};
  1347. +
  1348. +static struct platform_driver sun4i_dma_driver = {
  1349. + .probe = sun4i_dma_probe,
  1350. + .remove = sun4i_dma_remove,
  1351. + .driver = {
  1352. + .name = "sun4i-dma",
  1353. + .of_match_table = sun4i_dma_match,
  1354. + },
  1355. +};
  1356. +
  1357. +module_platform_driver(sun4i_dma_driver);
  1358. +
  1359. +MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
  1360. +MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
  1361. +MODULE_LICENSE("GPL");