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