dw.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2007-2008 Atmel Corporation
  3. // Copyright (C) 2010-2011 ST Microelectronics
  4. // Copyright (C) 2013,2018 Intel Corporation
  5. #include <linux/bitops.h>
  6. #include <linux/dmaengine.h>
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include "internal.h"
  11. static void dw_dma_initialize_chan(struct dw_dma_chan *dwc)
  12. {
  13. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  14. u32 cfghi = is_slave_direction(dwc->direction) ? 0 : DWC_CFGH_FIFO_MODE;
  15. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  16. bool hs_polarity = dwc->dws.hs_polarity;
  17. cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id);
  18. cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id);
  19. cfghi |= DWC_CFGH_PROTCTL(dw->pdata->protctl);
  20. /* Set polarity of handshake interface */
  21. cfglo |= hs_polarity ? DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL : 0;
  22. channel_writel(dwc, CFG_LO, cfglo);
  23. channel_writel(dwc, CFG_HI, cfghi);
  24. }
  25. static void dw_dma_suspend_chan(struct dw_dma_chan *dwc, bool drain)
  26. {
  27. u32 cfglo = channel_readl(dwc, CFG_LO);
  28. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  29. }
  30. static void dw_dma_resume_chan(struct dw_dma_chan *dwc, bool drain)
  31. {
  32. u32 cfglo = channel_readl(dwc, CFG_LO);
  33. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  34. }
  35. static u32 dw_dma_bytes2block(struct dw_dma_chan *dwc,
  36. size_t bytes, unsigned int width, size_t *len)
  37. {
  38. u32 block;
  39. if ((bytes >> width) > dwc->block_size) {
  40. block = dwc->block_size;
  41. *len = dwc->block_size << width;
  42. } else {
  43. block = bytes >> width;
  44. *len = bytes;
  45. }
  46. return block;
  47. }
  48. static size_t dw_dma_block2bytes(struct dw_dma_chan *dwc, u32 block, u32 width)
  49. {
  50. return DWC_CTLH_BLOCK_TS(block) << width;
  51. }
  52. static inline u8 dw_dma_encode_maxburst(u32 maxburst)
  53. {
  54. /*
  55. * Fix burst size according to dw_dmac. We need to convert them as:
  56. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  57. */
  58. return maxburst > 1 ? fls(maxburst) - 2 : 0;
  59. }
  60. static u32 dw_dma_prepare_ctllo(struct dw_dma_chan *dwc)
  61. {
  62. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  63. u8 smsize = 0, dmsize = 0;
  64. u8 sms, dms;
  65. if (dwc->direction == DMA_MEM_TO_DEV) {
  66. sms = dwc->dws.m_master;
  67. dms = dwc->dws.p_master;
  68. dmsize = dw_dma_encode_maxburst(sconfig->dst_maxburst);
  69. } else if (dwc->direction == DMA_DEV_TO_MEM) {
  70. sms = dwc->dws.p_master;
  71. dms = dwc->dws.m_master;
  72. smsize = dw_dma_encode_maxburst(sconfig->src_maxburst);
  73. } else /* DMA_MEM_TO_MEM */ {
  74. sms = dwc->dws.m_master;
  75. dms = dwc->dws.m_master;
  76. }
  77. return DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN |
  78. DWC_CTLL_DST_MSIZE(dmsize) | DWC_CTLL_SRC_MSIZE(smsize) |
  79. DWC_CTLL_DMS(dms) | DWC_CTLL_SMS(sms);
  80. }
  81. static void dw_dma_set_device_name(struct dw_dma *dw, int id)
  82. {
  83. snprintf(dw->name, sizeof(dw->name), "dw:dmac%d", id);
  84. }
  85. static void dw_dma_disable(struct dw_dma *dw)
  86. {
  87. do_dw_dma_off(dw);
  88. }
  89. static void dw_dma_enable(struct dw_dma *dw)
  90. {
  91. do_dw_dma_on(dw);
  92. }
  93. int dw_dma_probe(struct dw_dma_chip *chip)
  94. {
  95. struct dw_dma *dw;
  96. dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
  97. if (!dw)
  98. return -ENOMEM;
  99. /* Channel operations */
  100. dw->initialize_chan = dw_dma_initialize_chan;
  101. dw->suspend_chan = dw_dma_suspend_chan;
  102. dw->resume_chan = dw_dma_resume_chan;
  103. dw->prepare_ctllo = dw_dma_prepare_ctllo;
  104. dw->bytes2block = dw_dma_bytes2block;
  105. dw->block2bytes = dw_dma_block2bytes;
  106. /* Device operations */
  107. dw->set_device_name = dw_dma_set_device_name;
  108. dw->disable = dw_dma_disable;
  109. dw->enable = dw_dma_enable;
  110. chip->dw = dw;
  111. return do_dma_probe(chip);
  112. }
  113. EXPORT_SYMBOL_GPL(dw_dma_probe);
  114. int dw_dma_remove(struct dw_dma_chip *chip)
  115. {
  116. return do_dma_remove(chip);
  117. }
  118. EXPORT_SYMBOL_GPL(dw_dma_remove);