tegra210-adma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * ADMA driver for Nvidia's Tegra210 ADMA controller.
  3. *
  4. * Copyright (c) 2016, 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/clk.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/slab.h>
  26. #include "virt-dma.h"
  27. #define ADMA_CH_CMD 0x00
  28. #define ADMA_CH_STATUS 0x0c
  29. #define ADMA_CH_STATUS_XFER_EN BIT(0)
  30. #define ADMA_CH_INT_STATUS 0x10
  31. #define ADMA_CH_INT_STATUS_XFER_DONE BIT(0)
  32. #define ADMA_CH_INT_CLEAR 0x1c
  33. #define ADMA_CH_CTRL 0x24
  34. #define ADMA_CH_CTRL_TX_REQ(val) (((val) & 0xf) << 28)
  35. #define ADMA_CH_CTRL_TX_REQ_MAX 10
  36. #define ADMA_CH_CTRL_RX_REQ(val) (((val) & 0xf) << 24)
  37. #define ADMA_CH_CTRL_RX_REQ_MAX 10
  38. #define ADMA_CH_CTRL_DIR(val) (((val) & 0xf) << 12)
  39. #define ADMA_CH_CTRL_DIR_AHUB2MEM 2
  40. #define ADMA_CH_CTRL_DIR_MEM2AHUB 4
  41. #define ADMA_CH_CTRL_MODE_CONTINUOUS (2 << 8)
  42. #define ADMA_CH_CTRL_FLOWCTRL_EN BIT(1)
  43. #define ADMA_CH_CONFIG 0x28
  44. #define ADMA_CH_CONFIG_SRC_BUF(val) (((val) & 0x7) << 28)
  45. #define ADMA_CH_CONFIG_TRG_BUF(val) (((val) & 0x7) << 24)
  46. #define ADMA_CH_CONFIG_BURST_SIZE(val) (((val) & 0x7) << 20)
  47. #define ADMA_CH_CONFIG_BURST_16 5
  48. #define ADMA_CH_CONFIG_WEIGHT_FOR_WRR(val) ((val) & 0xf)
  49. #define ADMA_CH_CONFIG_MAX_BUFS 8
  50. #define ADMA_CH_FIFO_CTRL 0x2c
  51. #define ADMA_CH_FIFO_CTRL_OVRFW_THRES(val) (((val) & 0xf) << 24)
  52. #define ADMA_CH_FIFO_CTRL_STARV_THRES(val) (((val) & 0xf) << 16)
  53. #define ADMA_CH_FIFO_CTRL_TX_SIZE(val) (((val) & 0xf) << 8)
  54. #define ADMA_CH_FIFO_CTRL_RX_SIZE(val) ((val) & 0xf)
  55. #define ADMA_CH_LOWER_SRC_ADDR 0x34
  56. #define ADMA_CH_LOWER_TRG_ADDR 0x3c
  57. #define ADMA_CH_TC 0x44
  58. #define ADMA_CH_TC_COUNT_MASK 0x3ffffffc
  59. #define ADMA_CH_XFER_STATUS 0x54
  60. #define ADMA_CH_XFER_STATUS_COUNT_MASK 0xffff
  61. #define ADMA_GLOBAL_CMD 0xc00
  62. #define ADMA_GLOBAL_SOFT_RESET 0xc04
  63. #define ADMA_GLOBAL_INT_CLEAR 0xc20
  64. #define ADMA_GLOBAL_CTRL 0xc24
  65. #define ADMA_CH_REG_OFFSET(a) (a * 0x80)
  66. #define ADMA_CH_FIFO_CTRL_DEFAULT (ADMA_CH_FIFO_CTRL_OVRFW_THRES(1) | \
  67. ADMA_CH_FIFO_CTRL_STARV_THRES(1) | \
  68. ADMA_CH_FIFO_CTRL_TX_SIZE(3) | \
  69. ADMA_CH_FIFO_CTRL_RX_SIZE(3))
  70. struct tegra_adma;
  71. /*
  72. * struct tegra_adma_chip_data - Tegra chip specific data
  73. * @nr_channels: Number of DMA channels available.
  74. */
  75. struct tegra_adma_chip_data {
  76. int nr_channels;
  77. };
  78. /*
  79. * struct tegra_adma_chan_regs - Tegra ADMA channel registers
  80. */
  81. struct tegra_adma_chan_regs {
  82. unsigned int ctrl;
  83. unsigned int config;
  84. unsigned int src_addr;
  85. unsigned int trg_addr;
  86. unsigned int fifo_ctrl;
  87. unsigned int cmd;
  88. unsigned int tc;
  89. };
  90. /*
  91. * struct tegra_adma_desc - Tegra ADMA descriptor to manage transfer requests.
  92. */
  93. struct tegra_adma_desc {
  94. struct virt_dma_desc vd;
  95. struct tegra_adma_chan_regs ch_regs;
  96. size_t buf_len;
  97. size_t period_len;
  98. size_t num_periods;
  99. };
  100. /*
  101. * struct tegra_adma_chan - Tegra ADMA channel information
  102. */
  103. struct tegra_adma_chan {
  104. struct virt_dma_chan vc;
  105. struct tegra_adma_desc *desc;
  106. struct tegra_adma *tdma;
  107. int irq;
  108. void __iomem *chan_addr;
  109. /* Slave channel configuration info */
  110. struct dma_slave_config sconfig;
  111. enum dma_transfer_direction sreq_dir;
  112. unsigned int sreq_index;
  113. bool sreq_reserved;
  114. struct tegra_adma_chan_regs ch_regs;
  115. /* Transfer count and position info */
  116. unsigned int tx_buf_count;
  117. unsigned int tx_buf_pos;
  118. };
  119. /*
  120. * struct tegra_adma - Tegra ADMA controller information
  121. */
  122. struct tegra_adma {
  123. struct dma_device dma_dev;
  124. struct device *dev;
  125. void __iomem *base_addr;
  126. struct clk *ahub_clk;
  127. unsigned int nr_channels;
  128. unsigned long rx_requests_reserved;
  129. unsigned long tx_requests_reserved;
  130. /* Used to store global command register state when suspending */
  131. unsigned int global_cmd;
  132. /* Last member of the structure */
  133. struct tegra_adma_chan channels[0];
  134. };
  135. static inline void tdma_write(struct tegra_adma *tdma, u32 reg, u32 val)
  136. {
  137. writel(val, tdma->base_addr + reg);
  138. }
  139. static inline u32 tdma_read(struct tegra_adma *tdma, u32 reg)
  140. {
  141. return readl(tdma->base_addr + reg);
  142. }
  143. static inline void tdma_ch_write(struct tegra_adma_chan *tdc, u32 reg, u32 val)
  144. {
  145. writel(val, tdc->chan_addr + reg);
  146. }
  147. static inline u32 tdma_ch_read(struct tegra_adma_chan *tdc, u32 reg)
  148. {
  149. return readl(tdc->chan_addr + reg);
  150. }
  151. static inline struct tegra_adma_chan *to_tegra_adma_chan(struct dma_chan *dc)
  152. {
  153. return container_of(dc, struct tegra_adma_chan, vc.chan);
  154. }
  155. static inline struct tegra_adma_desc *to_tegra_adma_desc(
  156. struct dma_async_tx_descriptor *td)
  157. {
  158. return container_of(td, struct tegra_adma_desc, vd.tx);
  159. }
  160. static inline struct device *tdc2dev(struct tegra_adma_chan *tdc)
  161. {
  162. return tdc->tdma->dev;
  163. }
  164. static void tegra_adma_desc_free(struct virt_dma_desc *vd)
  165. {
  166. kfree(container_of(vd, struct tegra_adma_desc, vd));
  167. }
  168. static int tegra_adma_slave_config(struct dma_chan *dc,
  169. struct dma_slave_config *sconfig)
  170. {
  171. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  172. memcpy(&tdc->sconfig, sconfig, sizeof(*sconfig));
  173. return 0;
  174. }
  175. static int tegra_adma_init(struct tegra_adma *tdma)
  176. {
  177. u32 status;
  178. int ret;
  179. /* Clear any interrupts */
  180. tdma_write(tdma, ADMA_GLOBAL_INT_CLEAR, 0x1);
  181. /* Assert soft reset */
  182. tdma_write(tdma, ADMA_GLOBAL_SOFT_RESET, 0x1);
  183. /* Wait for reset to clear */
  184. ret = readx_poll_timeout(readl,
  185. tdma->base_addr + ADMA_GLOBAL_SOFT_RESET,
  186. status, status == 0, 20, 10000);
  187. if (ret)
  188. return ret;
  189. /* Enable global ADMA registers */
  190. tdma_write(tdma, ADMA_GLOBAL_CMD, 1);
  191. return 0;
  192. }
  193. static int tegra_adma_request_alloc(struct tegra_adma_chan *tdc,
  194. enum dma_transfer_direction direction)
  195. {
  196. struct tegra_adma *tdma = tdc->tdma;
  197. unsigned int sreq_index = tdc->sreq_index;
  198. if (tdc->sreq_reserved)
  199. return tdc->sreq_dir == direction ? 0 : -EINVAL;
  200. switch (direction) {
  201. case DMA_MEM_TO_DEV:
  202. if (sreq_index > ADMA_CH_CTRL_TX_REQ_MAX) {
  203. dev_err(tdma->dev, "invalid DMA request\n");
  204. return -EINVAL;
  205. }
  206. if (test_and_set_bit(sreq_index, &tdma->tx_requests_reserved)) {
  207. dev_err(tdma->dev, "DMA request reserved\n");
  208. return -EINVAL;
  209. }
  210. break;
  211. case DMA_DEV_TO_MEM:
  212. if (sreq_index > ADMA_CH_CTRL_RX_REQ_MAX) {
  213. dev_err(tdma->dev, "invalid DMA request\n");
  214. return -EINVAL;
  215. }
  216. if (test_and_set_bit(sreq_index, &tdma->rx_requests_reserved)) {
  217. dev_err(tdma->dev, "DMA request reserved\n");
  218. return -EINVAL;
  219. }
  220. break;
  221. default:
  222. dev_WARN(tdma->dev, "channel %s has invalid transfer type\n",
  223. dma_chan_name(&tdc->vc.chan));
  224. return -EINVAL;
  225. }
  226. tdc->sreq_dir = direction;
  227. tdc->sreq_reserved = true;
  228. return 0;
  229. }
  230. static void tegra_adma_request_free(struct tegra_adma_chan *tdc)
  231. {
  232. struct tegra_adma *tdma = tdc->tdma;
  233. if (!tdc->sreq_reserved)
  234. return;
  235. switch (tdc->sreq_dir) {
  236. case DMA_MEM_TO_DEV:
  237. clear_bit(tdc->sreq_index, &tdma->tx_requests_reserved);
  238. break;
  239. case DMA_DEV_TO_MEM:
  240. clear_bit(tdc->sreq_index, &tdma->rx_requests_reserved);
  241. break;
  242. default:
  243. dev_WARN(tdma->dev, "channel %s has invalid transfer type\n",
  244. dma_chan_name(&tdc->vc.chan));
  245. return;
  246. }
  247. tdc->sreq_reserved = false;
  248. }
  249. static u32 tegra_adma_irq_status(struct tegra_adma_chan *tdc)
  250. {
  251. u32 status = tdma_ch_read(tdc, ADMA_CH_INT_STATUS);
  252. return status & ADMA_CH_INT_STATUS_XFER_DONE;
  253. }
  254. static u32 tegra_adma_irq_clear(struct tegra_adma_chan *tdc)
  255. {
  256. u32 status = tegra_adma_irq_status(tdc);
  257. if (status)
  258. tdma_ch_write(tdc, ADMA_CH_INT_CLEAR, status);
  259. return status;
  260. }
  261. static void tegra_adma_stop(struct tegra_adma_chan *tdc)
  262. {
  263. unsigned int status;
  264. /* Disable ADMA */
  265. tdma_ch_write(tdc, ADMA_CH_CMD, 0);
  266. /* Clear interrupt status */
  267. tegra_adma_irq_clear(tdc);
  268. if (readx_poll_timeout_atomic(readl, tdc->chan_addr + ADMA_CH_STATUS,
  269. status, !(status & ADMA_CH_STATUS_XFER_EN),
  270. 20, 10000)) {
  271. dev_err(tdc2dev(tdc), "unable to stop DMA channel\n");
  272. return;
  273. }
  274. kfree(tdc->desc);
  275. tdc->desc = NULL;
  276. }
  277. static void tegra_adma_start(struct tegra_adma_chan *tdc)
  278. {
  279. struct virt_dma_desc *vd = vchan_next_desc(&tdc->vc);
  280. struct tegra_adma_chan_regs *ch_regs;
  281. struct tegra_adma_desc *desc;
  282. if (!vd)
  283. return;
  284. list_del(&vd->node);
  285. desc = to_tegra_adma_desc(&vd->tx);
  286. if (!desc) {
  287. dev_warn(tdc2dev(tdc), "unable to start DMA, no descriptor\n");
  288. return;
  289. }
  290. ch_regs = &desc->ch_regs;
  291. tdc->tx_buf_pos = 0;
  292. tdc->tx_buf_count = 0;
  293. tdma_ch_write(tdc, ADMA_CH_TC, ch_regs->tc);
  294. tdma_ch_write(tdc, ADMA_CH_CTRL, ch_regs->ctrl);
  295. tdma_ch_write(tdc, ADMA_CH_LOWER_SRC_ADDR, ch_regs->src_addr);
  296. tdma_ch_write(tdc, ADMA_CH_LOWER_TRG_ADDR, ch_regs->trg_addr);
  297. tdma_ch_write(tdc, ADMA_CH_FIFO_CTRL, ch_regs->fifo_ctrl);
  298. tdma_ch_write(tdc, ADMA_CH_CONFIG, ch_regs->config);
  299. /* Start ADMA */
  300. tdma_ch_write(tdc, ADMA_CH_CMD, 1);
  301. tdc->desc = desc;
  302. }
  303. static unsigned int tegra_adma_get_residue(struct tegra_adma_chan *tdc)
  304. {
  305. struct tegra_adma_desc *desc = tdc->desc;
  306. unsigned int max = ADMA_CH_XFER_STATUS_COUNT_MASK + 1;
  307. unsigned int pos = tdma_ch_read(tdc, ADMA_CH_XFER_STATUS);
  308. unsigned int periods_remaining;
  309. /*
  310. * Handle wrap around of buffer count register
  311. */
  312. if (pos < tdc->tx_buf_pos)
  313. tdc->tx_buf_count += pos + (max - tdc->tx_buf_pos);
  314. else
  315. tdc->tx_buf_count += pos - tdc->tx_buf_pos;
  316. periods_remaining = tdc->tx_buf_count % desc->num_periods;
  317. tdc->tx_buf_pos = pos;
  318. return desc->buf_len - (periods_remaining * desc->period_len);
  319. }
  320. static irqreturn_t tegra_adma_isr(int irq, void *dev_id)
  321. {
  322. struct tegra_adma_chan *tdc = dev_id;
  323. unsigned long status;
  324. unsigned long flags;
  325. spin_lock_irqsave(&tdc->vc.lock, flags);
  326. status = tegra_adma_irq_clear(tdc);
  327. if (status == 0 || !tdc->desc) {
  328. spin_unlock_irqrestore(&tdc->vc.lock, flags);
  329. return IRQ_NONE;
  330. }
  331. vchan_cyclic_callback(&tdc->desc->vd);
  332. spin_unlock_irqrestore(&tdc->vc.lock, flags);
  333. return IRQ_HANDLED;
  334. }
  335. static void tegra_adma_issue_pending(struct dma_chan *dc)
  336. {
  337. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  338. unsigned long flags;
  339. spin_lock_irqsave(&tdc->vc.lock, flags);
  340. if (vchan_issue_pending(&tdc->vc)) {
  341. if (!tdc->desc)
  342. tegra_adma_start(tdc);
  343. }
  344. spin_unlock_irqrestore(&tdc->vc.lock, flags);
  345. }
  346. static int tegra_adma_terminate_all(struct dma_chan *dc)
  347. {
  348. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  349. unsigned long flags;
  350. LIST_HEAD(head);
  351. spin_lock_irqsave(&tdc->vc.lock, flags);
  352. if (tdc->desc)
  353. tegra_adma_stop(tdc);
  354. tegra_adma_request_free(tdc);
  355. vchan_get_all_descriptors(&tdc->vc, &head);
  356. spin_unlock_irqrestore(&tdc->vc.lock, flags);
  357. vchan_dma_desc_free_list(&tdc->vc, &head);
  358. return 0;
  359. }
  360. static enum dma_status tegra_adma_tx_status(struct dma_chan *dc,
  361. dma_cookie_t cookie,
  362. struct dma_tx_state *txstate)
  363. {
  364. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  365. struct tegra_adma_desc *desc;
  366. struct virt_dma_desc *vd;
  367. enum dma_status ret;
  368. unsigned long flags;
  369. unsigned int residual;
  370. ret = dma_cookie_status(dc, cookie, txstate);
  371. if (ret == DMA_COMPLETE || !txstate)
  372. return ret;
  373. spin_lock_irqsave(&tdc->vc.lock, flags);
  374. vd = vchan_find_desc(&tdc->vc, cookie);
  375. if (vd) {
  376. desc = to_tegra_adma_desc(&vd->tx);
  377. residual = desc->ch_regs.tc;
  378. } else if (tdc->desc && tdc->desc->vd.tx.cookie == cookie) {
  379. residual = tegra_adma_get_residue(tdc);
  380. } else {
  381. residual = 0;
  382. }
  383. spin_unlock_irqrestore(&tdc->vc.lock, flags);
  384. dma_set_residue(txstate, residual);
  385. return ret;
  386. }
  387. static int tegra_adma_set_xfer_params(struct tegra_adma_chan *tdc,
  388. struct tegra_adma_desc *desc,
  389. dma_addr_t buf_addr,
  390. enum dma_transfer_direction direction)
  391. {
  392. struct tegra_adma_chan_regs *ch_regs = &desc->ch_regs;
  393. unsigned int burst_size, adma_dir;
  394. if (desc->num_periods > ADMA_CH_CONFIG_MAX_BUFS)
  395. return -EINVAL;
  396. switch (direction) {
  397. case DMA_MEM_TO_DEV:
  398. adma_dir = ADMA_CH_CTRL_DIR_MEM2AHUB;
  399. burst_size = fls(tdc->sconfig.dst_maxburst);
  400. ch_regs->config = ADMA_CH_CONFIG_SRC_BUF(desc->num_periods - 1);
  401. ch_regs->ctrl = ADMA_CH_CTRL_TX_REQ(tdc->sreq_index);
  402. ch_regs->src_addr = buf_addr;
  403. break;
  404. case DMA_DEV_TO_MEM:
  405. adma_dir = ADMA_CH_CTRL_DIR_AHUB2MEM;
  406. burst_size = fls(tdc->sconfig.src_maxburst);
  407. ch_regs->config = ADMA_CH_CONFIG_TRG_BUF(desc->num_periods - 1);
  408. ch_regs->ctrl = ADMA_CH_CTRL_RX_REQ(tdc->sreq_index);
  409. ch_regs->trg_addr = buf_addr;
  410. break;
  411. default:
  412. dev_err(tdc2dev(tdc), "DMA direction is not supported\n");
  413. return -EINVAL;
  414. }
  415. if (!burst_size || burst_size > ADMA_CH_CONFIG_BURST_16)
  416. burst_size = ADMA_CH_CONFIG_BURST_16;
  417. ch_regs->ctrl |= ADMA_CH_CTRL_DIR(adma_dir) |
  418. ADMA_CH_CTRL_MODE_CONTINUOUS |
  419. ADMA_CH_CTRL_FLOWCTRL_EN;
  420. ch_regs->config |= ADMA_CH_CONFIG_BURST_SIZE(burst_size);
  421. ch_regs->config |= ADMA_CH_CONFIG_WEIGHT_FOR_WRR(1);
  422. ch_regs->fifo_ctrl = ADMA_CH_FIFO_CTRL_DEFAULT;
  423. ch_regs->tc = desc->period_len & ADMA_CH_TC_COUNT_MASK;
  424. return tegra_adma_request_alloc(tdc, direction);
  425. }
  426. static struct dma_async_tx_descriptor *tegra_adma_prep_dma_cyclic(
  427. struct dma_chan *dc, dma_addr_t buf_addr, size_t buf_len,
  428. size_t period_len, enum dma_transfer_direction direction,
  429. unsigned long flags)
  430. {
  431. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  432. struct tegra_adma_desc *desc = NULL;
  433. if (!buf_len || !period_len || period_len > ADMA_CH_TC_COUNT_MASK) {
  434. dev_err(tdc2dev(tdc), "invalid buffer/period len\n");
  435. return NULL;
  436. }
  437. if (buf_len % period_len) {
  438. dev_err(tdc2dev(tdc), "buf_len not a multiple of period_len\n");
  439. return NULL;
  440. }
  441. if (!IS_ALIGNED(buf_addr, 4)) {
  442. dev_err(tdc2dev(tdc), "invalid buffer alignment\n");
  443. return NULL;
  444. }
  445. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  446. if (!desc)
  447. return NULL;
  448. desc->buf_len = buf_len;
  449. desc->period_len = period_len;
  450. desc->num_periods = buf_len / period_len;
  451. if (tegra_adma_set_xfer_params(tdc, desc, buf_addr, direction)) {
  452. kfree(desc);
  453. return NULL;
  454. }
  455. return vchan_tx_prep(&tdc->vc, &desc->vd, flags);
  456. }
  457. static int tegra_adma_alloc_chan_resources(struct dma_chan *dc)
  458. {
  459. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  460. int ret;
  461. ret = request_irq(tdc->irq, tegra_adma_isr, 0, dma_chan_name(dc), tdc);
  462. if (ret) {
  463. dev_err(tdc2dev(tdc), "failed to get interrupt for %s\n",
  464. dma_chan_name(dc));
  465. return ret;
  466. }
  467. ret = pm_runtime_get_sync(tdc2dev(tdc));
  468. if (ret < 0) {
  469. free_irq(tdc->irq, tdc);
  470. return ret;
  471. }
  472. dma_cookie_init(&tdc->vc.chan);
  473. return 0;
  474. }
  475. static void tegra_adma_free_chan_resources(struct dma_chan *dc)
  476. {
  477. struct tegra_adma_chan *tdc = to_tegra_adma_chan(dc);
  478. tegra_adma_terminate_all(dc);
  479. vchan_free_chan_resources(&tdc->vc);
  480. tasklet_kill(&tdc->vc.task);
  481. free_irq(tdc->irq, tdc);
  482. pm_runtime_put(tdc2dev(tdc));
  483. tdc->sreq_index = 0;
  484. tdc->sreq_dir = DMA_TRANS_NONE;
  485. }
  486. static struct dma_chan *tegra_dma_of_xlate(struct of_phandle_args *dma_spec,
  487. struct of_dma *ofdma)
  488. {
  489. struct tegra_adma *tdma = ofdma->of_dma_data;
  490. struct tegra_adma_chan *tdc;
  491. struct dma_chan *chan;
  492. unsigned int sreq_index;
  493. if (dma_spec->args_count != 1)
  494. return NULL;
  495. sreq_index = dma_spec->args[0];
  496. if (sreq_index == 0) {
  497. dev_err(tdma->dev, "DMA request must not be 0\n");
  498. return NULL;
  499. }
  500. chan = dma_get_any_slave_channel(&tdma->dma_dev);
  501. if (!chan)
  502. return NULL;
  503. tdc = to_tegra_adma_chan(chan);
  504. tdc->sreq_index = sreq_index;
  505. return chan;
  506. }
  507. static int tegra_adma_runtime_suspend(struct device *dev)
  508. {
  509. struct tegra_adma *tdma = dev_get_drvdata(dev);
  510. struct tegra_adma_chan_regs *ch_reg;
  511. struct tegra_adma_chan *tdc;
  512. int i;
  513. tdma->global_cmd = tdma_read(tdma, ADMA_GLOBAL_CMD);
  514. if (!tdma->global_cmd)
  515. goto clk_disable;
  516. for (i = 0; i < tdma->nr_channels; i++) {
  517. tdc = &tdma->channels[i];
  518. ch_reg = &tdc->ch_regs;
  519. ch_reg->cmd = tdma_ch_read(tdc, ADMA_CH_CMD);
  520. /* skip if channel is not active */
  521. if (!ch_reg->cmd)
  522. continue;
  523. ch_reg->tc = tdma_ch_read(tdc, ADMA_CH_TC);
  524. ch_reg->src_addr = tdma_ch_read(tdc, ADMA_CH_LOWER_SRC_ADDR);
  525. ch_reg->trg_addr = tdma_ch_read(tdc, ADMA_CH_LOWER_TRG_ADDR);
  526. ch_reg->ctrl = tdma_ch_read(tdc, ADMA_CH_CTRL);
  527. ch_reg->fifo_ctrl = tdma_ch_read(tdc, ADMA_CH_FIFO_CTRL);
  528. ch_reg->config = tdma_ch_read(tdc, ADMA_CH_CONFIG);
  529. }
  530. clk_disable:
  531. clk_disable_unprepare(tdma->ahub_clk);
  532. return 0;
  533. }
  534. static int tegra_adma_runtime_resume(struct device *dev)
  535. {
  536. struct tegra_adma *tdma = dev_get_drvdata(dev);
  537. struct tegra_adma_chan_regs *ch_reg;
  538. struct tegra_adma_chan *tdc;
  539. int ret, i;
  540. ret = clk_prepare_enable(tdma->ahub_clk);
  541. if (ret) {
  542. dev_err(dev, "ahub clk_enable failed: %d\n", ret);
  543. return ret;
  544. }
  545. tdma_write(tdma, ADMA_GLOBAL_CMD, tdma->global_cmd);
  546. if (!tdma->global_cmd)
  547. return 0;
  548. for (i = 0; i < tdma->nr_channels; i++) {
  549. tdc = &tdma->channels[i];
  550. ch_reg = &tdc->ch_regs;
  551. /* skip if channel was not active earlier */
  552. if (!ch_reg->cmd)
  553. continue;
  554. tdma_ch_write(tdc, ADMA_CH_TC, ch_reg->tc);
  555. tdma_ch_write(tdc, ADMA_CH_LOWER_SRC_ADDR, ch_reg->src_addr);
  556. tdma_ch_write(tdc, ADMA_CH_LOWER_TRG_ADDR, ch_reg->trg_addr);
  557. tdma_ch_write(tdc, ADMA_CH_CTRL, ch_reg->ctrl);
  558. tdma_ch_write(tdc, ADMA_CH_FIFO_CTRL, ch_reg->fifo_ctrl);
  559. tdma_ch_write(tdc, ADMA_CH_CONFIG, ch_reg->config);
  560. tdma_ch_write(tdc, ADMA_CH_CMD, ch_reg->cmd);
  561. }
  562. return 0;
  563. }
  564. static const struct tegra_adma_chip_data tegra210_chip_data = {
  565. .nr_channels = 22,
  566. };
  567. static const struct of_device_id tegra_adma_of_match[] = {
  568. { .compatible = "nvidia,tegra210-adma", .data = &tegra210_chip_data },
  569. { },
  570. };
  571. MODULE_DEVICE_TABLE(of, tegra_adma_of_match);
  572. static int tegra_adma_probe(struct platform_device *pdev)
  573. {
  574. const struct tegra_adma_chip_data *cdata;
  575. struct tegra_adma *tdma;
  576. struct resource *res;
  577. int ret, i;
  578. cdata = of_device_get_match_data(&pdev->dev);
  579. if (!cdata) {
  580. dev_err(&pdev->dev, "device match data not found\n");
  581. return -ENODEV;
  582. }
  583. tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
  584. sizeof(struct tegra_adma_chan), GFP_KERNEL);
  585. if (!tdma)
  586. return -ENOMEM;
  587. tdma->dev = &pdev->dev;
  588. tdma->nr_channels = cdata->nr_channels;
  589. platform_set_drvdata(pdev, tdma);
  590. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  591. tdma->base_addr = devm_ioremap_resource(&pdev->dev, res);
  592. if (IS_ERR(tdma->base_addr))
  593. return PTR_ERR(tdma->base_addr);
  594. tdma->ahub_clk = devm_clk_get(&pdev->dev, "d_audio");
  595. if (IS_ERR(tdma->ahub_clk)) {
  596. dev_err(&pdev->dev, "Error: Missing ahub controller clock\n");
  597. return PTR_ERR(tdma->ahub_clk);
  598. }
  599. INIT_LIST_HEAD(&tdma->dma_dev.channels);
  600. for (i = 0; i < tdma->nr_channels; i++) {
  601. struct tegra_adma_chan *tdc = &tdma->channels[i];
  602. tdc->chan_addr = tdma->base_addr + ADMA_CH_REG_OFFSET(i);
  603. tdc->irq = of_irq_get(pdev->dev.of_node, i);
  604. if (tdc->irq <= 0) {
  605. ret = tdc->irq ?: -ENXIO;
  606. goto irq_dispose;
  607. }
  608. vchan_init(&tdc->vc, &tdma->dma_dev);
  609. tdc->vc.desc_free = tegra_adma_desc_free;
  610. tdc->tdma = tdma;
  611. }
  612. pm_runtime_enable(&pdev->dev);
  613. ret = pm_runtime_get_sync(&pdev->dev);
  614. if (ret < 0)
  615. goto rpm_disable;
  616. ret = tegra_adma_init(tdma);
  617. if (ret)
  618. goto rpm_put;
  619. dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
  620. dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
  621. dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
  622. tdma->dma_dev.dev = &pdev->dev;
  623. tdma->dma_dev.device_alloc_chan_resources =
  624. tegra_adma_alloc_chan_resources;
  625. tdma->dma_dev.device_free_chan_resources =
  626. tegra_adma_free_chan_resources;
  627. tdma->dma_dev.device_issue_pending = tegra_adma_issue_pending;
  628. tdma->dma_dev.device_prep_dma_cyclic = tegra_adma_prep_dma_cyclic;
  629. tdma->dma_dev.device_config = tegra_adma_slave_config;
  630. tdma->dma_dev.device_tx_status = tegra_adma_tx_status;
  631. tdma->dma_dev.device_terminate_all = tegra_adma_terminate_all;
  632. tdma->dma_dev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  633. tdma->dma_dev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  634. tdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  635. tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  636. ret = dma_async_device_register(&tdma->dma_dev);
  637. if (ret < 0) {
  638. dev_err(&pdev->dev, "ADMA registration failed: %d\n", ret);
  639. goto irq_dispose;
  640. }
  641. ret = of_dma_controller_register(pdev->dev.of_node,
  642. tegra_dma_of_xlate, tdma);
  643. if (ret < 0) {
  644. dev_err(&pdev->dev, "ADMA OF registration failed %d\n", ret);
  645. goto dma_remove;
  646. }
  647. pm_runtime_put(&pdev->dev);
  648. dev_info(&pdev->dev, "Tegra210 ADMA driver registered %d channels\n",
  649. tdma->nr_channels);
  650. return 0;
  651. dma_remove:
  652. dma_async_device_unregister(&tdma->dma_dev);
  653. rpm_put:
  654. pm_runtime_put_sync(&pdev->dev);
  655. rpm_disable:
  656. pm_runtime_disable(&pdev->dev);
  657. irq_dispose:
  658. while (--i >= 0)
  659. irq_dispose_mapping(tdma->channels[i].irq);
  660. return ret;
  661. }
  662. static int tegra_adma_remove(struct platform_device *pdev)
  663. {
  664. struct tegra_adma *tdma = platform_get_drvdata(pdev);
  665. int i;
  666. of_dma_controller_free(pdev->dev.of_node);
  667. dma_async_device_unregister(&tdma->dma_dev);
  668. for (i = 0; i < tdma->nr_channels; ++i)
  669. irq_dispose_mapping(tdma->channels[i].irq);
  670. pm_runtime_put_sync(&pdev->dev);
  671. pm_runtime_disable(&pdev->dev);
  672. return 0;
  673. }
  674. #ifdef CONFIG_PM_SLEEP
  675. static int tegra_adma_pm_suspend(struct device *dev)
  676. {
  677. return pm_runtime_suspended(dev) == false;
  678. }
  679. #endif
  680. static const struct dev_pm_ops tegra_adma_dev_pm_ops = {
  681. SET_RUNTIME_PM_OPS(tegra_adma_runtime_suspend,
  682. tegra_adma_runtime_resume, NULL)
  683. SET_SYSTEM_SLEEP_PM_OPS(tegra_adma_pm_suspend, NULL)
  684. };
  685. static struct platform_driver tegra_admac_driver = {
  686. .driver = {
  687. .name = "tegra-adma",
  688. .pm = &tegra_adma_dev_pm_ops,
  689. .of_match_table = tegra_adma_of_match,
  690. },
  691. .probe = tegra_adma_probe,
  692. .remove = tegra_adma_remove,
  693. };
  694. module_platform_driver(tegra_admac_driver);
  695. MODULE_ALIAS("platform:tegra210-adma");
  696. MODULE_DESCRIPTION("NVIDIA Tegra ADMA driver");
  697. MODULE_AUTHOR("Dara Ramesh <dramesh@nvidia.com>");
  698. MODULE_AUTHOR("Jon Hunter <jonathanh@nvidia.com>");
  699. MODULE_LICENSE("GPL v2");