amba-pl08x.c 54 KB

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