amba-pl08x.c 55 KB

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