bam_dma.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. /*
  609. * If we have transactions queued, then some might be committed to the
  610. * hardware in the desc fifo. The only way to reset the desc fifo is
  611. * to do a hardware reset (either by pipe or the entire block).
  612. * bam_chan_init_hw() will trigger a pipe reset, and also reinit the
  613. * pipe. If the pipe is left disabled (default state after pipe reset)
  614. * and is accessed by a connected hardware engine, a fatal error in
  615. * the BAM will occur. There is a small window where this could happen
  616. * with bam_chan_init_hw(), but it is assumed that the caller has
  617. * stopped activity on any attached hardware engine. Make sure to do
  618. * this first so that the BAM hardware doesn't cause memory corruption
  619. * by accessing freed resources.
  620. */
  621. if (!list_empty(&bchan->desc_list)) {
  622. async_desc = list_first_entry(&bchan->desc_list,
  623. struct bam_async_desc, desc_node);
  624. bam_chan_init_hw(bchan, async_desc->dir);
  625. }
  626. list_for_each_entry_safe(async_desc, tmp,
  627. &bchan->desc_list, desc_node) {
  628. list_add(&async_desc->vd.node, &bchan->vc.desc_issued);
  629. list_del(&async_desc->desc_node);
  630. }
  631. vchan_get_all_descriptors(&bchan->vc, &head);
  632. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  633. vchan_dma_desc_free_list(&bchan->vc, &head);
  634. return 0;
  635. }
  636. /**
  637. * bam_pause - Pause DMA channel
  638. * @chan: dma channel
  639. *
  640. */
  641. static int bam_pause(struct dma_chan *chan)
  642. {
  643. struct bam_chan *bchan = to_bam_chan(chan);
  644. struct bam_device *bdev = bchan->bdev;
  645. unsigned long flag;
  646. int ret;
  647. ret = bam_pm_runtime_get_sync(bdev->dev);
  648. if (ret < 0)
  649. return ret;
  650. spin_lock_irqsave(&bchan->vc.lock, flag);
  651. writel_relaxed(1, bam_addr(bdev, bchan->id, BAM_P_HALT));
  652. bchan->paused = 1;
  653. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  654. pm_runtime_mark_last_busy(bdev->dev);
  655. pm_runtime_put_autosuspend(bdev->dev);
  656. return 0;
  657. }
  658. /**
  659. * bam_resume - Resume DMA channel operations
  660. * @chan: dma channel
  661. *
  662. */
  663. static int bam_resume(struct dma_chan *chan)
  664. {
  665. struct bam_chan *bchan = to_bam_chan(chan);
  666. struct bam_device *bdev = bchan->bdev;
  667. unsigned long flag;
  668. int ret;
  669. ret = bam_pm_runtime_get_sync(bdev->dev);
  670. if (ret < 0)
  671. return ret;
  672. spin_lock_irqsave(&bchan->vc.lock, flag);
  673. writel_relaxed(0, bam_addr(bdev, bchan->id, BAM_P_HALT));
  674. bchan->paused = 0;
  675. spin_unlock_irqrestore(&bchan->vc.lock, flag);
  676. pm_runtime_mark_last_busy(bdev->dev);
  677. pm_runtime_put_autosuspend(bdev->dev);
  678. return 0;
  679. }
  680. /**
  681. * process_channel_irqs - processes the channel interrupts
  682. * @bdev: bam controller
  683. *
  684. * This function processes the channel interrupts
  685. *
  686. */
  687. static u32 process_channel_irqs(struct bam_device *bdev)
  688. {
  689. u32 i, srcs, pipe_stts, offset, avail;
  690. unsigned long flags;
  691. struct bam_async_desc *async_desc, *tmp;
  692. srcs = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_SRCS_EE));
  693. /* return early if no pipe/channel interrupts are present */
  694. if (!(srcs & P_IRQ))
  695. return srcs;
  696. for (i = 0; i < bdev->num_channels; i++) {
  697. struct bam_chan *bchan = &bdev->channels[i];
  698. if (!(srcs & BIT(i)))
  699. continue;
  700. /* clear pipe irq */
  701. pipe_stts = readl_relaxed(bam_addr(bdev, i, BAM_P_IRQ_STTS));
  702. writel_relaxed(pipe_stts, bam_addr(bdev, i, BAM_P_IRQ_CLR));
  703. spin_lock_irqsave(&bchan->vc.lock, flags);
  704. offset = readl_relaxed(bam_addr(bdev, i, BAM_P_SW_OFSTS)) &
  705. P_SW_OFSTS_MASK;
  706. offset /= sizeof(struct bam_desc_hw);
  707. /* Number of bytes available to read */
  708. avail = CIRC_CNT(offset, bchan->head, MAX_DESCRIPTORS + 1);
  709. if (offset < bchan->head)
  710. avail--;
  711. list_for_each_entry_safe(async_desc, tmp,
  712. &bchan->desc_list, desc_node) {
  713. /* Not enough data to read */
  714. if (avail < async_desc->xfer_len)
  715. break;
  716. /* manage FIFO */
  717. bchan->head += async_desc->xfer_len;
  718. bchan->head %= MAX_DESCRIPTORS;
  719. async_desc->num_desc -= async_desc->xfer_len;
  720. async_desc->curr_desc += async_desc->xfer_len;
  721. avail -= async_desc->xfer_len;
  722. /*
  723. * if complete, process cookie. Otherwise
  724. * push back to front of desc_issued so that
  725. * it gets restarted by the tasklet
  726. */
  727. if (!async_desc->num_desc) {
  728. vchan_cookie_complete(&async_desc->vd);
  729. } else {
  730. list_add(&async_desc->vd.node,
  731. &bchan->vc.desc_issued);
  732. }
  733. list_del(&async_desc->desc_node);
  734. }
  735. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  736. }
  737. return srcs;
  738. }
  739. /**
  740. * bam_dma_irq - irq handler for bam controller
  741. * @irq: IRQ of interrupt
  742. * @data: callback data
  743. *
  744. * IRQ handler for the bam controller
  745. */
  746. static irqreturn_t bam_dma_irq(int irq, void *data)
  747. {
  748. struct bam_device *bdev = data;
  749. u32 clr_mask = 0, srcs = 0;
  750. int ret;
  751. srcs |= process_channel_irqs(bdev);
  752. /* kick off tasklet to start next dma transfer */
  753. if (srcs & P_IRQ)
  754. tasklet_schedule(&bdev->task);
  755. ret = bam_pm_runtime_get_sync(bdev->dev);
  756. if (ret < 0)
  757. return ret;
  758. if (srcs & BAM_IRQ) {
  759. clr_mask = readl_relaxed(bam_addr(bdev, 0, BAM_IRQ_STTS));
  760. /*
  761. * don't allow reorder of the various accesses to the BAM
  762. * registers
  763. */
  764. mb();
  765. writel_relaxed(clr_mask, bam_addr(bdev, 0, BAM_IRQ_CLR));
  766. }
  767. pm_runtime_mark_last_busy(bdev->dev);
  768. pm_runtime_put_autosuspend(bdev->dev);
  769. return IRQ_HANDLED;
  770. }
  771. /**
  772. * bam_tx_status - returns status of transaction
  773. * @chan: dma channel
  774. * @cookie: transaction cookie
  775. * @txstate: DMA transaction state
  776. *
  777. * Return status of dma transaction
  778. */
  779. static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  780. struct dma_tx_state *txstate)
  781. {
  782. struct bam_chan *bchan = to_bam_chan(chan);
  783. struct bam_async_desc *async_desc;
  784. struct virt_dma_desc *vd;
  785. int ret;
  786. size_t residue = 0;
  787. unsigned int i;
  788. unsigned long flags;
  789. ret = dma_cookie_status(chan, cookie, txstate);
  790. if (ret == DMA_COMPLETE)
  791. return ret;
  792. if (!txstate)
  793. return bchan->paused ? DMA_PAUSED : ret;
  794. spin_lock_irqsave(&bchan->vc.lock, flags);
  795. vd = vchan_find_desc(&bchan->vc, cookie);
  796. if (vd) {
  797. residue = container_of(vd, struct bam_async_desc, vd)->length;
  798. } else {
  799. list_for_each_entry(async_desc, &bchan->desc_list, desc_node) {
  800. if (async_desc->vd.tx.cookie != cookie)
  801. continue;
  802. for (i = 0; i < async_desc->num_desc; i++)
  803. residue += le16_to_cpu(
  804. async_desc->curr_desc[i].size);
  805. }
  806. }
  807. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  808. dma_set_residue(txstate, residue);
  809. if (ret == DMA_IN_PROGRESS && bchan->paused)
  810. ret = DMA_PAUSED;
  811. return ret;
  812. }
  813. /**
  814. * bam_apply_new_config
  815. * @bchan: bam dma channel
  816. * @dir: DMA direction
  817. */
  818. static void bam_apply_new_config(struct bam_chan *bchan,
  819. enum dma_transfer_direction dir)
  820. {
  821. struct bam_device *bdev = bchan->bdev;
  822. u32 maxburst;
  823. if (!bdev->controlled_remotely) {
  824. if (dir == DMA_DEV_TO_MEM)
  825. maxburst = bchan->slave.src_maxburst;
  826. else
  827. maxburst = bchan->slave.dst_maxburst;
  828. writel_relaxed(maxburst,
  829. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  830. }
  831. bchan->reconfigure = 0;
  832. }
  833. /**
  834. * bam_start_dma - start next transaction
  835. * @bchan: bam dma channel
  836. */
  837. static void bam_start_dma(struct bam_chan *bchan)
  838. {
  839. struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
  840. struct bam_device *bdev = bchan->bdev;
  841. struct bam_async_desc *async_desc = NULL;
  842. struct bam_desc_hw *desc;
  843. struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
  844. sizeof(struct bam_desc_hw));
  845. int ret;
  846. unsigned int avail;
  847. struct dmaengine_desc_callback cb;
  848. lockdep_assert_held(&bchan->vc.lock);
  849. if (!vd)
  850. return;
  851. ret = bam_pm_runtime_get_sync(bdev->dev);
  852. if (ret < 0)
  853. return;
  854. while (vd && !IS_BUSY(bchan)) {
  855. list_del(&vd->node);
  856. async_desc = container_of(vd, struct bam_async_desc, vd);
  857. /* on first use, initialize the channel hardware */
  858. if (!bchan->initialized)
  859. bam_chan_init_hw(bchan, async_desc->dir);
  860. /* apply new slave config changes, if necessary */
  861. if (bchan->reconfigure)
  862. bam_apply_new_config(bchan, async_desc->dir);
  863. desc = async_desc->curr_desc;
  864. avail = CIRC_SPACE(bchan->tail, bchan->head,
  865. MAX_DESCRIPTORS + 1);
  866. if (async_desc->num_desc > avail)
  867. async_desc->xfer_len = avail;
  868. else
  869. async_desc->xfer_len = async_desc->num_desc;
  870. /* set any special flags on the last descriptor */
  871. if (async_desc->num_desc == async_desc->xfer_len)
  872. desc[async_desc->xfer_len - 1].flags |=
  873. cpu_to_le16(async_desc->flags);
  874. vd = vchan_next_desc(&bchan->vc);
  875. dmaengine_desc_get_callback(&async_desc->vd.tx, &cb);
  876. /*
  877. * An interrupt is generated at this desc, if
  878. * - FIFO is FULL.
  879. * - No more descriptors to add.
  880. * - If a callback completion was requested for this DESC,
  881. * In this case, BAM will deliver the completion callback
  882. * for this desc and continue processing the next desc.
  883. */
  884. if (((avail <= async_desc->xfer_len) || !vd ||
  885. dmaengine_desc_callback_valid(&cb)) &&
  886. !(async_desc->flags & DESC_FLAG_EOT))
  887. desc[async_desc->xfer_len - 1].flags |=
  888. cpu_to_le16(DESC_FLAG_INT);
  889. if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
  890. u32 partial = MAX_DESCRIPTORS - bchan->tail;
  891. memcpy(&fifo[bchan->tail], desc,
  892. partial * sizeof(struct bam_desc_hw));
  893. memcpy(fifo, &desc[partial],
  894. (async_desc->xfer_len - partial) *
  895. sizeof(struct bam_desc_hw));
  896. } else {
  897. memcpy(&fifo[bchan->tail], desc,
  898. async_desc->xfer_len *
  899. sizeof(struct bam_desc_hw));
  900. }
  901. bchan->tail += async_desc->xfer_len;
  902. bchan->tail %= MAX_DESCRIPTORS;
  903. list_add_tail(&async_desc->desc_node, &bchan->desc_list);
  904. }
  905. /* ensure descriptor writes and dma start not reordered */
  906. wmb();
  907. writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
  908. bam_addr(bdev, bchan->id, BAM_P_EVNT_REG));
  909. pm_runtime_mark_last_busy(bdev->dev);
  910. pm_runtime_put_autosuspend(bdev->dev);
  911. }
  912. /**
  913. * dma_tasklet - DMA IRQ tasklet
  914. * @data: tasklet argument (bam controller structure)
  915. *
  916. * Sets up next DMA operation and then processes all completed transactions
  917. */
  918. static void dma_tasklet(unsigned long data)
  919. {
  920. struct bam_device *bdev = (struct bam_device *)data;
  921. struct bam_chan *bchan;
  922. unsigned long flags;
  923. unsigned int i;
  924. /* go through the channels and kick off transactions */
  925. for (i = 0; i < bdev->num_channels; i++) {
  926. bchan = &bdev->channels[i];
  927. spin_lock_irqsave(&bchan->vc.lock, flags);
  928. if (!list_empty(&bchan->vc.desc_issued) && !IS_BUSY(bchan))
  929. bam_start_dma(bchan);
  930. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  931. }
  932. }
  933. /**
  934. * bam_issue_pending - starts pending transactions
  935. * @chan: dma channel
  936. *
  937. * Calls tasklet directly which in turn starts any pending transactions
  938. */
  939. static void bam_issue_pending(struct dma_chan *chan)
  940. {
  941. struct bam_chan *bchan = to_bam_chan(chan);
  942. unsigned long flags;
  943. spin_lock_irqsave(&bchan->vc.lock, flags);
  944. /* if work pending and idle, start a transaction */
  945. if (vchan_issue_pending(&bchan->vc) && !IS_BUSY(bchan))
  946. bam_start_dma(bchan);
  947. spin_unlock_irqrestore(&bchan->vc.lock, flags);
  948. }
  949. /**
  950. * bam_dma_free_desc - free descriptor memory
  951. * @vd: virtual descriptor
  952. *
  953. */
  954. static void bam_dma_free_desc(struct virt_dma_desc *vd)
  955. {
  956. struct bam_async_desc *async_desc = container_of(vd,
  957. struct bam_async_desc, vd);
  958. kfree(async_desc);
  959. }
  960. static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
  961. struct of_dma *of)
  962. {
  963. struct bam_device *bdev = container_of(of->of_dma_data,
  964. struct bam_device, common);
  965. unsigned int request;
  966. if (dma_spec->args_count != 1)
  967. return NULL;
  968. request = dma_spec->args[0];
  969. if (request >= bdev->num_channels)
  970. return NULL;
  971. return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
  972. }
  973. /**
  974. * bam_init
  975. * @bdev: bam device
  976. *
  977. * Initialization helper for global bam registers
  978. */
  979. static int bam_init(struct bam_device *bdev)
  980. {
  981. u32 val;
  982. /* read revision and configuration information */
  983. if (!bdev->num_ees) {
  984. val = readl_relaxed(bam_addr(bdev, 0, BAM_REVISION));
  985. bdev->num_ees = (val >> NUM_EES_SHIFT) & NUM_EES_MASK;
  986. }
  987. /* check that configured EE is within range */
  988. if (bdev->ee >= bdev->num_ees)
  989. return -EINVAL;
  990. if (!bdev->num_channels) {
  991. val = readl_relaxed(bam_addr(bdev, 0, BAM_NUM_PIPES));
  992. bdev->num_channels = val & BAM_NUM_PIPES_MASK;
  993. }
  994. if (bdev->controlled_remotely)
  995. return 0;
  996. /* s/w reset bam */
  997. /* after reset all pipes are disabled and idle */
  998. val = readl_relaxed(bam_addr(bdev, 0, BAM_CTRL));
  999. val |= BAM_SW_RST;
  1000. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  1001. val &= ~BAM_SW_RST;
  1002. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  1003. /* make sure previous stores are visible before enabling BAM */
  1004. wmb();
  1005. /* enable bam */
  1006. val |= BAM_EN;
  1007. writel_relaxed(val, bam_addr(bdev, 0, BAM_CTRL));
  1008. /* set descriptor threshhold, start with 4 bytes */
  1009. writel_relaxed(DEFAULT_CNT_THRSHLD,
  1010. bam_addr(bdev, 0, BAM_DESC_CNT_TRSHLD));
  1011. /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
  1012. writel_relaxed(BAM_CNFG_BITS_DEFAULT, bam_addr(bdev, 0, BAM_CNFG_BITS));
  1013. /* enable irqs for errors */
  1014. writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
  1015. bam_addr(bdev, 0, BAM_IRQ_EN));
  1016. /* unmask global bam interrupt */
  1017. writel_relaxed(BAM_IRQ_MSK, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  1018. return 0;
  1019. }
  1020. static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
  1021. u32 index)
  1022. {
  1023. bchan->id = index;
  1024. bchan->bdev = bdev;
  1025. vchan_init(&bchan->vc, &bdev->common);
  1026. bchan->vc.desc_free = bam_dma_free_desc;
  1027. INIT_LIST_HEAD(&bchan->desc_list);
  1028. }
  1029. static const struct of_device_id bam_of_match[] = {
  1030. { .compatible = "qcom,bam-v1.3.0", .data = &bam_v1_3_reg_info },
  1031. { .compatible = "qcom,bam-v1.4.0", .data = &bam_v1_4_reg_info },
  1032. { .compatible = "qcom,bam-v1.7.0", .data = &bam_v1_7_reg_info },
  1033. {}
  1034. };
  1035. MODULE_DEVICE_TABLE(of, bam_of_match);
  1036. static int bam_dma_probe(struct platform_device *pdev)
  1037. {
  1038. struct bam_device *bdev;
  1039. const struct of_device_id *match;
  1040. struct resource *iores;
  1041. int ret, i;
  1042. bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
  1043. if (!bdev)
  1044. return -ENOMEM;
  1045. bdev->dev = &pdev->dev;
  1046. match = of_match_node(bam_of_match, pdev->dev.of_node);
  1047. if (!match) {
  1048. dev_err(&pdev->dev, "Unsupported BAM module\n");
  1049. return -ENODEV;
  1050. }
  1051. bdev->layout = match->data;
  1052. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1053. bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
  1054. if (IS_ERR(bdev->regs))
  1055. return PTR_ERR(bdev->regs);
  1056. bdev->irq = platform_get_irq(pdev, 0);
  1057. if (bdev->irq < 0)
  1058. return bdev->irq;
  1059. ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
  1060. if (ret) {
  1061. dev_err(bdev->dev, "Execution environment unspecified\n");
  1062. return ret;
  1063. }
  1064. bdev->controlled_remotely = of_property_read_bool(pdev->dev.of_node,
  1065. "qcom,controlled-remotely");
  1066. if (bdev->controlled_remotely) {
  1067. ret = of_property_read_u32(pdev->dev.of_node, "num-channels",
  1068. &bdev->num_channels);
  1069. if (ret)
  1070. dev_err(bdev->dev, "num-channels unspecified in dt\n");
  1071. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-ees",
  1072. &bdev->num_ees);
  1073. if (ret)
  1074. dev_err(bdev->dev, "num-ees unspecified in dt\n");
  1075. }
  1076. bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
  1077. if (IS_ERR(bdev->bamclk)) {
  1078. if (!bdev->controlled_remotely)
  1079. return PTR_ERR(bdev->bamclk);
  1080. bdev->bamclk = NULL;
  1081. }
  1082. ret = clk_prepare_enable(bdev->bamclk);
  1083. if (ret) {
  1084. dev_err(bdev->dev, "failed to prepare/enable clock\n");
  1085. return ret;
  1086. }
  1087. ret = bam_init(bdev);
  1088. if (ret)
  1089. goto err_disable_clk;
  1090. tasklet_init(&bdev->task, dma_tasklet, (unsigned long)bdev);
  1091. bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
  1092. sizeof(*bdev->channels), GFP_KERNEL);
  1093. if (!bdev->channels) {
  1094. ret = -ENOMEM;
  1095. goto err_tasklet_kill;
  1096. }
  1097. /* allocate and initialize channels */
  1098. INIT_LIST_HEAD(&bdev->common.channels);
  1099. for (i = 0; i < bdev->num_channels; i++)
  1100. bam_channel_init(bdev, &bdev->channels[i], i);
  1101. ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
  1102. IRQF_TRIGGER_HIGH, "bam_dma", bdev);
  1103. if (ret)
  1104. goto err_bam_channel_exit;
  1105. /* set max dma segment size */
  1106. bdev->common.dev = bdev->dev;
  1107. bdev->common.dev->dma_parms = &bdev->dma_parms;
  1108. ret = dma_set_max_seg_size(bdev->common.dev, BAM_FIFO_SIZE);
  1109. if (ret) {
  1110. dev_err(bdev->dev, "cannot set maximum segment size\n");
  1111. goto err_bam_channel_exit;
  1112. }
  1113. platform_set_drvdata(pdev, bdev);
  1114. /* set capabilities */
  1115. dma_cap_zero(bdev->common.cap_mask);
  1116. dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
  1117. /* initialize dmaengine apis */
  1118. bdev->common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1119. bdev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1120. bdev->common.src_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1121. bdev->common.dst_addr_widths = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1122. bdev->common.device_alloc_chan_resources = bam_alloc_chan;
  1123. bdev->common.device_free_chan_resources = bam_free_chan;
  1124. bdev->common.device_prep_slave_sg = bam_prep_slave_sg;
  1125. bdev->common.device_config = bam_slave_config;
  1126. bdev->common.device_pause = bam_pause;
  1127. bdev->common.device_resume = bam_resume;
  1128. bdev->common.device_terminate_all = bam_dma_terminate_all;
  1129. bdev->common.device_issue_pending = bam_issue_pending;
  1130. bdev->common.device_tx_status = bam_tx_status;
  1131. bdev->common.dev = bdev->dev;
  1132. ret = dma_async_device_register(&bdev->common);
  1133. if (ret) {
  1134. dev_err(bdev->dev, "failed to register dma async device\n");
  1135. goto err_bam_channel_exit;
  1136. }
  1137. ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
  1138. &bdev->common);
  1139. if (ret)
  1140. goto err_unregister_dma;
  1141. if (bdev->controlled_remotely) {
  1142. pm_runtime_disable(&pdev->dev);
  1143. return 0;
  1144. }
  1145. pm_runtime_irq_safe(&pdev->dev);
  1146. pm_runtime_set_autosuspend_delay(&pdev->dev, BAM_DMA_AUTOSUSPEND_DELAY);
  1147. pm_runtime_use_autosuspend(&pdev->dev);
  1148. pm_runtime_mark_last_busy(&pdev->dev);
  1149. pm_runtime_set_active(&pdev->dev);
  1150. pm_runtime_enable(&pdev->dev);
  1151. return 0;
  1152. err_unregister_dma:
  1153. dma_async_device_unregister(&bdev->common);
  1154. err_bam_channel_exit:
  1155. for (i = 0; i < bdev->num_channels; i++)
  1156. tasklet_kill(&bdev->channels[i].vc.task);
  1157. err_tasklet_kill:
  1158. tasklet_kill(&bdev->task);
  1159. err_disable_clk:
  1160. clk_disable_unprepare(bdev->bamclk);
  1161. return ret;
  1162. }
  1163. static int bam_dma_remove(struct platform_device *pdev)
  1164. {
  1165. struct bam_device *bdev = platform_get_drvdata(pdev);
  1166. u32 i;
  1167. pm_runtime_force_suspend(&pdev->dev);
  1168. of_dma_controller_free(pdev->dev.of_node);
  1169. dma_async_device_unregister(&bdev->common);
  1170. /* mask all interrupts for this execution environment */
  1171. writel_relaxed(0, bam_addr(bdev, 0, BAM_IRQ_SRCS_MSK_EE));
  1172. devm_free_irq(bdev->dev, bdev->irq, bdev);
  1173. for (i = 0; i < bdev->num_channels; i++) {
  1174. bam_dma_terminate_all(&bdev->channels[i].vc.chan);
  1175. tasklet_kill(&bdev->channels[i].vc.task);
  1176. if (!bdev->channels[i].fifo_virt)
  1177. continue;
  1178. dma_free_wc(bdev->dev, BAM_DESC_FIFO_SIZE,
  1179. bdev->channels[i].fifo_virt,
  1180. bdev->channels[i].fifo_phys);
  1181. }
  1182. tasklet_kill(&bdev->task);
  1183. clk_disable_unprepare(bdev->bamclk);
  1184. return 0;
  1185. }
  1186. static int __maybe_unused bam_dma_runtime_suspend(struct device *dev)
  1187. {
  1188. struct bam_device *bdev = dev_get_drvdata(dev);
  1189. clk_disable(bdev->bamclk);
  1190. return 0;
  1191. }
  1192. static int __maybe_unused bam_dma_runtime_resume(struct device *dev)
  1193. {
  1194. struct bam_device *bdev = dev_get_drvdata(dev);
  1195. int ret;
  1196. ret = clk_enable(bdev->bamclk);
  1197. if (ret < 0) {
  1198. dev_err(dev, "clk_enable failed: %d\n", ret);
  1199. return ret;
  1200. }
  1201. return 0;
  1202. }
  1203. static int __maybe_unused bam_dma_suspend(struct device *dev)
  1204. {
  1205. struct bam_device *bdev = dev_get_drvdata(dev);
  1206. if (!bdev->controlled_remotely)
  1207. pm_runtime_force_suspend(dev);
  1208. clk_unprepare(bdev->bamclk);
  1209. return 0;
  1210. }
  1211. static int __maybe_unused bam_dma_resume(struct device *dev)
  1212. {
  1213. struct bam_device *bdev = dev_get_drvdata(dev);
  1214. int ret;
  1215. ret = clk_prepare(bdev->bamclk);
  1216. if (ret)
  1217. return ret;
  1218. if (!bdev->controlled_remotely)
  1219. pm_runtime_force_resume(dev);
  1220. return 0;
  1221. }
  1222. static const struct dev_pm_ops bam_dma_pm_ops = {
  1223. SET_LATE_SYSTEM_SLEEP_PM_OPS(bam_dma_suspend, bam_dma_resume)
  1224. SET_RUNTIME_PM_OPS(bam_dma_runtime_suspend, bam_dma_runtime_resume,
  1225. NULL)
  1226. };
  1227. static struct platform_driver bam_dma_driver = {
  1228. .probe = bam_dma_probe,
  1229. .remove = bam_dma_remove,
  1230. .driver = {
  1231. .name = "bam-dma-engine",
  1232. .pm = &bam_dma_pm_ops,
  1233. .of_match_table = bam_of_match,
  1234. },
  1235. };
  1236. module_platform_driver(bam_dma_driver);
  1237. MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
  1238. MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
  1239. MODULE_LICENSE("GPL v2");