Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-21 Thread Long Cheng
On Sun, 2019-01-20 at 10:57 +0530, Vinod Koul wrote:
> On 10-01-19, 18:33, Long Cheng wrote:
> > On Fri, 2019-01-04 at 22:49 +0530, Vinod Koul wrote:
> > > On 02-01-19, 10:12, Long Cheng wrote:
> > > > In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> > > > If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> > > > the performance, can enable the function.
> > > 
> > > Is the DMA controller UART specific, can it work with other controllers
> > > as well, if so you should get rid of uart name in patch
> > 
> > I don't know that it can work or not on other controller. but it's for
> > MediaTek SOC
> 
> What I meant was that if can work with other controllers (users) apart
> from UART, how about say audio, spi etc!!
> 

it's just for UART APDMA. can't work on spi ...

> > 
> > > > +#define MTK_UART_APDMA_CHANNELS
> > > > (CONFIG_SERIAL_8250_NR_UARTS * 2)
> > > 
> > > Why are the channels not coming from DT?
> > > 
> > 
> > i will using dma-requests install of it.
> > 
> > > > +
> > > > +#define VFF_EN_B   BIT(0)
> > > > +#define VFF_STOP_B BIT(0)
> > > > +#define VFF_FLUSH_BBIT(0)
> > > > +#define VFF_4G_SUPPORT_B   BIT(0)
> > > > +#define VFF_RX_INT_EN0_B   BIT(0)  /*rx valid size >=  vff thre*/
> > > > +#define VFF_RX_INT_EN1_B   BIT(1)
> > > > +#define VFF_TX_INT_EN_BBIT(0)  /*tx left size >= vff 
> > > > thre*/
> > > 
> > > space around /* space */ also run checkpatch to check for style errors
> > > 
> > 
> > ok.
> > 
> > > > +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> > > > +{
> > > > +   unsigned int len, send, left, wpt, d_wpt, tmp;
> > > > +   int ret;
> > > > +
> > > > +   left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> > > > +   if (!left) {
> > > > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > > +   return;
> > > > +   }
> > > > +
> > > > +   /* Wait 1sec for flush,  can't sleep*/
> > > > +   ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> > > > +   tmp != VFF_FLUSH_B, 0, 100);
> > > > +   if (ret)
> > > > +   dev_warn(c->vc.chan.device->dev, "tx: fail, 
> > > > debug=0x%x\n",
> > > > +   mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> > > > +
> > > > +   send = min_t(unsigned int, left, c->desc->avail_len);
> > > > +   wpt = mtk_uart_apdma_read(c, VFF_WPT);
> > > > +   len = mtk_uart_apdma_read(c, VFF_LEN);
> > > > +
> > > > +   d_wpt = wpt + send;
> > > > +   if ((d_wpt & VFF_RING_SIZE) >= len) {
> > > > +   d_wpt = d_wpt - len;
> > > > +   d_wpt = d_wpt ^ VFF_RING_WRAP;
> > > > +   }
> > > > +   mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> > > > +
> > > > +   c->desc->avail_len -= send;
> > > > +
> > > > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > > +   if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> > > > +   mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> > > > +}
> > > > +
> > > > +static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
> > > > +{
> > > > +   struct mtk_uart_apdma_desc *d = c->desc;
> > > > +   unsigned int len, wg, rg, cnt;
> > > > +
> > > > +   if ((mtk_uart_apdma_read(c, VFF_VALID_SIZE) == 0U) ||
> > > > +   !d || !vchan_next_desc(>vc))
> > > > +   return;
> > > > +
> > > > +   len = mtk_uart_apdma_read(c, VFF_LEN);
> > > > +   rg = mtk_uart_apdma_read(c, VFF_RPT);
> > > > +   wg = mtk_uart_apdma_read(c, VFF_WPT);
> > > > +   if ((rg ^ wg) & VFF_RING_WRAP)
> > > > +   cnt = (wg & VFF_RING_SIZE) + len - (rg & VFF_RING_SIZE);
> > > > +   else
> > > > +   cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
> > > > +
> > > > +   c->rx_status = cnt;
> > > > +   mtk_uart_apdma_write(c, VFF_RPT, wg);
> > > > +
> > > > +   list_del(>vd.node);
> > > > +   vchan_cookie_complete(>vd);
> > > > +}
> > > 
> > > this looks odd, why do you have different rx and tx start routines?
> > > 
> > 
> > Would you like explain it in more detail? thanks.
> > In tx function, will wait the last data flush done. and the count the
> > size that send.
> > In Rx function, will count the size that receive.
> > Any way, in rx / tx, need andle WPT or RPT.
> > 
> > > > +static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
> > > > +{
> > > > +   struct mtk_uart_apdmadev *mtkd = 
> > > > to_mtk_uart_apdma_dev(chan->device);
> > > > +   struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> > > > +   u32 tmp;
> > > > +   int ret;
> > > > +
> > > > +   pm_runtime_get_sync(mtkd->ddev.dev);
> > > > +
> > > > +   mtk_uart_apdma_write(c, VFF_ADDR, 0);
> > > > +   mtk_uart_apdma_write(c, VFF_THRE, 0);
> > > > +   mtk_uart_apdma_write(c, VFF_LEN, 0);
> > > > +   mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
> > 

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-19 Thread Vinod Koul
On 10-01-19, 18:33, Long Cheng wrote:
> On Fri, 2019-01-04 at 22:49 +0530, Vinod Koul wrote:
> > On 02-01-19, 10:12, Long Cheng wrote:
> > > In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> > > If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> > > the performance, can enable the function.
> > 
> > Is the DMA controller UART specific, can it work with other controllers
> > as well, if so you should get rid of uart name in patch
> 
> I don't know that it can work or not on other controller. but it's for
> MediaTek SOC

What I meant was that if can work with other controllers (users) apart
from UART, how about say audio, spi etc!!

> 
> > > +#define MTK_UART_APDMA_CHANNELS  (CONFIG_SERIAL_8250_NR_UARTS * 
> > > 2)
> > 
> > Why are the channels not coming from DT?
> > 
> 
> i will using dma-requests install of it.
> 
> > > +
> > > +#define VFF_EN_B BIT(0)
> > > +#define VFF_STOP_B   BIT(0)
> > > +#define VFF_FLUSH_B  BIT(0)
> > > +#define VFF_4G_SUPPORT_B BIT(0)
> > > +#define VFF_RX_INT_EN0_B BIT(0)  /*rx valid size >=  vff thre*/
> > > +#define VFF_RX_INT_EN1_B BIT(1)
> > > +#define VFF_TX_INT_EN_B  BIT(0)  /*tx left size >= vff thre*/
> > 
> > space around /* space */ also run checkpatch to check for style errors
> > 
> 
> ok.
> 
> > > +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> > > +{
> > > + unsigned int len, send, left, wpt, d_wpt, tmp;
> > > + int ret;
> > > +
> > > + left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> > > + if (!left) {
> > > + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > + return;
> > > + }
> > > +
> > > + /* Wait 1sec for flush,  can't sleep*/
> > > + ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> > > + tmp != VFF_FLUSH_B, 0, 100);
> > > + if (ret)
> > > + dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
> > > + mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> > > +
> > > + send = min_t(unsigned int, left, c->desc->avail_len);
> > > + wpt = mtk_uart_apdma_read(c, VFF_WPT);
> > > + len = mtk_uart_apdma_read(c, VFF_LEN);
> > > +
> > > + d_wpt = wpt + send;
> > > + if ((d_wpt & VFF_RING_SIZE) >= len) {
> > > + d_wpt = d_wpt - len;
> > > + d_wpt = d_wpt ^ VFF_RING_WRAP;
> > > + }
> > > + mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> > > +
> > > + c->desc->avail_len -= send;
> > > +
> > > + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > + if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> > > + mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> > > +}
> > > +
> > > +static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
> > > +{
> > > + struct mtk_uart_apdma_desc *d = c->desc;
> > > + unsigned int len, wg, rg, cnt;
> > > +
> > > + if ((mtk_uart_apdma_read(c, VFF_VALID_SIZE) == 0U) ||
> > > + !d || !vchan_next_desc(>vc))
> > > + return;
> > > +
> > > + len = mtk_uart_apdma_read(c, VFF_LEN);
> > > + rg = mtk_uart_apdma_read(c, VFF_RPT);
> > > + wg = mtk_uart_apdma_read(c, VFF_WPT);
> > > + if ((rg ^ wg) & VFF_RING_WRAP)
> > > + cnt = (wg & VFF_RING_SIZE) + len - (rg & VFF_RING_SIZE);
> > > + else
> > > + cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
> > > +
> > > + c->rx_status = cnt;
> > > + mtk_uart_apdma_write(c, VFF_RPT, wg);
> > > +
> > > + list_del(>vd.node);
> > > + vchan_cookie_complete(>vd);
> > > +}
> > 
> > this looks odd, why do you have different rx and tx start routines?
> > 
> 
> Would you like explain it in more detail? thanks.
> In tx function, will wait the last data flush done. and the count the
> size that send.
> In Rx function, will count the size that receive.
> Any way, in rx / tx, need andle WPT or RPT.
> 
> > > +static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
> > > +{
> > > + struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
> > > + struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> > > + u32 tmp;
> > > + int ret;
> > > +
> > > + pm_runtime_get_sync(mtkd->ddev.dev);
> > > +
> > > + mtk_uart_apdma_write(c, VFF_ADDR, 0);
> > > + mtk_uart_apdma_write(c, VFF_THRE, 0);
> > > + mtk_uart_apdma_write(c, VFF_LEN, 0);
> > > + mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
> > > +
> > > + ret = readx_poll_timeout(readl, c->base + VFF_EN, tmp,
> > > + tmp == 0, 10, 100);
> > > + if (ret) {
> > > + dev_err(chan->device->dev, "dma reset: fail, timeout\n");
> > > + return ret;
> > > + }
> > 
> > register read does reset?
> > 
> 
> 'mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);' is reset. resd just
> poll reset done.
> 
> > > +
> > > + if (!c->requested) {
> > > + c->requested = true;
> > > + ret = request_irq(mtkd->dma_irq[chan->chan_id],
> > > +   mtk_uart_apdma_irq_handler, IRQF_TRIGGER_NONE,
> > > +   KBUILD_MODNAME, chan);
> > 
> > why is the irq not requested in driver 

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-10 Thread Long Cheng
On Thu, 2019-01-10 at 18:33 +0800, Long Cheng wrote:
fix spell error

> On Fri, 2019-01-04 at 22:49 +0530, Vinod Koul wrote:
> > On 02-01-19, 10:12, Long Cheng wrote:
> > > In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> > > If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> > > the performance, can enable the function.
> > 
> > Is the DMA controller UART specific, can it work with other controllers
> > as well, if so you should get rid of uart name in patch
> > 
> 
> I don't know that it can work or not on other controller. but it's for
> MediaTek SOC
> 
> > > +#define MTK_UART_APDMA_CHANNELS  (CONFIG_SERIAL_8250_NR_UARTS * 
> > > 2)
> > 
> > Why are the channels not coming from DT?
> > 
> 
> i will using dma-requests install of it.
> 
i will using 'dma-requests' instead of it.

> > > +
> > > +#define VFF_EN_B BIT(0)
> > > +#define VFF_STOP_B   BIT(0)
> > > +#define VFF_FLUSH_B  BIT(0)
> > > +#define VFF_4G_SUPPORT_B BIT(0)
> > > +#define VFF_RX_INT_EN0_B BIT(0)  /*rx valid size >=  vff thre*/
> > > +#define VFF_RX_INT_EN1_B BIT(1)
> > > +#define VFF_TX_INT_EN_B  BIT(0)  /*tx left size >= vff thre*/
> > 
> > space around /* space */ also run checkpatch to check for style errors
> > 
> 
> ok.
> 
> > > +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> > > +{
> > > + unsigned int len, send, left, wpt, d_wpt, tmp;
> > > + int ret;
> > > +
> > > + left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> > > + if (!left) {
> > > + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > + return;
> > > + }
> > > +
> > > + /* Wait 1sec for flush,  can't sleep*/
> > > + ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> > > + tmp != VFF_FLUSH_B, 0, 100);
> > > + if (ret)
> > > + dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
> > > + mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> > > +
> > > + send = min_t(unsigned int, left, c->desc->avail_len);
> > > + wpt = mtk_uart_apdma_read(c, VFF_WPT);
> > > + len = mtk_uart_apdma_read(c, VFF_LEN);
> > > +
> > > + d_wpt = wpt + send;
> > > + if ((d_wpt & VFF_RING_SIZE) >= len) {
> > > + d_wpt = d_wpt - len;
> > > + d_wpt = d_wpt ^ VFF_RING_WRAP;
> > > + }
> > > + mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> > > +
> > > + c->desc->avail_len -= send;
> > > +
> > > + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > > + if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> > > + mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> > > +}
> > > +
> > > +static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
> > > +{
> > > + struct mtk_uart_apdma_desc *d = c->desc;
> > > + unsigned int len, wg, rg, cnt;
> > > +
> > > + if ((mtk_uart_apdma_read(c, VFF_VALID_SIZE) == 0U) ||
> > > + !d || !vchan_next_desc(>vc))
> > > + return;
> > > +
> > > + len = mtk_uart_apdma_read(c, VFF_LEN);
> > > + rg = mtk_uart_apdma_read(c, VFF_RPT);
> > > + wg = mtk_uart_apdma_read(c, VFF_WPT);
> > > + if ((rg ^ wg) & VFF_RING_WRAP)
> > > + cnt = (wg & VFF_RING_SIZE) + len - (rg & VFF_RING_SIZE);
> > > + else
> > > + cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
> > > +
> > > + c->rx_status = cnt;
> > > + mtk_uart_apdma_write(c, VFF_RPT, wg);
> > > +
> > > + list_del(>vd.node);
> > > + vchan_cookie_complete(>vd);
> > > +}
> > 
> > this looks odd, why do you have different rx and tx start routines?
> > 
> 
> Would you like explain it in more detail? thanks.
> In tx function, will wait the last data flush done. and the count the
> size that send.
> In Rx function, will count the size that receive.
> Any way, in rx / tx, need andle WPT or RPT.
> 
Any way, in rx / tx, need handle WPT or RPT.

