amba-pl08x.c 54 KB

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