amba-pl08x.c 54 KB

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