tegra20-apb-dma.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * DMA driver for Nvidia's Tegra20 APB DMA controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/slab.h>
  34. #include <linux/clk/tegra.h>
  35. #include "dmaengine.h"
  36. #define TEGRA_APBDMA_GENERAL 0x0
  37. #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
  38. #define TEGRA_APBDMA_CONTROL 0x010
  39. #define TEGRA_APBDMA_IRQ_MASK 0x01c
  40. #define TEGRA_APBDMA_IRQ_MASK_SET 0x020
  41. /* CSR register */
  42. #define TEGRA_APBDMA_CHAN_CSR 0x00
  43. #define TEGRA_APBDMA_CSR_ENB BIT(31)
  44. #define TEGRA_APBDMA_CSR_IE_EOC BIT(30)
  45. #define TEGRA_APBDMA_CSR_HOLD BIT(29)
  46. #define TEGRA_APBDMA_CSR_DIR BIT(28)
  47. #define TEGRA_APBDMA_CSR_ONCE BIT(27)
  48. #define TEGRA_APBDMA_CSR_FLOW BIT(21)
  49. #define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16
  50. #define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC
  51. /* STATUS register */
  52. #define TEGRA_APBDMA_CHAN_STATUS 0x004
  53. #define TEGRA_APBDMA_STATUS_BUSY BIT(31)
  54. #define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30)
  55. #define TEGRA_APBDMA_STATUS_HALT BIT(29)
  56. #define TEGRA_APBDMA_STATUS_PING_PONG BIT(28)
  57. #define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2
  58. #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
  59. #define TEGRA_APBDMA_CHAN_CSRE 0x00C
  60. #define TEGRA_APBDMA_CHAN_CSRE_PAUSE (1 << 31)
  61. /* AHB memory address */
  62. #define TEGRA_APBDMA_CHAN_AHBPTR 0x010
  63. /* AHB sequence register */
  64. #define TEGRA_APBDMA_CHAN_AHBSEQ 0x14
  65. #define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31)
  66. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28)
  67. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28)
  68. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28)
  69. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28)
  70. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28)
  71. #define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27)
  72. #define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24)
  73. #define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24)
  74. #define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24)
  75. #define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19)
  76. #define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16
  77. #define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0
  78. /* APB address */
  79. #define TEGRA_APBDMA_CHAN_APBPTR 0x018
  80. /* APB sequence register */
  81. #define TEGRA_APBDMA_CHAN_APBSEQ 0x01c
  82. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28)
  83. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28)
  84. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28)
  85. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28)
  86. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28)
  87. #define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27)
  88. #define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16)
  89. /*
  90. * If any burst is in flight and DMA paused then this is the time to complete
  91. * on-flight burst and update DMA status register.
  92. */
  93. #define TEGRA_APBDMA_BURST_COMPLETE_TIME 20
  94. /* Channel base address offset from APBDMA base address */
  95. #define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000
  96. /* DMA channel register space size */
  97. #define TEGRA_APBDMA_CHANNEL_REGISTER_SIZE 0x20
  98. struct tegra_dma;
  99. /*
  100. * tegra_dma_chip_data Tegra chip specific DMA data
  101. * @nr_channels: Number of channels available in the controller.
  102. * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
  103. * @support_channel_pause: Support channel wise pause of dma.
  104. */
  105. struct tegra_dma_chip_data {
  106. int nr_channels;
  107. int max_dma_count;
  108. bool support_channel_pause;
  109. };
  110. /* DMA channel registers */
  111. struct tegra_dma_channel_regs {
  112. unsigned long csr;
  113. unsigned long ahb_ptr;
  114. unsigned long apb_ptr;
  115. unsigned long ahb_seq;
  116. unsigned long apb_seq;
  117. };
  118. /*
  119. * tegra_dma_sg_req: Dma request details to configure hardware. This
  120. * contains the details for one transfer to configure DMA hw.
  121. * The client's request for data transfer can be broken into multiple
  122. * sub-transfer as per requester details and hw support.
  123. * This sub transfer get added in the list of transfer and point to Tegra
  124. * DMA descriptor which manages the transfer details.
  125. */
  126. struct tegra_dma_sg_req {
  127. struct tegra_dma_channel_regs ch_regs;
  128. int req_len;
  129. bool configured;
  130. bool last_sg;
  131. bool half_done;
  132. struct list_head node;
  133. struct tegra_dma_desc *dma_desc;
  134. };
  135. /*
  136. * tegra_dma_desc: Tegra DMA descriptors which manages the client requests.
  137. * This descriptor keep track of transfer status, callbacks and request
  138. * counts etc.
  139. */
  140. struct tegra_dma_desc {
  141. struct dma_async_tx_descriptor txd;
  142. int bytes_requested;
  143. int bytes_transferred;
  144. enum dma_status dma_status;
  145. struct list_head node;
  146. struct list_head tx_list;
  147. struct list_head cb_node;
  148. int cb_count;
  149. };
  150. struct tegra_dma_channel;
  151. typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
  152. bool to_terminate);
  153. /* tegra_dma_channel: Channel specific information */
  154. struct tegra_dma_channel {
  155. struct dma_chan dma_chan;
  156. char name[30];
  157. bool config_init;
  158. int id;
  159. int irq;
  160. unsigned long chan_base_offset;
  161. spinlock_t lock;
  162. bool busy;
  163. struct tegra_dma *tdma;
  164. bool cyclic;
  165. /* Different lists for managing the requests */
  166. struct list_head free_sg_req;
  167. struct list_head pending_sg_req;
  168. struct list_head free_dma_desc;
  169. struct list_head cb_desc;
  170. /* ISR handler and tasklet for bottom half of isr handling */
  171. dma_isr_handler isr_handler;
  172. struct tasklet_struct tasklet;
  173. dma_async_tx_callback callback;
  174. void *callback_param;
  175. /* Channel-slave specific configuration */
  176. struct dma_slave_config dma_sconfig;
  177. };
  178. /* tegra_dma: Tegra DMA specific information */
  179. struct tegra_dma {
  180. struct dma_device dma_dev;
  181. struct device *dev;
  182. struct clk *dma_clk;
  183. spinlock_t global_lock;
  184. void __iomem *base_addr;
  185. const struct tegra_dma_chip_data *chip_data;
  186. /* Some register need to be cache before suspend */
  187. u32 reg_gen;
  188. /* Last member of the structure */
  189. struct tegra_dma_channel channels[0];
  190. };
  191. static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
  192. {
  193. writel(val, tdma->base_addr + reg);
  194. }
  195. static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg)
  196. {
  197. return readl(tdma->base_addr + reg);
  198. }
  199. static inline void tdc_write(struct tegra_dma_channel *tdc,
  200. u32 reg, u32 val)
  201. {
  202. writel(val, tdc->tdma->base_addr + tdc->chan_base_offset + reg);
  203. }
  204. static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
  205. {
  206. return readl(tdc->tdma->base_addr + tdc->chan_base_offset + reg);
  207. }
  208. static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
  209. {
  210. return container_of(dc, struct tegra_dma_channel, dma_chan);
  211. }
  212. static inline struct tegra_dma_desc *txd_to_tegra_dma_desc(
  213. struct dma_async_tx_descriptor *td)
  214. {
  215. return container_of(td, struct tegra_dma_desc, txd);
  216. }
  217. static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
  218. {
  219. return &tdc->dma_chan.dev->device;
  220. }
  221. static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
  222. static int tegra_dma_runtime_suspend(struct device *dev);
  223. static int tegra_dma_runtime_resume(struct device *dev);
  224. /* Get DMA desc from free list, if not there then allocate it. */
  225. static struct tegra_dma_desc *tegra_dma_desc_get(
  226. struct tegra_dma_channel *tdc)
  227. {
  228. struct tegra_dma_desc *dma_desc;
  229. unsigned long flags;
  230. spin_lock_irqsave(&tdc->lock, flags);
  231. /* Do not allocate if desc are waiting for ack */
  232. list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
  233. if (async_tx_test_ack(&dma_desc->txd)) {
  234. list_del(&dma_desc->node);
  235. spin_unlock_irqrestore(&tdc->lock, flags);
  236. dma_desc->txd.flags = 0;
  237. return dma_desc;
  238. }
  239. }
  240. spin_unlock_irqrestore(&tdc->lock, flags);
  241. /* Allocate DMA desc */
  242. dma_desc = kzalloc(sizeof(*dma_desc), GFP_ATOMIC);
  243. if (!dma_desc) {
  244. dev_err(tdc2dev(tdc), "dma_desc alloc failed\n");
  245. return NULL;
  246. }
  247. dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
  248. dma_desc->txd.tx_submit = tegra_dma_tx_submit;
  249. dma_desc->txd.flags = 0;
  250. return dma_desc;
  251. }
  252. static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
  253. struct tegra_dma_desc *dma_desc)
  254. {
  255. unsigned long flags;
  256. spin_lock_irqsave(&tdc->lock, flags);
  257. if (!list_empty(&dma_desc->tx_list))
  258. list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
  259. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  260. spin_unlock_irqrestore(&tdc->lock, flags);
  261. }
  262. static struct tegra_dma_sg_req *tegra_dma_sg_req_get(
  263. struct tegra_dma_channel *tdc)
  264. {
  265. struct tegra_dma_sg_req *sg_req = NULL;
  266. unsigned long flags;
  267. spin_lock_irqsave(&tdc->lock, flags);
  268. if (!list_empty(&tdc->free_sg_req)) {
  269. sg_req = list_first_entry(&tdc->free_sg_req,
  270. typeof(*sg_req), node);
  271. list_del(&sg_req->node);
  272. spin_unlock_irqrestore(&tdc->lock, flags);
  273. return sg_req;
  274. }
  275. spin_unlock_irqrestore(&tdc->lock, flags);
  276. sg_req = kzalloc(sizeof(struct tegra_dma_sg_req), GFP_ATOMIC);
  277. if (!sg_req)
  278. dev_err(tdc2dev(tdc), "sg_req alloc failed\n");
  279. return sg_req;
  280. }
  281. static int tegra_dma_slave_config(struct dma_chan *dc,
  282. struct dma_slave_config *sconfig)
  283. {
  284. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  285. if (!list_empty(&tdc->pending_sg_req)) {
  286. dev_err(tdc2dev(tdc), "Configuration not allowed\n");
  287. return -EBUSY;
  288. }
  289. memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
  290. tdc->config_init = true;
  291. return 0;
  292. }
  293. static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
  294. bool wait_for_burst_complete)
  295. {
  296. struct tegra_dma *tdma = tdc->tdma;
  297. spin_lock(&tdma->global_lock);
  298. tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0);
  299. if (wait_for_burst_complete)
  300. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  301. }
  302. static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
  303. {
  304. struct tegra_dma *tdma = tdc->tdma;
  305. tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
  306. spin_unlock(&tdma->global_lock);
  307. }
  308. static void tegra_dma_pause(struct tegra_dma_channel *tdc,
  309. bool wait_for_burst_complete)
  310. {
  311. struct tegra_dma *tdma = tdc->tdma;
  312. if (tdma->chip_data->support_channel_pause) {
  313. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE,
  314. TEGRA_APBDMA_CHAN_CSRE_PAUSE);
  315. if (wait_for_burst_complete)
  316. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  317. } else {
  318. tegra_dma_global_pause(tdc, wait_for_burst_complete);
  319. }
  320. }
  321. static void tegra_dma_resume(struct tegra_dma_channel *tdc)
  322. {
  323. struct tegra_dma *tdma = tdc->tdma;
  324. if (tdma->chip_data->support_channel_pause) {
  325. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
  326. } else {
  327. tegra_dma_global_resume(tdc);
  328. }
  329. }
  330. static void tegra_dma_stop(struct tegra_dma_channel *tdc)
  331. {
  332. u32 csr;
  333. u32 status;
  334. /* Disable interrupts */
  335. csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
  336. csr &= ~TEGRA_APBDMA_CSR_IE_EOC;
  337. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
  338. /* Disable DMA */
  339. csr &= ~TEGRA_APBDMA_CSR_ENB;
  340. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
  341. /* Clear interrupt status if it is there */
  342. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  343. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  344. dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
  345. tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
  346. }
  347. tdc->busy = false;
  348. }
  349. static void tegra_dma_start(struct tegra_dma_channel *tdc,
  350. struct tegra_dma_sg_req *sg_req)
  351. {
  352. struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
  353. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr);
  354. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq);
  355. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr);
  356. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq);
  357. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr);
  358. /* Start DMA */
  359. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
  360. ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
  361. }
  362. static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
  363. struct tegra_dma_sg_req *nsg_req)
  364. {
  365. unsigned long status;
  366. /*
  367. * The DMA controller reloads the new configuration for next transfer
  368. * after last burst of current transfer completes.
  369. * If there is no IEC status then this makes sure that last burst
  370. * has not be completed. There may be case that last burst is on
  371. * flight and so it can complete but because DMA is paused, it
  372. * will not generates interrupt as well as not reload the new
  373. * configuration.
  374. * If there is already IEC status then interrupt handler need to
  375. * load new configuration.
  376. */
  377. tegra_dma_pause(tdc, false);
  378. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  379. /*
  380. * If interrupt is pending then do nothing as the ISR will handle
  381. * the programing for new request.
  382. */
  383. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  384. dev_err(tdc2dev(tdc),
  385. "Skipping new configuration as interrupt is pending\n");
  386. tegra_dma_resume(tdc);
  387. return;
  388. }
  389. /* Safe to program new configuration */
  390. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr);
  391. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
  392. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
  393. nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
  394. nsg_req->configured = true;
  395. tegra_dma_resume(tdc);
  396. }
  397. static void tdc_start_head_req(struct tegra_dma_channel *tdc)
  398. {
  399. struct tegra_dma_sg_req *sg_req;
  400. if (list_empty(&tdc->pending_sg_req))
  401. return;
  402. sg_req = list_first_entry(&tdc->pending_sg_req,
  403. typeof(*sg_req), node);
  404. tegra_dma_start(tdc, sg_req);
  405. sg_req->configured = true;
  406. tdc->busy = true;
  407. }
  408. static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
  409. {
  410. struct tegra_dma_sg_req *hsgreq;
  411. struct tegra_dma_sg_req *hnsgreq;
  412. if (list_empty(&tdc->pending_sg_req))
  413. return;
  414. hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
  415. if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
  416. hnsgreq = list_first_entry(&hsgreq->node,
  417. typeof(*hnsgreq), node);
  418. tegra_dma_configure_for_next(tdc, hnsgreq);
  419. }
  420. }
  421. static inline int get_current_xferred_count(struct tegra_dma_channel *tdc,
  422. struct tegra_dma_sg_req *sg_req, unsigned long status)
  423. {
  424. return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
  425. }
  426. static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
  427. {
  428. struct tegra_dma_sg_req *sgreq;
  429. struct tegra_dma_desc *dma_desc;
  430. while (!list_empty(&tdc->pending_sg_req)) {
  431. sgreq = list_first_entry(&tdc->pending_sg_req,
  432. typeof(*sgreq), node);
  433. list_move_tail(&sgreq->node, &tdc->free_sg_req);
  434. if (sgreq->last_sg) {
  435. dma_desc = sgreq->dma_desc;
  436. dma_desc->dma_status = DMA_ERROR;
  437. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  438. /* Add in cb list if it is not there. */
  439. if (!dma_desc->cb_count)
  440. list_add_tail(&dma_desc->cb_node,
  441. &tdc->cb_desc);
  442. dma_desc->cb_count++;
  443. }
  444. }
  445. tdc->isr_handler = NULL;
  446. }
  447. static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
  448. struct tegra_dma_sg_req *last_sg_req, bool to_terminate)
  449. {
  450. struct tegra_dma_sg_req *hsgreq = NULL;
  451. if (list_empty(&tdc->pending_sg_req)) {
  452. dev_err(tdc2dev(tdc), "Dma is running without req\n");
  453. tegra_dma_stop(tdc);
  454. return false;
  455. }
  456. /*
  457. * Check that head req on list should be in flight.
  458. * If it is not in flight then abort transfer as
  459. * looping of transfer can not continue.
  460. */
  461. hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
  462. if (!hsgreq->configured) {
  463. tegra_dma_stop(tdc);
  464. dev_err(tdc2dev(tdc), "Error in dma transfer, aborting dma\n");
  465. tegra_dma_abort_all(tdc);
  466. return false;
  467. }
  468. /* Configure next request */
  469. if (!to_terminate)
  470. tdc_configure_next_head_desc(tdc);
  471. return true;
  472. }
  473. static void handle_once_dma_done(struct tegra_dma_channel *tdc,
  474. bool to_terminate)
  475. {
  476. struct tegra_dma_sg_req *sgreq;
  477. struct tegra_dma_desc *dma_desc;
  478. tdc->busy = false;
  479. sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
  480. dma_desc = sgreq->dma_desc;
  481. dma_desc->bytes_transferred += sgreq->req_len;
  482. list_del(&sgreq->node);
  483. if (sgreq->last_sg) {
  484. dma_desc->dma_status = DMA_SUCCESS;
  485. dma_cookie_complete(&dma_desc->txd);
  486. if (!dma_desc->cb_count)
  487. list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
  488. dma_desc->cb_count++;
  489. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  490. }
  491. list_add_tail(&sgreq->node, &tdc->free_sg_req);
  492. /* Do not start DMA if it is going to be terminate */
  493. if (to_terminate || list_empty(&tdc->pending_sg_req))
  494. return;
  495. tdc_start_head_req(tdc);
  496. return;
  497. }
  498. static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
  499. bool to_terminate)
  500. {
  501. struct tegra_dma_sg_req *sgreq;
  502. struct tegra_dma_desc *dma_desc;
  503. bool st;
  504. sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
  505. dma_desc = sgreq->dma_desc;
  506. dma_desc->bytes_transferred += sgreq->req_len;
  507. /* Callback need to be call */
  508. if (!dma_desc->cb_count)
  509. list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
  510. dma_desc->cb_count++;
  511. /* If not last req then put at end of pending list */
  512. if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
  513. list_move_tail(&sgreq->node, &tdc->pending_sg_req);
  514. sgreq->configured = false;
  515. st = handle_continuous_head_request(tdc, sgreq, to_terminate);
  516. if (!st)
  517. dma_desc->dma_status = DMA_ERROR;
  518. }
  519. return;
  520. }
  521. static void tegra_dma_tasklet(unsigned long data)
  522. {
  523. struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
  524. dma_async_tx_callback callback = NULL;
  525. void *callback_param = NULL;
  526. struct tegra_dma_desc *dma_desc;
  527. unsigned long flags;
  528. int cb_count;
  529. spin_lock_irqsave(&tdc->lock, flags);
  530. while (!list_empty(&tdc->cb_desc)) {
  531. dma_desc = list_first_entry(&tdc->cb_desc,
  532. typeof(*dma_desc), cb_node);
  533. list_del(&dma_desc->cb_node);
  534. callback = dma_desc->txd.callback;
  535. callback_param = dma_desc->txd.callback_param;
  536. cb_count = dma_desc->cb_count;
  537. dma_desc->cb_count = 0;
  538. spin_unlock_irqrestore(&tdc->lock, flags);
  539. while (cb_count-- && callback)
  540. callback(callback_param);
  541. spin_lock_irqsave(&tdc->lock, flags);
  542. }
  543. spin_unlock_irqrestore(&tdc->lock, flags);
  544. }
  545. static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
  546. {
  547. struct tegra_dma_channel *tdc = dev_id;
  548. unsigned long status;
  549. unsigned long flags;
  550. spin_lock_irqsave(&tdc->lock, flags);
  551. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  552. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  553. tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
  554. tdc->isr_handler(tdc, false);
  555. tasklet_schedule(&tdc->tasklet);
  556. spin_unlock_irqrestore(&tdc->lock, flags);
  557. return IRQ_HANDLED;
  558. }
  559. spin_unlock_irqrestore(&tdc->lock, flags);
  560. dev_info(tdc2dev(tdc),
  561. "Interrupt already served status 0x%08lx\n", status);
  562. return IRQ_NONE;
  563. }
  564. static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  565. {
  566. struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
  567. struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
  568. unsigned long flags;
  569. dma_cookie_t cookie;
  570. spin_lock_irqsave(&tdc->lock, flags);
  571. dma_desc->dma_status = DMA_IN_PROGRESS;
  572. cookie = dma_cookie_assign(&dma_desc->txd);
  573. list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
  574. spin_unlock_irqrestore(&tdc->lock, flags);
  575. return cookie;
  576. }
  577. static void tegra_dma_issue_pending(struct dma_chan *dc)
  578. {
  579. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  580. unsigned long flags;
  581. spin_lock_irqsave(&tdc->lock, flags);
  582. if (list_empty(&tdc->pending_sg_req)) {
  583. dev_err(tdc2dev(tdc), "No DMA request\n");
  584. goto end;
  585. }
  586. if (!tdc->busy) {
  587. tdc_start_head_req(tdc);
  588. /* Continuous single mode: Configure next req */
  589. if (tdc->cyclic) {
  590. /*
  591. * Wait for 1 burst time for configure DMA for
  592. * next transfer.
  593. */
  594. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  595. tdc_configure_next_head_desc(tdc);
  596. }
  597. }
  598. end:
  599. spin_unlock_irqrestore(&tdc->lock, flags);
  600. return;
  601. }
  602. static void tegra_dma_terminate_all(struct dma_chan *dc)
  603. {
  604. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  605. struct tegra_dma_sg_req *sgreq;
  606. struct tegra_dma_desc *dma_desc;
  607. unsigned long flags;
  608. unsigned long status;
  609. bool was_busy;
  610. spin_lock_irqsave(&tdc->lock, flags);
  611. if (list_empty(&tdc->pending_sg_req)) {
  612. spin_unlock_irqrestore(&tdc->lock, flags);
  613. return;
  614. }
  615. if (!tdc->busy)
  616. goto skip_dma_stop;
  617. /* Pause DMA before checking the queue status */
  618. tegra_dma_pause(tdc, true);
  619. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  620. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  621. dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
  622. tdc->isr_handler(tdc, true);
  623. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  624. }
  625. was_busy = tdc->busy;
  626. tegra_dma_stop(tdc);
  627. if (!list_empty(&tdc->pending_sg_req) && was_busy) {
  628. sgreq = list_first_entry(&tdc->pending_sg_req,
  629. typeof(*sgreq), node);
  630. sgreq->dma_desc->bytes_transferred +=
  631. get_current_xferred_count(tdc, sgreq, status);
  632. }
  633. tegra_dma_resume(tdc);
  634. skip_dma_stop:
  635. tegra_dma_abort_all(tdc);
  636. while (!list_empty(&tdc->cb_desc)) {
  637. dma_desc = list_first_entry(&tdc->cb_desc,
  638. typeof(*dma_desc), cb_node);
  639. list_del(&dma_desc->cb_node);
  640. dma_desc->cb_count = 0;
  641. }
  642. spin_unlock_irqrestore(&tdc->lock, flags);
  643. }
  644. static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
  645. dma_cookie_t cookie, struct dma_tx_state *txstate)
  646. {
  647. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  648. struct tegra_dma_desc *dma_desc;
  649. struct tegra_dma_sg_req *sg_req;
  650. enum dma_status ret;
  651. unsigned long flags;
  652. unsigned int residual;
  653. spin_lock_irqsave(&tdc->lock, flags);
  654. ret = dma_cookie_status(dc, cookie, txstate);
  655. if (ret == DMA_SUCCESS) {
  656. spin_unlock_irqrestore(&tdc->lock, flags);
  657. return ret;
  658. }
  659. /* Check on wait_ack desc status */
  660. list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
  661. if (dma_desc->txd.cookie == cookie) {
  662. residual = dma_desc->bytes_requested -
  663. (dma_desc->bytes_transferred %
  664. dma_desc->bytes_requested);
  665. dma_set_residue(txstate, residual);
  666. ret = dma_desc->dma_status;
  667. spin_unlock_irqrestore(&tdc->lock, flags);
  668. return ret;
  669. }
  670. }
  671. /* Check in pending list */
  672. list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
  673. dma_desc = sg_req->dma_desc;
  674. if (dma_desc->txd.cookie == cookie) {
  675. residual = dma_desc->bytes_requested -
  676. (dma_desc->bytes_transferred %
  677. dma_desc->bytes_requested);
  678. dma_set_residue(txstate, residual);
  679. ret = dma_desc->dma_status;
  680. spin_unlock_irqrestore(&tdc->lock, flags);
  681. return ret;
  682. }
  683. }
  684. dev_dbg(tdc2dev(tdc), "cookie %d does not found\n", cookie);
  685. spin_unlock_irqrestore(&tdc->lock, flags);
  686. return ret;
  687. }
  688. static int tegra_dma_device_control(struct dma_chan *dc, enum dma_ctrl_cmd cmd,
  689. unsigned long arg)
  690. {
  691. switch (cmd) {
  692. case DMA_SLAVE_CONFIG:
  693. return tegra_dma_slave_config(dc,
  694. (struct dma_slave_config *)arg);
  695. case DMA_TERMINATE_ALL:
  696. tegra_dma_terminate_all(dc);
  697. return 0;
  698. default:
  699. break;
  700. }
  701. return -ENXIO;
  702. }
  703. static inline int get_bus_width(struct tegra_dma_channel *tdc,
  704. enum dma_slave_buswidth slave_bw)
  705. {
  706. switch (slave_bw) {
  707. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  708. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8;
  709. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  710. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16;
  711. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  712. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
  713. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  714. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
  715. default:
  716. dev_warn(tdc2dev(tdc),
  717. "slave bw is not supported, using 32bits\n");
  718. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
  719. }
  720. }
  721. static inline int get_burst_size(struct tegra_dma_channel *tdc,
  722. u32 burst_size, enum dma_slave_buswidth slave_bw, int len)
  723. {
  724. int burst_byte;
  725. int burst_ahb_width;
  726. /*
  727. * burst_size from client is in terms of the bus_width.
  728. * convert them into AHB memory width which is 4 byte.
  729. */
  730. burst_byte = burst_size * slave_bw;
  731. burst_ahb_width = burst_byte / 4;
  732. /* If burst size is 0 then calculate the burst size based on length */
  733. if (!burst_ahb_width) {
  734. if (len & 0xF)
  735. return TEGRA_APBDMA_AHBSEQ_BURST_1;
  736. else if ((len >> 4) & 0x1)
  737. return TEGRA_APBDMA_AHBSEQ_BURST_4;
  738. else
  739. return TEGRA_APBDMA_AHBSEQ_BURST_8;
  740. }
  741. if (burst_ahb_width < 4)
  742. return TEGRA_APBDMA_AHBSEQ_BURST_1;
  743. else if (burst_ahb_width < 8)
  744. return TEGRA_APBDMA_AHBSEQ_BURST_4;
  745. else
  746. return TEGRA_APBDMA_AHBSEQ_BURST_8;
  747. }
  748. static int get_transfer_param(struct tegra_dma_channel *tdc,
  749. enum dma_transfer_direction direction, unsigned long *apb_addr,
  750. unsigned long *apb_seq, unsigned long *csr, unsigned int *burst_size,
  751. enum dma_slave_buswidth *slave_bw)
  752. {
  753. switch (direction) {
  754. case DMA_MEM_TO_DEV:
  755. *apb_addr = tdc->dma_sconfig.dst_addr;
  756. *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width);
  757. *burst_size = tdc->dma_sconfig.dst_maxburst;
  758. *slave_bw = tdc->dma_sconfig.dst_addr_width;
  759. *csr = TEGRA_APBDMA_CSR_DIR;
  760. return 0;
  761. case DMA_DEV_TO_MEM:
  762. *apb_addr = tdc->dma_sconfig.src_addr;
  763. *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width);
  764. *burst_size = tdc->dma_sconfig.src_maxburst;
  765. *slave_bw = tdc->dma_sconfig.src_addr_width;
  766. *csr = 0;
  767. return 0;
  768. default:
  769. dev_err(tdc2dev(tdc), "Dma direction is not supported\n");
  770. return -EINVAL;
  771. }
  772. return -EINVAL;
  773. }
  774. static struct dma_async_tx_descriptor *tegra_dma_prep_slave_sg(
  775. struct dma_chan *dc, struct scatterlist *sgl, unsigned int sg_len,
  776. enum dma_transfer_direction direction, unsigned long flags,
  777. void *context)
  778. {
  779. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  780. struct tegra_dma_desc *dma_desc;
  781. unsigned int i;
  782. struct scatterlist *sg;
  783. unsigned long csr, ahb_seq, apb_ptr, apb_seq;
  784. struct list_head req_list;
  785. struct tegra_dma_sg_req *sg_req = NULL;
  786. u32 burst_size;
  787. enum dma_slave_buswidth slave_bw;
  788. int ret;
  789. if (!tdc->config_init) {
  790. dev_err(tdc2dev(tdc), "dma channel is not configured\n");
  791. return NULL;
  792. }
  793. if (sg_len < 1) {
  794. dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len);
  795. return NULL;
  796. }
  797. ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
  798. &burst_size, &slave_bw);
  799. if (ret < 0)
  800. return NULL;
  801. INIT_LIST_HEAD(&req_list);
  802. ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
  803. ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
  804. TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
  805. ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
  806. csr |= TEGRA_APBDMA_CSR_ONCE | TEGRA_APBDMA_CSR_FLOW;
  807. csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
  808. if (flags & DMA_PREP_INTERRUPT)
  809. csr |= TEGRA_APBDMA_CSR_IE_EOC;
  810. apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
  811. dma_desc = tegra_dma_desc_get(tdc);
  812. if (!dma_desc) {
  813. dev_err(tdc2dev(tdc), "Dma descriptors not available\n");
  814. return NULL;
  815. }
  816. INIT_LIST_HEAD(&dma_desc->tx_list);
  817. INIT_LIST_HEAD(&dma_desc->cb_node);
  818. dma_desc->cb_count = 0;
  819. dma_desc->bytes_requested = 0;
  820. dma_desc->bytes_transferred = 0;
  821. dma_desc->dma_status = DMA_IN_PROGRESS;
  822. /* Make transfer requests */
  823. for_each_sg(sgl, sg, sg_len, i) {
  824. u32 len, mem;
  825. mem = sg_dma_address(sg);
  826. len = sg_dma_len(sg);
  827. if ((len & 3) || (mem & 3) ||
  828. (len > tdc->tdma->chip_data->max_dma_count)) {
  829. dev_err(tdc2dev(tdc),
  830. "Dma length/memory address is not supported\n");
  831. tegra_dma_desc_put(tdc, dma_desc);
  832. return NULL;
  833. }
  834. sg_req = tegra_dma_sg_req_get(tdc);
  835. if (!sg_req) {
  836. dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
  837. tegra_dma_desc_put(tdc, dma_desc);
  838. return NULL;
  839. }
  840. ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
  841. dma_desc->bytes_requested += len;
  842. sg_req->ch_regs.apb_ptr = apb_ptr;
  843. sg_req->ch_regs.ahb_ptr = mem;
  844. sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
  845. sg_req->ch_regs.apb_seq = apb_seq;
  846. sg_req->ch_regs.ahb_seq = ahb_seq;
  847. sg_req->configured = false;
  848. sg_req->last_sg = false;
  849. sg_req->dma_desc = dma_desc;
  850. sg_req->req_len = len;
  851. list_add_tail(&sg_req->node, &dma_desc->tx_list);
  852. }
  853. sg_req->last_sg = true;
  854. if (flags & DMA_CTRL_ACK)
  855. dma_desc->txd.flags = DMA_CTRL_ACK;
  856. /*
  857. * Make sure that mode should not be conflicting with currently
  858. * configured mode.
  859. */
  860. if (!tdc->isr_handler) {
  861. tdc->isr_handler = handle_once_dma_done;
  862. tdc->cyclic = false;
  863. } else {
  864. if (tdc->cyclic) {
  865. dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n");
  866. tegra_dma_desc_put(tdc, dma_desc);
  867. return NULL;
  868. }
  869. }
  870. return &dma_desc->txd;
  871. }
  872. struct dma_async_tx_descriptor *tegra_dma_prep_dma_cyclic(
  873. struct dma_chan *dc, dma_addr_t buf_addr, size_t buf_len,
  874. size_t period_len, enum dma_transfer_direction direction,
  875. unsigned long flags, void *context)
  876. {
  877. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  878. struct tegra_dma_desc *dma_desc = NULL;
  879. struct tegra_dma_sg_req *sg_req = NULL;
  880. unsigned long csr, ahb_seq, apb_ptr, apb_seq;
  881. int len;
  882. size_t remain_len;
  883. dma_addr_t mem = buf_addr;
  884. u32 burst_size;
  885. enum dma_slave_buswidth slave_bw;
  886. int ret;
  887. if (!buf_len || !period_len) {
  888. dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
  889. return NULL;
  890. }
  891. if (!tdc->config_init) {
  892. dev_err(tdc2dev(tdc), "DMA slave is not configured\n");
  893. return NULL;
  894. }
  895. /*
  896. * We allow to take more number of requests till DMA is
  897. * not started. The driver will loop over all requests.
  898. * Once DMA is started then new requests can be queued only after
  899. * terminating the DMA.
  900. */
  901. if (tdc->busy) {
  902. dev_err(tdc2dev(tdc), "Request not allowed when dma running\n");
  903. return NULL;
  904. }
  905. /*
  906. * We only support cycle transfer when buf_len is multiple of
  907. * period_len.
  908. */
  909. if (buf_len % period_len) {
  910. dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n");
  911. return NULL;
  912. }
  913. len = period_len;
  914. if ((len & 3) || (buf_addr & 3) ||
  915. (len > tdc->tdma->chip_data->max_dma_count)) {
  916. dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
  917. return NULL;
  918. }
  919. ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
  920. &burst_size, &slave_bw);
  921. if (ret < 0)
  922. return NULL;
  923. ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
  924. ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
  925. TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
  926. ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
  927. csr |= TEGRA_APBDMA_CSR_FLOW;
  928. if (flags & DMA_PREP_INTERRUPT)
  929. csr |= TEGRA_APBDMA_CSR_IE_EOC;
  930. csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
  931. apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
  932. dma_desc = tegra_dma_desc_get(tdc);
  933. if (!dma_desc) {
  934. dev_err(tdc2dev(tdc), "not enough descriptors available\n");
  935. return NULL;
  936. }
  937. INIT_LIST_HEAD(&dma_desc->tx_list);
  938. INIT_LIST_HEAD(&dma_desc->cb_node);
  939. dma_desc->cb_count = 0;
  940. dma_desc->bytes_transferred = 0;
  941. dma_desc->bytes_requested = buf_len;
  942. remain_len = buf_len;
  943. /* Split transfer equal to period size */
  944. while (remain_len) {
  945. sg_req = tegra_dma_sg_req_get(tdc);
  946. if (!sg_req) {
  947. dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
  948. tegra_dma_desc_put(tdc, dma_desc);
  949. return NULL;
  950. }
  951. ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
  952. sg_req->ch_regs.apb_ptr = apb_ptr;
  953. sg_req->ch_regs.ahb_ptr = mem;
  954. sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
  955. sg_req->ch_regs.apb_seq = apb_seq;
  956. sg_req->ch_regs.ahb_seq = ahb_seq;
  957. sg_req->configured = false;
  958. sg_req->half_done = false;
  959. sg_req->last_sg = false;
  960. sg_req->dma_desc = dma_desc;
  961. sg_req->req_len = len;
  962. list_add_tail(&sg_req->node, &dma_desc->tx_list);
  963. remain_len -= len;
  964. mem += len;
  965. }
  966. sg_req->last_sg = true;
  967. if (flags & DMA_CTRL_ACK)
  968. dma_desc->txd.flags = DMA_CTRL_ACK;
  969. /*
  970. * Make sure that mode should not be conflicting with currently
  971. * configured mode.
  972. */
  973. if (!tdc->isr_handler) {
  974. tdc->isr_handler = handle_cont_sngl_cycle_dma_done;
  975. tdc->cyclic = true;
  976. } else {
  977. if (!tdc->cyclic) {
  978. dev_err(tdc2dev(tdc), "DMA configuration conflict\n");
  979. tegra_dma_desc_put(tdc, dma_desc);
  980. return NULL;
  981. }
  982. }
  983. return &dma_desc->txd;
  984. }
  985. static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
  986. {
  987. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  988. struct tegra_dma *tdma = tdc->tdma;
  989. int ret;
  990. dma_cookie_init(&tdc->dma_chan);
  991. tdc->config_init = false;
  992. ret = clk_prepare_enable(tdma->dma_clk);
  993. if (ret < 0)
  994. dev_err(tdc2dev(tdc), "clk_prepare_enable failed: %d\n", ret);
  995. return ret;
  996. }
  997. static void tegra_dma_free_chan_resources(struct dma_chan *dc)
  998. {
  999. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  1000. struct tegra_dma *tdma = tdc->tdma;
  1001. struct tegra_dma_desc *dma_desc;
  1002. struct tegra_dma_sg_req *sg_req;
  1003. struct list_head dma_desc_list;
  1004. struct list_head sg_req_list;
  1005. unsigned long flags;
  1006. INIT_LIST_HEAD(&dma_desc_list);
  1007. INIT_LIST_HEAD(&sg_req_list);
  1008. dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
  1009. if (tdc->busy)
  1010. tegra_dma_terminate_all(dc);
  1011. spin_lock_irqsave(&tdc->lock, flags);
  1012. list_splice_init(&tdc->pending_sg_req, &sg_req_list);
  1013. list_splice_init(&tdc->free_sg_req, &sg_req_list);
  1014. list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
  1015. INIT_LIST_HEAD(&tdc->cb_desc);
  1016. tdc->config_init = false;
  1017. spin_unlock_irqrestore(&tdc->lock, flags);
  1018. while (!list_empty(&dma_desc_list)) {
  1019. dma_desc = list_first_entry(&dma_desc_list,
  1020. typeof(*dma_desc), node);
  1021. list_del(&dma_desc->node);
  1022. kfree(dma_desc);
  1023. }
  1024. while (!list_empty(&sg_req_list)) {
  1025. sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node);
  1026. list_del(&sg_req->node);
  1027. kfree(sg_req);
  1028. }
  1029. clk_disable_unprepare(tdma->dma_clk);
  1030. }
  1031. /* Tegra20 specific DMA controller information */
  1032. static const struct tegra_dma_chip_data tegra20_dma_chip_data = {
  1033. .nr_channels = 16,
  1034. .max_dma_count = 1024UL * 64,
  1035. .support_channel_pause = false,
  1036. };
  1037. #if defined(CONFIG_OF)
  1038. /* Tegra30 specific DMA controller information */
  1039. static const struct tegra_dma_chip_data tegra30_dma_chip_data = {
  1040. .nr_channels = 32,
  1041. .max_dma_count = 1024UL * 64,
  1042. .support_channel_pause = false,
  1043. };
  1044. /* Tegra114 specific DMA controller information */
  1045. static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
  1046. .nr_channels = 32,
  1047. .max_dma_count = 1024UL * 64,
  1048. .support_channel_pause = true,
  1049. };
  1050. static const struct of_device_id tegra_dma_of_match[] = {
  1051. {
  1052. .compatible = "nvidia,tegra114-apbdma",
  1053. .data = &tegra114_dma_chip_data,
  1054. }, {
  1055. .compatible = "nvidia,tegra30-apbdma",
  1056. .data = &tegra30_dma_chip_data,
  1057. }, {
  1058. .compatible = "nvidia,tegra20-apbdma",
  1059. .data = &tegra20_dma_chip_data,
  1060. }, {
  1061. },
  1062. };
  1063. MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
  1064. #endif
  1065. static int tegra_dma_probe(struct platform_device *pdev)
  1066. {
  1067. struct resource *res;
  1068. struct tegra_dma *tdma;
  1069. int ret;
  1070. int i;
  1071. const struct tegra_dma_chip_data *cdata = NULL;
  1072. if (pdev->dev.of_node) {
  1073. const struct of_device_id *match;
  1074. match = of_match_device(of_match_ptr(tegra_dma_of_match),
  1075. &pdev->dev);
  1076. if (!match) {
  1077. dev_err(&pdev->dev, "Error: No device match found\n");
  1078. return -ENODEV;
  1079. }
  1080. cdata = match->data;
  1081. } else {
  1082. /* If no device tree then fallback to tegra20 */
  1083. cdata = &tegra20_dma_chip_data;
  1084. }
  1085. tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
  1086. sizeof(struct tegra_dma_channel), GFP_KERNEL);
  1087. if (!tdma) {
  1088. dev_err(&pdev->dev, "Error: memory allocation failed\n");
  1089. return -ENOMEM;
  1090. }
  1091. tdma->dev = &pdev->dev;
  1092. tdma->chip_data = cdata;
  1093. platform_set_drvdata(pdev, tdma);
  1094. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1095. if (!res) {
  1096. dev_err(&pdev->dev, "No mem resource for DMA\n");
  1097. return -EINVAL;
  1098. }
  1099. tdma->base_addr = devm_ioremap_resource(&pdev->dev, res);
  1100. if (IS_ERR(tdma->base_addr))
  1101. return PTR_ERR(tdma->base_addr);
  1102. tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
  1103. if (IS_ERR(tdma->dma_clk)) {
  1104. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  1105. return PTR_ERR(tdma->dma_clk);
  1106. }
  1107. spin_lock_init(&tdma->global_lock);
  1108. pm_runtime_enable(&pdev->dev);
  1109. if (!pm_runtime_enabled(&pdev->dev)) {
  1110. ret = tegra_dma_runtime_resume(&pdev->dev);
  1111. if (ret) {
  1112. dev_err(&pdev->dev, "dma_runtime_resume failed %d\n",
  1113. ret);
  1114. goto err_pm_disable;
  1115. }
  1116. }
  1117. /* Enable clock before accessing registers */
  1118. ret = clk_prepare_enable(tdma->dma_clk);
  1119. if (ret < 0) {
  1120. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  1121. goto err_pm_disable;
  1122. }
  1123. /* Reset DMA controller */
  1124. tegra_periph_reset_assert(tdma->dma_clk);
  1125. udelay(2);
  1126. tegra_periph_reset_deassert(tdma->dma_clk);
  1127. /* Enable global DMA registers */
  1128. tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
  1129. tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
  1130. tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
  1131. clk_disable_unprepare(tdma->dma_clk);
  1132. INIT_LIST_HEAD(&tdma->dma_dev.channels);
  1133. for (i = 0; i < cdata->nr_channels; i++) {
  1134. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1135. tdc->chan_base_offset = TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
  1136. i * TEGRA_APBDMA_CHANNEL_REGISTER_SIZE;
  1137. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1138. if (!res) {
  1139. ret = -EINVAL;
  1140. dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
  1141. goto err_irq;
  1142. }
  1143. tdc->irq = res->start;
  1144. snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
  1145. ret = devm_request_irq(&pdev->dev, tdc->irq,
  1146. tegra_dma_isr, 0, tdc->name, tdc);
  1147. if (ret) {
  1148. dev_err(&pdev->dev,
  1149. "request_irq failed with err %d channel %d\n",
  1150. i, ret);
  1151. goto err_irq;
  1152. }
  1153. tdc->dma_chan.device = &tdma->dma_dev;
  1154. dma_cookie_init(&tdc->dma_chan);
  1155. list_add_tail(&tdc->dma_chan.device_node,
  1156. &tdma->dma_dev.channels);
  1157. tdc->tdma = tdma;
  1158. tdc->id = i;
  1159. tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
  1160. (unsigned long)tdc);
  1161. spin_lock_init(&tdc->lock);
  1162. INIT_LIST_HEAD(&tdc->pending_sg_req);
  1163. INIT_LIST_HEAD(&tdc->free_sg_req);
  1164. INIT_LIST_HEAD(&tdc->free_dma_desc);
  1165. INIT_LIST_HEAD(&tdc->cb_desc);
  1166. }
  1167. dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
  1168. dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
  1169. dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
  1170. tdma->dma_dev.dev = &pdev->dev;
  1171. tdma->dma_dev.device_alloc_chan_resources =
  1172. tegra_dma_alloc_chan_resources;
  1173. tdma->dma_dev.device_free_chan_resources =
  1174. tegra_dma_free_chan_resources;
  1175. tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
  1176. tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
  1177. tdma->dma_dev.device_control = tegra_dma_device_control;
  1178. tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
  1179. tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
  1180. ret = dma_async_device_register(&tdma->dma_dev);
  1181. if (ret < 0) {
  1182. dev_err(&pdev->dev,
  1183. "Tegra20 APB DMA driver registration failed %d\n", ret);
  1184. goto err_irq;
  1185. }
  1186. dev_info(&pdev->dev, "Tegra20 APB DMA driver register %d channels\n",
  1187. cdata->nr_channels);
  1188. return 0;
  1189. err_irq:
  1190. while (--i >= 0) {
  1191. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1192. tasklet_kill(&tdc->tasklet);
  1193. }
  1194. err_pm_disable:
  1195. pm_runtime_disable(&pdev->dev);
  1196. if (!pm_runtime_status_suspended(&pdev->dev))
  1197. tegra_dma_runtime_suspend(&pdev->dev);
  1198. return ret;
  1199. }
  1200. static int tegra_dma_remove(struct platform_device *pdev)
  1201. {
  1202. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1203. int i;
  1204. struct tegra_dma_channel *tdc;
  1205. dma_async_device_unregister(&tdma->dma_dev);
  1206. for (i = 0; i < tdma->chip_data->nr_channels; ++i) {
  1207. tdc = &tdma->channels[i];
  1208. tasklet_kill(&tdc->tasklet);
  1209. }
  1210. pm_runtime_disable(&pdev->dev);
  1211. if (!pm_runtime_status_suspended(&pdev->dev))
  1212. tegra_dma_runtime_suspend(&pdev->dev);
  1213. return 0;
  1214. }
  1215. static int tegra_dma_runtime_suspend(struct device *dev)
  1216. {
  1217. struct platform_device *pdev = to_platform_device(dev);
  1218. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1219. clk_disable_unprepare(tdma->dma_clk);
  1220. return 0;
  1221. }
  1222. static int tegra_dma_runtime_resume(struct device *dev)
  1223. {
  1224. struct platform_device *pdev = to_platform_device(dev);
  1225. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1226. int ret;
  1227. ret = clk_prepare_enable(tdma->dma_clk);
  1228. if (ret < 0) {
  1229. dev_err(dev, "clk_enable failed: %d\n", ret);
  1230. return ret;
  1231. }
  1232. return 0;
  1233. }
  1234. static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
  1235. #ifdef CONFIG_PM_RUNTIME
  1236. .runtime_suspend = tegra_dma_runtime_suspend,
  1237. .runtime_resume = tegra_dma_runtime_resume,
  1238. #endif
  1239. };
  1240. static struct platform_driver tegra_dmac_driver = {
  1241. .driver = {
  1242. .name = "tegra-apbdma",
  1243. .owner = THIS_MODULE,
  1244. .pm = &tegra_dma_dev_pm_ops,
  1245. .of_match_table = of_match_ptr(tegra_dma_of_match),
  1246. },
  1247. .probe = tegra_dma_probe,
  1248. .remove = tegra_dma_remove,
  1249. };
  1250. module_platform_driver(tegra_dmac_driver);
  1251. MODULE_ALIAS("platform:tegra20-apbdma");
  1252. MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
  1253. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1254. MODULE_LICENSE("GPL v2");