amba-pl08x.c 54 KB

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