amba-pl08x.c 53 KB

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