qcom_bam_dma.c 32 KB

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