qcom_bam_dma.c 29 KB

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