qcom_adm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/dma/qcom_adm.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reset.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/slab.h>
  24. #include "../dmaengine.h"
  25. #include "../virt-dma.h"
  26. /* ADM registers - calculated from channel number and security domain */
  27. #define ADM_CHAN_MULTI 0x4
  28. #define ADM_CI_MULTI 0x4
  29. #define ADM_CRCI_MULTI 0x4
  30. #define ADM_EE_MULTI 0x800
  31. #define ADM_CHAN_OFFS(chan) (ADM_CHAN_MULTI * (chan))
  32. #define ADM_EE_OFFS(ee) (ADM_EE_MULTI * (ee))
  33. #define ADM_CHAN_EE_OFFS(chan, ee) (ADM_CHAN_OFFS(chan) + ADM_EE_OFFS(ee))
  34. #define ADM_CHAN_OFFS(chan) (ADM_CHAN_MULTI * (chan))
  35. #define ADM_CI_OFFS(ci) (ADM_CHAN_OFF(ci))
  36. #define ADM_CH_CMD_PTR(chan, ee) (ADM_CHAN_EE_OFFS(chan, ee))
  37. #define ADM_CH_RSLT(chan, ee) (0x40 + ADM_CHAN_EE_OFFS(chan, ee))
  38. #define ADM_CH_FLUSH_STATE0(chan, ee) (0x80 + ADM_CHAN_EE_OFFS(chan, ee))
  39. #define ADM_CH_STATUS_SD(chan, ee) (0x200 + ADM_CHAN_EE_OFFS(chan, ee))
  40. #define ADM_CH_CONF(chan) (0x240 + ADM_CHAN_OFFS(chan))
  41. #define ADM_CH_RSLT_CONF(chan, ee) (0x300 + ADM_CHAN_EE_OFFS(chan, ee))
  42. #define ADM_SEC_DOMAIN_IRQ_STATUS(ee) (0x380 + ADM_EE_OFFS(ee))
  43. #define ADM_CI_CONF(ci) (0x390 + (ci) * ADM_CI_MULTI)
  44. #define ADM_GP_CTL 0x3d8
  45. #define ADM_CRCI_CTL(crci, ee) (0x400 + (crci) * ADM_CRCI_MULTI + \
  46. ADM_EE_OFFS(ee))
  47. /* channel status */
  48. #define ADM_CH_STATUS_VALID BIT(1)
  49. /* channel result */
  50. #define ADM_CH_RSLT_VALID BIT(31)
  51. #define ADM_CH_RSLT_ERR BIT(3)
  52. #define ADM_CH_RSLT_FLUSH BIT(2)
  53. #define ADM_CH_RSLT_TPD BIT(1)
  54. /* channel conf */
  55. #define ADM_CH_CONF_SHADOW_EN BIT(12)
  56. #define ADM_CH_CONF_MPU_DISABLE BIT(11)
  57. #define ADM_CH_CONF_PERM_MPU_CONF BIT(9)
  58. #define ADM_CH_CONF_FORCE_RSLT_EN BIT(7)
  59. #define ADM_CH_CONF_SEC_DOMAIN(ee) ((((ee) & 0x3) << 4) | (((ee) & 0x4) << 11))
  60. /* channel result conf */
  61. #define ADM_CH_RSLT_CONF_FLUSH_EN BIT(1)
  62. #define ADM_CH_RSLT_CONF_IRQ_EN BIT(0)
  63. /* CRCI CTL */
  64. #define ADM_CRCI_CTL_MUX_SEL BIT(18)
  65. #define ADM_CRCI_CTL_RST BIT(17)
  66. /* CI configuration */
  67. #define ADM_CI_RANGE_END(x) ((x) << 24)
  68. #define ADM_CI_RANGE_START(x) ((x) << 16)
  69. #define ADM_CI_BURST_4_WORDS BIT(2)
  70. #define ADM_CI_BURST_8_WORDS BIT(3)
  71. /* GP CTL */
  72. #define ADM_GP_CTL_LP_EN BIT(12)
  73. #define ADM_GP_CTL_LP_CNT(x) ((x) << 8)
  74. /* Command pointer list entry */
  75. #define ADM_CPLE_LP BIT(31)
  76. #define ADM_CPLE_CMD_PTR_LIST BIT(29)
  77. /* Command list entry */
  78. #define ADM_CMD_LC BIT(31)
  79. #define ADM_CMD_DST_CRCI(n) (((n) & 0xf) << 7)
  80. #define ADM_CMD_SRC_CRCI(n) (((n) & 0xf) << 3)
  81. #define ADM_CMD_TYPE_SINGLE 0x0
  82. #define ADM_CMD_TYPE_BOX 0x3
  83. #define ADM_CRCI_MUX_SEL BIT(4)
  84. #define ADM_DESC_ALIGN 8
  85. #define ADM_MAX_XFER (SZ_64K - 1)
  86. #define ADM_MAX_ROWS (SZ_64K - 1)
  87. #define ADM_MAX_CHANNELS 16
  88. struct adm_desc_hw_box {
  89. u32 cmd;
  90. u32 src_addr;
  91. u32 dst_addr;
  92. u32 row_len;
  93. u32 num_rows;
  94. u32 row_offset;
  95. };
  96. struct adm_desc_hw_single {
  97. u32 cmd;
  98. u32 src_addr;
  99. u32 dst_addr;
  100. u32 len;
  101. };
  102. struct adm_async_desc {
  103. struct virt_dma_desc vd;
  104. struct adm_device *adev;
  105. size_t length;
  106. enum dma_transfer_direction dir;
  107. dma_addr_t dma_addr;
  108. size_t dma_len;
  109. void *cpl;
  110. dma_addr_t cp_addr;
  111. u32 crci;
  112. u32 mux;
  113. u32 blk_size;
  114. };
  115. struct adm_chan {
  116. struct virt_dma_chan vc;
  117. struct adm_device *adev;
  118. /* parsed from DT */
  119. u32 id; /* channel id */
  120. struct adm_async_desc *curr_txd;
  121. struct dma_slave_config slave;
  122. u32 crci;
  123. u32 mux;
  124. struct list_head node;
  125. int error;
  126. int initialized;
  127. };
  128. static inline struct adm_chan *to_adm_chan(struct dma_chan *common)
  129. {
  130. return container_of(common, struct adm_chan, vc.chan);
  131. }
  132. struct adm_device {
  133. void __iomem *regs;
  134. struct device *dev;
  135. struct dma_device common;
  136. struct device_dma_parameters dma_parms;
  137. struct adm_chan *channels;
  138. u32 ee;
  139. struct clk *core_clk;
  140. struct clk *iface_clk;
  141. struct reset_control *clk_reset;
  142. struct reset_control *c0_reset;
  143. struct reset_control *c1_reset;
  144. struct reset_control *c2_reset;
  145. int irq;
  146. };
  147. /**
  148. * adm_free_chan - Frees dma resources associated with the specific channel
  149. *
  150. * @chan: dma channel
  151. *
  152. * Free all allocated descriptors associated with this channel
  153. */
  154. static void adm_free_chan(struct dma_chan *chan)
  155. {
  156. /* free all queued descriptors */
  157. vchan_free_chan_resources(to_virt_chan(chan));
  158. }
  159. /**
  160. * adm_get_blksize - Get block size from burst value
  161. *
  162. * @burst: Burst size of transaction
  163. */
  164. static int adm_get_blksize(unsigned int burst)
  165. {
  166. int ret;
  167. switch (burst) {
  168. case 16:
  169. case 32:
  170. case 64:
  171. case 128:
  172. ret = ffs(burst >> 4) - 1;
  173. break;
  174. case 192:
  175. ret = 4;
  176. break;
  177. case 256:
  178. ret = 5;
  179. break;
  180. default:
  181. ret = -EINVAL;
  182. break;
  183. }
  184. return ret;
  185. }
  186. /**
  187. * adm_process_fc_descriptors - Process descriptors for flow controlled xfers
  188. *
  189. * @achan: ADM channel
  190. * @desc: Descriptor memory pointer
  191. * @sg: Scatterlist entry
  192. * @crci: CRCI value
  193. * @burst: Burst size of transaction
  194. * @direction: DMA transfer direction
  195. */
  196. static void *adm_process_fc_descriptors(struct adm_chan *achan, void *desc,
  197. struct scatterlist *sg, u32 crci,
  198. u32 burst,
  199. enum dma_transfer_direction direction)
  200. {
  201. struct adm_desc_hw_box *box_desc = NULL;
  202. struct adm_desc_hw_single *single_desc;
  203. u32 remainder = sg_dma_len(sg);
  204. u32 rows, row_offset, crci_cmd;
  205. u32 mem_addr = sg_dma_address(sg);
  206. u32 *incr_addr = &mem_addr;
  207. u32 *src, *dst;
  208. if (direction == DMA_DEV_TO_MEM) {
  209. crci_cmd = ADM_CMD_SRC_CRCI(crci);
  210. row_offset = burst;
  211. src = &achan->slave.src_addr;
  212. dst = &mem_addr;
  213. } else {
  214. crci_cmd = ADM_CMD_DST_CRCI(crci);
  215. row_offset = burst << 16;
  216. src = &mem_addr;
  217. dst = &achan->slave.dst_addr;
  218. }
  219. while (remainder >= burst) {
  220. box_desc = desc;
  221. box_desc->cmd = ADM_CMD_TYPE_BOX | crci_cmd;
  222. box_desc->row_offset = row_offset;
  223. box_desc->src_addr = *src;
  224. box_desc->dst_addr = *dst;
  225. rows = remainder / burst;
  226. rows = min_t(u32, rows, ADM_MAX_ROWS);
  227. box_desc->num_rows = rows << 16 | rows;
  228. box_desc->row_len = burst << 16 | burst;
  229. *incr_addr += burst * rows;
  230. remainder -= burst * rows;
  231. desc += sizeof(*box_desc);
  232. }
  233. /* if leftover bytes, do one single descriptor */
  234. if (remainder) {
  235. single_desc = desc;
  236. single_desc->cmd = ADM_CMD_TYPE_SINGLE | crci_cmd;
  237. single_desc->len = remainder;
  238. single_desc->src_addr = *src;
  239. single_desc->dst_addr = *dst;
  240. desc += sizeof(*single_desc);
  241. if (sg_is_last(sg))
  242. single_desc->cmd |= ADM_CMD_LC;
  243. } else {
  244. if (box_desc && sg_is_last(sg))
  245. box_desc->cmd |= ADM_CMD_LC;
  246. }
  247. return desc;
  248. }
  249. /**
  250. * adm_process_non_fc_descriptors - Process descriptors for non-fc xfers
  251. *
  252. * @achan: ADM channel
  253. * @desc: Descriptor memory pointer
  254. * @sg: Scatterlist entry
  255. * @direction: DMA transfer direction
  256. */
  257. static void *adm_process_non_fc_descriptors(struct adm_chan *achan, void *desc,
  258. struct scatterlist *sg,
  259. enum dma_transfer_direction direction)
  260. {
  261. struct adm_desc_hw_single *single_desc;
  262. u32 remainder = sg_dma_len(sg);
  263. u32 mem_addr = sg_dma_address(sg);
  264. u32 *incr_addr = &mem_addr;
  265. u32 *src, *dst;
  266. if (direction == DMA_DEV_TO_MEM) {
  267. src = &achan->slave.src_addr;
  268. dst = &mem_addr;
  269. } else {
  270. src = &mem_addr;
  271. dst = &achan->slave.dst_addr;
  272. }
  273. do {
  274. single_desc = desc;
  275. single_desc->cmd = ADM_CMD_TYPE_SINGLE;
  276. single_desc->src_addr = *src;
  277. single_desc->dst_addr = *dst;
  278. single_desc->len = (remainder > ADM_MAX_XFER) ?
  279. ADM_MAX_XFER : remainder;
  280. remainder -= single_desc->len;
  281. *incr_addr += single_desc->len;
  282. desc += sizeof(*single_desc);
  283. } while (remainder);
  284. /* set last command if this is the end of the whole transaction */
  285. if (sg_is_last(sg))
  286. single_desc->cmd |= ADM_CMD_LC;
  287. return desc;
  288. }
  289. /**
  290. * adm_prep_slave_sg - Prep slave sg transaction
  291. *
  292. * @chan: dma channel
  293. * @sgl: scatter gather list
  294. * @sg_len: length of sg
  295. * @direction: DMA transfer direction
  296. * @flags: DMA flags
  297. * @context: transfer context (unused)
  298. */
  299. static struct dma_async_tx_descriptor *adm_prep_slave_sg(struct dma_chan *chan,
  300. struct scatterlist *sgl,
  301. unsigned int sg_len,
  302. enum dma_transfer_direction direction,
  303. unsigned long flags,
  304. void *context)
  305. {
  306. struct adm_chan *achan = to_adm_chan(chan);
  307. struct adm_device *adev = achan->adev;
  308. struct adm_async_desc *async_desc;
  309. struct scatterlist *sg;
  310. dma_addr_t cple_addr;
  311. u32 i, burst;
  312. u32 single_count = 0, box_count = 0, crci = 0;
  313. void *desc;
  314. u32 *cple;
  315. int blk_size = 0;
  316. if (!is_slave_direction(direction)) {
  317. dev_err(adev->dev, "invalid dma direction\n");
  318. return NULL;
  319. }
  320. /*
  321. * get burst value from slave configuration
  322. */
  323. burst = (direction == DMA_MEM_TO_DEV) ?
  324. achan->slave.dst_maxburst :
  325. achan->slave.src_maxburst;
  326. /* if using flow control, validate burst and crci values */
  327. if (achan->slave.device_fc) {
  328. blk_size = adm_get_blksize(burst);
  329. if (blk_size < 0) {
  330. dev_err(adev->dev, "invalid burst value: %d\n",
  331. burst);
  332. return NULL;
  333. }
  334. crci = achan->crci & 0xf;
  335. if (!crci || achan->crci > 0x1f) {
  336. dev_err(adev->dev, "invalid crci value\n");
  337. return NULL;
  338. }
  339. }
  340. /* iterate through sgs and compute allocation size of structures */
  341. for_each_sg(sgl, sg, sg_len, i) {
  342. if (achan->slave.device_fc) {
  343. box_count += DIV_ROUND_UP(sg_dma_len(sg) / burst,
  344. ADM_MAX_ROWS);
  345. if (sg_dma_len(sg) % burst)
  346. single_count++;
  347. } else {
  348. single_count += DIV_ROUND_UP(sg_dma_len(sg),
  349. ADM_MAX_XFER);
  350. }
  351. }
  352. async_desc = kzalloc(sizeof(*async_desc), GFP_NOWAIT);
  353. if (!async_desc) {
  354. dev_err(adev->dev, "not enough memory for async_desc struct\n");
  355. return NULL;
  356. }
  357. async_desc->mux = achan->mux ? ADM_CRCI_CTL_MUX_SEL : 0;
  358. async_desc->crci = crci;
  359. async_desc->blk_size = blk_size;
  360. async_desc->dma_len = single_count * sizeof(struct adm_desc_hw_single) +
  361. box_count * sizeof(struct adm_desc_hw_box) +
  362. sizeof(*cple) + 2 * ADM_DESC_ALIGN;
  363. async_desc->cpl = kzalloc(async_desc->dma_len, GFP_NOWAIT);
  364. if (!async_desc->cpl) {
  365. dev_err(adev->dev, "not enough memory for cpl struct\n");
  366. goto free;
  367. }
  368. async_desc->adev = adev;
  369. /* both command list entry and descriptors must be 8 byte aligned */
  370. cple = PTR_ALIGN(async_desc->cpl, ADM_DESC_ALIGN);
  371. desc = PTR_ALIGN(cple + 1, ADM_DESC_ALIGN);
  372. for_each_sg(sgl, sg, sg_len, i) {
  373. async_desc->length += sg_dma_len(sg);
  374. if (achan->slave.device_fc)
  375. desc = adm_process_fc_descriptors(achan, desc, sg, crci,
  376. burst, direction);
  377. else
  378. desc = adm_process_non_fc_descriptors(achan, desc, sg,
  379. direction);
  380. }
  381. async_desc->dma_addr = dma_map_single(adev->dev, async_desc->cpl,
  382. async_desc->dma_len,
  383. DMA_TO_DEVICE);
  384. if (dma_mapping_error(adev->dev, async_desc->dma_addr)) {
  385. dev_err(adev->dev, "dma mapping error for cpl\n");
  386. goto free;
  387. }
  388. cple_addr = async_desc->dma_addr + ((void *)cple - async_desc->cpl);
  389. /* init cmd list */
  390. dma_sync_single_for_cpu(adev->dev, cple_addr, sizeof(*cple),
  391. DMA_TO_DEVICE);
  392. *cple = ADM_CPLE_LP;
  393. *cple |= (async_desc->dma_addr + ADM_DESC_ALIGN) >> 3;
  394. dma_sync_single_for_device(adev->dev, cple_addr, sizeof(*cple),
  395. DMA_TO_DEVICE);
  396. return vchan_tx_prep(&achan->vc, &async_desc->vd, flags);
  397. free:
  398. kfree(async_desc);
  399. return NULL;
  400. }
  401. /**
  402. * adm_terminate_all - terminate all transactions on a channel
  403. * @chan: dma channel
  404. *
  405. * Dequeues and frees all transactions, aborts current transaction
  406. * No callbacks are done
  407. *
  408. */
  409. static int adm_terminate_all(struct dma_chan *chan)
  410. {
  411. struct adm_chan *achan = to_adm_chan(chan);
  412. struct adm_device *adev = achan->adev;
  413. unsigned long flags;
  414. LIST_HEAD(head);
  415. spin_lock_irqsave(&achan->vc.lock, flags);
  416. vchan_get_all_descriptors(&achan->vc, &head);
  417. /* send flush command to terminate current transaction */
  418. writel_relaxed(0x0,
  419. adev->regs + ADM_CH_FLUSH_STATE0(achan->id, adev->ee));
  420. spin_unlock_irqrestore(&achan->vc.lock, flags);
  421. vchan_dma_desc_free_list(&achan->vc, &head);
  422. return 0;
  423. }
  424. static int adm_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
  425. {
  426. struct adm_chan *achan = to_adm_chan(chan);
  427. struct qcom_adm_peripheral_config *config = cfg->peripheral_config;
  428. unsigned long flag;
  429. spin_lock_irqsave(&achan->vc.lock, flag);
  430. memcpy(&achan->slave, cfg, sizeof(struct dma_slave_config));
  431. if (cfg->peripheral_size == sizeof(*config))
  432. achan->crci = config->crci;
  433. spin_unlock_irqrestore(&achan->vc.lock, flag);
  434. return 0;
  435. }
  436. /**
  437. * adm_start_dma - start next transaction
  438. * @achan: ADM dma channel
  439. */
  440. static void adm_start_dma(struct adm_chan *achan)
  441. {
  442. struct virt_dma_desc *vd = vchan_next_desc(&achan->vc);
  443. struct adm_device *adev = achan->adev;
  444. struct adm_async_desc *async_desc;
  445. lockdep_assert_held(&achan->vc.lock);
  446. if (!vd)
  447. return;
  448. list_del(&vd->node);
  449. /* write next command list out to the CMD FIFO */
  450. async_desc = container_of(vd, struct adm_async_desc, vd);
  451. achan->curr_txd = async_desc;
  452. /* reset channel error */
  453. achan->error = 0;
  454. if (!achan->initialized) {
  455. /* enable interrupts */
  456. writel(ADM_CH_CONF_SHADOW_EN |
  457. ADM_CH_CONF_PERM_MPU_CONF |
  458. ADM_CH_CONF_MPU_DISABLE |
  459. ADM_CH_CONF_SEC_DOMAIN(adev->ee),
  460. adev->regs + ADM_CH_CONF(achan->id));
  461. writel(ADM_CH_RSLT_CONF_IRQ_EN | ADM_CH_RSLT_CONF_FLUSH_EN,
  462. adev->regs + ADM_CH_RSLT_CONF(achan->id, adev->ee));
  463. achan->initialized = 1;
  464. }
  465. /* set the crci block size if this transaction requires CRCI */
  466. if (async_desc->crci) {
  467. writel(async_desc->mux | async_desc->blk_size,
  468. adev->regs + ADM_CRCI_CTL(async_desc->crci, adev->ee));
  469. }
  470. /* make sure IRQ enable doesn't get reordered */
  471. wmb();
  472. /* write next command list out to the CMD FIFO */
  473. writel(ALIGN(async_desc->dma_addr, ADM_DESC_ALIGN) >> 3,
  474. adev->regs + ADM_CH_CMD_PTR(achan->id, adev->ee));
  475. }
  476. /**
  477. * adm_dma_irq - irq handler for ADM controller
  478. * @irq: IRQ of interrupt
  479. * @data: callback data
  480. *
  481. * IRQ handler for the bam controller
  482. */
  483. static irqreturn_t adm_dma_irq(int irq, void *data)
  484. {
  485. struct adm_device *adev = data;
  486. u32 srcs, i;
  487. struct adm_async_desc *async_desc;
  488. unsigned long flags;
  489. srcs = readl_relaxed(adev->regs +
  490. ADM_SEC_DOMAIN_IRQ_STATUS(adev->ee));
  491. for (i = 0; i < ADM_MAX_CHANNELS; i++) {
  492. struct adm_chan *achan = &adev->channels[i];
  493. u32 status, result;
  494. if (srcs & BIT(i)) {
  495. status = readl_relaxed(adev->regs +
  496. ADM_CH_STATUS_SD(i, adev->ee));
  497. /* if no result present, skip */
  498. if (!(status & ADM_CH_STATUS_VALID))
  499. continue;
  500. result = readl_relaxed(adev->regs +
  501. ADM_CH_RSLT(i, adev->ee));
  502. /* no valid results, skip */
  503. if (!(result & ADM_CH_RSLT_VALID))
  504. continue;
  505. /* flag error if transaction was flushed or failed */
  506. if (result & (ADM_CH_RSLT_ERR | ADM_CH_RSLT_FLUSH))
  507. achan->error = 1;
  508. spin_lock_irqsave(&achan->vc.lock, flags);
  509. async_desc = achan->curr_txd;
  510. achan->curr_txd = NULL;
  511. if (async_desc) {
  512. vchan_cookie_complete(&async_desc->vd);
  513. /* kick off next DMA */
  514. adm_start_dma(achan);
  515. }
  516. spin_unlock_irqrestore(&achan->vc.lock, flags);
  517. }
  518. }
  519. return IRQ_HANDLED;
  520. }
  521. /**
  522. * adm_tx_status - returns status of transaction
  523. * @chan: dma channel
  524. * @cookie: transaction cookie
  525. * @txstate: DMA transaction state
  526. *
  527. * Return status of dma transaction
  528. */
  529. static enum dma_status adm_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  530. struct dma_tx_state *txstate)
  531. {
  532. struct adm_chan *achan = to_adm_chan(chan);
  533. struct virt_dma_desc *vd;
  534. enum dma_status ret;
  535. unsigned long flags;
  536. size_t residue = 0;
  537. ret = dma_cookie_status(chan, cookie, txstate);
  538. if (ret == DMA_COMPLETE || !txstate)
  539. return ret;
  540. spin_lock_irqsave(&achan->vc.lock, flags);
  541. vd = vchan_find_desc(&achan->vc, cookie);
  542. if (vd)
  543. residue = container_of(vd, struct adm_async_desc, vd)->length;
  544. spin_unlock_irqrestore(&achan->vc.lock, flags);
  545. /*
  546. * residue is either the full length if it is in the issued list, or 0
  547. * if it is in progress. We have no reliable way of determining
  548. * anything in between
  549. */
  550. dma_set_residue(txstate, residue);
  551. if (achan->error)
  552. return DMA_ERROR;
  553. return ret;
  554. }
  555. /**
  556. * adm_issue_pending - starts pending transactions
  557. * @chan: dma channel
  558. *
  559. * Issues all pending transactions and starts DMA
  560. */
  561. static void adm_issue_pending(struct dma_chan *chan)
  562. {
  563. struct adm_chan *achan = to_adm_chan(chan);
  564. unsigned long flags;
  565. spin_lock_irqsave(&achan->vc.lock, flags);
  566. if (vchan_issue_pending(&achan->vc) && !achan->curr_txd)
  567. adm_start_dma(achan);
  568. spin_unlock_irqrestore(&achan->vc.lock, flags);
  569. }
  570. /**
  571. * adm_dma_free_desc - free descriptor memory
  572. * @vd: virtual descriptor
  573. *
  574. */
  575. static void adm_dma_free_desc(struct virt_dma_desc *vd)
  576. {
  577. struct adm_async_desc *async_desc = container_of(vd,
  578. struct adm_async_desc, vd);
  579. dma_unmap_single(async_desc->adev->dev, async_desc->dma_addr,
  580. async_desc->dma_len, DMA_TO_DEVICE);
  581. kfree(async_desc->cpl);
  582. kfree(async_desc);
  583. }
  584. static void adm_channel_init(struct adm_device *adev, struct adm_chan *achan,
  585. u32 index)
  586. {
  587. achan->id = index;
  588. achan->adev = adev;
  589. vchan_init(&achan->vc, &adev->common);
  590. achan->vc.desc_free = adm_dma_free_desc;
  591. }
  592. /**
  593. * adm_dma_xlate
  594. * @dma_spec: pointer to DMA specifier as found in the device tree
  595. * @ofdma: pointer to DMA controller data
  596. *
  597. * This can use either 1-cell or 2-cell formats, the first cell
  598. * identifies the slave device, while the optional second cell
  599. * contains the crci value.
  600. *
  601. * Returns pointer to appropriate dma channel on success or NULL on error.
  602. */
  603. static struct dma_chan *adm_dma_xlate(struct of_phandle_args *dma_spec,
  604. struct of_dma *ofdma)
  605. {
  606. struct dma_device *dev = ofdma->of_dma_data;
  607. struct dma_chan *chan, *candidate = NULL;
  608. struct adm_chan *achan;
  609. if (!dev || dma_spec->args_count > 2)
  610. return NULL;
  611. list_for_each_entry(chan, &dev->channels, device_node)
  612. if (chan->chan_id == dma_spec->args[0]) {
  613. candidate = chan;
  614. break;
  615. }
  616. if (!candidate)
  617. return NULL;
  618. achan = to_adm_chan(candidate);
  619. if (dma_spec->args_count == 2)
  620. achan->crci = dma_spec->args[1];
  621. else
  622. achan->crci = 0;
  623. return dma_get_slave_channel(candidate);
  624. }
  625. static int adm_dma_probe(struct platform_device *pdev)
  626. {
  627. struct adm_device *adev;
  628. int ret;
  629. u32 i;
  630. adev = devm_kzalloc(&pdev->dev, sizeof(*adev), GFP_KERNEL);
  631. if (!adev)
  632. return -ENOMEM;
  633. adev->dev = &pdev->dev;
  634. adev->regs = devm_platform_ioremap_resource(pdev, 0);
  635. if (IS_ERR(adev->regs))
  636. return PTR_ERR(adev->regs);
  637. adev->irq = platform_get_irq(pdev, 0);
  638. if (adev->irq < 0)
  639. return adev->irq;
  640. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &adev->ee);
  641. if (ret) {
  642. dev_err(adev->dev, "Execution environment unspecified\n");
  643. return ret;
  644. }
  645. adev->core_clk = devm_clk_get(adev->dev, "core");
  646. if (IS_ERR(adev->core_clk))
  647. return PTR_ERR(adev->core_clk);
  648. adev->iface_clk = devm_clk_get(adev->dev, "iface");
  649. if (IS_ERR(adev->iface_clk))
  650. return PTR_ERR(adev->iface_clk);
  651. adev->clk_reset = devm_reset_control_get_exclusive(&pdev->dev, "clk");
  652. if (IS_ERR(adev->clk_reset)) {
  653. dev_err(adev->dev, "failed to get ADM0 reset\n");
  654. return PTR_ERR(adev->clk_reset);
  655. }
  656. adev->c0_reset = devm_reset_control_get_exclusive(&pdev->dev, "c0");
  657. if (IS_ERR(adev->c0_reset)) {
  658. dev_err(adev->dev, "failed to get ADM0 C0 reset\n");
  659. return PTR_ERR(adev->c0_reset);
  660. }
  661. adev->c1_reset = devm_reset_control_get_exclusive(&pdev->dev, "c1");
  662. if (IS_ERR(adev->c1_reset)) {
  663. dev_err(adev->dev, "failed to get ADM0 C1 reset\n");
  664. return PTR_ERR(adev->c1_reset);
  665. }
  666. adev->c2_reset = devm_reset_control_get_exclusive(&pdev->dev, "c2");
  667. if (IS_ERR(adev->c2_reset)) {
  668. dev_err(adev->dev, "failed to get ADM0 C2 reset\n");
  669. return PTR_ERR(adev->c2_reset);
  670. }
  671. ret = clk_prepare_enable(adev->core_clk);
  672. if (ret) {
  673. dev_err(adev->dev, "failed to prepare/enable core clock\n");
  674. return ret;
  675. }
  676. ret = clk_prepare_enable(adev->iface_clk);
  677. if (ret) {
  678. dev_err(adev->dev, "failed to prepare/enable iface clock\n");
  679. goto err_disable_core_clk;
  680. }
  681. reset_control_assert(adev->clk_reset);
  682. reset_control_assert(adev->c0_reset);
  683. reset_control_assert(adev->c1_reset);
  684. reset_control_assert(adev->c2_reset);
  685. udelay(2);
  686. reset_control_deassert(adev->clk_reset);
  687. reset_control_deassert(adev->c0_reset);
  688. reset_control_deassert(adev->c1_reset);
  689. reset_control_deassert(adev->c2_reset);
  690. adev->channels = devm_kcalloc(adev->dev, ADM_MAX_CHANNELS,
  691. sizeof(*adev->channels), GFP_KERNEL);
  692. if (!adev->channels) {
  693. ret = -ENOMEM;
  694. goto err_disable_clks;
  695. }
  696. /* allocate and initialize channels */
  697. INIT_LIST_HEAD(&adev->common.channels);
  698. for (i = 0; i < ADM_MAX_CHANNELS; i++)
  699. adm_channel_init(adev, &adev->channels[i], i);
  700. /* reset CRCIs */
  701. for (i = 0; i < 16; i++)
  702. writel(ADM_CRCI_CTL_RST, adev->regs +
  703. ADM_CRCI_CTL(i, adev->ee));
  704. /* configure client interfaces */
  705. writel(ADM_CI_RANGE_START(0x40) | ADM_CI_RANGE_END(0xb0) |
  706. ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(0));
  707. writel(ADM_CI_RANGE_START(0x2a) | ADM_CI_RANGE_END(0x2c) |
  708. ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(1));
  709. writel(ADM_CI_RANGE_START(0x12) | ADM_CI_RANGE_END(0x28) |
  710. ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(2));
  711. writel(ADM_GP_CTL_LP_EN | ADM_GP_CTL_LP_CNT(0xf),
  712. adev->regs + ADM_GP_CTL);
  713. ret = devm_request_irq(adev->dev, adev->irq, adm_dma_irq,
  714. 0, "adm_dma", adev);
  715. if (ret)
  716. goto err_disable_clks;
  717. platform_set_drvdata(pdev, adev);
  718. adev->common.dev = adev->dev;
  719. adev->common.dev->dma_parms = &adev->dma_parms;
  720. /* set capabilities */
  721. dma_cap_zero(adev->common.cap_mask);
  722. dma_cap_set(DMA_SLAVE, adev->common.cap_mask);
  723. dma_cap_set(DMA_PRIVATE, adev->common.cap_mask);
  724. /* initialize dmaengine apis */
  725. adev->common.directions = BIT(DMA_DEV_TO_MEM | DMA_MEM_TO_DEV);
  726. adev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  727. adev->common.src_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  728. adev->common.dst_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  729. adev->common.device_free_chan_resources = adm_free_chan;
  730. adev->common.device_prep_slave_sg = adm_prep_slave_sg;
  731. adev->common.device_issue_pending = adm_issue_pending;
  732. adev->common.device_tx_status = adm_tx_status;
  733. adev->common.device_terminate_all = adm_terminate_all;
  734. adev->common.device_config = adm_slave_config;
  735. ret = dma_async_device_register(&adev->common);
  736. if (ret) {
  737. dev_err(adev->dev, "failed to register dma async device\n");
  738. goto err_disable_clks;
  739. }
  740. ret = of_dma_controller_register(pdev->dev.of_node, adm_dma_xlate,
  741. &adev->common);
  742. if (ret)
  743. goto err_unregister_dma;
  744. return 0;
  745. err_unregister_dma:
  746. dma_async_device_unregister(&adev->common);
  747. err_disable_clks:
  748. clk_disable_unprepare(adev->iface_clk);
  749. err_disable_core_clk:
  750. clk_disable_unprepare(adev->core_clk);
  751. return ret;
  752. }
  753. static void adm_dma_remove(struct platform_device *pdev)
  754. {
  755. struct adm_device *adev = platform_get_drvdata(pdev);
  756. struct adm_chan *achan;
  757. u32 i;
  758. of_dma_controller_free(pdev->dev.of_node);
  759. dma_async_device_unregister(&adev->common);
  760. for (i = 0; i < ADM_MAX_CHANNELS; i++) {
  761. achan = &adev->channels[i];
  762. /* mask IRQs for this channel/EE pair */
  763. writel(0, adev->regs + ADM_CH_RSLT_CONF(achan->id, adev->ee));
  764. tasklet_kill(&adev->channels[i].vc.task);
  765. adm_terminate_all(&adev->channels[i].vc.chan);
  766. }
  767. devm_free_irq(adev->dev, adev->irq, adev);
  768. clk_disable_unprepare(adev->core_clk);
  769. clk_disable_unprepare(adev->iface_clk);
  770. }
  771. static const struct of_device_id adm_of_match[] = {
  772. { .compatible = "qcom,adm", },
  773. {}
  774. };
  775. MODULE_DEVICE_TABLE(of, adm_of_match);
  776. static struct platform_driver adm_dma_driver = {
  777. .probe = adm_dma_probe,
  778. .remove_new = adm_dma_remove,
  779. .driver = {
  780. .name = "adm-dma-engine",
  781. .of_match_table = adm_of_match,
  782. },
  783. };
  784. module_platform_driver(adm_dma_driver);
  785. MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  786. MODULE_DESCRIPTION("QCOM ADM DMA engine driver");
  787. MODULE_LICENSE("GPL v2");