imx-sdma.c 41 KB

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