> > > +static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
> > > +{
> > > + struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
> > > + struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> > > + u32 tmp;
> > > + int ret;
> > > +
> > > + pm_runtime_get_sync(mtkd->ddev.dev);
> > > +
> > > + mtk_uart_apdma_write(c, VFF_ADDR, 0);
> > > + mtk_uart_apdma_write(c, VFF_THRE, 0);
> > > + mtk_uart_apdma_write(c, VFF_LEN, 0);
> > > + mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
> > > +
> > > + ret = readx_poll_timeout(readl, c->base + VFF_EN, tmp,
> > > + tmp == 0, 10, 100);
> > > + if (ret) {
> > > + dev_err(chan->device->dev, "dma reset: fail, timeout\n");
> > > + return ret;
> > > + }
> > 
> > register read does reset?
> > 
> 
> 'mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);' is reset. resd just
> poll reset done.
> 
> > > +
> > > + if (!c->requested) {
> > > + c->requested = true;
> > > + ret = request_irq(mtkd->dma_irq[chan->chan_id],
> > > +   mtk_uart_apdma_irq_handler, IRQF_TRIGGER_NONE,
> > > +   KBUILD_MODNAME, chan);
> > 
> > why is the irq not requested in 

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-10 Thread Long Cheng
On Fri, 2019-01-04 at 22:49 +0530, Vinod Koul wrote:
> On 02-01-19, 10:12, Long Cheng wrote:
> > In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> > If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> > the performance, can enable the function.
> 
> Is the DMA controller UART specific, can it work with other controllers
> as well, if so you should get rid of uart name in patch
> 

I don't know that it can work or not on other controller. but it's for
MediaTek SOC

> > +#define MTK_UART_APDMA_CHANNELS(CONFIG_SERIAL_8250_NR_UARTS * 
> > 2)
> 
> Why are the channels not coming from DT?
> 

i will using dma-requests install of it.

> > +
> > +#define VFF_EN_B   BIT(0)
> > +#define VFF_STOP_B BIT(0)
> > +#define VFF_FLUSH_BBIT(0)
> > +#define VFF_4G_SUPPORT_B   BIT(0)
> > +#define VFF_RX_INT_EN0_B   BIT(0)  /*rx valid size >=  vff thre*/
> > +#define VFF_RX_INT_EN1_B   BIT(1)
> > +#define VFF_TX_INT_EN_BBIT(0)  /*tx left size >= vff thre*/
> 
> space around /* space */ also run checkpatch to check for style errors
> 

ok.

> > +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> > +{
> > +   unsigned int len, send, left, wpt, d_wpt, tmp;
> > +   int ret;
> > +
> > +   left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> > +   if (!left) {
> > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > +   return;
> > +   }
> > +
> > +   /* Wait 1sec for flush,  can't sleep*/
> > +   ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> > +   tmp != VFF_FLUSH_B, 0, 100);
> > +   if (ret)
> > +   dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
> > +   mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> > +
> > +   send = min_t(unsigned int, left, c->desc->avail_len);
> > +   wpt = mtk_uart_apdma_read(c, VFF_WPT);
> > +   len = mtk_uart_apdma_read(c, VFF_LEN);
> > +
> > +   d_wpt = wpt + send;
> > +   if ((d_wpt & VFF_RING_SIZE) >= len) {
> > +   d_wpt = d_wpt - len;
> > +   d_wpt = d_wpt ^ VFF_RING_WRAP;
> > +   }
> > +   mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> > +
> > +   c->desc->avail_len -= send;
> > +
> > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > +   if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> > +   mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> > +}
> > +
> > +static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
> > +{
> > +   struct mtk_uart_apdma_desc *d = c->desc;
> > +   unsigned int len, wg, rg, cnt;
> > +
> > +   if ((mtk_uart_apdma_read(c, VFF_VALID_SIZE) == 0U) ||
> > +   !d || !vchan_next_desc(>vc))
> > +   return;
> > +
> > +   len = mtk_uart_apdma_read(c, VFF_LEN);
> > +   rg = mtk_uart_apdma_read(c, VFF_RPT);
> > +   wg = mtk_uart_apdma_read(c, VFF_WPT);
> > +   if ((rg ^ wg) & VFF_RING_WRAP)
> > +   cnt = (wg & VFF_RING_SIZE) + len - (rg & VFF_RING_SIZE);
> > +   else
> > +   cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
> > +
> > +   c->rx_status = cnt;
> > +   mtk_uart_apdma_write(c, VFF_RPT, wg);
> > +
> > +   list_del(>vd.node);
> > +   vchan_cookie_complete(>vd);
> > +}
> 
> this looks odd, why do you have different rx and tx start routines?
> 

