amba-pl08x.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /*
  2. * Copyright (c) 2006 ARM Ltd.
  3. * Copyright (c) 2010 ST-Ericsson SA
  4. *
  5. * Author: Peter Pearse <peter.pearse@arm.com>
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * The full GNU General Public License is in this distribution in the file
  19. * called COPYING.
  20. *
  21. * Documentation: ARM DDI 0196G == PL080
  22. * Documentation: ARM DDI 0218E == PL081
  23. * Documentation: S3C6410 User's Manual == PL080S
  24. *
  25. * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
  26. * channel.
  27. *
  28. * The PL080 has 8 channels available for simultaneous use, and the PL081
  29. * has only two channels. So on these DMA controllers the number of channels
  30. * and the number of incoming DMA signals are two totally different things.
  31. * It is usually not possible to theoretically handle all physical signals,
  32. * so a multiplexing scheme with possible denial of use is necessary.
  33. *
  34. * The PL080 has a dual bus master, PL081 has a single master.
  35. *
  36. * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
  37. * It differs in following aspects:
  38. * - CH_CONFIG register at different offset,
  39. * - separate CH_CONTROL2 register for transfer size,
  40. * - bigger maximum transfer size,
  41. * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
  42. * - no support for peripheral flow control.
  43. *
  44. * Memory to peripheral transfer may be visualized as
  45. * Get data from memory to DMAC
  46. * Until no data left
  47. * On burst request from peripheral
  48. * Destination burst from DMAC to peripheral
  49. * Clear burst request
  50. * Raise terminal count interrupt
  51. *
  52. * For peripherals with a FIFO:
  53. * Source burst size == half the depth of the peripheral FIFO
  54. * Destination burst size == the depth of the peripheral FIFO
  55. *
  56. * (Bursts are irrelevant for mem to mem transfers - there are no burst
  57. * signals, the DMA controller will simply facilitate its AHB master.)
  58. *
  59. * ASSUMES default (little) endianness for DMA transfers
  60. *
  61. * The PL08x has two flow control settings:
  62. * - DMAC flow control: the transfer size defines the number of transfers
  63. * which occur for the current LLI entry, and the DMAC raises TC at the
  64. * end of every LLI entry. Observed behaviour shows the DMAC listening
  65. * to both the BREQ and SREQ signals (contrary to documented),
  66. * transferring data if either is active. The LBREQ and LSREQ signals
  67. * are ignored.
  68. *
  69. * - Peripheral flow control: the transfer size is ignored (and should be
  70. * zero). The data is transferred from the current LLI entry, until
  71. * after the final transfer signalled by LBREQ or LSREQ. The DMAC
  72. * will then move to the next LLI entry. Unsupported by PL080S.
  73. */
  74. #include <linux/amba/bus.h>
  75. #include <linux/amba/pl08x.h>
  76. #include <linux/debugfs.h>
  77. #include <linux/delay.h>
  78. #include <linux/device.h>
  79. #include <linux/dmaengine.h>
  80. #include <linux/dmapool.h>
  81. #include <linux/dma-mapping.h>
  82. #include <linux/export.h>
  83. #include <linux/init.h>
  84. #include <linux/interrupt.h>
  85. #include <linux/module.h>
  86. #include <linux/of.h>
  87. #include <linux/of_dma.h>
  88. #include <linux/pm_runtime.h>
  89. #include <linux/seq_file.h>
  90. #include <linux/slab.h>
  91. #include <linux/amba/pl080.h>
  92. #include "dmaengine.h"
  93. #include "virt-dma.h"
  94. #define DRIVER_NAME "pl08xdmac"
  95. #define PL80X_DMA_BUSWIDTHS \
  96. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  97. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  98. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  99. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  100. static struct amba_driver pl08x_amba_driver;
  101. struct pl08x_driver_data;
  102. /**
  103. * struct vendor_data - vendor-specific config parameters for PL08x derivatives
  104. * @channels: the number of channels available in this variant
  105. * @signals: the number of request signals available from the hardware
  106. * @dualmaster: whether this version supports dual AHB masters or not.
  107. * @nomadik: whether the channels have Nomadik security extension bits
  108. * that need to be checked for permission before use and some registers are
  109. * missing
  110. * @pl080s: whether this version is a PL080S, which has separate register and
  111. * LLI word for transfer size.
  112. * @max_transfer_size: the maximum single element transfer size for this
  113. * PL08x variant.
  114. */
  115. struct vendor_data {
  116. u8 config_offset;
  117. u8 channels;
  118. u8 signals;
  119. bool dualmaster;
  120. bool nomadik;
  121. bool pl080s;
  122. u32 max_transfer_size;
  123. };
  124. /**
  125. * struct pl08x_bus_data - information of source or destination
  126. * busses for a transfer
  127. * @addr: current address
  128. * @maxwidth: the maximum width of a transfer on this bus
  129. * @buswidth: the width of this bus in bytes: 1, 2 or 4
  130. */
  131. struct pl08x_bus_data {
  132. dma_addr_t addr;
  133. u8 maxwidth;
  134. u8 buswidth;
  135. };
  136. #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
  137. /**
  138. * struct pl08x_phy_chan - holder for the physical channels
  139. * @id: physical index to this channel
  140. * @lock: a lock to use when altering an instance of this struct
  141. * @serving: the virtual channel currently being served by this physical
  142. * channel
  143. * @locked: channel unavailable for the system, e.g. dedicated to secure
  144. * world
  145. */
  146. struct pl08x_phy_chan {
  147. unsigned int id;
  148. void __iomem *base;
  149. void __iomem *reg_config;
  150. spinlock_t lock;
  151. struct pl08x_dma_chan *serving;
  152. bool locked;
  153. };
  154. /**
  155. * struct pl08x_sg - structure containing data per sg
  156. * @src_addr: src address of sg
  157. * @dst_addr: dst address of sg
  158. * @len: transfer len in bytes
  159. * @node: node for txd's dsg_list
  160. */
  161. struct pl08x_sg {
  162. dma_addr_t src_addr;
  163. dma_addr_t dst_addr;
  164. size_t len;
  165. struct list_head node;
  166. };
  167. /**
  168. * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
  169. * @vd: virtual DMA descriptor
  170. * @dsg_list: list of children sg's
  171. * @llis_bus: DMA memory address (physical) start for the LLIs
  172. * @llis_va: virtual memory address start for the LLIs
  173. * @cctl: control reg values for current txd
  174. * @ccfg: config reg values for current txd
  175. * @done: this marks completed descriptors, which should not have their
  176. * mux released.
  177. * @cyclic: indicate cyclic transfers
  178. */
  179. struct pl08x_txd {
  180. struct virt_dma_desc vd;
  181. struct list_head dsg_list;
  182. dma_addr_t llis_bus;
  183. u32 *llis_va;
  184. /* Default cctl value for LLIs */
  185. u32 cctl;
  186. /*
  187. * Settings to be put into the physical channel when we
  188. * trigger this txd. Other registers are in llis_va[0].
  189. */
  190. u32 ccfg;
  191. bool done;
  192. bool cyclic;
  193. };
  194. /**
  195. * struct pl08x_dma_chan_state - holds the PL08x specific virtual channel
  196. * states
  197. * @PL08X_CHAN_IDLE: the channel is idle
  198. * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
  199. * channel and is running a transfer on it
  200. * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
  201. * channel, but the transfer is currently paused
  202. * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
  203. * channel to become available (only pertains to memcpy channels)
  204. */
  205. enum pl08x_dma_chan_state {
  206. PL08X_CHAN_IDLE,
  207. PL08X_CHAN_RUNNING,
  208. PL08X_CHAN_PAUSED,
  209. PL08X_CHAN_WAITING,
  210. };
  211. /**
  212. * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
  213. * @vc: wrappped virtual channel
  214. * @phychan: the physical channel utilized by this channel, if there is one
  215. * @name: name of channel
  216. * @cd: channel platform data
  217. * @runtime_addr: address for RX/TX according to the runtime config
  218. * @at: active transaction on this channel
  219. * @lock: a lock for this channel data
  220. * @host: a pointer to the host (internal use)
  221. * @state: whether the channel is idle, paused, running etc
  222. * @slave: whether this channel is a device (slave) or for memcpy
  223. * @signal: the physical DMA request signal which this channel is using
  224. * @mux_use: count of descriptors using this DMA request signal setting
  225. */
  226. struct pl08x_dma_chan {
  227. struct virt_dma_chan vc;
  228. struct pl08x_phy_chan *phychan;
  229. const char *name;
  230. struct pl08x_channel_data *cd;
  231. struct dma_slave_config cfg;
  232. struct pl08x_txd *at;
  233. struct pl08x_driver_data *host;
  234. enum pl08x_dma_chan_state state;
  235. bool slave;
  236. int signal;
  237. unsigned mux_use;
  238. };
  239. /**
  240. * struct pl08x_driver_data - the local state holder for the PL08x
  241. * @slave: slave engine for this instance
  242. * @memcpy: memcpy engine for this instance
  243. * @base: virtual memory base (remapped) for the PL08x
  244. * @adev: the corresponding AMBA (PrimeCell) bus entry
  245. * @vd: vendor data for this PL08x variant
  246. * @pd: platform data passed in from the platform/machine
  247. * @phy_chans: array of data for the physical channels
  248. * @pool: a pool for the LLI descriptors
  249. * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
  250. * fetches
  251. * @mem_buses: set to indicate memory transfers on AHB2.
  252. * @lock: a spinlock for this struct
  253. */
  254. struct pl08x_driver_data {
  255. struct dma_device slave;
  256. struct dma_device memcpy;
  257. void __iomem *base;
  258. struct amba_device *adev;
  259. const struct vendor_data *vd;
  260. struct pl08x_platform_data *pd;
  261. struct pl08x_phy_chan *phy_chans;
  262. struct dma_pool *pool;
  263. u8 lli_buses;
  264. u8 mem_buses;
  265. u8 lli_words;
  266. };
  267. /*
  268. * PL08X specific defines
  269. */
  270. /* The order of words in an LLI. */
  271. #define PL080_LLI_SRC 0
  272. #define PL080_LLI_DST 1
  273. #define PL080_LLI_LLI 2
  274. #define PL080_LLI_CCTL 3
  275. #define PL080S_LLI_CCTL2 4
  276. /* Total words in an LLI. */
  277. #define PL080_LLI_WORDS 4
  278. #define PL080S_LLI_WORDS 8
  279. /*
  280. * Number of LLIs in each LLI buffer allocated for one transfer
  281. * (maximum times we call dma_pool_alloc on this pool without freeing)
  282. */
  283. #define MAX_NUM_TSFR_LLIS 512
  284. #define PL08X_ALIGN 8
  285. static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
  286. {
  287. return container_of(chan, struct pl08x_dma_chan, vc.chan);
  288. }
  289. static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
  290. {
  291. return container_of(tx, struct pl08x_txd, vd.tx);
  292. }
  293. /*
  294. * Mux handling.
  295. *
  296. * This gives us the DMA request input to the PL08x primecell which the
  297. * peripheral described by the channel data will be routed to, possibly
  298. * via a board/SoC specific external MUX. One important point to note
  299. * here is that this does not depend on the physical channel.
  300. */
  301. static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
  302. {
  303. const struct pl08x_platform_data *pd = plchan->host->pd;
  304. int ret;
  305. if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
  306. ret = pd->get_xfer_signal(plchan->cd);
  307. if (ret < 0) {
  308. plchan->mux_use = 0;
  309. return ret;
  310. }
  311. plchan->signal = ret;
  312. }
  313. return 0;
  314. }
  315. static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
  316. {
  317. const struct pl08x_platform_data *pd = plchan->host->pd;
  318. if (plchan->signal >= 0) {
  319. WARN_ON(plchan->mux_use == 0);
  320. if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
  321. pd->put_xfer_signal(plchan->cd, plchan->signal);
  322. plchan->signal = -1;
  323. }
  324. }
  325. }
  326. /*
  327. * Physical channel handling
  328. */
  329. /* Whether a certain channel is busy or not */
  330. static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
  331. {
  332. unsigned int val;
  333. val = readl(ch->reg_config);
  334. return val & PL080_CONFIG_ACTIVE;
  335. }
  336. static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
  337. struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
  338. {
  339. if (pl08x->vd->pl080s)
  340. dev_vdbg(&pl08x->adev->dev,
  341. "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
  342. "clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
  343. phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
  344. lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
  345. lli[PL080S_LLI_CCTL2], ccfg);
  346. else
  347. dev_vdbg(&pl08x->adev->dev,
  348. "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
  349. "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
  350. phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
  351. lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
  352. writel_relaxed(lli[PL080_LLI_SRC], phychan->base + PL080_CH_SRC_ADDR);
  353. writel_relaxed(lli[PL080_LLI_DST], phychan->base + PL080_CH_DST_ADDR);
  354. writel_relaxed(lli[PL080_LLI_LLI], phychan->base + PL080_CH_LLI);
  355. writel_relaxed(lli[PL080_LLI_CCTL], phychan->base + PL080_CH_CONTROL);
  356. if (pl08x->vd->pl080s)
  357. writel_relaxed(lli[PL080S_LLI_CCTL2],
  358. phychan->base + PL080S_CH_CONTROL2);
  359. writel(ccfg, phychan->reg_config);
  360. }
  361. /*
  362. * Set the initial DMA register values i.e. those for the first LLI
  363. * The next LLI pointer and the configuration interrupt bit have
  364. * been set when the LLIs were constructed. Poke them into the hardware
  365. * and start the transfer.
  366. */
  367. static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
  368. {
  369. struct pl08x_driver_data *pl08x = plchan->host;
  370. struct pl08x_phy_chan *phychan = plchan->phychan;
  371. struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
  372. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  373. u32 val;
  374. list_del(&txd->vd.node);
  375. plchan->at = txd;
  376. /* Wait for channel inactive */
  377. while (pl08x_phy_channel_busy(phychan))
  378. cpu_relax();
  379. pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
  380. /* Enable the DMA channel */
  381. /* Do not access config register until channel shows as disabled */
  382. while (readl(pl08x->base + PL080_EN_CHAN) & (1 << phychan->id))
  383. cpu_relax();
  384. /* Do not access config register until channel shows as inactive */
  385. val = readl(phychan->reg_config);
  386. while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
  387. val = readl(phychan->reg_config);
  388. writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
  389. }
  390. /*
  391. * Pause the channel by setting the HALT bit.
  392. *
  393. * For M->P transfers, pause the DMAC first and then stop the peripheral -
  394. * the FIFO can only drain if the peripheral is still requesting data.
  395. * (note: this can still timeout if the DMAC FIFO never drains of data.)
  396. *
  397. * For P->M transfers, disable the peripheral first to stop it filling
  398. * the DMAC FIFO, and then pause the DMAC.
  399. */
  400. static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
  401. {
  402. u32 val;
  403. int timeout;
  404. /* Set the HALT bit and wait for the FIFO to drain */
  405. val = readl(ch->reg_config);
  406. val |= PL080_CONFIG_HALT;
  407. writel(val, ch->reg_config);
  408. /* Wait for channel inactive */
  409. for (timeout = 1000; timeout; timeout--) {
  410. if (!pl08x_phy_channel_busy(ch))
  411. break;
  412. udelay(1);
  413. }
  414. if (pl08x_phy_channel_busy(ch))
  415. pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
  416. }
  417. static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
  418. {
  419. u32 val;
  420. /* Clear the HALT bit */
  421. val = readl(ch->reg_config);
  422. val &= ~PL080_CONFIG_HALT;
  423. writel(val, ch->reg_config);
  424. }
  425. /*
  426. * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
  427. * clears any pending interrupt status. This should not be used for
  428. * an on-going transfer, but as a method of shutting down a channel
  429. * (eg, when it's no longer used) or terminating a transfer.
  430. */
  431. static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
  432. struct pl08x_phy_chan *ch)
  433. {
  434. u32 val = readl(ch->reg_config);
  435. val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
  436. PL080_CONFIG_TC_IRQ_MASK);
  437. writel(val, ch->reg_config);
  438. writel(1 << ch->id, pl08x->base + PL080_ERR_CLEAR);
  439. writel(1 << ch->id, pl08x->base + PL080_TC_CLEAR);
  440. }
  441. static inline u32 get_bytes_in_cctl(u32 cctl)
  442. {
  443. /* The source width defines the number of bytes */
  444. u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
  445. cctl &= PL080_CONTROL_SWIDTH_MASK;
  446. switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
  447. case PL080_WIDTH_8BIT:
  448. break;
  449. case PL080_WIDTH_16BIT:
  450. bytes *= 2;
  451. break;
  452. case PL080_WIDTH_32BIT:
  453. bytes *= 4;
  454. break;
  455. }
  456. return bytes;
  457. }
  458. static inline u32 get_bytes_in_cctl_pl080s(u32 cctl, u32 cctl1)
  459. {
  460. /* The source width defines the number of bytes */
  461. u32 bytes = cctl1 & PL080S_CONTROL_TRANSFER_SIZE_MASK;
  462. cctl &= PL080_CONTROL_SWIDTH_MASK;
  463. switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
  464. case PL080_WIDTH_8BIT:
  465. break;
  466. case PL080_WIDTH_16BIT:
  467. bytes *= 2;
  468. break;
  469. case PL080_WIDTH_32BIT:
  470. bytes *= 4;
  471. break;
  472. }
  473. return bytes;
  474. }
  475. /* The channel should be paused when calling this */
  476. static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
  477. {
  478. struct pl08x_driver_data *pl08x = plchan->host;
  479. const u32 *llis_va, *llis_va_limit;
  480. struct pl08x_phy_chan *ch;
  481. dma_addr_t llis_bus;
  482. struct pl08x_txd *txd;
  483. u32 llis_max_words;
  484. size_t bytes;
  485. u32 clli;
  486. ch = plchan->phychan;
  487. txd = plchan->at;
  488. if (!ch || !txd)
  489. return 0;
  490. /*
  491. * Follow the LLIs to get the number of remaining
  492. * bytes in the currently active transaction.
  493. */
  494. clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
  495. /* First get the remaining bytes in the active transfer */
  496. if (pl08x->vd->pl080s)
  497. bytes = get_bytes_in_cctl_pl080s(
  498. readl(ch->base + PL080_CH_CONTROL),
  499. readl(ch->base + PL080S_CH_CONTROL2));
  500. else
  501. bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
  502. if (!clli)
  503. return bytes;
  504. llis_va = txd->llis_va;
  505. llis_bus = txd->llis_bus;
  506. llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
  507. BUG_ON(clli < llis_bus || clli >= llis_bus +
  508. sizeof(u32) * llis_max_words);
  509. /*
  510. * Locate the next LLI - as this is an array,
  511. * it's simple maths to find.
  512. */
  513. llis_va += (clli - llis_bus) / sizeof(u32);
  514. llis_va_limit = llis_va + llis_max_words;
  515. for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
  516. if (pl08x->vd->pl080s)
  517. bytes += get_bytes_in_cctl_pl080s(
  518. llis_va[PL080_LLI_CCTL],
  519. llis_va[PL080S_LLI_CCTL2]);
  520. else
  521. bytes += get_bytes_in_cctl(llis_va[PL080_LLI_CCTL]);
  522. /*
  523. * A LLI pointer going backward terminates the LLI list
  524. */
  525. if (llis_va[PL080_LLI_LLI] <= clli)
  526. break;
  527. }
  528. return bytes;
  529. }
  530. /*
  531. * Allocate a physical channel for a virtual channel
  532. *
  533. * Try to locate a physical channel to be used for this transfer. If all
  534. * are taken return NULL and the requester will have to cope by using
  535. * some fallback PIO mode or retrying later.
  536. */
  537. static struct pl08x_phy_chan *
  538. pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
  539. struct pl08x_dma_chan *virt_chan)
  540. {
  541. struct pl08x_phy_chan *ch = NULL;
  542. unsigned long flags;
  543. int i;
  544. for (i = 0; i < pl08x->vd->channels; i++) {
  545. ch = &pl08x->phy_chans[i];
  546. spin_lock_irqsave(&ch->lock, flags);
  547. if (!ch->locked && !ch->serving) {
  548. ch->serving = virt_chan;
  549. spin_unlock_irqrestore(&ch->lock, flags);
  550. break;
  551. }
  552. spin_unlock_irqrestore(&ch->lock, flags);
  553. }
  554. if (i == pl08x->vd->channels) {
  555. /* No physical channel available, cope with it */
  556. return NULL;
  557. }
  558. return ch;
  559. }
  560. /* Mark the physical channel as free. Note, this write is atomic. */
  561. static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
  562. struct pl08x_phy_chan *ch)
  563. {
  564. ch->serving = NULL;
  565. }
  566. /*
  567. * Try to allocate a physical channel. When successful, assign it to
  568. * this virtual channel, and initiate the next descriptor. The
  569. * virtual channel lock must be held at this point.
  570. */
  571. static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
  572. {
  573. struct pl08x_driver_data *pl08x = plchan->host;
  574. struct pl08x_phy_chan *ch;
  575. ch = pl08x_get_phy_channel(pl08x, plchan);
  576. if (!ch) {
  577. dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
  578. plchan->state = PL08X_CHAN_WAITING;
  579. return;
  580. }
  581. dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
  582. ch->id, plchan->name);
  583. plchan->phychan = ch;
  584. plchan->state = PL08X_CHAN_RUNNING;
  585. pl08x_start_next_txd(plchan);
  586. }
  587. static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
  588. struct pl08x_dma_chan *plchan)
  589. {
  590. struct pl08x_driver_data *pl08x = plchan->host;
  591. dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
  592. ch->id, plchan->name);
  593. /*
  594. * We do this without taking the lock; we're really only concerned
  595. * about whether this pointer is NULL or not, and we're guaranteed
  596. * that this will only be called when it _already_ is non-NULL.
  597. */
  598. ch->serving = plchan;
  599. plchan->phychan = ch;
  600. plchan->state = PL08X_CHAN_RUNNING;
  601. pl08x_start_next_txd(plchan);
  602. }
  603. /*
  604. * Free a physical DMA channel, potentially reallocating it to another
  605. * virtual channel if we have any pending.
  606. */
  607. static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
  608. {
  609. struct pl08x_driver_data *pl08x = plchan->host;
  610. struct pl08x_dma_chan *p, *next;
  611. retry:
  612. next = NULL;
  613. /* Find a waiting virtual channel for the next transfer. */
  614. list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
  615. if (p->state == PL08X_CHAN_WAITING) {
  616. next = p;
  617. break;
  618. }
  619. if (!next) {
  620. list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
  621. if (p->state == PL08X_CHAN_WAITING) {
  622. next = p;
  623. break;
  624. }
  625. }
  626. /* Ensure that the physical channel is stopped */
  627. pl08x_terminate_phy_chan(pl08x, plchan->phychan);
  628. if (next) {
  629. bool success;
  630. /*
  631. * Eww. We know this isn't going to deadlock
  632. * but lockdep probably doesn't.
  633. */
  634. spin_lock(&next->vc.lock);
  635. /* Re-check the state now that we have the lock */
  636. success = next->state == PL08X_CHAN_WAITING;
  637. if (success)
  638. pl08x_phy_reassign_start(plchan->phychan, next);
  639. spin_unlock(&next->vc.lock);
  640. /* If the state changed, try to find another channel */
  641. if (!success)
  642. goto retry;
  643. } else {
  644. /* No more jobs, so free up the physical channel */
  645. pl08x_put_phy_channel(pl08x, plchan->phychan);
  646. }
  647. plchan->phychan = NULL;
  648. plchan->state = PL08X_CHAN_IDLE;
  649. }
  650. /*
  651. * LLI handling
  652. */
  653. static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
  654. {
  655. switch (coded) {
  656. case PL080_WIDTH_8BIT:
  657. return 1;
  658. case PL080_WIDTH_16BIT:
  659. return 2;
  660. case PL080_WIDTH_32BIT:
  661. return 4;
  662. default:
  663. break;
  664. }
  665. BUG();
  666. return 0;
  667. }
  668. static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
  669. size_t tsize)
  670. {
  671. u32 retbits = cctl;
  672. /* Remove all src, dst and transfer size bits */
  673. retbits &= ~PL080_CONTROL_DWIDTH_MASK;
  674. retbits &= ~PL080_CONTROL_SWIDTH_MASK;
  675. retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
  676. /* Then set the bits according to the parameters */
  677. switch (srcwidth) {
  678. case 1:
  679. retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
  680. break;
  681. case 2:
  682. retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
  683. break;
  684. case 4:
  685. retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
  686. break;
  687. default:
  688. BUG();
  689. break;
  690. }
  691. switch (dstwidth) {
  692. case 1:
  693. retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
  694. break;
  695. case 2:
  696. retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
  697. break;
  698. case 4:
  699. retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
  700. break;
  701. default:
  702. BUG();
  703. break;
  704. }
  705. tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
  706. retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
  707. return retbits;
  708. }
  709. struct pl08x_lli_build_data {
  710. struct pl08x_txd *txd;
  711. struct pl08x_bus_data srcbus;
  712. struct pl08x_bus_data dstbus;
  713. size_t remainder;
  714. u32 lli_bus;
  715. };
  716. /*
  717. * Autoselect a master bus to use for the transfer. Slave will be the chosen as
  718. * victim in case src & dest are not similarly aligned. i.e. If after aligning
  719. * masters address with width requirements of transfer (by sending few byte by
  720. * byte data), slave is still not aligned, then its width will be reduced to
  721. * BYTE.
  722. * - prefers the destination bus if both available
  723. * - prefers bus with fixed address (i.e. peripheral)
  724. */
  725. static void pl08x_choose_master_bus(struct pl08x_lli_build_data *bd,
  726. struct pl08x_bus_data **mbus, struct pl08x_bus_data **sbus, u32 cctl)
  727. {
  728. if (!(cctl & PL080_CONTROL_DST_INCR)) {
  729. *mbus = &bd->dstbus;
  730. *sbus = &bd->srcbus;
  731. } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
  732. *mbus = &bd->srcbus;
  733. *sbus = &bd->dstbus;
  734. } else {
  735. if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
  736. *mbus = &bd->dstbus;
  737. *sbus = &bd->srcbus;
  738. } else {
  739. *mbus = &bd->srcbus;
  740. *sbus = &bd->dstbus;
  741. }
  742. }
  743. }
  744. /*
  745. * Fills in one LLI for a certain transfer descriptor and advance the counter
  746. */
  747. static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
  748. struct pl08x_lli_build_data *bd,
  749. int num_llis, int len, u32 cctl, u32 cctl2)
  750. {
  751. u32 offset = num_llis * pl08x->lli_words;
  752. u32 *llis_va = bd->txd->llis_va + offset;
  753. dma_addr_t llis_bus = bd->txd->llis_bus;
  754. BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
  755. /* Advance the offset to next LLI. */
  756. offset += pl08x->lli_words;
  757. llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
  758. llis_va[PL080_LLI_DST] = bd->dstbus.addr;
  759. llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
  760. llis_va[PL080_LLI_LLI] |= bd->lli_bus;
  761. llis_va[PL080_LLI_CCTL] = cctl;
  762. if (pl08x->vd->pl080s)
  763. llis_va[PL080S_LLI_CCTL2] = cctl2;
  764. if (cctl & PL080_CONTROL_SRC_INCR)
  765. bd->srcbus.addr += len;
  766. if (cctl & PL080_CONTROL_DST_INCR)
  767. bd->dstbus.addr += len;
  768. BUG_ON(bd->remainder < len);
  769. bd->remainder -= len;
  770. }
  771. static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
  772. struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
  773. int num_llis, size_t *total_bytes)
  774. {
  775. *cctl = pl08x_cctl_bits(*cctl, 1, 1, len);
  776. pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
  777. (*total_bytes) += len;
  778. }
  779. #ifdef VERBOSE_DEBUG
  780. static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
  781. const u32 *llis_va, int num_llis)
  782. {
  783. int i;
  784. if (pl08x->vd->pl080s) {
  785. dev_vdbg(&pl08x->adev->dev,
  786. "%-3s %-9s %-10s %-10s %-10s %-10s %s\n",
  787. "lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
  788. for (i = 0; i < num_llis; i++) {
  789. dev_vdbg(&pl08x->adev->dev,
  790. "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  791. i, llis_va, llis_va[PL080_LLI_SRC],
  792. llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
  793. llis_va[PL080_LLI_CCTL],
  794. llis_va[PL080S_LLI_CCTL2]);
  795. llis_va += pl08x->lli_words;
  796. }
  797. } else {
  798. dev_vdbg(&pl08x->adev->dev,
  799. "%-3s %-9s %-10s %-10s %-10s %s\n",
  800. "lli", "", "csrc", "cdst", "clli", "cctl");
  801. for (i = 0; i < num_llis; i++) {
  802. dev_vdbg(&pl08x->adev->dev,
  803. "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  804. i, llis_va, llis_va[PL080_LLI_SRC],
  805. llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
  806. llis_va[PL080_LLI_CCTL]);
  807. llis_va += pl08x->lli_words;
  808. }
  809. }
  810. }
  811. #else
  812. static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
  813. const u32 *llis_va, int num_llis) {}
  814. #endif
  815. /*
  816. * This fills in the table of LLIs for the transfer descriptor
  817. * Note that we assume we never have to change the burst sizes
  818. * Return 0 for error
  819. */
  820. static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
  821. struct pl08x_txd *txd)
  822. {
  823. struct pl08x_bus_data *mbus, *sbus;
  824. struct pl08x_lli_build_data bd;
  825. int num_llis = 0;
  826. u32 cctl, early_bytes = 0;
  827. size_t max_bytes_per_lli, total_bytes;
  828. u32 *llis_va, *last_lli;
  829. struct pl08x_sg *dsg;
  830. txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
  831. if (!txd->llis_va) {
  832. dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
  833. return 0;
  834. }
  835. bd.txd = txd;
  836. bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
  837. cctl = txd->cctl;
  838. /* Find maximum width of the source bus */
  839. bd.srcbus.maxwidth =
  840. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
  841. PL080_CONTROL_SWIDTH_SHIFT);
  842. /* Find maximum width of the destination bus */
  843. bd.dstbus.maxwidth =
  844. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
  845. PL080_CONTROL_DWIDTH_SHIFT);
  846. list_for_each_entry(dsg, &txd->dsg_list, node) {
  847. total_bytes = 0;
  848. cctl = txd->cctl;
  849. bd.srcbus.addr = dsg->src_addr;
  850. bd.dstbus.addr = dsg->dst_addr;
  851. bd.remainder = dsg->len;
  852. bd.srcbus.buswidth = bd.srcbus.maxwidth;
  853. bd.dstbus.buswidth = bd.dstbus.maxwidth;
  854. pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl);
  855. dev_vdbg(&pl08x->adev->dev,
  856. "src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
  857. (u64)bd.srcbus.addr,
  858. cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
  859. bd.srcbus.buswidth,
  860. (u64)bd.dstbus.addr,
  861. cctl & PL080_CONTROL_DST_INCR ? "+" : "",
  862. bd.dstbus.buswidth,
  863. bd.remainder);
  864. dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
  865. mbus == &bd.srcbus ? "src" : "dst",
  866. sbus == &bd.srcbus ? "src" : "dst");
  867. /*
  868. * Zero length is only allowed if all these requirements are
  869. * met:
  870. * - flow controller is peripheral.
  871. * - src.addr is aligned to src.width
  872. * - dst.addr is aligned to dst.width
  873. *
  874. * sg_len == 1 should be true, as there can be two cases here:
  875. *
  876. * - Memory addresses are contiguous and are not scattered.
  877. * Here, Only one sg will be passed by user driver, with
  878. * memory address and zero length. We pass this to controller
  879. * and after the transfer it will receive the last burst
  880. * request from peripheral and so transfer finishes.
  881. *
  882. * - Memory addresses are scattered and are not contiguous.
  883. * Here, Obviously as DMA controller doesn't know when a lli's
  884. * transfer gets over, it can't load next lli. So in this
  885. * case, there has to be an assumption that only one lli is
  886. * supported. Thus, we can't have scattered addresses.
  887. */
  888. if (!bd.remainder) {
  889. u32 fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
  890. PL080_CONFIG_FLOW_CONTROL_SHIFT;
  891. if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
  892. (fc <= PL080_FLOW_SRC2DST_SRC))) {
  893. dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
  894. __func__);
  895. return 0;
  896. }
  897. if (!IS_BUS_ALIGNED(&bd.srcbus) ||
  898. !IS_BUS_ALIGNED(&bd.dstbus)) {
  899. dev_err(&pl08x->adev->dev,
  900. "%s src & dst address must be aligned to src"
  901. " & dst width if peripheral is flow controller",
  902. __func__);
  903. return 0;
  904. }
  905. cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
  906. bd.dstbus.buswidth, 0);
  907. pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
  908. 0, cctl, 0);
  909. break;
  910. }
  911. /*
  912. * Send byte by byte for following cases
  913. * - Less than a bus width available
  914. * - until master bus is aligned
  915. */
  916. if (bd.remainder < mbus->buswidth)
  917. early_bytes = bd.remainder;
  918. else if (!IS_BUS_ALIGNED(mbus)) {
  919. early_bytes = mbus->buswidth -
  920. (mbus->addr & (mbus->buswidth - 1));
  921. if ((bd.remainder - early_bytes) < mbus->buswidth)
  922. early_bytes = bd.remainder;
  923. }
  924. if (early_bytes) {
  925. dev_vdbg(&pl08x->adev->dev,
  926. "%s byte width LLIs (remain 0x%08zx)\n",
  927. __func__, bd.remainder);
  928. prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
  929. num_llis++, &total_bytes);
  930. }
  931. if (bd.remainder) {
  932. /*
  933. * Master now aligned
  934. * - if slave is not then we must set its width down
  935. */
  936. if (!IS_BUS_ALIGNED(sbus)) {
  937. dev_dbg(&pl08x->adev->dev,
  938. "%s set down bus width to one byte\n",
  939. __func__);
  940. sbus->buswidth = 1;
  941. }
  942. /*
  943. * Bytes transferred = tsize * src width, not
  944. * MIN(buswidths)
  945. */
  946. max_bytes_per_lli = bd.srcbus.buswidth *
  947. pl08x->vd->max_transfer_size;
  948. dev_vdbg(&pl08x->adev->dev,
  949. "%s max bytes per lli = %zu\n",
  950. __func__, max_bytes_per_lli);
  951. /*
  952. * Make largest possible LLIs until less than one bus
  953. * width left
  954. */
  955. while (bd.remainder > (mbus->buswidth - 1)) {
  956. size_t lli_len, tsize, width;
  957. /*
  958. * If enough left try to send max possible,
  959. * otherwise try to send the remainder
  960. */
  961. lli_len = min(bd.remainder, max_bytes_per_lli);
  962. /*
  963. * Check against maximum bus alignment:
  964. * Calculate actual transfer size in relation to
  965. * bus width an get a maximum remainder of the
  966. * highest bus width - 1
  967. */
  968. width = max(mbus->buswidth, sbus->buswidth);
  969. lli_len = (lli_len / width) * width;
  970. tsize = lli_len / bd.srcbus.buswidth;
  971. dev_vdbg(&pl08x->adev->dev,
  972. "%s fill lli with single lli chunk of "
  973. "size 0x%08zx (remainder 0x%08zx)\n",
  974. __func__, lli_len, bd.remainder);
  975. cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
  976. bd.dstbus.buswidth, tsize);
  977. pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
  978. lli_len, cctl, tsize);
  979. total_bytes += lli_len;
  980. }
  981. /*
  982. * Send any odd bytes
  983. */
  984. if (bd.remainder) {
  985. dev_vdbg(&pl08x->adev->dev,
  986. "%s align with boundary, send odd bytes (remain %zu)\n",
  987. __func__, bd.remainder);
  988. prep_byte_width_lli(pl08x, &bd, &cctl,
  989. bd.remainder, num_llis++, &total_bytes);
  990. }
  991. }
  992. if (total_bytes != dsg->len) {
  993. dev_err(&pl08x->adev->dev,
  994. "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
  995. __func__, total_bytes, dsg->len);
  996. return 0;
  997. }
  998. if (num_llis >= MAX_NUM_TSFR_LLIS) {
  999. dev_err(&pl08x->adev->dev,
  1000. "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
  1001. __func__, MAX_NUM_TSFR_LLIS);
  1002. return 0;
  1003. }
  1004. }
  1005. llis_va = txd->llis_va;
  1006. last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
  1007. if (txd->cyclic) {
  1008. /* Link back to the first LLI. */
  1009. last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
  1010. } else {
  1011. /* The final LLI terminates the LLI. */
  1012. last_lli[PL080_LLI_LLI] = 0;
  1013. /* The final LLI element shall also fire an interrupt. */
  1014. last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
  1015. }
  1016. pl08x_dump_lli(pl08x, llis_va, num_llis);
  1017. return num_llis;
  1018. }
  1019. static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
  1020. struct pl08x_txd *txd)
  1021. {
  1022. struct pl08x_sg *dsg, *_dsg;
  1023. if (txd->llis_va)
  1024. dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
  1025. list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
  1026. list_del(&dsg->node);
  1027. kfree(dsg);
  1028. }
  1029. kfree(txd);
  1030. }
  1031. static void pl08x_desc_free(struct virt_dma_desc *vd)
  1032. {
  1033. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  1034. struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
  1035. dma_descriptor_unmap(&vd->tx);
  1036. if (!txd->done)
  1037. pl08x_release_mux(plchan);
  1038. pl08x_free_txd(plchan->host, txd);
  1039. }
  1040. static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
  1041. struct pl08x_dma_chan *plchan)
  1042. {
  1043. LIST_HEAD(head);
  1044. vchan_get_all_descriptors(&plchan->vc, &head);
  1045. vchan_dma_desc_free_list(&plchan->vc, &head);
  1046. }
  1047. /*
  1048. * The DMA ENGINE API
  1049. */
  1050. static void pl08x_free_chan_resources(struct dma_chan *chan)
  1051. {
  1052. /* Ensure all queued descriptors are freed */
  1053. vchan_free_chan_resources(to_virt_chan(chan));
  1054. }
  1055. static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
  1056. struct dma_chan *chan, unsigned long flags)
  1057. {
  1058. struct dma_async_tx_descriptor *retval = NULL;
  1059. return retval;
  1060. }
  1061. /*
  1062. * Code accessing dma_async_is_complete() in a tight loop may give problems.
  1063. * If slaves are relying on interrupts to signal completion this function
  1064. * must not be called with interrupts disabled.
  1065. */
  1066. static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
  1067. dma_cookie_t cookie, struct dma_tx_state *txstate)
  1068. {
  1069. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1070. struct virt_dma_desc *vd;
  1071. unsigned long flags;
  1072. enum dma_status ret;
  1073. size_t bytes = 0;
  1074. ret = dma_cookie_status(chan, cookie, txstate);
  1075. if (ret == DMA_COMPLETE)
  1076. return ret;
  1077. /*
  1078. * There's no point calculating the residue if there's
  1079. * no txstate to store the value.
  1080. */
  1081. if (!txstate) {
  1082. if (plchan->state == PL08X_CHAN_PAUSED)
  1083. ret = DMA_PAUSED;
  1084. return ret;
  1085. }
  1086. spin_lock_irqsave(&plchan->vc.lock, flags);
  1087. ret = dma_cookie_status(chan, cookie, txstate);
  1088. if (ret != DMA_COMPLETE) {
  1089. vd = vchan_find_desc(&plchan->vc, cookie);
  1090. if (vd) {
  1091. /* On the issued list, so hasn't been processed yet */
  1092. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  1093. struct pl08x_sg *dsg;
  1094. list_for_each_entry(dsg, &txd->dsg_list, node)
  1095. bytes += dsg->len;
  1096. } else {
  1097. bytes = pl08x_getbytes_chan(plchan);
  1098. }
  1099. }
  1100. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1101. /*
  1102. * This cookie not complete yet
  1103. * Get number of bytes left in the active transactions and queue
  1104. */
  1105. dma_set_residue(txstate, bytes);
  1106. if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
  1107. ret = DMA_PAUSED;
  1108. /* Whether waiting or running, we're in progress */
  1109. return ret;
  1110. }
  1111. /* PrimeCell DMA extension */
  1112. struct burst_table {
  1113. u32 burstwords;
  1114. u32 reg;
  1115. };
  1116. static const struct burst_table burst_sizes[] = {
  1117. {
  1118. .burstwords = 256,
  1119. .reg = PL080_BSIZE_256,
  1120. },
  1121. {
  1122. .burstwords = 128,
  1123. .reg = PL080_BSIZE_128,
  1124. },
  1125. {
  1126. .burstwords = 64,
  1127. .reg = PL080_BSIZE_64,
  1128. },
  1129. {
  1130. .burstwords = 32,
  1131. .reg = PL080_BSIZE_32,
  1132. },
  1133. {
  1134. .burstwords = 16,
  1135. .reg = PL080_BSIZE_16,
  1136. },
  1137. {
  1138. .burstwords = 8,
  1139. .reg = PL080_BSIZE_8,
  1140. },
  1141. {
  1142. .burstwords = 4,
  1143. .reg = PL080_BSIZE_4,
  1144. },
  1145. {
  1146. .burstwords = 0,
  1147. .reg = PL080_BSIZE_1,
  1148. },
  1149. };
  1150. /*
  1151. * Given the source and destination available bus masks, select which
  1152. * will be routed to each port. We try to have source and destination
  1153. * on separate ports, but always respect the allowable settings.
  1154. */
  1155. static u32 pl08x_select_bus(u8 src, u8 dst)
  1156. {
  1157. u32 cctl = 0;
  1158. if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
  1159. cctl |= PL080_CONTROL_DST_AHB2;
  1160. if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
  1161. cctl |= PL080_CONTROL_SRC_AHB2;
  1162. return cctl;
  1163. }
  1164. static u32 pl08x_cctl(u32 cctl)
  1165. {
  1166. cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
  1167. PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
  1168. PL080_CONTROL_PROT_MASK);
  1169. /* Access the cell in privileged mode, non-bufferable, non-cacheable */
  1170. return cctl | PL080_CONTROL_PROT_SYS;
  1171. }
  1172. static u32 pl08x_width(enum dma_slave_buswidth width)
  1173. {
  1174. switch (width) {
  1175. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1176. return PL080_WIDTH_8BIT;
  1177. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1178. return PL080_WIDTH_16BIT;
  1179. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1180. return PL080_WIDTH_32BIT;
  1181. default:
  1182. return ~0;
  1183. }
  1184. }
  1185. static u32 pl08x_burst(u32 maxburst)
  1186. {
  1187. int i;
  1188. for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
  1189. if (burst_sizes[i].burstwords <= maxburst)
  1190. break;
  1191. return burst_sizes[i].reg;
  1192. }
  1193. static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
  1194. enum dma_slave_buswidth addr_width, u32 maxburst)
  1195. {
  1196. u32 width, burst, cctl = 0;
  1197. width = pl08x_width(addr_width);
  1198. if (width == ~0)
  1199. return ~0;
  1200. cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
  1201. cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
  1202. /*
  1203. * If this channel will only request single transfers, set this
  1204. * down to ONE element. Also select one element if no maxburst
  1205. * is specified.
  1206. */
  1207. if (plchan->cd->single)
  1208. maxburst = 1;
  1209. burst = pl08x_burst(maxburst);
  1210. cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
  1211. cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
  1212. return pl08x_cctl(cctl);
  1213. }
  1214. /*
  1215. * Slave transactions callback to the slave device to allow
  1216. * synchronization of slave DMA signals with the DMAC enable
  1217. */
  1218. static void pl08x_issue_pending(struct dma_chan *chan)
  1219. {
  1220. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1221. unsigned long flags;
  1222. spin_lock_irqsave(&plchan->vc.lock, flags);
  1223. if (vchan_issue_pending(&plchan->vc)) {
  1224. if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
  1225. pl08x_phy_alloc_and_start(plchan);
  1226. }
  1227. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1228. }
  1229. static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
  1230. {
  1231. struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  1232. if (txd) {
  1233. INIT_LIST_HEAD(&txd->dsg_list);
  1234. /* Always enable error and terminal interrupts */
  1235. txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
  1236. PL080_CONFIG_TC_IRQ_MASK;
  1237. }
  1238. return txd;
  1239. }
  1240. /*
  1241. * Initialize a descriptor to be used by memcpy submit
  1242. */
  1243. static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
  1244. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  1245. size_t len, unsigned long flags)
  1246. {
  1247. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1248. struct pl08x_driver_data *pl08x = plchan->host;
  1249. struct pl08x_txd *txd;
  1250. struct pl08x_sg *dsg;
  1251. int ret;
  1252. txd = pl08x_get_txd(plchan);
  1253. if (!txd) {
  1254. dev_err(&pl08x->adev->dev,
  1255. "%s no memory for descriptor\n", __func__);
  1256. return NULL;
  1257. }
  1258. dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
  1259. if (!dsg) {
  1260. pl08x_free_txd(pl08x, txd);
  1261. return NULL;
  1262. }
  1263. list_add_tail(&dsg->node, &txd->dsg_list);
  1264. dsg->src_addr = src;
  1265. dsg->dst_addr = dest;
  1266. dsg->len = len;
  1267. /* Set platform data for m2m */
  1268. txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1269. txd->cctl = pl08x->pd->memcpy_channel.cctl_memcpy &
  1270. ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
  1271. /* Both to be incremented or the code will break */
  1272. txd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
  1273. if (pl08x->vd->dualmaster)
  1274. txd->cctl |= pl08x_select_bus(pl08x->mem_buses,
  1275. pl08x->mem_buses);
  1276. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1277. if (!ret) {
  1278. pl08x_free_txd(pl08x, txd);
  1279. return NULL;
  1280. }
  1281. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1282. }
  1283. static struct pl08x_txd *pl08x_init_txd(
  1284. struct dma_chan *chan,
  1285. enum dma_transfer_direction direction,
  1286. dma_addr_t *slave_addr)
  1287. {
  1288. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1289. struct pl08x_driver_data *pl08x = plchan->host;
  1290. struct pl08x_txd *txd;
  1291. enum dma_slave_buswidth addr_width;
  1292. int ret, tmp;
  1293. u8 src_buses, dst_buses;
  1294. u32 maxburst, cctl;
  1295. txd = pl08x_get_txd(plchan);
  1296. if (!txd) {
  1297. dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
  1298. return NULL;
  1299. }
  1300. /*
  1301. * Set up addresses, the PrimeCell configured address
  1302. * will take precedence since this may configure the
  1303. * channel target address dynamically at runtime.
  1304. */
  1305. if (direction == DMA_MEM_TO_DEV) {
  1306. cctl = PL080_CONTROL_SRC_INCR;
  1307. *slave_addr = plchan->cfg.dst_addr;
  1308. addr_width = plchan->cfg.dst_addr_width;
  1309. maxburst = plchan->cfg.dst_maxburst;
  1310. src_buses = pl08x->mem_buses;
  1311. dst_buses = plchan->cd->periph_buses;
  1312. } else if (direction == DMA_DEV_TO_MEM) {
  1313. cctl = PL080_CONTROL_DST_INCR;
  1314. *slave_addr = plchan->cfg.src_addr;
  1315. addr_width = plchan->cfg.src_addr_width;
  1316. maxburst = plchan->cfg.src_maxburst;
  1317. src_buses = plchan->cd->periph_buses;
  1318. dst_buses = pl08x->mem_buses;
  1319. } else {
  1320. pl08x_free_txd(pl08x, txd);
  1321. dev_err(&pl08x->adev->dev,
  1322. "%s direction unsupported\n", __func__);
  1323. return NULL;
  1324. }
  1325. cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
  1326. if (cctl == ~0) {
  1327. pl08x_free_txd(pl08x, txd);
  1328. dev_err(&pl08x->adev->dev,
  1329. "DMA slave configuration botched?\n");
  1330. return NULL;
  1331. }
  1332. txd->cctl = cctl | pl08x_select_bus(src_buses, dst_buses);
  1333. if (plchan->cfg.device_fc)
  1334. tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
  1335. PL080_FLOW_PER2MEM_PER;
  1336. else
  1337. tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
  1338. PL080_FLOW_PER2MEM;
  1339. txd->ccfg |= tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1340. ret = pl08x_request_mux(plchan);
  1341. if (ret < 0) {
  1342. pl08x_free_txd(pl08x, txd);
  1343. dev_dbg(&pl08x->adev->dev,
  1344. "unable to mux for transfer on %s due to platform restrictions\n",
  1345. plchan->name);
  1346. return NULL;
  1347. }
  1348. dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
  1349. plchan->signal, plchan->name);
  1350. /* Assign the flow control signal to this channel */
  1351. if (direction == DMA_MEM_TO_DEV)
  1352. txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
  1353. else
  1354. txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
  1355. return txd;
  1356. }
  1357. static int pl08x_tx_add_sg(struct pl08x_txd *txd,
  1358. enum dma_transfer_direction direction,
  1359. dma_addr_t slave_addr,
  1360. dma_addr_t buf_addr,
  1361. unsigned int len)
  1362. {
  1363. struct pl08x_sg *dsg;
  1364. dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
  1365. if (!dsg)
  1366. return -ENOMEM;
  1367. list_add_tail(&dsg->node, &txd->dsg_list);
  1368. dsg->len = len;
  1369. if (direction == DMA_MEM_TO_DEV) {
  1370. dsg->src_addr = buf_addr;
  1371. dsg->dst_addr = slave_addr;
  1372. } else {
  1373. dsg->src_addr = slave_addr;
  1374. dsg->dst_addr = buf_addr;
  1375. }
  1376. return 0;
  1377. }
  1378. static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
  1379. struct dma_chan *chan, struct scatterlist *sgl,
  1380. unsigned int sg_len, enum dma_transfer_direction direction,
  1381. unsigned long flags, void *context)
  1382. {
  1383. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1384. struct pl08x_driver_data *pl08x = plchan->host;
  1385. struct pl08x_txd *txd;
  1386. struct scatterlist *sg;
  1387. int ret, tmp;
  1388. dma_addr_t slave_addr;
  1389. dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
  1390. __func__, sg_dma_len(sgl), plchan->name);
  1391. txd = pl08x_init_txd(chan, direction, &slave_addr);
  1392. if (!txd)
  1393. return NULL;
  1394. for_each_sg(sgl, sg, sg_len, tmp) {
  1395. ret = pl08x_tx_add_sg(txd, direction, slave_addr,
  1396. sg_dma_address(sg),
  1397. sg_dma_len(sg));
  1398. if (ret) {
  1399. pl08x_release_mux(plchan);
  1400. pl08x_free_txd(pl08x, txd);
  1401. dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
  1402. __func__);
  1403. return NULL;
  1404. }
  1405. }
  1406. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1407. if (!ret) {
  1408. pl08x_release_mux(plchan);
  1409. pl08x_free_txd(pl08x, txd);
  1410. return NULL;
  1411. }
  1412. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1413. }
  1414. static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
  1415. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1416. size_t period_len, enum dma_transfer_direction direction,
  1417. unsigned long flags)
  1418. {
  1419. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1420. struct pl08x_driver_data *pl08x = plchan->host;
  1421. struct pl08x_txd *txd;
  1422. int ret, tmp;
  1423. dma_addr_t slave_addr;
  1424. dev_dbg(&pl08x->adev->dev,
  1425. "%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
  1426. __func__, period_len, buf_len,
  1427. direction == DMA_MEM_TO_DEV ? "to" : "from",
  1428. plchan->name);
  1429. txd = pl08x_init_txd(chan, direction, &slave_addr);
  1430. if (!txd)
  1431. return NULL;
  1432. txd->cyclic = true;
  1433. txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
  1434. for (tmp = 0; tmp < buf_len; tmp += period_len) {
  1435. ret = pl08x_tx_add_sg(txd, direction, slave_addr,
  1436. buf_addr + tmp, period_len);
  1437. if (ret) {
  1438. pl08x_release_mux(plchan);
  1439. pl08x_free_txd(pl08x, txd);
  1440. return NULL;
  1441. }
  1442. }
  1443. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1444. if (!ret) {
  1445. pl08x_release_mux(plchan);
  1446. pl08x_free_txd(pl08x, txd);
  1447. return NULL;
  1448. }
  1449. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1450. }
  1451. static int pl08x_config(struct dma_chan *chan,
  1452. struct dma_slave_config *config)
  1453. {
  1454. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1455. struct pl08x_driver_data *pl08x = plchan->host;
  1456. if (!plchan->slave)
  1457. return -EINVAL;
  1458. /* Reject definitely invalid configurations */
  1459. if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  1460. config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  1461. return -EINVAL;
  1462. if (config->device_fc && pl08x->vd->pl080s) {
  1463. dev_err(&pl08x->adev->dev,
  1464. "%s: PL080S does not support peripheral flow control\n",
  1465. __func__);
  1466. return -EINVAL;
  1467. }
  1468. plchan->cfg = *config;
  1469. return 0;
  1470. }
  1471. static int pl08x_terminate_all(struct dma_chan *chan)
  1472. {
  1473. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1474. struct pl08x_driver_data *pl08x = plchan->host;
  1475. unsigned long flags;
  1476. spin_lock_irqsave(&plchan->vc.lock, flags);
  1477. if (!plchan->phychan && !plchan->at) {
  1478. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1479. return 0;
  1480. }
  1481. plchan->state = PL08X_CHAN_IDLE;
  1482. if (plchan->phychan) {
  1483. /*
  1484. * Mark physical channel as free and free any slave
  1485. * signal
  1486. */
  1487. pl08x_phy_free(plchan);
  1488. }
  1489. /* Dequeue jobs and free LLIs */
  1490. if (plchan->at) {
  1491. pl08x_desc_free(&plchan->at->vd);
  1492. plchan->at = NULL;
  1493. }
  1494. /* Dequeue jobs not yet fired as well */
  1495. pl08x_free_txd_list(pl08x, plchan);
  1496. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1497. return 0;
  1498. }
  1499. static int pl08x_pause(struct dma_chan *chan)
  1500. {
  1501. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1502. unsigned long flags;
  1503. /*
  1504. * Anything succeeds on channels with no physical allocation and
  1505. * no queued transfers.
  1506. */
  1507. spin_lock_irqsave(&plchan->vc.lock, flags);
  1508. if (!plchan->phychan && !plchan->at) {
  1509. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1510. return 0;
  1511. }
  1512. pl08x_pause_phy_chan(plchan->phychan);
  1513. plchan->state = PL08X_CHAN_PAUSED;
  1514. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1515. return 0;
  1516. }
  1517. static int pl08x_resume(struct dma_chan *chan)
  1518. {
  1519. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1520. unsigned long flags;
  1521. /*
  1522. * Anything succeeds on channels with no physical allocation and
  1523. * no queued transfers.
  1524. */
  1525. spin_lock_irqsave(&plchan->vc.lock, flags);
  1526. if (!plchan->phychan && !plchan->at) {
  1527. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1528. return 0;
  1529. }
  1530. pl08x_resume_phy_chan(plchan->phychan);
  1531. plchan->state = PL08X_CHAN_RUNNING;
  1532. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1533. return 0;
  1534. }
  1535. bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
  1536. {
  1537. struct pl08x_dma_chan *plchan;
  1538. char *name = chan_id;
  1539. /* Reject channels for devices not bound to this driver */
  1540. if (chan->device->dev->driver != &pl08x_amba_driver.drv)
  1541. return false;
  1542. plchan = to_pl08x_chan(chan);
  1543. /* Check that the channel is not taken! */
  1544. if (!strcmp(plchan->name, name))
  1545. return true;
  1546. return false;
  1547. }
  1548. EXPORT_SYMBOL_GPL(pl08x_filter_id);
  1549. static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
  1550. {
  1551. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1552. return plchan->cd == chan_id;
  1553. }
  1554. /*
  1555. * Just check that the device is there and active
  1556. * TODO: turn this bit on/off depending on the number of physical channels
  1557. * actually used, if it is zero... well shut it off. That will save some
  1558. * power. Cut the clock at the same time.
  1559. */
  1560. static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
  1561. {
  1562. /* The Nomadik variant does not have the config register */
  1563. if (pl08x->vd->nomadik)
  1564. return;
  1565. writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
  1566. }
  1567. static irqreturn_t pl08x_irq(int irq, void *dev)
  1568. {
  1569. struct pl08x_driver_data *pl08x = dev;
  1570. u32 mask = 0, err, tc, i;
  1571. /* check & clear - ERR & TC interrupts */
  1572. err = readl(pl08x->base + PL080_ERR_STATUS);
  1573. if (err) {
  1574. dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
  1575. __func__, err);
  1576. writel(err, pl08x->base + PL080_ERR_CLEAR);
  1577. }
  1578. tc = readl(pl08x->base + PL080_TC_STATUS);
  1579. if (tc)
  1580. writel(tc, pl08x->base + PL080_TC_CLEAR);
  1581. if (!err && !tc)
  1582. return IRQ_NONE;
  1583. for (i = 0; i < pl08x->vd->channels; i++) {
  1584. if (((1 << i) & err) || ((1 << i) & tc)) {
  1585. /* Locate physical channel */
  1586. struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
  1587. struct pl08x_dma_chan *plchan = phychan->serving;
  1588. struct pl08x_txd *tx;
  1589. if (!plchan) {
  1590. dev_err(&pl08x->adev->dev,
  1591. "%s Error TC interrupt on unused channel: 0x%08x\n",
  1592. __func__, i);
  1593. continue;
  1594. }
  1595. spin_lock(&plchan->vc.lock);
  1596. tx = plchan->at;
  1597. if (tx && tx->cyclic) {
  1598. vchan_cyclic_callback(&tx->vd);
  1599. } else if (tx) {
  1600. plchan->at = NULL;
  1601. /*
  1602. * This descriptor is done, release its mux
  1603. * reservation.
  1604. */
  1605. pl08x_release_mux(plchan);
  1606. tx->done = true;
  1607. vchan_cookie_complete(&tx->vd);
  1608. /*
  1609. * And start the next descriptor (if any),
  1610. * otherwise free this channel.
  1611. */
  1612. if (vchan_next_desc(&plchan->vc))
  1613. pl08x_start_next_txd(plchan);
  1614. else
  1615. pl08x_phy_free(plchan);
  1616. }
  1617. spin_unlock(&plchan->vc.lock);
  1618. mask |= (1 << i);
  1619. }
  1620. }
  1621. return mask ? IRQ_HANDLED : IRQ_NONE;
  1622. }
  1623. static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
  1624. {
  1625. chan->slave = true;
  1626. chan->name = chan->cd->bus_id;
  1627. chan->cfg.src_addr = chan->cd->addr;
  1628. chan->cfg.dst_addr = chan->cd->addr;
  1629. }
  1630. /*
  1631. * Initialise the DMAC memcpy/slave channels.
  1632. * Make a local wrapper to hold required data
  1633. */
  1634. static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
  1635. struct dma_device *dmadev, unsigned int channels, bool slave)
  1636. {
  1637. struct pl08x_dma_chan *chan;
  1638. int i;
  1639. INIT_LIST_HEAD(&dmadev->channels);
  1640. /*
  1641. * Register as many many memcpy as we have physical channels,
  1642. * we won't always be able to use all but the code will have
  1643. * to cope with that situation.
  1644. */
  1645. for (i = 0; i < channels; i++) {
  1646. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  1647. if (!chan)
  1648. return -ENOMEM;
  1649. chan->host = pl08x;
  1650. chan->state = PL08X_CHAN_IDLE;
  1651. chan->signal = -1;
  1652. if (slave) {
  1653. chan->cd = &pl08x->pd->slave_channels[i];
  1654. /*
  1655. * Some implementations have muxed signals, whereas some
  1656. * use a mux in front of the signals and need dynamic
  1657. * assignment of signals.
  1658. */
  1659. chan->signal = i;
  1660. pl08x_dma_slave_init(chan);
  1661. } else {
  1662. chan->cd = &pl08x->pd->memcpy_channel;
  1663. chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
  1664. if (!chan->name) {
  1665. kfree(chan);
  1666. return -ENOMEM;
  1667. }
  1668. }
  1669. dev_dbg(&pl08x->adev->dev,
  1670. "initialize virtual channel \"%s\"\n",
  1671. chan->name);
  1672. chan->vc.desc_free = pl08x_desc_free;
  1673. vchan_init(&chan->vc, dmadev);
  1674. }
  1675. dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
  1676. i, slave ? "slave" : "memcpy");
  1677. return i;
  1678. }
  1679. static void pl08x_free_virtual_channels(struct dma_device *dmadev)
  1680. {
  1681. struct pl08x_dma_chan *chan = NULL;
  1682. struct pl08x_dma_chan *next;
  1683. list_for_each_entry_safe(chan,
  1684. next, &dmadev->channels, vc.chan.device_node) {
  1685. list_del(&chan->vc.chan.device_node);
  1686. kfree(chan);
  1687. }
  1688. }
  1689. #ifdef CONFIG_DEBUG_FS
  1690. static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
  1691. {
  1692. switch (state) {
  1693. case PL08X_CHAN_IDLE:
  1694. return "idle";
  1695. case PL08X_CHAN_RUNNING:
  1696. return "running";
  1697. case PL08X_CHAN_PAUSED:
  1698. return "paused";
  1699. case PL08X_CHAN_WAITING:
  1700. return "waiting";
  1701. default:
  1702. break;
  1703. }
  1704. return "UNKNOWN STATE";
  1705. }
  1706. static int pl08x_debugfs_show(struct seq_file *s, void *data)
  1707. {
  1708. struct pl08x_driver_data *pl08x = s->private;
  1709. struct pl08x_dma_chan *chan;
  1710. struct pl08x_phy_chan *ch;
  1711. unsigned long flags;
  1712. int i;
  1713. seq_printf(s, "PL08x physical channels:\n");
  1714. seq_printf(s, "CHANNEL:\tUSER:\n");
  1715. seq_printf(s, "--------\t-----\n");
  1716. for (i = 0; i < pl08x->vd->channels; i++) {
  1717. struct pl08x_dma_chan *virt_chan;
  1718. ch = &pl08x->phy_chans[i];
  1719. spin_lock_irqsave(&ch->lock, flags);
  1720. virt_chan = ch->serving;
  1721. seq_printf(s, "%d\t\t%s%s\n",
  1722. ch->id,
  1723. virt_chan ? virt_chan->name : "(none)",
  1724. ch->locked ? " LOCKED" : "");
  1725. spin_unlock_irqrestore(&ch->lock, flags);
  1726. }
  1727. seq_printf(s, "\nPL08x virtual memcpy channels:\n");
  1728. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1729. seq_printf(s, "--------\t------\n");
  1730. list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
  1731. seq_printf(s, "%s\t\t%s\n", chan->name,
  1732. pl08x_state_str(chan->state));
  1733. }
  1734. seq_printf(s, "\nPL08x virtual slave channels:\n");
  1735. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1736. seq_printf(s, "--------\t------\n");
  1737. list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
  1738. seq_printf(s, "%s\t\t%s\n", chan->name,
  1739. pl08x_state_str(chan->state));
  1740. }
  1741. return 0;
  1742. }
  1743. static int pl08x_debugfs_open(struct inode *inode, struct file *file)
  1744. {
  1745. return single_open(file, pl08x_debugfs_show, inode->i_private);
  1746. }
  1747. static const struct file_operations pl08x_debugfs_operations = {
  1748. .open = pl08x_debugfs_open,
  1749. .read = seq_read,
  1750. .llseek = seq_lseek,
  1751. .release = single_release,
  1752. };
  1753. static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1754. {
  1755. /* Expose a simple debugfs interface to view all clocks */
  1756. (void) debugfs_create_file(dev_name(&pl08x->adev->dev),
  1757. S_IFREG | S_IRUGO, NULL, pl08x,
  1758. &pl08x_debugfs_operations);
  1759. }
  1760. #else
  1761. static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1762. {
  1763. }
  1764. #endif
  1765. #ifdef CONFIG_OF
  1766. static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
  1767. u32 id)
  1768. {
  1769. struct pl08x_dma_chan *chan;
  1770. list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
  1771. if (chan->signal == id)
  1772. return &chan->vc.chan;
  1773. }
  1774. return NULL;
  1775. }
  1776. static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
  1777. struct of_dma *ofdma)
  1778. {
  1779. struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
  1780. struct dma_chan *dma_chan;
  1781. struct pl08x_dma_chan *plchan;
  1782. if (!pl08x)
  1783. return NULL;
  1784. if (dma_spec->args_count != 2) {
  1785. dev_err(&pl08x->adev->dev,
  1786. "DMA channel translation requires two cells\n");
  1787. return NULL;
  1788. }
  1789. dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
  1790. if (!dma_chan) {
  1791. dev_err(&pl08x->adev->dev,
  1792. "DMA slave channel not found\n");
  1793. return NULL;
  1794. }
  1795. plchan = to_pl08x_chan(dma_chan);
  1796. dev_dbg(&pl08x->adev->dev,
  1797. "translated channel for signal %d\n",
  1798. dma_spec->args[0]);
  1799. /* Augment channel data for applicable AHB buses */
  1800. plchan->cd->periph_buses = dma_spec->args[1];
  1801. return dma_get_slave_channel(dma_chan);
  1802. }
  1803. static int pl08x_of_probe(struct amba_device *adev,
  1804. struct pl08x_driver_data *pl08x,
  1805. struct device_node *np)
  1806. {
  1807. struct pl08x_platform_data *pd;
  1808. struct pl08x_channel_data *chanp = NULL;
  1809. u32 cctl_memcpy = 0;
  1810. u32 val;
  1811. int ret;
  1812. int i;
  1813. pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
  1814. if (!pd)
  1815. return -ENOMEM;
  1816. /* Eligible bus masters for fetching LLIs */
  1817. if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
  1818. pd->lli_buses |= PL08X_AHB1;
  1819. if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
  1820. pd->lli_buses |= PL08X_AHB2;
  1821. if (!pd->lli_buses) {
  1822. dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
  1823. pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
  1824. }
  1825. /* Eligible bus masters for memory access */
  1826. if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
  1827. pd->mem_buses |= PL08X_AHB1;
  1828. if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
  1829. pd->mem_buses |= PL08X_AHB2;
  1830. if (!pd->mem_buses) {
  1831. dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
  1832. pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
  1833. }
  1834. /* Parse the memcpy channel properties */
  1835. ret = of_property_read_u32(np, "memcpy-burst-size", &val);
  1836. if (ret) {
  1837. dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
  1838. val = 1;
  1839. }
  1840. switch (val) {
  1841. default:
  1842. dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
  1843. /* Fall through */
  1844. case 1:
  1845. cctl_memcpy |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
  1846. PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
  1847. break;
  1848. case 4:
  1849. cctl_memcpy |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
  1850. PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
  1851. break;
  1852. case 8:
  1853. cctl_memcpy |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
  1854. PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
  1855. break;
  1856. case 16:
  1857. cctl_memcpy |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
  1858. PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
  1859. break;
  1860. case 32:
  1861. cctl_memcpy |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
  1862. PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
  1863. break;
  1864. case 64:
  1865. cctl_memcpy |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
  1866. PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
  1867. break;
  1868. case 128:
  1869. cctl_memcpy |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
  1870. PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
  1871. break;
  1872. case 256:
  1873. cctl_memcpy |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
  1874. PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
  1875. break;
  1876. }
  1877. ret = of_property_read_u32(np, "memcpy-bus-width", &val);
  1878. if (ret) {
  1879. dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
  1880. val = 8;
  1881. }
  1882. switch (val) {
  1883. default:
  1884. dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
  1885. /* Fall through */
  1886. case 8:
  1887. cctl_memcpy |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1888. PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1889. break;
  1890. case 16:
  1891. cctl_memcpy |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1892. PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1893. break;
  1894. case 32:
  1895. cctl_memcpy |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1896. PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1897. break;
  1898. }
  1899. /* This is currently the only thing making sense */
  1900. cctl_memcpy |= PL080_CONTROL_PROT_SYS;
  1901. /* Set up memcpy channel */
  1902. pd->memcpy_channel.bus_id = "memcpy";
  1903. pd->memcpy_channel.cctl_memcpy = cctl_memcpy;
  1904. /* Use the buses that can access memory, obviously */
  1905. pd->memcpy_channel.periph_buses = pd->mem_buses;
  1906. /*
  1907. * Allocate channel data for all possible slave channels (one
  1908. * for each possible signal), channels will then be allocated
  1909. * for a device and have it's AHB interfaces set up at
  1910. * translation time.
  1911. */
  1912. chanp = devm_kcalloc(&adev->dev,
  1913. pl08x->vd->signals,
  1914. sizeof(struct pl08x_channel_data),
  1915. GFP_KERNEL);
  1916. if (!chanp)
  1917. return -ENOMEM;
  1918. pd->slave_channels = chanp;
  1919. for (i = 0; i < pl08x->vd->signals; i++) {
  1920. /* chanp->periph_buses will be assigned at translation */
  1921. chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
  1922. chanp++;
  1923. }
  1924. pd->num_slave_channels = pl08x->vd->signals;
  1925. pl08x->pd = pd;
  1926. return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
  1927. pl08x);
  1928. }
  1929. #else
  1930. static inline int pl08x_of_probe(struct amba_device *adev,
  1931. struct pl08x_driver_data *pl08x,
  1932. struct device_node *np)
  1933. {
  1934. return -EINVAL;
  1935. }
  1936. #endif
  1937. static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
  1938. {
  1939. struct pl08x_driver_data *pl08x;
  1940. const struct vendor_data *vd = id->data;
  1941. struct device_node *np = adev->dev.of_node;
  1942. u32 tsfr_size;
  1943. int ret = 0;
  1944. int i;
  1945. ret = amba_request_regions(adev, NULL);
  1946. if (ret)
  1947. return ret;
  1948. /* Ensure that we can do DMA */
  1949. ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
  1950. if (ret)
  1951. goto out_no_pl08x;
  1952. /* Create the driver state holder */
  1953. pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
  1954. if (!pl08x) {
  1955. ret = -ENOMEM;
  1956. goto out_no_pl08x;
  1957. }
  1958. /* Assign useful pointers to the driver state */
  1959. pl08x->adev = adev;
  1960. pl08x->vd = vd;
  1961. /* Initialize memcpy engine */
  1962. dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
  1963. pl08x->memcpy.dev = &adev->dev;
  1964. pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
  1965. pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
  1966. pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1967. pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
  1968. pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
  1969. pl08x->memcpy.device_config = pl08x_config;
  1970. pl08x->memcpy.device_pause = pl08x_pause;
  1971. pl08x->memcpy.device_resume = pl08x_resume;
  1972. pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
  1973. pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
  1974. pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
  1975. pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
  1976. pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1977. /* Initialize slave engine */
  1978. dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
  1979. dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
  1980. pl08x->slave.dev = &adev->dev;
  1981. pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
  1982. pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1983. pl08x->slave.device_tx_status = pl08x_dma_tx_status;
  1984. pl08x->slave.device_issue_pending = pl08x_issue_pending;
  1985. pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
  1986. pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
  1987. pl08x->slave.device_config = pl08x_config;
  1988. pl08x->slave.device_pause = pl08x_pause;
  1989. pl08x->slave.device_resume = pl08x_resume;
  1990. pl08x->slave.device_terminate_all = pl08x_terminate_all;
  1991. pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
  1992. pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
  1993. pl08x->slave.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1994. pl08x->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1995. /* Get the platform data */
  1996. pl08x->pd = dev_get_platdata(&adev->dev);
  1997. if (!pl08x->pd) {
  1998. if (np) {
  1999. ret = pl08x_of_probe(adev, pl08x, np);
  2000. if (ret)
  2001. goto out_no_platdata;
  2002. } else {
  2003. dev_err(&adev->dev, "no platform data supplied\n");
  2004. ret = -EINVAL;
  2005. goto out_no_platdata;
  2006. }
  2007. } else {
  2008. pl08x->slave.filter.map = pl08x->pd->slave_map;
  2009. pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
  2010. pl08x->slave.filter.fn = pl08x_filter_fn;
  2011. }
  2012. /* By default, AHB1 only. If dualmaster, from platform */
  2013. pl08x->lli_buses = PL08X_AHB1;
  2014. pl08x->mem_buses = PL08X_AHB1;
  2015. if (pl08x->vd->dualmaster) {
  2016. pl08x->lli_buses = pl08x->pd->lli_buses;
  2017. pl08x->mem_buses = pl08x->pd->mem_buses;
  2018. }
  2019. if (vd->pl080s)
  2020. pl08x->lli_words = PL080S_LLI_WORDS;
  2021. else
  2022. pl08x->lli_words = PL080_LLI_WORDS;
  2023. tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
  2024. /* A DMA memory pool for LLIs, align on 1-byte boundary */
  2025. pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
  2026. tsfr_size, PL08X_ALIGN, 0);
  2027. if (!pl08x->pool) {
  2028. ret = -ENOMEM;
  2029. goto out_no_lli_pool;
  2030. }
  2031. pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
  2032. if (!pl08x->base) {
  2033. ret = -ENOMEM;
  2034. goto out_no_ioremap;
  2035. }
  2036. /* Turn on the PL08x */
  2037. pl08x_ensure_on(pl08x);
  2038. /* Attach the interrupt handler */
  2039. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  2040. writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
  2041. ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
  2042. if (ret) {
  2043. dev_err(&adev->dev, "%s failed to request interrupt %d\n",
  2044. __func__, adev->irq[0]);
  2045. goto out_no_irq;
  2046. }
  2047. /* Initialize physical channels */
  2048. pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
  2049. GFP_KERNEL);
  2050. if (!pl08x->phy_chans) {
  2051. ret = -ENOMEM;
  2052. goto out_no_phychans;
  2053. }
  2054. for (i = 0; i < vd->channels; i++) {
  2055. struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
  2056. ch->id = i;
  2057. ch->base = pl08x->base + PL080_Cx_BASE(i);
  2058. ch->reg_config = ch->base + vd->config_offset;
  2059. spin_lock_init(&ch->lock);
  2060. /*
  2061. * Nomadik variants can have channels that are locked
  2062. * down for the secure world only. Lock up these channels
  2063. * by perpetually serving a dummy virtual channel.
  2064. */
  2065. if (vd->nomadik) {
  2066. u32 val;
  2067. val = readl(ch->reg_config);
  2068. if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
  2069. dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
  2070. ch->locked = true;
  2071. }
  2072. }
  2073. dev_dbg(&adev->dev, "physical channel %d is %s\n",
  2074. i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
  2075. }
  2076. /* Register as many memcpy channels as there are physical channels */
  2077. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
  2078. pl08x->vd->channels, false);
  2079. if (ret <= 0) {
  2080. dev_warn(&pl08x->adev->dev,
  2081. "%s failed to enumerate memcpy channels - %d\n",
  2082. __func__, ret);
  2083. goto out_no_memcpy;
  2084. }
  2085. /* Register slave channels */
  2086. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
  2087. pl08x->pd->num_slave_channels, true);
  2088. if (ret < 0) {
  2089. dev_warn(&pl08x->adev->dev,
  2090. "%s failed to enumerate slave channels - %d\n",
  2091. __func__, ret);
  2092. goto out_no_slave;
  2093. }
  2094. ret = dma_async_device_register(&pl08x->memcpy);
  2095. if (ret) {
  2096. dev_warn(&pl08x->adev->dev,
  2097. "%s failed to register memcpy as an async device - %d\n",
  2098. __func__, ret);
  2099. goto out_no_memcpy_reg;
  2100. }
  2101. ret = dma_async_device_register(&pl08x->slave);
  2102. if (ret) {
  2103. dev_warn(&pl08x->adev->dev,
  2104. "%s failed to register slave as an async device - %d\n",
  2105. __func__, ret);
  2106. goto out_no_slave_reg;
  2107. }
  2108. amba_set_drvdata(adev, pl08x);
  2109. init_pl08x_debugfs(pl08x);
  2110. dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
  2111. amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
  2112. (unsigned long long)adev->res.start, adev->irq[0]);
  2113. return 0;
  2114. out_no_slave_reg:
  2115. dma_async_device_unregister(&pl08x->memcpy);
  2116. out_no_memcpy_reg:
  2117. pl08x_free_virtual_channels(&pl08x->slave);
  2118. out_no_slave:
  2119. pl08x_free_virtual_channels(&pl08x->memcpy);
  2120. out_no_memcpy:
  2121. kfree(pl08x->phy_chans);
  2122. out_no_phychans:
  2123. free_irq(adev->irq[0], pl08x);
  2124. out_no_irq:
  2125. iounmap(pl08x->base);
  2126. out_no_ioremap:
  2127. dma_pool_destroy(pl08x->pool);
  2128. out_no_lli_pool:
  2129. out_no_platdata:
  2130. kfree(pl08x);
  2131. out_no_pl08x:
  2132. amba_release_regions(adev);
  2133. return ret;
  2134. }
  2135. /* PL080 has 8 channels and the PL080 have just 2 */
  2136. static struct vendor_data vendor_pl080 = {
  2137. .config_offset = PL080_CH_CONFIG,
  2138. .channels = 8,
  2139. .signals = 16,
  2140. .dualmaster = true,
  2141. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2142. };
  2143. static struct vendor_data vendor_nomadik = {
  2144. .config_offset = PL080_CH_CONFIG,
  2145. .channels = 8,
  2146. .signals = 32,
  2147. .dualmaster = true,
  2148. .nomadik = true,
  2149. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2150. };
  2151. static struct vendor_data vendor_pl080s = {
  2152. .config_offset = PL080S_CH_CONFIG,
  2153. .channels = 8,
  2154. .signals = 32,
  2155. .pl080s = true,
  2156. .max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
  2157. };
  2158. static struct vendor_data vendor_pl081 = {
  2159. .config_offset = PL080_CH_CONFIG,
  2160. .channels = 2,
  2161. .signals = 16,
  2162. .dualmaster = false,
  2163. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2164. };
  2165. static struct amba_id pl08x_ids[] = {
  2166. /* Samsung PL080S variant */
  2167. {
  2168. .id = 0x0a141080,
  2169. .mask = 0xffffffff,
  2170. .data = &vendor_pl080s,
  2171. },
  2172. /* PL080 */
  2173. {
  2174. .id = 0x00041080,
  2175. .mask = 0x000fffff,
  2176. .data = &vendor_pl080,
  2177. },
  2178. /* PL081 */
  2179. {
  2180. .id = 0x00041081,
  2181. .mask = 0x000fffff,
  2182. .data = &vendor_pl081,
  2183. },
  2184. /* Nomadik 8815 PL080 variant */
  2185. {
  2186. .id = 0x00280080,
  2187. .mask = 0x00ffffff,
  2188. .data = &vendor_nomadik,
  2189. },
  2190. { 0, 0 },
  2191. };
  2192. MODULE_DEVICE_TABLE(amba, pl08x_ids);
  2193. static struct amba_driver pl08x_amba_driver = {
  2194. .drv.name = DRIVER_NAME,
  2195. .id_table = pl08x_ids,
  2196. .probe = pl08x_probe,
  2197. };
  2198. static int __init pl08x_init(void)
  2199. {
  2200. int retval;
  2201. retval = amba_driver_register(&pl08x_amba_driver);
  2202. if (retval)
  2203. printk(KERN_WARNING DRIVER_NAME
  2204. "failed to register as an AMBA device (%d)\n",
  2205. retval);
  2206. return retval;
  2207. }
  2208. subsys_initcall(pl08x_init);