ste_dma40.c 66 KB

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