bam_dma.c 37 KB

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