amba-pl08x.c 56 KB

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