qcom_bam_dma.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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] = { 0x102C, 0x00, 0x1000, 0x00 },
  153. [BAM_P_EVNT_REG] = { 0x1018, 0x00, 0x1000, 0x00 },
  154. [BAM_P_SW_OFSTS] = { 0x1000, 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 void bam_slave_config(struct bam_chan *bchan,
  461. struct dma_slave_config *cfg)
  462. {
  463. memcpy(&bchan->slave, cfg, sizeof(*cfg));
  464. bchan->reconfigure = 1;
  465. }
  466. /**
  467. * bam_prep_slave_sg - Prep slave sg transaction
  468. *
  469. * @chan: dma channel
  470. * @sgl: scatter gather list
  471. * @sg_len: length of sg
  472. * @direction: DMA transfer direction
  473. * @flags: DMA flags
  474. * @context: transfer context (unused)
  475. */
  476. static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,
  477. struct scatterlist *sgl, unsigned int sg_len,
  478. enum dma_transfer_direction direction, unsigned long flags,
  479. void *context)
  480. {
  481. struct bam_chan *bchan = to_bam_chan(chan);
  482. struct bam_device *bdev = bchan->bdev;
  483. struct bam_async_desc *async_desc;
  484. struct scatterlist *sg;
  485. u32 i;
  486. struct bam_desc_hw *desc;
  487. unsigned int num_alloc = 0;
  488. if (!is_slave_direction(direction)) {
  489. dev_err(bdev->dev, "invalid dma direction\n");
  490. return NULL;
  491. }
  492. /* calculate number of required entries */
  493. for_each_sg(sgl, sg, sg_len, i)
  494. num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_MAX_DATA_SIZE);
  495. /* allocate enough room to accomodate the number of entries */
  496. async_desc = kzalloc(sizeof(*async_desc) +
  497. (num_alloc * sizeof(struct bam_desc_hw)), GFP_NOWAIT);
  498. if (!async_desc)
  499. goto err_out;
  500. if (flags & DMA_PREP_FENCE)
  501. async_desc->flags |= DESC_FLAG_NWD;
  502. if (flags & DMA_PREP_INTERRUPT)
  503. async_desc->flags |= DESC_FLAG_EOT;
  504. else
  505. async_desc->flags |= DESC_FLAG_INT;
  506. async_desc->num_desc = num_alloc;
  507. async_desc->curr_desc = async_desc->desc;
  508. async_desc->dir = direction;
  509. /* fill in temporary descriptors */
  510. desc = async_desc->desc;
  511. for_each_sg(sgl, sg, sg_len, i) {
  512. unsigned int remainder = sg_dma_len(sg);
  513. unsigned int curr_offset = 0;
  514. do {
  515. desc->addr = sg_dma_address(sg) + curr_offset;
  516. if (remainder > BAM_MAX_DATA_SIZE) {
  517. desc->size = BAM_MAX_DATA_SIZE;
  518. remainder -= BAM_MAX_DATA_SIZE;
  519. curr_offset += BAM_MAX_DATA_SIZE;
  520. } else {
  521. desc->size = remainder;
  522. remainder = 0;
  523. }
  524. async_desc->length += desc->size;
  525. desc++;
  526. } while (remainder > 0);
  527. }
  528. return vchan_tx_prep(&bchan->vc, &async_desc->vd, flags);
  529. err_out:
  530. kfree(async_desc);
  531. return NULL;
  532. }
  533. /**
  534. * bam_dma_terminate_all - terminate all transactions on a channel
  535. * @bchan: bam dma channel
  536. *
  537. * Dequeues and frees all transactions
  538. * No callbacks are done
  539. *
  540. */
  541. static void bam_dma_terminate_all(struct bam_chan *bchan)
  542. {
  543. unsigned long flag;
  544. LIST_HEAD(head);
  545. /* remove all transactions, including active transaction */
  546. spin_lock_irqsave(&bchan->vc.lock, flag);
  547. if (bchan->curr_txd) {
  548. list_add(&bchan->curr_txd->vd.node, &bchan->vc.desc_issued);
  549. bchan->curr_txd = NULL;
  550. }
  551. vchan_get_all_descriptors(&bchan->vc, &head);
  552. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  553. vchan_dma_desc_free_list(&bchan->vc, &head);
  554. }
  555. /**
  556. * bam_control - DMA device control
  557. * @chan: dma channel
  558. * @cmd: control cmd
  559. * @arg: cmd argument
  560. *
  561. * Perform DMA control command
  562. *
  563. */
  564. static int bam_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  565. unsigned long arg)
  566. {
  567. struct bam_chan *bchan = to_bam_chan(chan);
  568. struct bam_device *bdev = bchan->bdev;
  569. int ret = 0;
  570. unsigned long flag;
  571. switch (cmd) {
  572. case DMA_PAUSE:
  573. spin_lock_irqsave(&bchan->vc.lock, flag);
  574. writel_relaxed(1, bam_addr(bdev, bchan->id, BAM_P_HALT));
  575. bchan->paused = 1;
  576. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  577. break;
  578. case DMA_RESUME:
  579. spin_lock_irqsave(&bchan->vc.lock, flag);
  580. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_HALT));
  581. bchan->paused = 0;
  582. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  583. break;
  584. case DMA_TERMINATE_ALL:
  585. bam_dma_terminate_all(bchan);
  586. break;
  587. case DMA_SLAVE_CONFIG:
  588. spin_lock_irqsave(&bchan->vc.lock, flag);
  589. bam_slave_config(bchan, (struct dma_slave_config *)arg);
  590. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  591. break;
  592. default:
  593. ret = -ENXIO;
  594. break;
  595. }
  596. return ret;
  597. }
  598. /**
  599. * process_channel_irqs - processes the channel interrupts
  600. * @bdev: bam controller
  601. *
  602. * This function processes the channel interrupts
  603. *
  604. */
  605. static u32 process_channel_irqs(struct bam_device *bdev)
  606. {
  607. u32 i, srcs, pipe_stts;
  608. unsigned long flags;
  609. struct bam_async_desc *async_desc;
  610. srcs = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_EE));
  611. /* return early if no pipe/channel interrupts are present */
  612. if (!(srcs & P_IRQ))
  613. return srcs;
  614. for (i = 0; i < bdev->num_channels; i++) {
  615. struct bam_chan *bchan = &bdev->channels[i];
  616. if (!(srcs & BIT(i)))
  617. continue;
  618. /* clear pipe irq */
  619. pipe_stts = readl_relaxed(bam_addr(bdev, i, BAM_P_IRQ_STTS));
  620. writel_relaxed(pipe_stts, bam_addr(bdev, i, BAM_P_IRQ_CLR));
  621. spin_lock_irqsave(&bchan->vc.lock, flags);
  622. async_desc = bchan->curr_txd;
  623. if (async_desc) {
  624. async_desc->num_desc -= async_desc->xfer_len;
  625. async_desc->curr_desc += async_desc->xfer_len;
  626. bchan->curr_txd = NULL;
  627. /* manage FIFO */
  628. bchan->head += async_desc->xfer_len;
  629. bchan->head %= MAX_DESCRIPTORS;
  630. /*
  631. * if complete, process cookie. Otherwise
  632. * push back to front of desc_issued so that
  633. * it gets restarted by the tasklet
  634. */
  635. if (!async_desc->num_desc)
  636. vchan_cookie_complete(&async_desc->vd);
  637. else
  638. list_add(&async_desc->vd.node,
  639. &bchan->vc.desc_issued);
  640. }
  641. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  642. }
  643. return srcs;
  644. }
  645. /**
  646. * bam_dma_irq - irq handler for bam controller
  647. * @irq: IRQ of interrupt
  648. * @data: callback data
  649. *
  650. * IRQ handler for the bam controller
  651. */
  652. static irqreturn_t bam_dma_irq(int irq, void *data)
  653. {
  654. struct bam_device *bdev = data;
  655. u32 clr_mask = 0, srcs = 0;
  656. srcs |= process_channel_irqs(bdev);
  657. /* kick off tasklet to start next dma transfer */
  658. if (srcs & P_IRQ)
  659. tasklet_schedule(&bdev->task);
  660. if (srcs & BAM_IRQ)
  661. clr_mask = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_STTS));
  662. /* don't allow reorder of the various accesses to the BAM registers */
  663. mb();
  664. writel_relaxed(clr_mask, bam_addr(bdev, 0, BAM_IRQ_CLR));
  665. return IRQ_HANDLED;
  666. }
  667. /**
  668. * bam_tx_status - returns status of transaction
  669. * @chan: dma channel
  670. * @cookie: transaction cookie
  671. * @txstate: DMA transaction state
  672. *
  673. * Return status of dma transaction
  674. */
  675. static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  676. struct dma_tx_state *txstate)
  677. {
  678. struct bam_chan *bchan = to_bam_chan(chan);
  679. struct virt_dma_desc *vd;
  680. int ret;
  681. size_t residue = 0;
  682. unsigned int i;
  683. unsigned long flags;
  684. ret = dma_cookie_status(chan, cookie, txstate);
  685. if (ret == DMA_COMPLETE)
  686. return ret;
  687. if (!txstate)
  688. return bchan->paused ? DMA_PAUSED : ret;
  689. spin_lock_irqsave(&bchan->vc.lock, flags);
  690. vd = vchan_find_desc(&bchan->vc, cookie);
  691. if (vd)
  692. residue = container_of(vd, struct bam_async_desc, vd)->length;
  693. else if (bchan->curr_txd && bchan->curr_txd->vd.tx.cookie == cookie)
  694. for (i = 0; i < bchan->curr_txd->num_desc; i++)
  695. residue += bchan->curr_txd->curr_desc[i].size;
  696. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  697. dma_set_residue(txstate, residue);
  698. if (ret == DMA_IN_PROGRESS && bchan->paused)
  699. ret = DMA_PAUSED;
  700. return ret;
  701. }
  702. /**
  703. * bam_apply_new_config
  704. * @bchan: bam dma channel
  705. * @dir: DMA direction
  706. */
  707. static void bam_apply_new_config(struct bam_chan *bchan,
  708. enum dma_transfer_direction dir)
  709. {
  710. struct bam_device *bdev = bchan->bdev;
  711. u32 maxburst;
  712. if (dir == DMA_DEV_TO_MEM)
  713. maxburst = bchan->slave.src_maxburst;
  714. else
  715. maxburst = bchan->slave.dst_maxburst;
  716. writel_relaxed(maxburst, bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  717. bchan->reconfigure = 0;
  718. }
  719. /**
  720. * bam_start_dma - start next transaction
  721. * @bchan - bam dma channel
  722. */
  723. static void bam_start_dma(struct bam_chan *bchan)
  724. {
  725. struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
  726. struct bam_device *bdev = bchan->bdev;
  727. struct bam_async_desc *async_desc;
  728. struct bam_desc_hw *desc;
  729. struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
  730. sizeof(struct bam_desc_hw));
  731. lockdep_assert_held(&bchan->vc.lock);
  732. if (!vd)
  733. return;
  734. list_del(&vd->node);
  735. async_desc = container_of(vd, struct bam_async_desc, vd);
  736. bchan->curr_txd = async_desc;
  737. /* on first use, initialize the channel hardware */
  738. if (!bchan->initialized)
  739. bam_chan_init_hw(bchan, async_desc->dir);
  740. /* apply new slave config changes, if necessary */
  741. if (bchan->reconfigure)
  742. bam_apply_new_config(bchan, async_desc->dir);
  743. desc = bchan->curr_txd->curr_desc;
  744. if (async_desc->num_desc > MAX_DESCRIPTORS)
  745. async_desc->xfer_len = MAX_DESCRIPTORS;
  746. else
  747. async_desc->xfer_len = async_desc->num_desc;
  748. /* set any special flags on the last descriptor */
  749. if (async_desc->num_desc == async_desc->xfer_len)
  750. desc[async_desc->xfer_len - 1].flags = async_desc->flags;
  751. else
  752. desc[async_desc->xfer_len - 1].flags |= DESC_FLAG_INT;
  753. if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
  754. u32 partial = MAX_DESCRIPTORS - bchan->tail;
  755. memcpy(&fifo[bchan->tail], desc,
  756. partial * sizeof(struct bam_desc_hw));
  757. memcpy(fifo, &desc[partial], (async_desc->xfer_len - partial) *
  758. sizeof(struct bam_desc_hw));
  759. } else {
  760. memcpy(&fifo[bchan->tail], desc,
  761. async_desc->xfer_len * sizeof(struct bam_desc_hw));
  762. }
  763. bchan->tail += async_desc->xfer_len;
  764. bchan->tail %= MAX_DESCRIPTORS;
  765. /* ensure descriptor writes and dma start not reordered */
  766. wmb();
  767. writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
  768. bam_addr(bdev, bchan->id, BAM_P_EVNT_REG));
  769. }
  770. /**
  771. * dma_tasklet - DMA IRQ tasklet
  772. * @data: tasklet argument (bam controller structure)
  773. *
  774. * Sets up next DMA operation and then processes all completed transactions
  775. */
  776. static void dma_tasklet(unsigned long data)
  777. {
  778. struct bam_device *bdev = (struct bam_device *)data;
  779. struct bam_chan *bchan;
  780. unsigned long flags;
  781. unsigned int i;
  782. /* go through the channels and kick off transactions */
  783. for (i = 0; i < bdev->num_channels; i++) {
  784. bchan = &bdev->channels[i];
  785. spin_lock_irqsave(&bchan->vc.lock, flags);
  786. if (!list_empty(&bchan->vc.desc_issued) && !bchan->curr_txd)
  787. bam_start_dma(bchan);
  788. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  789. }
  790. }
  791. /**
  792. * bam_issue_pending - starts pending transactions
  793. * @chan: dma channel
  794. *
  795. * Calls tasklet directly which in turn starts any pending transactions
  796. */
  797. static void bam_issue_pending(struct dma_chan *chan)
  798. {
  799. struct bam_chan *bchan = to_bam_chan(chan);
  800. unsigned long flags;
  801. spin_lock_irqsave(&bchan->vc.lock, flags);
  802. /* if work pending and idle, start a transaction */
  803. if (vchan_issue_pending(&bchan->vc) && !bchan->curr_txd)
  804. bam_start_dma(bchan);
  805. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  806. }
  807. /**
  808. * bam_dma_free_desc - free descriptor memory
  809. * @vd: virtual descriptor
  810. *
  811. */
  812. static void bam_dma_free_desc(struct virt_dma_desc *vd)
  813. {
  814. struct bam_async_desc *async_desc = container_of(vd,
  815. struct bam_async_desc, vd);
  816. kfree(async_desc);
  817. }
  818. static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
  819. struct of_dma *of)
  820. {
  821. struct bam_device *bdev = container_of(of->of_dma_data,
  822. struct bam_device, common);
  823. unsigned int request;
  824. if (dma_spec->args_count != 1)
  825. return NULL;
  826. request = dma_spec->args[0];
  827. if (request >= bdev->num_channels)
  828. return NULL;
  829. return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
  830. }
  831. /**
  832. * bam_init
  833. * @bdev: bam device
  834. *
  835. * Initialization helper for global bam registers
  836. */
  837. static int bam_init(struct bam_device *bdev)
  838. {
  839. u32 val;
  840. /* read revision and configuration information */
  841. val = readl_relaxed(bam_addr(bdev, 0, BAM_REVISION)) >> NUM_EES_SHIFT;
  842. val &= NUM_EES_MASK;
  843. /* check that configured EE is within range */
  844. if (bdev->ee >= val)
  845. return -EINVAL;
  846. val = readl_relaxed(bam_addr(bdev, 0, BAM_NUM_PIPES));
  847. bdev->num_channels = val & BAM_NUM_PIPES_MASK;
  848. /* s/w reset bam */
  849. /* after reset all pipes are disabled and idle */
  850. val = readl_relaxed(bam_addr(bdev, 0, BAM_CTRL));
  851. val |= BAM_SW_RST;
  852. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  853. val &= ~BAM_SW_RST;
  854. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  855. /* make sure previous stores are visible before enabling BAM */
  856. wmb();
  857. /* enable bam */
  858. val |= BAM_EN;
  859. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  860. /* set descriptor threshhold, start with 4 bytes */
  861. writel_relaxed(DEFAULT_CNT_THRSHLD,
  862. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  863. /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
  864. writel_relaxed(BAM_CNFG_BITS_DEFAULT, bam_addr(bdev, 0, BAM_CNFG_BITS));
  865. /* enable irqs for errors */
  866. writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
  867. bam_addr(bdev, 0, BAM_IRQ_EN));
  868. /* unmask global bam interrupt */
  869. writel_relaxed(BAM_IRQ_MSK, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  870. return 0;
  871. }
  872. static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
  873. u32 index)
  874. {
  875. bchan->id = index;
  876. bchan->bdev = bdev;
  877. vchan_init(&bchan->vc, &bdev->common);
  878. bchan->vc.desc_free = bam_dma_free_desc;
  879. }
  880. static const struct of_device_id bam_of_match[] = {
  881. { .compatible = "qcom,bam-v1.3.0", .data = &bam_v1_3_reg_info },
  882. { .compatible = "qcom,bam-v1.4.0", .data = &bam_v1_4_reg_info },
  883. {}
  884. };
  885. MODULE_DEVICE_TABLE(of, bam_of_match);
  886. static int bam_dma_probe(struct platform_device *pdev)
  887. {
  888. struct bam_device *bdev;
  889. const struct of_device_id *match;
  890. struct resource *iores;
  891. int ret, i;
  892. bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
  893. if (!bdev)
  894. return -ENOMEM;
  895. bdev->dev = &pdev->dev;
  896. match = of_match_node(bam_of_match, pdev->dev.of_node);
  897. if (!match) {
  898. dev_err(&pdev->dev, "Unsupported BAM module\n");
  899. return -ENODEV;
  900. }
  901. bdev->layout = match->data;
  902. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  903. bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
  904. if (IS_ERR(bdev->regs))
  905. return PTR_ERR(bdev->regs);
  906. bdev->irq = platform_get_irq(pdev, 0);
  907. if (bdev->irq < 0)
  908. return bdev->irq;
  909. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
  910. if (ret) {
  911. dev_err(bdev->dev, "Execution environment unspecified\n");
  912. return ret;
  913. }
  914. bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
  915. if (IS_ERR(bdev->bamclk))
  916. return PTR_ERR(bdev->bamclk);
  917. ret = clk_prepare_enable(bdev->bamclk);
  918. if (ret) {
  919. dev_err(bdev->dev, "failed to prepare/enable clock\n");
  920. return ret;
  921. }
  922. ret = bam_init(bdev);
  923. if (ret)
  924. goto err_disable_clk;
  925. tasklet_init(&bdev->task, dma_tasklet, (unsigned long)bdev);
  926. bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
  927. sizeof(*bdev->channels), GFP_KERNEL);
  928. if (!bdev->channels) {
  929. ret = -ENOMEM;
  930. goto err_disable_clk;
  931. }
  932. /* allocate and initialize channels */
  933. INIT_LIST_HEAD(&bdev->common.channels);
  934. for (i = 0; i < bdev->num_channels; i++)
  935. bam_channel_init(bdev, &bdev->channels[i], i);
  936. ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
  937. IRQF_TRIGGER_HIGH, "bam_dma", bdev);
  938. if (ret)
  939. goto err_disable_clk;
  940. /* set max dma segment size */
  941. bdev->common.dev = bdev->dev;
  942. bdev->common.dev->dma_parms = &bdev->dma_parms;
  943. ret = dma_set_max_seg_size(bdev->common.dev, BAM_MAX_DATA_SIZE);
  944. if (ret) {
  945. dev_err(bdev->dev, "cannot set maximum segment size\n");
  946. goto err_disable_clk;
  947. }
  948. platform_set_drvdata(pdev, bdev);
  949. /* set capabilities */
  950. dma_cap_zero(bdev->common.cap_mask);
  951. dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
  952. /* initialize dmaengine apis */
  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_control = bam_control;
  957. bdev->common.device_issue_pending = bam_issue_pending;
  958. bdev->common.device_tx_status = bam_tx_status;
  959. bdev->common.dev = bdev->dev;
  960. ret = dma_async_device_register(&bdev->common);
  961. if (ret) {
  962. dev_err(bdev->dev, "failed to register dma async device\n");
  963. goto err_disable_clk;
  964. }
  965. ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
  966. &bdev->common);
  967. if (ret)
  968. goto err_unregister_dma;
  969. return 0;
  970. err_unregister_dma:
  971. dma_async_device_unregister(&bdev->common);
  972. err_disable_clk:
  973. clk_disable_unprepare(bdev->bamclk);
  974. return ret;
  975. }
  976. static int bam_dma_remove(struct platform_device *pdev)
  977. {
  978. struct bam_device *bdev = platform_get_drvdata(pdev);
  979. u32 i;
  980. of_dma_controller_free(pdev->dev.of_node);
  981. dma_async_device_unregister(&bdev->common);
  982. /* mask all interrupts for this execution environment */
  983. writel_relaxed(0, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  984. devm_free_irq(bdev->dev, bdev->irq, bdev);
  985. for (i = 0; i < bdev->num_channels; i++) {
  986. bam_dma_terminate_all(&bdev->channels[i]);
  987. tasklet_kill(&bdev->channels[i].vc.task);
  988. dma_free_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE,
  989. bdev->channels[i].fifo_virt,
  990. bdev->channels[i].fifo_phys);
  991. }
  992. tasklet_kill(&bdev->task);
  993. clk_disable_unprepare(bdev->bamclk);
  994. return 0;
  995. }
  996. static struct platform_driver bam_dma_driver = {
  997. .probe = bam_dma_probe,
  998. .remove = bam_dma_remove,
  999. .driver = {
  1000. .name = "bam-dma-engine",
  1001. .of_match_table = bam_of_match,
  1002. },
  1003. };
  1004. module_platform_driver(bam_dma_driver);
  1005. MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  1006. MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
  1007. MODULE_LICENSE("GPL v2");