pl330.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Copyright (C) 2010 Samsung Electronics Co. Ltd.
  7. * Jaswinder Singh <[email protected]>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/io.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/amba/bus.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/of.h>
  23. #include <linux/of_dma.h>
  24. #include <linux/err.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/bug.h>
  27. #include <linux/reset.h>
  28. #include "dmaengine.h"
  29. #define PL330_MAX_CHAN 8
  30. #define PL330_MAX_IRQS 32
  31. #define PL330_MAX_PERI 32
  32. #define PL330_MAX_BURST 16
  33. #define PL330_QUIRK_BROKEN_NO_FLUSHP BIT(0)
  34. #define PL330_QUIRK_PERIPH_BURST BIT(1)
  35. enum pl330_cachectrl {
  36. CCTRL0, /* Noncacheable and nonbufferable */
  37. CCTRL1, /* Bufferable only */
  38. CCTRL2, /* Cacheable, but do not allocate */
  39. CCTRL3, /* Cacheable and bufferable, but do not allocate */
  40. INVALID1, /* AWCACHE = 0x1000 */
  41. INVALID2,
  42. CCTRL6, /* Cacheable write-through, allocate on writes only */
  43. CCTRL7, /* Cacheable write-back, allocate on writes only */
  44. };
  45. enum pl330_byteswap {
  46. SWAP_NO,
  47. SWAP_2,
  48. SWAP_4,
  49. SWAP_8,
  50. SWAP_16,
  51. };
  52. /* Register and Bit field Definitions */
  53. #define DS 0x0
  54. #define DS_ST_STOP 0x0
  55. #define DS_ST_EXEC 0x1
  56. #define DS_ST_CMISS 0x2
  57. #define DS_ST_UPDTPC 0x3
  58. #define DS_ST_WFE 0x4
  59. #define DS_ST_ATBRR 0x5
  60. #define DS_ST_QBUSY 0x6
  61. #define DS_ST_WFP 0x7
  62. #define DS_ST_KILL 0x8
  63. #define DS_ST_CMPLT 0x9
  64. #define DS_ST_FLTCMP 0xe
  65. #define DS_ST_FAULT 0xf
  66. #define DPC 0x4
  67. #define INTEN 0x20
  68. #define ES 0x24
  69. #define INTSTATUS 0x28
  70. #define INTCLR 0x2c
  71. #define FSM 0x30
  72. #define FSC 0x34
  73. #define FTM 0x38
  74. #define _FTC 0x40
  75. #define FTC(n) (_FTC + (n)*0x4)
  76. #define _CS 0x100
  77. #define CS(n) (_CS + (n)*0x8)
  78. #define CS_CNS (1 << 21)
  79. #define _CPC 0x104
  80. #define CPC(n) (_CPC + (n)*0x8)
  81. #define _SA 0x400
  82. #define SA(n) (_SA + (n)*0x20)
  83. #define _DA 0x404
  84. #define DA(n) (_DA + (n)*0x20)
  85. #define _CC 0x408
  86. #define CC(n) (_CC + (n)*0x20)
  87. #define CC_SRCINC (1 << 0)
  88. #define CC_DSTINC (1 << 14)
  89. #define CC_SRCPRI (1 << 8)
  90. #define CC_DSTPRI (1 << 22)
  91. #define CC_SRCNS (1 << 9)
  92. #define CC_DSTNS (1 << 23)
  93. #define CC_SRCIA (1 << 10)
  94. #define CC_DSTIA (1 << 24)
  95. #define CC_SRCBRSTLEN_SHFT 4
  96. #define CC_DSTBRSTLEN_SHFT 18
  97. #define CC_SRCBRSTSIZE_SHFT 1
  98. #define CC_DSTBRSTSIZE_SHFT 15
  99. #define CC_SRCCCTRL_SHFT 11
  100. #define CC_SRCCCTRL_MASK 0x7
  101. #define CC_DSTCCTRL_SHFT 25
  102. #define CC_DRCCCTRL_MASK 0x7
  103. #define CC_SWAP_SHFT 28
  104. #define _LC0 0x40c
  105. #define LC0(n) (_LC0 + (n)*0x20)
  106. #define _LC1 0x410
  107. #define LC1(n) (_LC1 + (n)*0x20)
  108. #define DBGSTATUS 0xd00
  109. #define DBG_BUSY (1 << 0)
  110. #define DBGCMD 0xd04
  111. #define DBGINST0 0xd08
  112. #define DBGINST1 0xd0c
  113. #define CR0 0xe00
  114. #define CR1 0xe04
  115. #define CR2 0xe08
  116. #define CR3 0xe0c
  117. #define CR4 0xe10
  118. #define CRD 0xe14
  119. #define PERIPH_ID 0xfe0
  120. #define PERIPH_REV_SHIFT 20
  121. #define PERIPH_REV_MASK 0xf
  122. #define PERIPH_REV_R0P0 0
  123. #define PERIPH_REV_R1P0 1
  124. #define PERIPH_REV_R1P1 2
  125. #define CR0_PERIPH_REQ_SET (1 << 0)
  126. #define CR0_BOOT_EN_SET (1 << 1)
  127. #define CR0_BOOT_MAN_NS (1 << 2)
  128. #define CR0_NUM_CHANS_SHIFT 4
  129. #define CR0_NUM_CHANS_MASK 0x7
  130. #define CR0_NUM_PERIPH_SHIFT 12
  131. #define CR0_NUM_PERIPH_MASK 0x1f
  132. #define CR0_NUM_EVENTS_SHIFT 17
  133. #define CR0_NUM_EVENTS_MASK 0x1f
  134. #define CR1_ICACHE_LEN_SHIFT 0
  135. #define CR1_ICACHE_LEN_MASK 0x7
  136. #define CR1_NUM_ICACHELINES_SHIFT 4
  137. #define CR1_NUM_ICACHELINES_MASK 0xf
  138. #define CRD_DATA_WIDTH_SHIFT 0
  139. #define CRD_DATA_WIDTH_MASK 0x7
  140. #define CRD_WR_CAP_SHIFT 4
  141. #define CRD_WR_CAP_MASK 0x7
  142. #define CRD_WR_Q_DEP_SHIFT 8
  143. #define CRD_WR_Q_DEP_MASK 0xf
  144. #define CRD_RD_CAP_SHIFT 12
  145. #define CRD_RD_CAP_MASK 0x7
  146. #define CRD_RD_Q_DEP_SHIFT 16
  147. #define CRD_RD_Q_DEP_MASK 0xf
  148. #define CRD_DATA_BUFF_SHIFT 20
  149. #define CRD_DATA_BUFF_MASK 0x3ff
  150. #define PART 0x330
  151. #define DESIGNER 0x41
  152. #define REVISION 0x0
  153. #define INTEG_CFG 0x0
  154. #define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
  155. #define PL330_STATE_STOPPED (1 << 0)
  156. #define PL330_STATE_EXECUTING (1 << 1)
  157. #define PL330_STATE_WFE (1 << 2)
  158. #define PL330_STATE_FAULTING (1 << 3)
  159. #define PL330_STATE_COMPLETING (1 << 4)
  160. #define PL330_STATE_WFP (1 << 5)
  161. #define PL330_STATE_KILLING (1 << 6)
  162. #define PL330_STATE_FAULT_COMPLETING (1 << 7)
  163. #define PL330_STATE_CACHEMISS (1 << 8)
  164. #define PL330_STATE_UPDTPC (1 << 9)
  165. #define PL330_STATE_ATBARRIER (1 << 10)
  166. #define PL330_STATE_QUEUEBUSY (1 << 11)
  167. #define PL330_STATE_INVALID (1 << 15)
  168. #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
  169. | PL330_STATE_WFE | PL330_STATE_FAULTING)
  170. #define CMD_DMAADDH 0x54
  171. #define CMD_DMAEND 0x00
  172. #define CMD_DMAFLUSHP 0x35
  173. #define CMD_DMAGO 0xa0
  174. #define CMD_DMALD 0x04
  175. #define CMD_DMALDP 0x25
  176. #define CMD_DMALP 0x20
  177. #define CMD_DMALPEND 0x28
  178. #define CMD_DMAKILL 0x01
  179. #define CMD_DMAMOV 0xbc
  180. #define CMD_DMANOP 0x18
  181. #define CMD_DMARMB 0x12
  182. #define CMD_DMASEV 0x34
  183. #define CMD_DMAST 0x08
  184. #define CMD_DMASTP 0x29
  185. #define CMD_DMASTZ 0x0c
  186. #define CMD_DMAWFE 0x36
  187. #define CMD_DMAWFP 0x30
  188. #define CMD_DMAWMB 0x13
  189. #define SZ_DMAADDH 3
  190. #define SZ_DMAEND 1
  191. #define SZ_DMAFLUSHP 2
  192. #define SZ_DMALD 1
  193. #define SZ_DMALDP 2
  194. #define SZ_DMALP 2
  195. #define SZ_DMALPEND 2
  196. #define SZ_DMAKILL 1
  197. #define SZ_DMAMOV 6
  198. #define SZ_DMANOP 1
  199. #define SZ_DMARMB 1
  200. #define SZ_DMASEV 2
  201. #define SZ_DMAST 1
  202. #define SZ_DMASTP 2
  203. #define SZ_DMASTZ 1
  204. #define SZ_DMAWFE 2
  205. #define SZ_DMAWFP 2
  206. #define SZ_DMAWMB 1
  207. #define SZ_DMAGO 6
  208. #define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
  209. #define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
  210. #define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
  211. #define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
  212. /*
  213. * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
  214. * at 1byte/burst for P<->M and M<->M respectively.
  215. * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
  216. * should be enough for P<->M and M<->M respectively.
  217. */
  218. #define MCODE_BUFF_PER_REQ 256
  219. /* Use this _only_ to wait on transient states */
  220. #define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
  221. #ifdef PL330_DEBUG_MCGEN
  222. static unsigned cmd_line;
  223. #define PL330_DBGCMD_DUMP(off, x...) do { \
  224. printk("%x:", cmd_line); \
  225. printk(KERN_CONT x); \
  226. cmd_line += off; \
  227. } while (0)
  228. #define PL330_DBGMC_START(addr) (cmd_line = addr)
  229. #else
  230. #define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
  231. #define PL330_DBGMC_START(addr) do {} while (0)
  232. #endif
  233. /* The number of default descriptors */
  234. #define NR_DEFAULT_DESC 16
  235. /* Delay for runtime PM autosuspend, ms */
  236. #define PL330_AUTOSUSPEND_DELAY 20
  237. /* Populated by the PL330 core driver for DMA API driver's info */
  238. struct pl330_config {
  239. u32 periph_id;
  240. #define DMAC_MODE_NS (1 << 0)
  241. unsigned int mode;
  242. unsigned int data_bus_width:10; /* In number of bits */
  243. unsigned int data_buf_dep:11;
  244. unsigned int num_chan:4;
  245. unsigned int num_peri:6;
  246. u32 peri_ns;
  247. unsigned int num_events:6;
  248. u32 irq_ns;
  249. };
  250. /*
  251. * Request Configuration.
  252. * The PL330 core does not modify this and uses the last
  253. * working configuration if the request doesn't provide any.
  254. *
  255. * The Client may want to provide this info only for the
  256. * first request and a request with new settings.
  257. */
  258. struct pl330_reqcfg {
  259. /* Address Incrementing */
  260. unsigned dst_inc:1;
  261. unsigned src_inc:1;
  262. /*
  263. * For now, the SRC & DST protection levels
  264. * and burst size/length are assumed same.
  265. */
  266. bool nonsecure;
  267. bool privileged;
  268. bool insnaccess;
  269. unsigned brst_len:5;
  270. unsigned brst_size:3; /* in power of 2 */
  271. enum pl330_cachectrl dcctl;
  272. enum pl330_cachectrl scctl;
  273. enum pl330_byteswap swap;
  274. struct pl330_config *pcfg;
  275. };
  276. /*
  277. * One cycle of DMAC operation.
  278. * There may be more than one xfer in a request.
  279. */
  280. struct pl330_xfer {
  281. u32 src_addr;
  282. u32 dst_addr;
  283. /* Size to xfer */
  284. u32 bytes;
  285. };
  286. /* The xfer callbacks are made with one of these arguments. */
  287. enum pl330_op_err {
  288. /* The all xfers in the request were success. */
  289. PL330_ERR_NONE,
  290. /* If req aborted due to global error. */
  291. PL330_ERR_ABORT,
  292. /* If req failed due to problem with Channel. */
  293. PL330_ERR_FAIL,
  294. };
  295. enum dmamov_dst {
  296. SAR = 0,
  297. CCR,
  298. DAR,
  299. };
  300. enum pl330_dst {
  301. SRC = 0,
  302. DST,
  303. };
  304. enum pl330_cond {
  305. SINGLE,
  306. BURST,
  307. ALWAYS,
  308. };
  309. struct dma_pl330_desc;
  310. struct _pl330_req {
  311. u32 mc_bus;
  312. void *mc_cpu;
  313. struct dma_pl330_desc *desc;
  314. };
  315. /* ToBeDone for tasklet */
  316. struct _pl330_tbd {
  317. bool reset_dmac;
  318. bool reset_mngr;
  319. u8 reset_chan;
  320. };
  321. /* A DMAC Thread */
  322. struct pl330_thread {
  323. u8 id;
  324. int ev;
  325. /* If the channel is not yet acquired by any client */
  326. bool free;
  327. /* Parent DMAC */
  328. struct pl330_dmac *dmac;
  329. /* Only two at a time */
  330. struct _pl330_req req[2];
  331. /* Index of the last enqueued request */
  332. unsigned lstenq;
  333. /* Index of the last submitted request or -1 if the DMA is stopped */
  334. int req_running;
  335. };
  336. enum pl330_dmac_state {
  337. UNINIT,
  338. INIT,
  339. DYING,
  340. };
  341. enum desc_status {
  342. /* In the DMAC pool */
  343. FREE,
  344. /*
  345. * Allocated to some channel during prep_xxx
  346. * Also may be sitting on the work_list.
  347. */
  348. PREP,
  349. /*
  350. * Sitting on the work_list and already submitted
  351. * to the PL330 core. Not more than two descriptors
  352. * of a channel can be BUSY at any time.
  353. */
  354. BUSY,
  355. /*
  356. * Pause was called while descriptor was BUSY. Due to hardware
  357. * limitations, only termination is possible for descriptors
  358. * that have been paused.
  359. */
  360. PAUSED,
  361. /*
  362. * Sitting on the channel work_list but xfer done
  363. * by PL330 core
  364. */
  365. DONE,
  366. };
  367. struct dma_pl330_chan {
  368. /* Schedule desc completion */
  369. struct tasklet_struct task;
  370. /* DMA-Engine Channel */
  371. struct dma_chan chan;
  372. /* List of submitted descriptors */
  373. struct list_head submitted_list;
  374. /* List of issued descriptors */
  375. struct list_head work_list;
  376. /* List of completed descriptors */
  377. struct list_head completed_list;
  378. /* Pointer to the DMAC that manages this channel,
  379. * NULL if the channel is available to be acquired.
  380. * As the parent, this DMAC also provides descriptors
  381. * to the channel.
  382. */
  383. struct pl330_dmac *dmac;
  384. /* To protect channel manipulation */
  385. spinlock_t lock;
  386. /*
  387. * Hardware channel thread of PL330 DMAC. NULL if the channel is
  388. * available.
  389. */
  390. struct pl330_thread *thread;
  391. /* For D-to-M and M-to-D channels */
  392. int burst_sz; /* the peripheral fifo width */
  393. int burst_len; /* the number of burst */
  394. phys_addr_t fifo_addr;
  395. /* DMA-mapped view of the FIFO; may differ if an IOMMU is present */
  396. dma_addr_t fifo_dma;
  397. enum dma_data_direction dir;
  398. struct dma_slave_config slave_config;
  399. /* for cyclic capability */
  400. bool cyclic;
  401. /* for runtime pm tracking */
  402. bool active;
  403. };
  404. struct pl330_dmac {
  405. /* DMA-Engine Device */
  406. struct dma_device ddma;
  407. /* Pool of descriptors available for the DMAC's channels */
  408. struct list_head desc_pool;
  409. /* To protect desc_pool manipulation */
  410. spinlock_t pool_lock;
  411. /* Size of MicroCode buffers for each channel. */
  412. unsigned mcbufsz;
  413. /* ioremap'ed address of PL330 registers. */
  414. void __iomem *base;
  415. /* Populated by the PL330 core driver during pl330_add */
  416. struct pl330_config pcfg;
  417. spinlock_t lock;
  418. /* Maximum possible events/irqs */
  419. int events[32];
  420. /* BUS address of MicroCode buffer */
  421. dma_addr_t mcode_bus;
  422. /* CPU address of MicroCode buffer */
  423. void *mcode_cpu;
  424. /* List of all Channel threads */
  425. struct pl330_thread *channels;
  426. /* Pointer to the MANAGER thread */
  427. struct pl330_thread *manager;
  428. /* To handle bad news in interrupt */
  429. struct tasklet_struct tasks;
  430. struct _pl330_tbd dmac_tbd;
  431. /* State of DMAC operation */
  432. enum pl330_dmac_state state;
  433. /* Holds list of reqs with due callbacks */
  434. struct list_head req_done;
  435. /* Peripheral channels connected to this DMAC */
  436. unsigned int num_peripherals;
  437. struct dma_pl330_chan *peripherals; /* keep at end */
  438. int quirks;
  439. struct reset_control *rstc;
  440. struct reset_control *rstc_ocp;
  441. };
  442. static struct pl330_of_quirks {
  443. char *quirk;
  444. int id;
  445. } of_quirks[] = {
  446. {
  447. .quirk = "arm,pl330-broken-no-flushp",
  448. .id = PL330_QUIRK_BROKEN_NO_FLUSHP,
  449. },
  450. {
  451. .quirk = "arm,pl330-periph-burst",
  452. .id = PL330_QUIRK_PERIPH_BURST,
  453. }
  454. };
  455. struct dma_pl330_desc {
  456. /* To attach to a queue as child */
  457. struct list_head node;
  458. /* Descriptor for the DMA Engine API */
  459. struct dma_async_tx_descriptor txd;
  460. /* Xfer for PL330 core */
  461. struct pl330_xfer px;
  462. struct pl330_reqcfg rqcfg;
  463. enum desc_status status;
  464. int bytes_requested;
  465. bool last;
  466. /* The channel which currently holds this desc */
  467. struct dma_pl330_chan *pchan;
  468. enum dma_transfer_direction rqtype;
  469. /* Index of peripheral for the xfer. */
  470. unsigned peri:5;
  471. /* Hook to attach to DMAC's list of reqs with due callback */
  472. struct list_head rqd;
  473. };
  474. struct _xfer_spec {
  475. u32 ccr;
  476. struct dma_pl330_desc *desc;
  477. };
  478. static int pl330_config_write(struct dma_chan *chan,
  479. struct dma_slave_config *slave_config,
  480. enum dma_transfer_direction direction);
  481. static inline bool _queue_full(struct pl330_thread *thrd)
  482. {
  483. return thrd->req[0].desc != NULL && thrd->req[1].desc != NULL;
  484. }
  485. static inline bool is_manager(struct pl330_thread *thrd)
  486. {
  487. return thrd->dmac->manager == thrd;
  488. }
  489. /* If manager of the thread is in Non-Secure mode */
  490. static inline bool _manager_ns(struct pl330_thread *thrd)
  491. {
  492. return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
  493. }
  494. static inline u32 get_revision(u32 periph_id)
  495. {
  496. return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
  497. }
  498. static inline u32 _emit_END(unsigned dry_run, u8 buf[])
  499. {
  500. if (dry_run)
  501. return SZ_DMAEND;
  502. buf[0] = CMD_DMAEND;
  503. PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
  504. return SZ_DMAEND;
  505. }
  506. static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
  507. {
  508. if (dry_run)
  509. return SZ_DMAFLUSHP;
  510. buf[0] = CMD_DMAFLUSHP;
  511. peri &= 0x1f;
  512. peri <<= 3;
  513. buf[1] = peri;
  514. PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
  515. return SZ_DMAFLUSHP;
  516. }
  517. static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  518. {
  519. if (dry_run)
  520. return SZ_DMALD;
  521. buf[0] = CMD_DMALD;
  522. if (cond == SINGLE)
  523. buf[0] |= (0 << 1) | (1 << 0);
  524. else if (cond == BURST)
  525. buf[0] |= (1 << 1) | (1 << 0);
  526. PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
  527. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  528. return SZ_DMALD;
  529. }
  530. static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
  531. enum pl330_cond cond, u8 peri)
  532. {
  533. if (dry_run)
  534. return SZ_DMALDP;
  535. buf[0] = CMD_DMALDP;
  536. if (cond == BURST)
  537. buf[0] |= (1 << 1);
  538. peri &= 0x1f;
  539. peri <<= 3;
  540. buf[1] = peri;
  541. PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
  542. cond == SINGLE ? 'S' : 'B', peri >> 3);
  543. return SZ_DMALDP;
  544. }
  545. static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
  546. unsigned loop, u8 cnt)
  547. {
  548. if (dry_run)
  549. return SZ_DMALP;
  550. buf[0] = CMD_DMALP;
  551. if (loop)
  552. buf[0] |= (1 << 1);
  553. cnt--; /* DMAC increments by 1 internally */
  554. buf[1] = cnt;
  555. PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
  556. return SZ_DMALP;
  557. }
  558. struct _arg_LPEND {
  559. enum pl330_cond cond;
  560. bool forever;
  561. unsigned loop;
  562. u8 bjump;
  563. };
  564. static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
  565. const struct _arg_LPEND *arg)
  566. {
  567. enum pl330_cond cond = arg->cond;
  568. bool forever = arg->forever;
  569. unsigned loop = arg->loop;
  570. u8 bjump = arg->bjump;
  571. if (dry_run)
  572. return SZ_DMALPEND;
  573. buf[0] = CMD_DMALPEND;
  574. if (loop)
  575. buf[0] |= (1 << 2);
  576. if (!forever)
  577. buf[0] |= (1 << 4);
  578. if (cond == SINGLE)
  579. buf[0] |= (0 << 1) | (1 << 0);
  580. else if (cond == BURST)
  581. buf[0] |= (1 << 1) | (1 << 0);
  582. buf[1] = bjump;
  583. PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
  584. forever ? "FE" : "END",
  585. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
  586. loop ? '1' : '0',
  587. bjump);
  588. return SZ_DMALPEND;
  589. }
  590. static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
  591. {
  592. if (dry_run)
  593. return SZ_DMAKILL;
  594. buf[0] = CMD_DMAKILL;
  595. return SZ_DMAKILL;
  596. }
  597. static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
  598. enum dmamov_dst dst, u32 val)
  599. {
  600. if (dry_run)
  601. return SZ_DMAMOV;
  602. buf[0] = CMD_DMAMOV;
  603. buf[1] = dst;
  604. buf[2] = val;
  605. buf[3] = val >> 8;
  606. buf[4] = val >> 16;
  607. buf[5] = val >> 24;
  608. PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
  609. dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
  610. return SZ_DMAMOV;
  611. }
  612. static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
  613. {
  614. if (dry_run)
  615. return SZ_DMARMB;
  616. buf[0] = CMD_DMARMB;
  617. PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
  618. return SZ_DMARMB;
  619. }
  620. static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
  621. {
  622. if (dry_run)
  623. return SZ_DMASEV;
  624. buf[0] = CMD_DMASEV;
  625. ev &= 0x1f;
  626. ev <<= 3;
  627. buf[1] = ev;
  628. PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
  629. return SZ_DMASEV;
  630. }
  631. static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  632. {
  633. if (dry_run)
  634. return SZ_DMAST;
  635. buf[0] = CMD_DMAST;
  636. if (cond == SINGLE)
  637. buf[0] |= (0 << 1) | (1 << 0);
  638. else if (cond == BURST)
  639. buf[0] |= (1 << 1) | (1 << 0);
  640. PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
  641. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  642. return SZ_DMAST;
  643. }
  644. static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
  645. enum pl330_cond cond, u8 peri)
  646. {
  647. if (dry_run)
  648. return SZ_DMASTP;
  649. buf[0] = CMD_DMASTP;
  650. if (cond == BURST)
  651. buf[0] |= (1 << 1);
  652. peri &= 0x1f;
  653. peri <<= 3;
  654. buf[1] = peri;
  655. PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
  656. cond == SINGLE ? 'S' : 'B', peri >> 3);
  657. return SZ_DMASTP;
  658. }
  659. static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
  660. enum pl330_cond cond, u8 peri)
  661. {
  662. if (dry_run)
  663. return SZ_DMAWFP;
  664. buf[0] = CMD_DMAWFP;
  665. if (cond == SINGLE)
  666. buf[0] |= (0 << 1) | (0 << 0);
  667. else if (cond == BURST)
  668. buf[0] |= (1 << 1) | (0 << 0);
  669. else
  670. buf[0] |= (0 << 1) | (1 << 0);
  671. peri &= 0x1f;
  672. peri <<= 3;
  673. buf[1] = peri;
  674. PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
  675. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
  676. return SZ_DMAWFP;
  677. }
  678. static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
  679. {
  680. if (dry_run)
  681. return SZ_DMAWMB;
  682. buf[0] = CMD_DMAWMB;
  683. PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
  684. return SZ_DMAWMB;
  685. }
  686. struct _arg_GO {
  687. u8 chan;
  688. u32 addr;
  689. unsigned ns;
  690. };
  691. static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
  692. const struct _arg_GO *arg)
  693. {
  694. u8 chan = arg->chan;
  695. u32 addr = arg->addr;
  696. unsigned ns = arg->ns;
  697. if (dry_run)
  698. return SZ_DMAGO;
  699. buf[0] = CMD_DMAGO;
  700. buf[0] |= (ns << 1);
  701. buf[1] = chan & 0x7;
  702. buf[2] = addr;
  703. buf[3] = addr >> 8;
  704. buf[4] = addr >> 16;
  705. buf[5] = addr >> 24;
  706. return SZ_DMAGO;
  707. }
  708. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  709. /* Returns Time-Out */
  710. static bool _until_dmac_idle(struct pl330_thread *thrd)
  711. {
  712. void __iomem *regs = thrd->dmac->base;
  713. unsigned long loops = msecs_to_loops(5);
  714. do {
  715. /* Until Manager is Idle */
  716. if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
  717. break;
  718. cpu_relax();
  719. } while (--loops);
  720. if (!loops)
  721. return true;
  722. return false;
  723. }
  724. static inline void _execute_DBGINSN(struct pl330_thread *thrd,
  725. u8 insn[], bool as_manager)
  726. {
  727. void __iomem *regs = thrd->dmac->base;
  728. u32 val;
  729. /* If timed out due to halted state-machine */
  730. if (_until_dmac_idle(thrd)) {
  731. dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
  732. return;
  733. }
  734. val = (insn[0] << 16) | (insn[1] << 24);
  735. if (!as_manager) {
  736. val |= (1 << 0);
  737. val |= (thrd->id << 8); /* Channel Number */
  738. }
  739. writel(val, regs + DBGINST0);
  740. val = le32_to_cpu(*((__le32 *)&insn[2]));
  741. writel(val, regs + DBGINST1);
  742. /* Get going */
  743. writel(0, regs + DBGCMD);
  744. }
  745. static inline u32 _state(struct pl330_thread *thrd)
  746. {
  747. void __iomem *regs = thrd->dmac->base;
  748. u32 val;
  749. if (is_manager(thrd))
  750. val = readl(regs + DS) & 0xf;
  751. else
  752. val = readl(regs + CS(thrd->id)) & 0xf;
  753. switch (val) {
  754. case DS_ST_STOP:
  755. return PL330_STATE_STOPPED;
  756. case DS_ST_EXEC:
  757. return PL330_STATE_EXECUTING;
  758. case DS_ST_CMISS:
  759. return PL330_STATE_CACHEMISS;
  760. case DS_ST_UPDTPC:
  761. return PL330_STATE_UPDTPC;
  762. case DS_ST_WFE:
  763. return PL330_STATE_WFE;
  764. case DS_ST_FAULT:
  765. return PL330_STATE_FAULTING;
  766. case DS_ST_ATBRR:
  767. if (is_manager(thrd))
  768. return PL330_STATE_INVALID;
  769. else
  770. return PL330_STATE_ATBARRIER;
  771. case DS_ST_QBUSY:
  772. if (is_manager(thrd))
  773. return PL330_STATE_INVALID;
  774. else
  775. return PL330_STATE_QUEUEBUSY;
  776. case DS_ST_WFP:
  777. if (is_manager(thrd))
  778. return PL330_STATE_INVALID;
  779. else
  780. return PL330_STATE_WFP;
  781. case DS_ST_KILL:
  782. if (is_manager(thrd))
  783. return PL330_STATE_INVALID;
  784. else
  785. return PL330_STATE_KILLING;
  786. case DS_ST_CMPLT:
  787. if (is_manager(thrd))
  788. return PL330_STATE_INVALID;
  789. else
  790. return PL330_STATE_COMPLETING;
  791. case DS_ST_FLTCMP:
  792. if (is_manager(thrd))
  793. return PL330_STATE_INVALID;
  794. else
  795. return PL330_STATE_FAULT_COMPLETING;
  796. default:
  797. return PL330_STATE_INVALID;
  798. }
  799. }
  800. static void _stop(struct pl330_thread *thrd)
  801. {
  802. void __iomem *regs = thrd->dmac->base;
  803. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  804. u32 inten = readl(regs + INTEN);
  805. if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
  806. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  807. /* Return if nothing needs to be done */
  808. if (_state(thrd) == PL330_STATE_COMPLETING
  809. || _state(thrd) == PL330_STATE_KILLING
  810. || _state(thrd) == PL330_STATE_STOPPED)
  811. return;
  812. _emit_KILL(0, insn);
  813. _execute_DBGINSN(thrd, insn, is_manager(thrd));
  814. /* clear the event */
  815. if (inten & (1 << thrd->ev))
  816. writel(1 << thrd->ev, regs + INTCLR);
  817. /* Stop generating interrupts for SEV */
  818. writel(inten & ~(1 << thrd->ev), regs + INTEN);
  819. }
  820. /* Start doing req 'idx' of thread 'thrd' */
  821. static bool _trigger(struct pl330_thread *thrd)
  822. {
  823. void __iomem *regs = thrd->dmac->base;
  824. struct _pl330_req *req;
  825. struct dma_pl330_desc *desc;
  826. struct _arg_GO go;
  827. unsigned ns;
  828. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  829. int idx;
  830. /* Return if already ACTIVE */
  831. if (_state(thrd) != PL330_STATE_STOPPED)
  832. return true;
  833. idx = 1 - thrd->lstenq;
  834. if (thrd->req[idx].desc != NULL) {
  835. req = &thrd->req[idx];
  836. } else {
  837. idx = thrd->lstenq;
  838. if (thrd->req[idx].desc != NULL)
  839. req = &thrd->req[idx];
  840. else
  841. req = NULL;
  842. }
  843. /* Return if no request */
  844. if (!req)
  845. return true;
  846. /* Return if req is running */
  847. if (idx == thrd->req_running)
  848. return true;
  849. desc = req->desc;
  850. ns = desc->rqcfg.nonsecure ? 1 : 0;
  851. /* See 'Abort Sources' point-4 at Page 2-25 */
  852. if (_manager_ns(thrd) && !ns)
  853. dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
  854. __func__, __LINE__);
  855. go.chan = thrd->id;
  856. go.addr = req->mc_bus;
  857. go.ns = ns;
  858. _emit_GO(0, insn, &go);
  859. /* Set to generate interrupts for SEV */
  860. writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
  861. /* Only manager can execute GO */
  862. _execute_DBGINSN(thrd, insn, true);
  863. thrd->req_running = idx;
  864. return true;
  865. }
  866. static bool pl330_start_thread(struct pl330_thread *thrd)
  867. {
  868. switch (_state(thrd)) {
  869. case PL330_STATE_FAULT_COMPLETING:
  870. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  871. if (_state(thrd) == PL330_STATE_KILLING)
  872. UNTIL(thrd, PL330_STATE_STOPPED)
  873. fallthrough;
  874. case PL330_STATE_FAULTING:
  875. _stop(thrd);
  876. fallthrough;
  877. case PL330_STATE_KILLING:
  878. case PL330_STATE_COMPLETING:
  879. UNTIL(thrd, PL330_STATE_STOPPED)
  880. fallthrough;
  881. case PL330_STATE_STOPPED:
  882. return _trigger(thrd);
  883. case PL330_STATE_WFP:
  884. case PL330_STATE_QUEUEBUSY:
  885. case PL330_STATE_ATBARRIER:
  886. case PL330_STATE_UPDTPC:
  887. case PL330_STATE_CACHEMISS:
  888. case PL330_STATE_EXECUTING:
  889. return true;
  890. case PL330_STATE_WFE: /* For RESUME, nothing yet */
  891. default:
  892. return false;
  893. }
  894. }
  895. static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
  896. const struct _xfer_spec *pxs, int cyc)
  897. {
  898. int off = 0;
  899. struct pl330_config *pcfg = pxs->desc->rqcfg.pcfg;
  900. /* check lock-up free version */
  901. if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
  902. while (cyc--) {
  903. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  904. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  905. }
  906. } else {
  907. while (cyc--) {
  908. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  909. off += _emit_RMB(dry_run, &buf[off]);
  910. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  911. off += _emit_WMB(dry_run, &buf[off]);
  912. }
  913. }
  914. return off;
  915. }
  916. static u32 _emit_load(unsigned int dry_run, u8 buf[],
  917. enum pl330_cond cond, enum dma_transfer_direction direction,
  918. u8 peri)
  919. {
  920. int off = 0;
  921. switch (direction) {
  922. case DMA_MEM_TO_MEM:
  923. case DMA_MEM_TO_DEV:
  924. off += _emit_LD(dry_run, &buf[off], cond);
  925. break;
  926. case DMA_DEV_TO_MEM:
  927. if (cond == ALWAYS) {
  928. off += _emit_LDP(dry_run, &buf[off], SINGLE,
  929. peri);
  930. off += _emit_LDP(dry_run, &buf[off], BURST,
  931. peri);
  932. } else {
  933. off += _emit_LDP(dry_run, &buf[off], cond,
  934. peri);
  935. }
  936. break;
  937. default:
  938. /* this code should be unreachable */
  939. WARN_ON(1);
  940. break;
  941. }
  942. return off;
  943. }
  944. static inline u32 _emit_store(unsigned int dry_run, u8 buf[],
  945. enum pl330_cond cond, enum dma_transfer_direction direction,
  946. u8 peri)
  947. {
  948. int off = 0;
  949. switch (direction) {
  950. case DMA_MEM_TO_MEM:
  951. case DMA_DEV_TO_MEM:
  952. off += _emit_ST(dry_run, &buf[off], cond);
  953. break;
  954. case DMA_MEM_TO_DEV:
  955. if (cond == ALWAYS) {
  956. off += _emit_STP(dry_run, &buf[off], SINGLE,
  957. peri);
  958. off += _emit_STP(dry_run, &buf[off], BURST,
  959. peri);
  960. } else {
  961. off += _emit_STP(dry_run, &buf[off], cond,
  962. peri);
  963. }
  964. break;
  965. default:
  966. /* this code should be unreachable */
  967. WARN_ON(1);
  968. break;
  969. }
  970. return off;
  971. }
  972. static inline int _ldst_peripheral(struct pl330_dmac *pl330,
  973. unsigned dry_run, u8 buf[],
  974. const struct _xfer_spec *pxs, int cyc,
  975. enum pl330_cond cond)
  976. {
  977. int off = 0;
  978. /*
  979. * do FLUSHP at beginning to clear any stale dma requests before the
  980. * first WFP.
  981. */
  982. if (!(pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP))
  983. off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
  984. while (cyc--) {
  985. off += _emit_WFP(dry_run, &buf[off], cond, pxs->desc->peri);
  986. off += _emit_load(dry_run, &buf[off], cond, pxs->desc->rqtype,
  987. pxs->desc->peri);
  988. off += _emit_store(dry_run, &buf[off], cond, pxs->desc->rqtype,
  989. pxs->desc->peri);
  990. }
  991. return off;
  992. }
  993. static int _bursts(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
  994. const struct _xfer_spec *pxs, int cyc)
  995. {
  996. int off = 0;
  997. enum pl330_cond cond = BRST_LEN(pxs->ccr) > 1 ? BURST : SINGLE;
  998. if (pl330->quirks & PL330_QUIRK_PERIPH_BURST)
  999. cond = BURST;
  1000. switch (pxs->desc->rqtype) {
  1001. case DMA_MEM_TO_DEV:
  1002. case DMA_DEV_TO_MEM:
  1003. off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs, cyc,
  1004. cond);
  1005. break;
  1006. case DMA_MEM_TO_MEM:
  1007. off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
  1008. break;
  1009. default:
  1010. /* this code should be unreachable */
  1011. WARN_ON(1);
  1012. break;
  1013. }
  1014. return off;
  1015. }
  1016. /*
  1017. * only the unaligned burst transfers have the dregs.
  1018. * so, still transfer dregs with a reduced size burst
  1019. * for mem-to-mem, mem-to-dev or dev-to-mem.
  1020. */
  1021. static int _dregs(struct pl330_dmac *pl330, unsigned int dry_run, u8 buf[],
  1022. const struct _xfer_spec *pxs, int transfer_length)
  1023. {
  1024. int off = 0;
  1025. int dregs_ccr;
  1026. if (transfer_length == 0)
  1027. return off;
  1028. /*
  1029. * dregs_len = (total bytes - BURST_TO_BYTE(bursts, ccr)) /
  1030. * BRST_SIZE(ccr)
  1031. * the dregs len must be smaller than burst len,
  1032. * so, for higher efficiency, we can modify CCR
  1033. * to use a reduced size burst len for the dregs.
  1034. */
  1035. dregs_ccr = pxs->ccr;
  1036. dregs_ccr &= ~((0xf << CC_SRCBRSTLEN_SHFT) |
  1037. (0xf << CC_DSTBRSTLEN_SHFT));
  1038. dregs_ccr |= (((transfer_length - 1) & 0xf) <<
  1039. CC_SRCBRSTLEN_SHFT);
  1040. dregs_ccr |= (((transfer_length - 1) & 0xf) <<
  1041. CC_DSTBRSTLEN_SHFT);
  1042. switch (pxs->desc->rqtype) {
  1043. case DMA_MEM_TO_DEV:
  1044. case DMA_DEV_TO_MEM:
  1045. off += _emit_MOV(dry_run, &buf[off], CCR, dregs_ccr);
  1046. off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs, 1,
  1047. BURST);
  1048. break;
  1049. case DMA_MEM_TO_MEM:
  1050. off += _emit_MOV(dry_run, &buf[off], CCR, dregs_ccr);
  1051. off += _ldst_memtomem(dry_run, &buf[off], pxs, 1);
  1052. break;
  1053. default:
  1054. /* this code should be unreachable */
  1055. WARN_ON(1);
  1056. break;
  1057. }
  1058. return off;
  1059. }
  1060. /* Returns bytes consumed and updates bursts */
  1061. static inline int _loop(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
  1062. unsigned long *bursts, const struct _xfer_spec *pxs)
  1063. {
  1064. int cyc, cycmax, szlp, szlpend, szbrst, off;
  1065. unsigned lcnt0, lcnt1, ljmp0, ljmp1;
  1066. struct _arg_LPEND lpend;
  1067. if (*bursts == 1)
  1068. return _bursts(pl330, dry_run, buf, pxs, 1);
  1069. /* Max iterations possible in DMALP is 256 */
  1070. if (*bursts >= 256*256) {
  1071. lcnt1 = 256;
  1072. lcnt0 = 256;
  1073. cyc = *bursts / lcnt1 / lcnt0;
  1074. } else if (*bursts > 256) {
  1075. lcnt1 = 256;
  1076. lcnt0 = *bursts / lcnt1;
  1077. cyc = 1;
  1078. } else {
  1079. lcnt1 = *bursts;
  1080. lcnt0 = 0;
  1081. cyc = 1;
  1082. }
  1083. szlp = _emit_LP(1, buf, 0, 0);
  1084. szbrst = _bursts(pl330, 1, buf, pxs, 1);
  1085. lpend.cond = ALWAYS;
  1086. lpend.forever = false;
  1087. lpend.loop = 0;
  1088. lpend.bjump = 0;
  1089. szlpend = _emit_LPEND(1, buf, &lpend);
  1090. if (lcnt0) {
  1091. szlp *= 2;
  1092. szlpend *= 2;
  1093. }
  1094. /*
  1095. * Max bursts that we can unroll due to limit on the
  1096. * size of backward jump that can be encoded in DMALPEND
  1097. * which is 8-bits and hence 255
  1098. */
  1099. cycmax = (255 - (szlp + szlpend)) / szbrst;
  1100. cyc = (cycmax < cyc) ? cycmax : cyc;
  1101. off = 0;
  1102. if (lcnt0) {
  1103. off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
  1104. ljmp0 = off;
  1105. }
  1106. off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
  1107. ljmp1 = off;
  1108. off += _bursts(pl330, dry_run, &buf[off], pxs, cyc);
  1109. lpend.cond = ALWAYS;
  1110. lpend.forever = false;
  1111. lpend.loop = 1;
  1112. lpend.bjump = off - ljmp1;
  1113. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1114. if (lcnt0) {
  1115. lpend.cond = ALWAYS;
  1116. lpend.forever = false;
  1117. lpend.loop = 0;
  1118. lpend.bjump = off - ljmp0;
  1119. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1120. }
  1121. *bursts = lcnt1 * cyc;
  1122. if (lcnt0)
  1123. *bursts *= lcnt0;
  1124. return off;
  1125. }
  1126. static inline int _setup_loops(struct pl330_dmac *pl330,
  1127. unsigned dry_run, u8 buf[],
  1128. const struct _xfer_spec *pxs)
  1129. {
  1130. struct pl330_xfer *x = &pxs->desc->px;
  1131. u32 ccr = pxs->ccr;
  1132. unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
  1133. int num_dregs = (x->bytes - BURST_TO_BYTE(bursts, ccr)) /
  1134. BRST_SIZE(ccr);
  1135. int off = 0;
  1136. while (bursts) {
  1137. c = bursts;
  1138. off += _loop(pl330, dry_run, &buf[off], &c, pxs);
  1139. bursts -= c;
  1140. }
  1141. off += _dregs(pl330, dry_run, &buf[off], pxs, num_dregs);
  1142. return off;
  1143. }
  1144. static inline int _setup_xfer(struct pl330_dmac *pl330,
  1145. unsigned dry_run, u8 buf[],
  1146. const struct _xfer_spec *pxs)
  1147. {
  1148. struct pl330_xfer *x = &pxs->desc->px;
  1149. int off = 0;
  1150. /* DMAMOV SAR, x->src_addr */
  1151. off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
  1152. /* DMAMOV DAR, x->dst_addr */
  1153. off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
  1154. /* Setup Loop(s) */
  1155. off += _setup_loops(pl330, dry_run, &buf[off], pxs);
  1156. return off;
  1157. }
  1158. /*
  1159. * A req is a sequence of one or more xfer units.
  1160. * Returns the number of bytes taken to setup the MC for the req.
  1161. */
  1162. static int _setup_req(struct pl330_dmac *pl330, unsigned dry_run,
  1163. struct pl330_thread *thrd, unsigned index,
  1164. struct _xfer_spec *pxs)
  1165. {
  1166. struct _pl330_req *req = &thrd->req[index];
  1167. u8 *buf = req->mc_cpu;
  1168. int off = 0;
  1169. PL330_DBGMC_START(req->mc_bus);
  1170. /* DMAMOV CCR, ccr */
  1171. off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
  1172. off += _setup_xfer(pl330, dry_run, &buf[off], pxs);
  1173. /* DMASEV peripheral/event */
  1174. off += _emit_SEV(dry_run, &buf[off], thrd->ev);
  1175. /* DMAEND */
  1176. off += _emit_END(dry_run, &buf[off]);
  1177. return off;
  1178. }
  1179. static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
  1180. {
  1181. u32 ccr = 0;
  1182. if (rqc->src_inc)
  1183. ccr |= CC_SRCINC;
  1184. if (rqc->dst_inc)
  1185. ccr |= CC_DSTINC;
  1186. /* We set same protection levels for Src and DST for now */
  1187. if (rqc->privileged)
  1188. ccr |= CC_SRCPRI | CC_DSTPRI;
  1189. if (rqc->nonsecure)
  1190. ccr |= CC_SRCNS | CC_DSTNS;
  1191. if (rqc->insnaccess)
  1192. ccr |= CC_SRCIA | CC_DSTIA;
  1193. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
  1194. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
  1195. ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
  1196. ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
  1197. ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
  1198. ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
  1199. ccr |= (rqc->swap << CC_SWAP_SHFT);
  1200. return ccr;
  1201. }
  1202. /*
  1203. * Submit a list of xfers after which the client wants notification.
  1204. * Client is not notified after each xfer unit, just once after all
  1205. * xfer units are done or some error occurs.
  1206. */
  1207. static int pl330_submit_req(struct pl330_thread *thrd,
  1208. struct dma_pl330_desc *desc)
  1209. {
  1210. struct pl330_dmac *pl330 = thrd->dmac;
  1211. struct _xfer_spec xs;
  1212. unsigned long flags;
  1213. unsigned idx;
  1214. u32 ccr;
  1215. int ret = 0;
  1216. switch (desc->rqtype) {
  1217. case DMA_MEM_TO_DEV:
  1218. break;
  1219. case DMA_DEV_TO_MEM:
  1220. break;
  1221. case DMA_MEM_TO_MEM:
  1222. break;
  1223. default:
  1224. return -ENOTSUPP;
  1225. }
  1226. if (pl330->state == DYING
  1227. || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
  1228. dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
  1229. __func__, __LINE__);
  1230. return -EAGAIN;
  1231. }
  1232. /* If request for non-existing peripheral */
  1233. if (desc->rqtype != DMA_MEM_TO_MEM &&
  1234. desc->peri >= pl330->pcfg.num_peri) {
  1235. dev_info(thrd->dmac->ddma.dev,
  1236. "%s:%d Invalid peripheral(%u)!\n",
  1237. __func__, __LINE__, desc->peri);
  1238. return -EINVAL;
  1239. }
  1240. spin_lock_irqsave(&pl330->lock, flags);
  1241. if (_queue_full(thrd)) {
  1242. ret = -EAGAIN;
  1243. goto xfer_exit;
  1244. }
  1245. /* Prefer Secure Channel */
  1246. if (!_manager_ns(thrd))
  1247. desc->rqcfg.nonsecure = 0;
  1248. else
  1249. desc->rqcfg.nonsecure = 1;
  1250. ccr = _prepare_ccr(&desc->rqcfg);
  1251. idx = thrd->req[0].desc == NULL ? 0 : 1;
  1252. xs.ccr = ccr;
  1253. xs.desc = desc;
  1254. /* First dry run to check if req is acceptable */
  1255. ret = _setup_req(pl330, 1, thrd, idx, &xs);
  1256. if (ret > pl330->mcbufsz / 2) {
  1257. dev_info(pl330->ddma.dev, "%s:%d Try increasing mcbufsz (%i/%i)\n",
  1258. __func__, __LINE__, ret, pl330->mcbufsz / 2);
  1259. ret = -ENOMEM;
  1260. goto xfer_exit;
  1261. }
  1262. /* Hook the request */
  1263. thrd->lstenq = idx;
  1264. thrd->req[idx].desc = desc;
  1265. _setup_req(pl330, 0, thrd, idx, &xs);
  1266. ret = 0;
  1267. xfer_exit:
  1268. spin_unlock_irqrestore(&pl330->lock, flags);
  1269. return ret;
  1270. }
  1271. static void dma_pl330_rqcb(struct dma_pl330_desc *desc, enum pl330_op_err err)
  1272. {
  1273. struct dma_pl330_chan *pch;
  1274. unsigned long flags;
  1275. if (!desc)
  1276. return;
  1277. pch = desc->pchan;
  1278. /* If desc aborted */
  1279. if (!pch)
  1280. return;
  1281. spin_lock_irqsave(&pch->lock, flags);
  1282. desc->status = DONE;
  1283. spin_unlock_irqrestore(&pch->lock, flags);
  1284. tasklet_schedule(&pch->task);
  1285. }
  1286. static void pl330_dotask(struct tasklet_struct *t)
  1287. {
  1288. struct pl330_dmac *pl330 = from_tasklet(pl330, t, tasks);
  1289. unsigned long flags;
  1290. int i;
  1291. spin_lock_irqsave(&pl330->lock, flags);
  1292. /* The DMAC itself gone nuts */
  1293. if (pl330->dmac_tbd.reset_dmac) {
  1294. pl330->state = DYING;
  1295. /* Reset the manager too */
  1296. pl330->dmac_tbd.reset_mngr = true;
  1297. /* Clear the reset flag */
  1298. pl330->dmac_tbd.reset_dmac = false;
  1299. }
  1300. if (pl330->dmac_tbd.reset_mngr) {
  1301. _stop(pl330->manager);
  1302. /* Reset all channels */
  1303. pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
  1304. /* Clear the reset flag */
  1305. pl330->dmac_tbd.reset_mngr = false;
  1306. }
  1307. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1308. if (pl330->dmac_tbd.reset_chan & (1 << i)) {
  1309. struct pl330_thread *thrd = &pl330->channels[i];
  1310. void __iomem *regs = pl330->base;
  1311. enum pl330_op_err err;
  1312. _stop(thrd);
  1313. if (readl(regs + FSC) & (1 << thrd->id))
  1314. err = PL330_ERR_FAIL;
  1315. else
  1316. err = PL330_ERR_ABORT;
  1317. spin_unlock_irqrestore(&pl330->lock, flags);
  1318. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, err);
  1319. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, err);
  1320. spin_lock_irqsave(&pl330->lock, flags);
  1321. thrd->req[0].desc = NULL;
  1322. thrd->req[1].desc = NULL;
  1323. thrd->req_running = -1;
  1324. /* Clear the reset flag */
  1325. pl330->dmac_tbd.reset_chan &= ~(1 << i);
  1326. }
  1327. }
  1328. spin_unlock_irqrestore(&pl330->lock, flags);
  1329. return;
  1330. }
  1331. /* Returns 1 if state was updated, 0 otherwise */
  1332. static int pl330_update(struct pl330_dmac *pl330)
  1333. {
  1334. struct dma_pl330_desc *descdone;
  1335. unsigned long flags;
  1336. void __iomem *regs;
  1337. u32 val;
  1338. int id, ev, ret = 0;
  1339. regs = pl330->base;
  1340. spin_lock_irqsave(&pl330->lock, flags);
  1341. val = readl(regs + FSM) & 0x1;
  1342. if (val)
  1343. pl330->dmac_tbd.reset_mngr = true;
  1344. else
  1345. pl330->dmac_tbd.reset_mngr = false;
  1346. val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
  1347. pl330->dmac_tbd.reset_chan |= val;
  1348. if (val) {
  1349. int i = 0;
  1350. while (i < pl330->pcfg.num_chan) {
  1351. if (val & (1 << i)) {
  1352. dev_info(pl330->ddma.dev,
  1353. "Reset Channel-%d\t CS-%x FTC-%x\n",
  1354. i, readl(regs + CS(i)),
  1355. readl(regs + FTC(i)));
  1356. _stop(&pl330->channels[i]);
  1357. }
  1358. i++;
  1359. }
  1360. }
  1361. /* Check which event happened i.e, thread notified */
  1362. val = readl(regs + ES);
  1363. if (pl330->pcfg.num_events < 32
  1364. && val & ~((1 << pl330->pcfg.num_events) - 1)) {
  1365. pl330->dmac_tbd.reset_dmac = true;
  1366. dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
  1367. __LINE__);
  1368. ret = 1;
  1369. goto updt_exit;
  1370. }
  1371. for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
  1372. if (val & (1 << ev)) { /* Event occurred */
  1373. struct pl330_thread *thrd;
  1374. u32 inten = readl(regs + INTEN);
  1375. int active;
  1376. /* Clear the event */
  1377. if (inten & (1 << ev))
  1378. writel(1 << ev, regs + INTCLR);
  1379. ret = 1;
  1380. id = pl330->events[ev];
  1381. thrd = &pl330->channels[id];
  1382. active = thrd->req_running;
  1383. if (active == -1) /* Aborted */
  1384. continue;
  1385. /* Detach the req */
  1386. descdone = thrd->req[active].desc;
  1387. thrd->req[active].desc = NULL;
  1388. thrd->req_running = -1;
  1389. /* Get going again ASAP */
  1390. pl330_start_thread(thrd);
  1391. /* For now, just make a list of callbacks to be done */
  1392. list_add_tail(&descdone->rqd, &pl330->req_done);
  1393. }
  1394. }
  1395. /* Now that we are in no hurry, do the callbacks */
  1396. while (!list_empty(&pl330->req_done)) {
  1397. descdone = list_first_entry(&pl330->req_done,
  1398. struct dma_pl330_desc, rqd);
  1399. list_del(&descdone->rqd);
  1400. spin_unlock_irqrestore(&pl330->lock, flags);
  1401. dma_pl330_rqcb(descdone, PL330_ERR_NONE);
  1402. spin_lock_irqsave(&pl330->lock, flags);
  1403. }
  1404. updt_exit:
  1405. spin_unlock_irqrestore(&pl330->lock, flags);
  1406. if (pl330->dmac_tbd.reset_dmac
  1407. || pl330->dmac_tbd.reset_mngr
  1408. || pl330->dmac_tbd.reset_chan) {
  1409. ret = 1;
  1410. tasklet_schedule(&pl330->tasks);
  1411. }
  1412. return ret;
  1413. }
  1414. /* Reserve an event */
  1415. static inline int _alloc_event(struct pl330_thread *thrd)
  1416. {
  1417. struct pl330_dmac *pl330 = thrd->dmac;
  1418. int ev;
  1419. for (ev = 0; ev < pl330->pcfg.num_events; ev++)
  1420. if (pl330->events[ev] == -1) {
  1421. pl330->events[ev] = thrd->id;
  1422. return ev;
  1423. }
  1424. return -1;
  1425. }
  1426. static bool _chan_ns(const struct pl330_dmac *pl330, int i)
  1427. {
  1428. return pl330->pcfg.irq_ns & (1 << i);
  1429. }
  1430. /* Upon success, returns IdentityToken for the
  1431. * allocated channel, NULL otherwise.
  1432. */
  1433. static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
  1434. {
  1435. struct pl330_thread *thrd = NULL;
  1436. int chans, i;
  1437. if (pl330->state == DYING)
  1438. return NULL;
  1439. chans = pl330->pcfg.num_chan;
  1440. for (i = 0; i < chans; i++) {
  1441. thrd = &pl330->channels[i];
  1442. if ((thrd->free) && (!_manager_ns(thrd) ||
  1443. _chan_ns(pl330, i))) {
  1444. thrd->ev = _alloc_event(thrd);
  1445. if (thrd->ev >= 0) {
  1446. thrd->free = false;
  1447. thrd->lstenq = 1;
  1448. thrd->req[0].desc = NULL;
  1449. thrd->req[1].desc = NULL;
  1450. thrd->req_running = -1;
  1451. break;
  1452. }
  1453. }
  1454. thrd = NULL;
  1455. }
  1456. return thrd;
  1457. }
  1458. /* Release an event */
  1459. static inline void _free_event(struct pl330_thread *thrd, int ev)
  1460. {
  1461. struct pl330_dmac *pl330 = thrd->dmac;
  1462. /* If the event is valid and was held by the thread */
  1463. if (ev >= 0 && ev < pl330->pcfg.num_events
  1464. && pl330->events[ev] == thrd->id)
  1465. pl330->events[ev] = -1;
  1466. }
  1467. static void pl330_release_channel(struct pl330_thread *thrd)
  1468. {
  1469. if (!thrd || thrd->free)
  1470. return;
  1471. _stop(thrd);
  1472. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, PL330_ERR_ABORT);
  1473. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, PL330_ERR_ABORT);
  1474. _free_event(thrd, thrd->ev);
  1475. thrd->free = true;
  1476. }
  1477. /* Initialize the structure for PL330 configuration, that can be used
  1478. * by the client driver the make best use of the DMAC
  1479. */
  1480. static void read_dmac_config(struct pl330_dmac *pl330)
  1481. {
  1482. void __iomem *regs = pl330->base;
  1483. u32 val;
  1484. val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
  1485. val &= CRD_DATA_WIDTH_MASK;
  1486. pl330->pcfg.data_bus_width = 8 * (1 << val);
  1487. val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
  1488. val &= CRD_DATA_BUFF_MASK;
  1489. pl330->pcfg.data_buf_dep = val + 1;
  1490. val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
  1491. val &= CR0_NUM_CHANS_MASK;
  1492. val += 1;
  1493. pl330->pcfg.num_chan = val;
  1494. val = readl(regs + CR0);
  1495. if (val & CR0_PERIPH_REQ_SET) {
  1496. val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
  1497. val += 1;
  1498. pl330->pcfg.num_peri = val;
  1499. pl330->pcfg.peri_ns = readl(regs + CR4);
  1500. } else {
  1501. pl330->pcfg.num_peri = 0;
  1502. }
  1503. val = readl(regs + CR0);
  1504. if (val & CR0_BOOT_MAN_NS)
  1505. pl330->pcfg.mode |= DMAC_MODE_NS;
  1506. else
  1507. pl330->pcfg.mode &= ~DMAC_MODE_NS;
  1508. val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
  1509. val &= CR0_NUM_EVENTS_MASK;
  1510. val += 1;
  1511. pl330->pcfg.num_events = val;
  1512. pl330->pcfg.irq_ns = readl(regs + CR3);
  1513. }
  1514. static inline void _reset_thread(struct pl330_thread *thrd)
  1515. {
  1516. struct pl330_dmac *pl330 = thrd->dmac;
  1517. thrd->req[0].mc_cpu = pl330->mcode_cpu
  1518. + (thrd->id * pl330->mcbufsz);
  1519. thrd->req[0].mc_bus = pl330->mcode_bus
  1520. + (thrd->id * pl330->mcbufsz);
  1521. thrd->req[0].desc = NULL;
  1522. thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
  1523. + pl330->mcbufsz / 2;
  1524. thrd->req[1].mc_bus = thrd->req[0].mc_bus
  1525. + pl330->mcbufsz / 2;
  1526. thrd->req[1].desc = NULL;
  1527. thrd->req_running = -1;
  1528. }
  1529. static int dmac_alloc_threads(struct pl330_dmac *pl330)
  1530. {
  1531. int chans = pl330->pcfg.num_chan;
  1532. struct pl330_thread *thrd;
  1533. int i;
  1534. /* Allocate 1 Manager and 'chans' Channel threads */
  1535. pl330->channels = kcalloc(1 + chans, sizeof(*thrd),
  1536. GFP_KERNEL);
  1537. if (!pl330->channels)
  1538. return -ENOMEM;
  1539. /* Init Channel threads */
  1540. for (i = 0; i < chans; i++) {
  1541. thrd = &pl330->channels[i];
  1542. thrd->id = i;
  1543. thrd->dmac = pl330;
  1544. _reset_thread(thrd);
  1545. thrd->free = true;
  1546. }
  1547. /* MANAGER is indexed at the end */
  1548. thrd = &pl330->channels[chans];
  1549. thrd->id = chans;
  1550. thrd->dmac = pl330;
  1551. thrd->free = false;
  1552. pl330->manager = thrd;
  1553. return 0;
  1554. }
  1555. static int dmac_alloc_resources(struct pl330_dmac *pl330)
  1556. {
  1557. int chans = pl330->pcfg.num_chan;
  1558. int ret;
  1559. /*
  1560. * Alloc MicroCode buffer for 'chans' Channel threads.
  1561. * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
  1562. */
  1563. pl330->mcode_cpu = dma_alloc_attrs(pl330->ddma.dev,
  1564. chans * pl330->mcbufsz,
  1565. &pl330->mcode_bus, GFP_KERNEL,
  1566. DMA_ATTR_PRIVILEGED);
  1567. if (!pl330->mcode_cpu) {
  1568. dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
  1569. __func__, __LINE__);
  1570. return -ENOMEM;
  1571. }
  1572. ret = dmac_alloc_threads(pl330);
  1573. if (ret) {
  1574. dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
  1575. __func__, __LINE__);
  1576. dma_free_attrs(pl330->ddma.dev,
  1577. chans * pl330->mcbufsz,
  1578. pl330->mcode_cpu, pl330->mcode_bus,
  1579. DMA_ATTR_PRIVILEGED);
  1580. return ret;
  1581. }
  1582. return 0;
  1583. }
  1584. static int pl330_add(struct pl330_dmac *pl330)
  1585. {
  1586. int i, ret;
  1587. /* Check if we can handle this DMAC */
  1588. if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
  1589. dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
  1590. pl330->pcfg.periph_id);
  1591. return -EINVAL;
  1592. }
  1593. /* Read the configuration of the DMAC */
  1594. read_dmac_config(pl330);
  1595. if (pl330->pcfg.num_events == 0) {
  1596. dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
  1597. __func__, __LINE__);
  1598. return -EINVAL;
  1599. }
  1600. spin_lock_init(&pl330->lock);
  1601. INIT_LIST_HEAD(&pl330->req_done);
  1602. /* Use default MC buffer size if not provided */
  1603. if (!pl330->mcbufsz)
  1604. pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
  1605. /* Mark all events as free */
  1606. for (i = 0; i < pl330->pcfg.num_events; i++)
  1607. pl330->events[i] = -1;
  1608. /* Allocate resources needed by the DMAC */
  1609. ret = dmac_alloc_resources(pl330);
  1610. if (ret) {
  1611. dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
  1612. return ret;
  1613. }
  1614. tasklet_setup(&pl330->tasks, pl330_dotask);
  1615. pl330->state = INIT;
  1616. return 0;
  1617. }
  1618. static int dmac_free_threads(struct pl330_dmac *pl330)
  1619. {
  1620. struct pl330_thread *thrd;
  1621. int i;
  1622. /* Release Channel threads */
  1623. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1624. thrd = &pl330->channels[i];
  1625. pl330_release_channel(thrd);
  1626. }
  1627. /* Free memory */
  1628. kfree(pl330->channels);
  1629. return 0;
  1630. }
  1631. static void pl330_del(struct pl330_dmac *pl330)
  1632. {
  1633. pl330->state = UNINIT;
  1634. tasklet_kill(&pl330->tasks);
  1635. /* Free DMAC resources */
  1636. dmac_free_threads(pl330);
  1637. dma_free_attrs(pl330->ddma.dev,
  1638. pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
  1639. pl330->mcode_bus, DMA_ATTR_PRIVILEGED);
  1640. }
  1641. /* forward declaration */
  1642. static struct amba_driver pl330_driver;
  1643. static inline struct dma_pl330_chan *
  1644. to_pchan(struct dma_chan *ch)
  1645. {
  1646. if (!ch)
  1647. return NULL;
  1648. return container_of(ch, struct dma_pl330_chan, chan);
  1649. }
  1650. static inline struct dma_pl330_desc *
  1651. to_desc(struct dma_async_tx_descriptor *tx)
  1652. {
  1653. return container_of(tx, struct dma_pl330_desc, txd);
  1654. }
  1655. static inline void fill_queue(struct dma_pl330_chan *pch)
  1656. {
  1657. struct dma_pl330_desc *desc;
  1658. int ret;
  1659. list_for_each_entry(desc, &pch->work_list, node) {
  1660. /* If already submitted */
  1661. if (desc->status == BUSY || desc->status == PAUSED)
  1662. continue;
  1663. ret = pl330_submit_req(pch->thread, desc);
  1664. if (!ret) {
  1665. desc->status = BUSY;
  1666. } else if (ret == -EAGAIN) {
  1667. /* QFull or DMAC Dying */
  1668. break;
  1669. } else {
  1670. /* Unacceptable request */
  1671. desc->status = DONE;
  1672. dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
  1673. __func__, __LINE__, desc->txd.cookie);
  1674. tasklet_schedule(&pch->task);
  1675. }
  1676. }
  1677. }
  1678. static void pl330_tasklet(struct tasklet_struct *t)
  1679. {
  1680. struct dma_pl330_chan *pch = from_tasklet(pch, t, task);
  1681. struct dma_pl330_desc *desc, *_dt;
  1682. unsigned long flags;
  1683. bool power_down = false;
  1684. spin_lock_irqsave(&pch->lock, flags);
  1685. /* Pick up ripe tomatoes */
  1686. list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
  1687. if (desc->status == DONE) {
  1688. if (!pch->cyclic)
  1689. dma_cookie_complete(&desc->txd);
  1690. list_move_tail(&desc->node, &pch->completed_list);
  1691. }
  1692. /* Try to submit a req imm. next to the last completed cookie */
  1693. fill_queue(pch);
  1694. if (list_empty(&pch->work_list)) {
  1695. spin_lock(&pch->thread->dmac->lock);
  1696. _stop(pch->thread);
  1697. spin_unlock(&pch->thread->dmac->lock);
  1698. power_down = true;
  1699. pch->active = false;
  1700. } else {
  1701. /* Make sure the PL330 Channel thread is active */
  1702. spin_lock(&pch->thread->dmac->lock);
  1703. pl330_start_thread(pch->thread);
  1704. spin_unlock(&pch->thread->dmac->lock);
  1705. }
  1706. while (!list_empty(&pch->completed_list)) {
  1707. struct dmaengine_desc_callback cb;
  1708. desc = list_first_entry(&pch->completed_list,
  1709. struct dma_pl330_desc, node);
  1710. dmaengine_desc_get_callback(&desc->txd, &cb);
  1711. if (pch->cyclic) {
  1712. desc->status = PREP;
  1713. list_move_tail(&desc->node, &pch->work_list);
  1714. if (power_down) {
  1715. pch->active = true;
  1716. spin_lock(&pch->thread->dmac->lock);
  1717. pl330_start_thread(pch->thread);
  1718. spin_unlock(&pch->thread->dmac->lock);
  1719. power_down = false;
  1720. }
  1721. } else {
  1722. desc->status = FREE;
  1723. list_move_tail(&desc->node, &pch->dmac->desc_pool);
  1724. }
  1725. dma_descriptor_unmap(&desc->txd);
  1726. if (dmaengine_desc_callback_valid(&cb)) {
  1727. spin_unlock_irqrestore(&pch->lock, flags);
  1728. dmaengine_desc_callback_invoke(&cb, NULL);
  1729. spin_lock_irqsave(&pch->lock, flags);
  1730. }
  1731. }
  1732. spin_unlock_irqrestore(&pch->lock, flags);
  1733. /* If work list empty, power down */
  1734. if (power_down) {
  1735. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1736. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1737. }
  1738. }
  1739. static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
  1740. struct of_dma *ofdma)
  1741. {
  1742. int count = dma_spec->args_count;
  1743. struct pl330_dmac *pl330 = ofdma->of_dma_data;
  1744. unsigned int chan_id;
  1745. if (!pl330)
  1746. return NULL;
  1747. if (count != 1)
  1748. return NULL;
  1749. chan_id = dma_spec->args[0];
  1750. if (chan_id >= pl330->num_peripherals)
  1751. return NULL;
  1752. return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
  1753. }
  1754. static int pl330_alloc_chan_resources(struct dma_chan *chan)
  1755. {
  1756. struct dma_pl330_chan *pch = to_pchan(chan);
  1757. struct pl330_dmac *pl330 = pch->dmac;
  1758. unsigned long flags;
  1759. spin_lock_irqsave(&pl330->lock, flags);
  1760. dma_cookie_init(chan);
  1761. pch->cyclic = false;
  1762. pch->thread = pl330_request_channel(pl330);
  1763. if (!pch->thread) {
  1764. spin_unlock_irqrestore(&pl330->lock, flags);
  1765. return -ENOMEM;
  1766. }
  1767. tasklet_setup(&pch->task, pl330_tasklet);
  1768. spin_unlock_irqrestore(&pl330->lock, flags);
  1769. return 1;
  1770. }
  1771. /*
  1772. * We need the data direction between the DMAC (the dma-mapping "device") and
  1773. * the FIFO (the dmaengine "dev"), from the FIFO's point of view. Confusing!
  1774. */
  1775. static enum dma_data_direction
  1776. pl330_dma_slave_map_dir(enum dma_transfer_direction dir)
  1777. {
  1778. switch (dir) {
  1779. case DMA_MEM_TO_DEV:
  1780. return DMA_FROM_DEVICE;
  1781. case DMA_DEV_TO_MEM:
  1782. return DMA_TO_DEVICE;
  1783. case DMA_DEV_TO_DEV:
  1784. return DMA_BIDIRECTIONAL;
  1785. default:
  1786. return DMA_NONE;
  1787. }
  1788. }
  1789. static void pl330_unprep_slave_fifo(struct dma_pl330_chan *pch)
  1790. {
  1791. if (pch->dir != DMA_NONE)
  1792. dma_unmap_resource(pch->chan.device->dev, pch->fifo_dma,
  1793. 1 << pch->burst_sz, pch->dir, 0);
  1794. pch->dir = DMA_NONE;
  1795. }
  1796. static bool pl330_prep_slave_fifo(struct dma_pl330_chan *pch,
  1797. enum dma_transfer_direction dir)
  1798. {
  1799. struct device *dev = pch->chan.device->dev;
  1800. enum dma_data_direction dma_dir = pl330_dma_slave_map_dir(dir);
  1801. /* Already mapped for this config? */
  1802. if (pch->dir == dma_dir)
  1803. return true;
  1804. pl330_unprep_slave_fifo(pch);
  1805. pch->fifo_dma = dma_map_resource(dev, pch->fifo_addr,
  1806. 1 << pch->burst_sz, dma_dir, 0);
  1807. if (dma_mapping_error(dev, pch->fifo_dma))
  1808. return false;
  1809. pch->dir = dma_dir;
  1810. return true;
  1811. }
  1812. static int fixup_burst_len(int max_burst_len, int quirks)
  1813. {
  1814. if (max_burst_len > PL330_MAX_BURST)
  1815. return PL330_MAX_BURST;
  1816. else if (max_burst_len < 1)
  1817. return 1;
  1818. else
  1819. return max_burst_len;
  1820. }
  1821. static int pl330_config_write(struct dma_chan *chan,
  1822. struct dma_slave_config *slave_config,
  1823. enum dma_transfer_direction direction)
  1824. {
  1825. struct dma_pl330_chan *pch = to_pchan(chan);
  1826. pl330_unprep_slave_fifo(pch);
  1827. if (direction == DMA_MEM_TO_DEV) {
  1828. if (slave_config->dst_addr)
  1829. pch->fifo_addr = slave_config->dst_addr;
  1830. if (slave_config->dst_addr_width)
  1831. pch->burst_sz = __ffs(slave_config->dst_addr_width);
  1832. pch->burst_len = fixup_burst_len(slave_config->dst_maxburst,
  1833. pch->dmac->quirks);
  1834. } else if (direction == DMA_DEV_TO_MEM) {
  1835. if (slave_config->src_addr)
  1836. pch->fifo_addr = slave_config->src_addr;
  1837. if (slave_config->src_addr_width)
  1838. pch->burst_sz = __ffs(slave_config->src_addr_width);
  1839. pch->burst_len = fixup_burst_len(slave_config->src_maxburst,
  1840. pch->dmac->quirks);
  1841. }
  1842. return 0;
  1843. }
  1844. static int pl330_config(struct dma_chan *chan,
  1845. struct dma_slave_config *slave_config)
  1846. {
  1847. struct dma_pl330_chan *pch = to_pchan(chan);
  1848. memcpy(&pch->slave_config, slave_config, sizeof(*slave_config));
  1849. return 0;
  1850. }
  1851. static int pl330_terminate_all(struct dma_chan *chan)
  1852. {
  1853. struct dma_pl330_chan *pch = to_pchan(chan);
  1854. struct dma_pl330_desc *desc;
  1855. unsigned long flags;
  1856. struct pl330_dmac *pl330 = pch->dmac;
  1857. bool power_down = false;
  1858. pm_runtime_get_sync(pl330->ddma.dev);
  1859. spin_lock_irqsave(&pch->lock, flags);
  1860. spin_lock(&pl330->lock);
  1861. _stop(pch->thread);
  1862. pch->thread->req[0].desc = NULL;
  1863. pch->thread->req[1].desc = NULL;
  1864. pch->thread->req_running = -1;
  1865. spin_unlock(&pl330->lock);
  1866. power_down = pch->active;
  1867. pch->active = false;
  1868. /* Mark all desc done */
  1869. list_for_each_entry(desc, &pch->submitted_list, node) {
  1870. desc->status = FREE;
  1871. dma_cookie_complete(&desc->txd);
  1872. }
  1873. list_for_each_entry(desc, &pch->work_list , node) {
  1874. desc->status = FREE;
  1875. dma_cookie_complete(&desc->txd);
  1876. }
  1877. list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
  1878. list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
  1879. list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
  1880. spin_unlock_irqrestore(&pch->lock, flags);
  1881. pm_runtime_mark_last_busy(pl330->ddma.dev);
  1882. if (power_down)
  1883. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1884. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1885. return 0;
  1886. }
  1887. /*
  1888. * We don't support DMA_RESUME command because of hardware
  1889. * limitations, so after pausing the channel we cannot restore
  1890. * it to active state. We have to terminate channel and setup
  1891. * DMA transfer again. This pause feature was implemented to
  1892. * allow safely read residue before channel termination.
  1893. */
  1894. static int pl330_pause(struct dma_chan *chan)
  1895. {
  1896. struct dma_pl330_chan *pch = to_pchan(chan);
  1897. struct pl330_dmac *pl330 = pch->dmac;
  1898. struct dma_pl330_desc *desc;
  1899. unsigned long flags;
  1900. pm_runtime_get_sync(pl330->ddma.dev);
  1901. spin_lock_irqsave(&pch->lock, flags);
  1902. spin_lock(&pl330->lock);
  1903. _stop(pch->thread);
  1904. spin_unlock(&pl330->lock);
  1905. list_for_each_entry(desc, &pch->work_list, node) {
  1906. if (desc->status == BUSY)
  1907. desc->status = PAUSED;
  1908. }
  1909. spin_unlock_irqrestore(&pch->lock, flags);
  1910. pm_runtime_mark_last_busy(pl330->ddma.dev);
  1911. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1912. return 0;
  1913. }
  1914. static void pl330_free_chan_resources(struct dma_chan *chan)
  1915. {
  1916. struct dma_pl330_chan *pch = to_pchan(chan);
  1917. struct pl330_dmac *pl330 = pch->dmac;
  1918. unsigned long flags;
  1919. tasklet_kill(&pch->task);
  1920. pm_runtime_get_sync(pch->dmac->ddma.dev);
  1921. spin_lock_irqsave(&pl330->lock, flags);
  1922. pl330_release_channel(pch->thread);
  1923. pch->thread = NULL;
  1924. if (pch->cyclic)
  1925. list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
  1926. spin_unlock_irqrestore(&pl330->lock, flags);
  1927. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1928. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1929. pl330_unprep_slave_fifo(pch);
  1930. }
  1931. static int pl330_get_current_xferred_count(struct dma_pl330_chan *pch,
  1932. struct dma_pl330_desc *desc)
  1933. {
  1934. struct pl330_thread *thrd = pch->thread;
  1935. struct pl330_dmac *pl330 = pch->dmac;
  1936. void __iomem *regs = thrd->dmac->base;
  1937. u32 val, addr;
  1938. pm_runtime_get_sync(pl330->ddma.dev);
  1939. val = addr = 0;
  1940. if (desc->rqcfg.src_inc) {
  1941. val = readl(regs + SA(thrd->id));
  1942. addr = desc->px.src_addr;
  1943. } else {
  1944. val = readl(regs + DA(thrd->id));
  1945. addr = desc->px.dst_addr;
  1946. }
  1947. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1948. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1949. /* If DMAMOV hasn't finished yet, SAR/DAR can be zero */
  1950. if (!val)
  1951. return 0;
  1952. return val - addr;
  1953. }
  1954. static enum dma_status
  1955. pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1956. struct dma_tx_state *txstate)
  1957. {
  1958. enum dma_status ret;
  1959. unsigned long flags;
  1960. struct dma_pl330_desc *desc, *running = NULL, *last_enq = NULL;
  1961. struct dma_pl330_chan *pch = to_pchan(chan);
  1962. unsigned int transferred, residual = 0;
  1963. ret = dma_cookie_status(chan, cookie, txstate);
  1964. if (!txstate)
  1965. return ret;
  1966. if (ret == DMA_COMPLETE)
  1967. goto out;
  1968. spin_lock_irqsave(&pch->lock, flags);
  1969. spin_lock(&pch->thread->dmac->lock);
  1970. if (pch->thread->req_running != -1)
  1971. running = pch->thread->req[pch->thread->req_running].desc;
  1972. last_enq = pch->thread->req[pch->thread->lstenq].desc;
  1973. /* Check in pending list */
  1974. list_for_each_entry(desc, &pch->work_list, node) {
  1975. if (desc->status == DONE)
  1976. transferred = desc->bytes_requested;
  1977. else if (running && desc == running)
  1978. transferred =
  1979. pl330_get_current_xferred_count(pch, desc);
  1980. else if (desc->status == BUSY || desc->status == PAUSED)
  1981. /*
  1982. * Busy but not running means either just enqueued,
  1983. * or finished and not yet marked done
  1984. */
  1985. if (desc == last_enq)
  1986. transferred = 0;
  1987. else
  1988. transferred = desc->bytes_requested;
  1989. else
  1990. transferred = 0;
  1991. residual += desc->bytes_requested - transferred;
  1992. if (desc->txd.cookie == cookie) {
  1993. switch (desc->status) {
  1994. case DONE:
  1995. ret = DMA_COMPLETE;
  1996. break;
  1997. case PAUSED:
  1998. ret = DMA_PAUSED;
  1999. break;
  2000. case PREP:
  2001. case BUSY:
  2002. ret = DMA_IN_PROGRESS;
  2003. break;
  2004. default:
  2005. WARN_ON(1);
  2006. }
  2007. break;
  2008. }
  2009. if (desc->last)
  2010. residual = 0;
  2011. }
  2012. spin_unlock(&pch->thread->dmac->lock);
  2013. spin_unlock_irqrestore(&pch->lock, flags);
  2014. out:
  2015. dma_set_residue(txstate, residual);
  2016. return ret;
  2017. }
  2018. static void pl330_issue_pending(struct dma_chan *chan)
  2019. {
  2020. struct dma_pl330_chan *pch = to_pchan(chan);
  2021. unsigned long flags;
  2022. spin_lock_irqsave(&pch->lock, flags);
  2023. if (list_empty(&pch->work_list)) {
  2024. /*
  2025. * Warn on nothing pending. Empty submitted_list may
  2026. * break our pm_runtime usage counter as it is
  2027. * updated on work_list emptiness status.
  2028. */
  2029. WARN_ON(list_empty(&pch->submitted_list));
  2030. pch->active = true;
  2031. pm_runtime_get_sync(pch->dmac->ddma.dev);
  2032. }
  2033. list_splice_tail_init(&pch->submitted_list, &pch->work_list);
  2034. spin_unlock_irqrestore(&pch->lock, flags);
  2035. pl330_tasklet(&pch->task);
  2036. }
  2037. /*
  2038. * We returned the last one of the circular list of descriptor(s)
  2039. * from prep_xxx, so the argument to submit corresponds to the last
  2040. * descriptor of the list.
  2041. */
  2042. static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
  2043. {
  2044. struct dma_pl330_desc *desc, *last = to_desc(tx);
  2045. struct dma_pl330_chan *pch = to_pchan(tx->chan);
  2046. dma_cookie_t cookie;
  2047. unsigned long flags;
  2048. spin_lock_irqsave(&pch->lock, flags);
  2049. /* Assign cookies to all nodes */
  2050. while (!list_empty(&last->node)) {
  2051. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  2052. if (pch->cyclic) {
  2053. desc->txd.callback = last->txd.callback;
  2054. desc->txd.callback_param = last->txd.callback_param;
  2055. }
  2056. desc->last = false;
  2057. dma_cookie_assign(&desc->txd);
  2058. list_move_tail(&desc->node, &pch->submitted_list);
  2059. }
  2060. last->last = true;
  2061. cookie = dma_cookie_assign(&last->txd);
  2062. list_add_tail(&last->node, &pch->submitted_list);
  2063. spin_unlock_irqrestore(&pch->lock, flags);
  2064. return cookie;
  2065. }
  2066. static inline void _init_desc(struct dma_pl330_desc *desc)
  2067. {
  2068. desc->rqcfg.swap = SWAP_NO;
  2069. desc->rqcfg.scctl = CCTRL0;
  2070. desc->rqcfg.dcctl = CCTRL0;
  2071. desc->txd.tx_submit = pl330_tx_submit;
  2072. INIT_LIST_HEAD(&desc->node);
  2073. }
  2074. /* Returns the number of descriptors added to the DMAC pool */
  2075. static int add_desc(struct list_head *pool, spinlock_t *lock,
  2076. gfp_t flg, int count)
  2077. {
  2078. struct dma_pl330_desc *desc;
  2079. unsigned long flags;
  2080. int i;
  2081. desc = kcalloc(count, sizeof(*desc), flg);
  2082. if (!desc)
  2083. return 0;
  2084. spin_lock_irqsave(lock, flags);
  2085. for (i = 0; i < count; i++) {
  2086. _init_desc(&desc[i]);
  2087. list_add_tail(&desc[i].node, pool);
  2088. }
  2089. spin_unlock_irqrestore(lock, flags);
  2090. return count;
  2091. }
  2092. static struct dma_pl330_desc *pluck_desc(struct list_head *pool,
  2093. spinlock_t *lock)
  2094. {
  2095. struct dma_pl330_desc *desc = NULL;
  2096. unsigned long flags;
  2097. spin_lock_irqsave(lock, flags);
  2098. if (!list_empty(pool)) {
  2099. desc = list_entry(pool->next,
  2100. struct dma_pl330_desc, node);
  2101. list_del_init(&desc->node);
  2102. desc->status = PREP;
  2103. desc->txd.callback = NULL;
  2104. }
  2105. spin_unlock_irqrestore(lock, flags);
  2106. return desc;
  2107. }
  2108. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  2109. {
  2110. struct pl330_dmac *pl330 = pch->dmac;
  2111. u8 *peri_id = pch->chan.private;
  2112. struct dma_pl330_desc *desc;
  2113. /* Pluck one desc from the pool of DMAC */
  2114. desc = pluck_desc(&pl330->desc_pool, &pl330->pool_lock);
  2115. /* If the DMAC pool is empty, alloc new */
  2116. if (!desc) {
  2117. static DEFINE_SPINLOCK(lock);
  2118. LIST_HEAD(pool);
  2119. if (!add_desc(&pool, &lock, GFP_ATOMIC, 1))
  2120. return NULL;
  2121. desc = pluck_desc(&pool, &lock);
  2122. WARN_ON(!desc || !list_empty(&pool));
  2123. }
  2124. /* Initialize the descriptor */
  2125. desc->pchan = pch;
  2126. desc->txd.cookie = 0;
  2127. async_tx_ack(&desc->txd);
  2128. desc->peri = peri_id ? pch->chan.chan_id : 0;
  2129. desc->rqcfg.pcfg = &pch->dmac->pcfg;
  2130. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  2131. return desc;
  2132. }
  2133. static inline void fill_px(struct pl330_xfer *px,
  2134. dma_addr_t dst, dma_addr_t src, size_t len)
  2135. {
  2136. px->bytes = len;
  2137. px->dst_addr = dst;
  2138. px->src_addr = src;
  2139. }
  2140. static struct dma_pl330_desc *
  2141. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  2142. dma_addr_t src, size_t len)
  2143. {
  2144. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  2145. if (!desc) {
  2146. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  2147. __func__, __LINE__);
  2148. return NULL;
  2149. }
  2150. /*
  2151. * Ideally we should lookout for reqs bigger than
  2152. * those that can be programmed with 256 bytes of
  2153. * MC buffer, but considering a req size is seldom
  2154. * going to be word-unaligned and more than 200MB,
  2155. * we take it easy.
  2156. * Also, should the limit is reached we'd rather
  2157. * have the platform increase MC buffer size than
  2158. * complicating this API driver.
  2159. */
  2160. fill_px(&desc->px, dst, src, len);
  2161. return desc;
  2162. }
  2163. /* Call after fixing burst size */
  2164. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  2165. {
  2166. struct dma_pl330_chan *pch = desc->pchan;
  2167. struct pl330_dmac *pl330 = pch->dmac;
  2168. int burst_len;
  2169. burst_len = pl330->pcfg.data_bus_width / 8;
  2170. burst_len *= pl330->pcfg.data_buf_dep / pl330->pcfg.num_chan;
  2171. burst_len >>= desc->rqcfg.brst_size;
  2172. /* src/dst_burst_len can't be more than 16 */
  2173. if (burst_len > PL330_MAX_BURST)
  2174. burst_len = PL330_MAX_BURST;
  2175. return burst_len;
  2176. }
  2177. static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
  2178. struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
  2179. size_t period_len, enum dma_transfer_direction direction,
  2180. unsigned long flags)
  2181. {
  2182. struct dma_pl330_desc *desc = NULL, *first = NULL;
  2183. struct dma_pl330_chan *pch = to_pchan(chan);
  2184. struct pl330_dmac *pl330 = pch->dmac;
  2185. unsigned int i;
  2186. dma_addr_t dst;
  2187. dma_addr_t src;
  2188. if (len % period_len != 0)
  2189. return NULL;
  2190. if (!is_slave_direction(direction)) {
  2191. dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
  2192. __func__, __LINE__);
  2193. return NULL;
  2194. }
  2195. pl330_config_write(chan, &pch->slave_config, direction);
  2196. if (!pl330_prep_slave_fifo(pch, direction))
  2197. return NULL;
  2198. for (i = 0; i < len / period_len; i++) {
  2199. desc = pl330_get_desc(pch);
  2200. if (!desc) {
  2201. unsigned long iflags;
  2202. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  2203. __func__, __LINE__);
  2204. if (!first)
  2205. return NULL;
  2206. spin_lock_irqsave(&pl330->pool_lock, iflags);
  2207. while (!list_empty(&first->node)) {
  2208. desc = list_entry(first->node.next,
  2209. struct dma_pl330_desc, node);
  2210. list_move_tail(&desc->node, &pl330->desc_pool);
  2211. }
  2212. list_move_tail(&first->node, &pl330->desc_pool);
  2213. spin_unlock_irqrestore(&pl330->pool_lock, iflags);
  2214. return NULL;
  2215. }
  2216. switch (direction) {
  2217. case DMA_MEM_TO_DEV:
  2218. desc->rqcfg.src_inc = 1;
  2219. desc->rqcfg.dst_inc = 0;
  2220. src = dma_addr;
  2221. dst = pch->fifo_dma;
  2222. break;
  2223. case DMA_DEV_TO_MEM:
  2224. desc->rqcfg.src_inc = 0;
  2225. desc->rqcfg.dst_inc = 1;
  2226. src = pch->fifo_dma;
  2227. dst = dma_addr;
  2228. break;
  2229. default:
  2230. break;
  2231. }
  2232. desc->rqtype = direction;
  2233. desc->rqcfg.brst_size = pch->burst_sz;
  2234. desc->rqcfg.brst_len = pch->burst_len;
  2235. desc->bytes_requested = period_len;
  2236. fill_px(&desc->px, dst, src, period_len);
  2237. if (!first)
  2238. first = desc;
  2239. else
  2240. list_add_tail(&desc->node, &first->node);
  2241. dma_addr += period_len;
  2242. }
  2243. if (!desc)
  2244. return NULL;
  2245. pch->cyclic = true;
  2246. return &desc->txd;
  2247. }
  2248. static struct dma_async_tx_descriptor *
  2249. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  2250. dma_addr_t src, size_t len, unsigned long flags)
  2251. {
  2252. struct dma_pl330_desc *desc;
  2253. struct dma_pl330_chan *pch = to_pchan(chan);
  2254. struct pl330_dmac *pl330;
  2255. int burst;
  2256. if (unlikely(!pch || !len))
  2257. return NULL;
  2258. pl330 = pch->dmac;
  2259. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  2260. if (!desc)
  2261. return NULL;
  2262. desc->rqcfg.src_inc = 1;
  2263. desc->rqcfg.dst_inc = 1;
  2264. desc->rqtype = DMA_MEM_TO_MEM;
  2265. /* Select max possible burst size */
  2266. burst = pl330->pcfg.data_bus_width / 8;
  2267. /*
  2268. * Make sure we use a burst size that aligns with all the memcpy
  2269. * parameters because our DMA programming algorithm doesn't cope with
  2270. * transfers which straddle an entry in the DMA device's MFIFO.
  2271. */
  2272. while ((src | dst | len) & (burst - 1))
  2273. burst /= 2;
  2274. desc->rqcfg.brst_size = 0;
  2275. while (burst != (1 << desc->rqcfg.brst_size))
  2276. desc->rqcfg.brst_size++;
  2277. desc->rqcfg.brst_len = get_burst_len(desc, len);
  2278. /*
  2279. * If burst size is smaller than bus width then make sure we only
  2280. * transfer one at a time to avoid a burst stradling an MFIFO entry.
  2281. */
  2282. if (burst * 8 < pl330->pcfg.data_bus_width)
  2283. desc->rqcfg.brst_len = 1;
  2284. desc->bytes_requested = len;
  2285. return &desc->txd;
  2286. }
  2287. static void __pl330_giveback_desc(struct pl330_dmac *pl330,
  2288. struct dma_pl330_desc *first)
  2289. {
  2290. unsigned long flags;
  2291. struct dma_pl330_desc *desc;
  2292. if (!first)
  2293. return;
  2294. spin_lock_irqsave(&pl330->pool_lock, flags);
  2295. while (!list_empty(&first->node)) {
  2296. desc = list_entry(first->node.next,
  2297. struct dma_pl330_desc, node);
  2298. list_move_tail(&desc->node, &pl330->desc_pool);
  2299. }
  2300. list_move_tail(&first->node, &pl330->desc_pool);
  2301. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  2302. }
  2303. static struct dma_async_tx_descriptor *
  2304. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  2305. unsigned int sg_len, enum dma_transfer_direction direction,
  2306. unsigned long flg, void *context)
  2307. {
  2308. struct dma_pl330_desc *first, *desc = NULL;
  2309. struct dma_pl330_chan *pch = to_pchan(chan);
  2310. struct scatterlist *sg;
  2311. int i;
  2312. if (unlikely(!pch || !sgl || !sg_len))
  2313. return NULL;
  2314. pl330_config_write(chan, &pch->slave_config, direction);
  2315. if (!pl330_prep_slave_fifo(pch, direction))
  2316. return NULL;
  2317. first = NULL;
  2318. for_each_sg(sgl, sg, sg_len, i) {
  2319. desc = pl330_get_desc(pch);
  2320. if (!desc) {
  2321. struct pl330_dmac *pl330 = pch->dmac;
  2322. dev_err(pch->dmac->ddma.dev,
  2323. "%s:%d Unable to fetch desc\n",
  2324. __func__, __LINE__);
  2325. __pl330_giveback_desc(pl330, first);
  2326. return NULL;
  2327. }
  2328. if (!first)
  2329. first = desc;
  2330. else
  2331. list_add_tail(&desc->node, &first->node);
  2332. if (direction == DMA_MEM_TO_DEV) {
  2333. desc->rqcfg.src_inc = 1;
  2334. desc->rqcfg.dst_inc = 0;
  2335. fill_px(&desc->px, pch->fifo_dma, sg_dma_address(sg),
  2336. sg_dma_len(sg));
  2337. } else {
  2338. desc->rqcfg.src_inc = 0;
  2339. desc->rqcfg.dst_inc = 1;
  2340. fill_px(&desc->px, sg_dma_address(sg), pch->fifo_dma,
  2341. sg_dma_len(sg));
  2342. }
  2343. desc->rqcfg.brst_size = pch->burst_sz;
  2344. desc->rqcfg.brst_len = pch->burst_len;
  2345. desc->rqtype = direction;
  2346. desc->bytes_requested = sg_dma_len(sg);
  2347. }
  2348. /* Return the last desc in the chain */
  2349. return &desc->txd;
  2350. }
  2351. static irqreturn_t pl330_irq_handler(int irq, void *data)
  2352. {
  2353. if (pl330_update(data))
  2354. return IRQ_HANDLED;
  2355. else
  2356. return IRQ_NONE;
  2357. }
  2358. #define PL330_DMA_BUSWIDTHS \
  2359. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  2360. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  2361. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  2362. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  2363. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
  2364. #ifdef CONFIG_DEBUG_FS
  2365. static int pl330_debugfs_show(struct seq_file *s, void *data)
  2366. {
  2367. struct pl330_dmac *pl330 = s->private;
  2368. int chans, pchs, ch, pr;
  2369. chans = pl330->pcfg.num_chan;
  2370. pchs = pl330->num_peripherals;
  2371. seq_puts(s, "PL330 physical channels:\n");
  2372. seq_puts(s, "THREAD:\t\tCHANNEL:\n");
  2373. seq_puts(s, "--------\t-----\n");
  2374. for (ch = 0; ch < chans; ch++) {
  2375. struct pl330_thread *thrd = &pl330->channels[ch];
  2376. int found = -1;
  2377. for (pr = 0; pr < pchs; pr++) {
  2378. struct dma_pl330_chan *pch = &pl330->peripherals[pr];
  2379. if (!pch->thread || thrd->id != pch->thread->id)
  2380. continue;
  2381. found = pr;
  2382. }
  2383. seq_printf(s, "%d\t\t", thrd->id);
  2384. if (found == -1)
  2385. seq_puts(s, "--\n");
  2386. else
  2387. seq_printf(s, "%d\n", found);
  2388. }
  2389. return 0;
  2390. }
  2391. DEFINE_SHOW_ATTRIBUTE(pl330_debugfs);
  2392. static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
  2393. {
  2394. debugfs_create_file(dev_name(pl330->ddma.dev),
  2395. S_IFREG | 0444, NULL, pl330,
  2396. &pl330_debugfs_fops);
  2397. }
  2398. #else
  2399. static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
  2400. {
  2401. }
  2402. #endif
  2403. /*
  2404. * Runtime PM callbacks are provided by amba/bus.c driver.
  2405. *
  2406. * It is assumed here that IRQ safe runtime PM is chosen in probe and amba
  2407. * bus driver will only disable/enable the clock in runtime PM callbacks.
  2408. */
  2409. static int __maybe_unused pl330_suspend(struct device *dev)
  2410. {
  2411. struct amba_device *pcdev = to_amba_device(dev);
  2412. pm_runtime_force_suspend(dev);
  2413. clk_unprepare(pcdev->pclk);
  2414. return 0;
  2415. }
  2416. static int __maybe_unused pl330_resume(struct device *dev)
  2417. {
  2418. struct amba_device *pcdev = to_amba_device(dev);
  2419. int ret;
  2420. ret = clk_prepare(pcdev->pclk);
  2421. if (ret)
  2422. return ret;
  2423. pm_runtime_force_resume(dev);
  2424. return ret;
  2425. }
  2426. static const struct dev_pm_ops pl330_pm = {
  2427. SET_LATE_SYSTEM_SLEEP_PM_OPS(pl330_suspend, pl330_resume)
  2428. };
  2429. static int
  2430. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  2431. {
  2432. struct pl330_config *pcfg;
  2433. struct pl330_dmac *pl330;
  2434. struct dma_pl330_chan *pch, *_p;
  2435. struct dma_device *pd;
  2436. struct resource *res;
  2437. int i, ret, irq;
  2438. int num_chan;
  2439. struct device_node *np = adev->dev.of_node;
  2440. ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
  2441. if (ret)
  2442. return ret;
  2443. /* Allocate a new DMAC and its Channels */
  2444. pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
  2445. if (!pl330)
  2446. return -ENOMEM;
  2447. pd = &pl330->ddma;
  2448. pd->dev = &adev->dev;
  2449. pl330->mcbufsz = 0;
  2450. /* get quirk */
  2451. for (i = 0; i < ARRAY_SIZE(of_quirks); i++)
  2452. if (of_property_read_bool(np, of_quirks[i].quirk))
  2453. pl330->quirks |= of_quirks[i].id;
  2454. res = &adev->res;
  2455. pl330->base = devm_ioremap_resource(&adev->dev, res);
  2456. if (IS_ERR(pl330->base))
  2457. return PTR_ERR(pl330->base);
  2458. amba_set_drvdata(adev, pl330);
  2459. pl330->rstc = devm_reset_control_get_optional(&adev->dev, "dma");
  2460. if (IS_ERR(pl330->rstc)) {
  2461. return dev_err_probe(&adev->dev, PTR_ERR(pl330->rstc), "Failed to get reset!\n");
  2462. } else {
  2463. ret = reset_control_deassert(pl330->rstc);
  2464. if (ret) {
  2465. dev_err(&adev->dev, "Couldn't deassert the device from reset!\n");
  2466. return ret;
  2467. }
  2468. }
  2469. pl330->rstc_ocp = devm_reset_control_get_optional(&adev->dev, "dma-ocp");
  2470. if (IS_ERR(pl330->rstc_ocp)) {
  2471. return dev_err_probe(&adev->dev, PTR_ERR(pl330->rstc_ocp),
  2472. "Failed to get OCP reset!\n");
  2473. } else {
  2474. ret = reset_control_deassert(pl330->rstc_ocp);
  2475. if (ret) {
  2476. dev_err(&adev->dev, "Couldn't deassert the device from OCP reset!\n");
  2477. return ret;
  2478. }
  2479. }
  2480. for (i = 0; i < AMBA_NR_IRQS; i++) {
  2481. irq = adev->irq[i];
  2482. if (irq) {
  2483. ret = devm_request_irq(&adev->dev, irq,
  2484. pl330_irq_handler, 0,
  2485. dev_name(&adev->dev), pl330);
  2486. if (ret)
  2487. return ret;
  2488. } else {
  2489. break;
  2490. }
  2491. }
  2492. pcfg = &pl330->pcfg;
  2493. pcfg->periph_id = adev->periphid;
  2494. ret = pl330_add(pl330);
  2495. if (ret)
  2496. return ret;
  2497. INIT_LIST_HEAD(&pl330->desc_pool);
  2498. spin_lock_init(&pl330->pool_lock);
  2499. /* Create a descriptor pool of default size */
  2500. if (!add_desc(&pl330->desc_pool, &pl330->pool_lock,
  2501. GFP_KERNEL, NR_DEFAULT_DESC))
  2502. dev_warn(&adev->dev, "unable to allocate desc\n");
  2503. INIT_LIST_HEAD(&pd->channels);
  2504. /* Initialize channel parameters */
  2505. num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
  2506. pl330->num_peripherals = num_chan;
  2507. pl330->peripherals = kcalloc(num_chan, sizeof(*pch), GFP_KERNEL);
  2508. if (!pl330->peripherals) {
  2509. ret = -ENOMEM;
  2510. goto probe_err2;
  2511. }
  2512. for (i = 0; i < num_chan; i++) {
  2513. pch = &pl330->peripherals[i];
  2514. pch->chan.private = adev->dev.of_node;
  2515. INIT_LIST_HEAD(&pch->submitted_list);
  2516. INIT_LIST_HEAD(&pch->work_list);
  2517. INIT_LIST_HEAD(&pch->completed_list);
  2518. spin_lock_init(&pch->lock);
  2519. pch->thread = NULL;
  2520. pch->chan.device = pd;
  2521. pch->dmac = pl330;
  2522. pch->dir = DMA_NONE;
  2523. /* Add the channel to the DMAC list */
  2524. list_add_tail(&pch->chan.device_node, &pd->channels);
  2525. }
  2526. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  2527. if (pcfg->num_peri) {
  2528. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  2529. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  2530. dma_cap_set(DMA_PRIVATE, pd->cap_mask);
  2531. }
  2532. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  2533. pd->device_free_chan_resources = pl330_free_chan_resources;
  2534. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  2535. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  2536. pd->device_tx_status = pl330_tx_status;
  2537. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  2538. pd->device_config = pl330_config;
  2539. pd->device_pause = pl330_pause;
  2540. pd->device_terminate_all = pl330_terminate_all;
  2541. pd->device_issue_pending = pl330_issue_pending;
  2542. pd->src_addr_widths = PL330_DMA_BUSWIDTHS;
  2543. pd->dst_addr_widths = PL330_DMA_BUSWIDTHS;
  2544. pd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  2545. pd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  2546. pd->max_burst = PL330_MAX_BURST;
  2547. ret = dma_async_device_register(pd);
  2548. if (ret) {
  2549. dev_err(&adev->dev, "unable to register DMAC\n");
  2550. goto probe_err3;
  2551. }
  2552. if (adev->dev.of_node) {
  2553. ret = of_dma_controller_register(adev->dev.of_node,
  2554. of_dma_pl330_xlate, pl330);
  2555. if (ret) {
  2556. dev_err(&adev->dev,
  2557. "unable to register DMA to the generic DT DMA helpers\n");
  2558. }
  2559. }
  2560. /*
  2561. * This is the limit for transfers with a buswidth of 1, larger
  2562. * buswidths will have larger limits.
  2563. */
  2564. ret = dma_set_max_seg_size(&adev->dev, 1900800);
  2565. if (ret)
  2566. dev_err(&adev->dev, "unable to set the seg size\n");
  2567. init_pl330_debugfs(pl330);
  2568. dev_info(&adev->dev,
  2569. "Loaded driver for PL330 DMAC-%x\n", adev->periphid);
  2570. dev_info(&adev->dev,
  2571. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  2572. pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
  2573. pcfg->num_peri, pcfg->num_events);
  2574. pm_runtime_irq_safe(&adev->dev);
  2575. pm_runtime_use_autosuspend(&adev->dev);
  2576. pm_runtime_set_autosuspend_delay(&adev->dev, PL330_AUTOSUSPEND_DELAY);
  2577. pm_runtime_mark_last_busy(&adev->dev);
  2578. pm_runtime_put_autosuspend(&adev->dev);
  2579. return 0;
  2580. probe_err3:
  2581. /* Idle the DMAC */
  2582. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2583. chan.device_node) {
  2584. /* Remove the channel */
  2585. list_del(&pch->chan.device_node);
  2586. /* Flush the channel */
  2587. if (pch->thread) {
  2588. pl330_terminate_all(&pch->chan);
  2589. pl330_free_chan_resources(&pch->chan);
  2590. }
  2591. }
  2592. probe_err2:
  2593. pl330_del(pl330);
  2594. if (pl330->rstc_ocp)
  2595. reset_control_assert(pl330->rstc_ocp);
  2596. if (pl330->rstc)
  2597. reset_control_assert(pl330->rstc);
  2598. return ret;
  2599. }
  2600. static void pl330_remove(struct amba_device *adev)
  2601. {
  2602. struct pl330_dmac *pl330 = amba_get_drvdata(adev);
  2603. struct dma_pl330_chan *pch, *_p;
  2604. int i, irq;
  2605. pm_runtime_get_noresume(pl330->ddma.dev);
  2606. if (adev->dev.of_node)
  2607. of_dma_controller_free(adev->dev.of_node);
  2608. for (i = 0; i < AMBA_NR_IRQS; i++) {
  2609. irq = adev->irq[i];
  2610. if (irq)
  2611. devm_free_irq(&adev->dev, irq, pl330);
  2612. }
  2613. dma_async_device_unregister(&pl330->ddma);
  2614. /* Idle the DMAC */
  2615. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2616. chan.device_node) {
  2617. /* Remove the channel */
  2618. list_del(&pch->chan.device_node);
  2619. /* Flush the channel */
  2620. if (pch->thread) {
  2621. pl330_terminate_all(&pch->chan);
  2622. pl330_free_chan_resources(&pch->chan);
  2623. }
  2624. }
  2625. pl330_del(pl330);
  2626. if (pl330->rstc_ocp)
  2627. reset_control_assert(pl330->rstc_ocp);
  2628. if (pl330->rstc)
  2629. reset_control_assert(pl330->rstc);
  2630. }
  2631. static const struct amba_id pl330_ids[] = {
  2632. {
  2633. .id = 0x00041330,
  2634. .mask = 0x000fffff,
  2635. },
  2636. { 0, 0 },
  2637. };
  2638. MODULE_DEVICE_TABLE(amba, pl330_ids);
  2639. static struct amba_driver pl330_driver = {
  2640. .drv = {
  2641. .owner = THIS_MODULE,
  2642. .name = "dma-pl330",
  2643. .pm = &pl330_pm,
  2644. },
  2645. .id_table = pl330_ids,
  2646. .probe = pl330_probe,
  2647. .remove = pl330_remove,
  2648. };
  2649. module_amba_driver(pl330_driver);
  2650. MODULE_AUTHOR("Jaswinder Singh <[email protected]>");
  2651. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  2652. MODULE_LICENSE("GPL");