bam_dma.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. /* 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. if (flags & DMA_PREP_CMD)
  560. desc->flags |= cpu_to_le16(DESC_FLAG_CMD);
  561. desc->addr = cpu_to_le32(sg_dma_address(sg) +
  562. curr_offset);
  563. if (remainder > BAM_FIFO_SIZE) {
  564. desc->size = cpu_to_le16(BAM_FIFO_SIZE);
  565. remainder -= BAM_FIFO_SIZE;
  566. curr_offset += BAM_FIFO_SIZE;
  567. } else {
  568. desc->size = cpu_to_le16(remainder);
  569. remainder = 0;
  570. }
  571. async_desc->length += desc->size;
  572. desc++;
  573. } while (remainder > 0);
  574. }
  575. return vchan_tx_prep(&bchan->vc, &async_desc->vd, flags);
  576. err_out:
  577. kfree(async_desc);
  578. return NULL;
  579. }
  580. /**
  581. * bam_dma_terminate_all - terminate all transactions on a channel
  582. * @bchan: bam dma channel
  583. *
  584. * Dequeues and frees all transactions
  585. * No callbacks are done
  586. *
  587. */
  588. static int bam_dma_terminate_all(struct dma_chan *chan)
  589. {
  590. struct bam_chan *bchan = to_bam_chan(chan);
  591. unsigned long flag;
  592. LIST_HEAD(head);
  593. /* remove all transactions, including active transaction */
  594. spin_lock_irqsave(&bchan->vc.lock, flag);
  595. if (bchan->curr_txd) {
  596. list_add(&bchan->curr_txd->vd.node, &bchan->vc.desc_issued);
  597. bchan->curr_txd = NULL;
  598. }
  599. vchan_get_all_descriptors(&bchan->vc, &head);
  600. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  601. vchan_dma_desc_free_list(&bchan->vc, &head);
  602. return 0;
  603. }
  604. /**
  605. * bam_pause - Pause DMA channel
  606. * @chan: dma channel
  607. *
  608. */
  609. static int bam_pause(struct dma_chan *chan)
  610. {
  611. struct bam_chan *bchan = to_bam_chan(chan);
  612. struct bam_device *bdev = bchan->bdev;
  613. unsigned long flag;
  614. int ret;
  615. ret = pm_runtime_get_sync(bdev->dev);
  616. if (ret < 0)
  617. return ret;
  618. spin_lock_irqsave(&bchan->vc.lock, flag);
  619. writel_relaxed(1, bam_addr(bdev, bchan->id, BAM_P_HALT));
  620. bchan->paused = 1;
  621. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  622. pm_runtime_mark_last_busy(bdev->dev);
  623. pm_runtime_put_autosuspend(bdev->dev);
  624. return 0;
  625. }
  626. /**
  627. * bam_resume - Resume DMA channel operations
  628. * @chan: dma channel
  629. *
  630. */
  631. static int bam_resume(struct dma_chan *chan)
  632. {
  633. struct bam_chan *bchan = to_bam_chan(chan);
  634. struct bam_device *bdev = bchan->bdev;
  635. unsigned long flag;
  636. int ret;
  637. ret = pm_runtime_get_sync(bdev->dev);
  638. if (ret < 0)
  639. return ret;
  640. spin_lock_irqsave(&bchan->vc.lock, flag);
  641. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_HALT));
  642. bchan->paused = 0;
  643. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  644. pm_runtime_mark_last_busy(bdev->dev);
  645. pm_runtime_put_autosuspend(bdev->dev);
  646. return 0;
  647. }
  648. /**
  649. * process_channel_irqs - processes the channel interrupts
  650. * @bdev: bam controller
  651. *
  652. * This function processes the channel interrupts
  653. *
  654. */
  655. static u32 process_channel_irqs(struct bam_device *bdev)
  656. {
  657. u32 i, srcs, pipe_stts;
  658. unsigned long flags;
  659. struct bam_async_desc *async_desc;
  660. srcs = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_EE));
  661. /* return early if no pipe/channel interrupts are present */
  662. if (!(srcs & P_IRQ))
  663. return srcs;
  664. for (i = 0; i < bdev->num_channels; i++) {
  665. struct bam_chan *bchan = &bdev->channels[i];
  666. if (!(srcs & BIT(i)))
  667. continue;
  668. /* clear pipe irq */
  669. pipe_stts = readl_relaxed(bam_addr(bdev, i, BAM_P_IRQ_STTS));
  670. writel_relaxed(pipe_stts, bam_addr(bdev, i, BAM_P_IRQ_CLR));
  671. spin_lock_irqsave(&bchan->vc.lock, flags);
  672. async_desc = bchan->curr_txd;
  673. if (async_desc) {
  674. async_desc->num_desc -= async_desc->xfer_len;
  675. async_desc->curr_desc += async_desc->xfer_len;
  676. bchan->curr_txd = NULL;
  677. /* manage FIFO */
  678. bchan->head += async_desc->xfer_len;
  679. bchan->head %= MAX_DESCRIPTORS;
  680. /*
  681. * if complete, process cookie. Otherwise
  682. * push back to front of desc_issued so that
  683. * it gets restarted by the tasklet
  684. */
  685. if (!async_desc->num_desc)
  686. vchan_cookie_complete(&async_desc->vd);
  687. else
  688. list_add(&async_desc->vd.node,
  689. &bchan->vc.desc_issued);
  690. }
  691. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  692. }
  693. return srcs;
  694. }
  695. /**
  696. * bam_dma_irq - irq handler for bam controller
  697. * @irq: IRQ of interrupt
  698. * @data: callback data
  699. *
  700. * IRQ handler for the bam controller
  701. */
  702. static irqreturn_t bam_dma_irq(int irq, void *data)
  703. {
  704. struct bam_device *bdev = data;
  705. u32 clr_mask = 0, srcs = 0;
  706. int ret;
  707. srcs |= process_channel_irqs(bdev);
  708. /* kick off tasklet to start next dma transfer */
  709. if (srcs & P_IRQ)
  710. tasklet_schedule(&bdev->task);
  711. ret = pm_runtime_get_sync(bdev->dev);
  712. if (ret < 0)
  713. return ret;
  714. if (srcs & BAM_IRQ) {
  715. clr_mask = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_STTS));
  716. /*
  717. * don't allow reorder of the various accesses to the BAM
  718. * registers
  719. */
  720. mb();
  721. writel_relaxed(clr_mask, bam_addr(bdev, 0, BAM_IRQ_CLR));
  722. }
  723. pm_runtime_mark_last_busy(bdev->dev);
  724. pm_runtime_put_autosuspend(bdev->dev);
  725. return IRQ_HANDLED;
  726. }
  727. /**
  728. * bam_tx_status - returns status of transaction
  729. * @chan: dma channel
  730. * @cookie: transaction cookie
  731. * @txstate: DMA transaction state
  732. *
  733. * Return status of dma transaction
  734. */
  735. static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  736. struct dma_tx_state *txstate)
  737. {
  738. struct bam_chan *bchan = to_bam_chan(chan);
  739. struct virt_dma_desc *vd;
  740. int ret;
  741. size_t residue = 0;
  742. unsigned int i;
  743. unsigned long flags;
  744. ret = dma_cookie_status(chan, cookie, txstate);
  745. if (ret == DMA_COMPLETE)
  746. return ret;
  747. if (!txstate)
  748. return bchan->paused ? DMA_PAUSED : ret;
  749. spin_lock_irqsave(&bchan->vc.lock, flags);
  750. vd = vchan_find_desc(&bchan->vc, cookie);
  751. if (vd)
  752. residue = container_of(vd, struct bam_async_desc, vd)->length;
  753. else if (bchan->curr_txd && bchan->curr_txd->vd.tx.cookie == cookie)
  754. for (i = 0; i < bchan->curr_txd->num_desc; i++)
  755. residue += bchan->curr_txd->curr_desc[i].size;
  756. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  757. dma_set_residue(txstate, residue);
  758. if (ret == DMA_IN_PROGRESS && bchan->paused)
  759. ret = DMA_PAUSED;
  760. return ret;
  761. }
  762. /**
  763. * bam_apply_new_config
  764. * @bchan: bam dma channel
  765. * @dir: DMA direction
  766. */
  767. static void bam_apply_new_config(struct bam_chan *bchan,
  768. enum dma_transfer_direction dir)
  769. {
  770. struct bam_device *bdev = bchan->bdev;
  771. u32 maxburst;
  772. if (dir == DMA_DEV_TO_MEM)
  773. maxburst = bchan->slave.src_maxburst;
  774. else
  775. maxburst = bchan->slave.dst_maxburst;
  776. writel_relaxed(maxburst, bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  777. bchan->reconfigure = 0;
  778. }
  779. /**
  780. * bam_start_dma - start next transaction
  781. * @bchan - bam dma channel
  782. */
  783. static void bam_start_dma(struct bam_chan *bchan)
  784. {
  785. struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
  786. struct bam_device *bdev = bchan->bdev;
  787. struct bam_async_desc *async_desc;
  788. struct bam_desc_hw *desc;
  789. struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
  790. sizeof(struct bam_desc_hw));
  791. int ret;
  792. lockdep_assert_held(&bchan->vc.lock);
  793. if (!vd)
  794. return;
  795. list_del(&vd->node);
  796. async_desc = container_of(vd, struct bam_async_desc, vd);
  797. bchan->curr_txd = async_desc;
  798. ret = pm_runtime_get_sync(bdev->dev);
  799. if (ret < 0)
  800. return;
  801. /* on first use, initialize the channel hardware */
  802. if (!bchan->initialized)
  803. bam_chan_init_hw(bchan, async_desc->dir);
  804. /* apply new slave config changes, if necessary */
  805. if (bchan->reconfigure)
  806. bam_apply_new_config(bchan, async_desc->dir);
  807. desc = bchan->curr_txd->curr_desc;
  808. if (async_desc->num_desc > MAX_DESCRIPTORS)
  809. async_desc->xfer_len = MAX_DESCRIPTORS;
  810. else
  811. async_desc->xfer_len = async_desc->num_desc;
  812. /* set any special flags on the last descriptor */
  813. if (async_desc->num_desc == async_desc->xfer_len)
  814. desc[async_desc->xfer_len - 1].flags |=
  815. cpu_to_le16(async_desc->flags);
  816. else
  817. desc[async_desc->xfer_len - 1].flags |=
  818. cpu_to_le16(DESC_FLAG_INT);
  819. if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
  820. u32 partial = MAX_DESCRIPTORS - bchan->tail;
  821. memcpy(&fifo[bchan->tail], desc,
  822. partial * sizeof(struct bam_desc_hw));
  823. memcpy(fifo, &desc[partial], (async_desc->xfer_len - partial) *
  824. sizeof(struct bam_desc_hw));
  825. } else {
  826. memcpy(&fifo[bchan->tail], desc,
  827. async_desc->xfer_len * sizeof(struct bam_desc_hw));
  828. }
  829. bchan->tail += async_desc->xfer_len;
  830. bchan->tail %= MAX_DESCRIPTORS;
  831. /* ensure descriptor writes and dma start not reordered */
  832. wmb();
  833. writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
  834. bam_addr(bdev, bchan->id, BAM_P_EVNT_REG));
  835. pm_runtime_mark_last_busy(bdev->dev);
  836. pm_runtime_put_autosuspend(bdev->dev);
  837. }
  838. /**
  839. * dma_tasklet - DMA IRQ tasklet
  840. * @data: tasklet argument (bam controller structure)
  841. *
  842. * Sets up next DMA operation and then processes all completed transactions
  843. */
  844. static void dma_tasklet(unsigned long data)
  845. {
  846. struct bam_device *bdev = (struct bam_device *)data;
  847. struct bam_chan *bchan;
  848. unsigned long flags;
  849. unsigned int i;
  850. /* go through the channels and kick off transactions */
  851. for (i = 0; i < bdev->num_channels; i++) {
  852. bchan = &bdev->channels[i];
  853. spin_lock_irqsave(&bchan->vc.lock, flags);
  854. if (!list_empty(&bchan->vc.desc_issued) && !bchan->curr_txd)
  855. bam_start_dma(bchan);
  856. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  857. }
  858. }
  859. /**
  860. * bam_issue_pending - starts pending transactions
  861. * @chan: dma channel
  862. *
  863. * Calls tasklet directly which in turn starts any pending transactions
  864. */
  865. static void bam_issue_pending(struct dma_chan *chan)
  866. {
  867. struct bam_chan *bchan = to_bam_chan(chan);
  868. unsigned long flags;
  869. spin_lock_irqsave(&bchan->vc.lock, flags);
  870. /* if work pending and idle, start a transaction */
  871. if (vchan_issue_pending(&bchan->vc) && !bchan->curr_txd)
  872. bam_start_dma(bchan);
  873. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  874. }
  875. /**
  876. * bam_dma_free_desc - free descriptor memory
  877. * @vd: virtual descriptor
  878. *
  879. */
  880. static void bam_dma_free_desc(struct virt_dma_desc *vd)
  881. {
  882. struct bam_async_desc *async_desc = container_of(vd,
  883. struct bam_async_desc, vd);
  884. kfree(async_desc);
  885. }
  886. static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
  887. struct of_dma *of)
  888. {
  889. struct bam_device *bdev = container_of(of->of_dma_data,
  890. struct bam_device, common);
  891. unsigned int request;
  892. if (dma_spec->args_count != 1)
  893. return NULL;
  894. request = dma_spec->args[0];
  895. if (request >= bdev->num_channels)
  896. return NULL;
  897. return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
  898. }
  899. /**
  900. * bam_init
  901. * @bdev: bam device
  902. *
  903. * Initialization helper for global bam registers
  904. */
  905. static int bam_init(struct bam_device *bdev)
  906. {
  907. u32 val;
  908. /* read revision and configuration information */
  909. val = readl_relaxed(bam_addr(bdev, 0, BAM_REVISION)) >> NUM_EES_SHIFT;
  910. val &= NUM_EES_MASK;
  911. /* check that configured EE is within range */
  912. if (bdev->ee >= val)
  913. return -EINVAL;
  914. val = readl_relaxed(bam_addr(bdev, 0, BAM_NUM_PIPES));
  915. bdev->num_channels = val & BAM_NUM_PIPES_MASK;
  916. if (bdev->controlled_remotely)
  917. return 0;
  918. /* s/w reset bam */
  919. /* after reset all pipes are disabled and idle */
  920. val = readl_relaxed(bam_addr(bdev, 0, BAM_CTRL));
  921. val |= BAM_SW_RST;
  922. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  923. val &= ~BAM_SW_RST;
  924. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  925. /* make sure previous stores are visible before enabling BAM */
  926. wmb();
  927. /* enable bam */
  928. val |= BAM_EN;
  929. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  930. /* set descriptor threshhold, start with 4 bytes */
  931. writel_relaxed(DEFAULT_CNT_THRSHLD,
  932. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  933. /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
  934. writel_relaxed(BAM_CNFG_BITS_DEFAULT, bam_addr(bdev, 0, BAM_CNFG_BITS));
  935. /* enable irqs for errors */
  936. writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
  937. bam_addr(bdev, 0, BAM_IRQ_EN));
  938. /* unmask global bam interrupt */
  939. writel_relaxed(BAM_IRQ_MSK, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  940. return 0;
  941. }
  942. static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
  943. u32 index)
  944. {
  945. bchan->id = index;
  946. bchan->bdev = bdev;
  947. vchan_init(&bchan->vc, &bdev->common);
  948. bchan->vc.desc_free = bam_dma_free_desc;
  949. }
  950. static const struct of_device_id bam_of_match[] = {
  951. { .compatible = "qcom,bam-v1.3.0", .data = &bam_v1_3_reg_info },
  952. { .compatible = "qcom,bam-v1.4.0", .data = &bam_v1_4_reg_info },
  953. { .compatible = "qcom,bam-v1.7.0", .data = &bam_v1_7_reg_info },
  954. {}
  955. };
  956. MODULE_DEVICE_TABLE(of, bam_of_match);
  957. static int bam_dma_probe(struct platform_device *pdev)
  958. {
  959. struct bam_device *bdev;
  960. const struct of_device_id *match;
  961. struct resource *iores;
  962. int ret, i;
  963. bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
  964. if (!bdev)
  965. return -ENOMEM;
  966. bdev->dev = &pdev->dev;
  967. match = of_match_node(bam_of_match, pdev->dev.of_node);
  968. if (!match) {
  969. dev_err(&pdev->dev, "Unsupported BAM module\n");
  970. return -ENODEV;
  971. }
  972. bdev->layout = match->data;
  973. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  974. bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
  975. if (IS_ERR(bdev->regs))
  976. return PTR_ERR(bdev->regs);
  977. bdev->irq = platform_get_irq(pdev, 0);
  978. if (bdev->irq < 0)
  979. return bdev->irq;
  980. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
  981. if (ret) {
  982. dev_err(bdev->dev, "Execution environment unspecified\n");
  983. return ret;
  984. }
  985. bdev->controlled_remotely = of_property_read_bool(pdev->dev.of_node,
  986. "qcom,controlled-remotely");
  987. bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
  988. if (IS_ERR(bdev->bamclk))
  989. return PTR_ERR(bdev->bamclk);
  990. ret = clk_prepare_enable(bdev->bamclk);
  991. if (ret) {
  992. dev_err(bdev->dev, "failed to prepare/enable clock\n");
  993. return ret;
  994. }
  995. ret = bam_init(bdev);
  996. if (ret)
  997. goto err_disable_clk;
  998. tasklet_init(&bdev->task, dma_tasklet, (unsigned long)bdev);
  999. bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
  1000. sizeof(*bdev->channels), GFP_KERNEL);
  1001. if (!bdev->channels) {
  1002. ret = -ENOMEM;
  1003. goto err_tasklet_kill;
  1004. }
  1005. /* allocate and initialize channels */
  1006. INIT_LIST_HEAD(&bdev->common.channels);
  1007. for (i = 0; i < bdev->num_channels; i++)
  1008. bam_channel_init(bdev, &bdev->channels[i], i);
  1009. ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
  1010. IRQF_TRIGGER_HIGH, "bam_dma", bdev);
  1011. if (ret)
  1012. goto err_bam_channel_exit;
  1013. /* set max dma segment size */
  1014. bdev->common.dev = bdev->dev;
  1015. bdev->common.dev->dma_parms = &bdev->dma_parms;
  1016. ret = dma_set_max_seg_size(bdev->common.dev, BAM_FIFO_SIZE);
  1017. if (ret) {
  1018. dev_err(bdev->dev, "cannot set maximum segment size\n");
  1019. goto err_bam_channel_exit;
  1020. }
  1021. platform_set_drvdata(pdev, bdev);
  1022. /* set capabilities */
  1023. dma_cap_zero(bdev->common.cap_mask);
  1024. dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
  1025. /* initialize dmaengine apis */
  1026. bdev->common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1027. bdev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1028. bdev->common.src_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1029. bdev->common.dst_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1030. bdev->common.device_alloc_chan_resources = bam_alloc_chan;
  1031. bdev->common.device_free_chan_resources = bam_free_chan;
  1032. bdev->common.device_prep_slave_sg = bam_prep_slave_sg;
  1033. bdev->common.device_config = bam_slave_config;
  1034. bdev->common.device_pause = bam_pause;
  1035. bdev->common.device_resume = bam_resume;
  1036. bdev->common.device_terminate_all = bam_dma_terminate_all;
  1037. bdev->common.device_issue_pending = bam_issue_pending;
  1038. bdev->common.device_tx_status = bam_tx_status;
  1039. bdev->common.dev = bdev->dev;
  1040. ret = dma_async_device_register(&bdev->common);
  1041. if (ret) {
  1042. dev_err(bdev->dev, "failed to register dma async device\n");
  1043. goto err_bam_channel_exit;
  1044. }
  1045. ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
  1046. &bdev->common);
  1047. if (ret)
  1048. goto err_unregister_dma;
  1049. pm_runtime_irq_safe(&pdev->dev);
  1050. pm_runtime_set_autosuspend_delay(&pdev->dev, BAM_DMA_AUTOSUSPEND_DELAY);
  1051. pm_runtime_use_autosuspend(&pdev->dev);
  1052. pm_runtime_mark_last_busy(&pdev->dev);
  1053. pm_runtime_set_active(&pdev->dev);
  1054. pm_runtime_enable(&pdev->dev);
  1055. return 0;
  1056. err_unregister_dma:
  1057. dma_async_device_unregister(&bdev->common);
  1058. err_bam_channel_exit:
  1059. for (i = 0; i < bdev->num_channels; i++)
  1060. tasklet_kill(&bdev->channels[i].vc.task);
  1061. err_tasklet_kill:
  1062. tasklet_kill(&bdev->task);
  1063. err_disable_clk:
  1064. clk_disable_unprepare(bdev->bamclk);
  1065. return ret;
  1066. }
  1067. static int bam_dma_remove(struct platform_device *pdev)
  1068. {
  1069. struct bam_device *bdev = platform_get_drvdata(pdev);
  1070. u32 i;
  1071. pm_runtime_force_suspend(&pdev->dev);
  1072. of_dma_controller_free(pdev->dev.of_node);
  1073. dma_async_device_unregister(&bdev->common);
  1074. /* mask all interrupts for this execution environment */
  1075. writel_relaxed(0, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  1076. devm_free_irq(bdev->dev, bdev->irq, bdev);
  1077. for (i = 0; i < bdev->num_channels; i++) {
  1078. bam_dma_terminate_all(&bdev->channels[i].vc.chan);
  1079. tasklet_kill(&bdev->channels[i].vc.task);
  1080. if (!bdev->channels[i].fifo_virt)
  1081. continue;
  1082. dma_free_wc(bdev->dev, BAM_DESC_FIFO_SIZE,
  1083. bdev->channels[i].fifo_virt,
  1084. bdev->channels[i].fifo_phys);
  1085. }
  1086. tasklet_kill(&bdev->task);
  1087. clk_disable_unprepare(bdev->bamclk);
  1088. return 0;
  1089. }
  1090. static int __maybe_unused bam_dma_runtime_suspend(struct device *dev)
  1091. {
  1092. struct bam_device *bdev = dev_get_drvdata(dev);
  1093. clk_disable(bdev->bamclk);
  1094. return 0;
  1095. }
  1096. static int __maybe_unused bam_dma_runtime_resume(struct device *dev)
  1097. {
  1098. struct bam_device *bdev = dev_get_drvdata(dev);
  1099. int ret;
  1100. ret = clk_enable(bdev->bamclk);
  1101. if (ret < 0) {
  1102. dev_err(dev, "clk_enable failed: %d\n", ret);
  1103. return ret;
  1104. }
  1105. return 0;
  1106. }
  1107. static int __maybe_unused bam_dma_suspend(struct device *dev)
  1108. {
  1109. struct bam_device *bdev = dev_get_drvdata(dev);
  1110. pm_runtime_force_suspend(dev);
  1111. clk_unprepare(bdev->bamclk);
  1112. return 0;
  1113. }
  1114. static int __maybe_unused bam_dma_resume(struct device *dev)
  1115. {
  1116. struct bam_device *bdev = dev_get_drvdata(dev);
  1117. int ret;
  1118. ret = clk_prepare(bdev->bamclk);
  1119. if (ret)
  1120. return ret;
  1121. pm_runtime_force_resume(dev);
  1122. return 0;
  1123. }
  1124. static const struct dev_pm_ops bam_dma_pm_ops = {
  1125. SET_LATE_SYSTEM_SLEEP_PM_OPS(bam_dma_suspend, bam_dma_resume)
  1126. SET_RUNTIME_PM_OPS(bam_dma_runtime_suspend, bam_dma_runtime_resume,
  1127. NULL)
  1128. };
  1129. static struct platform_driver bam_dma_driver = {
  1130. .probe = bam_dma_probe,
  1131. .remove = bam_dma_remove,
  1132. .driver = {
  1133. .name = "bam-dma-engine",
  1134. .pm = &bam_dma_pm_ops,
  1135. .of_match_table = bam_of_match,
  1136. },
  1137. };
  1138. module_platform_driver(bam_dma_driver);
  1139. MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  1140. MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
  1141. MODULE_LICENSE("GPL v2");