nbpfaxi.c 41 KB

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