amba-pl08x.c 58 KB

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