amba-pl08x.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  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 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. 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 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 lli *llis_va = txd->llis_va;
  319. struct lli *llis_bus = (struct 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 lli *llis_va = txd->llis_va;
  496. struct lli *llis_bus = (struct 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 lli *llis_va;
  547. struct 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. /*
  819. * Decide whether this is a loop or a terminated transfer
  820. */
  821. llis_va = txd->llis_va;
  822. llis_bus = (struct lli *) txd->llis_bus;
  823. if (cd->circular_buffer) {
  824. /*
  825. * Loop the circular buffer so that the next element
  826. * points back to the beginning of the LLI.
  827. */
  828. llis_va[num_llis - 1].next =
  829. (dma_addr_t)((unsigned int)&(llis_bus[0]));
  830. } else {
  831. /*
  832. * On non-circular buffers, the final LLI terminates
  833. * the LLI.
  834. */
  835. llis_va[num_llis - 1].next = 0;
  836. /*
  837. * The final LLI element shall also fire an interrupt
  838. */
  839. llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
  840. }
  841. /* Now store the channel register values */
  842. txd->csrc = llis_va[0].src;
  843. txd->cdst = llis_va[0].dst;
  844. if (num_llis > 1)
  845. txd->clli = llis_va[0].next;
  846. else
  847. txd->clli = 0;
  848. txd->cctl = llis_va[0].cctl;
  849. /* ccfg will be set at physical channel allocation time */
  850. #ifdef VERBOSE_DEBUG
  851. {
  852. int i;
  853. for (i = 0; i < num_llis; i++) {
  854. dev_vdbg(&pl08x->adev->dev,
  855. "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
  856. i,
  857. &llis_va[i],
  858. llis_va[i].src,
  859. llis_va[i].dst,
  860. llis_va[i].cctl,
  861. llis_va[i].next
  862. );
  863. }
  864. }
  865. #endif
  866. return num_llis;
  867. }
  868. /* You should call this with the struct pl08x lock held */
  869. static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
  870. struct pl08x_txd *txd)
  871. {
  872. /* Free the LLI */
  873. dma_pool_free(pl08x->pool, txd->llis_va,
  874. txd->llis_bus);
  875. pl08x->pool_ctr--;
  876. kfree(txd);
  877. }
  878. static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
  879. struct pl08x_dma_chan *plchan)
  880. {
  881. struct pl08x_txd *txdi = NULL;
  882. struct pl08x_txd *next;
  883. if (!list_empty(&plchan->desc_list)) {
  884. list_for_each_entry_safe(txdi,
  885. next, &plchan->desc_list, node) {
  886. list_del(&txdi->node);
  887. pl08x_free_txd(pl08x, txdi);
  888. }
  889. }
  890. }
  891. /*
  892. * The DMA ENGINE API
  893. */
  894. static int pl08x_alloc_chan_resources(struct dma_chan *chan)
  895. {
  896. return 0;
  897. }
  898. static void pl08x_free_chan_resources(struct dma_chan *chan)
  899. {
  900. }
  901. /*
  902. * This should be called with the channel plchan->lock held
  903. */
  904. static int prep_phy_channel(struct pl08x_dma_chan *plchan,
  905. struct pl08x_txd *txd)
  906. {
  907. struct pl08x_driver_data *pl08x = plchan->host;
  908. struct pl08x_phy_chan *ch;
  909. int ret;
  910. /* Check if we already have a channel */
  911. if (plchan->phychan)
  912. return 0;
  913. ch = pl08x_get_phy_channel(pl08x, plchan);
  914. if (!ch) {
  915. /* No physical channel available, cope with it */
  916. dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
  917. return -EBUSY;
  918. }
  919. /*
  920. * OK we have a physical channel: for memcpy() this is all we
  921. * need, but for slaves the physical signals may be muxed!
  922. * Can the platform allow us to use this channel?
  923. */
  924. if (plchan->slave &&
  925. ch->signal < 0 &&
  926. pl08x->pd->get_signal) {
  927. ret = pl08x->pd->get_signal(plchan);
  928. if (ret < 0) {
  929. dev_dbg(&pl08x->adev->dev,
  930. "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
  931. ch->id, plchan->name);
  932. /* Release physical channel & return */
  933. pl08x_put_phy_channel(pl08x, ch);
  934. return -EBUSY;
  935. }
  936. ch->signal = ret;
  937. }
  938. dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
  939. ch->id,
  940. ch->signal,
  941. plchan->name);
  942. plchan->phychan = ch;
  943. return 0;
  944. }
  945. static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
  946. {
  947. struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
  948. plchan->chan.cookie += 1;
  949. if (plchan->chan.cookie < 0)
  950. plchan->chan.cookie = 1;
  951. tx->cookie = plchan->chan.cookie;
  952. /* This unlock follows the lock in the prep() function */
  953. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  954. return tx->cookie;
  955. }
  956. static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
  957. struct dma_chan *chan, unsigned long flags)
  958. {
  959. struct dma_async_tx_descriptor *retval = NULL;
  960. return retval;
  961. }
  962. /*
  963. * Code accessing dma_async_is_complete() in a tight loop
  964. * may give problems - could schedule where indicated.
  965. * If slaves are relying on interrupts to signal completion this
  966. * function must not be called with interrupts disabled
  967. */
  968. static enum dma_status
  969. pl08x_dma_tx_status(struct dma_chan *chan,
  970. dma_cookie_t cookie,
  971. struct dma_tx_state *txstate)
  972. {
  973. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  974. dma_cookie_t last_used;
  975. dma_cookie_t last_complete;
  976. enum dma_status ret;
  977. u32 bytesleft = 0;
  978. last_used = plchan->chan.cookie;
  979. last_complete = plchan->lc;
  980. ret = dma_async_is_complete(cookie, last_complete, last_used);
  981. if (ret == DMA_SUCCESS) {
  982. dma_set_tx_state(txstate, last_complete, last_used, 0);
  983. return ret;
  984. }
  985. /*
  986. * schedule(); could be inserted here
  987. */
  988. /*
  989. * This cookie not complete yet
  990. */
  991. last_used = plchan->chan.cookie;
  992. last_complete = plchan->lc;
  993. /* Get number of bytes left in the active transactions and queue */
  994. bytesleft = pl08x_getbytes_chan(plchan);
  995. dma_set_tx_state(txstate, last_complete, last_used,
  996. bytesleft);
  997. if (plchan->state == PL08X_CHAN_PAUSED)
  998. return DMA_PAUSED;
  999. /* Whether waiting or running, we're in progress */
  1000. return DMA_IN_PROGRESS;
  1001. }
  1002. /* PrimeCell DMA extension */
  1003. struct burst_table {
  1004. int burstwords;
  1005. u32 reg;
  1006. };
  1007. static const struct burst_table burst_sizes[] = {
  1008. {
  1009. .burstwords = 256,
  1010. .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1011. (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
  1012. },
  1013. {
  1014. .burstwords = 128,
  1015. .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1016. (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
  1017. },
  1018. {
  1019. .burstwords = 64,
  1020. .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1021. (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
  1022. },
  1023. {
  1024. .burstwords = 32,
  1025. .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1026. (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
  1027. },
  1028. {
  1029. .burstwords = 16,
  1030. .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1031. (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
  1032. },
  1033. {
  1034. .burstwords = 8,
  1035. .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1036. (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
  1037. },
  1038. {
  1039. .burstwords = 4,
  1040. .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1041. (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
  1042. },
  1043. {
  1044. .burstwords = 1,
  1045. .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1046. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
  1047. },
  1048. };
  1049. static void dma_set_runtime_config(struct dma_chan *chan,
  1050. struct dma_slave_config *config)
  1051. {
  1052. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1053. struct pl08x_driver_data *pl08x = plchan->host;
  1054. struct pl08x_channel_data *cd = plchan->cd;
  1055. enum dma_slave_buswidth addr_width;
  1056. u32 maxburst;
  1057. u32 cctl = 0;
  1058. /* Mask out all except src and dst channel */
  1059. u32 ccfg = cd->ccfg & 0x000003DEU;
  1060. int i;
  1061. /* Transfer direction */
  1062. plchan->runtime_direction = config->direction;
  1063. if (config->direction == DMA_TO_DEVICE) {
  1064. plchan->runtime_addr = config->dst_addr;
  1065. cctl |= PL080_CONTROL_SRC_INCR;
  1066. ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1067. addr_width = config->dst_addr_width;
  1068. maxburst = config->dst_maxburst;
  1069. } else if (config->direction == DMA_FROM_DEVICE) {
  1070. plchan->runtime_addr = config->src_addr;
  1071. cctl |= PL080_CONTROL_DST_INCR;
  1072. ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1073. addr_width = config->src_addr_width;
  1074. maxburst = config->src_maxburst;
  1075. } else {
  1076. dev_err(&pl08x->adev->dev,
  1077. "bad runtime_config: alien transfer direction\n");
  1078. return;
  1079. }
  1080. switch (addr_width) {
  1081. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1082. cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1083. (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1084. break;
  1085. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1086. cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1087. (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1088. break;
  1089. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1090. cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
  1091. (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
  1092. break;
  1093. default:
  1094. dev_err(&pl08x->adev->dev,
  1095. "bad runtime_config: alien address width\n");
  1096. return;
  1097. }
  1098. /*
  1099. * Now decide on a maxburst:
  1100. * If this channel will only request single transfers, set this
  1101. * down to ONE element. Also select one element if no maxburst
  1102. * is specified.
  1103. */
  1104. if (plchan->cd->single || maxburst == 0) {
  1105. cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
  1106. (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
  1107. } else {
  1108. for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
  1109. if (burst_sizes[i].burstwords <= maxburst)
  1110. break;
  1111. cctl |= burst_sizes[i].reg;
  1112. }
  1113. /* Access the cell in privileged mode, non-bufferable, non-cacheable */
  1114. cctl &= ~PL080_CONTROL_PROT_MASK;
  1115. cctl |= PL080_CONTROL_PROT_SYS;
  1116. /* Modify the default channel data to fit PrimeCell request */
  1117. cd->cctl = cctl;
  1118. cd->ccfg = ccfg;
  1119. dev_dbg(&pl08x->adev->dev,
  1120. "configured channel %s (%s) for %s, data width %d, "
  1121. "maxburst %d words, LE, CCTL=0x%08x, CCFG=0x%08x\n",
  1122. dma_chan_name(chan), plchan->name,
  1123. (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
  1124. addr_width,
  1125. maxburst,
  1126. cctl, ccfg);
  1127. }
  1128. /*
  1129. * Slave transactions callback to the slave device to allow
  1130. * synchronization of slave DMA signals with the DMAC enable
  1131. */
  1132. static void pl08x_issue_pending(struct dma_chan *chan)
  1133. {
  1134. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1135. struct pl08x_driver_data *pl08x = plchan->host;
  1136. unsigned long flags;
  1137. spin_lock_irqsave(&plchan->lock, flags);
  1138. /* Something is already active, or we're waiting for a channel... */
  1139. if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
  1140. spin_unlock_irqrestore(&plchan->lock, flags);
  1141. return;
  1142. }
  1143. /* Take the first element in the queue and execute it */
  1144. if (!list_empty(&plchan->desc_list)) {
  1145. struct pl08x_txd *next;
  1146. next = list_first_entry(&plchan->desc_list,
  1147. struct pl08x_txd,
  1148. node);
  1149. list_del(&next->node);
  1150. plchan->at = next;
  1151. plchan->state = PL08X_CHAN_RUNNING;
  1152. /* Configure the physical channel for the active txd */
  1153. pl08x_config_phychan_for_txd(plchan);
  1154. pl08x_set_cregs(pl08x, plchan->phychan);
  1155. pl08x_enable_phy_chan(pl08x, plchan->phychan);
  1156. }
  1157. spin_unlock_irqrestore(&plchan->lock, flags);
  1158. }
  1159. static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
  1160. struct pl08x_txd *txd)
  1161. {
  1162. int num_llis;
  1163. struct pl08x_driver_data *pl08x = plchan->host;
  1164. int ret;
  1165. num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
  1166. if (!num_llis) {
  1167. kfree(txd);
  1168. return -EINVAL;
  1169. }
  1170. spin_lock_irqsave(&plchan->lock, plchan->lockflags);
  1171. /*
  1172. * If this device is not using a circular buffer then
  1173. * queue this new descriptor for transfer.
  1174. * The descriptor for a circular buffer continues
  1175. * to be used until the channel is freed.
  1176. */
  1177. if (txd->cd->circular_buffer)
  1178. dev_err(&pl08x->adev->dev,
  1179. "%s attempting to queue a circular buffer\n",
  1180. __func__);
  1181. else
  1182. list_add_tail(&txd->node,
  1183. &plchan->desc_list);
  1184. /*
  1185. * See if we already have a physical channel allocated,
  1186. * else this is the time to try to get one.
  1187. */
  1188. ret = prep_phy_channel(plchan, txd);
  1189. if (ret) {
  1190. /*
  1191. * No physical channel available, we will
  1192. * stack up the memcpy channels until there is a channel
  1193. * available to handle it whereas slave transfers may
  1194. * have been denied due to platform channel muxing restrictions
  1195. * and since there is no guarantee that this will ever be
  1196. * resolved, and since the signal must be acquired AFTER
  1197. * acquiring the physical channel, we will let them be NACK:ed
  1198. * with -EBUSY here. The drivers can alway retry the prep()
  1199. * call if they are eager on doing this using DMA.
  1200. */
  1201. if (plchan->slave) {
  1202. pl08x_free_txd_list(pl08x, plchan);
  1203. spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
  1204. return -EBUSY;
  1205. }
  1206. /* Do this memcpy whenever there is a channel ready */
  1207. plchan->state = PL08X_CHAN_WAITING;
  1208. plchan->waiting = txd;
  1209. } else
  1210. /*
  1211. * Else we're all set, paused and ready to roll,
  1212. * status will switch to PL08X_CHAN_RUNNING when
  1213. * we call issue_pending(). If there is something
  1214. * running on the channel already we don't change
  1215. * its state.
  1216. */
  1217. if (plchan->state == PL08X_CHAN_IDLE)
  1218. plchan->state = PL08X_CHAN_PAUSED;
  1219. /*
  1220. * Notice that we leave plchan->lock locked on purpose:
  1221. * it will be unlocked in the subsequent tx_submit()
  1222. * call. This is a consequence of the current API.
  1223. */
  1224. return 0;
  1225. }
  1226. /*
  1227. * Initialize a descriptor to be used by memcpy submit
  1228. */
  1229. static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
  1230. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  1231. size_t len, unsigned long flags)
  1232. {
  1233. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1234. struct pl08x_driver_data *pl08x = plchan->host;
  1235. struct pl08x_txd *txd;
  1236. int ret;
  1237. txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
  1238. if (!txd) {
  1239. dev_err(&pl08x->adev->dev,
  1240. "%s no memory for descriptor\n", __func__);
  1241. return NULL;
  1242. }
  1243. dma_async_tx_descriptor_init(&txd->tx, chan);
  1244. txd->direction = DMA_NONE;
  1245. txd->srcbus.addr = src;
  1246. txd->dstbus.addr = dest;
  1247. /* Set platform data for m2m */
  1248. txd->cd = &pl08x->pd->memcpy_channel;
  1249. /* Both to be incremented or the code will break */
  1250. txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
  1251. txd->tx.tx_submit = pl08x_tx_submit;
  1252. txd->tx.callback = NULL;
  1253. txd->tx.callback_param = NULL;
  1254. txd->len = len;
  1255. INIT_LIST_HEAD(&txd->node);
  1256. ret = pl08x_prep_channel_resources(plchan, txd);
  1257. if (ret)
  1258. return NULL;
  1259. /*
  1260. * NB: the channel lock is held at this point so tx_submit()
  1261. * must be called in direct succession.
  1262. */
  1263. return &txd->tx;
  1264. }
  1265. static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
  1266. struct dma_chan *chan, struct scatterlist *sgl,
  1267. unsigned int sg_len, enum dma_data_direction direction,
  1268. unsigned long flags)
  1269. {
  1270. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1271. struct pl08x_driver_data *pl08x = plchan->host;
  1272. struct pl08x_txd *txd;
  1273. int ret;
  1274. /*
  1275. * Current implementation ASSUMES only one sg
  1276. */
  1277. if (sg_len != 1) {
  1278. dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
  1279. __func__);
  1280. BUG();
  1281. }
  1282. dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
  1283. __func__, sgl->length, plchan->name);
  1284. txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
  1285. if (!txd) {
  1286. dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
  1287. return NULL;
  1288. }
  1289. dma_async_tx_descriptor_init(&txd->tx, chan);
  1290. if (direction != plchan->runtime_direction)
  1291. dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
  1292. "the direction configured for the PrimeCell\n",
  1293. __func__);
  1294. /*
  1295. * Set up addresses, the PrimeCell configured address
  1296. * will take precedence since this may configure the
  1297. * channel target address dynamically at runtime.
  1298. */
  1299. txd->direction = direction;
  1300. if (direction == DMA_TO_DEVICE) {
  1301. txd->srcbus.addr = sgl->dma_address;
  1302. if (plchan->runtime_addr)
  1303. txd->dstbus.addr = plchan->runtime_addr;
  1304. else
  1305. txd->dstbus.addr = plchan->cd->addr;
  1306. } else if (direction == DMA_FROM_DEVICE) {
  1307. if (plchan->runtime_addr)
  1308. txd->srcbus.addr = plchan->runtime_addr;
  1309. else
  1310. txd->srcbus.addr = plchan->cd->addr;
  1311. txd->dstbus.addr = sgl->dma_address;
  1312. } else {
  1313. dev_err(&pl08x->adev->dev,
  1314. "%s direction unsupported\n", __func__);
  1315. return NULL;
  1316. }
  1317. txd->cd = plchan->cd;
  1318. txd->tx.tx_submit = pl08x_tx_submit;
  1319. txd->tx.callback = NULL;
  1320. txd->tx.callback_param = NULL;
  1321. txd->len = sgl->length;
  1322. INIT_LIST_HEAD(&txd->node);
  1323. ret = pl08x_prep_channel_resources(plchan, txd);
  1324. if (ret)
  1325. return NULL;
  1326. /*
  1327. * NB: the channel lock is held at this point so tx_submit()
  1328. * must be called in direct succession.
  1329. */
  1330. return &txd->tx;
  1331. }
  1332. static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1333. unsigned long arg)
  1334. {
  1335. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1336. struct pl08x_driver_data *pl08x = plchan->host;
  1337. unsigned long flags;
  1338. int ret = 0;
  1339. /* Controls applicable to inactive channels */
  1340. if (cmd == DMA_SLAVE_CONFIG) {
  1341. dma_set_runtime_config(chan,
  1342. (struct dma_slave_config *)
  1343. arg);
  1344. return 0;
  1345. }
  1346. /*
  1347. * Anything succeeds on channels with no physical allocation and
  1348. * no queued transfers.
  1349. */
  1350. spin_lock_irqsave(&plchan->lock, flags);
  1351. if (!plchan->phychan && !plchan->at) {
  1352. spin_unlock_irqrestore(&plchan->lock, flags);
  1353. return 0;
  1354. }
  1355. switch (cmd) {
  1356. case DMA_TERMINATE_ALL:
  1357. plchan->state = PL08X_CHAN_IDLE;
  1358. if (plchan->phychan) {
  1359. pl08x_stop_phy_chan(plchan->phychan);
  1360. /*
  1361. * Mark physical channel as free and free any slave
  1362. * signal
  1363. */
  1364. if ((plchan->phychan->signal >= 0) &&
  1365. pl08x->pd->put_signal) {
  1366. pl08x->pd->put_signal(plchan);
  1367. plchan->phychan->signal = -1;
  1368. }
  1369. pl08x_put_phy_channel(pl08x, plchan->phychan);
  1370. plchan->phychan = NULL;
  1371. }
  1372. /* Dequeue jobs and free LLIs */
  1373. if (plchan->at) {
  1374. pl08x_free_txd(pl08x, plchan->at);
  1375. plchan->at = NULL;
  1376. }
  1377. /* Dequeue jobs not yet fired as well */
  1378. pl08x_free_txd_list(pl08x, plchan);
  1379. break;
  1380. case DMA_PAUSE:
  1381. pl08x_pause_phy_chan(plchan->phychan);
  1382. plchan->state = PL08X_CHAN_PAUSED;
  1383. break;
  1384. case DMA_RESUME:
  1385. pl08x_resume_phy_chan(plchan->phychan);
  1386. plchan->state = PL08X_CHAN_RUNNING;
  1387. break;
  1388. default:
  1389. /* Unknown command */
  1390. ret = -ENXIO;
  1391. break;
  1392. }
  1393. spin_unlock_irqrestore(&plchan->lock, flags);
  1394. return ret;
  1395. }
  1396. bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
  1397. {
  1398. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1399. char *name = chan_id;
  1400. /* Check that the channel is not taken! */
  1401. if (!strcmp(plchan->name, name))
  1402. return true;
  1403. return false;
  1404. }
  1405. /*
  1406. * Just check that the device is there and active
  1407. * TODO: turn this bit on/off depending on the number of
  1408. * physical channels actually used, if it is zero... well
  1409. * shut it off. That will save some power. Cut the clock
  1410. * at the same time.
  1411. */
  1412. static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
  1413. {
  1414. u32 val;
  1415. val = readl(pl08x->base + PL080_CONFIG);
  1416. val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
  1417. /* We implicitly clear bit 1 and that means little-endian mode */
  1418. val |= PL080_CONFIG_ENABLE;
  1419. writel(val, pl08x->base + PL080_CONFIG);
  1420. }
  1421. static void pl08x_tasklet(unsigned long data)
  1422. {
  1423. struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
  1424. struct pl08x_phy_chan *phychan = plchan->phychan;
  1425. struct pl08x_driver_data *pl08x = plchan->host;
  1426. unsigned long flags;
  1427. spin_lock_irqsave(&plchan->lock, flags);
  1428. if (plchan->at) {
  1429. dma_async_tx_callback callback =
  1430. plchan->at->tx.callback;
  1431. void *callback_param =
  1432. plchan->at->tx.callback_param;
  1433. /*
  1434. * Update last completed
  1435. */
  1436. plchan->lc = plchan->at->tx.cookie;
  1437. /*
  1438. * Callback to signal completion
  1439. */
  1440. if (callback)
  1441. callback(callback_param);
  1442. /*
  1443. * Free the descriptor if it's not for a device
  1444. * using a circular buffer
  1445. */
  1446. if (!plchan->at->cd->circular_buffer) {
  1447. pl08x_free_txd(pl08x, plchan->at);
  1448. plchan->at = NULL;
  1449. }
  1450. /*
  1451. * else descriptor for circular
  1452. * buffers only freed when
  1453. * client has disabled dma
  1454. */
  1455. }
  1456. /*
  1457. * If a new descriptor is queued, set it up
  1458. * plchan->at is NULL here
  1459. */
  1460. if (!list_empty(&plchan->desc_list)) {
  1461. struct pl08x_txd *next;
  1462. next = list_first_entry(&plchan->desc_list,
  1463. struct pl08x_txd,
  1464. node);
  1465. list_del(&next->node);
  1466. plchan->at = next;
  1467. /* Configure the physical channel for the next txd */
  1468. pl08x_config_phychan_for_txd(plchan);
  1469. pl08x_set_cregs(pl08x, plchan->phychan);
  1470. pl08x_enable_phy_chan(pl08x, plchan->phychan);
  1471. } else {
  1472. struct pl08x_dma_chan *waiting = NULL;
  1473. /*
  1474. * No more jobs, so free up the physical channel
  1475. * Free any allocated signal on slave transfers too
  1476. */
  1477. if ((phychan->signal >= 0) && pl08x->pd->put_signal) {
  1478. pl08x->pd->put_signal(plchan);
  1479. phychan->signal = -1;
  1480. }
  1481. pl08x_put_phy_channel(pl08x, phychan);
  1482. plchan->phychan = NULL;
  1483. plchan->state = PL08X_CHAN_IDLE;
  1484. /*
  1485. * And NOW before anyone else can grab that free:d
  1486. * up physical channel, see if there is some memcpy
  1487. * pending that seriously needs to start because of
  1488. * being stacked up while we were choking the
  1489. * physical channels with data.
  1490. */
  1491. list_for_each_entry(waiting, &pl08x->memcpy.channels,
  1492. chan.device_node) {
  1493. if (waiting->state == PL08X_CHAN_WAITING &&
  1494. waiting->waiting != NULL) {
  1495. int ret;
  1496. /* This should REALLY not fail now */
  1497. ret = prep_phy_channel(waiting,
  1498. waiting->waiting);
  1499. BUG_ON(ret);
  1500. waiting->state = PL08X_CHAN_RUNNING;
  1501. waiting->waiting = NULL;
  1502. pl08x_issue_pending(&waiting->chan);
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. spin_unlock_irqrestore(&plchan->lock, flags);
  1508. }
  1509. static irqreturn_t pl08x_irq(int irq, void *dev)
  1510. {
  1511. struct pl08x_driver_data *pl08x = dev;
  1512. u32 mask = 0;
  1513. u32 val;
  1514. int i;
  1515. val = readl(pl08x->base + PL080_ERR_STATUS);
  1516. if (val) {
  1517. /*
  1518. * An error interrupt (on one or more channels)
  1519. */
  1520. dev_err(&pl08x->adev->dev,
  1521. "%s error interrupt, register value 0x%08x\n",
  1522. __func__, val);
  1523. /*
  1524. * Simply clear ALL PL08X error interrupts,
  1525. * regardless of channel and cause
  1526. * FIXME: should be 0x00000003 on PL081 really.
  1527. */
  1528. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  1529. }
  1530. val = readl(pl08x->base + PL080_INT_STATUS);
  1531. for (i = 0; i < pl08x->vd->channels; i++) {
  1532. if ((1 << i) & val) {
  1533. /* Locate physical channel */
  1534. struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
  1535. struct pl08x_dma_chan *plchan = phychan->serving;
  1536. /* Schedule tasklet on this channel */
  1537. tasklet_schedule(&plchan->tasklet);
  1538. mask |= (1 << i);
  1539. }
  1540. }
  1541. /*
  1542. * Clear only the terminal interrupts on channels we processed
  1543. */
  1544. writel(mask, pl08x->base + PL080_TC_CLEAR);
  1545. return mask ? IRQ_HANDLED : IRQ_NONE;
  1546. }
  1547. /*
  1548. * Initialise the DMAC memcpy/slave channels.
  1549. * Make a local wrapper to hold required data
  1550. */
  1551. static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
  1552. struct dma_device *dmadev,
  1553. unsigned int channels,
  1554. bool slave)
  1555. {
  1556. struct pl08x_dma_chan *chan;
  1557. int i;
  1558. INIT_LIST_HEAD(&dmadev->channels);
  1559. /*
  1560. * Register as many many memcpy as we have physical channels,
  1561. * we won't always be able to use all but the code will have
  1562. * to cope with that situation.
  1563. */
  1564. for (i = 0; i < channels; i++) {
  1565. chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
  1566. if (!chan) {
  1567. dev_err(&pl08x->adev->dev,
  1568. "%s no memory for channel\n", __func__);
  1569. return -ENOMEM;
  1570. }
  1571. chan->host = pl08x;
  1572. chan->state = PL08X_CHAN_IDLE;
  1573. if (slave) {
  1574. chan->slave = true;
  1575. chan->name = pl08x->pd->slave_channels[i].bus_id;
  1576. chan->cd = &pl08x->pd->slave_channels[i];
  1577. } else {
  1578. chan->cd = &pl08x->pd->memcpy_channel;
  1579. chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
  1580. if (!chan->name) {
  1581. kfree(chan);
  1582. return -ENOMEM;
  1583. }
  1584. }
  1585. dev_info(&pl08x->adev->dev,
  1586. "initialize virtual channel \"%s\"\n",
  1587. chan->name);
  1588. chan->chan.device = dmadev;
  1589. chan->chan.cookie = 0;
  1590. chan->lc = 0;
  1591. spin_lock_init(&chan->lock);
  1592. INIT_LIST_HEAD(&chan->desc_list);
  1593. tasklet_init(&chan->tasklet, pl08x_tasklet,
  1594. (unsigned long) chan);
  1595. list_add_tail(&chan->chan.device_node, &dmadev->channels);
  1596. }
  1597. dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
  1598. i, slave ? "slave" : "memcpy");
  1599. return i;
  1600. }
  1601. static void pl08x_free_virtual_channels(struct dma_device *dmadev)
  1602. {
  1603. struct pl08x_dma_chan *chan = NULL;
  1604. struct pl08x_dma_chan *next;
  1605. list_for_each_entry_safe(chan,
  1606. next, &dmadev->channels, chan.device_node) {
  1607. list_del(&chan->chan.device_node);
  1608. kfree(chan);
  1609. }
  1610. }
  1611. #ifdef CONFIG_DEBUG_FS
  1612. static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
  1613. {
  1614. switch (state) {
  1615. case PL08X_CHAN_IDLE:
  1616. return "idle";
  1617. case PL08X_CHAN_RUNNING:
  1618. return "running";
  1619. case PL08X_CHAN_PAUSED:
  1620. return "paused";
  1621. case PL08X_CHAN_WAITING:
  1622. return "waiting";
  1623. default:
  1624. break;
  1625. }
  1626. return "UNKNOWN STATE";
  1627. }
  1628. static int pl08x_debugfs_show(struct seq_file *s, void *data)
  1629. {
  1630. struct pl08x_driver_data *pl08x = s->private;
  1631. struct pl08x_dma_chan *chan;
  1632. struct pl08x_phy_chan *ch;
  1633. unsigned long flags;
  1634. int i;
  1635. seq_printf(s, "PL08x physical channels:\n");
  1636. seq_printf(s, "CHANNEL:\tUSER:\n");
  1637. seq_printf(s, "--------\t-----\n");
  1638. for (i = 0; i < pl08x->vd->channels; i++) {
  1639. struct pl08x_dma_chan *virt_chan;
  1640. ch = &pl08x->phy_chans[i];
  1641. spin_lock_irqsave(&ch->lock, flags);
  1642. virt_chan = ch->serving;
  1643. seq_printf(s, "%d\t\t%s\n",
  1644. ch->id, virt_chan ? virt_chan->name : "(none)");
  1645. spin_unlock_irqrestore(&ch->lock, flags);
  1646. }
  1647. seq_printf(s, "\nPL08x virtual memcpy channels:\n");
  1648. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1649. seq_printf(s, "--------\t------\n");
  1650. list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
  1651. seq_printf(s, "%s\t\t%s\n", chan->name,
  1652. pl08x_state_str(chan->state));
  1653. }
  1654. seq_printf(s, "\nPL08x virtual slave channels:\n");
  1655. seq_printf(s, "CHANNEL:\tSTATE:\n");
  1656. seq_printf(s, "--------\t------\n");
  1657. list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
  1658. seq_printf(s, "%s\t\t%s\n", chan->name,
  1659. pl08x_state_str(chan->state));
  1660. }
  1661. return 0;
  1662. }
  1663. static int pl08x_debugfs_open(struct inode *inode, struct file *file)
  1664. {
  1665. return single_open(file, pl08x_debugfs_show, inode->i_private);
  1666. }
  1667. static const struct file_operations pl08x_debugfs_operations = {
  1668. .open = pl08x_debugfs_open,
  1669. .read = seq_read,
  1670. .llseek = seq_lseek,
  1671. .release = single_release,
  1672. };
  1673. static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1674. {
  1675. /* Expose a simple debugfs interface to view all clocks */
  1676. (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
  1677. NULL, pl08x,
  1678. &pl08x_debugfs_operations);
  1679. }
  1680. #else
  1681. static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  1682. {
  1683. }
  1684. #endif
  1685. static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
  1686. {
  1687. struct pl08x_driver_data *pl08x;
  1688. struct vendor_data *vd = id->data;
  1689. int ret = 0;
  1690. int i;
  1691. ret = amba_request_regions(adev, NULL);
  1692. if (ret)
  1693. return ret;
  1694. /* Create the driver state holder */
  1695. pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
  1696. if (!pl08x) {
  1697. ret = -ENOMEM;
  1698. goto out_no_pl08x;
  1699. }
  1700. /* Initialize memcpy engine */
  1701. dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
  1702. pl08x->memcpy.dev = &adev->dev;
  1703. pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
  1704. pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
  1705. pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
  1706. pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1707. pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
  1708. pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
  1709. pl08x->memcpy.device_control = pl08x_control;
  1710. /* Initialize slave engine */
  1711. dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
  1712. pl08x->slave.dev = &adev->dev;
  1713. pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
  1714. pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
  1715. pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  1716. pl08x->slave.device_tx_status = pl08x_dma_tx_status;
  1717. pl08x->slave.device_issue_pending = pl08x_issue_pending;
  1718. pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
  1719. pl08x->slave.device_control = pl08x_control;
  1720. /* Get the platform data */
  1721. pl08x->pd = dev_get_platdata(&adev->dev);
  1722. if (!pl08x->pd) {
  1723. dev_err(&adev->dev, "no platform data supplied\n");
  1724. goto out_no_platdata;
  1725. }
  1726. /* Assign useful pointers to the driver state */
  1727. pl08x->adev = adev;
  1728. pl08x->vd = vd;
  1729. /* A DMA memory pool for LLIs, align on 1-byte boundary */
  1730. pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
  1731. PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
  1732. if (!pl08x->pool) {
  1733. ret = -ENOMEM;
  1734. goto out_no_lli_pool;
  1735. }
  1736. spin_lock_init(&pl08x->lock);
  1737. pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
  1738. if (!pl08x->base) {
  1739. ret = -ENOMEM;
  1740. goto out_no_ioremap;
  1741. }
  1742. /* Turn on the PL08x */
  1743. pl08x_ensure_on(pl08x);
  1744. /*
  1745. * Attach the interrupt handler
  1746. */
  1747. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  1748. writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
  1749. ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
  1750. DRIVER_NAME, pl08x);
  1751. if (ret) {
  1752. dev_err(&adev->dev, "%s failed to request interrupt %d\n",
  1753. __func__, adev->irq[0]);
  1754. goto out_no_irq;
  1755. }
  1756. /* Initialize physical channels */
  1757. pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
  1758. GFP_KERNEL);
  1759. if (!pl08x->phy_chans) {
  1760. dev_err(&adev->dev, "%s failed to allocate "
  1761. "physical channel holders\n",
  1762. __func__);
  1763. goto out_no_phychans;
  1764. }
  1765. for (i = 0; i < vd->channels; i++) {
  1766. struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
  1767. ch->id = i;
  1768. ch->base = pl08x->base + PL080_Cx_BASE(i);
  1769. spin_lock_init(&ch->lock);
  1770. ch->serving = NULL;
  1771. ch->signal = -1;
  1772. dev_info(&adev->dev,
  1773. "physical channel %d is %s\n", i,
  1774. pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
  1775. }
  1776. /* Register as many memcpy channels as there are physical channels */
  1777. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
  1778. pl08x->vd->channels, false);
  1779. if (ret <= 0) {
  1780. dev_warn(&pl08x->adev->dev,
  1781. "%s failed to enumerate memcpy channels - %d\n",
  1782. __func__, ret);
  1783. goto out_no_memcpy;
  1784. }
  1785. pl08x->memcpy.chancnt = ret;
  1786. /* Register slave channels */
  1787. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
  1788. pl08x->pd->num_slave_channels,
  1789. true);
  1790. if (ret <= 0) {
  1791. dev_warn(&pl08x->adev->dev,
  1792. "%s failed to enumerate slave channels - %d\n",
  1793. __func__, ret);
  1794. goto out_no_slave;
  1795. }
  1796. pl08x->slave.chancnt = ret;
  1797. ret = dma_async_device_register(&pl08x->memcpy);
  1798. if (ret) {
  1799. dev_warn(&pl08x->adev->dev,
  1800. "%s failed to register memcpy as an async device - %d\n",
  1801. __func__, ret);
  1802. goto out_no_memcpy_reg;
  1803. }
  1804. ret = dma_async_device_register(&pl08x->slave);
  1805. if (ret) {
  1806. dev_warn(&pl08x->adev->dev,
  1807. "%s failed to register slave as an async device - %d\n",
  1808. __func__, ret);
  1809. goto out_no_slave_reg;
  1810. }
  1811. amba_set_drvdata(adev, pl08x);
  1812. init_pl08x_debugfs(pl08x);
  1813. dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
  1814. amba_part(adev), amba_rev(adev),
  1815. (unsigned long long)adev->res.start, adev->irq[0]);
  1816. return 0;
  1817. out_no_slave_reg:
  1818. dma_async_device_unregister(&pl08x->memcpy);
  1819. out_no_memcpy_reg:
  1820. pl08x_free_virtual_channels(&pl08x->slave);
  1821. out_no_slave:
  1822. pl08x_free_virtual_channels(&pl08x->memcpy);
  1823. out_no_memcpy:
  1824. kfree(pl08x->phy_chans);
  1825. out_no_phychans:
  1826. free_irq(adev->irq[0], pl08x);
  1827. out_no_irq:
  1828. iounmap(pl08x->base);
  1829. out_no_ioremap:
  1830. dma_pool_destroy(pl08x->pool);
  1831. out_no_lli_pool:
  1832. out_no_platdata:
  1833. kfree(pl08x);
  1834. out_no_pl08x:
  1835. amba_release_regions(adev);
  1836. return ret;
  1837. }
  1838. /* PL080 has 8 channels and the PL080 have just 2 */
  1839. static struct vendor_data vendor_pl080 = {
  1840. .channels = 8,
  1841. .dualmaster = true,
  1842. };
  1843. static struct vendor_data vendor_pl081 = {
  1844. .channels = 2,
  1845. .dualmaster = false,
  1846. };
  1847. static struct amba_id pl08x_ids[] = {
  1848. /* PL080 */
  1849. {
  1850. .id = 0x00041080,
  1851. .mask = 0x000fffff,
  1852. .data = &vendor_pl080,
  1853. },
  1854. /* PL081 */
  1855. {
  1856. .id = 0x00041081,
  1857. .mask = 0x000fffff,
  1858. .data = &vendor_pl081,
  1859. },
  1860. /* Nomadik 8815 PL080 variant */
  1861. {
  1862. .id = 0x00280880,
  1863. .mask = 0x00ffffff,
  1864. .data = &vendor_pl080,
  1865. },
  1866. { 0, 0 },
  1867. };
  1868. static struct amba_driver pl08x_amba_driver = {
  1869. .drv.name = DRIVER_NAME,
  1870. .id_table = pl08x_ids,
  1871. .probe = pl08x_probe,
  1872. };
  1873. static int __init pl08x_init(void)
  1874. {
  1875. int retval;
  1876. retval = amba_driver_register(&pl08x_amba_driver);
  1877. if (retval)
  1878. printk(KERN_WARNING DRIVER_NAME
  1879. "failed to register as an AMBA device (%d)\n",
  1880. retval);
  1881. return retval;
  1882. }
  1883. subsys_initcall(pl08x_init);