amba-pl08x.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. /*
  2. * Copyright (c) 2006 ARM Ltd.
  3. * Copyright (c) 2010 ST-Ericsson SA
  4. * Copyirght (c) 2017 Linaro Ltd.
  5. *
  6. * Author: Peter Pearse <peter.pearse@arm.com>
  7. * Author: Linus Walleij <linus.walleij@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * The full GNU General Public License is in this distribution in the file
  20. * called COPYING.
  21. *
  22. * Documentation: ARM DDI 0196G == PL080
  23. * Documentation: ARM DDI 0218E == PL081
  24. * Documentation: S3C6410 User's Manual == PL080S
  25. *
  26. * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
  27. * channel.
  28. *
  29. * The PL080 has 8 channels available for simultaneous use, and the PL081
  30. * has only two channels. So on these DMA controllers the number of channels
  31. * and the number of incoming DMA signals are two totally different things.
  32. * It is usually not possible to theoretically handle all physical signals,
  33. * so a multiplexing scheme with possible denial of use is necessary.
  34. *
  35. * The PL080 has a dual bus master, PL081 has a single master.
  36. *
  37. * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
  38. * It differs in following aspects:
  39. * - CH_CONFIG register at different offset,
  40. * - separate CH_CONTROL2 register for transfer size,
  41. * - bigger maximum transfer size,
  42. * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
  43. * - no support for peripheral flow control.
  44. *
  45. * Memory to peripheral transfer may be visualized as
  46. * Get data from memory to DMAC
  47. * Until no data left
  48. * On burst request from peripheral
  49. * Destination burst from DMAC to peripheral
  50. * Clear burst request
  51. * Raise terminal count interrupt
  52. *
  53. * For peripherals with a FIFO:
  54. * Source burst size == half the depth of the peripheral FIFO
  55. * Destination burst size == the depth of the peripheral FIFO
  56. *
  57. * (Bursts are irrelevant for mem to mem transfers - there are no burst
  58. * signals, the DMA controller will simply facilitate its AHB master.)
  59. *
  60. * ASSUMES default (little) endianness for DMA transfers
  61. *
  62. * The PL08x has two flow control settings:
  63. * - DMAC flow control: the transfer size defines the number of transfers
  64. * which occur for the current LLI entry, and the DMAC raises TC at the
  65. * end of every LLI entry. Observed behaviour shows the DMAC listening
  66. * to both the BREQ and SREQ signals (contrary to documented),
  67. * transferring data if either is active. The LBREQ and LSREQ signals
  68. * are ignored.
  69. *
  70. * - Peripheral flow control: the transfer size is ignored (and should be
  71. * zero). The data is transferred from the current LLI entry, until
  72. * after the final transfer signalled by LBREQ or LSREQ. The DMAC
  73. * will then move to the next LLI entry. Unsupported by PL080S.
  74. */
  75. #include <linux/amba/bus.h>
  76. #include <linux/amba/pl08x.h>
  77. #include <linux/debugfs.h>
  78. #include <linux/delay.h>
  79. #include <linux/device.h>
  80. #include <linux/dmaengine.h>
  81. #include <linux/dmapool.h>
  82. #include <linux/dma-mapping.h>
  83. #include <linux/export.h>
  84. #include <linux/init.h>
  85. #include <linux/interrupt.h>
  86. #include <linux/module.h>
  87. #include <linux/of.h>
  88. #include <linux/of_dma.h>
  89. #include <linux/pm_runtime.h>
  90. #include <linux/seq_file.h>
  91. #include <linux/slab.h>
  92. #include <linux/amba/pl080.h>
  93. #include "dmaengine.h"
  94. #include "virt-dma.h"
  95. #define DRIVER_NAME "pl08xdmac"
  96. #define PL80X_DMA_BUSWIDTHS \
  97. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  98. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  99. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  100. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  101. static struct amba_driver pl08x_amba_driver;
  102. struct pl08x_driver_data;
  103. /**
  104. * struct vendor_data - vendor-specific config parameters for PL08x derivatives
  105. * @config_offset: offset to the configuration register
  106. * @channels: the number of channels available in this variant
  107. * @signals: the number of request signals available from the hardware
  108. * @dualmaster: whether this version supports dual AHB masters or not.
  109. * @nomadik: whether this variant is a ST Microelectronics Nomadik, where the
  110. * channels have Nomadik security extension bits that need to be checked
  111. * for permission before use and some registers are missing
  112. * @pl080s: whether this variant is a Samsung PL080S, which has separate
  113. * register and LLI word for transfer size.
  114. * @ftdmac020: whether this variant is a Faraday Technology FTDMAC020
  115. * @max_transfer_size: the maximum single element transfer size for this
  116. * PL08x variant.
  117. */
  118. struct vendor_data {
  119. u8 config_offset;
  120. u8 channels;
  121. u8 signals;
  122. bool dualmaster;
  123. bool nomadik;
  124. bool pl080s;
  125. bool ftdmac020;
  126. u32 max_transfer_size;
  127. };
  128. /**
  129. * struct pl08x_bus_data - information of source or destination
  130. * busses for a transfer
  131. * @addr: current address
  132. * @maxwidth: the maximum width of a transfer on this bus
  133. * @buswidth: the width of this bus in bytes: 1, 2 or 4
  134. */
  135. struct pl08x_bus_data {
  136. dma_addr_t addr;
  137. u8 maxwidth;
  138. u8 buswidth;
  139. };
  140. #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
  141. /**
  142. * struct pl08x_phy_chan - holder for the physical channels
  143. * @id: physical index to this channel
  144. * @base: memory base address for this physical channel
  145. * @reg_config: configuration address for this physical channel
  146. * @reg_control: control address for this physical channel
  147. * @reg_src: transfer source address register
  148. * @reg_dst: transfer destination address register
  149. * @reg_lli: transfer LLI address register
  150. * @reg_busy: if the variant has a special per-channel busy register,
  151. * this contains a pointer to it
  152. * @lock: a lock to use when altering an instance of this struct
  153. * @serving: the virtual channel currently being served by this physical
  154. * channel
  155. * @locked: channel unavailable for the system, e.g. dedicated to secure
  156. * world
  157. * @ftdmac020: channel is on a FTDMAC020
  158. * @pl080s: channel is on a PL08s
  159. */
  160. struct pl08x_phy_chan {
  161. unsigned int id;
  162. void __iomem *base;
  163. void __iomem *reg_config;
  164. void __iomem *reg_control;
  165. void __iomem *reg_src;
  166. void __iomem *reg_dst;
  167. void __iomem *reg_lli;
  168. void __iomem *reg_busy;
  169. spinlock_t lock;
  170. struct pl08x_dma_chan *serving;
  171. bool locked;
  172. bool ftdmac020;
  173. bool pl080s;
  174. };
  175. /**
  176. * struct pl08x_sg - structure containing data per sg
  177. * @src_addr: src address of sg
  178. * @dst_addr: dst address of sg
  179. * @len: transfer len in bytes
  180. * @node: node for txd's dsg_list
  181. */
  182. struct pl08x_sg {
  183. dma_addr_t src_addr;
  184. dma_addr_t dst_addr;
  185. size_t len;
  186. struct list_head node;
  187. };
  188. /**
  189. * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
  190. * @vd: virtual DMA descriptor
  191. * @dsg_list: list of children sg's
  192. * @llis_bus: DMA memory address (physical) start for the LLIs
  193. * @llis_va: virtual memory address start for the LLIs
  194. * @cctl: control reg values for current txd
  195. * @ccfg: config reg values for current txd
  196. * @done: this marks completed descriptors, which should not have their
  197. * mux released.
  198. * @cyclic: indicate cyclic transfers
  199. */
  200. struct pl08x_txd {
  201. struct virt_dma_desc vd;
  202. struct list_head dsg_list;
  203. dma_addr_t llis_bus;
  204. u32 *llis_va;
  205. /* Default cctl value for LLIs */
  206. u32 cctl;
  207. /*
  208. * Settings to be put into the physical channel when we
  209. * trigger this txd. Other registers are in llis_va[0].
  210. */
  211. u32 ccfg;
  212. bool done;
  213. bool cyclic;
  214. };
  215. /**
  216. * enum pl08x_dma_chan_state - holds the PL08x specific virtual channel
  217. * states
  218. * @PL08X_CHAN_IDLE: the channel is idle
  219. * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
  220. * channel and is running a transfer on it
  221. * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
  222. * channel, but the transfer is currently paused
  223. * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
  224. * channel to become available (only pertains to memcpy channels)
  225. */
  226. enum pl08x_dma_chan_state {
  227. PL08X_CHAN_IDLE,
  228. PL08X_CHAN_RUNNING,
  229. PL08X_CHAN_PAUSED,
  230. PL08X_CHAN_WAITING,
  231. };
  232. /**
  233. * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
  234. * @vc: wrappped virtual channel
  235. * @phychan: the physical channel utilized by this channel, if there is one
  236. * @name: name of channel
  237. * @cd: channel platform data
  238. * @cfg: slave configuration
  239. * @at: active transaction on this channel
  240. * @host: a pointer to the host (internal use)
  241. * @state: whether the channel is idle, paused, running etc
  242. * @slave: whether this channel is a device (slave) or for memcpy
  243. * @signal: the physical DMA request signal which this channel is using
  244. * @mux_use: count of descriptors using this DMA request signal setting
  245. */
  246. struct pl08x_dma_chan {
  247. struct virt_dma_chan vc;
  248. struct pl08x_phy_chan *phychan;
  249. const char *name;
  250. struct pl08x_channel_data *cd;
  251. struct dma_slave_config cfg;
  252. struct pl08x_txd *at;
  253. struct pl08x_driver_data *host;
  254. enum pl08x_dma_chan_state state;
  255. bool slave;
  256. int signal;
  257. unsigned mux_use;
  258. };
  259. /**
  260. * struct pl08x_driver_data - the local state holder for the PL08x
  261. * @slave: optional slave engine for this instance
  262. * @memcpy: memcpy engine for this instance
  263. * @has_slave: the PL08x has a slave engine (routed signals)
  264. * @base: virtual memory base (remapped) for the PL08x
  265. * @adev: the corresponding AMBA (PrimeCell) bus entry
  266. * @vd: vendor data for this PL08x variant
  267. * @pd: platform data passed in from the platform/machine
  268. * @phy_chans: array of data for the physical channels
  269. * @pool: a pool for the LLI descriptors
  270. * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
  271. * fetches
  272. * @mem_buses: set to indicate memory transfers on AHB2.
  273. * @lli_words: how many words are used in each LLI item for this variant
  274. */
  275. struct pl08x_driver_data {
  276. struct dma_device slave;
  277. struct dma_device memcpy;
  278. bool has_slave;
  279. void __iomem *base;
  280. struct amba_device *adev;
  281. const struct vendor_data *vd;
  282. struct pl08x_platform_data *pd;
  283. struct pl08x_phy_chan *phy_chans;
  284. struct dma_pool *pool;
  285. u8 lli_buses;
  286. u8 mem_buses;
  287. u8 lli_words;
  288. };
  289. /*
  290. * PL08X specific defines
  291. */
  292. /* The order of words in an LLI. */
  293. #define PL080_LLI_SRC 0
  294. #define PL080_LLI_DST 1
  295. #define PL080_LLI_LLI 2
  296. #define PL080_LLI_CCTL 3
  297. #define PL080S_LLI_CCTL2 4
  298. /* Total words in an LLI. */
  299. #define PL080_LLI_WORDS 4
  300. #define PL080S_LLI_WORDS 8
  301. /*
  302. * Number of LLIs in each LLI buffer allocated for one transfer
  303. * (maximum times we call dma_pool_alloc on this pool without freeing)
  304. */
  305. #define MAX_NUM_TSFR_LLIS 512
  306. #define PL08X_ALIGN 8
  307. static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
  308. {
  309. return container_of(chan, struct pl08x_dma_chan, vc.chan);
  310. }
  311. static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
  312. {
  313. return container_of(tx, struct pl08x_txd, vd.tx);
  314. }
  315. /*
  316. * Mux handling.
  317. *
  318. * This gives us the DMA request input to the PL08x primecell which the
  319. * peripheral described by the channel data will be routed to, possibly
  320. * via a board/SoC specific external MUX. One important point to note
  321. * here is that this does not depend on the physical channel.
  322. */
  323. static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
  324. {
  325. const struct pl08x_platform_data *pd = plchan->host->pd;
  326. int ret;
  327. if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
  328. ret = pd->get_xfer_signal(plchan->cd);
  329. if (ret < 0) {
  330. plchan->mux_use = 0;
  331. return ret;
  332. }
  333. plchan->signal = ret;
  334. }
  335. return 0;
  336. }
  337. static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
  338. {
  339. const struct pl08x_platform_data *pd = plchan->host->pd;
  340. if (plchan->signal >= 0) {
  341. WARN_ON(plchan->mux_use == 0);
  342. if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
  343. pd->put_xfer_signal(plchan->cd, plchan->signal);
  344. plchan->signal = -1;
  345. }
  346. }
  347. }
  348. /*
  349. * Physical channel handling
  350. */
  351. /* Whether a certain channel is busy or not */
  352. static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
  353. {
  354. unsigned int val;
  355. /* If we have a special busy register, take a shortcut */
  356. if (ch->reg_busy) {
  357. val = readl(ch->reg_busy);
  358. return !!(val & BIT(ch->id));
  359. }
  360. val = readl(ch->reg_config);
  361. return val & PL080_CONFIG_ACTIVE;
  362. }
  363. /*
  364. * pl08x_write_lli() - Write an LLI into the DMA controller.
  365. *
  366. * The PL08x derivatives support linked lists, but the first item of the
  367. * list containing the source, destination, control word and next LLI is
  368. * ignored. Instead the driver has to write those values directly into the
  369. * SRC, DST, LLI and control registers. On FTDMAC020 also the SIZE
  370. * register need to be set up for the first transfer.
  371. */
  372. static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
  373. struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
  374. {
  375. if (pl08x->vd->pl080s)
  376. dev_vdbg(&pl08x->adev->dev,
  377. "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
  378. "clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
  379. phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
  380. lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
  381. lli[PL080S_LLI_CCTL2], ccfg);
  382. else
  383. dev_vdbg(&pl08x->adev->dev,
  384. "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
  385. "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
  386. phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
  387. lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
  388. writel_relaxed(lli[PL080_LLI_SRC], phychan->reg_src);
  389. writel_relaxed(lli[PL080_LLI_DST], phychan->reg_dst);
  390. writel_relaxed(lli[PL080_LLI_LLI], phychan->reg_lli);
  391. /*
  392. * The FTMAC020 has a different layout in the CCTL word of the LLI
  393. * and the CCTL register which is split in CSR and SIZE registers.
  394. * Convert the LLI item CCTL into the proper values to write into
  395. * the CSR and SIZE registers.
  396. */
  397. if (phychan->ftdmac020) {
  398. u32 llictl = lli[PL080_LLI_CCTL];
  399. u32 val = 0;
  400. /* Write the transfer size (12 bits) to the size register */
  401. writel_relaxed(llictl & FTDMAC020_LLI_TRANSFER_SIZE_MASK,
  402. phychan->base + FTDMAC020_CH_SIZE);
  403. /*
  404. * Then write the control bits 28..16 to the control register
  405. * by shuffleing the bits around to where they are in the
  406. * main register. The mapping is as follows:
  407. * Bit 28: TC_MSK - mask on all except last LLI
  408. * Bit 27..25: SRC_WIDTH
  409. * Bit 24..22: DST_WIDTH
  410. * Bit 21..20: SRCAD_CTRL
  411. * Bit 19..17: DSTAD_CTRL
  412. * Bit 17: SRC_SEL
  413. * Bit 16: DST_SEL
  414. */
  415. if (llictl & FTDMAC020_LLI_TC_MSK)
  416. val |= FTDMAC020_CH_CSR_TC_MSK;
  417. val |= ((llictl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
  418. (FTDMAC020_LLI_SRC_WIDTH_SHIFT -
  419. FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT));
  420. val |= ((llictl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
  421. (FTDMAC020_LLI_DST_WIDTH_SHIFT -
  422. FTDMAC020_CH_CSR_DST_WIDTH_SHIFT));
  423. val |= ((llictl & FTDMAC020_LLI_SRCAD_CTL_MSK) >>
  424. (FTDMAC020_LLI_SRCAD_CTL_SHIFT -
  425. FTDMAC020_CH_CSR_SRCAD_CTL_SHIFT));
  426. val |= ((llictl & FTDMAC020_LLI_DSTAD_CTL_MSK) >>
  427. (FTDMAC020_LLI_DSTAD_CTL_SHIFT -
  428. FTDMAC020_CH_CSR_DSTAD_CTL_SHIFT));
  429. if (llictl & FTDMAC020_LLI_SRC_SEL)
  430. val |= FTDMAC020_CH_CSR_SRC_SEL;
  431. if (llictl & FTDMAC020_LLI_DST_SEL)
  432. val |= FTDMAC020_CH_CSR_DST_SEL;
  433. /*
  434. * Set up the bits that exist in the CSR but are not
  435. * part the LLI, i.e. only gets written to the control
  436. * register right here.
  437. *
  438. * FIXME: do not just handle memcpy, also handle slave DMA.
  439. */
  440. switch (pl08x->pd->memcpy_burst_size) {
  441. default:
  442. case PL08X_BURST_SZ_1:
  443. val |= PL080_BSIZE_1 <<
  444. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  445. break;
  446. case PL08X_BURST_SZ_4:
  447. val |= PL080_BSIZE_4 <<
  448. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  449. break;
  450. case PL08X_BURST_SZ_8:
  451. val |= PL080_BSIZE_8 <<
  452. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  453. break;
  454. case PL08X_BURST_SZ_16:
  455. val |= PL080_BSIZE_16 <<
  456. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  457. break;
  458. case PL08X_BURST_SZ_32:
  459. val |= PL080_BSIZE_32 <<
  460. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  461. break;
  462. case PL08X_BURST_SZ_64:
  463. val |= PL080_BSIZE_64 <<
  464. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  465. break;
  466. case PL08X_BURST_SZ_128:
  467. val |= PL080_BSIZE_128 <<
  468. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  469. break;
  470. case PL08X_BURST_SZ_256:
  471. val |= PL080_BSIZE_256 <<
  472. FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
  473. break;
  474. }
  475. /* Protection flags */
  476. if (pl08x->pd->memcpy_prot_buff)
  477. val |= FTDMAC020_CH_CSR_PROT2;
  478. if (pl08x->pd->memcpy_prot_cache)
  479. val |= FTDMAC020_CH_CSR_PROT3;
  480. /* We are the kernel, so we are in privileged mode */
  481. val |= FTDMAC020_CH_CSR_PROT1;
  482. writel_relaxed(val, phychan->reg_control);
  483. } else {
  484. /* Bits are just identical */
  485. writel_relaxed(lli[PL080_LLI_CCTL], phychan->reg_control);
  486. }
  487. /* Second control word on the PL080s */
  488. if (pl08x->vd->pl080s)
  489. writel_relaxed(lli[PL080S_LLI_CCTL2],
  490. phychan->base + PL080S_CH_CONTROL2);
  491. writel(ccfg, phychan->reg_config);
  492. }
  493. /*
  494. * Set the initial DMA register values i.e. those for the first LLI
  495. * The next LLI pointer and the configuration interrupt bit have
  496. * been set when the LLIs were constructed. Poke them into the hardware
  497. * and start the transfer.
  498. */
  499. static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
  500. {
  501. struct pl08x_driver_data *pl08x = plchan->host;
  502. struct pl08x_phy_chan *phychan = plchan->phychan;
  503. struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
  504. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  505. u32 val;
  506. list_del(&txd->vd.node);
  507. plchan->at = txd;
  508. /* Wait for channel inactive */
  509. while (pl08x_phy_channel_busy(phychan))
  510. cpu_relax();
  511. pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
  512. /* Enable the DMA channel */
  513. /* Do not access config register until channel shows as disabled */
  514. while (readl(pl08x->base + PL080_EN_CHAN) & BIT(phychan->id))
  515. cpu_relax();
  516. /* Do not access config register until channel shows as inactive */
  517. if (phychan->ftdmac020) {
  518. val = readl(phychan->reg_config);
  519. while (val & FTDMAC020_CH_CFG_BUSY)
  520. val = readl(phychan->reg_config);
  521. val = readl(phychan->reg_control);
  522. while (val & FTDMAC020_CH_CSR_EN)
  523. val = readl(phychan->reg_control);
  524. writel(val | FTDMAC020_CH_CSR_EN,
  525. phychan->reg_control);
  526. } else {
  527. val = readl(phychan->reg_config);
  528. while ((val & PL080_CONFIG_ACTIVE) ||
  529. (val & PL080_CONFIG_ENABLE))
  530. val = readl(phychan->reg_config);
  531. writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
  532. }
  533. }
  534. /*
  535. * Pause the channel by setting the HALT bit.
  536. *
  537. * For M->P transfers, pause the DMAC first and then stop the peripheral -
  538. * the FIFO can only drain if the peripheral is still requesting data.
  539. * (note: this can still timeout if the DMAC FIFO never drains of data.)
  540. *
  541. * For P->M transfers, disable the peripheral first to stop it filling
  542. * the DMAC FIFO, and then pause the DMAC.
  543. */
  544. static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
  545. {
  546. u32 val;
  547. int timeout;
  548. if (ch->ftdmac020) {
  549. /* Use the enable bit on the FTDMAC020 */
  550. val = readl(ch->reg_control);
  551. val &= ~FTDMAC020_CH_CSR_EN;
  552. writel(val, ch->reg_control);
  553. return;
  554. }
  555. /* Set the HALT bit and wait for the FIFO to drain */
  556. val = readl(ch->reg_config);
  557. val |= PL080_CONFIG_HALT;
  558. writel(val, ch->reg_config);
  559. /* Wait for channel inactive */
  560. for (timeout = 1000; timeout; timeout--) {
  561. if (!pl08x_phy_channel_busy(ch))
  562. break;
  563. udelay(1);
  564. }
  565. if (pl08x_phy_channel_busy(ch))
  566. pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
  567. }
  568. static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
  569. {
  570. u32 val;
  571. /* Use the enable bit on the FTDMAC020 */
  572. if (ch->ftdmac020) {
  573. val = readl(ch->reg_control);
  574. val |= FTDMAC020_CH_CSR_EN;
  575. writel(val, ch->reg_control);
  576. return;
  577. }
  578. /* Clear the HALT bit */
  579. val = readl(ch->reg_config);
  580. val &= ~PL080_CONFIG_HALT;
  581. writel(val, ch->reg_config);
  582. }
  583. /*
  584. * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
  585. * clears any pending interrupt status. This should not be used for
  586. * an on-going transfer, but as a method of shutting down a channel
  587. * (eg, when it's no longer used) or terminating a transfer.
  588. */
  589. static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
  590. struct pl08x_phy_chan *ch)
  591. {
  592. u32 val;
  593. /* The layout for the FTDMAC020 is different */
  594. if (ch->ftdmac020) {
  595. /* Disable all interrupts */
  596. val = readl(ch->reg_config);
  597. val |= (FTDMAC020_CH_CFG_INT_ABT_MASK |
  598. FTDMAC020_CH_CFG_INT_ERR_MASK |
  599. FTDMAC020_CH_CFG_INT_TC_MASK);
  600. writel(val, ch->reg_config);
  601. /* Abort and disable channel */
  602. val = readl(ch->reg_control);
  603. val &= ~FTDMAC020_CH_CSR_EN;
  604. val |= FTDMAC020_CH_CSR_ABT;
  605. writel(val, ch->reg_control);
  606. /* Clear ABT and ERR interrupt flags */
  607. writel(BIT(ch->id) | BIT(ch->id + 16),
  608. pl08x->base + PL080_ERR_CLEAR);
  609. writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
  610. return;
  611. }
  612. val = readl(ch->reg_config);
  613. val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
  614. PL080_CONFIG_TC_IRQ_MASK);
  615. writel(val, ch->reg_config);
  616. writel(BIT(ch->id), pl08x->base + PL080_ERR_CLEAR);
  617. writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
  618. }
  619. static u32 get_bytes_in_phy_channel(struct pl08x_phy_chan *ch)
  620. {
  621. u32 val;
  622. u32 bytes;
  623. if (ch->ftdmac020) {
  624. bytes = readl(ch->base + FTDMAC020_CH_SIZE);
  625. val = readl(ch->reg_control);
  626. val &= FTDMAC020_CH_CSR_SRC_WIDTH_MSK;
  627. val >>= FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT;
  628. } else if (ch->pl080s) {
  629. val = readl(ch->base + PL080S_CH_CONTROL2);
  630. bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
  631. val = readl(ch->reg_control);
  632. val &= PL080_CONTROL_SWIDTH_MASK;
  633. val >>= PL080_CONTROL_SWIDTH_SHIFT;
  634. } else {
  635. /* Plain PL08x */
  636. val = readl(ch->reg_control);
  637. bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
  638. val &= PL080_CONTROL_SWIDTH_MASK;
  639. val >>= PL080_CONTROL_SWIDTH_SHIFT;
  640. }
  641. switch (val) {
  642. case PL080_WIDTH_8BIT:
  643. break;
  644. case PL080_WIDTH_16BIT:
  645. bytes *= 2;
  646. break;
  647. case PL080_WIDTH_32BIT:
  648. bytes *= 4;
  649. break;
  650. }
  651. return bytes;
  652. }
  653. static u32 get_bytes_in_lli(struct pl08x_phy_chan *ch, const u32 *llis_va)
  654. {
  655. u32 val;
  656. u32 bytes;
  657. if (ch->ftdmac020) {
  658. val = llis_va[PL080_LLI_CCTL];
  659. bytes = val & FTDMAC020_LLI_TRANSFER_SIZE_MASK;
  660. val = llis_va[PL080_LLI_CCTL];
  661. val &= FTDMAC020_LLI_SRC_WIDTH_MSK;
  662. val >>= FTDMAC020_LLI_SRC_WIDTH_SHIFT;
  663. } else if (ch->pl080s) {
  664. val = llis_va[PL080S_LLI_CCTL2];
  665. bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
  666. val = llis_va[PL080_LLI_CCTL];
  667. val &= PL080_CONTROL_SWIDTH_MASK;
  668. val >>= PL080_CONTROL_SWIDTH_SHIFT;
  669. } else {
  670. /* Plain PL08x */
  671. val = llis_va[PL080_LLI_CCTL];
  672. bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
  673. val &= PL080_CONTROL_SWIDTH_MASK;
  674. val >>= PL080_CONTROL_SWIDTH_SHIFT;
  675. }
  676. switch (val) {
  677. case PL080_WIDTH_8BIT:
  678. break;
  679. case PL080_WIDTH_16BIT:
  680. bytes *= 2;
  681. break;
  682. case PL080_WIDTH_32BIT:
  683. bytes *= 4;
  684. break;
  685. }
  686. return bytes;
  687. }
  688. /* The channel should be paused when calling this */
  689. static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
  690. {
  691. struct pl08x_driver_data *pl08x = plchan->host;
  692. const u32 *llis_va, *llis_va_limit;
  693. struct pl08x_phy_chan *ch;
  694. dma_addr_t llis_bus;
  695. struct pl08x_txd *txd;
  696. u32 llis_max_words;
  697. size_t bytes;
  698. u32 clli;
  699. ch = plchan->phychan;
  700. txd = plchan->at;
  701. if (!ch || !txd)
  702. return 0;
  703. /*
  704. * Follow the LLIs to get the number of remaining
  705. * bytes in the currently active transaction.
  706. */
  707. clli = readl(ch->reg_lli) & ~PL080_LLI_LM_AHB2;
  708. /* First get the remaining bytes in the active transfer */
  709. bytes = get_bytes_in_phy_channel(ch);
  710. if (!clli)
  711. return bytes;
  712. llis_va = txd->llis_va;
  713. llis_bus = txd->llis_bus;
  714. llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
  715. BUG_ON(clli < llis_bus || clli >= llis_bus +
  716. sizeof(u32) * llis_max_words);
  717. /*
  718. * Locate the next LLI - as this is an array,
  719. * it's simple maths to find.
  720. */
  721. llis_va += (clli - llis_bus) / sizeof(u32);
  722. llis_va_limit = llis_va + llis_max_words;
  723. for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
  724. bytes += get_bytes_in_lli(ch, llis_va);
  725. /*
  726. * A LLI pointer going backward terminates the LLI list
  727. */
  728. if (llis_va[PL080_LLI_LLI] <= clli)
  729. break;
  730. }
  731. return bytes;
  732. }
  733. /*
  734. * Allocate a physical channel for a virtual channel
  735. *
  736. * Try to locate a physical channel to be used for this transfer. If all
  737. * are taken return NULL and the requester will have to cope by using
  738. * some fallback PIO mode or retrying later.
  739. */
  740. static struct pl08x_phy_chan *
  741. pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
  742. struct pl08x_dma_chan *virt_chan)
  743. {
  744. struct pl08x_phy_chan *ch = NULL;
  745. unsigned long flags;
  746. int i;
  747. for (i = 0; i < pl08x->vd->channels; i++) {
  748. ch = &pl08x->phy_chans[i];
  749. spin_lock_irqsave(&ch->lock, flags);
  750. if (!ch->locked && !ch->serving) {
  751. ch->serving = virt_chan;
  752. spin_unlock_irqrestore(&ch->lock, flags);
  753. break;
  754. }
  755. spin_unlock_irqrestore(&ch->lock, flags);
  756. }
  757. if (i == pl08x->vd->channels) {
  758. /* No physical channel available, cope with it */
  759. return NULL;
  760. }
  761. return ch;
  762. }
  763. /* Mark the physical channel as free. Note, this write is atomic. */
  764. static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
  765. struct pl08x_phy_chan *ch)
  766. {
  767. ch->serving = NULL;
  768. }
  769. /*
  770. * Try to allocate a physical channel. When successful, assign it to
  771. * this virtual channel, and initiate the next descriptor. The
  772. * virtual channel lock must be held at this point.
  773. */
  774. static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
  775. {
  776. struct pl08x_driver_data *pl08x = plchan->host;
  777. struct pl08x_phy_chan *ch;
  778. ch = pl08x_get_phy_channel(pl08x, plchan);
  779. if (!ch) {
  780. dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
  781. plchan->state = PL08X_CHAN_WAITING;
  782. return;
  783. }
  784. dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
  785. ch->id, plchan->name);
  786. plchan->phychan = ch;
  787. plchan->state = PL08X_CHAN_RUNNING;
  788. pl08x_start_next_txd(plchan);
  789. }
  790. static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
  791. struct pl08x_dma_chan *plchan)
  792. {
  793. struct pl08x_driver_data *pl08x = plchan->host;
  794. dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
  795. ch->id, plchan->name);
  796. /*
  797. * We do this without taking the lock; we're really only concerned
  798. * about whether this pointer is NULL or not, and we're guaranteed
  799. * that this will only be called when it _already_ is non-NULL.
  800. */
  801. ch->serving = plchan;
  802. plchan->phychan = ch;
  803. plchan->state = PL08X_CHAN_RUNNING;
  804. pl08x_start_next_txd(plchan);
  805. }
  806. /*
  807. * Free a physical DMA channel, potentially reallocating it to another
  808. * virtual channel if we have any pending.
  809. */
  810. static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
  811. {
  812. struct pl08x_driver_data *pl08x = plchan->host;
  813. struct pl08x_dma_chan *p, *next;
  814. retry:
  815. next = NULL;
  816. /* Find a waiting virtual channel for the next transfer. */
  817. list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
  818. if (p->state == PL08X_CHAN_WAITING) {
  819. next = p;
  820. break;
  821. }
  822. if (!next && pl08x->has_slave) {
  823. list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
  824. if (p->state == PL08X_CHAN_WAITING) {
  825. next = p;
  826. break;
  827. }
  828. }
  829. /* Ensure that the physical channel is stopped */
  830. pl08x_terminate_phy_chan(pl08x, plchan->phychan);
  831. if (next) {
  832. bool success;
  833. /*
  834. * Eww. We know this isn't going to deadlock
  835. * but lockdep probably doesn't.
  836. */
  837. spin_lock(&next->vc.lock);
  838. /* Re-check the state now that we have the lock */
  839. success = next->state == PL08X_CHAN_WAITING;
  840. if (success)
  841. pl08x_phy_reassign_start(plchan->phychan, next);
  842. spin_unlock(&next->vc.lock);
  843. /* If the state changed, try to find another channel */
  844. if (!success)
  845. goto retry;
  846. } else {
  847. /* No more jobs, so free up the physical channel */
  848. pl08x_put_phy_channel(pl08x, plchan->phychan);
  849. }
  850. plchan->phychan = NULL;
  851. plchan->state = PL08X_CHAN_IDLE;
  852. }
  853. /*
  854. * LLI handling
  855. */
  856. static inline unsigned int
  857. pl08x_get_bytes_for_lli(struct pl08x_driver_data *pl08x,
  858. u32 cctl,
  859. bool source)
  860. {
  861. u32 val;
  862. if (pl08x->vd->ftdmac020) {
  863. if (source)
  864. val = (cctl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
  865. FTDMAC020_LLI_SRC_WIDTH_SHIFT;
  866. else
  867. val = (cctl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
  868. FTDMAC020_LLI_DST_WIDTH_SHIFT;
  869. } else {
  870. if (source)
  871. val = (cctl & PL080_CONTROL_SWIDTH_MASK) >>
  872. PL080_CONTROL_SWIDTH_SHIFT;
  873. else
  874. val = (cctl & PL080_CONTROL_DWIDTH_MASK) >>
  875. PL080_CONTROL_DWIDTH_SHIFT;
  876. }
  877. switch (val) {
  878. case PL080_WIDTH_8BIT:
  879. return 1;
  880. case PL080_WIDTH_16BIT:
  881. return 2;
  882. case PL080_WIDTH_32BIT:
  883. return 4;
  884. default:
  885. break;
  886. }
  887. BUG();
  888. return 0;
  889. }
  890. static inline u32 pl08x_lli_control_bits(struct pl08x_driver_data *pl08x,
  891. u32 cctl,
  892. u8 srcwidth, u8 dstwidth,
  893. size_t tsize)
  894. {
  895. u32 retbits = cctl;
  896. /*
  897. * Remove all src, dst and transfer size bits, then set the
  898. * width and size according to the parameters. The bit offsets
  899. * are different in the FTDMAC020 so we need to accound for this.
  900. */
  901. if (pl08x->vd->ftdmac020) {
  902. retbits &= ~FTDMAC020_LLI_DST_WIDTH_MSK;
  903. retbits &= ~FTDMAC020_LLI_SRC_WIDTH_MSK;
  904. retbits &= ~FTDMAC020_LLI_TRANSFER_SIZE_MASK;
  905. switch (srcwidth) {
  906. case 1:
  907. retbits |= PL080_WIDTH_8BIT <<
  908. FTDMAC020_LLI_SRC_WIDTH_SHIFT;
  909. break;
  910. case 2:
  911. retbits |= PL080_WIDTH_16BIT <<
  912. FTDMAC020_LLI_SRC_WIDTH_SHIFT;
  913. break;
  914. case 4:
  915. retbits |= PL080_WIDTH_32BIT <<
  916. FTDMAC020_LLI_SRC_WIDTH_SHIFT;
  917. break;
  918. default:
  919. BUG();
  920. break;
  921. }
  922. switch (dstwidth) {
  923. case 1:
  924. retbits |= PL080_WIDTH_8BIT <<
  925. FTDMAC020_LLI_DST_WIDTH_SHIFT;
  926. break;
  927. case 2:
  928. retbits |= PL080_WIDTH_16BIT <<
  929. FTDMAC020_LLI_DST_WIDTH_SHIFT;
  930. break;
  931. case 4:
  932. retbits |= PL080_WIDTH_32BIT <<
  933. FTDMAC020_LLI_DST_WIDTH_SHIFT;
  934. break;
  935. default:
  936. BUG();
  937. break;
  938. }
  939. tsize &= FTDMAC020_LLI_TRANSFER_SIZE_MASK;
  940. retbits |= tsize << FTDMAC020_LLI_TRANSFER_SIZE_SHIFT;
  941. } else {
  942. retbits &= ~PL080_CONTROL_DWIDTH_MASK;
  943. retbits &= ~PL080_CONTROL_SWIDTH_MASK;
  944. retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
  945. switch (srcwidth) {
  946. case 1:
  947. retbits |= PL080_WIDTH_8BIT <<
  948. PL080_CONTROL_SWIDTH_SHIFT;
  949. break;
  950. case 2:
  951. retbits |= PL080_WIDTH_16BIT <<
  952. PL080_CONTROL_SWIDTH_SHIFT;
  953. break;
  954. case 4:
  955. retbits |= PL080_WIDTH_32BIT <<
  956. PL080_CONTROL_SWIDTH_SHIFT;
  957. break;
  958. default:
  959. BUG();
  960. break;
  961. }
  962. switch (dstwidth) {
  963. case 1:
  964. retbits |= PL080_WIDTH_8BIT <<
  965. PL080_CONTROL_DWIDTH_SHIFT;
  966. break;
  967. case 2:
  968. retbits |= PL080_WIDTH_16BIT <<
  969. PL080_CONTROL_DWIDTH_SHIFT;
  970. break;
  971. case 4:
  972. retbits |= PL080_WIDTH_32BIT <<
  973. PL080_CONTROL_DWIDTH_SHIFT;
  974. break;
  975. default:
  976. BUG();
  977. break;
  978. }
  979. tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
  980. retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
  981. }
  982. return retbits;
  983. }
  984. struct pl08x_lli_build_data {
  985. struct pl08x_txd *txd;
  986. struct pl08x_bus_data srcbus;
  987. struct pl08x_bus_data dstbus;
  988. size_t remainder;
  989. u32 lli_bus;
  990. };
  991. /*
  992. * Autoselect a master bus to use for the transfer. Slave will be the chosen as
  993. * victim in case src & dest are not similarly aligned. i.e. If after aligning
  994. * masters address with width requirements of transfer (by sending few byte by
  995. * byte data), slave is still not aligned, then its width will be reduced to
  996. * BYTE.
  997. * - prefers the destination bus if both available
  998. * - prefers bus with fixed address (i.e. peripheral)
  999. */
  1000. static void pl08x_choose_master_bus(struct pl08x_driver_data *pl08x,
  1001. struct pl08x_lli_build_data *bd,
  1002. struct pl08x_bus_data **mbus,
  1003. struct pl08x_bus_data **sbus,
  1004. u32 cctl)
  1005. {
  1006. bool dst_incr;
  1007. bool src_incr;
  1008. /*
  1009. * The FTDMAC020 only supports memory-to-memory transfer, so
  1010. * source and destination always increase.
  1011. */
  1012. if (pl08x->vd->ftdmac020) {
  1013. dst_incr = true;
  1014. src_incr = true;
  1015. } else {
  1016. dst_incr = !!(cctl & PL080_CONTROL_DST_INCR);
  1017. src_incr = !!(cctl & PL080_CONTROL_SRC_INCR);
  1018. }
  1019. /*
  1020. * If either bus is not advancing, i.e. it is a peripheral, that
  1021. * one becomes master
  1022. */
  1023. if (!dst_incr) {
  1024. *mbus = &bd->dstbus;
  1025. *sbus = &bd->srcbus;
  1026. } else if (!src_incr) {
  1027. *mbus = &bd->srcbus;
  1028. *sbus = &bd->dstbus;
  1029. } else {
  1030. if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
  1031. *mbus = &bd->dstbus;
  1032. *sbus = &bd->srcbus;
  1033. } else {
  1034. *mbus = &bd->srcbus;
  1035. *sbus = &bd->dstbus;
  1036. }
  1037. }
  1038. }
  1039. /*
  1040. * Fills in one LLI for a certain transfer descriptor and advance the counter
  1041. */
  1042. static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
  1043. struct pl08x_lli_build_data *bd,
  1044. int num_llis, int len, u32 cctl, u32 cctl2)
  1045. {
  1046. u32 offset = num_llis * pl08x->lli_words;
  1047. u32 *llis_va = bd->txd->llis_va + offset;
  1048. dma_addr_t llis_bus = bd->txd->llis_bus;
  1049. BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
  1050. /* Advance the offset to next LLI. */
  1051. offset += pl08x->lli_words;
  1052. llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
  1053. llis_va[PL080_LLI_DST] = bd->dstbus.addr;
  1054. llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
  1055. llis_va[PL080_LLI_LLI] |= bd->lli_bus;
  1056. llis_va[PL080_LLI_CCTL] = cctl;
  1057. if (pl08x->vd->pl080s)
  1058. llis_va[PL080S_LLI_CCTL2] = cctl2;
  1059. if (pl08x->vd->ftdmac020) {
  1060. /* FIXME: only memcpy so far so both increase */
  1061. bd->srcbus.addr += len;
  1062. bd->dstbus.addr += len;
  1063. } else {
  1064. if (cctl & PL080_CONTROL_SRC_INCR)
  1065. bd->srcbus.addr += len;
  1066. if (cctl & PL080_CONTROL_DST_INCR)
  1067. bd->dstbus.addr += len;
  1068. }
  1069. BUG_ON(bd->remainder < len);
  1070. bd->remainder -= len;
  1071. }
  1072. static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
  1073. struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
  1074. int num_llis, size_t *total_bytes)
  1075. {
  1076. *cctl = pl08x_lli_control_bits(pl08x, *cctl, 1, 1, len);
  1077. pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
  1078. (*total_bytes) += len;
  1079. }
  1080. #if 1
  1081. static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
  1082. const u32 *llis_va, int num_llis)
  1083. {
  1084. int i;
  1085. if (pl08x->vd->pl080s) {
  1086. dev_vdbg(&pl08x->adev->dev,
  1087. "%-3s %-9s %-10s %-10s %-10s %-10s %s\n",
  1088. "lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
  1089. for (i = 0; i < num_llis; i++) {
  1090. dev_vdbg(&pl08x->adev->dev,
  1091. "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1092. i, llis_va, llis_va[PL080_LLI_SRC],
  1093. llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
  1094. llis_va[PL080_LLI_CCTL],
  1095. llis_va[PL080S_LLI_CCTL2]);
  1096. llis_va += pl08x->lli_words;
  1097. }
  1098. } else {
  1099. dev_vdbg(&pl08x->adev->dev,
  1100. "%-3s %-9s %-10s %-10s %-10s %s\n",
  1101. "lli", "", "csrc", "cdst", "clli", "cctl");
  1102. for (i = 0; i < num_llis; i++) {
  1103. dev_vdbg(&pl08x->adev->dev,
  1104. "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1105. i, llis_va, llis_va[PL080_LLI_SRC],
  1106. llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
  1107. llis_va[PL080_LLI_CCTL]);
  1108. llis_va += pl08x->lli_words;
  1109. }
  1110. }
  1111. }
  1112. #else
  1113. static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
  1114. const u32 *llis_va, int num_llis) {}
  1115. #endif
  1116. /*
  1117. * This fills in the table of LLIs for the transfer descriptor
  1118. * Note that we assume we never have to change the burst sizes
  1119. * Return 0 for error
  1120. */
  1121. static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
  1122. struct pl08x_txd *txd)
  1123. {
  1124. struct pl08x_bus_data *mbus, *sbus;
  1125. struct pl08x_lli_build_data bd;
  1126. int num_llis = 0;
  1127. u32 cctl, early_bytes = 0;
  1128. size_t max_bytes_per_lli, total_bytes;
  1129. u32 *llis_va, *last_lli;
  1130. struct pl08x_sg *dsg;
  1131. txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
  1132. if (!txd->llis_va) {
  1133. dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
  1134. return 0;
  1135. }
  1136. bd.txd = txd;
  1137. bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
  1138. cctl = txd->cctl;
  1139. /* Find maximum width of the source bus */
  1140. bd.srcbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, true);
  1141. /* Find maximum width of the destination bus */
  1142. bd.dstbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, false);
  1143. list_for_each_entry(dsg, &txd->dsg_list, node) {
  1144. total_bytes = 0;
  1145. cctl = txd->cctl;
  1146. bd.srcbus.addr = dsg->src_addr;
  1147. bd.dstbus.addr = dsg->dst_addr;
  1148. bd.remainder = dsg->len;
  1149. bd.srcbus.buswidth = bd.srcbus.maxwidth;
  1150. bd.dstbus.buswidth = bd.dstbus.maxwidth;
  1151. pl08x_choose_master_bus(pl08x, &bd, &mbus, &sbus, cctl);
  1152. dev_vdbg(&pl08x->adev->dev,
  1153. "src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
  1154. (u64)bd.srcbus.addr,
  1155. cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
  1156. bd.srcbus.buswidth,
  1157. (u64)bd.dstbus.addr,
  1158. cctl & PL080_CONTROL_DST_INCR ? "+" : "",
  1159. bd.dstbus.buswidth,
  1160. bd.remainder);
  1161. dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
  1162. mbus == &bd.srcbus ? "src" : "dst",
  1163. sbus == &bd.srcbus ? "src" : "dst");
  1164. /*
  1165. * Zero length is only allowed if all these requirements are
  1166. * met:
  1167. * - flow controller is peripheral.
  1168. * - src.addr is aligned to src.width
  1169. * - dst.addr is aligned to dst.width
  1170. *
  1171. * sg_len == 1 should be true, as there can be two cases here:
  1172. *
  1173. * - Memory addresses are contiguous and are not scattered.
  1174. * Here, Only one sg will be passed by user driver, with
  1175. * memory address and zero length. We pass this to controller
  1176. * and after the transfer it will receive the last burst
  1177. * request from peripheral and so transfer finishes.
  1178. *
  1179. * - Memory addresses are scattered and are not contiguous.
  1180. * Here, Obviously as DMA controller doesn't know when a lli's
  1181. * transfer gets over, it can't load next lli. So in this
  1182. * case, there has to be an assumption that only one lli is
  1183. * supported. Thus, we can't have scattered addresses.
  1184. */
  1185. if (!bd.remainder) {
  1186. u32 fc;
  1187. /* FTDMAC020 only does memory-to-memory */
  1188. if (pl08x->vd->ftdmac020)
  1189. fc = PL080_FLOW_MEM2MEM;
  1190. else
  1191. fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
  1192. PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1193. if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
  1194. (fc <= PL080_FLOW_SRC2DST_SRC))) {
  1195. dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
  1196. __func__);
  1197. return 0;
  1198. }
  1199. if (!IS_BUS_ALIGNED(&bd.srcbus) ||
  1200. !IS_BUS_ALIGNED(&bd.dstbus)) {
  1201. dev_err(&pl08x->adev->dev,
  1202. "%s src & dst address must be aligned to src"
  1203. " & dst width if peripheral is flow controller",
  1204. __func__);
  1205. return 0;
  1206. }
  1207. cctl = pl08x_lli_control_bits(pl08x, cctl,
  1208. bd.srcbus.buswidth, bd.dstbus.buswidth,
  1209. 0);
  1210. pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
  1211. 0, cctl, 0);
  1212. break;
  1213. }
  1214. /*
  1215. * Send byte by byte for following cases
  1216. * - Less than a bus width available
  1217. * - until master bus is aligned
  1218. */
  1219. if (bd.remainder < mbus->buswidth)
  1220. early_bytes = bd.remainder;
  1221. else if (!IS_BUS_ALIGNED(mbus)) {
  1222. early_bytes = mbus->buswidth -
  1223. (mbus->addr & (mbus->buswidth - 1));
  1224. if ((bd.remainder - early_bytes) < mbus->buswidth)
  1225. early_bytes = bd.remainder;
  1226. }
  1227. if (early_bytes) {
  1228. dev_vdbg(&pl08x->adev->dev,
  1229. "%s byte width LLIs (remain 0x%08zx)\n",
  1230. __func__, bd.remainder);
  1231. prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
  1232. num_llis++, &total_bytes);
  1233. }
  1234. if (bd.remainder) {
  1235. /*
  1236. * Master now aligned
  1237. * - if slave is not then we must set its width down
  1238. */
  1239. if (!IS_BUS_ALIGNED(sbus)) {
  1240. dev_dbg(&pl08x->adev->dev,
  1241. "%s set down bus width to one byte\n",
  1242. __func__);
  1243. sbus->buswidth = 1;
  1244. }
  1245. /*
  1246. * Bytes transferred = tsize * src width, not
  1247. * MIN(buswidths)
  1248. */
  1249. max_bytes_per_lli = bd.srcbus.buswidth *
  1250. pl08x->vd->max_transfer_size;
  1251. dev_vdbg(&pl08x->adev->dev,
  1252. "%s max bytes per lli = %zu\n",
  1253. __func__, max_bytes_per_lli);
  1254. /*
  1255. * Make largest possible LLIs until less than one bus
  1256. * width left
  1257. */
  1258. while (bd.remainder > (mbus->buswidth - 1)) {
  1259. size_t lli_len, tsize, width;
  1260. /*
  1261. * If enough left try to send max possible,
  1262. * otherwise try to send the remainder
  1263. */
  1264. lli_len = min(bd.remainder, max_bytes_per_lli);
  1265. /*
  1266. * Check against maximum bus alignment:
  1267. * Calculate actual transfer size in relation to
  1268. * bus width an get a maximum remainder of the
  1269. * highest bus width - 1
  1270. */
  1271. width = max(mbus->buswidth, sbus->buswidth);
  1272. lli_len = (lli_len / width) * width;
  1273. tsize = lli_len / bd.srcbus.buswidth;
  1274. dev_vdbg(&pl08x->adev->dev,
  1275. "%s fill lli with single lli chunk of "
  1276. "size 0x%08zx (remainder 0x%08zx)\n",
  1277. __func__, lli_len, bd.remainder);
  1278. cctl = pl08x_lli_control_bits(pl08x, cctl,
  1279. bd.srcbus.buswidth, bd.dstbus.buswidth,
  1280. tsize);
  1281. pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
  1282. lli_len, cctl, tsize);
  1283. total_bytes += lli_len;
  1284. }
  1285. /*
  1286. * Send any odd bytes
  1287. */
  1288. if (bd.remainder) {
  1289. dev_vdbg(&pl08x->adev->dev,
  1290. "%s align with boundary, send odd bytes (remain %zu)\n",
  1291. __func__, bd.remainder);
  1292. prep_byte_width_lli(pl08x, &bd, &cctl,
  1293. bd.remainder, num_llis++, &total_bytes);
  1294. }
  1295. }
  1296. if (total_bytes != dsg->len) {
  1297. dev_err(&pl08x->adev->dev,
  1298. "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
  1299. __func__, total_bytes, dsg->len);
  1300. return 0;
  1301. }
  1302. if (num_llis >= MAX_NUM_TSFR_LLIS) {
  1303. dev_err(&pl08x->adev->dev,
  1304. "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
  1305. __func__, MAX_NUM_TSFR_LLIS);
  1306. return 0;
  1307. }
  1308. }
  1309. llis_va = txd->llis_va;
  1310. last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
  1311. if (txd->cyclic) {
  1312. /* Link back to the first LLI. */
  1313. last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
  1314. } else {
  1315. /* The final LLI terminates the LLI. */
  1316. last_lli[PL080_LLI_LLI] = 0;
  1317. /* The final LLI element shall also fire an interrupt. */
  1318. if (pl08x->vd->ftdmac020)
  1319. last_lli[PL080_LLI_CCTL] &= ~FTDMAC020_LLI_TC_MSK;
  1320. else
  1321. last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
  1322. }
  1323. pl08x_dump_lli(pl08x, llis_va, num_llis);
  1324. return num_llis;
  1325. }
  1326. static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
  1327. struct pl08x_txd *txd)
  1328. {
  1329. struct pl08x_sg *dsg, *_dsg;
  1330. if (txd->llis_va)
  1331. dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
  1332. list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
  1333. list_del(&dsg->node);
  1334. kfree(dsg);
  1335. }
  1336. kfree(txd);
  1337. }
  1338. static void pl08x_desc_free(struct virt_dma_desc *vd)
  1339. {
  1340. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  1341. struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
  1342. dma_descriptor_unmap(&vd->tx);
  1343. if (!txd->done)
  1344. pl08x_release_mux(plchan);
  1345. pl08x_free_txd(plchan->host, txd);
  1346. }
  1347. static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
  1348. struct pl08x_dma_chan *plchan)
  1349. {
  1350. LIST_HEAD(head);
  1351. vchan_get_all_descriptors(&plchan->vc, &head);
  1352. vchan_dma_desc_free_list(&plchan->vc, &head);
  1353. }
  1354. /*
  1355. * The DMA ENGINE API
  1356. */
  1357. static void pl08x_free_chan_resources(struct dma_chan *chan)
  1358. {
  1359. /* Ensure all queued descriptors are freed */
  1360. vchan_free_chan_resources(to_virt_chan(chan));
  1361. }
  1362. static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
  1363. struct dma_chan *chan, unsigned long flags)
  1364. {
  1365. struct dma_async_tx_descriptor *retval = NULL;
  1366. return retval;
  1367. }
  1368. /*
  1369. * Code accessing dma_async_is_complete() in a tight loop may give problems.
  1370. * If slaves are relying on interrupts to signal completion this function
  1371. * must not be called with interrupts disabled.
  1372. */
  1373. static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
  1374. dma_cookie_t cookie, struct dma_tx_state *txstate)
  1375. {
  1376. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1377. struct virt_dma_desc *vd;
  1378. unsigned long flags;
  1379. enum dma_status ret;
  1380. size_t bytes = 0;
  1381. ret = dma_cookie_status(chan, cookie, txstate);
  1382. if (ret == DMA_COMPLETE)
  1383. return ret;
  1384. /*
  1385. * There's no point calculating the residue if there's
  1386. * no txstate to store the value.
  1387. */
  1388. if (!txstate) {
  1389. if (plchan->state == PL08X_CHAN_PAUSED)
  1390. ret = DMA_PAUSED;
  1391. return ret;
  1392. }
  1393. spin_lock_irqsave(&plchan->vc.lock, flags);
  1394. ret = dma_cookie_status(chan, cookie, txstate);
  1395. if (ret != DMA_COMPLETE) {
  1396. vd = vchan_find_desc(&plchan->vc, cookie);
  1397. if (vd) {
  1398. /* On the issued list, so hasn't been processed yet */
  1399. struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
  1400. struct pl08x_sg *dsg;
  1401. list_for_each_entry(dsg, &txd->dsg_list, node)
  1402. bytes += dsg->len;
  1403. } else {
  1404. bytes = pl08x_getbytes_chan(plchan);
  1405. }
  1406. }
  1407. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1408. /*
  1409. * This cookie not complete yet
  1410. * Get number of bytes left in the active transactions and queue
  1411. */
  1412. dma_set_residue(txstate, bytes);
  1413. if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
  1414. ret = DMA_PAUSED;
  1415. /* Whether waiting or running, we're in progress */
  1416. return ret;
  1417. }
  1418. /* PrimeCell DMA extension */
  1419. struct burst_table {
  1420. u32 burstwords;
  1421. u32 reg;
  1422. };
  1423. static const struct burst_table burst_sizes[] = {
  1424. {
  1425. .burstwords = 256,
  1426. .reg = PL080_BSIZE_256,
  1427. },
  1428. {
  1429. .burstwords = 128,
  1430. .reg = PL080_BSIZE_128,
  1431. },
  1432. {
  1433. .burstwords = 64,
  1434. .reg = PL080_BSIZE_64,
  1435. },
  1436. {
  1437. .burstwords = 32,
  1438. .reg = PL080_BSIZE_32,
  1439. },
  1440. {
  1441. .burstwords = 16,
  1442. .reg = PL080_BSIZE_16,
  1443. },
  1444. {
  1445. .burstwords = 8,
  1446. .reg = PL080_BSIZE_8,
  1447. },
  1448. {
  1449. .burstwords = 4,
  1450. .reg = PL080_BSIZE_4,
  1451. },
  1452. {
  1453. .burstwords = 0,
  1454. .reg = PL080_BSIZE_1,
  1455. },
  1456. };
  1457. /*
  1458. * Given the source and destination available bus masks, select which
  1459. * will be routed to each port. We try to have source and destination
  1460. * on separate ports, but always respect the allowable settings.
  1461. */
  1462. static u32 pl08x_select_bus(bool ftdmac020, u8 src, u8 dst)
  1463. {
  1464. u32 cctl = 0;
  1465. u32 dst_ahb2;
  1466. u32 src_ahb2;
  1467. /* The FTDMAC020 use different bits to indicate src/dst bus */
  1468. if (ftdmac020) {
  1469. dst_ahb2 = FTDMAC020_LLI_DST_SEL;
  1470. src_ahb2 = FTDMAC020_LLI_SRC_SEL;
  1471. } else {
  1472. dst_ahb2 = PL080_CONTROL_DST_AHB2;
  1473. src_ahb2 = PL080_CONTROL_SRC_AHB2;
  1474. }
  1475. if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
  1476. cctl |= dst_ahb2;
  1477. if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
  1478. cctl |= src_ahb2;
  1479. return cctl;
  1480. }
  1481. static u32 pl08x_cctl(u32 cctl)
  1482. {
  1483. cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
  1484. PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
  1485. PL080_CONTROL_PROT_MASK);
  1486. /* Access the cell in privileged mode, non-bufferable, non-cacheable */
  1487. return cctl | PL080_CONTROL_PROT_SYS;
  1488. }
  1489. static u32 pl08x_width(enum dma_slave_buswidth width)
  1490. {
  1491. switch (width) {
  1492. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1493. return PL080_WIDTH_8BIT;
  1494. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1495. return PL080_WIDTH_16BIT;
  1496. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1497. return PL080_WIDTH_32BIT;
  1498. default:
  1499. return ~0;
  1500. }
  1501. }
  1502. static u32 pl08x_burst(u32 maxburst)
  1503. {
  1504. int i;
  1505. for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
  1506. if (burst_sizes[i].burstwords <= maxburst)
  1507. break;
  1508. return burst_sizes[i].reg;
  1509. }
  1510. static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
  1511. enum dma_slave_buswidth addr_width, u32 maxburst)
  1512. {
  1513. u32 width, burst, cctl = 0;
  1514. width = pl08x_width(addr_width);
  1515. if (width == ~0)
  1516. return ~0;
  1517. cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
  1518. cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
  1519. /*
  1520. * If this channel will only request single transfers, set this
  1521. * down to ONE element. Also select one element if no maxburst
  1522. * is specified.
  1523. */
  1524. if (plchan->cd->single)
  1525. maxburst = 1;
  1526. burst = pl08x_burst(maxburst);
  1527. cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
  1528. cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
  1529. return pl08x_cctl(cctl);
  1530. }
  1531. /*
  1532. * Slave transactions callback to the slave device to allow
  1533. * synchronization of slave DMA signals with the DMAC enable
  1534. */
  1535. static void pl08x_issue_pending(struct dma_chan *chan)
  1536. {
  1537. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1538. unsigned long flags;
  1539. spin_lock_irqsave(&plchan->vc.lock, flags);
  1540. if (vchan_issue_pending(&plchan->vc)) {
  1541. if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
  1542. pl08x_phy_alloc_and_start(plchan);
  1543. }
  1544. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1545. }
  1546. static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
  1547. {
  1548. struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  1549. if (txd)
  1550. INIT_LIST_HEAD(&txd->dsg_list);
  1551. return txd;
  1552. }
  1553. static u32 pl08x_memcpy_cctl(struct pl08x_driver_data *pl08x)
  1554. {
  1555. u32 cctl = 0;
  1556. /* Conjure cctl */
  1557. switch (pl08x->pd->memcpy_burst_size) {
  1558. default:
  1559. dev_err(&pl08x->adev->dev,
  1560. "illegal burst size for memcpy, set to 1\n");
  1561. /* Fall through */
  1562. case PL08X_BURST_SZ_1:
  1563. cctl |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
  1564. PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
  1565. break;
  1566. case PL08X_BURST_SZ_4:
  1567. cctl |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
  1568. PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
  1569. break;
  1570. case PL08X_BURST_SZ_8:
  1571. cctl |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
  1572. PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
  1573. break;
  1574. case PL08X_BURST_SZ_16:
  1575. cctl |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
  1576. PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
  1577. break;
  1578. case PL08X_BURST_SZ_32:
  1579. cctl |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
  1580. PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
  1581. break;
  1582. case PL08X_BURST_SZ_64:
  1583. cctl |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
  1584. PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
  1585. break;
  1586. case PL08X_BURST_SZ_128:
  1587. cctl |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
  1588. PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
  1589. break;
  1590. case PL08X_BURST_SZ_256:
  1591. cctl |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
  1592. PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
  1593. break;
  1594. }
  1595. switch (pl08x->pd->memcpy_bus_width) {
  1596. default:
  1597. dev_err(&pl08x->adev->dev,
  1598. "illegal bus width for memcpy, set to 8 bits\n");
  1599. /* Fall through */
  1600. case PL08X_BUS_WIDTH_8_BITS:
  1601. cctl |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1602. PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1603. break;
  1604. case PL08X_BUS_WIDTH_16_BITS:
  1605. cctl |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1606. PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1607. break;
  1608. case PL08X_BUS_WIDTH_32_BITS:
  1609. cctl |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
  1610. PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
  1611. break;
  1612. }
  1613. /* Protection flags */
  1614. if (pl08x->pd->memcpy_prot_buff)
  1615. cctl |= PL080_CONTROL_PROT_BUFF;
  1616. if (pl08x->pd->memcpy_prot_cache)
  1617. cctl |= PL080_CONTROL_PROT_CACHE;
  1618. /* We are the kernel, so we are in privileged mode */
  1619. cctl |= PL080_CONTROL_PROT_SYS;
  1620. /* Both to be incremented or the code will break */
  1621. cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
  1622. if (pl08x->vd->dualmaster)
  1623. cctl |= pl08x_select_bus(false,
  1624. pl08x->mem_buses,
  1625. pl08x->mem_buses);
  1626. return cctl;
  1627. }
  1628. static u32 pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data *pl08x)
  1629. {
  1630. u32 cctl = 0;
  1631. /* Conjure cctl */
  1632. switch (pl08x->pd->memcpy_bus_width) {
  1633. default:
  1634. dev_err(&pl08x->adev->dev,
  1635. "illegal bus width for memcpy, set to 8 bits\n");
  1636. /* Fall through */
  1637. case PL08X_BUS_WIDTH_8_BITS:
  1638. cctl |= PL080_WIDTH_8BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
  1639. PL080_WIDTH_8BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
  1640. break;
  1641. case PL08X_BUS_WIDTH_16_BITS:
  1642. cctl |= PL080_WIDTH_16BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
  1643. PL080_WIDTH_16BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
  1644. break;
  1645. case PL08X_BUS_WIDTH_32_BITS:
  1646. cctl |= PL080_WIDTH_32BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
  1647. PL080_WIDTH_32BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
  1648. break;
  1649. }
  1650. /*
  1651. * By default mask the TC IRQ on all LLIs, it will be unmasked on
  1652. * the last LLI item by other code.
  1653. */
  1654. cctl |= FTDMAC020_LLI_TC_MSK;
  1655. /*
  1656. * Both to be incremented so leave bits FTDMAC020_LLI_SRCAD_CTL
  1657. * and FTDMAC020_LLI_DSTAD_CTL as zero
  1658. */
  1659. if (pl08x->vd->dualmaster)
  1660. cctl |= pl08x_select_bus(true,
  1661. pl08x->mem_buses,
  1662. pl08x->mem_buses);
  1663. return cctl;
  1664. }
  1665. /*
  1666. * Initialize a descriptor to be used by memcpy submit
  1667. */
  1668. static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
  1669. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  1670. size_t len, unsigned long flags)
  1671. {
  1672. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1673. struct pl08x_driver_data *pl08x = plchan->host;
  1674. struct pl08x_txd *txd;
  1675. struct pl08x_sg *dsg;
  1676. int ret;
  1677. txd = pl08x_get_txd(plchan);
  1678. if (!txd) {
  1679. dev_err(&pl08x->adev->dev,
  1680. "%s no memory for descriptor\n", __func__);
  1681. return NULL;
  1682. }
  1683. dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
  1684. if (!dsg) {
  1685. pl08x_free_txd(pl08x, txd);
  1686. return NULL;
  1687. }
  1688. list_add_tail(&dsg->node, &txd->dsg_list);
  1689. dsg->src_addr = src;
  1690. dsg->dst_addr = dest;
  1691. dsg->len = len;
  1692. if (pl08x->vd->ftdmac020) {
  1693. /* Writing CCFG zero ENABLES all interrupts */
  1694. txd->ccfg = 0;
  1695. txd->cctl = pl08x_ftdmac020_memcpy_cctl(pl08x);
  1696. } else {
  1697. txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
  1698. PL080_CONFIG_TC_IRQ_MASK |
  1699. PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1700. txd->cctl = pl08x_memcpy_cctl(pl08x);
  1701. }
  1702. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1703. if (!ret) {
  1704. pl08x_free_txd(pl08x, txd);
  1705. return NULL;
  1706. }
  1707. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1708. }
  1709. static struct pl08x_txd *pl08x_init_txd(
  1710. struct dma_chan *chan,
  1711. enum dma_transfer_direction direction,
  1712. dma_addr_t *slave_addr)
  1713. {
  1714. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1715. struct pl08x_driver_data *pl08x = plchan->host;
  1716. struct pl08x_txd *txd;
  1717. enum dma_slave_buswidth addr_width;
  1718. int ret, tmp;
  1719. u8 src_buses, dst_buses;
  1720. u32 maxburst, cctl;
  1721. txd = pl08x_get_txd(plchan);
  1722. if (!txd) {
  1723. dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
  1724. return NULL;
  1725. }
  1726. /*
  1727. * Set up addresses, the PrimeCell configured address
  1728. * will take precedence since this may configure the
  1729. * channel target address dynamically at runtime.
  1730. */
  1731. if (direction == DMA_MEM_TO_DEV) {
  1732. cctl = PL080_CONTROL_SRC_INCR;
  1733. *slave_addr = plchan->cfg.dst_addr;
  1734. addr_width = plchan->cfg.dst_addr_width;
  1735. maxburst = plchan->cfg.dst_maxburst;
  1736. src_buses = pl08x->mem_buses;
  1737. dst_buses = plchan->cd->periph_buses;
  1738. } else if (direction == DMA_DEV_TO_MEM) {
  1739. cctl = PL080_CONTROL_DST_INCR;
  1740. *slave_addr = plchan->cfg.src_addr;
  1741. addr_width = plchan->cfg.src_addr_width;
  1742. maxburst = plchan->cfg.src_maxburst;
  1743. src_buses = plchan->cd->periph_buses;
  1744. dst_buses = pl08x->mem_buses;
  1745. } else {
  1746. pl08x_free_txd(pl08x, txd);
  1747. dev_err(&pl08x->adev->dev,
  1748. "%s direction unsupported\n", __func__);
  1749. return NULL;
  1750. }
  1751. cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
  1752. if (cctl == ~0) {
  1753. pl08x_free_txd(pl08x, txd);
  1754. dev_err(&pl08x->adev->dev,
  1755. "DMA slave configuration botched?\n");
  1756. return NULL;
  1757. }
  1758. txd->cctl = cctl | pl08x_select_bus(false, src_buses, dst_buses);
  1759. if (plchan->cfg.device_fc)
  1760. tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
  1761. PL080_FLOW_PER2MEM_PER;
  1762. else
  1763. tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
  1764. PL080_FLOW_PER2MEM;
  1765. txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
  1766. PL080_CONFIG_TC_IRQ_MASK |
  1767. tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
  1768. ret = pl08x_request_mux(plchan);
  1769. if (ret < 0) {
  1770. pl08x_free_txd(pl08x, txd);
  1771. dev_dbg(&pl08x->adev->dev,
  1772. "unable to mux for transfer on %s due to platform restrictions\n",
  1773. plchan->name);
  1774. return NULL;
  1775. }
  1776. dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
  1777. plchan->signal, plchan->name);
  1778. /* Assign the flow control signal to this channel */
  1779. if (direction == DMA_MEM_TO_DEV)
  1780. txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
  1781. else
  1782. txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
  1783. return txd;
  1784. }
  1785. static int pl08x_tx_add_sg(struct pl08x_txd *txd,
  1786. enum dma_transfer_direction direction,
  1787. dma_addr_t slave_addr,
  1788. dma_addr_t buf_addr,
  1789. unsigned int len)
  1790. {
  1791. struct pl08x_sg *dsg;
  1792. dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
  1793. if (!dsg)
  1794. return -ENOMEM;
  1795. list_add_tail(&dsg->node, &txd->dsg_list);
  1796. dsg->len = len;
  1797. if (direction == DMA_MEM_TO_DEV) {
  1798. dsg->src_addr = buf_addr;
  1799. dsg->dst_addr = slave_addr;
  1800. } else {
  1801. dsg->src_addr = slave_addr;
  1802. dsg->dst_addr = buf_addr;
  1803. }
  1804. return 0;
  1805. }
  1806. static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
  1807. struct dma_chan *chan, struct scatterlist *sgl,
  1808. unsigned int sg_len, enum dma_transfer_direction direction,
  1809. unsigned long flags, void *context)
  1810. {
  1811. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1812. struct pl08x_driver_data *pl08x = plchan->host;
  1813. struct pl08x_txd *txd;
  1814. struct scatterlist *sg;
  1815. int ret, tmp;
  1816. dma_addr_t slave_addr;
  1817. dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
  1818. __func__, sg_dma_len(sgl), plchan->name);
  1819. txd = pl08x_init_txd(chan, direction, &slave_addr);
  1820. if (!txd)
  1821. return NULL;
  1822. for_each_sg(sgl, sg, sg_len, tmp) {
  1823. ret = pl08x_tx_add_sg(txd, direction, slave_addr,
  1824. sg_dma_address(sg),
  1825. sg_dma_len(sg));
  1826. if (ret) {
  1827. pl08x_release_mux(plchan);
  1828. pl08x_free_txd(pl08x, txd);
  1829. dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
  1830. __func__);
  1831. return NULL;
  1832. }
  1833. }
  1834. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1835. if (!ret) {
  1836. pl08x_release_mux(plchan);
  1837. pl08x_free_txd(pl08x, txd);
  1838. return NULL;
  1839. }
  1840. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1841. }
  1842. static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
  1843. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1844. size_t period_len, enum dma_transfer_direction direction,
  1845. unsigned long flags)
  1846. {
  1847. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1848. struct pl08x_driver_data *pl08x = plchan->host;
  1849. struct pl08x_txd *txd;
  1850. int ret, tmp;
  1851. dma_addr_t slave_addr;
  1852. dev_dbg(&pl08x->adev->dev,
  1853. "%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
  1854. __func__, period_len, buf_len,
  1855. direction == DMA_MEM_TO_DEV ? "to" : "from",
  1856. plchan->name);
  1857. txd = pl08x_init_txd(chan, direction, &slave_addr);
  1858. if (!txd)
  1859. return NULL;
  1860. txd->cyclic = true;
  1861. txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
  1862. for (tmp = 0; tmp < buf_len; tmp += period_len) {
  1863. ret = pl08x_tx_add_sg(txd, direction, slave_addr,
  1864. buf_addr + tmp, period_len);
  1865. if (ret) {
  1866. pl08x_release_mux(plchan);
  1867. pl08x_free_txd(pl08x, txd);
  1868. return NULL;
  1869. }
  1870. }
  1871. ret = pl08x_fill_llis_for_desc(plchan->host, txd);
  1872. if (!ret) {
  1873. pl08x_release_mux(plchan);
  1874. pl08x_free_txd(pl08x, txd);
  1875. return NULL;
  1876. }
  1877. return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
  1878. }
  1879. static int pl08x_config(struct dma_chan *chan,
  1880. struct dma_slave_config *config)
  1881. {
  1882. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1883. struct pl08x_driver_data *pl08x = plchan->host;
  1884. if (!plchan->slave)
  1885. return -EINVAL;
  1886. /* Reject definitely invalid configurations */
  1887. if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  1888. config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  1889. return -EINVAL;
  1890. if (config->device_fc && pl08x->vd->pl080s) {
  1891. dev_err(&pl08x->adev->dev,
  1892. "%s: PL080S does not support peripheral flow control\n",
  1893. __func__);
  1894. return -EINVAL;
  1895. }
  1896. plchan->cfg = *config;
  1897. return 0;
  1898. }
  1899. static int pl08x_terminate_all(struct dma_chan *chan)
  1900. {
  1901. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1902. struct pl08x_driver_data *pl08x = plchan->host;
  1903. unsigned long flags;
  1904. spin_lock_irqsave(&plchan->vc.lock, flags);
  1905. if (!plchan->phychan && !plchan->at) {
  1906. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1907. return 0;
  1908. }
  1909. plchan->state = PL08X_CHAN_IDLE;
  1910. if (plchan->phychan) {
  1911. /*
  1912. * Mark physical channel as free and free any slave
  1913. * signal
  1914. */
  1915. pl08x_phy_free(plchan);
  1916. }
  1917. /* Dequeue jobs and free LLIs */
  1918. if (plchan->at) {
  1919. pl08x_desc_free(&plchan->at->vd);
  1920. plchan->at = NULL;
  1921. }
  1922. /* Dequeue jobs not yet fired as well */
  1923. pl08x_free_txd_list(pl08x, plchan);
  1924. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1925. return 0;
  1926. }
  1927. static int pl08x_pause(struct dma_chan *chan)
  1928. {
  1929. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1930. unsigned long flags;
  1931. /*
  1932. * Anything succeeds on channels with no physical allocation and
  1933. * no queued transfers.
  1934. */
  1935. spin_lock_irqsave(&plchan->vc.lock, flags);
  1936. if (!plchan->phychan && !plchan->at) {
  1937. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1938. return 0;
  1939. }
  1940. pl08x_pause_phy_chan(plchan->phychan);
  1941. plchan->state = PL08X_CHAN_PAUSED;
  1942. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1943. return 0;
  1944. }
  1945. static int pl08x_resume(struct dma_chan *chan)
  1946. {
  1947. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1948. unsigned long flags;
  1949. /*
  1950. * Anything succeeds on channels with no physical allocation and
  1951. * no queued transfers.
  1952. */
  1953. spin_lock_irqsave(&plchan->vc.lock, flags);
  1954. if (!plchan->phychan && !plchan->at) {
  1955. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1956. return 0;
  1957. }
  1958. pl08x_resume_phy_chan(plchan->phychan);
  1959. plchan->state = PL08X_CHAN_RUNNING;
  1960. spin_unlock_irqrestore(&plchan->vc.lock, flags);
  1961. return 0;
  1962. }
  1963. bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
  1964. {
  1965. struct pl08x_dma_chan *plchan;
  1966. char *name = chan_id;
  1967. /* Reject channels for devices not bound to this driver */
  1968. if (chan->device->dev->driver != &pl08x_amba_driver.drv)
  1969. return false;
  1970. plchan = to_pl08x_chan(chan);
  1971. /* Check that the channel is not taken! */
  1972. if (!strcmp(plchan->name, name))
  1973. return true;
  1974. return false;
  1975. }
  1976. EXPORT_SYMBOL_GPL(pl08x_filter_id);
  1977. static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
  1978. {
  1979. struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
  1980. return plchan->cd == chan_id;
  1981. }
  1982. /*
  1983. * Just check that the device is there and active
  1984. * TODO: turn this bit on/off depending on the number of physical channels
  1985. * actually used, if it is zero... well shut it off. That will save some
  1986. * power. Cut the clock at the same time.
  1987. */
  1988. static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
  1989. {
  1990. /* The Nomadik variant does not have the config register */
  1991. if (pl08x->vd->nomadik)
  1992. return;
  1993. /* The FTDMAC020 variant does this in another register */
  1994. if (pl08x->vd->ftdmac020) {
  1995. writel(PL080_CONFIG_ENABLE, pl08x->base + FTDMAC020_CSR);
  1996. return;
  1997. }
  1998. writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
  1999. }
  2000. static irqreturn_t pl08x_irq(int irq, void *dev)
  2001. {
  2002. struct pl08x_driver_data *pl08x = dev;
  2003. u32 mask = 0, err, tc, i;
  2004. /* check & clear - ERR & TC interrupts */
  2005. err = readl(pl08x->base + PL080_ERR_STATUS);
  2006. if (err) {
  2007. dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
  2008. __func__, err);
  2009. writel(err, pl08x->base + PL080_ERR_CLEAR);
  2010. }
  2011. tc = readl(pl08x->base + PL080_TC_STATUS);
  2012. if (tc)
  2013. writel(tc, pl08x->base + PL080_TC_CLEAR);
  2014. if (!err && !tc)
  2015. return IRQ_NONE;
  2016. for (i = 0; i < pl08x->vd->channels; i++) {
  2017. if ((BIT(i) & err) || (BIT(i) & tc)) {
  2018. /* Locate physical channel */
  2019. struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
  2020. struct pl08x_dma_chan *plchan = phychan->serving;
  2021. struct pl08x_txd *tx;
  2022. if (!plchan) {
  2023. dev_err(&pl08x->adev->dev,
  2024. "%s Error TC interrupt on unused channel: 0x%08x\n",
  2025. __func__, i);
  2026. continue;
  2027. }
  2028. spin_lock(&plchan->vc.lock);
  2029. tx = plchan->at;
  2030. if (tx && tx->cyclic) {
  2031. vchan_cyclic_callback(&tx->vd);
  2032. } else if (tx) {
  2033. plchan->at = NULL;
  2034. /*
  2035. * This descriptor is done, release its mux
  2036. * reservation.
  2037. */
  2038. pl08x_release_mux(plchan);
  2039. tx->done = true;
  2040. vchan_cookie_complete(&tx->vd);
  2041. /*
  2042. * And start the next descriptor (if any),
  2043. * otherwise free this channel.
  2044. */
  2045. if (vchan_next_desc(&plchan->vc))
  2046. pl08x_start_next_txd(plchan);
  2047. else
  2048. pl08x_phy_free(plchan);
  2049. }
  2050. spin_unlock(&plchan->vc.lock);
  2051. mask |= BIT(i);
  2052. }
  2053. }
  2054. return mask ? IRQ_HANDLED : IRQ_NONE;
  2055. }
  2056. static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
  2057. {
  2058. chan->slave = true;
  2059. chan->name = chan->cd->bus_id;
  2060. chan->cfg.src_addr = chan->cd->addr;
  2061. chan->cfg.dst_addr = chan->cd->addr;
  2062. }
  2063. /*
  2064. * Initialise the DMAC memcpy/slave channels.
  2065. * Make a local wrapper to hold required data
  2066. */
  2067. static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
  2068. struct dma_device *dmadev, unsigned int channels, bool slave)
  2069. {
  2070. struct pl08x_dma_chan *chan;
  2071. int i;
  2072. INIT_LIST_HEAD(&dmadev->channels);
  2073. /*
  2074. * Register as many many memcpy as we have physical channels,
  2075. * we won't always be able to use all but the code will have
  2076. * to cope with that situation.
  2077. */
  2078. for (i = 0; i < channels; i++) {
  2079. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  2080. if (!chan)
  2081. return -ENOMEM;
  2082. chan->host = pl08x;
  2083. chan->state = PL08X_CHAN_IDLE;
  2084. chan->signal = -1;
  2085. if (slave) {
  2086. chan->cd = &pl08x->pd->slave_channels[i];
  2087. /*
  2088. * Some implementations have muxed signals, whereas some
  2089. * use a mux in front of the signals and need dynamic
  2090. * assignment of signals.
  2091. */
  2092. chan->signal = i;
  2093. pl08x_dma_slave_init(chan);
  2094. } else {
  2095. chan->cd = kzalloc(sizeof(*chan->cd), GFP_KERNEL);
  2096. if (!chan->cd) {
  2097. kfree(chan);
  2098. return -ENOMEM;
  2099. }
  2100. chan->cd->bus_id = "memcpy";
  2101. chan->cd->periph_buses = pl08x->pd->mem_buses;
  2102. chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
  2103. if (!chan->name) {
  2104. kfree(chan->cd);
  2105. kfree(chan);
  2106. return -ENOMEM;
  2107. }
  2108. }
  2109. dev_dbg(&pl08x->adev->dev,
  2110. "initialize virtual channel \"%s\"\n",
  2111. chan->name);
  2112. chan->vc.desc_free = pl08x_desc_free;
  2113. vchan_init(&chan->vc, dmadev);
  2114. }
  2115. dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
  2116. i, slave ? "slave" : "memcpy");
  2117. return i;
  2118. }
  2119. static void pl08x_free_virtual_channels(struct dma_device *dmadev)
  2120. {
  2121. struct pl08x_dma_chan *chan = NULL;
  2122. struct pl08x_dma_chan *next;
  2123. list_for_each_entry_safe(chan,
  2124. next, &dmadev->channels, vc.chan.device_node) {
  2125. list_del(&chan->vc.chan.device_node);
  2126. kfree(chan);
  2127. }
  2128. }
  2129. #ifdef CONFIG_DEBUG_FS
  2130. static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
  2131. {
  2132. switch (state) {
  2133. case PL08X_CHAN_IDLE:
  2134. return "idle";
  2135. case PL08X_CHAN_RUNNING:
  2136. return "running";
  2137. case PL08X_CHAN_PAUSED:
  2138. return "paused";
  2139. case PL08X_CHAN_WAITING:
  2140. return "waiting";
  2141. default:
  2142. break;
  2143. }
  2144. return "UNKNOWN STATE";
  2145. }
  2146. static int pl08x_debugfs_show(struct seq_file *s, void *data)
  2147. {
  2148. struct pl08x_driver_data *pl08x = s->private;
  2149. struct pl08x_dma_chan *chan;
  2150. struct pl08x_phy_chan *ch;
  2151. unsigned long flags;
  2152. int i;
  2153. seq_printf(s, "PL08x physical channels:\n");
  2154. seq_printf(s, "CHANNEL:\tUSER:\n");
  2155. seq_printf(s, "--------\t-----\n");
  2156. for (i = 0; i < pl08x->vd->channels; i++) {
  2157. struct pl08x_dma_chan *virt_chan;
  2158. ch = &pl08x->phy_chans[i];
  2159. spin_lock_irqsave(&ch->lock, flags);
  2160. virt_chan = ch->serving;
  2161. seq_printf(s, "%d\t\t%s%s\n",
  2162. ch->id,
  2163. virt_chan ? virt_chan->name : "(none)",
  2164. ch->locked ? " LOCKED" : "");
  2165. spin_unlock_irqrestore(&ch->lock, flags);
  2166. }
  2167. seq_printf(s, "\nPL08x virtual memcpy channels:\n");
  2168. seq_printf(s, "CHANNEL:\tSTATE:\n");
  2169. seq_printf(s, "--------\t------\n");
  2170. list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
  2171. seq_printf(s, "%s\t\t%s\n", chan->name,
  2172. pl08x_state_str(chan->state));
  2173. }
  2174. if (pl08x->has_slave) {
  2175. seq_printf(s, "\nPL08x virtual slave channels:\n");
  2176. seq_printf(s, "CHANNEL:\tSTATE:\n");
  2177. seq_printf(s, "--------\t------\n");
  2178. list_for_each_entry(chan, &pl08x->slave.channels,
  2179. vc.chan.device_node) {
  2180. seq_printf(s, "%s\t\t%s\n", chan->name,
  2181. pl08x_state_str(chan->state));
  2182. }
  2183. }
  2184. return 0;
  2185. }
  2186. static int pl08x_debugfs_open(struct inode *inode, struct file *file)
  2187. {
  2188. return single_open(file, pl08x_debugfs_show, inode->i_private);
  2189. }
  2190. static const struct file_operations pl08x_debugfs_operations = {
  2191. .open = pl08x_debugfs_open,
  2192. .read = seq_read,
  2193. .llseek = seq_lseek,
  2194. .release = single_release,
  2195. };
  2196. static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  2197. {
  2198. /* Expose a simple debugfs interface to view all clocks */
  2199. (void) debugfs_create_file(dev_name(&pl08x->adev->dev),
  2200. S_IFREG | S_IRUGO, NULL, pl08x,
  2201. &pl08x_debugfs_operations);
  2202. }
  2203. #else
  2204. static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
  2205. {
  2206. }
  2207. #endif
  2208. #ifdef CONFIG_OF
  2209. static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
  2210. u32 id)
  2211. {
  2212. struct pl08x_dma_chan *chan;
  2213. /* Trying to get a slave channel from something with no slave support */
  2214. if (!pl08x->has_slave)
  2215. return NULL;
  2216. list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
  2217. if (chan->signal == id)
  2218. return &chan->vc.chan;
  2219. }
  2220. return NULL;
  2221. }
  2222. static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
  2223. struct of_dma *ofdma)
  2224. {
  2225. struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
  2226. struct dma_chan *dma_chan;
  2227. struct pl08x_dma_chan *plchan;
  2228. if (!pl08x)
  2229. return NULL;
  2230. if (dma_spec->args_count != 2) {
  2231. dev_err(&pl08x->adev->dev,
  2232. "DMA channel translation requires two cells\n");
  2233. return NULL;
  2234. }
  2235. dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
  2236. if (!dma_chan) {
  2237. dev_err(&pl08x->adev->dev,
  2238. "DMA slave channel not found\n");
  2239. return NULL;
  2240. }
  2241. plchan = to_pl08x_chan(dma_chan);
  2242. dev_dbg(&pl08x->adev->dev,
  2243. "translated channel for signal %d\n",
  2244. dma_spec->args[0]);
  2245. /* Augment channel data for applicable AHB buses */
  2246. plchan->cd->periph_buses = dma_spec->args[1];
  2247. return dma_get_slave_channel(dma_chan);
  2248. }
  2249. static int pl08x_of_probe(struct amba_device *adev,
  2250. struct pl08x_driver_data *pl08x,
  2251. struct device_node *np)
  2252. {
  2253. struct pl08x_platform_data *pd;
  2254. struct pl08x_channel_data *chanp = NULL;
  2255. u32 val;
  2256. int ret;
  2257. int i;
  2258. pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
  2259. if (!pd)
  2260. return -ENOMEM;
  2261. /* Eligible bus masters for fetching LLIs */
  2262. if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
  2263. pd->lli_buses |= PL08X_AHB1;
  2264. if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
  2265. pd->lli_buses |= PL08X_AHB2;
  2266. if (!pd->lli_buses) {
  2267. dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
  2268. pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
  2269. }
  2270. /* Eligible bus masters for memory access */
  2271. if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
  2272. pd->mem_buses |= PL08X_AHB1;
  2273. if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
  2274. pd->mem_buses |= PL08X_AHB2;
  2275. if (!pd->mem_buses) {
  2276. dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
  2277. pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
  2278. }
  2279. /* Parse the memcpy channel properties */
  2280. ret = of_property_read_u32(np, "memcpy-burst-size", &val);
  2281. if (ret) {
  2282. dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
  2283. val = 1;
  2284. }
  2285. switch (val) {
  2286. default:
  2287. dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
  2288. /* Fall through */
  2289. case 1:
  2290. pd->memcpy_burst_size = PL08X_BURST_SZ_1;
  2291. break;
  2292. case 4:
  2293. pd->memcpy_burst_size = PL08X_BURST_SZ_4;
  2294. break;
  2295. case 8:
  2296. pd->memcpy_burst_size = PL08X_BURST_SZ_8;
  2297. break;
  2298. case 16:
  2299. pd->memcpy_burst_size = PL08X_BURST_SZ_16;
  2300. break;
  2301. case 32:
  2302. pd->memcpy_burst_size = PL08X_BURST_SZ_32;
  2303. break;
  2304. case 64:
  2305. pd->memcpy_burst_size = PL08X_BURST_SZ_64;
  2306. break;
  2307. case 128:
  2308. pd->memcpy_burst_size = PL08X_BURST_SZ_128;
  2309. break;
  2310. case 256:
  2311. pd->memcpy_burst_size = PL08X_BURST_SZ_256;
  2312. break;
  2313. }
  2314. ret = of_property_read_u32(np, "memcpy-bus-width", &val);
  2315. if (ret) {
  2316. dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
  2317. val = 8;
  2318. }
  2319. switch (val) {
  2320. default:
  2321. dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
  2322. /* Fall through */
  2323. case 8:
  2324. pd->memcpy_bus_width = PL08X_BUS_WIDTH_8_BITS;
  2325. break;
  2326. case 16:
  2327. pd->memcpy_bus_width = PL08X_BUS_WIDTH_16_BITS;
  2328. break;
  2329. case 32:
  2330. pd->memcpy_bus_width = PL08X_BUS_WIDTH_32_BITS;
  2331. break;
  2332. }
  2333. /*
  2334. * Allocate channel data for all possible slave channels (one
  2335. * for each possible signal), channels will then be allocated
  2336. * for a device and have it's AHB interfaces set up at
  2337. * translation time.
  2338. */
  2339. if (pl08x->vd->signals) {
  2340. chanp = devm_kcalloc(&adev->dev,
  2341. pl08x->vd->signals,
  2342. sizeof(struct pl08x_channel_data),
  2343. GFP_KERNEL);
  2344. if (!chanp)
  2345. return -ENOMEM;
  2346. pd->slave_channels = chanp;
  2347. for (i = 0; i < pl08x->vd->signals; i++) {
  2348. /*
  2349. * chanp->periph_buses will be assigned at translation
  2350. */
  2351. chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
  2352. chanp++;
  2353. }
  2354. pd->num_slave_channels = pl08x->vd->signals;
  2355. }
  2356. pl08x->pd = pd;
  2357. return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
  2358. pl08x);
  2359. }
  2360. #else
  2361. static inline int pl08x_of_probe(struct amba_device *adev,
  2362. struct pl08x_driver_data *pl08x,
  2363. struct device_node *np)
  2364. {
  2365. return -EINVAL;
  2366. }
  2367. #endif
  2368. static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
  2369. {
  2370. struct pl08x_driver_data *pl08x;
  2371. struct vendor_data *vd = id->data;
  2372. struct device_node *np = adev->dev.of_node;
  2373. u32 tsfr_size;
  2374. int ret = 0;
  2375. int i;
  2376. ret = amba_request_regions(adev, NULL);
  2377. if (ret)
  2378. return ret;
  2379. /* Ensure that we can do DMA */
  2380. ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
  2381. if (ret)
  2382. goto out_no_pl08x;
  2383. /* Create the driver state holder */
  2384. pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
  2385. if (!pl08x) {
  2386. ret = -ENOMEM;
  2387. goto out_no_pl08x;
  2388. }
  2389. /* Assign useful pointers to the driver state */
  2390. pl08x->adev = adev;
  2391. pl08x->vd = vd;
  2392. pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
  2393. if (!pl08x->base) {
  2394. ret = -ENOMEM;
  2395. goto out_no_ioremap;
  2396. }
  2397. if (vd->ftdmac020) {
  2398. u32 val;
  2399. val = readl(pl08x->base + FTDMAC020_REVISION);
  2400. dev_info(&pl08x->adev->dev, "FTDMAC020 %d.%d rel %d\n",
  2401. (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
  2402. val = readl(pl08x->base + FTDMAC020_FEATURE);
  2403. dev_info(&pl08x->adev->dev, "FTDMAC020 %d channels, "
  2404. "%s built-in bridge, %s, %s linked lists\n",
  2405. (val >> 12) & 0x0f,
  2406. (val & BIT(10)) ? "no" : "has",
  2407. (val & BIT(9)) ? "AHB0 and AHB1" : "AHB0",
  2408. (val & BIT(8)) ? "supports" : "does not support");
  2409. /* Vendor data from feature register */
  2410. if (!(val & BIT(8)))
  2411. dev_warn(&pl08x->adev->dev,
  2412. "linked lists not supported, required\n");
  2413. vd->channels = (val >> 12) & 0x0f;
  2414. vd->dualmaster = !!(val & BIT(9));
  2415. }
  2416. /* Initialize memcpy engine */
  2417. dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
  2418. pl08x->memcpy.dev = &adev->dev;
  2419. pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
  2420. pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
  2421. pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
  2422. pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
  2423. pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
  2424. pl08x->memcpy.device_config = pl08x_config;
  2425. pl08x->memcpy.device_pause = pl08x_pause;
  2426. pl08x->memcpy.device_resume = pl08x_resume;
  2427. pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
  2428. pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
  2429. pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
  2430. pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
  2431. pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  2432. if (vd->ftdmac020)
  2433. pl08x->memcpy.copy_align = DMAENGINE_ALIGN_4_BYTES;
  2434. /*
  2435. * Initialize slave engine, if the block has no signals, that means
  2436. * we have no slave support.
  2437. */
  2438. if (vd->signals) {
  2439. pl08x->has_slave = true;
  2440. dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
  2441. dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
  2442. pl08x->slave.dev = &adev->dev;
  2443. pl08x->slave.device_free_chan_resources =
  2444. pl08x_free_chan_resources;
  2445. pl08x->slave.device_prep_dma_interrupt =
  2446. pl08x_prep_dma_interrupt;
  2447. pl08x->slave.device_tx_status = pl08x_dma_tx_status;
  2448. pl08x->slave.device_issue_pending = pl08x_issue_pending;
  2449. pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
  2450. pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
  2451. pl08x->slave.device_config = pl08x_config;
  2452. pl08x->slave.device_pause = pl08x_pause;
  2453. pl08x->slave.device_resume = pl08x_resume;
  2454. pl08x->slave.device_terminate_all = pl08x_terminate_all;
  2455. pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
  2456. pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
  2457. pl08x->slave.directions =
  2458. BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  2459. pl08x->slave.residue_granularity =
  2460. DMA_RESIDUE_GRANULARITY_SEGMENT;
  2461. }
  2462. /* Get the platform data */
  2463. pl08x->pd = dev_get_platdata(&adev->dev);
  2464. if (!pl08x->pd) {
  2465. if (np) {
  2466. ret = pl08x_of_probe(adev, pl08x, np);
  2467. if (ret)
  2468. goto out_no_platdata;
  2469. } else {
  2470. dev_err(&adev->dev, "no platform data supplied\n");
  2471. ret = -EINVAL;
  2472. goto out_no_platdata;
  2473. }
  2474. } else {
  2475. pl08x->slave.filter.map = pl08x->pd->slave_map;
  2476. pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
  2477. pl08x->slave.filter.fn = pl08x_filter_fn;
  2478. }
  2479. /* By default, AHB1 only. If dualmaster, from platform */
  2480. pl08x->lli_buses = PL08X_AHB1;
  2481. pl08x->mem_buses = PL08X_AHB1;
  2482. if (pl08x->vd->dualmaster) {
  2483. pl08x->lli_buses = pl08x->pd->lli_buses;
  2484. pl08x->mem_buses = pl08x->pd->mem_buses;
  2485. }
  2486. if (vd->pl080s)
  2487. pl08x->lli_words = PL080S_LLI_WORDS;
  2488. else
  2489. pl08x->lli_words = PL080_LLI_WORDS;
  2490. tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
  2491. /* A DMA memory pool for LLIs, align on 1-byte boundary */
  2492. pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
  2493. tsfr_size, PL08X_ALIGN, 0);
  2494. if (!pl08x->pool) {
  2495. ret = -ENOMEM;
  2496. goto out_no_lli_pool;
  2497. }
  2498. /* Turn on the PL08x */
  2499. pl08x_ensure_on(pl08x);
  2500. /* Clear any pending interrupts */
  2501. if (vd->ftdmac020)
  2502. /* This variant has error IRQs in bits 16-19 */
  2503. writel(0x0000FFFF, pl08x->base + PL080_ERR_CLEAR);
  2504. else
  2505. writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
  2506. writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
  2507. /* Attach the interrupt handler */
  2508. ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
  2509. if (ret) {
  2510. dev_err(&adev->dev, "%s failed to request interrupt %d\n",
  2511. __func__, adev->irq[0]);
  2512. goto out_no_irq;
  2513. }
  2514. /* Initialize physical channels */
  2515. pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
  2516. GFP_KERNEL);
  2517. if (!pl08x->phy_chans) {
  2518. ret = -ENOMEM;
  2519. goto out_no_phychans;
  2520. }
  2521. for (i = 0; i < vd->channels; i++) {
  2522. struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
  2523. ch->id = i;
  2524. ch->base = pl08x->base + PL080_Cx_BASE(i);
  2525. if (vd->ftdmac020) {
  2526. /* FTDMA020 has a special channel busy register */
  2527. ch->reg_busy = ch->base + FTDMAC020_CH_BUSY;
  2528. ch->reg_config = ch->base + FTDMAC020_CH_CFG;
  2529. ch->reg_control = ch->base + FTDMAC020_CH_CSR;
  2530. ch->reg_src = ch->base + FTDMAC020_CH_SRC_ADDR;
  2531. ch->reg_dst = ch->base + FTDMAC020_CH_DST_ADDR;
  2532. ch->reg_lli = ch->base + FTDMAC020_CH_LLP;
  2533. ch->ftdmac020 = true;
  2534. } else {
  2535. ch->reg_config = ch->base + vd->config_offset;
  2536. ch->reg_control = ch->base + PL080_CH_CONTROL;
  2537. ch->reg_src = ch->base + PL080_CH_SRC_ADDR;
  2538. ch->reg_dst = ch->base + PL080_CH_DST_ADDR;
  2539. ch->reg_lli = ch->base + PL080_CH_LLI;
  2540. }
  2541. if (vd->pl080s)
  2542. ch->pl080s = true;
  2543. spin_lock_init(&ch->lock);
  2544. /*
  2545. * Nomadik variants can have channels that are locked
  2546. * down for the secure world only. Lock up these channels
  2547. * by perpetually serving a dummy virtual channel.
  2548. */
  2549. if (vd->nomadik) {
  2550. u32 val;
  2551. val = readl(ch->reg_config);
  2552. if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
  2553. dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
  2554. ch->locked = true;
  2555. }
  2556. }
  2557. dev_dbg(&adev->dev, "physical channel %d is %s\n",
  2558. i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
  2559. }
  2560. /* Register as many memcpy channels as there are physical channels */
  2561. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
  2562. pl08x->vd->channels, false);
  2563. if (ret <= 0) {
  2564. dev_warn(&pl08x->adev->dev,
  2565. "%s failed to enumerate memcpy channels - %d\n",
  2566. __func__, ret);
  2567. goto out_no_memcpy;
  2568. }
  2569. /* Register slave channels */
  2570. if (pl08x->has_slave) {
  2571. ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
  2572. pl08x->pd->num_slave_channels, true);
  2573. if (ret < 0) {
  2574. dev_warn(&pl08x->adev->dev,
  2575. "%s failed to enumerate slave channels - %d\n",
  2576. __func__, ret);
  2577. goto out_no_slave;
  2578. }
  2579. }
  2580. ret = dma_async_device_register(&pl08x->memcpy);
  2581. if (ret) {
  2582. dev_warn(&pl08x->adev->dev,
  2583. "%s failed to register memcpy as an async device - %d\n",
  2584. __func__, ret);
  2585. goto out_no_memcpy_reg;
  2586. }
  2587. if (pl08x->has_slave) {
  2588. ret = dma_async_device_register(&pl08x->slave);
  2589. if (ret) {
  2590. dev_warn(&pl08x->adev->dev,
  2591. "%s failed to register slave as an async device - %d\n",
  2592. __func__, ret);
  2593. goto out_no_slave_reg;
  2594. }
  2595. }
  2596. amba_set_drvdata(adev, pl08x);
  2597. init_pl08x_debugfs(pl08x);
  2598. dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
  2599. amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
  2600. (unsigned long long)adev->res.start, adev->irq[0]);
  2601. return 0;
  2602. out_no_slave_reg:
  2603. dma_async_device_unregister(&pl08x->memcpy);
  2604. out_no_memcpy_reg:
  2605. if (pl08x->has_slave)
  2606. pl08x_free_virtual_channels(&pl08x->slave);
  2607. out_no_slave:
  2608. pl08x_free_virtual_channels(&pl08x->memcpy);
  2609. out_no_memcpy:
  2610. kfree(pl08x->phy_chans);
  2611. out_no_phychans:
  2612. free_irq(adev->irq[0], pl08x);
  2613. out_no_irq:
  2614. dma_pool_destroy(pl08x->pool);
  2615. out_no_lli_pool:
  2616. out_no_platdata:
  2617. iounmap(pl08x->base);
  2618. out_no_ioremap:
  2619. kfree(pl08x);
  2620. out_no_pl08x:
  2621. amba_release_regions(adev);
  2622. return ret;
  2623. }
  2624. /* PL080 has 8 channels and the PL080 have just 2 */
  2625. static struct vendor_data vendor_pl080 = {
  2626. .config_offset = PL080_CH_CONFIG,
  2627. .channels = 8,
  2628. .signals = 16,
  2629. .dualmaster = true,
  2630. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2631. };
  2632. static struct vendor_data vendor_nomadik = {
  2633. .config_offset = PL080_CH_CONFIG,
  2634. .channels = 8,
  2635. .signals = 32,
  2636. .dualmaster = true,
  2637. .nomadik = true,
  2638. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2639. };
  2640. static struct vendor_data vendor_pl080s = {
  2641. .config_offset = PL080S_CH_CONFIG,
  2642. .channels = 8,
  2643. .signals = 32,
  2644. .pl080s = true,
  2645. .max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
  2646. };
  2647. static struct vendor_data vendor_pl081 = {
  2648. .config_offset = PL080_CH_CONFIG,
  2649. .channels = 2,
  2650. .signals = 16,
  2651. .dualmaster = false,
  2652. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2653. };
  2654. static struct vendor_data vendor_ftdmac020 = {
  2655. .config_offset = PL080_CH_CONFIG,
  2656. .ftdmac020 = true,
  2657. .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
  2658. };
  2659. static const struct amba_id pl08x_ids[] = {
  2660. /* Samsung PL080S variant */
  2661. {
  2662. .id = 0x0a141080,
  2663. .mask = 0xffffffff,
  2664. .data = &vendor_pl080s,
  2665. },
  2666. /* PL080 */
  2667. {
  2668. .id = 0x00041080,
  2669. .mask = 0x000fffff,
  2670. .data = &vendor_pl080,
  2671. },
  2672. /* PL081 */
  2673. {
  2674. .id = 0x00041081,
  2675. .mask = 0x000fffff,
  2676. .data = &vendor_pl081,
  2677. },
  2678. /* Nomadik 8815 PL080 variant */
  2679. {
  2680. .id = 0x00280080,
  2681. .mask = 0x00ffffff,
  2682. .data = &vendor_nomadik,
  2683. },
  2684. /* Faraday Technology FTDMAC020 */
  2685. {
  2686. .id = 0x0003b080,
  2687. .mask = 0x000fffff,
  2688. .data = &vendor_ftdmac020,
  2689. },
  2690. { 0, 0 },
  2691. };
  2692. MODULE_DEVICE_TABLE(amba, pl08x_ids);
  2693. static struct amba_driver pl08x_amba_driver = {
  2694. .drv.name = DRIVER_NAME,
  2695. .id_table = pl08x_ids,
  2696. .probe = pl08x_probe,
  2697. };
  2698. static int __init pl08x_init(void)
  2699. {
  2700. int retval;
  2701. retval = amba_driver_register(&pl08x_amba_driver);
  2702. if (retval)
  2703. printk(KERN_WARNING DRIVER_NAME
  2704. "failed to register as an AMBA device (%d)\n",
  2705. retval);
  2706. return retval;
  2707. }
  2708. subsys_initcall(pl08x_init);