mmp_tdma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Driver For Marvell Two-channel DMA Engine
  3. *
  4. * Copyright: Marvell International Ltd.
  5. *
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. */
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/slab.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/device.h>
  21. #include <linux/platform_data/dma-mmp_tdma.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_dma.h>
  24. #include "dmaengine.h"
  25. /*
  26. * Two-Channel DMA registers
  27. */
  28. #define TDBCR 0x00 /* Byte Count */
  29. #define TDSAR 0x10 /* Src Addr */
  30. #define TDDAR 0x20 /* Dst Addr */
  31. #define TDNDPR 0x30 /* Next Desc */
  32. #define TDCR 0x40 /* Control */
  33. #define TDCP 0x60 /* Priority*/
  34. #define TDCDPR 0x70 /* Current Desc */
  35. #define TDIMR 0x80 /* Int Mask */
  36. #define TDISR 0xa0 /* Int Status */
  37. /* Two-Channel DMA Control Register */
  38. #define TDCR_SSZ_8_BITS (0x0 << 22) /* Sample Size */
  39. #define TDCR_SSZ_12_BITS (0x1 << 22)
  40. #define TDCR_SSZ_16_BITS (0x2 << 22)
  41. #define TDCR_SSZ_20_BITS (0x3 << 22)
  42. #define TDCR_SSZ_24_BITS (0x4 << 22)
  43. #define TDCR_SSZ_32_BITS (0x5 << 22)
  44. #define TDCR_SSZ_SHIFT (0x1 << 22)
  45. #define TDCR_SSZ_MASK (0x7 << 22)
  46. #define TDCR_SSPMOD (0x1 << 21) /* SSP MOD */
  47. #define TDCR_ABR (0x1 << 20) /* Channel Abort */
  48. #define TDCR_CDE (0x1 << 17) /* Close Desc Enable */
  49. #define TDCR_PACKMOD (0x1 << 16) /* Pack Mode (ADMA Only) */
  50. #define TDCR_CHANACT (0x1 << 14) /* Channel Active */
  51. #define TDCR_FETCHND (0x1 << 13) /* Fetch Next Desc */
  52. #define TDCR_CHANEN (0x1 << 12) /* Channel Enable */
  53. #define TDCR_INTMODE (0x1 << 10) /* Interrupt Mode */
  54. #define TDCR_CHAINMOD (0x1 << 9) /* Chain Mode */
  55. #define TDCR_BURSTSZ_MSK (0x7 << 6) /* Burst Size */
  56. #define TDCR_BURSTSZ_4B (0x0 << 6)
  57. #define TDCR_BURSTSZ_8B (0x1 << 6)
  58. #define TDCR_BURSTSZ_16B (0x3 << 6)
  59. #define TDCR_BURSTSZ_32B (0x6 << 6)
  60. #define TDCR_BURSTSZ_64B (0x7 << 6)
  61. #define TDCR_BURSTSZ_SQU_1B (0x5 << 6)
  62. #define TDCR_BURSTSZ_SQU_2B (0x6 << 6)
  63. #define TDCR_BURSTSZ_SQU_4B (0x0 << 6)
  64. #define TDCR_BURSTSZ_SQU_8B (0x1 << 6)
  65. #define TDCR_BURSTSZ_SQU_16B (0x3 << 6)
  66. #define TDCR_BURSTSZ_SQU_32B (0x7 << 6)
  67. #define TDCR_BURSTSZ_128B (0x5 << 6)
  68. #define TDCR_DSTDIR_MSK (0x3 << 4) /* Dst Direction */
  69. #define TDCR_DSTDIR_ADDR_HOLD (0x2 << 4) /* Dst Addr Hold */
  70. #define TDCR_DSTDIR_ADDR_INC (0x0 << 4) /* Dst Addr Increment */
  71. #define TDCR_SRCDIR_MSK (0x3 << 2) /* Src Direction */
  72. #define TDCR_SRCDIR_ADDR_HOLD (0x2 << 2) /* Src Addr Hold */
  73. #define TDCR_SRCDIR_ADDR_INC (0x0 << 2) /* Src Addr Increment */
  74. #define TDCR_DSTDESCCONT (0x1 << 1)
  75. #define TDCR_SRCDESTCONT (0x1 << 0)
  76. /* Two-Channel DMA Int Mask Register */
  77. #define TDIMR_COMP (0x1 << 0)
  78. /* Two-Channel DMA Int Status Register */
  79. #define TDISR_COMP (0x1 << 0)
  80. /*
  81. * Two-Channel DMA Descriptor Struct
  82. * NOTE: desc's buf must be aligned to 16 bytes.
  83. */
  84. struct mmp_tdma_desc {
  85. u32 byte_cnt;
  86. u32 src_addr;
  87. u32 dst_addr;
  88. u32 nxt_desc;
  89. };
  90. enum mmp_tdma_type {
  91. MMP_AUD_TDMA = 0,
  92. PXA910_SQU,
  93. };
  94. #define TDMA_ALIGNMENT 3
  95. #define TDMA_MAX_XFER_BYTES SZ_64K
  96. struct mmp_tdma_chan {
  97. struct device *dev;
  98. struct dma_chan chan;
  99. struct dma_async_tx_descriptor desc;
  100. struct tasklet_struct tasklet;
  101. struct mmp_tdma_desc *desc_arr;
  102. dma_addr_t desc_arr_phys;
  103. int desc_num;
  104. enum dma_transfer_direction dir;
  105. dma_addr_t dev_addr;
  106. u32 burst_sz;
  107. enum dma_slave_buswidth buswidth;
  108. enum dma_status status;
  109. int idx;
  110. enum mmp_tdma_type type;
  111. int irq;
  112. void __iomem *reg_base;
  113. size_t buf_len;
  114. size_t period_len;
  115. size_t pos;
  116. struct gen_pool *pool;
  117. };
  118. #define TDMA_CHANNEL_NUM 2
  119. struct mmp_tdma_device {
  120. struct device *dev;
  121. void __iomem *base;
  122. struct dma_device device;
  123. struct mmp_tdma_chan *tdmac[TDMA_CHANNEL_NUM];
  124. };
  125. #define to_mmp_tdma_chan(dchan) container_of(dchan, struct mmp_tdma_chan, chan)
  126. static void mmp_tdma_chan_set_desc(struct mmp_tdma_chan *tdmac, dma_addr_t phys)
  127. {
  128. writel(phys, tdmac->reg_base + TDNDPR);
  129. writel(readl(tdmac->reg_base + TDCR) | TDCR_FETCHND,
  130. tdmac->reg_base + TDCR);
  131. }
  132. static void mmp_tdma_enable_irq(struct mmp_tdma_chan *tdmac, bool enable)
  133. {
  134. if (enable)
  135. writel(TDIMR_COMP, tdmac->reg_base + TDIMR);
  136. else
  137. writel(0, tdmac->reg_base + TDIMR);
  138. }
  139. static void mmp_tdma_enable_chan(struct mmp_tdma_chan *tdmac)
  140. {
  141. /* enable dma chan */
  142. writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
  143. tdmac->reg_base + TDCR);
  144. tdmac->status = DMA_IN_PROGRESS;
  145. }
  146. static int mmp_tdma_disable_chan(struct dma_chan *chan)
  147. {
  148. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  149. u32 tdcr;
  150. tdcr = readl(tdmac->reg_base + TDCR);
  151. tdcr |= TDCR_ABR;
  152. tdcr &= ~TDCR_CHANEN;
  153. writel(tdcr, tdmac->reg_base + TDCR);
  154. tdmac->status = DMA_COMPLETE;
  155. return 0;
  156. }
  157. static int mmp_tdma_resume_chan(struct dma_chan *chan)
  158. {
  159. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  160. writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
  161. tdmac->reg_base + TDCR);
  162. tdmac->status = DMA_IN_PROGRESS;
  163. return 0;
  164. }
  165. static int mmp_tdma_pause_chan(struct dma_chan *chan)
  166. {
  167. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  168. writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN,
  169. tdmac->reg_base + TDCR);
  170. tdmac->status = DMA_PAUSED;
  171. return 0;
  172. }
  173. static int mmp_tdma_config_chan(struct dma_chan *chan)
  174. {
  175. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  176. unsigned int tdcr = 0;
  177. mmp_tdma_disable_chan(chan);
  178. if (tdmac->dir == DMA_MEM_TO_DEV)
  179. tdcr = TDCR_DSTDIR_ADDR_HOLD | TDCR_SRCDIR_ADDR_INC;
  180. else if (tdmac->dir == DMA_DEV_TO_MEM)
  181. tdcr = TDCR_SRCDIR_ADDR_HOLD | TDCR_DSTDIR_ADDR_INC;
  182. if (tdmac->type == MMP_AUD_TDMA) {
  183. tdcr |= TDCR_PACKMOD;
  184. switch (tdmac->burst_sz) {
  185. case 4:
  186. tdcr |= TDCR_BURSTSZ_4B;
  187. break;
  188. case 8:
  189. tdcr |= TDCR_BURSTSZ_8B;
  190. break;
  191. case 16:
  192. tdcr |= TDCR_BURSTSZ_16B;
  193. break;
  194. case 32:
  195. tdcr |= TDCR_BURSTSZ_32B;
  196. break;
  197. case 64:
  198. tdcr |= TDCR_BURSTSZ_64B;
  199. break;
  200. case 128:
  201. tdcr |= TDCR_BURSTSZ_128B;
  202. break;
  203. default:
  204. dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
  205. return -EINVAL;
  206. }
  207. switch (tdmac->buswidth) {
  208. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  209. tdcr |= TDCR_SSZ_8_BITS;
  210. break;
  211. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  212. tdcr |= TDCR_SSZ_16_BITS;
  213. break;
  214. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  215. tdcr |= TDCR_SSZ_32_BITS;
  216. break;
  217. default:
  218. dev_err(tdmac->dev, "mmp_tdma: unknown bus size.\n");
  219. return -EINVAL;
  220. }
  221. } else if (tdmac->type == PXA910_SQU) {
  222. tdcr |= TDCR_SSPMOD;
  223. switch (tdmac->burst_sz) {
  224. case 1:
  225. tdcr |= TDCR_BURSTSZ_SQU_1B;
  226. break;
  227. case 2:
  228. tdcr |= TDCR_BURSTSZ_SQU_2B;
  229. break;
  230. case 4:
  231. tdcr |= TDCR_BURSTSZ_SQU_4B;
  232. break;
  233. case 8:
  234. tdcr |= TDCR_BURSTSZ_SQU_8B;
  235. break;
  236. case 16:
  237. tdcr |= TDCR_BURSTSZ_SQU_16B;
  238. break;
  239. case 32:
  240. tdcr |= TDCR_BURSTSZ_SQU_32B;
  241. break;
  242. default:
  243. dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
  244. return -EINVAL;
  245. }
  246. }
  247. writel(tdcr, tdmac->reg_base + TDCR);
  248. return 0;
  249. }
  250. static int mmp_tdma_clear_chan_irq(struct mmp_tdma_chan *tdmac)
  251. {
  252. u32 reg = readl(tdmac->reg_base + TDISR);
  253. if (reg & TDISR_COMP) {
  254. /* clear irq */
  255. reg &= ~TDISR_COMP;
  256. writel(reg, tdmac->reg_base + TDISR);
  257. return 0;
  258. }
  259. return -EAGAIN;
  260. }
  261. static size_t mmp_tdma_get_pos(struct mmp_tdma_chan *tdmac)
  262. {
  263. size_t reg;
  264. if (tdmac->idx == 0) {
  265. reg = __raw_readl(tdmac->reg_base + TDSAR);
  266. reg -= tdmac->desc_arr[0].src_addr;
  267. } else if (tdmac->idx == 1) {
  268. reg = __raw_readl(tdmac->reg_base + TDDAR);
  269. reg -= tdmac->desc_arr[0].dst_addr;
  270. } else
  271. return -EINVAL;
  272. return reg;
  273. }
  274. static irqreturn_t mmp_tdma_chan_handler(int irq, void *dev_id)
  275. {
  276. struct mmp_tdma_chan *tdmac = dev_id;
  277. if (mmp_tdma_clear_chan_irq(tdmac) == 0) {
  278. tasklet_schedule(&tdmac->tasklet);
  279. return IRQ_HANDLED;
  280. } else
  281. return IRQ_NONE;
  282. }
  283. static irqreturn_t mmp_tdma_int_handler(int irq, void *dev_id)
  284. {
  285. struct mmp_tdma_device *tdev = dev_id;
  286. int i, ret;
  287. int irq_num = 0;
  288. for (i = 0; i < TDMA_CHANNEL_NUM; i++) {
  289. struct mmp_tdma_chan *tdmac = tdev->tdmac[i];
  290. ret = mmp_tdma_chan_handler(irq, tdmac);
  291. if (ret == IRQ_HANDLED)
  292. irq_num++;
  293. }
  294. if (irq_num)
  295. return IRQ_HANDLED;
  296. else
  297. return IRQ_NONE;
  298. }
  299. static void dma_do_tasklet(unsigned long data)
  300. {
  301. struct mmp_tdma_chan *tdmac = (struct mmp_tdma_chan *)data;
  302. if (tdmac->desc.callback)
  303. tdmac->desc.callback(tdmac->desc.callback_param);
  304. }
  305. static void mmp_tdma_free_descriptor(struct mmp_tdma_chan *tdmac)
  306. {
  307. struct gen_pool *gpool;
  308. int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
  309. gpool = tdmac->pool;
  310. if (gpool && tdmac->desc_arr)
  311. gen_pool_free(gpool, (unsigned long)tdmac->desc_arr,
  312. size);
  313. tdmac->desc_arr = NULL;
  314. return;
  315. }
  316. static dma_cookie_t mmp_tdma_tx_submit(struct dma_async_tx_descriptor *tx)
  317. {
  318. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(tx->chan);
  319. mmp_tdma_chan_set_desc(tdmac, tdmac->desc_arr_phys);
  320. return 0;
  321. }
  322. static int mmp_tdma_alloc_chan_resources(struct dma_chan *chan)
  323. {
  324. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  325. int ret;
  326. dma_async_tx_descriptor_init(&tdmac->desc, chan);
  327. tdmac->desc.tx_submit = mmp_tdma_tx_submit;
  328. if (tdmac->irq) {
  329. ret = devm_request_irq(tdmac->dev, tdmac->irq,
  330. mmp_tdma_chan_handler, 0, "tdma", tdmac);
  331. if (ret)
  332. return ret;
  333. }
  334. return 1;
  335. }
  336. static void mmp_tdma_free_chan_resources(struct dma_chan *chan)
  337. {
  338. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  339. if (tdmac->irq)
  340. devm_free_irq(tdmac->dev, tdmac->irq, tdmac);
  341. mmp_tdma_free_descriptor(tdmac);
  342. return;
  343. }
  344. struct mmp_tdma_desc *mmp_tdma_alloc_descriptor(struct mmp_tdma_chan *tdmac)
  345. {
  346. struct gen_pool *gpool;
  347. int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
  348. gpool = tdmac->pool;
  349. if (!gpool)
  350. return NULL;
  351. tdmac->desc_arr = gen_pool_dma_alloc(gpool, size, &tdmac->desc_arr_phys);
  352. return tdmac->desc_arr;
  353. }
  354. static struct dma_async_tx_descriptor *mmp_tdma_prep_dma_cyclic(
  355. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  356. size_t period_len, enum dma_transfer_direction direction,
  357. unsigned long flags)
  358. {
  359. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  360. struct mmp_tdma_desc *desc;
  361. int num_periods = buf_len / period_len;
  362. int i = 0, buf = 0;
  363. if (tdmac->status != DMA_COMPLETE)
  364. return NULL;
  365. if (period_len > TDMA_MAX_XFER_BYTES) {
  366. dev_err(tdmac->dev,
  367. "maximum period size exceeded: %d > %d\n",
  368. period_len, TDMA_MAX_XFER_BYTES);
  369. goto err_out;
  370. }
  371. tdmac->status = DMA_IN_PROGRESS;
  372. tdmac->desc_num = num_periods;
  373. desc = mmp_tdma_alloc_descriptor(tdmac);
  374. if (!desc)
  375. goto err_out;
  376. while (buf < buf_len) {
  377. desc = &tdmac->desc_arr[i];
  378. if (i + 1 == num_periods)
  379. desc->nxt_desc = tdmac->desc_arr_phys;
  380. else
  381. desc->nxt_desc = tdmac->desc_arr_phys +
  382. sizeof(*desc) * (i + 1);
  383. if (direction == DMA_MEM_TO_DEV) {
  384. desc->src_addr = dma_addr;
  385. desc->dst_addr = tdmac->dev_addr;
  386. } else {
  387. desc->src_addr = tdmac->dev_addr;
  388. desc->dst_addr = dma_addr;
  389. }
  390. desc->byte_cnt = period_len;
  391. dma_addr += period_len;
  392. buf += period_len;
  393. i++;
  394. }
  395. /* enable interrupt */
  396. if (flags & DMA_PREP_INTERRUPT)
  397. mmp_tdma_enable_irq(tdmac, true);
  398. tdmac->buf_len = buf_len;
  399. tdmac->period_len = period_len;
  400. tdmac->pos = 0;
  401. return &tdmac->desc;
  402. err_out:
  403. tdmac->status = DMA_ERROR;
  404. return NULL;
  405. }
  406. static int mmp_tdma_terminate_all(struct dma_chan *chan)
  407. {
  408. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  409. mmp_tdma_disable_chan(chan);
  410. /* disable interrupt */
  411. mmp_tdma_enable_irq(tdmac, false);
  412. return 0;
  413. }
  414. static int mmp_tdma_config(struct dma_chan *chan,
  415. struct dma_slave_config *dmaengine_cfg)
  416. {
  417. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  418. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  419. tdmac->dev_addr = dmaengine_cfg->src_addr;
  420. tdmac->burst_sz = dmaengine_cfg->src_maxburst;
  421. tdmac->buswidth = dmaengine_cfg->src_addr_width;
  422. } else {
  423. tdmac->dev_addr = dmaengine_cfg->dst_addr;
  424. tdmac->burst_sz = dmaengine_cfg->dst_maxburst;
  425. tdmac->buswidth = dmaengine_cfg->dst_addr_width;
  426. }
  427. tdmac->dir = dmaengine_cfg->direction;
  428. return mmp_tdma_config_chan(chan);
  429. }
  430. static enum dma_status mmp_tdma_tx_status(struct dma_chan *chan,
  431. dma_cookie_t cookie, struct dma_tx_state *txstate)
  432. {
  433. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  434. tdmac->pos = mmp_tdma_get_pos(tdmac);
  435. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  436. tdmac->buf_len - tdmac->pos);
  437. return tdmac->status;
  438. }
  439. static void mmp_tdma_issue_pending(struct dma_chan *chan)
  440. {
  441. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  442. mmp_tdma_enable_chan(tdmac);
  443. }
  444. static int mmp_tdma_remove(struct platform_device *pdev)
  445. {
  446. struct mmp_tdma_device *tdev = platform_get_drvdata(pdev);
  447. dma_async_device_unregister(&tdev->device);
  448. return 0;
  449. }
  450. static int mmp_tdma_chan_init(struct mmp_tdma_device *tdev,
  451. int idx, int irq,
  452. int type, struct gen_pool *pool)
  453. {
  454. struct mmp_tdma_chan *tdmac;
  455. if (idx >= TDMA_CHANNEL_NUM) {
  456. dev_err(tdev->dev, "too many channels for device!\n");
  457. return -EINVAL;
  458. }
  459. /* alloc channel */
  460. tdmac = devm_kzalloc(tdev->dev, sizeof(*tdmac), GFP_KERNEL);
  461. if (!tdmac) {
  462. dev_err(tdev->dev, "no free memory for DMA channels!\n");
  463. return -ENOMEM;
  464. }
  465. if (irq)
  466. tdmac->irq = irq;
  467. tdmac->dev = tdev->dev;
  468. tdmac->chan.device = &tdev->device;
  469. tdmac->idx = idx;
  470. tdmac->type = type;
  471. tdmac->reg_base = tdev->base + idx * 4;
  472. tdmac->pool = pool;
  473. tdmac->status = DMA_COMPLETE;
  474. tdev->tdmac[tdmac->idx] = tdmac;
  475. tasklet_init(&tdmac->tasklet, dma_do_tasklet, (unsigned long)tdmac);
  476. /* add the channel to tdma_chan list */
  477. list_add_tail(&tdmac->chan.device_node,
  478. &tdev->device.channels);
  479. return 0;
  480. }
  481. struct mmp_tdma_filter_param {
  482. struct device_node *of_node;
  483. unsigned int chan_id;
  484. };
  485. static bool mmp_tdma_filter_fn(struct dma_chan *chan, void *fn_param)
  486. {
  487. struct mmp_tdma_filter_param *param = fn_param;
  488. struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
  489. struct dma_device *pdma_device = tdmac->chan.device;
  490. if (pdma_device->dev->of_node != param->of_node)
  491. return false;
  492. if (chan->chan_id != param->chan_id)
  493. return false;
  494. return true;
  495. }
  496. struct dma_chan *mmp_tdma_xlate(struct of_phandle_args *dma_spec,
  497. struct of_dma *ofdma)
  498. {
  499. struct mmp_tdma_device *tdev = ofdma->of_dma_data;
  500. dma_cap_mask_t mask = tdev->device.cap_mask;
  501. struct mmp_tdma_filter_param param;
  502. if (dma_spec->args_count != 1)
  503. return NULL;
  504. param.of_node = ofdma->of_node;
  505. param.chan_id = dma_spec->args[0];
  506. if (param.chan_id >= TDMA_CHANNEL_NUM)
  507. return NULL;
  508. return dma_request_channel(mask, mmp_tdma_filter_fn, &param);
  509. }
  510. static const struct of_device_id mmp_tdma_dt_ids[] = {
  511. { .compatible = "marvell,adma-1.0", .data = (void *)MMP_AUD_TDMA},
  512. { .compatible = "marvell,pxa910-squ", .data = (void *)PXA910_SQU},
  513. {}
  514. };
  515. MODULE_DEVICE_TABLE(of, mmp_tdma_dt_ids);
  516. static int mmp_tdma_probe(struct platform_device *pdev)
  517. {
  518. enum mmp_tdma_type type;
  519. const struct of_device_id *of_id;
  520. struct mmp_tdma_device *tdev;
  521. struct resource *iores;
  522. int i, ret;
  523. int irq = 0, irq_num = 0;
  524. int chan_num = TDMA_CHANNEL_NUM;
  525. struct gen_pool *pool = NULL;
  526. of_id = of_match_device(mmp_tdma_dt_ids, &pdev->dev);
  527. if (of_id)
  528. type = (enum mmp_tdma_type) of_id->data;
  529. else
  530. type = platform_get_device_id(pdev)->driver_data;
  531. /* always have couple channels */
  532. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  533. if (!tdev)
  534. return -ENOMEM;
  535. tdev->dev = &pdev->dev;
  536. for (i = 0; i < chan_num; i++) {
  537. if (platform_get_irq(pdev, i) > 0)
  538. irq_num++;
  539. }
  540. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  541. tdev->base = devm_ioremap_resource(&pdev->dev, iores);
  542. if (IS_ERR(tdev->base))
  543. return PTR_ERR(tdev->base);
  544. INIT_LIST_HEAD(&tdev->device.channels);
  545. if (pdev->dev.of_node)
  546. pool = of_get_named_gen_pool(pdev->dev.of_node, "asram", 0);
  547. else
  548. pool = sram_get_gpool("asram");
  549. if (!pool) {
  550. dev_err(&pdev->dev, "asram pool not available\n");
  551. return -ENOMEM;
  552. }
  553. if (irq_num != chan_num) {
  554. irq = platform_get_irq(pdev, 0);
  555. ret = devm_request_irq(&pdev->dev, irq,
  556. mmp_tdma_int_handler, 0, "tdma", tdev);
  557. if (ret)
  558. return ret;
  559. }
  560. /* initialize channel parameters */
  561. for (i = 0; i < chan_num; i++) {
  562. irq = (irq_num != chan_num) ? 0 : platform_get_irq(pdev, i);
  563. ret = mmp_tdma_chan_init(tdev, i, irq, type, pool);
  564. if (ret)
  565. return ret;
  566. }
  567. dma_cap_set(DMA_SLAVE, tdev->device.cap_mask);
  568. dma_cap_set(DMA_CYCLIC, tdev->device.cap_mask);
  569. tdev->device.dev = &pdev->dev;
  570. tdev->device.device_alloc_chan_resources =
  571. mmp_tdma_alloc_chan_resources;
  572. tdev->device.device_free_chan_resources =
  573. mmp_tdma_free_chan_resources;
  574. tdev->device.device_prep_dma_cyclic = mmp_tdma_prep_dma_cyclic;
  575. tdev->device.device_tx_status = mmp_tdma_tx_status;
  576. tdev->device.device_issue_pending = mmp_tdma_issue_pending;
  577. tdev->device.device_config = mmp_tdma_config;
  578. tdev->device.device_pause = mmp_tdma_pause_chan;
  579. tdev->device.device_resume = mmp_tdma_resume_chan;
  580. tdev->device.device_terminate_all = mmp_tdma_terminate_all;
  581. tdev->device.copy_align = TDMA_ALIGNMENT;
  582. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  583. platform_set_drvdata(pdev, tdev);
  584. ret = dma_async_device_register(&tdev->device);
  585. if (ret) {
  586. dev_err(tdev->device.dev, "unable to register\n");
  587. return ret;
  588. }
  589. if (pdev->dev.of_node) {
  590. ret = of_dma_controller_register(pdev->dev.of_node,
  591. mmp_tdma_xlate, tdev);
  592. if (ret) {
  593. dev_err(tdev->device.dev,
  594. "failed to register controller\n");
  595. dma_async_device_unregister(&tdev->device);
  596. }
  597. }
  598. dev_info(tdev->device.dev, "initialized\n");
  599. return 0;
  600. }
  601. static const struct platform_device_id mmp_tdma_id_table[] = {
  602. { "mmp-adma", MMP_AUD_TDMA },
  603. { "pxa910-squ", PXA910_SQU },
  604. { },
  605. };
  606. static struct platform_driver mmp_tdma_driver = {
  607. .driver = {
  608. .name = "mmp-tdma",
  609. .of_match_table = mmp_tdma_dt_ids,
  610. },
  611. .id_table = mmp_tdma_id_table,
  612. .probe = mmp_tdma_probe,
  613. .remove = mmp_tdma_remove,
  614. };
  615. module_platform_driver(mmp_tdma_driver);
  616. MODULE_LICENSE("GPL");
  617. MODULE_DESCRIPTION("MMP Two-Channel DMA Driver");
  618. MODULE_ALIAS("platform:mmp-tdma");
  619. MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
  620. MODULE_AUTHOR("Zhangfei Gao <zhangfei.gao@marvell.com>");