imx-sdma.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // drivers/dma/imx-sdma.c
  4. //
  5. // This file contains a driver for the Freescale Smart DMA engine
  6. //
  7. // Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  8. //
  9. // Based on code from Freescale:
  10. //
  11. // Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  12. #include <linux/init.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/semaphore.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/firmware.h>
  27. #include <linux/slab.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_dma.h>
  34. #include <linux/workqueue.h>
  35. #include <asm/irq.h>
  36. #include <linux/platform_data/dma-imx-sdma.h>
  37. #include <linux/platform_data/dma-imx.h>
  38. #include <linux/regmap.h>
  39. #include <linux/mfd/syscon.h>
  40. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  41. #include "dmaengine.h"
  42. #include "virt-dma.h"
  43. /* SDMA registers */
  44. #define SDMA_H_C0PTR 0x000
  45. #define SDMA_H_INTR 0x004
  46. #define SDMA_H_STATSTOP 0x008
  47. #define SDMA_H_START 0x00c
  48. #define SDMA_H_EVTOVR 0x010
  49. #define SDMA_H_DSPOVR 0x014
  50. #define SDMA_H_HOSTOVR 0x018
  51. #define SDMA_H_EVTPEND 0x01c
  52. #define SDMA_H_DSPENBL 0x020
  53. #define SDMA_H_RESET 0x024
  54. #define SDMA_H_EVTERR 0x028
  55. #define SDMA_H_INTRMSK 0x02c
  56. #define SDMA_H_PSW 0x030
  57. #define SDMA_H_EVTERRDBG 0x034
  58. #define SDMA_H_CONFIG 0x038
  59. #define SDMA_ONCE_ENB 0x040
  60. #define SDMA_ONCE_DATA 0x044
  61. #define SDMA_ONCE_INSTR 0x048
  62. #define SDMA_ONCE_STAT 0x04c
  63. #define SDMA_ONCE_CMD 0x050
  64. #define SDMA_EVT_MIRROR 0x054
  65. #define SDMA_ILLINSTADDR 0x058
  66. #define SDMA_CHN0ADDR 0x05c
  67. #define SDMA_ONCE_RTB 0x060
  68. #define SDMA_XTRIG_CONF1 0x070
  69. #define SDMA_XTRIG_CONF2 0x074
  70. #define SDMA_CHNENBL0_IMX35 0x200
  71. #define SDMA_CHNENBL0_IMX31 0x080
  72. #define SDMA_CHNPRI_0 0x100
  73. /*
  74. * Buffer descriptor status values.
  75. */
  76. #define BD_DONE 0x01
  77. #define BD_WRAP 0x02
  78. #define BD_CONT 0x04
  79. #define BD_INTR 0x08
  80. #define BD_RROR 0x10
  81. #define BD_LAST 0x20
  82. #define BD_EXTD 0x80
  83. /*
  84. * Data Node descriptor status values.
  85. */
  86. #define DND_END_OF_FRAME 0x80
  87. #define DND_END_OF_XFER 0x40
  88. #define DND_DONE 0x20
  89. #define DND_UNUSED 0x01
  90. /*
  91. * IPCV2 descriptor status values.
  92. */
  93. #define BD_IPCV2_END_OF_FRAME 0x40
  94. #define IPCV2_MAX_NODES 50
  95. /*
  96. * Error bit set in the CCB status field by the SDMA,
  97. * in setbd routine, in case of a transfer error
  98. */
  99. #define DATA_ERROR 0x10000000
  100. /*
  101. * Buffer descriptor commands.
  102. */
  103. #define C0_ADDR 0x01
  104. #define C0_LOAD 0x02
  105. #define C0_DUMP 0x03
  106. #define C0_SETCTX 0x07
  107. #define C0_GETCTX 0x03
  108. #define C0_SETDM 0x01
  109. #define C0_SETPM 0x04
  110. #define C0_GETDM 0x02
  111. #define C0_GETPM 0x08
  112. /*
  113. * Change endianness indicator in the BD command field
  114. */
  115. #define CHANGE_ENDIANNESS 0x80
  116. /*
  117. * p_2_p watermark_level description
  118. * Bits Name Description
  119. * 0-7 Lower WML Lower watermark level
  120. * 8 PS 1: Pad Swallowing
  121. * 0: No Pad Swallowing
  122. * 9 PA 1: Pad Adding
  123. * 0: No Pad Adding
  124. * 10 SPDIF If this bit is set both source
  125. * and destination are on SPBA
  126. * 11 Source Bit(SP) 1: Source on SPBA
  127. * 0: Source on AIPS
  128. * 12 Destination Bit(DP) 1: Destination on SPBA
  129. * 0: Destination on AIPS
  130. * 13-15 --------- MUST BE 0
  131. * 16-23 Higher WML HWML
  132. * 24-27 N Total number of samples after
  133. * which Pad adding/Swallowing
  134. * must be done. It must be odd.
  135. * 28 Lower WML Event(LWE) SDMA events reg to check for
  136. * LWML event mask
  137. * 0: LWE in EVENTS register
  138. * 1: LWE in EVENTS2 register
  139. * 29 Higher WML Event(HWE) SDMA events reg to check for
  140. * HWML event mask
  141. * 0: HWE in EVENTS register
  142. * 1: HWE in EVENTS2 register
  143. * 30 --------- MUST BE 0
  144. * 31 CONT 1: Amount of samples to be
  145. * transferred is unknown and
  146. * script will keep on
  147. * transferring samples as long as
  148. * both events are detected and
  149. * script must be manually stopped
  150. * by the application
  151. * 0: The amount of samples to be
  152. * transferred is equal to the
  153. * count field of mode word
  154. */
  155. #define SDMA_WATERMARK_LEVEL_LWML 0xFF
  156. #define SDMA_WATERMARK_LEVEL_PS BIT(8)
  157. #define SDMA_WATERMARK_LEVEL_PA BIT(9)
  158. #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
  159. #define SDMA_WATERMARK_LEVEL_SP BIT(11)
  160. #define SDMA_WATERMARK_LEVEL_DP BIT(12)
  161. #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
  162. #define SDMA_WATERMARK_LEVEL_LWE BIT(28)
  163. #define SDMA_WATERMARK_LEVEL_HWE BIT(29)
  164. #define SDMA_WATERMARK_LEVEL_CONT BIT(31)
  165. #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  166. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  167. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  168. #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
  169. BIT(DMA_MEM_TO_DEV) | \
  170. BIT(DMA_DEV_TO_DEV))
  171. /*
  172. * Mode/Count of data node descriptors - IPCv2
  173. */
  174. struct sdma_mode_count {
  175. #define SDMA_BD_MAX_CNT 0xffff
  176. u32 count : 16; /* size of the buffer pointed by this BD */
  177. u32 status : 8; /* E,R,I,C,W,D status bits stored here */
  178. u32 command : 8; /* command mostly used for channel 0 */
  179. };
  180. /*
  181. * Buffer descriptor
  182. */
  183. struct sdma_buffer_descriptor {
  184. struct sdma_mode_count mode;
  185. u32 buffer_addr; /* address of the buffer described */
  186. u32 ext_buffer_addr; /* extended buffer address */
  187. } __attribute__ ((packed));
  188. /**
  189. * struct sdma_channel_control - Channel control Block
  190. *
  191. * @current_bd_ptr: current buffer descriptor processed
  192. * @base_bd_ptr: first element of buffer descriptor array
  193. * @unused: padding. The SDMA engine expects an array of 128 byte
  194. * control blocks
  195. */
  196. struct sdma_channel_control {
  197. u32 current_bd_ptr;
  198. u32 base_bd_ptr;
  199. u32 unused[2];
  200. } __attribute__ ((packed));
  201. /**
  202. * struct sdma_state_registers - SDMA context for a channel
  203. *
  204. * @pc: program counter
  205. * @unused1: unused
  206. * @t: test bit: status of arithmetic & test instruction
  207. * @rpc: return program counter
  208. * @unused0: unused
  209. * @sf: source fault while loading data
  210. * @spc: loop start program counter
  211. * @unused2: unused
  212. * @df: destination fault while storing data
  213. * @epc: loop end program counter
  214. * @lm: loop mode
  215. */
  216. struct sdma_state_registers {
  217. u32 pc :14;
  218. u32 unused1: 1;
  219. u32 t : 1;
  220. u32 rpc :14;
  221. u32 unused0: 1;
  222. u32 sf : 1;
  223. u32 spc :14;
  224. u32 unused2: 1;
  225. u32 df : 1;
  226. u32 epc :14;
  227. u32 lm : 2;
  228. } __attribute__ ((packed));
  229. /**
  230. * struct sdma_context_data - sdma context specific to a channel
  231. *
  232. * @channel_state: channel state bits
  233. * @gReg: general registers
  234. * @mda: burst dma destination address register
  235. * @msa: burst dma source address register
  236. * @ms: burst dma status register
  237. * @md: burst dma data register
  238. * @pda: peripheral dma destination address register
  239. * @psa: peripheral dma source address register
  240. * @ps: peripheral dma status register
  241. * @pd: peripheral dma data register
  242. * @ca: CRC polynomial register
  243. * @cs: CRC accumulator register
  244. * @dda: dedicated core destination address register
  245. * @dsa: dedicated core source address register
  246. * @ds: dedicated core status register
  247. * @dd: dedicated core data register
  248. * @scratch0: 1st word of dedicated ram for context switch
  249. * @scratch1: 2nd word of dedicated ram for context switch
  250. * @scratch2: 3rd word of dedicated ram for context switch
  251. * @scratch3: 4th word of dedicated ram for context switch
  252. * @scratch4: 5th word of dedicated ram for context switch
  253. * @scratch5: 6th word of dedicated ram for context switch
  254. * @scratch6: 7th word of dedicated ram for context switch
  255. * @scratch7: 8th word of dedicated ram for context switch
  256. */
  257. struct sdma_context_data {
  258. struct sdma_state_registers channel_state;
  259. u32 gReg[8];
  260. u32 mda;
  261. u32 msa;
  262. u32 ms;
  263. u32 md;
  264. u32 pda;
  265. u32 psa;
  266. u32 ps;
  267. u32 pd;
  268. u32 ca;
  269. u32 cs;
  270. u32 dda;
  271. u32 dsa;
  272. u32 ds;
  273. u32 dd;
  274. u32 scratch0;
  275. u32 scratch1;
  276. u32 scratch2;
  277. u32 scratch3;
  278. u32 scratch4;
  279. u32 scratch5;
  280. u32 scratch6;
  281. u32 scratch7;
  282. } __attribute__ ((packed));
  283. struct sdma_engine;
  284. /**
  285. * struct sdma_desc - descriptor structor for one transfer
  286. * @vd: descriptor for virt dma
  287. * @num_bd: number of descriptors currently handling
  288. * @bd_phys: physical address of bd
  289. * @buf_tail: ID of the buffer that was processed
  290. * @buf_ptail: ID of the previous buffer that was processed
  291. * @period_len: period length, used in cyclic.
  292. * @chn_real_count: the real count updated from bd->mode.count
  293. * @chn_count: the transfer count set
  294. * @sdmac: sdma_channel pointer
  295. * @bd: pointer of allocate bd
  296. */
  297. struct sdma_desc {
  298. struct virt_dma_desc vd;
  299. unsigned int num_bd;
  300. dma_addr_t bd_phys;
  301. unsigned int buf_tail;
  302. unsigned int buf_ptail;
  303. unsigned int period_len;
  304. unsigned int chn_real_count;
  305. unsigned int chn_count;
  306. struct sdma_channel *sdmac;
  307. struct sdma_buffer_descriptor *bd;
  308. };
  309. /**
  310. * struct sdma_channel - housekeeping for a SDMA channel
  311. *
  312. * @vc: virt_dma base structure
  313. * @desc: sdma description including vd and other special member
  314. * @sdma: pointer to the SDMA engine for this channel
  315. * @channel: the channel number, matches dmaengine chan_id + 1
  316. * @direction: transfer type. Needed for setting SDMA script
  317. * @slave_config Slave configuration
  318. * @peripheral_type: Peripheral type. Needed for setting SDMA script
  319. * @event_id0: aka dma request line
  320. * @event_id1: for channels that use 2 events
  321. * @word_size: peripheral access size
  322. * @pc_from_device: script address for those device_2_memory
  323. * @pc_to_device: script address for those memory_2_device
  324. * @device_to_device: script address for those device_2_device
  325. * @pc_to_pc: script address for those memory_2_memory
  326. * @flags: loop mode or not
  327. * @per_address: peripheral source or destination address in common case
  328. * destination address in p_2_p case
  329. * @per_address2: peripheral source address in p_2_p case
  330. * @event_mask: event mask used in p_2_p script
  331. * @watermark_level: value for gReg[7], some script will extend it from
  332. * basic watermark such as p_2_p
  333. * @shp_addr: value for gReg[6]
  334. * @per_addr: value for gReg[2]
  335. * @status: status of dma channel
  336. * @data: specific sdma interface structure
  337. * @bd_pool: dma_pool for bd
  338. */
  339. struct sdma_channel {
  340. struct virt_dma_chan vc;
  341. struct sdma_desc *desc;
  342. struct sdma_engine *sdma;
  343. unsigned int channel;
  344. enum dma_transfer_direction direction;
  345. struct dma_slave_config slave_config;
  346. enum sdma_peripheral_type peripheral_type;
  347. unsigned int event_id0;
  348. unsigned int event_id1;
  349. enum dma_slave_buswidth word_size;
  350. unsigned int pc_from_device, pc_to_device;
  351. unsigned int device_to_device;
  352. unsigned int pc_to_pc;
  353. unsigned long flags;
  354. dma_addr_t per_address, per_address2;
  355. unsigned long event_mask[2];
  356. unsigned long watermark_level;
  357. u32 shp_addr, per_addr;
  358. enum dma_status status;
  359. struct imx_dma_data data;
  360. struct work_struct terminate_worker;
  361. };
  362. #define IMX_DMA_SG_LOOP BIT(0)
  363. #define MAX_DMA_CHANNELS 32
  364. #define MXC_SDMA_DEFAULT_PRIORITY 1
  365. #define MXC_SDMA_MIN_PRIORITY 1
  366. #define MXC_SDMA_MAX_PRIORITY 7
  367. #define SDMA_FIRMWARE_MAGIC 0x414d4453
  368. /**
  369. * struct sdma_firmware_header - Layout of the firmware image
  370. *
  371. * @magic: "SDMA"
  372. * @version_major: increased whenever layout of struct
  373. * sdma_script_start_addrs changes.
  374. * @version_minor: firmware minor version (for binary compatible changes)
  375. * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
  376. * @num_script_addrs: Number of script addresses in this image
  377. * @ram_code_start: offset of SDMA ram image in this firmware image
  378. * @ram_code_size: size of SDMA ram image
  379. * @script_addrs: Stores the start address of the SDMA scripts
  380. * (in SDMA memory space)
  381. */
  382. struct sdma_firmware_header {
  383. u32 magic;
  384. u32 version_major;
  385. u32 version_minor;
  386. u32 script_addrs_start;
  387. u32 num_script_addrs;
  388. u32 ram_code_start;
  389. u32 ram_code_size;
  390. };
  391. struct sdma_driver_data {
  392. int chnenbl0;
  393. int num_events;
  394. struct sdma_script_start_addrs *script_addrs;
  395. };
  396. struct sdma_engine {
  397. struct device *dev;
  398. struct device_dma_parameters dma_parms;
  399. struct sdma_channel channel[MAX_DMA_CHANNELS];
  400. struct sdma_channel_control *channel_control;
  401. void __iomem *regs;
  402. struct sdma_context_data *context;
  403. dma_addr_t context_phys;
  404. struct dma_device dma_device;
  405. struct clk *clk_ipg;
  406. struct clk *clk_ahb;
  407. spinlock_t channel_0_lock;
  408. u32 script_number;
  409. struct sdma_script_start_addrs *script_addrs;
  410. const struct sdma_driver_data *drvdata;
  411. u32 spba_start_addr;
  412. u32 spba_end_addr;
  413. unsigned int irq;
  414. dma_addr_t bd0_phys;
  415. struct sdma_buffer_descriptor *bd0;
  416. };
  417. static int sdma_config_write(struct dma_chan *chan,
  418. struct dma_slave_config *dmaengine_cfg,
  419. enum dma_transfer_direction direction);
  420. static struct sdma_driver_data sdma_imx31 = {
  421. .chnenbl0 = SDMA_CHNENBL0_IMX31,
  422. .num_events = 32,
  423. };
  424. static struct sdma_script_start_addrs sdma_script_imx25 = {
  425. .ap_2_ap_addr = 729,
  426. .uart_2_mcu_addr = 904,
  427. .per_2_app_addr = 1255,
  428. .mcu_2_app_addr = 834,
  429. .uartsh_2_mcu_addr = 1120,
  430. .per_2_shp_addr = 1329,
  431. .mcu_2_shp_addr = 1048,
  432. .ata_2_mcu_addr = 1560,
  433. .mcu_2_ata_addr = 1479,
  434. .app_2_per_addr = 1189,
  435. .app_2_mcu_addr = 770,
  436. .shp_2_per_addr = 1407,
  437. .shp_2_mcu_addr = 979,
  438. };
  439. static struct sdma_driver_data sdma_imx25 = {
  440. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  441. .num_events = 48,
  442. .script_addrs = &sdma_script_imx25,
  443. };
  444. static struct sdma_driver_data sdma_imx35 = {
  445. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  446. .num_events = 48,
  447. };
  448. static struct sdma_script_start_addrs sdma_script_imx51 = {
  449. .ap_2_ap_addr = 642,
  450. .uart_2_mcu_addr = 817,
  451. .mcu_2_app_addr = 747,
  452. .mcu_2_shp_addr = 961,
  453. .ata_2_mcu_addr = 1473,
  454. .mcu_2_ata_addr = 1392,
  455. .app_2_per_addr = 1033,
  456. .app_2_mcu_addr = 683,
  457. .shp_2_per_addr = 1251,
  458. .shp_2_mcu_addr = 892,
  459. };
  460. static struct sdma_driver_data sdma_imx51 = {
  461. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  462. .num_events = 48,
  463. .script_addrs = &sdma_script_imx51,
  464. };
  465. static struct sdma_script_start_addrs sdma_script_imx53 = {
  466. .ap_2_ap_addr = 642,
  467. .app_2_mcu_addr = 683,
  468. .mcu_2_app_addr = 747,
  469. .uart_2_mcu_addr = 817,
  470. .shp_2_mcu_addr = 891,
  471. .mcu_2_shp_addr = 960,
  472. .uartsh_2_mcu_addr = 1032,
  473. .spdif_2_mcu_addr = 1100,
  474. .mcu_2_spdif_addr = 1134,
  475. .firi_2_mcu_addr = 1193,
  476. .mcu_2_firi_addr = 1290,
  477. };
  478. static struct sdma_driver_data sdma_imx53 = {
  479. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  480. .num_events = 48,
  481. .script_addrs = &sdma_script_imx53,
  482. };
  483. static struct sdma_script_start_addrs sdma_script_imx6q = {
  484. .ap_2_ap_addr = 642,
  485. .uart_2_mcu_addr = 817,
  486. .mcu_2_app_addr = 747,
  487. .per_2_per_addr = 6331,
  488. .uartsh_2_mcu_addr = 1032,
  489. .mcu_2_shp_addr = 960,
  490. .app_2_mcu_addr = 683,
  491. .shp_2_mcu_addr = 891,
  492. .spdif_2_mcu_addr = 1100,
  493. .mcu_2_spdif_addr = 1134,
  494. };
  495. static struct sdma_driver_data sdma_imx6q = {
  496. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  497. .num_events = 48,
  498. .script_addrs = &sdma_script_imx6q,
  499. };
  500. static struct sdma_script_start_addrs sdma_script_imx7d = {
  501. .ap_2_ap_addr = 644,
  502. .uart_2_mcu_addr = 819,
  503. .mcu_2_app_addr = 749,
  504. .uartsh_2_mcu_addr = 1034,
  505. .mcu_2_shp_addr = 962,
  506. .app_2_mcu_addr = 685,
  507. .shp_2_mcu_addr = 893,
  508. .spdif_2_mcu_addr = 1102,
  509. .mcu_2_spdif_addr = 1136,
  510. };
  511. static struct sdma_driver_data sdma_imx7d = {
  512. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  513. .num_events = 48,
  514. .script_addrs = &sdma_script_imx7d,
  515. };
  516. static const struct platform_device_id sdma_devtypes[] = {
  517. {
  518. .name = "imx25-sdma",
  519. .driver_data = (unsigned long)&sdma_imx25,
  520. }, {
  521. .name = "imx31-sdma",
  522. .driver_data = (unsigned long)&sdma_imx31,
  523. }, {
  524. .name = "imx35-sdma",
  525. .driver_data = (unsigned long)&sdma_imx35,
  526. }, {
  527. .name = "imx51-sdma",
  528. .driver_data = (unsigned long)&sdma_imx51,
  529. }, {
  530. .name = "imx53-sdma",
  531. .driver_data = (unsigned long)&sdma_imx53,
  532. }, {
  533. .name = "imx6q-sdma",
  534. .driver_data = (unsigned long)&sdma_imx6q,
  535. }, {
  536. .name = "imx7d-sdma",
  537. .driver_data = (unsigned long)&sdma_imx7d,
  538. }, {
  539. /* sentinel */
  540. }
  541. };
  542. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  543. static const struct of_device_id sdma_dt_ids[] = {
  544. { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
  545. { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
  546. { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
  547. { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
  548. { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
  549. { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
  550. { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
  551. { /* sentinel */ }
  552. };
  553. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  554. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  555. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  556. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  557. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  558. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  559. {
  560. u32 chnenbl0 = sdma->drvdata->chnenbl0;
  561. return chnenbl0 + event * 4;
  562. }
  563. static int sdma_config_ownership(struct sdma_channel *sdmac,
  564. bool event_override, bool mcu_override, bool dsp_override)
  565. {
  566. struct sdma_engine *sdma = sdmac->sdma;
  567. int channel = sdmac->channel;
  568. unsigned long evt, mcu, dsp;
  569. if (event_override && mcu_override && dsp_override)
  570. return -EINVAL;
  571. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  572. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  573. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  574. if (dsp_override)
  575. __clear_bit(channel, &dsp);
  576. else
  577. __set_bit(channel, &dsp);
  578. if (event_override)
  579. __clear_bit(channel, &evt);
  580. else
  581. __set_bit(channel, &evt);
  582. if (mcu_override)
  583. __clear_bit(channel, &mcu);
  584. else
  585. __set_bit(channel, &mcu);
  586. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  587. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  588. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  589. return 0;
  590. }
  591. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  592. {
  593. writel(BIT(channel), sdma->regs + SDMA_H_START);
  594. }
  595. /*
  596. * sdma_run_channel0 - run a channel and wait till it's done
  597. */
  598. static int sdma_run_channel0(struct sdma_engine *sdma)
  599. {
  600. int ret;
  601. u32 reg;
  602. sdma_enable_channel(sdma, 0);
  603. ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
  604. reg, !(reg & 1), 1, 500);
  605. if (ret)
  606. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  607. /* Set bits of CONFIG register with dynamic context switching */
  608. if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
  609. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  610. return ret;
  611. }
  612. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  613. u32 address)
  614. {
  615. struct sdma_buffer_descriptor *bd0 = sdma->bd0;
  616. void *buf_virt;
  617. dma_addr_t buf_phys;
  618. int ret;
  619. unsigned long flags;
  620. buf_virt = dma_alloc_coherent(NULL,
  621. size,
  622. &buf_phys, GFP_KERNEL);
  623. if (!buf_virt) {
  624. return -ENOMEM;
  625. }
  626. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  627. bd0->mode.command = C0_SETPM;
  628. bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
  629. bd0->mode.count = size / 2;
  630. bd0->buffer_addr = buf_phys;
  631. bd0->ext_buffer_addr = address;
  632. memcpy(buf_virt, buf, size);
  633. ret = sdma_run_channel0(sdma);
  634. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  635. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  636. return ret;
  637. }
  638. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  639. {
  640. struct sdma_engine *sdma = sdmac->sdma;
  641. int channel = sdmac->channel;
  642. unsigned long val;
  643. u32 chnenbl = chnenbl_ofs(sdma, event);
  644. val = readl_relaxed(sdma->regs + chnenbl);
  645. __set_bit(channel, &val);
  646. writel_relaxed(val, sdma->regs + chnenbl);
  647. }
  648. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  649. {
  650. struct sdma_engine *sdma = sdmac->sdma;
  651. int channel = sdmac->channel;
  652. u32 chnenbl = chnenbl_ofs(sdma, event);
  653. unsigned long val;
  654. val = readl_relaxed(sdma->regs + chnenbl);
  655. __clear_bit(channel, &val);
  656. writel_relaxed(val, sdma->regs + chnenbl);
  657. }
  658. static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
  659. {
  660. return container_of(t, struct sdma_desc, vd.tx);
  661. }
  662. static void sdma_start_desc(struct sdma_channel *sdmac)
  663. {
  664. struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
  665. struct sdma_desc *desc;
  666. struct sdma_engine *sdma = sdmac->sdma;
  667. int channel = sdmac->channel;
  668. if (!vd) {
  669. sdmac->desc = NULL;
  670. return;
  671. }
  672. sdmac->desc = desc = to_sdma_desc(&vd->tx);
  673. /*
  674. * Do not delete the node in desc_issued list in cyclic mode, otherwise
  675. * the desc allocated will never be freed in vchan_dma_desc_free_list
  676. */
  677. if (!(sdmac->flags & IMX_DMA_SG_LOOP))
  678. list_del(&vd->node);
  679. sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
  680. sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
  681. sdma_enable_channel(sdma, sdmac->channel);
  682. }
  683. static void sdma_update_channel_loop(struct sdma_channel *sdmac)
  684. {
  685. struct sdma_buffer_descriptor *bd;
  686. int error = 0;
  687. enum dma_status old_status = sdmac->status;
  688. /*
  689. * loop mode. Iterate over descriptors, re-setup them and
  690. * call callback function.
  691. */
  692. while (sdmac->desc) {
  693. struct sdma_desc *desc = sdmac->desc;
  694. bd = &desc->bd[desc->buf_tail];
  695. if (bd->mode.status & BD_DONE)
  696. break;
  697. if (bd->mode.status & BD_RROR) {
  698. bd->mode.status &= ~BD_RROR;
  699. sdmac->status = DMA_ERROR;
  700. error = -EIO;
  701. }
  702. /*
  703. * We use bd->mode.count to calculate the residue, since contains
  704. * the number of bytes present in the current buffer descriptor.
  705. */
  706. desc->chn_real_count = bd->mode.count;
  707. bd->mode.status |= BD_DONE;
  708. bd->mode.count = desc->period_len;
  709. desc->buf_ptail = desc->buf_tail;
  710. desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
  711. /*
  712. * The callback is called from the interrupt context in order
  713. * to reduce latency and to avoid the risk of altering the
  714. * SDMA transaction status by the time the client tasklet is
  715. * executed.
  716. */
  717. spin_unlock(&sdmac->vc.lock);
  718. dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
  719. spin_lock(&sdmac->vc.lock);
  720. if (error)
  721. sdmac->status = old_status;
  722. }
  723. }
  724. static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
  725. {
  726. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  727. struct sdma_buffer_descriptor *bd;
  728. int i, error = 0;
  729. sdmac->desc->chn_real_count = 0;
  730. /*
  731. * non loop mode. Iterate over all descriptors, collect
  732. * errors and call callback function
  733. */
  734. for (i = 0; i < sdmac->desc->num_bd; i++) {
  735. bd = &sdmac->desc->bd[i];
  736. if (bd->mode.status & (BD_DONE | BD_RROR))
  737. error = -EIO;
  738. sdmac->desc->chn_real_count += bd->mode.count;
  739. }
  740. if (error)
  741. sdmac->status = DMA_ERROR;
  742. else
  743. sdmac->status = DMA_COMPLETE;
  744. }
  745. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  746. {
  747. struct sdma_engine *sdma = dev_id;
  748. unsigned long stat;
  749. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  750. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  751. /* channel 0 is special and not handled here, see run_channel0() */
  752. stat &= ~1;
  753. while (stat) {
  754. int channel = fls(stat) - 1;
  755. struct sdma_channel *sdmac = &sdma->channel[channel];
  756. struct sdma_desc *desc;
  757. spin_lock(&sdmac->vc.lock);
  758. desc = sdmac->desc;
  759. if (desc) {
  760. if (sdmac->flags & IMX_DMA_SG_LOOP) {
  761. sdma_update_channel_loop(sdmac);
  762. } else {
  763. mxc_sdma_handle_channel_normal(sdmac);
  764. vchan_cookie_complete(&desc->vd);
  765. sdma_start_desc(sdmac);
  766. }
  767. }
  768. spin_unlock(&sdmac->vc.lock);
  769. __clear_bit(channel, &stat);
  770. }
  771. return IRQ_HANDLED;
  772. }
  773. /*
  774. * sets the pc of SDMA script according to the peripheral type
  775. */
  776. static void sdma_get_pc(struct sdma_channel *sdmac,
  777. enum sdma_peripheral_type peripheral_type)
  778. {
  779. struct sdma_engine *sdma = sdmac->sdma;
  780. int per_2_emi = 0, emi_2_per = 0;
  781. /*
  782. * These are needed once we start to support transfers between
  783. * two peripherals or memory-to-memory transfers
  784. */
  785. int per_2_per = 0, emi_2_emi = 0;
  786. sdmac->pc_from_device = 0;
  787. sdmac->pc_to_device = 0;
  788. sdmac->device_to_device = 0;
  789. sdmac->pc_to_pc = 0;
  790. switch (peripheral_type) {
  791. case IMX_DMATYPE_MEMORY:
  792. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  793. break;
  794. case IMX_DMATYPE_DSP:
  795. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  796. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  797. break;
  798. case IMX_DMATYPE_FIRI:
  799. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  800. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  801. break;
  802. case IMX_DMATYPE_UART:
  803. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  804. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  805. break;
  806. case IMX_DMATYPE_UART_SP:
  807. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  808. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  809. break;
  810. case IMX_DMATYPE_ATA:
  811. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  812. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  813. break;
  814. case IMX_DMATYPE_CSPI:
  815. case IMX_DMATYPE_EXT:
  816. case IMX_DMATYPE_SSI:
  817. case IMX_DMATYPE_SAI:
  818. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  819. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  820. break;
  821. case IMX_DMATYPE_SSI_DUAL:
  822. per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
  823. emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
  824. break;
  825. case IMX_DMATYPE_SSI_SP:
  826. case IMX_DMATYPE_MMC:
  827. case IMX_DMATYPE_SDHC:
  828. case IMX_DMATYPE_CSPI_SP:
  829. case IMX_DMATYPE_ESAI:
  830. case IMX_DMATYPE_MSHC_SP:
  831. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  832. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  833. break;
  834. case IMX_DMATYPE_ASRC:
  835. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  836. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  837. per_2_per = sdma->script_addrs->per_2_per_addr;
  838. break;
  839. case IMX_DMATYPE_ASRC_SP:
  840. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  841. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  842. per_2_per = sdma->script_addrs->per_2_per_addr;
  843. break;
  844. case IMX_DMATYPE_MSHC:
  845. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  846. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  847. break;
  848. case IMX_DMATYPE_CCM:
  849. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  850. break;
  851. case IMX_DMATYPE_SPDIF:
  852. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  853. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  854. break;
  855. case IMX_DMATYPE_IPU_MEMORY:
  856. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  857. break;
  858. default:
  859. break;
  860. }
  861. sdmac->pc_from_device = per_2_emi;
  862. sdmac->pc_to_device = emi_2_per;
  863. sdmac->device_to_device = per_2_per;
  864. sdmac->pc_to_pc = emi_2_emi;
  865. }
  866. static int sdma_load_context(struct sdma_channel *sdmac)
  867. {
  868. struct sdma_engine *sdma = sdmac->sdma;
  869. int channel = sdmac->channel;
  870. int load_address;
  871. struct sdma_context_data *context = sdma->context;
  872. struct sdma_buffer_descriptor *bd0 = sdma->bd0;
  873. int ret;
  874. unsigned long flags;
  875. if (sdmac->direction == DMA_DEV_TO_MEM)
  876. load_address = sdmac->pc_from_device;
  877. else if (sdmac->direction == DMA_DEV_TO_DEV)
  878. load_address = sdmac->device_to_device;
  879. else if (sdmac->direction == DMA_MEM_TO_MEM)
  880. load_address = sdmac->pc_to_pc;
  881. else
  882. load_address = sdmac->pc_to_device;
  883. if (load_address < 0)
  884. return load_address;
  885. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  886. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  887. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  888. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  889. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  890. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  891. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  892. memset(context, 0, sizeof(*context));
  893. context->channel_state.pc = load_address;
  894. /* Send by context the event mask,base address for peripheral
  895. * and watermark level
  896. */
  897. context->gReg[0] = sdmac->event_mask[1];
  898. context->gReg[1] = sdmac->event_mask[0];
  899. context->gReg[2] = sdmac->per_addr;
  900. context->gReg[6] = sdmac->shp_addr;
  901. context->gReg[7] = sdmac->watermark_level;
  902. bd0->mode.command = C0_SETDM;
  903. bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
  904. bd0->mode.count = sizeof(*context) / 4;
  905. bd0->buffer_addr = sdma->context_phys;
  906. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  907. ret = sdma_run_channel0(sdma);
  908. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  909. return ret;
  910. }
  911. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  912. {
  913. return container_of(chan, struct sdma_channel, vc.chan);
  914. }
  915. static int sdma_disable_channel(struct dma_chan *chan)
  916. {
  917. struct sdma_channel *sdmac = to_sdma_chan(chan);
  918. struct sdma_engine *sdma = sdmac->sdma;
  919. int channel = sdmac->channel;
  920. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  921. sdmac->status = DMA_ERROR;
  922. return 0;
  923. }
  924. static void sdma_channel_terminate_work(struct work_struct *work)
  925. {
  926. struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
  927. terminate_worker);
  928. unsigned long flags;
  929. LIST_HEAD(head);
  930. /*
  931. * According to NXP R&D team a delay of one BD SDMA cost time
  932. * (maximum is 1ms) should be added after disable of the channel
  933. * bit, to ensure SDMA core has really been stopped after SDMA
  934. * clients call .device_terminate_all.
  935. */
  936. usleep_range(1000, 2000);
  937. spin_lock_irqsave(&sdmac->vc.lock, flags);
  938. vchan_get_all_descriptors(&sdmac->vc, &head);
  939. sdmac->desc = NULL;
  940. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  941. vchan_dma_desc_free_list(&sdmac->vc, &head);
  942. }
  943. static int sdma_disable_channel_async(struct dma_chan *chan)
  944. {
  945. struct sdma_channel *sdmac = to_sdma_chan(chan);
  946. sdma_disable_channel(chan);
  947. if (sdmac->desc)
  948. schedule_work(&sdmac->terminate_worker);
  949. return 0;
  950. }
  951. static void sdma_channel_synchronize(struct dma_chan *chan)
  952. {
  953. struct sdma_channel *sdmac = to_sdma_chan(chan);
  954. vchan_synchronize(&sdmac->vc);
  955. flush_work(&sdmac->terminate_worker);
  956. }
  957. static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
  958. {
  959. struct sdma_engine *sdma = sdmac->sdma;
  960. int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
  961. int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
  962. set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
  963. set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
  964. if (sdmac->event_id0 > 31)
  965. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
  966. if (sdmac->event_id1 > 31)
  967. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
  968. /*
  969. * If LWML(src_maxburst) > HWML(dst_maxburst), we need
  970. * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
  971. * r0(event_mask[1]) and r1(event_mask[0]).
  972. */
  973. if (lwml > hwml) {
  974. sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
  975. SDMA_WATERMARK_LEVEL_HWML);
  976. sdmac->watermark_level |= hwml;
  977. sdmac->watermark_level |= lwml << 16;
  978. swap(sdmac->event_mask[0], sdmac->event_mask[1]);
  979. }
  980. if (sdmac->per_address2 >= sdma->spba_start_addr &&
  981. sdmac->per_address2 <= sdma->spba_end_addr)
  982. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
  983. if (sdmac->per_address >= sdma->spba_start_addr &&
  984. sdmac->per_address <= sdma->spba_end_addr)
  985. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
  986. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
  987. }
  988. static int sdma_config_channel(struct dma_chan *chan)
  989. {
  990. struct sdma_channel *sdmac = to_sdma_chan(chan);
  991. int ret;
  992. sdma_disable_channel(chan);
  993. sdmac->event_mask[0] = 0;
  994. sdmac->event_mask[1] = 0;
  995. sdmac->shp_addr = 0;
  996. sdmac->per_addr = 0;
  997. switch (sdmac->peripheral_type) {
  998. case IMX_DMATYPE_DSP:
  999. sdma_config_ownership(sdmac, false, true, true);
  1000. break;
  1001. case IMX_DMATYPE_MEMORY:
  1002. sdma_config_ownership(sdmac, false, true, false);
  1003. break;
  1004. default:
  1005. sdma_config_ownership(sdmac, true, true, false);
  1006. break;
  1007. }
  1008. sdma_get_pc(sdmac, sdmac->peripheral_type);
  1009. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  1010. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  1011. /* Handle multiple event channels differently */
  1012. if (sdmac->event_id1) {
  1013. if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
  1014. sdmac->peripheral_type == IMX_DMATYPE_ASRC)
  1015. sdma_set_watermarklevel_for_p2p(sdmac);
  1016. } else
  1017. __set_bit(sdmac->event_id0, sdmac->event_mask);
  1018. /* Address */
  1019. sdmac->shp_addr = sdmac->per_address;
  1020. sdmac->per_addr = sdmac->per_address2;
  1021. } else {
  1022. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  1023. }
  1024. ret = sdma_load_context(sdmac);
  1025. return ret;
  1026. }
  1027. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  1028. unsigned int priority)
  1029. {
  1030. struct sdma_engine *sdma = sdmac->sdma;
  1031. int channel = sdmac->channel;
  1032. if (priority < MXC_SDMA_MIN_PRIORITY
  1033. || priority > MXC_SDMA_MAX_PRIORITY) {
  1034. return -EINVAL;
  1035. }
  1036. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  1037. return 0;
  1038. }
  1039. static int sdma_request_channel0(struct sdma_engine *sdma)
  1040. {
  1041. int ret = -EBUSY;
  1042. sdma->bd0 = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdma->bd0_phys,
  1043. GFP_NOWAIT);
  1044. if (!sdma->bd0) {
  1045. ret = -ENOMEM;
  1046. goto out;
  1047. }
  1048. sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
  1049. sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
  1050. sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
  1051. return 0;
  1052. out:
  1053. return ret;
  1054. }
  1055. static int sdma_alloc_bd(struct sdma_desc *desc)
  1056. {
  1057. u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
  1058. int ret = 0;
  1059. desc->bd = dma_zalloc_coherent(NULL, bd_size, &desc->bd_phys,
  1060. GFP_NOWAIT);
  1061. if (!desc->bd) {
  1062. ret = -ENOMEM;
  1063. goto out;
  1064. }
  1065. out:
  1066. return ret;
  1067. }
  1068. static void sdma_free_bd(struct sdma_desc *desc)
  1069. {
  1070. u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
  1071. dma_free_coherent(NULL, bd_size, desc->bd, desc->bd_phys);
  1072. }
  1073. static void sdma_desc_free(struct virt_dma_desc *vd)
  1074. {
  1075. struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
  1076. sdma_free_bd(desc);
  1077. kfree(desc);
  1078. }
  1079. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  1080. {
  1081. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1082. struct imx_dma_data *data = chan->private;
  1083. struct imx_dma_data mem_data;
  1084. int prio, ret;
  1085. /*
  1086. * MEMCPY may never setup chan->private by filter function such as
  1087. * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
  1088. * Please note in any other slave case, you have to setup chan->private
  1089. * with 'struct imx_dma_data' in your own filter function if you want to
  1090. * request dma channel by dma_request_channel() rather than
  1091. * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
  1092. * to warn you to correct your filter function.
  1093. */
  1094. if (!data) {
  1095. dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
  1096. mem_data.priority = 2;
  1097. mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
  1098. mem_data.dma_request = 0;
  1099. mem_data.dma_request2 = 0;
  1100. data = &mem_data;
  1101. sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
  1102. }
  1103. switch (data->priority) {
  1104. case DMA_PRIO_HIGH:
  1105. prio = 3;
  1106. break;
  1107. case DMA_PRIO_MEDIUM:
  1108. prio = 2;
  1109. break;
  1110. case DMA_PRIO_LOW:
  1111. default:
  1112. prio = 1;
  1113. break;
  1114. }
  1115. sdmac->peripheral_type = data->peripheral_type;
  1116. sdmac->event_id0 = data->dma_request;
  1117. sdmac->event_id1 = data->dma_request2;
  1118. ret = clk_enable(sdmac->sdma->clk_ipg);
  1119. if (ret)
  1120. return ret;
  1121. ret = clk_enable(sdmac->sdma->clk_ahb);
  1122. if (ret)
  1123. goto disable_clk_ipg;
  1124. ret = sdma_set_channel_priority(sdmac, prio);
  1125. if (ret)
  1126. goto disable_clk_ahb;
  1127. return 0;
  1128. disable_clk_ahb:
  1129. clk_disable(sdmac->sdma->clk_ahb);
  1130. disable_clk_ipg:
  1131. clk_disable(sdmac->sdma->clk_ipg);
  1132. return ret;
  1133. }
  1134. static void sdma_free_chan_resources(struct dma_chan *chan)
  1135. {
  1136. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1137. struct sdma_engine *sdma = sdmac->sdma;
  1138. sdma_disable_channel_async(chan);
  1139. sdma_channel_synchronize(chan);
  1140. if (sdmac->event_id0)
  1141. sdma_event_disable(sdmac, sdmac->event_id0);
  1142. if (sdmac->event_id1)
  1143. sdma_event_disable(sdmac, sdmac->event_id1);
  1144. sdmac->event_id0 = 0;
  1145. sdmac->event_id1 = 0;
  1146. sdma_set_channel_priority(sdmac, 0);
  1147. clk_disable(sdma->clk_ipg);
  1148. clk_disable(sdma->clk_ahb);
  1149. }
  1150. static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
  1151. enum dma_transfer_direction direction, u32 bds)
  1152. {
  1153. struct sdma_desc *desc;
  1154. desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
  1155. if (!desc)
  1156. goto err_out;
  1157. sdmac->status = DMA_IN_PROGRESS;
  1158. sdmac->direction = direction;
  1159. sdmac->flags = 0;
  1160. desc->chn_count = 0;
  1161. desc->chn_real_count = 0;
  1162. desc->buf_tail = 0;
  1163. desc->buf_ptail = 0;
  1164. desc->sdmac = sdmac;
  1165. desc->num_bd = bds;
  1166. if (sdma_alloc_bd(desc))
  1167. goto err_desc_out;
  1168. /* No slave_config called in MEMCPY case, so do here */
  1169. if (direction == DMA_MEM_TO_MEM)
  1170. sdma_config_ownership(sdmac, false, true, false);
  1171. if (sdma_load_context(sdmac))
  1172. goto err_desc_out;
  1173. return desc;
  1174. err_desc_out:
  1175. kfree(desc);
  1176. err_out:
  1177. return NULL;
  1178. }
  1179. static struct dma_async_tx_descriptor *sdma_prep_memcpy(
  1180. struct dma_chan *chan, dma_addr_t dma_dst,
  1181. dma_addr_t dma_src, size_t len, unsigned long flags)
  1182. {
  1183. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1184. struct sdma_engine *sdma = sdmac->sdma;
  1185. int channel = sdmac->channel;
  1186. size_t count;
  1187. int i = 0, param;
  1188. struct sdma_buffer_descriptor *bd;
  1189. struct sdma_desc *desc;
  1190. if (!chan || !len)
  1191. return NULL;
  1192. dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
  1193. &dma_src, &dma_dst, len, channel);
  1194. desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
  1195. len / SDMA_BD_MAX_CNT + 1);
  1196. if (!desc)
  1197. return NULL;
  1198. do {
  1199. count = min_t(size_t, len, SDMA_BD_MAX_CNT);
  1200. bd = &desc->bd[i];
  1201. bd->buffer_addr = dma_src;
  1202. bd->ext_buffer_addr = dma_dst;
  1203. bd->mode.count = count;
  1204. desc->chn_count += count;
  1205. bd->mode.command = 0;
  1206. dma_src += count;
  1207. dma_dst += count;
  1208. len -= count;
  1209. i++;
  1210. param = BD_DONE | BD_EXTD | BD_CONT;
  1211. /* last bd */
  1212. if (!len) {
  1213. param |= BD_INTR;
  1214. param |= BD_LAST;
  1215. param &= ~BD_CONT;
  1216. }
  1217. dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
  1218. i, count, bd->buffer_addr,
  1219. param & BD_WRAP ? "wrap" : "",
  1220. param & BD_INTR ? " intr" : "");
  1221. bd->mode.status = param;
  1222. } while (len);
  1223. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1224. }
  1225. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  1226. struct dma_chan *chan, struct scatterlist *sgl,
  1227. unsigned int sg_len, enum dma_transfer_direction direction,
  1228. unsigned long flags, void *context)
  1229. {
  1230. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1231. struct sdma_engine *sdma = sdmac->sdma;
  1232. int i, count;
  1233. int channel = sdmac->channel;
  1234. struct scatterlist *sg;
  1235. struct sdma_desc *desc;
  1236. sdma_config_write(chan, &sdmac->slave_config, direction);
  1237. desc = sdma_transfer_init(sdmac, direction, sg_len);
  1238. if (!desc)
  1239. goto err_out;
  1240. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  1241. sg_len, channel);
  1242. for_each_sg(sgl, sg, sg_len, i) {
  1243. struct sdma_buffer_descriptor *bd = &desc->bd[i];
  1244. int param;
  1245. bd->buffer_addr = sg->dma_address;
  1246. count = sg_dma_len(sg);
  1247. if (count > SDMA_BD_MAX_CNT) {
  1248. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  1249. channel, count, SDMA_BD_MAX_CNT);
  1250. goto err_bd_out;
  1251. }
  1252. bd->mode.count = count;
  1253. desc->chn_count += count;
  1254. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1255. goto err_bd_out;
  1256. switch (sdmac->word_size) {
  1257. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1258. bd->mode.command = 0;
  1259. if (count & 3 || sg->dma_address & 3)
  1260. goto err_bd_out;
  1261. break;
  1262. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1263. bd->mode.command = 2;
  1264. if (count & 1 || sg->dma_address & 1)
  1265. goto err_bd_out;
  1266. break;
  1267. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1268. bd->mode.command = 1;
  1269. break;
  1270. default:
  1271. goto err_bd_out;
  1272. }
  1273. param = BD_DONE | BD_EXTD | BD_CONT;
  1274. if (i + 1 == sg_len) {
  1275. param |= BD_INTR;
  1276. param |= BD_LAST;
  1277. param &= ~BD_CONT;
  1278. }
  1279. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  1280. i, count, (u64)sg->dma_address,
  1281. param & BD_WRAP ? "wrap" : "",
  1282. param & BD_INTR ? " intr" : "");
  1283. bd->mode.status = param;
  1284. }
  1285. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1286. err_bd_out:
  1287. sdma_free_bd(desc);
  1288. kfree(desc);
  1289. err_out:
  1290. sdmac->status = DMA_ERROR;
  1291. return NULL;
  1292. }
  1293. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  1294. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  1295. size_t period_len, enum dma_transfer_direction direction,
  1296. unsigned long flags)
  1297. {
  1298. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1299. struct sdma_engine *sdma = sdmac->sdma;
  1300. int num_periods = buf_len / period_len;
  1301. int channel = sdmac->channel;
  1302. int i = 0, buf = 0;
  1303. struct sdma_desc *desc;
  1304. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  1305. sdma_config_write(chan, &sdmac->slave_config, direction);
  1306. desc = sdma_transfer_init(sdmac, direction, num_periods);
  1307. if (!desc)
  1308. goto err_out;
  1309. desc->period_len = period_len;
  1310. sdmac->flags |= IMX_DMA_SG_LOOP;
  1311. if (period_len > SDMA_BD_MAX_CNT) {
  1312. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
  1313. channel, period_len, SDMA_BD_MAX_CNT);
  1314. goto err_bd_out;
  1315. }
  1316. while (buf < buf_len) {
  1317. struct sdma_buffer_descriptor *bd = &desc->bd[i];
  1318. int param;
  1319. bd->buffer_addr = dma_addr;
  1320. bd->mode.count = period_len;
  1321. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1322. goto err_bd_out;
  1323. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  1324. bd->mode.command = 0;
  1325. else
  1326. bd->mode.command = sdmac->word_size;
  1327. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  1328. if (i + 1 == num_periods)
  1329. param |= BD_WRAP;
  1330. dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
  1331. i, period_len, (u64)dma_addr,
  1332. param & BD_WRAP ? "wrap" : "",
  1333. param & BD_INTR ? " intr" : "");
  1334. bd->mode.status = param;
  1335. dma_addr += period_len;
  1336. buf += period_len;
  1337. i++;
  1338. }
  1339. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1340. err_bd_out:
  1341. sdma_free_bd(desc);
  1342. kfree(desc);
  1343. err_out:
  1344. sdmac->status = DMA_ERROR;
  1345. return NULL;
  1346. }
  1347. static int sdma_config_write(struct dma_chan *chan,
  1348. struct dma_slave_config *dmaengine_cfg,
  1349. enum dma_transfer_direction direction)
  1350. {
  1351. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1352. if (direction == DMA_DEV_TO_MEM) {
  1353. sdmac->per_address = dmaengine_cfg->src_addr;
  1354. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  1355. dmaengine_cfg->src_addr_width;
  1356. sdmac->word_size = dmaengine_cfg->src_addr_width;
  1357. } else if (direction == DMA_DEV_TO_DEV) {
  1358. sdmac->per_address2 = dmaengine_cfg->src_addr;
  1359. sdmac->per_address = dmaengine_cfg->dst_addr;
  1360. sdmac->watermark_level = dmaengine_cfg->src_maxburst &
  1361. SDMA_WATERMARK_LEVEL_LWML;
  1362. sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
  1363. SDMA_WATERMARK_LEVEL_HWML;
  1364. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1365. } else {
  1366. sdmac->per_address = dmaengine_cfg->dst_addr;
  1367. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  1368. dmaengine_cfg->dst_addr_width;
  1369. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1370. }
  1371. sdmac->direction = direction;
  1372. return sdma_config_channel(chan);
  1373. }
  1374. static int sdma_config(struct dma_chan *chan,
  1375. struct dma_slave_config *dmaengine_cfg)
  1376. {
  1377. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1378. memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
  1379. /* Set ENBLn earlier to make sure dma request triggered after that */
  1380. if (sdmac->event_id0) {
  1381. if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
  1382. return -EINVAL;
  1383. sdma_event_enable(sdmac, sdmac->event_id0);
  1384. }
  1385. if (sdmac->event_id1) {
  1386. if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
  1387. return -EINVAL;
  1388. sdma_event_enable(sdmac, sdmac->event_id1);
  1389. }
  1390. return 0;
  1391. }
  1392. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  1393. dma_cookie_t cookie,
  1394. struct dma_tx_state *txstate)
  1395. {
  1396. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1397. struct sdma_desc *desc;
  1398. u32 residue;
  1399. struct virt_dma_desc *vd;
  1400. enum dma_status ret;
  1401. unsigned long flags;
  1402. ret = dma_cookie_status(chan, cookie, txstate);
  1403. if (ret == DMA_COMPLETE || !txstate)
  1404. return ret;
  1405. spin_lock_irqsave(&sdmac->vc.lock, flags);
  1406. vd = vchan_find_desc(&sdmac->vc, cookie);
  1407. if (vd) {
  1408. desc = to_sdma_desc(&vd->tx);
  1409. if (sdmac->flags & IMX_DMA_SG_LOOP)
  1410. residue = (desc->num_bd - desc->buf_ptail) *
  1411. desc->period_len - desc->chn_real_count;
  1412. else
  1413. residue = desc->chn_count - desc->chn_real_count;
  1414. } else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) {
  1415. residue = sdmac->desc->chn_count - sdmac->desc->chn_real_count;
  1416. } else {
  1417. residue = 0;
  1418. }
  1419. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  1420. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  1421. residue);
  1422. return sdmac->status;
  1423. }
  1424. static void sdma_issue_pending(struct dma_chan *chan)
  1425. {
  1426. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1427. unsigned long flags;
  1428. spin_lock_irqsave(&sdmac->vc.lock, flags);
  1429. if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
  1430. sdma_start_desc(sdmac);
  1431. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  1432. }
  1433. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  1434. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
  1435. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
  1436. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42
  1437. static void sdma_add_scripts(struct sdma_engine *sdma,
  1438. const struct sdma_script_start_addrs *addr)
  1439. {
  1440. s32 *addr_arr = (u32 *)addr;
  1441. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  1442. int i;
  1443. /* use the default firmware in ROM if missing external firmware */
  1444. if (!sdma->script_number)
  1445. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1446. if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
  1447. / sizeof(s32)) {
  1448. dev_err(sdma->dev,
  1449. "SDMA script number %d not match with firmware.\n",
  1450. sdma->script_number);
  1451. return;
  1452. }
  1453. for (i = 0; i < sdma->script_number; i++)
  1454. if (addr_arr[i] > 0)
  1455. saddr_arr[i] = addr_arr[i];
  1456. }
  1457. static void sdma_load_firmware(const struct firmware *fw, void *context)
  1458. {
  1459. struct sdma_engine *sdma = context;
  1460. const struct sdma_firmware_header *header;
  1461. const struct sdma_script_start_addrs *addr;
  1462. unsigned short *ram_code;
  1463. if (!fw)
  1464. return;
  1465. if (fw->size < sizeof(*header))
  1466. goto err_firmware;
  1467. header = (struct sdma_firmware_header *)fw->data;
  1468. if (header->magic != SDMA_FIRMWARE_MAGIC)
  1469. goto err_firmware;
  1470. if (header->ram_code_start + header->ram_code_size > fw->size)
  1471. goto err_firmware;
  1472. switch (header->version_major) {
  1473. case 1:
  1474. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1475. break;
  1476. case 2:
  1477. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
  1478. break;
  1479. case 3:
  1480. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
  1481. break;
  1482. case 4:
  1483. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
  1484. break;
  1485. default:
  1486. dev_err(sdma->dev, "unknown firmware version\n");
  1487. goto err_firmware;
  1488. }
  1489. addr = (void *)header + header->script_addrs_start;
  1490. ram_code = (void *)header + header->ram_code_start;
  1491. clk_enable(sdma->clk_ipg);
  1492. clk_enable(sdma->clk_ahb);
  1493. /* download the RAM image for SDMA */
  1494. sdma_load_script(sdma, ram_code,
  1495. header->ram_code_size,
  1496. addr->ram_code_start_addr);
  1497. clk_disable(sdma->clk_ipg);
  1498. clk_disable(sdma->clk_ahb);
  1499. sdma_add_scripts(sdma, addr);
  1500. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  1501. header->version_major,
  1502. header->version_minor);
  1503. err_firmware:
  1504. release_firmware(fw);
  1505. }
  1506. #define EVENT_REMAP_CELLS 3
  1507. static int sdma_event_remap(struct sdma_engine *sdma)
  1508. {
  1509. struct device_node *np = sdma->dev->of_node;
  1510. struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
  1511. struct property *event_remap;
  1512. struct regmap *gpr;
  1513. char propname[] = "fsl,sdma-event-remap";
  1514. u32 reg, val, shift, num_map, i;
  1515. int ret = 0;
  1516. if (IS_ERR(np) || IS_ERR(gpr_np))
  1517. goto out;
  1518. event_remap = of_find_property(np, propname, NULL);
  1519. num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
  1520. if (!num_map) {
  1521. dev_dbg(sdma->dev, "no event needs to be remapped\n");
  1522. goto out;
  1523. } else if (num_map % EVENT_REMAP_CELLS) {
  1524. dev_err(sdma->dev, "the property %s must modulo %d\n",
  1525. propname, EVENT_REMAP_CELLS);
  1526. ret = -EINVAL;
  1527. goto out;
  1528. }
  1529. gpr = syscon_node_to_regmap(gpr_np);
  1530. if (IS_ERR(gpr)) {
  1531. dev_err(sdma->dev, "failed to get gpr regmap\n");
  1532. ret = PTR_ERR(gpr);
  1533. goto out;
  1534. }
  1535. for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
  1536. ret = of_property_read_u32_index(np, propname, i, &reg);
  1537. if (ret) {
  1538. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1539. propname, i);
  1540. goto out;
  1541. }
  1542. ret = of_property_read_u32_index(np, propname, i + 1, &shift);
  1543. if (ret) {
  1544. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1545. propname, i + 1);
  1546. goto out;
  1547. }
  1548. ret = of_property_read_u32_index(np, propname, i + 2, &val);
  1549. if (ret) {
  1550. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1551. propname, i + 2);
  1552. goto out;
  1553. }
  1554. regmap_update_bits(gpr, reg, BIT(shift), val << shift);
  1555. }
  1556. out:
  1557. if (!IS_ERR(gpr_np))
  1558. of_node_put(gpr_np);
  1559. return ret;
  1560. }
  1561. static int sdma_get_firmware(struct sdma_engine *sdma,
  1562. const char *fw_name)
  1563. {
  1564. int ret;
  1565. ret = request_firmware_nowait(THIS_MODULE,
  1566. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1567. GFP_KERNEL, sdma, sdma_load_firmware);
  1568. return ret;
  1569. }
  1570. static int sdma_init(struct sdma_engine *sdma)
  1571. {
  1572. int i, ret;
  1573. dma_addr_t ccb_phys;
  1574. ret = clk_enable(sdma->clk_ipg);
  1575. if (ret)
  1576. return ret;
  1577. ret = clk_enable(sdma->clk_ahb);
  1578. if (ret)
  1579. goto disable_clk_ipg;
  1580. /* Be sure SDMA has not started yet */
  1581. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1582. sdma->channel_control = dma_alloc_coherent(NULL,
  1583. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1584. sizeof(struct sdma_context_data),
  1585. &ccb_phys, GFP_KERNEL);
  1586. if (!sdma->channel_control) {
  1587. ret = -ENOMEM;
  1588. goto err_dma_alloc;
  1589. }
  1590. sdma->context = (void *)sdma->channel_control +
  1591. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1592. sdma->context_phys = ccb_phys +
  1593. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1594. /* Zero-out the CCB structures array just allocated */
  1595. memset(sdma->channel_control, 0,
  1596. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1597. /* disable all channels */
  1598. for (i = 0; i < sdma->drvdata->num_events; i++)
  1599. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1600. /* All channels have priority 0 */
  1601. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1602. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1603. ret = sdma_request_channel0(sdma);
  1604. if (ret)
  1605. goto err_dma_alloc;
  1606. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1607. /* Set Command Channel (Channel Zero) */
  1608. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1609. /* Set bits of CONFIG register but with static context switching */
  1610. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1611. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1612. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1613. /* Initializes channel's priorities */
  1614. sdma_set_channel_priority(&sdma->channel[0], 7);
  1615. clk_disable(sdma->clk_ipg);
  1616. clk_disable(sdma->clk_ahb);
  1617. return 0;
  1618. err_dma_alloc:
  1619. clk_disable(sdma->clk_ahb);
  1620. disable_clk_ipg:
  1621. clk_disable(sdma->clk_ipg);
  1622. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1623. return ret;
  1624. }
  1625. static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
  1626. {
  1627. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1628. struct imx_dma_data *data = fn_param;
  1629. if (!imx_dma_is_general_purpose(chan))
  1630. return false;
  1631. sdmac->data = *data;
  1632. chan->private = &sdmac->data;
  1633. return true;
  1634. }
  1635. static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
  1636. struct of_dma *ofdma)
  1637. {
  1638. struct sdma_engine *sdma = ofdma->of_dma_data;
  1639. dma_cap_mask_t mask = sdma->dma_device.cap_mask;
  1640. struct imx_dma_data data;
  1641. if (dma_spec->args_count != 3)
  1642. return NULL;
  1643. data.dma_request = dma_spec->args[0];
  1644. data.peripheral_type = dma_spec->args[1];
  1645. data.priority = dma_spec->args[2];
  1646. /*
  1647. * init dma_request2 to zero, which is not used by the dts.
  1648. * For P2P, dma_request2 is init from dma_request_channel(),
  1649. * chan->private will point to the imx_dma_data, and in
  1650. * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
  1651. * be set to sdmac->event_id1.
  1652. */
  1653. data.dma_request2 = 0;
  1654. return dma_request_channel(mask, sdma_filter_fn, &data);
  1655. }
  1656. static int sdma_probe(struct platform_device *pdev)
  1657. {
  1658. const struct of_device_id *of_id =
  1659. of_match_device(sdma_dt_ids, &pdev->dev);
  1660. struct device_node *np = pdev->dev.of_node;
  1661. struct device_node *spba_bus;
  1662. const char *fw_name;
  1663. int ret;
  1664. int irq;
  1665. struct resource *iores;
  1666. struct resource spba_res;
  1667. struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1668. int i;
  1669. struct sdma_engine *sdma;
  1670. s32 *saddr_arr;
  1671. const struct sdma_driver_data *drvdata = NULL;
  1672. if (of_id)
  1673. drvdata = of_id->data;
  1674. else if (pdev->id_entry)
  1675. drvdata = (void *)pdev->id_entry->driver_data;
  1676. if (!drvdata) {
  1677. dev_err(&pdev->dev, "unable to find driver data\n");
  1678. return -EINVAL;
  1679. }
  1680. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1681. if (ret)
  1682. return ret;
  1683. sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
  1684. if (!sdma)
  1685. return -ENOMEM;
  1686. spin_lock_init(&sdma->channel_0_lock);
  1687. sdma->dev = &pdev->dev;
  1688. sdma->drvdata = drvdata;
  1689. irq = platform_get_irq(pdev, 0);
  1690. if (irq < 0)
  1691. return irq;
  1692. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1693. sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
  1694. if (IS_ERR(sdma->regs))
  1695. return PTR_ERR(sdma->regs);
  1696. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1697. if (IS_ERR(sdma->clk_ipg))
  1698. return PTR_ERR(sdma->clk_ipg);
  1699. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1700. if (IS_ERR(sdma->clk_ahb))
  1701. return PTR_ERR(sdma->clk_ahb);
  1702. ret = clk_prepare(sdma->clk_ipg);
  1703. if (ret)
  1704. return ret;
  1705. ret = clk_prepare(sdma->clk_ahb);
  1706. if (ret)
  1707. goto err_clk;
  1708. ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
  1709. sdma);
  1710. if (ret)
  1711. goto err_irq;
  1712. sdma->irq = irq;
  1713. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1714. if (!sdma->script_addrs) {
  1715. ret = -ENOMEM;
  1716. goto err_irq;
  1717. }
  1718. /* initially no scripts available */
  1719. saddr_arr = (s32 *)sdma->script_addrs;
  1720. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1721. saddr_arr[i] = -EINVAL;
  1722. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1723. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1724. dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
  1725. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1726. /* Initialize channel parameters */
  1727. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1728. struct sdma_channel *sdmac = &sdma->channel[i];
  1729. sdmac->sdma = sdma;
  1730. sdmac->channel = i;
  1731. sdmac->vc.desc_free = sdma_desc_free;
  1732. INIT_WORK(&sdmac->terminate_worker,
  1733. sdma_channel_terminate_work);
  1734. /*
  1735. * Add the channel to the DMAC list. Do not add channel 0 though
  1736. * because we need it internally in the SDMA driver. This also means
  1737. * that channel 0 in dmaengine counting matches sdma channel 1.
  1738. */
  1739. if (i)
  1740. vchan_init(&sdmac->vc, &sdma->dma_device);
  1741. }
  1742. ret = sdma_init(sdma);
  1743. if (ret)
  1744. goto err_init;
  1745. ret = sdma_event_remap(sdma);
  1746. if (ret)
  1747. goto err_init;
  1748. if (sdma->drvdata->script_addrs)
  1749. sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
  1750. if (pdata && pdata->script_addrs)
  1751. sdma_add_scripts(sdma, pdata->script_addrs);
  1752. sdma->dma_device.dev = &pdev->dev;
  1753. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1754. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1755. sdma->dma_device.device_tx_status = sdma_tx_status;
  1756. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1757. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1758. sdma->dma_device.device_config = sdma_config;
  1759. sdma->dma_device.device_terminate_all = sdma_disable_channel_async;
  1760. sdma->dma_device.device_synchronize = sdma_channel_synchronize;
  1761. sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
  1762. sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
  1763. sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
  1764. sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1765. sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
  1766. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1767. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1768. dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
  1769. platform_set_drvdata(pdev, sdma);
  1770. ret = dma_async_device_register(&sdma->dma_device);
  1771. if (ret) {
  1772. dev_err(&pdev->dev, "unable to register\n");
  1773. goto err_init;
  1774. }
  1775. if (np) {
  1776. ret = of_dma_controller_register(np, sdma_xlate, sdma);
  1777. if (ret) {
  1778. dev_err(&pdev->dev, "failed to register controller\n");
  1779. goto err_register;
  1780. }
  1781. spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
  1782. ret = of_address_to_resource(spba_bus, 0, &spba_res);
  1783. if (!ret) {
  1784. sdma->spba_start_addr = spba_res.start;
  1785. sdma->spba_end_addr = spba_res.end;
  1786. }
  1787. of_node_put(spba_bus);
  1788. }
  1789. /*
  1790. * Kick off firmware loading as the very last step:
  1791. * attempt to load firmware only if we're not on the error path, because
  1792. * the firmware callback requires a fully functional and allocated sdma
  1793. * instance.
  1794. */
  1795. if (pdata) {
  1796. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1797. if (ret)
  1798. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1799. } else {
  1800. /*
  1801. * Because that device tree does not encode ROM script address,
  1802. * the RAM script in firmware is mandatory for device tree
  1803. * probe, otherwise it fails.
  1804. */
  1805. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1806. &fw_name);
  1807. if (ret) {
  1808. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1809. } else {
  1810. ret = sdma_get_firmware(sdma, fw_name);
  1811. if (ret)
  1812. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1813. }
  1814. }
  1815. return 0;
  1816. err_register:
  1817. dma_async_device_unregister(&sdma->dma_device);
  1818. err_init:
  1819. kfree(sdma->script_addrs);
  1820. err_irq:
  1821. clk_unprepare(sdma->clk_ahb);
  1822. err_clk:
  1823. clk_unprepare(sdma->clk_ipg);
  1824. return ret;
  1825. }
  1826. static int sdma_remove(struct platform_device *pdev)
  1827. {
  1828. struct sdma_engine *sdma = platform_get_drvdata(pdev);
  1829. int i;
  1830. devm_free_irq(&pdev->dev, sdma->irq, sdma);
  1831. dma_async_device_unregister(&sdma->dma_device);
  1832. kfree(sdma->script_addrs);
  1833. clk_unprepare(sdma->clk_ahb);
  1834. clk_unprepare(sdma->clk_ipg);
  1835. /* Kill the tasklet */
  1836. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1837. struct sdma_channel *sdmac = &sdma->channel[i];
  1838. tasklet_kill(&sdmac->vc.task);
  1839. sdma_free_chan_resources(&sdmac->vc.chan);
  1840. }
  1841. platform_set_drvdata(pdev, NULL);
  1842. return 0;
  1843. }
  1844. static struct platform_driver sdma_driver = {
  1845. .driver = {
  1846. .name = "imx-sdma",
  1847. .of_match_table = sdma_dt_ids,
  1848. },
  1849. .id_table = sdma_devtypes,
  1850. .remove = sdma_remove,
  1851. .probe = sdma_probe,
  1852. };
  1853. module_platform_driver(sdma_driver);
  1854. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1855. MODULE_DESCRIPTION("i.MX SDMA driver");
  1856. #if IS_ENABLED(CONFIG_SOC_IMX6Q)
  1857. MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
  1858. #endif
  1859. #if IS_ENABLED(CONFIG_SOC_IMX7D)
  1860. MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
  1861. #endif
  1862. MODULE_LICENSE("GPL");