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