pl330.c 62 KB

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