ste_dma40.c 67 KB

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