sh: prevent the DMA driver from unloading, while in use
[safe/jmp/linux-2.6] / drivers / dma / ste_dma40.c
index e4295a2..c426829 100644 (file)
@@ -659,7 +659,7 @@ static void d40_config_set_event(struct d40_chan *d40c, bool do_enable)
        spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
 }
 
-static bool d40_chan_has_events(struct d40_chan *d40c)
+static u32 d40_chan_has_events(struct d40_chan *d40c)
 {
        u32 val = 0;
 
@@ -674,7 +674,7 @@ static bool d40_chan_has_events(struct d40_chan *d40c)
                val = readl(d40c->base->virtbase + D40_DREG_PCBASE +
                            d40c->phy_chan->num * D40_DREG_PCDELTA +
                            D40_CHAN_REG_SDLNK);
-       return (bool) val;
+       return val;
 }
 
 static void d40_config_enable_lidx(struct d40_chan *d40c)
@@ -1039,11 +1039,11 @@ static int d40_validate_conf(struct d40_chan *d40c,
 }
 
 static bool d40_alloc_mask_set(struct d40_phy_res *phy, bool is_src,
-                              int log_event_line)
+                              int log_event_line, bool is_log)
 {
        unsigned long flags;
        spin_lock_irqsave(&phy->lock, flags);
-       if (!log_event_line) {
+       if (!is_log) {
                /* Physical interrupts are masked per physical full channel */
                if (phy->allocated_src == D40_ALLOC_FREE &&
                    phy->allocated_dst == D40_ALLOC_FREE) {
@@ -1161,15 +1161,16 @@ static int d40_allocate_channel(struct d40_chan *d40c)
                        /* Find physical half channel */
                        for (i = 0; i < d40c->base->num_phy_chans; i++) {
 
-                               if (d40_alloc_mask_set(&phys[i], is_src, 0))
+                               if (d40_alloc_mask_set(&phys[i], is_src,
+                                                      0, is_log))
                                        goto found_phy;
                        }
                } else
                        for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
                                int phy_num = j  + event_group * 2;
                                for (i = phy_num; i < phy_num + 2; i++) {
-                                       if (d40_alloc_mask_set(&phys[i],
-                                                              is_src, 0))
+                                       if (d40_alloc_mask_set(&phys[i], is_src,
+                                                              0, is_log))
                                                goto found_phy;
                                }
                        }
@@ -1193,13 +1194,13 @@ found_phy:
                if (is_src) {
                        for (i = phy_num; i < phy_num + 2; i++) {
                                if (d40_alloc_mask_set(&phys[i], is_src,
-                                                      event_line))
+                                                      event_line, is_log))
                                        goto found_log;
                        }
                } else {
                        for (i = phy_num + 1; i >= phy_num; i--) {
                                if (d40_alloc_mask_set(&phys[i], is_src,
-                                                      event_line))
+                                                      event_line, is_log))
                                        goto found_log;
                        }
                }
@@ -1389,6 +1390,65 @@ static int d40_pause(struct dma_chan *chan)
        return res;
 }
 
+static bool d40_is_paused(struct d40_chan *d40c)
+{
+       bool is_paused = false;
+       unsigned long flags;
+       void __iomem *active_reg;
+       u32 status;
+       u32 event;
+       int res;
+
+       spin_lock_irqsave(&d40c->lock, flags);
+
+       if (d40c->log_num == D40_PHY_CHAN) {
+               if (d40c->phy_chan->num % 2 == 0)
+                       active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
+               else
+                       active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
+
+               status = (readl(active_reg) &
+                         D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
+                       D40_CHAN_POS(d40c->phy_chan->num);
+               if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
+                       is_paused = true;
+
+               goto _exit;
+       }
+
+       res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
+       if (res != 0)
+               goto _exit;
+
+       if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
+           d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM)
+               event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
+       else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
+               event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
+       else {
+               dev_err(&d40c->chan.dev->device,
+                       "[%s] Unknown direction\n", __func__);
+               goto _exit;
+       }
+       status = d40_chan_has_events(d40c);
+       status = (status & D40_EVENTLINE_MASK(event)) >>
+               D40_EVENTLINE_POS(event);
+
+       if (status != D40_DMA_RUN)
+               is_paused = true;
+
+       /* Resume the other logical channels if any */
+       if (d40_chan_has_events(d40c))
+               res = d40_channel_execute_command(d40c,
+                                                 D40_DMA_RUN);
+
+_exit:
+       spin_unlock_irqrestore(&d40c->lock, flags);
+       return is_paused;
+
+}
+
+
 static bool d40_tx_is_linked(struct d40_chan *d40c)
 {
        bool is_link;
@@ -1980,13 +2040,13 @@ static enum dma_status d40_tx_status(struct dma_chan *chan,
        last_complete = d40c->completed;
        last_used = chan->cookie;
 
-       ret = dma_async_is_complete(cookie, last_complete, last_used);
+       if (d40_is_paused(d40c))
+               ret = DMA_PAUSED;
+       else
+               ret = dma_async_is_complete(cookie, last_complete, last_used);
 
-       if (txstate) {
-               txstate->last = last_complete;
-               txstate->used = last_used;
-               txstate->residue = stedma40_residue(chan);
-       }
+       dma_set_tx_state(txstate, last_complete, last_used,
+                        stedma40_residue(chan));
 
        return ret;
 }
@@ -2005,7 +2065,8 @@ static void d40_issue_pending(struct dma_chan *chan)
        spin_unlock_irqrestore(&d40c->lock, flags);
 }
 
-static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd)
+static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
+                      unsigned long arg)
 {
        unsigned long flags;
        struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);