amba-pl08x.c 53 KB

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