amba-pl08x.c 53 KB

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