ste_dma40.c 66 KB

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