amba-pl08x.c 57 KB

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