bam_dma.c 40 KB

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