nbpfaxi.c 41 KB

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