bam_dma.c 38 KB

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