amba-pl08x.c 54 KB

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