nbpfaxi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*
  2. * Copyright (C) 2013-2014 Renesas Electronics Europe Ltd.
  3. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/bitmap.h>
  10. #include <linux/bitops.h>
  11. #include <linux/clk.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/log2.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <dt-bindings/dma/nbpfaxi.h>
  25. #include "dmaengine.h"
  26. #define NBPF_REG_CHAN_OFFSET 0
  27. #define NBPF_REG_CHAN_SIZE 0x40
  28. /* Channel Current Transaction Byte register */
  29. #define NBPF_CHAN_CUR_TR_BYTE 0x20
  30. /* Channel Status register */
  31. #define NBPF_CHAN_STAT 0x24
  32. #define NBPF_CHAN_STAT_EN 1
  33. #define NBPF_CHAN_STAT_TACT 4
  34. #define NBPF_CHAN_STAT_ERR 0x10
  35. #define NBPF_CHAN_STAT_END 0x20
  36. #define NBPF_CHAN_STAT_TC 0x40
  37. #define NBPF_CHAN_STAT_DER 0x400
  38. /* Channel Control register */
  39. #define NBPF_CHAN_CTRL 0x28
  40. #define NBPF_CHAN_CTRL_SETEN 1
  41. #define NBPF_CHAN_CTRL_CLREN 2
  42. #define NBPF_CHAN_CTRL_STG 4
  43. #define NBPF_CHAN_CTRL_SWRST 8
  44. #define NBPF_CHAN_CTRL_CLRRQ 0x10
  45. #define NBPF_CHAN_CTRL_CLREND 0x20
  46. #define NBPF_CHAN_CTRL_CLRTC 0x40
  47. #define NBPF_CHAN_CTRL_SETSUS 0x100
  48. #define NBPF_CHAN_CTRL_CLRSUS 0x200
  49. /* Channel Configuration register */
  50. #define NBPF_CHAN_CFG 0x2c
  51. #define NBPF_CHAN_CFG_SEL 7 /* terminal SELect: 0..7 */
  52. #define NBPF_CHAN_CFG_REQD 8 /* REQuest Direction: DMAREQ is 0: input, 1: output */
  53. #define NBPF_CHAN_CFG_LOEN 0x10 /* LOw ENable: low DMA request line is: 0: inactive, 1: active */
  54. #define NBPF_CHAN_CFG_HIEN 0x20 /* HIgh ENable: high DMA request line is: 0: inactive, 1: active */
  55. #define NBPF_CHAN_CFG_LVL 0x40 /* LeVeL: DMA request line is sensed as 0: edge, 1: level */
  56. #define NBPF_CHAN_CFG_AM 0x700 /* ACK Mode: 0: Pulse mode, 1: Level mode, b'1x: Bus Cycle */
  57. #define NBPF_CHAN_CFG_SDS 0xf000 /* Source Data Size: 0: 8 bits,... , 7: 1024 bits */
  58. #define NBPF_CHAN_CFG_DDS 0xf0000 /* Destination Data Size: as above */
  59. #define NBPF_CHAN_CFG_SAD 0x100000 /* Source ADdress counting: 0: increment, 1: fixed */
  60. #define NBPF_CHAN_CFG_DAD 0x200000 /* Destination ADdress counting: 0: increment, 1: fixed */
  61. #define NBPF_CHAN_CFG_TM 0x400000 /* Transfer Mode: 0: single, 1: block TM */
  62. #define NBPF_CHAN_CFG_DEM 0x1000000 /* DMAEND interrupt Mask */
  63. #define NBPF_CHAN_CFG_TCM 0x2000000 /* DMATCO interrupt Mask */
  64. #define NBPF_CHAN_CFG_SBE 0x8000000 /* Sweep Buffer Enable */
  65. #define NBPF_CHAN_CFG_RSEL 0x10000000 /* RM: Register Set sELect */
  66. #define NBPF_CHAN_CFG_RSW 0x20000000 /* RM: Register Select sWitch */
  67. #define NBPF_CHAN_CFG_REN 0x40000000 /* RM: Register Set Enable */
  68. #define NBPF_CHAN_CFG_DMS 0x80000000 /* 0: register mode (RM), 1: link mode (LM) */
  69. #define NBPF_CHAN_NXLA 0x38
  70. #define NBPF_CHAN_CRLA 0x3c
  71. /* Link Header field */
  72. #define NBPF_HEADER_LV 1
  73. #define NBPF_HEADER_LE 2
  74. #define NBPF_HEADER_WBD 4
  75. #define NBPF_HEADER_DIM 8
  76. #define NBPF_CTRL 0x300
  77. #define NBPF_CTRL_PR 1 /* 0: fixed priority, 1: round robin */
  78. #define NBPF_CTRL_LVINT 2 /* DMAEND and DMAERR signalling: 0: pulse, 1: level */
  79. #define NBPF_DSTAT_ER 0x314
  80. #define NBPF_DSTAT_END 0x318
  81. #define NBPF_DMA_BUSWIDTHS \
  82. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  83. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  84. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  85. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  86. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  87. struct nbpf_config {
  88. int num_channels;
  89. int buffer_size;
  90. };
  91. /*
  92. * We've got 3 types of objects, used to describe DMA transfers:
  93. * 1. high-level descriptor, containing a struct dma_async_tx_descriptor object
  94. * in it, used to communicate with the user
  95. * 2. hardware DMA link descriptors, that we pass to DMAC for DMA transfer
  96. * queuing, these must be DMAable, using either the streaming DMA API or
  97. * allocated from coherent memory - one per SG segment
  98. * 3. one per SG segment descriptors, used to manage HW link descriptors from
  99. * (2). They do not have to be DMAable. They can either be (a) allocated
  100. * together with link descriptors as mixed (DMA / CPU) objects, or (b)
  101. * separately. Even if allocated separately it would be best to link them
  102. * to link descriptors once during channel resource allocation and always
  103. * use them as a single object.
  104. * Therefore for both cases (a) and (b) at run-time objects (2) and (3) shall be
  105. * treated as a single SG segment descriptor.
  106. */
  107. struct nbpf_link_reg {
  108. u32 header;
  109. u32 src_addr;
  110. u32 dst_addr;
  111. u32 transaction_size;
  112. u32 config;
  113. u32 interval;
  114. u32 extension;
  115. u32 next;
  116. } __packed;
  117. struct nbpf_device;
  118. struct nbpf_channel;
  119. struct nbpf_desc;
  120. struct nbpf_link_desc {
  121. struct nbpf_link_reg *hwdesc;
  122. dma_addr_t hwdesc_dma_addr;
  123. struct nbpf_desc *desc;
  124. struct list_head node;
  125. };
  126. /**
  127. * struct nbpf_desc - DMA transfer descriptor
  128. * @async_tx: dmaengine object
  129. * @user_wait: waiting for a user ack
  130. * @length: total transfer length
  131. * @sg: list of hardware descriptors, represented by struct nbpf_link_desc
  132. * @node: member in channel descriptor lists
  133. */
  134. struct nbpf_desc {
  135. struct dma_async_tx_descriptor async_tx;
  136. bool user_wait;
  137. size_t length;
  138. struct nbpf_channel *chan;
  139. struct list_head sg;
  140. struct list_head node;
  141. };
  142. /* Take a wild guess: allocate 4 segments per descriptor */
  143. #define NBPF_SEGMENTS_PER_DESC 4
  144. #define NBPF_DESCS_PER_PAGE ((PAGE_SIZE - sizeof(struct list_head)) / \
  145. (sizeof(struct nbpf_desc) + \
  146. NBPF_SEGMENTS_PER_DESC * \
  147. (sizeof(struct nbpf_link_desc) + sizeof(struct nbpf_link_reg))))
  148. #define NBPF_SEGMENTS_PER_PAGE (NBPF_SEGMENTS_PER_DESC * NBPF_DESCS_PER_PAGE)
  149. struct nbpf_desc_page {
  150. struct list_head node;
  151. struct nbpf_desc desc[NBPF_DESCS_PER_PAGE];
  152. struct nbpf_link_desc ldesc[NBPF_SEGMENTS_PER_PAGE];
  153. struct nbpf_link_reg hwdesc[NBPF_SEGMENTS_PER_PAGE];
  154. };
  155. /**
  156. * struct nbpf_channel - one DMAC channel
  157. * @dma_chan: standard dmaengine channel object
  158. * @base: register address base
  159. * @nbpf: DMAC
  160. * @name: IRQ name
  161. * @irq: IRQ number
  162. * @slave_addr: address for slave DMA
  163. * @slave_width:slave data size in bytes
  164. * @slave_burst:maximum slave burst size in bytes
  165. * @terminal: DMA terminal, assigned to this channel
  166. * @dmarq_cfg: DMA request line configuration - high / low, edge / level for NBPF_CHAN_CFG
  167. * @flags: configuration flags from DT
  168. * @lock: protect descriptor lists
  169. * @free_links: list of free link descriptors
  170. * @free: list of free descriptors
  171. * @queued: list of queued descriptors
  172. * @active: list of descriptors, scheduled for processing
  173. * @done: list of completed descriptors, waiting post-processing
  174. * @desc_page: list of additionally allocated descriptor pages - if any
  175. */
  176. struct nbpf_channel {
  177. struct dma_chan dma_chan;
  178. struct tasklet_struct tasklet;
  179. void __iomem *base;
  180. struct nbpf_device *nbpf;
  181. char name[16];
  182. int irq;
  183. dma_addr_t slave_src_addr;
  184. size_t slave_src_width;
  185. size_t slave_src_burst;
  186. dma_addr_t slave_dst_addr;
  187. size_t slave_dst_width;
  188. size_t slave_dst_burst;
  189. unsigned int terminal;
  190. u32 dmarq_cfg;
  191. unsigned long flags;
  192. spinlock_t lock;
  193. struct list_head free_links;
  194. struct list_head free;
  195. struct list_head queued;
  196. struct list_head active;
  197. struct list_head done;
  198. struct list_head desc_page;
  199. struct nbpf_desc *running;
  200. bool paused;
  201. };
  202. struct nbpf_device {
  203. struct dma_device dma_dev;
  204. void __iomem *base;
  205. struct clk *clk;
  206. const struct nbpf_config *config;
  207. unsigned int eirq;
  208. struct nbpf_channel chan[];
  209. };
  210. enum nbpf_model {
  211. NBPF1B4,
  212. NBPF1B8,
  213. NBPF1B16,
  214. NBPF4B4,
  215. NBPF4B8,
  216. NBPF4B16,
  217. NBPF8B4,
  218. NBPF8B8,
  219. NBPF8B16,
  220. };
  221. static struct nbpf_config nbpf_cfg[] = {
  222. [NBPF1B4] = {
  223. .num_channels = 1,
  224. .buffer_size = 4,
  225. },
  226. [NBPF1B8] = {
  227. .num_channels = 1,
  228. .buffer_size = 8,
  229. },
  230. [NBPF1B16] = {
  231. .num_channels = 1,
  232. .buffer_size = 16,
  233. },
  234. [NBPF4B4] = {
  235. .num_channels = 4,
  236. .buffer_size = 4,
  237. },
  238. [NBPF4B8] = {
  239. .num_channels = 4,
  240. .buffer_size = 8,
  241. },
  242. [NBPF4B16] = {
  243. .num_channels = 4,
  244. .buffer_size = 16,
  245. },
  246. [NBPF8B4] = {
  247. .num_channels = 8,
  248. .buffer_size = 4,
  249. },
  250. [NBPF8B8] = {
  251. .num_channels = 8,
  252. .buffer_size = 8,
  253. },
  254. [NBPF8B16] = {
  255. .num_channels = 8,
  256. .buffer_size = 16,
  257. },
  258. };
  259. #define nbpf_to_chan(d) container_of(d, struct nbpf_channel, dma_chan)
  260. /*
  261. * dmaengine drivers seem to have a lot in common and instead of sharing more
  262. * code, they reimplement those common algorithms independently. In this driver
  263. * we try to separate the hardware-specific part from the (largely) generic
  264. * part. This improves code readability and makes it possible in the future to
  265. * reuse the generic code in form of a helper library. That generic code should
  266. * be suitable for various DMA controllers, using transfer descriptors in RAM
  267. * and pushing one SG list at a time to the DMA controller.
  268. */
  269. /* Hardware-specific part */
  270. static inline u32 nbpf_chan_read(struct nbpf_channel *chan,
  271. unsigned int offset)
  272. {
  273. u32 data = ioread32(chan->base + offset);
  274. dev_dbg(chan->dma_chan.device->dev, "%s(0x%p + 0x%x) = 0x%x\n",
  275. __func__, chan->base, offset, data);
  276. return data;
  277. }
  278. static inline void nbpf_chan_write(struct nbpf_channel *chan,
  279. unsigned int offset, u32 data)
  280. {
  281. iowrite32(data, chan->base + offset);
  282. dev_dbg(chan->dma_chan.device->dev, "%s(0x%p + 0x%x) = 0x%x\n",
  283. __func__, chan->base, offset, data);
  284. }
  285. static inline u32 nbpf_read(struct nbpf_device *nbpf,
  286. unsigned int offset)
  287. {
  288. u32 data = ioread32(nbpf->base + offset);
  289. dev_dbg(nbpf->dma_dev.dev, "%s(0x%p + 0x%x) = 0x%x\n",
  290. __func__, nbpf->base, offset, data);
  291. return data;
  292. }
  293. static inline void nbpf_write(struct nbpf_device *nbpf,
  294. unsigned int offset, u32 data)
  295. {
  296. iowrite32(data, nbpf->base + offset);
  297. dev_dbg(nbpf->dma_dev.dev, "%s(0x%p + 0x%x) = 0x%x\n",
  298. __func__, nbpf->base, offset, data);
  299. }
  300. static void nbpf_chan_halt(struct nbpf_channel *chan)
  301. {
  302. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREN);
  303. }
  304. static bool nbpf_status_get(struct nbpf_channel *chan)
  305. {
  306. u32 status = nbpf_read(chan->nbpf, NBPF_DSTAT_END);
  307. return status & BIT(chan - chan->nbpf->chan);
  308. }
  309. static void nbpf_status_ack(struct nbpf_channel *chan)
  310. {
  311. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREND);
  312. }
  313. static u32 nbpf_error_get(struct nbpf_device *nbpf)
  314. {
  315. return nbpf_read(nbpf, NBPF_DSTAT_ER);
  316. }
  317. static struct nbpf_channel *nbpf_error_get_channel(struct nbpf_device *nbpf, u32 error)
  318. {
  319. return nbpf->chan + __ffs(error);
  320. }
  321. static void nbpf_error_clear(struct nbpf_channel *chan)
  322. {
  323. u32 status;
  324. int i;
  325. /* Stop the channel, make sure DMA has been aborted */
  326. nbpf_chan_halt(chan);
  327. for (i = 1000; i; i--) {
  328. status = nbpf_chan_read(chan, NBPF_CHAN_STAT);
  329. if (!(status & NBPF_CHAN_STAT_TACT))
  330. break;
  331. cpu_relax();
  332. }
  333. if (!i)
  334. dev_err(chan->dma_chan.device->dev,
  335. "%s(): abort timeout, channel status 0x%x\n", __func__, status);
  336. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SWRST);
  337. }
  338. static int nbpf_start(struct nbpf_desc *desc)
  339. {
  340. struct nbpf_channel *chan = desc->chan;
  341. struct nbpf_link_desc *ldesc = list_first_entry(&desc->sg, struct nbpf_link_desc, node);
  342. nbpf_chan_write(chan, NBPF_CHAN_NXLA, (u32)ldesc->hwdesc_dma_addr);
  343. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SETEN | NBPF_CHAN_CTRL_CLRSUS);
  344. chan->paused = false;
  345. /* Software trigger MEMCPY - only MEMCPY uses the block mode */
  346. if (ldesc->hwdesc->config & NBPF_CHAN_CFG_TM)
  347. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_STG);
  348. dev_dbg(chan->nbpf->dma_dev.dev, "%s(): next 0x%x, cur 0x%x\n", __func__,
  349. nbpf_chan_read(chan, NBPF_CHAN_NXLA), nbpf_chan_read(chan, NBPF_CHAN_CRLA));
  350. return 0;
  351. }
  352. static void nbpf_chan_prepare(struct nbpf_channel *chan)
  353. {
  354. chan->dmarq_cfg = (chan->flags & NBPF_SLAVE_RQ_HIGH ? NBPF_CHAN_CFG_HIEN : 0) |
  355. (chan->flags & NBPF_SLAVE_RQ_LOW ? NBPF_CHAN_CFG_LOEN : 0) |
  356. (chan->flags & NBPF_SLAVE_RQ_LEVEL ?
  357. NBPF_CHAN_CFG_LVL | (NBPF_CHAN_CFG_AM & 0x200) : 0) |
  358. chan->terminal;
  359. }
  360. static void nbpf_chan_prepare_default(struct nbpf_channel *chan)
  361. {
  362. /* Don't output DMAACK */
  363. chan->dmarq_cfg = NBPF_CHAN_CFG_AM & 0x400;
  364. chan->terminal = 0;
  365. chan->flags = 0;
  366. }
  367. static void nbpf_chan_configure(struct nbpf_channel *chan)
  368. {
  369. /*
  370. * We assume, that only the link mode and DMA request line configuration
  371. * have to be set in the configuration register manually. Dynamic
  372. * per-transfer configuration will be loaded from transfer descriptors.
  373. */
  374. nbpf_chan_write(chan, NBPF_CHAN_CFG, NBPF_CHAN_CFG_DMS | chan->dmarq_cfg);
  375. }
  376. static u32 nbpf_xfer_ds(struct nbpf_device *nbpf, size_t size)
  377. {
  378. /* Maximum supported bursts depend on the buffer size */
  379. return min_t(int, __ffs(size), ilog2(nbpf->config->buffer_size * 8));
  380. }
  381. static size_t nbpf_xfer_size(struct nbpf_device *nbpf,
  382. enum dma_slave_buswidth width, u32 burst)
  383. {
  384. size_t size;
  385. if (!burst)
  386. burst = 1;
  387. switch (width) {
  388. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  389. size = 8 * burst;
  390. break;
  391. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  392. size = 4 * burst;
  393. break;
  394. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  395. size = 2 * burst;
  396. break;
  397. default:
  398. pr_warn("%s(): invalid bus width %u\n", __func__, width);
  399. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  400. size = burst;
  401. }
  402. return nbpf_xfer_ds(nbpf, size);
  403. }
  404. /*
  405. * We need a way to recognise slaves, whose data is sent "raw" over the bus,
  406. * i.e. it isn't known in advance how many bytes will be received. Therefore
  407. * the slave driver has to provide a "large enough" buffer and either read the
  408. * buffer, when it is full, or detect, that some data has arrived, then wait for
  409. * a timeout, if no more data arrives - receive what's already there. We want to
  410. * handle such slaves in a special way to allow an optimised mode for other
  411. * users, for whom the amount of data is known in advance. So far there's no way
  412. * to recognise such slaves. We use a data-width check to distinguish between
  413. * the SD host and the PL011 UART.
  414. */
  415. static int nbpf_prep_one(struct nbpf_link_desc *ldesc,
  416. enum dma_transfer_direction direction,
  417. dma_addr_t src, dma_addr_t dst, size_t size, bool last)
  418. {
  419. struct nbpf_link_reg *hwdesc = ldesc->hwdesc;
  420. struct nbpf_desc *desc = ldesc->desc;
  421. struct nbpf_channel *chan = desc->chan;
  422. struct device *dev = chan->dma_chan.device->dev;
  423. size_t mem_xfer, slave_xfer;
  424. bool can_burst;
  425. hwdesc->header = NBPF_HEADER_WBD | NBPF_HEADER_LV |
  426. (last ? NBPF_HEADER_LE : 0);
  427. hwdesc->src_addr = src;
  428. hwdesc->dst_addr = dst;
  429. hwdesc->transaction_size = size;
  430. /*
  431. * set config: SAD, DAD, DDS, SDS, etc.
  432. * Note on transfer sizes: the DMAC can perform unaligned DMA transfers,
  433. * but it is important to have transaction size a multiple of both
  434. * receiver and transmitter transfer sizes. It is also possible to use
  435. * different RAM and device transfer sizes, and it does work well with
  436. * some devices, e.g. with V08R07S01E SD host controllers, which can use
  437. * 128 byte transfers. But this doesn't work with other devices,
  438. * especially when the transaction size is unknown. This is the case,
  439. * e.g. with serial drivers like amba-pl011.c. For reception it sets up
  440. * the transaction size of 4K and if fewer bytes are received, it
  441. * pauses DMA and reads out data received via DMA as well as those left
  442. * in the Rx FIFO. For this to work with the RAM side using burst
  443. * transfers we enable the SBE bit and terminate the transfer in our
  444. * .device_pause handler.
  445. */
  446. mem_xfer = nbpf_xfer_ds(chan->nbpf, size);
  447. switch (direction) {
  448. case DMA_DEV_TO_MEM:
  449. can_burst = chan->slave_src_width >= 3;
  450. slave_xfer = min(mem_xfer, can_burst ?
  451. chan->slave_src_burst : chan->slave_src_width);
  452. /*
  453. * Is the slave narrower than 64 bits, i.e. isn't using the full
  454. * bus width and cannot use bursts?
  455. */
  456. if (mem_xfer > chan->slave_src_burst && !can_burst)
  457. mem_xfer = chan->slave_src_burst;
  458. /* Device-to-RAM DMA is unreliable without REQD set */
  459. hwdesc->config = NBPF_CHAN_CFG_SAD | (NBPF_CHAN_CFG_DDS & (mem_xfer << 16)) |
  460. (NBPF_CHAN_CFG_SDS & (slave_xfer << 12)) | NBPF_CHAN_CFG_REQD |
  461. NBPF_CHAN_CFG_SBE;
  462. break;
  463. case DMA_MEM_TO_DEV:
  464. slave_xfer = min(mem_xfer, chan->slave_dst_width >= 3 ?
  465. chan->slave_dst_burst : chan->slave_dst_width);
  466. hwdesc->config = NBPF_CHAN_CFG_DAD | (NBPF_CHAN_CFG_SDS & (mem_xfer << 12)) |
  467. (NBPF_CHAN_CFG_DDS & (slave_xfer << 16)) | NBPF_CHAN_CFG_REQD;
  468. break;
  469. case DMA_MEM_TO_MEM:
  470. hwdesc->config = NBPF_CHAN_CFG_TCM | NBPF_CHAN_CFG_TM |
  471. (NBPF_CHAN_CFG_SDS & (mem_xfer << 12)) |
  472. (NBPF_CHAN_CFG_DDS & (mem_xfer << 16));
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. hwdesc->config |= chan->dmarq_cfg | (last ? 0 : NBPF_CHAN_CFG_DEM) |
  478. NBPF_CHAN_CFG_DMS;
  479. dev_dbg(dev, "%s(): desc @ %pad: hdr 0x%x, cfg 0x%x, %zu @ %pad -> %pad\n",
  480. __func__, &ldesc->hwdesc_dma_addr, hwdesc->header,
  481. hwdesc->config, size, &src, &dst);
  482. dma_sync_single_for_device(dev, ldesc->hwdesc_dma_addr, sizeof(*hwdesc),
  483. DMA_TO_DEVICE);
  484. return 0;
  485. }
  486. static size_t nbpf_bytes_left(struct nbpf_channel *chan)
  487. {
  488. return nbpf_chan_read(chan, NBPF_CHAN_CUR_TR_BYTE);
  489. }
  490. static void nbpf_configure(struct nbpf_device *nbpf)
  491. {
  492. nbpf_write(nbpf, NBPF_CTRL, NBPF_CTRL_LVINT);
  493. }
  494. /* Generic part */
  495. /* DMA ENGINE functions */
  496. static void nbpf_issue_pending(struct dma_chan *dchan)
  497. {
  498. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  499. unsigned long flags;
  500. dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
  501. spin_lock_irqsave(&chan->lock, flags);
  502. if (list_empty(&chan->queued))
  503. goto unlock;
  504. list_splice_tail_init(&chan->queued, &chan->active);
  505. if (!chan->running) {
  506. struct nbpf_desc *desc = list_first_entry(&chan->active,
  507. struct nbpf_desc, node);
  508. if (!nbpf_start(desc))
  509. chan->running = desc;
  510. }
  511. unlock:
  512. spin_unlock_irqrestore(&chan->lock, flags);
  513. }
  514. static enum dma_status nbpf_tx_status(struct dma_chan *dchan,
  515. dma_cookie_t cookie, struct dma_tx_state *state)
  516. {
  517. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  518. enum dma_status status = dma_cookie_status(dchan, cookie, state);
  519. if (state) {
  520. dma_cookie_t running;
  521. unsigned long flags;
  522. spin_lock_irqsave(&chan->lock, flags);
  523. running = chan->running ? chan->running->async_tx.cookie : -EINVAL;
  524. if (cookie == running) {
  525. state->residue = nbpf_bytes_left(chan);
  526. dev_dbg(dchan->device->dev, "%s(): residue %u\n", __func__,
  527. state->residue);
  528. } else if (status == DMA_IN_PROGRESS) {
  529. struct nbpf_desc *desc;
  530. bool found = false;
  531. list_for_each_entry(desc, &chan->active, node)
  532. if (desc->async_tx.cookie == cookie) {
  533. found = true;
  534. break;
  535. }
  536. if (!found)
  537. list_for_each_entry(desc, &chan->queued, node)
  538. if (desc->async_tx.cookie == cookie) {
  539. found = true;
  540. break;
  541. }
  542. state->residue = found ? desc->length : 0;
  543. }
  544. spin_unlock_irqrestore(&chan->lock, flags);
  545. }
  546. if (chan->paused)
  547. status = DMA_PAUSED;
  548. return status;
  549. }
  550. static dma_cookie_t nbpf_tx_submit(struct dma_async_tx_descriptor *tx)
  551. {
  552. struct nbpf_desc *desc = container_of(tx, struct nbpf_desc, async_tx);
  553. struct nbpf_channel *chan = desc->chan;
  554. unsigned long flags;
  555. dma_cookie_t cookie;
  556. spin_lock_irqsave(&chan->lock, flags);
  557. cookie = dma_cookie_assign(tx);
  558. list_add_tail(&desc->node, &chan->queued);
  559. spin_unlock_irqrestore(&chan->lock, flags);
  560. dev_dbg(chan->dma_chan.device->dev, "Entry %s(%d)\n", __func__, cookie);
  561. return cookie;
  562. }
  563. static int nbpf_desc_page_alloc(struct nbpf_channel *chan)
  564. {
  565. struct dma_chan *dchan = &chan->dma_chan;
  566. struct nbpf_desc_page *dpage = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
  567. struct nbpf_link_desc *ldesc;
  568. struct nbpf_link_reg *hwdesc;
  569. struct nbpf_desc *desc;
  570. LIST_HEAD(head);
  571. LIST_HEAD(lhead);
  572. int i;
  573. struct device *dev = dchan->device->dev;
  574. if (!dpage)
  575. return -ENOMEM;
  576. dev_dbg(dev, "%s(): alloc %lu descriptors, %lu segments, total alloc %zu\n",
  577. __func__, NBPF_DESCS_PER_PAGE, NBPF_SEGMENTS_PER_PAGE, sizeof(*dpage));
  578. for (i = 0, ldesc = dpage->ldesc, hwdesc = dpage->hwdesc;
  579. i < ARRAY_SIZE(dpage->ldesc);
  580. i++, ldesc++, hwdesc++) {
  581. ldesc->hwdesc = hwdesc;
  582. list_add_tail(&ldesc->node, &lhead);
  583. ldesc->hwdesc_dma_addr = dma_map_single(dchan->device->dev,
  584. hwdesc, sizeof(*hwdesc), DMA_TO_DEVICE);
  585. dev_dbg(dev, "%s(): mapped 0x%p to %pad\n", __func__,
  586. hwdesc, &ldesc->hwdesc_dma_addr);
  587. }
  588. for (i = 0, desc = dpage->desc;
  589. i < ARRAY_SIZE(dpage->desc);
  590. i++, desc++) {
  591. dma_async_tx_descriptor_init(&desc->async_tx, dchan);
  592. desc->async_tx.tx_submit = nbpf_tx_submit;
  593. desc->chan = chan;
  594. INIT_LIST_HEAD(&desc->sg);
  595. list_add_tail(&desc->node, &head);
  596. }
  597. /*
  598. * This function cannot be called from interrupt context, so, no need to
  599. * save flags
  600. */
  601. spin_lock_irq(&chan->lock);
  602. list_splice_tail(&lhead, &chan->free_links);
  603. list_splice_tail(&head, &chan->free);
  604. list_add(&dpage->node, &chan->desc_page);
  605. spin_unlock_irq(&chan->lock);
  606. return ARRAY_SIZE(dpage->desc);
  607. }
  608. static void nbpf_desc_put(struct nbpf_desc *desc)
  609. {
  610. struct nbpf_channel *chan = desc->chan;
  611. struct nbpf_link_desc *ldesc, *tmp;
  612. unsigned long flags;
  613. spin_lock_irqsave(&chan->lock, flags);
  614. list_for_each_entry_safe(ldesc, tmp, &desc->sg, node)
  615. list_move(&ldesc->node, &chan->free_links);
  616. list_add(&desc->node, &chan->free);
  617. spin_unlock_irqrestore(&chan->lock, flags);
  618. }
  619. static void nbpf_scan_acked(struct nbpf_channel *chan)
  620. {
  621. struct nbpf_desc *desc, *tmp;
  622. unsigned long flags;
  623. LIST_HEAD(head);
  624. spin_lock_irqsave(&chan->lock, flags);
  625. list_for_each_entry_safe(desc, tmp, &chan->done, node)
  626. if (async_tx_test_ack(&desc->async_tx) && desc->user_wait) {
  627. list_move(&desc->node, &head);
  628. desc->user_wait = false;
  629. }
  630. spin_unlock_irqrestore(&chan->lock, flags);
  631. list_for_each_entry_safe(desc, tmp, &head, node) {
  632. list_del(&desc->node);
  633. nbpf_desc_put(desc);
  634. }
  635. }
  636. /*
  637. * We have to allocate descriptors with the channel lock dropped. This means,
  638. * before we re-acquire the lock buffers can be taken already, so we have to
  639. * re-check after re-acquiring the lock and possibly retry, if buffers are gone
  640. * again.
  641. */
  642. static struct nbpf_desc *nbpf_desc_get(struct nbpf_channel *chan, size_t len)
  643. {
  644. struct nbpf_desc *desc = NULL;
  645. struct nbpf_link_desc *ldesc, *prev = NULL;
  646. nbpf_scan_acked(chan);
  647. spin_lock_irq(&chan->lock);
  648. do {
  649. int i = 0, ret;
  650. if (list_empty(&chan->free)) {
  651. /* No more free descriptors */
  652. spin_unlock_irq(&chan->lock);
  653. ret = nbpf_desc_page_alloc(chan);
  654. if (ret < 0)
  655. return NULL;
  656. spin_lock_irq(&chan->lock);
  657. continue;
  658. }
  659. desc = list_first_entry(&chan->free, struct nbpf_desc, node);
  660. list_del(&desc->node);
  661. do {
  662. if (list_empty(&chan->free_links)) {
  663. /* No more free link descriptors */
  664. spin_unlock_irq(&chan->lock);
  665. ret = nbpf_desc_page_alloc(chan);
  666. if (ret < 0) {
  667. nbpf_desc_put(desc);
  668. return NULL;
  669. }
  670. spin_lock_irq(&chan->lock);
  671. continue;
  672. }
  673. ldesc = list_first_entry(&chan->free_links,
  674. struct nbpf_link_desc, node);
  675. ldesc->desc = desc;
  676. if (prev)
  677. prev->hwdesc->next = (u32)ldesc->hwdesc_dma_addr;
  678. prev = ldesc;
  679. list_move_tail(&ldesc->node, &desc->sg);
  680. i++;
  681. } while (i < len);
  682. } while (!desc);
  683. prev->hwdesc->next = 0;
  684. spin_unlock_irq(&chan->lock);
  685. return desc;
  686. }
  687. static void nbpf_chan_idle(struct nbpf_channel *chan)
  688. {
  689. struct nbpf_desc *desc, *tmp;
  690. unsigned long flags;
  691. LIST_HEAD(head);
  692. spin_lock_irqsave(&chan->lock, flags);
  693. list_splice_init(&chan->done, &head);
  694. list_splice_init(&chan->active, &head);
  695. list_splice_init(&chan->queued, &head);
  696. chan->running = NULL;
  697. spin_unlock_irqrestore(&chan->lock, flags);
  698. list_for_each_entry_safe(desc, tmp, &head, node) {
  699. dev_dbg(chan->nbpf->dma_dev.dev, "%s(): force-free desc %p cookie %d\n",
  700. __func__, desc, desc->async_tx.cookie);
  701. list_del(&desc->node);
  702. nbpf_desc_put(desc);
  703. }
  704. }
  705. static int nbpf_pause(struct dma_chan *dchan)
  706. {
  707. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  708. dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
  709. chan->paused = true;
  710. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SETSUS);
  711. /* See comment in nbpf_prep_one() */
  712. nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREN);
  713. return 0;
  714. }
  715. static int nbpf_terminate_all(struct dma_chan *dchan)
  716. {
  717. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  718. dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
  719. dev_dbg(dchan->device->dev, "Terminating\n");
  720. nbpf_chan_halt(chan);
  721. nbpf_chan_idle(chan);
  722. return 0;
  723. }
  724. static int nbpf_config(struct dma_chan *dchan,
  725. struct dma_slave_config *config)
  726. {
  727. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  728. dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
  729. /*
  730. * We could check config->slave_id to match chan->terminal here,
  731. * but with DT they would be coming from the same source, so
  732. * such a check would be superflous
  733. */
  734. chan->slave_dst_addr = config->dst_addr;
  735. chan->slave_dst_width = nbpf_xfer_size(chan->nbpf,
  736. config->dst_addr_width, 1);
  737. chan->slave_dst_burst = nbpf_xfer_size(chan->nbpf,
  738. config->dst_addr_width,
  739. config->dst_maxburst);
  740. chan->slave_src_addr = config->src_addr;
  741. chan->slave_src_width = nbpf_xfer_size(chan->nbpf,
  742. config->src_addr_width, 1);
  743. chan->slave_src_burst = nbpf_xfer_size(chan->nbpf,
  744. config->src_addr_width,
  745. config->src_maxburst);
  746. return 0;
  747. }
  748. static struct dma_async_tx_descriptor *nbpf_prep_sg(struct nbpf_channel *chan,
  749. struct scatterlist *src_sg, struct scatterlist *dst_sg,
  750. size_t len, enum dma_transfer_direction direction,
  751. unsigned long flags)
  752. {
  753. struct nbpf_link_desc *ldesc;
  754. struct scatterlist *mem_sg;
  755. struct nbpf_desc *desc;
  756. bool inc_src, inc_dst;
  757. size_t data_len = 0;
  758. int i = 0;
  759. switch (direction) {
  760. case DMA_DEV_TO_MEM:
  761. mem_sg = dst_sg;
  762. inc_src = false;
  763. inc_dst = true;
  764. break;
  765. case DMA_MEM_TO_DEV:
  766. mem_sg = src_sg;
  767. inc_src = true;
  768. inc_dst = false;
  769. break;
  770. default:
  771. case DMA_MEM_TO_MEM:
  772. mem_sg = src_sg;
  773. inc_src = true;
  774. inc_dst = true;
  775. }
  776. desc = nbpf_desc_get(chan, len);
  777. if (!desc)
  778. return NULL;
  779. desc->async_tx.flags = flags;
  780. desc->async_tx.cookie = -EBUSY;
  781. desc->user_wait = false;
  782. /*
  783. * This is a private descriptor list, and we own the descriptor. No need
  784. * to lock.
  785. */
  786. list_for_each_entry(ldesc, &desc->sg, node) {
  787. int ret = nbpf_prep_one(ldesc, direction,
  788. sg_dma_address(src_sg),
  789. sg_dma_address(dst_sg),
  790. sg_dma_len(mem_sg),
  791. i == len - 1);
  792. if (ret < 0) {
  793. nbpf_desc_put(desc);
  794. return NULL;
  795. }
  796. data_len += sg_dma_len(mem_sg);
  797. if (inc_src)
  798. src_sg = sg_next(src_sg);
  799. if (inc_dst)
  800. dst_sg = sg_next(dst_sg);
  801. mem_sg = direction == DMA_DEV_TO_MEM ? dst_sg : src_sg;
  802. i++;
  803. }
  804. desc->length = data_len;
  805. /* The user has to return the descriptor to us ASAP via .tx_submit() */
  806. return &desc->async_tx;
  807. }
  808. static struct dma_async_tx_descriptor *nbpf_prep_memcpy(
  809. struct dma_chan *dchan, dma_addr_t dst, dma_addr_t src,
  810. size_t len, unsigned long flags)
  811. {
  812. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  813. struct scatterlist dst_sg;
  814. struct scatterlist src_sg;
  815. sg_init_table(&dst_sg, 1);
  816. sg_init_table(&src_sg, 1);
  817. sg_dma_address(&dst_sg) = dst;
  818. sg_dma_address(&src_sg) = src;
  819. sg_dma_len(&dst_sg) = len;
  820. sg_dma_len(&src_sg) = len;
  821. dev_dbg(dchan->device->dev, "%s(): %zu @ %pad -> %pad\n",
  822. __func__, len, &src, &dst);
  823. return nbpf_prep_sg(chan, &src_sg, &dst_sg, 1,
  824. DMA_MEM_TO_MEM, flags);
  825. }
  826. static struct dma_async_tx_descriptor *nbpf_prep_memcpy_sg(
  827. struct dma_chan *dchan,
  828. struct scatterlist *dst_sg, unsigned int dst_nents,
  829. struct scatterlist *src_sg, unsigned int src_nents,
  830. unsigned long flags)
  831. {
  832. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  833. if (dst_nents != src_nents)
  834. return NULL;
  835. return nbpf_prep_sg(chan, src_sg, dst_sg, src_nents,
  836. DMA_MEM_TO_MEM, flags);
  837. }
  838. static struct dma_async_tx_descriptor *nbpf_prep_slave_sg(
  839. struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
  840. enum dma_transfer_direction direction, unsigned long flags, void *context)
  841. {
  842. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  843. struct scatterlist slave_sg;
  844. dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
  845. sg_init_table(&slave_sg, 1);
  846. switch (direction) {
  847. case DMA_MEM_TO_DEV:
  848. sg_dma_address(&slave_sg) = chan->slave_dst_addr;
  849. return nbpf_prep_sg(chan, sgl, &slave_sg, sg_len,
  850. direction, flags);
  851. case DMA_DEV_TO_MEM:
  852. sg_dma_address(&slave_sg) = chan->slave_src_addr;
  853. return nbpf_prep_sg(chan, &slave_sg, sgl, sg_len,
  854. direction, flags);
  855. default:
  856. return NULL;
  857. }
  858. }
  859. static int nbpf_alloc_chan_resources(struct dma_chan *dchan)
  860. {
  861. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  862. int ret;
  863. INIT_LIST_HEAD(&chan->free);
  864. INIT_LIST_HEAD(&chan->free_links);
  865. INIT_LIST_HEAD(&chan->queued);
  866. INIT_LIST_HEAD(&chan->active);
  867. INIT_LIST_HEAD(&chan->done);
  868. ret = nbpf_desc_page_alloc(chan);
  869. if (ret < 0)
  870. return ret;
  871. dev_dbg(dchan->device->dev, "Entry %s(): terminal %u\n", __func__,
  872. chan->terminal);
  873. nbpf_chan_configure(chan);
  874. return ret;
  875. }
  876. static void nbpf_free_chan_resources(struct dma_chan *dchan)
  877. {
  878. struct nbpf_channel *chan = nbpf_to_chan(dchan);
  879. struct nbpf_desc_page *dpage, *tmp;
  880. dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
  881. nbpf_chan_halt(chan);
  882. nbpf_chan_idle(chan);
  883. /* Clean up for if a channel is re-used for MEMCPY after slave DMA */
  884. nbpf_chan_prepare_default(chan);
  885. list_for_each_entry_safe(dpage, tmp, &chan->desc_page, node) {
  886. struct nbpf_link_desc *ldesc;
  887. int i;
  888. list_del(&dpage->node);
  889. for (i = 0, ldesc = dpage->ldesc;
  890. i < ARRAY_SIZE(dpage->ldesc);
  891. i++, ldesc++)
  892. dma_unmap_single(dchan->device->dev, ldesc->hwdesc_dma_addr,
  893. sizeof(*ldesc->hwdesc), DMA_TO_DEVICE);
  894. free_page((unsigned long)dpage);
  895. }
  896. }
  897. static struct dma_chan *nbpf_of_xlate(struct of_phandle_args *dma_spec,
  898. struct of_dma *ofdma)
  899. {
  900. struct nbpf_device *nbpf = ofdma->of_dma_data;
  901. struct dma_chan *dchan;
  902. struct nbpf_channel *chan;
  903. if (dma_spec->args_count != 2)
  904. return NULL;
  905. dchan = dma_get_any_slave_channel(&nbpf->dma_dev);
  906. if (!dchan)
  907. return NULL;
  908. dev_dbg(dchan->device->dev, "Entry %s(%s)\n", __func__,
  909. dma_spec->np->name);
  910. chan = nbpf_to_chan(dchan);
  911. chan->terminal = dma_spec->args[0];
  912. chan->flags = dma_spec->args[1];
  913. nbpf_chan_prepare(chan);
  914. nbpf_chan_configure(chan);
  915. return dchan;
  916. }
  917. static void nbpf_chan_tasklet(unsigned long data)
  918. {
  919. struct nbpf_channel *chan = (struct nbpf_channel *)data;
  920. struct nbpf_desc *desc, *tmp;
  921. struct dmaengine_desc_callback cb;
  922. while (!list_empty(&chan->done)) {
  923. bool found = false, must_put, recycling = false;
  924. spin_lock_irq(&chan->lock);
  925. list_for_each_entry_safe(desc, tmp, &chan->done, node) {
  926. if (!desc->user_wait) {
  927. /* Newly completed descriptor, have to process */
  928. found = true;
  929. break;
  930. } else if (async_tx_test_ack(&desc->async_tx)) {
  931. /*
  932. * This descriptor was waiting for a user ACK,
  933. * it can be recycled now.
  934. */
  935. list_del(&desc->node);
  936. spin_unlock_irq(&chan->lock);
  937. nbpf_desc_put(desc);
  938. recycling = true;
  939. break;
  940. }
  941. }
  942. if (recycling)
  943. continue;
  944. if (!found) {
  945. /* This can happen if TERMINATE_ALL has been called */
  946. spin_unlock_irq(&chan->lock);
  947. break;
  948. }
  949. dma_cookie_complete(&desc->async_tx);
  950. /*
  951. * With released lock we cannot dereference desc, maybe it's
  952. * still on the "done" list
  953. */
  954. if (async_tx_test_ack(&desc->async_tx)) {
  955. list_del(&desc->node);
  956. must_put = true;
  957. } else {
  958. desc->user_wait = true;
  959. must_put = false;
  960. }
  961. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  962. /* ack and callback completed descriptor */
  963. spin_unlock_irq(&chan->lock);
  964. dmaengine_desc_callback_invoke(&cb, NULL);
  965. if (must_put)
  966. nbpf_desc_put(desc);
  967. }
  968. }
  969. static irqreturn_t nbpf_chan_irq(int irq, void *dev)
  970. {
  971. struct nbpf_channel *chan = dev;
  972. bool done = nbpf_status_get(chan);
  973. struct nbpf_desc *desc;
  974. irqreturn_t ret;
  975. bool bh = false;
  976. if (!done)
  977. return IRQ_NONE;
  978. nbpf_status_ack(chan);
  979. dev_dbg(&chan->dma_chan.dev->device, "%s()\n", __func__);
  980. spin_lock(&chan->lock);
  981. desc = chan->running;
  982. if (WARN_ON(!desc)) {
  983. ret = IRQ_NONE;
  984. goto unlock;
  985. } else {
  986. ret = IRQ_HANDLED;
  987. bh = true;
  988. }
  989. list_move_tail(&desc->node, &chan->done);
  990. chan->running = NULL;
  991. if (!list_empty(&chan->active)) {
  992. desc = list_first_entry(&chan->active,
  993. struct nbpf_desc, node);
  994. if (!nbpf_start(desc))
  995. chan->running = desc;
  996. }
  997. unlock:
  998. spin_unlock(&chan->lock);
  999. if (bh)
  1000. tasklet_schedule(&chan->tasklet);
  1001. return ret;
  1002. }
  1003. static irqreturn_t nbpf_err_irq(int irq, void *dev)
  1004. {
  1005. struct nbpf_device *nbpf = dev;
  1006. u32 error = nbpf_error_get(nbpf);
  1007. dev_warn(nbpf->dma_dev.dev, "DMA error IRQ %u\n", irq);
  1008. if (!error)
  1009. return IRQ_NONE;
  1010. do {
  1011. struct nbpf_channel *chan = nbpf_error_get_channel(nbpf, error);
  1012. /* On error: abort all queued transfers, no callback */
  1013. nbpf_error_clear(chan);
  1014. nbpf_chan_idle(chan);
  1015. error = nbpf_error_get(nbpf);
  1016. } while (error);
  1017. return IRQ_HANDLED;
  1018. }
  1019. static int nbpf_chan_probe(struct nbpf_device *nbpf, int n)
  1020. {
  1021. struct dma_device *dma_dev = &nbpf->dma_dev;
  1022. struct nbpf_channel *chan = nbpf->chan + n;
  1023. int ret;
  1024. chan->nbpf = nbpf;
  1025. chan->base = nbpf->base + NBPF_REG_CHAN_OFFSET + NBPF_REG_CHAN_SIZE * n;
  1026. INIT_LIST_HEAD(&chan->desc_page);
  1027. spin_lock_init(&chan->lock);
  1028. chan->dma_chan.device = dma_dev;
  1029. dma_cookie_init(&chan->dma_chan);
  1030. nbpf_chan_prepare_default(chan);
  1031. dev_dbg(dma_dev->dev, "%s(): channel %d: -> %p\n", __func__, n, chan->base);
  1032. snprintf(chan->name, sizeof(chan->name), "nbpf %d", n);
  1033. tasklet_init(&chan->tasklet, nbpf_chan_tasklet, (unsigned long)chan);
  1034. ret = devm_request_irq(dma_dev->dev, chan->irq,
  1035. nbpf_chan_irq, IRQF_SHARED,
  1036. chan->name, chan);
  1037. if (ret < 0)
  1038. return ret;
  1039. /* Add the channel to DMA device channel list */
  1040. list_add_tail(&chan->dma_chan.device_node,
  1041. &dma_dev->channels);
  1042. return 0;
  1043. }
  1044. static const struct of_device_id nbpf_match[] = {
  1045. {.compatible = "renesas,nbpfaxi64dmac1b4", .data = &nbpf_cfg[NBPF1B4]},
  1046. {.compatible = "renesas,nbpfaxi64dmac1b8", .data = &nbpf_cfg[NBPF1B8]},
  1047. {.compatible = "renesas,nbpfaxi64dmac1b16", .data = &nbpf_cfg[NBPF1B16]},
  1048. {.compatible = "renesas,nbpfaxi64dmac4b4", .data = &nbpf_cfg[NBPF4B4]},
  1049. {.compatible = "renesas,nbpfaxi64dmac4b8", .data = &nbpf_cfg[NBPF4B8]},
  1050. {.compatible = "renesas,nbpfaxi64dmac4b16", .data = &nbpf_cfg[NBPF4B16]},
  1051. {.compatible = "renesas,nbpfaxi64dmac8b4", .data = &nbpf_cfg[NBPF8B4]},
  1052. {.compatible = "renesas,nbpfaxi64dmac8b8", .data = &nbpf_cfg[NBPF8B8]},
  1053. {.compatible = "renesas,nbpfaxi64dmac8b16", .data = &nbpf_cfg[NBPF8B16]},
  1054. {}
  1055. };
  1056. MODULE_DEVICE_TABLE(of, nbpf_match);
  1057. static int nbpf_probe(struct platform_device *pdev)
  1058. {
  1059. struct device *dev = &pdev->dev;
  1060. const struct of_device_id *of_id = of_match_device(nbpf_match, dev);
  1061. struct device_node *np = dev->of_node;
  1062. struct nbpf_device *nbpf;
  1063. struct dma_device *dma_dev;
  1064. struct resource *iomem, *irq_res;
  1065. const struct nbpf_config *cfg;
  1066. int num_channels;
  1067. int ret, irq, eirq, i;
  1068. int irqbuf[9] /* maximum 8 channels + error IRQ */;
  1069. unsigned int irqs = 0;
  1070. BUILD_BUG_ON(sizeof(struct nbpf_desc_page) > PAGE_SIZE);
  1071. /* DT only */
  1072. if (!np || !of_id || !of_id->data)
  1073. return -ENODEV;
  1074. cfg = of_id->data;
  1075. num_channels = cfg->num_channels;
  1076. nbpf = devm_kzalloc(dev, sizeof(*nbpf) + num_channels *
  1077. sizeof(nbpf->chan[0]), GFP_KERNEL);
  1078. if (!nbpf)
  1079. return -ENOMEM;
  1080. dma_dev = &nbpf->dma_dev;
  1081. dma_dev->dev = dev;
  1082. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1083. nbpf->base = devm_ioremap_resource(dev, iomem);
  1084. if (IS_ERR(nbpf->base))
  1085. return PTR_ERR(nbpf->base);
  1086. nbpf->clk = devm_clk_get(dev, NULL);
  1087. if (IS_ERR(nbpf->clk))
  1088. return PTR_ERR(nbpf->clk);
  1089. nbpf->config = cfg;
  1090. for (i = 0; irqs < ARRAY_SIZE(irqbuf); i++) {
  1091. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1092. if (!irq_res)
  1093. break;
  1094. for (irq = irq_res->start; irq <= irq_res->end;
  1095. irq++, irqs++)
  1096. irqbuf[irqs] = irq;
  1097. }
  1098. /*
  1099. * 3 IRQ resource schemes are supported:
  1100. * 1. 1 shared IRQ for error and all channels
  1101. * 2. 2 IRQs: one for error and one shared for all channels
  1102. * 3. 1 IRQ for error and an own IRQ for each channel
  1103. */
  1104. if (irqs != 1 && irqs != 2 && irqs != num_channels + 1)
  1105. return -ENXIO;
  1106. if (irqs == 1) {
  1107. eirq = irqbuf[0];
  1108. for (i = 0; i <= num_channels; i++)
  1109. nbpf->chan[i].irq = irqbuf[0];
  1110. } else {
  1111. eirq = platform_get_irq_byname(pdev, "error");
  1112. if (eirq < 0)
  1113. return eirq;
  1114. if (irqs == num_channels + 1) {
  1115. struct nbpf_channel *chan;
  1116. for (i = 0, chan = nbpf->chan; i <= num_channels;
  1117. i++, chan++) {
  1118. /* Skip the error IRQ */
  1119. if (irqbuf[i] == eirq)
  1120. i++;
  1121. chan->irq = irqbuf[i];
  1122. }
  1123. if (chan != nbpf->chan + num_channels)
  1124. return -EINVAL;
  1125. } else {
  1126. /* 2 IRQs and more than one channel */
  1127. if (irqbuf[0] == eirq)
  1128. irq = irqbuf[1];
  1129. else
  1130. irq = irqbuf[0];
  1131. for (i = 0; i <= num_channels; i++)
  1132. nbpf->chan[i].irq = irq;
  1133. }
  1134. }
  1135. ret = devm_request_irq(dev, eirq, nbpf_err_irq,
  1136. IRQF_SHARED, "dma error", nbpf);
  1137. if (ret < 0)
  1138. return ret;
  1139. nbpf->eirq = eirq;
  1140. INIT_LIST_HEAD(&dma_dev->channels);
  1141. /* Create DMA Channel */
  1142. for (i = 0; i < num_channels; i++) {
  1143. ret = nbpf_chan_probe(nbpf, i);
  1144. if (ret < 0)
  1145. return ret;
  1146. }
  1147. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  1148. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  1149. dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
  1150. dma_cap_set(DMA_SG, dma_dev->cap_mask);
  1151. /* Common and MEMCPY operations */
  1152. dma_dev->device_alloc_chan_resources
  1153. = nbpf_alloc_chan_resources;
  1154. dma_dev->device_free_chan_resources = nbpf_free_chan_resources;
  1155. dma_dev->device_prep_dma_sg = nbpf_prep_memcpy_sg;
  1156. dma_dev->device_prep_dma_memcpy = nbpf_prep_memcpy;
  1157. dma_dev->device_tx_status = nbpf_tx_status;
  1158. dma_dev->device_issue_pending = nbpf_issue_pending;
  1159. /*
  1160. * If we drop support for unaligned MEMCPY buffer addresses and / or
  1161. * lengths by setting
  1162. * dma_dev->copy_align = 4;
  1163. * then we can set transfer length to 4 bytes in nbpf_prep_one() for
  1164. * DMA_MEM_TO_MEM
  1165. */
  1166. /* Compulsory for DMA_SLAVE fields */
  1167. dma_dev->device_prep_slave_sg = nbpf_prep_slave_sg;
  1168. dma_dev->device_config = nbpf_config;
  1169. dma_dev->device_pause = nbpf_pause;
  1170. dma_dev->device_terminate_all = nbpf_terminate_all;
  1171. dma_dev->src_addr_widths = NBPF_DMA_BUSWIDTHS;
  1172. dma_dev->dst_addr_widths = NBPF_DMA_BUSWIDTHS;
  1173. dma_dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1174. platform_set_drvdata(pdev, nbpf);
  1175. ret = clk_prepare_enable(nbpf->clk);
  1176. if (ret < 0)
  1177. return ret;
  1178. nbpf_configure(nbpf);
  1179. ret = dma_async_device_register(dma_dev);
  1180. if (ret < 0)
  1181. goto e_clk_off;
  1182. ret = of_dma_controller_register(np, nbpf_of_xlate, nbpf);
  1183. if (ret < 0)
  1184. goto e_dma_dev_unreg;
  1185. return 0;
  1186. e_dma_dev_unreg:
  1187. dma_async_device_unregister(dma_dev);
  1188. e_clk_off:
  1189. clk_disable_unprepare(nbpf->clk);
  1190. return ret;
  1191. }
  1192. static int nbpf_remove(struct platform_device *pdev)
  1193. {
  1194. struct nbpf_device *nbpf = platform_get_drvdata(pdev);
  1195. int i;
  1196. devm_free_irq(&pdev->dev, nbpf->eirq, nbpf);
  1197. for (i = 0; i < nbpf->config->num_channels; i++) {
  1198. struct nbpf_channel *chan = nbpf->chan + i;
  1199. devm_free_irq(&pdev->dev, chan->irq, chan);
  1200. tasklet_kill(&chan->tasklet);
  1201. }
  1202. of_dma_controller_free(pdev->dev.of_node);
  1203. dma_async_device_unregister(&nbpf->dma_dev);
  1204. clk_disable_unprepare(nbpf->clk);
  1205. return 0;
  1206. }
  1207. static const struct platform_device_id nbpf_ids[] = {
  1208. {"nbpfaxi64dmac1b4", (kernel_ulong_t)&nbpf_cfg[NBPF1B4]},
  1209. {"nbpfaxi64dmac1b8", (kernel_ulong_t)&nbpf_cfg[NBPF1B8]},
  1210. {"nbpfaxi64dmac1b16", (kernel_ulong_t)&nbpf_cfg[NBPF1B16]},
  1211. {"nbpfaxi64dmac4b4", (kernel_ulong_t)&nbpf_cfg[NBPF4B4]},
  1212. {"nbpfaxi64dmac4b8", (kernel_ulong_t)&nbpf_cfg[NBPF4B8]},
  1213. {"nbpfaxi64dmac4b16", (kernel_ulong_t)&nbpf_cfg[NBPF4B16]},
  1214. {"nbpfaxi64dmac8b4", (kernel_ulong_t)&nbpf_cfg[NBPF8B4]},
  1215. {"nbpfaxi64dmac8b8", (kernel_ulong_t)&nbpf_cfg[NBPF8B8]},
  1216. {"nbpfaxi64dmac8b16", (kernel_ulong_t)&nbpf_cfg[NBPF8B16]},
  1217. {},
  1218. };
  1219. MODULE_DEVICE_TABLE(platform, nbpf_ids);
  1220. #ifdef CONFIG_PM
  1221. static int nbpf_runtime_suspend(struct device *dev)
  1222. {
  1223. struct nbpf_device *nbpf = platform_get_drvdata(to_platform_device(dev));
  1224. clk_disable_unprepare(nbpf->clk);
  1225. return 0;
  1226. }
  1227. static int nbpf_runtime_resume(struct device *dev)
  1228. {
  1229. struct nbpf_device *nbpf = platform_get_drvdata(to_platform_device(dev));
  1230. return clk_prepare_enable(nbpf->clk);
  1231. }
  1232. #endif
  1233. static const struct dev_pm_ops nbpf_pm_ops = {
  1234. SET_RUNTIME_PM_OPS(nbpf_runtime_suspend, nbpf_runtime_resume, NULL)
  1235. };
  1236. static struct platform_driver nbpf_driver = {
  1237. .driver = {
  1238. .name = "dma-nbpf",
  1239. .of_match_table = nbpf_match,
  1240. .pm = &nbpf_pm_ops,
  1241. },
  1242. .id_table = nbpf_ids,
  1243. .probe = nbpf_probe,
  1244. .remove = nbpf_remove,
  1245. };
  1246. module_platform_driver(nbpf_driver);
  1247. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1248. MODULE_DESCRIPTION("dmaengine driver for NBPFAXI64* DMACs");
  1249. MODULE_LICENSE("GPL v2");