amba-pl08x.c 58 KB

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