amba-pl08x.c 58 KB

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