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/processor.h>
  77. #include <asm/cacheflush.h>
  78. #define DRIVER_NAME "pl08xdmac"
  79. /**
  80. * struct vendor_data - vendor-specific config parameters
  81. * for PL08x derivatives
  82. * @name: the name of this specific variant
  83. * @channels: the number of channels available in this variant
  84. * @dualmaster: whether this version supports dual AHB masters
  85. * or not.
  86. */
  87. struct vendor_data {
  88. char *name;
  89. u8 channels;
  90. bool dualmaster;
  91. };
  92. /*
  93. * PL08X private data structures
  94. * An LLI struct - see PL08x TRM. Note that next uses bit[0] as a bus bit,
  95. * start & end do not - their bus bit info is in cctl.
  96. */
  97. struct lli {
  98. dma_addr_t src;
  99. dma_addr_t dst;
  100. dma_addr_t next;
  101. u32 cctl;
  102. };
  103. /**
  104. * struct pl08x_driver_data - the local state holder for the PL08x
  105. * @slave: slave engine for this instance
  106. * @memcpy: memcpy engine for this instance
  107. * @base: virtual memory base (remapped) for the PL08x
  108. * @adev: the corresponding AMBA (PrimeCell) bus entry
  109. * @vd: vendor data for this PL08x variant
  110. * @pd: platform data passed in from the platform/machine
  111. * @phy_chans: array of data for the physical channels
  112. * @pool: a pool for the LLI descriptors
  113. * @pool_ctr: counter of LLIs in the pool
  114. * @lock: a spinlock for this struct
  115. */
  116. struct pl08x_driver_data {
  117. struct dma_device slave;
  118. struct dma_device memcpy;
  119. void __iomem *base;
  120. struct amba_device *adev;
  121. struct vendor_data *vd;
  122. struct pl08x_platform_data *pd;
  123. struct pl08x_phy_chan *phy_chans;
  124. struct dma_pool *pool;
  125. int pool_ctr;
  126. spinlock_t lock;
  127. };
  128. /*
  129. * PL08X specific defines
  130. */
  131. /*
  132. * Memory boundaries: the manual for PL08x says that the controller
  133. * cannot read past a 1KiB boundary, so these defines are used to
  134. * create transfer LLIs that do not cross such boundaries.
  135. */
  136. #define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */
  137. #define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)
  138. /* Minimum period between work queue runs */
  139. #define PL08X_WQ_PERIODMIN 20
  140. /* Size (bytes) of each LLI buffer allocated for one transfer */
  141. # define PL08X_LLI_TSFR_SIZE 0x2000
  142. /* Maximum times we call dma_pool_alloc on this pool without freeing */
  143. #define PL08X_MAX_ALLOCS 0x40
  144. #define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct lli))
  145. #define PL08X_ALIGN 8
  146. static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
  147. {
  148. return container_of(chan, struct pl08x_dma_chan, chan);
  149. }
  150. /*
  151. * Physical channel handling
  152. */
  153. /* Whether a certain channel is busy or not */
  154. static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
  155. {
  156. unsigned int val;
  157. val = readl(ch->base + PL080_CH_CONFIG);
  158. return val & PL080_CONFIG_ACTIVE;
  159. }
  160. /*
  161. * Set the initial DMA register values i.e. those for the first LLI
  162. * The next LLI pointer and the configuration interrupt bit have
  163. * been set when the LLIs were constructed
  164. */
  165. static void pl08x_set_cregs(struct pl08x_driver_data *pl08x,
  166. struct pl08x_phy_chan *ch)
  167. {
  168. /* Wait for channel inactive */
  169. while (pl08x_phy_channel_busy(ch))
  170. ;
  171. dev_vdbg(&pl08x->adev->dev,
  172. "WRITE channel %d: csrc=%08x, cdst=%08x, "
  173. "cctl=%08x, clli=%08x, ccfg=%08x\n",
  174. ch->id,
  175. ch->csrc,
  176. ch->cdst,
  177. ch->cctl,
  178. ch->clli,
  179. ch->ccfg);
  180. writel(ch->csrc, ch->base + PL080_CH_SRC_ADDR);
  181. writel(ch->cdst, ch->base + PL080_CH_DST_ADDR);
  182. writel(ch->clli, ch->base + PL080_CH_LLI);
  183. writel(ch->cctl, ch->base + PL080_CH_CONTROL);
  184. writel(ch->ccfg, ch->base + PL080_CH_CONFIG);
  185. }
  186. static inline void pl08x_config_phychan_for_txd(struct pl08x_dma_chan *plchan)
  187. {
  188. struct pl08x_channel_data *cd = plchan->cd;
  189. struct pl08x_phy_chan *phychan = plchan->phychan;
  190. struct pl08x_txd *txd = plchan->at;
  191. /* Copy the basic control register calculated at transfer config */
  192. phychan->csrc = txd->csrc;
  193. phychan->cdst = txd->cdst;
  194. phychan->clli = txd->clli;
  195. phychan->cctl = txd->cctl;
  196. /* Assign the signal to the proper control registers */
  197. phychan->ccfg = cd->ccfg;
  198. phychan->ccfg &= ~PL080_CONFIG_SRC_SEL_MASK;
  199. phychan->ccfg &= ~PL080_CONFIG_DST_SEL_MASK;
  200. /* If it wasn't set from AMBA, ignore it */
  201. if (txd->direction == DMA_TO_DEVICE)
  202. /* Select signal as destination */
  203. phychan->ccfg |=
  204. (phychan->signal << PL080_CONFIG_DST_SEL_SHIFT);
  205. else if (txd->direction == DMA_FROM_DEVICE)
  206. /* Select signal as source */
  207. phychan->ccfg |=
  208. (phychan->signal << PL080_CONFIG_SRC_SEL_SHIFT);
  209. /* Always enable error interrupts */
  210. phychan->ccfg |= PL080_CONFIG_ERR_IRQ_MASK;
  211. /* Always enable terminal interrupts */
  212. phychan->ccfg |= PL080_CONFIG_TC_IRQ_MASK;
  213. }
  214. /*
  215. * Enable the DMA channel
  216. * Assumes all other configuration bits have been set
  217. * as desired before this code is called
  218. */
  219. static void pl08x_enable_phy_chan(struct pl08x_driver_data *pl08x,
  220. struct pl08x_phy_chan *ch)
  221. {
  222. u32 val;
  223. /*
  224. * Do not access config register until channel shows as disabled
  225. */
  226. while (readl(pl08x->base + PL080_EN_CHAN) & (1 << ch->id))
  227. ;
  228. /*
  229. * Do not access config register until channel shows as inactive
  230. */
  231. val = readl(ch->base + PL080_CH_CONFIG);
  232. while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
  233. val = readl(ch->base + PL080_CH_CONFIG);
  234. writel(val | PL080_CONFIG_ENABLE, ch->base + PL080_CH_CONFIG);
  235. }
  236. /*
  237. * Overall DMAC remains enabled always.
  238. *
  239. * Disabling individual channels could lose data.
  240. *
  241. * Disable the peripheral DMA after disabling the DMAC
  242. * in order to allow the DMAC FIFO to drain, and
  243. * hence allow the channel to show inactive
  244. *
  245. */
  246. static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
  247. {
  248. u32 val;
  249. /* Set the HALT bit and wait for the FIFO to drain */
  250. val = readl(ch->base + PL080_CH_CONFIG);
  251. val |= PL080_CONFIG_HALT;
  252. writel(val, ch->base + PL080_CH_CONFIG);
  253. /* Wait for channel inactive */
  254. while (pl08x_phy_channel_busy(ch))
  255. ;
  256. }
  257. static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
  258. {
  259. u32 val;
  260. /* Clear the HALT bit */
  261. val = readl(ch->base + PL080_CH_CONFIG);
  262. val &= ~PL080_CONFIG_HALT;
  263. writel(val, ch->base + PL080_CH_CONFIG);
  264. }
  265. /* Stops the channel */
  266. static void pl08x_stop_phy_chan(struct pl08x_phy_chan *ch)
  267. {
  268. u32 val;
  269. pl08x_pause_phy_chan(ch);
  270. /* Disable channel */
  271. val = readl(ch->base + PL080_CH_CONFIG);
  272. val &= ~PL080_CONFIG_ENABLE;
  273. val &= ~PL080_CONFIG_ERR_IRQ_MASK;
  274. val &= ~PL080_CONFIG_TC_IRQ_MASK;
  275. writel(val, ch->base + PL080_CH_CONFIG);
  276. }
  277. static inline u32 get_bytes_in_cctl(u32 cctl)
  278. {
  279. /* The source width defines the number of bytes */
  280. u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
  281. switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
  282. case PL080_WIDTH_8BIT:
  283. break;
  284. case PL080_WIDTH_16BIT:
  285. bytes *= 2;
  286. break;
  287. case PL080_WIDTH_32BIT:
  288. bytes *= 4;
  289. break;
  290. }
  291. return bytes;
  292. }
  293. /* The channel should be paused when calling this */
  294. static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
  295. {
  296. struct pl08x_phy_chan *ch;
  297. struct pl08x_txd *txdi = NULL;
  298. struct pl08x_txd *txd;
  299. unsigned long flags;
  300. u32 bytes = 0;
  301. spin_lock_irqsave(&plchan->lock, flags);
  302. ch = plchan->phychan;
  303. txd = plchan->at;
  304. /*
  305. * Next follow the LLIs to get the number of pending bytes in the
  306. * currently active transaction.
  307. */
  308. if (ch && txd) {
  309. struct lli *llis_va = txd->llis_va;
  310. struct lli *llis_bus = (struct lli *) txd->llis_bus;
  311. u32 clli = readl(ch->base + PL080_CH_LLI);
  312. /* First get the bytes in the current active LLI */
  313. bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
  314. if (clli) {
  315. int i = 0;
  316. /* Forward to the LLI pointed to by clli */
  317. while ((clli != (u32) &(llis_bus[i])) &&
  318. (i < MAX_NUM_TSFR_LLIS))
  319. i++;
  320. while (clli) {
  321. bytes += get_bytes_in_cctl(llis_va[i].cctl);
  322. /*
  323. * A LLI pointer of 0 terminates the LLI list
  324. */
  325. clli = llis_va[i].next;
  326. i++;
  327. }
  328. }
  329. }
  330. /* Sum up all queued transactions */
  331. if (!list_empty(&plchan->desc_list)) {
  332. list_for_each_entry(txdi, &plchan->desc_list, node) {
  333. bytes += txdi->len;
  334. }
  335. }
  336. spin_unlock_irqrestore(&plchan->lock, flags);
  337. return bytes;
  338. }
  339. /*
  340. * Allocate a physical channel for a virtual channel
  341. */
  342. static struct pl08x_phy_chan *
  343. pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
  344. struct pl08x_dma_chan *virt_chan)
  345. {
  346. struct pl08x_phy_chan *ch = NULL;
  347. unsigned long flags;
  348. int i;
  349. /*
  350. * Try to locate a physical channel to be used for
  351. * this transfer. If all are taken return NULL and
  352. * the requester will have to cope by using some fallback
  353. * PIO mode or retrying later.
  354. */
  355. for (i = 0; i < pl08x->vd->channels; i++) {
  356. ch = &pl08x->phy_chans[i];
  357. spin_lock_irqsave(&ch->lock, flags);
  358. if (!ch->serving) {
  359. ch->serving = virt_chan;
  360. ch->signal = -1;
  361. spin_unlock_irqrestore(&ch->lock, flags);
  362. break;
  363. }
  364. spin_unlock_irqrestore(&ch->lock, flags);
  365. }
  366. if (i == pl08x->vd->channels) {
  367. /* No physical channel available, cope with it */
  368. return NULL;
  369. }
  370. return ch;
  371. }
  372. static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
  373. struct pl08x_phy_chan *ch)
  374. {
  375. unsigned long flags;
  376. /* Stop the channel and clear its interrupts */
  377. pl08x_stop_phy_chan(ch);
  378. writel((1 << ch->id), pl08x->base + PL080_ERR_CLEAR);
  379. writel((1 << ch->id), pl08x->base + PL080_TC_CLEAR);
  380. /* Mark it as free */
  381. spin_lock_irqsave(&ch->lock, flags);
  382. ch->serving = NULL;
  383. spin_unlock_irqrestore(&ch->lock, flags);
  384. }
  385. /*
  386. * LLI handling
  387. */
  388. static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
  389. {
  390. switch (coded) {
  391. case PL080_WIDTH_8BIT:
  392. return 1;
  393. case PL080_WIDTH_16BIT:
  394. return 2;
  395. case PL080_WIDTH_32BIT:
  396. return 4;
  397. default:
  398. break;
  399. }
  400. BUG();
  401. return 0;
  402. }
  403. static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
  404. u32 tsize)
  405. {
  406. u32 retbits = cctl;
  407. /* Remove all src, dst and transfer size bits */
  408. retbits &= ~PL080_CONTROL_DWIDTH_MASK;
  409. retbits &= ~PL080_CONTROL_SWIDTH_MASK;
  410. retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
  411. /* Then set the bits according to the parameters */
  412. switch (srcwidth) {
  413. case 1:
  414. retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
  415. break;
  416. case 2:
  417. retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
  418. break;
  419. case 4:
  420. retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
  421. break;
  422. default:
  423. BUG();
  424. break;
  425. }
  426. switch (dstwidth) {
  427. case 1:
  428. retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
  429. break;
  430. case 2:
  431. retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
  432. break;
  433. case 4:
  434. retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
  435. break;
  436. default:
  437. BUG();
  438. break;
  439. }
  440. retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
  441. return retbits;
  442. }
  443. /*
  444. * Autoselect a master bus to use for the transfer
  445. * this prefers the destination bus if both available
  446. * if fixed address on one bus the other will be chosen
  447. */
  448. void pl08x_choose_master_bus(struct pl08x_bus_data *src_bus,
  449. struct pl08x_bus_data *dst_bus, struct pl08x_bus_data **mbus,
  450. struct pl08x_bus_data **sbus, u32 cctl)
  451. {
  452. if (!(cctl & PL080_CONTROL_DST_INCR)) {
  453. *mbus = src_bus;
  454. *sbus = dst_bus;
  455. } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
  456. *mbus = dst_bus;
  457. *sbus = src_bus;
  458. } else {
  459. if (dst_bus->buswidth == 4) {
  460. *mbus = dst_bus;
  461. *sbus = src_bus;
  462. } else if (src_bus->buswidth == 4) {
  463. *mbus = src_bus;
  464. *sbus = dst_bus;
  465. } else if (dst_bus->buswidth == 2) {
  466. *mbus = dst_bus;
  467. *sbus = src_bus;
  468. } else if (src_bus->buswidth == 2) {
  469. *mbus = src_bus;
  470. *sbus = dst_bus;
  471. } else {
  472. /* src_bus->buswidth == 1 */
  473. *mbus = dst_bus;
  474. *sbus = src_bus;
  475. }
  476. }
  477. }
  478. /*
  479. * Fills in one LLI for a certain transfer descriptor
  480. * and advance the counter
  481. */
  482. int pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
  483. struct pl08x_txd *txd, int num_llis, int len,
  484. u32 cctl, u32 *remainder)
  485. {
  486. struct lli *llis_va = txd->llis_va;
  487. struct lli *llis_bus = (struct lli *) txd->llis_bus;
  488. BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
  489. llis_va[num_llis].cctl = cctl;
  490. llis_va[num_llis].src = txd->srcbus.addr;
  491. llis_va[num_llis].dst = txd->dstbus.addr;
  492. /*
  493. * On versions with dual masters, you can optionally AND on
  494. * PL080_LLI_LM_AHB2 to the LLI to tell the hardware to read
  495. * in new LLIs with that controller, but we always try to
  496. * choose AHB1 to point into memory. The idea is to have AHB2
  497. * fixed on the peripheral and AHB1 messing around in the
  498. * memory. So we don't manipulate this bit currently.
  499. */
  500. llis_va[num_llis].next =
  501. (dma_addr_t)((u32) &(llis_bus[num_llis + 1]));
  502. if (cctl & PL080_CONTROL_SRC_INCR)
  503. txd->srcbus.addr += len;
  504. if (cctl & PL080_CONTROL_DST_INCR)
  505. txd->dstbus.addr += len;
  506. *remainder -= len;
  507. return num_llis + 1;
  508. }
  509. /*
  510. * Return number of bytes to fill to boundary, or len
  511. */
  512. static inline u32 pl08x_pre_boundary(u32 addr, u32 len)
  513. {
  514. u32 boundary;
  515. boundary = ((addr >> PL08X_BOUNDARY_SHIFT) + 1)
  516. << PL08X_BOUNDARY_SHIFT;
  517. if (boundary < addr + len)
  518. return boundary - addr;
  519. else
  520. return len;
  521. }
  522. /*
  523. * This fills in the table of LLIs for the transfer descriptor
  524. * Note that we assume we never have to change the burst sizes
  525. * Return 0 for error
  526. */
  527. static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
  528. struct pl08x_txd *txd)
  529. {
  530. struct pl08x_channel_data *cd = txd->cd;
  531. struct pl08x_bus_data *mbus, *sbus;
  532. u32 remainder;
  533. int num_llis = 0;
  534. u32 cctl;
  535. int max_bytes_per_lli;
  536. int total_bytes = 0;
  537. struct lli *llis_va;
  538. struct lli *llis_bus;
  539. if (!txd) {
  540. dev_err(&pl08x->adev->dev, "%s no descriptor\n", __func__);
  541. return 0;
  542. }
  543. txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
  544. &txd->llis_bus);
  545. if (!txd->llis_va) {
  546. dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
  547. return 0;
  548. }
  549. pl08x->pool_ctr++;
  550. /*
  551. * Initialize bus values for this transfer
  552. * from the passed optimal values
  553. */
  554. if (!cd) {
  555. dev_err(&pl08x->adev->dev, "%s no channel data\n", __func__);
  556. return 0;
  557. }
  558. /* Get the default CCTL from the platform data */
  559. cctl = cd->cctl;
  560. /*
  561. * On the PL080 we have two bus masters and we
  562. * should select one for source and one for
  563. * destination. We try to use AHB2 for the
  564. * bus which does not increment (typically the
  565. * peripheral) else we just choose something.
  566. */
  567. cctl &= ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
  568. if (pl08x->vd->dualmaster) {
  569. if (cctl & PL080_CONTROL_SRC_INCR)
  570. /* Source increments, use AHB2 for destination */
  571. cctl |= PL080_CONTROL_DST_AHB2;
  572. else if (cctl & PL080_CONTROL_DST_INCR)
  573. /* Destination increments, use AHB2 for source */
  574. cctl |= PL080_CONTROL_SRC_AHB2;
  575. else
  576. /* Just pick something, source AHB1 dest AHB2 */
  577. cctl |= PL080_CONTROL_DST_AHB2;
  578. }
  579. /* Find maximum width of the source bus */
  580. txd->srcbus.maxwidth =
  581. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
  582. PL080_CONTROL_SWIDTH_SHIFT);
  583. /* Find maximum width of the destination bus */
  584. txd->dstbus.maxwidth =
  585. pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
  586. PL080_CONTROL_DWIDTH_SHIFT);
  587. /* Set up the bus widths to the maximum */
  588. txd->srcbus.buswidth = txd->srcbus.maxwidth;
  589. txd->dstbus.buswidth = txd->dstbus.maxwidth;
  590. dev_vdbg(&pl08x->adev->dev,
  591. "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
  592. __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
  593. /*
  594. * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
  595. */
  596. max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
  597. PL080_CONTROL_TRANSFER_SIZE_MASK;
  598. dev_vdbg(&pl08x->adev->dev,
  599. "%s max bytes per lli = %d\n",
  600. __func__, max_bytes_per_lli);
  601. /* We need to count this down to zero */
  602. remainder = txd->len;
  603. dev_vdbg(&pl08x->adev->dev,
  604. "%s remainder = %d\n",
  605. __func__, remainder);
  606. /*
  607. * Choose bus to align to
  608. * - prefers destination bus if both available
  609. * - if fixed address on one bus chooses other
  610. * - modifies cctl to choose an appropriate master
  611. */
  612. pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
  613. &mbus, &sbus, cctl);
  614. /*
  615. * The lowest bit of the LLI register
  616. * is also used to indicate which master to
  617. * use for reading the LLIs.
  618. */
  619. if (txd->len < mbus->buswidth) {
  620. /*
  621. * Less than a bus width available
  622. * - send as single bytes
  623. */
  624. while (remainder) {
  625. dev_vdbg(&pl08x->adev->dev,
  626. "%s single byte LLIs for a transfer of "
  627. "less than a bus width (remain %08x)\n",
  628. __func__, remainder);
  629. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  630. num_llis =
  631. pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,
  632. cctl, &remainder);
  633. total_bytes++;
  634. }
  635. } else {
  636. /*
  637. * Make one byte LLIs until master bus is aligned
  638. * - slave will then be aligned also
  639. */
  640. while ((mbus->addr) % (mbus->buswidth)) {
  641. dev_vdbg(&pl08x->adev->dev,
  642. "%s adjustment lli for less than bus width "
  643. "(remain %08x)\n",
  644. __func__, remainder);
  645. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  646. num_llis = pl08x_fill_lli_for_desc
  647. (pl08x, txd, num_llis, 1, cctl, &remainder);
  648. total_bytes++;
  649. }
  650. /*
  651. * Master now aligned
  652. * - if slave is not then we must set its width down
  653. */
  654. if (sbus->addr % sbus->buswidth) {
  655. dev_dbg(&pl08x->adev->dev,
  656. "%s set down bus width to one byte\n",
  657. __func__);
  658. sbus->buswidth = 1;
  659. }
  660. /*
  661. * Make largest possible LLIs until less than one bus
  662. * width left
  663. */
  664. while (remainder > (mbus->buswidth - 1)) {
  665. int lli_len, target_len;
  666. int tsize;
  667. int odd_bytes;
  668. /*
  669. * If enough left try to send max possible,
  670. * otherwise try to send the remainder
  671. */
  672. target_len = remainder;
  673. if (remainder > max_bytes_per_lli)
  674. target_len = max_bytes_per_lli;
  675. /*
  676. * Set bus lengths for incrementing buses
  677. * to number of bytes which fill to next memory
  678. * boundary
  679. */
  680. if (cctl & PL080_CONTROL_SRC_INCR)
  681. txd->srcbus.fill_bytes =
  682. pl08x_pre_boundary(
  683. txd->srcbus.addr,
  684. remainder);
  685. else
  686. txd->srcbus.fill_bytes =
  687. max_bytes_per_lli;
  688. if (cctl & PL080_CONTROL_DST_INCR)
  689. txd->dstbus.fill_bytes =
  690. pl08x_pre_boundary(
  691. txd->dstbus.addr,
  692. remainder);
  693. else
  694. txd->dstbus.fill_bytes =
  695. max_bytes_per_lli;
  696. /*
  697. * Find the nearest
  698. */
  699. lli_len = min(txd->srcbus.fill_bytes,
  700. txd->dstbus.fill_bytes);
  701. BUG_ON(lli_len > remainder);
  702. if (lli_len <= 0) {
  703. dev_err(&pl08x->adev->dev,
  704. "%s lli_len is %d, <= 0\n",
  705. __func__, lli_len);
  706. return 0;
  707. }
  708. if (lli_len == target_len) {
  709. /*
  710. * Can send what we wanted
  711. */
  712. /*
  713. * Maintain alignment
  714. */
  715. lli_len = (lli_len/mbus->buswidth) *
  716. mbus->buswidth;
  717. odd_bytes = 0;
  718. } else {
  719. /*
  720. * So now we know how many bytes to transfer
  721. * to get to the nearest boundary
  722. * The next LLI will past the boundary
  723. * - however we may be working to a boundary
  724. * on the slave bus
  725. * We need to ensure the master stays aligned
  726. */
  727. odd_bytes = lli_len % mbus->buswidth;
  728. /*
  729. * - and that we are working in multiples
  730. * of the bus widths
  731. */
  732. lli_len -= odd_bytes;
  733. }
  734. if (lli_len) {
  735. /*
  736. * Check against minimum bus alignment:
  737. * Calculate actual transfer size in relation
  738. * to bus width an get a maximum remainder of
  739. * the smallest bus width - 1
  740. */
  741. /* FIXME: use round_down()? */
  742. tsize = lli_len / min(mbus->buswidth,
  743. sbus->buswidth);
  744. lli_len = tsize * min(mbus->buswidth,
  745. sbus->buswidth);
  746. if (target_len != lli_len) {
  747. dev_vdbg(&pl08x->adev->dev,
  748. "%s can't send what we want. Desired %08x, lli of %08x bytes in txd of %08x\n",
  749. __func__, target_len, lli_len, txd->len);
  750. }
  751. cctl = pl08x_cctl_bits(cctl,
  752. txd->srcbus.buswidth,
  753. txd->dstbus.buswidth,
  754. tsize);
  755. dev_vdbg(&pl08x->adev->dev,
  756. "%s fill lli with single lli chunk of size %08x (remainder %08x)\n",
  757. __func__, lli_len, remainder);
  758. num_llis = pl08x_fill_lli_for_desc(pl08x, txd,
  759. num_llis, lli_len, cctl,
  760. &remainder);
  761. total_bytes += lli_len;
  762. }
  763. if (odd_bytes) {
  764. /*
  765. * Creep past the boundary,
  766. * maintaining master alignment
  767. */
  768. int j;
  769. for (j = 0; (j < mbus->buswidth)
  770. && (remainder); j++) {
  771. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  772. dev_vdbg(&pl08x->adev->dev,
  773. "%s align with boundary, single byte (remain %08x)\n",
  774. __func__, remainder);
  775. num_llis =
  776. pl08x_fill_lli_for_desc(pl08x,
  777. txd, num_llis, 1,
  778. cctl, &remainder);
  779. total_bytes++;
  780. }
  781. }
  782. }
  783. /*
  784. * Send any odd bytes
  785. */
  786. if (remainder < 0) {
  787. dev_err(&pl08x->adev->dev, "%s remainder not fitted 0x%08x bytes\n",
  788. __func__, remainder);
  789. return 0;
  790. }
  791. while (remainder) {
  792. cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
  793. dev_vdbg(&pl08x->adev->dev,
  794. "%s align with boundary, single odd byte (remain %d)\n",
  795. __func__, remainder);
  796. num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,
  797. 1, cctl, &remainder);
  798. total_bytes++;
  799. }
  800. }
  801. if (total_bytes != txd->len) {
  802. dev_err(&pl08x->adev->dev,
  803. "%s size of encoded lli:s don't match total txd, transferred 0x%08x from size 0x%08x\n",
  804. __func__, total_bytes, txd->len);
  805. return 0;
  806. }
  807. if (num_llis >= MAX_NUM_TSFR_LLIS) {
  808. dev_err(&pl08x->adev->dev,
  809. "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
  810. __func__, (u32) MAX_NUM_TSFR_LLIS);
  811. return 0;
  812. }
  813. /*
  814. * Decide whether this is a loop or a terminated transfer
  815. */
  816. llis_va = txd->llis_va;
  817. llis_bus = (struct lli *) txd->llis_bus;
  818. if (cd->circular_buffer) {
  819. /*
  820. * Loop the circular buffer so that the next element
  821. * points back to the beginning of the LLI.
  822. */
  823. llis_va[num_llis - 1].next =
  824. (dma_addr_t)((unsigned int)&(llis_bus[0]));
  825. } else {
  826. /*
  827. * On non-circular buffers, the final LLI terminates
  828. * the LLI.
  829. */
  830. llis_va[num_llis - 1].next = 0;
  831. /*
  832. * The final LLI element shall also fire an interrupt
  833. */
  834. llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
  835. }
  836. /* Now store the channel register values */
  837. txd->csrc = llis_va[0].src;
  838. txd->cdst = llis_va[0].dst;
  839. if (num_llis > 1)
  840. txd->clli = llis_va[0].next;
  841. else
  842. txd->clli = 0;
  843. txd->cctl = llis_va[0].cctl;
  844. /* ccfg will be set at physical channel allocation time */
  845. #ifdef VERBOSE_DEBUG
  846. {
  847. int i;
  848. for (i = 0; i < num_llis; i++) {
  849. dev_vdbg(&pl08x->adev->dev,
  850. "lli %d @%p: csrc=%08x, cdst=%08x, cctl=%08x, clli=%08x\n",
  851. i,
  852. &llis_va[i],
  853. llis_va[i].src,
  854. llis_va[i].dst,
  855. llis_va[i].cctl,
  856. llis_va[i].next
  857. );
  858. }
  859. }
  860. #endif
  861. return num_llis;
  862. }
  863. /* You should call this with the struct pl08x lock held */
  864. static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
  865. struct pl08x_txd *txd)
  866. {
  867. if (!txd)
  868. dev_err(&pl08x->adev->dev,
  869. "%s no descriptor to free\n",
  870. __func__);
  871. /* Free the LLI */
  872. dma_pool_free(pl08x->pool, txd->llis_va,
  873. txd->llis_bus);
  874. pl08x->pool_ctr--;
  875. kfree(txd);
  876. }
  877. static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
  878. struct pl08x_dma_chan *plchan)
  879. {
  880. struct pl08x_txd *txdi = NULL;
  881. struct pl08x_txd *next;
  882. if (!list_empty(&plchan->desc_list)) {
  883. list_for_each_entry_safe(txdi,
  884. next, &plchan->desc_list, node) {
  885. list_del(&txdi->node);
  886. pl08x_free_txd(pl08x, txdi);
  887. }
  888. }
  889. }
  890. /*
  891. * The DMA ENGINE API
  892. */
  893. static int pl08x_alloc_chan_resources(struct dma_chan *chan)
  894. {
  895. return 0;
  896. }
  897. static void pl08x_free_chan_resources(struct dma_chan *chan)
  898. {
  899. }
  900. /*
  901. * This should be called with the channel plchan->lock held
  902. */
  903. static int prep_phy_channel(struct pl08x_dma_chan *plchan,
  904. struct pl08x_txd *txd)
  905. {
  906. struct pl08x_driver_data *pl08x = plchan->host;
  907. struct pl08x_phy_chan *ch;
  908. int ret;
  909. /* Check if we already have a channel */
  910. if (plchan->phychan)
  911. return 0;
  912. ch = pl08x_get_phy_channel(pl08x, plchan);
  913. if (!ch) {
  914. /* No physical channel available, cope with it */
  915. dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
  916. return -EBUSY;
  917. }
  918. /*
  919. * OK we have a physical channel: for memcpy() this is all we
  920. * need, but for slaves the physical signals may be muxed!
  921. * Can the platform allow us to use this channel?
  922. */
  923. if (plchan->slave &&
  924. ch->signal < 0 &&
  925. pl08x->pd->get_signal) {
  926. ret = pl08x->pd->get_signal(plchan);
  927. if (ret < 0) {
  928. dev_dbg(&pl08x->adev->dev,
  929. "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
  930. ch->id, plchan->name);
  931. /* Release physical channel & return */
  932. pl08x_put_phy_channel(pl08x, ch);
  933. return -EBUSY;
  934. }
  935. ch->signal = ret;
  936. }
  937. dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
  938. ch->id,
  939. ch->signal,
  940. plchan->name);
  941. plchan->phychan = ch;
  942. return 0;
  943. }
  944. static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
  945. {
  946. struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
  947. plchan->chan.cookie += 1;
  948. if (plchan->chan.cookie < 0)
  949. plchan->chan.cookie = 1;
  950. tx->cookie = plchan->chan.cookie;
  951. /* This unlock follows the lock in the prep() function */
  952. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  953. return tx->cookie;
  954. }
  955. static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
  956. struct dma_chan *chan, unsigned long flags)
  957. {
  958. struct dma_async_tx_descriptor *retval = NULL;
  959. return retval;
  960. }
  961. /*
  962. * Code accessing dma_async_is_complete() in a tight loop
  963. * may give problems - could schedule where indicated.
  964. * If slaves are relying on interrupts to signal completion this
  965. * function must not be called with interrupts disabled
  966. */
  967. static enum dma_status
  968. pl08x_dma_tx_status(struct dma_chan *chan,
  969. dma_cookie_t cookie,
  970. struct dma_tx_state *txstate)
  971. {
  972. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  973. dma_cookie_t last_used;
  974. dma_cookie_t last_complete;
  975. enum dma_status ret;
  976. u32 bytesleft = 0;
  977. last_used = plchan->chan.cookie;
  978. last_complete = plchan->lc;
  979. ret = dma_async_is_complete(cookie, last_complete, last_used);
  980. if (ret == DMA_SUCCESS) {
  981. dma_set_tx_state(txstate, last_complete, last_used, 0);
  982. return ret;
  983. }
  984. /*
  985. * schedule(); could be inserted here
  986. */
  987. /*
  988. * This cookie not complete yet
  989. */
  990. last_used = plchan->chan.cookie;
  991. last_complete = plchan->lc;
  992. /* Get number of bytes left in the active transactions and queue */
  993. bytesleft = pl08x_getbytes_chan(plchan);
  994. dma_set_tx_state(txstate, last_complete, last_used,
  995. bytesleft);
  996. if (plchan->state == PL08X_CHAN_PAUSED)
  997. return DMA_PAUSED;
  998. /* Whether waiting or running, we're in progress */
  999. return DMA_IN_PROGRESS;
  1000. }
  1001. /* PrimeCell DMA extension */
  1002. struct burst_table {
  1003. int burstwords;
  1004. u32 reg;
  1005. };
  1006. static const struct burst_table burst_sizes[] = {
  1007. {
  1008. .burstwords = 256,
  1009. .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1010. (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
  1011. },
  1012. {
  1013. .burstwords = 128,
  1014. .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1015. (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
  1016. },
  1017. {
  1018. .burstwords = 64,
  1019. .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1020. (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
  1021. },
  1022. {
  1023. .burstwords = 32,
  1024. .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1025. (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
  1026. },
  1027. {
  1028. .burstwords = 16,
  1029. .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1030. (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
  1031. },
  1032. {
  1033. .burstwords = 8,
  1034. .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1035. (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
  1036. },
  1037. {
  1038. .burstwords = 4,
  1039. .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1040. (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
  1041. },
  1042. {
  1043. .burstwords = 1,
  1044. .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1045. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
  1046. },
  1047. };
  1048. static void dma_set_runtime_config(struct dma_chan *chan,
  1049. struct dma_slave_config *config)
  1050. {
  1051. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1052. struct pl08x_driver_data *pl08x = plchan->host;
  1053. struct pl08x_channel_data *cd = plchan->cd;
  1054. enum dma_slave_buswidth addr_width;
  1055. u32 maxburst;
  1056. u32 cctl = 0;
  1057. /* Mask out all except src and dst channel */
  1058. u32 ccfg = cd->ccfg & 0x000003DEU;
  1059. int i;
  1060. /* Transfer direction */
  1061. plchan->runtime_direction = config->direction;
  1062. if (config->direction == DMA_TO_DEVICE) {
  1063. plchan->runtime_addr = config->dst_addr;
  1064. cctl |= PL080_CONTROL_SRC_INCR;
  1065. ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1066. addr_width = config->dst_addr_width;
  1067. maxburst = config->dst_maxburst;
  1068. } else if (config->direction == DMA_FROM_DEVICE) {
  1069. plchan->runtime_addr = config->src_addr;
  1070. cctl |= PL080_CONTROL_DST_INCR;
  1071. ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1072. addr_width = config->src_addr_width;
  1073. maxburst = config->src_maxburst;
  1074. } else {
  1075. dev_err(&pl08x->adev->dev,
  1076. "bad runtime_config: alien transfer direction\n");
  1077. return;
  1078. }
  1079. switch (addr_width) {
  1080. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1081. cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1082. (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1083. break;
  1084. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1085. cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1086. (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1087. break;
  1088. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1089. cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1090. (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1091. break;
  1092. default:
  1093. dev_err(&pl08x->adev->dev,
  1094. "bad runtime_config: alien address width\n");
  1095. return;
  1096. }
  1097. /*
  1098. * Now decide on a maxburst:
  1099. * If this channel will only request single transfers, set this
  1100. * down to ONE element. Also select one element if no maxburst
  1101. * is specified.
  1102. */
  1103. if (plchan->cd->single || maxburst == 0) {
  1104. cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1105. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
  1106. } else {
  1107. for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
  1108. if (burst_sizes[i].burstwords <= maxburst)
  1109. break;
  1110. cctl |= burst_sizes[i].reg;
  1111. }
  1112. /* Access the cell in privileged mode, non-bufferable, non-cacheable */
  1113. cctl &= ~PL080_CONTROL_PROT_MASK;
  1114. cctl |= PL080_CONTROL_PROT_SYS;
  1115. /* Modify the default channel data to fit PrimeCell request */
  1116. cd->cctl = cctl;
  1117. cd->ccfg = ccfg;
  1118. dev_dbg(&pl08x->adev->dev,
  1119. "configured channel %s (%s) for %s, data width %d, "
  1120. "maxburst %d words, LE, CCTL=%08x, CCFG=%08x\n",
  1121. dma_chan_name(chan), plchan->name,
  1122. (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
  1123. addr_width,
  1124. maxburst,
  1125. cctl, ccfg);
  1126. }
  1127. /*
  1128. * Slave transactions callback to the slave device to allow
  1129. * synchronization of slave DMA signals with the DMAC enable
  1130. */
  1131. static void pl08x_issue_pending(struct dma_chan *chan)
  1132. {
  1133. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1134. struct pl08x_driver_data *pl08x = plchan->host;
  1135. unsigned long flags;
  1136. spin_lock_irqsave(&plchan->lock, flags);
  1137. /* Something is already active */
  1138. if (plchan->at) {
  1139. spin_unlock_irqrestore(&plchan->lock, flags);
  1140. return;
  1141. }
  1142. /* Didn't get a physical channel so waiting for it ... */
  1143. if (plchan->state == PL08X_CHAN_WAITING)
  1144. return;
  1145. /* Take the first element in the queue and execute it */
  1146. if (!list_empty(&plchan->desc_list)) {
  1147. struct pl08x_txd *next;
  1148. next = list_first_entry(&plchan->desc_list,
  1149. struct pl08x_txd,
  1150. node);
  1151. list_del(&next->node);
  1152. plchan->at = next;
  1153. plchan->state = PL08X_CHAN_RUNNING;
  1154. /* Configure the physical channel for the active txd */
  1155. pl08x_config_phychan_for_txd(plchan);
  1156. pl08x_set_cregs(pl08x, plchan->phychan);
  1157. pl08x_enable_phy_chan(pl08x, plchan->phychan);
  1158. }
  1159. spin_unlock_irqrestore(&plchan->lock, flags);
  1160. }
  1161. static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
  1162. struct pl08x_txd *txd)
  1163. {
  1164. int num_llis;
  1165. struct pl08x_driver_data *pl08x = plchan->host;
  1166. int ret;
  1167. num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
  1168. if (!num_llis)
  1169. return -EINVAL;
  1170. spin_lock_irqsave(&plchan->lock, plchan->lockflags);
  1171. /*
  1172. * If this device is not using a circular buffer then
  1173. * queue this new descriptor for transfer.
  1174. * The descriptor for a circular buffer continues
  1175. * to be used until the channel is freed.
  1176. */
  1177. if (txd->cd->circular_buffer)
  1178. dev_err(&pl08x->adev->dev,
  1179. "%s attempting to queue a circular buffer\n",
  1180. __func__);
  1181. else
  1182. list_add_tail(&txd->node,
  1183. &plchan->desc_list);
  1184. /*
  1185. * See if we already have a physical channel allocated,
  1186. * else this is the time to try to get one.
  1187. */
  1188. ret = prep_phy_channel(plchan, txd);
  1189. if (ret) {
  1190. /*
  1191. * No physical channel available, we will
  1192. * stack up the memcpy channels until there is a channel
  1193. * available to handle it whereas slave transfers may
  1194. * have been denied due to platform channel muxing restrictions
  1195. * and since there is no guarantee that this will ever be
  1196. * resolved, and since the signal must be acquired AFTER
  1197. * acquiring the physical channel, we will let them be NACK:ed
  1198. * with -EBUSY here. The drivers can alway retry the prep()
  1199. * call if they are eager on doing this using DMA.
  1200. */
  1201. if (plchan->slave) {
  1202. pl08x_free_txd_list(pl08x, plchan);
  1203. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  1204. return -EBUSY;
  1205. }
  1206. /* Do this memcpy whenever there is a channel ready */
  1207. plchan->state = PL08X_CHAN_WAITING;
  1208. plchan->waiting = txd;
  1209. } else
  1210. /*
  1211. * Else we're all set, paused and ready to roll,
  1212. * status will switch to PL08X_CHAN_RUNNING when
  1213. * we call issue_pending(). If there is something
  1214. * running on the channel already we don't change
  1215. * its state.
  1216. */
  1217. if (plchan->state == PL08X_CHAN_IDLE)
  1218. plchan->state = PL08X_CHAN_PAUSED;
  1219. /*
  1220. * Notice that we leave plchan->lock locked on purpose:
  1221. * it will be unlocked in the subsequent tx_submit()
  1222. * call. This is a consequence of the current API.
  1223. */
  1224. return 0;
  1225. }
  1226. /*
  1227. * Initialize a descriptor to be used by memcpy submit
  1228. */
  1229. static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
  1230. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  1231. size_t len, unsigned long flags)
  1232. {
  1233. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1234. struct pl08x_driver_data *pl08x = plchan->host;
  1235. struct pl08x_txd *txd;
  1236. int ret;
  1237. txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
  1238. if (!txd) {
  1239. dev_err(&pl08x->adev->dev,
  1240. "%s no memory for descriptor\n", __func__);
  1241. return NULL;
  1242. }
  1243. dma_async_tx_descriptor_init(&txd->tx, chan);
  1244. txd->direction = DMA_NONE;
  1245. txd->srcbus.addr = src;
  1246. txd->dstbus.addr = dest;
  1247. /* Set platform data for m2m */
  1248. txd->cd = &pl08x->pd->memcpy_channel;
  1249. /* Both to be incremented or the code will break */
  1250. txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
  1251. txd->tx.tx_submit = pl08x_tx_submit;
  1252. txd->tx.callback = NULL;
  1253. txd->tx.callback_param = NULL;
  1254. txd->len = len;
  1255. INIT_LIST_HEAD(&txd->node);
  1256. ret = pl08x_prep_channel_resources(plchan, txd);
  1257. if (ret)
  1258. return NULL;
  1259. /*
  1260. * NB: the channel lock is held at this point so tx_submit()
  1261. * must be called in direct succession.
  1262. */
  1263. return &txd->tx;
  1264. }
  1265. struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
  1266. struct dma_chan *chan, struct scatterlist *sgl,
  1267. unsigned int sg_len, enum dma_data_direction direction,
  1268. unsigned long flags)
  1269. {
  1270. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1271. struct pl08x_driver_data *pl08x = plchan->host;
  1272. struct pl08x_txd *txd;
  1273. int ret;
  1274. /*
  1275. * Current implementation ASSUMES only one sg
  1276. */
  1277. if (sg_len != 1) {
  1278. dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
  1279. __func__);
  1280. BUG();
  1281. }
  1282. dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
  1283. __func__, sgl->length, plchan->name);
  1284. txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
  1285. if (!txd) {
  1286. dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
  1287. return NULL;
  1288. }
  1289. dma_async_tx_descriptor_init(&txd->tx, chan);
  1290. if (direction != plchan->runtime_direction)
  1291. dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
  1292. "the direction configured for the PrimeCell\n",
  1293. __func__);
  1294. /*
  1295. * Set up addresses, the PrimeCell configured address
  1296. * will take precedence since this may configure the
  1297. * channel target address dynamically at runtime.
  1298. */
  1299. txd->direction = direction;
  1300. if (direction == DMA_TO_DEVICE) {
  1301. txd->srcbus.addr = sgl->dma_address;
  1302. if (plchan->runtime_addr)
  1303. txd->dstbus.addr = plchan->runtime_addr;
  1304. else
  1305. txd->dstbus.addr = plchan->cd->addr;
  1306. } else if (direction == DMA_FROM_DEVICE) {
  1307. if (plchan->runtime_addr)
  1308. txd->srcbus.addr = plchan->runtime_addr;
  1309. else
  1310. txd->srcbus.addr = plchan->cd->addr;
  1311. txd->dstbus.addr = sgl->dma_address;
  1312. } else {
  1313. dev_err(&pl08x->adev->dev,
  1314. "%s direction unsupported\n", __func__);
  1315. return NULL;
  1316. }
  1317. txd->cd = plchan->cd;
  1318. txd->tx.tx_submit = pl08x_tx_submit;
  1319. txd->tx.callback = NULL;
  1320. txd->tx.callback_param = NULL;
  1321. txd->len = sgl->length;
  1322. INIT_LIST_HEAD(&txd->node);
  1323. ret = pl08x_prep_channel_resources(plchan, txd);
  1324. if (ret)
  1325. return NULL;
  1326. /*
  1327. * NB: the channel lock is held at this point so tx_submit()
  1328. * must be called in direct succession.
  1329. */
  1330. return &txd->tx;
  1331. }
  1332. static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1333. unsigned long arg)
  1334. {
  1335. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1336. struct pl08x_driver_data *pl08x = plchan->host;
  1337. unsigned long flags;
  1338. int ret = 0;
  1339. /* Controls applicable to inactive channels */
  1340. if (cmd == DMA_SLAVE_CONFIG) {
  1341. dma_set_runtime_config(chan,
  1342. (struct dma_slave_config *)
  1343. arg);
  1344. return 0;
  1345. }
  1346. /*
  1347. * Anything succeeds on channels with no physical allocation and
  1348. * no queued transfers.
  1349. */
  1350. spin_lock_irqsave(&plchan->lock, flags);
  1351. if (!plchan->phychan && !plchan->at) {
  1352. spin_unlock_irqrestore(&plchan->lock, flags);
  1353. return 0;
  1354. }
  1355. switch (cmd) {
  1356. case DMA_TERMINATE_ALL:
  1357. plchan->state = PL08X_CHAN_IDLE;
  1358. if (plchan->phychan) {
  1359. pl08x_stop_phy_chan(plchan->phychan);
  1360. /*
  1361. * Mark physical channel as free and free any slave
  1362. * signal
  1363. */
  1364. if ((plchan->phychan->signal >= 0) &&
  1365. pl08x->pd->put_signal) {
  1366. pl08x->pd->put_signal(plchan);
  1367. plchan->phychan->signal = -1;
  1368. }
  1369. pl08x_put_phy_channel(pl08x, plchan->phychan);
  1370. plchan->phychan = NULL;
  1371. }
  1372. /* Stop any pending tasklet */
  1373. tasklet_disable(&plchan->tasklet);
  1374. /* Dequeue jobs and free LLIs */
  1375. if (plchan->at) {
  1376. pl08x_free_txd(pl08x, plchan->at);
  1377. plchan->at = NULL;
  1378. }
  1379. /* Dequeue jobs not yet fired as well */
  1380. pl08x_free_txd_list(pl08x, plchan);
  1381. break;
  1382. case DMA_PAUSE:
  1383. pl08x_pause_phy_chan(plchan->phychan);
  1384. plchan->state = PL08X_CHAN_PAUSED;
  1385. break;
  1386. case DMA_RESUME:
  1387. pl08x_resume_phy_chan(plchan->phychan);
  1388. plchan->state = PL08X_CHAN_RUNNING;
  1389. break;
  1390. default:
  1391. /* Unknown command */
  1392. ret = -ENXIO;
  1393. break;
  1394. }
  1395. spin_unlock_irqrestore(&plchan->lock, flags);
  1396. return ret;
  1397. }
  1398. bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
  1399. {
  1400. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1401. char *name = chan_id;
  1402. /* Check that the channel is not taken! */
  1403. if (!strcmp(plchan->name, name))
  1404. return true;
  1405. return false;
  1406. }
  1407. /*
  1408. * Just check that the device is there and active
  1409. * TODO: turn this bit on/off depending on the number of
  1410. * physical channels actually used, if it is zero... well
  1411. * shut it off. That will save some power. Cut the clock
  1412. * at the same time.
  1413. */
  1414. static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
  1415. {
  1416. u32 val;
  1417. val = readl(pl08x->base + PL080_CONFIG);
  1418. val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
  1419. /* We implicitly clear bit 1 and that means little-endian mode */
  1420. val |= PL080_CONFIG_ENABLE;
  1421. writel(val, pl08x->base + PL080_CONFIG);
  1422. }
  1423. static void pl08x_tasklet(unsigned long data)
  1424. {
  1425. struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
  1426. struct pl08x_phy_chan *phychan = plchan->phychan;
  1427. struct pl08x_driver_data *pl08x = plchan->host;
  1428. if (!plchan)
  1429. BUG();
  1430. spin_lock(&plchan->lock);
  1431. if (plchan->at) {
  1432. dma_async_tx_callback callback =
  1433. plchan->at->tx.callback;
  1434. void *callback_param =
  1435. plchan->at->tx.callback_param;
  1436. /*
  1437. * Update last completed
  1438. */
  1439. plchan->lc = 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. chan->chan.cookie = 0;
  1600. chan->lc = 0;
  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);