amba-pl08x.c 55 KB

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