imx-sdma.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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 mostlky 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. * @num_bd max NUM_BD. number of descriptors currently handling
  284. */
  285. struct sdma_channel {
  286. struct sdma_engine *sdma;
  287. unsigned int channel;
  288. enum dma_transfer_direction direction;
  289. enum sdma_peripheral_type peripheral_type;
  290. unsigned int event_id0;
  291. unsigned int event_id1;
  292. enum dma_slave_buswidth word_size;
  293. unsigned int buf_tail;
  294. unsigned int num_bd;
  295. unsigned int period_len;
  296. struct sdma_buffer_descriptor *bd;
  297. dma_addr_t bd_phys;
  298. unsigned int pc_from_device, pc_to_device;
  299. unsigned int device_to_device;
  300. unsigned long flags;
  301. dma_addr_t per_address, per_address2;
  302. unsigned long event_mask[2];
  303. unsigned long watermark_level;
  304. u32 shp_addr, per_addr;
  305. struct dma_chan chan;
  306. spinlock_t lock;
  307. struct dma_async_tx_descriptor desc;
  308. enum dma_status status;
  309. unsigned int chn_count;
  310. unsigned int chn_real_count;
  311. struct tasklet_struct tasklet;
  312. struct imx_dma_data data;
  313. };
  314. #define IMX_DMA_SG_LOOP BIT(0)
  315. #define MAX_DMA_CHANNELS 32
  316. #define MXC_SDMA_DEFAULT_PRIORITY 1
  317. #define MXC_SDMA_MIN_PRIORITY 1
  318. #define MXC_SDMA_MAX_PRIORITY 7
  319. #define SDMA_FIRMWARE_MAGIC 0x414d4453
  320. /**
  321. * struct sdma_firmware_header - Layout of the firmware image
  322. *
  323. * @magic "SDMA"
  324. * @version_major increased whenever layout of struct sdma_script_start_addrs
  325. * changes.
  326. * @version_minor firmware minor version (for binary compatible changes)
  327. * @script_addrs_start offset of struct sdma_script_start_addrs in this image
  328. * @num_script_addrs Number of script addresses in this image
  329. * @ram_code_start offset of SDMA ram image in this firmware image
  330. * @ram_code_size size of SDMA ram image
  331. * @script_addrs Stores the start address of the SDMA scripts
  332. * (in SDMA memory space)
  333. */
  334. struct sdma_firmware_header {
  335. u32 magic;
  336. u32 version_major;
  337. u32 version_minor;
  338. u32 script_addrs_start;
  339. u32 num_script_addrs;
  340. u32 ram_code_start;
  341. u32 ram_code_size;
  342. };
  343. struct sdma_driver_data {
  344. int chnenbl0;
  345. int num_events;
  346. struct sdma_script_start_addrs *script_addrs;
  347. };
  348. struct sdma_engine {
  349. struct device *dev;
  350. struct device_dma_parameters dma_parms;
  351. struct sdma_channel channel[MAX_DMA_CHANNELS];
  352. struct sdma_channel_control *channel_control;
  353. void __iomem *regs;
  354. struct sdma_context_data *context;
  355. dma_addr_t context_phys;
  356. struct dma_device dma_device;
  357. struct clk *clk_ipg;
  358. struct clk *clk_ahb;
  359. spinlock_t channel_0_lock;
  360. u32 script_number;
  361. struct sdma_script_start_addrs *script_addrs;
  362. const struct sdma_driver_data *drvdata;
  363. u32 spba_start_addr;
  364. u32 spba_end_addr;
  365. unsigned int irq;
  366. };
  367. static struct sdma_driver_data sdma_imx31 = {
  368. .chnenbl0 = SDMA_CHNENBL0_IMX31,
  369. .num_events = 32,
  370. };
  371. static struct sdma_script_start_addrs sdma_script_imx25 = {
  372. .ap_2_ap_addr = 729,
  373. .uart_2_mcu_addr = 904,
  374. .per_2_app_addr = 1255,
  375. .mcu_2_app_addr = 834,
  376. .uartsh_2_mcu_addr = 1120,
  377. .per_2_shp_addr = 1329,
  378. .mcu_2_shp_addr = 1048,
  379. .ata_2_mcu_addr = 1560,
  380. .mcu_2_ata_addr = 1479,
  381. .app_2_per_addr = 1189,
  382. .app_2_mcu_addr = 770,
  383. .shp_2_per_addr = 1407,
  384. .shp_2_mcu_addr = 979,
  385. };
  386. static struct sdma_driver_data sdma_imx25 = {
  387. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  388. .num_events = 48,
  389. .script_addrs = &sdma_script_imx25,
  390. };
  391. static struct sdma_driver_data sdma_imx35 = {
  392. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  393. .num_events = 48,
  394. };
  395. static struct sdma_script_start_addrs sdma_script_imx51 = {
  396. .ap_2_ap_addr = 642,
  397. .uart_2_mcu_addr = 817,
  398. .mcu_2_app_addr = 747,
  399. .mcu_2_shp_addr = 961,
  400. .ata_2_mcu_addr = 1473,
  401. .mcu_2_ata_addr = 1392,
  402. .app_2_per_addr = 1033,
  403. .app_2_mcu_addr = 683,
  404. .shp_2_per_addr = 1251,
  405. .shp_2_mcu_addr = 892,
  406. };
  407. static struct sdma_driver_data sdma_imx51 = {
  408. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  409. .num_events = 48,
  410. .script_addrs = &sdma_script_imx51,
  411. };
  412. static struct sdma_script_start_addrs sdma_script_imx53 = {
  413. .ap_2_ap_addr = 642,
  414. .app_2_mcu_addr = 683,
  415. .mcu_2_app_addr = 747,
  416. .uart_2_mcu_addr = 817,
  417. .shp_2_mcu_addr = 891,
  418. .mcu_2_shp_addr = 960,
  419. .uartsh_2_mcu_addr = 1032,
  420. .spdif_2_mcu_addr = 1100,
  421. .mcu_2_spdif_addr = 1134,
  422. .firi_2_mcu_addr = 1193,
  423. .mcu_2_firi_addr = 1290,
  424. };
  425. static struct sdma_driver_data sdma_imx53 = {
  426. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  427. .num_events = 48,
  428. .script_addrs = &sdma_script_imx53,
  429. };
  430. static struct sdma_script_start_addrs sdma_script_imx6q = {
  431. .ap_2_ap_addr = 642,
  432. .uart_2_mcu_addr = 817,
  433. .mcu_2_app_addr = 747,
  434. .per_2_per_addr = 6331,
  435. .uartsh_2_mcu_addr = 1032,
  436. .mcu_2_shp_addr = 960,
  437. .app_2_mcu_addr = 683,
  438. .shp_2_mcu_addr = 891,
  439. .spdif_2_mcu_addr = 1100,
  440. .mcu_2_spdif_addr = 1134,
  441. };
  442. static struct sdma_driver_data sdma_imx6q = {
  443. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  444. .num_events = 48,
  445. .script_addrs = &sdma_script_imx6q,
  446. };
  447. static const struct platform_device_id sdma_devtypes[] = {
  448. {
  449. .name = "imx25-sdma",
  450. .driver_data = (unsigned long)&sdma_imx25,
  451. }, {
  452. .name = "imx31-sdma",
  453. .driver_data = (unsigned long)&sdma_imx31,
  454. }, {
  455. .name = "imx35-sdma",
  456. .driver_data = (unsigned long)&sdma_imx35,
  457. }, {
  458. .name = "imx51-sdma",
  459. .driver_data = (unsigned long)&sdma_imx51,
  460. }, {
  461. .name = "imx53-sdma",
  462. .driver_data = (unsigned long)&sdma_imx53,
  463. }, {
  464. .name = "imx6q-sdma",
  465. .driver_data = (unsigned long)&sdma_imx6q,
  466. }, {
  467. /* sentinel */
  468. }
  469. };
  470. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  471. static const struct of_device_id sdma_dt_ids[] = {
  472. { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
  473. { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
  474. { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
  475. { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
  476. { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
  477. { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
  478. { /* sentinel */ }
  479. };
  480. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  481. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  482. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  483. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  484. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  485. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  486. {
  487. u32 chnenbl0 = sdma->drvdata->chnenbl0;
  488. return chnenbl0 + event * 4;
  489. }
  490. static int sdma_config_ownership(struct sdma_channel *sdmac,
  491. bool event_override, bool mcu_override, bool dsp_override)
  492. {
  493. struct sdma_engine *sdma = sdmac->sdma;
  494. int channel = sdmac->channel;
  495. unsigned long evt, mcu, dsp;
  496. if (event_override && mcu_override && dsp_override)
  497. return -EINVAL;
  498. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  499. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  500. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  501. if (dsp_override)
  502. __clear_bit(channel, &dsp);
  503. else
  504. __set_bit(channel, &dsp);
  505. if (event_override)
  506. __clear_bit(channel, &evt);
  507. else
  508. __set_bit(channel, &evt);
  509. if (mcu_override)
  510. __clear_bit(channel, &mcu);
  511. else
  512. __set_bit(channel, &mcu);
  513. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  514. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  515. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  516. return 0;
  517. }
  518. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  519. {
  520. writel(BIT(channel), sdma->regs + SDMA_H_START);
  521. }
  522. /*
  523. * sdma_run_channel0 - run a channel and wait till it's done
  524. */
  525. static int sdma_run_channel0(struct sdma_engine *sdma)
  526. {
  527. int ret;
  528. u32 reg;
  529. sdma_enable_channel(sdma, 0);
  530. ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
  531. reg, !(reg & 1), 1, 500);
  532. if (ret)
  533. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  534. /* Set bits of CONFIG register with dynamic context switching */
  535. if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
  536. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  537. return ret;
  538. }
  539. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  540. u32 address)
  541. {
  542. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  543. void *buf_virt;
  544. dma_addr_t buf_phys;
  545. int ret;
  546. unsigned long flags;
  547. buf_virt = dma_alloc_coherent(NULL,
  548. size,
  549. &buf_phys, GFP_KERNEL);
  550. if (!buf_virt) {
  551. return -ENOMEM;
  552. }
  553. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  554. bd0->mode.command = C0_SETPM;
  555. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  556. bd0->mode.count = size / 2;
  557. bd0->buffer_addr = buf_phys;
  558. bd0->ext_buffer_addr = address;
  559. memcpy(buf_virt, buf, size);
  560. ret = sdma_run_channel0(sdma);
  561. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  562. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  563. return ret;
  564. }
  565. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  566. {
  567. struct sdma_engine *sdma = sdmac->sdma;
  568. int channel = sdmac->channel;
  569. unsigned long val;
  570. u32 chnenbl = chnenbl_ofs(sdma, event);
  571. val = readl_relaxed(sdma->regs + chnenbl);
  572. __set_bit(channel, &val);
  573. writel_relaxed(val, sdma->regs + chnenbl);
  574. }
  575. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  576. {
  577. struct sdma_engine *sdma = sdmac->sdma;
  578. int channel = sdmac->channel;
  579. u32 chnenbl = chnenbl_ofs(sdma, event);
  580. unsigned long val;
  581. val = readl_relaxed(sdma->regs + chnenbl);
  582. __clear_bit(channel, &val);
  583. writel_relaxed(val, sdma->regs + chnenbl);
  584. }
  585. static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
  586. {
  587. if (sdmac->desc.callback)
  588. sdmac->desc.callback(sdmac->desc.callback_param);
  589. }
  590. static void sdma_update_channel_loop(struct sdma_channel *sdmac)
  591. {
  592. struct sdma_buffer_descriptor *bd;
  593. /*
  594. * loop mode. Iterate over descriptors, re-setup them and
  595. * call callback function.
  596. */
  597. while (1) {
  598. bd = &sdmac->bd[sdmac->buf_tail];
  599. if (bd->mode.status & BD_DONE)
  600. break;
  601. if (bd->mode.status & BD_RROR)
  602. sdmac->status = DMA_ERROR;
  603. bd->mode.status |= BD_DONE;
  604. sdmac->buf_tail++;
  605. sdmac->buf_tail %= sdmac->num_bd;
  606. }
  607. }
  608. static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
  609. {
  610. struct sdma_buffer_descriptor *bd;
  611. int i, error = 0;
  612. sdmac->chn_real_count = 0;
  613. /*
  614. * non loop mode. Iterate over all descriptors, collect
  615. * errors and call callback function
  616. */
  617. for (i = 0; i < sdmac->num_bd; i++) {
  618. bd = &sdmac->bd[i];
  619. if (bd->mode.status & (BD_DONE | BD_RROR))
  620. error = -EIO;
  621. sdmac->chn_real_count += bd->mode.count;
  622. }
  623. if (error)
  624. sdmac->status = DMA_ERROR;
  625. else
  626. sdmac->status = DMA_COMPLETE;
  627. dma_cookie_complete(&sdmac->desc);
  628. if (sdmac->desc.callback)
  629. sdmac->desc.callback(sdmac->desc.callback_param);
  630. }
  631. static void sdma_tasklet(unsigned long data)
  632. {
  633. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  634. if (sdmac->flags & IMX_DMA_SG_LOOP)
  635. sdma_handle_channel_loop(sdmac);
  636. else
  637. mxc_sdma_handle_channel_normal(sdmac);
  638. }
  639. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  640. {
  641. struct sdma_engine *sdma = dev_id;
  642. unsigned long stat;
  643. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  644. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  645. /* channel 0 is special and not handled here, see run_channel0() */
  646. stat &= ~1;
  647. while (stat) {
  648. int channel = fls(stat) - 1;
  649. struct sdma_channel *sdmac = &sdma->channel[channel];
  650. if (sdmac->flags & IMX_DMA_SG_LOOP)
  651. sdma_update_channel_loop(sdmac);
  652. tasklet_schedule(&sdmac->tasklet);
  653. __clear_bit(channel, &stat);
  654. }
  655. return IRQ_HANDLED;
  656. }
  657. /*
  658. * sets the pc of SDMA script according to the peripheral type
  659. */
  660. static void sdma_get_pc(struct sdma_channel *sdmac,
  661. enum sdma_peripheral_type peripheral_type)
  662. {
  663. struct sdma_engine *sdma = sdmac->sdma;
  664. int per_2_emi = 0, emi_2_per = 0;
  665. /*
  666. * These are needed once we start to support transfers between
  667. * two peripherals or memory-to-memory transfers
  668. */
  669. int per_2_per = 0;
  670. sdmac->pc_from_device = 0;
  671. sdmac->pc_to_device = 0;
  672. sdmac->device_to_device = 0;
  673. switch (peripheral_type) {
  674. case IMX_DMATYPE_MEMORY:
  675. break;
  676. case IMX_DMATYPE_DSP:
  677. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  678. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  679. break;
  680. case IMX_DMATYPE_FIRI:
  681. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  682. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  683. break;
  684. case IMX_DMATYPE_UART:
  685. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  686. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  687. break;
  688. case IMX_DMATYPE_UART_SP:
  689. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  690. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  691. break;
  692. case IMX_DMATYPE_ATA:
  693. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  694. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  695. break;
  696. case IMX_DMATYPE_CSPI:
  697. case IMX_DMATYPE_EXT:
  698. case IMX_DMATYPE_SSI:
  699. case IMX_DMATYPE_SAI:
  700. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  701. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  702. break;
  703. case IMX_DMATYPE_SSI_DUAL:
  704. per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
  705. emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
  706. break;
  707. case IMX_DMATYPE_SSI_SP:
  708. case IMX_DMATYPE_MMC:
  709. case IMX_DMATYPE_SDHC:
  710. case IMX_DMATYPE_CSPI_SP:
  711. case IMX_DMATYPE_ESAI:
  712. case IMX_DMATYPE_MSHC_SP:
  713. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  714. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  715. break;
  716. case IMX_DMATYPE_ASRC:
  717. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  718. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  719. per_2_per = sdma->script_addrs->per_2_per_addr;
  720. break;
  721. case IMX_DMATYPE_ASRC_SP:
  722. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  723. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  724. per_2_per = sdma->script_addrs->per_2_per_addr;
  725. break;
  726. case IMX_DMATYPE_MSHC:
  727. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  728. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  729. break;
  730. case IMX_DMATYPE_CCM:
  731. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  732. break;
  733. case IMX_DMATYPE_SPDIF:
  734. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  735. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  736. break;
  737. case IMX_DMATYPE_IPU_MEMORY:
  738. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  739. break;
  740. default:
  741. break;
  742. }
  743. sdmac->pc_from_device = per_2_emi;
  744. sdmac->pc_to_device = emi_2_per;
  745. sdmac->device_to_device = per_2_per;
  746. }
  747. static int sdma_load_context(struct sdma_channel *sdmac)
  748. {
  749. struct sdma_engine *sdma = sdmac->sdma;
  750. int channel = sdmac->channel;
  751. int load_address;
  752. struct sdma_context_data *context = sdma->context;
  753. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  754. int ret;
  755. unsigned long flags;
  756. if (sdmac->direction == DMA_DEV_TO_MEM)
  757. load_address = sdmac->pc_from_device;
  758. else if (sdmac->direction == DMA_DEV_TO_DEV)
  759. load_address = sdmac->device_to_device;
  760. else
  761. load_address = sdmac->pc_to_device;
  762. if (load_address < 0)
  763. return load_address;
  764. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  765. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  766. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  767. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  768. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  769. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  770. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  771. memset(context, 0, sizeof(*context));
  772. context->channel_state.pc = load_address;
  773. /* Send by context the event mask,base address for peripheral
  774. * and watermark level
  775. */
  776. context->gReg[0] = sdmac->event_mask[1];
  777. context->gReg[1] = sdmac->event_mask[0];
  778. context->gReg[2] = sdmac->per_addr;
  779. context->gReg[6] = sdmac->shp_addr;
  780. context->gReg[7] = sdmac->watermark_level;
  781. bd0->mode.command = C0_SETDM;
  782. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  783. bd0->mode.count = sizeof(*context) / 4;
  784. bd0->buffer_addr = sdma->context_phys;
  785. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  786. ret = sdma_run_channel0(sdma);
  787. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  788. return ret;
  789. }
  790. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  791. {
  792. return container_of(chan, struct sdma_channel, chan);
  793. }
  794. static int sdma_disable_channel(struct dma_chan *chan)
  795. {
  796. struct sdma_channel *sdmac = to_sdma_chan(chan);
  797. struct sdma_engine *sdma = sdmac->sdma;
  798. int channel = sdmac->channel;
  799. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  800. sdmac->status = DMA_ERROR;
  801. return 0;
  802. }
  803. static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
  804. {
  805. struct sdma_engine *sdma = sdmac->sdma;
  806. int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
  807. int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
  808. set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
  809. set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
  810. if (sdmac->event_id0 > 31)
  811. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
  812. if (sdmac->event_id1 > 31)
  813. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
  814. /*
  815. * If LWML(src_maxburst) > HWML(dst_maxburst), we need
  816. * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
  817. * r0(event_mask[1]) and r1(event_mask[0]).
  818. */
  819. if (lwml > hwml) {
  820. sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
  821. SDMA_WATERMARK_LEVEL_HWML);
  822. sdmac->watermark_level |= hwml;
  823. sdmac->watermark_level |= lwml << 16;
  824. swap(sdmac->event_mask[0], sdmac->event_mask[1]);
  825. }
  826. if (sdmac->per_address2 >= sdma->spba_start_addr &&
  827. sdmac->per_address2 <= sdma->spba_end_addr)
  828. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
  829. if (sdmac->per_address >= sdma->spba_start_addr &&
  830. sdmac->per_address <= sdma->spba_end_addr)
  831. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
  832. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
  833. }
  834. static int sdma_config_channel(struct dma_chan *chan)
  835. {
  836. struct sdma_channel *sdmac = to_sdma_chan(chan);
  837. int ret;
  838. sdma_disable_channel(chan);
  839. sdmac->event_mask[0] = 0;
  840. sdmac->event_mask[1] = 0;
  841. sdmac->shp_addr = 0;
  842. sdmac->per_addr = 0;
  843. if (sdmac->event_id0) {
  844. if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
  845. return -EINVAL;
  846. sdma_event_enable(sdmac, sdmac->event_id0);
  847. }
  848. if (sdmac->event_id1) {
  849. if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
  850. return -EINVAL;
  851. sdma_event_enable(sdmac, sdmac->event_id1);
  852. }
  853. switch (sdmac->peripheral_type) {
  854. case IMX_DMATYPE_DSP:
  855. sdma_config_ownership(sdmac, false, true, true);
  856. break;
  857. case IMX_DMATYPE_MEMORY:
  858. sdma_config_ownership(sdmac, false, true, false);
  859. break;
  860. default:
  861. sdma_config_ownership(sdmac, true, true, false);
  862. break;
  863. }
  864. sdma_get_pc(sdmac, sdmac->peripheral_type);
  865. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  866. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  867. /* Handle multiple event channels differently */
  868. if (sdmac->event_id1) {
  869. if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
  870. sdmac->peripheral_type == IMX_DMATYPE_ASRC)
  871. sdma_set_watermarklevel_for_p2p(sdmac);
  872. } else
  873. __set_bit(sdmac->event_id0, sdmac->event_mask);
  874. /* Address */
  875. sdmac->shp_addr = sdmac->per_address;
  876. sdmac->per_addr = sdmac->per_address2;
  877. } else {
  878. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  879. }
  880. ret = sdma_load_context(sdmac);
  881. return ret;
  882. }
  883. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  884. unsigned int priority)
  885. {
  886. struct sdma_engine *sdma = sdmac->sdma;
  887. int channel = sdmac->channel;
  888. if (priority < MXC_SDMA_MIN_PRIORITY
  889. || priority > MXC_SDMA_MAX_PRIORITY) {
  890. return -EINVAL;
  891. }
  892. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  893. return 0;
  894. }
  895. static int sdma_request_channel(struct sdma_channel *sdmac)
  896. {
  897. struct sdma_engine *sdma = sdmac->sdma;
  898. int channel = sdmac->channel;
  899. int ret = -EBUSY;
  900. sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
  901. GFP_KERNEL);
  902. if (!sdmac->bd) {
  903. ret = -ENOMEM;
  904. goto out;
  905. }
  906. sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
  907. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  908. sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
  909. return 0;
  910. out:
  911. return ret;
  912. }
  913. static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
  914. {
  915. unsigned long flags;
  916. struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
  917. dma_cookie_t cookie;
  918. spin_lock_irqsave(&sdmac->lock, flags);
  919. cookie = dma_cookie_assign(tx);
  920. spin_unlock_irqrestore(&sdmac->lock, flags);
  921. return cookie;
  922. }
  923. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  924. {
  925. struct sdma_channel *sdmac = to_sdma_chan(chan);
  926. struct imx_dma_data *data = chan->private;
  927. int prio, ret;
  928. if (!data)
  929. return -EINVAL;
  930. switch (data->priority) {
  931. case DMA_PRIO_HIGH:
  932. prio = 3;
  933. break;
  934. case DMA_PRIO_MEDIUM:
  935. prio = 2;
  936. break;
  937. case DMA_PRIO_LOW:
  938. default:
  939. prio = 1;
  940. break;
  941. }
  942. sdmac->peripheral_type = data->peripheral_type;
  943. sdmac->event_id0 = data->dma_request;
  944. sdmac->event_id1 = data->dma_request2;
  945. ret = clk_enable(sdmac->sdma->clk_ipg);
  946. if (ret)
  947. return ret;
  948. ret = clk_enable(sdmac->sdma->clk_ahb);
  949. if (ret)
  950. goto disable_clk_ipg;
  951. ret = sdma_request_channel(sdmac);
  952. if (ret)
  953. goto disable_clk_ahb;
  954. ret = sdma_set_channel_priority(sdmac, prio);
  955. if (ret)
  956. goto disable_clk_ahb;
  957. dma_async_tx_descriptor_init(&sdmac->desc, chan);
  958. sdmac->desc.tx_submit = sdma_tx_submit;
  959. /* txd.flags will be overwritten in prep funcs */
  960. sdmac->desc.flags = DMA_CTRL_ACK;
  961. return 0;
  962. disable_clk_ahb:
  963. clk_disable(sdmac->sdma->clk_ahb);
  964. disable_clk_ipg:
  965. clk_disable(sdmac->sdma->clk_ipg);
  966. return ret;
  967. }
  968. static void sdma_free_chan_resources(struct dma_chan *chan)
  969. {
  970. struct sdma_channel *sdmac = to_sdma_chan(chan);
  971. struct sdma_engine *sdma = sdmac->sdma;
  972. sdma_disable_channel(chan);
  973. if (sdmac->event_id0)
  974. sdma_event_disable(sdmac, sdmac->event_id0);
  975. if (sdmac->event_id1)
  976. sdma_event_disable(sdmac, sdmac->event_id1);
  977. sdmac->event_id0 = 0;
  978. sdmac->event_id1 = 0;
  979. sdma_set_channel_priority(sdmac, 0);
  980. dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
  981. clk_disable(sdma->clk_ipg);
  982. clk_disable(sdma->clk_ahb);
  983. }
  984. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  985. struct dma_chan *chan, struct scatterlist *sgl,
  986. unsigned int sg_len, enum dma_transfer_direction direction,
  987. unsigned long flags, void *context)
  988. {
  989. struct sdma_channel *sdmac = to_sdma_chan(chan);
  990. struct sdma_engine *sdma = sdmac->sdma;
  991. int ret, i, count;
  992. int channel = sdmac->channel;
  993. struct scatterlist *sg;
  994. if (sdmac->status == DMA_IN_PROGRESS)
  995. return NULL;
  996. sdmac->status = DMA_IN_PROGRESS;
  997. sdmac->flags = 0;
  998. sdmac->buf_tail = 0;
  999. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  1000. sg_len, channel);
  1001. sdmac->direction = direction;
  1002. ret = sdma_load_context(sdmac);
  1003. if (ret)
  1004. goto err_out;
  1005. if (sg_len > NUM_BD) {
  1006. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  1007. channel, sg_len, NUM_BD);
  1008. ret = -EINVAL;
  1009. goto err_out;
  1010. }
  1011. sdmac->chn_count = 0;
  1012. for_each_sg(sgl, sg, sg_len, i) {
  1013. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  1014. int param;
  1015. bd->buffer_addr = sg->dma_address;
  1016. count = sg_dma_len(sg);
  1017. if (count > 0xffff) {
  1018. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  1019. channel, count, 0xffff);
  1020. ret = -EINVAL;
  1021. goto err_out;
  1022. }
  1023. bd->mode.count = count;
  1024. sdmac->chn_count += count;
  1025. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
  1026. ret = -EINVAL;
  1027. goto err_out;
  1028. }
  1029. switch (sdmac->word_size) {
  1030. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1031. bd->mode.command = 0;
  1032. if (count & 3 || sg->dma_address & 3)
  1033. return NULL;
  1034. break;
  1035. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1036. bd->mode.command = 2;
  1037. if (count & 1 || sg->dma_address & 1)
  1038. return NULL;
  1039. break;
  1040. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1041. bd->mode.command = 1;
  1042. break;
  1043. default:
  1044. return NULL;
  1045. }
  1046. param = BD_DONE | BD_EXTD | BD_CONT;
  1047. if (i + 1 == sg_len) {
  1048. param |= BD_INTR;
  1049. param |= BD_LAST;
  1050. param &= ~BD_CONT;
  1051. }
  1052. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  1053. i, count, (u64)sg->dma_address,
  1054. param & BD_WRAP ? "wrap" : "",
  1055. param & BD_INTR ? " intr" : "");
  1056. bd->mode.status = param;
  1057. }
  1058. sdmac->num_bd = sg_len;
  1059. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  1060. return &sdmac->desc;
  1061. err_out:
  1062. sdmac->status = DMA_ERROR;
  1063. return NULL;
  1064. }
  1065. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  1066. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  1067. size_t period_len, enum dma_transfer_direction direction,
  1068. unsigned long flags)
  1069. {
  1070. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1071. struct sdma_engine *sdma = sdmac->sdma;
  1072. int num_periods = buf_len / period_len;
  1073. int channel = sdmac->channel;
  1074. int ret, i = 0, buf = 0;
  1075. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  1076. if (sdmac->status == DMA_IN_PROGRESS)
  1077. return NULL;
  1078. sdmac->status = DMA_IN_PROGRESS;
  1079. sdmac->buf_tail = 0;
  1080. sdmac->period_len = period_len;
  1081. sdmac->flags |= IMX_DMA_SG_LOOP;
  1082. sdmac->direction = direction;
  1083. ret = sdma_load_context(sdmac);
  1084. if (ret)
  1085. goto err_out;
  1086. if (num_periods > NUM_BD) {
  1087. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  1088. channel, num_periods, NUM_BD);
  1089. goto err_out;
  1090. }
  1091. if (period_len > 0xffff) {
  1092. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
  1093. channel, period_len, 0xffff);
  1094. goto err_out;
  1095. }
  1096. while (buf < buf_len) {
  1097. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  1098. int param;
  1099. bd->buffer_addr = dma_addr;
  1100. bd->mode.count = period_len;
  1101. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1102. goto err_out;
  1103. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  1104. bd->mode.command = 0;
  1105. else
  1106. bd->mode.command = sdmac->word_size;
  1107. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  1108. if (i + 1 == num_periods)
  1109. param |= BD_WRAP;
  1110. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  1111. i, period_len, (u64)dma_addr,
  1112. param & BD_WRAP ? "wrap" : "",
  1113. param & BD_INTR ? " intr" : "");
  1114. bd->mode.status = param;
  1115. dma_addr += period_len;
  1116. buf += period_len;
  1117. i++;
  1118. }
  1119. sdmac->num_bd = num_periods;
  1120. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  1121. return &sdmac->desc;
  1122. err_out:
  1123. sdmac->status = DMA_ERROR;
  1124. return NULL;
  1125. }
  1126. static int sdma_config(struct dma_chan *chan,
  1127. struct dma_slave_config *dmaengine_cfg)
  1128. {
  1129. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1130. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  1131. sdmac->per_address = dmaengine_cfg->src_addr;
  1132. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  1133. dmaengine_cfg->src_addr_width;
  1134. sdmac->word_size = dmaengine_cfg->src_addr_width;
  1135. } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
  1136. sdmac->per_address2 = dmaengine_cfg->src_addr;
  1137. sdmac->per_address = dmaengine_cfg->dst_addr;
  1138. sdmac->watermark_level = dmaengine_cfg->src_maxburst &
  1139. SDMA_WATERMARK_LEVEL_LWML;
  1140. sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
  1141. SDMA_WATERMARK_LEVEL_HWML;
  1142. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1143. } else {
  1144. sdmac->per_address = dmaengine_cfg->dst_addr;
  1145. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  1146. dmaengine_cfg->dst_addr_width;
  1147. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1148. }
  1149. sdmac->direction = dmaengine_cfg->direction;
  1150. return sdma_config_channel(chan);
  1151. }
  1152. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  1153. dma_cookie_t cookie,
  1154. struct dma_tx_state *txstate)
  1155. {
  1156. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1157. u32 residue;
  1158. if (sdmac->flags & IMX_DMA_SG_LOOP)
  1159. residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;
  1160. else
  1161. residue = sdmac->chn_count - sdmac->chn_real_count;
  1162. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  1163. residue);
  1164. return sdmac->status;
  1165. }
  1166. static void sdma_issue_pending(struct dma_chan *chan)
  1167. {
  1168. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1169. struct sdma_engine *sdma = sdmac->sdma;
  1170. if (sdmac->status == DMA_IN_PROGRESS)
  1171. sdma_enable_channel(sdma, sdmac->channel);
  1172. }
  1173. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  1174. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
  1175. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
  1176. static void sdma_add_scripts(struct sdma_engine *sdma,
  1177. const struct sdma_script_start_addrs *addr)
  1178. {
  1179. s32 *addr_arr = (u32 *)addr;
  1180. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  1181. int i;
  1182. /* use the default firmware in ROM if missing external firmware */
  1183. if (!sdma->script_number)
  1184. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1185. for (i = 0; i < sdma->script_number; i++)
  1186. if (addr_arr[i] > 0)
  1187. saddr_arr[i] = addr_arr[i];
  1188. }
  1189. static void sdma_load_firmware(const struct firmware *fw, void *context)
  1190. {
  1191. struct sdma_engine *sdma = context;
  1192. const struct sdma_firmware_header *header;
  1193. const struct sdma_script_start_addrs *addr;
  1194. unsigned short *ram_code;
  1195. if (!fw) {
  1196. dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
  1197. /* In this case we just use the ROM firmware. */
  1198. return;
  1199. }
  1200. if (fw->size < sizeof(*header))
  1201. goto err_firmware;
  1202. header = (struct sdma_firmware_header *)fw->data;
  1203. if (header->magic != SDMA_FIRMWARE_MAGIC)
  1204. goto err_firmware;
  1205. if (header->ram_code_start + header->ram_code_size > fw->size)
  1206. goto err_firmware;
  1207. switch (header->version_major) {
  1208. case 1:
  1209. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1210. break;
  1211. case 2:
  1212. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
  1213. break;
  1214. case 3:
  1215. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
  1216. break;
  1217. default:
  1218. dev_err(sdma->dev, "unknown firmware version\n");
  1219. goto err_firmware;
  1220. }
  1221. addr = (void *)header + header->script_addrs_start;
  1222. ram_code = (void *)header + header->ram_code_start;
  1223. clk_enable(sdma->clk_ipg);
  1224. clk_enable(sdma->clk_ahb);
  1225. /* download the RAM image for SDMA */
  1226. sdma_load_script(sdma, ram_code,
  1227. header->ram_code_size,
  1228. addr->ram_code_start_addr);
  1229. clk_disable(sdma->clk_ipg);
  1230. clk_disable(sdma->clk_ahb);
  1231. sdma_add_scripts(sdma, addr);
  1232. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  1233. header->version_major,
  1234. header->version_minor);
  1235. err_firmware:
  1236. release_firmware(fw);
  1237. }
  1238. #define EVENT_REMAP_CELLS 3
  1239. static int sdma_event_remap(struct sdma_engine *sdma)
  1240. {
  1241. struct device_node *np = sdma->dev->of_node;
  1242. struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
  1243. struct property *event_remap;
  1244. struct regmap *gpr;
  1245. char propname[] = "fsl,sdma-event-remap";
  1246. u32 reg, val, shift, num_map, i;
  1247. int ret = 0;
  1248. if (IS_ERR(np) || IS_ERR(gpr_np))
  1249. goto out;
  1250. event_remap = of_find_property(np, propname, NULL);
  1251. num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
  1252. if (!num_map) {
  1253. dev_dbg(sdma->dev, "no event needs to be remapped\n");
  1254. goto out;
  1255. } else if (num_map % EVENT_REMAP_CELLS) {
  1256. dev_err(sdma->dev, "the property %s must modulo %d\n",
  1257. propname, EVENT_REMAP_CELLS);
  1258. ret = -EINVAL;
  1259. goto out;
  1260. }
  1261. gpr = syscon_node_to_regmap(gpr_np);
  1262. if (IS_ERR(gpr)) {
  1263. dev_err(sdma->dev, "failed to get gpr regmap\n");
  1264. ret = PTR_ERR(gpr);
  1265. goto out;
  1266. }
  1267. for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
  1268. ret = of_property_read_u32_index(np, propname, i, &reg);
  1269. if (ret) {
  1270. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1271. propname, i);
  1272. goto out;
  1273. }
  1274. ret = of_property_read_u32_index(np, propname, i + 1, &shift);
  1275. if (ret) {
  1276. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1277. propname, i + 1);
  1278. goto out;
  1279. }
  1280. ret = of_property_read_u32_index(np, propname, i + 2, &val);
  1281. if (ret) {
  1282. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1283. propname, i + 2);
  1284. goto out;
  1285. }
  1286. regmap_update_bits(gpr, reg, BIT(shift), val << shift);
  1287. }
  1288. out:
  1289. if (!IS_ERR(gpr_np))
  1290. of_node_put(gpr_np);
  1291. return ret;
  1292. }
  1293. static int sdma_get_firmware(struct sdma_engine *sdma,
  1294. const char *fw_name)
  1295. {
  1296. int ret;
  1297. ret = request_firmware_nowait(THIS_MODULE,
  1298. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1299. GFP_KERNEL, sdma, sdma_load_firmware);
  1300. return ret;
  1301. }
  1302. static int sdma_init(struct sdma_engine *sdma)
  1303. {
  1304. int i, ret;
  1305. dma_addr_t ccb_phys;
  1306. ret = clk_enable(sdma->clk_ipg);
  1307. if (ret)
  1308. return ret;
  1309. ret = clk_enable(sdma->clk_ahb);
  1310. if (ret)
  1311. goto disable_clk_ipg;
  1312. /* Be sure SDMA has not started yet */
  1313. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1314. sdma->channel_control = dma_alloc_coherent(NULL,
  1315. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1316. sizeof(struct sdma_context_data),
  1317. &ccb_phys, GFP_KERNEL);
  1318. if (!sdma->channel_control) {
  1319. ret = -ENOMEM;
  1320. goto err_dma_alloc;
  1321. }
  1322. sdma->context = (void *)sdma->channel_control +
  1323. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1324. sdma->context_phys = ccb_phys +
  1325. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1326. /* Zero-out the CCB structures array just allocated */
  1327. memset(sdma->channel_control, 0,
  1328. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1329. /* disable all channels */
  1330. for (i = 0; i < sdma->drvdata->num_events; i++)
  1331. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1332. /* All channels have priority 0 */
  1333. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1334. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1335. ret = sdma_request_channel(&sdma->channel[0]);
  1336. if (ret)
  1337. goto err_dma_alloc;
  1338. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1339. /* Set Command Channel (Channel Zero) */
  1340. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1341. /* Set bits of CONFIG register but with static context switching */
  1342. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1343. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1344. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1345. /* Initializes channel's priorities */
  1346. sdma_set_channel_priority(&sdma->channel[0], 7);
  1347. clk_disable(sdma->clk_ipg);
  1348. clk_disable(sdma->clk_ahb);
  1349. return 0;
  1350. err_dma_alloc:
  1351. clk_disable(sdma->clk_ahb);
  1352. disable_clk_ipg:
  1353. clk_disable(sdma->clk_ipg);
  1354. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1355. return ret;
  1356. }
  1357. static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
  1358. {
  1359. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1360. struct imx_dma_data *data = fn_param;
  1361. if (!imx_dma_is_general_purpose(chan))
  1362. return false;
  1363. sdmac->data = *data;
  1364. chan->private = &sdmac->data;
  1365. return true;
  1366. }
  1367. static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
  1368. struct of_dma *ofdma)
  1369. {
  1370. struct sdma_engine *sdma = ofdma->of_dma_data;
  1371. dma_cap_mask_t mask = sdma->dma_device.cap_mask;
  1372. struct imx_dma_data data;
  1373. if (dma_spec->args_count != 3)
  1374. return NULL;
  1375. data.dma_request = dma_spec->args[0];
  1376. data.peripheral_type = dma_spec->args[1];
  1377. data.priority = dma_spec->args[2];
  1378. /*
  1379. * init dma_request2 to zero, which is not used by the dts.
  1380. * For P2P, dma_request2 is init from dma_request_channel(),
  1381. * chan->private will point to the imx_dma_data, and in
  1382. * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
  1383. * be set to sdmac->event_id1.
  1384. */
  1385. data.dma_request2 = 0;
  1386. return dma_request_channel(mask, sdma_filter_fn, &data);
  1387. }
  1388. static int sdma_probe(struct platform_device *pdev)
  1389. {
  1390. const struct of_device_id *of_id =
  1391. of_match_device(sdma_dt_ids, &pdev->dev);
  1392. struct device_node *np = pdev->dev.of_node;
  1393. struct device_node *spba_bus;
  1394. const char *fw_name;
  1395. int ret;
  1396. int irq;
  1397. struct resource *iores;
  1398. struct resource spba_res;
  1399. struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1400. int i;
  1401. struct sdma_engine *sdma;
  1402. s32 *saddr_arr;
  1403. const struct sdma_driver_data *drvdata = NULL;
  1404. if (of_id)
  1405. drvdata = of_id->data;
  1406. else if (pdev->id_entry)
  1407. drvdata = (void *)pdev->id_entry->driver_data;
  1408. if (!drvdata) {
  1409. dev_err(&pdev->dev, "unable to find driver data\n");
  1410. return -EINVAL;
  1411. }
  1412. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1413. if (ret)
  1414. return ret;
  1415. sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
  1416. if (!sdma)
  1417. return -ENOMEM;
  1418. spin_lock_init(&sdma->channel_0_lock);
  1419. sdma->dev = &pdev->dev;
  1420. sdma->drvdata = drvdata;
  1421. irq = platform_get_irq(pdev, 0);
  1422. if (irq < 0)
  1423. return irq;
  1424. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1425. sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
  1426. if (IS_ERR(sdma->regs))
  1427. return PTR_ERR(sdma->regs);
  1428. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1429. if (IS_ERR(sdma->clk_ipg))
  1430. return PTR_ERR(sdma->clk_ipg);
  1431. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1432. if (IS_ERR(sdma->clk_ahb))
  1433. return PTR_ERR(sdma->clk_ahb);
  1434. clk_prepare(sdma->clk_ipg);
  1435. clk_prepare(sdma->clk_ahb);
  1436. ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
  1437. sdma);
  1438. if (ret)
  1439. return ret;
  1440. sdma->irq = irq;
  1441. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1442. if (!sdma->script_addrs)
  1443. return -ENOMEM;
  1444. /* initially no scripts available */
  1445. saddr_arr = (s32 *)sdma->script_addrs;
  1446. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1447. saddr_arr[i] = -EINVAL;
  1448. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1449. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1450. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1451. /* Initialize channel parameters */
  1452. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1453. struct sdma_channel *sdmac = &sdma->channel[i];
  1454. sdmac->sdma = sdma;
  1455. spin_lock_init(&sdmac->lock);
  1456. sdmac->chan.device = &sdma->dma_device;
  1457. dma_cookie_init(&sdmac->chan);
  1458. sdmac->channel = i;
  1459. tasklet_init(&sdmac->tasklet, sdma_tasklet,
  1460. (unsigned long) sdmac);
  1461. /*
  1462. * Add the channel to the DMAC list. Do not add channel 0 though
  1463. * because we need it internally in the SDMA driver. This also means
  1464. * that channel 0 in dmaengine counting matches sdma channel 1.
  1465. */
  1466. if (i)
  1467. list_add_tail(&sdmac->chan.device_node,
  1468. &sdma->dma_device.channels);
  1469. }
  1470. ret = sdma_init(sdma);
  1471. if (ret)
  1472. goto err_init;
  1473. ret = sdma_event_remap(sdma);
  1474. if (ret)
  1475. goto err_init;
  1476. if (sdma->drvdata->script_addrs)
  1477. sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
  1478. if (pdata && pdata->script_addrs)
  1479. sdma_add_scripts(sdma, pdata->script_addrs);
  1480. if (pdata) {
  1481. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1482. if (ret)
  1483. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1484. } else {
  1485. /*
  1486. * Because that device tree does not encode ROM script address,
  1487. * the RAM script in firmware is mandatory for device tree
  1488. * probe, otherwise it fails.
  1489. */
  1490. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1491. &fw_name);
  1492. if (ret)
  1493. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1494. else {
  1495. ret = sdma_get_firmware(sdma, fw_name);
  1496. if (ret)
  1497. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1498. }
  1499. }
  1500. sdma->dma_device.dev = &pdev->dev;
  1501. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1502. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1503. sdma->dma_device.device_tx_status = sdma_tx_status;
  1504. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1505. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1506. sdma->dma_device.device_config = sdma_config;
  1507. sdma->dma_device.device_terminate_all = sdma_disable_channel;
  1508. sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1509. sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1510. sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1511. sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1512. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1513. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1514. dma_set_max_seg_size(sdma->dma_device.dev, 65535);
  1515. platform_set_drvdata(pdev, sdma);
  1516. ret = dma_async_device_register(&sdma->dma_device);
  1517. if (ret) {
  1518. dev_err(&pdev->dev, "unable to register\n");
  1519. goto err_init;
  1520. }
  1521. if (np) {
  1522. ret = of_dma_controller_register(np, sdma_xlate, sdma);
  1523. if (ret) {
  1524. dev_err(&pdev->dev, "failed to register controller\n");
  1525. goto err_register;
  1526. }
  1527. spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
  1528. ret = of_address_to_resource(spba_bus, 0, &spba_res);
  1529. if (!ret) {
  1530. sdma->spba_start_addr = spba_res.start;
  1531. sdma->spba_end_addr = spba_res.end;
  1532. }
  1533. of_node_put(spba_bus);
  1534. }
  1535. return 0;
  1536. err_register:
  1537. dma_async_device_unregister(&sdma->dma_device);
  1538. err_init:
  1539. kfree(sdma->script_addrs);
  1540. return ret;
  1541. }
  1542. static int sdma_remove(struct platform_device *pdev)
  1543. {
  1544. struct sdma_engine *sdma = platform_get_drvdata(pdev);
  1545. int i;
  1546. devm_free_irq(&pdev->dev, sdma->irq, sdma);
  1547. dma_async_device_unregister(&sdma->dma_device);
  1548. kfree(sdma->script_addrs);
  1549. /* Kill the tasklet */
  1550. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1551. struct sdma_channel *sdmac = &sdma->channel[i];
  1552. tasklet_kill(&sdmac->tasklet);
  1553. }
  1554. platform_set_drvdata(pdev, NULL);
  1555. return 0;
  1556. }
  1557. static struct platform_driver sdma_driver = {
  1558. .driver = {
  1559. .name = "imx-sdma",
  1560. .of_match_table = sdma_dt_ids,
  1561. },
  1562. .id_table = sdma_devtypes,
  1563. .remove = sdma_remove,
  1564. .probe = sdma_probe,
  1565. };
  1566. module_platform_driver(sdma_driver);
  1567. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1568. MODULE_DESCRIPTION("i.MX SDMA driver");
  1569. MODULE_LICENSE("GPL");