imx-sdma.c 50 KB

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