ste_dma40.c 68 KB

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