amba-pl08x.c 54 KB

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