amba-pl08x.c 54 KB

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