tegra20-apb-dma.c 39 KB

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