amba-pl08x.c 58 KB

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