amba-pl08x.c 54 KB

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