ste_dma40.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709
  1. /*
  2. * Copyright (C) Ericsson AB 2007-2008
  3. * Copyright (C) ST-Ericsson SA 2008-2010
  4. * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
  5. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/log2.h>
  17. #include <linux/pm.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/err.h>
  20. #include <linux/of.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/platform_data/dma-ste-dma40.h>
  25. #include "dmaengine.h"
  26. #include "ste_dma40_ll.h"
  27. #define D40_NAME "dma40"
  28. #define D40_PHY_CHAN -1
  29. /* For masking out/in 2 bit channel positions */
  30. #define D40_CHAN_POS(chan) (2 * (chan / 2))
  31. #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
  32. /* Maximum iterations taken before giving up suspending a channel */
  33. #define D40_SUSPEND_MAX_IT 500
  34. /* Milliseconds */
  35. #define DMA40_AUTOSUSPEND_DELAY 100
  36. /* Hardware requirement on LCLA alignment */
  37. #define LCLA_ALIGNMENT 0x40000
  38. /* Max number of links per event group */
  39. #define D40_LCLA_LINK_PER_EVENT_GRP 128
  40. #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
  41. /* Max number of logical channels per physical channel */
  42. #define D40_MAX_LOG_CHAN_PER_PHY 32
  43. /* Attempts before giving up to trying to get pages that are aligned */
  44. #define MAX_LCLA_ALLOC_ATTEMPTS 256
  45. /* Bit markings for allocation map */
  46. #define D40_ALLOC_FREE BIT(31)
  47. #define D40_ALLOC_PHY BIT(30)
  48. #define D40_ALLOC_LOG_FREE 0
  49. #define D40_MEMCPY_MAX_CHANS 8
  50. /* Reserved event lines for memcpy only. */
  51. #define DB8500_DMA_MEMCPY_EV_0 51
  52. #define DB8500_DMA_MEMCPY_EV_1 56
  53. #define DB8500_DMA_MEMCPY_EV_2 57
  54. #define DB8500_DMA_MEMCPY_EV_3 58
  55. #define DB8500_DMA_MEMCPY_EV_4 59
  56. #define DB8500_DMA_MEMCPY_EV_5 60
  57. static int dma40_memcpy_channels[] = {
  58. DB8500_DMA_MEMCPY_EV_0,
  59. DB8500_DMA_MEMCPY_EV_1,
  60. DB8500_DMA_MEMCPY_EV_2,
  61. DB8500_DMA_MEMCPY_EV_3,
  62. DB8500_DMA_MEMCPY_EV_4,
  63. DB8500_DMA_MEMCPY_EV_5,
  64. };
  65. /* Default configuration for physcial memcpy */
  66. static const struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
  67. .mode = STEDMA40_MODE_PHYSICAL,
  68. .dir = DMA_MEM_TO_MEM,
  69. .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  70. .src_info.psize = STEDMA40_PSIZE_PHY_1,
  71. .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  72. .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  73. .dst_info.psize = STEDMA40_PSIZE_PHY_1,
  74. .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  75. };
  76. /* Default configuration for logical memcpy */
  77. static const struct stedma40_chan_cfg dma40_memcpy_conf_log = {
  78. .mode = STEDMA40_MODE_LOGICAL,
  79. .dir = DMA_MEM_TO_MEM,
  80. .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  81. .src_info.psize = STEDMA40_PSIZE_LOG_1,
  82. .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  83. .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  84. .dst_info.psize = STEDMA40_PSIZE_LOG_1,
  85. .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  86. };
  87. /**
  88. * enum 40_command - The different commands and/or statuses.
  89. *
  90. * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
  91. * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
  92. * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
  93. * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
  94. */
  95. enum d40_command {
  96. D40_DMA_STOP = 0,
  97. D40_DMA_RUN = 1,
  98. D40_DMA_SUSPEND_REQ = 2,
  99. D40_DMA_SUSPENDED = 3
  100. };
  101. /*
  102. * enum d40_events - The different Event Enables for the event lines.
  103. *
  104. * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
  105. * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
  106. * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
  107. * @D40_ROUND_EVENTLINE: Status check for event line.
  108. */
  109. enum d40_events {
  110. D40_DEACTIVATE_EVENTLINE = 0,
  111. D40_ACTIVATE_EVENTLINE = 1,
  112. D40_SUSPEND_REQ_EVENTLINE = 2,
  113. D40_ROUND_EVENTLINE = 3
  114. };
  115. /*
  116. * These are the registers that has to be saved and later restored
  117. * when the DMA hw is powered off.
  118. * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
  119. */
  120. static __maybe_unused u32 d40_backup_regs[] = {
  121. D40_DREG_LCPA,
  122. D40_DREG_LCLA,
  123. D40_DREG_PRMSE,
  124. D40_DREG_PRMSO,
  125. D40_DREG_PRMOE,
  126. D40_DREG_PRMOO,
  127. };
  128. #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
  129. /*
  130. * since 9540 and 8540 has the same HW revision
  131. * use v4a for 9540 or ealier
  132. * use v4b for 8540 or later
  133. * HW revision:
  134. * DB8500ed has revision 0
  135. * DB8500v1 has revision 2
  136. * DB8500v2 has revision 3
  137. * AP9540v1 has revision 4
  138. * DB8540v1 has revision 4
  139. * TODO: Check if all these registers have to be saved/restored on dma40 v4a
  140. */
  141. static u32 d40_backup_regs_v4a[] = {
  142. D40_DREG_PSEG1,
  143. D40_DREG_PSEG2,
  144. D40_DREG_PSEG3,
  145. D40_DREG_PSEG4,
  146. D40_DREG_PCEG1,
  147. D40_DREG_PCEG2,
  148. D40_DREG_PCEG3,
  149. D40_DREG_PCEG4,
  150. D40_DREG_RSEG1,
  151. D40_DREG_RSEG2,
  152. D40_DREG_RSEG3,
  153. D40_DREG_RSEG4,
  154. D40_DREG_RCEG1,
  155. D40_DREG_RCEG2,
  156. D40_DREG_RCEG3,
  157. D40_DREG_RCEG4,
  158. };
  159. #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
  160. static u32 d40_backup_regs_v4b[] = {
  161. D40_DREG_CPSEG1,
  162. D40_DREG_CPSEG2,
  163. D40_DREG_CPSEG3,
  164. D40_DREG_CPSEG4,
  165. D40_DREG_CPSEG5,
  166. D40_DREG_CPCEG1,
  167. D40_DREG_CPCEG2,
  168. D40_DREG_CPCEG3,
  169. D40_DREG_CPCEG4,
  170. D40_DREG_CPCEG5,
  171. D40_DREG_CRSEG1,
  172. D40_DREG_CRSEG2,
  173. D40_DREG_CRSEG3,
  174. D40_DREG_CRSEG4,
  175. D40_DREG_CRSEG5,
  176. D40_DREG_CRCEG1,
  177. D40_DREG_CRCEG2,
  178. D40_DREG_CRCEG3,
  179. D40_DREG_CRCEG4,
  180. D40_DREG_CRCEG5,
  181. };
  182. #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
  183. static __maybe_unused u32 d40_backup_regs_chan[] = {
  184. D40_CHAN_REG_SSCFG,
  185. D40_CHAN_REG_SSELT,
  186. D40_CHAN_REG_SSPTR,
  187. D40_CHAN_REG_SSLNK,
  188. D40_CHAN_REG_SDCFG,
  189. D40_CHAN_REG_SDELT,
  190. D40_CHAN_REG_SDPTR,
  191. D40_CHAN_REG_SDLNK,
  192. };
  193. #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
  194. BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
  195. /**
  196. * struct d40_interrupt_lookup - lookup table for interrupt handler
  197. *
  198. * @src: Interrupt mask register.
  199. * @clr: Interrupt clear register.
  200. * @is_error: true if this is an error interrupt.
  201. * @offset: start delta in the lookup_log_chans in d40_base. If equals to
  202. * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
  203. */
  204. struct d40_interrupt_lookup {
  205. u32 src;
  206. u32 clr;
  207. bool is_error;
  208. int offset;
  209. };
  210. static struct d40_interrupt_lookup il_v4a[] = {
  211. {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
  212. {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
  213. {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
  214. {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
  215. {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
  216. {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
  217. {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
  218. {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
  219. {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
  220. {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
  221. };
  222. static struct d40_interrupt_lookup il_v4b[] = {
  223. {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
  224. {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
  225. {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
  226. {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
  227. {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
  228. {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
  229. {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
  230. {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
  231. {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
  232. {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
  233. {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
  234. {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
  235. };
  236. /**
  237. * struct d40_reg_val - simple lookup struct
  238. *
  239. * @reg: The register.
  240. * @val: The value that belongs to the register in reg.
  241. */
  242. struct d40_reg_val {
  243. unsigned int reg;
  244. unsigned int val;
  245. };
  246. static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
  247. /* Clock every part of the DMA block from start */
  248. { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
  249. /* Interrupts on all logical channels */
  250. { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
  251. { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
  252. { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
  253. { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
  254. { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
  255. { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
  256. { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
  257. { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
  258. { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
  259. { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
  260. { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
  261. { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
  262. };
  263. static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
  264. /* Clock every part of the DMA block from start */
  265. { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
  266. /* Interrupts on all logical channels */
  267. { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
  268. { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
  269. { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
  270. { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
  271. { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
  272. { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
  273. { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
  274. { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
  275. { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
  276. { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
  277. { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
  278. { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
  279. { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
  280. { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
  281. { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
  282. };
  283. /**
  284. * struct d40_lli_pool - Structure for keeping LLIs in memory
  285. *
  286. * @base: Pointer to memory area when the pre_alloc_lli's are not large
  287. * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
  288. * pre_alloc_lli is used.
  289. * @dma_addr: DMA address, if mapped
  290. * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
  291. * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
  292. * one buffer to one buffer.
  293. */
  294. struct d40_lli_pool {
  295. void *base;
  296. int size;
  297. dma_addr_t dma_addr;
  298. /* Space for dst and src, plus an extra for padding */
  299. u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
  300. };
  301. /**
  302. * struct d40_desc - A descriptor is one DMA job.
  303. *
  304. * @lli_phy: LLI settings for physical channel. Both src and dst=
  305. * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
  306. * lli_len equals one.
  307. * @lli_log: Same as above but for logical channels.
  308. * @lli_pool: The pool with two entries pre-allocated.
  309. * @lli_len: Number of llis of current descriptor.
  310. * @lli_current: Number of transferred llis.
  311. * @lcla_alloc: Number of LCLA entries allocated.
  312. * @txd: DMA engine struct. Used for among other things for communication
  313. * during a transfer.
  314. * @node: List entry.
  315. * @is_in_client_list: true if the client owns this descriptor.
  316. * @cyclic: true if this is a cyclic job
  317. *
  318. * This descriptor is used for both logical and physical transfers.
  319. */
  320. struct d40_desc {
  321. /* LLI physical */
  322. struct d40_phy_lli_bidir lli_phy;
  323. /* LLI logical */
  324. struct d40_log_lli_bidir lli_log;
  325. struct d40_lli_pool lli_pool;
  326. int lli_len;
  327. int lli_current;
  328. int lcla_alloc;
  329. struct dma_async_tx_descriptor txd;
  330. struct list_head node;
  331. bool is_in_client_list;
  332. bool cyclic;
  333. };
  334. /**
  335. * struct d40_lcla_pool - LCLA pool settings and data.
  336. *
  337. * @base: The virtual address of LCLA. 18 bit aligned.
  338. * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
  339. * This pointer is only there for clean-up on error.
  340. * @pages: The number of pages needed for all physical channels.
  341. * Only used later for clean-up on error
  342. * @lock: Lock to protect the content in this struct.
  343. * @alloc_map: big map over which LCLA entry is own by which job.
  344. */
  345. struct d40_lcla_pool {
  346. void *base;
  347. dma_addr_t dma_addr;
  348. void *base_unaligned;
  349. int pages;
  350. spinlock_t lock;
  351. struct d40_desc **alloc_map;
  352. };
  353. /**
  354. * struct d40_phy_res - struct for handling eventlines mapped to physical
  355. * channels.
  356. *
  357. * @lock: A lock protection this entity.
  358. * @reserved: True if used by secure world or otherwise.
  359. * @num: The physical channel number of this entity.
  360. * @allocated_src: Bit mapped to show which src event line's are mapped to
  361. * this physical channel. Can also be free or physically allocated.
  362. * @allocated_dst: Same as for src but is dst.
  363. * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
  364. * event line number.
  365. * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
  366. */
  367. struct d40_phy_res {
  368. spinlock_t lock;
  369. bool reserved;
  370. int num;
  371. u32 allocated_src;
  372. u32 allocated_dst;
  373. bool use_soft_lli;
  374. };
  375. struct d40_base;
  376. /**
  377. * struct d40_chan - Struct that describes a channel.
  378. *
  379. * @lock: A spinlock to protect this struct.
  380. * @log_num: The logical number, if any of this channel.
  381. * @pending_tx: The number of pending transfers. Used between interrupt handler
  382. * and tasklet.
  383. * @busy: Set to true when transfer is ongoing on this channel.
  384. * @phy_chan: Pointer to physical channel which this instance runs on. If this
  385. * point is NULL, then the channel is not allocated.
  386. * @chan: DMA engine handle.
  387. * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
  388. * transfer and call client callback.
  389. * @client: Cliented owned descriptor list.
  390. * @pending_queue: Submitted jobs, to be issued by issue_pending()
  391. * @active: Active descriptor.
  392. * @done: Completed jobs
  393. * @queue: Queued jobs.
  394. * @prepare_queue: Prepared jobs.
  395. * @dma_cfg: The client configuration of this dma channel.
  396. * @configured: whether the dma_cfg configuration is valid
  397. * @base: Pointer to the device instance struct.
  398. * @src_def_cfg: Default cfg register setting for src.
  399. * @dst_def_cfg: Default cfg register setting for dst.
  400. * @log_def: Default logical channel settings.
  401. * @lcpa: Pointer to dst and src lcpa settings.
  402. * @runtime_addr: runtime configured address.
  403. * @runtime_direction: runtime configured direction.
  404. *
  405. * This struct can either "be" a logical or a physical channel.
  406. */
  407. struct d40_chan {
  408. spinlock_t lock;
  409. int log_num;
  410. int pending_tx;
  411. bool busy;
  412. struct d40_phy_res *phy_chan;
  413. struct dma_chan chan;
  414. struct tasklet_struct tasklet;
  415. struct list_head client;
  416. struct list_head pending_queue;
  417. struct list_head active;
  418. struct list_head done;
  419. struct list_head queue;
  420. struct list_head prepare_queue;
  421. struct stedma40_chan_cfg dma_cfg;
  422. bool configured;
  423. struct d40_base *base;
  424. /* Default register configurations */
  425. u32 src_def_cfg;
  426. u32 dst_def_cfg;
  427. struct d40_def_lcsp log_def;
  428. struct d40_log_lli_full *lcpa;
  429. /* Runtime reconfiguration */
  430. dma_addr_t runtime_addr;
  431. enum dma_transfer_direction runtime_direction;
  432. };
  433. /**
  434. * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
  435. * controller
  436. *
  437. * @backup: the pointer to the registers address array for backup
  438. * @backup_size: the size of the registers address array for backup
  439. * @realtime_en: the realtime enable register
  440. * @realtime_clear: the realtime clear register
  441. * @high_prio_en: the high priority enable register
  442. * @high_prio_clear: the high priority clear register
  443. * @interrupt_en: the interrupt enable register
  444. * @interrupt_clear: the interrupt clear register
  445. * @il: the pointer to struct d40_interrupt_lookup
  446. * @il_size: the size of d40_interrupt_lookup array
  447. * @init_reg: the pointer to the struct d40_reg_val
  448. * @init_reg_size: the size of d40_reg_val array
  449. */
  450. struct d40_gen_dmac {
  451. u32 *backup;
  452. u32 backup_size;
  453. u32 realtime_en;
  454. u32 realtime_clear;
  455. u32 high_prio_en;
  456. u32 high_prio_clear;
  457. u32 interrupt_en;
  458. u32 interrupt_clear;
  459. struct d40_interrupt_lookup *il;
  460. u32 il_size;
  461. struct d40_reg_val *init_reg;
  462. u32 init_reg_size;
  463. };
  464. /**
  465. * struct d40_base - The big global struct, one for each probe'd instance.
  466. *
  467. * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
  468. * @execmd_lock: Lock for execute command usage since several channels share
  469. * the same physical register.
  470. * @dev: The device structure.
  471. * @virtbase: The virtual base address of the DMA's register.
  472. * @rev: silicon revision detected.
  473. * @clk: Pointer to the DMA clock structure.
  474. * @phy_start: Physical memory start of the DMA registers.
  475. * @phy_size: Size of the DMA register map.
  476. * @irq: The IRQ number.
  477. * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
  478. * transfers).
  479. * @num_phy_chans: The number of physical channels. Read from HW. This
  480. * is the number of available channels for this driver, not counting "Secure
  481. * mode" allocated physical channels.
  482. * @num_log_chans: The number of logical channels. Calculated from
  483. * num_phy_chans.
  484. * @dma_both: dma_device channels that can do both memcpy and slave transfers.
  485. * @dma_slave: dma_device channels that can do only do slave transfers.
  486. * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
  487. * @phy_chans: Room for all possible physical channels in system.
  488. * @log_chans: Room for all possible logical channels in system.
  489. * @lookup_log_chans: Used to map interrupt number to logical channel. Points
  490. * to log_chans entries.
  491. * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
  492. * to phy_chans entries.
  493. * @plat_data: Pointer to provided platform_data which is the driver
  494. * configuration.
  495. * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
  496. * @phy_res: Vector containing all physical channels.
  497. * @lcla_pool: lcla pool settings and data.
  498. * @lcpa_base: The virtual mapped address of LCPA.
  499. * @phy_lcpa: The physical address of the LCPA.
  500. * @lcpa_size: The size of the LCPA area.
  501. * @desc_slab: cache for descriptors.
  502. * @reg_val_backup: Here the values of some hardware registers are stored
  503. * before the DMA is powered off. They are restored when the power is back on.
  504. * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
  505. * later
  506. * @reg_val_backup_chan: Backup data for standard channel parameter registers.
  507. * @regs_interrupt: Scratch space for registers during interrupt.
  508. * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
  509. * @gen_dmac: the struct for generic registers values to represent u8500/8540
  510. * DMA controller
  511. */
  512. struct d40_base {
  513. spinlock_t interrupt_lock;
  514. spinlock_t execmd_lock;
  515. struct device *dev;
  516. void __iomem *virtbase;
  517. u8 rev:4;
  518. struct clk *clk;
  519. phys_addr_t phy_start;
  520. resource_size_t phy_size;
  521. int irq;
  522. int num_memcpy_chans;
  523. int num_phy_chans;
  524. int num_log_chans;
  525. struct device_dma_parameters dma_parms;
  526. struct dma_device dma_both;
  527. struct dma_device dma_slave;
  528. struct dma_device dma_memcpy;
  529. struct d40_chan *phy_chans;
  530. struct d40_chan *log_chans;
  531. struct d40_chan **lookup_log_chans;
  532. struct d40_chan **lookup_phy_chans;
  533. struct stedma40_platform_data *plat_data;
  534. struct regulator *lcpa_regulator;
  535. /* Physical half channels */
  536. struct d40_phy_res *phy_res;
  537. struct d40_lcla_pool lcla_pool;
  538. void *lcpa_base;
  539. dma_addr_t phy_lcpa;
  540. resource_size_t lcpa_size;
  541. struct kmem_cache *desc_slab;
  542. u32 reg_val_backup[BACKUP_REGS_SZ];
  543. u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
  544. u32 *reg_val_backup_chan;
  545. u32 *regs_interrupt;
  546. u16 gcc_pwr_off_mask;
  547. struct d40_gen_dmac gen_dmac;
  548. };
  549. static struct device *chan2dev(struct d40_chan *d40c)
  550. {
  551. return &d40c->chan.dev->device;
  552. }
  553. static bool chan_is_physical(struct d40_chan *chan)
  554. {
  555. return chan->log_num == D40_PHY_CHAN;
  556. }
  557. static bool chan_is_logical(struct d40_chan *chan)
  558. {
  559. return !chan_is_physical(chan);
  560. }
  561. static void __iomem *chan_base(struct d40_chan *chan)
  562. {
  563. return chan->base->virtbase + D40_DREG_PCBASE +
  564. chan->phy_chan->num * D40_DREG_PCDELTA;
  565. }
  566. #define d40_err(dev, format, arg...) \
  567. dev_err(dev, "[%s] " format, __func__, ## arg)
  568. #define chan_err(d40c, format, arg...) \
  569. d40_err(chan2dev(d40c), format, ## arg)
  570. static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
  571. int lli_len)
  572. {
  573. bool is_log = chan_is_logical(d40c);
  574. u32 align;
  575. void *base;
  576. if (is_log)
  577. align = sizeof(struct d40_log_lli);
  578. else
  579. align = sizeof(struct d40_phy_lli);
  580. if (lli_len == 1) {
  581. base = d40d->lli_pool.pre_alloc_lli;
  582. d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
  583. d40d->lli_pool.base = NULL;
  584. } else {
  585. d40d->lli_pool.size = lli_len * 2 * align;
  586. base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
  587. d40d->lli_pool.base = base;
  588. if (d40d->lli_pool.base == NULL)
  589. return -ENOMEM;
  590. }
  591. if (is_log) {
  592. d40d->lli_log.src = PTR_ALIGN(base, align);
  593. d40d->lli_log.dst = d40d->lli_log.src + lli_len;
  594. d40d->lli_pool.dma_addr = 0;
  595. } else {
  596. d40d->lli_phy.src = PTR_ALIGN(base, align);
  597. d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
  598. d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
  599. d40d->lli_phy.src,
  600. d40d->lli_pool.size,
  601. DMA_TO_DEVICE);
  602. if (dma_mapping_error(d40c->base->dev,
  603. d40d->lli_pool.dma_addr)) {
  604. kfree(d40d->lli_pool.base);
  605. d40d->lli_pool.base = NULL;
  606. d40d->lli_pool.dma_addr = 0;
  607. return -ENOMEM;
  608. }
  609. }
  610. return 0;
  611. }
  612. static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
  613. {
  614. if (d40d->lli_pool.dma_addr)
  615. dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
  616. d40d->lli_pool.size, DMA_TO_DEVICE);
  617. kfree(d40d->lli_pool.base);
  618. d40d->lli_pool.base = NULL;
  619. d40d->lli_pool.size = 0;
  620. d40d->lli_log.src = NULL;
  621. d40d->lli_log.dst = NULL;
  622. d40d->lli_phy.src = NULL;
  623. d40d->lli_phy.dst = NULL;
  624. }
  625. static int d40_lcla_alloc_one(struct d40_chan *d40c,
  626. struct d40_desc *d40d)
  627. {
  628. unsigned long flags;
  629. int i;
  630. int ret = -EINVAL;
  631. spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
  632. /*
  633. * Allocate both src and dst at the same time, therefore the half
  634. * start on 1 since 0 can't be used since zero is used as end marker.
  635. */
  636. for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
  637. int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
  638. if (!d40c->base->lcla_pool.alloc_map[idx]) {
  639. d40c->base->lcla_pool.alloc_map[idx] = d40d;
  640. d40d->lcla_alloc++;
  641. ret = i;
  642. break;
  643. }
  644. }
  645. spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
  646. return ret;
  647. }
  648. static int d40_lcla_free_all(struct d40_chan *d40c,
  649. struct d40_desc *d40d)
  650. {
  651. unsigned long flags;
  652. int i;
  653. int ret = -EINVAL;
  654. if (chan_is_physical(d40c))
  655. return 0;
  656. spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
  657. for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
  658. int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
  659. if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
  660. d40c->base->lcla_pool.alloc_map[idx] = NULL;
  661. d40d->lcla_alloc--;
  662. if (d40d->lcla_alloc == 0) {
  663. ret = 0;
  664. break;
  665. }
  666. }
  667. }
  668. spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
  669. return ret;
  670. }
  671. static void d40_desc_remove(struct d40_desc *d40d)
  672. {
  673. list_del(&d40d->node);
  674. }
  675. static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
  676. {
  677. struct d40_desc *desc = NULL;
  678. if (!list_empty(&d40c->client)) {
  679. struct d40_desc *d;
  680. struct d40_desc *_d;
  681. list_for_each_entry_safe(d, _d, &d40c->client, node) {
  682. if (async_tx_test_ack(&d->txd)) {
  683. d40_desc_remove(d);
  684. desc = d;
  685. memset(desc, 0, sizeof(*desc));
  686. break;
  687. }
  688. }
  689. }
  690. if (!desc)
  691. desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
  692. if (desc)
  693. INIT_LIST_HEAD(&desc->node);
  694. return desc;
  695. }
  696. static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
  697. {
  698. d40_pool_lli_free(d40c, d40d);
  699. d40_lcla_free_all(d40c, d40d);
  700. kmem_cache_free(d40c->base->desc_slab, d40d);
  701. }
  702. static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
  703. {
  704. list_add_tail(&desc->node, &d40c->active);
  705. }
  706. static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
  707. {
  708. struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
  709. struct d40_phy_lli *lli_src = desc->lli_phy.src;
  710. void __iomem *base = chan_base(chan);
  711. writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
  712. writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
  713. writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
  714. writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
  715. writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
  716. writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
  717. writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
  718. writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
  719. }
  720. static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
  721. {
  722. list_add_tail(&desc->node, &d40c->done);
  723. }
  724. static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
  725. {
  726. struct d40_lcla_pool *pool = &chan->base->lcla_pool;
  727. struct d40_log_lli_bidir *lli = &desc->lli_log;
  728. int lli_current = desc->lli_current;
  729. int lli_len = desc->lli_len;
  730. bool cyclic = desc->cyclic;
  731. int curr_lcla = -EINVAL;
  732. int first_lcla = 0;
  733. bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
  734. bool linkback;
  735. /*
  736. * We may have partially running cyclic transfers, in case we did't get
  737. * enough LCLA entries.
  738. */
  739. linkback = cyclic && lli_current == 0;
  740. /*
  741. * For linkback, we need one LCLA even with only one link, because we
  742. * can't link back to the one in LCPA space
  743. */
  744. if (linkback || (lli_len - lli_current > 1)) {
  745. /*
  746. * If the channel is expected to use only soft_lli don't
  747. * allocate a lcla. This is to avoid a HW issue that exists
  748. * in some controller during a peripheral to memory transfer
  749. * that uses linked lists.
  750. */
  751. if (!(chan->phy_chan->use_soft_lli &&
  752. chan->dma_cfg.dir == DMA_DEV_TO_MEM))
  753. curr_lcla = d40_lcla_alloc_one(chan, desc);
  754. first_lcla = curr_lcla;
  755. }
  756. /*
  757. * For linkback, we normally load the LCPA in the loop since we need to
  758. * link it to the second LCLA and not the first. However, if we
  759. * couldn't even get a first LCLA, then we have to run in LCPA and
  760. * reload manually.
  761. */
  762. if (!linkback || curr_lcla == -EINVAL) {
  763. unsigned int flags = 0;
  764. if (curr_lcla == -EINVAL)
  765. flags |= LLI_TERM_INT;
  766. d40_log_lli_lcpa_write(chan->lcpa,
  767. &lli->dst[lli_current],
  768. &lli->src[lli_current],
  769. curr_lcla,
  770. flags);
  771. lli_current++;
  772. }
  773. if (curr_lcla < 0)
  774. goto set_current;
  775. for (; lli_current < lli_len; lli_current++) {
  776. unsigned int lcla_offset = chan->phy_chan->num * 1024 +
  777. 8 * curr_lcla * 2;
  778. struct d40_log_lli *lcla = pool->base + lcla_offset;
  779. unsigned int flags = 0;
  780. int next_lcla;
  781. if (lli_current + 1 < lli_len)
  782. next_lcla = d40_lcla_alloc_one(chan, desc);
  783. else
  784. next_lcla = linkback ? first_lcla : -EINVAL;
  785. if (cyclic || next_lcla == -EINVAL)
  786. flags |= LLI_TERM_INT;
  787. if (linkback && curr_lcla == first_lcla) {
  788. /* First link goes in both LCPA and LCLA */
  789. d40_log_lli_lcpa_write(chan->lcpa,
  790. &lli->dst[lli_current],
  791. &lli->src[lli_current],
  792. next_lcla, flags);
  793. }
  794. /*
  795. * One unused LCLA in the cyclic case if the very first
  796. * next_lcla fails...
  797. */
  798. d40_log_lli_lcla_write(lcla,
  799. &lli->dst[lli_current],
  800. &lli->src[lli_current],
  801. next_lcla, flags);
  802. /*
  803. * Cache maintenance is not needed if lcla is
  804. * mapped in esram
  805. */
  806. if (!use_esram_lcla) {
  807. dma_sync_single_range_for_device(chan->base->dev,
  808. pool->dma_addr, lcla_offset,
  809. 2 * sizeof(struct d40_log_lli),
  810. DMA_TO_DEVICE);
  811. }
  812. curr_lcla = next_lcla;
  813. if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
  814. lli_current++;
  815. break;
  816. }
  817. }
  818. set_current:
  819. desc->lli_current = lli_current;
  820. }
  821. static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
  822. {
  823. if (chan_is_physical(d40c)) {
  824. d40_phy_lli_load(d40c, d40d);
  825. d40d->lli_current = d40d->lli_len;
  826. } else
  827. d40_log_lli_to_lcxa(d40c, d40d);
  828. }
  829. static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
  830. {
  831. return list_first_entry_or_null(&d40c->active, struct d40_desc, node);
  832. }
  833. /* remove desc from current queue and add it to the pending_queue */
  834. static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
  835. {
  836. d40_desc_remove(desc);
  837. desc->is_in_client_list = false;
  838. list_add_tail(&desc->node, &d40c->pending_queue);
  839. }
  840. static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
  841. {
  842. return list_first_entry_or_null(&d40c->pending_queue, struct d40_desc,
  843. node);
  844. }
  845. static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
  846. {
  847. return list_first_entry_or_null(&d40c->queue, struct d40_desc, node);
  848. }
  849. static struct d40_desc *d40_first_done(struct d40_chan *d40c)
  850. {
  851. return list_first_entry_or_null(&d40c->done, struct d40_desc, node);
  852. }
  853. static int d40_psize_2_burst_size(bool is_log, int psize)
  854. {
  855. if (is_log) {
  856. if (psize == STEDMA40_PSIZE_LOG_1)
  857. return 1;
  858. } else {
  859. if (psize == STEDMA40_PSIZE_PHY_1)
  860. return 1;
  861. }
  862. return 2 << psize;
  863. }
  864. /*
  865. * The dma only supports transmitting packages up to
  866. * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
  867. *
  868. * Calculate the total number of dma elements required to send the entire sg list.
  869. */
  870. static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
  871. {
  872. int dmalen;
  873. u32 max_w = max(data_width1, data_width2);
  874. u32 min_w = min(data_width1, data_width2);
  875. u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
  876. if (seg_max > STEDMA40_MAX_SEG_SIZE)
  877. seg_max -= max_w;
  878. if (!IS_ALIGNED(size, max_w))
  879. return -EINVAL;
  880. if (size <= seg_max)
  881. dmalen = 1;
  882. else {
  883. dmalen = size / seg_max;
  884. if (dmalen * seg_max < size)
  885. dmalen++;
  886. }
  887. return dmalen;
  888. }
  889. static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
  890. u32 data_width1, u32 data_width2)
  891. {
  892. struct scatterlist *sg;
  893. int i;
  894. int len = 0;
  895. int ret;
  896. for_each_sg(sgl, sg, sg_len, i) {
  897. ret = d40_size_2_dmalen(sg_dma_len(sg),
  898. data_width1, data_width2);
  899. if (ret < 0)
  900. return ret;
  901. len += ret;
  902. }
  903. return len;
  904. }
  905. static int __d40_execute_command_phy(struct d40_chan *d40c,
  906. enum d40_command command)
  907. {
  908. u32 status;
  909. int i;
  910. void __iomem *active_reg;
  911. int ret = 0;
  912. unsigned long flags;
  913. u32 wmask;
  914. if (command == D40_DMA_STOP) {
  915. ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
  916. if (ret)
  917. return ret;
  918. }
  919. spin_lock_irqsave(&d40c->base->execmd_lock, flags);
  920. if (d40c->phy_chan->num % 2 == 0)
  921. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  922. else
  923. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  924. if (command == D40_DMA_SUSPEND_REQ) {
  925. status = (readl(active_reg) &
  926. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  927. D40_CHAN_POS(d40c->phy_chan->num);
  928. if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
  929. goto unlock;
  930. }
  931. wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
  932. writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
  933. active_reg);
  934. if (command == D40_DMA_SUSPEND_REQ) {
  935. for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
  936. status = (readl(active_reg) &
  937. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  938. D40_CHAN_POS(d40c->phy_chan->num);
  939. cpu_relax();
  940. /*
  941. * Reduce the number of bus accesses while
  942. * waiting for the DMA to suspend.
  943. */
  944. udelay(3);
  945. if (status == D40_DMA_STOP ||
  946. status == D40_DMA_SUSPENDED)
  947. break;
  948. }
  949. if (i == D40_SUSPEND_MAX_IT) {
  950. chan_err(d40c,
  951. "unable to suspend the chl %d (log: %d) status %x\n",
  952. d40c->phy_chan->num, d40c->log_num,
  953. status);
  954. dump_stack();
  955. ret = -EBUSY;
  956. }
  957. }
  958. unlock:
  959. spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
  960. return ret;
  961. }
  962. static void d40_term_all(struct d40_chan *d40c)
  963. {
  964. struct d40_desc *d40d;
  965. struct d40_desc *_d;
  966. /* Release completed descriptors */
  967. while ((d40d = d40_first_done(d40c))) {
  968. d40_desc_remove(d40d);
  969. d40_desc_free(d40c, d40d);
  970. }
  971. /* Release active descriptors */
  972. while ((d40d = d40_first_active_get(d40c))) {
  973. d40_desc_remove(d40d);
  974. d40_desc_free(d40c, d40d);
  975. }
  976. /* Release queued descriptors waiting for transfer */
  977. while ((d40d = d40_first_queued(d40c))) {
  978. d40_desc_remove(d40d);
  979. d40_desc_free(d40c, d40d);
  980. }
  981. /* Release pending descriptors */
  982. while ((d40d = d40_first_pending(d40c))) {
  983. d40_desc_remove(d40d);
  984. d40_desc_free(d40c, d40d);
  985. }
  986. /* Release client owned descriptors */
  987. if (!list_empty(&d40c->client))
  988. list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
  989. d40_desc_remove(d40d);
  990. d40_desc_free(d40c, d40d);
  991. }
  992. /* Release descriptors in prepare queue */
  993. if (!list_empty(&d40c->prepare_queue))
  994. list_for_each_entry_safe(d40d, _d,
  995. &d40c->prepare_queue, node) {
  996. d40_desc_remove(d40d);
  997. d40_desc_free(d40c, d40d);
  998. }
  999. d40c->pending_tx = 0;
  1000. }
  1001. static void __d40_config_set_event(struct d40_chan *d40c,
  1002. enum d40_events event_type, u32 event,
  1003. int reg)
  1004. {
  1005. void __iomem *addr = chan_base(d40c) + reg;
  1006. int tries;
  1007. u32 status;
  1008. switch (event_type) {
  1009. case D40_DEACTIVATE_EVENTLINE:
  1010. writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
  1011. | ~D40_EVENTLINE_MASK(event), addr);
  1012. break;
  1013. case D40_SUSPEND_REQ_EVENTLINE:
  1014. status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
  1015. D40_EVENTLINE_POS(event);
  1016. if (status == D40_DEACTIVATE_EVENTLINE ||
  1017. status == D40_SUSPEND_REQ_EVENTLINE)
  1018. break;
  1019. writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
  1020. | ~D40_EVENTLINE_MASK(event), addr);
  1021. for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
  1022. status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
  1023. D40_EVENTLINE_POS(event);
  1024. cpu_relax();
  1025. /*
  1026. * Reduce the number of bus accesses while
  1027. * waiting for the DMA to suspend.
  1028. */
  1029. udelay(3);
  1030. if (status == D40_DEACTIVATE_EVENTLINE)
  1031. break;
  1032. }
  1033. if (tries == D40_SUSPEND_MAX_IT) {
  1034. chan_err(d40c,
  1035. "unable to stop the event_line chl %d (log: %d)"
  1036. "status %x\n", d40c->phy_chan->num,
  1037. d40c->log_num, status);
  1038. }
  1039. break;
  1040. case D40_ACTIVATE_EVENTLINE:
  1041. /*
  1042. * The hardware sometimes doesn't register the enable when src and dst
  1043. * event lines are active on the same logical channel. Retry to ensure
  1044. * it does. Usually only one retry is sufficient.
  1045. */
  1046. tries = 100;
  1047. while (--tries) {
  1048. writel((D40_ACTIVATE_EVENTLINE <<
  1049. D40_EVENTLINE_POS(event)) |
  1050. ~D40_EVENTLINE_MASK(event), addr);
  1051. if (readl(addr) & D40_EVENTLINE_MASK(event))
  1052. break;
  1053. }
  1054. if (tries != 99)
  1055. dev_dbg(chan2dev(d40c),
  1056. "[%s] workaround enable S%cLNK (%d tries)\n",
  1057. __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
  1058. 100 - tries);
  1059. WARN_ON(!tries);
  1060. break;
  1061. case D40_ROUND_EVENTLINE:
  1062. BUG();
  1063. break;
  1064. }
  1065. }
  1066. static void d40_config_set_event(struct d40_chan *d40c,
  1067. enum d40_events event_type)
  1068. {
  1069. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1070. /* Enable event line connected to device (or memcpy) */
  1071. if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
  1072. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1073. __d40_config_set_event(d40c, event_type, event,
  1074. D40_CHAN_REG_SSLNK);
  1075. if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
  1076. __d40_config_set_event(d40c, event_type, event,
  1077. D40_CHAN_REG_SDLNK);
  1078. }
  1079. static u32 d40_chan_has_events(struct d40_chan *d40c)
  1080. {
  1081. void __iomem *chanbase = chan_base(d40c);
  1082. u32 val;
  1083. val = readl(chanbase + D40_CHAN_REG_SSLNK);
  1084. val |= readl(chanbase + D40_CHAN_REG_SDLNK);
  1085. return val;
  1086. }
  1087. static int
  1088. __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
  1089. {
  1090. unsigned long flags;
  1091. int ret = 0;
  1092. u32 active_status;
  1093. void __iomem *active_reg;
  1094. if (d40c->phy_chan->num % 2 == 0)
  1095. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  1096. else
  1097. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  1098. spin_lock_irqsave(&d40c->phy_chan->lock, flags);
  1099. switch (command) {
  1100. case D40_DMA_STOP:
  1101. case D40_DMA_SUSPEND_REQ:
  1102. active_status = (readl(active_reg) &
  1103. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  1104. D40_CHAN_POS(d40c->phy_chan->num);
  1105. if (active_status == D40_DMA_RUN)
  1106. d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
  1107. else
  1108. d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
  1109. if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
  1110. ret = __d40_execute_command_phy(d40c, command);
  1111. break;
  1112. case D40_DMA_RUN:
  1113. d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
  1114. ret = __d40_execute_command_phy(d40c, command);
  1115. break;
  1116. case D40_DMA_SUSPENDED:
  1117. BUG();
  1118. break;
  1119. }
  1120. spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
  1121. return ret;
  1122. }
  1123. static int d40_channel_execute_command(struct d40_chan *d40c,
  1124. enum d40_command command)
  1125. {
  1126. if (chan_is_logical(d40c))
  1127. return __d40_execute_command_log(d40c, command);
  1128. else
  1129. return __d40_execute_command_phy(d40c, command);
  1130. }
  1131. static u32 d40_get_prmo(struct d40_chan *d40c)
  1132. {
  1133. static const unsigned int phy_map[] = {
  1134. [STEDMA40_PCHAN_BASIC_MODE]
  1135. = D40_DREG_PRMO_PCHAN_BASIC,
  1136. [STEDMA40_PCHAN_MODULO_MODE]
  1137. = D40_DREG_PRMO_PCHAN_MODULO,
  1138. [STEDMA40_PCHAN_DOUBLE_DST_MODE]
  1139. = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
  1140. };
  1141. static const unsigned int log_map[] = {
  1142. [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
  1143. = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
  1144. [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
  1145. = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
  1146. [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
  1147. = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
  1148. };
  1149. if (chan_is_physical(d40c))
  1150. return phy_map[d40c->dma_cfg.mode_opt];
  1151. else
  1152. return log_map[d40c->dma_cfg.mode_opt];
  1153. }
  1154. static void d40_config_write(struct d40_chan *d40c)
  1155. {
  1156. u32 addr_base;
  1157. u32 var;
  1158. /* Odd addresses are even addresses + 4 */
  1159. addr_base = (d40c->phy_chan->num % 2) * 4;
  1160. /* Setup channel mode to logical or physical */
  1161. var = ((u32)(chan_is_logical(d40c)) + 1) <<
  1162. D40_CHAN_POS(d40c->phy_chan->num);
  1163. writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
  1164. /* Setup operational mode option register */
  1165. var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
  1166. writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
  1167. if (chan_is_logical(d40c)) {
  1168. int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
  1169. & D40_SREG_ELEM_LOG_LIDX_MASK;
  1170. void __iomem *chanbase = chan_base(d40c);
  1171. /* Set default config for CFG reg */
  1172. writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
  1173. writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
  1174. /* Set LIDX for lcla */
  1175. writel(lidx, chanbase + D40_CHAN_REG_SSELT);
  1176. writel(lidx, chanbase + D40_CHAN_REG_SDELT);
  1177. /* Clear LNK which will be used by d40_chan_has_events() */
  1178. writel(0, chanbase + D40_CHAN_REG_SSLNK);
  1179. writel(0, chanbase + D40_CHAN_REG_SDLNK);
  1180. }
  1181. }
  1182. static u32 d40_residue(struct d40_chan *d40c)
  1183. {
  1184. u32 num_elt;
  1185. if (chan_is_logical(d40c))
  1186. num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
  1187. >> D40_MEM_LCSP2_ECNT_POS;
  1188. else {
  1189. u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
  1190. num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
  1191. >> D40_SREG_ELEM_PHY_ECNT_POS;
  1192. }
  1193. return num_elt * d40c->dma_cfg.dst_info.data_width;
  1194. }
  1195. static bool d40_tx_is_linked(struct d40_chan *d40c)
  1196. {
  1197. bool is_link;
  1198. if (chan_is_logical(d40c))
  1199. is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
  1200. else
  1201. is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
  1202. & D40_SREG_LNK_PHYS_LNK_MASK;
  1203. return is_link;
  1204. }
  1205. static int d40_pause(struct dma_chan *chan)
  1206. {
  1207. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1208. int res = 0;
  1209. unsigned long flags;
  1210. if (d40c->phy_chan == NULL) {
  1211. chan_err(d40c, "Channel is not allocated!\n");
  1212. return -EINVAL;
  1213. }
  1214. if (!d40c->busy)
  1215. return 0;
  1216. spin_lock_irqsave(&d40c->lock, flags);
  1217. pm_runtime_get_sync(d40c->base->dev);
  1218. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  1219. pm_runtime_mark_last_busy(d40c->base->dev);
  1220. pm_runtime_put_autosuspend(d40c->base->dev);
  1221. spin_unlock_irqrestore(&d40c->lock, flags);
  1222. return res;
  1223. }
  1224. static int d40_resume(struct dma_chan *chan)
  1225. {
  1226. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1227. int res = 0;
  1228. unsigned long flags;
  1229. if (d40c->phy_chan == NULL) {
  1230. chan_err(d40c, "Channel is not allocated!\n");
  1231. return -EINVAL;
  1232. }
  1233. if (!d40c->busy)
  1234. return 0;
  1235. spin_lock_irqsave(&d40c->lock, flags);
  1236. pm_runtime_get_sync(d40c->base->dev);
  1237. /* If bytes left to transfer or linked tx resume job */
  1238. if (d40_residue(d40c) || d40_tx_is_linked(d40c))
  1239. res = d40_channel_execute_command(d40c, D40_DMA_RUN);
  1240. pm_runtime_mark_last_busy(d40c->base->dev);
  1241. pm_runtime_put_autosuspend(d40c->base->dev);
  1242. spin_unlock_irqrestore(&d40c->lock, flags);
  1243. return res;
  1244. }
  1245. static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
  1246. {
  1247. struct d40_chan *d40c = container_of(tx->chan,
  1248. struct d40_chan,
  1249. chan);
  1250. struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
  1251. unsigned long flags;
  1252. dma_cookie_t cookie;
  1253. spin_lock_irqsave(&d40c->lock, flags);
  1254. cookie = dma_cookie_assign(tx);
  1255. d40_desc_queue(d40c, d40d);
  1256. spin_unlock_irqrestore(&d40c->lock, flags);
  1257. return cookie;
  1258. }
  1259. static int d40_start(struct d40_chan *d40c)
  1260. {
  1261. return d40_channel_execute_command(d40c, D40_DMA_RUN);
  1262. }
  1263. static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
  1264. {
  1265. struct d40_desc *d40d;
  1266. int err;
  1267. /* Start queued jobs, if any */
  1268. d40d = d40_first_queued(d40c);
  1269. if (d40d != NULL) {
  1270. if (!d40c->busy) {
  1271. d40c->busy = true;
  1272. pm_runtime_get_sync(d40c->base->dev);
  1273. }
  1274. /* Remove from queue */
  1275. d40_desc_remove(d40d);
  1276. /* Add to active queue */
  1277. d40_desc_submit(d40c, d40d);
  1278. /* Initiate DMA job */
  1279. d40_desc_load(d40c, d40d);
  1280. /* Start dma job */
  1281. err = d40_start(d40c);
  1282. if (err)
  1283. return NULL;
  1284. }
  1285. return d40d;
  1286. }
  1287. /* called from interrupt context */
  1288. static void dma_tc_handle(struct d40_chan *d40c)
  1289. {
  1290. struct d40_desc *d40d;
  1291. /* Get first active entry from list */
  1292. d40d = d40_first_active_get(d40c);
  1293. if (d40d == NULL)
  1294. return;
  1295. if (d40d->cyclic) {
  1296. /*
  1297. * If this was a paritially loaded list, we need to reloaded
  1298. * it, and only when the list is completed. We need to check
  1299. * for done because the interrupt will hit for every link, and
  1300. * not just the last one.
  1301. */
  1302. if (d40d->lli_current < d40d->lli_len
  1303. && !d40_tx_is_linked(d40c)
  1304. && !d40_residue(d40c)) {
  1305. d40_lcla_free_all(d40c, d40d);
  1306. d40_desc_load(d40c, d40d);
  1307. (void) d40_start(d40c);
  1308. if (d40d->lli_current == d40d->lli_len)
  1309. d40d->lli_current = 0;
  1310. }
  1311. } else {
  1312. d40_lcla_free_all(d40c, d40d);
  1313. if (d40d->lli_current < d40d->lli_len) {
  1314. d40_desc_load(d40c, d40d);
  1315. /* Start dma job */
  1316. (void) d40_start(d40c);
  1317. return;
  1318. }
  1319. if (d40_queue_start(d40c) == NULL) {
  1320. d40c->busy = false;
  1321. pm_runtime_mark_last_busy(d40c->base->dev);
  1322. pm_runtime_put_autosuspend(d40c->base->dev);
  1323. }
  1324. d40_desc_remove(d40d);
  1325. d40_desc_done(d40c, d40d);
  1326. }
  1327. d40c->pending_tx++;
  1328. tasklet_schedule(&d40c->tasklet);
  1329. }
  1330. static void dma_tasklet(unsigned long data)
  1331. {
  1332. struct d40_chan *d40c = (struct d40_chan *) data;
  1333. struct d40_desc *d40d;
  1334. unsigned long flags;
  1335. bool callback_active;
  1336. struct dmaengine_desc_callback cb;
  1337. spin_lock_irqsave(&d40c->lock, flags);
  1338. /* Get first entry from the done list */
  1339. d40d = d40_first_done(d40c);
  1340. if (d40d == NULL) {
  1341. /* Check if we have reached here for cyclic job */
  1342. d40d = d40_first_active_get(d40c);
  1343. if (d40d == NULL || !d40d->cyclic)
  1344. goto check_pending_tx;
  1345. }
  1346. if (!d40d->cyclic)
  1347. dma_cookie_complete(&d40d->txd);
  1348. /*
  1349. * If terminating a channel pending_tx is set to zero.
  1350. * This prevents any finished active jobs to return to the client.
  1351. */
  1352. if (d40c->pending_tx == 0) {
  1353. spin_unlock_irqrestore(&d40c->lock, flags);
  1354. return;
  1355. }
  1356. /* Callback to client */
  1357. callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
  1358. dmaengine_desc_get_callback(&d40d->txd, &cb);
  1359. if (!d40d->cyclic) {
  1360. if (async_tx_test_ack(&d40d->txd)) {
  1361. d40_desc_remove(d40d);
  1362. d40_desc_free(d40c, d40d);
  1363. } else if (!d40d->is_in_client_list) {
  1364. d40_desc_remove(d40d);
  1365. d40_lcla_free_all(d40c, d40d);
  1366. list_add_tail(&d40d->node, &d40c->client);
  1367. d40d->is_in_client_list = true;
  1368. }
  1369. }
  1370. d40c->pending_tx--;
  1371. if (d40c->pending_tx)
  1372. tasklet_schedule(&d40c->tasklet);
  1373. spin_unlock_irqrestore(&d40c->lock, flags);
  1374. if (callback_active)
  1375. dmaengine_desc_callback_invoke(&cb, NULL);
  1376. return;
  1377. check_pending_tx:
  1378. /* Rescue manouver if receiving double interrupts */
  1379. if (d40c->pending_tx > 0)
  1380. d40c->pending_tx--;
  1381. spin_unlock_irqrestore(&d40c->lock, flags);
  1382. }
  1383. static irqreturn_t d40_handle_interrupt(int irq, void *data)
  1384. {
  1385. int i;
  1386. u32 idx;
  1387. u32 row;
  1388. long chan = -1;
  1389. struct d40_chan *d40c;
  1390. unsigned long flags;
  1391. struct d40_base *base = data;
  1392. u32 *regs = base->regs_interrupt;
  1393. struct d40_interrupt_lookup *il = base->gen_dmac.il;
  1394. u32 il_size = base->gen_dmac.il_size;
  1395. spin_lock_irqsave(&base->interrupt_lock, flags);
  1396. /* Read interrupt status of both logical and physical channels */
  1397. for (i = 0; i < il_size; i++)
  1398. regs[i] = readl(base->virtbase + il[i].src);
  1399. for (;;) {
  1400. chan = find_next_bit((unsigned long *)regs,
  1401. BITS_PER_LONG * il_size, chan + 1);
  1402. /* No more set bits found? */
  1403. if (chan == BITS_PER_LONG * il_size)
  1404. break;
  1405. row = chan / BITS_PER_LONG;
  1406. idx = chan & (BITS_PER_LONG - 1);
  1407. if (il[row].offset == D40_PHY_CHAN)
  1408. d40c = base->lookup_phy_chans[idx];
  1409. else
  1410. d40c = base->lookup_log_chans[il[row].offset + idx];
  1411. if (!d40c) {
  1412. /*
  1413. * No error because this can happen if something else
  1414. * in the system is using the channel.
  1415. */
  1416. continue;
  1417. }
  1418. /* ACK interrupt */
  1419. writel(BIT(idx), base->virtbase + il[row].clr);
  1420. spin_lock(&d40c->lock);
  1421. if (!il[row].is_error)
  1422. dma_tc_handle(d40c);
  1423. else
  1424. d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
  1425. chan, il[row].offset, idx);
  1426. spin_unlock(&d40c->lock);
  1427. }
  1428. spin_unlock_irqrestore(&base->interrupt_lock, flags);
  1429. return IRQ_HANDLED;
  1430. }
  1431. static int d40_validate_conf(struct d40_chan *d40c,
  1432. struct stedma40_chan_cfg *conf)
  1433. {
  1434. int res = 0;
  1435. bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
  1436. if (!conf->dir) {
  1437. chan_err(d40c, "Invalid direction.\n");
  1438. res = -EINVAL;
  1439. }
  1440. if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
  1441. (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
  1442. (conf->dev_type < 0)) {
  1443. chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
  1444. res = -EINVAL;
  1445. }
  1446. if (conf->dir == DMA_DEV_TO_DEV) {
  1447. /*
  1448. * DMAC HW supports it. Will be added to this driver,
  1449. * in case any dma client requires it.
  1450. */
  1451. chan_err(d40c, "periph to periph not supported\n");
  1452. res = -EINVAL;
  1453. }
  1454. if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
  1455. conf->src_info.data_width !=
  1456. d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
  1457. conf->dst_info.data_width) {
  1458. /*
  1459. * The DMAC hardware only supports
  1460. * src (burst x width) == dst (burst x width)
  1461. */
  1462. chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
  1463. res = -EINVAL;
  1464. }
  1465. return res;
  1466. }
  1467. static bool d40_alloc_mask_set(struct d40_phy_res *phy,
  1468. bool is_src, int log_event_line, bool is_log,
  1469. bool *first_user)
  1470. {
  1471. unsigned long flags;
  1472. spin_lock_irqsave(&phy->lock, flags);
  1473. *first_user = ((phy->allocated_src | phy->allocated_dst)
  1474. == D40_ALLOC_FREE);
  1475. if (!is_log) {
  1476. /* Physical interrupts are masked per physical full channel */
  1477. if (phy->allocated_src == D40_ALLOC_FREE &&
  1478. phy->allocated_dst == D40_ALLOC_FREE) {
  1479. phy->allocated_dst = D40_ALLOC_PHY;
  1480. phy->allocated_src = D40_ALLOC_PHY;
  1481. goto found_unlock;
  1482. } else
  1483. goto not_found_unlock;
  1484. }
  1485. /* Logical channel */
  1486. if (is_src) {
  1487. if (phy->allocated_src == D40_ALLOC_PHY)
  1488. goto not_found_unlock;
  1489. if (phy->allocated_src == D40_ALLOC_FREE)
  1490. phy->allocated_src = D40_ALLOC_LOG_FREE;
  1491. if (!(phy->allocated_src & BIT(log_event_line))) {
  1492. phy->allocated_src |= BIT(log_event_line);
  1493. goto found_unlock;
  1494. } else
  1495. goto not_found_unlock;
  1496. } else {
  1497. if (phy->allocated_dst == D40_ALLOC_PHY)
  1498. goto not_found_unlock;
  1499. if (phy->allocated_dst == D40_ALLOC_FREE)
  1500. phy->allocated_dst = D40_ALLOC_LOG_FREE;
  1501. if (!(phy->allocated_dst & BIT(log_event_line))) {
  1502. phy->allocated_dst |= BIT(log_event_line);
  1503. goto found_unlock;
  1504. }
  1505. }
  1506. not_found_unlock:
  1507. spin_unlock_irqrestore(&phy->lock, flags);
  1508. return false;
  1509. found_unlock:
  1510. spin_unlock_irqrestore(&phy->lock, flags);
  1511. return true;
  1512. }
  1513. static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
  1514. int log_event_line)
  1515. {
  1516. unsigned long flags;
  1517. bool is_free = false;
  1518. spin_lock_irqsave(&phy->lock, flags);
  1519. if (!log_event_line) {
  1520. phy->allocated_dst = D40_ALLOC_FREE;
  1521. phy->allocated_src = D40_ALLOC_FREE;
  1522. is_free = true;
  1523. goto unlock;
  1524. }
  1525. /* Logical channel */
  1526. if (is_src) {
  1527. phy->allocated_src &= ~BIT(log_event_line);
  1528. if (phy->allocated_src == D40_ALLOC_LOG_FREE)
  1529. phy->allocated_src = D40_ALLOC_FREE;
  1530. } else {
  1531. phy->allocated_dst &= ~BIT(log_event_line);
  1532. if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
  1533. phy->allocated_dst = D40_ALLOC_FREE;
  1534. }
  1535. is_free = ((phy->allocated_src | phy->allocated_dst) ==
  1536. D40_ALLOC_FREE);
  1537. unlock:
  1538. spin_unlock_irqrestore(&phy->lock, flags);
  1539. return is_free;
  1540. }
  1541. static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
  1542. {
  1543. int dev_type = d40c->dma_cfg.dev_type;
  1544. int event_group;
  1545. int event_line;
  1546. struct d40_phy_res *phys;
  1547. int i;
  1548. int j;
  1549. int log_num;
  1550. int num_phy_chans;
  1551. bool is_src;
  1552. bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
  1553. phys = d40c->base->phy_res;
  1554. num_phy_chans = d40c->base->num_phy_chans;
  1555. if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
  1556. log_num = 2 * dev_type;
  1557. is_src = true;
  1558. } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1559. d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1560. /* dst event lines are used for logical memcpy */
  1561. log_num = 2 * dev_type + 1;
  1562. is_src = false;
  1563. } else
  1564. return -EINVAL;
  1565. event_group = D40_TYPE_TO_GROUP(dev_type);
  1566. event_line = D40_TYPE_TO_EVENT(dev_type);
  1567. if (!is_log) {
  1568. if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1569. /* Find physical half channel */
  1570. if (d40c->dma_cfg.use_fixed_channel) {
  1571. i = d40c->dma_cfg.phy_channel;
  1572. if (d40_alloc_mask_set(&phys[i], is_src,
  1573. 0, is_log,
  1574. first_phy_user))
  1575. goto found_phy;
  1576. } else {
  1577. for (i = 0; i < num_phy_chans; i++) {
  1578. if (d40_alloc_mask_set(&phys[i], is_src,
  1579. 0, is_log,
  1580. first_phy_user))
  1581. goto found_phy;
  1582. }
  1583. }
  1584. } else
  1585. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  1586. int phy_num = j + event_group * 2;
  1587. for (i = phy_num; i < phy_num + 2; i++) {
  1588. if (d40_alloc_mask_set(&phys[i],
  1589. is_src,
  1590. 0,
  1591. is_log,
  1592. first_phy_user))
  1593. goto found_phy;
  1594. }
  1595. }
  1596. return -EINVAL;
  1597. found_phy:
  1598. d40c->phy_chan = &phys[i];
  1599. d40c->log_num = D40_PHY_CHAN;
  1600. goto out;
  1601. }
  1602. if (dev_type == -1)
  1603. return -EINVAL;
  1604. /* Find logical channel */
  1605. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  1606. int phy_num = j + event_group * 2;
  1607. if (d40c->dma_cfg.use_fixed_channel) {
  1608. i = d40c->dma_cfg.phy_channel;
  1609. if ((i != phy_num) && (i != phy_num + 1)) {
  1610. dev_err(chan2dev(d40c),
  1611. "invalid fixed phy channel %d\n", i);
  1612. return -EINVAL;
  1613. }
  1614. if (d40_alloc_mask_set(&phys[i], is_src, event_line,
  1615. is_log, first_phy_user))
  1616. goto found_log;
  1617. dev_err(chan2dev(d40c),
  1618. "could not allocate fixed phy channel %d\n", i);
  1619. return -EINVAL;
  1620. }
  1621. /*
  1622. * Spread logical channels across all available physical rather
  1623. * than pack every logical channel at the first available phy
  1624. * channels.
  1625. */
  1626. if (is_src) {
  1627. for (i = phy_num; i < phy_num + 2; i++) {
  1628. if (d40_alloc_mask_set(&phys[i], is_src,
  1629. event_line, is_log,
  1630. first_phy_user))
  1631. goto found_log;
  1632. }
  1633. } else {
  1634. for (i = phy_num + 1; i >= phy_num; i--) {
  1635. if (d40_alloc_mask_set(&phys[i], is_src,
  1636. event_line, is_log,
  1637. first_phy_user))
  1638. goto found_log;
  1639. }
  1640. }
  1641. }
  1642. return -EINVAL;
  1643. found_log:
  1644. d40c->phy_chan = &phys[i];
  1645. d40c->log_num = log_num;
  1646. out:
  1647. if (is_log)
  1648. d40c->base->lookup_log_chans[d40c->log_num] = d40c;
  1649. else
  1650. d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
  1651. return 0;
  1652. }
  1653. static int d40_config_memcpy(struct d40_chan *d40c)
  1654. {
  1655. dma_cap_mask_t cap = d40c->chan.device->cap_mask;
  1656. if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
  1657. d40c->dma_cfg = dma40_memcpy_conf_log;
  1658. d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
  1659. d40_log_cfg(&d40c->dma_cfg,
  1660. &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
  1661. } else if (dma_has_cap(DMA_MEMCPY, cap) &&
  1662. dma_has_cap(DMA_SLAVE, cap)) {
  1663. d40c->dma_cfg = dma40_memcpy_conf_phy;
  1664. /* Generate interrrupt at end of transfer or relink. */
  1665. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
  1666. /* Generate interrupt on error. */
  1667. d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
  1668. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
  1669. } else {
  1670. chan_err(d40c, "No memcpy\n");
  1671. return -EINVAL;
  1672. }
  1673. return 0;
  1674. }
  1675. static int d40_free_dma(struct d40_chan *d40c)
  1676. {
  1677. int res = 0;
  1678. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1679. struct d40_phy_res *phy = d40c->phy_chan;
  1680. bool is_src;
  1681. /* Terminate all queued and active transfers */
  1682. d40_term_all(d40c);
  1683. if (phy == NULL) {
  1684. chan_err(d40c, "phy == null\n");
  1685. return -EINVAL;
  1686. }
  1687. if (phy->allocated_src == D40_ALLOC_FREE &&
  1688. phy->allocated_dst == D40_ALLOC_FREE) {
  1689. chan_err(d40c, "channel already free\n");
  1690. return -EINVAL;
  1691. }
  1692. if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1693. d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
  1694. is_src = false;
  1695. else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
  1696. is_src = true;
  1697. else {
  1698. chan_err(d40c, "Unknown direction\n");
  1699. return -EINVAL;
  1700. }
  1701. pm_runtime_get_sync(d40c->base->dev);
  1702. res = d40_channel_execute_command(d40c, D40_DMA_STOP);
  1703. if (res) {
  1704. chan_err(d40c, "stop failed\n");
  1705. goto mark_last_busy;
  1706. }
  1707. d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
  1708. if (chan_is_logical(d40c))
  1709. d40c->base->lookup_log_chans[d40c->log_num] = NULL;
  1710. else
  1711. d40c->base->lookup_phy_chans[phy->num] = NULL;
  1712. if (d40c->busy) {
  1713. pm_runtime_mark_last_busy(d40c->base->dev);
  1714. pm_runtime_put_autosuspend(d40c->base->dev);
  1715. }
  1716. d40c->busy = false;
  1717. d40c->phy_chan = NULL;
  1718. d40c->configured = false;
  1719. mark_last_busy:
  1720. pm_runtime_mark_last_busy(d40c->base->dev);
  1721. pm_runtime_put_autosuspend(d40c->base->dev);
  1722. return res;
  1723. }
  1724. static bool d40_is_paused(struct d40_chan *d40c)
  1725. {
  1726. void __iomem *chanbase = chan_base(d40c);
  1727. bool is_paused = false;
  1728. unsigned long flags;
  1729. void __iomem *active_reg;
  1730. u32 status;
  1731. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1732. spin_lock_irqsave(&d40c->lock, flags);
  1733. if (chan_is_physical(d40c)) {
  1734. if (d40c->phy_chan->num % 2 == 0)
  1735. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  1736. else
  1737. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  1738. status = (readl(active_reg) &
  1739. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  1740. D40_CHAN_POS(d40c->phy_chan->num);
  1741. if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
  1742. is_paused = true;
  1743. goto unlock;
  1744. }
  1745. if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1746. d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1747. status = readl(chanbase + D40_CHAN_REG_SDLNK);
  1748. } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
  1749. status = readl(chanbase + D40_CHAN_REG_SSLNK);
  1750. } else {
  1751. chan_err(d40c, "Unknown direction\n");
  1752. goto unlock;
  1753. }
  1754. status = (status & D40_EVENTLINE_MASK(event)) >>
  1755. D40_EVENTLINE_POS(event);
  1756. if (status != D40_DMA_RUN)
  1757. is_paused = true;
  1758. unlock:
  1759. spin_unlock_irqrestore(&d40c->lock, flags);
  1760. return is_paused;
  1761. }
  1762. static u32 stedma40_residue(struct dma_chan *chan)
  1763. {
  1764. struct d40_chan *d40c =
  1765. container_of(chan, struct d40_chan, chan);
  1766. u32 bytes_left;
  1767. unsigned long flags;
  1768. spin_lock_irqsave(&d40c->lock, flags);
  1769. bytes_left = d40_residue(d40c);
  1770. spin_unlock_irqrestore(&d40c->lock, flags);
  1771. return bytes_left;
  1772. }
  1773. static int
  1774. d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
  1775. struct scatterlist *sg_src, struct scatterlist *sg_dst,
  1776. unsigned int sg_len, dma_addr_t src_dev_addr,
  1777. dma_addr_t dst_dev_addr)
  1778. {
  1779. struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
  1780. struct stedma40_half_channel_info *src_info = &cfg->src_info;
  1781. struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
  1782. int ret;
  1783. ret = d40_log_sg_to_lli(sg_src, sg_len,
  1784. src_dev_addr,
  1785. desc->lli_log.src,
  1786. chan->log_def.lcsp1,
  1787. src_info->data_width,
  1788. dst_info->data_width);
  1789. ret = d40_log_sg_to_lli(sg_dst, sg_len,
  1790. dst_dev_addr,
  1791. desc->lli_log.dst,
  1792. chan->log_def.lcsp3,
  1793. dst_info->data_width,
  1794. src_info->data_width);
  1795. return ret < 0 ? ret : 0;
  1796. }
  1797. static int
  1798. d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
  1799. struct scatterlist *sg_src, struct scatterlist *sg_dst,
  1800. unsigned int sg_len, dma_addr_t src_dev_addr,
  1801. dma_addr_t dst_dev_addr)
  1802. {
  1803. struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
  1804. struct stedma40_half_channel_info *src_info = &cfg->src_info;
  1805. struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
  1806. unsigned long flags = 0;
  1807. int ret;
  1808. if (desc->cyclic)
  1809. flags |= LLI_CYCLIC | LLI_TERM_INT;
  1810. ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
  1811. desc->lli_phy.src,
  1812. virt_to_phys(desc->lli_phy.src),
  1813. chan->src_def_cfg,
  1814. src_info, dst_info, flags);
  1815. ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
  1816. desc->lli_phy.dst,
  1817. virt_to_phys(desc->lli_phy.dst),
  1818. chan->dst_def_cfg,
  1819. dst_info, src_info, flags);
  1820. dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
  1821. desc->lli_pool.size, DMA_TO_DEVICE);
  1822. return ret < 0 ? ret : 0;
  1823. }
  1824. static struct d40_desc *
  1825. d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
  1826. unsigned int sg_len, unsigned long dma_flags)
  1827. {
  1828. struct stedma40_chan_cfg *cfg;
  1829. struct d40_desc *desc;
  1830. int ret;
  1831. desc = d40_desc_get(chan);
  1832. if (!desc)
  1833. return NULL;
  1834. cfg = &chan->dma_cfg;
  1835. desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
  1836. cfg->dst_info.data_width);
  1837. if (desc->lli_len < 0) {
  1838. chan_err(chan, "Unaligned size\n");
  1839. goto free_desc;
  1840. }
  1841. ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
  1842. if (ret < 0) {
  1843. chan_err(chan, "Could not allocate lli\n");
  1844. goto free_desc;
  1845. }
  1846. desc->lli_current = 0;
  1847. desc->txd.flags = dma_flags;
  1848. desc->txd.tx_submit = d40_tx_submit;
  1849. dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
  1850. return desc;
  1851. free_desc:
  1852. d40_desc_free(chan, desc);
  1853. return NULL;
  1854. }
  1855. static struct dma_async_tx_descriptor *
  1856. d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
  1857. struct scatterlist *sg_dst, unsigned int sg_len,
  1858. enum dma_transfer_direction direction, unsigned long dma_flags)
  1859. {
  1860. struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
  1861. dma_addr_t src_dev_addr;
  1862. dma_addr_t dst_dev_addr;
  1863. struct d40_desc *desc;
  1864. unsigned long flags;
  1865. int ret;
  1866. if (!chan->phy_chan) {
  1867. chan_err(chan, "Cannot prepare unallocated channel\n");
  1868. return NULL;
  1869. }
  1870. spin_lock_irqsave(&chan->lock, flags);
  1871. desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
  1872. if (desc == NULL)
  1873. goto unlock;
  1874. if (sg_next(&sg_src[sg_len - 1]) == sg_src)
  1875. desc->cyclic = true;
  1876. src_dev_addr = 0;
  1877. dst_dev_addr = 0;
  1878. if (direction == DMA_DEV_TO_MEM)
  1879. src_dev_addr = chan->runtime_addr;
  1880. else if (direction == DMA_MEM_TO_DEV)
  1881. dst_dev_addr = chan->runtime_addr;
  1882. if (chan_is_logical(chan))
  1883. ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
  1884. sg_len, src_dev_addr, dst_dev_addr);
  1885. else
  1886. ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
  1887. sg_len, src_dev_addr, dst_dev_addr);
  1888. if (ret) {
  1889. chan_err(chan, "Failed to prepare %s sg job: %d\n",
  1890. chan_is_logical(chan) ? "log" : "phy", ret);
  1891. goto free_desc;
  1892. }
  1893. /*
  1894. * add descriptor to the prepare queue in order to be able
  1895. * to free them later in terminate_all
  1896. */
  1897. list_add_tail(&desc->node, &chan->prepare_queue);
  1898. spin_unlock_irqrestore(&chan->lock, flags);
  1899. return &desc->txd;
  1900. free_desc:
  1901. d40_desc_free(chan, desc);
  1902. unlock:
  1903. spin_unlock_irqrestore(&chan->lock, flags);
  1904. return NULL;
  1905. }
  1906. bool stedma40_filter(struct dma_chan *chan, void *data)
  1907. {
  1908. struct stedma40_chan_cfg *info = data;
  1909. struct d40_chan *d40c =
  1910. container_of(chan, struct d40_chan, chan);
  1911. int err;
  1912. if (data) {
  1913. err = d40_validate_conf(d40c, info);
  1914. if (!err)
  1915. d40c->dma_cfg = *info;
  1916. } else
  1917. err = d40_config_memcpy(d40c);
  1918. if (!err)
  1919. d40c->configured = true;
  1920. return err == 0;
  1921. }
  1922. EXPORT_SYMBOL(stedma40_filter);
  1923. static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
  1924. {
  1925. bool realtime = d40c->dma_cfg.realtime;
  1926. bool highprio = d40c->dma_cfg.high_priority;
  1927. u32 rtreg;
  1928. u32 event = D40_TYPE_TO_EVENT(dev_type);
  1929. u32 group = D40_TYPE_TO_GROUP(dev_type);
  1930. u32 bit = BIT(event);
  1931. u32 prioreg;
  1932. struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
  1933. rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
  1934. /*
  1935. * Due to a hardware bug, in some cases a logical channel triggered by
  1936. * a high priority destination event line can generate extra packet
  1937. * transactions.
  1938. *
  1939. * The workaround is to not set the high priority level for the
  1940. * destination event lines that trigger logical channels.
  1941. */
  1942. if (!src && chan_is_logical(d40c))
  1943. highprio = false;
  1944. prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
  1945. /* Destination event lines are stored in the upper halfword */
  1946. if (!src)
  1947. bit <<= 16;
  1948. writel(bit, d40c->base->virtbase + prioreg + group * 4);
  1949. writel(bit, d40c->base->virtbase + rtreg + group * 4);
  1950. }
  1951. static void d40_set_prio_realtime(struct d40_chan *d40c)
  1952. {
  1953. if (d40c->base->rev < 3)
  1954. return;
  1955. if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
  1956. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1957. __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
  1958. if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
  1959. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1960. __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
  1961. }
  1962. #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
  1963. #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
  1964. #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
  1965. #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
  1966. #define D40_DT_FLAGS_HIGH_PRIO(flags) ((flags >> 4) & 0x1)
  1967. static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
  1968. struct of_dma *ofdma)
  1969. {
  1970. struct stedma40_chan_cfg cfg;
  1971. dma_cap_mask_t cap;
  1972. u32 flags;
  1973. memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
  1974. dma_cap_zero(cap);
  1975. dma_cap_set(DMA_SLAVE, cap);
  1976. cfg.dev_type = dma_spec->args[0];
  1977. flags = dma_spec->args[2];
  1978. switch (D40_DT_FLAGS_MODE(flags)) {
  1979. case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
  1980. case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
  1981. }
  1982. switch (D40_DT_FLAGS_DIR(flags)) {
  1983. case 0:
  1984. cfg.dir = DMA_MEM_TO_DEV;
  1985. cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
  1986. break;
  1987. case 1:
  1988. cfg.dir = DMA_DEV_TO_MEM;
  1989. cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
  1990. break;
  1991. }
  1992. if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
  1993. cfg.phy_channel = dma_spec->args[1];
  1994. cfg.use_fixed_channel = true;
  1995. }
  1996. if (D40_DT_FLAGS_HIGH_PRIO(flags))
  1997. cfg.high_priority = true;
  1998. return dma_request_channel(cap, stedma40_filter, &cfg);
  1999. }
  2000. /* DMA ENGINE functions */
  2001. static int d40_alloc_chan_resources(struct dma_chan *chan)
  2002. {
  2003. int err;
  2004. unsigned long flags;
  2005. struct d40_chan *d40c =
  2006. container_of(chan, struct d40_chan, chan);
  2007. bool is_free_phy;
  2008. spin_lock_irqsave(&d40c->lock, flags);
  2009. dma_cookie_init(chan);
  2010. /* If no dma configuration is set use default configuration (memcpy) */
  2011. if (!d40c->configured) {
  2012. err = d40_config_memcpy(d40c);
  2013. if (err) {
  2014. chan_err(d40c, "Failed to configure memcpy channel\n");
  2015. goto mark_last_busy;
  2016. }
  2017. }
  2018. err = d40_allocate_channel(d40c, &is_free_phy);
  2019. if (err) {
  2020. chan_err(d40c, "Failed to allocate channel\n");
  2021. d40c->configured = false;
  2022. goto mark_last_busy;
  2023. }
  2024. pm_runtime_get_sync(d40c->base->dev);
  2025. d40_set_prio_realtime(d40c);
  2026. if (chan_is_logical(d40c)) {
  2027. if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
  2028. d40c->lcpa = d40c->base->lcpa_base +
  2029. d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
  2030. else
  2031. d40c->lcpa = d40c->base->lcpa_base +
  2032. d40c->dma_cfg.dev_type *
  2033. D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
  2034. /* Unmask the Global Interrupt Mask. */
  2035. d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
  2036. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
  2037. }
  2038. dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
  2039. chan_is_logical(d40c) ? "logical" : "physical",
  2040. d40c->phy_chan->num,
  2041. d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
  2042. /*
  2043. * Only write channel configuration to the DMA if the physical
  2044. * resource is free. In case of multiple logical channels
  2045. * on the same physical resource, only the first write is necessary.
  2046. */
  2047. if (is_free_phy)
  2048. d40_config_write(d40c);
  2049. mark_last_busy:
  2050. pm_runtime_mark_last_busy(d40c->base->dev);
  2051. pm_runtime_put_autosuspend(d40c->base->dev);
  2052. spin_unlock_irqrestore(&d40c->lock, flags);
  2053. return err;
  2054. }
  2055. static void d40_free_chan_resources(struct dma_chan *chan)
  2056. {
  2057. struct d40_chan *d40c =
  2058. container_of(chan, struct d40_chan, chan);
  2059. int err;
  2060. unsigned long flags;
  2061. if (d40c->phy_chan == NULL) {
  2062. chan_err(d40c, "Cannot free unallocated channel\n");
  2063. return;
  2064. }
  2065. spin_lock_irqsave(&d40c->lock, flags);
  2066. err = d40_free_dma(d40c);
  2067. if (err)
  2068. chan_err(d40c, "Failed to free channel\n");
  2069. spin_unlock_irqrestore(&d40c->lock, flags);
  2070. }
  2071. static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
  2072. dma_addr_t dst,
  2073. dma_addr_t src,
  2074. size_t size,
  2075. unsigned long dma_flags)
  2076. {
  2077. struct scatterlist dst_sg;
  2078. struct scatterlist src_sg;
  2079. sg_init_table(&dst_sg, 1);
  2080. sg_init_table(&src_sg, 1);
  2081. sg_dma_address(&dst_sg) = dst;
  2082. sg_dma_address(&src_sg) = src;
  2083. sg_dma_len(&dst_sg) = size;
  2084. sg_dma_len(&src_sg) = size;
  2085. return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
  2086. DMA_MEM_TO_MEM, dma_flags);
  2087. }
  2088. static struct dma_async_tx_descriptor *
  2089. d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  2090. unsigned int sg_len, enum dma_transfer_direction direction,
  2091. unsigned long dma_flags, void *context)
  2092. {
  2093. if (!is_slave_direction(direction))
  2094. return NULL;
  2095. return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
  2096. }
  2097. static struct dma_async_tx_descriptor *
  2098. dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
  2099. size_t buf_len, size_t period_len,
  2100. enum dma_transfer_direction direction, unsigned long flags)
  2101. {
  2102. unsigned int periods = buf_len / period_len;
  2103. struct dma_async_tx_descriptor *txd;
  2104. struct scatterlist *sg;
  2105. int i;
  2106. sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
  2107. if (!sg)
  2108. return NULL;
  2109. for (i = 0; i < periods; i++) {
  2110. sg_dma_address(&sg[i]) = dma_addr;
  2111. sg_dma_len(&sg[i]) = period_len;
  2112. dma_addr += period_len;
  2113. }
  2114. sg_chain(sg, periods + 1, sg);
  2115. txd = d40_prep_sg(chan, sg, sg, periods, direction,
  2116. DMA_PREP_INTERRUPT);
  2117. kfree(sg);
  2118. return txd;
  2119. }
  2120. static enum dma_status d40_tx_status(struct dma_chan *chan,
  2121. dma_cookie_t cookie,
  2122. struct dma_tx_state *txstate)
  2123. {
  2124. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2125. enum dma_status ret;
  2126. if (d40c->phy_chan == NULL) {
  2127. chan_err(d40c, "Cannot read status of unallocated channel\n");
  2128. return -EINVAL;
  2129. }
  2130. ret = dma_cookie_status(chan, cookie, txstate);
  2131. if (ret != DMA_COMPLETE && txstate)
  2132. dma_set_residue(txstate, stedma40_residue(chan));
  2133. if (d40_is_paused(d40c))
  2134. ret = DMA_PAUSED;
  2135. return ret;
  2136. }
  2137. static void d40_issue_pending(struct dma_chan *chan)
  2138. {
  2139. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2140. unsigned long flags;
  2141. if (d40c->phy_chan == NULL) {
  2142. chan_err(d40c, "Channel is not allocated!\n");
  2143. return;
  2144. }
  2145. spin_lock_irqsave(&d40c->lock, flags);
  2146. list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
  2147. /* Busy means that queued jobs are already being processed */
  2148. if (!d40c->busy)
  2149. (void) d40_queue_start(d40c);
  2150. spin_unlock_irqrestore(&d40c->lock, flags);
  2151. }
  2152. static int d40_terminate_all(struct dma_chan *chan)
  2153. {
  2154. unsigned long flags;
  2155. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2156. int ret;
  2157. if (d40c->phy_chan == NULL) {
  2158. chan_err(d40c, "Channel is not allocated!\n");
  2159. return -EINVAL;
  2160. }
  2161. spin_lock_irqsave(&d40c->lock, flags);
  2162. pm_runtime_get_sync(d40c->base->dev);
  2163. ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
  2164. if (ret)
  2165. chan_err(d40c, "Failed to stop channel\n");
  2166. d40_term_all(d40c);
  2167. pm_runtime_mark_last_busy(d40c->base->dev);
  2168. pm_runtime_put_autosuspend(d40c->base->dev);
  2169. if (d40c->busy) {
  2170. pm_runtime_mark_last_busy(d40c->base->dev);
  2171. pm_runtime_put_autosuspend(d40c->base->dev);
  2172. }
  2173. d40c->busy = false;
  2174. spin_unlock_irqrestore(&d40c->lock, flags);
  2175. return 0;
  2176. }
  2177. static int
  2178. dma40_config_to_halfchannel(struct d40_chan *d40c,
  2179. struct stedma40_half_channel_info *info,
  2180. u32 maxburst)
  2181. {
  2182. int psize;
  2183. if (chan_is_logical(d40c)) {
  2184. if (maxburst >= 16)
  2185. psize = STEDMA40_PSIZE_LOG_16;
  2186. else if (maxburst >= 8)
  2187. psize = STEDMA40_PSIZE_LOG_8;
  2188. else if (maxburst >= 4)
  2189. psize = STEDMA40_PSIZE_LOG_4;
  2190. else
  2191. psize = STEDMA40_PSIZE_LOG_1;
  2192. } else {
  2193. if (maxburst >= 16)
  2194. psize = STEDMA40_PSIZE_PHY_16;
  2195. else if (maxburst >= 8)
  2196. psize = STEDMA40_PSIZE_PHY_8;
  2197. else if (maxburst >= 4)
  2198. psize = STEDMA40_PSIZE_PHY_4;
  2199. else
  2200. psize = STEDMA40_PSIZE_PHY_1;
  2201. }
  2202. info->psize = psize;
  2203. info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
  2204. return 0;
  2205. }
  2206. /* Runtime reconfiguration extension */
  2207. static int d40_set_runtime_config(struct dma_chan *chan,
  2208. struct dma_slave_config *config)
  2209. {
  2210. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2211. struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
  2212. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  2213. dma_addr_t config_addr;
  2214. u32 src_maxburst, dst_maxburst;
  2215. int ret;
  2216. if (d40c->phy_chan == NULL) {
  2217. chan_err(d40c, "Channel is not allocated!\n");
  2218. return -EINVAL;
  2219. }
  2220. src_addr_width = config->src_addr_width;
  2221. src_maxburst = config->src_maxburst;
  2222. dst_addr_width = config->dst_addr_width;
  2223. dst_maxburst = config->dst_maxburst;
  2224. if (config->direction == DMA_DEV_TO_MEM) {
  2225. config_addr = config->src_addr;
  2226. if (cfg->dir != DMA_DEV_TO_MEM)
  2227. dev_dbg(d40c->base->dev,
  2228. "channel was not configured for peripheral "
  2229. "to memory transfer (%d) overriding\n",
  2230. cfg->dir);
  2231. cfg->dir = DMA_DEV_TO_MEM;
  2232. /* Configure the memory side */
  2233. if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  2234. dst_addr_width = src_addr_width;
  2235. if (dst_maxburst == 0)
  2236. dst_maxburst = src_maxburst;
  2237. } else if (config->direction == DMA_MEM_TO_DEV) {
  2238. config_addr = config->dst_addr;
  2239. if (cfg->dir != DMA_MEM_TO_DEV)
  2240. dev_dbg(d40c->base->dev,
  2241. "channel was not configured for memory "
  2242. "to peripheral transfer (%d) overriding\n",
  2243. cfg->dir);
  2244. cfg->dir = DMA_MEM_TO_DEV;
  2245. /* Configure the memory side */
  2246. if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  2247. src_addr_width = dst_addr_width;
  2248. if (src_maxburst == 0)
  2249. src_maxburst = dst_maxburst;
  2250. } else {
  2251. dev_err(d40c->base->dev,
  2252. "unrecognized channel direction %d\n",
  2253. config->direction);
  2254. return -EINVAL;
  2255. }
  2256. if (config_addr <= 0) {
  2257. dev_err(d40c->base->dev, "no address supplied\n");
  2258. return -EINVAL;
  2259. }
  2260. if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
  2261. dev_err(d40c->base->dev,
  2262. "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
  2263. src_maxburst,
  2264. src_addr_width,
  2265. dst_maxburst,
  2266. dst_addr_width);
  2267. return -EINVAL;
  2268. }
  2269. if (src_maxburst > 16) {
  2270. src_maxburst = 16;
  2271. dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
  2272. } else if (dst_maxburst > 16) {
  2273. dst_maxburst = 16;
  2274. src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
  2275. }
  2276. /* Only valid widths are; 1, 2, 4 and 8. */
  2277. if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
  2278. src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
  2279. dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
  2280. dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
  2281. !is_power_of_2(src_addr_width) ||
  2282. !is_power_of_2(dst_addr_width))
  2283. return -EINVAL;
  2284. cfg->src_info.data_width = src_addr_width;
  2285. cfg->dst_info.data_width = dst_addr_width;
  2286. ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
  2287. src_maxburst);
  2288. if (ret)
  2289. return ret;
  2290. ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
  2291. dst_maxburst);
  2292. if (ret)
  2293. return ret;
  2294. /* Fill in register values */
  2295. if (chan_is_logical(d40c))
  2296. d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
  2297. else
  2298. d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
  2299. /* These settings will take precedence later */
  2300. d40c->runtime_addr = config_addr;
  2301. d40c->runtime_direction = config->direction;
  2302. dev_dbg(d40c->base->dev,
  2303. "configured channel %s for %s, data width %d/%d, "
  2304. "maxburst %d/%d elements, LE, no flow control\n",
  2305. dma_chan_name(chan),
  2306. (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
  2307. src_addr_width, dst_addr_width,
  2308. src_maxburst, dst_maxburst);
  2309. return 0;
  2310. }
  2311. /* Initialization functions */
  2312. static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
  2313. struct d40_chan *chans, int offset,
  2314. int num_chans)
  2315. {
  2316. int i = 0;
  2317. struct d40_chan *d40c;
  2318. INIT_LIST_HEAD(&dma->channels);
  2319. for (i = offset; i < offset + num_chans; i++) {
  2320. d40c = &chans[i];
  2321. d40c->base = base;
  2322. d40c->chan.device = dma;
  2323. spin_lock_init(&d40c->lock);
  2324. d40c->log_num = D40_PHY_CHAN;
  2325. INIT_LIST_HEAD(&d40c->done);
  2326. INIT_LIST_HEAD(&d40c->active);
  2327. INIT_LIST_HEAD(&d40c->queue);
  2328. INIT_LIST_HEAD(&d40c->pending_queue);
  2329. INIT_LIST_HEAD(&d40c->client);
  2330. INIT_LIST_HEAD(&d40c->prepare_queue);
  2331. tasklet_init(&d40c->tasklet, dma_tasklet,
  2332. (unsigned long) d40c);
  2333. list_add_tail(&d40c->chan.device_node,
  2334. &dma->channels);
  2335. }
  2336. }
  2337. static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
  2338. {
  2339. if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) {
  2340. dev->device_prep_slave_sg = d40_prep_slave_sg;
  2341. dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  2342. }
  2343. if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
  2344. dev->device_prep_dma_memcpy = d40_prep_memcpy;
  2345. dev->directions = BIT(DMA_MEM_TO_MEM);
  2346. /*
  2347. * This controller can only access address at even
  2348. * 32bit boundaries, i.e. 2^2
  2349. */
  2350. dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
  2351. }
  2352. if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
  2353. dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
  2354. dev->device_alloc_chan_resources = d40_alloc_chan_resources;
  2355. dev->device_free_chan_resources = d40_free_chan_resources;
  2356. dev->device_issue_pending = d40_issue_pending;
  2357. dev->device_tx_status = d40_tx_status;
  2358. dev->device_config = d40_set_runtime_config;
  2359. dev->device_pause = d40_pause;
  2360. dev->device_resume = d40_resume;
  2361. dev->device_terminate_all = d40_terminate_all;
  2362. dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  2363. dev->dev = base->dev;
  2364. }
  2365. static int __init d40_dmaengine_init(struct d40_base *base,
  2366. int num_reserved_chans)
  2367. {
  2368. int err ;
  2369. d40_chan_init(base, &base->dma_slave, base->log_chans,
  2370. 0, base->num_log_chans);
  2371. dma_cap_zero(base->dma_slave.cap_mask);
  2372. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  2373. dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
  2374. d40_ops_init(base, &base->dma_slave);
  2375. err = dma_async_device_register(&base->dma_slave);
  2376. if (err) {
  2377. d40_err(base->dev, "Failed to register slave channels\n");
  2378. goto exit;
  2379. }
  2380. d40_chan_init(base, &base->dma_memcpy, base->log_chans,
  2381. base->num_log_chans, base->num_memcpy_chans);
  2382. dma_cap_zero(base->dma_memcpy.cap_mask);
  2383. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  2384. d40_ops_init(base, &base->dma_memcpy);
  2385. err = dma_async_device_register(&base->dma_memcpy);
  2386. if (err) {
  2387. d40_err(base->dev,
  2388. "Failed to register memcpy only channels\n");
  2389. goto unregister_slave;
  2390. }
  2391. d40_chan_init(base, &base->dma_both, base->phy_chans,
  2392. 0, num_reserved_chans);
  2393. dma_cap_zero(base->dma_both.cap_mask);
  2394. dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
  2395. dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
  2396. dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
  2397. d40_ops_init(base, &base->dma_both);
  2398. err = dma_async_device_register(&base->dma_both);
  2399. if (err) {
  2400. d40_err(base->dev,
  2401. "Failed to register logical and physical capable channels\n");
  2402. goto unregister_memcpy;
  2403. }
  2404. return 0;
  2405. unregister_memcpy:
  2406. dma_async_device_unregister(&base->dma_memcpy);
  2407. unregister_slave:
  2408. dma_async_device_unregister(&base->dma_slave);
  2409. exit:
  2410. return err;
  2411. }
  2412. /* Suspend resume functionality */
  2413. #ifdef CONFIG_PM_SLEEP
  2414. static int dma40_suspend(struct device *dev)
  2415. {
  2416. struct d40_base *base = dev_get_drvdata(dev);
  2417. int ret;
  2418. ret = pm_runtime_force_suspend(dev);
  2419. if (ret)
  2420. return ret;
  2421. if (base->lcpa_regulator)
  2422. ret = regulator_disable(base->lcpa_regulator);
  2423. return ret;
  2424. }
  2425. static int dma40_resume(struct device *dev)
  2426. {
  2427. struct d40_base *base = dev_get_drvdata(dev);
  2428. int ret = 0;
  2429. if (base->lcpa_regulator) {
  2430. ret = regulator_enable(base->lcpa_regulator);
  2431. if (ret)
  2432. return ret;
  2433. }
  2434. return pm_runtime_force_resume(dev);
  2435. }
  2436. #endif
  2437. #ifdef CONFIG_PM
  2438. static void dma40_backup(void __iomem *baseaddr, u32 *backup,
  2439. u32 *regaddr, int num, bool save)
  2440. {
  2441. int i;
  2442. for (i = 0; i < num; i++) {
  2443. void __iomem *addr = baseaddr + regaddr[i];
  2444. if (save)
  2445. backup[i] = readl_relaxed(addr);
  2446. else
  2447. writel_relaxed(backup[i], addr);
  2448. }
  2449. }
  2450. static void d40_save_restore_registers(struct d40_base *base, bool save)
  2451. {
  2452. int i;
  2453. /* Save/Restore channel specific registers */
  2454. for (i = 0; i < base->num_phy_chans; i++) {
  2455. void __iomem *addr;
  2456. int idx;
  2457. if (base->phy_res[i].reserved)
  2458. continue;
  2459. addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
  2460. idx = i * ARRAY_SIZE(d40_backup_regs_chan);
  2461. dma40_backup(addr, &base->reg_val_backup_chan[idx],
  2462. d40_backup_regs_chan,
  2463. ARRAY_SIZE(d40_backup_regs_chan),
  2464. save);
  2465. }
  2466. /* Save/Restore global registers */
  2467. dma40_backup(base->virtbase, base->reg_val_backup,
  2468. d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
  2469. save);
  2470. /* Save/Restore registers only existing on dma40 v3 and later */
  2471. if (base->gen_dmac.backup)
  2472. dma40_backup(base->virtbase, base->reg_val_backup_v4,
  2473. base->gen_dmac.backup,
  2474. base->gen_dmac.backup_size,
  2475. save);
  2476. }
  2477. static int dma40_runtime_suspend(struct device *dev)
  2478. {
  2479. struct d40_base *base = dev_get_drvdata(dev);
  2480. d40_save_restore_registers(base, true);
  2481. /* Don't disable/enable clocks for v1 due to HW bugs */
  2482. if (base->rev != 1)
  2483. writel_relaxed(base->gcc_pwr_off_mask,
  2484. base->virtbase + D40_DREG_GCC);
  2485. return 0;
  2486. }
  2487. static int dma40_runtime_resume(struct device *dev)
  2488. {
  2489. struct d40_base *base = dev_get_drvdata(dev);
  2490. d40_save_restore_registers(base, false);
  2491. writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
  2492. base->virtbase + D40_DREG_GCC);
  2493. return 0;
  2494. }
  2495. #endif
  2496. static const struct dev_pm_ops dma40_pm_ops = {
  2497. SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
  2498. SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
  2499. dma40_runtime_resume,
  2500. NULL)
  2501. };
  2502. /* Initialization functions. */
  2503. static int __init d40_phy_res_init(struct d40_base *base)
  2504. {
  2505. int i;
  2506. int num_phy_chans_avail = 0;
  2507. u32 val[2];
  2508. int odd_even_bit = -2;
  2509. int gcc = D40_DREG_GCC_ENA;
  2510. val[0] = readl(base->virtbase + D40_DREG_PRSME);
  2511. val[1] = readl(base->virtbase + D40_DREG_PRSMO);
  2512. for (i = 0; i < base->num_phy_chans; i++) {
  2513. base->phy_res[i].num = i;
  2514. odd_even_bit += 2 * ((i % 2) == 0);
  2515. if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
  2516. /* Mark security only channels as occupied */
  2517. base->phy_res[i].allocated_src = D40_ALLOC_PHY;
  2518. base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
  2519. base->phy_res[i].reserved = true;
  2520. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
  2521. D40_DREG_GCC_SRC);
  2522. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
  2523. D40_DREG_GCC_DST);
  2524. } else {
  2525. base->phy_res[i].allocated_src = D40_ALLOC_FREE;
  2526. base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
  2527. base->phy_res[i].reserved = false;
  2528. num_phy_chans_avail++;
  2529. }
  2530. spin_lock_init(&base->phy_res[i].lock);
  2531. }
  2532. /* Mark disabled channels as occupied */
  2533. for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
  2534. int chan = base->plat_data->disabled_channels[i];
  2535. base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
  2536. base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
  2537. base->phy_res[chan].reserved = true;
  2538. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
  2539. D40_DREG_GCC_SRC);
  2540. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
  2541. D40_DREG_GCC_DST);
  2542. num_phy_chans_avail--;
  2543. }
  2544. /* Mark soft_lli channels */
  2545. for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
  2546. int chan = base->plat_data->soft_lli_chans[i];
  2547. base->phy_res[chan].use_soft_lli = true;
  2548. }
  2549. dev_info(base->dev, "%d of %d physical DMA channels available\n",
  2550. num_phy_chans_avail, base->num_phy_chans);
  2551. /* Verify settings extended vs standard */
  2552. val[0] = readl(base->virtbase + D40_DREG_PRTYP);
  2553. for (i = 0; i < base->num_phy_chans; i++) {
  2554. if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
  2555. (val[0] & 0x3) != 1)
  2556. dev_info(base->dev,
  2557. "[%s] INFO: channel %d is misconfigured (%d)\n",
  2558. __func__, i, val[0] & 0x3);
  2559. val[0] = val[0] >> 2;
  2560. }
  2561. /*
  2562. * To keep things simple, Enable all clocks initially.
  2563. * The clocks will get managed later post channel allocation.
  2564. * The clocks for the event lines on which reserved channels exists
  2565. * are not managed here.
  2566. */
  2567. writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
  2568. base->gcc_pwr_off_mask = gcc;
  2569. return num_phy_chans_avail;
  2570. }
  2571. static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
  2572. {
  2573. struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
  2574. struct clk *clk;
  2575. void __iomem *virtbase;
  2576. struct resource *res;
  2577. struct d40_base *base;
  2578. int num_log_chans;
  2579. int num_phy_chans;
  2580. int num_memcpy_chans;
  2581. int clk_ret = -EINVAL;
  2582. int i;
  2583. u32 pid;
  2584. u32 cid;
  2585. u8 rev;
  2586. clk = clk_get(&pdev->dev, NULL);
  2587. if (IS_ERR(clk)) {
  2588. d40_err(&pdev->dev, "No matching clock found\n");
  2589. goto check_prepare_enabled;
  2590. }
  2591. clk_ret = clk_prepare_enable(clk);
  2592. if (clk_ret) {
  2593. d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
  2594. goto disable_unprepare;
  2595. }
  2596. /* Get IO for DMAC base address */
  2597. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
  2598. if (!res)
  2599. goto disable_unprepare;
  2600. if (request_mem_region(res->start, resource_size(res),
  2601. D40_NAME " I/O base") == NULL)
  2602. goto release_region;
  2603. virtbase = ioremap(res->start, resource_size(res));
  2604. if (!virtbase)
  2605. goto release_region;
  2606. /* This is just a regular AMBA PrimeCell ID actually */
  2607. for (pid = 0, i = 0; i < 4; i++)
  2608. pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
  2609. & 255) << (i * 8);
  2610. for (cid = 0, i = 0; i < 4; i++)
  2611. cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
  2612. & 255) << (i * 8);
  2613. if (cid != AMBA_CID) {
  2614. d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
  2615. goto unmap_io;
  2616. }
  2617. if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
  2618. d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
  2619. AMBA_MANF_BITS(pid),
  2620. AMBA_VENDOR_ST);
  2621. goto unmap_io;
  2622. }
  2623. /*
  2624. * HW revision:
  2625. * DB8500ed has revision 0
  2626. * ? has revision 1
  2627. * DB8500v1 has revision 2
  2628. * DB8500v2 has revision 3
  2629. * AP9540v1 has revision 4
  2630. * DB8540v1 has revision 4
  2631. */
  2632. rev = AMBA_REV_BITS(pid);
  2633. if (rev < 2) {
  2634. d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
  2635. goto unmap_io;
  2636. }
  2637. /* The number of physical channels on this HW */
  2638. if (plat_data->num_of_phy_chans)
  2639. num_phy_chans = plat_data->num_of_phy_chans;
  2640. else
  2641. num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
  2642. /* The number of channels used for memcpy */
  2643. if (plat_data->num_of_memcpy_chans)
  2644. num_memcpy_chans = plat_data->num_of_memcpy_chans;
  2645. else
  2646. num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
  2647. num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
  2648. dev_info(&pdev->dev,
  2649. "hardware rev: %d @ %pa with %d physical and %d logical channels\n",
  2650. rev, &res->start, num_phy_chans, num_log_chans);
  2651. base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
  2652. (num_phy_chans + num_log_chans + num_memcpy_chans) *
  2653. sizeof(struct d40_chan), GFP_KERNEL);
  2654. if (base == NULL)
  2655. goto unmap_io;
  2656. base->rev = rev;
  2657. base->clk = clk;
  2658. base->num_memcpy_chans = num_memcpy_chans;
  2659. base->num_phy_chans = num_phy_chans;
  2660. base->num_log_chans = num_log_chans;
  2661. base->phy_start = res->start;
  2662. base->phy_size = resource_size(res);
  2663. base->virtbase = virtbase;
  2664. base->plat_data = plat_data;
  2665. base->dev = &pdev->dev;
  2666. base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
  2667. base->log_chans = &base->phy_chans[num_phy_chans];
  2668. if (base->plat_data->num_of_phy_chans == 14) {
  2669. base->gen_dmac.backup = d40_backup_regs_v4b;
  2670. base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
  2671. base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
  2672. base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
  2673. base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
  2674. base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
  2675. base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
  2676. base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
  2677. base->gen_dmac.il = il_v4b;
  2678. base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
  2679. base->gen_dmac.init_reg = dma_init_reg_v4b;
  2680. base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
  2681. } else {
  2682. if (base->rev >= 3) {
  2683. base->gen_dmac.backup = d40_backup_regs_v4a;
  2684. base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
  2685. }
  2686. base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
  2687. base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
  2688. base->gen_dmac.realtime_en = D40_DREG_RSEG1;
  2689. base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
  2690. base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
  2691. base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
  2692. base->gen_dmac.il = il_v4a;
  2693. base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
  2694. base->gen_dmac.init_reg = dma_init_reg_v4a;
  2695. base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
  2696. }
  2697. base->phy_res = kcalloc(num_phy_chans,
  2698. sizeof(*base->phy_res),
  2699. GFP_KERNEL);
  2700. if (!base->phy_res)
  2701. goto free_base;
  2702. base->lookup_phy_chans = kcalloc(num_phy_chans,
  2703. sizeof(*base->lookup_phy_chans),
  2704. GFP_KERNEL);
  2705. if (!base->lookup_phy_chans)
  2706. goto free_phy_res;
  2707. base->lookup_log_chans = kcalloc(num_log_chans,
  2708. sizeof(*base->lookup_log_chans),
  2709. GFP_KERNEL);
  2710. if (!base->lookup_log_chans)
  2711. goto free_phy_chans;
  2712. base->reg_val_backup_chan = kmalloc_array(base->num_phy_chans,
  2713. sizeof(d40_backup_regs_chan),
  2714. GFP_KERNEL);
  2715. if (!base->reg_val_backup_chan)
  2716. goto free_log_chans;
  2717. base->lcla_pool.alloc_map = kcalloc(num_phy_chans
  2718. * D40_LCLA_LINK_PER_EVENT_GRP,
  2719. sizeof(*base->lcla_pool.alloc_map),
  2720. GFP_KERNEL);
  2721. if (!base->lcla_pool.alloc_map)
  2722. goto free_backup_chan;
  2723. base->regs_interrupt = kmalloc_array(base->gen_dmac.il_size,
  2724. sizeof(*base->regs_interrupt),
  2725. GFP_KERNEL);
  2726. if (!base->regs_interrupt)
  2727. goto free_map;
  2728. base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
  2729. 0, SLAB_HWCACHE_ALIGN,
  2730. NULL);
  2731. if (base->desc_slab == NULL)
  2732. goto free_regs;
  2733. return base;
  2734. free_regs:
  2735. kfree(base->regs_interrupt);
  2736. free_map:
  2737. kfree(base->lcla_pool.alloc_map);
  2738. free_backup_chan:
  2739. kfree(base->reg_val_backup_chan);
  2740. free_log_chans:
  2741. kfree(base->lookup_log_chans);
  2742. free_phy_chans:
  2743. kfree(base->lookup_phy_chans);
  2744. free_phy_res:
  2745. kfree(base->phy_res);
  2746. free_base:
  2747. kfree(base);
  2748. unmap_io:
  2749. iounmap(virtbase);
  2750. release_region:
  2751. release_mem_region(res->start, resource_size(res));
  2752. check_prepare_enabled:
  2753. if (!clk_ret)
  2754. disable_unprepare:
  2755. clk_disable_unprepare(clk);
  2756. if (!IS_ERR(clk))
  2757. clk_put(clk);
  2758. return NULL;
  2759. }
  2760. static void __init d40_hw_init(struct d40_base *base)
  2761. {
  2762. int i;
  2763. u32 prmseo[2] = {0, 0};
  2764. u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
  2765. u32 pcmis = 0;
  2766. u32 pcicr = 0;
  2767. struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
  2768. u32 reg_size = base->gen_dmac.init_reg_size;
  2769. for (i = 0; i < reg_size; i++)
  2770. writel(dma_init_reg[i].val,
  2771. base->virtbase + dma_init_reg[i].reg);
  2772. /* Configure all our dma channels to default settings */
  2773. for (i = 0; i < base->num_phy_chans; i++) {
  2774. activeo[i % 2] = activeo[i % 2] << 2;
  2775. if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
  2776. == D40_ALLOC_PHY) {
  2777. activeo[i % 2] |= 3;
  2778. continue;
  2779. }
  2780. /* Enable interrupt # */
  2781. pcmis = (pcmis << 1) | 1;
  2782. /* Clear interrupt # */
  2783. pcicr = (pcicr << 1) | 1;
  2784. /* Set channel to physical mode */
  2785. prmseo[i % 2] = prmseo[i % 2] << 2;
  2786. prmseo[i % 2] |= 1;
  2787. }
  2788. writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
  2789. writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
  2790. writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
  2791. writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
  2792. /* Write which interrupt to enable */
  2793. writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
  2794. /* Write which interrupt to clear */
  2795. writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
  2796. /* These are __initdata and cannot be accessed after init */
  2797. base->gen_dmac.init_reg = NULL;
  2798. base->gen_dmac.init_reg_size = 0;
  2799. }
  2800. static int __init d40_lcla_allocate(struct d40_base *base)
  2801. {
  2802. struct d40_lcla_pool *pool = &base->lcla_pool;
  2803. unsigned long *page_list;
  2804. int i, j;
  2805. int ret;
  2806. /*
  2807. * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
  2808. * To full fill this hardware requirement without wasting 256 kb
  2809. * we allocate pages until we get an aligned one.
  2810. */
  2811. page_list = kmalloc_array(MAX_LCLA_ALLOC_ATTEMPTS,
  2812. sizeof(*page_list),
  2813. GFP_KERNEL);
  2814. if (!page_list)
  2815. return -ENOMEM;
  2816. /* Calculating how many pages that are required */
  2817. base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
  2818. for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
  2819. page_list[i] = __get_free_pages(GFP_KERNEL,
  2820. base->lcla_pool.pages);
  2821. if (!page_list[i]) {
  2822. d40_err(base->dev, "Failed to allocate %d pages.\n",
  2823. base->lcla_pool.pages);
  2824. ret = -ENOMEM;
  2825. for (j = 0; j < i; j++)
  2826. free_pages(page_list[j], base->lcla_pool.pages);
  2827. goto free_page_list;
  2828. }
  2829. if ((virt_to_phys((void *)page_list[i]) &
  2830. (LCLA_ALIGNMENT - 1)) == 0)
  2831. break;
  2832. }
  2833. for (j = 0; j < i; j++)
  2834. free_pages(page_list[j], base->lcla_pool.pages);
  2835. if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
  2836. base->lcla_pool.base = (void *)page_list[i];
  2837. } else {
  2838. /*
  2839. * After many attempts and no succees with finding the correct
  2840. * alignment, try with allocating a big buffer.
  2841. */
  2842. dev_warn(base->dev,
  2843. "[%s] Failed to get %d pages @ 18 bit align.\n",
  2844. __func__, base->lcla_pool.pages);
  2845. base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
  2846. base->num_phy_chans +
  2847. LCLA_ALIGNMENT,
  2848. GFP_KERNEL);
  2849. if (!base->lcla_pool.base_unaligned) {
  2850. ret = -ENOMEM;
  2851. goto free_page_list;
  2852. }
  2853. base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
  2854. LCLA_ALIGNMENT);
  2855. }
  2856. pool->dma_addr = dma_map_single(base->dev, pool->base,
  2857. SZ_1K * base->num_phy_chans,
  2858. DMA_TO_DEVICE);
  2859. if (dma_mapping_error(base->dev, pool->dma_addr)) {
  2860. pool->dma_addr = 0;
  2861. ret = -ENOMEM;
  2862. goto free_page_list;
  2863. }
  2864. writel(virt_to_phys(base->lcla_pool.base),
  2865. base->virtbase + D40_DREG_LCLA);
  2866. ret = 0;
  2867. free_page_list:
  2868. kfree(page_list);
  2869. return ret;
  2870. }
  2871. static int __init d40_of_probe(struct platform_device *pdev,
  2872. struct device_node *np)
  2873. {
  2874. struct stedma40_platform_data *pdata;
  2875. int num_phy = 0, num_memcpy = 0, num_disabled = 0;
  2876. const __be32 *list;
  2877. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  2878. if (!pdata)
  2879. return -ENOMEM;
  2880. /* If absent this value will be obtained from h/w. */
  2881. of_property_read_u32(np, "dma-channels", &num_phy);
  2882. if (num_phy > 0)
  2883. pdata->num_of_phy_chans = num_phy;
  2884. list = of_get_property(np, "memcpy-channels", &num_memcpy);
  2885. num_memcpy /= sizeof(*list);
  2886. if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
  2887. d40_err(&pdev->dev,
  2888. "Invalid number of memcpy channels specified (%d)\n",
  2889. num_memcpy);
  2890. return -EINVAL;
  2891. }
  2892. pdata->num_of_memcpy_chans = num_memcpy;
  2893. of_property_read_u32_array(np, "memcpy-channels",
  2894. dma40_memcpy_channels,
  2895. num_memcpy);
  2896. list = of_get_property(np, "disabled-channels", &num_disabled);
  2897. num_disabled /= sizeof(*list);
  2898. if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
  2899. d40_err(&pdev->dev,
  2900. "Invalid number of disabled channels specified (%d)\n",
  2901. num_disabled);
  2902. return -EINVAL;
  2903. }
  2904. of_property_read_u32_array(np, "disabled-channels",
  2905. pdata->disabled_channels,
  2906. num_disabled);
  2907. pdata->disabled_channels[num_disabled] = -1;
  2908. pdev->dev.platform_data = pdata;
  2909. return 0;
  2910. }
  2911. static int __init d40_probe(struct platform_device *pdev)
  2912. {
  2913. struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
  2914. struct device_node *np = pdev->dev.of_node;
  2915. int ret = -ENOENT;
  2916. struct d40_base *base;
  2917. struct resource *res;
  2918. int num_reserved_chans;
  2919. u32 val;
  2920. if (!plat_data) {
  2921. if (np) {
  2922. if (d40_of_probe(pdev, np)) {
  2923. ret = -ENOMEM;
  2924. goto report_failure;
  2925. }
  2926. } else {
  2927. d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
  2928. goto report_failure;
  2929. }
  2930. }
  2931. base = d40_hw_detect_init(pdev);
  2932. if (!base)
  2933. goto report_failure;
  2934. num_reserved_chans = d40_phy_res_init(base);
  2935. platform_set_drvdata(pdev, base);
  2936. spin_lock_init(&base->interrupt_lock);
  2937. spin_lock_init(&base->execmd_lock);
  2938. /* Get IO for logical channel parameter address */
  2939. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
  2940. if (!res) {
  2941. ret = -ENOENT;
  2942. d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
  2943. goto destroy_cache;
  2944. }
  2945. base->lcpa_size = resource_size(res);
  2946. base->phy_lcpa = res->start;
  2947. if (request_mem_region(res->start, resource_size(res),
  2948. D40_NAME " I/O lcpa") == NULL) {
  2949. ret = -EBUSY;
  2950. d40_err(&pdev->dev, "Failed to request LCPA region %pR\n", res);
  2951. goto destroy_cache;
  2952. }
  2953. /* We make use of ESRAM memory for this. */
  2954. val = readl(base->virtbase + D40_DREG_LCPA);
  2955. if (res->start != val && val != 0) {
  2956. dev_warn(&pdev->dev,
  2957. "[%s] Mismatch LCPA dma 0x%x, def %pa\n",
  2958. __func__, val, &res->start);
  2959. } else
  2960. writel(res->start, base->virtbase + D40_DREG_LCPA);
  2961. base->lcpa_base = ioremap(res->start, resource_size(res));
  2962. if (!base->lcpa_base) {
  2963. ret = -ENOMEM;
  2964. d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
  2965. goto destroy_cache;
  2966. }
  2967. /* If lcla has to be located in ESRAM we don't need to allocate */
  2968. if (base->plat_data->use_esram_lcla) {
  2969. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2970. "lcla_esram");
  2971. if (!res) {
  2972. ret = -ENOENT;
  2973. d40_err(&pdev->dev,
  2974. "No \"lcla_esram\" memory resource\n");
  2975. goto destroy_cache;
  2976. }
  2977. base->lcla_pool.base = ioremap(res->start,
  2978. resource_size(res));
  2979. if (!base->lcla_pool.base) {
  2980. ret = -ENOMEM;
  2981. d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
  2982. goto destroy_cache;
  2983. }
  2984. writel(res->start, base->virtbase + D40_DREG_LCLA);
  2985. } else {
  2986. ret = d40_lcla_allocate(base);
  2987. if (ret) {
  2988. d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
  2989. goto destroy_cache;
  2990. }
  2991. }
  2992. spin_lock_init(&base->lcla_pool.lock);
  2993. base->irq = platform_get_irq(pdev, 0);
  2994. ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
  2995. if (ret) {
  2996. d40_err(&pdev->dev, "No IRQ defined\n");
  2997. goto destroy_cache;
  2998. }
  2999. if (base->plat_data->use_esram_lcla) {
  3000. base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
  3001. if (IS_ERR(base->lcpa_regulator)) {
  3002. d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
  3003. ret = PTR_ERR(base->lcpa_regulator);
  3004. base->lcpa_regulator = NULL;
  3005. goto destroy_cache;
  3006. }
  3007. ret = regulator_enable(base->lcpa_regulator);
  3008. if (ret) {
  3009. d40_err(&pdev->dev,
  3010. "Failed to enable lcpa_regulator\n");
  3011. regulator_put(base->lcpa_regulator);
  3012. base->lcpa_regulator = NULL;
  3013. goto destroy_cache;
  3014. }
  3015. }
  3016. writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
  3017. pm_runtime_irq_safe(base->dev);
  3018. pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
  3019. pm_runtime_use_autosuspend(base->dev);
  3020. pm_runtime_mark_last_busy(base->dev);
  3021. pm_runtime_set_active(base->dev);
  3022. pm_runtime_enable(base->dev);
  3023. ret = d40_dmaengine_init(base, num_reserved_chans);
  3024. if (ret)
  3025. goto destroy_cache;
  3026. base->dev->dma_parms = &base->dma_parms;
  3027. ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
  3028. if (ret) {
  3029. d40_err(&pdev->dev, "Failed to set dma max seg size\n");
  3030. goto destroy_cache;
  3031. }
  3032. d40_hw_init(base);
  3033. if (np) {
  3034. ret = of_dma_controller_register(np, d40_xlate, NULL);
  3035. if (ret)
  3036. dev_err(&pdev->dev,
  3037. "could not register of_dma_controller\n");
  3038. }
  3039. dev_info(base->dev, "initialized\n");
  3040. return 0;
  3041. destroy_cache:
  3042. kmem_cache_destroy(base->desc_slab);
  3043. if (base->virtbase)
  3044. iounmap(base->virtbase);
  3045. if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
  3046. iounmap(base->lcla_pool.base);
  3047. base->lcla_pool.base = NULL;
  3048. }
  3049. if (base->lcla_pool.dma_addr)
  3050. dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
  3051. SZ_1K * base->num_phy_chans,
  3052. DMA_TO_DEVICE);
  3053. if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
  3054. free_pages((unsigned long)base->lcla_pool.base,
  3055. base->lcla_pool.pages);
  3056. kfree(base->lcla_pool.base_unaligned);
  3057. if (base->phy_lcpa)
  3058. release_mem_region(base->phy_lcpa,
  3059. base->lcpa_size);
  3060. if (base->phy_start)
  3061. release_mem_region(base->phy_start,
  3062. base->phy_size);
  3063. if (base->clk) {
  3064. clk_disable_unprepare(base->clk);
  3065. clk_put(base->clk);
  3066. }
  3067. if (base->lcpa_regulator) {
  3068. regulator_disable(base->lcpa_regulator);
  3069. regulator_put(base->lcpa_regulator);
  3070. }
  3071. kfree(base->lcla_pool.alloc_map);
  3072. kfree(base->lookup_log_chans);
  3073. kfree(base->lookup_phy_chans);
  3074. kfree(base->phy_res);
  3075. kfree(base);
  3076. report_failure:
  3077. d40_err(&pdev->dev, "probe failed\n");
  3078. return ret;
  3079. }
  3080. static const struct of_device_id d40_match[] = {
  3081. { .compatible = "stericsson,dma40", },
  3082. {}
  3083. };
  3084. static struct platform_driver d40_driver = {
  3085. .driver = {
  3086. .name = D40_NAME,
  3087. .pm = &dma40_pm_ops,
  3088. .of_match_table = d40_match,
  3089. },
  3090. };
  3091. static int __init stedma40_init(void)
  3092. {
  3093. return platform_driver_probe(&d40_driver, d40_probe);
  3094. }
  3095. subsys_initcall(stedma40_init);