bam_dma.c 39 KB

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