amba-pl08x.c 67 KB

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