amba-pl08x.c 58 KB

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