dma.c 20 KB

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