amba-pl08x.c 53 KB

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