amba-pl08x.c 54 KB

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