amba-pl08x.c 52 KB

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