amba-pl08x.c 58 KB

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