amba-pl08x.c 55 KB

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