bam_dma.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2014, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /*
  7. * QCOM BAM DMA engine driver
  8. *
  9. * QCOM BAM DMA blocks are distributed amongst a number of the on-chip
  10. * peripherals on the MSM 8x74. The configuration of the channels are dependent
  11. * on the way they are hard wired to that specific peripheral. The peripheral
  12. * device tree entries specify the configuration of each channel.
  13. *
  14. * The DMA controller requires the use of external memory for storage of the
  15. * hardware descriptors for each channel. The descriptor FIFO is accessed as a
  16. * circular buffer and operations are managed according to the offset within the
  17. * FIFO. After pipe/channel reset, all of the pipe registers and internal state
  18. * are back to defaults.
  19. *
  20. * During DMA operations, we write descriptors to the FIFO, being careful to
  21. * handle wrapping and then write the last FIFO offset to that channel's
  22. * P_EVNT_REG register to kick off the transaction. The P_SW_OFSTS register
  23. * indicates the current FIFO offset that is being processed, so there is some
  24. * indication of where the hardware is currently working.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/io.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/device.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/of.h>
  37. #include <linux/of_address.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/of_dma.h>
  40. #include <linux/circ_buf.h>
  41. #include <linux/clk.h>
  42. #include <linux/dmaengine.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/ipc_logging.h>
  45. #include "../dmaengine.h"
  46. #include "../virt-dma.h"
  47. struct bam_desc_hw {
  48. __le32 addr; /* Buffer physical address */
  49. __le16 size; /* Buffer size in bytes */
  50. __le16 flags;
  51. };
  52. #define BAM_DMA_AUTOSUSPEND_DELAY 100
  53. #define DESC_FLAG_INT BIT(15)
  54. #define DESC_FLAG_EOT BIT(14)
  55. #define DESC_FLAG_EOB BIT(13)
  56. #define DESC_FLAG_NWD BIT(12)
  57. #define DESC_FLAG_CMD BIT(11)
  58. #define CREATE_TRACE_POINTS
  59. #include "bam_dma_trace.h"
  60. /* FTRACE Logging */
  61. static void __ftrace_dbg(struct device *dev, const char *fmt, ...)
  62. {
  63. struct va_format vaf = {
  64. .fmt = fmt,
  65. };
  66. va_list args;
  67. va_start(args, fmt);
  68. vaf.va = &args;
  69. trace_bam_dma_info(dev_name(dev), &vaf);
  70. va_end(args);
  71. }
  72. #define ftrace_dbg(dev, fmt, ...) \
  73. __ftrace_dbg(dev, fmt, ##__VA_ARGS__)\
  74. #ifdef CONFIG_DEBUG_FS
  75. #define DMA_IPC_LOGPAGES 1
  76. #define DMA_BAM_DBG(ctxt, dev, fmt...) do { \
  77. if (ctxt) { \
  78. ipc_log_string(ctxt, fmt); \
  79. } \
  80. ftrace_dbg(dev, fmt); \
  81. } while (0)
  82. #else
  83. #define DMA_BAM_DBG(ctxt, dev, fmt...) do { \
  84. pr_debug(fmt); \
  85. ftrace_dbg(dev, fmt); \
  86. } while (0)
  87. #endif
  88. struct bam_async_desc {
  89. struct virt_dma_desc vd;
  90. u32 num_desc;
  91. u32 xfer_len;
  92. /* transaction flags, EOT|EOB|NWD */
  93. u16 flags;
  94. struct bam_desc_hw *curr_desc;
  95. /* list node for the desc in the bam_chan list of descriptors */
  96. struct list_head desc_node;
  97. enum dma_transfer_direction dir;
  98. size_t length;
  99. struct bam_desc_hw desc[];
  100. };
  101. enum bam_reg {
  102. BAM_CTRL,
  103. BAM_REVISION,
  104. BAM_NUM_PIPES,
  105. BAM_DESC_CNT_TRSHLD,
  106. BAM_IRQ_SRCS,
  107. BAM_IRQ_SRCS_MSK,
  108. BAM_IRQ_SRCS_UNMASKED,
  109. BAM_IRQ_STTS,
  110. BAM_IRQ_CLR,
  111. BAM_IRQ_EN,
  112. BAM_CNFG_BITS,
  113. BAM_IRQ_SRCS_EE,
  114. BAM_IRQ_SRCS_MSK_EE,
  115. BAM_P_CTRL,
  116. BAM_P_RST,
  117. BAM_P_HALT,
  118. BAM_P_IRQ_STTS,
  119. BAM_P_IRQ_CLR,
  120. BAM_P_IRQ_EN,
  121. BAM_P_EVNT_DEST_ADDR,
  122. BAM_P_EVNT_REG,
  123. BAM_P_SW_OFSTS,
  124. BAM_P_DATA_FIFO_ADDR,
  125. BAM_P_DESC_FIFO_ADDR,
  126. BAM_P_EVNT_GEN_TRSHLD,
  127. BAM_P_FIFO_SIZES,
  128. };
  129. struct reg_offset_data {
  130. u32 base_offset;
  131. unsigned int pipe_mult, evnt_mult, ee_mult;
  132. };
  133. static const struct reg_offset_data bam_v1_3_reg_info[] = {
  134. [BAM_CTRL] = { 0x0F80, 0x00, 0x00, 0x00 },
  135. [BAM_REVISION] = { 0x0F84, 0x00, 0x00, 0x00 },
  136. [BAM_NUM_PIPES] = { 0x0FBC, 0x00, 0x00, 0x00 },
  137. [BAM_DESC_CNT_TRSHLD] = { 0x0F88, 0x00, 0x00, 0x00 },
  138. [BAM_IRQ_SRCS] = { 0x0F8C, 0x00, 0x00, 0x00 },
  139. [BAM_IRQ_SRCS_MSK] = { 0x0F90, 0x00, 0x00, 0x00 },
  140. [BAM_IRQ_SRCS_UNMASKED] = { 0x0FB0, 0x00, 0x00, 0x00 },
  141. [BAM_IRQ_STTS] = { 0x0F94, 0x00, 0x00, 0x00 },
  142. [BAM_IRQ_CLR] = { 0x0F98, 0x00, 0x00, 0x00 },
  143. [BAM_IRQ_EN] = { 0x0F9C, 0x00, 0x00, 0x00 },
  144. [BAM_CNFG_BITS] = { 0x0FFC, 0x00, 0x00, 0x00 },
  145. [BAM_IRQ_SRCS_EE] = { 0x1800, 0x00, 0x00, 0x80 },
  146. [BAM_IRQ_SRCS_MSK_EE] = { 0x1804, 0x00, 0x00, 0x80 },
  147. [BAM_P_CTRL] = { 0x0000, 0x80, 0x00, 0x00 },
  148. [BAM_P_RST] = { 0x0004, 0x80, 0x00, 0x00 },
  149. [BAM_P_HALT] = { 0x0008, 0x80, 0x00, 0x00 },
  150. [BAM_P_IRQ_STTS] = { 0x0010, 0x80, 0x00, 0x00 },
  151. [BAM_P_IRQ_CLR] = { 0x0014, 0x80, 0x00, 0x00 },
  152. [BAM_P_IRQ_EN] = { 0x0018, 0x80, 0x00, 0x00 },
  153. [BAM_P_EVNT_DEST_ADDR] = { 0x102C, 0x00, 0x40, 0x00 },
  154. [BAM_P_EVNT_REG] = { 0x1018, 0x00, 0x40, 0x00 },
  155. [BAM_P_SW_OFSTS] = { 0x1000, 0x00, 0x40, 0x00 },
  156. [BAM_P_DATA_FIFO_ADDR] = { 0x1024, 0x00, 0x40, 0x00 },
  157. [BAM_P_DESC_FIFO_ADDR] = { 0x101C, 0x00, 0x40, 0x00 },
  158. [BAM_P_EVNT_GEN_TRSHLD] = { 0x1028, 0x00, 0x40, 0x00 },
  159. [BAM_P_FIFO_SIZES] = { 0x1020, 0x00, 0x40, 0x00 },
  160. };
  161. static const struct reg_offset_data bam_v1_4_reg_info[] = {
  162. [BAM_CTRL] = { 0x0000, 0x00, 0x00, 0x00 },
  163. [BAM_REVISION] = { 0x0004, 0x00, 0x00, 0x00 },
  164. [BAM_NUM_PIPES] = { 0x003C, 0x00, 0x00, 0x00 },
  165. [BAM_DESC_CNT_TRSHLD] = { 0x0008, 0x00, 0x00, 0x00 },
  166. [BAM_IRQ_SRCS] = { 0x000C, 0x00, 0x00, 0x00 },
  167. [BAM_IRQ_SRCS_MSK] = { 0x0010, 0x00, 0x00, 0x00 },
  168. [BAM_IRQ_SRCS_UNMASKED] = { 0x0030, 0x00, 0x00, 0x00 },
  169. [BAM_IRQ_STTS] = { 0x0014, 0x00, 0x00, 0x00 },
  170. [BAM_IRQ_CLR] = { 0x0018, 0x00, 0x00, 0x00 },
  171. [BAM_IRQ_EN] = { 0x001C, 0x00, 0x00, 0x00 },
  172. [BAM_CNFG_BITS] = { 0x007C, 0x00, 0x00, 0x00 },
  173. [BAM_IRQ_SRCS_EE] = { 0x0800, 0x00, 0x00, 0x80 },
  174. [BAM_IRQ_SRCS_MSK_EE] = { 0x0804, 0x00, 0x00, 0x80 },
  175. [BAM_P_CTRL] = { 0x1000, 0x1000, 0x00, 0x00 },
  176. [BAM_P_RST] = { 0x1004, 0x1000, 0x00, 0x00 },
  177. [BAM_P_HALT] = { 0x1008, 0x1000, 0x00, 0x00 },
  178. [BAM_P_IRQ_STTS] = { 0x1010, 0x1000, 0x00, 0x00 },
  179. [BAM_P_IRQ_CLR] = { 0x1014, 0x1000, 0x00, 0x00 },
  180. [BAM_P_IRQ_EN] = { 0x1018, 0x1000, 0x00, 0x00 },
  181. [BAM_P_EVNT_DEST_ADDR] = { 0x182C, 0x00, 0x1000, 0x00 },
  182. [BAM_P_EVNT_REG] = { 0x1818, 0x00, 0x1000, 0x00 },
  183. [BAM_P_SW_OFSTS] = { 0x1800, 0x00, 0x1000, 0x00 },
  184. [BAM_P_DATA_FIFO_ADDR] = { 0x1824, 0x00, 0x1000, 0x00 },
  185. [BAM_P_DESC_FIFO_ADDR] = { 0x181C, 0x00, 0x1000, 0x00 },
  186. [BAM_P_EVNT_GEN_TRSHLD] = { 0x1828, 0x00, 0x1000, 0x00 },
  187. [BAM_P_FIFO_SIZES] = { 0x1820, 0x00, 0x1000, 0x00 },
  188. };
  189. static const struct reg_offset_data bam_v1_7_reg_info[] = {
  190. [BAM_CTRL] = { 0x00000, 0x00, 0x00, 0x00 },
  191. [BAM_REVISION] = { 0x01000, 0x00, 0x00, 0x00 },
  192. [BAM_NUM_PIPES] = { 0x01008, 0x00, 0x00, 0x00 },
  193. [BAM_DESC_CNT_TRSHLD] = { 0x00008, 0x00, 0x00, 0x00 },
  194. [BAM_IRQ_SRCS] = { 0x03010, 0x00, 0x00, 0x00 },
  195. [BAM_IRQ_SRCS_MSK] = { 0x03014, 0x00, 0x00, 0x00 },
  196. [BAM_IRQ_SRCS_UNMASKED] = { 0x03018, 0x00, 0x00, 0x00 },
  197. [BAM_IRQ_STTS] = { 0x00014, 0x00, 0x00, 0x00 },
  198. [BAM_IRQ_CLR] = { 0x00018, 0x00, 0x00, 0x00 },
  199. [BAM_IRQ_EN] = { 0x0001C, 0x00, 0x00, 0x00 },
  200. [BAM_CNFG_BITS] = { 0x0007C, 0x00, 0x00, 0x00 },
  201. [BAM_IRQ_SRCS_EE] = { 0x03000, 0x00, 0x00, 0x1000 },
  202. [BAM_IRQ_SRCS_MSK_EE] = { 0x03004, 0x00, 0x00, 0x1000 },
  203. [BAM_P_CTRL] = { 0x13000, 0x1000, 0x00, 0x00 },
  204. [BAM_P_RST] = { 0x13004, 0x1000, 0x00, 0x00 },
  205. [BAM_P_HALT] = { 0x13008, 0x1000, 0x00, 0x00 },
  206. [BAM_P_IRQ_STTS] = { 0x13010, 0x1000, 0x00, 0x00 },
  207. [BAM_P_IRQ_CLR] = { 0x13014, 0x1000, 0x00, 0x00 },
  208. [BAM_P_IRQ_EN] = { 0x13018, 0x1000, 0x00, 0x00 },
  209. [BAM_P_EVNT_DEST_ADDR] = { 0x1382C, 0x00, 0x1000, 0x00 },
  210. [BAM_P_EVNT_REG] = { 0x13818, 0x00, 0x1000, 0x00 },
  211. [BAM_P_SW_OFSTS] = { 0x13800, 0x00, 0x1000, 0x00 },
  212. [BAM_P_DATA_FIFO_ADDR] = { 0x13824, 0x00, 0x1000, 0x00 },
  213. [BAM_P_DESC_FIFO_ADDR] = { 0x1381C, 0x00, 0x1000, 0x00 },
  214. [BAM_P_EVNT_GEN_TRSHLD] = { 0x13828, 0x00, 0x1000, 0x00 },
  215. [BAM_P_FIFO_SIZES] = { 0x13820, 0x00, 0x1000, 0x00 },
  216. };
  217. /* BAM CTRL */
  218. #define BAM_SW_RST BIT(0)
  219. #define BAM_EN BIT(1)
  220. #define BAM_EN_ACCUM BIT(4)
  221. #define BAM_TESTBUS_SEL_SHIFT 5
  222. #define BAM_TESTBUS_SEL_MASK 0x3F
  223. #define BAM_DESC_CACHE_SEL_SHIFT 13
  224. #define BAM_DESC_CACHE_SEL_MASK 0x3
  225. #define BAM_CACHED_DESC_STORE BIT(15)
  226. #define IBC_DISABLE BIT(16)
  227. /* BAM REVISION */
  228. #define REVISION_SHIFT 0
  229. #define REVISION_MASK 0xFF
  230. #define NUM_EES_SHIFT 8
  231. #define NUM_EES_MASK 0xF
  232. #define CE_BUFFER_SIZE BIT(13)
  233. #define AXI_ACTIVE BIT(14)
  234. #define USE_VMIDMT BIT(15)
  235. #define SECURED BIT(16)
  236. #define BAM_HAS_NO_BYPASS BIT(17)
  237. #define HIGH_FREQUENCY_BAM BIT(18)
  238. #define INACTIV_TMRS_EXST BIT(19)
  239. #define NUM_INACTIV_TMRS BIT(20)
  240. #define DESC_CACHE_DEPTH_SHIFT 21
  241. #define DESC_CACHE_DEPTH_1 (0 << DESC_CACHE_DEPTH_SHIFT)
  242. #define DESC_CACHE_DEPTH_2 (1 << DESC_CACHE_DEPTH_SHIFT)
  243. #define DESC_CACHE_DEPTH_3 (2 << DESC_CACHE_DEPTH_SHIFT)
  244. #define DESC_CACHE_DEPTH_4 (3 << DESC_CACHE_DEPTH_SHIFT)
  245. #define CMD_DESC_EN BIT(23)
  246. #define INACTIV_TMR_BASE_SHIFT 24
  247. #define INACTIV_TMR_BASE_MASK 0xFF
  248. /* BAM NUM PIPES */
  249. #define BAM_NUM_PIPES_SHIFT 0
  250. #define BAM_NUM_PIPES_MASK 0xFF
  251. #define PERIPH_NON_PIPE_GRP_SHIFT 16
  252. #define PERIPH_NON_PIP_GRP_MASK 0xFF
  253. #define BAM_NON_PIPE_GRP_SHIFT 24
  254. #define BAM_NON_PIPE_GRP_MASK 0xFF
  255. /* BAM CNFG BITS */
  256. #define BAM_PIPE_CNFG BIT(2)
  257. #define BAM_FULL_PIPE BIT(11)
  258. #define BAM_NO_EXT_P_RST BIT(12)
  259. #define BAM_IBC_DISABLE BIT(13)
  260. #define BAM_SB_CLK_REQ BIT(14)
  261. #define BAM_PSM_CSW_REQ BIT(15)
  262. #define BAM_PSM_P_RES BIT(16)
  263. #define BAM_AU_P_RES BIT(17)
  264. #define BAM_SI_P_RES BIT(18)
  265. #define BAM_WB_P_RES BIT(19)
  266. #define BAM_WB_BLK_CSW BIT(20)
  267. #define BAM_WB_CSW_ACK_IDL BIT(21)
  268. #define BAM_WB_RETR_SVPNT BIT(22)
  269. #define BAM_WB_DSC_AVL_P_RST BIT(23)
  270. #define BAM_REG_P_EN BIT(24)
  271. #define BAM_PSM_P_HD_DATA BIT(25)
  272. #define BAM_AU_ACCUMED BIT(26)
  273. #define BAM_CMD_ENABLE BIT(27)
  274. #define BAM_CNFG_BITS_DEFAULT (BAM_PIPE_CNFG | \
  275. BAM_NO_EXT_P_RST | \
  276. BAM_IBC_DISABLE | \
  277. BAM_SB_CLK_REQ | \
  278. BAM_PSM_CSW_REQ | \
  279. BAM_PSM_P_RES | \
  280. BAM_AU_P_RES | \
  281. BAM_SI_P_RES | \
  282. BAM_WB_P_RES | \
  283. BAM_WB_BLK_CSW | \
  284. BAM_WB_CSW_ACK_IDL | \
  285. BAM_WB_RETR_SVPNT | \
  286. BAM_WB_DSC_AVL_P_RST | \
  287. BAM_REG_P_EN | \
  288. BAM_PSM_P_HD_DATA | \
  289. BAM_AU_ACCUMED | \
  290. BAM_CMD_ENABLE)
  291. /* PIPE CTRL */
  292. #define P_EN BIT(1)
  293. #define P_DIRECTION BIT(3)
  294. #define P_SYS_STRM BIT(4)
  295. #define P_SYS_MODE BIT(5)
  296. #define P_AUTO_EOB BIT(6)
  297. #define P_AUTO_EOB_SEL_SHIFT 7
  298. #define P_AUTO_EOB_SEL_512 (0 << P_AUTO_EOB_SEL_SHIFT)
  299. #define P_AUTO_EOB_SEL_256 (1 << P_AUTO_EOB_SEL_SHIFT)
  300. #define P_AUTO_EOB_SEL_128 (2 << P_AUTO_EOB_SEL_SHIFT)
  301. #define P_AUTO_EOB_SEL_64 (3 << P_AUTO_EOB_SEL_SHIFT)
  302. #define P_PREFETCH_LIMIT_SHIFT 9
  303. #define P_PREFETCH_LIMIT_32 (0 << P_PREFETCH_LIMIT_SHIFT)
  304. #define P_PREFETCH_LIMIT_16 (1 << P_PREFETCH_LIMIT_SHIFT)
  305. #define P_PREFETCH_LIMIT_4 (2 << P_PREFETCH_LIMIT_SHIFT)
  306. #define P_WRITE_NWD BIT(11)
  307. #define P_LOCK_GROUP_SHIFT 16
  308. #define P_LOCK_GROUP_MASK 0x1F
  309. /* BAM_DESC_CNT_TRSHLD */
  310. #define CNT_TRSHLD 0xffff
  311. #define DEFAULT_CNT_THRSHLD 0x4
  312. /* BAM_IRQ_SRCS */
  313. #define BAM_IRQ BIT(31)
  314. #define P_IRQ 0x7fffffff
  315. /* BAM_IRQ_SRCS_MSK */
  316. #define BAM_IRQ_MSK BAM_IRQ
  317. #define P_IRQ_MSK P_IRQ
  318. /* BAM_IRQ_STTS */
  319. #define BAM_TIMER_IRQ BIT(4)
  320. #define BAM_EMPTY_IRQ BIT(3)
  321. #define BAM_ERROR_IRQ BIT(2)
  322. #define BAM_HRESP_ERR_IRQ BIT(1)
  323. /* BAM_IRQ_CLR */
  324. #define BAM_TIMER_CLR BIT(4)
  325. #define BAM_EMPTY_CLR BIT(3)
  326. #define BAM_ERROR_CLR BIT(2)
  327. #define BAM_HRESP_ERR_CLR BIT(1)
  328. /* BAM_IRQ_EN */
  329. #define BAM_TIMER_EN BIT(4)
  330. #define BAM_EMPTY_EN BIT(3)
  331. #define BAM_ERROR_EN BIT(2)
  332. #define BAM_HRESP_ERR_EN BIT(1)
  333. /* BAM_P_IRQ_EN */
  334. #define P_PRCSD_DESC_EN BIT(0)
  335. #define P_TIMER_EN BIT(1)
  336. #define P_WAKE_EN BIT(2)
  337. #define P_OUT_OF_DESC_EN BIT(3)
  338. #define P_ERR_EN BIT(4)
  339. #define P_TRNSFR_END_EN BIT(5)
  340. #define P_DEFAULT_IRQS_EN (P_PRCSD_DESC_EN | P_ERR_EN | P_TRNSFR_END_EN)
  341. /* BAM_P_SW_OFSTS */
  342. #define P_SW_OFSTS_MASK 0xffff
  343. #define MSM_SLIM_DESC_NUM 32
  344. #define MSM_SLIM_DESC_FIFO_SIZE (MSM_SLIM_DESC_NUM * 8)
  345. #define BAM_DESC_FIFO_SIZE (bdev->r_mem.is_r_mem ? (MSM_SLIM_DESC_FIFO_SIZE) : SZ_32K)
  346. #define MAX_DESCRIPTORS (BAM_DESC_FIFO_SIZE / sizeof(struct bam_desc_hw) - 1)
  347. #define BAM_FIFO_SIZE (SZ_32K - 8)
  348. #define IS_BUSY(chan) (CIRC_SPACE(bchan->tail, bchan->head,\
  349. MAX_DESCRIPTORS + 1) == 0)
  350. struct bam_chan {
  351. struct virt_dma_chan vc;
  352. struct bam_device *bdev;
  353. /* configuration from device tree */
  354. u32 id;
  355. /* runtime configuration */
  356. struct dma_slave_config slave;
  357. /* fifo storage */
  358. struct bam_desc_hw *fifo_virt;
  359. dma_addr_t fifo_phys;
  360. /* fifo markers */
  361. unsigned short head; /* start of active descriptor entries */
  362. unsigned short tail; /* end of active descriptor entries */
  363. unsigned int initialized; /* is the channel hw initialized? */
  364. unsigned int paused; /* is the channel paused? */
  365. unsigned int reconfigure; /* new slave config? */
  366. /* list of descriptors currently processed */
  367. struct list_head desc_list;
  368. struct list_head node;
  369. };
  370. static inline struct bam_chan *to_bam_chan(struct dma_chan *common)
  371. {
  372. return container_of(common, struct bam_chan, vc.chan);
  373. }
  374. /**
  375. * struct remote_mem - Stores remote memory information
  376. * @r_res: Memory resource structure parsed from devicetree
  377. * @r_vbase: Virtual base address of remote memory region
  378. * @r_vsbase: Saved virtual base address of remote memory region
  379. * @r_pbase: Physical base address of remote memory region
  380. * @is_r_mem: Indicates if remote memory is used or not
  381. *
  382. * Some BAM clients require the use of a specific memory region for the
  383. * pipe descriptor fifo. This structure is used to hold the remote
  384. * memory region information.
  385. */
  386. struct remote_mem {
  387. struct resource *r_res;
  388. void __iomem *r_vbase;
  389. void __iomem *r_vsbase;
  390. u32 r_pbase;
  391. bool is_r_mem;
  392. };
  393. struct bam_device {
  394. void __iomem *regs;
  395. struct device *dev;
  396. struct dma_device common;
  397. struct bam_chan *channels;
  398. u32 num_channels;
  399. u32 num_ees;
  400. /* execution environment ID, from DT */
  401. u32 ee;
  402. bool controlled_remotely;
  403. bool powered_remotely;
  404. u32 active_channels;
  405. const struct reg_offset_data *layout;
  406. struct clk *bamclk;
  407. int irq;
  408. /* dma start transaction tasklet */
  409. struct tasklet_struct task;
  410. struct remote_mem r_mem;
  411. void *ipc_log_dma;
  412. };
  413. /**
  414. * bam_addr - returns BAM register address
  415. * @bdev: bam device
  416. * @pipe: pipe instance (ignored when register doesn't have multiple instances)
  417. * @reg: register enum
  418. */
  419. static inline void __iomem *bam_addr(struct bam_device *bdev, u32 pipe,
  420. enum bam_reg reg)
  421. {
  422. const struct reg_offset_data r = bdev->layout[reg];
  423. return bdev->regs + r.base_offset +
  424. r.pipe_mult * pipe +
  425. r.evnt_mult * pipe +
  426. r.ee_mult * bdev->ee;
  427. }
  428. /**
  429. * bam_reset() - reset and initialize BAM registers
  430. * @bdev: bam device
  431. */
  432. static void bam_reset(struct bam_device *bdev)
  433. {
  434. u32 val;
  435. /* s/w reset bam */
  436. /* after reset all pipes are disabled and idle */
  437. val = readl_relaxed(bam_addr(bdev, 0, BAM_CTRL));
  438. val |= BAM_SW_RST;
  439. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  440. val &= ~BAM_SW_RST;
  441. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  442. /* make sure previous stores are visible before enabling BAM */
  443. wmb();
  444. /* enable bam */
  445. val |= BAM_EN;
  446. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  447. /* set descriptor threshhold, start with 4 bytes */
  448. writel_relaxed(DEFAULT_CNT_THRSHLD,
  449. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  450. /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
  451. writel_relaxed(BAM_CNFG_BITS_DEFAULT, bam_addr(bdev, 0, BAM_CNFG_BITS));
  452. /* enable irqs for errors */
  453. writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
  454. bam_addr(bdev, 0, BAM_IRQ_EN));
  455. /* unmask global bam interrupt */
  456. writel_relaxed(BAM_IRQ_MSK, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  457. }
  458. /**
  459. * bam_reset_channel - Reset individual BAM DMA channel
  460. * @bchan: bam channel
  461. *
  462. * This function resets a specific BAM channel
  463. */
  464. static void bam_reset_channel(struct bam_chan *bchan)
  465. {
  466. struct bam_device *bdev = bchan->bdev;
  467. lockdep_assert_held(&bchan->vc.lock);
  468. /* reset channel */
  469. writel_relaxed(1, bam_addr(bdev, bchan->id, BAM_P_RST));
  470. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_RST));
  471. /* don't allow cpu to reorder BAM register accesses done after this */
  472. wmb();
  473. /* make sure hw is initialized when channel is used the first time */
  474. bchan->initialized = 0;
  475. }
  476. /**
  477. * bam_chan_init_hw - Initialize channel hardware
  478. * @bchan: bam channel
  479. * @dir: DMA transfer direction
  480. *
  481. * This function resets and initializes the BAM channel
  482. */
  483. static void bam_chan_init_hw(struct bam_chan *bchan,
  484. enum dma_transfer_direction dir)
  485. {
  486. struct bam_device *bdev = bchan->bdev;
  487. u32 val;
  488. /* Reset the channel to clear internal state of the FIFO */
  489. bam_reset_channel(bchan);
  490. /*
  491. * write out 8 byte aligned address. We have enough space for this
  492. * because we allocated 1 more descriptor (8 bytes) than we can use
  493. */
  494. writel_relaxed(ALIGN(bchan->fifo_phys, sizeof(struct bam_desc_hw)),
  495. bam_addr(bdev, bchan->id, BAM_P_DESC_FIFO_ADDR));
  496. writel_relaxed(BAM_DESC_FIFO_SIZE,
  497. bam_addr(bdev, bchan->id, BAM_P_FIFO_SIZES));
  498. /* enable the per pipe interrupts, enable EOT, ERR, and INT irqs */
  499. writel_relaxed(P_DEFAULT_IRQS_EN,
  500. bam_addr(bdev, bchan->id, BAM_P_IRQ_EN));
  501. /* unmask the specific pipe and EE combo */
  502. val = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  503. val |= BIT(bchan->id);
  504. writel_relaxed(val, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  505. /* don't allow cpu to reorder the channel enable done below */
  506. wmb();
  507. /* set fixed direction and mode, then enable channel */
  508. val = P_EN | P_SYS_MODE;
  509. if (dir == DMA_DEV_TO_MEM)
  510. val |= P_DIRECTION;
  511. writel_relaxed(val, bam_addr(bdev, bchan->id, BAM_P_CTRL));
  512. bchan->initialized = 1;
  513. /* init FIFO pointers */
  514. bchan->head = 0;
  515. bchan->tail = 0;
  516. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  517. "%s: bam_desc_fifo:%d\n", __func__, BAM_DESC_FIFO_SIZE);
  518. }
  519. /**
  520. * bam_alloc_chan - Allocate channel resources for DMA channel.
  521. * @chan: specified channel
  522. *
  523. * This function allocates the FIFO descriptor memory
  524. */
  525. static int bam_alloc_chan(struct dma_chan *chan)
  526. {
  527. struct bam_chan *bchan = to_bam_chan(chan);
  528. struct bam_device *bdev = bchan->bdev;
  529. if (bchan->fifo_virt)
  530. return 0;
  531. if (bdev->r_mem.is_r_mem) {
  532. bchan->fifo_virt = bdev->r_mem.r_vbase;
  533. bchan->fifo_phys = bdev->r_mem.r_res->start;
  534. } else {
  535. /* allocate FIFO descriptor space, but only if necessary */
  536. bchan->fifo_virt = dma_alloc_wc(bdev->dev, BAM_DESC_FIFO_SIZE,
  537. &bchan->fifo_phys, GFP_KERNEL);
  538. }
  539. if (bdev->r_mem.is_r_mem) {
  540. memset_io(bchan->fifo_virt, 0x0, MSM_SLIM_DESC_NUM * 8);
  541. bdev->r_mem.r_vbase = bdev->r_mem.r_vbase + (MSM_SLIM_DESC_NUM * 8);
  542. bdev->r_mem.r_res->start = bdev->r_mem.r_res->start + (MSM_SLIM_DESC_NUM * 8);
  543. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  544. "dma_bam:%s: r_mem_virt_base:%x r_mem_start:%x\n",
  545. __func__, bdev->r_mem.r_vbase,
  546. bdev->r_mem.r_res->start);
  547. }
  548. if (!bchan->fifo_virt) {
  549. dev_err(bdev->dev, "Failed to allocate desc fifo\n");
  550. return -ENOMEM;
  551. }
  552. if (bdev->active_channels++ == 0 && bdev->powered_remotely)
  553. bam_reset(bdev);
  554. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  555. "%s chan id:%d\n", __func__, bchan->id);
  556. return 0;
  557. }
  558. /**
  559. * bam_free_chan - Frees dma resources associated with specific channel
  560. * @chan: specified channel
  561. *
  562. * Free the allocated fifo descriptor memory and channel resources
  563. *
  564. */
  565. static void bam_free_chan(struct dma_chan *chan)
  566. {
  567. struct bam_chan *bchan = to_bam_chan(chan);
  568. struct bam_device *bdev = bchan->bdev;
  569. u32 val;
  570. unsigned long flags;
  571. int ret;
  572. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  573. "%s chan id:%d\n", __func__, bchan->id);
  574. ret = pm_runtime_get_sync(bdev->dev);
  575. if (ret < 0)
  576. return;
  577. vchan_free_chan_resources(to_virt_chan(chan));
  578. if (!list_empty(&bchan->desc_list)) {
  579. dev_err(bchan->bdev->dev, "Cannot free busy channel\n");
  580. goto err;
  581. }
  582. spin_lock_irqsave(&bchan->vc.lock, flags);
  583. bam_reset_channel(bchan);
  584. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  585. if (!bdev->r_mem.is_r_mem) {
  586. dma_free_wc(bdev->dev, BAM_DESC_FIFO_SIZE, bchan->fifo_virt,
  587. bchan->fifo_phys);
  588. } else {
  589. bdev->r_mem.r_vbase = bdev->r_mem.r_vsbase;
  590. bdev->r_mem.r_res->start = bdev->r_mem.r_pbase;
  591. }
  592. bchan->fifo_virt = NULL;
  593. bchan->fifo_phys = 0;
  594. /* mask irq for pipe/channel */
  595. val = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  596. val &= ~BIT(bchan->id);
  597. writel_relaxed(val, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  598. /* disable irq */
  599. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_IRQ_EN));
  600. if (--bdev->active_channels == 0 && bdev->powered_remotely) {
  601. /* s/w reset bam */
  602. val = readl_relaxed(bam_addr(bdev, 0, BAM_CTRL));
  603. val |= BAM_SW_RST;
  604. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  605. }
  606. err:
  607. pm_runtime_mark_last_busy(bdev->dev);
  608. pm_runtime_put_autosuspend(bdev->dev);
  609. }
  610. /**
  611. * bam_slave_config - set slave configuration for channel
  612. * @chan: dma channel
  613. * @cfg: slave configuration
  614. *
  615. * Sets slave configuration for channel
  616. *
  617. */
  618. static int bam_slave_config(struct dma_chan *chan,
  619. struct dma_slave_config *cfg)
  620. {
  621. struct bam_chan *bchan = to_bam_chan(chan);
  622. unsigned long flag;
  623. DMA_BAM_DBG(bchan->bdev->ipc_log_dma, bchan->bdev->dev,
  624. "%s chan id:%d\n", __func__, bchan->id);
  625. spin_lock_irqsave(&bchan->vc.lock, flag);
  626. memcpy(&bchan->slave, cfg, sizeof(*cfg));
  627. bchan->reconfigure = 1;
  628. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  629. return 0;
  630. }
  631. /**
  632. * bam_prep_slave_sg - Prep slave sg transaction
  633. *
  634. * @chan: dma channel
  635. * @sgl: scatter gather list
  636. * @sg_len: length of sg
  637. * @direction: DMA transfer direction
  638. * @flags: DMA flags
  639. * @context: transfer context (unused)
  640. */
  641. static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,
  642. struct scatterlist *sgl, unsigned int sg_len,
  643. enum dma_transfer_direction direction, unsigned long flags,
  644. void *context)
  645. {
  646. struct bam_chan *bchan = to_bam_chan(chan);
  647. struct bam_device *bdev = bchan->bdev;
  648. struct bam_async_desc *async_desc;
  649. struct scatterlist *sg;
  650. u32 i;
  651. struct bam_desc_hw *desc;
  652. unsigned int num_alloc = 0;
  653. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  654. "%s DMA direction:%d\n", __func__, direction);
  655. if (!is_slave_direction(direction)) {
  656. dev_err(bdev->dev, "invalid dma direction\n");
  657. return NULL;
  658. }
  659. /* calculate number of required entries */
  660. for_each_sg(sgl, sg, sg_len, i)
  661. num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_FIFO_SIZE);
  662. /* allocate enough room to accomodate the number of entries */
  663. async_desc = kzalloc(struct_size(async_desc, desc, num_alloc),
  664. GFP_NOWAIT);
  665. if (!async_desc)
  666. return NULL;
  667. if (flags & DMA_PREP_FENCE)
  668. async_desc->flags |= DESC_FLAG_NWD;
  669. if (flags & DMA_PREP_INTERRUPT)
  670. async_desc->flags |= DESC_FLAG_EOT;
  671. async_desc->num_desc = num_alloc;
  672. async_desc->curr_desc = async_desc->desc;
  673. async_desc->dir = direction;
  674. /* fill in temporary descriptors */
  675. desc = async_desc->desc;
  676. for_each_sg(sgl, sg, sg_len, i) {
  677. unsigned int remainder = sg_dma_len(sg);
  678. unsigned int curr_offset = 0;
  679. do {
  680. if (flags & DMA_PREP_CMD)
  681. desc->flags |= cpu_to_le16(DESC_FLAG_CMD);
  682. desc->addr = cpu_to_le32(sg_dma_address(sg) +
  683. curr_offset);
  684. if (remainder > BAM_FIFO_SIZE) {
  685. desc->size = cpu_to_le16(BAM_FIFO_SIZE);
  686. remainder -= BAM_FIFO_SIZE;
  687. curr_offset += BAM_FIFO_SIZE;
  688. } else {
  689. desc->size = cpu_to_le16(remainder);
  690. remainder = 0;
  691. }
  692. async_desc->length += le16_to_cpu(desc->size);
  693. desc++;
  694. } while (remainder > 0);
  695. }
  696. return vchan_tx_prep(&bchan->vc, &async_desc->vd, flags);
  697. }
  698. /**
  699. * bam_dma_terminate_all - terminate all transactions on a channel
  700. * @chan: bam dma channel
  701. *
  702. * Dequeues and frees all transactions
  703. * No callbacks are done
  704. *
  705. */
  706. static int bam_dma_terminate_all(struct dma_chan *chan)
  707. {
  708. struct bam_chan *bchan = to_bam_chan(chan);
  709. struct bam_async_desc *async_desc, *tmp;
  710. unsigned long flag;
  711. LIST_HEAD(head);
  712. DMA_BAM_DBG(bchan->bdev->ipc_log_dma, bchan->bdev->dev,
  713. "%s chan id:%d\n", __func__, bchan->id);
  714. /* remove all transactions, including active transaction */
  715. spin_lock_irqsave(&bchan->vc.lock, flag);
  716. /*
  717. * If we have transactions queued, then some might be committed to the
  718. * hardware in the desc fifo. The only way to reset the desc fifo is
  719. * to do a hardware reset (either by pipe or the entire block).
  720. * bam_chan_init_hw() will trigger a pipe reset, and also reinit the
  721. * pipe. If the pipe is left disabled (default state after pipe reset)
  722. * and is accessed by a connected hardware engine, a fatal error in
  723. * the BAM will occur. There is a small window where this could happen
  724. * with bam_chan_init_hw(), but it is assumed that the caller has
  725. * stopped activity on any attached hardware engine. Make sure to do
  726. * this first so that the BAM hardware doesn't cause memory corruption
  727. * by accessing freed resources.
  728. */
  729. if (!list_empty(&bchan->desc_list)) {
  730. async_desc = list_first_entry(&bchan->desc_list,
  731. struct bam_async_desc, desc_node);
  732. bam_chan_init_hw(bchan, async_desc->dir);
  733. }
  734. list_for_each_entry_safe(async_desc, tmp,
  735. &bchan->desc_list, desc_node) {
  736. list_add(&async_desc->vd.node, &bchan->vc.desc_issued);
  737. list_del(&async_desc->desc_node);
  738. }
  739. vchan_get_all_descriptors(&bchan->vc, &head);
  740. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  741. vchan_dma_desc_free_list(&bchan->vc, &head);
  742. return 0;
  743. }
  744. /**
  745. * bam_pause - Pause DMA channel
  746. * @chan: dma channel
  747. *
  748. */
  749. static int bam_pause(struct dma_chan *chan)
  750. {
  751. struct bam_chan *bchan = to_bam_chan(chan);
  752. struct bam_device *bdev = bchan->bdev;
  753. unsigned long flag;
  754. int ret;
  755. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  756. "%s chan id:%d\n", __func__, bchan->id);
  757. ret = pm_runtime_get_sync(bdev->dev);
  758. if (ret < 0)
  759. return ret;
  760. spin_lock_irqsave(&bchan->vc.lock, flag);
  761. writel_relaxed(1, bam_addr(bdev, bchan->id, BAM_P_HALT));
  762. bchan->paused = 1;
  763. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  764. pm_runtime_mark_last_busy(bdev->dev);
  765. pm_runtime_put_autosuspend(bdev->dev);
  766. return 0;
  767. }
  768. /**
  769. * bam_resume - Resume DMA channel operations
  770. * @chan: dma channel
  771. *
  772. */
  773. static int bam_resume(struct dma_chan *chan)
  774. {
  775. struct bam_chan *bchan = to_bam_chan(chan);
  776. struct bam_device *bdev = bchan->bdev;
  777. unsigned long flag;
  778. int ret;
  779. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  780. "%s chan id:%d\n", __func__, bchan->id);
  781. ret = pm_runtime_get_sync(bdev->dev);
  782. if (ret < 0)
  783. return ret;
  784. spin_lock_irqsave(&bchan->vc.lock, flag);
  785. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_HALT));
  786. bchan->paused = 0;
  787. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  788. pm_runtime_mark_last_busy(bdev->dev);
  789. pm_runtime_put_autosuspend(bdev->dev);
  790. return 0;
  791. }
  792. /**
  793. * process_channel_irqs - processes the channel interrupts
  794. * @bdev: bam controller
  795. *
  796. * This function processes the channel interrupts
  797. *
  798. */
  799. static u32 process_channel_irqs(struct bam_device *bdev)
  800. {
  801. u32 i, srcs, pipe_stts, offset, avail;
  802. unsigned long flags;
  803. struct bam_async_desc *async_desc, *tmp;
  804. srcs = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_EE));
  805. /* return early if no pipe/channel interrupts are present */
  806. if (!(srcs & P_IRQ))
  807. return srcs;
  808. for (i = 0; i < bdev->num_channels; i++) {
  809. struct bam_chan *bchan = &bdev->channels[i];
  810. if (!(srcs & BIT(i)))
  811. continue;
  812. /* clear pipe irq */
  813. pipe_stts = readl_relaxed(bam_addr(bdev, i, BAM_P_IRQ_STTS));
  814. writel_relaxed(pipe_stts, bam_addr(bdev, i, BAM_P_IRQ_CLR));
  815. spin_lock_irqsave(&bchan->vc.lock, flags);
  816. offset = readl_relaxed(bam_addr(bdev, i, BAM_P_SW_OFSTS)) &
  817. P_SW_OFSTS_MASK;
  818. offset /= sizeof(struct bam_desc_hw);
  819. /* Number of bytes available to read */
  820. avail = CIRC_CNT(offset, bchan->head, MAX_DESCRIPTORS + 1);
  821. if (offset < bchan->head)
  822. avail--;
  823. list_for_each_entry_safe(async_desc, tmp,
  824. &bchan->desc_list, desc_node) {
  825. /* Not enough data to read */
  826. if (avail < async_desc->xfer_len)
  827. break;
  828. /* manage FIFO */
  829. bchan->head += async_desc->xfer_len;
  830. bchan->head %= MAX_DESCRIPTORS;
  831. async_desc->num_desc -= async_desc->xfer_len;
  832. async_desc->curr_desc += async_desc->xfer_len;
  833. avail -= async_desc->xfer_len;
  834. /*
  835. * if complete, process cookie. Otherwise
  836. * push back to front of desc_issued so that
  837. * it gets restarted by the tasklet
  838. */
  839. if (!async_desc->num_desc) {
  840. vchan_cookie_complete(&async_desc->vd);
  841. } else {
  842. list_add(&async_desc->vd.node,
  843. &bchan->vc.desc_issued);
  844. }
  845. list_del(&async_desc->desc_node);
  846. }
  847. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  848. }
  849. return srcs;
  850. }
  851. /**
  852. * bam_dma_irq - irq handler for bam controller
  853. * @irq: IRQ of interrupt
  854. * @data: callback data
  855. *
  856. * IRQ handler for the bam controller
  857. */
  858. static irqreturn_t bam_dma_irq(int irq, void *data)
  859. {
  860. struct bam_device *bdev = data;
  861. u32 clr_mask = 0, srcs = 0;
  862. int ret;
  863. srcs |= process_channel_irqs(bdev);
  864. /* kick off tasklet to start next dma transfer */
  865. if (srcs & P_IRQ)
  866. tasklet_schedule(&bdev->task);
  867. ret = pm_runtime_get_sync(bdev->dev);
  868. if (ret < 0)
  869. return IRQ_NONE;
  870. if (srcs & BAM_IRQ) {
  871. clr_mask = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_STTS));
  872. /*
  873. * don't allow reorder of the various accesses to the BAM
  874. * registers
  875. */
  876. mb();
  877. writel_relaxed(clr_mask, bam_addr(bdev, 0, BAM_IRQ_CLR));
  878. }
  879. pm_runtime_mark_last_busy(bdev->dev);
  880. pm_runtime_put_autosuspend(bdev->dev);
  881. return IRQ_HANDLED;
  882. }
  883. /**
  884. * bam_tx_status - returns status of transaction
  885. * @chan: dma channel
  886. * @cookie: transaction cookie
  887. * @txstate: DMA transaction state
  888. *
  889. * Return status of dma transaction
  890. */
  891. static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  892. struct dma_tx_state *txstate)
  893. {
  894. struct bam_chan *bchan = to_bam_chan(chan);
  895. struct bam_async_desc *async_desc;
  896. struct virt_dma_desc *vd;
  897. int ret;
  898. size_t residue = 0;
  899. unsigned int i;
  900. unsigned long flags;
  901. DMA_BAM_DBG(bchan->bdev->ipc_log_dma, bchan->bdev->dev,
  902. "%s chan id:%d\n", __func__, bchan->id);
  903. ret = dma_cookie_status(chan, cookie, txstate);
  904. if (ret == DMA_COMPLETE)
  905. return ret;
  906. if (!txstate)
  907. return bchan->paused ? DMA_PAUSED : ret;
  908. spin_lock_irqsave(&bchan->vc.lock, flags);
  909. vd = vchan_find_desc(&bchan->vc, cookie);
  910. if (vd) {
  911. residue = container_of(vd, struct bam_async_desc, vd)->length;
  912. } else {
  913. list_for_each_entry(async_desc, &bchan->desc_list, desc_node) {
  914. if (async_desc->vd.tx.cookie != cookie)
  915. continue;
  916. for (i = 0; i < async_desc->num_desc; i++)
  917. residue += le16_to_cpu(
  918. async_desc->curr_desc[i].size);
  919. }
  920. }
  921. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  922. dma_set_residue(txstate, residue);
  923. if (ret == DMA_IN_PROGRESS && bchan->paused)
  924. ret = DMA_PAUSED;
  925. return ret;
  926. }
  927. /**
  928. * bam_apply_new_config
  929. * @bchan: bam dma channel
  930. * @dir: DMA direction
  931. */
  932. static void bam_apply_new_config(struct bam_chan *bchan,
  933. enum dma_transfer_direction dir)
  934. {
  935. struct bam_device *bdev = bchan->bdev;
  936. u32 maxburst;
  937. if (!bdev->controlled_remotely) {
  938. if (dir == DMA_DEV_TO_MEM)
  939. maxburst = bchan->slave.src_maxburst;
  940. else
  941. maxburst = bchan->slave.dst_maxburst;
  942. writel_relaxed(maxburst,
  943. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  944. }
  945. bchan->reconfigure = 0;
  946. }
  947. /**
  948. * bam_start_dma - start next transaction
  949. * @bchan: bam dma channel
  950. */
  951. static void bam_start_dma(struct bam_chan *bchan)
  952. {
  953. struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
  954. struct bam_device *bdev = bchan->bdev;
  955. struct bam_async_desc *async_desc = NULL;
  956. struct bam_desc_hw *desc;
  957. struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
  958. sizeof(struct bam_desc_hw));
  959. int ret;
  960. unsigned int avail;
  961. struct dmaengine_desc_callback cb;
  962. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  963. "%s chan id:%d\n", __func__, bchan->id);
  964. lockdep_assert_held(&bchan->vc.lock);
  965. if (!vd)
  966. return;
  967. ret = pm_runtime_get_sync(bdev->dev);
  968. if (ret < 0)
  969. return;
  970. while (vd && !IS_BUSY(bchan)) {
  971. list_del(&vd->node);
  972. async_desc = container_of(vd, struct bam_async_desc, vd);
  973. /* on first use, initialize the channel hardware */
  974. if (!bchan->initialized)
  975. bam_chan_init_hw(bchan, async_desc->dir);
  976. /* apply new slave config changes, if necessary */
  977. if (bchan->reconfigure)
  978. bam_apply_new_config(bchan, async_desc->dir);
  979. desc = async_desc->curr_desc;
  980. avail = CIRC_SPACE(bchan->tail, bchan->head,
  981. MAX_DESCRIPTORS + 1);
  982. if (async_desc->num_desc > avail)
  983. async_desc->xfer_len = avail;
  984. else
  985. async_desc->xfer_len = async_desc->num_desc;
  986. /* set any special flags on the last descriptor */
  987. if (async_desc->num_desc == async_desc->xfer_len)
  988. desc[async_desc->xfer_len - 1].flags |=
  989. cpu_to_le16(async_desc->flags);
  990. vd = vchan_next_desc(&bchan->vc);
  991. dmaengine_desc_get_callback(&async_desc->vd.tx, &cb);
  992. /*
  993. * An interrupt is generated at this desc, if
  994. * - FIFO is FULL.
  995. * - No more descriptors to add.
  996. * - If a callback completion was requested for this DESC,
  997. * In this case, BAM will deliver the completion callback
  998. * for this desc and continue processing the next desc.
  999. */
  1000. if (((avail <= async_desc->xfer_len) || !vd ||
  1001. dmaengine_desc_callback_valid(&cb)) &&
  1002. !(async_desc->flags & DESC_FLAG_EOT))
  1003. desc[async_desc->xfer_len - 1].flags |=
  1004. cpu_to_le16(DESC_FLAG_INT);
  1005. if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
  1006. u32 partial = MAX_DESCRIPTORS - bchan->tail;
  1007. memcpy(&fifo[bchan->tail], desc,
  1008. partial * sizeof(struct bam_desc_hw));
  1009. memcpy(fifo, &desc[partial],
  1010. (async_desc->xfer_len - partial) *
  1011. sizeof(struct bam_desc_hw));
  1012. } else {
  1013. memcpy(&fifo[bchan->tail], desc,
  1014. async_desc->xfer_len *
  1015. sizeof(struct bam_desc_hw));
  1016. }
  1017. bchan->tail += async_desc->xfer_len;
  1018. bchan->tail %= MAX_DESCRIPTORS;
  1019. list_add_tail(&async_desc->desc_node, &bchan->desc_list);
  1020. }
  1021. /* ensure descriptor writes and dma start not reordered */
  1022. wmb();
  1023. writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
  1024. bam_addr(bdev, bchan->id, BAM_P_EVNT_REG));
  1025. pm_runtime_mark_last_busy(bdev->dev);
  1026. pm_runtime_put_autosuspend(bdev->dev);
  1027. }
  1028. /**
  1029. * dma_tasklet - DMA IRQ tasklet
  1030. * @t: tasklet argument (bam controller structure)
  1031. *
  1032. * Sets up next DMA operation and then processes all completed transactions
  1033. */
  1034. static void dma_tasklet(struct tasklet_struct *t)
  1035. {
  1036. struct bam_device *bdev = from_tasklet(bdev, t, task);
  1037. struct bam_chan *bchan;
  1038. unsigned long flags;
  1039. unsigned int i;
  1040. /* go through the channels and kick off transactions */
  1041. for (i = 0; i < bdev->num_channels; i++) {
  1042. bchan = &bdev->channels[i];
  1043. spin_lock_irqsave(&bchan->vc.lock, flags);
  1044. if (!list_empty(&bchan->vc.desc_issued) && !IS_BUSY(bchan))
  1045. bam_start_dma(bchan);
  1046. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  1047. }
  1048. }
  1049. /**
  1050. * bam_issue_pending - starts pending transactions
  1051. * @chan: dma channel
  1052. *
  1053. * Calls tasklet directly which in turn starts any pending transactions
  1054. */
  1055. static void bam_issue_pending(struct dma_chan *chan)
  1056. {
  1057. struct bam_chan *bchan = to_bam_chan(chan);
  1058. struct bam_device *bdev = bchan->bdev;
  1059. unsigned long flags;
  1060. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  1061. "%s chan id:%d\n", __func__, bchan->id);
  1062. spin_lock_irqsave(&bchan->vc.lock, flags);
  1063. /* if work pending and idle, start a transaction */
  1064. if (vchan_issue_pending(&bchan->vc) && !IS_BUSY(bchan))
  1065. bam_start_dma(bchan);
  1066. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  1067. }
  1068. /**
  1069. * bam_dma_free_desc - free descriptor memory
  1070. * @vd: virtual descriptor
  1071. *
  1072. */
  1073. static void bam_dma_free_desc(struct virt_dma_desc *vd)
  1074. {
  1075. struct bam_async_desc *async_desc = container_of(vd,
  1076. struct bam_async_desc, vd);
  1077. kfree(async_desc);
  1078. }
  1079. static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
  1080. struct of_dma *of)
  1081. {
  1082. struct bam_device *bdev = container_of(of->of_dma_data,
  1083. struct bam_device, common);
  1084. unsigned int request;
  1085. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  1086. "%s No of channels:%d\n", __func__, bdev->num_channels);
  1087. if (dma_spec->args_count != 1)
  1088. return NULL;
  1089. request = dma_spec->args[0];
  1090. if (request >= bdev->num_channels)
  1091. return NULL;
  1092. return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
  1093. }
  1094. /**
  1095. * bam_init
  1096. * @bdev: bam device
  1097. *
  1098. * Initialization helper for global bam registers
  1099. */
  1100. static int bam_init(struct bam_device *bdev)
  1101. {
  1102. u32 val;
  1103. /* read revision and configuration information */
  1104. if (!bdev->num_ees) {
  1105. val = readl_relaxed(bam_addr(bdev, 0, BAM_REVISION));
  1106. bdev->num_ees = (val >> NUM_EES_SHIFT) & NUM_EES_MASK;
  1107. }
  1108. /* check that configured EE is within range */
  1109. if (bdev->ee >= bdev->num_ees)
  1110. return -EINVAL;
  1111. if (!bdev->num_channels) {
  1112. val = readl_relaxed(bam_addr(bdev, 0, BAM_NUM_PIPES));
  1113. bdev->num_channels = val & BAM_NUM_PIPES_MASK;
  1114. }
  1115. /* Reset BAM now if fully controlled locally */
  1116. if (!bdev->controlled_remotely && !bdev->powered_remotely)
  1117. bam_reset(bdev);
  1118. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  1119. "%s ret:%d\n", __func__, 0);
  1120. return 0;
  1121. }
  1122. static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
  1123. u32 index)
  1124. {
  1125. bchan->id = index;
  1126. bchan->bdev = bdev;
  1127. vchan_init(&bchan->vc, &bdev->common);
  1128. bchan->vc.desc_free = bam_dma_free_desc;
  1129. INIT_LIST_HEAD(&bchan->desc_list);
  1130. }
  1131. static const struct of_device_id bam_of_match[] = {
  1132. { .compatible = "qcom,bam-v1.3.0", .data = &bam_v1_3_reg_info },
  1133. { .compatible = "qcom,bam-v1.4.0", .data = &bam_v1_4_reg_info },
  1134. { .compatible = "qcom,bam-v1.7.0", .data = &bam_v1_7_reg_info },
  1135. {}
  1136. };
  1137. MODULE_DEVICE_TABLE(of, bam_of_match);
  1138. static int bam_dma_probe(struct platform_device *pdev)
  1139. {
  1140. struct bam_device *bdev;
  1141. const struct of_device_id *match;
  1142. struct resource *iores;
  1143. struct resource *remote_res;
  1144. int ret, i;
  1145. bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
  1146. if (!bdev)
  1147. return -ENOMEM;
  1148. bdev->dev = &pdev->dev;
  1149. match = of_match_node(bam_of_match, pdev->dev.of_node);
  1150. if (!match) {
  1151. dev_err(&pdev->dev, "Unsupported BAM module\n");
  1152. return -ENODEV;
  1153. }
  1154. bdev->layout = match->data;
  1155. bdev->ipc_log_dma = ipc_log_context_create(DMA_IPC_LOGPAGES,
  1156. "dma_bam_log", 0);
  1157. if (!bdev->ipc_log_dma)
  1158. dev_err(bdev->dev, "Failed to create dma bam log\n");
  1159. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  1160. "%s start %d\n", __func__, true);
  1161. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1162. bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
  1163. if (IS_ERR(bdev->regs))
  1164. return PTR_ERR(bdev->regs);
  1165. bdev->r_mem.is_r_mem = false;
  1166. remote_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1167. "bam_remote_mem");
  1168. if (remote_res) {
  1169. bdev->r_mem.is_r_mem = true;
  1170. bdev->r_mem.r_pbase = (unsigned long long)remote_res->start;
  1171. bdev->r_mem.r_vbase = devm_ioremap(&pdev->dev,
  1172. remote_res->start, resource_size(remote_res));
  1173. if (!bdev->r_mem.r_vbase) {
  1174. dev_err(&pdev->dev, "Remote mem ioremap failed\n");
  1175. return -ENOMEM;
  1176. }
  1177. bdev->r_mem.r_vsbase = bdev->r_mem.r_vbase;
  1178. bdev->r_mem.r_res = remote_res;
  1179. }
  1180. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1181. if (ret) {
  1182. dev_err(&pdev->dev, "Could not set 32 bit mask\n");
  1183. return -ENODEV;
  1184. }
  1185. bdev->irq = platform_get_irq(pdev, 0);
  1186. if (bdev->irq < 0)
  1187. return bdev->irq;
  1188. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
  1189. if (ret) {
  1190. dev_err(bdev->dev, "Execution environment unspecified\n");
  1191. return ret;
  1192. }
  1193. bdev->controlled_remotely = of_property_read_bool(pdev->dev.of_node,
  1194. "qcom,controlled-remotely");
  1195. bdev->powered_remotely = of_property_read_bool(pdev->dev.of_node,
  1196. "qcom,powered-remotely");
  1197. if (bdev->controlled_remotely || bdev->powered_remotely) {
  1198. ret = of_property_read_u32(pdev->dev.of_node, "num-channels",
  1199. &bdev->num_channels);
  1200. if (ret)
  1201. dev_err(bdev->dev, "num-channels unspecified in dt\n");
  1202. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-ees",
  1203. &bdev->num_ees);
  1204. if (ret)
  1205. dev_err(bdev->dev, "num-ees unspecified in dt\n");
  1206. }
  1207. if (bdev->controlled_remotely || bdev->powered_remotely)
  1208. bdev->bamclk = devm_clk_get_optional(bdev->dev, "bam_clk");
  1209. else
  1210. bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
  1211. if (IS_ERR(bdev->bamclk))
  1212. return PTR_ERR(bdev->bamclk);
  1213. ret = clk_prepare_enable(bdev->bamclk);
  1214. if (ret) {
  1215. dev_err(bdev->dev, "failed to prepare/enable clock\n");
  1216. return ret;
  1217. }
  1218. ret = bam_init(bdev);
  1219. if (ret)
  1220. goto err_disable_clk;
  1221. tasklet_setup(&bdev->task, dma_tasklet);
  1222. bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
  1223. sizeof(*bdev->channels), GFP_KERNEL);
  1224. if (!bdev->channels) {
  1225. ret = -ENOMEM;
  1226. goto err_tasklet_kill;
  1227. }
  1228. /* allocate and initialize channels */
  1229. INIT_LIST_HEAD(&bdev->common.channels);
  1230. for (i = 0; i < bdev->num_channels; i++)
  1231. bam_channel_init(bdev, &bdev->channels[i], i);
  1232. ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
  1233. IRQF_TRIGGER_HIGH, "bam_dma", bdev);
  1234. if (ret)
  1235. goto err_bam_channel_exit;
  1236. /* set max dma segment size */
  1237. bdev->common.dev = bdev->dev;
  1238. ret = dma_set_max_seg_size(bdev->common.dev, BAM_FIFO_SIZE);
  1239. if (ret) {
  1240. dev_err(bdev->dev, "cannot set maximum segment size\n");
  1241. goto err_bam_channel_exit;
  1242. }
  1243. platform_set_drvdata(pdev, bdev);
  1244. /* set capabilities */
  1245. dma_cap_zero(bdev->common.cap_mask);
  1246. dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
  1247. /* initialize dmaengine apis */
  1248. bdev->common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1249. bdev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1250. bdev->common.src_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1251. bdev->common.dst_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1252. bdev->common.device_alloc_chan_resources = bam_alloc_chan;
  1253. bdev->common.device_free_chan_resources = bam_free_chan;
  1254. bdev->common.device_prep_slave_sg = bam_prep_slave_sg;
  1255. bdev->common.device_config = bam_slave_config;
  1256. bdev->common.device_pause = bam_pause;
  1257. bdev->common.device_resume = bam_resume;
  1258. bdev->common.device_terminate_all = bam_dma_terminate_all;
  1259. bdev->common.device_issue_pending = bam_issue_pending;
  1260. bdev->common.device_tx_status = bam_tx_status;
  1261. bdev->common.dev = bdev->dev;
  1262. ret = dma_async_device_register(&bdev->common);
  1263. if (ret) {
  1264. dev_err(bdev->dev, "failed to register dma async device\n");
  1265. goto err_bam_channel_exit;
  1266. }
  1267. ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
  1268. &bdev->common);
  1269. if (ret)
  1270. goto err_unregister_dma;
  1271. pm_runtime_irq_safe(&pdev->dev);
  1272. pm_runtime_set_autosuspend_delay(&pdev->dev, BAM_DMA_AUTOSUSPEND_DELAY);
  1273. pm_runtime_use_autosuspend(&pdev->dev);
  1274. pm_runtime_mark_last_busy(&pdev->dev);
  1275. pm_runtime_set_active(&pdev->dev);
  1276. pm_runtime_enable(&pdev->dev);
  1277. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev,
  1278. "%s end ret:%d\n", __func__, 0);
  1279. return 0;
  1280. err_unregister_dma:
  1281. dma_async_device_unregister(&bdev->common);
  1282. err_bam_channel_exit:
  1283. for (i = 0; i < bdev->num_channels; i++)
  1284. tasklet_kill(&bdev->channels[i].vc.task);
  1285. err_tasklet_kill:
  1286. tasklet_kill(&bdev->task);
  1287. err_disable_clk:
  1288. clk_disable_unprepare(bdev->bamclk);
  1289. return ret;
  1290. }
  1291. static int bam_dma_remove(struct platform_device *pdev)
  1292. {
  1293. struct bam_device *bdev = platform_get_drvdata(pdev);
  1294. u32 i;
  1295. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev, "%s ret:%d\n", __func__, 0);
  1296. if (bdev->ipc_log_dma)
  1297. ipc_log_context_destroy(bdev->ipc_log_dma);
  1298. pm_runtime_force_suspend(&pdev->dev);
  1299. of_dma_controller_free(pdev->dev.of_node);
  1300. dma_async_device_unregister(&bdev->common);
  1301. /* mask all interrupts for this execution environment */
  1302. writel_relaxed(0, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  1303. devm_free_irq(bdev->dev, bdev->irq, bdev);
  1304. for (i = 0; i < bdev->num_channels; i++) {
  1305. bam_dma_terminate_all(&bdev->channels[i].vc.chan);
  1306. tasklet_kill(&bdev->channels[i].vc.task);
  1307. if (!bdev->channels[i].fifo_virt)
  1308. continue;
  1309. dma_free_wc(bdev->dev, BAM_DESC_FIFO_SIZE,
  1310. bdev->channels[i].fifo_virt,
  1311. bdev->channels[i].fifo_phys);
  1312. }
  1313. tasklet_kill(&bdev->task);
  1314. clk_disable_unprepare(bdev->bamclk);
  1315. return 0;
  1316. }
  1317. static int __maybe_unused bam_dma_runtime_suspend(struct device *dev)
  1318. {
  1319. struct bam_device *bdev = dev_get_drvdata(dev);
  1320. clk_disable(bdev->bamclk);
  1321. return 0;
  1322. }
  1323. static int __maybe_unused bam_dma_runtime_resume(struct device *dev)
  1324. {
  1325. struct bam_device *bdev = dev_get_drvdata(dev);
  1326. int ret;
  1327. ret = clk_enable(bdev->bamclk);
  1328. if (ret < 0) {
  1329. dev_err(dev, "clk_enable failed: %d\n", ret);
  1330. return ret;
  1331. }
  1332. return 0;
  1333. }
  1334. static int __maybe_unused bam_dma_suspend(struct device *dev)
  1335. {
  1336. struct bam_device *bdev = dev_get_drvdata(dev);
  1337. pm_runtime_force_suspend(dev);
  1338. clk_unprepare(bdev->bamclk);
  1339. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev, "%s ret:%d\n", __func__, 0);
  1340. return 0;
  1341. }
  1342. static int __maybe_unused bam_dma_resume(struct device *dev)
  1343. {
  1344. struct bam_device *bdev = dev_get_drvdata(dev);
  1345. int ret;
  1346. ret = clk_prepare(bdev->bamclk);
  1347. if (ret)
  1348. return ret;
  1349. pm_runtime_force_resume(dev);
  1350. DMA_BAM_DBG(bdev->ipc_log_dma, bdev->dev, "%s ret:%d\n", __func__, 0);
  1351. return 0;
  1352. }
  1353. static const struct dev_pm_ops bam_dma_pm_ops = {
  1354. SET_LATE_SYSTEM_SLEEP_PM_OPS(bam_dma_suspend, bam_dma_resume)
  1355. SET_RUNTIME_PM_OPS(bam_dma_runtime_suspend, bam_dma_runtime_resume,
  1356. NULL)
  1357. };
  1358. static struct platform_driver bam_dma_driver = {
  1359. .probe = bam_dma_probe,
  1360. .remove = bam_dma_remove,
  1361. .driver = {
  1362. .name = "bam-dma-engine",
  1363. .pm = &bam_dma_pm_ops,
  1364. .of_match_table = bam_of_match,
  1365. },
  1366. };
  1367. module_platform_driver(bam_dma_driver);
  1368. MODULE_AUTHOR("Andy Gross <[email protected]>");
  1369. MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
  1370. MODULE_LICENSE("GPL v2");