bam_dma.c 39 KB

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