qcom_bam_dma.c 34 KB

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