dma.c 20 KB

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