amba-pl08x.c 60 KB

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