amba-pl08x.c 56 KB

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