imx-sdma.c 49 KB

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