amba-pl08x.c 53 KB

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