amba-pl08x.c 55 KB

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