ste_dma40.c 66 KB

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