amba-pl08x.c 80 KB

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