amba-pl08x.c 54 KB

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