imx-sdma.c 51 KB

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