amba-pl08x.c 54 KB

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