Would you like explain it in more detail? thanks.
In tx function, will wait the last data flush done. and the count the
size that send.
In Rx function, will count the size that receive.
Any way, in rx / tx, need andle WPT or RPT.

> > +static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
> > +{
> > +   struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
> > +   struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> > +   u32 tmp;
> > +   int ret;
> > +
> > +   pm_runtime_get_sync(mtkd->ddev.dev);
> > +
> > +   mtk_uart_apdma_write(c, VFF_ADDR, 0);
> > +   mtk_uart_apdma_write(c, VFF_THRE, 0);
> > +   mtk_uart_apdma_write(c, VFF_LEN, 0);
> > +   mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
> > +
> > +   ret = readx_poll_timeout(readl, c->base + VFF_EN, tmp,
> > +   tmp == 0, 10, 100);
> > +   if (ret) {
> > +   dev_err(chan->device->dev, "dma reset: fail, timeout\n");
> > +   return ret;
> > +   }
> 
> register read does reset?
> 

'mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);' is reset. resd just
poll reset done.

> > +
> > +   if (!c->requested) {
> > +   c->requested = true;
> > +   ret = request_irq(mtkd->dma_irq[chan->chan_id],
> > + mtk_uart_apdma_irq_handler, IRQF_TRIGGER_NONE,
> > + KBUILD_MODNAME, chan);
> 
> why is the irq not requested in driver probe?
> 

I have explained in below,
http://lists.infradead.org/pipermail/linux-mediatek/2018-December/016418.html

> > +static enum dma_status mtk_uart_apdma_tx_status(struct dma_chan *chan,
> > +dma_cookie_t cookie,
> > +

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-10 Thread Long Cheng
On Wed, 2019-01-02 at 18:03 -0800, Randy Dunlap wrote:
> Hi,
> 
> While you are making changes, here are a few more:
> 

hi

OK, thanks for your comments.

> 
> On 1/2/19 5:39 PM, Nicolas Boichat wrote:
> > diff --git a/drivers/dma/mediatek/Kconfig b/drivers/dma/mediatek/Kconfig
> > index 27bac0b..1a523c87 100644
> > --- a/drivers/dma/mediatek/Kconfig
> > +++ b/drivers/dma/mediatek/Kconfig
> > @@ -1,4 +1,15 @@
> > 
> > +config DMA_MTK_UART
> > +   tristate "MediaTek SoCs APDMA support for UART"
> > +   depends on OF && SERIAL_8250_MT6577
> > +   select DMA_ENGINE
> > +   select DMA_VIRTUAL_CHANNELS
> > +   help
> > + Support for the UART DMA engine found on MediaTek MTK SoCs.
> > + when SERIAL_8250_MT6577 is enabled, and if you want to use DMA,
> 
>   When
> 
> > + you can enable the config. the DMA engine can only be used
> 
>  The
> 
> > + with MediaTek SoCs.
> > +
> 

above, i will modify.

> Also, use tabs to indent instead of spaces.
> The lines (tristate, depends, select, and help) should be indented with one 
> tab.
> The help text lines should be indented with one tab + 2 spaces.
> 

in my patch, i already do this. So don't need modify.

> >  config MTK_HSDMA
> > tristate "MediaTek High-Speed DMA controller support"
> > depends on ARCH_MEDIATEK || COMPILE_TEST
> 
> 
> thanks,




Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-10 Thread Long Cheng
On Thu, 2019-01-03 at 09:39 +0800, Nicolas Boichat wrote:
> On Wed, Jan 2, 2019 at 10:13 AM Long Cheng  wrote:
> >

.

> > +/* interrupt trigger level for tx */
> > +#define VFF_TX_THRE(n) ((n) * 7 / 8)
> > +/* interrupt trigger level for rx */
> > +#define VFF_RX_THRE(n) ((n) * 3 / 4)
> > +
> > +#define VFF_RING_SIZE  0xU
> 
> Well, the size is actually 0x1. Maybe call this VFF_RING_SIZE_MASK?
> 

the max length is 0x.  the bit 16 is wrap bit. our buffer is ring
buffer. So not mask.

> > +/* invert this bit when wrap ring head again*/
> > +#define VFF_RING_WRAP  0x1U
> > +
> > +   writel(val, c->base + reg);
> > +}
> > +

..

> > +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> > +{
> > +   unsigned int len, send, left, wpt, d_wpt, tmp;
> > +   int ret;
> > +
> > +   left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> > +   if (!left) {
> > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > +   return;
> > +   }
> > +
> > +   /* Wait 1sec for flush,  can't sleep*/
> 
> nit: one space after ',', period after 'sleep', space before '*'.
> 
> > +   ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> > +   tmp != VFF_FLUSH_B, 0, 100);
> > +   if (ret)
> > +   dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
> > +   mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> 
> Why do we need to wait for flush now? The previous implementation did
> not require this...
> 

because our HW buffer is cyclic. at one tx, if the size of data is
bigger, data maybe cover. So must wait flush finish. confirm the data is
right. like 128 bytes size, small length can't reproduce the issue.

> > +
> > +   send = min_t(unsigned int, left, c->desc->avail_len);
> > +   wpt = mtk_uart_apdma_read(c, VFF_WPT);
> > +   len = mtk_uart_apdma_read(c, VFF_LEN);
> > +
> > +   d_wpt = wpt + send;
> > +   if ((d_wpt & VFF_RING_SIZE) >= len) {
> 
> I don't get why you need to add "& VFF_RING_SIZE". If wpt + send >
> VFF_RING_SIZE, don't you need to toggle VFF_RING_WRAP too?

the longest actual length is VFF_RING_SIZE. one cyclic, will set bit[16]
to ~bit[16]. the bit[0 ~ 15] is actual address. So need get rid of
bit[16]

> 
> > +   d_wpt = d_wpt - len;
> > +   d_wpt = d_wpt ^ VFF_RING_WRAP;
> > +   }
> > +   mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> > +
> > +   c->desc->avail_len -= send;
> > +
> > +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> > +   if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> > +   mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> > +}
> 
> (thanks for the rest of the changes, this looks much more readable)
> 
> > +
..

> > +
> > +static int mtk_uart_apdma_device_pause(struct dma_chan *chan)
> > +{
> > +   /* just for check caps pass */
> > +   return 0;
> > +}
> 
> This is still not right... Hopefully somebody more familiar with the
> DMA subsystem can weigh in, but maybe it's enough to wait for the
> current transfer to be flushed and temporarily disable interrupts?
> e.g. call mtk_uart_apdma_terminate_all above?
> 

i had review the 8250 UART framework. just check the function pointer,
not use. and our HW can't support the feature. So i just keep it at
first version.

> > +
> > +static int mtk_uart_apdma_device_resume(struct dma_chan *chan)
> > +{
> > +   /* just for check caps pass */
> > +   return 0;
> > +}
> 
> Drop this one since you don't really need it.
> 

ok, i will remove it.

> > +
> > +static void mtk_uart_apdma_free(struct mtk_uart_apdmadev *mtkd)
> > +{
> > +   while (list_empty(>ddev.channels) == 0) {
> 
> !list_empty(
> 
> > +   struct mtk_chan *c = list_first_entry(>ddev.channels,
> > +   struct mtk_chan, vc.chan.device_node);
> > +
> > +   list_del(>vc.chan.device_node);
> > +   tasklet_kill(>vc.task);
> > +   }
> > +}
> > +
> > +static const struct of_device_id mtk_uart_apdma_match[] = {
> > +   { .compatible = "mediatek,mt6577-uart-dma", },
> > +   { /* sentinel */ },
> > +};
> > +MODULE_DEVICE_TABLE(of, mtk_uart_apdma_match);
> > +
> > +static int mtk_uart_apdma_probe(struct platform_device *pdev)
> > +{
> > +   struct mtk_uart_apdmadev *mtkd;
> > +   struct resource *res;
> > +   struct mtk_chan *c;
> > +   unsigned int i;
> > +   int rc;
> > +
> > +   mtkd = devm_kzalloc(>dev, sizeof(*mtkd), GFP_KERNEL);
> > +   if (!mtkd)
> > +   return -ENOMEM;
> > +
> > +   mtkd->clk = devm_clk_get(>dev, NULL);
> > +   if (IS_ERR(mtkd->clk)) {
> > +   dev_err(>dev, "No clock specified\n");
> > +   rc = PTR_ERR(mtkd->clk);
> > +   return rc;
> > +   }
> > +
> > +   if (of_property_read_bool(pdev->dev.of_node, "dma-33bits"))
> > +   mtkd->support_33bits = 

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-04 Thread Vinod Koul
On 02-01-19, 10:12, Long Cheng wrote:
> In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> the performance, can enable the function.

Is the DMA controller UART specific, can it work with other controllers
as well, if so you should get rid of uart name in patch

> +#define MTK_UART_APDMA_CHANNELS  (CONFIG_SERIAL_8250_NR_UARTS * 
> 2)

Why are the channels not coming from DT?

> +
> +#define VFF_EN_B BIT(0)
> +#define VFF_STOP_B   BIT(0)
> +#define VFF_FLUSH_B  BIT(0)
> +#define VFF_4G_SUPPORT_B BIT(0)
> +#define VFF_RX_INT_EN0_B BIT(0)  /*rx valid size >=  vff thre*/
> +#define VFF_RX_INT_EN1_B BIT(1)
> +#define VFF_TX_INT_EN_B  BIT(0)  /*tx left size >= vff thre*/

space around /* space */ also run checkpatch to check for style errors

> +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> +{
> + unsigned int len, send, left, wpt, d_wpt, tmp;
> + int ret;
> +
> + left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> + if (!left) {
> + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> + return;
> + }
> +
> + /* Wait 1sec for flush,  can't sleep*/
> + ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
> + tmp != VFF_FLUSH_B, 0, 100);
> + if (ret)
> + dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
> + mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
> +
> + send = min_t(unsigned int, left, c->desc->avail_len);
> + wpt = mtk_uart_apdma_read(c, VFF_WPT);
> + len = mtk_uart_apdma_read(c, VFF_LEN);
> +
> + d_wpt = wpt + send;
> + if ((d_wpt & VFF_RING_SIZE) >= len) {
> + d_wpt = d_wpt - len;
> + d_wpt = d_wpt ^ VFF_RING_WRAP;
> + }
> + mtk_uart_apdma_write(c, VFF_WPT, d_wpt);
> +
> + c->desc->avail_len -= send;
> +
> + mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> + if (mtk_uart_apdma_read(c, VFF_FLUSH) == 0U)
> + mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
> +}
> +
> +static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
> +{
> + struct mtk_uart_apdma_desc *d = c->desc;
> + unsigned int len, wg, rg, cnt;
> +
> + if ((mtk_uart_apdma_read(c, VFF_VALID_SIZE) == 0U) ||
> + !d || !vchan_next_desc(>vc))
> + return;
> +
> + len = mtk_uart_apdma_read(c, VFF_LEN);
> + rg = mtk_uart_apdma_read(c, VFF_RPT);
> + wg = mtk_uart_apdma_read(c, VFF_WPT);
> + if ((rg ^ wg) & VFF_RING_WRAP)
> + cnt = (wg & VFF_RING_SIZE) + len - (rg & VFF_RING_SIZE);
> + else
> + cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
> +
> + c->rx_status = cnt;
> + mtk_uart_apdma_write(c, VFF_RPT, wg);
> +
> + list_del(>vd.node);
> + vchan_cookie_complete(>vd);
> +}

this looks odd, why do you have different rx and tx start routines?

> +static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
> +{
> + struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
> + struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> + u32 tmp;
> + int ret;
> +
> + pm_runtime_get_sync(mtkd->ddev.dev);
> +
> + mtk_uart_apdma_write(c, VFF_ADDR, 0);
> + mtk_uart_apdma_write(c, VFF_THRE, 0);
> + mtk_uart_apdma_write(c, VFF_LEN, 0);
> + mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
> +
> + ret = readx_poll_timeout(readl, c->base + VFF_EN, tmp,
> + tmp == 0, 10, 100);
> + if (ret) {
> + dev_err(chan->device->dev, "dma reset: fail, timeout\n");
> + return ret;
> + }

register read does reset?

> +
> + if (!c->requested) {
> + c->requested = true;
> + ret = request_irq(mtkd->dma_irq[chan->chan_id],
> +   mtk_uart_apdma_irq_handler, IRQF_TRIGGER_NONE,
> +   KBUILD_MODNAME, chan);

why is the irq not requested in driver probe?

> +static enum dma_status mtk_uart_apdma_tx_status(struct dma_chan *chan,
> +  dma_cookie_t cookie,
> +  struct dma_tx_state *txstate)
> +{
> + struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> + enum dma_status ret;
> + unsigned long flags;
> +
> + if (!txstate)
> + return DMA_ERROR;
> +
> + ret = dma_cookie_status(chan, cookie, txstate);
> + spin_lock_irqsave(>vc.lock, flags);
> + if (ret == DMA_IN_PROGRESS) {
> + c->rx_status = mtk_uart_apdma_read(c, VFF_RPT) & VFF_RING_SIZE;
> + dma_set_residue(txstate, c->rx_status);
> + } else if (ret == DMA_COMPLETE && c->cfg.direction == DMA_DEV_TO_MEM) {

why set reside when it is complete? also reside can be null, that should
be checked as well

> +static struct dma_async_tx_descriptor 

Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-02 Thread Randy Dunlap
Hi,

While you are making changes, here are a few more:


On 1/2/19 5:39 PM, Nicolas Boichat wrote:
> diff --git a/drivers/dma/mediatek/Kconfig b/drivers/dma/mediatek/Kconfig
> index 27bac0b..1a523c87 100644
> --- a/drivers/dma/mediatek/Kconfig
> +++ b/drivers/dma/mediatek/Kconfig
> @@ -1,4 +1,15 @@
> 
> +config DMA_MTK_UART
> +   tristate "MediaTek SoCs APDMA support for UART"
> +   depends on OF && SERIAL_8250_MT6577
> +   select DMA_ENGINE
> +   select DMA_VIRTUAL_CHANNELS
> +   help
> + Support for the UART DMA engine found on MediaTek MTK SoCs.
> + when SERIAL_8250_MT6577 is enabled, and if you want to use DMA,

When

> + you can enable the config. the DMA engine can only be used

   The

> + with MediaTek SoCs.
> +

Also, use tabs to indent instead of spaces.
The lines (tristate, depends, select, and help) should be indented with one tab.
The help text lines should be indented with one tab + 2 spaces.

>  config MTK_HSDMA
> tristate "MediaTek High-Speed DMA controller support"
> depends on ARCH_MEDIATEK || COMPILE_TEST


thanks,
-- 
~Randy


Re: [PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-02 Thread Nicolas Boichat
On Wed, Jan 2, 2019 at 10:13 AM Long Cheng  wrote:
>
> In DMA engine framework, add 8250 uart dma to support MediaTek uart.
> If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
> the performance, can enable the function.
>
> Signed-off-by: Long Cheng 
> ---
>  drivers/dma/mediatek/8250_mtk_dma.c |  652 
> +++
>  drivers/dma/mediatek/Kconfig|   11 +
>  drivers/dma/mediatek/Makefile   |1 +
>  3 files changed, 664 insertions(+)
>  create mode 100644 drivers/dma/mediatek/8250_mtk_dma.c
>
> diff --git a/drivers/dma/mediatek/8250_mtk_dma.c 
> b/drivers/dma/mediatek/8250_mtk_dma.c
> new file mode 100644
> index 000..dbf811e
> --- /dev/null
> +++ b/drivers/dma/mediatek/8250_mtk_dma.c
> @@ -0,0 +1,652 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * MediaTek 8250 DMA driver.
> + *
> + * Copyright (c) 2018 MediaTek Inc.
> + * Author: Long Cheng 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include "../virt-dma.h"
> +
> +#define MTK_UART_APDMA_CHANNELS(CONFIG_SERIAL_8250_NR_UARTS 
> * 2)
> +
> +#define VFF_EN_B   BIT(0)
> +#define VFF_STOP_B BIT(0)
> +#define VFF_FLUSH_BBIT(0)
> +#define VFF_4G_SUPPORT_B   BIT(0)
> +#define VFF_RX_INT_EN0_B   BIT(0)  /*rx valid size >=  vff thre*/
> +#define VFF_RX_INT_EN1_B   BIT(1)
> +#define VFF_TX_INT_EN_BBIT(0)  /*tx left size >= vff thre*/
> +#define VFF_WARM_RST_B BIT(0)
> +#define VFF_RX_INT_FLAG_CLR_B  (BIT(0) | BIT(1))
> +#define VFF_TX_INT_FLAG_CLR_B  0
> +#define VFF_STOP_CLR_B 0
> +#define VFF_INT_EN_CLR_B   0
> +#define VFF_4G_SUPPORT_CLR_B   0
> +
> +/* interrupt trigger level for tx */
> +#define VFF_TX_THRE(n) ((n) * 7 / 8)
> +/* interrupt trigger level for rx */
> +#define VFF_RX_THRE(n) ((n) * 3 / 4)
> +
> +#define VFF_RING_SIZE  0xU

Well, the size is actually 0x1. Maybe call this VFF_RING_SIZE_MASK?

> +/* invert this bit when wrap ring head again*/
> +#define VFF_RING_WRAP  0x1U
> +
> +#define VFF_INT_FLAG   0x00
> +#define VFF_INT_EN 0x04
> +#define VFF_EN 0x08
> +#define VFF_RST0x0c
> +#define VFF_STOP   0x10
> +#define VFF_FLUSH  0x14
> +#define VFF_ADDR   0x1c
> +#define VFF_LEN0x24
> +#define VFF_THRE   0x28
> +#define VFF_WPT0x2c
> +#define VFF_RPT0x30
> +/*TX: the buffer size HW can read. RX: the buffer size SW can read.*/

nit: Spaces after /* and before */ (and a lot more occurences below,
please fix them all).

> +#define VFF_VALID_SIZE 0x3c
> +/*TX: the buffer size SW can write. RX: the buffer size HW can write.*/
> +#define VFF_LEFT_SIZE  0x40
> +#define VFF_DEBUG_STATUS   0x50
> +#define VFF_4G_SUPPORT 0x54
> +
> +struct mtk_uart_apdmadev {
> +   struct dma_device ddev;
> +   struct clk *clk;
> +   bool support_33bits;
> +   unsigned int dma_irq[MTK_UART_APDMA_CHANNELS];
> +};
> +
> +struct mtk_uart_apdma_desc {
> +   struct virt_dma_desc vd;
> +
> +   unsigned int avail_len;
> +};
> +
> +struct mtk_chan {
> +   struct virt_dma_chan vc;
> +   struct dma_slave_config cfg;
> +   void __iomem *base;
> +   struct mtk_uart_apdma_desc *desc;
> +
> +   bool requested;
> +
> +   unsigned int rx_status;
> +};
> +
> +static inline struct mtk_uart_apdmadev *
> +to_mtk_uart_apdma_dev(struct dma_device *d)
> +{
> +   return container_of(d, struct mtk_uart_apdmadev, ddev);
> +}
> +
> +static inline struct mtk_chan *to_mtk_uart_apdma_chan(struct dma_chan *c)
> +{
> +   return container_of(c, struct mtk_chan, vc.chan);
> +}
> +
> +static inline struct mtk_uart_apdma_desc *to_mtk_uart_apdma_desc
> +   (struct dma_async_tx_descriptor *t)
> +{
> +   return container_of(t, struct mtk_uart_apdma_desc, vd.tx);
> +}
> +
> +static void mtk_uart_apdma_write(struct mtk_chan *c,
> +  unsigned int reg, unsigned int val)
> +{
> +   writel(val, c->base + reg);
> +}
> +
> +static unsigned int mtk_uart_apdma_read(struct mtk_chan *c, unsigned int reg)
> +{
> +   return readl(c->base + reg);
> +}
> +
> +static void mtk_uart_apdma_desc_free(struct virt_dma_desc *vd)
> +{
> +   struct dma_chan *chan = vd->tx.chan;
> +   struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
> +
> +   kfree(c->desc);
> +}
> +
> +static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
> +{
> +   unsigned int len, send, left, wpt, d_wpt, tmp;
> +   int ret;
> +
> +   left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
> +   if (!left) {
> +   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
> 

[PATCH v9 1/2] dmaengine: 8250_mtk_dma: add MediaTek uart DMA support

2019-01-01 Thread Long Cheng
In DMA engine framework, add 8250 uart dma to support MediaTek uart.
If MediaTek uart enabled(SERIAL_8250_MT6577), and want to improve
the performance, can enable the function.

Signed-off-by: Long Cheng 
---
 drivers/dma/mediatek/8250_mtk_dma.c |  652 +++
 drivers/dma/mediatek/Kconfig|   11 +
 drivers/dma/mediatek/Makefile   |1 +
 3 files changed, 664 insertions(+)
 create mode 100644 drivers/dma/mediatek/8250_mtk_dma.c

diff --git a/drivers/dma/mediatek/8250_mtk_dma.c 
b/drivers/dma/mediatek/8250_mtk_dma.c
new file mode 100644
index 000..dbf811e
--- /dev/null
+++ b/drivers/dma/mediatek/8250_mtk_dma.c
@@ -0,0 +1,652 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * MediaTek 8250 DMA driver.
+ *
+ * Copyright (c) 2018 MediaTek Inc.
+ * Author: Long Cheng 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../virt-dma.h"
+
+#define MTK_UART_APDMA_CHANNELS(CONFIG_SERIAL_8250_NR_UARTS * 
2)
+
+#define VFF_EN_B   BIT(0)
+#define VFF_STOP_B BIT(0)
+#define VFF_FLUSH_BBIT(0)
+#define VFF_4G_SUPPORT_B   BIT(0)
+#define VFF_RX_INT_EN0_B   BIT(0)  /*rx valid size >=  vff thre*/
+#define VFF_RX_INT_EN1_B   BIT(1)
+#define VFF_TX_INT_EN_BBIT(0)  /*tx left size >= vff thre*/
+#define VFF_WARM_RST_B BIT(0)
+#define VFF_RX_INT_FLAG_CLR_B  (BIT(0) | BIT(1))
+#define VFF_TX_INT_FLAG_CLR_B  0
+#define VFF_STOP_CLR_B 0
+#define VFF_INT_EN_CLR_B   0
+#define VFF_4G_SUPPORT_CLR_B   0
+
+/* interrupt trigger level for tx */
+#define VFF_TX_THRE(n) ((n) * 7 / 8)
+/* interrupt trigger level for rx */
+#define VFF_RX_THRE(n) ((n) * 3 / 4)
+
+#define VFF_RING_SIZE  0xU
+/* invert this bit when wrap ring head again*/
+#define VFF_RING_WRAP  0x1U
+
+#define VFF_INT_FLAG   0x00
+#define VFF_INT_EN 0x04
+#define VFF_EN 0x08
+#define VFF_RST0x0c
+#define VFF_STOP   0x10
+#define VFF_FLUSH  0x14
+#define VFF_ADDR   0x1c
+#define VFF_LEN0x24
+#define VFF_THRE   0x28
+#define VFF_WPT0x2c
+#define VFF_RPT0x30
+/*TX: the buffer size HW can read. RX: the buffer size SW can read.*/
+#define VFF_VALID_SIZE 0x3c
+/*TX: the buffer size SW can write. RX: the buffer size HW can write.*/
+#define VFF_LEFT_SIZE  0x40
+#define VFF_DEBUG_STATUS   0x50
+#define VFF_4G_SUPPORT 0x54
+
+struct mtk_uart_apdmadev {
+   struct dma_device ddev;
+   struct clk *clk;
+   bool support_33bits;
+   unsigned int dma_irq[MTK_UART_APDMA_CHANNELS];
+};
+
+struct mtk_uart_apdma_desc {
+   struct virt_dma_desc vd;
+
+   unsigned int avail_len;
+};
+
+struct mtk_chan {
+   struct virt_dma_chan vc;
+   struct dma_slave_config cfg;
+   void __iomem *base;
+   struct mtk_uart_apdma_desc *desc;
+
+   bool requested;
+
+   unsigned int rx_status;
+};
+
+static inline struct mtk_uart_apdmadev *
+to_mtk_uart_apdma_dev(struct dma_device *d)
+{
+   return container_of(d, struct mtk_uart_apdmadev, ddev);
+}
+
+static inline struct mtk_chan *to_mtk_uart_apdma_chan(struct dma_chan *c)
+{
+   return container_of(c, struct mtk_chan, vc.chan);
+}
+
+static inline struct mtk_uart_apdma_desc *to_mtk_uart_apdma_desc
+   (struct dma_async_tx_descriptor *t)
+{
+   return container_of(t, struct mtk_uart_apdma_desc, vd.tx);
+}
+
+static void mtk_uart_apdma_write(struct mtk_chan *c,
+  unsigned int reg, unsigned int val)
+{
+   writel(val, c->base + reg);
+}
+
+static unsigned int mtk_uart_apdma_read(struct mtk_chan *c, unsigned int reg)
+{
+   return readl(c->base + reg);
+}
+
+static void mtk_uart_apdma_desc_free(struct virt_dma_desc *vd)
+{
+   struct dma_chan *chan = vd->tx.chan;
+   struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
+
+   kfree(c->desc);
+}
+
+static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
+{
+   unsigned int len, send, left, wpt, d_wpt, tmp;
+   int ret;
+
+   left = mtk_uart_apdma_read(c, VFF_LEFT_SIZE);
+   if (!left) {
+   mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
+   return;
+   }
+
+   /* Wait 1sec for flush,  can't sleep*/
+   ret = readx_poll_timeout(readl, c->base + VFF_FLUSH, tmp,
+   tmp != VFF_FLUSH_B, 0, 100);
+   if (ret)
+   dev_warn(c->vc.chan.device->dev, "tx: fail, debug=0x%x\n",
+   mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
+
+   send = min_t(unsigned int, left, c->desc->avail_len);
+   wpt = mtk_uart_apdma_read(c, VFF_WPT);
+   len = mtk_uart_apdma_read(c, VFF_LEN);
+
+   d_wpt