ste_dma40.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. /*
  2. * driver/dma/ste_dma40.c
  3. *
  4. * Copyright (C) ST-Ericsson 2007-2010
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Author: Per Friden <per.friden@stericsson.com>
  7. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <plat/ste_dma40.h>
  17. #include "ste_dma40_ll.h"
  18. #define D40_NAME "dma40"
  19. #define D40_PHY_CHAN -1
  20. /* For masking out/in 2 bit channel positions */
  21. #define D40_CHAN_POS(chan) (2 * (chan / 2))
  22. #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
  23. /* Maximum iterations taken before giving up suspending a channel */
  24. #define D40_SUSPEND_MAX_IT 500
  25. #define D40_ALLOC_FREE (1 << 31)
  26. #define D40_ALLOC_PHY (1 << 30)
  27. #define D40_ALLOC_LOG_FREE 0
  28. /* The number of free d40_desc to keep in memory before starting
  29. * to kfree() them */
  30. #define D40_DESC_CACHE_SIZE 50
  31. /* Hardware designer of the block */
  32. #define D40_PERIPHID2_DESIGNER 0x8
  33. /**
  34. * enum 40_command - The different commands and/or statuses.
  35. *
  36. * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
  37. * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
  38. * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
  39. * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
  40. */
  41. enum d40_command {
  42. D40_DMA_STOP = 0,
  43. D40_DMA_RUN = 1,
  44. D40_DMA_SUSPEND_REQ = 2,
  45. D40_DMA_SUSPENDED = 3
  46. };
  47. /**
  48. * struct d40_lli_pool - Structure for keeping LLIs in memory
  49. *
  50. * @base: Pointer to memory area when the pre_alloc_lli's are not large
  51. * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
  52. * pre_alloc_lli is used.
  53. * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
  54. * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
  55. * one buffer to one buffer.
  56. */
  57. struct d40_lli_pool {
  58. void *base;
  59. int size;
  60. /* Space for dst and src, plus an extra for padding */
  61. u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
  62. };
  63. /**
  64. * struct d40_desc - A descriptor is one DMA job.
  65. *
  66. * @lli_phy: LLI settings for physical channel. Both src and dst=
  67. * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
  68. * lli_len equals one.
  69. * @lli_log: Same as above but for logical channels.
  70. * @lli_pool: The pool with two entries pre-allocated.
  71. * @lli_len: Number of LLI's in lli_pool
  72. * @lli_tcount: Number of LLIs processed in the transfer. When equals lli_len
  73. * then this transfer job is done.
  74. * @txd: DMA engine struct. Used for among other things for communication
  75. * during a transfer.
  76. * @node: List entry.
  77. * @dir: The transfer direction of this job.
  78. * @is_in_client_list: true if the client owns this descriptor.
  79. *
  80. * This descriptor is used for both logical and physical transfers.
  81. */
  82. struct d40_desc {
  83. /* LLI physical */
  84. struct d40_phy_lli_bidir lli_phy;
  85. /* LLI logical */
  86. struct d40_log_lli_bidir lli_log;
  87. struct d40_lli_pool lli_pool;
  88. u32 lli_len;
  89. u32 lli_tcount;
  90. struct dma_async_tx_descriptor txd;
  91. struct list_head node;
  92. enum dma_data_direction dir;
  93. bool is_in_client_list;
  94. };
  95. /**
  96. * struct d40_lcla_pool - LCLA pool settings and data.
  97. *
  98. * @base: The virtual address of LCLA.
  99. * @phy: Physical base address of LCLA.
  100. * @base_size: size of lcla.
  101. * @lock: Lock to protect the content in this struct.
  102. * @alloc_map: Mapping between physical channel and LCLA entries.
  103. * @num_blocks: The number of entries of alloc_map. Equals to the
  104. * number of physical channels.
  105. */
  106. struct d40_lcla_pool {
  107. void *base;
  108. dma_addr_t phy;
  109. resource_size_t base_size;
  110. spinlock_t lock;
  111. u32 *alloc_map;
  112. int num_blocks;
  113. };
  114. /**
  115. * struct d40_phy_res - struct for handling eventlines mapped to physical
  116. * channels.
  117. *
  118. * @lock: A lock protection this entity.
  119. * @num: The physical channel number of this entity.
  120. * @allocated_src: Bit mapped to show which src event line's are mapped to
  121. * this physical channel. Can also be free or physically allocated.
  122. * @allocated_dst: Same as for src but is dst.
  123. * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
  124. * event line number. Both allocated_src and allocated_dst can not be
  125. * allocated to a physical channel, since the interrupt handler has then
  126. * no way of figure out which one the interrupt belongs to.
  127. */
  128. struct d40_phy_res {
  129. spinlock_t lock;
  130. int num;
  131. u32 allocated_src;
  132. u32 allocated_dst;
  133. };
  134. struct d40_base;
  135. /**
  136. * struct d40_chan - Struct that describes a channel.
  137. *
  138. * @lock: A spinlock to protect this struct.
  139. * @log_num: The logical number, if any of this channel.
  140. * @completed: Starts with 1, after first interrupt it is set to dma engine's
  141. * current cookie.
  142. * @pending_tx: The number of pending transfers. Used between interrupt handler
  143. * and tasklet.
  144. * @busy: Set to true when transfer is ongoing on this channel.
  145. * @phy_chan: Pointer to physical channel which this instance runs on.
  146. * @chan: DMA engine handle.
  147. * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
  148. * transfer and call client callback.
  149. * @client: Cliented owned descriptor list.
  150. * @active: Active descriptor.
  151. * @queue: Queued jobs.
  152. * @free: List of free descripts, ready to be reused.
  153. * @free_len: Number of descriptors in the free list.
  154. * @dma_cfg: The client configuration of this dma channel.
  155. * @base: Pointer to the device instance struct.
  156. * @src_def_cfg: Default cfg register setting for src.
  157. * @dst_def_cfg: Default cfg register setting for dst.
  158. * @log_def: Default logical channel settings.
  159. * @lcla: Space for one dst src pair for logical channel transfers.
  160. * @lcpa: Pointer to dst and src lcpa settings.
  161. *
  162. * This struct can either "be" a logical or a physical channel.
  163. */
  164. struct d40_chan {
  165. spinlock_t lock;
  166. int log_num;
  167. /* ID of the most recent completed transfer */
  168. int completed;
  169. int pending_tx;
  170. bool busy;
  171. struct d40_phy_res *phy_chan;
  172. struct dma_chan chan;
  173. struct tasklet_struct tasklet;
  174. struct list_head client;
  175. struct list_head active;
  176. struct list_head queue;
  177. struct list_head free;
  178. int free_len;
  179. struct stedma40_chan_cfg dma_cfg;
  180. struct d40_base *base;
  181. /* Default register configurations */
  182. u32 src_def_cfg;
  183. u32 dst_def_cfg;
  184. struct d40_def_lcsp log_def;
  185. struct d40_lcla_elem lcla;
  186. struct d40_log_lli_full *lcpa;
  187. };
  188. /**
  189. * struct d40_base - The big global struct, one for each probe'd instance.
  190. *
  191. * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
  192. * @execmd_lock: Lock for execute command usage since several channels share
  193. * the same physical register.
  194. * @dev: The device structure.
  195. * @virtbase: The virtual base address of the DMA's register.
  196. * @clk: Pointer to the DMA clock structure.
  197. * @phy_start: Physical memory start of the DMA registers.
  198. * @phy_size: Size of the DMA register map.
  199. * @irq: The IRQ number.
  200. * @num_phy_chans: The number of physical channels. Read from HW. This
  201. * is the number of available channels for this driver, not counting "Secure
  202. * mode" allocated physical channels.
  203. * @num_log_chans: The number of logical channels. Calculated from
  204. * num_phy_chans.
  205. * @dma_both: dma_device channels that can do both memcpy and slave transfers.
  206. * @dma_slave: dma_device channels that can do only do slave transfers.
  207. * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
  208. * @phy_chans: Room for all possible physical channels in system.
  209. * @log_chans: Room for all possible logical channels in system.
  210. * @lookup_log_chans: Used to map interrupt number to logical channel. Points
  211. * to log_chans entries.
  212. * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
  213. * to phy_chans entries.
  214. * @plat_data: Pointer to provided platform_data which is the driver
  215. * configuration.
  216. * @phy_res: Vector containing all physical channels.
  217. * @lcla_pool: lcla pool settings and data.
  218. * @lcpa_base: The virtual mapped address of LCPA.
  219. * @phy_lcpa: The physical address of the LCPA.
  220. * @lcpa_size: The size of the LCPA area.
  221. */
  222. struct d40_base {
  223. spinlock_t interrupt_lock;
  224. spinlock_t execmd_lock;
  225. struct device *dev;
  226. void __iomem *virtbase;
  227. struct clk *clk;
  228. phys_addr_t phy_start;
  229. resource_size_t phy_size;
  230. int irq;
  231. int num_phy_chans;
  232. int num_log_chans;
  233. struct dma_device dma_both;
  234. struct dma_device dma_slave;
  235. struct dma_device dma_memcpy;
  236. struct d40_chan *phy_chans;
  237. struct d40_chan *log_chans;
  238. struct d40_chan **lookup_log_chans;
  239. struct d40_chan **lookup_phy_chans;
  240. struct stedma40_platform_data *plat_data;
  241. /* Physical half channels */
  242. struct d40_phy_res *phy_res;
  243. struct d40_lcla_pool lcla_pool;
  244. void *lcpa_base;
  245. dma_addr_t phy_lcpa;
  246. resource_size_t lcpa_size;
  247. };
  248. /**
  249. * struct d40_interrupt_lookup - lookup table for interrupt handler
  250. *
  251. * @src: Interrupt mask register.
  252. * @clr: Interrupt clear register.
  253. * @is_error: true if this is an error interrupt.
  254. * @offset: start delta in the lookup_log_chans in d40_base. If equals to
  255. * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
  256. */
  257. struct d40_interrupt_lookup {
  258. u32 src;
  259. u32 clr;
  260. bool is_error;
  261. int offset;
  262. };
  263. /**
  264. * struct d40_reg_val - simple lookup struct
  265. *
  266. * @reg: The register.
  267. * @val: The value that belongs to the register in reg.
  268. */
  269. struct d40_reg_val {
  270. unsigned int reg;
  271. unsigned int val;
  272. };
  273. static int d40_pool_lli_alloc(struct d40_desc *d40d,
  274. int lli_len, bool is_log)
  275. {
  276. u32 align;
  277. void *base;
  278. if (is_log)
  279. align = sizeof(struct d40_log_lli);
  280. else
  281. align = sizeof(struct d40_phy_lli);
  282. if (lli_len == 1) {
  283. base = d40d->lli_pool.pre_alloc_lli;
  284. d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
  285. d40d->lli_pool.base = NULL;
  286. } else {
  287. d40d->lli_pool.size = ALIGN(lli_len * 2 * align, align);
  288. base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
  289. d40d->lli_pool.base = base;
  290. if (d40d->lli_pool.base == NULL)
  291. return -ENOMEM;
  292. }
  293. if (is_log) {
  294. d40d->lli_log.src = PTR_ALIGN((struct d40_log_lli *) base,
  295. align);
  296. d40d->lli_log.dst = PTR_ALIGN(d40d->lli_log.src + lli_len,
  297. align);
  298. } else {
  299. d40d->lli_phy.src = PTR_ALIGN((struct d40_phy_lli *)base,
  300. align);
  301. d40d->lli_phy.dst = PTR_ALIGN(d40d->lli_phy.src + lli_len,
  302. align);
  303. d40d->lli_phy.src_addr = virt_to_phys(d40d->lli_phy.src);
  304. d40d->lli_phy.dst_addr = virt_to_phys(d40d->lli_phy.dst);
  305. }
  306. return 0;
  307. }
  308. static void d40_pool_lli_free(struct d40_desc *d40d)
  309. {
  310. kfree(d40d->lli_pool.base);
  311. d40d->lli_pool.base = NULL;
  312. d40d->lli_pool.size = 0;
  313. d40d->lli_log.src = NULL;
  314. d40d->lli_log.dst = NULL;
  315. d40d->lli_phy.src = NULL;
  316. d40d->lli_phy.dst = NULL;
  317. d40d->lli_phy.src_addr = 0;
  318. d40d->lli_phy.dst_addr = 0;
  319. }
  320. static dma_cookie_t d40_assign_cookie(struct d40_chan *d40c,
  321. struct d40_desc *desc)
  322. {
  323. dma_cookie_t cookie = d40c->chan.cookie;
  324. if (++cookie < 0)
  325. cookie = 1;
  326. d40c->chan.cookie = cookie;
  327. desc->txd.cookie = cookie;
  328. return cookie;
  329. }
  330. static void d40_desc_reset(struct d40_desc *d40d)
  331. {
  332. d40d->lli_tcount = 0;
  333. }
  334. static void d40_desc_remove(struct d40_desc *d40d)
  335. {
  336. list_del(&d40d->node);
  337. }
  338. static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
  339. {
  340. struct d40_desc *desc;
  341. struct d40_desc *d;
  342. struct d40_desc *_d;
  343. if (!list_empty(&d40c->client)) {
  344. list_for_each_entry_safe(d, _d, &d40c->client, node)
  345. if (async_tx_test_ack(&d->txd)) {
  346. d40_pool_lli_free(d);
  347. d40_desc_remove(d);
  348. desc = d;
  349. goto out;
  350. }
  351. }
  352. if (list_empty(&d40c->free)) {
  353. /* Alloc new desc because we're out of used ones */
  354. desc = kzalloc(sizeof(struct d40_desc), GFP_NOWAIT);
  355. if (desc == NULL)
  356. goto out;
  357. INIT_LIST_HEAD(&desc->node);
  358. } else {
  359. /* Reuse an old desc. */
  360. desc = list_first_entry(&d40c->free,
  361. struct d40_desc,
  362. node);
  363. list_del(&desc->node);
  364. d40c->free_len--;
  365. }
  366. out:
  367. return desc;
  368. }
  369. static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
  370. {
  371. if (d40c->free_len < D40_DESC_CACHE_SIZE) {
  372. list_add_tail(&d40d->node, &d40c->free);
  373. d40c->free_len++;
  374. } else
  375. kfree(d40d);
  376. }
  377. static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
  378. {
  379. list_add_tail(&desc->node, &d40c->active);
  380. }
  381. static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
  382. {
  383. struct d40_desc *d;
  384. if (list_empty(&d40c->active))
  385. return NULL;
  386. d = list_first_entry(&d40c->active,
  387. struct d40_desc,
  388. node);
  389. return d;
  390. }
  391. static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
  392. {
  393. list_add_tail(&desc->node, &d40c->queue);
  394. }
  395. static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
  396. {
  397. struct d40_desc *d;
  398. if (list_empty(&d40c->queue))
  399. return NULL;
  400. d = list_first_entry(&d40c->queue,
  401. struct d40_desc,
  402. node);
  403. return d;
  404. }
  405. /* Support functions for logical channels */
  406. static int d40_lcla_id_get(struct d40_chan *d40c,
  407. struct d40_lcla_pool *pool)
  408. {
  409. int src_id = 0;
  410. int dst_id = 0;
  411. struct d40_log_lli *lcla_lidx_base =
  412. pool->base + d40c->phy_chan->num * 1024;
  413. int i;
  414. int lli_per_log = d40c->base->plat_data->llis_per_log;
  415. if (d40c->lcla.src_id >= 0 && d40c->lcla.dst_id >= 0)
  416. return 0;
  417. if (pool->num_blocks > 32)
  418. return -EINVAL;
  419. spin_lock(&pool->lock);
  420. for (i = 0; i < pool->num_blocks; i++) {
  421. if (!(pool->alloc_map[d40c->phy_chan->num] & (0x1 << i))) {
  422. pool->alloc_map[d40c->phy_chan->num] |= (0x1 << i);
  423. break;
  424. }
  425. }
  426. src_id = i;
  427. if (src_id >= pool->num_blocks)
  428. goto err;
  429. for (; i < pool->num_blocks; i++) {
  430. if (!(pool->alloc_map[d40c->phy_chan->num] & (0x1 << i))) {
  431. pool->alloc_map[d40c->phy_chan->num] |= (0x1 << i);
  432. break;
  433. }
  434. }
  435. dst_id = i;
  436. if (dst_id == src_id)
  437. goto err;
  438. d40c->lcla.src_id = src_id;
  439. d40c->lcla.dst_id = dst_id;
  440. d40c->lcla.dst = lcla_lidx_base + dst_id * lli_per_log + 1;
  441. d40c->lcla.src = lcla_lidx_base + src_id * lli_per_log + 1;
  442. spin_unlock(&pool->lock);
  443. return 0;
  444. err:
  445. spin_unlock(&pool->lock);
  446. return -EINVAL;
  447. }
  448. static void d40_lcla_id_put(struct d40_chan *d40c,
  449. struct d40_lcla_pool *pool,
  450. int id)
  451. {
  452. if (id < 0)
  453. return;
  454. d40c->lcla.src_id = -1;
  455. d40c->lcla.dst_id = -1;
  456. spin_lock(&pool->lock);
  457. pool->alloc_map[d40c->phy_chan->num] &= (~(0x1 << id));
  458. spin_unlock(&pool->lock);
  459. }
  460. static int d40_channel_execute_command(struct d40_chan *d40c,
  461. enum d40_command command)
  462. {
  463. int status, i;
  464. void __iomem *active_reg;
  465. int ret = 0;
  466. unsigned long flags;
  467. spin_lock_irqsave(&d40c->base->execmd_lock, flags);
  468. if (d40c->phy_chan->num % 2 == 0)
  469. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  470. else
  471. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  472. if (command == D40_DMA_SUSPEND_REQ) {
  473. status = (readl(active_reg) &
  474. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  475. D40_CHAN_POS(d40c->phy_chan->num);
  476. if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
  477. goto done;
  478. }
  479. writel(command << D40_CHAN_POS(d40c->phy_chan->num), active_reg);
  480. if (command == D40_DMA_SUSPEND_REQ) {
  481. for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
  482. status = (readl(active_reg) &
  483. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  484. D40_CHAN_POS(d40c->phy_chan->num);
  485. cpu_relax();
  486. /*
  487. * Reduce the number of bus accesses while
  488. * waiting for the DMA to suspend.
  489. */
  490. udelay(3);
  491. if (status == D40_DMA_STOP ||
  492. status == D40_DMA_SUSPENDED)
  493. break;
  494. }
  495. if (i == D40_SUSPEND_MAX_IT) {
  496. dev_err(&d40c->chan.dev->device,
  497. "[%s]: unable to suspend the chl %d (log: %d) status %x\n",
  498. __func__, d40c->phy_chan->num, d40c->log_num,
  499. status);
  500. dump_stack();
  501. ret = -EBUSY;
  502. }
  503. }
  504. done:
  505. spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
  506. return ret;
  507. }
  508. static void d40_term_all(struct d40_chan *d40c)
  509. {
  510. struct d40_desc *d40d;
  511. struct d40_desc *d;
  512. struct d40_desc *_d;
  513. /* Release active descriptors */
  514. while ((d40d = d40_first_active_get(d40c))) {
  515. d40_desc_remove(d40d);
  516. /* Return desc to free-list */
  517. d40_desc_free(d40c, d40d);
  518. }
  519. /* Release queued descriptors waiting for transfer */
  520. while ((d40d = d40_first_queued(d40c))) {
  521. d40_desc_remove(d40d);
  522. /* Return desc to free-list */
  523. d40_desc_free(d40c, d40d);
  524. }
  525. /* Release client owned descriptors */
  526. if (!list_empty(&d40c->client))
  527. list_for_each_entry_safe(d, _d, &d40c->client, node) {
  528. d40_pool_lli_free(d);
  529. d40_desc_remove(d);
  530. /* Return desc to free-list */
  531. d40_desc_free(d40c, d40d);
  532. }
  533. d40_lcla_id_put(d40c, &d40c->base->lcla_pool,
  534. d40c->lcla.src_id);
  535. d40_lcla_id_put(d40c, &d40c->base->lcla_pool,
  536. d40c->lcla.dst_id);
  537. d40c->pending_tx = 0;
  538. d40c->busy = false;
  539. }
  540. static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
  541. {
  542. u32 val;
  543. unsigned long flags;
  544. if (do_enable)
  545. val = D40_ACTIVATE_EVENTLINE;
  546. else
  547. val = D40_DEACTIVATE_EVENTLINE;
  548. spin_lock_irqsave(&d40c->phy_chan->lock, flags);
  549. /* Enable event line connected to device (or memcpy) */
  550. if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
  551. (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
  552. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
  553. writel((val << D40_EVENTLINE_POS(event)) |
  554. ~D40_EVENTLINE_MASK(event),
  555. d40c->base->virtbase + D40_DREG_PCBASE +
  556. d40c->phy_chan->num * D40_DREG_PCDELTA +
  557. D40_CHAN_REG_SSLNK);
  558. }
  559. if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) {
  560. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
  561. writel((val << D40_EVENTLINE_POS(event)) |
  562. ~D40_EVENTLINE_MASK(event),
  563. d40c->base->virtbase + D40_DREG_PCBASE +
  564. d40c->phy_chan->num * D40_DREG_PCDELTA +
  565. D40_CHAN_REG_SDLNK);
  566. }
  567. spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
  568. }
  569. static bool d40_chan_has_events(struct d40_chan *d40c)
  570. {
  571. u32 val = 0;
  572. /* If SSLNK or SDLNK is zero all events are disabled */
  573. if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) ||
  574. (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
  575. val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
  576. d40c->phy_chan->num * D40_DREG_PCDELTA +
  577. D40_CHAN_REG_SSLNK);
  578. if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM)
  579. val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
  580. d40c->phy_chan->num * D40_DREG_PCDELTA +
  581. D40_CHAN_REG_SDLNK);
  582. return (bool) val;
  583. }
  584. static void d40_config_enable_lidx(struct d40_chan *d40c)
  585. {
  586. /* Set LIDX for lcla */
  587. writel((d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) &
  588. D40_SREG_ELEM_LOG_LIDX_MASK,
  589. d40c->base->virtbase + D40_DREG_PCBASE +
  590. d40c->phy_chan->num * D40_DREG_PCDELTA + D40_CHAN_REG_SDELT);
  591. writel((d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) &
  592. D40_SREG_ELEM_LOG_LIDX_MASK,
  593. d40c->base->virtbase + D40_DREG_PCBASE +
  594. d40c->phy_chan->num * D40_DREG_PCDELTA + D40_CHAN_REG_SSELT);
  595. }
  596. static int d40_config_write(struct d40_chan *d40c)
  597. {
  598. u32 addr_base;
  599. u32 var;
  600. int res;
  601. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  602. if (res)
  603. return res;
  604. /* Odd addresses are even addresses + 4 */
  605. addr_base = (d40c->phy_chan->num % 2) * 4;
  606. /* Setup channel mode to logical or physical */
  607. var = ((u32)(d40c->log_num != D40_PHY_CHAN) + 1) <<
  608. D40_CHAN_POS(d40c->phy_chan->num);
  609. writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
  610. /* Setup operational mode option register */
  611. var = ((d40c->dma_cfg.channel_type >> STEDMA40_INFO_CH_MODE_OPT_POS) &
  612. 0x3) << D40_CHAN_POS(d40c->phy_chan->num);
  613. writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
  614. if (d40c->log_num != D40_PHY_CHAN) {
  615. /* Set default config for CFG reg */
  616. writel(d40c->src_def_cfg,
  617. d40c->base->virtbase + D40_DREG_PCBASE +
  618. d40c->phy_chan->num * D40_DREG_PCDELTA +
  619. D40_CHAN_REG_SSCFG);
  620. writel(d40c->dst_def_cfg,
  621. d40c->base->virtbase + D40_DREG_PCBASE +
  622. d40c->phy_chan->num * D40_DREG_PCDELTA +
  623. D40_CHAN_REG_SDCFG);
  624. d40_config_enable_lidx(d40c);
  625. }
  626. return res;
  627. }
  628. static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
  629. {
  630. if (d40d->lli_phy.dst && d40d->lli_phy.src) {
  631. d40_phy_lli_write(d40c->base->virtbase,
  632. d40c->phy_chan->num,
  633. d40d->lli_phy.dst,
  634. d40d->lli_phy.src);
  635. d40d->lli_tcount = d40d->lli_len;
  636. } else if (d40d->lli_log.dst && d40d->lli_log.src) {
  637. u32 lli_len;
  638. struct d40_log_lli *src = d40d->lli_log.src;
  639. struct d40_log_lli *dst = d40d->lli_log.dst;
  640. src += d40d->lli_tcount;
  641. dst += d40d->lli_tcount;
  642. if (d40d->lli_len <= d40c->base->plat_data->llis_per_log)
  643. lli_len = d40d->lli_len;
  644. else
  645. lli_len = d40c->base->plat_data->llis_per_log;
  646. d40d->lli_tcount += lli_len;
  647. d40_log_lli_write(d40c->lcpa, d40c->lcla.src,
  648. d40c->lcla.dst,
  649. dst, src,
  650. d40c->base->plat_data->llis_per_log);
  651. }
  652. }
  653. static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
  654. {
  655. struct d40_chan *d40c = container_of(tx->chan,
  656. struct d40_chan,
  657. chan);
  658. struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
  659. unsigned long flags;
  660. spin_lock_irqsave(&d40c->lock, flags);
  661. tx->cookie = d40_assign_cookie(d40c, d40d);
  662. d40_desc_queue(d40c, d40d);
  663. spin_unlock_irqrestore(&d40c->lock, flags);
  664. return tx->cookie;
  665. }
  666. static int d40_start(struct d40_chan *d40c)
  667. {
  668. int err;
  669. if (d40c->log_num != D40_PHY_CHAN) {
  670. err = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  671. if (err)
  672. return err;
  673. d40_config_set_event(d40c, true);
  674. }
  675. err = d40_channel_execute_command(d40c, D40_DMA_RUN);
  676. return err;
  677. }
  678. static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
  679. {
  680. struct d40_desc *d40d;
  681. int err;
  682. /* Start queued jobs, if any */
  683. d40d = d40_first_queued(d40c);
  684. if (d40d != NULL) {
  685. d40c->busy = true;
  686. /* Remove from queue */
  687. d40_desc_remove(d40d);
  688. /* Add to active queue */
  689. d40_desc_submit(d40c, d40d);
  690. /* Initiate DMA job */
  691. d40_desc_load(d40c, d40d);
  692. /* Start dma job */
  693. err = d40_start(d40c);
  694. if (err)
  695. return NULL;
  696. }
  697. return d40d;
  698. }
  699. /* called from interrupt context */
  700. static void dma_tc_handle(struct d40_chan *d40c)
  701. {
  702. struct d40_desc *d40d;
  703. if (!d40c->phy_chan)
  704. return;
  705. /* Get first active entry from list */
  706. d40d = d40_first_active_get(d40c);
  707. if (d40d == NULL)
  708. return;
  709. if (d40d->lli_tcount < d40d->lli_len) {
  710. d40_desc_load(d40c, d40d);
  711. /* Start dma job */
  712. (void) d40_start(d40c);
  713. return;
  714. }
  715. if (d40_queue_start(d40c) == NULL)
  716. d40c->busy = false;
  717. d40c->pending_tx++;
  718. tasklet_schedule(&d40c->tasklet);
  719. }
  720. static void dma_tasklet(unsigned long data)
  721. {
  722. struct d40_chan *d40c = (struct d40_chan *) data;
  723. struct d40_desc *d40d_fin;
  724. unsigned long flags;
  725. dma_async_tx_callback callback;
  726. void *callback_param;
  727. spin_lock_irqsave(&d40c->lock, flags);
  728. /* Get first active entry from list */
  729. d40d_fin = d40_first_active_get(d40c);
  730. if (d40d_fin == NULL)
  731. goto err;
  732. d40c->completed = d40d_fin->txd.cookie;
  733. /*
  734. * If terminating a channel pending_tx is set to zero.
  735. * This prevents any finished active jobs to return to the client.
  736. */
  737. if (d40c->pending_tx == 0) {
  738. spin_unlock_irqrestore(&d40c->lock, flags);
  739. return;
  740. }
  741. /* Callback to client */
  742. callback = d40d_fin->txd.callback;
  743. callback_param = d40d_fin->txd.callback_param;
  744. if (async_tx_test_ack(&d40d_fin->txd)) {
  745. d40_pool_lli_free(d40d_fin);
  746. d40_desc_remove(d40d_fin);
  747. /* Return desc to free-list */
  748. d40_desc_free(d40c, d40d_fin);
  749. } else {
  750. d40_desc_reset(d40d_fin);
  751. if (!d40d_fin->is_in_client_list) {
  752. d40_desc_remove(d40d_fin);
  753. list_add_tail(&d40d_fin->node, &d40c->client);
  754. d40d_fin->is_in_client_list = true;
  755. }
  756. }
  757. d40c->pending_tx--;
  758. if (d40c->pending_tx)
  759. tasklet_schedule(&d40c->tasklet);
  760. spin_unlock_irqrestore(&d40c->lock, flags);
  761. if (callback)
  762. callback(callback_param);
  763. return;
  764. err:
  765. /* Rescue manouver if receiving double interrupts */
  766. if (d40c->pending_tx > 0)
  767. d40c->pending_tx--;
  768. spin_unlock_irqrestore(&d40c->lock, flags);
  769. }
  770. static irqreturn_t d40_handle_interrupt(int irq, void *data)
  771. {
  772. static const struct d40_interrupt_lookup il[] = {
  773. {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
  774. {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
  775. {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
  776. {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
  777. {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
  778. {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
  779. {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
  780. {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
  781. {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
  782. {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
  783. };
  784. int i;
  785. u32 regs[ARRAY_SIZE(il)];
  786. u32 tmp;
  787. u32 idx;
  788. u32 row;
  789. long chan = -1;
  790. struct d40_chan *d40c;
  791. unsigned long flags;
  792. struct d40_base *base = data;
  793. spin_lock_irqsave(&base->interrupt_lock, flags);
  794. /* Read interrupt status of both logical and physical channels */
  795. for (i = 0; i < ARRAY_SIZE(il); i++)
  796. regs[i] = readl(base->virtbase + il[i].src);
  797. for (;;) {
  798. chan = find_next_bit((unsigned long *)regs,
  799. BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
  800. /* No more set bits found? */
  801. if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
  802. break;
  803. row = chan / BITS_PER_LONG;
  804. idx = chan & (BITS_PER_LONG - 1);
  805. /* ACK interrupt */
  806. tmp = readl(base->virtbase + il[row].clr);
  807. tmp |= 1 << idx;
  808. writel(tmp, base->virtbase + il[row].clr);
  809. if (il[row].offset == D40_PHY_CHAN)
  810. d40c = base->lookup_phy_chans[idx];
  811. else
  812. d40c = base->lookup_log_chans[il[row].offset + idx];
  813. spin_lock(&d40c->lock);
  814. if (!il[row].is_error)
  815. dma_tc_handle(d40c);
  816. else
  817. dev_err(base->dev, "[%s] IRQ chan: %ld offset %d idx %d\n",
  818. __func__, chan, il[row].offset, idx);
  819. spin_unlock(&d40c->lock);
  820. }
  821. spin_unlock_irqrestore(&base->interrupt_lock, flags);
  822. return IRQ_HANDLED;
  823. }
  824. static int d40_validate_conf(struct d40_chan *d40c,
  825. struct stedma40_chan_cfg *conf)
  826. {
  827. int res = 0;
  828. u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
  829. u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
  830. bool is_log = (conf->channel_type & STEDMA40_CHANNEL_IN_OPER_MODE)
  831. == STEDMA40_CHANNEL_IN_LOG_MODE;
  832. if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH &&
  833. dst_event_group == STEDMA40_DEV_DST_MEMORY) {
  834. dev_err(&d40c->chan.dev->device, "[%s] Invalid dst\n",
  835. __func__);
  836. res = -EINVAL;
  837. }
  838. if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM &&
  839. src_event_group == STEDMA40_DEV_SRC_MEMORY) {
  840. dev_err(&d40c->chan.dev->device, "[%s] Invalid src\n",
  841. __func__);
  842. res = -EINVAL;
  843. }
  844. if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
  845. dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
  846. dev_err(&d40c->chan.dev->device,
  847. "[%s] No event line\n", __func__);
  848. res = -EINVAL;
  849. }
  850. if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
  851. (src_event_group != dst_event_group)) {
  852. dev_err(&d40c->chan.dev->device,
  853. "[%s] Invalid event group\n", __func__);
  854. res = -EINVAL;
  855. }
  856. if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
  857. /*
  858. * DMAC HW supports it. Will be added to this driver,
  859. * in case any dma client requires it.
  860. */
  861. dev_err(&d40c->chan.dev->device,
  862. "[%s] periph to periph not supported\n",
  863. __func__);
  864. res = -EINVAL;
  865. }
  866. return res;
  867. }
  868. static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
  869. int log_event_line)
  870. {
  871. unsigned long flags;
  872. spin_lock_irqsave(&phy->lock, flags);
  873. if (!log_event_line) {
  874. /* Physical interrupts are masked per physical full channel */
  875. if (phy->allocated_src == D40_ALLOC_FREE &&
  876. phy->allocated_dst == D40_ALLOC_FREE) {
  877. phy->allocated_dst = D40_ALLOC_PHY;
  878. phy->allocated_src = D40_ALLOC_PHY;
  879. goto found;
  880. } else
  881. goto not_found;
  882. }
  883. /* Logical channel */
  884. if (is_src) {
  885. if (phy->allocated_src == D40_ALLOC_PHY)
  886. goto not_found;
  887. if (phy->allocated_src == D40_ALLOC_FREE)
  888. phy->allocated_src = D40_ALLOC_LOG_FREE;
  889. if (!(phy->allocated_src & (1 << log_event_line))) {
  890. phy->allocated_src |= 1 << log_event_line;
  891. goto found;
  892. } else
  893. goto not_found;
  894. } else {
  895. if (phy->allocated_dst == D40_ALLOC_PHY)
  896. goto not_found;
  897. if (phy->allocated_dst == D40_ALLOC_FREE)
  898. phy->allocated_dst = D40_ALLOC_LOG_FREE;
  899. if (!(phy->allocated_dst & (1 << log_event_line))) {
  900. phy->allocated_dst |= 1 << log_event_line;
  901. goto found;
  902. } else
  903. goto not_found;
  904. }
  905. not_found:
  906. spin_unlock_irqrestore(&phy->lock, flags);
  907. return false;
  908. found:
  909. spin_unlock_irqrestore(&phy->lock, flags);
  910. return true;
  911. }
  912. static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
  913. int log_event_line)
  914. {
  915. unsigned long flags;
  916. bool is_free = false;
  917. spin_lock_irqsave(&phy->lock, flags);
  918. if (!log_event_line) {
  919. /* Physical interrupts are masked per physical full channel */
  920. phy->allocated_dst = D40_ALLOC_FREE;
  921. phy->allocated_src = D40_ALLOC_FREE;
  922. is_free = true;
  923. goto out;
  924. }
  925. /* Logical channel */
  926. if (is_src) {
  927. phy->allocated_src &= ~(1 << log_event_line);
  928. if (phy->allocated_src == D40_ALLOC_LOG_FREE)
  929. phy->allocated_src = D40_ALLOC_FREE;
  930. } else {
  931. phy->allocated_dst &= ~(1 << log_event_line);
  932. if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
  933. phy->allocated_dst = D40_ALLOC_FREE;
  934. }
  935. is_free = ((phy->allocated_src | phy->allocated_dst) ==
  936. D40_ALLOC_FREE);
  937. out:
  938. spin_unlock_irqrestore(&phy->lock, flags);
  939. return is_free;
  940. }
  941. static int d40_allocate_channel(struct d40_chan *d40c)
  942. {
  943. int dev_type;
  944. int event_group;
  945. int event_line;
  946. struct d40_phy_res *phys;
  947. int i;
  948. int j;
  949. int log_num;
  950. bool is_src;
  951. bool is_log = (d40c->dma_cfg.channel_type & STEDMA40_CHANNEL_IN_OPER_MODE)
  952. == STEDMA40_CHANNEL_IN_LOG_MODE;
  953. phys = d40c->base->phy_res;
  954. if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
  955. dev_type = d40c->dma_cfg.src_dev_type;
  956. log_num = 2 * dev_type;
  957. is_src = true;
  958. } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
  959. d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
  960. /* dst event lines are used for logical memcpy */
  961. dev_type = d40c->dma_cfg.dst_dev_type;
  962. log_num = 2 * dev_type + 1;
  963. is_src = false;
  964. } else
  965. return -EINVAL;
  966. event_group = D40_TYPE_TO_GROUP(dev_type);
  967. event_line = D40_TYPE_TO_EVENT(dev_type);
  968. if (!is_log) {
  969. if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
  970. /* Find physical half channel */
  971. for (i = 0; i < d40c->base->num_phy_chans; i++) {
  972. if (d40_alloc_mask_set(&phys[i], is_src, 0))
  973. goto found_phy;
  974. }
  975. } else
  976. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  977. int phy_num = j + event_group * 2;
  978. for (i = phy_num; i < phy_num + 2; i++) {
  979. if (d40_alloc_mask_set(&phys[i],
  980. is_src, 0))
  981. goto found_phy;
  982. }
  983. }
  984. return -EINVAL;
  985. found_phy:
  986. d40c->phy_chan = &phys[i];
  987. d40c->log_num = D40_PHY_CHAN;
  988. goto out;
  989. }
  990. if (dev_type == -1)
  991. return -EINVAL;
  992. /* Find logical channel */
  993. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  994. int phy_num = j + event_group * 2;
  995. /*
  996. * Spread logical channels across all available physical rather
  997. * than pack every logical channel at the first available phy
  998. * channels.
  999. */
  1000. if (is_src) {
  1001. for (i = phy_num; i < phy_num + 2; i++) {
  1002. if (d40_alloc_mask_set(&phys[i], is_src,
  1003. event_line))
  1004. goto found_log;
  1005. }
  1006. } else {
  1007. for (i = phy_num + 1; i >= phy_num; i--) {
  1008. if (d40_alloc_mask_set(&phys[i], is_src,
  1009. event_line))
  1010. goto found_log;
  1011. }
  1012. }
  1013. }
  1014. return -EINVAL;
  1015. found_log:
  1016. d40c->phy_chan = &phys[i];
  1017. d40c->log_num = log_num;
  1018. out:
  1019. if (is_log)
  1020. d40c->base->lookup_log_chans[d40c->log_num] = d40c;
  1021. else
  1022. d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
  1023. return 0;
  1024. }
  1025. static int d40_config_chan(struct d40_chan *d40c,
  1026. struct stedma40_chan_cfg *info)
  1027. {
  1028. /* Fill in basic CFG register values */
  1029. d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
  1030. &d40c->dst_def_cfg, d40c->log_num != D40_PHY_CHAN);
  1031. if (d40c->log_num != D40_PHY_CHAN) {
  1032. d40_log_cfg(&d40c->dma_cfg,
  1033. &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
  1034. if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
  1035. d40c->lcpa = d40c->base->lcpa_base +
  1036. d40c->dma_cfg.src_dev_type * 32;
  1037. else
  1038. d40c->lcpa = d40c->base->lcpa_base +
  1039. d40c->dma_cfg.dst_dev_type * 32 + 16;
  1040. }
  1041. /* Write channel configuration to the DMA */
  1042. return d40_config_write(d40c);
  1043. }
  1044. static int d40_config_memcpy(struct d40_chan *d40c)
  1045. {
  1046. dma_cap_mask_t cap = d40c->chan.device->cap_mask;
  1047. if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
  1048. d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
  1049. d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
  1050. d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
  1051. memcpy[d40c->chan.chan_id];
  1052. } else if (dma_has_cap(DMA_MEMCPY, cap) &&
  1053. dma_has_cap(DMA_SLAVE, cap)) {
  1054. d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
  1055. } else {
  1056. dev_err(&d40c->chan.dev->device, "[%s] No memcpy\n",
  1057. __func__);
  1058. return -EINVAL;
  1059. }
  1060. return 0;
  1061. }
  1062. static int d40_free_dma(struct d40_chan *d40c)
  1063. {
  1064. int res = 0;
  1065. u32 event, dir;
  1066. struct d40_phy_res *phy = d40c->phy_chan;
  1067. bool is_src;
  1068. /* Terminate all queued and active transfers */
  1069. d40_term_all(d40c);
  1070. if (phy == NULL) {
  1071. dev_err(&d40c->chan.dev->device, "[%s] phy == null\n",
  1072. __func__);
  1073. return -EINVAL;
  1074. }
  1075. if (phy->allocated_src == D40_ALLOC_FREE &&
  1076. phy->allocated_dst == D40_ALLOC_FREE) {
  1077. dev_err(&d40c->chan.dev->device, "[%s] channel already free\n",
  1078. __func__);
  1079. return -EINVAL;
  1080. }
  1081. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  1082. if (res) {
  1083. dev_err(&d40c->chan.dev->device, "[%s] suspend\n",
  1084. __func__);
  1085. return res;
  1086. }
  1087. if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
  1088. d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
  1089. event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
  1090. dir = D40_CHAN_REG_SDLNK;
  1091. is_src = false;
  1092. } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
  1093. event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
  1094. dir = D40_CHAN_REG_SSLNK;
  1095. is_src = true;
  1096. } else {
  1097. dev_err(&d40c->chan.dev->device,
  1098. "[%s] Unknown direction\n", __func__);
  1099. return -EINVAL;
  1100. }
  1101. if (d40c->log_num != D40_PHY_CHAN) {
  1102. /*
  1103. * Release logical channel, deactivate the event line during
  1104. * the time physical res is suspended.
  1105. */
  1106. writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) &
  1107. D40_EVENTLINE_MASK(event),
  1108. d40c->base->virtbase + D40_DREG_PCBASE +
  1109. phy->num * D40_DREG_PCDELTA + dir);
  1110. d40c->base->lookup_log_chans[d40c->log_num] = NULL;
  1111. /*
  1112. * Check if there are more logical allocation
  1113. * on this phy channel.
  1114. */
  1115. if (!d40_alloc_mask_free(phy, is_src, event)) {
  1116. /* Resume the other logical channels if any */
  1117. if (d40_chan_has_events(d40c)) {
  1118. res = d40_channel_execute_command(d40c,
  1119. D40_DMA_RUN);
  1120. if (res) {
  1121. dev_err(&d40c->chan.dev->device,
  1122. "[%s] Executing RUN command\n",
  1123. __func__);
  1124. return res;
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. } else
  1130. d40_alloc_mask_free(phy, is_src, 0);
  1131. /* Release physical channel */
  1132. res = d40_channel_execute_command(d40c, D40_DMA_STOP);
  1133. if (res) {
  1134. dev_err(&d40c->chan.dev->device,
  1135. "[%s] Failed to stop channel\n", __func__);
  1136. return res;
  1137. }
  1138. d40c->phy_chan = NULL;
  1139. /* Invalidate channel type */
  1140. d40c->dma_cfg.channel_type = 0;
  1141. d40c->base->lookup_phy_chans[phy->num] = NULL;
  1142. return 0;
  1143. }
  1144. static int d40_pause(struct dma_chan *chan)
  1145. {
  1146. struct d40_chan *d40c =
  1147. container_of(chan, struct d40_chan, chan);
  1148. int res;
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&d40c->lock, flags);
  1151. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  1152. if (res == 0) {
  1153. if (d40c->log_num != D40_PHY_CHAN) {
  1154. d40_config_set_event(d40c, false);
  1155. /* Resume the other logical channels if any */
  1156. if (d40_chan_has_events(d40c))
  1157. res = d40_channel_execute_command(d40c,
  1158. D40_DMA_RUN);
  1159. }
  1160. }
  1161. spin_unlock_irqrestore(&d40c->lock, flags);
  1162. return res;
  1163. }
  1164. static bool d40_tx_is_linked(struct d40_chan *d40c)
  1165. {
  1166. bool is_link;
  1167. if (d40c->log_num != D40_PHY_CHAN)
  1168. is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
  1169. else
  1170. is_link = readl(d40c->base->virtbase + D40_DREG_PCBASE +
  1171. d40c->phy_chan->num * D40_DREG_PCDELTA +
  1172. D40_CHAN_REG_SDLNK) &
  1173. D40_SREG_LNK_PHYS_LNK_MASK;
  1174. return is_link;
  1175. }
  1176. static u32 d40_residue(struct d40_chan *d40c)
  1177. {
  1178. u32 num_elt;
  1179. if (d40c->log_num != D40_PHY_CHAN)
  1180. num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
  1181. >> D40_MEM_LCSP2_ECNT_POS;
  1182. else
  1183. num_elt = (readl(d40c->base->virtbase + D40_DREG_PCBASE +
  1184. d40c->phy_chan->num * D40_DREG_PCDELTA +
  1185. D40_CHAN_REG_SDELT) &
  1186. D40_SREG_ELEM_PHY_ECNT_MASK) >> D40_SREG_ELEM_PHY_ECNT_POS;
  1187. return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
  1188. }
  1189. static int d40_resume(struct dma_chan *chan)
  1190. {
  1191. struct d40_chan *d40c =
  1192. container_of(chan, struct d40_chan, chan);
  1193. int res = 0;
  1194. unsigned long flags;
  1195. spin_lock_irqsave(&d40c->lock, flags);
  1196. if (d40c->log_num != D40_PHY_CHAN) {
  1197. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  1198. if (res)
  1199. goto out;
  1200. /* If bytes left to transfer or linked tx resume job */
  1201. if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
  1202. d40_config_set_event(d40c, true);
  1203. res = d40_channel_execute_command(d40c, D40_DMA_RUN);
  1204. }
  1205. } else if (d40_residue(d40c) || d40_tx_is_linked(d40c))
  1206. res = d40_channel_execute_command(d40c, D40_DMA_RUN);
  1207. out:
  1208. spin_unlock_irqrestore(&d40c->lock, flags);
  1209. return res;
  1210. }
  1211. static u32 stedma40_residue(struct dma_chan *chan)
  1212. {
  1213. struct d40_chan *d40c =
  1214. container_of(chan, struct d40_chan, chan);
  1215. u32 bytes_left;
  1216. unsigned long flags;
  1217. spin_lock_irqsave(&d40c->lock, flags);
  1218. bytes_left = d40_residue(d40c);
  1219. spin_unlock_irqrestore(&d40c->lock, flags);
  1220. return bytes_left;
  1221. }
  1222. /* Public DMA functions in addition to the DMA engine framework */
  1223. int stedma40_set_psize(struct dma_chan *chan,
  1224. int src_psize,
  1225. int dst_psize)
  1226. {
  1227. struct d40_chan *d40c =
  1228. container_of(chan, struct d40_chan, chan);
  1229. unsigned long flags;
  1230. spin_lock_irqsave(&d40c->lock, flags);
  1231. if (d40c->log_num != D40_PHY_CHAN) {
  1232. d40c->log_def.lcsp1 &= ~D40_MEM_LCSP1_SCFG_PSIZE_MASK;
  1233. d40c->log_def.lcsp3 &= ~D40_MEM_LCSP1_SCFG_PSIZE_MASK;
  1234. d40c->log_def.lcsp1 |= src_psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  1235. d40c->log_def.lcsp3 |= dst_psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  1236. goto out;
  1237. }
  1238. if (src_psize == STEDMA40_PSIZE_PHY_1)
  1239. d40c->src_def_cfg &= ~(1 << D40_SREG_CFG_PHY_PEN_POS);
  1240. else {
  1241. d40c->src_def_cfg |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  1242. d40c->src_def_cfg &= ~(STEDMA40_PSIZE_PHY_16 <<
  1243. D40_SREG_CFG_PSIZE_POS);
  1244. d40c->src_def_cfg |= src_psize << D40_SREG_CFG_PSIZE_POS;
  1245. }
  1246. if (dst_psize == STEDMA40_PSIZE_PHY_1)
  1247. d40c->dst_def_cfg &= ~(1 << D40_SREG_CFG_PHY_PEN_POS);
  1248. else {
  1249. d40c->dst_def_cfg |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  1250. d40c->dst_def_cfg &= ~(STEDMA40_PSIZE_PHY_16 <<
  1251. D40_SREG_CFG_PSIZE_POS);
  1252. d40c->dst_def_cfg |= dst_psize << D40_SREG_CFG_PSIZE_POS;
  1253. }
  1254. out:
  1255. spin_unlock_irqrestore(&d40c->lock, flags);
  1256. return 0;
  1257. }
  1258. EXPORT_SYMBOL(stedma40_set_psize);
  1259. struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
  1260. struct scatterlist *sgl_dst,
  1261. struct scatterlist *sgl_src,
  1262. unsigned int sgl_len,
  1263. unsigned long flags)
  1264. {
  1265. int res;
  1266. struct d40_desc *d40d;
  1267. struct d40_chan *d40c = container_of(chan, struct d40_chan,
  1268. chan);
  1269. unsigned long flg;
  1270. int lli_max = d40c->base->plat_data->llis_per_log;
  1271. spin_lock_irqsave(&d40c->lock, flg);
  1272. d40d = d40_desc_get(d40c);
  1273. if (d40d == NULL)
  1274. goto err;
  1275. memset(d40d, 0, sizeof(struct d40_desc));
  1276. d40d->lli_len = sgl_len;
  1277. d40d->txd.flags = flags;
  1278. if (d40c->log_num != D40_PHY_CHAN) {
  1279. if (sgl_len > 1)
  1280. /*
  1281. * Check if there is space available in lcla. If not,
  1282. * split list into 1-length and run only in lcpa
  1283. * space.
  1284. */
  1285. if (d40_lcla_id_get(d40c,
  1286. &d40c->base->lcla_pool) != 0)
  1287. lli_max = 1;
  1288. if (d40_pool_lli_alloc(d40d, sgl_len, true) < 0) {
  1289. dev_err(&d40c->chan.dev->device,
  1290. "[%s] Out of memory\n", __func__);
  1291. goto err;
  1292. }
  1293. (void) d40_log_sg_to_lli(d40c->lcla.src_id,
  1294. sgl_src,
  1295. sgl_len,
  1296. d40d->lli_log.src,
  1297. d40c->log_def.lcsp1,
  1298. d40c->dma_cfg.src_info.data_width,
  1299. flags & DMA_PREP_INTERRUPT, lli_max,
  1300. d40c->base->plat_data->llis_per_log);
  1301. (void) d40_log_sg_to_lli(d40c->lcla.dst_id,
  1302. sgl_dst,
  1303. sgl_len,
  1304. d40d->lli_log.dst,
  1305. d40c->log_def.lcsp3,
  1306. d40c->dma_cfg.dst_info.data_width,
  1307. flags & DMA_PREP_INTERRUPT, lli_max,
  1308. d40c->base->plat_data->llis_per_log);
  1309. } else {
  1310. if (d40_pool_lli_alloc(d40d, sgl_len, false) < 0) {
  1311. dev_err(&d40c->chan.dev->device,
  1312. "[%s] Out of memory\n", __func__);
  1313. goto err;
  1314. }
  1315. res = d40_phy_sg_to_lli(sgl_src,
  1316. sgl_len,
  1317. 0,
  1318. d40d->lli_phy.src,
  1319. d40d->lli_phy.src_addr,
  1320. d40c->src_def_cfg,
  1321. d40c->dma_cfg.src_info.data_width,
  1322. d40c->dma_cfg.src_info.psize,
  1323. true);
  1324. if (res < 0)
  1325. goto err;
  1326. res = d40_phy_sg_to_lli(sgl_dst,
  1327. sgl_len,
  1328. 0,
  1329. d40d->lli_phy.dst,
  1330. d40d->lli_phy.dst_addr,
  1331. d40c->dst_def_cfg,
  1332. d40c->dma_cfg.dst_info.data_width,
  1333. d40c->dma_cfg.dst_info.psize,
  1334. true);
  1335. if (res < 0)
  1336. goto err;
  1337. (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
  1338. d40d->lli_pool.size, DMA_TO_DEVICE);
  1339. }
  1340. dma_async_tx_descriptor_init(&d40d->txd, chan);
  1341. d40d->txd.tx_submit = d40_tx_submit;
  1342. spin_unlock_irqrestore(&d40c->lock, flg);
  1343. return &d40d->txd;
  1344. err:
  1345. spin_unlock_irqrestore(&d40c->lock, flg);
  1346. return NULL;
  1347. }
  1348. EXPORT_SYMBOL(stedma40_memcpy_sg);
  1349. bool stedma40_filter(struct dma_chan *chan, void *data)
  1350. {
  1351. struct stedma40_chan_cfg *info = data;
  1352. struct d40_chan *d40c =
  1353. container_of(chan, struct d40_chan, chan);
  1354. int err;
  1355. if (data) {
  1356. err = d40_validate_conf(d40c, info);
  1357. if (!err)
  1358. d40c->dma_cfg = *info;
  1359. } else
  1360. err = d40_config_memcpy(d40c);
  1361. return err == 0;
  1362. }
  1363. EXPORT_SYMBOL(stedma40_filter);
  1364. /* DMA ENGINE functions */
  1365. static int d40_alloc_chan_resources(struct dma_chan *chan)
  1366. {
  1367. int err;
  1368. unsigned long flags;
  1369. struct d40_chan *d40c =
  1370. container_of(chan, struct d40_chan, chan);
  1371. spin_lock_irqsave(&d40c->lock, flags);
  1372. d40c->completed = chan->cookie = 1;
  1373. /*
  1374. * If no dma configuration is set (channel_type == 0)
  1375. * use default configuration
  1376. */
  1377. if (d40c->dma_cfg.channel_type == 0) {
  1378. err = d40_config_memcpy(d40c);
  1379. if (err)
  1380. goto err_alloc;
  1381. }
  1382. err = d40_allocate_channel(d40c);
  1383. if (err) {
  1384. dev_err(&d40c->chan.dev->device,
  1385. "[%s] Failed to allocate channel\n", __func__);
  1386. goto err_alloc;
  1387. }
  1388. err = d40_config_chan(d40c, &d40c->dma_cfg);
  1389. if (err) {
  1390. dev_err(&d40c->chan.dev->device,
  1391. "[%s] Failed to configure channel\n",
  1392. __func__);
  1393. goto err_config;
  1394. }
  1395. spin_unlock_irqrestore(&d40c->lock, flags);
  1396. return 0;
  1397. err_config:
  1398. (void) d40_free_dma(d40c);
  1399. err_alloc:
  1400. spin_unlock_irqrestore(&d40c->lock, flags);
  1401. dev_err(&d40c->chan.dev->device,
  1402. "[%s] Channel allocation failed\n", __func__);
  1403. return -EINVAL;
  1404. }
  1405. static void d40_free_chan_resources(struct dma_chan *chan)
  1406. {
  1407. struct d40_chan *d40c =
  1408. container_of(chan, struct d40_chan, chan);
  1409. int err;
  1410. unsigned long flags;
  1411. spin_lock_irqsave(&d40c->lock, flags);
  1412. err = d40_free_dma(d40c);
  1413. if (err)
  1414. dev_err(&d40c->chan.dev->device,
  1415. "[%s] Failed to free channel\n", __func__);
  1416. spin_unlock_irqrestore(&d40c->lock, flags);
  1417. }
  1418. static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
  1419. dma_addr_t dst,
  1420. dma_addr_t src,
  1421. size_t size,
  1422. unsigned long flags)
  1423. {
  1424. struct d40_desc *d40d;
  1425. struct d40_chan *d40c = container_of(chan, struct d40_chan,
  1426. chan);
  1427. unsigned long flg;
  1428. int err = 0;
  1429. spin_lock_irqsave(&d40c->lock, flg);
  1430. d40d = d40_desc_get(d40c);
  1431. if (d40d == NULL) {
  1432. dev_err(&d40c->chan.dev->device,
  1433. "[%s] Descriptor is NULL\n", __func__);
  1434. goto err;
  1435. }
  1436. memset(d40d, 0, sizeof(struct d40_desc));
  1437. d40d->txd.flags = flags;
  1438. dma_async_tx_descriptor_init(&d40d->txd, chan);
  1439. d40d->txd.tx_submit = d40_tx_submit;
  1440. if (d40c->log_num != D40_PHY_CHAN) {
  1441. if (d40_pool_lli_alloc(d40d, 1, true) < 0) {
  1442. dev_err(&d40c->chan.dev->device,
  1443. "[%s] Out of memory\n", __func__);
  1444. goto err;
  1445. }
  1446. d40d->lli_len = 1;
  1447. d40_log_fill_lli(d40d->lli_log.src,
  1448. src,
  1449. size,
  1450. 0,
  1451. d40c->log_def.lcsp1,
  1452. d40c->dma_cfg.src_info.data_width,
  1453. true, true);
  1454. d40_log_fill_lli(d40d->lli_log.dst,
  1455. dst,
  1456. size,
  1457. 0,
  1458. d40c->log_def.lcsp3,
  1459. d40c->dma_cfg.dst_info.data_width,
  1460. true, true);
  1461. } else {
  1462. if (d40_pool_lli_alloc(d40d, 1, false) < 0) {
  1463. dev_err(&d40c->chan.dev->device,
  1464. "[%s] Out of memory\n", __func__);
  1465. goto err;
  1466. }
  1467. err = d40_phy_fill_lli(d40d->lli_phy.src,
  1468. src,
  1469. size,
  1470. d40c->dma_cfg.src_info.psize,
  1471. 0,
  1472. d40c->src_def_cfg,
  1473. true,
  1474. d40c->dma_cfg.src_info.data_width,
  1475. false);
  1476. if (err)
  1477. goto err_fill_lli;
  1478. err = d40_phy_fill_lli(d40d->lli_phy.dst,
  1479. dst,
  1480. size,
  1481. d40c->dma_cfg.dst_info.psize,
  1482. 0,
  1483. d40c->dst_def_cfg,
  1484. true,
  1485. d40c->dma_cfg.dst_info.data_width,
  1486. false);
  1487. if (err)
  1488. goto err_fill_lli;
  1489. (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
  1490. d40d->lli_pool.size, DMA_TO_DEVICE);
  1491. }
  1492. spin_unlock_irqrestore(&d40c->lock, flg);
  1493. return &d40d->txd;
  1494. err_fill_lli:
  1495. dev_err(&d40c->chan.dev->device,
  1496. "[%s] Failed filling in PHY LLI\n", __func__);
  1497. d40_pool_lli_free(d40d);
  1498. err:
  1499. spin_unlock_irqrestore(&d40c->lock, flg);
  1500. return NULL;
  1501. }
  1502. static int d40_prep_slave_sg_log(struct d40_desc *d40d,
  1503. struct d40_chan *d40c,
  1504. struct scatterlist *sgl,
  1505. unsigned int sg_len,
  1506. enum dma_data_direction direction,
  1507. unsigned long flags)
  1508. {
  1509. dma_addr_t dev_addr = 0;
  1510. int total_size;
  1511. int lli_max = d40c->base->plat_data->llis_per_log;
  1512. if (d40_pool_lli_alloc(d40d, sg_len, true) < 0) {
  1513. dev_err(&d40c->chan.dev->device,
  1514. "[%s] Out of memory\n", __func__);
  1515. return -ENOMEM;
  1516. }
  1517. d40d->lli_len = sg_len;
  1518. d40d->lli_tcount = 0;
  1519. if (sg_len > 1)
  1520. /*
  1521. * Check if there is space available in lcla.
  1522. * If not, split list into 1-length and run only
  1523. * in lcpa space.
  1524. */
  1525. if (d40_lcla_id_get(d40c, &d40c->base->lcla_pool) != 0)
  1526. lli_max = 1;
  1527. if (direction == DMA_FROM_DEVICE) {
  1528. dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
  1529. total_size = d40_log_sg_to_dev(&d40c->lcla,
  1530. sgl, sg_len,
  1531. &d40d->lli_log,
  1532. &d40c->log_def,
  1533. d40c->dma_cfg.src_info.data_width,
  1534. d40c->dma_cfg.dst_info.data_width,
  1535. direction,
  1536. flags & DMA_PREP_INTERRUPT,
  1537. dev_addr, lli_max,
  1538. d40c->base->plat_data->llis_per_log);
  1539. } else if (direction == DMA_TO_DEVICE) {
  1540. dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
  1541. total_size = d40_log_sg_to_dev(&d40c->lcla,
  1542. sgl, sg_len,
  1543. &d40d->lli_log,
  1544. &d40c->log_def,
  1545. d40c->dma_cfg.src_info.data_width,
  1546. d40c->dma_cfg.dst_info.data_width,
  1547. direction,
  1548. flags & DMA_PREP_INTERRUPT,
  1549. dev_addr, lli_max,
  1550. d40c->base->plat_data->llis_per_log);
  1551. } else
  1552. return -EINVAL;
  1553. if (total_size < 0)
  1554. return -EINVAL;
  1555. return 0;
  1556. }
  1557. static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
  1558. struct d40_chan *d40c,
  1559. struct scatterlist *sgl,
  1560. unsigned int sgl_len,
  1561. enum dma_data_direction direction,
  1562. unsigned long flags)
  1563. {
  1564. dma_addr_t src_dev_addr;
  1565. dma_addr_t dst_dev_addr;
  1566. int res;
  1567. if (d40_pool_lli_alloc(d40d, sgl_len, false) < 0) {
  1568. dev_err(&d40c->chan.dev->device,
  1569. "[%s] Out of memory\n", __func__);
  1570. return -ENOMEM;
  1571. }
  1572. d40d->lli_len = sgl_len;
  1573. d40d->lli_tcount = 0;
  1574. if (direction == DMA_FROM_DEVICE) {
  1575. dst_dev_addr = 0;
  1576. src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
  1577. } else if (direction == DMA_TO_DEVICE) {
  1578. dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
  1579. src_dev_addr = 0;
  1580. } else
  1581. return -EINVAL;
  1582. res = d40_phy_sg_to_lli(sgl,
  1583. sgl_len,
  1584. src_dev_addr,
  1585. d40d->lli_phy.src,
  1586. d40d->lli_phy.src_addr,
  1587. d40c->src_def_cfg,
  1588. d40c->dma_cfg.src_info.data_width,
  1589. d40c->dma_cfg.src_info.psize,
  1590. true);
  1591. if (res < 0)
  1592. return res;
  1593. res = d40_phy_sg_to_lli(sgl,
  1594. sgl_len,
  1595. dst_dev_addr,
  1596. d40d->lli_phy.dst,
  1597. d40d->lli_phy.dst_addr,
  1598. d40c->dst_def_cfg,
  1599. d40c->dma_cfg.dst_info.data_width,
  1600. d40c->dma_cfg.dst_info.psize,
  1601. true);
  1602. if (res < 0)
  1603. return res;
  1604. (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
  1605. d40d->lli_pool.size, DMA_TO_DEVICE);
  1606. return 0;
  1607. }
  1608. static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
  1609. struct scatterlist *sgl,
  1610. unsigned int sg_len,
  1611. enum dma_data_direction direction,
  1612. unsigned long flags)
  1613. {
  1614. struct d40_desc *d40d;
  1615. struct d40_chan *d40c = container_of(chan, struct d40_chan,
  1616. chan);
  1617. unsigned long flg;
  1618. int err;
  1619. if (d40c->dma_cfg.pre_transfer)
  1620. d40c->dma_cfg.pre_transfer(chan,
  1621. d40c->dma_cfg.pre_transfer_data,
  1622. sg_dma_len(sgl));
  1623. spin_lock_irqsave(&d40c->lock, flg);
  1624. d40d = d40_desc_get(d40c);
  1625. spin_unlock_irqrestore(&d40c->lock, flg);
  1626. if (d40d == NULL)
  1627. return NULL;
  1628. memset(d40d, 0, sizeof(struct d40_desc));
  1629. if (d40c->log_num != D40_PHY_CHAN)
  1630. err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
  1631. direction, flags);
  1632. else
  1633. err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
  1634. direction, flags);
  1635. if (err) {
  1636. dev_err(&d40c->chan.dev->device,
  1637. "[%s] Failed to prepare %s slave sg job: %d\n",
  1638. __func__,
  1639. d40c->log_num != D40_PHY_CHAN ? "log" : "phy", err);
  1640. return NULL;
  1641. }
  1642. d40d->txd.flags = flags;
  1643. dma_async_tx_descriptor_init(&d40d->txd, chan);
  1644. d40d->txd.tx_submit = d40_tx_submit;
  1645. return &d40d->txd;
  1646. }
  1647. static enum dma_status d40_tx_status(struct dma_chan *chan,
  1648. dma_cookie_t cookie,
  1649. struct dma_tx_state *txstate)
  1650. {
  1651. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1652. dma_cookie_t last_used;
  1653. dma_cookie_t last_complete;
  1654. int ret;
  1655. last_complete = d40c->completed;
  1656. last_used = chan->cookie;
  1657. ret = dma_async_is_complete(cookie, last_complete, last_used);
  1658. if (txstate) {
  1659. txstate->last = last_complete;
  1660. txstate->used = last_used;
  1661. txstate->residue = stedma40_residue(chan);
  1662. }
  1663. return ret;
  1664. }
  1665. static void d40_issue_pending(struct dma_chan *chan)
  1666. {
  1667. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1668. unsigned long flags;
  1669. spin_lock_irqsave(&d40c->lock, flags);
  1670. /* Busy means that pending jobs are already being processed */
  1671. if (!d40c->busy)
  1672. (void) d40_queue_start(d40c);
  1673. spin_unlock_irqrestore(&d40c->lock, flags);
  1674. }
  1675. static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd)
  1676. {
  1677. unsigned long flags;
  1678. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1679. switch (cmd) {
  1680. case DMA_TERMINATE_ALL:
  1681. spin_lock_irqsave(&d40c->lock, flags);
  1682. d40_term_all(d40c);
  1683. spin_unlock_irqrestore(&d40c->lock, flags);
  1684. return 0;
  1685. case DMA_PAUSE:
  1686. return d40_pause(chan);
  1687. case DMA_RESUME:
  1688. return d40_resume(chan);
  1689. }
  1690. /* Other commands are unimplemented */
  1691. return -ENXIO;
  1692. }
  1693. /* Initialization functions */
  1694. static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
  1695. struct d40_chan *chans, int offset,
  1696. int num_chans)
  1697. {
  1698. int i = 0;
  1699. struct d40_chan *d40c;
  1700. INIT_LIST_HEAD(&dma->channels);
  1701. for (i = offset; i < offset + num_chans; i++) {
  1702. d40c = &chans[i];
  1703. d40c->base = base;
  1704. d40c->chan.device = dma;
  1705. /* Invalidate lcla element */
  1706. d40c->lcla.src_id = -1;
  1707. d40c->lcla.dst_id = -1;
  1708. spin_lock_init(&d40c->lock);
  1709. d40c->log_num = D40_PHY_CHAN;
  1710. INIT_LIST_HEAD(&d40c->free);
  1711. INIT_LIST_HEAD(&d40c->active);
  1712. INIT_LIST_HEAD(&d40c->queue);
  1713. INIT_LIST_HEAD(&d40c->client);
  1714. d40c->free_len = 0;
  1715. tasklet_init(&d40c->tasklet, dma_tasklet,
  1716. (unsigned long) d40c);
  1717. list_add_tail(&d40c->chan.device_node,
  1718. &dma->channels);
  1719. }
  1720. }
  1721. static int __init d40_dmaengine_init(struct d40_base *base,
  1722. int num_reserved_chans)
  1723. {
  1724. int err ;
  1725. d40_chan_init(base, &base->dma_slave, base->log_chans,
  1726. 0, base->num_log_chans);
  1727. dma_cap_zero(base->dma_slave.cap_mask);
  1728. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  1729. base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
  1730. base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
  1731. base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
  1732. base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
  1733. base->dma_slave.device_tx_status = d40_tx_status;
  1734. base->dma_slave.device_issue_pending = d40_issue_pending;
  1735. base->dma_slave.device_control = d40_control;
  1736. base->dma_slave.dev = base->dev;
  1737. err = dma_async_device_register(&base->dma_slave);
  1738. if (err) {
  1739. dev_err(base->dev,
  1740. "[%s] Failed to register slave channels\n",
  1741. __func__);
  1742. goto failure1;
  1743. }
  1744. d40_chan_init(base, &base->dma_memcpy, base->log_chans,
  1745. base->num_log_chans, base->plat_data->memcpy_len);
  1746. dma_cap_zero(base->dma_memcpy.cap_mask);
  1747. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  1748. base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
  1749. base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
  1750. base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
  1751. base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
  1752. base->dma_memcpy.device_tx_status = d40_tx_status;
  1753. base->dma_memcpy.device_issue_pending = d40_issue_pending;
  1754. base->dma_memcpy.device_control = d40_control;
  1755. base->dma_memcpy.dev = base->dev;
  1756. /*
  1757. * This controller can only access address at even
  1758. * 32bit boundaries, i.e. 2^2
  1759. */
  1760. base->dma_memcpy.copy_align = 2;
  1761. err = dma_async_device_register(&base->dma_memcpy);
  1762. if (err) {
  1763. dev_err(base->dev,
  1764. "[%s] Failed to regsiter memcpy only channels\n",
  1765. __func__);
  1766. goto failure2;
  1767. }
  1768. d40_chan_init(base, &base->dma_both, base->phy_chans,
  1769. 0, num_reserved_chans);
  1770. dma_cap_zero(base->dma_both.cap_mask);
  1771. dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
  1772. dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
  1773. base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
  1774. base->dma_both.device_free_chan_resources = d40_free_chan_resources;
  1775. base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
  1776. base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
  1777. base->dma_both.device_tx_status = d40_tx_status;
  1778. base->dma_both.device_issue_pending = d40_issue_pending;
  1779. base->dma_both.device_control = d40_control;
  1780. base->dma_both.dev = base->dev;
  1781. base->dma_both.copy_align = 2;
  1782. err = dma_async_device_register(&base->dma_both);
  1783. if (err) {
  1784. dev_err(base->dev,
  1785. "[%s] Failed to register logical and physical capable channels\n",
  1786. __func__);
  1787. goto failure3;
  1788. }
  1789. return 0;
  1790. failure3:
  1791. dma_async_device_unregister(&base->dma_memcpy);
  1792. failure2:
  1793. dma_async_device_unregister(&base->dma_slave);
  1794. failure1:
  1795. return err;
  1796. }
  1797. /* Initialization functions. */
  1798. static int __init d40_phy_res_init(struct d40_base *base)
  1799. {
  1800. int i;
  1801. int num_phy_chans_avail = 0;
  1802. u32 val[2];
  1803. int odd_even_bit = -2;
  1804. val[0] = readl(base->virtbase + D40_DREG_PRSME);
  1805. val[1] = readl(base->virtbase + D40_DREG_PRSMO);
  1806. for (i = 0; i < base->num_phy_chans; i++) {
  1807. base->phy_res[i].num = i;
  1808. odd_even_bit += 2 * ((i % 2) == 0);
  1809. if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
  1810. /* Mark security only channels as occupied */
  1811. base->phy_res[i].allocated_src = D40_ALLOC_PHY;
  1812. base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
  1813. } else {
  1814. base->phy_res[i].allocated_src = D40_ALLOC_FREE;
  1815. base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
  1816. num_phy_chans_avail++;
  1817. }
  1818. spin_lock_init(&base->phy_res[i].lock);
  1819. }
  1820. dev_info(base->dev, "%d of %d physical DMA channels available\n",
  1821. num_phy_chans_avail, base->num_phy_chans);
  1822. /* Verify settings extended vs standard */
  1823. val[0] = readl(base->virtbase + D40_DREG_PRTYP);
  1824. for (i = 0; i < base->num_phy_chans; i++) {
  1825. if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
  1826. (val[0] & 0x3) != 1)
  1827. dev_info(base->dev,
  1828. "[%s] INFO: channel %d is misconfigured (%d)\n",
  1829. __func__, i, val[0] & 0x3);
  1830. val[0] = val[0] >> 2;
  1831. }
  1832. return num_phy_chans_avail;
  1833. }
  1834. static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
  1835. {
  1836. static const struct d40_reg_val dma_id_regs[] = {
  1837. /* Peripheral Id */
  1838. { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
  1839. { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
  1840. /*
  1841. * D40_DREG_PERIPHID2 Depends on HW revision:
  1842. * MOP500/HREF ED has 0x0008,
  1843. * ? has 0x0018,
  1844. * HREF V1 has 0x0028
  1845. */
  1846. { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
  1847. /* PCell Id */
  1848. { .reg = D40_DREG_CELLID0, .val = 0x000d},
  1849. { .reg = D40_DREG_CELLID1, .val = 0x00f0},
  1850. { .reg = D40_DREG_CELLID2, .val = 0x0005},
  1851. { .reg = D40_DREG_CELLID3, .val = 0x00b1}
  1852. };
  1853. struct stedma40_platform_data *plat_data;
  1854. struct clk *clk = NULL;
  1855. void __iomem *virtbase = NULL;
  1856. struct resource *res = NULL;
  1857. struct d40_base *base = NULL;
  1858. int num_log_chans = 0;
  1859. int num_phy_chans;
  1860. int i;
  1861. clk = clk_get(&pdev->dev, NULL);
  1862. if (IS_ERR(clk)) {
  1863. dev_err(&pdev->dev, "[%s] No matching clock found\n",
  1864. __func__);
  1865. goto failure;
  1866. }
  1867. clk_enable(clk);
  1868. /* Get IO for DMAC base address */
  1869. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
  1870. if (!res)
  1871. goto failure;
  1872. if (request_mem_region(res->start, resource_size(res),
  1873. D40_NAME " I/O base") == NULL)
  1874. goto failure;
  1875. virtbase = ioremap(res->start, resource_size(res));
  1876. if (!virtbase)
  1877. goto failure;
  1878. /* HW version check */
  1879. for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
  1880. if (dma_id_regs[i].val !=
  1881. readl(virtbase + dma_id_regs[i].reg)) {
  1882. dev_err(&pdev->dev,
  1883. "[%s] Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
  1884. __func__,
  1885. dma_id_regs[i].val,
  1886. dma_id_regs[i].reg,
  1887. readl(virtbase + dma_id_regs[i].reg));
  1888. goto failure;
  1889. }
  1890. }
  1891. i = readl(virtbase + D40_DREG_PERIPHID2);
  1892. if ((i & 0xf) != D40_PERIPHID2_DESIGNER) {
  1893. dev_err(&pdev->dev,
  1894. "[%s] Unknown designer! Got %x wanted %x\n",
  1895. __func__, i & 0xf, D40_PERIPHID2_DESIGNER);
  1896. goto failure;
  1897. }
  1898. /* The number of physical channels on this HW */
  1899. num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
  1900. dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
  1901. (i >> 4) & 0xf, res->start);
  1902. plat_data = pdev->dev.platform_data;
  1903. /* Count the number of logical channels in use */
  1904. for (i = 0; i < plat_data->dev_len; i++)
  1905. if (plat_data->dev_rx[i] != 0)
  1906. num_log_chans++;
  1907. for (i = 0; i < plat_data->dev_len; i++)
  1908. if (plat_data->dev_tx[i] != 0)
  1909. num_log_chans++;
  1910. base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
  1911. (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
  1912. sizeof(struct d40_chan), GFP_KERNEL);
  1913. if (base == NULL) {
  1914. dev_err(&pdev->dev, "[%s] Out of memory\n", __func__);
  1915. goto failure;
  1916. }
  1917. base->clk = clk;
  1918. base->num_phy_chans = num_phy_chans;
  1919. base->num_log_chans = num_log_chans;
  1920. base->phy_start = res->start;
  1921. base->phy_size = resource_size(res);
  1922. base->virtbase = virtbase;
  1923. base->plat_data = plat_data;
  1924. base->dev = &pdev->dev;
  1925. base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
  1926. base->log_chans = &base->phy_chans[num_phy_chans];
  1927. base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
  1928. GFP_KERNEL);
  1929. if (!base->phy_res)
  1930. goto failure;
  1931. base->lookup_phy_chans = kzalloc(num_phy_chans *
  1932. sizeof(struct d40_chan *),
  1933. GFP_KERNEL);
  1934. if (!base->lookup_phy_chans)
  1935. goto failure;
  1936. if (num_log_chans + plat_data->memcpy_len) {
  1937. /*
  1938. * The max number of logical channels are event lines for all
  1939. * src devices and dst devices
  1940. */
  1941. base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
  1942. sizeof(struct d40_chan *),
  1943. GFP_KERNEL);
  1944. if (!base->lookup_log_chans)
  1945. goto failure;
  1946. }
  1947. base->lcla_pool.alloc_map = kzalloc(num_phy_chans * sizeof(u32),
  1948. GFP_KERNEL);
  1949. if (!base->lcla_pool.alloc_map)
  1950. goto failure;
  1951. return base;
  1952. failure:
  1953. if (clk) {
  1954. clk_disable(clk);
  1955. clk_put(clk);
  1956. }
  1957. if (virtbase)
  1958. iounmap(virtbase);
  1959. if (res)
  1960. release_mem_region(res->start,
  1961. resource_size(res));
  1962. if (virtbase)
  1963. iounmap(virtbase);
  1964. if (base) {
  1965. kfree(base->lcla_pool.alloc_map);
  1966. kfree(base->lookup_log_chans);
  1967. kfree(base->lookup_phy_chans);
  1968. kfree(base->phy_res);
  1969. kfree(base);
  1970. }
  1971. return NULL;
  1972. }
  1973. static void __init d40_hw_init(struct d40_base *base)
  1974. {
  1975. static const struct d40_reg_val dma_init_reg[] = {
  1976. /* Clock every part of the DMA block from start */
  1977. { .reg = D40_DREG_GCC, .val = 0x0000ff01},
  1978. /* Interrupts on all logical channels */
  1979. { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
  1980. { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
  1981. { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
  1982. { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
  1983. { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
  1984. { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
  1985. { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
  1986. { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
  1987. { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
  1988. { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
  1989. { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
  1990. { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
  1991. };
  1992. int i;
  1993. u32 prmseo[2] = {0, 0};
  1994. u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
  1995. u32 pcmis = 0;
  1996. u32 pcicr = 0;
  1997. for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
  1998. writel(dma_init_reg[i].val,
  1999. base->virtbase + dma_init_reg[i].reg);
  2000. /* Configure all our dma channels to default settings */
  2001. for (i = 0; i < base->num_phy_chans; i++) {
  2002. activeo[i % 2] = activeo[i % 2] << 2;
  2003. if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
  2004. == D40_ALLOC_PHY) {
  2005. activeo[i % 2] |= 3;
  2006. continue;
  2007. }
  2008. /* Enable interrupt # */
  2009. pcmis = (pcmis << 1) | 1;
  2010. /* Clear interrupt # */
  2011. pcicr = (pcicr << 1) | 1;
  2012. /* Set channel to physical mode */
  2013. prmseo[i % 2] = prmseo[i % 2] << 2;
  2014. prmseo[i % 2] |= 1;
  2015. }
  2016. writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
  2017. writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
  2018. writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
  2019. writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
  2020. /* Write which interrupt to enable */
  2021. writel(pcmis, base->virtbase + D40_DREG_PCMIS);
  2022. /* Write which interrupt to clear */
  2023. writel(pcicr, base->virtbase + D40_DREG_PCICR);
  2024. }
  2025. static int __init d40_probe(struct platform_device *pdev)
  2026. {
  2027. int err;
  2028. int ret = -ENOENT;
  2029. struct d40_base *base;
  2030. struct resource *res = NULL;
  2031. int num_reserved_chans;
  2032. u32 val;
  2033. base = d40_hw_detect_init(pdev);
  2034. if (!base)
  2035. goto failure;
  2036. num_reserved_chans = d40_phy_res_init(base);
  2037. platform_set_drvdata(pdev, base);
  2038. spin_lock_init(&base->interrupt_lock);
  2039. spin_lock_init(&base->execmd_lock);
  2040. /* Get IO for logical channel parameter address */
  2041. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
  2042. if (!res) {
  2043. ret = -ENOENT;
  2044. dev_err(&pdev->dev,
  2045. "[%s] No \"lcpa\" memory resource\n",
  2046. __func__);
  2047. goto failure;
  2048. }
  2049. base->lcpa_size = resource_size(res);
  2050. base->phy_lcpa = res->start;
  2051. if (request_mem_region(res->start, resource_size(res),
  2052. D40_NAME " I/O lcpa") == NULL) {
  2053. ret = -EBUSY;
  2054. dev_err(&pdev->dev,
  2055. "[%s] Failed to request LCPA region 0x%x-0x%x\n",
  2056. __func__, res->start, res->end);
  2057. goto failure;
  2058. }
  2059. /* We make use of ESRAM memory for this. */
  2060. val = readl(base->virtbase + D40_DREG_LCPA);
  2061. if (res->start != val && val != 0) {
  2062. dev_warn(&pdev->dev,
  2063. "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
  2064. __func__, val, res->start);
  2065. } else
  2066. writel(res->start, base->virtbase + D40_DREG_LCPA);
  2067. base->lcpa_base = ioremap(res->start, resource_size(res));
  2068. if (!base->lcpa_base) {
  2069. ret = -ENOMEM;
  2070. dev_err(&pdev->dev,
  2071. "[%s] Failed to ioremap LCPA region\n",
  2072. __func__);
  2073. goto failure;
  2074. }
  2075. /* Get IO for logical channel link address */
  2076. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcla");
  2077. if (!res) {
  2078. ret = -ENOENT;
  2079. dev_err(&pdev->dev,
  2080. "[%s] No \"lcla\" resource defined\n",
  2081. __func__);
  2082. goto failure;
  2083. }
  2084. base->lcla_pool.base_size = resource_size(res);
  2085. base->lcla_pool.phy = res->start;
  2086. if (request_mem_region(res->start, resource_size(res),
  2087. D40_NAME " I/O lcla") == NULL) {
  2088. ret = -EBUSY;
  2089. dev_err(&pdev->dev,
  2090. "[%s] Failed to request LCLA region 0x%x-0x%x\n",
  2091. __func__, res->start, res->end);
  2092. goto failure;
  2093. }
  2094. val = readl(base->virtbase + D40_DREG_LCLA);
  2095. if (res->start != val && val != 0) {
  2096. dev_warn(&pdev->dev,
  2097. "[%s] Mismatch LCLA dma 0x%x, def 0x%x\n",
  2098. __func__, val, res->start);
  2099. } else
  2100. writel(res->start, base->virtbase + D40_DREG_LCLA);
  2101. base->lcla_pool.base = ioremap(res->start, resource_size(res));
  2102. if (!base->lcla_pool.base) {
  2103. ret = -ENOMEM;
  2104. dev_err(&pdev->dev,
  2105. "[%s] Failed to ioremap LCLA 0x%x-0x%x\n",
  2106. __func__, res->start, res->end);
  2107. goto failure;
  2108. }
  2109. spin_lock_init(&base->lcla_pool.lock);
  2110. base->lcla_pool.num_blocks = base->num_phy_chans;
  2111. base->irq = platform_get_irq(pdev, 0);
  2112. ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
  2113. if (ret) {
  2114. dev_err(&pdev->dev, "[%s] No IRQ defined\n", __func__);
  2115. goto failure;
  2116. }
  2117. err = d40_dmaengine_init(base, num_reserved_chans);
  2118. if (err)
  2119. goto failure;
  2120. d40_hw_init(base);
  2121. dev_info(base->dev, "initialized\n");
  2122. return 0;
  2123. failure:
  2124. if (base) {
  2125. if (base->virtbase)
  2126. iounmap(base->virtbase);
  2127. if (base->lcla_pool.phy)
  2128. release_mem_region(base->lcla_pool.phy,
  2129. base->lcla_pool.base_size);
  2130. if (base->phy_lcpa)
  2131. release_mem_region(base->phy_lcpa,
  2132. base->lcpa_size);
  2133. if (base->phy_start)
  2134. release_mem_region(base->phy_start,
  2135. base->phy_size);
  2136. if (base->clk) {
  2137. clk_disable(base->clk);
  2138. clk_put(base->clk);
  2139. }
  2140. kfree(base->lcla_pool.alloc_map);
  2141. kfree(base->lookup_log_chans);
  2142. kfree(base->lookup_phy_chans);
  2143. kfree(base->phy_res);
  2144. kfree(base);
  2145. }
  2146. dev_err(&pdev->dev, "[%s] probe failed\n", __func__);
  2147. return ret;
  2148. }
  2149. static struct platform_driver d40_driver = {
  2150. .driver = {
  2151. .owner = THIS_MODULE,
  2152. .name = D40_NAME,
  2153. },
  2154. };
  2155. int __init stedma40_init(void)
  2156. {
  2157. return platform_driver_probe(&d40_driver, d40_probe);
  2158. }
  2159. arch_initcall(stedma40_init);