amba-pl08x.c 55 KB

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