amba-pl08x.c 54 KB

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