amba-pl08x.c 66 KB

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