dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * arch/arm/mach-tegra/dma.c
  3. *
  4. * System DMA driver for NVIDIA Tegra SoCs
  5. *
  6. * Copyright (c) 2008-2009, NVIDIA Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/err.h>
  27. #include <linux/irq.h>
  28. #include <linux/delay.h>
  29. #include <linux/clk.h>
  30. #include <mach/dma.h>
  31. #include <mach/irqs.h>
  32. #include <mach/iomap.h>
  33. #include <mach/suspend.h>
  34. #define APB_DMA_GEN 0x000
  35. #define GEN_ENABLE (1<<31)
  36. #define APB_DMA_CNTRL 0x010
  37. #define APB_DMA_IRQ_MASK 0x01c
  38. #define APB_DMA_IRQ_MASK_SET 0x020
  39. #define APB_DMA_CHAN_CSR 0x000
  40. #define CSR_ENB (1<<31)
  41. #define CSR_IE_EOC (1<<30)
  42. #define CSR_HOLD (1<<29)
  43. #define CSR_DIR (1<<28)
  44. #define CSR_ONCE (1<<27)
  45. #define CSR_FLOW (1<<21)
  46. #define CSR_REQ_SEL_SHIFT 16
  47. #define CSR_REQ_SEL_MASK (0x1F<<CSR_REQ_SEL_SHIFT)
  48. #define CSR_REQ_SEL_INVALID (31<<CSR_REQ_SEL_SHIFT)
  49. #define CSR_WCOUNT_SHIFT 2
  50. #define CSR_WCOUNT_MASK 0xFFFC
  51. #define APB_DMA_CHAN_STA 0x004
  52. #define STA_BUSY (1<<31)
  53. #define STA_ISE_EOC (1<<30)
  54. #define STA_HALT (1<<29)
  55. #define STA_PING_PONG (1<<28)
  56. #define STA_COUNT_SHIFT 2
  57. #define STA_COUNT_MASK 0xFFFC
  58. #define APB_DMA_CHAN_AHB_PTR 0x010
  59. #define APB_DMA_CHAN_AHB_SEQ 0x014
  60. #define AHB_SEQ_INTR_ENB (1<<31)
  61. #define AHB_SEQ_BUS_WIDTH_SHIFT 28
  62. #define AHB_SEQ_BUS_WIDTH_MASK (0x7<<AHB_SEQ_BUS_WIDTH_SHIFT)
  63. #define AHB_SEQ_BUS_WIDTH_8 (0<<AHB_SEQ_BUS_WIDTH_SHIFT)
  64. #define AHB_SEQ_BUS_WIDTH_16 (1<<AHB_SEQ_BUS_WIDTH_SHIFT)
  65. #define AHB_SEQ_BUS_WIDTH_32 (2<<AHB_SEQ_BUS_WIDTH_SHIFT)
  66. #define AHB_SEQ_BUS_WIDTH_64 (3<<AHB_SEQ_BUS_WIDTH_SHIFT)
  67. #define AHB_SEQ_BUS_WIDTH_128 (4<<AHB_SEQ_BUS_WIDTH_SHIFT)
  68. #define AHB_SEQ_DATA_SWAP (1<<27)
  69. #define AHB_SEQ_BURST_MASK (0x7<<24)
  70. #define AHB_SEQ_BURST_1 (4<<24)
  71. #define AHB_SEQ_BURST_4 (5<<24)
  72. #define AHB_SEQ_BURST_8 (6<<24)
  73. #define AHB_SEQ_DBL_BUF (1<<19)
  74. #define AHB_SEQ_WRAP_SHIFT 16
  75. #define AHB_SEQ_WRAP_MASK (0x7<<AHB_SEQ_WRAP_SHIFT)
  76. #define APB_DMA_CHAN_APB_PTR 0x018
  77. #define APB_DMA_CHAN_APB_SEQ 0x01c
  78. #define APB_SEQ_BUS_WIDTH_SHIFT 28
  79. #define APB_SEQ_BUS_WIDTH_MASK (0x7<<APB_SEQ_BUS_WIDTH_SHIFT)
  80. #define APB_SEQ_BUS_WIDTH_8 (0<<APB_SEQ_BUS_WIDTH_SHIFT)
  81. #define APB_SEQ_BUS_WIDTH_16 (1<<APB_SEQ_BUS_WIDTH_SHIFT)
  82. #define APB_SEQ_BUS_WIDTH_32 (2<<APB_SEQ_BUS_WIDTH_SHIFT)
  83. #define APB_SEQ_BUS_WIDTH_64 (3<<APB_SEQ_BUS_WIDTH_SHIFT)
  84. #define APB_SEQ_BUS_WIDTH_128 (4<<APB_SEQ_BUS_WIDTH_SHIFT)
  85. #define APB_SEQ_DATA_SWAP (1<<27)
  86. #define APB_SEQ_WRAP_SHIFT 16
  87. #define APB_SEQ_WRAP_MASK (0x7<<APB_SEQ_WRAP_SHIFT)
  88. #define TEGRA_SYSTEM_DMA_CH_NR 16
  89. #define TEGRA_SYSTEM_DMA_AVP_CH_NUM 4
  90. #define TEGRA_SYSTEM_DMA_CH_MIN 0
  91. #define TEGRA_SYSTEM_DMA_CH_MAX \
  92. (TEGRA_SYSTEM_DMA_CH_NR - TEGRA_SYSTEM_DMA_AVP_CH_NUM - 1)
  93. #define NV_DMA_MAX_TRASFER_SIZE 0x10000
  94. static const unsigned int ahb_addr_wrap_table[8] = {
  95. 0, 32, 64, 128, 256, 512, 1024, 2048
  96. };
  97. static const unsigned int apb_addr_wrap_table[8] = {
  98. 0, 1, 2, 4, 8, 16, 32, 64
  99. };
  100. static const unsigned int bus_width_table[5] = {
  101. 8, 16, 32, 64, 128
  102. };
  103. #define TEGRA_DMA_NAME_SIZE 16
  104. struct tegra_dma_channel {
  105. struct list_head list;
  106. int id;
  107. spinlock_t lock;
  108. char name[TEGRA_DMA_NAME_SIZE];
  109. void __iomem *addr;
  110. int mode;
  111. int irq;
  112. int req_transfer_count;
  113. };
  114. #define NV_DMA_MAX_CHANNELS 32
  115. static bool tegra_dma_initialized;
  116. static DEFINE_MUTEX(tegra_dma_lock);
  117. static DECLARE_BITMAP(channel_usage, NV_DMA_MAX_CHANNELS);
  118. static struct tegra_dma_channel dma_channels[NV_DMA_MAX_CHANNELS];
  119. static void tegra_dma_update_hw(struct tegra_dma_channel *ch,
  120. struct tegra_dma_req *req);
  121. static void tegra_dma_update_hw_partial(struct tegra_dma_channel *ch,
  122. struct tegra_dma_req *req);
  123. static void tegra_dma_stop(struct tegra_dma_channel *ch);
  124. void tegra_dma_flush(struct tegra_dma_channel *ch)
  125. {
  126. }
  127. EXPORT_SYMBOL(tegra_dma_flush);
  128. void tegra_dma_dequeue(struct tegra_dma_channel *ch)
  129. {
  130. struct tegra_dma_req *req;
  131. if (tegra_dma_is_empty(ch))
  132. return;
  133. req = list_entry(ch->list.next, typeof(*req), node);
  134. tegra_dma_dequeue_req(ch, req);
  135. return;
  136. }
  137. static void tegra_dma_stop(struct tegra_dma_channel *ch)
  138. {
  139. u32 csr;
  140. u32 status;
  141. csr = readl(ch->addr + APB_DMA_CHAN_CSR);
  142. csr &= ~CSR_IE_EOC;
  143. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  144. csr &= ~CSR_ENB;
  145. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  146. status = readl(ch->addr + APB_DMA_CHAN_STA);
  147. if (status & STA_ISE_EOC)
  148. writel(status, ch->addr + APB_DMA_CHAN_STA);
  149. }
  150. static int tegra_dma_cancel(struct tegra_dma_channel *ch)
  151. {
  152. u32 csr;
  153. unsigned long irq_flags;
  154. spin_lock_irqsave(&ch->lock, irq_flags);
  155. while (!list_empty(&ch->list))
  156. list_del(ch->list.next);
  157. csr = readl(ch->addr + APB_DMA_CHAN_CSR);
  158. csr &= ~CSR_REQ_SEL_MASK;
  159. csr |= CSR_REQ_SEL_INVALID;
  160. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  161. tegra_dma_stop(ch);
  162. spin_unlock_irqrestore(&ch->lock, irq_flags);
  163. return 0;
  164. }
  165. int tegra_dma_dequeue_req(struct tegra_dma_channel *ch,
  166. struct tegra_dma_req *_req)
  167. {
  168. unsigned int csr;
  169. unsigned int status;
  170. struct tegra_dma_req *req = NULL;
  171. int found = 0;
  172. unsigned long irq_flags;
  173. int to_transfer;
  174. int req_transfer_count;
  175. spin_lock_irqsave(&ch->lock, irq_flags);
  176. list_for_each_entry(req, &ch->list, node) {
  177. if (req == _req) {
  178. list_del(&req->node);
  179. found = 1;
  180. break;
  181. }
  182. }
  183. if (!found) {
  184. spin_unlock_irqrestore(&ch->lock, irq_flags);
  185. return 0;
  186. }
  187. /* STOP the DMA and get the transfer count.
  188. * Getting the transfer count is tricky.
  189. * - Change the source selector to invalid to stop the DMA from
  190. * FIFO to memory.
  191. * - Read the status register to know the number of pending
  192. * bytes to be transferred.
  193. * - Finally stop or program the DMA to the next buffer in the
  194. * list.
  195. */
  196. csr = readl(ch->addr + APB_DMA_CHAN_CSR);
  197. csr &= ~CSR_REQ_SEL_MASK;
  198. csr |= CSR_REQ_SEL_INVALID;
  199. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  200. /* Get the transfer count */
  201. status = readl(ch->addr + APB_DMA_CHAN_STA);
  202. to_transfer = (status & STA_COUNT_MASK) >> STA_COUNT_SHIFT;
  203. req_transfer_count = ch->req_transfer_count;
  204. req_transfer_count += 1;
  205. to_transfer += 1;
  206. req->bytes_transferred = req_transfer_count;
  207. if (status & STA_BUSY)
  208. req->bytes_transferred -= to_transfer;
  209. /* In continuous transfer mode, DMA only tracks the count of the
  210. * half DMA buffer. So, if the DMA already finished half the DMA
  211. * then add the half buffer to the completed count.
  212. *
  213. * FIXME: There can be a race here. What if the req to
  214. * dequue happens at the same time as the DMA just moved to
  215. * the new buffer and SW didn't yet received the interrupt?
  216. */
  217. if (ch->mode & TEGRA_DMA_MODE_CONTINOUS)
  218. if (req->buffer_status == TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL)
  219. req->bytes_transferred += req_transfer_count;
  220. req->bytes_transferred *= 4;
  221. tegra_dma_stop(ch);
  222. if (!list_empty(&ch->list)) {
  223. /* if the list is not empty, queue the next request */
  224. struct tegra_dma_req *next_req;
  225. next_req = list_entry(ch->list.next,
  226. typeof(*next_req), node);
  227. tegra_dma_update_hw(ch, next_req);
  228. }
  229. req->status = -TEGRA_DMA_REQ_ERROR_ABORTED;
  230. spin_unlock_irqrestore(&ch->lock, irq_flags);
  231. /* Callback should be called without any lock */
  232. req->complete(req);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(tegra_dma_dequeue_req);
  236. bool tegra_dma_is_empty(struct tegra_dma_channel *ch)
  237. {
  238. unsigned long irq_flags;
  239. bool is_empty;
  240. spin_lock_irqsave(&ch->lock, irq_flags);
  241. if (list_empty(&ch->list))
  242. is_empty = true;
  243. else
  244. is_empty = false;
  245. spin_unlock_irqrestore(&ch->lock, irq_flags);
  246. return is_empty;
  247. }
  248. EXPORT_SYMBOL(tegra_dma_is_empty);
  249. bool tegra_dma_is_req_inflight(struct tegra_dma_channel *ch,
  250. struct tegra_dma_req *_req)
  251. {
  252. unsigned long irq_flags;
  253. struct tegra_dma_req *req;
  254. spin_lock_irqsave(&ch->lock, irq_flags);
  255. list_for_each_entry(req, &ch->list, node) {
  256. if (req == _req) {
  257. spin_unlock_irqrestore(&ch->lock, irq_flags);
  258. return true;
  259. }
  260. }
  261. spin_unlock_irqrestore(&ch->lock, irq_flags);
  262. return false;
  263. }
  264. EXPORT_SYMBOL(tegra_dma_is_req_inflight);
  265. int tegra_dma_enqueue_req(struct tegra_dma_channel *ch,
  266. struct tegra_dma_req *req)
  267. {
  268. unsigned long irq_flags;
  269. struct tegra_dma_req *_req;
  270. int start_dma = 0;
  271. if (req->size > NV_DMA_MAX_TRASFER_SIZE ||
  272. req->source_addr & 0x3 || req->dest_addr & 0x3) {
  273. pr_err("Invalid DMA request for channel %d\n", ch->id);
  274. return -EINVAL;
  275. }
  276. spin_lock_irqsave(&ch->lock, irq_flags);
  277. list_for_each_entry(_req, &ch->list, node) {
  278. if (req == _req) {
  279. spin_unlock_irqrestore(&ch->lock, irq_flags);
  280. return -EEXIST;
  281. }
  282. }
  283. req->bytes_transferred = 0;
  284. req->status = 0;
  285. req->buffer_status = 0;
  286. if (list_empty(&ch->list))
  287. start_dma = 1;
  288. list_add_tail(&req->node, &ch->list);
  289. if (start_dma)
  290. tegra_dma_update_hw(ch, req);
  291. spin_unlock_irqrestore(&ch->lock, irq_flags);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(tegra_dma_enqueue_req);
  295. struct tegra_dma_channel *tegra_dma_allocate_channel(int mode)
  296. {
  297. int channel;
  298. struct tegra_dma_channel *ch = NULL;
  299. if (WARN_ON(!tegra_dma_initialized))
  300. return NULL;
  301. mutex_lock(&tegra_dma_lock);
  302. /* first channel is the shared channel */
  303. if (mode & TEGRA_DMA_SHARED) {
  304. channel = TEGRA_SYSTEM_DMA_CH_MIN;
  305. } else {
  306. channel = find_first_zero_bit(channel_usage,
  307. ARRAY_SIZE(dma_channels));
  308. if (channel >= ARRAY_SIZE(dma_channels))
  309. goto out;
  310. }
  311. __set_bit(channel, channel_usage);
  312. ch = &dma_channels[channel];
  313. ch->mode = mode;
  314. out:
  315. mutex_unlock(&tegra_dma_lock);
  316. return ch;
  317. }
  318. EXPORT_SYMBOL(tegra_dma_allocate_channel);
  319. void tegra_dma_free_channel(struct tegra_dma_channel *ch)
  320. {
  321. if (ch->mode & TEGRA_DMA_SHARED)
  322. return;
  323. tegra_dma_cancel(ch);
  324. mutex_lock(&tegra_dma_lock);
  325. __clear_bit(ch->id, channel_usage);
  326. mutex_unlock(&tegra_dma_lock);
  327. }
  328. EXPORT_SYMBOL(tegra_dma_free_channel);
  329. static void tegra_dma_update_hw_partial(struct tegra_dma_channel *ch,
  330. struct tegra_dma_req *req)
  331. {
  332. u32 apb_ptr;
  333. u32 ahb_ptr;
  334. if (req->to_memory) {
  335. apb_ptr = req->source_addr;
  336. ahb_ptr = req->dest_addr;
  337. } else {
  338. apb_ptr = req->dest_addr;
  339. ahb_ptr = req->source_addr;
  340. }
  341. writel(apb_ptr, ch->addr + APB_DMA_CHAN_APB_PTR);
  342. writel(ahb_ptr, ch->addr + APB_DMA_CHAN_AHB_PTR);
  343. req->status = TEGRA_DMA_REQ_INFLIGHT;
  344. return;
  345. }
  346. static void tegra_dma_update_hw(struct tegra_dma_channel *ch,
  347. struct tegra_dma_req *req)
  348. {
  349. int ahb_addr_wrap;
  350. int apb_addr_wrap;
  351. int ahb_bus_width;
  352. int apb_bus_width;
  353. int index;
  354. u32 ahb_seq;
  355. u32 apb_seq;
  356. u32 ahb_ptr;
  357. u32 apb_ptr;
  358. u32 csr;
  359. csr = CSR_IE_EOC | CSR_FLOW;
  360. ahb_seq = AHB_SEQ_INTR_ENB | AHB_SEQ_BURST_1;
  361. apb_seq = 0;
  362. csr |= req->req_sel << CSR_REQ_SEL_SHIFT;
  363. /* One shot mode is always single buffered,
  364. * continuous mode is always double buffered
  365. * */
  366. if (ch->mode & TEGRA_DMA_MODE_ONESHOT) {
  367. csr |= CSR_ONCE;
  368. ch->req_transfer_count = (req->size >> 2) - 1;
  369. } else {
  370. ahb_seq |= AHB_SEQ_DBL_BUF;
  371. /* In double buffered mode, we set the size to half the
  372. * requested size and interrupt when half the buffer
  373. * is full */
  374. ch->req_transfer_count = (req->size >> 3) - 1;
  375. }
  376. csr |= ch->req_transfer_count << CSR_WCOUNT_SHIFT;
  377. if (req->to_memory) {
  378. apb_ptr = req->source_addr;
  379. ahb_ptr = req->dest_addr;
  380. apb_addr_wrap = req->source_wrap;
  381. ahb_addr_wrap = req->dest_wrap;
  382. apb_bus_width = req->source_bus_width;
  383. ahb_bus_width = req->dest_bus_width;
  384. } else {
  385. csr |= CSR_DIR;
  386. apb_ptr = req->dest_addr;
  387. ahb_ptr = req->source_addr;
  388. apb_addr_wrap = req->dest_wrap;
  389. ahb_addr_wrap = req->source_wrap;
  390. apb_bus_width = req->dest_bus_width;
  391. ahb_bus_width = req->source_bus_width;
  392. }
  393. apb_addr_wrap >>= 2;
  394. ahb_addr_wrap >>= 2;
  395. /* set address wrap for APB size */
  396. index = 0;
  397. do {
  398. if (apb_addr_wrap_table[index] == apb_addr_wrap)
  399. break;
  400. index++;
  401. } while (index < ARRAY_SIZE(apb_addr_wrap_table));
  402. BUG_ON(index == ARRAY_SIZE(apb_addr_wrap_table));
  403. apb_seq |= index << APB_SEQ_WRAP_SHIFT;
  404. /* set address wrap for AHB size */
  405. index = 0;
  406. do {
  407. if (ahb_addr_wrap_table[index] == ahb_addr_wrap)
  408. break;
  409. index++;
  410. } while (index < ARRAY_SIZE(ahb_addr_wrap_table));
  411. BUG_ON(index == ARRAY_SIZE(ahb_addr_wrap_table));
  412. ahb_seq |= index << AHB_SEQ_WRAP_SHIFT;
  413. for (index = 0; index < ARRAY_SIZE(bus_width_table); index++) {
  414. if (bus_width_table[index] == ahb_bus_width)
  415. break;
  416. }
  417. BUG_ON(index == ARRAY_SIZE(bus_width_table));
  418. ahb_seq |= index << AHB_SEQ_BUS_WIDTH_SHIFT;
  419. for (index = 0; index < ARRAY_SIZE(bus_width_table); index++) {
  420. if (bus_width_table[index] == apb_bus_width)
  421. break;
  422. }
  423. BUG_ON(index == ARRAY_SIZE(bus_width_table));
  424. apb_seq |= index << APB_SEQ_BUS_WIDTH_SHIFT;
  425. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  426. writel(apb_seq, ch->addr + APB_DMA_CHAN_APB_SEQ);
  427. writel(apb_ptr, ch->addr + APB_DMA_CHAN_APB_PTR);
  428. writel(ahb_seq, ch->addr + APB_DMA_CHAN_AHB_SEQ);
  429. writel(ahb_ptr, ch->addr + APB_DMA_CHAN_AHB_PTR);
  430. csr |= CSR_ENB;
  431. writel(csr, ch->addr + APB_DMA_CHAN_CSR);
  432. req->status = TEGRA_DMA_REQ_INFLIGHT;
  433. }
  434. static void handle_oneshot_dma(struct tegra_dma_channel *ch)
  435. {
  436. struct tegra_dma_req *req;
  437. unsigned long irq_flags;
  438. spin_lock_irqsave(&ch->lock, irq_flags);
  439. if (list_empty(&ch->list)) {
  440. spin_unlock_irqrestore(&ch->lock, irq_flags);
  441. return;
  442. }
  443. req = list_entry(ch->list.next, typeof(*req), node);
  444. if (req) {
  445. int bytes_transferred;
  446. bytes_transferred = ch->req_transfer_count;
  447. bytes_transferred += 1;
  448. bytes_transferred <<= 2;
  449. list_del(&req->node);
  450. req->bytes_transferred = bytes_transferred;
  451. req->status = TEGRA_DMA_REQ_SUCCESS;
  452. spin_unlock_irqrestore(&ch->lock, irq_flags);
  453. /* Callback should be called without any lock */
  454. pr_debug("%s: transferred %d bytes\n", __func__,
  455. req->bytes_transferred);
  456. req->complete(req);
  457. spin_lock_irqsave(&ch->lock, irq_flags);
  458. }
  459. if (!list_empty(&ch->list)) {
  460. req = list_entry(ch->list.next, typeof(*req), node);
  461. /* the complete function we just called may have enqueued
  462. another req, in which case dma has already started */
  463. if (req->status != TEGRA_DMA_REQ_INFLIGHT)
  464. tegra_dma_update_hw(ch, req);
  465. }
  466. spin_unlock_irqrestore(&ch->lock, irq_flags);
  467. }
  468. static void handle_continuous_dma(struct tegra_dma_channel *ch)
  469. {
  470. struct tegra_dma_req *req;
  471. unsigned long irq_flags;
  472. spin_lock_irqsave(&ch->lock, irq_flags);
  473. if (list_empty(&ch->list)) {
  474. spin_unlock_irqrestore(&ch->lock, irq_flags);
  475. return;
  476. }
  477. req = list_entry(ch->list.next, typeof(*req), node);
  478. if (req) {
  479. if (req->buffer_status == TEGRA_DMA_REQ_BUF_STATUS_EMPTY) {
  480. bool is_dma_ping_complete;
  481. is_dma_ping_complete = (readl(ch->addr + APB_DMA_CHAN_STA)
  482. & STA_PING_PONG) ? true : false;
  483. if (req->to_memory)
  484. is_dma_ping_complete = !is_dma_ping_complete;
  485. /* Out of sync - Release current buffer */
  486. if (!is_dma_ping_complete) {
  487. int bytes_transferred;
  488. bytes_transferred = ch->req_transfer_count;
  489. bytes_transferred += 1;
  490. bytes_transferred <<= 3;
  491. req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_FULL;
  492. req->bytes_transferred = bytes_transferred;
  493. req->status = TEGRA_DMA_REQ_SUCCESS;
  494. tegra_dma_stop(ch);
  495. if (!list_is_last(&req->node, &ch->list)) {
  496. struct tegra_dma_req *next_req;
  497. next_req = list_entry(req->node.next,
  498. typeof(*next_req), node);
  499. tegra_dma_update_hw(ch, next_req);
  500. }
  501. list_del(&req->node);
  502. /* DMA lock is NOT held when callbak is called */
  503. spin_unlock_irqrestore(&ch->lock, irq_flags);
  504. req->complete(req);
  505. return;
  506. }
  507. /* Load the next request into the hardware, if available
  508. * */
  509. if (!list_is_last(&req->node, &ch->list)) {
  510. struct tegra_dma_req *next_req;
  511. next_req = list_entry(req->node.next,
  512. typeof(*next_req), node);
  513. tegra_dma_update_hw_partial(ch, next_req);
  514. }
  515. req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL;
  516. req->status = TEGRA_DMA_REQ_SUCCESS;
  517. /* DMA lock is NOT held when callback is called */
  518. spin_unlock_irqrestore(&ch->lock, irq_flags);
  519. if (likely(req->threshold))
  520. req->threshold(req);
  521. return;
  522. } else if (req->buffer_status ==
  523. TEGRA_DMA_REQ_BUF_STATUS_HALF_FULL) {
  524. /* Callback when the buffer is completely full (i.e on
  525. * the second interrupt */
  526. int bytes_transferred;
  527. bytes_transferred = ch->req_transfer_count;
  528. bytes_transferred += 1;
  529. bytes_transferred <<= 3;
  530. req->buffer_status = TEGRA_DMA_REQ_BUF_STATUS_FULL;
  531. req->bytes_transferred = bytes_transferred;
  532. req->status = TEGRA_DMA_REQ_SUCCESS;
  533. list_del(&req->node);
  534. /* DMA lock is NOT held when callbak is called */
  535. spin_unlock_irqrestore(&ch->lock, irq_flags);
  536. req->complete(req);
  537. return;
  538. } else {
  539. BUG();
  540. }
  541. }
  542. spin_unlock_irqrestore(&ch->lock, irq_flags);
  543. }
  544. static irqreturn_t dma_isr(int irq, void *data)
  545. {
  546. struct tegra_dma_channel *ch = data;
  547. unsigned long status;
  548. status = readl(ch->addr + APB_DMA_CHAN_STA);
  549. if (status & STA_ISE_EOC)
  550. writel(status, ch->addr + APB_DMA_CHAN_STA);
  551. else {
  552. pr_warning("Got a spurious ISR for DMA channel %d\n", ch->id);
  553. return IRQ_HANDLED;
  554. }
  555. return IRQ_WAKE_THREAD;
  556. }
  557. static irqreturn_t dma_thread_fn(int irq, void *data)
  558. {
  559. struct tegra_dma_channel *ch = data;
  560. if (ch->mode & TEGRA_DMA_MODE_ONESHOT)
  561. handle_oneshot_dma(ch);
  562. else
  563. handle_continuous_dma(ch);
  564. return IRQ_HANDLED;
  565. }
  566. int __init tegra_dma_init(void)
  567. {
  568. int ret = 0;
  569. int i;
  570. unsigned int irq;
  571. void __iomem *addr;
  572. struct clk *c;
  573. bitmap_fill(channel_usage, NV_DMA_MAX_CHANNELS);
  574. c = clk_get_sys("tegra-dma", NULL);
  575. if (IS_ERR(c)) {
  576. pr_err("Unable to get clock for APB DMA\n");
  577. ret = PTR_ERR(c);
  578. goto fail;
  579. }
  580. ret = clk_enable(c);
  581. if (ret != 0) {
  582. pr_err("Unable to enable clock for APB DMA\n");
  583. goto fail;
  584. }
  585. addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
  586. writel(GEN_ENABLE, addr + APB_DMA_GEN);
  587. writel(0, addr + APB_DMA_CNTRL);
  588. writel(0xFFFFFFFFul >> (31 - TEGRA_SYSTEM_DMA_CH_MAX),
  589. addr + APB_DMA_IRQ_MASK_SET);
  590. for (i = TEGRA_SYSTEM_DMA_CH_MIN; i <= TEGRA_SYSTEM_DMA_CH_MAX; i++) {
  591. struct tegra_dma_channel *ch = &dma_channels[i];
  592. ch->id = i;
  593. snprintf(ch->name, TEGRA_DMA_NAME_SIZE, "dma_channel_%d", i);
  594. ch->addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
  595. TEGRA_APB_DMA_CH0_SIZE * i);
  596. spin_lock_init(&ch->lock);
  597. INIT_LIST_HEAD(&ch->list);
  598. irq = INT_APB_DMA_CH0 + i;
  599. ret = request_threaded_irq(irq, dma_isr, dma_thread_fn, 0,
  600. dma_channels[i].name, ch);
  601. if (ret) {
  602. pr_err("Failed to register IRQ %d for DMA %d\n",
  603. irq, i);
  604. goto fail;
  605. }
  606. ch->irq = irq;
  607. __clear_bit(i, channel_usage);
  608. }
  609. /* mark the shared channel allocated */
  610. __set_bit(TEGRA_SYSTEM_DMA_CH_MIN, channel_usage);
  611. tegra_dma_initialized = true;
  612. return 0;
  613. fail:
  614. writel(0, addr + APB_DMA_GEN);
  615. for (i = TEGRA_SYSTEM_DMA_CH_MIN; i <= TEGRA_SYSTEM_DMA_CH_MAX; i++) {
  616. struct tegra_dma_channel *ch = &dma_channels[i];
  617. if (ch->irq)
  618. free_irq(ch->irq, ch);
  619. }
  620. return ret;
  621. }
  622. postcore_initcall(tegra_dma_init);
  623. #ifdef CONFIG_PM
  624. static u32 apb_dma[5*TEGRA_SYSTEM_DMA_CH_NR + 3];
  625. void tegra_dma_suspend(void)
  626. {
  627. void __iomem *addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
  628. u32 *ctx = apb_dma;
  629. int i;
  630. *ctx++ = readl(addr + APB_DMA_GEN);
  631. *ctx++ = readl(addr + APB_DMA_CNTRL);
  632. *ctx++ = readl(addr + APB_DMA_IRQ_MASK);
  633. for (i = 0; i < TEGRA_SYSTEM_DMA_CH_NR; i++) {
  634. addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
  635. TEGRA_APB_DMA_CH0_SIZE * i);
  636. *ctx++ = readl(addr + APB_DMA_CHAN_CSR);
  637. *ctx++ = readl(addr + APB_DMA_CHAN_AHB_PTR);
  638. *ctx++ = readl(addr + APB_DMA_CHAN_AHB_SEQ);
  639. *ctx++ = readl(addr + APB_DMA_CHAN_APB_PTR);
  640. *ctx++ = readl(addr + APB_DMA_CHAN_APB_SEQ);
  641. }
  642. }
  643. void tegra_dma_resume(void)
  644. {
  645. void __iomem *addr = IO_ADDRESS(TEGRA_APB_DMA_BASE);
  646. u32 *ctx = apb_dma;
  647. int i;
  648. writel(*ctx++, addr + APB_DMA_GEN);
  649. writel(*ctx++, addr + APB_DMA_CNTRL);
  650. writel(*ctx++, addr + APB_DMA_IRQ_MASK);
  651. for (i = 0; i < TEGRA_SYSTEM_DMA_CH_NR; i++) {
  652. addr = IO_ADDRESS(TEGRA_APB_DMA_CH0_BASE +
  653. TEGRA_APB_DMA_CH0_SIZE * i);
  654. writel(*ctx++, addr + APB_DMA_CHAN_CSR);
  655. writel(*ctx++, addr + APB_DMA_CHAN_AHB_PTR);
  656. writel(*ctx++, addr + APB_DMA_CHAN_AHB_SEQ);
  657. writel(*ctx++, addr + APB_DMA_CHAN_APB_PTR);
  658. writel(*ctx++, addr + APB_DMA_CHAN_APB_SEQ);
  659. }
  660. }
  661. #endif