amba-pl08x.c 55 KB

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