amba-pl08x.c 54 KB

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