LCOV - code coverage report
Current view: top level - drivers/dma - bcm2835-dma.c (source / functions) Hit Total Coverage
Test: Real Lines: 197 424 46.5 %
Date: 2020-10-17 15:46:16 Functions: 0 30 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0+
       2                 :            : /*
       3                 :            :  * BCM2835 DMA engine support
       4                 :            :  *
       5                 :            :  * Author:      Florian Meier <florian.meier@koalo.de>
       6                 :            :  *              Copyright 2013
       7                 :            :  *
       8                 :            :  * Based on
       9                 :            :  *      OMAP DMAengine support by Russell King
      10                 :            :  *
      11                 :            :  *      BCM2708 DMA Driver
      12                 :            :  *      Copyright (C) 2010 Broadcom
      13                 :            :  *
      14                 :            :  *      Raspberry Pi PCM I2S ALSA Driver
      15                 :            :  *      Copyright (c) by Phil Poole 2013
      16                 :            :  *
      17                 :            :  *      MARVELL MMP Peripheral DMA Driver
      18                 :            :  *      Copyright 2012 Marvell International Ltd.
      19                 :            :  */
      20                 :            : #include <linux/dmaengine.h>
      21                 :            : #include <linux/dma-mapping.h>
      22                 :            : #include <linux/dmapool.h>
      23                 :            : #include <linux/err.h>
      24                 :            : #include <linux/init.h>
      25                 :            : #include <linux/interrupt.h>
      26                 :            : #include <linux/list.h>
      27                 :            : #include <linux/module.h>
      28                 :            : #include <linux/platform_data/dma-bcm2708.h>
      29                 :            : #include <linux/platform_device.h>
      30                 :            : #include <linux/slab.h>
      31                 :            : #include <linux/io.h>
      32                 :            : #include <linux/spinlock.h>
      33                 :            : #include <linux/of.h>
      34                 :            : #include <linux/of_dma.h>
      35                 :            : 
      36                 :            : #include "virt-dma.h"
      37                 :            : 
      38                 :            : #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
      39                 :            : #define BCM2835_DMA_CHAN_NAME_SIZE 8
      40                 :            : #define BCM2835_DMA_BULK_MASK  BIT(0)
      41                 :            : #define BCM2711_DMA_MEMCPY_CHAN 14
      42                 :            : 
      43                 :            : struct bcm2835_dma_cfg_data {
      44                 :            :         u64     dma_mask;
      45                 :            :         u32     chan_40bit_mask;
      46                 :            : };
      47                 :            : 
      48                 :            : /**
      49                 :            :  * struct bcm2835_dmadev - BCM2835 DMA controller
      50                 :            :  * @ddev: DMA device
      51                 :            :  * @base: base address of register map
      52                 :            :  * @dma_parms: DMA parameters (to convey 1 GByte max segment size to clients)
      53                 :            :  * @zero_page: bus address of zero page (to detect transactions copying from
      54                 :            :  *      zero page and avoid accessing memory if so)
      55                 :            :  */
      56                 :            : struct bcm2835_dmadev {
      57                 :            :         struct dma_device ddev;
      58                 :            :         void __iomem *base;
      59                 :            :         struct device_dma_parameters dma_parms;
      60                 :            :         dma_addr_t zero_page;
      61                 :            :         const struct bcm2835_dma_cfg_data *cfg_data;
      62                 :            : };
      63                 :            : 
      64                 :            : struct bcm2835_dma_cb {
      65                 :            :         uint32_t info;
      66                 :            :         uint32_t src;
      67                 :            :         uint32_t dst;
      68                 :            :         uint32_t length;
      69                 :            :         uint32_t stride;
      70                 :            :         uint32_t next;
      71                 :            :         uint32_t pad[2];
      72                 :            : };
      73                 :            : 
      74                 :            : struct bcm2711_dma40_scb {
      75                 :            :         uint32_t ti;
      76                 :            :         uint32_t src;
      77                 :            :         uint32_t srci;
      78                 :            :         uint32_t dst;
      79                 :            :         uint32_t dsti;
      80                 :            :         uint32_t len;
      81                 :            :         uint32_t next_cb;
      82                 :            :         uint32_t rsvd;
      83                 :            : };
      84                 :            : 
      85                 :            : struct bcm2835_cb_entry {
      86                 :            :         struct bcm2835_dma_cb *cb;
      87                 :            :         dma_addr_t paddr;
      88                 :            : };
      89                 :            : 
      90                 :            : struct bcm2835_chan {
      91                 :            :         struct virt_dma_chan vc;
      92                 :            : 
      93                 :            :         struct dma_slave_config cfg;
      94                 :            :         unsigned int dreq;
      95                 :            : 
      96                 :            :         int ch;
      97                 :            :         struct bcm2835_desc *desc;
      98                 :            :         struct dma_pool *cb_pool;
      99                 :            : 
     100                 :            :         void __iomem *chan_base;
     101                 :            :         int irq_number;
     102                 :            :         unsigned int irq_flags;
     103                 :            : 
     104                 :            :         bool is_lite_channel;
     105                 :            :         bool is_40bit_channel;
     106                 :            : };
     107                 :            : 
     108                 :            : struct bcm2835_desc {
     109                 :            :         struct bcm2835_chan *c;
     110                 :            :         struct virt_dma_desc vd;
     111                 :            :         enum dma_transfer_direction dir;
     112                 :            : 
     113                 :            :         unsigned int frames;
     114                 :            :         size_t size;
     115                 :            : 
     116                 :            :         bool cyclic;
     117                 :            : 
     118                 :            :         struct bcm2835_cb_entry cb_list[];
     119                 :            : };
     120                 :            : 
     121                 :            : #define BCM2835_DMA_CS          0x00
     122                 :            : #define BCM2835_DMA_ADDR        0x04
     123                 :            : #define BCM2835_DMA_TI          0x08
     124                 :            : #define BCM2835_DMA_SOURCE_AD   0x0c
     125                 :            : #define BCM2835_DMA_DEST_AD     0x10
     126                 :            : #define BCM2835_DMA_LEN         0x14
     127                 :            : #define BCM2835_DMA_STRIDE      0x18
     128                 :            : #define BCM2835_DMA_NEXTCB      0x1c
     129                 :            : #define BCM2835_DMA_DEBUG       0x20
     130                 :            : 
     131                 :            : /* DMA CS Control and Status bits */
     132                 :            : #define BCM2835_DMA_ACTIVE      BIT(0)  /* activate the DMA */
     133                 :            : #define BCM2835_DMA_END         BIT(1)  /* current CB has ended */
     134                 :            : #define BCM2835_DMA_INT         BIT(2)  /* interrupt status */
     135                 :            : #define BCM2835_DMA_DREQ        BIT(3)  /* DREQ state */
     136                 :            : #define BCM2835_DMA_ISPAUSED    BIT(4)  /* Pause requested or not active */
     137                 :            : #define BCM2835_DMA_ISHELD      BIT(5)  /* Is held by DREQ flow control */
     138                 :            : #define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last
     139                 :            :                                                * AXI-write to ack
     140                 :            :                                                */
     141                 :            : #define BCM2835_DMA_ERR         BIT(8)
     142                 :            : #define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */
     143                 :            : #define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */
     144                 :            : /* current value of TI.BCM2835_DMA_WAIT_RESP */
     145                 :            : #define BCM2835_DMA_WAIT_FOR_WRITES BIT(28)
     146                 :            : #define BCM2835_DMA_DIS_DEBUG   BIT(29) /* disable debug pause signal */
     147                 :            : #define BCM2835_DMA_ABORT       BIT(30) /* Stop current CB, go to next, WO */
     148                 :            : #define BCM2835_DMA_RESET       BIT(31) /* WO, self clearing */
     149                 :            : 
     150                 :            : /* Transfer information bits - also bcm2835_cb.info field */
     151                 :            : #define BCM2835_DMA_INT_EN      BIT(0)
     152                 :            : #define BCM2835_DMA_TDMODE      BIT(1) /* 2D-Mode */
     153                 :            : #define BCM2835_DMA_WAIT_RESP   BIT(3) /* wait for AXI-write to be acked */
     154                 :            : #define BCM2835_DMA_D_INC       BIT(4)
     155                 :            : #define BCM2835_DMA_D_WIDTH     BIT(5) /* 128bit writes if set */
     156                 :            : #define BCM2835_DMA_D_DREQ      BIT(6) /* enable DREQ for destination */
     157                 :            : #define BCM2835_DMA_D_IGNORE    BIT(7) /* ignore destination writes */
     158                 :            : #define BCM2835_DMA_S_INC       BIT(8)
     159                 :            : #define BCM2835_DMA_S_WIDTH     BIT(9) /* 128bit writes if set */
     160                 :            : #define BCM2835_DMA_S_DREQ      BIT(10) /* enable SREQ for source */
     161                 :            : #define BCM2835_DMA_S_IGNORE    BIT(11) /* ignore source reads - read 0 */
     162                 :            : #define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12)
     163                 :            : #define BCM2835_DMA_CS_FLAGS(x) (x & (BCM2835_DMA_PRIORITY(15) | \
     164                 :            :                                       BCM2835_DMA_PANIC_PRIORITY(15) | \
     165                 :            :                                       BCM2835_DMA_WAIT_FOR_WRITES | \
     166                 :            :                                       BCM2835_DMA_DIS_DEBUG))
     167                 :            : #define BCM2835_DMA_PER_MAP(x)  ((x & 31) << 16) /* REQ source */
     168                 :            : #define BCM2835_DMA_WAIT(x)     ((x & 31) << 21) /* add DMA-wait cycles */
     169                 :            : #define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */
     170                 :            : 
     171                 :            : /* A fake bit to request that the driver doesn't set the WAIT_RESP bit. */
     172                 :            : #define BCM2835_DMA_NO_WAIT_RESP BIT(27)
     173                 :            : #define WAIT_RESP(x) ((x & BCM2835_DMA_NO_WAIT_RESP) ? \
     174                 :            :                       0 : BCM2835_DMA_WAIT_RESP)
     175                 :            : 
     176                 :            : /* debug register bits */
     177                 :            : #define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR      BIT(0)
     178                 :            : #define BCM2835_DMA_DEBUG_FIFO_ERR              BIT(1)
     179                 :            : #define BCM2835_DMA_DEBUG_READ_ERR              BIT(2)
     180                 :            : #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4
     181                 :            : #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4
     182                 :            : #define BCM2835_DMA_DEBUG_ID_SHIFT              16
     183                 :            : #define BCM2835_DMA_DEBUG_ID_BITS               9
     184                 :            : #define BCM2835_DMA_DEBUG_STATE_SHIFT           16
     185                 :            : #define BCM2835_DMA_DEBUG_STATE_BITS            9
     186                 :            : #define BCM2835_DMA_DEBUG_VERSION_SHIFT         25
     187                 :            : #define BCM2835_DMA_DEBUG_VERSION_BITS          3
     188                 :            : #define BCM2835_DMA_DEBUG_LITE                  BIT(28)
     189                 :            : 
     190                 :            : /* shared registers for all dma channels */
     191                 :            : #define BCM2835_DMA_INT_STATUS         0xfe0
     192                 :            : #define BCM2835_DMA_ENABLE             0xff0
     193                 :            : 
     194                 :            : #define BCM2835_DMA_DATA_TYPE_S8        1
     195                 :            : #define BCM2835_DMA_DATA_TYPE_S16       2
     196                 :            : #define BCM2835_DMA_DATA_TYPE_S32       4
     197                 :            : #define BCM2835_DMA_DATA_TYPE_S128      16
     198                 :            : 
     199                 :            : /* Valid only for channels 0 - 14, 15 has its own base address */
     200                 :            : #define BCM2835_DMA_CHAN_SIZE   0x100
     201                 :            : #define BCM2835_DMA_CHAN(n)     ((n) * BCM2835_DMA_CHAN_SIZE) /* Base address */
     202                 :            : #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
     203                 :            : 
     204                 :            : /* the max dma length for different channels */
     205                 :            : #define MAX_DMA_LEN SZ_1G
     206                 :            : #define MAX_LITE_DMA_LEN (SZ_64K - 4)
     207                 :            : 
     208                 :            : /* 40-bit DMA support */
     209                 :            : #define BCM2711_DMA40_CS        0x00
     210                 :            : #define BCM2711_DMA40_CB        0x04
     211                 :            : #define BCM2711_DMA40_DEBUG     0x0c
     212                 :            : #define BCM2711_DMA40_TI        0x10
     213                 :            : #define BCM2711_DMA40_SRC       0x14
     214                 :            : #define BCM2711_DMA40_SRCI      0x18
     215                 :            : #define BCM2711_DMA40_DEST      0x1c
     216                 :            : #define BCM2711_DMA40_DESTI     0x20
     217                 :            : #define BCM2711_DMA40_LEN       0x24
     218                 :            : #define BCM2711_DMA40_NEXT_CB   0x28
     219                 :            : #define BCM2711_DMA40_DEBUG2    0x2c
     220                 :            : 
     221                 :            : #define BCM2711_DMA40_ACTIVE            BIT(0)
     222                 :            : #define BCM2711_DMA40_END               BIT(1)
     223                 :            : #define BCM2711_DMA40_INT               BIT(2)
     224                 :            : #define BCM2711_DMA40_DREQ              BIT(3)  /* DREQ state */
     225                 :            : #define BCM2711_DMA40_RD_PAUSED         BIT(4)  /* Reading is paused */
     226                 :            : #define BCM2711_DMA40_WR_PAUSED         BIT(5)  /* Writing is paused */
     227                 :            : #define BCM2711_DMA40_DREQ_PAUSED       BIT(6)  /* Is paused by DREQ flow control */
     228                 :            : #define BCM2711_DMA40_WAITING_FOR_WRITES BIT(7)  /* Waiting for last write */
     229                 :            : #define BCM2711_DMA40_ERR               BIT(10)
     230                 :            : #define BCM2711_DMA40_QOS(x)            (((x) & 0x1f) << 16)
     231                 :            : #define BCM2711_DMA40_PANIC_QOS(x)      (((x) & 0x1f) << 20)
     232                 :            : #define BCM2711_DMA40_WAIT_FOR_WRITES   BIT(28)
     233                 :            : #define BCM2711_DMA40_DISDEBUG          BIT(29)
     234                 :            : #define BCM2711_DMA40_ABORT             BIT(30)
     235                 :            : #define BCM2711_DMA40_HALT              BIT(31)
     236                 :            : #define BCM2711_DMA40_CS_FLAGS(x) (x & (BCM2711_DMA40_QOS(15) | \
     237                 :            :                                         BCM2711_DMA40_PANIC_QOS(15) | \
     238                 :            :                                         BCM2711_DMA40_WAIT_FOR_WRITES | \
     239                 :            :                                         BCM2711_DMA40_DISDEBUG))
     240                 :            : 
     241                 :            : /* Transfer information bits */
     242                 :            : #define BCM2711_DMA40_INTEN             BIT(0)
     243                 :            : #define BCM2711_DMA40_TDMODE            BIT(1) /* 2D-Mode */
     244                 :            : #define BCM2711_DMA40_WAIT_RESP         BIT(2) /* wait for AXI write to be acked */
     245                 :            : #define BCM2711_DMA40_WAIT_RD_RESP      BIT(3) /* wait for AXI read to complete */
     246                 :            : #define BCM2711_DMA40_PER_MAP(x)        ((x & 31) << 9) /* REQ source */
     247                 :            : #define BCM2711_DMA40_S_DREQ            BIT(14) /* enable SREQ for source */
     248                 :            : #define BCM2711_DMA40_D_DREQ            BIT(15) /* enable DREQ for destination */
     249                 :            : #define BCM2711_DMA40_S_WAIT(x)         ((x & 0xff) << 16) /* add DMA read-wait cycles */
     250                 :            : #define BCM2711_DMA40_D_WAIT(x)         ((x & 0xff) << 24) /* add DMA write-wait cycles */
     251                 :            : 
     252                 :            : /* debug register bits */
     253                 :            : #define BCM2711_DMA40_DEBUG_WRITE_ERR           BIT(0)
     254                 :            : #define BCM2711_DMA40_DEBUG_FIFO_ERR            BIT(1)
     255                 :            : #define BCM2711_DMA40_DEBUG_READ_ERR            BIT(2)
     256                 :            : #define BCM2711_DMA40_DEBUG_READ_CB_ERR         BIT(3)
     257                 :            : #define BCM2711_DMA40_DEBUG_IN_ON_ERR           BIT(8)
     258                 :            : #define BCM2711_DMA40_DEBUG_ABORT_ON_ERR        BIT(9)
     259                 :            : #define BCM2711_DMA40_DEBUG_HALT_ON_ERR         BIT(10)
     260                 :            : #define BCM2711_DMA40_DEBUG_DISABLE_CLK_GATE    BIT(11)
     261                 :            : #define BCM2711_DMA40_DEBUG_RSTATE_SHIFT        14
     262                 :            : #define BCM2711_DMA40_DEBUG_RSTATE_BITS         4
     263                 :            : #define BCM2711_DMA40_DEBUG_WSTATE_SHIFT        18
     264                 :            : #define BCM2711_DMA40_DEBUG_WSTATE_BITS         4
     265                 :            : #define BCM2711_DMA40_DEBUG_RESET               BIT(23)
     266                 :            : #define BCM2711_DMA40_DEBUG_ID_SHIFT            24
     267                 :            : #define BCM2711_DMA40_DEBUG_ID_BITS             4
     268                 :            : #define BCM2711_DMA40_DEBUG_VERSION_SHIFT       28
     269                 :            : #define BCM2711_DMA40_DEBUG_VERSION_BITS        4
     270                 :            : 
     271                 :            : /* Valid only for channels 0 - 3 (11 - 14) */
     272                 :            : #define BCM2711_DMA40_CHAN(n)   (((n) + 11) << 8) /* Base address */
     273                 :            : #define BCM2711_DMA40_CHANIO(base, n) ((base) + BCM2711_DMA_CHAN(n))
     274                 :            : 
     275                 :            : /* the max dma length for different channels */
     276                 :            : #define MAX_DMA40_LEN SZ_1G
     277                 :            : 
     278                 :            : #define BCM2711_DMA40_BURST_LEN(x)      ((min(x,16) - 1) << 8)
     279                 :            : #define BCM2711_DMA40_INC               BIT(12)
     280                 :            : #define BCM2711_DMA40_SIZE_32           (0 << 13)
     281                 :            : #define BCM2711_DMA40_SIZE_64           (1 << 13)
     282                 :            : #define BCM2711_DMA40_SIZE_128          (2 << 13)
     283                 :            : #define BCM2711_DMA40_SIZE_256          (3 << 13)
     284                 :            : #define BCM2711_DMA40_IGNORE            BIT(15)
     285                 :            : #define BCM2711_DMA40_STRIDE(x)         ((x) << 16) /* For 2D mode */
     286                 :            : 
     287                 :            : #define BCM2711_DMA40_MEMCPY_FLAGS \
     288                 :            :         (BCM2711_DMA40_QOS(0) | \
     289                 :            :          BCM2711_DMA40_PANIC_QOS(0) | \
     290                 :            :          BCM2711_DMA40_WAIT_FOR_WRITES | \
     291                 :            :          BCM2711_DMA40_DISDEBUG)
     292                 :            : 
     293                 :            : #define BCM2711_DMA40_MEMCPY_XFER_INFO \
     294                 :            :         (BCM2711_DMA40_SIZE_128 | \
     295                 :            :          BCM2711_DMA40_INC | \
     296                 :            :          BCM2711_DMA40_BURST_LEN(16))
     297                 :            : 
     298                 :            : struct bcm2835_dmadev *memcpy_parent;
     299                 :            : static void __iomem *memcpy_chan;
     300                 :            : static struct bcm2711_dma40_scb *memcpy_scb;
     301                 :            : static dma_addr_t memcpy_scb_dma;
     302                 :            : DEFINE_SPINLOCK(memcpy_lock);
     303                 :            : 
     304                 :            : static const struct bcm2835_dma_cfg_data bcm2835_dma_cfg = {
     305                 :            :         .chan_40bit_mask = 0,
     306                 :            :         .dma_mask = DMA_BIT_MASK(32),
     307                 :            : };
     308                 :            : 
     309                 :            : static const struct bcm2835_dma_cfg_data bcm2711_dma_cfg = {
     310                 :            :         .chan_40bit_mask = BIT(11) | BIT(12) | BIT(13) | BIT(14),
     311                 :            :         .dma_mask = DMA_BIT_MASK(36),
     312                 :            : };
     313                 :            : 
     314                 :            : static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
     315                 :            : {
     316                 :            :         /* lite and normal channels have different max frame length */
     317                 :          3 :         return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN;
     318                 :            : }
     319                 :            : 
     320                 :            : /* how many frames of max_len size do we need to transfer len bytes */
     321                 :            : static inline size_t bcm2835_dma_frames_for_length(size_t len,
     322                 :            :                                                    size_t max_len)
     323                 :            : {
     324                 :          3 :         return DIV_ROUND_UP(len, max_len);
     325                 :            : }
     326                 :            : 
     327                 :            : static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
     328                 :            : {
     329                 :            :         return container_of(d, struct bcm2835_dmadev, ddev);
     330                 :            : }
     331                 :            : 
     332                 :            : static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
     333                 :            : {
     334                 :            :         return container_of(c, struct bcm2835_chan, vc.chan);
     335                 :            : }
     336                 :            : 
     337                 :            : static inline struct bcm2835_desc *to_bcm2835_dma_desc(
     338                 :            :                 struct dma_async_tx_descriptor *t)
     339                 :            : {
     340                 :          3 :         return container_of(t, struct bcm2835_desc, vd.tx);
     341                 :            : }
     342                 :            : 
     343                 :          0 : static inline uint32_t to_bcm2711_ti(uint32_t info)
     344                 :            : {
     345                 :          0 :         return ((info & BCM2835_DMA_INT_EN) ? BCM2711_DMA40_INTEN : 0) |
     346                 :          0 :                 ((info & BCM2835_DMA_WAIT_RESP) ? BCM2711_DMA40_WAIT_RESP : 0) |
     347                 :          0 :                 ((info & BCM2835_DMA_S_DREQ) ?
     348                 :          0 :                  (BCM2711_DMA40_S_DREQ | BCM2711_DMA40_WAIT_RD_RESP) : 0) |
     349                 :          0 :                 ((info & BCM2835_DMA_D_DREQ) ? BCM2711_DMA40_D_DREQ : 0) |
     350                 :          0 :                 BCM2711_DMA40_PER_MAP((info >> 16) & 0x1f);
     351                 :            : }
     352                 :            : 
     353                 :            : static inline uint32_t to_bcm2711_srci(uint32_t info)
     354                 :            : {
     355                 :          0 :         return ((info & BCM2835_DMA_S_INC) ? BCM2711_DMA40_INC : 0);
     356                 :            : }
     357                 :            : 
     358                 :            : static inline uint32_t to_bcm2711_dsti(uint32_t info)
     359                 :            : {
     360                 :          0 :         return ((info & BCM2835_DMA_D_INC) ? BCM2711_DMA40_INC : 0);
     361                 :            : }
     362                 :            : 
     363                 :            : static inline uint32_t to_bcm2711_cbaddr(dma_addr_t addr)
     364                 :            : {
     365                 :          0 :         BUG_ON(addr & 0x1f);
     366                 :          0 :         return (addr >> 5);
     367                 :            : }
     368                 :            : 
     369                 :          3 : static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
     370                 :            : {
     371                 :            :         size_t i;
     372                 :            : 
     373                 :          3 :         for (i = 0; i < desc->frames; i++)
     374                 :          3 :                 dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
     375                 :            :                               desc->cb_list[i].paddr);
     376                 :            : 
     377                 :          3 :         kfree(desc);
     378                 :          3 : }
     379                 :            : 
     380                 :          3 : static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
     381                 :            : {
     382                 :          3 :         bcm2835_dma_free_cb_chain(
     383                 :          3 :                 container_of(vd, struct bcm2835_desc, vd));
     384                 :          3 : }
     385                 :            : 
     386                 :          0 : static void bcm2835_dma_create_cb_set_length(
     387                 :            :         struct bcm2835_chan *c,
     388                 :            :         struct bcm2835_dma_cb *control_block,
     389                 :            :         size_t len,
     390                 :            :         size_t period_len,
     391                 :            :         size_t *total_len,
     392                 :            :         u32 finalextrainfo)
     393                 :            : {
     394                 :            :         size_t max_len = bcm2835_dma_max_frame_length(c);
     395                 :            :         uint32_t cb_len;
     396                 :            : 
     397                 :            :         /* set the length taking lite-channel limitations into account */
     398                 :          0 :         cb_len = min_t(u32, len, max_len);
     399                 :            : 
     400                 :          0 :         if (period_len) {
     401                 :            :                 /*
     402                 :            :                  * period_len means: that we need to generate
     403                 :            :                  * transfers that are terminating at every
     404                 :            :                  * multiple of period_len - this is typically
     405                 :            :                  * used to set the interrupt flag in info
     406                 :            :                  * which is required during cyclic transfers
     407                 :            :                  */
     408                 :            : 
     409                 :            :                 /* have we filled in period_length yet? */
     410                 :          0 :                 if (*total_len + cb_len < period_len) {
     411                 :            :                         /* update number of bytes in this period so far */
     412                 :          0 :                         *total_len += cb_len;
     413                 :            :                 } else {
     414                 :            :                         /* calculate the length that remains to reach period_len */
     415                 :          0 :                         cb_len = period_len - *total_len;
     416                 :            : 
     417                 :            :                         /* reset total_length for next period */
     418                 :          0 :                         *total_len = 0;
     419                 :            :                 }
     420                 :            :         }
     421                 :            : 
     422                 :          0 :         if (c->is_40bit_channel) {
     423                 :            :                 struct bcm2711_dma40_scb *scb =
     424                 :            :                         (struct bcm2711_dma40_scb *)control_block;
     425                 :            : 
     426                 :          0 :                 scb->len = cb_len;
     427                 :            :                 /* add extrainfo bits to ti */
     428                 :          0 :                 scb->ti |= to_bcm2711_ti(finalextrainfo);
     429                 :            :         } else {
     430                 :          0 :                 control_block->length = cb_len;
     431                 :            :                 /* add extrainfo bits to info */
     432                 :          0 :                 control_block->info |= finalextrainfo;
     433                 :            :         }
     434                 :          0 : }
     435                 :            : 
     436                 :          3 : static inline size_t bcm2835_dma_count_frames_for_sg(
     437                 :            :         struct bcm2835_chan *c,
     438                 :            :         struct scatterlist *sgl,
     439                 :            :         unsigned int sg_len)
     440                 :            : {
     441                 :            :         size_t frames = 0;
     442                 :            :         struct scatterlist *sgent;
     443                 :            :         unsigned int i;
     444                 :            :         size_t plength = bcm2835_dma_max_frame_length(c);
     445                 :            : 
     446                 :          3 :         for_each_sg(sgl, sgent, sg_len, i)
     447                 :          3 :                 frames += bcm2835_dma_frames_for_length(
     448                 :            :                         sg_dma_len(sgent), plength);
     449                 :            : 
     450                 :          3 :         return frames;
     451                 :            : }
     452                 :            : 
     453                 :            : /**
     454                 :            :  * bcm2835_dma_create_cb_chain - create a control block and fills data in
     455                 :            :  *
     456                 :            :  * @c:              the @bcm2835_chan for which we run this
     457                 :            :  * @direction:      the direction in which we transfer
     458                 :            :  * @cyclic:         it is a cyclic transfer
     459                 :            :  * @info:           the default info bits to apply per controlblock
     460                 :            :  * @frames:         number of controlblocks to allocate
     461                 :            :  * @src:            the src address to assign (if the S_INC bit is set
     462                 :            :  *                  in @info, then it gets incremented)
     463                 :            :  * @dst:            the dst address to assign (if the D_INC bit is set
     464                 :            :  *                  in @info, then it gets incremented)
     465                 :            :  * @buf_len:        the full buffer length (may also be 0)
     466                 :            :  * @period_len:     the period length when to apply @finalextrainfo
     467                 :            :  *                  in addition to the last transfer
     468                 :            :  *                  this will also break some control-blocks early
     469                 :            :  * @finalextrainfo: additional bits in last controlblock
     470                 :            :  *                  (or when period_len is reached in case of cyclic)
     471                 :            :  * @gfp:            the GFP flag to use for allocation
     472                 :            :  */
     473                 :          3 : static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
     474                 :            :         struct bcm2835_chan *c, enum dma_transfer_direction direction,
     475                 :            :         bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
     476                 :            :         dma_addr_t src, dma_addr_t dst, size_t buf_len,
     477                 :            :         size_t period_len, gfp_t gfp)
     478                 :            : {
     479                 :            :         size_t len = buf_len, total_len;
     480                 :            :         size_t frame;
     481                 :            :         struct bcm2835_desc *d;
     482                 :            :         struct bcm2835_cb_entry *cb_entry;
     483                 :            :         struct bcm2835_dma_cb *control_block;
     484                 :            : 
     485                 :          3 :         if (!frames)
     486                 :            :                 return NULL;
     487                 :            : 
     488                 :            :         /* allocate and setup the descriptor. */
     489                 :          3 :         d = kzalloc(struct_size(d, cb_list, frames), gfp);
     490                 :          3 :         if (!d)
     491                 :            :                 return NULL;
     492                 :            : 
     493                 :          3 :         d->c = c;
     494                 :          3 :         d->dir = direction;
     495                 :          3 :         d->cyclic = cyclic;
     496                 :            : 
     497                 :            :         /*
     498                 :            :          * Iterate over all frames, create a control block
     499                 :            :          * for each frame and link them together.
     500                 :            :          */
     501                 :          3 :         for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
     502                 :            :                 cb_entry = &d->cb_list[frame];
     503                 :          3 :                 cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
     504                 :            :                                               &cb_entry->paddr);
     505                 :          3 :                 if (!cb_entry->cb)
     506                 :            :                         goto error_cb;
     507                 :            : 
     508                 :            :                 /* fill in the control block */
     509                 :            :                 control_block = cb_entry->cb;
     510                 :          3 :                 if (c->is_40bit_channel) {
     511                 :            :                         struct bcm2711_dma40_scb *scb =
     512                 :            :                                 (struct bcm2711_dma40_scb *)control_block;
     513                 :          0 :                         scb->ti = to_bcm2711_ti(info);
     514                 :          0 :                         scb->src = lower_32_bits(src);
     515                 :          0 :                         scb->srci= upper_32_bits(src) | to_bcm2711_srci(info);
     516                 :          0 :                         scb->dst = lower_32_bits(dst);
     517                 :          0 :                         scb->dsti = upper_32_bits(dst) | to_bcm2711_dsti(info);
     518                 :          0 :                         scb->next_cb = 0;
     519                 :            :                 } else {
     520                 :          3 :                         control_block->info = info;
     521                 :          3 :                         control_block->src = src;
     522                 :          3 :                         control_block->dst = dst;
     523                 :          3 :                         control_block->stride = 0;
     524                 :          3 :                         control_block->next = 0;
     525                 :            :                 }
     526                 :            : 
     527                 :            :                 /* set up length in control_block if requested */
     528                 :          3 :                 if (buf_len) {
     529                 :            :                         /* calculate length honoring period_length */
     530                 :          0 :                         bcm2835_dma_create_cb_set_length(
     531                 :            :                                 c, control_block,
     532                 :            :                                 len, period_len, &total_len,
     533                 :            :                                 cyclic ? finalextrainfo : 0);
     534                 :            : 
     535                 :            :                         /* calculate new remaining length */
     536                 :          0 :                         len -= control_block->length;
     537                 :            :                 }
     538                 :            : 
     539                 :            :                 /* link this the last controlblock */
     540                 :          3 :                 if (frame && c->is_40bit_channel)
     541                 :            :                         ((struct bcm2711_dma40_scb *)
     542                 :          0 :                          d->cb_list[frame - 1].cb)->next_cb =
     543                 :          0 :                                 to_bcm2711_cbaddr(cb_entry->paddr);
     544                 :          3 :                 if (frame && !c->is_40bit_channel)
     545                 :          3 :                         d->cb_list[frame - 1].cb->next = cb_entry->paddr;
     546                 :            : 
     547                 :            :                 /* update src and dst and length */
     548                 :          3 :                 if (src && (info & BCM2835_DMA_S_INC))
     549                 :          0 :                         src += control_block->length;
     550                 :          3 :                 if (dst && (info & BCM2835_DMA_D_INC))
     551                 :          0 :                         dst += control_block->length;
     552                 :            : 
     553                 :            :                 /* Length of total transfer */
     554                 :          3 :                 if (c->is_40bit_channel)
     555                 :          0 :                         d->size += ((struct bcm2711_dma40_scb *)control_block)->len;
     556                 :            :                 else
     557                 :          3 :                         d->size += control_block->length;
     558                 :            :         }
     559                 :            : 
     560                 :            :         /* the last frame requires extra flags */
     561                 :          3 :         if (c->is_40bit_channel) {
     562                 :          0 :                 struct bcm2711_dma40_scb *scb =
     563                 :          0 :                         (struct bcm2711_dma40_scb *)d->cb_list[d->frames-1].cb;
     564                 :            : 
     565                 :          0 :                 scb->ti |= to_bcm2711_ti(finalextrainfo);
     566                 :            :         } else {
     567                 :          3 :                 d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
     568                 :            :         }
     569                 :            : 
     570                 :            :         /* detect a size missmatch */
     571                 :          3 :         if (buf_len && (d->size != buf_len))
     572                 :            :                 goto error_cb;
     573                 :            : 
     574                 :            :         return d;
     575                 :            : error_cb:
     576                 :          0 :         bcm2835_dma_free_cb_chain(d);
     577                 :            : 
     578                 :          0 :         return NULL;
     579                 :            : }
     580                 :            : 
     581                 :          3 : static void bcm2835_dma_fill_cb_chain_with_sg(
     582                 :            :         struct bcm2835_chan *c,
     583                 :            :         enum dma_transfer_direction direction,
     584                 :            :         struct bcm2835_cb_entry *cb,
     585                 :            :         struct scatterlist *sgl,
     586                 :            :         unsigned int sg_len)
     587                 :            : {
     588                 :            :         size_t len, max_len;
     589                 :            :         unsigned int i;
     590                 :            :         dma_addr_t addr;
     591                 :            :         struct scatterlist *sgent;
     592                 :            : 
     593                 :            :         max_len = bcm2835_dma_max_frame_length(c);
     594                 :          3 :         for_each_sg(sgl, sgent, sg_len, i) {
     595                 :          3 :                 if (c->is_40bit_channel) {
     596                 :            :                         struct bcm2711_dma40_scb *scb;
     597                 :            : 
     598                 :          0 :                         for (addr = sg_dma_address(sgent),
     599                 :          0 :                                      len = sg_dma_len(sgent);
     600                 :            :                                      len > 0;
     601                 :          0 :                              addr += scb->len, len -= scb->len, cb++) {
     602                 :          0 :                                 scb = (struct bcm2711_dma40_scb *)cb->cb;
     603                 :          0 :                                 if (direction == DMA_DEV_TO_MEM) {
     604                 :          0 :                                         scb->dst = lower_32_bits(addr);
     605                 :          0 :                                         scb->dsti = upper_32_bits(addr) | BCM2711_DMA40_INC;
     606                 :            :                                 } else {
     607                 :          0 :                                         scb->src = lower_32_bits(addr);
     608                 :          0 :                                         scb->srci = upper_32_bits(addr) | BCM2711_DMA40_INC;
     609                 :            :                                 }
     610                 :          0 :                                 scb->len = min(len, max_len);
     611                 :            :                         }
     612                 :            :                 } else {
     613                 :          3 :                         for (addr = sg_dma_address(sgent),
     614                 :          3 :                                      len = sg_dma_len(sgent);
     615                 :            :                              len > 0;
     616                 :          3 :                              addr += cb->cb->length, len -= cb->cb->length,
     617                 :          3 :                              cb++) {
     618                 :          3 :                                 if (direction == DMA_DEV_TO_MEM)
     619                 :          3 :                                         cb->cb->dst = addr;
     620                 :            :                                 else
     621                 :          3 :                                         cb->cb->src = addr;
     622                 :          3 :                                 cb->cb->length = min(len, max_len);
     623                 :            :                         }
     624                 :            :                 }
     625                 :            :         }
     626                 :          3 : }
     627                 :            : 
     628                 :          0 : static void bcm2835_dma_abort(struct bcm2835_chan *c)
     629                 :            : {
     630                 :          0 :         void __iomem *chan_base = c->chan_base;
     631                 :            :         long int timeout = 10000;
     632                 :            :         u32 wait_mask = BCM2835_DMA_WAITING_FOR_WRITES;
     633                 :            : 
     634                 :          0 :         if (c->is_40bit_channel)
     635                 :            :                 wait_mask = BCM2711_DMA40_WAITING_FOR_WRITES;
     636                 :            : 
     637                 :            :         /*
     638                 :            :          * A zero control block address means the channel is idle.
     639                 :            :          * (The ACTIVE flag in the CS register is not a reliable indicator.)
     640                 :            :          */
     641                 :          0 :         if (!readl(chan_base + BCM2835_DMA_ADDR))
     642                 :          0 :                 return;
     643                 :            : 
     644                 :            :         /* Write 0 to the active bit - Pause the DMA */
     645                 :          0 :         writel(0, chan_base + BCM2835_DMA_CS);
     646                 :            : 
     647                 :            :         /* Wait for any current AXI transfer to complete */
     648                 :          0 :         while ((readl(chan_base + BCM2835_DMA_CS) & wait_mask) && --timeout)
     649                 :          0 :                 cpu_relax();
     650                 :            : 
     651                 :            :         /* Peripheral might be stuck and fail to signal AXI write responses */
     652                 :          0 :         if (!timeout)
     653                 :          0 :                 dev_err(c->vc.chan.device->dev,
     654                 :            :                         "failed to complete outstanding writes\n");
     655                 :            : 
     656                 :          0 :         writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
     657                 :            : }
     658                 :            : 
     659                 :          3 : static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
     660                 :            : {
     661                 :            :         struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
     662                 :            :         struct bcm2835_desc *d;
     663                 :            : 
     664                 :          3 :         if (!vd) {
     665                 :          3 :                 c->desc = NULL;
     666                 :          3 :                 return;
     667                 :            :         }
     668                 :            : 
     669                 :            :         list_del(&vd->node);
     670                 :            : 
     671                 :          3 :         c->desc = d = to_bcm2835_dma_desc(&vd->tx);
     672                 :            : 
     673                 :          3 :         if (c->is_40bit_channel) {
     674                 :          0 :                 writel(to_bcm2711_cbaddr(d->cb_list[0].paddr),
     675                 :            :                        c->chan_base + BCM2711_DMA40_CB);
     676                 :          0 :                 writel(BCM2711_DMA40_ACTIVE | BCM2711_DMA40_CS_FLAGS(c->dreq),
     677                 :            :                        c->chan_base + BCM2711_DMA40_CS);
     678                 :            :         } else {
     679                 :          3 :                 writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
     680                 :          3 :                 writel(BCM2835_DMA_ACTIVE | BCM2835_DMA_CS_FLAGS(c->dreq),
     681                 :            :                        c->chan_base + BCM2835_DMA_CS);
     682                 :            :         }
     683                 :            : }
     684                 :            : 
     685                 :          3 : static irqreturn_t bcm2835_dma_callback(int irq, void *data)
     686                 :            : {
     687                 :            :         struct bcm2835_chan *c = data;
     688                 :            :         struct bcm2835_desc *d;
     689                 :            :         unsigned long flags;
     690                 :            : 
     691                 :            :         /* check the shared interrupt */
     692                 :          3 :         if (c->irq_flags & IRQF_SHARED) {
     693                 :            :                 /* check if the interrupt is enabled */
     694                 :          0 :                 flags = readl(c->chan_base + BCM2835_DMA_CS);
     695                 :            :                 /* if not set then we are not the reason for the irq */
     696                 :          0 :                 if (!(flags & BCM2835_DMA_INT))
     697                 :            :                         return IRQ_NONE;
     698                 :            :         }
     699                 :            : 
     700                 :          3 :         spin_lock_irqsave(&c->vc.lock, flags);
     701                 :            : 
     702                 :            :         /*
     703                 :            :          * Clear the INT flag to receive further interrupts. Keep the channel
     704                 :            :          * active in case the descriptor is cyclic or in case the client has
     705                 :            :          * already terminated the descriptor and issued a new one. (May happen
     706                 :            :          * if this IRQ handler is threaded.) If the channel is finished, it
     707                 :            :          * will remain idle despite the ACTIVE flag being set.
     708                 :            :          */
     709                 :          3 :         writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE,
     710                 :            :                c->chan_base + BCM2835_DMA_CS);
     711                 :            : 
     712                 :          3 :         d = c->desc;
     713                 :            : 
     714                 :          3 :         if (d) {
     715                 :          3 :                 if (d->cyclic) {
     716                 :            :                         /* call the cyclic callback */
     717                 :          0 :                         vchan_cyclic_callback(&d->vd);
     718                 :          3 :                 } else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
     719                 :          3 :                         vchan_cookie_complete(&c->desc->vd);
     720                 :          3 :                         bcm2835_dma_start_desc(c);
     721                 :            :                 }
     722                 :            :         }
     723                 :            : 
     724                 :            :         spin_unlock_irqrestore(&c->vc.lock, flags);
     725                 :            : 
     726                 :          3 :         return IRQ_HANDLED;
     727                 :            : }
     728                 :            : 
     729                 :          3 : static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
     730                 :            : {
     731                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     732                 :          3 :         struct device *dev = c->vc.chan.device->dev;
     733                 :            : 
     734                 :            :         dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
     735                 :            : 
     736                 :            :         /*
     737                 :            :          * Control blocks are 256 bit in length and must start at a 256 bit
     738                 :            :          * (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1).
     739                 :            :          */
     740                 :          3 :         c->cb_pool = dma_pool_create(dev_name(dev), dev,
     741                 :            :                                      sizeof(struct bcm2835_dma_cb), 32, 0);
     742                 :          3 :         if (!c->cb_pool) {
     743                 :          0 :                 dev_err(dev, "unable to allocate descriptor pool\n");
     744                 :          0 :                 return -ENOMEM;
     745                 :            :         }
     746                 :            : 
     747                 :          3 :         return request_irq(c->irq_number, bcm2835_dma_callback,
     748                 :          3 :                            c->irq_flags, "DMA IRQ", c);
     749                 :            : }
     750                 :            : 
     751                 :          3 : static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
     752                 :            : {
     753                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     754                 :            : 
     755                 :          3 :         vchan_free_chan_resources(&c->vc);
     756                 :          3 :         free_irq(c->irq_number, c);
     757                 :          3 :         dma_pool_destroy(c->cb_pool);
     758                 :            : 
     759                 :            :         dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
     760                 :          3 : }
     761                 :            : 
     762                 :            : static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
     763                 :            : {
     764                 :          0 :         return d->size;
     765                 :            : }
     766                 :            : 
     767                 :          0 : static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
     768                 :            : {
     769                 :            :         unsigned int i;
     770                 :            :         size_t size;
     771                 :            : 
     772                 :          0 :         for (size = i = 0; i < d->frames; i++) {
     773                 :          0 :                 struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
     774                 :          0 :                 size_t this_size = control_block->length;
     775                 :            :                 dma_addr_t dma;
     776                 :            : 
     777                 :          0 :                 if (d->dir == DMA_DEV_TO_MEM)
     778                 :          0 :                         dma = control_block->dst;
     779                 :            :                 else
     780                 :          0 :                         dma = control_block->src;
     781                 :            : 
     782                 :          0 :                 if (size)
     783                 :          0 :                         size += this_size;
     784                 :          0 :                 else if (addr >= dma && addr < dma + this_size)
     785                 :          0 :                         size += dma + this_size - addr;
     786                 :            :         }
     787                 :            : 
     788                 :          0 :         return size;
     789                 :            : }
     790                 :            : 
     791                 :          0 : static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
     792                 :            :         dma_cookie_t cookie, struct dma_tx_state *txstate)
     793                 :            : {
     794                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     795                 :            :         struct virt_dma_desc *vd;
     796                 :            :         enum dma_status ret;
     797                 :            :         unsigned long flags;
     798                 :            : 
     799                 :          0 :         ret = dma_cookie_status(chan, cookie, txstate);
     800                 :          0 :         if (ret == DMA_COMPLETE || !txstate)
     801                 :            :                 return ret;
     802                 :            : 
     803                 :          0 :         spin_lock_irqsave(&c->vc.lock, flags);
     804                 :          0 :         vd = vchan_find_desc(&c->vc, cookie);
     805                 :          0 :         if (vd) {
     806                 :          0 :                 txstate->residue =
     807                 :            :                         bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
     808                 :          0 :         } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
     809                 :            :                 struct bcm2835_desc *d = c->desc;
     810                 :            :                 dma_addr_t pos;
     811                 :            : 
     812                 :          0 :                 if (d->dir == DMA_MEM_TO_DEV && c->is_40bit_channel)
     813                 :          0 :                         pos = readl(c->chan_base + BCM2711_DMA40_SRC) +
     814                 :          0 :                                 ((readl(c->chan_base + BCM2711_DMA40_SRCI) &
     815                 :          0 :                                   0xff) << 8);
     816                 :          0 :                 else if (d->dir == DMA_MEM_TO_DEV && !c->is_40bit_channel)
     817                 :          0 :                         pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
     818                 :          0 :                 else if (d->dir == DMA_DEV_TO_MEM && c->is_40bit_channel)
     819                 :          0 :                         pos = readl(c->chan_base + BCM2711_DMA40_DEST) +
     820                 :          0 :                                 ((readl(c->chan_base + BCM2711_DMA40_DESTI) &
     821                 :          0 :                                   0xff) << 8);
     822                 :          0 :                 else if (d->dir == DMA_DEV_TO_MEM && !c->is_40bit_channel)
     823                 :          0 :                         pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
     824                 :            :                 else
     825                 :            :                         pos = 0;
     826                 :            : 
     827                 :          0 :                 txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
     828                 :            :         } else {
     829                 :          0 :                 txstate->residue = 0;
     830                 :            :         }
     831                 :            : 
     832                 :            :         spin_unlock_irqrestore(&c->vc.lock, flags);
     833                 :            : 
     834                 :          0 :         return ret;
     835                 :            : }
     836                 :            : 
     837                 :          3 : static void bcm2835_dma_issue_pending(struct dma_chan *chan)
     838                 :            : {
     839                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     840                 :            :         unsigned long flags;
     841                 :            : 
     842                 :          3 :         spin_lock_irqsave(&c->vc.lock, flags);
     843                 :          3 :         if (vchan_issue_pending(&c->vc) && !c->desc)
     844                 :          3 :                 bcm2835_dma_start_desc(c);
     845                 :            : 
     846                 :            :         spin_unlock_irqrestore(&c->vc.lock, flags);
     847                 :          3 : }
     848                 :            : 
     849                 :          0 : static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
     850                 :            :         struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
     851                 :            :         size_t len, unsigned long flags)
     852                 :            : {
     853                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     854                 :            :         struct bcm2835_desc *d;
     855                 :            :         u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
     856                 :          0 :         u32 extra = BCM2835_DMA_INT_EN | WAIT_RESP(c->dreq);
     857                 :            :         size_t max_len = bcm2835_dma_max_frame_length(c);
     858                 :            :         size_t frames;
     859                 :            : 
     860                 :            :         /* if src, dst or len is not given return with an error */
     861                 :          0 :         if (!src || !dst || !len)
     862                 :            :                 return NULL;
     863                 :            : 
     864                 :            :         /* calculate number of frames */
     865                 :            :         frames = bcm2835_dma_frames_for_length(len, max_len);
     866                 :            : 
     867                 :            :         /* allocate the CB chain - this also fills in the pointers */
     868                 :          0 :         d = bcm2835_dma_create_cb_chain(c, DMA_MEM_TO_MEM, false,
     869                 :            :                                         info, extra, frames,
     870                 :            :                                         src, dst, len, 0, GFP_KERNEL);
     871                 :          0 :         if (!d)
     872                 :            :                 return NULL;
     873                 :            : 
     874                 :          0 :         return vchan_tx_prep(&c->vc, &d->vd, flags);
     875                 :            : }
     876                 :            : 
     877                 :          3 : static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
     878                 :            :         struct dma_chan *chan,
     879                 :            :         struct scatterlist *sgl, unsigned int sg_len,
     880                 :            :         enum dma_transfer_direction direction,
     881                 :            :         unsigned long flags, void *context)
     882                 :            : {
     883                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     884                 :            :         struct bcm2835_desc *d;
     885                 :            :         dma_addr_t src = 0, dst = 0;
     886                 :          3 :         u32 info = WAIT_RESP(c->dreq);
     887                 :            :         u32 extra = BCM2835_DMA_INT_EN;
     888                 :            :         size_t frames;
     889                 :            : 
     890                 :          3 :         if (!is_slave_direction(direction)) {
     891                 :          0 :                 dev_err(chan->device->dev,
     892                 :            :                         "%s: bad direction?\n", __func__);
     893                 :          0 :                 return NULL;
     894                 :            :         }
     895                 :            : 
     896                 :          3 :         if (c->dreq != 0)
     897                 :          3 :                 info |= BCM2835_DMA_PER_MAP(c->dreq);
     898                 :            : 
     899                 :          3 :         if (direction == DMA_DEV_TO_MEM) {
     900                 :          3 :                 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
     901                 :            :                         return NULL;
     902                 :          3 :                 src = c->cfg.src_addr;
     903                 :            :                 /*
     904                 :            :                  * One would think it ought to be possible to get the physical
     905                 :            :                  * to dma address mapping information from the dma-ranges DT
     906                 :            :                  * property, but I've not found a way yet that doesn't involve
     907                 :            :                  * open-coding the whole thing.
     908                 :            :                  */
     909                 :            :                 if (c->is_40bit_channel)
     910                 :            :                     src |= 0x400000000ull;
     911                 :          3 :                 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
     912                 :            :         } else {
     913                 :          3 :                 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
     914                 :            :                         return NULL;
     915                 :          3 :                 dst = c->cfg.dst_addr;
     916                 :            :                 if (c->is_40bit_channel)
     917                 :            :                     dst |= 0x400000000ull;
     918                 :          3 :                 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
     919                 :            :         }
     920                 :            : 
     921                 :            :         /* count frames in sg list */
     922                 :          3 :         frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
     923                 :            : 
     924                 :            :         /* allocate the CB chain */
     925                 :          3 :         d = bcm2835_dma_create_cb_chain(c, direction, false,
     926                 :            :                                         info, extra,
     927                 :            :                                         frames, src, dst, 0, 0,
     928                 :            :                                         GFP_NOWAIT);
     929                 :          3 :         if (!d)
     930                 :            :                 return NULL;
     931                 :            : 
     932                 :            :         /* fill in frames with scatterlist pointers */
     933                 :          3 :         bcm2835_dma_fill_cb_chain_with_sg(c, direction, d->cb_list,
     934                 :            :                                           sgl, sg_len);
     935                 :            : 
     936                 :          3 :         return vchan_tx_prep(&c->vc, &d->vd, flags);
     937                 :            : }
     938                 :            : 
     939                 :          0 : static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
     940                 :            :         struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
     941                 :            :         size_t period_len, enum dma_transfer_direction direction,
     942                 :            :         unsigned long flags)
     943                 :            : {
     944                 :          0 :         struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
     945                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
     946                 :            :         struct bcm2835_desc *d;
     947                 :            :         dma_addr_t src, dst;
     948                 :          0 :         u32 info = WAIT_RESP(c->dreq);
     949                 :            :         u32 extra = 0;
     950                 :            :         size_t max_len = bcm2835_dma_max_frame_length(c);
     951                 :            :         size_t frames;
     952                 :            : 
     953                 :            :         /* Grab configuration */
     954                 :          0 :         if (!is_slave_direction(direction)) {
     955                 :          0 :                 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
     956                 :          0 :                 return NULL;
     957                 :            :         }
     958                 :            : 
     959                 :          0 :         if (!buf_len) {
     960                 :          0 :                 dev_err(chan->device->dev,
     961                 :            :                         "%s: bad buffer length (= 0)\n", __func__);
     962                 :          0 :                 return NULL;
     963                 :            :         }
     964                 :            : 
     965                 :          0 :         if (flags & DMA_PREP_INTERRUPT)
     966                 :            :                 extra |= BCM2835_DMA_INT_EN;
     967                 :            :         else
     968                 :            :                 period_len = buf_len;
     969                 :            : 
     970                 :            :         /*
     971                 :            :          * warn if buf_len is not a multiple of period_len - this may leed
     972                 :            :          * to unexpected latencies for interrupts and thus audiable clicks
     973                 :            :          */
     974                 :          0 :         if (buf_len % period_len)
     975                 :          0 :                 dev_warn_once(chan->device->dev,
     976                 :            :                               "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
     977                 :            :                               __func__, buf_len, period_len);
     978                 :            : 
     979                 :            :         /* Setup DREQ channel */
     980                 :          0 :         if (c->dreq != 0)
     981                 :          0 :                 info |= BCM2835_DMA_PER_MAP(c->dreq);
     982                 :            : 
     983                 :          0 :         if (direction == DMA_DEV_TO_MEM) {
     984                 :          0 :                 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
     985                 :            :                         return NULL;
     986                 :          0 :                 src = c->cfg.src_addr;
     987                 :            :                 if (c->is_40bit_channel)
     988                 :            :                     src |= 0x400000000ull;
     989                 :            :                 dst = buf_addr;
     990                 :          0 :                 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
     991                 :            :         } else {
     992                 :          0 :                 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
     993                 :            :                         return NULL;
     994                 :          0 :                 dst = c->cfg.dst_addr;
     995                 :            :                 if (c->is_40bit_channel)
     996                 :            :                     dst |= 0x400000000ull;
     997                 :            :                 src = buf_addr;
     998                 :          0 :                 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
     999                 :            : 
    1000                 :            :                 /* non-lite channels can write zeroes w/o accessing memory */
    1001                 :          0 :                 if (buf_addr == od->zero_page && !c->is_lite_channel)
    1002                 :          0 :                         info |= BCM2835_DMA_S_IGNORE;
    1003                 :            :         }
    1004                 :            : 
    1005                 :            :         /* calculate number of frames */
    1006                 :          0 :         frames = /* number of periods */
    1007                 :          0 :                  DIV_ROUND_UP(buf_len, period_len) *
    1008                 :            :                  /* number of frames per period */
    1009                 :            :                  bcm2835_dma_frames_for_length(period_len, max_len);
    1010                 :            : 
    1011                 :            :         /*
    1012                 :            :          * allocate the CB chain
    1013                 :            :          * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
    1014                 :            :          * implementation calls prep_dma_cyclic with interrupts disabled.
    1015                 :            :          */
    1016                 :          0 :         d = bcm2835_dma_create_cb_chain(c, direction, true,
    1017                 :            :                                         info, extra,
    1018                 :            :                                         frames, src, dst, buf_len,
    1019                 :            :                                         period_len, GFP_NOWAIT);
    1020                 :          0 :         if (!d)
    1021                 :            :                 return NULL;
    1022                 :            : 
    1023                 :            :         /* wrap around into a loop */
    1024                 :          0 :         if (c->is_40bit_channel)
    1025                 :            :                 ((struct bcm2711_dma40_scb *)
    1026                 :          0 :                  d->cb_list[frames - 1].cb)->next_cb =
    1027                 :          0 :                         to_bcm2711_cbaddr(d->cb_list[0].paddr);
    1028                 :            :         else
    1029                 :          0 :                 d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
    1030                 :            : 
    1031                 :          0 :         return vchan_tx_prep(&c->vc, &d->vd, flags);
    1032                 :            : }
    1033                 :            : 
    1034                 :          3 : static int bcm2835_dma_slave_config(struct dma_chan *chan,
    1035                 :            :                                     struct dma_slave_config *cfg)
    1036                 :            : {
    1037                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
    1038                 :            : 
    1039                 :          3 :         c->cfg = *cfg;
    1040                 :            : 
    1041                 :          3 :         return 0;
    1042                 :            : }
    1043                 :            : 
    1044                 :          0 : static int bcm2835_dma_terminate_all(struct dma_chan *chan)
    1045                 :            : {
    1046                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
    1047                 :            :         unsigned long flags;
    1048                 :          0 :         LIST_HEAD(head);
    1049                 :            : 
    1050                 :          0 :         spin_lock_irqsave(&c->vc.lock, flags);
    1051                 :            : 
    1052                 :            :         /* stop DMA activity */
    1053                 :          0 :         if (c->desc) {
    1054                 :          0 :                 if (c->desc->vd.tx.flags & DMA_PREP_INTERRUPT)
    1055                 :          0 :                         vchan_terminate_vdesc(&c->desc->vd);
    1056                 :            :                 else
    1057                 :          0 :                         vchan_vdesc_fini(&c->desc->vd);
    1058                 :          0 :                 c->desc = NULL;
    1059                 :          0 :                 bcm2835_dma_abort(c);
    1060                 :            :         }
    1061                 :            : 
    1062                 :          0 :         vchan_get_all_descriptors(&c->vc, &head);
    1063                 :            :         spin_unlock_irqrestore(&c->vc.lock, flags);
    1064                 :          0 :         vchan_dma_desc_free_list(&c->vc, &head);
    1065                 :            : 
    1066                 :          0 :         return 0;
    1067                 :            : }
    1068                 :            : 
    1069                 :          3 : static void bcm2835_dma_synchronize(struct dma_chan *chan)
    1070                 :            : {
    1071                 :            :         struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
    1072                 :            : 
    1073                 :          3 :         vchan_synchronize(&c->vc);
    1074                 :          3 : }
    1075                 :            : 
    1076                 :          3 : static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
    1077                 :            :                                  int irq, unsigned int irq_flags)
    1078                 :            : {
    1079                 :            :         struct bcm2835_chan *c;
    1080                 :            : 
    1081                 :          3 :         c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
    1082                 :          3 :         if (!c)
    1083                 :            :                 return -ENOMEM;
    1084                 :            : 
    1085                 :          3 :         c->vc.desc_free = bcm2835_dma_desc_free;
    1086                 :          3 :         vchan_init(&c->vc, &d->ddev);
    1087                 :            : 
    1088                 :          3 :         c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
    1089                 :          3 :         c->ch = chan_id;
    1090                 :          3 :         c->irq_number = irq;
    1091                 :          3 :         c->irq_flags = irq_flags;
    1092                 :            : 
    1093                 :            :         /* check for 40bit and lite channels */
    1094                 :          3 :         if (d->cfg_data->chan_40bit_mask & BIT(chan_id))
    1095                 :          0 :                 c->is_40bit_channel = true;
    1096                 :          3 :         else if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
    1097                 :            :                  BCM2835_DMA_DEBUG_LITE)
    1098                 :          2 :                 c->is_lite_channel = true;
    1099                 :            : 
    1100                 :            :         return 0;
    1101                 :            : }
    1102                 :            : 
    1103                 :          0 : static void bcm2835_dma_free(struct bcm2835_dmadev *od)
    1104                 :            : {
    1105                 :            :         struct bcm2835_chan *c, *next;
    1106                 :            : 
    1107                 :          0 :         list_for_each_entry_safe(c, next, &od->ddev.channels,
    1108                 :            :                                  vc.chan.device_node) {
    1109                 :            :                 list_del(&c->vc.chan.device_node);
    1110                 :          0 :                 tasklet_kill(&c->vc.task);
    1111                 :            :         }
    1112                 :            : 
    1113                 :          0 :         dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
    1114                 :            :                              DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
    1115                 :          0 : }
    1116                 :            : 
    1117                 :          0 : int bcm2711_dma40_memcpy_init(void)
    1118                 :            : {
    1119                 :          0 :         if (!memcpy_parent)
    1120                 :            :                 return -EPROBE_DEFER;
    1121                 :            : 
    1122                 :          0 :         if (!memcpy_chan)
    1123                 :            :                 return -EINVAL;
    1124                 :            : 
    1125                 :          0 :         if (!memcpy_scb)
    1126                 :            :                 return -ENOMEM;
    1127                 :            : 
    1128                 :          0 :         return 0;
    1129                 :            : }
    1130                 :            : EXPORT_SYMBOL(bcm2711_dma40_memcpy_init);
    1131                 :            : 
    1132                 :          0 : void bcm2711_dma40_memcpy(dma_addr_t dst, dma_addr_t src, size_t size)
    1133                 :            : {
    1134                 :          0 :         struct bcm2711_dma40_scb *scb = memcpy_scb;
    1135                 :            :         unsigned long flags;
    1136                 :            : 
    1137                 :          0 :         if (!scb) {
    1138                 :          0 :                 pr_err("bcm2711_dma40_memcpy not initialised!\n");
    1139                 :          0 :                 return;
    1140                 :            :         }
    1141                 :            : 
    1142                 :          0 :         spin_lock_irqsave(&memcpy_lock, flags);
    1143                 :            : 
    1144                 :          0 :         scb->ti = 0;
    1145                 :          0 :         scb->src = lower_32_bits(src);
    1146                 :          0 :         scb->srci = upper_32_bits(src) | BCM2711_DMA40_MEMCPY_XFER_INFO;
    1147                 :          0 :         scb->dst = lower_32_bits(dst);
    1148                 :          0 :         scb->dsti = upper_32_bits(dst) | BCM2711_DMA40_MEMCPY_XFER_INFO;
    1149                 :          0 :         scb->len = size;
    1150                 :          0 :         scb->next_cb = 0;
    1151                 :            : 
    1152                 :          0 :         writel((u32)(memcpy_scb_dma >> 5), memcpy_chan + BCM2711_DMA40_CB);
    1153                 :          0 :         writel(BCM2711_DMA40_MEMCPY_FLAGS + BCM2711_DMA40_ACTIVE,
    1154                 :            :                memcpy_chan + BCM2711_DMA40_CS);
    1155                 :            : 
    1156                 :            :         /* Poll for completion */
    1157                 :          0 :         while (!(readl(memcpy_chan + BCM2711_DMA40_CS) & BCM2711_DMA40_END))
    1158                 :          0 :                 cpu_relax();
    1159                 :            : 
    1160                 :          0 :         writel(BCM2711_DMA40_END, memcpy_chan + BCM2711_DMA40_CS);
    1161                 :            : 
    1162                 :            :         spin_unlock_irqrestore(&memcpy_lock, flags);
    1163                 :            : }
    1164                 :            : EXPORT_SYMBOL(bcm2711_dma40_memcpy);
    1165                 :            : 
    1166                 :            : static const struct of_device_id bcm2835_dma_of_match[] = {
    1167                 :            :         { .compatible = "brcm,bcm2835-dma", .data = &bcm2835_dma_cfg },
    1168                 :            :         { .compatible = "brcm,bcm2711-dma", .data = &bcm2711_dma_cfg },
    1169                 :            :         {},
    1170                 :            : };
    1171                 :            : MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
    1172                 :            : 
    1173                 :          3 : static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
    1174                 :            :                                            struct of_dma *ofdma)
    1175                 :            : {
    1176                 :          3 :         struct bcm2835_dmadev *d = ofdma->of_dma_data;
    1177                 :            :         struct dma_chan *chan;
    1178                 :            : 
    1179                 :          3 :         chan = dma_get_any_slave_channel(&d->ddev);
    1180                 :          3 :         if (!chan)
    1181                 :            :                 return NULL;
    1182                 :            : 
    1183                 :            :         /* Set DREQ from param */
    1184                 :          3 :         to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
    1185                 :            : 
    1186                 :          3 :         return chan;
    1187                 :            : }
    1188                 :            : 
    1189                 :          3 : static int bcm2835_dma_probe(struct platform_device *pdev)
    1190                 :            : {
    1191                 :            :         const struct bcm2835_dma_cfg_data *cfg_data;
    1192                 :            :         const struct of_device_id *of_id;
    1193                 :            :         struct bcm2835_dmadev *od;
    1194                 :            :         struct resource *res;
    1195                 :            :         void __iomem *base;
    1196                 :            :         int rc;
    1197                 :            :         int i, j;
    1198                 :            :         int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
    1199                 :            :         int irq_flags;
    1200                 :            :         uint32_t chans_available;
    1201                 :            :         char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
    1202                 :            :         int chan_count, chan_start, chan_end;
    1203                 :            : 
    1204                 :          3 :         of_id = of_match_node(bcm2835_dma_of_match, pdev->dev.of_node);
    1205                 :          3 :         if (!of_id) {
    1206                 :          0 :                 dev_err(&pdev->dev, "Failed to match compatible string\n");
    1207                 :          0 :                 return -EINVAL;
    1208                 :            :         }
    1209                 :            : 
    1210                 :          3 :         cfg_data = of_id->data;
    1211                 :            : 
    1212                 :          3 :         if (!pdev->dev.dma_mask)
    1213                 :          0 :                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
    1214                 :            : 
    1215                 :          3 :         rc = dma_set_mask_and_coherent(&pdev->dev, cfg_data->dma_mask);
    1216                 :          3 :         if (rc) {
    1217                 :          0 :                 dev_err(&pdev->dev, "Unable to set DMA mask\n");
    1218                 :          0 :                 return rc;
    1219                 :            :         }
    1220                 :            : 
    1221                 :            :         od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
    1222                 :          3 :         if (!od)
    1223                 :            :                 return -ENOMEM;
    1224                 :            : 
    1225                 :          3 :         pdev->dev.dma_parms = &od->dma_parms;
    1226                 :            :         dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
    1227                 :            : 
    1228                 :          3 :         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    1229                 :          3 :         base = devm_ioremap_resource(&pdev->dev, res);
    1230                 :          3 :         if (IS_ERR(base))
    1231                 :          0 :                 return PTR_ERR(base);
    1232                 :            : 
    1233                 :            :         /* The set of channels can be split across multiple instances. */
    1234                 :          3 :         chan_start = ((u32)(uintptr_t)base / BCM2835_DMA_CHAN_SIZE) & 0xf;
    1235                 :          3 :         base -= BCM2835_DMA_CHAN(chan_start);
    1236                 :          3 :         chan_count = resource_size(res) / BCM2835_DMA_CHAN_SIZE;
    1237                 :          3 :         chan_end = min(chan_start + chan_count,
    1238                 :            :                          BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1);
    1239                 :            : 
    1240                 :          3 :         od->base = base;
    1241                 :            : 
    1242                 :            :         dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
    1243                 :            :         dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
    1244                 :            :         dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
    1245                 :            :         dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
    1246                 :          3 :         od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
    1247                 :          3 :         od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
    1248                 :          3 :         od->ddev.device_tx_status = bcm2835_dma_tx_status;
    1249                 :          3 :         od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
    1250                 :          3 :         od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
    1251                 :          3 :         od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
    1252                 :          3 :         od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
    1253                 :          3 :         od->ddev.device_config = bcm2835_dma_slave_config;
    1254                 :          3 :         od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
    1255                 :          3 :         od->ddev.device_synchronize = bcm2835_dma_synchronize;
    1256                 :          3 :         od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
    1257                 :          3 :         od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
    1258                 :          3 :         od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
    1259                 :            :                               BIT(DMA_MEM_TO_MEM);
    1260                 :          3 :         od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
    1261                 :          3 :         od->ddev.descriptor_reuse = true;
    1262                 :          3 :         od->ddev.dev = &pdev->dev;
    1263                 :          3 :         INIT_LIST_HEAD(&od->ddev.channels);
    1264                 :            : 
    1265                 :            :         platform_set_drvdata(pdev, od);
    1266                 :            : 
    1267                 :          3 :         od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
    1268                 :            :                                            PAGE_SIZE, DMA_TO_DEVICE,
    1269                 :            :                                            DMA_ATTR_SKIP_CPU_SYNC);
    1270                 :          3 :         if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
    1271                 :          0 :                 dev_err(&pdev->dev, "Failed to map zero page\n");
    1272                 :          0 :                 return -ENOMEM;
    1273                 :            :         }
    1274                 :            : 
    1275                 :          3 :         of_id = of_match_node(bcm2835_dma_of_match, pdev->dev.of_node);
    1276                 :          3 :         if (!of_id) {
    1277                 :          0 :                 dev_err(&pdev->dev, "Failed to match compatible string\n");
    1278                 :          0 :                 return -EINVAL;
    1279                 :            :         }
    1280                 :            : 
    1281                 :          3 :         od->cfg_data = cfg_data;
    1282                 :            : 
    1283                 :            :         /* Request DMA channel mask from device tree */
    1284                 :          3 :         if (of_property_read_u32(pdev->dev.of_node,
    1285                 :            :                         "brcm,dma-channel-mask",
    1286                 :            :                         &chans_available)) {
    1287                 :          0 :                 dev_err(&pdev->dev, "Failed to get channel mask\n");
    1288                 :            :                 rc = -EINVAL;
    1289                 :          0 :                 goto err_no_dma;
    1290                 :            :         }
    1291                 :            : 
    1292                 :            :         /* One channel is reserved for the legacy API */
    1293                 :          3 :         if (chans_available & BCM2835_DMA_BULK_MASK) {
    1294                 :          3 :                 rc = bcm_dmaman_probe(pdev, base,
    1295                 :            :                                       chans_available & BCM2835_DMA_BULK_MASK);
    1296                 :          3 :                 if (rc)
    1297                 :          0 :                         dev_err(&pdev->dev,
    1298                 :            :                                 "Failed to initialize the legacy API\n");
    1299                 :            : 
    1300                 :          3 :                 chans_available &= ~BCM2835_DMA_BULK_MASK;
    1301                 :            :         }
    1302                 :            : 
    1303                 :            :         /* And possibly one for the 40-bit DMA memcpy API */
    1304                 :          3 :         if (chans_available & od->cfg_data->chan_40bit_mask &
    1305                 :            :             BIT(BCM2711_DMA_MEMCPY_CHAN)) {
    1306                 :          0 :                 memcpy_parent = od;
    1307                 :          0 :                 memcpy_chan = BCM2835_DMA_CHANIO(base, BCM2711_DMA_MEMCPY_CHAN);
    1308                 :          0 :                 memcpy_scb = dma_alloc_coherent(memcpy_parent->ddev.dev,
    1309                 :            :                                                 sizeof(*memcpy_scb),
    1310                 :            :                                                 &memcpy_scb_dma, GFP_KERNEL);
    1311                 :          0 :                 if (!memcpy_scb)
    1312                 :          0 :                         dev_warn(&pdev->dev,
    1313                 :            :                                  "Failed to allocated memcpy scb\n");
    1314                 :            : 
    1315                 :          0 :                 chans_available &= ~BIT(BCM2711_DMA_MEMCPY_CHAN);
    1316                 :            :         }
    1317                 :            : 
    1318                 :            :         /* get irqs for each channel that we support */
    1319                 :          3 :         for (i = chan_start; i < chan_end; i++) {
    1320                 :            :                 /* skip masked out channels */
    1321                 :          3 :                 if (!(chans_available & (1 << i))) {
    1322                 :          3 :                         irq[i] = -1;
    1323                 :          3 :                         continue;
    1324                 :            :                 }
    1325                 :            : 
    1326                 :            :                 /* get the named irq */
    1327                 :          3 :                 snprintf(chan_name, sizeof(chan_name), "dma%i", i);
    1328                 :          3 :                 irq[i] = platform_get_irq_byname(pdev, chan_name);
    1329                 :          3 :                 if (irq[i] >= 0)
    1330                 :          3 :                         continue;
    1331                 :            : 
    1332                 :            :                 /* legacy device tree case handling */
    1333                 :          0 :                 dev_warn_once(&pdev->dev,
    1334                 :            :                               "missing interrupt-names property in device tree - legacy interpretation is used\n");
    1335                 :            :                 /*
    1336                 :            :                  * in case of channel >= 11
    1337                 :            :                  * use the 11th interrupt and that is shared
    1338                 :            :                  */
    1339                 :          0 :                 irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
    1340                 :            :         }
    1341                 :            : 
    1342                 :            :         chan_count = 0;
    1343                 :            : 
    1344                 :            :         /* get irqs for each channel */
    1345                 :          3 :         for (i = chan_start; i < chan_end; i++) {
    1346                 :            :                 /* skip channels without irq */
    1347                 :          3 :                 if (irq[i] < 0)
    1348                 :          3 :                         continue;
    1349                 :            : 
    1350                 :            :                 /* check if there are other channels that also use this irq */
    1351                 :            :                 /* FIXME: This will fail if interrupts are shared across
    1352                 :            :                    instances */
    1353                 :            :                 irq_flags = 0;
    1354                 :          3 :                 for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
    1355                 :          3 :                         if ((i != j) && (irq[j] == irq[i])) {
    1356                 :            :                                 irq_flags = IRQF_SHARED;
    1357                 :            :                                 break;
    1358                 :            :                         }
    1359                 :            : 
    1360                 :            :                 /* initialize the channel */
    1361                 :          3 :                 rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
    1362                 :          3 :                 if (rc)
    1363                 :            :                         goto err_no_dma;
    1364                 :            :                 chan_count++;
    1365                 :            :         }
    1366                 :            : 
    1367                 :            :         dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", chan_count);
    1368                 :            : 
    1369                 :            :         /* Device-tree DMA controller registration */
    1370                 :          3 :         rc = of_dma_controller_register(pdev->dev.of_node,
    1371                 :            :                         bcm2835_dma_xlate, od);
    1372                 :          3 :         if (rc) {
    1373                 :          0 :                 dev_err(&pdev->dev, "Failed to register DMA controller\n");
    1374                 :          0 :                 goto err_no_dma;
    1375                 :            :         }
    1376                 :            : 
    1377                 :          3 :         rc = dma_async_device_register(&od->ddev);
    1378                 :          3 :         if (rc) {
    1379                 :          0 :                 dev_err(&pdev->dev,
    1380                 :            :                         "Failed to register slave DMA engine device: %d\n", rc);
    1381                 :          0 :                 goto err_no_dma;
    1382                 :            :         }
    1383                 :            : 
    1384                 :            :         dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
    1385                 :            : 
    1386                 :            :         return 0;
    1387                 :            : 
    1388                 :            : err_no_dma:
    1389                 :          0 :         bcm2835_dma_free(od);
    1390                 :          0 :         return rc;
    1391                 :            : }
    1392                 :            : 
    1393                 :          0 : static int bcm2835_dma_remove(struct platform_device *pdev)
    1394                 :            : {
    1395                 :            :         struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
    1396                 :            : 
    1397                 :          0 :         bcm_dmaman_remove(pdev);
    1398                 :          0 :         dma_async_device_unregister(&od->ddev);
    1399                 :          0 :         if (memcpy_parent == od) {
    1400                 :          0 :                 dma_free_coherent(&pdev->dev, sizeof(*memcpy_scb), memcpy_scb,
    1401                 :            :                                   memcpy_scb_dma);
    1402                 :          0 :                 memcpy_parent = NULL;
    1403                 :          0 :                 memcpy_scb = NULL;
    1404                 :          0 :                 memcpy_chan = NULL;
    1405                 :            :         }
    1406                 :          0 :         bcm2835_dma_free(od);
    1407                 :            : 
    1408                 :          0 :         return 0;
    1409                 :            : }
    1410                 :            : 
    1411                 :            : static struct platform_driver bcm2835_dma_driver = {
    1412                 :            :         .probe  = bcm2835_dma_probe,
    1413                 :            :         .remove = bcm2835_dma_remove,
    1414                 :            :         .driver = {
    1415                 :            :                 .name = "bcm2835-dma",
    1416                 :            :                 .of_match_table = of_match_ptr(bcm2835_dma_of_match),
    1417                 :            :         },
    1418                 :            : };
    1419                 :            : 
    1420                 :          3 : static int bcm2835_dma_init(void)
    1421                 :            : {
    1422                 :          3 :         return platform_driver_register(&bcm2835_dma_driver);
    1423                 :            : }
    1424                 :            : 
    1425                 :          0 : static void bcm2835_dma_exit(void)
    1426                 :            : {
    1427                 :          0 :         platform_driver_unregister(&bcm2835_dma_driver);
    1428                 :          0 : }
    1429                 :            : 
    1430                 :            : /*
    1431                 :            :  * Load after serial driver (arch_initcall) so we see the messages if it fails,
    1432                 :            :  * but before drivers (module_init) that need a DMA channel.
    1433                 :            :  */
    1434                 :            : subsys_initcall(bcm2835_dma_init);
    1435                 :            : module_exit(bcm2835_dma_exit);
    1436                 :            : 
    1437                 :            : MODULE_ALIAS("platform:bcm2835-dma");
    1438                 :            : MODULE_DESCRIPTION("BCM2835 DMA engine driver");
    1439                 :            : MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
    1440                 :            : MODULE_LICENSE("GPL");
    

Generated by: LCOV version 1.14