mmc: atmel-mci: fix a potential issue about pending PDC interrupts
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / host / atmel-mci.c
index caae967dcafecb92ad5d439df9e53c2c6f54dc2f..a7ee5027146528aafc6b18fa8c2e55f32e32a174 100644 (file)
@@ -603,12 +603,12 @@ static void atmci_pdc_set_single_buf(struct atmel_mci *host,
        }
 
        if (buf_nb == PDC_SECOND_BUF) {
-               pointer_reg += 0x10;
-               counter_reg += 0x10;
+               pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
+               counter_reg += ATMEL_PDC_SCND_BUF_OFF;
        }
 
        atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
-       if (host->data_size <= PAGE_SIZE) {
+       if (host->data_size <= sg_dma_len(host->sg)) {
                if (host->data_size & 0x3) {
                        /* If size is different from modulo 4, transfer bytes */
                        atmci_writel(host, counter_reg, host->data_size);
@@ -620,8 +620,8 @@ static void atmci_pdc_set_single_buf(struct atmel_mci *host,
                host->data_size = 0;
        } else {
                /* We assume the size of a page is 32-bits aligned */
-               atmci_writel(host, counter_reg, PAGE_SIZE / 4);
-               host->data_size -= PAGE_SIZE;
+               atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
+               host->data_size -= sg_dma_len(host->sg);
                if (host->data_size)
                        host->sg = sg_next(host->sg);
        }
@@ -808,7 +808,6 @@ atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
        /* Configure PDC */
        host->data_size = data->blocks * data->blksz;
        sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
-       BUG_ON(sg_len < host->data_size / PAGE_SIZE);
        if (host->data_size)
                atmci_pdc_set_both_buf(host,
                        ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
@@ -1730,17 +1729,9 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
                        tasklet_schedule(&host->tasklet);
                }
 
-               if (pending & ATMCI_ENDTX) {
-                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
-                       if (host->data_size) {
-                               atmci_pdc_set_single_buf(host,
-                                               XFER_TRANSMIT, PDC_SECOND_BUF);
-                               atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
-                       }
-               }
-
                if (pending & ATMCI_TXBUFE) {
                        atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
+                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
                        /*
                         * We can receive this interruption before having configured
                         * the second pdc buffer, so we need to reconfigure first and
@@ -1748,24 +1739,24 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
                         */
                        if (host->data_size) {
                                atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
+                               atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
                                atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
                        } else {
                                atmci_pdc_complete(host);
                        }
-               }
-
-               if (pending & ATMCI_ENDRX) {
-                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
+               } else if (pending & ATMCI_ENDTX) {
+                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
 
                        if (host->data_size) {
                                atmci_pdc_set_single_buf(host,
-                                               XFER_RECEIVE, PDC_SECOND_BUF);
-                               atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
+                                               XFER_TRANSMIT, PDC_SECOND_BUF);
+                               atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
                        }
                }
 
                if (pending & ATMCI_RXBUFF) {
                        atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
+                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
                        /*
                         * We can receive this interruption before having configured
                         * the second pdc buffer, so we need to reconfigure first and
@@ -1773,12 +1764,22 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
                         */
                        if (host->data_size) {
                                atmci_pdc_set_both_buf(host, XFER_RECEIVE);
+                               atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
                                atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
                        } else {
                                atmci_pdc_complete(host);
                        }
+               } else if (pending & ATMCI_ENDRX) {
+                       atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
+
+                       if (host->data_size) {
+                               atmci_pdc_set_single_buf(host,
+                                               XFER_RECEIVE, PDC_SECOND_BUF);
+                               atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
+                       }
                }
 
+
                if (pending & ATMCI_NOTBUSY) {
                        atmci_writel(host, ATMCI_IDR,
                                        ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY);