156-dmaengine-Add-ADM-driver.patch 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. Content-Type: text/plain; charset="utf-8"
  2. MIME-Version: 1.0
  3. Content-Transfer-Encoding: 7bit
  4. Subject: [v6,2/2] dmaengine: Add ADM driver
  5. From: Andy Gross <agross@codeaurora.org>
  6. X-Patchwork-Id: 6027351
  7. Message-Id: <1426571172-9711-3-git-send-email-agross@codeaurora.org>
  8. To: Vinod Koul <vinod.koul@intel.com>
  9. Cc: devicetree@vger.kernel.org, dmaengine@vger.kernel.org,
  10. linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org,
  11. linux-arm-kernel@lists.infradead.org, Kumar Gala <galak@codeaurora.org>,
  12. Bjorn Andersson <bjorn.andersson@sonymobile.com>,
  13. Andy Gross <agross@codeaurora.org>
  14. Date: Tue, 17 Mar 2015 00:46:12 -0500
  15. Add the DMA engine driver for the QCOM Application Data Mover (ADM) DMA
  16. controller found in the MSM8x60 and IPQ/APQ8064 platforms.
  17. The ADM supports both memory to memory transactions and memory
  18. to/from peripheral device transactions. The controller also provides flow
  19. control capabilities for transactions to/from peripheral devices.
  20. The initial release of this driver supports slave transfers to/from peripherals
  21. and also incorporates CRCI (client rate control interface) flow control.
  22. Signed-off-by: Andy Gross <agross@codeaurora.org>
  23. Reviewed-by: sricharan <sricharan@codeaurora.org>
  24. ---
  25. drivers/dma/Kconfig | 10 +
  26. drivers/dma/Makefile | 1 +
  27. drivers/dma/qcom_adm.c | 900 ++++++++++++++++++++++++++++++++++++++++++++++++
  28. 3 files changed, 911 insertions(+)
  29. create mode 100644 drivers/dma/qcom_adm.c
  30. --- a/drivers/dma/Kconfig
  31. +++ b/drivers/dma/Kconfig
  32. @@ -558,4 +558,14 @@ config DMATEST
  33. config DMA_ENGINE_RAID
  34. bool
  35. +config QCOM_ADM
  36. + tristate "Qualcomm ADM support"
  37. + depends on ARCH_QCOM || (COMPILE_TEST && OF && ARM)
  38. + select DMA_ENGINE
  39. + select DMA_VIRTUAL_CHANNELS
  40. + ---help---
  41. + Enable support for the Qualcomm ADM DMA controller. This controller
  42. + provides DMA capabilities for both general purpose and on-chip
  43. + peripheral devices.
  44. +
  45. endif
  46. --- /dev/null
  47. +++ b/drivers/dma/qcom_adm.c
  48. @@ -0,0 +1,900 @@
  49. +/*
  50. + * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  51. + *
  52. + * This program is free software; you can redistribute it and/or modify
  53. + * it under the terms of the GNU General Public License version 2 and
  54. + * only version 2 as published by the Free Software Foundation.
  55. + *
  56. + * This program is distributed in the hope that it will be useful,
  57. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  58. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  59. + * GNU General Public License for more details.
  60. + *
  61. + */
  62. +
  63. +#include <linux/kernel.h>
  64. +#include <linux/io.h>
  65. +#include <linux/init.h>
  66. +#include <linux/slab.h>
  67. +#include <linux/module.h>
  68. +#include <linux/interrupt.h>
  69. +#include <linux/dma-mapping.h>
  70. +#include <linux/scatterlist.h>
  71. +#include <linux/device.h>
  72. +#include <linux/platform_device.h>
  73. +#include <linux/of.h>
  74. +#include <linux/of_address.h>
  75. +#include <linux/of_irq.h>
  76. +#include <linux/of_dma.h>
  77. +#include <linux/reset.h>
  78. +#include <linux/clk.h>
  79. +#include <linux/dmaengine.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,
  281. + void *desc, struct scatterlist *sg, u32 crci, u32 burst,
  282. + enum dma_transfer_direction direction)
  283. +{
  284. + struct adm_desc_hw_box *box_desc = NULL;
  285. + struct adm_desc_hw_single *single_desc;
  286. + u32 remainder = sg_dma_len(sg);
  287. + u32 rows, row_offset, crci_cmd;
  288. + u32 mem_addr = sg_dma_address(sg);
  289. + u32 *incr_addr = &mem_addr;
  290. + u32 *src, *dst;
  291. +
  292. + if (direction == DMA_DEV_TO_MEM) {
  293. + crci_cmd = ADM_CMD_SRC_CRCI(crci);
  294. + row_offset = burst;
  295. + src = &achan->slave.src_addr;
  296. + dst = &mem_addr;
  297. + } else {
  298. + crci_cmd = ADM_CMD_DST_CRCI(crci);
  299. + row_offset = burst << 16;
  300. + src = &mem_addr;
  301. + dst = &achan->slave.dst_addr;
  302. + }
  303. +
  304. + while (remainder >= burst) {
  305. + box_desc = desc;
  306. + box_desc->cmd = ADM_CMD_TYPE_BOX | crci_cmd;
  307. + box_desc->row_offset = row_offset;
  308. + box_desc->src_addr = *src;
  309. + box_desc->dst_addr = *dst;
  310. +
  311. + rows = remainder / burst;
  312. + rows = min_t(u32, rows, ADM_MAX_ROWS);
  313. + box_desc->num_rows = rows << 16 | rows;
  314. + box_desc->row_len = burst << 16 | burst;
  315. +
  316. + *incr_addr += burst * rows;
  317. + remainder -= burst * rows;
  318. + desc += sizeof(*box_desc);
  319. + }
  320. +
  321. + /* if leftover bytes, do one single descriptor */
  322. + if (remainder) {
  323. + single_desc = desc;
  324. + single_desc->cmd = ADM_CMD_TYPE_SINGLE | crci_cmd;
  325. + single_desc->len = remainder;
  326. + single_desc->src_addr = *src;
  327. + single_desc->dst_addr = *dst;
  328. + desc += sizeof(*single_desc);
  329. +
  330. + if (sg_is_last(sg))
  331. + single_desc->cmd |= ADM_CMD_LC;
  332. + } else {
  333. + if (box_desc && sg_is_last(sg))
  334. + box_desc->cmd |= ADM_CMD_LC;
  335. + }
  336. +
  337. + return desc;
  338. +}
  339. +
  340. +/**
  341. + * adm_process_non_fc_descriptors - Process descriptors for non-fc xfers
  342. + *
  343. + * @achan: ADM channel
  344. + * @desc: Descriptor memory pointer
  345. + * @sg: Scatterlist entry
  346. + * @direction: DMA transfer direction
  347. + */
  348. +static void *adm_process_non_fc_descriptors(struct adm_chan *achan,
  349. + void *desc, struct scatterlist *sg,
  350. + enum dma_transfer_direction direction)
  351. +{
  352. + struct adm_desc_hw_single *single_desc;
  353. + u32 remainder = sg_dma_len(sg);
  354. + u32 mem_addr = sg_dma_address(sg);
  355. + u32 *incr_addr = &mem_addr;
  356. + u32 *src, *dst;
  357. +
  358. + if (direction == DMA_DEV_TO_MEM) {
  359. + src = &achan->slave.src_addr;
  360. + dst = &mem_addr;
  361. + } else {
  362. + src = &mem_addr;
  363. + dst = &achan->slave.dst_addr;
  364. + }
  365. +
  366. + do {
  367. + single_desc = desc;
  368. + single_desc->cmd = ADM_CMD_TYPE_SINGLE;
  369. + single_desc->src_addr = *src;
  370. + single_desc->dst_addr = *dst;
  371. + single_desc->len = (remainder > ADM_MAX_XFER) ?
  372. + ADM_MAX_XFER : remainder;
  373. +
  374. + remainder -= single_desc->len;
  375. + *incr_addr += single_desc->len;
  376. + desc += sizeof(*single_desc);
  377. + } while (remainder);
  378. +
  379. + /* set last command if this is the end of the whole transaction */
  380. + if (sg_is_last(sg))
  381. + single_desc->cmd |= ADM_CMD_LC;
  382. +
  383. + return desc;
  384. +}
  385. +
  386. +/**
  387. + * adm_prep_slave_sg - Prep slave sg transaction
  388. + *
  389. + * @chan: dma channel
  390. + * @sgl: scatter gather list
  391. + * @sg_len: length of sg
  392. + * @direction: DMA transfer direction
  393. + * @flags: DMA flags
  394. + * @context: transfer context (unused)
  395. + */
  396. +static struct dma_async_tx_descriptor *adm_prep_slave_sg(struct dma_chan *chan,
  397. + struct scatterlist *sgl, unsigned int sg_len,
  398. + enum dma_transfer_direction direction, unsigned long flags,
  399. + void *context)
  400. +{
  401. + struct adm_chan *achan = to_adm_chan(chan);
  402. + struct adm_device *adev = achan->adev;
  403. + struct adm_async_desc *async_desc;
  404. + struct scatterlist *sg;
  405. + u32 i, burst;
  406. + u32 single_count = 0, box_count = 0, crci = 0;
  407. + void *desc;
  408. + u32 *cple;
  409. + int blk_size = 0;
  410. +
  411. + if (!is_slave_direction(direction)) {
  412. + dev_err(adev->dev, "invalid dma direction\n");
  413. + return NULL;
  414. + }
  415. +
  416. + /*
  417. + * get burst value from slave configuration
  418. + */
  419. + burst = (direction == DMA_MEM_TO_DEV) ?
  420. + achan->slave.dst_maxburst :
  421. + achan->slave.src_maxburst;
  422. +
  423. + /* if using flow control, validate burst and crci values */
  424. + if (achan->slave.device_fc) {
  425. +
  426. + blk_size = adm_get_blksize(burst);
  427. + if (blk_size < 0) {
  428. + dev_err(adev->dev, "invalid burst value: %d\n",
  429. + burst);
  430. + return ERR_PTR(-EINVAL);
  431. + }
  432. +
  433. + crci = achan->slave.slave_id & 0xf;
  434. + if (!crci || achan->slave.slave_id > 0x1f) {
  435. + dev_err(adev->dev, "invalid crci value\n");
  436. + return ERR_PTR(-EINVAL);
  437. + }
  438. + }
  439. +
  440. + /* iterate through sgs and compute allocation size of structures */
  441. + for_each_sg(sgl, sg, sg_len, i) {
  442. + if (achan->slave.device_fc) {
  443. + box_count += DIV_ROUND_UP(sg_dma_len(sg) / burst,
  444. + ADM_MAX_ROWS);
  445. + if (sg_dma_len(sg) % burst)
  446. + single_count++;
  447. + } else {
  448. + single_count += DIV_ROUND_UP(sg_dma_len(sg),
  449. + ADM_MAX_XFER);
  450. + }
  451. + }
  452. +
  453. + async_desc = kzalloc(sizeof(*async_desc), GFP_NOWAIT);
  454. + if (!async_desc)
  455. + return ERR_PTR(-ENOMEM);
  456. +
  457. + if (crci)
  458. + async_desc->mux = achan->slave.slave_id & ADM_CRCI_MUX_SEL ?
  459. + ADM_CRCI_CTL_MUX_SEL : 0;
  460. + async_desc->crci = crci;
  461. + async_desc->blk_size = blk_size;
  462. + async_desc->dma_len = single_count * sizeof(struct adm_desc_hw_single) +
  463. + box_count * sizeof(struct adm_desc_hw_box) +
  464. + sizeof(*cple) + 2 * ADM_DESC_ALIGN;
  465. +
  466. + async_desc->cpl = dma_alloc_writecombine(adev->dev, async_desc->dma_len,
  467. + &async_desc->dma_addr, GFP_NOWAIT);
  468. +
  469. + if (!async_desc->cpl) {
  470. + kfree(async_desc);
  471. + return ERR_PTR(-ENOMEM);
  472. + }
  473. +
  474. + async_desc->adev = adev;
  475. +
  476. + /* both command list entry and descriptors must be 8 byte aligned */
  477. + cple = PTR_ALIGN(async_desc->cpl, ADM_DESC_ALIGN);
  478. + desc = PTR_ALIGN(cple + 1, ADM_DESC_ALIGN);
  479. +
  480. + /* init cmd list */
  481. + *cple = ADM_CPLE_LP;
  482. + *cple |= (desc - async_desc->cpl + async_desc->dma_addr) >> 3;
  483. +
  484. + for_each_sg(sgl, sg, sg_len, i) {
  485. + async_desc->length += sg_dma_len(sg);
  486. +
  487. + if (achan->slave.device_fc)
  488. + desc = adm_process_fc_descriptors(achan, desc, sg, crci,
  489. + burst, direction);
  490. + else
  491. + desc = adm_process_non_fc_descriptors(achan, desc, sg,
  492. + direction);
  493. + }
  494. +
  495. + return vchan_tx_prep(&achan->vc, &async_desc->vd, flags);
  496. +}
  497. +
  498. +/**
  499. + * adm_terminate_all - terminate all transactions on a channel
  500. + * @achan: adm dma channel
  501. + *
  502. + * Dequeues and frees all transactions, aborts current transaction
  503. + * No callbacks are done
  504. + *
  505. + */
  506. +static int adm_terminate_all(struct dma_chan *chan)
  507. +{
  508. + struct adm_chan *achan = to_adm_chan(chan);
  509. + struct adm_device *adev = achan->adev;
  510. + unsigned long flags;
  511. + LIST_HEAD(head);
  512. +
  513. + spin_lock_irqsave(&achan->vc.lock, flags);
  514. + vchan_get_all_descriptors(&achan->vc, &head);
  515. +
  516. + /* send flush command to terminate current transaction */
  517. + writel_relaxed(0x0,
  518. + adev->regs + ADM_CH_FLUSH_STATE0(achan->id, adev->ee));
  519. +
  520. + spin_unlock_irqrestore(&achan->vc.lock, flags);
  521. +
  522. + vchan_dma_desc_free_list(&achan->vc, &head);
  523. +
  524. + return 0;
  525. +}
  526. +
  527. +static int adm_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
  528. +{
  529. + struct adm_chan *achan = to_adm_chan(chan);
  530. + unsigned long flag;
  531. +
  532. + spin_lock_irqsave(&achan->vc.lock, flag);
  533. + memcpy(&achan->slave, cfg, sizeof(struct dma_slave_config));
  534. + spin_unlock_irqrestore(&achan->vc.lock, flag);
  535. +
  536. + return 0;
  537. +}
  538. +
  539. +/**
  540. + * adm_start_dma - start next transaction
  541. + * @achan - ADM dma channel
  542. + */
  543. +static void adm_start_dma(struct adm_chan *achan)
  544. +{
  545. + struct virt_dma_desc *vd = vchan_next_desc(&achan->vc);
  546. + struct adm_device *adev = achan->adev;
  547. + struct adm_async_desc *async_desc;
  548. +
  549. + lockdep_assert_held(&achan->vc.lock);
  550. +
  551. + if (!vd)
  552. + return;
  553. +
  554. + list_del(&vd->node);
  555. +
  556. + /* write next command list out to the CMD FIFO */
  557. + async_desc = container_of(vd, struct adm_async_desc, vd);
  558. + achan->curr_txd = async_desc;
  559. +
  560. + /* reset channel error */
  561. + achan->error = 0;
  562. +
  563. + if (!achan->initialized) {
  564. + /* enable interrupts */
  565. + writel(ADM_CH_CONF_SHADOW_EN |
  566. + ADM_CH_CONF_PERM_MPU_CONF |
  567. + ADM_CH_CONF_MPU_DISABLE |
  568. + ADM_CH_CONF_SEC_DOMAIN(adev->ee),
  569. + adev->regs + ADM_CH_CONF(achan->id));
  570. +
  571. + writel(ADM_CH_RSLT_CONF_IRQ_EN | ADM_CH_RSLT_CONF_FLUSH_EN,
  572. + adev->regs + ADM_CH_RSLT_CONF(achan->id, adev->ee));
  573. +
  574. + achan->initialized = 1;
  575. + }
  576. +
  577. + /* set the crci block size if this transaction requires CRCI */
  578. + if (async_desc->crci) {
  579. + writel(async_desc->mux | async_desc->blk_size,
  580. + adev->regs + ADM_CRCI_CTL(async_desc->crci, adev->ee));
  581. + }
  582. +
  583. + /* make sure IRQ enable doesn't get reordered */
  584. + wmb();
  585. +
  586. + /* write next command list out to the CMD FIFO */
  587. + writel(ALIGN(async_desc->dma_addr, ADM_DESC_ALIGN) >> 3,
  588. + adev->regs + ADM_CH_CMD_PTR(achan->id, adev->ee));
  589. +}
  590. +
  591. +/**
  592. + * adm_dma_irq - irq handler for ADM controller
  593. + * @irq: IRQ of interrupt
  594. + * @data: callback data
  595. + *
  596. + * IRQ handler for the bam controller
  597. + */
  598. +static irqreturn_t adm_dma_irq(int irq, void *data)
  599. +{
  600. + struct adm_device *adev = data;
  601. + u32 srcs, i;
  602. + struct adm_async_desc *async_desc;
  603. + unsigned long flags;
  604. +
  605. + srcs = readl_relaxed(adev->regs +
  606. + ADM_SEC_DOMAIN_IRQ_STATUS(adev->ee));
  607. +
  608. + for (i = 0; i < ADM_MAX_CHANNELS; i++) {
  609. + struct adm_chan *achan = &adev->channels[i];
  610. + u32 status, result;
  611. +
  612. + if (srcs & BIT(i)) {
  613. + status = readl_relaxed(adev->regs +
  614. + ADM_CH_STATUS_SD(i, adev->ee));
  615. +
  616. + /* if no result present, skip */
  617. + if (!(status & ADM_CH_STATUS_VALID))
  618. + continue;
  619. +
  620. + result = readl_relaxed(adev->regs +
  621. + ADM_CH_RSLT(i, adev->ee));
  622. +
  623. + /* no valid results, skip */
  624. + if (!(result & ADM_CH_RSLT_VALID))
  625. + continue;
  626. +
  627. + /* flag error if transaction was flushed or failed */
  628. + if (result & (ADM_CH_RSLT_ERR | ADM_CH_RSLT_FLUSH))
  629. + achan->error = 1;
  630. +
  631. + spin_lock_irqsave(&achan->vc.lock, flags);
  632. + async_desc = achan->curr_txd;
  633. +
  634. + achan->curr_txd = NULL;
  635. +
  636. + if (async_desc) {
  637. + vchan_cookie_complete(&async_desc->vd);
  638. +
  639. + /* kick off next DMA */
  640. + adm_start_dma(achan);
  641. + }
  642. +
  643. + spin_unlock_irqrestore(&achan->vc.lock, flags);
  644. + }
  645. + }
  646. +
  647. + return IRQ_HANDLED;
  648. +}
  649. +
  650. +/**
  651. + * adm_tx_status - returns status of transaction
  652. + * @chan: dma channel
  653. + * @cookie: transaction cookie
  654. + * @txstate: DMA transaction state
  655. + *
  656. + * Return status of dma transaction
  657. + */
  658. +static enum dma_status adm_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  659. + struct dma_tx_state *txstate)
  660. +{
  661. + struct adm_chan *achan = to_adm_chan(chan);
  662. + struct virt_dma_desc *vd;
  663. + enum dma_status ret;
  664. + unsigned long flags;
  665. + size_t residue = 0;
  666. +
  667. + ret = dma_cookie_status(chan, cookie, txstate);
  668. + if (ret == DMA_COMPLETE || !txstate)
  669. + return ret;
  670. +
  671. + spin_lock_irqsave(&achan->vc.lock, flags);
  672. +
  673. + vd = vchan_find_desc(&achan->vc, cookie);
  674. + if (vd)
  675. + residue = container_of(vd, struct adm_async_desc, vd)->length;
  676. +
  677. + spin_unlock_irqrestore(&achan->vc.lock, flags);
  678. +
  679. + /*
  680. + * residue is either the full length if it is in the issued list, or 0
  681. + * if it is in progress. We have no reliable way of determining
  682. + * anything inbetween
  683. + */
  684. + dma_set_residue(txstate, residue);
  685. +
  686. + if (achan->error)
  687. + return DMA_ERROR;
  688. +
  689. + return ret;
  690. +}
  691. +
  692. +/**
  693. + * adm_issue_pending - starts pending transactions
  694. + * @chan: dma channel
  695. + *
  696. + * Issues all pending transactions and starts DMA
  697. + */
  698. +static void adm_issue_pending(struct dma_chan *chan)
  699. +{
  700. + struct adm_chan *achan = to_adm_chan(chan);
  701. + unsigned long flags;
  702. +
  703. + spin_lock_irqsave(&achan->vc.lock, flags);
  704. +
  705. + if (vchan_issue_pending(&achan->vc) && !achan->curr_txd)
  706. + adm_start_dma(achan);
  707. + spin_unlock_irqrestore(&achan->vc.lock, flags);
  708. +}
  709. +
  710. +/**
  711. + * adm_dma_free_desc - free descriptor memory
  712. + * @vd: virtual descriptor
  713. + *
  714. + */
  715. +static void adm_dma_free_desc(struct virt_dma_desc *vd)
  716. +{
  717. + struct adm_async_desc *async_desc = container_of(vd,
  718. + struct adm_async_desc, vd);
  719. +
  720. + dma_free_writecombine(async_desc->adev->dev, async_desc->dma_len,
  721. + async_desc->cpl, async_desc->dma_addr);
  722. + kfree(async_desc);
  723. +}
  724. +
  725. +static void adm_channel_init(struct adm_device *adev, struct adm_chan *achan,
  726. + u32 index)
  727. +{
  728. + achan->id = index;
  729. + achan->adev = adev;
  730. +
  731. + vchan_init(&achan->vc, &adev->common);
  732. + achan->vc.desc_free = adm_dma_free_desc;
  733. +}
  734. +
  735. +static int adm_dma_probe(struct platform_device *pdev)
  736. +{
  737. + struct adm_device *adev;
  738. + struct resource *iores;
  739. + int ret;
  740. + u32 i;
  741. +
  742. + adev = devm_kzalloc(&pdev->dev, sizeof(*adev), GFP_KERNEL);
  743. + if (!adev)
  744. + return -ENOMEM;
  745. +
  746. + adev->dev = &pdev->dev;
  747. +
  748. + iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  749. + adev->regs = devm_ioremap_resource(&pdev->dev, iores);
  750. + if (IS_ERR(adev->regs))
  751. + return PTR_ERR(adev->regs);
  752. +
  753. + adev->irq = platform_get_irq(pdev, 0);
  754. + if (adev->irq < 0)
  755. + return adev->irq;
  756. +
  757. + ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &adev->ee);
  758. + if (ret) {
  759. + dev_err(adev->dev, "Execution environment unspecified\n");
  760. + return ret;
  761. + }
  762. +
  763. + adev->core_clk = devm_clk_get(adev->dev, "core");
  764. + if (IS_ERR(adev->core_clk))
  765. + return PTR_ERR(adev->core_clk);
  766. +
  767. + ret = clk_prepare_enable(adev->core_clk);
  768. + if (ret) {
  769. + dev_err(adev->dev, "failed to prepare/enable core clock\n");
  770. + return ret;
  771. + }
  772. +
  773. + adev->iface_clk = devm_clk_get(adev->dev, "iface");
  774. + if (IS_ERR(adev->iface_clk)) {
  775. + ret = PTR_ERR(adev->iface_clk);
  776. + goto err_disable_core_clk;
  777. + }
  778. +
  779. + ret = clk_prepare_enable(adev->iface_clk);
  780. + if (ret) {
  781. + dev_err(adev->dev, "failed to prepare/enable iface clock\n");
  782. + goto err_disable_core_clk;
  783. + }
  784. +
  785. + adev->clk_reset = devm_reset_control_get(&pdev->dev, "clk");
  786. + if (IS_ERR(adev->clk_reset)) {
  787. + dev_err(adev->dev, "failed to get ADM0 reset\n");
  788. + ret = PTR_ERR(adev->clk_reset);
  789. + goto err_disable_clks;
  790. + }
  791. +
  792. + adev->c0_reset = devm_reset_control_get(&pdev->dev, "c0");
  793. + if (IS_ERR(adev->c0_reset)) {
  794. + dev_err(adev->dev, "failed to get ADM0 C0 reset\n");
  795. + ret = PTR_ERR(adev->c0_reset);
  796. + goto err_disable_clks;
  797. + }
  798. +
  799. + adev->c1_reset = devm_reset_control_get(&pdev->dev, "c1");
  800. + if (IS_ERR(adev->c1_reset)) {
  801. + dev_err(adev->dev, "failed to get ADM0 C1 reset\n");
  802. + ret = PTR_ERR(adev->c1_reset);
  803. + goto err_disable_clks;
  804. + }
  805. +
  806. + adev->c2_reset = devm_reset_control_get(&pdev->dev, "c2");
  807. + if (IS_ERR(adev->c2_reset)) {
  808. + dev_err(adev->dev, "failed to get ADM0 C2 reset\n");
  809. + ret = PTR_ERR(adev->c2_reset);
  810. + goto err_disable_clks;
  811. + }
  812. +
  813. + reset_control_assert(adev->clk_reset);
  814. + reset_control_assert(adev->c0_reset);
  815. + reset_control_assert(adev->c1_reset);
  816. + reset_control_assert(adev->c2_reset);
  817. +
  818. + reset_control_deassert(adev->clk_reset);
  819. + reset_control_deassert(adev->c0_reset);
  820. + reset_control_deassert(adev->c1_reset);
  821. + reset_control_deassert(adev->c2_reset);
  822. +
  823. + adev->channels = devm_kcalloc(adev->dev, ADM_MAX_CHANNELS,
  824. + sizeof(*adev->channels), GFP_KERNEL);
  825. +
  826. + if (!adev->channels) {
  827. + ret = -ENOMEM;
  828. + goto err_disable_clks;
  829. + }
  830. +
  831. + /* allocate and initialize channels */
  832. + INIT_LIST_HEAD(&adev->common.channels);
  833. +
  834. + for (i = 0; i < ADM_MAX_CHANNELS; i++)
  835. + adm_channel_init(adev, &adev->channels[i], i);
  836. +
  837. + /* reset CRCIs */
  838. + for (i = 0; i < 16; i++)
  839. + writel(ADM_CRCI_CTL_RST, adev->regs +
  840. + ADM_CRCI_CTL(i, adev->ee));
  841. +
  842. + /* configure client interfaces */
  843. + writel(ADM_CI_RANGE_START(0x40) | ADM_CI_RANGE_END(0xb0) |
  844. + ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(0));
  845. + writel(ADM_CI_RANGE_START(0x2a) | ADM_CI_RANGE_END(0x2c) |
  846. + ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(1));
  847. + writel(ADM_CI_RANGE_START(0x12) | ADM_CI_RANGE_END(0x28) |
  848. + ADM_CI_BURST_8_WORDS, adev->regs + ADM_CI_CONF(2));
  849. + writel(ADM_GP_CTL_LP_EN | ADM_GP_CTL_LP_CNT(0xf),
  850. + adev->regs + ADM_GP_CTL);
  851. +
  852. + ret = devm_request_irq(adev->dev, adev->irq, adm_dma_irq,
  853. + 0, "adm_dma", adev);
  854. + if (ret)
  855. + goto err_disable_clks;
  856. +
  857. + platform_set_drvdata(pdev, adev);
  858. +
  859. + adev->common.dev = adev->dev;
  860. + adev->common.dev->dma_parms = &adev->dma_parms;
  861. +
  862. + /* set capabilities */
  863. + dma_cap_zero(adev->common.cap_mask);
  864. + dma_cap_set(DMA_SLAVE, adev->common.cap_mask);
  865. + dma_cap_set(DMA_PRIVATE, adev->common.cap_mask);
  866. +
  867. + /* initialize dmaengine apis */
  868. + adev->common.directions = BIT(DMA_DEV_TO_MEM | DMA_MEM_TO_DEV);
  869. + adev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  870. + adev->common.src_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  871. + adev->common.dst_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  872. + adev->common.device_free_chan_resources = adm_free_chan;
  873. + adev->common.device_prep_slave_sg = adm_prep_slave_sg;
  874. + adev->common.device_issue_pending = adm_issue_pending;
  875. + adev->common.device_tx_status = adm_tx_status;
  876. + adev->common.device_terminate_all = adm_terminate_all;
  877. + adev->common.device_config = adm_slave_config;
  878. +
  879. + ret = dma_async_device_register(&adev->common);
  880. + if (ret) {
  881. + dev_err(adev->dev, "failed to register dma async device\n");
  882. + goto err_disable_clks;
  883. + }
  884. +
  885. + ret = of_dma_controller_register(pdev->dev.of_node,
  886. + of_dma_xlate_by_chan_id,
  887. + &adev->common);
  888. + if (ret)
  889. + goto err_unregister_dma;
  890. +
  891. + return 0;
  892. +
  893. +err_unregister_dma:
  894. + dma_async_device_unregister(&adev->common);
  895. +err_disable_clks:
  896. + clk_disable_unprepare(adev->iface_clk);
  897. +err_disable_core_clk:
  898. + clk_disable_unprepare(adev->core_clk);
  899. +
  900. + return ret;
  901. +}
  902. +
  903. +static int adm_dma_remove(struct platform_device *pdev)
  904. +{
  905. + struct adm_device *adev = platform_get_drvdata(pdev);
  906. + struct adm_chan *achan;
  907. + u32 i;
  908. +
  909. + of_dma_controller_free(pdev->dev.of_node);
  910. + dma_async_device_unregister(&adev->common);
  911. +
  912. + for (i = 0; i < ADM_MAX_CHANNELS; i++) {
  913. + achan = &adev->channels[i];
  914. +
  915. + /* mask IRQs for this channel/EE pair */
  916. + writel(0, adev->regs + ADM_CH_RSLT_CONF(achan->id, adev->ee));
  917. +
  918. + adm_terminate_all(&adev->channels[i].vc.chan);
  919. + }
  920. +
  921. + devm_free_irq(adev->dev, adev->irq, adev);
  922. +
  923. + clk_disable_unprepare(adev->core_clk);
  924. + clk_disable_unprepare(adev->iface_clk);
  925. +
  926. + return 0;
  927. +}
  928. +
  929. +static const struct of_device_id adm_of_match[] = {
  930. + { .compatible = "qcom,adm", },
  931. + {}
  932. +};
  933. +MODULE_DEVICE_TABLE(of, adm_of_match);
  934. +
  935. +static struct platform_driver adm_dma_driver = {
  936. + .probe = adm_dma_probe,
  937. + .remove = adm_dma_remove,
  938. + .driver = {
  939. + .name = "adm-dma-engine",
  940. + .of_match_table = adm_of_match,
  941. + },
  942. +};
  943. +
  944. +module_platform_driver(adm_dma_driver);
  945. +
  946. +MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  947. +MODULE_DESCRIPTION("QCOM ADM DMA engine driver");
  948. +MODULE_LICENSE("GPL v2");
  949. --- a/drivers/dma/Makefile
  950. +++ b/drivers/dma/Makefile
  951. @@ -65,5 +65,6 @@ obj-$(CONFIG_TI_DMA_CROSSBAR) += ti-dma-
  952. obj-$(CONFIG_TI_EDMA) += edma.o
  953. obj-$(CONFIG_XGENE_DMA) += xgene-dma.o
  954. obj-$(CONFIG_ZX_DMA) += zx296702_dma.o
  955. +obj-$(CONFIG_QCOM_ADM) += qcom_adm.o
  956. obj-y += xilinx/