amba-pl08x.c 54 KB

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