amba-pl08x.c 58 KB

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