amba-pl08x.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. /*
  2. * Copyright (c) 2006 ARM Ltd.
  3. * Copyright (c) 2010 ST-Ericsson SA
  4. *
  5. * Author: Peter Pearse <peter.pearse@arm.com>
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * The full GNU General Public License is in this distribution in the
  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_channel_data *cd = txd->cd;
  505. struct pl08x_bus_data *mbus, *sbus;
  506. size_t remainder;
  507. int num_llis = 0;
  508. u32 cctl;
  509. size_t max_bytes_per_lli;
  510. size_t total_bytes = 0;
  511. struct pl08x_lli *llis_va;
  512. txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
  513. &txd->llis_bus);
  514. if (!txd->llis_va) {
  515. dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
  516. return 0;
  517. }
  518. pl08x->pool_ctr++;
  519. /*
  520. * Initialize bus values for this transfer
  521. * from the passed optimal values
  522. */
  523. if (!cd) {
  524. dev_err(&pl08x->adev->dev, "%s no channel data\n", __func__);
  525. return 0;
  526. }
  527. /* Get the default CCTL from the platform data */
  528. cctl = cd->cctl;
  529. /*
  530. * On the PL080 we have two bus masters and we
  531. * should select one for source and one for
  532. * destination. We try to use AHB2 for the
  533. * bus which does not increment (typically the
  534. * peripheral) else we just choose something.
  535. */
  536. cctl &= ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
  537. if (pl08x->vd->dualmaster) {
  538. if (cctl & PL080_CONTROL_SRC_INCR)
  539. /* Source increments, use AHB2 for destination */
  540. cctl |= PL080_CONTROL_DST_AHB2;
  541. else if (cctl & PL080_CONTROL_DST_INCR)
  542. /* Destination increments, use AHB2 for source */
  543. cctl |= PL080_CONTROL_SRC_AHB2;
  544. else
  545. /* Just pick something, source AHB1 dest AHB2 */
  546. cctl |= PL080_CONTROL_DST_AHB2;
  547. }
  548. /* Find maximum width of the source bus */
  549. txd->srcbus.maxwidth =
  550. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
  551. PL080_CONTROL_SWIDTH_SHIFT);
  552. /* Find maximum width of the destination bus */
  553. txd->dstbus.maxwidth =
  554. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
  555. PL080_CONTROL_DWIDTH_SHIFT);
  556. /* Set up the bus widths to the maximum */
  557. txd->srcbus.buswidth = txd->srcbus.maxwidth;
  558. txd->dstbus.buswidth = txd->dstbus.maxwidth;
  559. dev_vdbg(&pl08x->adev->dev,
  560. "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
  561. __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
  562. /*
  563. * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
  564. */
  565. max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
  566. PL080_CONTROL_TRANSFER_SIZE_MASK;
  567. dev_vdbg(&pl08x->adev->dev,
  568. "%s max bytes per lli = %zu\n",
  569. __func__, max_bytes_per_lli);
  570. /* We need to count this down to zero */
  571. remainder = txd->len;
  572. dev_vdbg(&pl08x->adev->dev,
  573. "%s remainder = %zu\n",
  574. __func__, remainder);
  575. /*
  576. * Choose bus to align to
  577. * - prefers destination bus if both available
  578. * - if fixed address on one bus chooses other
  579. * - modifies cctl to choose an appropriate master
  580. */
  581. pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
  582. &mbus, &sbus, cctl);
  583. /*
  584. * The lowest bit of the LLI register
  585. * is also used to indicate which master to
  586. * use for reading the LLIs.
  587. */
  588. if (txd->len < mbus->buswidth) {
  589. /*
  590. * Less than a bus width available
  591. * - send as single bytes
  592. */
  593. while (remainder) {
  594. dev_vdbg(&pl08x->adev->dev,
  595. "%s single byte LLIs for a transfer of "
  596. "less than a bus width (remain 0x%08x)\n",
  597. __func__, remainder);
  598. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  599. num_llis =
  600. pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,
  601. cctl, &remainder);
  602. total_bytes++;
  603. }
  604. } else {
  605. /*
  606. * Make one byte LLIs until master bus is aligned
  607. * - slave will then be aligned also
  608. */
  609. while ((mbus->addr) % (mbus->buswidth)) {
  610. dev_vdbg(&pl08x->adev->dev,
  611. "%s adjustment lli for less than bus width "
  612. "(remain 0x%08x)\n",
  613. __func__, remainder);
  614. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  615. num_llis = pl08x_fill_lli_for_desc
  616. (pl08x, txd, num_llis, 1, cctl, &remainder);
  617. total_bytes++;
  618. }
  619. /*
  620. * Master now aligned
  621. * - if slave is not then we must set its width down
  622. */
  623. if (sbus->addr % sbus->buswidth) {
  624. dev_dbg(&pl08x->adev->dev,
  625. "%s set down bus width to one byte\n",
  626. __func__);
  627. sbus->buswidth = 1;
  628. }
  629. /*
  630. * Make largest possible LLIs until less than one bus
  631. * width left
  632. */
  633. while (remainder > (mbus->buswidth - 1)) {
  634. size_t lli_len, target_len, tsize, odd_bytes;
  635. /*
  636. * If enough left try to send max possible,
  637. * otherwise try to send the remainder
  638. */
  639. target_len = remainder;
  640. if (remainder > max_bytes_per_lli)
  641. target_len = max_bytes_per_lli;
  642. /*
  643. * Set bus lengths for incrementing buses
  644. * to number of bytes which fill to next memory
  645. * boundary
  646. */
  647. if (cctl & PL080_CONTROL_SRC_INCR)
  648. txd->srcbus.fill_bytes =
  649. pl08x_pre_boundary(
  650. txd->srcbus.addr,
  651. remainder);
  652. else
  653. txd->srcbus.fill_bytes =
  654. max_bytes_per_lli;
  655. if (cctl & PL080_CONTROL_DST_INCR)
  656. txd->dstbus.fill_bytes =
  657. pl08x_pre_boundary(
  658. txd->dstbus.addr,
  659. remainder);
  660. else
  661. txd->dstbus.fill_bytes =
  662. max_bytes_per_lli;
  663. /*
  664. * Find the nearest
  665. */
  666. lli_len = min(txd->srcbus.fill_bytes,
  667. txd->dstbus.fill_bytes);
  668. BUG_ON(lli_len > remainder);
  669. if (lli_len <= 0) {
  670. dev_err(&pl08x->adev->dev,
  671. "%s lli_len is %zu, <= 0\n",
  672. __func__, lli_len);
  673. return 0;
  674. }
  675. if (lli_len == target_len) {
  676. /*
  677. * Can send what we wanted
  678. */
  679. /*
  680. * Maintain alignment
  681. */
  682. lli_len = (lli_len/mbus->buswidth) *
  683. mbus->buswidth;
  684. odd_bytes = 0;
  685. } else {
  686. /*
  687. * So now we know how many bytes to transfer
  688. * to get to the nearest boundary
  689. * The next LLI will past the boundary
  690. * - however we may be working to a boundary
  691. * on the slave bus
  692. * We need to ensure the master stays aligned
  693. */
  694. odd_bytes = lli_len % mbus->buswidth;
  695. /*
  696. * - and that we are working in multiples
  697. * of the bus widths
  698. */
  699. lli_len -= odd_bytes;
  700. }
  701. if (lli_len) {
  702. /*
  703. * Check against minimum bus alignment:
  704. * Calculate actual transfer size in relation
  705. * to bus width an get a maximum remainder of
  706. * the smallest bus width - 1
  707. */
  708. /* FIXME: use round_down()? */
  709. tsize = lli_len / min(mbus->buswidth,
  710. sbus->buswidth);
  711. lli_len = tsize * min(mbus->buswidth,
  712. sbus->buswidth);
  713. if (target_len != lli_len) {
  714. dev_vdbg(&pl08x->adev->dev,
  715. "%s can't send what we want. Desired 0x%08zx, lli of 0x%08zx bytes in txd of 0x%08zx\n",
  716. __func__, target_len, lli_len, txd->len);
  717. }
  718. cctl = pl08x_cctl_bits(cctl,
  719. txd->srcbus.buswidth,
  720. txd->dstbus.buswidth,
  721. tsize);
  722. dev_vdbg(&pl08x->adev->dev,
  723. "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
  724. __func__, lli_len, remainder);
  725. num_llis = pl08x_fill_lli_for_desc(pl08x, txd,
  726. num_llis, lli_len, cctl,
  727. &remainder);
  728. total_bytes += lli_len;
  729. }
  730. if (odd_bytes) {
  731. /*
  732. * Creep past the boundary,
  733. * maintaining master alignment
  734. */
  735. int j;
  736. for (j = 0; (j < mbus->buswidth)
  737. && (remainder); j++) {
  738. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  739. dev_vdbg(&pl08x->adev->dev,
  740. "%s align with boundary, single byte (remain 0x%08zx)\n",
  741. __func__, remainder);
  742. num_llis =
  743. pl08x_fill_lli_for_desc(pl08x,
  744. txd, num_llis, 1,
  745. cctl, &remainder);
  746. total_bytes++;
  747. }
  748. }
  749. }
  750. /*
  751. * Send any odd bytes
  752. */
  753. while (remainder) {
  754. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  755. dev_vdbg(&pl08x->adev->dev,
  756. "%s align with boundary, single odd byte (remain %zu)\n",
  757. __func__, remainder);
  758. num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,
  759. 1, cctl, &remainder);
  760. total_bytes++;
  761. }
  762. }
  763. if (total_bytes != txd->len) {
  764. dev_err(&pl08x->adev->dev,
  765. "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
  766. __func__, total_bytes, txd->len);
  767. return 0;
  768. }
  769. if (num_llis >= MAX_NUM_TSFR_LLIS) {
  770. dev_err(&pl08x->adev->dev,
  771. "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
  772. __func__, (u32) MAX_NUM_TSFR_LLIS);
  773. return 0;
  774. }
  775. llis_va = txd->llis_va;
  776. /*
  777. * The final LLI terminates the LLI.
  778. */
  779. llis_va[num_llis - 1].lli = 0;
  780. /*
  781. * The final LLI element shall also fire an interrupt
  782. */
  783. llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
  784. #ifdef VERBOSE_DEBUG
  785. {
  786. int i;
  787. for (i = 0; i < num_llis; i++) {
  788. dev_vdbg(&pl08x->adev->dev,
  789. "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
  790. i,
  791. &llis_va[i],
  792. llis_va[i].src,
  793. llis_va[i].dst,
  794. llis_va[i].cctl,
  795. llis_va[i].lli
  796. );
  797. }
  798. }
  799. #endif
  800. return num_llis;
  801. }
  802. /* You should call this with the struct pl08x lock held */
  803. static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
  804. struct pl08x_txd *txd)
  805. {
  806. /* Free the LLI */
  807. dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
  808. pl08x->pool_ctr--;
  809. kfree(txd);
  810. }
  811. static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
  812. struct pl08x_dma_chan *plchan)
  813. {
  814. struct pl08x_txd *txdi = NULL;
  815. struct pl08x_txd *next;
  816. if (!list_empty(&plchan->desc_list)) {
  817. list_for_each_entry_safe(txdi,
  818. next, &plchan->desc_list, node) {
  819. list_del(&txdi->node);
  820. pl08x_free_txd(pl08x, txdi);
  821. }
  822. }
  823. }
  824. /*
  825. * The DMA ENGINE API
  826. */
  827. static int pl08x_alloc_chan_resources(struct dma_chan *chan)
  828. {
  829. return 0;
  830. }
  831. static void pl08x_free_chan_resources(struct dma_chan *chan)
  832. {
  833. }
  834. /*
  835. * This should be called with the channel plchan->lock held
  836. */
  837. static int prep_phy_channel(struct pl08x_dma_chan *plchan,
  838. struct pl08x_txd *txd)
  839. {
  840. struct pl08x_driver_data *pl08x = plchan->host;
  841. struct pl08x_phy_chan *ch;
  842. int ret;
  843. /* Check if we already have a channel */
  844. if (plchan->phychan)
  845. return 0;
  846. ch = pl08x_get_phy_channel(pl08x, plchan);
  847. if (!ch) {
  848. /* No physical channel available, cope with it */
  849. dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
  850. return -EBUSY;
  851. }
  852. /*
  853. * OK we have a physical channel: for memcpy() this is all we
  854. * need, but for slaves the physical signals may be muxed!
  855. * Can the platform allow us to use this channel?
  856. */
  857. if (plchan->slave &&
  858. ch->signal < 0 &&
  859. pl08x->pd->get_signal) {
  860. ret = pl08x->pd->get_signal(plchan);
  861. if (ret < 0) {
  862. dev_dbg(&pl08x->adev->dev,
  863. "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
  864. ch->id, plchan->name);
  865. /* Release physical channel & return */
  866. pl08x_put_phy_channel(pl08x, ch);
  867. return -EBUSY;
  868. }
  869. ch->signal = ret;
  870. /* Assign the flow control signal to this channel */
  871. if (txd->direction == DMA_TO_DEVICE)
  872. txd->ccfg |= ch->signal << PL080_CONFIG_DST_SEL_SHIFT;
  873. else if (txd->direction == DMA_FROM_DEVICE)
  874. txd->ccfg |= ch->signal << PL080_CONFIG_SRC_SEL_SHIFT;
  875. }
  876. dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
  877. ch->id,
  878. ch->signal,
  879. plchan->name);
  880. plchan->phychan = ch;
  881. return 0;
  882. }
  883. static void release_phy_channel(struct pl08x_dma_chan *plchan)
  884. {
  885. struct pl08x_driver_data *pl08x = plchan->host;
  886. if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
  887. pl08x->pd->put_signal(plchan);
  888. plchan->phychan->signal = -1;
  889. }
  890. pl08x_put_phy_channel(pl08x, plchan->phychan);
  891. plchan->phychan = NULL;
  892. }
  893. static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
  894. {
  895. struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
  896. plchan->chan.cookie += 1;
  897. if (plchan->chan.cookie < 0)
  898. plchan->chan.cookie = 1;
  899. tx->cookie = plchan->chan.cookie;
  900. /* This unlock follows the lock in the prep() function */
  901. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  902. return tx->cookie;
  903. }
  904. static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
  905. struct dma_chan *chan, unsigned long flags)
  906. {
  907. struct dma_async_tx_descriptor *retval = NULL;
  908. return retval;
  909. }
  910. /*
  911. * Code accessing dma_async_is_complete() in a tight loop
  912. * may give problems - could schedule where indicated.
  913. * If slaves are relying on interrupts to signal completion this
  914. * function must not be called with interrupts disabled
  915. */
  916. static enum dma_status
  917. pl08x_dma_tx_status(struct dma_chan *chan,
  918. dma_cookie_t cookie,
  919. struct dma_tx_state *txstate)
  920. {
  921. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  922. dma_cookie_t last_used;
  923. dma_cookie_t last_complete;
  924. enum dma_status ret;
  925. u32 bytesleft = 0;
  926. last_used = plchan->chan.cookie;
  927. last_complete = plchan->lc;
  928. ret = dma_async_is_complete(cookie, last_complete, last_used);
  929. if (ret == DMA_SUCCESS) {
  930. dma_set_tx_state(txstate, last_complete, last_used, 0);
  931. return ret;
  932. }
  933. /*
  934. * schedule(); could be inserted here
  935. */
  936. /*
  937. * This cookie not complete yet
  938. */
  939. last_used = plchan->chan.cookie;
  940. last_complete = plchan->lc;
  941. /* Get number of bytes left in the active transactions and queue */
  942. bytesleft = pl08x_getbytes_chan(plchan);
  943. dma_set_tx_state(txstate, last_complete, last_used,
  944. bytesleft);
  945. if (plchan->state == PL08X_CHAN_PAUSED)
  946. return DMA_PAUSED;
  947. /* Whether waiting or running, we're in progress */
  948. return DMA_IN_PROGRESS;
  949. }
  950. /* PrimeCell DMA extension */
  951. struct burst_table {
  952. int burstwords;
  953. u32 reg;
  954. };
  955. static const struct burst_table burst_sizes[] = {
  956. {
  957. .burstwords = 256,
  958. .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
  959. (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
  960. },
  961. {
  962. .burstwords = 128,
  963. .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
  964. (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
  965. },
  966. {
  967. .burstwords = 64,
  968. .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
  969. (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
  970. },
  971. {
  972. .burstwords = 32,
  973. .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
  974. (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
  975. },
  976. {
  977. .burstwords = 16,
  978. .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
  979. (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
  980. },
  981. {
  982. .burstwords = 8,
  983. .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
  984. (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
  985. },
  986. {
  987. .burstwords = 4,
  988. .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
  989. (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
  990. },
  991. {
  992. .burstwords = 1,
  993. .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  994. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
  995. },
  996. };
  997. static void dma_set_runtime_config(struct dma_chan *chan,
  998. struct dma_slave_config *config)
  999. {
  1000. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1001. struct pl08x_driver_data *pl08x = plchan->host;
  1002. struct pl08x_channel_data *cd = plchan->cd;
  1003. enum dma_slave_buswidth addr_width;
  1004. u32 maxburst;
  1005. u32 cctl = 0;
  1006. int i;
  1007. /* Transfer direction */
  1008. plchan->runtime_direction = config->direction;
  1009. if (config->direction == DMA_TO_DEVICE) {
  1010. plchan->runtime_addr = config->dst_addr;
  1011. cctl |= PL080_CONTROL_SRC_INCR;
  1012. addr_width = config->dst_addr_width;
  1013. maxburst = config->dst_maxburst;
  1014. } else if (config->direction == DMA_FROM_DEVICE) {
  1015. plchan->runtime_addr = config->src_addr;
  1016. cctl |= PL080_CONTROL_DST_INCR;
  1017. addr_width = config->src_addr_width;
  1018. maxburst = config->src_maxburst;
  1019. } else {
  1020. dev_err(&pl08x->adev->dev,
  1021. "bad runtime_config: alien transfer direction\n");
  1022. return;
  1023. }
  1024. switch (addr_width) {
  1025. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1026. cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1027. (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1028. break;
  1029. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1030. cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1031. (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1032. break;
  1033. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1034. cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1035. (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1036. break;
  1037. default:
  1038. dev_err(&pl08x->adev->dev,
  1039. "bad runtime_config: alien address width\n");
  1040. return;
  1041. }
  1042. /*
  1043. * Now decide on a maxburst:
  1044. * If this channel will only request single transfers, set this
  1045. * down to ONE element. Also select one element if no maxburst
  1046. * is specified.
  1047. */
  1048. if (plchan->cd->single || maxburst == 0) {
  1049. cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1050. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
  1051. } else {
  1052. for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
  1053. if (burst_sizes[i].burstwords <= maxburst)
  1054. break;
  1055. cctl |= burst_sizes[i].reg;
  1056. }
  1057. /* Access the cell in privileged mode, non-bufferable, non-cacheable */
  1058. cctl &= ~PL080_CONTROL_PROT_MASK;
  1059. cctl |= PL080_CONTROL_PROT_SYS;
  1060. /* Modify the default channel data to fit PrimeCell request */
  1061. cd->cctl = cctl;
  1062. dev_dbg(&pl08x->adev->dev,
  1063. "configured channel %s (%s) for %s, data width %d, "
  1064. "maxburst %d words, LE, CCTL=0x%08x\n",
  1065. dma_chan_name(chan), plchan->name,
  1066. (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
  1067. addr_width,
  1068. maxburst,
  1069. cctl);
  1070. }
  1071. /*
  1072. * Slave transactions callback to the slave device to allow
  1073. * synchronization of slave DMA signals with the DMAC enable
  1074. */
  1075. static void pl08x_issue_pending(struct dma_chan *chan)
  1076. {
  1077. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1078. unsigned long flags;
  1079. spin_lock_irqsave(&plchan->lock, flags);
  1080. /* Something is already active, or we're waiting for a channel... */
  1081. if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
  1082. spin_unlock_irqrestore(&plchan->lock, flags);
  1083. return;
  1084. }
  1085. /* Take the first element in the queue and execute it */
  1086. if (!list_empty(&plchan->desc_list)) {
  1087. struct pl08x_txd *next;
  1088. next = list_first_entry(&plchan->desc_list,
  1089. struct pl08x_txd,
  1090. node);
  1091. list_del(&next->node);
  1092. plchan->state = PL08X_CHAN_RUNNING;
  1093. pl08x_start_txd(plchan, next);
  1094. }
  1095. spin_unlock_irqrestore(&plchan->lock, flags);
  1096. }
  1097. static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
  1098. struct pl08x_txd *txd)
  1099. {
  1100. int num_llis;
  1101. struct pl08x_driver_data *pl08x = plchan->host;
  1102. int ret;
  1103. num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
  1104. if (!num_llis) {
  1105. kfree(txd);
  1106. return -EINVAL;
  1107. }
  1108. spin_lock_irqsave(&plchan->lock, plchan->lockflags);
  1109. list_add_tail(&txd->node, &plchan->desc_list);
  1110. /*
  1111. * See if we already have a physical channel allocated,
  1112. * else this is the time to try to get one.
  1113. */
  1114. ret = prep_phy_channel(plchan, txd);
  1115. if (ret) {
  1116. /*
  1117. * No physical channel available, we will
  1118. * stack up the memcpy channels until there is a channel
  1119. * available to handle it whereas slave transfers may
  1120. * have been denied due to platform channel muxing restrictions
  1121. * and since there is no guarantee that this will ever be
  1122. * resolved, and since the signal must be acquired AFTER
  1123. * acquiring the physical channel, we will let them be NACK:ed
  1124. * with -EBUSY here. The drivers can alway retry the prep()
  1125. * call if they are eager on doing this using DMA.
  1126. */
  1127. if (plchan->slave) {
  1128. pl08x_free_txd_list(pl08x, plchan);
  1129. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  1130. return -EBUSY;
  1131. }
  1132. /* Do this memcpy whenever there is a channel ready */
  1133. plchan->state = PL08X_CHAN_WAITING;
  1134. plchan->waiting = txd;
  1135. } else
  1136. /*
  1137. * Else we're all set, paused and ready to roll,
  1138. * status will switch to PL08X_CHAN_RUNNING when
  1139. * we call issue_pending(). If there is something
  1140. * running on the channel already we don't change
  1141. * its state.
  1142. */
  1143. if (plchan->state == PL08X_CHAN_IDLE)
  1144. plchan->state = PL08X_CHAN_PAUSED;
  1145. /*
  1146. * Notice that we leave plchan->lock locked on purpose:
  1147. * it will be unlocked in the subsequent tx_submit()
  1148. * call. This is a consequence of the current API.
  1149. */
  1150. return 0;
  1151. }
  1152. static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
  1153. {
  1154. struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
  1155. if (txd) {
  1156. dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
  1157. txd->tx.tx_submit = pl08x_tx_submit;
  1158. INIT_LIST_HEAD(&txd->node);
  1159. /* Always enable error and terminal interrupts */
  1160. txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
  1161. PL080_CONFIG_TC_IRQ_MASK;
  1162. }
  1163. return txd;
  1164. }
  1165. /*
  1166. * Initialize a descriptor to be used by memcpy submit
  1167. */
  1168. static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
  1169. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  1170. size_t len, unsigned long flags)
  1171. {
  1172. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1173. struct pl08x_driver_data *pl08x = plchan->host;
  1174. struct pl08x_txd *txd;
  1175. int ret;
  1176. txd = pl08x_get_txd(plchan);
  1177. if (!txd) {
  1178. dev_err(&pl08x->adev->dev,
  1179. "%s no memory for descriptor\n", __func__);
  1180. return NULL;
  1181. }
  1182. txd->direction = DMA_NONE;
  1183. txd->srcbus.addr = src;
  1184. txd->dstbus.addr = dest;
  1185. /* Set platform data for m2m */
  1186. txd->cd = &pl08x->pd->memcpy_channel;
  1187. txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1188. /* Both to be incremented or the code will break */
  1189. txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
  1190. txd->len = len;
  1191. ret = pl08x_prep_channel_resources(plchan, txd);
  1192. if (ret)
  1193. return NULL;
  1194. /*
  1195. * NB: the channel lock is held at this point so tx_submit()
  1196. * must be called in direct succession.
  1197. */
  1198. return &txd->tx;
  1199. }
  1200. static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
  1201. struct dma_chan *chan, struct scatterlist *sgl,
  1202. unsigned int sg_len, enum dma_data_direction direction,
  1203. unsigned long flags)
  1204. {
  1205. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1206. struct pl08x_driver_data *pl08x = plchan->host;
  1207. struct pl08x_txd *txd;
  1208. int ret;
  1209. /*
  1210. * Current implementation ASSUMES only one sg
  1211. */
  1212. if (sg_len != 1) {
  1213. dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
  1214. __func__);
  1215. BUG();
  1216. }
  1217. dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
  1218. __func__, sgl->length, plchan->name);
  1219. txd = pl08x_get_txd(plchan);
  1220. if (!txd) {
  1221. dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
  1222. return NULL;
  1223. }
  1224. if (direction != plchan->runtime_direction)
  1225. dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
  1226. "the direction configured for the PrimeCell\n",
  1227. __func__);
  1228. /*
  1229. * Set up addresses, the PrimeCell configured address
  1230. * will take precedence since this may configure the
  1231. * channel target address dynamically at runtime.
  1232. */
  1233. txd->direction = direction;
  1234. if (direction == DMA_TO_DEVICE) {
  1235. txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1236. txd->srcbus.addr = sgl->dma_address;
  1237. if (plchan->runtime_addr)
  1238. txd->dstbus.addr = plchan->runtime_addr;
  1239. else
  1240. txd->dstbus.addr = plchan->cd->addr;
  1241. } else if (direction == DMA_FROM_DEVICE) {
  1242. txd->ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1243. if (plchan->runtime_addr)
  1244. txd->srcbus.addr = plchan->runtime_addr;
  1245. else
  1246. txd->srcbus.addr = plchan->cd->addr;
  1247. txd->dstbus.addr = sgl->dma_address;
  1248. } else {
  1249. dev_err(&pl08x->adev->dev,
  1250. "%s direction unsupported\n", __func__);
  1251. return NULL;
  1252. }
  1253. txd->cd = plchan->cd;
  1254. txd->len = sgl->length;
  1255. ret = pl08x_prep_channel_resources(plchan, txd);
  1256. if (ret)
  1257. return NULL;
  1258. /*
  1259. * NB: the channel lock is held at this point so tx_submit()
  1260. * must be called in direct succession.
  1261. */
  1262. return &txd->tx;
  1263. }
  1264. static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1265. unsigned long arg)
  1266. {
  1267. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1268. struct pl08x_driver_data *pl08x = plchan->host;
  1269. unsigned long flags;
  1270. int ret = 0;
  1271. /* Controls applicable to inactive channels */
  1272. if (cmd == DMA_SLAVE_CONFIG) {
  1273. dma_set_runtime_config(chan,
  1274. (struct dma_slave_config *)
  1275. arg);
  1276. return 0;
  1277. }
  1278. /*
  1279. * Anything succeeds on channels with no physical allocation and
  1280. * no queued transfers.
  1281. */
  1282. spin_lock_irqsave(&plchan->lock, flags);
  1283. if (!plchan->phychan && !plchan->at) {
  1284. spin_unlock_irqrestore(&plchan->lock, flags);
  1285. return 0;
  1286. }
  1287. switch (cmd) {
  1288. case DMA_TERMINATE_ALL:
  1289. plchan->state = PL08X_CHAN_IDLE;
  1290. if (plchan->phychan) {
  1291. pl08x_stop_phy_chan(plchan->phychan);
  1292. /*
  1293. * Mark physical channel as free and free any slave
  1294. * signal
  1295. */
  1296. release_phy_channel(plchan);
  1297. }
  1298. /* Dequeue jobs and free LLIs */
  1299. if (plchan->at) {
  1300. pl08x_free_txd(pl08x, plchan->at);
  1301. plchan->at = NULL;
  1302. }
  1303. /* Dequeue jobs not yet fired as well */
  1304. pl08x_free_txd_list(pl08x, plchan);
  1305. break;
  1306. case DMA_PAUSE:
  1307. pl08x_pause_phy_chan(plchan->phychan);
  1308. plchan->state = PL08X_CHAN_PAUSED;
  1309. break;
  1310. case DMA_RESUME:
  1311. pl08x_resume_phy_chan(plchan->phychan);
  1312. plchan->state = PL08X_CHAN_RUNNING;
  1313. break;
  1314. default:
  1315. /* Unknown command */
  1316. ret = -ENXIO;
  1317. break;
  1318. }
  1319. spin_unlock_irqrestore(&plchan->lock, flags);
  1320. return ret;
  1321. }
  1322. bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
  1323. {
  1324. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1325. char *name = chan_id;
  1326. /* Check that the channel is not taken! */
  1327. if (!strcmp(plchan->name, name))
  1328. return true;
  1329. return false;
  1330. }
  1331. /*
  1332. * Just check that the device is there and active
  1333. * TODO: turn this bit on/off depending on the number of
  1334. * physical channels actually used, if it is zero... well
  1335. * shut it off. That will save some power. Cut the clock
  1336. * at the same time.
  1337. */
  1338. static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
  1339. {
  1340. u32 val;
  1341. val = readl(pl08x->base + PL080_CONFIG);
  1342. val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
  1343. /* We implicitly clear bit 1 and that means little-endian mode */
  1344. val |= PL080_CONFIG_ENABLE;
  1345. writel(val, pl08x->base + PL080_CONFIG);
  1346. }
  1347. static void pl08x_tasklet(unsigned long data)
  1348. {
  1349. struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
  1350. struct pl08x_driver_data *pl08x = plchan->host;
  1351. unsigned long flags;
  1352. spin_lock_irqsave(&plchan->lock, flags);
  1353. if (plchan->at) {
  1354. dma_async_tx_callback callback =
  1355. plchan->at->tx.callback;
  1356. void *callback_param =
  1357. plchan->at->tx.callback_param;
  1358. /*
  1359. * Update last completed
  1360. */
  1361. plchan->lc = plchan->at->tx.cookie;
  1362. /*
  1363. * Callback to signal completion
  1364. */
  1365. if (callback)
  1366. callback(callback_param);
  1367. /*
  1368. * Free the descriptor
  1369. */
  1370. pl08x_free_txd(pl08x, plchan->at);
  1371. plchan->at = NULL;
  1372. }
  1373. /*
  1374. * If a new descriptor is queued, set it up
  1375. * plchan->at is NULL here
  1376. */
  1377. if (!list_empty(&plchan->desc_list)) {
  1378. struct pl08x_txd *next;
  1379. next = list_first_entry(&plchan->desc_list,
  1380. struct pl08x_txd,
  1381. node);
  1382. list_del(&next->node);
  1383. pl08x_start_txd(plchan, next);
  1384. } else {
  1385. struct pl08x_dma_chan *waiting = NULL;
  1386. /*
  1387. * No more jobs, so free up the physical channel
  1388. * Free any allocated signal on slave transfers too
  1389. */
  1390. release_phy_channel(plchan);
  1391. plchan->state = PL08X_CHAN_IDLE;
  1392. /*
  1393. * And NOW before anyone else can grab that free:d
  1394. * up physical channel, see if there is some memcpy
  1395. * pending that seriously needs to start because of
  1396. * being stacked up while we were choking the
  1397. * physical channels with data.
  1398. */
  1399. list_for_each_entry(waiting, &pl08x->memcpy.channels,
  1400. chan.device_node) {
  1401. if (waiting->state == PL08X_CHAN_WAITING &&
  1402. waiting->waiting != NULL) {
  1403. int ret;
  1404. /* This should REALLY not fail now */
  1405. ret = prep_phy_channel(waiting,
  1406. waiting->waiting);
  1407. BUG_ON(ret);
  1408. waiting->state = PL08X_CHAN_RUNNING;
  1409. waiting->waiting = NULL;
  1410. pl08x_issue_pending(&waiting->chan);
  1411. break;
  1412. }
  1413. }
  1414. }
  1415. spin_unlock_irqrestore(&plchan->lock, flags);
  1416. }
  1417. static irqreturn_t pl08x_irq(int irq, void *dev)
  1418. {
  1419. struct pl08x_driver_data *pl08x = dev;
  1420. u32 mask = 0;
  1421. u32 val;
  1422. int i;
  1423. val = readl(pl08x->base + PL080_ERR_STATUS);
  1424. if (val) {
  1425. /*
  1426. * An error interrupt (on one or more channels)
  1427. */
  1428. dev_err(&pl08x->adev->dev,
  1429. "%s error interrupt, register value 0x%08x\n",
  1430. __func__, val);
  1431. /*
  1432. * Simply clear ALL PL08X error interrupts,
  1433. * regardless of channel and cause
  1434. * FIXME: should be 0x00000003 on PL081 really.
  1435. */
  1436. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  1437. }
  1438. val = readl(pl08x->base + PL080_INT_STATUS);
  1439. for (i = 0; i < pl08x->vd->channels; i++) {
  1440. if ((1 << i) & val) {
  1441. /* Locate physical channel */
  1442. struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
  1443. struct pl08x_dma_chan *plchan = phychan->serving;
  1444. /* Schedule tasklet on this channel */
  1445. tasklet_schedule(&plchan->tasklet);
  1446. mask |= (1 << i);
  1447. }
  1448. }
  1449. /*
  1450. * Clear only the terminal interrupts on channels we processed
  1451. */
  1452. writel(mask, pl08x->base + PL080_TC_CLEAR);
  1453. return mask ? IRQ_HANDLED : IRQ_NONE;
  1454. }
  1455. /*
  1456. * Initialise the DMAC memcpy/slave channels.
  1457. * Make a local wrapper to hold required data
  1458. */
  1459. static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
  1460. struct dma_device *dmadev,
  1461. unsigned int channels,
  1462. bool slave)
  1463. {
  1464. struct pl08x_dma_chan *chan;
  1465. int i;
  1466. INIT_LIST_HEAD(&dmadev->channels);
  1467. /*
  1468. * Register as many many memcpy as we have physical channels,
  1469. * we won't always be able to use all but the code will have
  1470. * to cope with that situation.
  1471. */
  1472. for (i = 0; i < channels; i++) {
  1473. chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
  1474. if (!chan) {
  1475. dev_err(&pl08x->adev->dev,
  1476. "%s no memory for channel\n", __func__);
  1477. return -ENOMEM;
  1478. }
  1479. chan->host = pl08x;
  1480. chan->state = PL08X_CHAN_IDLE;
  1481. if (slave) {
  1482. chan->slave = true;
  1483. chan->name = pl08x->pd->slave_channels[i].bus_id;
  1484. chan->cd = &pl08x->pd->slave_channels[i];
  1485. } else {
  1486. chan->cd = &pl08x->pd->memcpy_channel;
  1487. chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
  1488. if (!chan->name) {
  1489. kfree(chan);
  1490. return -ENOMEM;
  1491. }
  1492. }
  1493. if (chan->cd->circular_buffer) {
  1494. dev_err(&pl08x->adev->dev,
  1495. "channel %s: circular buffers not supported\n",
  1496. chan->name);
  1497. kfree(chan);
  1498. continue;
  1499. }
  1500. dev_info(&pl08x->adev->dev,
  1501. "initialize virtual channel \"%s\"\n",
  1502. chan->name);
  1503. chan->chan.device = dmadev;
  1504. chan->chan.cookie = 0;
  1505. chan->lc = 0;
  1506. spin_lock_init(&chan->lock);
  1507. INIT_LIST_HEAD(&chan->desc_list);
  1508. tasklet_init(&chan->tasklet, pl08x_tasklet,
  1509. (unsigned long) chan);
  1510. list_add_tail(&chan->chan.device_node, &dmadev->channels);
  1511. }
  1512. dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
  1513. i, slave ? "slave" : "memcpy");
  1514. return i;
  1515. }
  1516. static void pl08x_free_virtual_channels(struct dma_device *dmadev)
  1517. {
  1518. struct pl08x_dma_chan *chan = NULL;
  1519. struct pl08x_dma_chan *next;
  1520. list_for_each_entry_safe(chan,
  1521. next, &dmadev->channels, chan.device_node) {
  1522. list_del(&chan->chan.device_node);
  1523. kfree(chan);
  1524. }
  1525. }
  1526. #ifdef CONFIG_DEBUG_FS
  1527. static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
  1528. {
  1529. switch (state) {
  1530. case PL08X_CHAN_IDLE:
  1531. return "idle";
  1532. case PL08X_CHAN_RUNNING:
  1533. return "running";
  1534. case PL08X_CHAN_PAUSED:
  1535. return "paused";
  1536. case PL08X_CHAN_WAITING:
  1537. return "waiting";
  1538. default:
  1539. break;
  1540. }
  1541. return "UNKNOWN STATE";
  1542. }
  1543. static int pl08x_debugfs_show(struct seq_file *s, void *data)
  1544. {
  1545. struct pl08x_driver_data *pl08x = s->private;
  1546. struct pl08x_dma_chan *chan;
  1547. struct pl08x_phy_chan *ch;
  1548. unsigned long flags;
  1549. int i;
  1550. seq_printf(s, "PL08x physical channels:\n");
  1551. seq_printf(s, "CHANNEL:\tUSER:\n");
  1552. seq_printf(s, "--------\t-----\n");
  1553. for (i = 0; i < pl08x->vd->channels; i++) {
  1554. struct pl08x_dma_chan *virt_chan;
  1555. ch = &pl08x->phy_chans[i];
  1556. spin_lock_irqsave(&ch->lock, flags);
  1557. virt_chan = ch->serving;
  1558. seq_printf(s, "%d\t\t%s\n",
  1559. ch->id, virt_chan ? virt_chan->name : "(none)");
  1560. spin_unlock_irqrestore(&ch->lock, flags);
  1561. }
  1562. seq_printf(s, "\nPL08x virtual memcpy channels:\n");
  1563. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1564. seq_printf(s, "--------\t------\n");
  1565. list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
  1566. seq_printf(s, "%s\t\t%s\n", chan->name,
  1567. pl08x_state_str(chan->state));
  1568. }
  1569. seq_printf(s, "\nPL08x virtual slave channels:\n");
  1570. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1571. seq_printf(s, "--------\t------\n");
  1572. list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
  1573. seq_printf(s, "%s\t\t%s\n", chan->name,
  1574. pl08x_state_str(chan->state));
  1575. }
  1576. return 0;
  1577. }
  1578. static int pl08x_debugfs_open(struct inode *inode, struct file *file)
  1579. {
  1580. return single_open(file, pl08x_debugfs_show, inode->i_private);
  1581. }
  1582. static const struct file_operations pl08x_debugfs_operations = {
  1583. .open = pl08x_debugfs_open,
  1584. .read = seq_read,
  1585. .llseek = seq_lseek,
  1586. .release = single_release,
  1587. };
  1588. static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1589. {
  1590. /* Expose a simple debugfs interface to view all clocks */
  1591. (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
  1592. NULL, pl08x,
  1593. &pl08x_debugfs_operations);
  1594. }
  1595. #else
  1596. static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1597. {
  1598. }
  1599. #endif
  1600. static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
  1601. {
  1602. struct pl08x_driver_data *pl08x;
  1603. const struct vendor_data *vd = id->data;
  1604. int ret = 0;
  1605. int i;
  1606. ret = amba_request_regions(adev, NULL);
  1607. if (ret)
  1608. return ret;
  1609. /* Create the driver state holder */
  1610. pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
  1611. if (!pl08x) {
  1612. ret = -ENOMEM;
  1613. goto out_no_pl08x;
  1614. }
  1615. /* Initialize memcpy engine */
  1616. dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
  1617. pl08x->memcpy.dev = &adev->dev;
  1618. pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
  1619. pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
  1620. pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
  1621. pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1622. pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
  1623. pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
  1624. pl08x->memcpy.device_control = pl08x_control;
  1625. /* Initialize slave engine */
  1626. dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
  1627. pl08x->slave.dev = &adev->dev;
  1628. pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
  1629. pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
  1630. pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1631. pl08x->slave.device_tx_status = pl08x_dma_tx_status;
  1632. pl08x->slave.device_issue_pending = pl08x_issue_pending;
  1633. pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
  1634. pl08x->slave.device_control = pl08x_control;
  1635. /* Get the platform data */
  1636. pl08x->pd = dev_get_platdata(&adev->dev);
  1637. if (!pl08x->pd) {
  1638. dev_err(&adev->dev, "no platform data supplied\n");
  1639. goto out_no_platdata;
  1640. }
  1641. /* Assign useful pointers to the driver state */
  1642. pl08x->adev = adev;
  1643. pl08x->vd = vd;
  1644. /* A DMA memory pool for LLIs, align on 1-byte boundary */
  1645. pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
  1646. PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
  1647. if (!pl08x->pool) {
  1648. ret = -ENOMEM;
  1649. goto out_no_lli_pool;
  1650. }
  1651. spin_lock_init(&pl08x->lock);
  1652. pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
  1653. if (!pl08x->base) {
  1654. ret = -ENOMEM;
  1655. goto out_no_ioremap;
  1656. }
  1657. /* Turn on the PL08x */
  1658. pl08x_ensure_on(pl08x);
  1659. /*
  1660. * Attach the interrupt handler
  1661. */
  1662. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  1663. writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
  1664. ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
  1665. DRIVER_NAME, pl08x);
  1666. if (ret) {
  1667. dev_err(&adev->dev, "%s failed to request interrupt %d\n",
  1668. __func__, adev->irq[0]);
  1669. goto out_no_irq;
  1670. }
  1671. /* Initialize physical channels */
  1672. pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
  1673. GFP_KERNEL);
  1674. if (!pl08x->phy_chans) {
  1675. dev_err(&adev->dev, "%s failed to allocate "
  1676. "physical channel holders\n",
  1677. __func__);
  1678. goto out_no_phychans;
  1679. }
  1680. for (i = 0; i < vd->channels; i++) {
  1681. struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
  1682. ch->id = i;
  1683. ch->base = pl08x->base + PL080_Cx_BASE(i);
  1684. spin_lock_init(&ch->lock);
  1685. ch->serving = NULL;
  1686. ch->signal = -1;
  1687. dev_info(&adev->dev,
  1688. "physical channel %d is %s\n", i,
  1689. pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
  1690. }
  1691. /* Register as many memcpy channels as there are physical channels */
  1692. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
  1693. pl08x->vd->channels, false);
  1694. if (ret <= 0) {
  1695. dev_warn(&pl08x->adev->dev,
  1696. "%s failed to enumerate memcpy channels - %d\n",
  1697. __func__, ret);
  1698. goto out_no_memcpy;
  1699. }
  1700. pl08x->memcpy.chancnt = ret;
  1701. /* Register slave channels */
  1702. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
  1703. pl08x->pd->num_slave_channels,
  1704. true);
  1705. if (ret <= 0) {
  1706. dev_warn(&pl08x->adev->dev,
  1707. "%s failed to enumerate slave channels - %d\n",
  1708. __func__, ret);
  1709. goto out_no_slave;
  1710. }
  1711. pl08x->slave.chancnt = ret;
  1712. ret = dma_async_device_register(&pl08x->memcpy);
  1713. if (ret) {
  1714. dev_warn(&pl08x->adev->dev,
  1715. "%s failed to register memcpy as an async device - %d\n",
  1716. __func__, ret);
  1717. goto out_no_memcpy_reg;
  1718. }
  1719. ret = dma_async_device_register(&pl08x->slave);
  1720. if (ret) {
  1721. dev_warn(&pl08x->adev->dev,
  1722. "%s failed to register slave as an async device - %d\n",
  1723. __func__, ret);
  1724. goto out_no_slave_reg;
  1725. }
  1726. amba_set_drvdata(adev, pl08x);
  1727. init_pl08x_debugfs(pl08x);
  1728. dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
  1729. amba_part(adev), amba_rev(adev),
  1730. (unsigned long long)adev->res.start, adev->irq[0]);
  1731. return 0;
  1732. out_no_slave_reg:
  1733. dma_async_device_unregister(&pl08x->memcpy);
  1734. out_no_memcpy_reg:
  1735. pl08x_free_virtual_channels(&pl08x->slave);
  1736. out_no_slave:
  1737. pl08x_free_virtual_channels(&pl08x->memcpy);
  1738. out_no_memcpy:
  1739. kfree(pl08x->phy_chans);
  1740. out_no_phychans:
  1741. free_irq(adev->irq[0], pl08x);
  1742. out_no_irq:
  1743. iounmap(pl08x->base);
  1744. out_no_ioremap:
  1745. dma_pool_destroy(pl08x->pool);
  1746. out_no_lli_pool:
  1747. out_no_platdata:
  1748. kfree(pl08x);
  1749. out_no_pl08x:
  1750. amba_release_regions(adev);
  1751. return ret;
  1752. }
  1753. /* PL080 has 8 channels and the PL080 have just 2 */
  1754. static struct vendor_data vendor_pl080 = {
  1755. .channels = 8,
  1756. .dualmaster = true,
  1757. };
  1758. static struct vendor_data vendor_pl081 = {
  1759. .channels = 2,
  1760. .dualmaster = false,
  1761. };
  1762. static struct amba_id pl08x_ids[] = {
  1763. /* PL080 */
  1764. {
  1765. .id = 0x00041080,
  1766. .mask = 0x000fffff,
  1767. .data = &vendor_pl080,
  1768. },
  1769. /* PL081 */
  1770. {
  1771. .id = 0x00041081,
  1772. .mask = 0x000fffff,
  1773. .data = &vendor_pl081,
  1774. },
  1775. /* Nomadik 8815 PL080 variant */
  1776. {
  1777. .id = 0x00280880,
  1778. .mask = 0x00ffffff,
  1779. .data = &vendor_pl080,
  1780. },
  1781. { 0, 0 },
  1782. };
  1783. static struct amba_driver pl08x_amba_driver = {
  1784. .drv.name = DRIVER_NAME,
  1785. .id_table = pl08x_ids,
  1786. .probe = pl08x_probe,
  1787. };
  1788. static int __init pl08x_init(void)
  1789. {
  1790. int retval;
  1791. retval = amba_driver_register(&pl08x_amba_driver);
  1792. if (retval)
  1793. printk(KERN_WARNING DRIVER_NAME
  1794. "failed to register as an AMBA device (%d)\n",
  1795. retval);
  1796. return retval;
  1797. }
  1798. subsys_initcall(pl08x_init);