imx-sdma.c 48 KB

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