ste_dma40.c 66 KB

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