100-v5.11-dmaengine-qcom-add_ADM_driver.patch 26 KB

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