qcom_bam_dma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. /*
  15. * QCOM BAM DMA engine driver
  16. *
  17. * QCOM BAM DMA blocks are distributed amongst a number of the on-chip
  18. * peripherals on the MSM 8x74. The configuration of the channels are dependent
  19. * on the way they are hard wired to that specific peripheral. The peripheral
  20. * device tree entries specify the configuration of each channel.
  21. *
  22. * The DMA controller requires the use of external memory for storage of the
  23. * hardware descriptors for each channel. The descriptor FIFO is accessed as a
  24. * circular buffer and operations are managed according to the offset within the
  25. * FIFO. After pipe/channel reset, all of the pipe registers and internal state
  26. * are back to defaults.
  27. *
  28. * During DMA operations, we write descriptors to the FIFO, being careful to
  29. * handle wrapping and then write the last FIFO offset to that channel's
  30. * P_EVNT_REG register to kick off the transaction. The P_SW_OFSTS register
  31. * indicates the current FIFO offset that is being processed, so there is some
  32. * indication of where the hardware is currently working.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/io.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/scatterlist.h>
  42. #include <linux/device.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/of.h>
  45. #include <linux/of_address.h>
  46. #include <linux/of_irq.h>
  47. #include <linux/of_dma.h>
  48. #include <linux/clk.h>
  49. #include <linux/dmaengine.h>
  50. #include "dmaengine.h"
  51. #include "virt-dma.h"
  52. struct bam_desc_hw {
  53. u32 addr; /* Buffer physical address */
  54. u16 size; /* Buffer size in bytes */
  55. u16 flags;
  56. };
  57. #define DESC_FLAG_INT BIT(15)
  58. #define DESC_FLAG_EOT BIT(14)
  59. #define DESC_FLAG_EOB BIT(13)
  60. struct bam_async_desc {
  61. struct virt_dma_desc vd;
  62. u32 num_desc;
  63. u32 xfer_len;
  64. struct bam_desc_hw *curr_desc;
  65. enum dma_transfer_direction dir;
  66. size_t length;
  67. struct bam_desc_hw desc[0];
  68. };
  69. #define BAM_CTRL 0x0000
  70. #define BAM_REVISION 0x0004
  71. #define BAM_SW_REVISION 0x0080
  72. #define BAM_NUM_PIPES 0x003C
  73. #define BAM_TIMER 0x0040
  74. #define BAM_TIMER_CTRL 0x0044
  75. #define BAM_DESC_CNT_TRSHLD 0x0008
  76. #define BAM_IRQ_SRCS 0x000C
  77. #define BAM_IRQ_SRCS_MSK 0x0010
  78. #define BAM_IRQ_SRCS_UNMASKED 0x0030
  79. #define BAM_IRQ_STTS 0x0014
  80. #define BAM_IRQ_CLR 0x0018
  81. #define BAM_IRQ_EN 0x001C
  82. #define BAM_CNFG_BITS 0x007C
  83. #define BAM_IRQ_SRCS_EE(ee) (0x0800 + ((ee) * 0x80))
  84. #define BAM_IRQ_SRCS_MSK_EE(ee) (0x0804 + ((ee) * 0x80))
  85. #define BAM_P_CTRL(pipe) (0x1000 + ((pipe) * 0x1000))
  86. #define BAM_P_RST(pipe) (0x1004 + ((pipe) * 0x1000))
  87. #define BAM_P_HALT(pipe) (0x1008 + ((pipe) * 0x1000))
  88. #define BAM_P_IRQ_STTS(pipe) (0x1010 + ((pipe) * 0x1000))
  89. #define BAM_P_IRQ_CLR(pipe) (0x1014 + ((pipe) * 0x1000))
  90. #define BAM_P_IRQ_EN(pipe) (0x1018 + ((pipe) * 0x1000))
  91. #define BAM_P_EVNT_DEST_ADDR(pipe) (0x182C + ((pipe) * 0x1000))
  92. #define BAM_P_EVNT_REG(pipe) (0x1818 + ((pipe) * 0x1000))
  93. #define BAM_P_SW_OFSTS(pipe) (0x1800 + ((pipe) * 0x1000))
  94. #define BAM_P_DATA_FIFO_ADDR(pipe) (0x1824 + ((pipe) * 0x1000))
  95. #define BAM_P_DESC_FIFO_ADDR(pipe) (0x181C + ((pipe) * 0x1000))
  96. #define BAM_P_EVNT_TRSHLD(pipe) (0x1828 + ((pipe) * 0x1000))
  97. #define BAM_P_FIFO_SIZES(pipe) (0x1820 + ((pipe) * 0x1000))
  98. /* BAM CTRL */
  99. #define BAM_SW_RST BIT(0)
  100. #define BAM_EN BIT(1)
  101. #define BAM_EN_ACCUM BIT(4)
  102. #define BAM_TESTBUS_SEL_SHIFT 5
  103. #define BAM_TESTBUS_SEL_MASK 0x3F
  104. #define BAM_DESC_CACHE_SEL_SHIFT 13
  105. #define BAM_DESC_CACHE_SEL_MASK 0x3
  106. #define BAM_CACHED_DESC_STORE BIT(15)
  107. #define IBC_DISABLE BIT(16)
  108. /* BAM REVISION */
  109. #define REVISION_SHIFT 0
  110. #define REVISION_MASK 0xFF
  111. #define NUM_EES_SHIFT 8
  112. #define NUM_EES_MASK 0xF
  113. #define CE_BUFFER_SIZE BIT(13)
  114. #define AXI_ACTIVE BIT(14)
  115. #define USE_VMIDMT BIT(15)
  116. #define SECURED BIT(16)
  117. #define BAM_HAS_NO_BYPASS BIT(17)
  118. #define HIGH_FREQUENCY_BAM BIT(18)
  119. #define INACTIV_TMRS_EXST BIT(19)
  120. #define NUM_INACTIV_TMRS BIT(20)
  121. #define DESC_CACHE_DEPTH_SHIFT 21
  122. #define DESC_CACHE_DEPTH_1 (0 << DESC_CACHE_DEPTH_SHIFT)
  123. #define DESC_CACHE_DEPTH_2 (1 << DESC_CACHE_DEPTH_SHIFT)
  124. #define DESC_CACHE_DEPTH_3 (2 << DESC_CACHE_DEPTH_SHIFT)
  125. #define DESC_CACHE_DEPTH_4 (3 << DESC_CACHE_DEPTH_SHIFT)
  126. #define CMD_DESC_EN BIT(23)
  127. #define INACTIV_TMR_BASE_SHIFT 24
  128. #define INACTIV_TMR_BASE_MASK 0xFF
  129. /* BAM NUM PIPES */
  130. #define BAM_NUM_PIPES_SHIFT 0
  131. #define BAM_NUM_PIPES_MASK 0xFF
  132. #define PERIPH_NON_PIPE_GRP_SHIFT 16
  133. #define PERIPH_NON_PIP_GRP_MASK 0xFF
  134. #define BAM_NON_PIPE_GRP_SHIFT 24
  135. #define BAM_NON_PIPE_GRP_MASK 0xFF
  136. /* BAM CNFG BITS */
  137. #define BAM_PIPE_CNFG BIT(2)
  138. #define BAM_FULL_PIPE BIT(11)
  139. #define BAM_NO_EXT_P_RST BIT(12)
  140. #define BAM_IBC_DISABLE BIT(13)
  141. #define BAM_SB_CLK_REQ BIT(14)
  142. #define BAM_PSM_CSW_REQ BIT(15)
  143. #define BAM_PSM_P_RES BIT(16)
  144. #define BAM_AU_P_RES BIT(17)
  145. #define BAM_SI_P_RES BIT(18)
  146. #define BAM_WB_P_RES BIT(19)
  147. #define BAM_WB_BLK_CSW BIT(20)
  148. #define BAM_WB_CSW_ACK_IDL BIT(21)
  149. #define BAM_WB_RETR_SVPNT BIT(22)
  150. #define BAM_WB_DSC_AVL_P_RST BIT(23)
  151. #define BAM_REG_P_EN BIT(24)
  152. #define BAM_PSM_P_HD_DATA BIT(25)
  153. #define BAM_AU_ACCUMED BIT(26)
  154. #define BAM_CMD_ENABLE BIT(27)
  155. #define BAM_CNFG_BITS_DEFAULT (BAM_PIPE_CNFG | \
  156. BAM_NO_EXT_P_RST | \
  157. BAM_IBC_DISABLE | \
  158. BAM_SB_CLK_REQ | \
  159. BAM_PSM_CSW_REQ | \
  160. BAM_PSM_P_RES | \
  161. BAM_AU_P_RES | \
  162. BAM_SI_P_RES | \
  163. BAM_WB_P_RES | \
  164. BAM_WB_BLK_CSW | \
  165. BAM_WB_CSW_ACK_IDL | \
  166. BAM_WB_RETR_SVPNT | \
  167. BAM_WB_DSC_AVL_P_RST | \
  168. BAM_REG_P_EN | \
  169. BAM_PSM_P_HD_DATA | \
  170. BAM_AU_ACCUMED | \
  171. BAM_CMD_ENABLE)
  172. /* PIPE CTRL */
  173. #define P_EN BIT(1)
  174. #define P_DIRECTION BIT(3)
  175. #define P_SYS_STRM BIT(4)
  176. #define P_SYS_MODE BIT(5)
  177. #define P_AUTO_EOB BIT(6)
  178. #define P_AUTO_EOB_SEL_SHIFT 7
  179. #define P_AUTO_EOB_SEL_512 (0 << P_AUTO_EOB_SEL_SHIFT)
  180. #define P_AUTO_EOB_SEL_256 (1 << P_AUTO_EOB_SEL_SHIFT)
  181. #define P_AUTO_EOB_SEL_128 (2 << P_AUTO_EOB_SEL_SHIFT)
  182. #define P_AUTO_EOB_SEL_64 (3 << P_AUTO_EOB_SEL_SHIFT)
  183. #define P_PREFETCH_LIMIT_SHIFT 9
  184. #define P_PREFETCH_LIMIT_32 (0 << P_PREFETCH_LIMIT_SHIFT)
  185. #define P_PREFETCH_LIMIT_16 (1 << P_PREFETCH_LIMIT_SHIFT)
  186. #define P_PREFETCH_LIMIT_4 (2 << P_PREFETCH_LIMIT_SHIFT)
  187. #define P_WRITE_NWD BIT(11)
  188. #define P_LOCK_GROUP_SHIFT 16
  189. #define P_LOCK_GROUP_MASK 0x1F
  190. /* BAM_DESC_CNT_TRSHLD */
  191. #define CNT_TRSHLD 0xffff
  192. #define DEFAULT_CNT_THRSHLD 0x4
  193. /* BAM_IRQ_SRCS */
  194. #define BAM_IRQ BIT(31)
  195. #define P_IRQ 0x7fffffff
  196. /* BAM_IRQ_SRCS_MSK */
  197. #define BAM_IRQ_MSK BAM_IRQ
  198. #define P_IRQ_MSK P_IRQ
  199. /* BAM_IRQ_STTS */
  200. #define BAM_TIMER_IRQ BIT(4)
  201. #define BAM_EMPTY_IRQ BIT(3)
  202. #define BAM_ERROR_IRQ BIT(2)
  203. #define BAM_HRESP_ERR_IRQ BIT(1)
  204. /* BAM_IRQ_CLR */
  205. #define BAM_TIMER_CLR BIT(4)
  206. #define BAM_EMPTY_CLR BIT(3)
  207. #define BAM_ERROR_CLR BIT(2)
  208. #define BAM_HRESP_ERR_CLR BIT(1)
  209. /* BAM_IRQ_EN */
  210. #define BAM_TIMER_EN BIT(4)
  211. #define BAM_EMPTY_EN BIT(3)
  212. #define BAM_ERROR_EN BIT(2)
  213. #define BAM_HRESP_ERR_EN BIT(1)
  214. /* BAM_P_IRQ_EN */
  215. #define P_PRCSD_DESC_EN BIT(0)
  216. #define P_TIMER_EN BIT(1)
  217. #define P_WAKE_EN BIT(2)
  218. #define P_OUT_OF_DESC_EN BIT(3)
  219. #define P_ERR_EN BIT(4)
  220. #define P_TRNSFR_END_EN BIT(5)
  221. #define P_DEFAULT_IRQS_EN (P_PRCSD_DESC_EN | P_ERR_EN | P_TRNSFR_END_EN)
  222. /* BAM_P_SW_OFSTS */
  223. #define P_SW_OFSTS_MASK 0xffff
  224. #define BAM_DESC_FIFO_SIZE SZ_32K
  225. #define MAX_DESCRIPTORS (BAM_DESC_FIFO_SIZE / sizeof(struct bam_desc_hw) - 1)
  226. #define BAM_MAX_DATA_SIZE (SZ_32K - 8)
  227. struct bam_chan {
  228. struct virt_dma_chan vc;
  229. struct bam_device *bdev;
  230. /* configuration from device tree */
  231. u32 id;
  232. struct bam_async_desc *curr_txd; /* current running dma */
  233. /* runtime configuration */
  234. struct dma_slave_config slave;
  235. /* fifo storage */
  236. struct bam_desc_hw *fifo_virt;
  237. dma_addr_t fifo_phys;
  238. /* fifo markers */
  239. unsigned short head; /* start of active descriptor entries */
  240. unsigned short tail; /* end of active descriptor entries */
  241. unsigned int initialized; /* is the channel hw initialized? */
  242. unsigned int paused; /* is the channel paused? */
  243. unsigned int reconfigure; /* new slave config? */
  244. struct list_head node;
  245. };
  246. static inline struct bam_chan *to_bam_chan(struct dma_chan *common)
  247. {
  248. return container_of(common, struct bam_chan, vc.chan);
  249. }
  250. struct bam_device {
  251. void __iomem *regs;
  252. struct device *dev;
  253. struct dma_device common;
  254. struct device_dma_parameters dma_parms;
  255. struct bam_chan *channels;
  256. u32 num_channels;
  257. /* execution environment ID, from DT */
  258. u32 ee;
  259. struct clk *bamclk;
  260. int irq;
  261. /* dma start transaction tasklet */
  262. struct tasklet_struct task;
  263. };
  264. /**
  265. * bam_reset_channel - Reset individual BAM DMA channel
  266. * @bchan: bam channel
  267. *
  268. * This function resets a specific BAM channel
  269. */
  270. static void bam_reset_channel(struct bam_chan *bchan)
  271. {
  272. struct bam_device *bdev = bchan->bdev;
  273. lockdep_assert_held(&bchan->vc.lock);
  274. /* reset channel */
  275. writel_relaxed(1, bdev->regs + BAM_P_RST(bchan->id));
  276. writel_relaxed(0, bdev->regs + BAM_P_RST(bchan->id));
  277. /* don't allow cpu to reorder BAM register accesses done after this */
  278. wmb();
  279. /* make sure hw is initialized when channel is used the first time */
  280. bchan->initialized = 0;
  281. }
  282. /**
  283. * bam_chan_init_hw - Initialize channel hardware
  284. * @bchan: bam channel
  285. *
  286. * This function resets and initializes the BAM channel
  287. */
  288. static void bam_chan_init_hw(struct bam_chan *bchan,
  289. enum dma_transfer_direction dir)
  290. {
  291. struct bam_device *bdev = bchan->bdev;
  292. u32 val;
  293. /* Reset the channel to clear internal state of the FIFO */
  294. bam_reset_channel(bchan);
  295. /*
  296. * write out 8 byte aligned address. We have enough space for this
  297. * because we allocated 1 more descriptor (8 bytes) than we can use
  298. */
  299. writel_relaxed(ALIGN(bchan->fifo_phys, sizeof(struct bam_desc_hw)),
  300. bdev->regs + BAM_P_DESC_FIFO_ADDR(bchan->id));
  301. writel_relaxed(BAM_DESC_FIFO_SIZE, bdev->regs +
  302. BAM_P_FIFO_SIZES(bchan->id));
  303. /* enable the per pipe interrupts, enable EOT, ERR, and INT irqs */
  304. writel_relaxed(P_DEFAULT_IRQS_EN, bdev->regs + BAM_P_IRQ_EN(bchan->id));
  305. /* unmask the specific pipe and EE combo */
  306. val = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  307. val |= BIT(bchan->id);
  308. writel_relaxed(val, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  309. /* don't allow cpu to reorder the channel enable done below */
  310. wmb();
  311. /* set fixed direction and mode, then enable channel */
  312. val = P_EN | P_SYS_MODE;
  313. if (dir == DMA_DEV_TO_MEM)
  314. val |= P_DIRECTION;
  315. writel_relaxed(val, bdev->regs + BAM_P_CTRL(bchan->id));
  316. bchan->initialized = 1;
  317. /* init FIFO pointers */
  318. bchan->head = 0;
  319. bchan->tail = 0;
  320. }
  321. /**
  322. * bam_alloc_chan - Allocate channel resources for DMA channel.
  323. * @chan: specified channel
  324. *
  325. * This function allocates the FIFO descriptor memory
  326. */
  327. static int bam_alloc_chan(struct dma_chan *chan)
  328. {
  329. struct bam_chan *bchan = to_bam_chan(chan);
  330. struct bam_device *bdev = bchan->bdev;
  331. if (bchan->fifo_virt)
  332. return 0;
  333. /* allocate FIFO descriptor space, but only if necessary */
  334. bchan->fifo_virt = dma_alloc_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE,
  335. &bchan->fifo_phys, GFP_KERNEL);
  336. if (!bchan->fifo_virt) {
  337. dev_err(bdev->dev, "Failed to allocate desc fifo\n");
  338. return -ENOMEM;
  339. }
  340. return 0;
  341. }
  342. /**
  343. * bam_free_chan - Frees dma resources associated with specific channel
  344. * @chan: specified channel
  345. *
  346. * Free the allocated fifo descriptor memory and channel resources
  347. *
  348. */
  349. static void bam_free_chan(struct dma_chan *chan)
  350. {
  351. struct bam_chan *bchan = to_bam_chan(chan);
  352. struct bam_device *bdev = bchan->bdev;
  353. u32 val;
  354. unsigned long flags;
  355. vchan_free_chan_resources(to_virt_chan(chan));
  356. if (bchan->curr_txd) {
  357. dev_err(bchan->bdev->dev, "Cannot free busy channel\n");
  358. return;
  359. }
  360. spin_lock_irqsave(&bchan->vc.lock, flags);
  361. bam_reset_channel(bchan);
  362. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  363. dma_free_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE, bchan->fifo_virt,
  364. bchan->fifo_phys);
  365. bchan->fifo_virt = NULL;
  366. /* mask irq for pipe/channel */
  367. val = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  368. val &= ~BIT(bchan->id);
  369. writel_relaxed(val, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  370. /* disable irq */
  371. writel_relaxed(0, bdev->regs + BAM_P_IRQ_EN(bchan->id));
  372. }
  373. /**
  374. * bam_slave_config - set slave configuration for channel
  375. * @chan: dma channel
  376. * @cfg: slave configuration
  377. *
  378. * Sets slave configuration for channel
  379. *
  380. */
  381. static void bam_slave_config(struct bam_chan *bchan,
  382. struct dma_slave_config *cfg)
  383. {
  384. memcpy(&bchan->slave, cfg, sizeof(*cfg));
  385. bchan->reconfigure = 1;
  386. }
  387. /**
  388. * bam_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 *bam_prep_slave_sg(struct dma_chan *chan,
  398. struct scatterlist *sgl, unsigned int sg_len,
  399. enum dma_transfer_direction direction, unsigned long flags,
  400. void *context)
  401. {
  402. struct bam_chan *bchan = to_bam_chan(chan);
  403. struct bam_device *bdev = bchan->bdev;
  404. struct bam_async_desc *async_desc;
  405. struct scatterlist *sg;
  406. u32 i;
  407. struct bam_desc_hw *desc;
  408. unsigned int num_alloc = 0;
  409. if (!is_slave_direction(direction)) {
  410. dev_err(bdev->dev, "invalid dma direction\n");
  411. return NULL;
  412. }
  413. /* calculate number of required entries */
  414. for_each_sg(sgl, sg, sg_len, i)
  415. num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_MAX_DATA_SIZE);
  416. /* allocate enough room to accomodate the number of entries */
  417. async_desc = kzalloc(sizeof(*async_desc) +
  418. (num_alloc * sizeof(struct bam_desc_hw)), GFP_NOWAIT);
  419. if (!async_desc)
  420. goto err_out;
  421. async_desc->num_desc = num_alloc;
  422. async_desc->curr_desc = async_desc->desc;
  423. async_desc->dir = direction;
  424. /* fill in temporary descriptors */
  425. desc = async_desc->desc;
  426. for_each_sg(sgl, sg, sg_len, i) {
  427. unsigned int remainder = sg_dma_len(sg);
  428. unsigned int curr_offset = 0;
  429. do {
  430. desc->addr = sg_dma_address(sg) + curr_offset;
  431. if (remainder > BAM_MAX_DATA_SIZE) {
  432. desc->size = BAM_MAX_DATA_SIZE;
  433. remainder -= BAM_MAX_DATA_SIZE;
  434. curr_offset += BAM_MAX_DATA_SIZE;
  435. } else {
  436. desc->size = remainder;
  437. remainder = 0;
  438. }
  439. async_desc->length += desc->size;
  440. desc++;
  441. } while (remainder > 0);
  442. }
  443. return vchan_tx_prep(&bchan->vc, &async_desc->vd, flags);
  444. err_out:
  445. kfree(async_desc);
  446. return NULL;
  447. }
  448. /**
  449. * bam_dma_terminate_all - terminate all transactions on a channel
  450. * @bchan: bam dma channel
  451. *
  452. * Dequeues and frees all transactions
  453. * No callbacks are done
  454. *
  455. */
  456. static void bam_dma_terminate_all(struct bam_chan *bchan)
  457. {
  458. unsigned long flag;
  459. LIST_HEAD(head);
  460. /* remove all transactions, including active transaction */
  461. spin_lock_irqsave(&bchan->vc.lock, flag);
  462. if (bchan->curr_txd) {
  463. list_add(&bchan->curr_txd->vd.node, &bchan->vc.desc_issued);
  464. bchan->curr_txd = NULL;
  465. }
  466. vchan_get_all_descriptors(&bchan->vc, &head);
  467. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  468. vchan_dma_desc_free_list(&bchan->vc, &head);
  469. }
  470. /**
  471. * bam_control - DMA device control
  472. * @chan: dma channel
  473. * @cmd: control cmd
  474. * @arg: cmd argument
  475. *
  476. * Perform DMA control command
  477. *
  478. */
  479. static int bam_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  480. unsigned long arg)
  481. {
  482. struct bam_chan *bchan = to_bam_chan(chan);
  483. struct bam_device *bdev = bchan->bdev;
  484. int ret = 0;
  485. unsigned long flag;
  486. switch (cmd) {
  487. case DMA_PAUSE:
  488. spin_lock_irqsave(&bchan->vc.lock, flag);
  489. writel_relaxed(1, bdev->regs + BAM_P_HALT(bchan->id));
  490. bchan->paused = 1;
  491. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  492. break;
  493. case DMA_RESUME:
  494. spin_lock_irqsave(&bchan->vc.lock, flag);
  495. writel_relaxed(0, bdev->regs + BAM_P_HALT(bchan->id));
  496. bchan->paused = 0;
  497. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  498. break;
  499. case DMA_TERMINATE_ALL:
  500. bam_dma_terminate_all(bchan);
  501. break;
  502. case DMA_SLAVE_CONFIG:
  503. spin_lock_irqsave(&bchan->vc.lock, flag);
  504. bam_slave_config(bchan, (struct dma_slave_config *)arg);
  505. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  506. break;
  507. default:
  508. ret = -ENXIO;
  509. break;
  510. }
  511. return ret;
  512. }
  513. /**
  514. * process_channel_irqs - processes the channel interrupts
  515. * @bdev: bam controller
  516. *
  517. * This function processes the channel interrupts
  518. *
  519. */
  520. static u32 process_channel_irqs(struct bam_device *bdev)
  521. {
  522. u32 i, srcs, pipe_stts;
  523. unsigned long flags;
  524. struct bam_async_desc *async_desc;
  525. srcs = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_EE(bdev->ee));
  526. /* return early if no pipe/channel interrupts are present */
  527. if (!(srcs & P_IRQ))
  528. return srcs;
  529. for (i = 0; i < bdev->num_channels; i++) {
  530. struct bam_chan *bchan = &bdev->channels[i];
  531. if (!(srcs & BIT(i)))
  532. continue;
  533. /* clear pipe irq */
  534. pipe_stts = readl_relaxed(bdev->regs +
  535. BAM_P_IRQ_STTS(i));
  536. writel_relaxed(pipe_stts, bdev->regs +
  537. BAM_P_IRQ_CLR(i));
  538. spin_lock_irqsave(&bchan->vc.lock, flags);
  539. async_desc = bchan->curr_txd;
  540. if (async_desc) {
  541. async_desc->num_desc -= async_desc->xfer_len;
  542. async_desc->curr_desc += async_desc->xfer_len;
  543. bchan->curr_txd = NULL;
  544. /* manage FIFO */
  545. bchan->head += async_desc->xfer_len;
  546. bchan->head %= MAX_DESCRIPTORS;
  547. /*
  548. * if complete, process cookie. Otherwise
  549. * push back to front of desc_issued so that
  550. * it gets restarted by the tasklet
  551. */
  552. if (!async_desc->num_desc)
  553. vchan_cookie_complete(&async_desc->vd);
  554. else
  555. list_add(&async_desc->vd.node,
  556. &bchan->vc.desc_issued);
  557. }
  558. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  559. }
  560. return srcs;
  561. }
  562. /**
  563. * bam_dma_irq - irq handler for bam controller
  564. * @irq: IRQ of interrupt
  565. * @data: callback data
  566. *
  567. * IRQ handler for the bam controller
  568. */
  569. static irqreturn_t bam_dma_irq(int irq, void *data)
  570. {
  571. struct bam_device *bdev = data;
  572. u32 clr_mask = 0, srcs = 0;
  573. srcs |= process_channel_irqs(bdev);
  574. /* kick off tasklet to start next dma transfer */
  575. if (srcs & P_IRQ)
  576. tasklet_schedule(&bdev->task);
  577. if (srcs & BAM_IRQ)
  578. clr_mask = readl_relaxed(bdev->regs + BAM_IRQ_STTS);
  579. /* don't allow reorder of the various accesses to the BAM registers */
  580. mb();
  581. writel_relaxed(clr_mask, bdev->regs + BAM_IRQ_CLR);
  582. return IRQ_HANDLED;
  583. }
  584. /**
  585. * bam_tx_status - returns status of transaction
  586. * @chan: dma channel
  587. * @cookie: transaction cookie
  588. * @txstate: DMA transaction state
  589. *
  590. * Return status of dma transaction
  591. */
  592. static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  593. struct dma_tx_state *txstate)
  594. {
  595. struct bam_chan *bchan = to_bam_chan(chan);
  596. struct virt_dma_desc *vd;
  597. int ret;
  598. size_t residue = 0;
  599. unsigned int i;
  600. unsigned long flags;
  601. ret = dma_cookie_status(chan, cookie, txstate);
  602. if (ret == DMA_COMPLETE)
  603. return ret;
  604. if (!txstate)
  605. return bchan->paused ? DMA_PAUSED : ret;
  606. spin_lock_irqsave(&bchan->vc.lock, flags);
  607. vd = vchan_find_desc(&bchan->vc, cookie);
  608. if (vd)
  609. residue = container_of(vd, struct bam_async_desc, vd)->length;
  610. else if (bchan->curr_txd && bchan->curr_txd->vd.tx.cookie == cookie)
  611. for (i = 0; i < bchan->curr_txd->num_desc; i++)
  612. residue += bchan->curr_txd->curr_desc[i].size;
  613. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  614. dma_set_residue(txstate, residue);
  615. if (ret == DMA_IN_PROGRESS && bchan->paused)
  616. ret = DMA_PAUSED;
  617. return ret;
  618. }
  619. /**
  620. * bam_apply_new_config
  621. * @bchan: bam dma channel
  622. * @dir: DMA direction
  623. */
  624. static void bam_apply_new_config(struct bam_chan *bchan,
  625. enum dma_transfer_direction dir)
  626. {
  627. struct bam_device *bdev = bchan->bdev;
  628. u32 maxburst;
  629. if (dir == DMA_DEV_TO_MEM)
  630. maxburst = bchan->slave.src_maxburst;
  631. else
  632. maxburst = bchan->slave.dst_maxburst;
  633. writel_relaxed(maxburst, bdev->regs + BAM_DESC_CNT_TRSHLD);
  634. bchan->reconfigure = 0;
  635. }
  636. /**
  637. * bam_start_dma - start next transaction
  638. * @bchan - bam dma channel
  639. */
  640. static void bam_start_dma(struct bam_chan *bchan)
  641. {
  642. struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
  643. struct bam_device *bdev = bchan->bdev;
  644. struct bam_async_desc *async_desc;
  645. struct bam_desc_hw *desc;
  646. struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
  647. sizeof(struct bam_desc_hw));
  648. lockdep_assert_held(&bchan->vc.lock);
  649. if (!vd)
  650. return;
  651. list_del(&vd->node);
  652. async_desc = container_of(vd, struct bam_async_desc, vd);
  653. bchan->curr_txd = async_desc;
  654. /* on first use, initialize the channel hardware */
  655. if (!bchan->initialized)
  656. bam_chan_init_hw(bchan, async_desc->dir);
  657. /* apply new slave config changes, if necessary */
  658. if (bchan->reconfigure)
  659. bam_apply_new_config(bchan, async_desc->dir);
  660. desc = bchan->curr_txd->curr_desc;
  661. if (async_desc->num_desc > MAX_DESCRIPTORS)
  662. async_desc->xfer_len = MAX_DESCRIPTORS;
  663. else
  664. async_desc->xfer_len = async_desc->num_desc;
  665. /* set INT on last descriptor */
  666. desc[async_desc->xfer_len - 1].flags |= DESC_FLAG_INT;
  667. if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
  668. u32 partial = MAX_DESCRIPTORS - bchan->tail;
  669. memcpy(&fifo[bchan->tail], desc,
  670. partial * sizeof(struct bam_desc_hw));
  671. memcpy(fifo, &desc[partial], (async_desc->xfer_len - partial) *
  672. sizeof(struct bam_desc_hw));
  673. } else {
  674. memcpy(&fifo[bchan->tail], desc,
  675. async_desc->xfer_len * sizeof(struct bam_desc_hw));
  676. }
  677. bchan->tail += async_desc->xfer_len;
  678. bchan->tail %= MAX_DESCRIPTORS;
  679. /* ensure descriptor writes and dma start not reordered */
  680. wmb();
  681. writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
  682. bdev->regs + BAM_P_EVNT_REG(bchan->id));
  683. }
  684. /**
  685. * dma_tasklet - DMA IRQ tasklet
  686. * @data: tasklet argument (bam controller structure)
  687. *
  688. * Sets up next DMA operation and then processes all completed transactions
  689. */
  690. static void dma_tasklet(unsigned long data)
  691. {
  692. struct bam_device *bdev = (struct bam_device *)data;
  693. struct bam_chan *bchan;
  694. unsigned long flags;
  695. unsigned int i;
  696. /* go through the channels and kick off transactions */
  697. for (i = 0; i < bdev->num_channels; i++) {
  698. bchan = &bdev->channels[i];
  699. spin_lock_irqsave(&bchan->vc.lock, flags);
  700. if (!list_empty(&bchan->vc.desc_issued) && !bchan->curr_txd)
  701. bam_start_dma(bchan);
  702. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  703. }
  704. }
  705. /**
  706. * bam_issue_pending - starts pending transactions
  707. * @chan: dma channel
  708. *
  709. * Calls tasklet directly which in turn starts any pending transactions
  710. */
  711. static void bam_issue_pending(struct dma_chan *chan)
  712. {
  713. struct bam_chan *bchan = to_bam_chan(chan);
  714. unsigned long flags;
  715. spin_lock_irqsave(&bchan->vc.lock, flags);
  716. /* if work pending and idle, start a transaction */
  717. if (vchan_issue_pending(&bchan->vc) && !bchan->curr_txd)
  718. bam_start_dma(bchan);
  719. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  720. }
  721. /**
  722. * bam_dma_free_desc - free descriptor memory
  723. * @vd: virtual descriptor
  724. *
  725. */
  726. static void bam_dma_free_desc(struct virt_dma_desc *vd)
  727. {
  728. struct bam_async_desc *async_desc = container_of(vd,
  729. struct bam_async_desc, vd);
  730. kfree(async_desc);
  731. }
  732. static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
  733. struct of_dma *of)
  734. {
  735. struct bam_device *bdev = container_of(of->of_dma_data,
  736. struct bam_device, common);
  737. unsigned int request;
  738. if (dma_spec->args_count != 1)
  739. return NULL;
  740. request = dma_spec->args[0];
  741. if (request >= bdev->num_channels)
  742. return NULL;
  743. return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
  744. }
  745. /**
  746. * bam_init
  747. * @bdev: bam device
  748. *
  749. * Initialization helper for global bam registers
  750. */
  751. static int bam_init(struct bam_device *bdev)
  752. {
  753. u32 val;
  754. /* read revision and configuration information */
  755. val = readl_relaxed(bdev->regs + BAM_REVISION) >> NUM_EES_SHIFT;
  756. val &= NUM_EES_MASK;
  757. /* check that configured EE is within range */
  758. if (bdev->ee >= val)
  759. return -EINVAL;
  760. val = readl_relaxed(bdev->regs + BAM_NUM_PIPES);
  761. bdev->num_channels = val & BAM_NUM_PIPES_MASK;
  762. /* s/w reset bam */
  763. /* after reset all pipes are disabled and idle */
  764. val = readl_relaxed(bdev->regs + BAM_CTRL);
  765. val |= BAM_SW_RST;
  766. writel_relaxed(val, bdev->regs + BAM_CTRL);
  767. val &= ~BAM_SW_RST;
  768. writel_relaxed(val, bdev->regs + BAM_CTRL);
  769. /* make sure previous stores are visible before enabling BAM */
  770. wmb();
  771. /* enable bam */
  772. val |= BAM_EN;
  773. writel_relaxed(val, bdev->regs + BAM_CTRL);
  774. /* set descriptor threshhold, start with 4 bytes */
  775. writel_relaxed(DEFAULT_CNT_THRSHLD, bdev->regs + BAM_DESC_CNT_TRSHLD);
  776. /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
  777. writel_relaxed(BAM_CNFG_BITS_DEFAULT, bdev->regs + BAM_CNFG_BITS);
  778. /* enable irqs for errors */
  779. writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
  780. bdev->regs + BAM_IRQ_EN);
  781. /* unmask global bam interrupt */
  782. writel_relaxed(BAM_IRQ_MSK, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  783. return 0;
  784. }
  785. static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
  786. u32 index)
  787. {
  788. bchan->id = index;
  789. bchan->bdev = bdev;
  790. vchan_init(&bchan->vc, &bdev->common);
  791. bchan->vc.desc_free = bam_dma_free_desc;
  792. }
  793. static int bam_dma_probe(struct platform_device *pdev)
  794. {
  795. struct bam_device *bdev;
  796. struct resource *iores;
  797. int ret, i;
  798. bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
  799. if (!bdev)
  800. return -ENOMEM;
  801. bdev->dev = &pdev->dev;
  802. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  803. bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
  804. if (IS_ERR(bdev->regs))
  805. return PTR_ERR(bdev->regs);
  806. bdev->irq = platform_get_irq(pdev, 0);
  807. if (bdev->irq < 0)
  808. return bdev->irq;
  809. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
  810. if (ret) {
  811. dev_err(bdev->dev, "Execution environment unspecified\n");
  812. return ret;
  813. }
  814. bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
  815. if (IS_ERR(bdev->bamclk))
  816. return PTR_ERR(bdev->bamclk);
  817. ret = clk_prepare_enable(bdev->bamclk);
  818. if (ret) {
  819. dev_err(bdev->dev, "failed to prepare/enable clock\n");
  820. return ret;
  821. }
  822. ret = bam_init(bdev);
  823. if (ret)
  824. goto err_disable_clk;
  825. tasklet_init(&bdev->task, dma_tasklet, (unsigned long)bdev);
  826. bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
  827. sizeof(*bdev->channels), GFP_KERNEL);
  828. if (!bdev->channels) {
  829. ret = -ENOMEM;
  830. goto err_disable_clk;
  831. }
  832. /* allocate and initialize channels */
  833. INIT_LIST_HEAD(&bdev->common.channels);
  834. for (i = 0; i < bdev->num_channels; i++)
  835. bam_channel_init(bdev, &bdev->channels[i], i);
  836. ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
  837. IRQF_TRIGGER_HIGH, "bam_dma", bdev);
  838. if (ret)
  839. goto err_disable_clk;
  840. /* set max dma segment size */
  841. bdev->common.dev = bdev->dev;
  842. bdev->common.dev->dma_parms = &bdev->dma_parms;
  843. ret = dma_set_max_seg_size(bdev->common.dev, BAM_MAX_DATA_SIZE);
  844. if (ret) {
  845. dev_err(bdev->dev, "cannot set maximum segment size\n");
  846. goto err_disable_clk;
  847. }
  848. platform_set_drvdata(pdev, bdev);
  849. /* set capabilities */
  850. dma_cap_zero(bdev->common.cap_mask);
  851. dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
  852. /* initialize dmaengine apis */
  853. bdev->common.device_alloc_chan_resources = bam_alloc_chan;
  854. bdev->common.device_free_chan_resources = bam_free_chan;
  855. bdev->common.device_prep_slave_sg = bam_prep_slave_sg;
  856. bdev->common.device_control = bam_control;
  857. bdev->common.device_issue_pending = bam_issue_pending;
  858. bdev->common.device_tx_status = bam_tx_status;
  859. bdev->common.dev = bdev->dev;
  860. ret = dma_async_device_register(&bdev->common);
  861. if (ret) {
  862. dev_err(bdev->dev, "failed to register dma async device\n");
  863. goto err_disable_clk;
  864. }
  865. ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
  866. &bdev->common);
  867. if (ret)
  868. goto err_unregister_dma;
  869. return 0;
  870. err_unregister_dma:
  871. dma_async_device_unregister(&bdev->common);
  872. err_disable_clk:
  873. clk_disable_unprepare(bdev->bamclk);
  874. return ret;
  875. }
  876. static int bam_dma_remove(struct platform_device *pdev)
  877. {
  878. struct bam_device *bdev = platform_get_drvdata(pdev);
  879. u32 i;
  880. of_dma_controller_free(pdev->dev.of_node);
  881. dma_async_device_unregister(&bdev->common);
  882. /* mask all interrupts for this execution environment */
  883. writel_relaxed(0, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
  884. devm_free_irq(bdev->dev, bdev->irq, bdev);
  885. for (i = 0; i < bdev->num_channels; i++) {
  886. bam_dma_terminate_all(&bdev->channels[i]);
  887. tasklet_kill(&bdev->channels[i].vc.task);
  888. dma_free_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE,
  889. bdev->channels[i].fifo_virt,
  890. bdev->channels[i].fifo_phys);
  891. }
  892. tasklet_kill(&bdev->task);
  893. clk_disable_unprepare(bdev->bamclk);
  894. return 0;
  895. }
  896. static const struct of_device_id bam_of_match[] = {
  897. { .compatible = "qcom,bam-v1.4.0", },
  898. {}
  899. };
  900. MODULE_DEVICE_TABLE(of, bam_of_match);
  901. static struct platform_driver bam_dma_driver = {
  902. .probe = bam_dma_probe,
  903. .remove = bam_dma_remove,
  904. .driver = {
  905. .name = "bam-dma-engine",
  906. .owner = THIS_MODULE,
  907. .of_match_table = bam_of_match,
  908. },
  909. };
  910. module_platform_driver(bam_dma_driver);
  911. MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  912. MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
  913. MODULE_LICENSE("GPL v2");