imx-sdma.c 49 KB

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