amba-pl08x.c 54 KB

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