amba-pl08x.c 57 KB

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