tegra20-apb-dma.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. /* Tegra114 specific DMA controller information */
  1041. static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
  1042. .nr_channels = 32,
  1043. .max_dma_count = 1024UL * 64,
  1044. .support_channel_pause = true,
  1045. };
  1046. static const struct of_device_id tegra_dma_of_match[] = {
  1047. {
  1048. .compatible = "nvidia,tegra114-apbdma",
  1049. .data = &tegra114_dma_chip_data,
  1050. }, {
  1051. .compatible = "nvidia,tegra30-apbdma",
  1052. .data = &tegra30_dma_chip_data,
  1053. }, {
  1054. .compatible = "nvidia,tegra20-apbdma",
  1055. .data = &tegra20_dma_chip_data,
  1056. }, {
  1057. },
  1058. };
  1059. MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
  1060. #endif
  1061. static int tegra_dma_probe(struct platform_device *pdev)
  1062. {
  1063. struct resource *res;
  1064. struct tegra_dma *tdma;
  1065. int ret;
  1066. int i;
  1067. const struct tegra_dma_chip_data *cdata = NULL;
  1068. if (pdev->dev.of_node) {
  1069. const struct of_device_id *match;
  1070. match = of_match_device(of_match_ptr(tegra_dma_of_match),
  1071. &pdev->dev);
  1072. if (!match) {
  1073. dev_err(&pdev->dev, "Error: No device match found\n");
  1074. return -ENODEV;
  1075. }
  1076. cdata = match->data;
  1077. } else {
  1078. /* If no device tree then fallback to tegra20 */
  1079. cdata = &tegra20_dma_chip_data;
  1080. }
  1081. tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
  1082. sizeof(struct tegra_dma_channel), GFP_KERNEL);
  1083. if (!tdma) {
  1084. dev_err(&pdev->dev, "Error: memory allocation failed\n");
  1085. return -ENOMEM;
  1086. }
  1087. tdma->dev = &pdev->dev;
  1088. tdma->chip_data = cdata;
  1089. platform_set_drvdata(pdev, tdma);
  1090. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1091. if (!res) {
  1092. dev_err(&pdev->dev, "No mem resource for DMA\n");
  1093. return -EINVAL;
  1094. }
  1095. tdma->base_addr = devm_request_and_ioremap(&pdev->dev, res);
  1096. if (!tdma->base_addr) {
  1097. dev_err(&pdev->dev,
  1098. "Cannot request memregion/iomap dma address\n");
  1099. return -EADDRNOTAVAIL;
  1100. }
  1101. tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
  1102. if (IS_ERR(tdma->dma_clk)) {
  1103. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  1104. return PTR_ERR(tdma->dma_clk);
  1105. }
  1106. spin_lock_init(&tdma->global_lock);
  1107. pm_runtime_enable(&pdev->dev);
  1108. if (!pm_runtime_enabled(&pdev->dev)) {
  1109. ret = tegra_dma_runtime_resume(&pdev->dev);
  1110. if (ret) {
  1111. dev_err(&pdev->dev, "dma_runtime_resume failed %d\n",
  1112. ret);
  1113. goto err_pm_disable;
  1114. }
  1115. }
  1116. /* Enable clock before accessing registers */
  1117. ret = clk_prepare_enable(tdma->dma_clk);
  1118. if (ret < 0) {
  1119. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  1120. goto err_pm_disable;
  1121. }
  1122. /* Reset DMA controller */
  1123. tegra_periph_reset_assert(tdma->dma_clk);
  1124. udelay(2);
  1125. tegra_periph_reset_deassert(tdma->dma_clk);
  1126. /* Enable global DMA registers */
  1127. tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
  1128. tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
  1129. tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
  1130. clk_disable_unprepare(tdma->dma_clk);
  1131. INIT_LIST_HEAD(&tdma->dma_dev.channels);
  1132. for (i = 0; i < cdata->nr_channels; i++) {
  1133. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1134. tdc->chan_base_offset = TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
  1135. i * TEGRA_APBDMA_CHANNEL_REGISTER_SIZE;
  1136. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1137. if (!res) {
  1138. ret = -EINVAL;
  1139. dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
  1140. goto err_irq;
  1141. }
  1142. tdc->irq = res->start;
  1143. snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
  1144. ret = devm_request_irq(&pdev->dev, tdc->irq,
  1145. tegra_dma_isr, 0, tdc->name, tdc);
  1146. if (ret) {
  1147. dev_err(&pdev->dev,
  1148. "request_irq failed with err %d channel %d\n",
  1149. i, ret);
  1150. goto err_irq;
  1151. }
  1152. tdc->dma_chan.device = &tdma->dma_dev;
  1153. dma_cookie_init(&tdc->dma_chan);
  1154. list_add_tail(&tdc->dma_chan.device_node,
  1155. &tdma->dma_dev.channels);
  1156. tdc->tdma = tdma;
  1157. tdc->id = i;
  1158. tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
  1159. (unsigned long)tdc);
  1160. spin_lock_init(&tdc->lock);
  1161. INIT_LIST_HEAD(&tdc->pending_sg_req);
  1162. INIT_LIST_HEAD(&tdc->free_sg_req);
  1163. INIT_LIST_HEAD(&tdc->free_dma_desc);
  1164. INIT_LIST_HEAD(&tdc->cb_desc);
  1165. }
  1166. dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
  1167. dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
  1168. dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
  1169. tdma->dma_dev.dev = &pdev->dev;
  1170. tdma->dma_dev.device_alloc_chan_resources =
  1171. tegra_dma_alloc_chan_resources;
  1172. tdma->dma_dev.device_free_chan_resources =
  1173. tegra_dma_free_chan_resources;
  1174. tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
  1175. tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
  1176. tdma->dma_dev.device_control = tegra_dma_device_control;
  1177. tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
  1178. tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
  1179. ret = dma_async_device_register(&tdma->dma_dev);
  1180. if (ret < 0) {
  1181. dev_err(&pdev->dev,
  1182. "Tegra20 APB DMA driver registration failed %d\n", ret);
  1183. goto err_irq;
  1184. }
  1185. dev_info(&pdev->dev, "Tegra20 APB DMA driver register %d channels\n",
  1186. cdata->nr_channels);
  1187. return 0;
  1188. err_irq:
  1189. while (--i >= 0) {
  1190. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1191. tasklet_kill(&tdc->tasklet);
  1192. }
  1193. err_pm_disable:
  1194. pm_runtime_disable(&pdev->dev);
  1195. if (!pm_runtime_status_suspended(&pdev->dev))
  1196. tegra_dma_runtime_suspend(&pdev->dev);
  1197. return ret;
  1198. }
  1199. static int __devexit tegra_dma_remove(struct platform_device *pdev)
  1200. {
  1201. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1202. int i;
  1203. struct tegra_dma_channel *tdc;
  1204. dma_async_device_unregister(&tdma->dma_dev);
  1205. for (i = 0; i < tdma->chip_data->nr_channels; ++i) {
  1206. tdc = &tdma->channels[i];
  1207. tasklet_kill(&tdc->tasklet);
  1208. }
  1209. pm_runtime_disable(&pdev->dev);
  1210. if (!pm_runtime_status_suspended(&pdev->dev))
  1211. tegra_dma_runtime_suspend(&pdev->dev);
  1212. return 0;
  1213. }
  1214. static int tegra_dma_runtime_suspend(struct device *dev)
  1215. {
  1216. struct platform_device *pdev = to_platform_device(dev);
  1217. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1218. clk_disable_unprepare(tdma->dma_clk);
  1219. return 0;
  1220. }
  1221. static int tegra_dma_runtime_resume(struct device *dev)
  1222. {
  1223. struct platform_device *pdev = to_platform_device(dev);
  1224. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1225. int ret;
  1226. ret = clk_prepare_enable(tdma->dma_clk);
  1227. if (ret < 0) {
  1228. dev_err(dev, "clk_enable failed: %d\n", ret);
  1229. return ret;
  1230. }
  1231. return 0;
  1232. }
  1233. static const struct dev_pm_ops tegra_dma_dev_pm_ops __devinitconst = {
  1234. #ifdef CONFIG_PM_RUNTIME
  1235. .runtime_suspend = tegra_dma_runtime_suspend,
  1236. .runtime_resume = tegra_dma_runtime_resume,
  1237. #endif
  1238. };
  1239. static struct platform_driver tegra_dmac_driver = {
  1240. .driver = {
  1241. .name = "tegra-apbdma",
  1242. .owner = THIS_MODULE,
  1243. .pm = &tegra_dma_dev_pm_ops,
  1244. .of_match_table = of_match_ptr(tegra_dma_of_match),
  1245. },
  1246. .probe = tegra_dma_probe,
  1247. .remove = tegra_dma_remove,
  1248. };
  1249. module_platform_driver(tegra_dmac_driver);
  1250. MODULE_ALIAS("platform:tegra20-apbdma");
  1251. MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
  1252. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1253. MODULE_LICENSE("GPL v2");