amba-pl08x.c 55 KB

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