amba-pl08x.c 55 KB

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