tegra20-apb-dma.c 39 KB

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