18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2016 Broadcom
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci * Broadcom PDC Mailbox Driver
88c2ecf20Sopenharmony_ci * The PDC provides a ring based programming interface to one or more hardware
98c2ecf20Sopenharmony_ci * offload engines. For example, the PDC driver works with both SPU-M and SPU2
108c2ecf20Sopenharmony_ci * cryptographic offload hardware. In some chips the PDC is referred to as MDE,
118c2ecf20Sopenharmony_ci * and in others the FA2/FA+ hardware is used with this PDC driver.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The PDC driver registers with the Linux mailbox framework as a mailbox
148c2ecf20Sopenharmony_ci * controller, once for each PDC instance. Ring 0 for each PDC is registered as
158c2ecf20Sopenharmony_ci * a mailbox channel. The PDC driver uses interrupts to determine when data
168c2ecf20Sopenharmony_ci * transfers to and from an offload engine are complete. The PDC driver uses
178c2ecf20Sopenharmony_ci * threaded IRQs so that response messages are handled outside of interrupt
188c2ecf20Sopenharmony_ci * context.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * The PDC driver allows multiple messages to be pending in the descriptor
218c2ecf20Sopenharmony_ci * rings. The tx_msg_start descriptor index indicates where the last message
228c2ecf20Sopenharmony_ci * starts. The txin_numd value at this index indicates how many descriptor
238c2ecf20Sopenharmony_ci * indexes make up the message. Similar state is kept on the receive side. When
248c2ecf20Sopenharmony_ci * an rx interrupt indicates a response is ready, the PDC driver processes numd
258c2ecf20Sopenharmony_ci * descriptors from the tx and rx ring, thus processing one response at a time.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/errno.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/init.h>
318c2ecf20Sopenharmony_ci#include <linux/slab.h>
328c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
338c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
348c2ecf20Sopenharmony_ci#include <linux/wait.h>
358c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
368c2ecf20Sopenharmony_ci#include <linux/io.h>
378c2ecf20Sopenharmony_ci#include <linux/of.h>
388c2ecf20Sopenharmony_ci#include <linux/of_device.h>
398c2ecf20Sopenharmony_ci#include <linux/of_address.h>
408c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
418c2ecf20Sopenharmony_ci#include <linux/mailbox_controller.h>
428c2ecf20Sopenharmony_ci#include <linux/mailbox/brcm-message.h>
438c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
448c2ecf20Sopenharmony_ci#include <linux/dma-direction.h>
458c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
468c2ecf20Sopenharmony_ci#include <linux/dmapool.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define PDC_SUCCESS  0
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define RING_ENTRY_SIZE   sizeof(struct dma64dd)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* # entries in PDC dma ring */
538c2ecf20Sopenharmony_ci#define PDC_RING_ENTRIES  512
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * Minimum number of ring descriptor entries that must be free to tell mailbox
568c2ecf20Sopenharmony_ci * framework that it can submit another request
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci#define PDC_RING_SPACE_MIN  15
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define PDC_RING_SIZE    (PDC_RING_ENTRIES * RING_ENTRY_SIZE)
618c2ecf20Sopenharmony_ci/* Rings are 8k aligned */
628c2ecf20Sopenharmony_ci#define RING_ALIGN_ORDER  13
638c2ecf20Sopenharmony_ci#define RING_ALIGN        BIT(RING_ALIGN_ORDER)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define RX_BUF_ALIGN_ORDER  5
668c2ecf20Sopenharmony_ci#define RX_BUF_ALIGN	    BIT(RX_BUF_ALIGN_ORDER)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* descriptor bumping macros */
698c2ecf20Sopenharmony_ci#define XXD(x, max_mask)              ((x) & (max_mask))
708c2ecf20Sopenharmony_ci#define TXD(x, max_mask)              XXD((x), (max_mask))
718c2ecf20Sopenharmony_ci#define RXD(x, max_mask)              XXD((x), (max_mask))
728c2ecf20Sopenharmony_ci#define NEXTTXD(i, max_mask)          TXD((i) + 1, (max_mask))
738c2ecf20Sopenharmony_ci#define PREVTXD(i, max_mask)          TXD((i) - 1, (max_mask))
748c2ecf20Sopenharmony_ci#define NEXTRXD(i, max_mask)          RXD((i) + 1, (max_mask))
758c2ecf20Sopenharmony_ci#define PREVRXD(i, max_mask)          RXD((i) - 1, (max_mask))
768c2ecf20Sopenharmony_ci#define NTXDACTIVE(h, t, max_mask)    TXD((t) - (h), (max_mask))
778c2ecf20Sopenharmony_ci#define NRXDACTIVE(h, t, max_mask)    RXD((t) - (h), (max_mask))
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Length of BCM header at start of SPU msg, in bytes */
808c2ecf20Sopenharmony_ci#define BCM_HDR_LEN  8
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * PDC driver reserves ringset 0 on each SPU for its own use. The driver does
848c2ecf20Sopenharmony_ci * not currently support use of multiple ringsets on a single PDC engine.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_ci#define PDC_RINGSET  0
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * Interrupt mask and status definitions. Enable interrupts for tx and rx on
908c2ecf20Sopenharmony_ci * ring 0
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_ci#define PDC_RCVINT_0         (16 + PDC_RINGSET)
938c2ecf20Sopenharmony_ci#define PDC_RCVINTEN_0       BIT(PDC_RCVINT_0)
948c2ecf20Sopenharmony_ci#define PDC_INTMASK	     (PDC_RCVINTEN_0)
958c2ecf20Sopenharmony_ci#define PDC_LAZY_FRAMECOUNT  1
968c2ecf20Sopenharmony_ci#define PDC_LAZY_TIMEOUT     10000
978c2ecf20Sopenharmony_ci#define PDC_LAZY_INT  (PDC_LAZY_TIMEOUT | (PDC_LAZY_FRAMECOUNT << 24))
988c2ecf20Sopenharmony_ci#define PDC_INTMASK_OFFSET   0x24
998c2ecf20Sopenharmony_ci#define PDC_INTSTATUS_OFFSET 0x20
1008c2ecf20Sopenharmony_ci#define PDC_RCVLAZY0_OFFSET  (0x30 + 4 * PDC_RINGSET)
1018c2ecf20Sopenharmony_ci#define FA_RCVLAZY0_OFFSET   0x100
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * For SPU2, configure MDE_CKSUM_CONTROL to write 17 bytes of metadata
1058c2ecf20Sopenharmony_ci * before frame
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci#define PDC_SPU2_RESP_HDR_LEN  17
1088c2ecf20Sopenharmony_ci#define PDC_CKSUM_CTRL         BIT(27)
1098c2ecf20Sopenharmony_ci#define PDC_CKSUM_CTRL_OFFSET  0x400
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define PDC_SPUM_RESP_HDR_LEN  32
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/*
1148c2ecf20Sopenharmony_ci * Sets the following bits for write to transmit control reg:
1158c2ecf20Sopenharmony_ci * 11    - PtyChkDisable - parity check is disabled
1168c2ecf20Sopenharmony_ci * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_ci#define PDC_TX_CTL		0x000C0800
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Bit in tx control reg to enable tx channel */
1218c2ecf20Sopenharmony_ci#define PDC_TX_ENABLE		0x1
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/*
1248c2ecf20Sopenharmony_ci * Sets the following bits for write to receive control reg:
1258c2ecf20Sopenharmony_ci * 7:1   - RcvOffset - size in bytes of status region at start of rx frame buf
1268c2ecf20Sopenharmony_ci * 9     - SepRxHdrDescEn - place start of new frames only in descriptors
1278c2ecf20Sopenharmony_ci *                          that have StartOfFrame set
1288c2ecf20Sopenharmony_ci * 10    - OflowContinue - on rx FIFO overflow, clear rx fifo, discard all
1298c2ecf20Sopenharmony_ci *                         remaining bytes in current frame, report error
1308c2ecf20Sopenharmony_ci *                         in rx frame status for current frame
1318c2ecf20Sopenharmony_ci * 11    - PtyChkDisable - parity check is disabled
1328c2ecf20Sopenharmony_ci * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ci#define PDC_RX_CTL		0x000C0E00
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* Bit in rx control reg to enable rx channel */
1378c2ecf20Sopenharmony_ci#define PDC_RX_ENABLE		0x1
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define CRYPTO_D64_RS0_CD_MASK   ((PDC_RING_ENTRIES * RING_ENTRY_SIZE) - 1)
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/* descriptor flags */
1428c2ecf20Sopenharmony_ci#define D64_CTRL1_EOT   BIT(28)	/* end of descriptor table */
1438c2ecf20Sopenharmony_ci#define D64_CTRL1_IOC   BIT(29)	/* interrupt on complete */
1448c2ecf20Sopenharmony_ci#define D64_CTRL1_EOF   BIT(30)	/* end of frame */
1458c2ecf20Sopenharmony_ci#define D64_CTRL1_SOF   BIT(31)	/* start of frame */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define RX_STATUS_OVERFLOW       0x00800000
1488c2ecf20Sopenharmony_ci#define RX_STATUS_LEN            0x0000FFFF
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#define PDC_TXREGS_OFFSET  0x200
1518c2ecf20Sopenharmony_ci#define PDC_RXREGS_OFFSET  0x220
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/* Maximum size buffer the DMA engine can handle */
1548c2ecf20Sopenharmony_ci#define PDC_DMA_BUF_MAX 16384
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cienum pdc_hw {
1578c2ecf20Sopenharmony_ci	FA_HW,		/* FA2/FA+ hardware (i.e. Northstar Plus) */
1588c2ecf20Sopenharmony_ci	PDC_HW		/* PDC/MDE hardware (i.e. Northstar 2, Pegasus) */
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistruct pdc_dma_map {
1628c2ecf20Sopenharmony_ci	void *ctx;          /* opaque context associated with frame */
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* dma descriptor */
1668c2ecf20Sopenharmony_cistruct dma64dd {
1678c2ecf20Sopenharmony_ci	u32 ctrl1;      /* misc control bits */
1688c2ecf20Sopenharmony_ci	u32 ctrl2;      /* buffer count and address extension */
1698c2ecf20Sopenharmony_ci	u32 addrlow;    /* memory address of the date buffer, bits 31:0 */
1708c2ecf20Sopenharmony_ci	u32 addrhigh;   /* memory address of the date buffer, bits 63:32 */
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* dma registers per channel(xmt or rcv) */
1748c2ecf20Sopenharmony_cistruct dma64_regs {
1758c2ecf20Sopenharmony_ci	u32  control;   /* enable, et al */
1768c2ecf20Sopenharmony_ci	u32  ptr;       /* last descriptor posted to chip */
1778c2ecf20Sopenharmony_ci	u32  addrlow;   /* descriptor ring base address low 32-bits */
1788c2ecf20Sopenharmony_ci	u32  addrhigh;  /* descriptor ring base address bits 63:32 */
1798c2ecf20Sopenharmony_ci	u32  status0;   /* last rx descriptor written by hw */
1808c2ecf20Sopenharmony_ci	u32  status1;   /* driver does not use */
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* cpp contortions to concatenate w/arg prescan */
1848c2ecf20Sopenharmony_ci#ifndef PAD
1858c2ecf20Sopenharmony_ci#define _PADLINE(line)  pad ## line
1868c2ecf20Sopenharmony_ci#define _XSTR(line)     _PADLINE(line)
1878c2ecf20Sopenharmony_ci#define PAD             _XSTR(__LINE__)
1888c2ecf20Sopenharmony_ci#endif  /* PAD */
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/* dma registers. matches hw layout. */
1918c2ecf20Sopenharmony_cistruct dma64 {
1928c2ecf20Sopenharmony_ci	struct dma64_regs dmaxmt;  /* dma tx */
1938c2ecf20Sopenharmony_ci	u32          PAD[2];
1948c2ecf20Sopenharmony_ci	struct dma64_regs dmarcv;  /* dma rx */
1958c2ecf20Sopenharmony_ci	u32          PAD[2];
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* PDC registers */
1998c2ecf20Sopenharmony_cistruct pdc_regs {
2008c2ecf20Sopenharmony_ci	u32  devcontrol;             /* 0x000 */
2018c2ecf20Sopenharmony_ci	u32  devstatus;              /* 0x004 */
2028c2ecf20Sopenharmony_ci	u32  PAD;
2038c2ecf20Sopenharmony_ci	u32  biststatus;             /* 0x00c */
2048c2ecf20Sopenharmony_ci	u32  PAD[4];
2058c2ecf20Sopenharmony_ci	u32  intstatus;              /* 0x020 */
2068c2ecf20Sopenharmony_ci	u32  intmask;                /* 0x024 */
2078c2ecf20Sopenharmony_ci	u32  gptimer;                /* 0x028 */
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	u32  PAD;
2108c2ecf20Sopenharmony_ci	u32  intrcvlazy_0;           /* 0x030 (Only in PDC, not FA2) */
2118c2ecf20Sopenharmony_ci	u32  intrcvlazy_1;           /* 0x034 (Only in PDC, not FA2) */
2128c2ecf20Sopenharmony_ci	u32  intrcvlazy_2;           /* 0x038 (Only in PDC, not FA2) */
2138c2ecf20Sopenharmony_ci	u32  intrcvlazy_3;           /* 0x03c (Only in PDC, not FA2) */
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	u32  PAD[48];
2168c2ecf20Sopenharmony_ci	u32  fa_intrecvlazy;         /* 0x100 (Only in FA2, not PDC) */
2178c2ecf20Sopenharmony_ci	u32  flowctlthresh;          /* 0x104 */
2188c2ecf20Sopenharmony_ci	u32  wrrthresh;              /* 0x108 */
2198c2ecf20Sopenharmony_ci	u32  gmac_idle_cnt_thresh;   /* 0x10c */
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	u32  PAD[4];
2228c2ecf20Sopenharmony_ci	u32  ifioaccessaddr;         /* 0x120 */
2238c2ecf20Sopenharmony_ci	u32  ifioaccessbyte;         /* 0x124 */
2248c2ecf20Sopenharmony_ci	u32  ifioaccessdata;         /* 0x128 */
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	u32  PAD[21];
2278c2ecf20Sopenharmony_ci	u32  phyaccess;              /* 0x180 */
2288c2ecf20Sopenharmony_ci	u32  PAD;
2298c2ecf20Sopenharmony_ci	u32  phycontrol;             /* 0x188 */
2308c2ecf20Sopenharmony_ci	u32  txqctl;                 /* 0x18c */
2318c2ecf20Sopenharmony_ci	u32  rxqctl;                 /* 0x190 */
2328c2ecf20Sopenharmony_ci	u32  gpioselect;             /* 0x194 */
2338c2ecf20Sopenharmony_ci	u32  gpio_output_en;         /* 0x198 */
2348c2ecf20Sopenharmony_ci	u32  PAD;                    /* 0x19c */
2358c2ecf20Sopenharmony_ci	u32  txq_rxq_mem_ctl;        /* 0x1a0 */
2368c2ecf20Sopenharmony_ci	u32  memory_ecc_status;      /* 0x1a4 */
2378c2ecf20Sopenharmony_ci	u32  serdes_ctl;             /* 0x1a8 */
2388c2ecf20Sopenharmony_ci	u32  serdes_status0;         /* 0x1ac */
2398c2ecf20Sopenharmony_ci	u32  serdes_status1;         /* 0x1b0 */
2408c2ecf20Sopenharmony_ci	u32  PAD[11];                /* 0x1b4-1dc */
2418c2ecf20Sopenharmony_ci	u32  clk_ctl_st;             /* 0x1e0 */
2428c2ecf20Sopenharmony_ci	u32  hw_war;                 /* 0x1e4 (Only in PDC, not FA2) */
2438c2ecf20Sopenharmony_ci	u32  pwrctl;                 /* 0x1e8 */
2448c2ecf20Sopenharmony_ci	u32  PAD[5];
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#define PDC_NUM_DMA_RINGS   4
2478c2ecf20Sopenharmony_ci	struct dma64 dmaregs[PDC_NUM_DMA_RINGS];  /* 0x0200 - 0x2fc */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/* more registers follow, but we don't use them */
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* structure for allocating/freeing DMA rings */
2538c2ecf20Sopenharmony_cistruct pdc_ring_alloc {
2548c2ecf20Sopenharmony_ci	dma_addr_t  dmabase; /* DMA address of start of ring */
2558c2ecf20Sopenharmony_ci	void	   *vbase;   /* base kernel virtual address of ring */
2568c2ecf20Sopenharmony_ci	u32	    size;    /* ring allocation size in bytes */
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/*
2608c2ecf20Sopenharmony_ci * context associated with a receive descriptor.
2618c2ecf20Sopenharmony_ci * @rxp_ctx: opaque context associated with frame that starts at each
2628c2ecf20Sopenharmony_ci *           rx ring index.
2638c2ecf20Sopenharmony_ci * @dst_sg:  Scatterlist used to form reply frames beginning at a given ring
2648c2ecf20Sopenharmony_ci *           index. Retained in order to unmap each sg after reply is processed.
2658c2ecf20Sopenharmony_ci * @rxin_numd: Number of rx descriptors associated with the message that starts
2668c2ecf20Sopenharmony_ci *             at a descriptor index. Not set for every index. For example,
2678c2ecf20Sopenharmony_ci *             if descriptor index i points to a scatterlist with 4 entries,
2688c2ecf20Sopenharmony_ci *             then the next three descriptor indexes don't have a value set.
2698c2ecf20Sopenharmony_ci * @resp_hdr: Virtual address of buffer used to catch DMA rx status
2708c2ecf20Sopenharmony_ci * @resp_hdr_daddr: physical address of DMA rx status buffer
2718c2ecf20Sopenharmony_ci */
2728c2ecf20Sopenharmony_cistruct pdc_rx_ctx {
2738c2ecf20Sopenharmony_ci	void *rxp_ctx;
2748c2ecf20Sopenharmony_ci	struct scatterlist *dst_sg;
2758c2ecf20Sopenharmony_ci	u32  rxin_numd;
2768c2ecf20Sopenharmony_ci	void *resp_hdr;
2778c2ecf20Sopenharmony_ci	dma_addr_t resp_hdr_daddr;
2788c2ecf20Sopenharmony_ci};
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* PDC state structure */
2818c2ecf20Sopenharmony_cistruct pdc_state {
2828c2ecf20Sopenharmony_ci	/* Index of the PDC whose state is in this structure instance */
2838c2ecf20Sopenharmony_ci	u8 pdc_idx;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* Platform device for this PDC instance */
2868c2ecf20Sopenharmony_ci	struct platform_device *pdev;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/*
2898c2ecf20Sopenharmony_ci	 * Each PDC instance has a mailbox controller. PDC receives request
2908c2ecf20Sopenharmony_ci	 * messages through mailboxes, and sends response messages through the
2918c2ecf20Sopenharmony_ci	 * mailbox framework.
2928c2ecf20Sopenharmony_ci	 */
2938c2ecf20Sopenharmony_ci	struct mbox_controller mbc;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	unsigned int pdc_irq;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* tasklet for deferred processing after DMA rx interrupt */
2988c2ecf20Sopenharmony_ci	struct tasklet_struct rx_tasklet;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* Number of bytes of receive status prior to each rx frame */
3018c2ecf20Sopenharmony_ci	u32 rx_status_len;
3028c2ecf20Sopenharmony_ci	/* Whether a BCM header is prepended to each frame */
3038c2ecf20Sopenharmony_ci	bool use_bcm_hdr;
3048c2ecf20Sopenharmony_ci	/* Sum of length of BCM header and rx status header */
3058c2ecf20Sopenharmony_ci	u32 pdc_resp_hdr_len;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* The base virtual address of DMA hw registers */
3088c2ecf20Sopenharmony_ci	void __iomem *pdc_reg_vbase;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Pool for allocation of DMA rings */
3118c2ecf20Sopenharmony_ci	struct dma_pool *ring_pool;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Pool for allocation of metadata buffers for response messages */
3148c2ecf20Sopenharmony_ci	struct dma_pool *rx_buf_pool;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/*
3178c2ecf20Sopenharmony_ci	 * The base virtual address of DMA tx/rx descriptor rings. Corresponding
3188c2ecf20Sopenharmony_ci	 * DMA address and size of ring allocation.
3198c2ecf20Sopenharmony_ci	 */
3208c2ecf20Sopenharmony_ci	struct pdc_ring_alloc tx_ring_alloc;
3218c2ecf20Sopenharmony_ci	struct pdc_ring_alloc rx_ring_alloc;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	struct pdc_regs *regs;    /* start of PDC registers */
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	struct dma64_regs *txregs_64; /* dma tx engine registers */
3268c2ecf20Sopenharmony_ci	struct dma64_regs *rxregs_64; /* dma rx engine registers */
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/*
3298c2ecf20Sopenharmony_ci	 * Arrays of PDC_RING_ENTRIES descriptors
3308c2ecf20Sopenharmony_ci	 * To use multiple ringsets, this needs to be extended
3318c2ecf20Sopenharmony_ci	 */
3328c2ecf20Sopenharmony_ci	struct dma64dd   *txd_64;  /* tx descriptor ring */
3338c2ecf20Sopenharmony_ci	struct dma64dd   *rxd_64;  /* rx descriptor ring */
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* descriptor ring sizes */
3368c2ecf20Sopenharmony_ci	u32      ntxd;       /* # tx descriptors */
3378c2ecf20Sopenharmony_ci	u32      nrxd;       /* # rx descriptors */
3388c2ecf20Sopenharmony_ci	u32      nrxpost;    /* # rx buffers to keep posted */
3398c2ecf20Sopenharmony_ci	u32      ntxpost;    /* max number of tx buffers that can be posted */
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/*
3428c2ecf20Sopenharmony_ci	 * Index of next tx descriptor to reclaim. That is, the descriptor
3438c2ecf20Sopenharmony_ci	 * index of the oldest tx buffer for which the host has yet to process
3448c2ecf20Sopenharmony_ci	 * the corresponding response.
3458c2ecf20Sopenharmony_ci	 */
3468c2ecf20Sopenharmony_ci	u32  txin;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * Index of the first receive descriptor for the sequence of
3508c2ecf20Sopenharmony_ci	 * message fragments currently under construction. Used to build up
3518c2ecf20Sopenharmony_ci	 * the rxin_numd count for a message. Updated to rxout when the host
3528c2ecf20Sopenharmony_ci	 * starts a new sequence of rx buffers for a new message.
3538c2ecf20Sopenharmony_ci	 */
3548c2ecf20Sopenharmony_ci	u32  tx_msg_start;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/* Index of next tx descriptor to post. */
3578c2ecf20Sopenharmony_ci	u32  txout;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/*
3608c2ecf20Sopenharmony_ci	 * Number of tx descriptors associated with the message that starts
3618c2ecf20Sopenharmony_ci	 * at this tx descriptor index.
3628c2ecf20Sopenharmony_ci	 */
3638c2ecf20Sopenharmony_ci	u32      txin_numd[PDC_RING_ENTRIES];
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	/*
3668c2ecf20Sopenharmony_ci	 * Index of next rx descriptor to reclaim. This is the index of
3678c2ecf20Sopenharmony_ci	 * the next descriptor whose data has yet to be processed by the host.
3688c2ecf20Sopenharmony_ci	 */
3698c2ecf20Sopenharmony_ci	u32  rxin;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/*
3728c2ecf20Sopenharmony_ci	 * Index of the first receive descriptor for the sequence of
3738c2ecf20Sopenharmony_ci	 * message fragments currently under construction. Used to build up
3748c2ecf20Sopenharmony_ci	 * the rxin_numd count for a message. Updated to rxout when the host
3758c2ecf20Sopenharmony_ci	 * starts a new sequence of rx buffers for a new message.
3768c2ecf20Sopenharmony_ci	 */
3778c2ecf20Sopenharmony_ci	u32  rx_msg_start;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/*
3808c2ecf20Sopenharmony_ci	 * Saved value of current hardware rx descriptor index.
3818c2ecf20Sopenharmony_ci	 * The last rx buffer written by the hw is the index previous to
3828c2ecf20Sopenharmony_ci	 * this one.
3838c2ecf20Sopenharmony_ci	 */
3848c2ecf20Sopenharmony_ci	u32  last_rx_curr;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* Index of next rx descriptor to post. */
3878c2ecf20Sopenharmony_ci	u32  rxout;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	struct pdc_rx_ctx rx_ctx[PDC_RING_ENTRIES];
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/*
3928c2ecf20Sopenharmony_ci	 * Scatterlists used to form request and reply frames beginning at a
3938c2ecf20Sopenharmony_ci	 * given ring index. Retained in order to unmap each sg after reply
3948c2ecf20Sopenharmony_ci	 * is processed
3958c2ecf20Sopenharmony_ci	 */
3968c2ecf20Sopenharmony_ci	struct scatterlist *src_sg[PDC_RING_ENTRIES];
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* counters */
3998c2ecf20Sopenharmony_ci	u32  pdc_requests;     /* number of request messages submitted */
4008c2ecf20Sopenharmony_ci	u32  pdc_replies;      /* number of reply messages received */
4018c2ecf20Sopenharmony_ci	u32  last_tx_not_done; /* too few tx descriptors to indicate done */
4028c2ecf20Sopenharmony_ci	u32  tx_ring_full;     /* unable to accept msg because tx ring full */
4038c2ecf20Sopenharmony_ci	u32  rx_ring_full;     /* unable to accept msg because rx ring full */
4048c2ecf20Sopenharmony_ci	u32  txnobuf;          /* unable to create tx descriptor */
4058c2ecf20Sopenharmony_ci	u32  rxnobuf;          /* unable to create rx descriptor */
4068c2ecf20Sopenharmony_ci	u32  rx_oflow;         /* count of rx overflows */
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* hardware type - FA2 or PDC/MDE */
4098c2ecf20Sopenharmony_ci	enum pdc_hw hw_type;
4108c2ecf20Sopenharmony_ci};
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/* Global variables */
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistruct pdc_globals {
4158c2ecf20Sopenharmony_ci	/* Actual number of SPUs in hardware, as reported by device tree */
4168c2ecf20Sopenharmony_ci	u32 num_spu;
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic struct pdc_globals pdcg;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci/* top level debug FS directory for PDC driver */
4228c2ecf20Sopenharmony_cistatic struct dentry *debugfs_dir;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic ssize_t pdc_debugfs_read(struct file *filp, char __user *ubuf,
4258c2ecf20Sopenharmony_ci				size_t count, loff_t *offp)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	struct pdc_state *pdcs;
4288c2ecf20Sopenharmony_ci	char *buf;
4298c2ecf20Sopenharmony_ci	ssize_t ret, out_offset, out_count;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	out_count = 512;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	buf = kmalloc(out_count, GFP_KERNEL);
4348c2ecf20Sopenharmony_ci	if (!buf)
4358c2ecf20Sopenharmony_ci		return -ENOMEM;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	pdcs = filp->private_data;
4388c2ecf20Sopenharmony_ci	out_offset = 0;
4398c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4408c2ecf20Sopenharmony_ci			       "SPU %u stats:\n", pdcs->pdc_idx);
4418c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4428c2ecf20Sopenharmony_ci			       "PDC requests....................%u\n",
4438c2ecf20Sopenharmony_ci			       pdcs->pdc_requests);
4448c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4458c2ecf20Sopenharmony_ci			       "PDC responses...................%u\n",
4468c2ecf20Sopenharmony_ci			       pdcs->pdc_replies);
4478c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4488c2ecf20Sopenharmony_ci			       "Tx not done.....................%u\n",
4498c2ecf20Sopenharmony_ci			       pdcs->last_tx_not_done);
4508c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4518c2ecf20Sopenharmony_ci			       "Tx ring full....................%u\n",
4528c2ecf20Sopenharmony_ci			       pdcs->tx_ring_full);
4538c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4548c2ecf20Sopenharmony_ci			       "Rx ring full....................%u\n",
4558c2ecf20Sopenharmony_ci			       pdcs->rx_ring_full);
4568c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4578c2ecf20Sopenharmony_ci			       "Tx desc write fail. Ring full...%u\n",
4588c2ecf20Sopenharmony_ci			       pdcs->txnobuf);
4598c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4608c2ecf20Sopenharmony_ci			       "Rx desc write fail. Ring full...%u\n",
4618c2ecf20Sopenharmony_ci			       pdcs->rxnobuf);
4628c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4638c2ecf20Sopenharmony_ci			       "Receive overflow................%u\n",
4648c2ecf20Sopenharmony_ci			       pdcs->rx_oflow);
4658c2ecf20Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
4668c2ecf20Sopenharmony_ci			       "Num frags in rx ring............%u\n",
4678c2ecf20Sopenharmony_ci			       NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr,
4688c2ecf20Sopenharmony_ci					  pdcs->nrxpost));
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if (out_offset > out_count)
4718c2ecf20Sopenharmony_ci		out_offset = out_count;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
4748c2ecf20Sopenharmony_ci	kfree(buf);
4758c2ecf20Sopenharmony_ci	return ret;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic const struct file_operations pdc_debugfs_stats = {
4798c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
4808c2ecf20Sopenharmony_ci	.open = simple_open,
4818c2ecf20Sopenharmony_ci	.read = pdc_debugfs_read,
4828c2ecf20Sopenharmony_ci};
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci/**
4858c2ecf20Sopenharmony_ci * pdc_setup_debugfs() - Create the debug FS directories. If the top-level
4868c2ecf20Sopenharmony_ci * directory has not yet been created, create it now. Create a stats file in
4878c2ecf20Sopenharmony_ci * this directory for a SPU.
4888c2ecf20Sopenharmony_ci * @pdcs: PDC state structure
4898c2ecf20Sopenharmony_ci */
4908c2ecf20Sopenharmony_cistatic void pdc_setup_debugfs(struct pdc_state *pdcs)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	char spu_stats_name[16];
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if (!debugfs_initialized())
4958c2ecf20Sopenharmony_ci		return;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	snprintf(spu_stats_name, 16, "pdc%d_stats", pdcs->pdc_idx);
4988c2ecf20Sopenharmony_ci	if (!debugfs_dir)
4998c2ecf20Sopenharmony_ci		debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* S_IRUSR == 0400 */
5028c2ecf20Sopenharmony_ci	debugfs_create_file(spu_stats_name, 0400, debugfs_dir, pdcs,
5038c2ecf20Sopenharmony_ci			    &pdc_debugfs_stats);
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic void pdc_free_debugfs(void)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	debugfs_remove_recursive(debugfs_dir);
5098c2ecf20Sopenharmony_ci	debugfs_dir = NULL;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/**
5138c2ecf20Sopenharmony_ci * pdc_build_rxd() - Build DMA descriptor to receive SPU result.
5148c2ecf20Sopenharmony_ci * @pdcs:      PDC state for SPU that will generate result
5158c2ecf20Sopenharmony_ci * @dma_addr:  DMA address of buffer that descriptor is being built for
5168c2ecf20Sopenharmony_ci * @buf_len:   Length of the receive buffer, in bytes
5178c2ecf20Sopenharmony_ci * @flags:     Flags to be stored in descriptor
5188c2ecf20Sopenharmony_ci */
5198c2ecf20Sopenharmony_cistatic inline void
5208c2ecf20Sopenharmony_cipdc_build_rxd(struct pdc_state *pdcs, dma_addr_t dma_addr,
5218c2ecf20Sopenharmony_ci	      u32 buf_len, u32 flags)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
5248c2ecf20Sopenharmony_ci	struct dma64dd *rxd = &pdcs->rxd_64[pdcs->rxout];
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	dev_dbg(dev,
5278c2ecf20Sopenharmony_ci		"Writing rx descriptor for PDC %u at index %u with length %u. flags %#x\n",
5288c2ecf20Sopenharmony_ci		pdcs->pdc_idx, pdcs->rxout, buf_len, flags);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	rxd->addrlow = cpu_to_le32(lower_32_bits(dma_addr));
5318c2ecf20Sopenharmony_ci	rxd->addrhigh = cpu_to_le32(upper_32_bits(dma_addr));
5328c2ecf20Sopenharmony_ci	rxd->ctrl1 = cpu_to_le32(flags);
5338c2ecf20Sopenharmony_ci	rxd->ctrl2 = cpu_to_le32(buf_len);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* bump ring index and return */
5368c2ecf20Sopenharmony_ci	pdcs->rxout = NEXTRXD(pdcs->rxout, pdcs->nrxpost);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci/**
5408c2ecf20Sopenharmony_ci * pdc_build_txd() - Build a DMA descriptor to transmit a SPU request to
5418c2ecf20Sopenharmony_ci * hardware.
5428c2ecf20Sopenharmony_ci * @pdcs:        PDC state for the SPU that will process this request
5438c2ecf20Sopenharmony_ci * @dma_addr:    DMA address of packet to be transmitted
5448c2ecf20Sopenharmony_ci * @buf_len:     Length of tx buffer, in bytes
5458c2ecf20Sopenharmony_ci * @flags:       Flags to be stored in descriptor
5468c2ecf20Sopenharmony_ci */
5478c2ecf20Sopenharmony_cistatic inline void
5488c2ecf20Sopenharmony_cipdc_build_txd(struct pdc_state *pdcs, dma_addr_t dma_addr, u32 buf_len,
5498c2ecf20Sopenharmony_ci	      u32 flags)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
5528c2ecf20Sopenharmony_ci	struct dma64dd *txd = &pdcs->txd_64[pdcs->txout];
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	dev_dbg(dev,
5558c2ecf20Sopenharmony_ci		"Writing tx descriptor for PDC %u at index %u with length %u, flags %#x\n",
5568c2ecf20Sopenharmony_ci		pdcs->pdc_idx, pdcs->txout, buf_len, flags);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	txd->addrlow = cpu_to_le32(lower_32_bits(dma_addr));
5598c2ecf20Sopenharmony_ci	txd->addrhigh = cpu_to_le32(upper_32_bits(dma_addr));
5608c2ecf20Sopenharmony_ci	txd->ctrl1 = cpu_to_le32(flags);
5618c2ecf20Sopenharmony_ci	txd->ctrl2 = cpu_to_le32(buf_len);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	/* bump ring index and return */
5648c2ecf20Sopenharmony_ci	pdcs->txout = NEXTTXD(pdcs->txout, pdcs->ntxpost);
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci/**
5688c2ecf20Sopenharmony_ci * pdc_receive_one() - Receive a response message from a given SPU.
5698c2ecf20Sopenharmony_ci * @pdcs:    PDC state for the SPU to receive from
5708c2ecf20Sopenharmony_ci *
5718c2ecf20Sopenharmony_ci * When the return code indicates success, the response message is available in
5728c2ecf20Sopenharmony_ci * the receive buffers provided prior to submission of the request.
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * Return:  PDC_SUCCESS if one or more receive descriptors was processed
5758c2ecf20Sopenharmony_ci *          -EAGAIN indicates that no response message is available
5768c2ecf20Sopenharmony_ci *          -EIO an error occurred
5778c2ecf20Sopenharmony_ci */
5788c2ecf20Sopenharmony_cistatic int
5798c2ecf20Sopenharmony_cipdc_receive_one(struct pdc_state *pdcs)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
5828c2ecf20Sopenharmony_ci	struct mbox_controller *mbc;
5838c2ecf20Sopenharmony_ci	struct mbox_chan *chan;
5848c2ecf20Sopenharmony_ci	struct brcm_message mssg;
5858c2ecf20Sopenharmony_ci	u32 len, rx_status;
5868c2ecf20Sopenharmony_ci	u32 num_frags;
5878c2ecf20Sopenharmony_ci	u8 *resp_hdr;    /* virtual addr of start of resp message DMA header */
5888c2ecf20Sopenharmony_ci	u32 frags_rdy;   /* number of fragments ready to read */
5898c2ecf20Sopenharmony_ci	u32 rx_idx;      /* ring index of start of receive frame */
5908c2ecf20Sopenharmony_ci	dma_addr_t resp_hdr_daddr;
5918c2ecf20Sopenharmony_ci	struct pdc_rx_ctx *rx_ctx;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	mbc = &pdcs->mbc;
5948c2ecf20Sopenharmony_ci	chan = &mbc->chans[0];
5958c2ecf20Sopenharmony_ci	mssg.type = BRCM_MESSAGE_SPU;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/*
5988c2ecf20Sopenharmony_ci	 * return if a complete response message is not yet ready.
5998c2ecf20Sopenharmony_ci	 * rxin_numd[rxin] is the number of fragments in the next msg
6008c2ecf20Sopenharmony_ci	 * to read.
6018c2ecf20Sopenharmony_ci	 */
6028c2ecf20Sopenharmony_ci	frags_rdy = NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr, pdcs->nrxpost);
6038c2ecf20Sopenharmony_ci	if ((frags_rdy == 0) ||
6048c2ecf20Sopenharmony_ci	    (frags_rdy < pdcs->rx_ctx[pdcs->rxin].rxin_numd))
6058c2ecf20Sopenharmony_ci		/* No response ready */
6068c2ecf20Sopenharmony_ci		return -EAGAIN;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	num_frags = pdcs->txin_numd[pdcs->txin];
6098c2ecf20Sopenharmony_ci	WARN_ON(num_frags == 0);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	dma_unmap_sg(dev, pdcs->src_sg[pdcs->txin],
6128c2ecf20Sopenharmony_ci		     sg_nents(pdcs->src_sg[pdcs->txin]), DMA_TO_DEVICE);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	pdcs->txin = (pdcs->txin + num_frags) & pdcs->ntxpost;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	dev_dbg(dev, "PDC %u reclaimed %d tx descriptors",
6178c2ecf20Sopenharmony_ci		pdcs->pdc_idx, num_frags);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	rx_idx = pdcs->rxin;
6208c2ecf20Sopenharmony_ci	rx_ctx = &pdcs->rx_ctx[rx_idx];
6218c2ecf20Sopenharmony_ci	num_frags = rx_ctx->rxin_numd;
6228c2ecf20Sopenharmony_ci	/* Return opaque context with result */
6238c2ecf20Sopenharmony_ci	mssg.ctx = rx_ctx->rxp_ctx;
6248c2ecf20Sopenharmony_ci	rx_ctx->rxp_ctx = NULL;
6258c2ecf20Sopenharmony_ci	resp_hdr = rx_ctx->resp_hdr;
6268c2ecf20Sopenharmony_ci	resp_hdr_daddr = rx_ctx->resp_hdr_daddr;
6278c2ecf20Sopenharmony_ci	dma_unmap_sg(dev, rx_ctx->dst_sg, sg_nents(rx_ctx->dst_sg),
6288c2ecf20Sopenharmony_ci		     DMA_FROM_DEVICE);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	pdcs->rxin = (pdcs->rxin + num_frags) & pdcs->nrxpost;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	dev_dbg(dev, "PDC %u reclaimed %d rx descriptors",
6338c2ecf20Sopenharmony_ci		pdcs->pdc_idx, num_frags);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	dev_dbg(dev,
6368c2ecf20Sopenharmony_ci		"PDC %u txin %u, txout %u, rxin %u, rxout %u, last_rx_curr %u\n",
6378c2ecf20Sopenharmony_ci		pdcs->pdc_idx, pdcs->txin, pdcs->txout, pdcs->rxin,
6388c2ecf20Sopenharmony_ci		pdcs->rxout, pdcs->last_rx_curr);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	if (pdcs->pdc_resp_hdr_len == PDC_SPUM_RESP_HDR_LEN) {
6418c2ecf20Sopenharmony_ci		/*
6428c2ecf20Sopenharmony_ci		 * For SPU-M, get length of response msg and rx overflow status.
6438c2ecf20Sopenharmony_ci		 */
6448c2ecf20Sopenharmony_ci		rx_status = *((u32 *)resp_hdr);
6458c2ecf20Sopenharmony_ci		len = rx_status & RX_STATUS_LEN;
6468c2ecf20Sopenharmony_ci		dev_dbg(dev,
6478c2ecf20Sopenharmony_ci			"SPU response length %u bytes", len);
6488c2ecf20Sopenharmony_ci		if (unlikely(((rx_status & RX_STATUS_OVERFLOW) || (!len)))) {
6498c2ecf20Sopenharmony_ci			if (rx_status & RX_STATUS_OVERFLOW) {
6508c2ecf20Sopenharmony_ci				dev_err_ratelimited(dev,
6518c2ecf20Sopenharmony_ci						    "crypto receive overflow");
6528c2ecf20Sopenharmony_ci				pdcs->rx_oflow++;
6538c2ecf20Sopenharmony_ci			} else {
6548c2ecf20Sopenharmony_ci				dev_info_ratelimited(dev, "crypto rx len = 0");
6558c2ecf20Sopenharmony_ci			}
6568c2ecf20Sopenharmony_ci			return -EIO;
6578c2ecf20Sopenharmony_ci		}
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	dma_pool_free(pdcs->rx_buf_pool, resp_hdr, resp_hdr_daddr);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	mbox_chan_received_data(chan, &mssg);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	pdcs->pdc_replies++;
6658c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci/**
6698c2ecf20Sopenharmony_ci * pdc_receive() - Process as many responses as are available in the rx ring.
6708c2ecf20Sopenharmony_ci * @pdcs:  PDC state
6718c2ecf20Sopenharmony_ci *
6728c2ecf20Sopenharmony_ci * Called within the hard IRQ.
6738c2ecf20Sopenharmony_ci * Return:
6748c2ecf20Sopenharmony_ci */
6758c2ecf20Sopenharmony_cistatic int
6768c2ecf20Sopenharmony_cipdc_receive(struct pdc_state *pdcs)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	int rx_status;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	/* read last_rx_curr from register once */
6818c2ecf20Sopenharmony_ci	pdcs->last_rx_curr =
6828c2ecf20Sopenharmony_ci	    (ioread32((const void __iomem *)&pdcs->rxregs_64->status0) &
6838c2ecf20Sopenharmony_ci	     CRYPTO_D64_RS0_CD_MASK) / RING_ENTRY_SIZE;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	do {
6868c2ecf20Sopenharmony_ci		/* Could be many frames ready */
6878c2ecf20Sopenharmony_ci		rx_status = pdc_receive_one(pdcs);
6888c2ecf20Sopenharmony_ci	} while (rx_status == PDC_SUCCESS);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	return 0;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci/**
6948c2ecf20Sopenharmony_ci * pdc_tx_list_sg_add() - Add the buffers in a scatterlist to the transmit
6958c2ecf20Sopenharmony_ci * descriptors for a given SPU. The scatterlist buffers contain the data for a
6968c2ecf20Sopenharmony_ci * SPU request message.
6978c2ecf20Sopenharmony_ci * @spu_idx:   The index of the SPU to submit the request to, [0, max_spu)
6988c2ecf20Sopenharmony_ci * @sg:        Scatterlist whose buffers contain part of the SPU request
6998c2ecf20Sopenharmony_ci *
7008c2ecf20Sopenharmony_ci * If a scatterlist buffer is larger than PDC_DMA_BUF_MAX, multiple descriptors
7018c2ecf20Sopenharmony_ci * are written for that buffer, each <= PDC_DMA_BUF_MAX byte in length.
7028c2ecf20Sopenharmony_ci *
7038c2ecf20Sopenharmony_ci * Return: PDC_SUCCESS if successful
7048c2ecf20Sopenharmony_ci *         < 0 otherwise
7058c2ecf20Sopenharmony_ci */
7068c2ecf20Sopenharmony_cistatic int pdc_tx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	u32 flags = 0;
7098c2ecf20Sopenharmony_ci	u32 eot;
7108c2ecf20Sopenharmony_ci	u32 tx_avail;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/*
7138c2ecf20Sopenharmony_ci	 * Num descriptors needed. Conservatively assume we need a descriptor
7148c2ecf20Sopenharmony_ci	 * for every entry in sg.
7158c2ecf20Sopenharmony_ci	 */
7168c2ecf20Sopenharmony_ci	u32 num_desc;
7178c2ecf20Sopenharmony_ci	u32 desc_w = 0;	/* Number of tx descriptors written */
7188c2ecf20Sopenharmony_ci	u32 bufcnt;	/* Number of bytes of buffer pointed to by descriptor */
7198c2ecf20Sopenharmony_ci	dma_addr_t databufptr;	/* DMA address to put in descriptor */
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	num_desc = (u32)sg_nents(sg);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/* check whether enough tx descriptors are available */
7248c2ecf20Sopenharmony_ci	tx_avail = pdcs->ntxpost - NTXDACTIVE(pdcs->txin, pdcs->txout,
7258c2ecf20Sopenharmony_ci					      pdcs->ntxpost);
7268c2ecf20Sopenharmony_ci	if (unlikely(num_desc > tx_avail)) {
7278c2ecf20Sopenharmony_ci		pdcs->txnobuf++;
7288c2ecf20Sopenharmony_ci		return -ENOSPC;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/* build tx descriptors */
7328c2ecf20Sopenharmony_ci	if (pdcs->tx_msg_start == pdcs->txout) {
7338c2ecf20Sopenharmony_ci		/* Start of frame */
7348c2ecf20Sopenharmony_ci		pdcs->txin_numd[pdcs->tx_msg_start] = 0;
7358c2ecf20Sopenharmony_ci		pdcs->src_sg[pdcs->txout] = sg;
7368c2ecf20Sopenharmony_ci		flags = D64_CTRL1_SOF;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	while (sg) {
7408c2ecf20Sopenharmony_ci		if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
7418c2ecf20Sopenharmony_ci			eot = D64_CTRL1_EOT;
7428c2ecf20Sopenharmony_ci		else
7438c2ecf20Sopenharmony_ci			eot = 0;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci		/*
7468c2ecf20Sopenharmony_ci		 * If sg buffer larger than PDC limit, split across
7478c2ecf20Sopenharmony_ci		 * multiple descriptors
7488c2ecf20Sopenharmony_ci		 */
7498c2ecf20Sopenharmony_ci		bufcnt = sg_dma_len(sg);
7508c2ecf20Sopenharmony_ci		databufptr = sg_dma_address(sg);
7518c2ecf20Sopenharmony_ci		while (bufcnt > PDC_DMA_BUF_MAX) {
7528c2ecf20Sopenharmony_ci			pdc_build_txd(pdcs, databufptr, PDC_DMA_BUF_MAX,
7538c2ecf20Sopenharmony_ci				      flags | eot);
7548c2ecf20Sopenharmony_ci			desc_w++;
7558c2ecf20Sopenharmony_ci			bufcnt -= PDC_DMA_BUF_MAX;
7568c2ecf20Sopenharmony_ci			databufptr += PDC_DMA_BUF_MAX;
7578c2ecf20Sopenharmony_ci			if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
7588c2ecf20Sopenharmony_ci				eot = D64_CTRL1_EOT;
7598c2ecf20Sopenharmony_ci			else
7608c2ecf20Sopenharmony_ci				eot = 0;
7618c2ecf20Sopenharmony_ci		}
7628c2ecf20Sopenharmony_ci		sg = sg_next(sg);
7638c2ecf20Sopenharmony_ci		if (!sg)
7648c2ecf20Sopenharmony_ci			/* Writing last descriptor for frame */
7658c2ecf20Sopenharmony_ci			flags |= (D64_CTRL1_EOF | D64_CTRL1_IOC);
7668c2ecf20Sopenharmony_ci		pdc_build_txd(pdcs, databufptr, bufcnt, flags | eot);
7678c2ecf20Sopenharmony_ci		desc_w++;
7688c2ecf20Sopenharmony_ci		/* Clear start of frame after first descriptor */
7698c2ecf20Sopenharmony_ci		flags &= ~D64_CTRL1_SOF;
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci	pdcs->txin_numd[pdcs->tx_msg_start] += desc_w;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci/**
7778c2ecf20Sopenharmony_ci * pdc_tx_list_final() - Initiate DMA transfer of last frame written to tx
7788c2ecf20Sopenharmony_ci * ring.
7798c2ecf20Sopenharmony_ci * @pdcs:  PDC state for SPU to process the request
7808c2ecf20Sopenharmony_ci *
7818c2ecf20Sopenharmony_ci * Sets the index of the last descriptor written in both the rx and tx ring.
7828c2ecf20Sopenharmony_ci *
7838c2ecf20Sopenharmony_ci * Return: PDC_SUCCESS
7848c2ecf20Sopenharmony_ci */
7858c2ecf20Sopenharmony_cistatic int pdc_tx_list_final(struct pdc_state *pdcs)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	/*
7888c2ecf20Sopenharmony_ci	 * write barrier to ensure all register writes are complete
7898c2ecf20Sopenharmony_ci	 * before chip starts to process new request
7908c2ecf20Sopenharmony_ci	 */
7918c2ecf20Sopenharmony_ci	wmb();
7928c2ecf20Sopenharmony_ci	iowrite32(pdcs->rxout << 4, &pdcs->rxregs_64->ptr);
7938c2ecf20Sopenharmony_ci	iowrite32(pdcs->txout << 4, &pdcs->txregs_64->ptr);
7948c2ecf20Sopenharmony_ci	pdcs->pdc_requests++;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci/**
8008c2ecf20Sopenharmony_ci * pdc_rx_list_init() - Start a new receive descriptor list for a given PDC.
8018c2ecf20Sopenharmony_ci * @pdcs:   PDC state for SPU handling request
8028c2ecf20Sopenharmony_ci * @dst_sg: scatterlist providing rx buffers for response to be returned to
8038c2ecf20Sopenharmony_ci *	    mailbox client
8048c2ecf20Sopenharmony_ci * @ctx:    Opaque context for this request
8058c2ecf20Sopenharmony_ci *
8068c2ecf20Sopenharmony_ci * Posts a single receive descriptor to hold the metadata that precedes a
8078c2ecf20Sopenharmony_ci * response. For example, with SPU-M, the metadata is a 32-byte DMA header and
8088c2ecf20Sopenharmony_ci * an 8-byte BCM header. Moves the msg_start descriptor indexes for both tx and
8098c2ecf20Sopenharmony_ci * rx to indicate the start of a new message.
8108c2ecf20Sopenharmony_ci *
8118c2ecf20Sopenharmony_ci * Return:  PDC_SUCCESS if successful
8128c2ecf20Sopenharmony_ci *          < 0 if an error (e.g., rx ring is full)
8138c2ecf20Sopenharmony_ci */
8148c2ecf20Sopenharmony_cistatic int pdc_rx_list_init(struct pdc_state *pdcs, struct scatterlist *dst_sg,
8158c2ecf20Sopenharmony_ci			    void *ctx)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	u32 flags = 0;
8188c2ecf20Sopenharmony_ci	u32 rx_avail;
8198c2ecf20Sopenharmony_ci	u32 rx_pkt_cnt = 1;	/* Adding a single rx buffer */
8208c2ecf20Sopenharmony_ci	dma_addr_t daddr;
8218c2ecf20Sopenharmony_ci	void *vaddr;
8228c2ecf20Sopenharmony_ci	struct pdc_rx_ctx *rx_ctx;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
8258c2ecf20Sopenharmony_ci					      pdcs->nrxpost);
8268c2ecf20Sopenharmony_ci	if (unlikely(rx_pkt_cnt > rx_avail)) {
8278c2ecf20Sopenharmony_ci		pdcs->rxnobuf++;
8288c2ecf20Sopenharmony_ci		return -ENOSPC;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/* allocate a buffer for the dma rx status */
8328c2ecf20Sopenharmony_ci	vaddr = dma_pool_zalloc(pdcs->rx_buf_pool, GFP_ATOMIC, &daddr);
8338c2ecf20Sopenharmony_ci	if (unlikely(!vaddr))
8348c2ecf20Sopenharmony_ci		return -ENOMEM;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	/*
8378c2ecf20Sopenharmony_ci	 * Update msg_start indexes for both tx and rx to indicate the start
8388c2ecf20Sopenharmony_ci	 * of a new sequence of descriptor indexes that contain the fragments
8398c2ecf20Sopenharmony_ci	 * of the same message.
8408c2ecf20Sopenharmony_ci	 */
8418c2ecf20Sopenharmony_ci	pdcs->rx_msg_start = pdcs->rxout;
8428c2ecf20Sopenharmony_ci	pdcs->tx_msg_start = pdcs->txout;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	/* This is always the first descriptor in the receive sequence */
8458c2ecf20Sopenharmony_ci	flags = D64_CTRL1_SOF;
8468c2ecf20Sopenharmony_ci	pdcs->rx_ctx[pdcs->rx_msg_start].rxin_numd = 1;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
8498c2ecf20Sopenharmony_ci		flags |= D64_CTRL1_EOT;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	rx_ctx = &pdcs->rx_ctx[pdcs->rxout];
8528c2ecf20Sopenharmony_ci	rx_ctx->rxp_ctx = ctx;
8538c2ecf20Sopenharmony_ci	rx_ctx->dst_sg = dst_sg;
8548c2ecf20Sopenharmony_ci	rx_ctx->resp_hdr = vaddr;
8558c2ecf20Sopenharmony_ci	rx_ctx->resp_hdr_daddr = daddr;
8568c2ecf20Sopenharmony_ci	pdc_build_rxd(pdcs, daddr, pdcs->pdc_resp_hdr_len, flags);
8578c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
8588c2ecf20Sopenharmony_ci}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci/**
8618c2ecf20Sopenharmony_ci * pdc_rx_list_sg_add() - Add the buffers in a scatterlist to the receive
8628c2ecf20Sopenharmony_ci * descriptors for a given SPU. The caller must have already DMA mapped the
8638c2ecf20Sopenharmony_ci * scatterlist.
8648c2ecf20Sopenharmony_ci * @spu_idx:    Indicates which SPU the buffers are for
8658c2ecf20Sopenharmony_ci * @sg:         Scatterlist whose buffers are added to the receive ring
8668c2ecf20Sopenharmony_ci *
8678c2ecf20Sopenharmony_ci * If a receive buffer in the scatterlist is larger than PDC_DMA_BUF_MAX,
8688c2ecf20Sopenharmony_ci * multiple receive descriptors are written, each with a buffer <=
8698c2ecf20Sopenharmony_ci * PDC_DMA_BUF_MAX.
8708c2ecf20Sopenharmony_ci *
8718c2ecf20Sopenharmony_ci * Return: PDC_SUCCESS if successful
8728c2ecf20Sopenharmony_ci *         < 0 otherwise (e.g., receive ring is full)
8738c2ecf20Sopenharmony_ci */
8748c2ecf20Sopenharmony_cistatic int pdc_rx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	u32 flags = 0;
8778c2ecf20Sopenharmony_ci	u32 rx_avail;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	/*
8808c2ecf20Sopenharmony_ci	 * Num descriptors needed. Conservatively assume we need a descriptor
8818c2ecf20Sopenharmony_ci	 * for every entry from our starting point in the scatterlist.
8828c2ecf20Sopenharmony_ci	 */
8838c2ecf20Sopenharmony_ci	u32 num_desc;
8848c2ecf20Sopenharmony_ci	u32 desc_w = 0;	/* Number of tx descriptors written */
8858c2ecf20Sopenharmony_ci	u32 bufcnt;	/* Number of bytes of buffer pointed to by descriptor */
8868c2ecf20Sopenharmony_ci	dma_addr_t databufptr;	/* DMA address to put in descriptor */
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	num_desc = (u32)sg_nents(sg);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
8918c2ecf20Sopenharmony_ci					      pdcs->nrxpost);
8928c2ecf20Sopenharmony_ci	if (unlikely(num_desc > rx_avail)) {
8938c2ecf20Sopenharmony_ci		pdcs->rxnobuf++;
8948c2ecf20Sopenharmony_ci		return -ENOSPC;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	while (sg) {
8988c2ecf20Sopenharmony_ci		if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
8998c2ecf20Sopenharmony_ci			flags = D64_CTRL1_EOT;
9008c2ecf20Sopenharmony_ci		else
9018c2ecf20Sopenharmony_ci			flags = 0;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci		/*
9048c2ecf20Sopenharmony_ci		 * If sg buffer larger than PDC limit, split across
9058c2ecf20Sopenharmony_ci		 * multiple descriptors
9068c2ecf20Sopenharmony_ci		 */
9078c2ecf20Sopenharmony_ci		bufcnt = sg_dma_len(sg);
9088c2ecf20Sopenharmony_ci		databufptr = sg_dma_address(sg);
9098c2ecf20Sopenharmony_ci		while (bufcnt > PDC_DMA_BUF_MAX) {
9108c2ecf20Sopenharmony_ci			pdc_build_rxd(pdcs, databufptr, PDC_DMA_BUF_MAX, flags);
9118c2ecf20Sopenharmony_ci			desc_w++;
9128c2ecf20Sopenharmony_ci			bufcnt -= PDC_DMA_BUF_MAX;
9138c2ecf20Sopenharmony_ci			databufptr += PDC_DMA_BUF_MAX;
9148c2ecf20Sopenharmony_ci			if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
9158c2ecf20Sopenharmony_ci				flags = D64_CTRL1_EOT;
9168c2ecf20Sopenharmony_ci			else
9178c2ecf20Sopenharmony_ci				flags = 0;
9188c2ecf20Sopenharmony_ci		}
9198c2ecf20Sopenharmony_ci		pdc_build_rxd(pdcs, databufptr, bufcnt, flags);
9208c2ecf20Sopenharmony_ci		desc_w++;
9218c2ecf20Sopenharmony_ci		sg = sg_next(sg);
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci	pdcs->rx_ctx[pdcs->rx_msg_start].rxin_numd += desc_w;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
9268c2ecf20Sopenharmony_ci}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci/**
9298c2ecf20Sopenharmony_ci * pdc_irq_handler() - Interrupt handler called in interrupt context.
9308c2ecf20Sopenharmony_ci * @irq:      Interrupt number that has fired
9318c2ecf20Sopenharmony_ci * @data:     device struct for DMA engine that generated the interrupt
9328c2ecf20Sopenharmony_ci *
9338c2ecf20Sopenharmony_ci * We have to clear the device interrupt status flags here. So cache the
9348c2ecf20Sopenharmony_ci * status for later use in the thread function. Other than that, just return
9358c2ecf20Sopenharmony_ci * WAKE_THREAD to invoke the thread function.
9368c2ecf20Sopenharmony_ci *
9378c2ecf20Sopenharmony_ci * Return: IRQ_WAKE_THREAD if interrupt is ours
9388c2ecf20Sopenharmony_ci *         IRQ_NONE otherwise
9398c2ecf20Sopenharmony_ci */
9408c2ecf20Sopenharmony_cistatic irqreturn_t pdc_irq_handler(int irq, void *data)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	struct device *dev = (struct device *)data;
9438c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = dev_get_drvdata(dev);
9448c2ecf20Sopenharmony_ci	u32 intstatus = ioread32(pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	if (unlikely(intstatus == 0))
9478c2ecf20Sopenharmony_ci		return IRQ_NONE;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	/* Disable interrupts until soft handler runs */
9508c2ecf20Sopenharmony_ci	iowrite32(0, pdcs->pdc_reg_vbase + PDC_INTMASK_OFFSET);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	/* Clear interrupt flags in device */
9538c2ecf20Sopenharmony_ci	iowrite32(intstatus, pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	/* Wakeup IRQ thread */
9568c2ecf20Sopenharmony_ci	tasklet_schedule(&pdcs->rx_tasklet);
9578c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci/**
9618c2ecf20Sopenharmony_ci * pdc_tasklet_cb() - Tasklet callback that runs the deferred processing after
9628c2ecf20Sopenharmony_ci * a DMA receive interrupt. Reenables the receive interrupt.
9638c2ecf20Sopenharmony_ci * @data: PDC state structure
9648c2ecf20Sopenharmony_ci */
9658c2ecf20Sopenharmony_cistatic void pdc_tasklet_cb(struct tasklet_struct *t)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = from_tasklet(pdcs, t, rx_tasklet);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	pdc_receive(pdcs);
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	/* reenable interrupts */
9728c2ecf20Sopenharmony_ci	iowrite32(PDC_INTMASK, pdcs->pdc_reg_vbase + PDC_INTMASK_OFFSET);
9738c2ecf20Sopenharmony_ci}
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci/**
9768c2ecf20Sopenharmony_ci * pdc_ring_init() - Allocate DMA rings and initialize constant fields of
9778c2ecf20Sopenharmony_ci * descriptors in one ringset.
9788c2ecf20Sopenharmony_ci * @pdcs:    PDC instance state
9798c2ecf20Sopenharmony_ci * @ringset: index of ringset being used
9808c2ecf20Sopenharmony_ci *
9818c2ecf20Sopenharmony_ci * Return: PDC_SUCCESS if ring initialized
9828c2ecf20Sopenharmony_ci *         < 0 otherwise
9838c2ecf20Sopenharmony_ci */
9848c2ecf20Sopenharmony_cistatic int pdc_ring_init(struct pdc_state *pdcs, int ringset)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	int i;
9878c2ecf20Sopenharmony_ci	int err = PDC_SUCCESS;
9888c2ecf20Sopenharmony_ci	struct dma64 *dma_reg;
9898c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
9908c2ecf20Sopenharmony_ci	struct pdc_ring_alloc tx;
9918c2ecf20Sopenharmony_ci	struct pdc_ring_alloc rx;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	/* Allocate tx ring */
9948c2ecf20Sopenharmony_ci	tx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &tx.dmabase);
9958c2ecf20Sopenharmony_ci	if (unlikely(!tx.vbase)) {
9968c2ecf20Sopenharmony_ci		err = -ENOMEM;
9978c2ecf20Sopenharmony_ci		goto done;
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* Allocate rx ring */
10018c2ecf20Sopenharmony_ci	rx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &rx.dmabase);
10028c2ecf20Sopenharmony_ci	if (unlikely(!rx.vbase)) {
10038c2ecf20Sopenharmony_ci		err = -ENOMEM;
10048c2ecf20Sopenharmony_ci		goto fail_dealloc;
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	dev_dbg(dev, " - base DMA addr of tx ring      %pad", &tx.dmabase);
10088c2ecf20Sopenharmony_ci	dev_dbg(dev, " - base virtual addr of tx ring  %p", tx.vbase);
10098c2ecf20Sopenharmony_ci	dev_dbg(dev, " - base DMA addr of rx ring      %pad", &rx.dmabase);
10108c2ecf20Sopenharmony_ci	dev_dbg(dev, " - base virtual addr of rx ring  %p", rx.vbase);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	memcpy(&pdcs->tx_ring_alloc, &tx, sizeof(tx));
10138c2ecf20Sopenharmony_ci	memcpy(&pdcs->rx_ring_alloc, &rx, sizeof(rx));
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	pdcs->rxin = 0;
10168c2ecf20Sopenharmony_ci	pdcs->rx_msg_start = 0;
10178c2ecf20Sopenharmony_ci	pdcs->last_rx_curr = 0;
10188c2ecf20Sopenharmony_ci	pdcs->rxout = 0;
10198c2ecf20Sopenharmony_ci	pdcs->txin = 0;
10208c2ecf20Sopenharmony_ci	pdcs->tx_msg_start = 0;
10218c2ecf20Sopenharmony_ci	pdcs->txout = 0;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	/* Set descriptor array base addresses */
10248c2ecf20Sopenharmony_ci	pdcs->txd_64 = (struct dma64dd *)pdcs->tx_ring_alloc.vbase;
10258c2ecf20Sopenharmony_ci	pdcs->rxd_64 = (struct dma64dd *)pdcs->rx_ring_alloc.vbase;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	/* Tell device the base DMA address of each ring */
10288c2ecf20Sopenharmony_ci	dma_reg = &pdcs->regs->dmaregs[ringset];
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	/* But first disable DMA and set curptr to 0 for both TX & RX */
10318c2ecf20Sopenharmony_ci	iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
10328c2ecf20Sopenharmony_ci	iowrite32((PDC_RX_CTL + (pdcs->rx_status_len << 1)),
10338c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.control);
10348c2ecf20Sopenharmony_ci	iowrite32(0, &dma_reg->dmaxmt.ptr);
10358c2ecf20Sopenharmony_ci	iowrite32(0, &dma_reg->dmarcv.ptr);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	/* Set base DMA addresses */
10388c2ecf20Sopenharmony_ci	iowrite32(lower_32_bits(pdcs->tx_ring_alloc.dmabase),
10398c2ecf20Sopenharmony_ci		  &dma_reg->dmaxmt.addrlow);
10408c2ecf20Sopenharmony_ci	iowrite32(upper_32_bits(pdcs->tx_ring_alloc.dmabase),
10418c2ecf20Sopenharmony_ci		  &dma_reg->dmaxmt.addrhigh);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	iowrite32(lower_32_bits(pdcs->rx_ring_alloc.dmabase),
10448c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.addrlow);
10458c2ecf20Sopenharmony_ci	iowrite32(upper_32_bits(pdcs->rx_ring_alloc.dmabase),
10468c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.addrhigh);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	/* Re-enable DMA */
10498c2ecf20Sopenharmony_ci	iowrite32(PDC_TX_CTL | PDC_TX_ENABLE, &dma_reg->dmaxmt.control);
10508c2ecf20Sopenharmony_ci	iowrite32((PDC_RX_CTL | PDC_RX_ENABLE | (pdcs->rx_status_len << 1)),
10518c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.control);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/* Initialize descriptors */
10548c2ecf20Sopenharmony_ci	for (i = 0; i < PDC_RING_ENTRIES; i++) {
10558c2ecf20Sopenharmony_ci		/* Every tx descriptor can be used for start of frame. */
10568c2ecf20Sopenharmony_ci		if (i != pdcs->ntxpost) {
10578c2ecf20Sopenharmony_ci			iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF,
10588c2ecf20Sopenharmony_ci				  &pdcs->txd_64[i].ctrl1);
10598c2ecf20Sopenharmony_ci		} else {
10608c2ecf20Sopenharmony_ci			/* Last descriptor in ringset. Set End of Table. */
10618c2ecf20Sopenharmony_ci			iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF |
10628c2ecf20Sopenharmony_ci				  D64_CTRL1_EOT, &pdcs->txd_64[i].ctrl1);
10638c2ecf20Sopenharmony_ci		}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci		/* Every rx descriptor can be used for start of frame */
10668c2ecf20Sopenharmony_ci		if (i != pdcs->nrxpost) {
10678c2ecf20Sopenharmony_ci			iowrite32(D64_CTRL1_SOF,
10688c2ecf20Sopenharmony_ci				  &pdcs->rxd_64[i].ctrl1);
10698c2ecf20Sopenharmony_ci		} else {
10708c2ecf20Sopenharmony_ci			/* Last descriptor in ringset. Set End of Table. */
10718c2ecf20Sopenharmony_ci			iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOT,
10728c2ecf20Sopenharmony_ci				  &pdcs->rxd_64[i].ctrl1);
10738c2ecf20Sopenharmony_ci		}
10748c2ecf20Sopenharmony_ci	}
10758c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cifail_dealloc:
10788c2ecf20Sopenharmony_ci	dma_pool_free(pdcs->ring_pool, tx.vbase, tx.dmabase);
10798c2ecf20Sopenharmony_cidone:
10808c2ecf20Sopenharmony_ci	return err;
10818c2ecf20Sopenharmony_ci}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_cistatic void pdc_ring_free(struct pdc_state *pdcs)
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	if (pdcs->tx_ring_alloc.vbase) {
10868c2ecf20Sopenharmony_ci		dma_pool_free(pdcs->ring_pool, pdcs->tx_ring_alloc.vbase,
10878c2ecf20Sopenharmony_ci			      pdcs->tx_ring_alloc.dmabase);
10888c2ecf20Sopenharmony_ci		pdcs->tx_ring_alloc.vbase = NULL;
10898c2ecf20Sopenharmony_ci	}
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	if (pdcs->rx_ring_alloc.vbase) {
10928c2ecf20Sopenharmony_ci		dma_pool_free(pdcs->ring_pool, pdcs->rx_ring_alloc.vbase,
10938c2ecf20Sopenharmony_ci			      pdcs->rx_ring_alloc.dmabase);
10948c2ecf20Sopenharmony_ci		pdcs->rx_ring_alloc.vbase = NULL;
10958c2ecf20Sopenharmony_ci	}
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci/**
10998c2ecf20Sopenharmony_ci * pdc_desc_count() - Count the number of DMA descriptors that will be required
11008c2ecf20Sopenharmony_ci * for a given scatterlist. Account for the max length of a DMA buffer.
11018c2ecf20Sopenharmony_ci * @sg:    Scatterlist to be DMA'd
11028c2ecf20Sopenharmony_ci * Return: Number of descriptors required
11038c2ecf20Sopenharmony_ci */
11048c2ecf20Sopenharmony_cistatic u32 pdc_desc_count(struct scatterlist *sg)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	u32 cnt = 0;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	while (sg) {
11098c2ecf20Sopenharmony_ci		cnt += ((sg->length / PDC_DMA_BUF_MAX) + 1);
11108c2ecf20Sopenharmony_ci		sg = sg_next(sg);
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci	return cnt;
11138c2ecf20Sopenharmony_ci}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci/**
11168c2ecf20Sopenharmony_ci * pdc_rings_full() - Check whether the tx ring has room for tx_cnt descriptors
11178c2ecf20Sopenharmony_ci * and the rx ring has room for rx_cnt descriptors.
11188c2ecf20Sopenharmony_ci * @pdcs:  PDC state
11198c2ecf20Sopenharmony_ci * @tx_cnt: The number of descriptors required in the tx ring
11208c2ecf20Sopenharmony_ci * @rx_cnt: The number of descriptors required i the rx ring
11218c2ecf20Sopenharmony_ci *
11228c2ecf20Sopenharmony_ci * Return: true if one of the rings does not have enough space
11238c2ecf20Sopenharmony_ci *         false if sufficient space is available in both rings
11248c2ecf20Sopenharmony_ci */
11258c2ecf20Sopenharmony_cistatic bool pdc_rings_full(struct pdc_state *pdcs, int tx_cnt, int rx_cnt)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	u32 rx_avail;
11288c2ecf20Sopenharmony_ci	u32 tx_avail;
11298c2ecf20Sopenharmony_ci	bool full = false;
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	/* Check if the tx and rx rings are likely to have enough space */
11328c2ecf20Sopenharmony_ci	rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
11338c2ecf20Sopenharmony_ci					      pdcs->nrxpost);
11348c2ecf20Sopenharmony_ci	if (unlikely(rx_cnt > rx_avail)) {
11358c2ecf20Sopenharmony_ci		pdcs->rx_ring_full++;
11368c2ecf20Sopenharmony_ci		full = true;
11378c2ecf20Sopenharmony_ci	}
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	if (likely(!full)) {
11408c2ecf20Sopenharmony_ci		tx_avail = pdcs->ntxpost - NTXDACTIVE(pdcs->txin, pdcs->txout,
11418c2ecf20Sopenharmony_ci						      pdcs->ntxpost);
11428c2ecf20Sopenharmony_ci		if (unlikely(tx_cnt > tx_avail)) {
11438c2ecf20Sopenharmony_ci			pdcs->tx_ring_full++;
11448c2ecf20Sopenharmony_ci			full = true;
11458c2ecf20Sopenharmony_ci		}
11468c2ecf20Sopenharmony_ci	}
11478c2ecf20Sopenharmony_ci	return full;
11488c2ecf20Sopenharmony_ci}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci/**
11518c2ecf20Sopenharmony_ci * pdc_last_tx_done() - If both the tx and rx rings have at least
11528c2ecf20Sopenharmony_ci * PDC_RING_SPACE_MIN descriptors available, then indicate that the mailbox
11538c2ecf20Sopenharmony_ci * framework can submit another message.
11548c2ecf20Sopenharmony_ci * @chan:  mailbox channel to check
11558c2ecf20Sopenharmony_ci * Return: true if PDC can accept another message on this channel
11568c2ecf20Sopenharmony_ci */
11578c2ecf20Sopenharmony_cistatic bool pdc_last_tx_done(struct mbox_chan *chan)
11588c2ecf20Sopenharmony_ci{
11598c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = chan->con_priv;
11608c2ecf20Sopenharmony_ci	bool ret;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	if (unlikely(pdc_rings_full(pdcs, PDC_RING_SPACE_MIN,
11638c2ecf20Sopenharmony_ci				    PDC_RING_SPACE_MIN))) {
11648c2ecf20Sopenharmony_ci		pdcs->last_tx_not_done++;
11658c2ecf20Sopenharmony_ci		ret = false;
11668c2ecf20Sopenharmony_ci	} else {
11678c2ecf20Sopenharmony_ci		ret = true;
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci	return ret;
11708c2ecf20Sopenharmony_ci}
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci/**
11738c2ecf20Sopenharmony_ci * pdc_send_data() - mailbox send_data function
11748c2ecf20Sopenharmony_ci * @chan:	The mailbox channel on which the data is sent. The channel
11758c2ecf20Sopenharmony_ci *              corresponds to a DMA ringset.
11768c2ecf20Sopenharmony_ci * @data:	The mailbox message to be sent. The message must be a
11778c2ecf20Sopenharmony_ci *              brcm_message structure.
11788c2ecf20Sopenharmony_ci *
11798c2ecf20Sopenharmony_ci * This function is registered as the send_data function for the mailbox
11808c2ecf20Sopenharmony_ci * controller. From the destination scatterlist in the mailbox message, it
11818c2ecf20Sopenharmony_ci * creates a sequence of receive descriptors in the rx ring. From the source
11828c2ecf20Sopenharmony_ci * scatterlist, it creates a sequence of transmit descriptors in the tx ring.
11838c2ecf20Sopenharmony_ci * After creating the descriptors, it writes the rx ptr and tx ptr registers to
11848c2ecf20Sopenharmony_ci * initiate the DMA transfer.
11858c2ecf20Sopenharmony_ci *
11868c2ecf20Sopenharmony_ci * This function does the DMA map and unmap of the src and dst scatterlists in
11878c2ecf20Sopenharmony_ci * the mailbox message.
11888c2ecf20Sopenharmony_ci *
11898c2ecf20Sopenharmony_ci * Return: 0 if successful
11908c2ecf20Sopenharmony_ci *	   -ENOTSUPP if the mailbox message is a type this driver does not
11918c2ecf20Sopenharmony_ci *			support
11928c2ecf20Sopenharmony_ci *         < 0 if an error
11938c2ecf20Sopenharmony_ci */
11948c2ecf20Sopenharmony_cistatic int pdc_send_data(struct mbox_chan *chan, void *data)
11958c2ecf20Sopenharmony_ci{
11968c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = chan->con_priv;
11978c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
11988c2ecf20Sopenharmony_ci	struct brcm_message *mssg = data;
11998c2ecf20Sopenharmony_ci	int err = PDC_SUCCESS;
12008c2ecf20Sopenharmony_ci	int src_nent;
12018c2ecf20Sopenharmony_ci	int dst_nent;
12028c2ecf20Sopenharmony_ci	int nent;
12038c2ecf20Sopenharmony_ci	u32 tx_desc_req;
12048c2ecf20Sopenharmony_ci	u32 rx_desc_req;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	if (unlikely(mssg->type != BRCM_MESSAGE_SPU))
12078c2ecf20Sopenharmony_ci		return -ENOTSUPP;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	src_nent = sg_nents(mssg->spu.src);
12108c2ecf20Sopenharmony_ci	if (likely(src_nent)) {
12118c2ecf20Sopenharmony_ci		nent = dma_map_sg(dev, mssg->spu.src, src_nent, DMA_TO_DEVICE);
12128c2ecf20Sopenharmony_ci		if (unlikely(nent == 0))
12138c2ecf20Sopenharmony_ci			return -EIO;
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	dst_nent = sg_nents(mssg->spu.dst);
12178c2ecf20Sopenharmony_ci	if (likely(dst_nent)) {
12188c2ecf20Sopenharmony_ci		nent = dma_map_sg(dev, mssg->spu.dst, dst_nent,
12198c2ecf20Sopenharmony_ci				  DMA_FROM_DEVICE);
12208c2ecf20Sopenharmony_ci		if (unlikely(nent == 0)) {
12218c2ecf20Sopenharmony_ci			dma_unmap_sg(dev, mssg->spu.src, src_nent,
12228c2ecf20Sopenharmony_ci				     DMA_TO_DEVICE);
12238c2ecf20Sopenharmony_ci			return -EIO;
12248c2ecf20Sopenharmony_ci		}
12258c2ecf20Sopenharmony_ci	}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	/*
12288c2ecf20Sopenharmony_ci	 * Check if the tx and rx rings have enough space. Do this prior to
12298c2ecf20Sopenharmony_ci	 * writing any tx or rx descriptors. Need to ensure that we do not write
12308c2ecf20Sopenharmony_ci	 * a partial set of descriptors, or write just rx descriptors but
12318c2ecf20Sopenharmony_ci	 * corresponding tx descriptors don't fit. Note that we want this check
12328c2ecf20Sopenharmony_ci	 * and the entire sequence of descriptor to happen without another
12338c2ecf20Sopenharmony_ci	 * thread getting in. The channel spin lock in the mailbox framework
12348c2ecf20Sopenharmony_ci	 * ensures this.
12358c2ecf20Sopenharmony_ci	 */
12368c2ecf20Sopenharmony_ci	tx_desc_req = pdc_desc_count(mssg->spu.src);
12378c2ecf20Sopenharmony_ci	rx_desc_req = pdc_desc_count(mssg->spu.dst);
12388c2ecf20Sopenharmony_ci	if (unlikely(pdc_rings_full(pdcs, tx_desc_req, rx_desc_req + 1)))
12398c2ecf20Sopenharmony_ci		return -ENOSPC;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	/* Create rx descriptors to SPU catch response */
12428c2ecf20Sopenharmony_ci	err = pdc_rx_list_init(pdcs, mssg->spu.dst, mssg->ctx);
12438c2ecf20Sopenharmony_ci	err |= pdc_rx_list_sg_add(pdcs, mssg->spu.dst);
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	/* Create tx descriptors to submit SPU request */
12468c2ecf20Sopenharmony_ci	err |= pdc_tx_list_sg_add(pdcs, mssg->spu.src);
12478c2ecf20Sopenharmony_ci	err |= pdc_tx_list_final(pdcs);	/* initiate transfer */
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	if (unlikely(err))
12508c2ecf20Sopenharmony_ci		dev_err(&pdcs->pdev->dev,
12518c2ecf20Sopenharmony_ci			"%s failed with error %d", __func__, err);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	return err;
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic int pdc_startup(struct mbox_chan *chan)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	return pdc_ring_init(chan->con_priv, PDC_RINGSET);
12598c2ecf20Sopenharmony_ci}
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_cistatic void pdc_shutdown(struct mbox_chan *chan)
12628c2ecf20Sopenharmony_ci{
12638c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = chan->con_priv;
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	if (!pdcs)
12668c2ecf20Sopenharmony_ci		return;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	dev_dbg(&pdcs->pdev->dev,
12698c2ecf20Sopenharmony_ci		"Shutdown mailbox channel for PDC %u", pdcs->pdc_idx);
12708c2ecf20Sopenharmony_ci	pdc_ring_free(pdcs);
12718c2ecf20Sopenharmony_ci}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci/**
12748c2ecf20Sopenharmony_ci * pdc_hw_init() - Use the given initialization parameters to initialize the
12758c2ecf20Sopenharmony_ci * state for one of the PDCs.
12768c2ecf20Sopenharmony_ci * @pdcs:  state of the PDC
12778c2ecf20Sopenharmony_ci */
12788c2ecf20Sopenharmony_cistatic
12798c2ecf20Sopenharmony_civoid pdc_hw_init(struct pdc_state *pdcs)
12808c2ecf20Sopenharmony_ci{
12818c2ecf20Sopenharmony_ci	struct platform_device *pdev;
12828c2ecf20Sopenharmony_ci	struct device *dev;
12838c2ecf20Sopenharmony_ci	struct dma64 *dma_reg;
12848c2ecf20Sopenharmony_ci	int ringset = PDC_RINGSET;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	pdev = pdcs->pdev;
12878c2ecf20Sopenharmony_ci	dev = &pdev->dev;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	dev_dbg(dev, "PDC %u initial values:", pdcs->pdc_idx);
12908c2ecf20Sopenharmony_ci	dev_dbg(dev, "state structure:                   %p",
12918c2ecf20Sopenharmony_ci		pdcs);
12928c2ecf20Sopenharmony_ci	dev_dbg(dev, " - base virtual addr of hw regs    %p",
12938c2ecf20Sopenharmony_ci		pdcs->pdc_reg_vbase);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* initialize data structures */
12968c2ecf20Sopenharmony_ci	pdcs->regs = (struct pdc_regs *)pdcs->pdc_reg_vbase;
12978c2ecf20Sopenharmony_ci	pdcs->txregs_64 = (struct dma64_regs *)
12988c2ecf20Sopenharmony_ci	    (((u8 *)pdcs->pdc_reg_vbase) +
12998c2ecf20Sopenharmony_ci		     PDC_TXREGS_OFFSET + (sizeof(struct dma64) * ringset));
13008c2ecf20Sopenharmony_ci	pdcs->rxregs_64 = (struct dma64_regs *)
13018c2ecf20Sopenharmony_ci	    (((u8 *)pdcs->pdc_reg_vbase) +
13028c2ecf20Sopenharmony_ci		     PDC_RXREGS_OFFSET + (sizeof(struct dma64) * ringset));
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	pdcs->ntxd = PDC_RING_ENTRIES;
13058c2ecf20Sopenharmony_ci	pdcs->nrxd = PDC_RING_ENTRIES;
13068c2ecf20Sopenharmony_ci	pdcs->ntxpost = PDC_RING_ENTRIES - 1;
13078c2ecf20Sopenharmony_ci	pdcs->nrxpost = PDC_RING_ENTRIES - 1;
13088c2ecf20Sopenharmony_ci	iowrite32(0, &pdcs->regs->intmask);
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci	dma_reg = &pdcs->regs->dmaregs[ringset];
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	/* Configure DMA but will enable later in pdc_ring_init() */
13138c2ecf20Sopenharmony_ci	iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	iowrite32(PDC_RX_CTL + (pdcs->rx_status_len << 1),
13168c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.control);
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	/* Reset current index pointers after making sure DMA is disabled */
13198c2ecf20Sopenharmony_ci	iowrite32(0, &dma_reg->dmaxmt.ptr);
13208c2ecf20Sopenharmony_ci	iowrite32(0, &dma_reg->dmarcv.ptr);
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	if (pdcs->pdc_resp_hdr_len == PDC_SPU2_RESP_HDR_LEN)
13238c2ecf20Sopenharmony_ci		iowrite32(PDC_CKSUM_CTRL,
13248c2ecf20Sopenharmony_ci			  pdcs->pdc_reg_vbase + PDC_CKSUM_CTRL_OFFSET);
13258c2ecf20Sopenharmony_ci}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci/**
13288c2ecf20Sopenharmony_ci * pdc_hw_disable() - Disable the tx and rx control in the hw.
13298c2ecf20Sopenharmony_ci * @pdcs: PDC state structure
13308c2ecf20Sopenharmony_ci *
13318c2ecf20Sopenharmony_ci */
13328c2ecf20Sopenharmony_cistatic void pdc_hw_disable(struct pdc_state *pdcs)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	struct dma64 *dma_reg;
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	dma_reg = &pdcs->regs->dmaregs[PDC_RINGSET];
13378c2ecf20Sopenharmony_ci	iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
13388c2ecf20Sopenharmony_ci	iowrite32(PDC_RX_CTL + (pdcs->rx_status_len << 1),
13398c2ecf20Sopenharmony_ci		  &dma_reg->dmarcv.control);
13408c2ecf20Sopenharmony_ci}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci/**
13438c2ecf20Sopenharmony_ci * pdc_rx_buf_pool_create() - Pool of receive buffers used to catch the metadata
13448c2ecf20Sopenharmony_ci * header returned with each response message.
13458c2ecf20Sopenharmony_ci * @pdcs: PDC state structure
13468c2ecf20Sopenharmony_ci *
13478c2ecf20Sopenharmony_ci * The metadata is not returned to the mailbox client. So the PDC driver
13488c2ecf20Sopenharmony_ci * manages these buffers.
13498c2ecf20Sopenharmony_ci *
13508c2ecf20Sopenharmony_ci * Return: PDC_SUCCESS
13518c2ecf20Sopenharmony_ci *         -ENOMEM if pool creation fails
13528c2ecf20Sopenharmony_ci */
13538c2ecf20Sopenharmony_cistatic int pdc_rx_buf_pool_create(struct pdc_state *pdcs)
13548c2ecf20Sopenharmony_ci{
13558c2ecf20Sopenharmony_ci	struct platform_device *pdev;
13568c2ecf20Sopenharmony_ci	struct device *dev;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	pdev = pdcs->pdev;
13598c2ecf20Sopenharmony_ci	dev = &pdev->dev;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	pdcs->pdc_resp_hdr_len = pdcs->rx_status_len;
13628c2ecf20Sopenharmony_ci	if (pdcs->use_bcm_hdr)
13638c2ecf20Sopenharmony_ci		pdcs->pdc_resp_hdr_len += BCM_HDR_LEN;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	pdcs->rx_buf_pool = dma_pool_create("pdc rx bufs", dev,
13668c2ecf20Sopenharmony_ci					    pdcs->pdc_resp_hdr_len,
13678c2ecf20Sopenharmony_ci					    RX_BUF_ALIGN, 0);
13688c2ecf20Sopenharmony_ci	if (!pdcs->rx_buf_pool)
13698c2ecf20Sopenharmony_ci		return -ENOMEM;
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci/**
13758c2ecf20Sopenharmony_ci * pdc_interrupts_init() - Initialize the interrupt configuration for a PDC and
13768c2ecf20Sopenharmony_ci * specify a threaded IRQ handler for deferred handling of interrupts outside of
13778c2ecf20Sopenharmony_ci * interrupt context.
13788c2ecf20Sopenharmony_ci * @pdcs:   PDC state
13798c2ecf20Sopenharmony_ci *
13808c2ecf20Sopenharmony_ci * Set the interrupt mask for transmit and receive done.
13818c2ecf20Sopenharmony_ci * Set the lazy interrupt frame count to generate an interrupt for just one pkt.
13828c2ecf20Sopenharmony_ci *
13838c2ecf20Sopenharmony_ci * Return:  PDC_SUCCESS
13848c2ecf20Sopenharmony_ci *          <0 if threaded irq request fails
13858c2ecf20Sopenharmony_ci */
13868c2ecf20Sopenharmony_cistatic int pdc_interrupts_init(struct pdc_state *pdcs)
13878c2ecf20Sopenharmony_ci{
13888c2ecf20Sopenharmony_ci	struct platform_device *pdev = pdcs->pdev;
13898c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
13908c2ecf20Sopenharmony_ci	struct device_node *dn = pdev->dev.of_node;
13918c2ecf20Sopenharmony_ci	int err;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	/* interrupt configuration */
13948c2ecf20Sopenharmony_ci	iowrite32(PDC_INTMASK, pdcs->pdc_reg_vbase + PDC_INTMASK_OFFSET);
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	if (pdcs->hw_type == FA_HW)
13978c2ecf20Sopenharmony_ci		iowrite32(PDC_LAZY_INT, pdcs->pdc_reg_vbase +
13988c2ecf20Sopenharmony_ci			  FA_RCVLAZY0_OFFSET);
13998c2ecf20Sopenharmony_ci	else
14008c2ecf20Sopenharmony_ci		iowrite32(PDC_LAZY_INT, pdcs->pdc_reg_vbase +
14018c2ecf20Sopenharmony_ci			  PDC_RCVLAZY0_OFFSET);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	/* read irq from device tree */
14048c2ecf20Sopenharmony_ci	pdcs->pdc_irq = irq_of_parse_and_map(dn, 0);
14058c2ecf20Sopenharmony_ci	dev_dbg(dev, "pdc device %s irq %u for pdcs %p",
14068c2ecf20Sopenharmony_ci		dev_name(dev), pdcs->pdc_irq, pdcs);
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	err = devm_request_irq(dev, pdcs->pdc_irq, pdc_irq_handler, 0,
14098c2ecf20Sopenharmony_ci			       dev_name(dev), dev);
14108c2ecf20Sopenharmony_ci	if (err) {
14118c2ecf20Sopenharmony_ci		dev_err(dev, "IRQ %u request failed with err %d\n",
14128c2ecf20Sopenharmony_ci			pdcs->pdc_irq, err);
14138c2ecf20Sopenharmony_ci		return err;
14148c2ecf20Sopenharmony_ci	}
14158c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
14168c2ecf20Sopenharmony_ci}
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_cistatic const struct mbox_chan_ops pdc_mbox_chan_ops = {
14198c2ecf20Sopenharmony_ci	.send_data = pdc_send_data,
14208c2ecf20Sopenharmony_ci	.last_tx_done = pdc_last_tx_done,
14218c2ecf20Sopenharmony_ci	.startup = pdc_startup,
14228c2ecf20Sopenharmony_ci	.shutdown = pdc_shutdown
14238c2ecf20Sopenharmony_ci};
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci/**
14268c2ecf20Sopenharmony_ci * pdc_mb_init() - Initialize the mailbox controller.
14278c2ecf20Sopenharmony_ci * @pdcs:  PDC state
14288c2ecf20Sopenharmony_ci *
14298c2ecf20Sopenharmony_ci * Each PDC is a mailbox controller. Each ringset is a mailbox channel. Kernel
14308c2ecf20Sopenharmony_ci * driver only uses one ringset and thus one mb channel. PDC uses the transmit
14318c2ecf20Sopenharmony_ci * complete interrupt to determine when a mailbox message has successfully been
14328c2ecf20Sopenharmony_ci * transmitted.
14338c2ecf20Sopenharmony_ci *
14348c2ecf20Sopenharmony_ci * Return: 0 on success
14358c2ecf20Sopenharmony_ci *         < 0 if there is an allocation or registration failure
14368c2ecf20Sopenharmony_ci */
14378c2ecf20Sopenharmony_cistatic int pdc_mb_init(struct pdc_state *pdcs)
14388c2ecf20Sopenharmony_ci{
14398c2ecf20Sopenharmony_ci	struct device *dev = &pdcs->pdev->dev;
14408c2ecf20Sopenharmony_ci	struct mbox_controller *mbc;
14418c2ecf20Sopenharmony_ci	int chan_index;
14428c2ecf20Sopenharmony_ci	int err;
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	mbc = &pdcs->mbc;
14458c2ecf20Sopenharmony_ci	mbc->dev = dev;
14468c2ecf20Sopenharmony_ci	mbc->ops = &pdc_mbox_chan_ops;
14478c2ecf20Sopenharmony_ci	mbc->num_chans = 1;
14488c2ecf20Sopenharmony_ci	mbc->chans = devm_kcalloc(dev, mbc->num_chans, sizeof(*mbc->chans),
14498c2ecf20Sopenharmony_ci				  GFP_KERNEL);
14508c2ecf20Sopenharmony_ci	if (!mbc->chans)
14518c2ecf20Sopenharmony_ci		return -ENOMEM;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	mbc->txdone_irq = false;
14548c2ecf20Sopenharmony_ci	mbc->txdone_poll = true;
14558c2ecf20Sopenharmony_ci	mbc->txpoll_period = 1;
14568c2ecf20Sopenharmony_ci	for (chan_index = 0; chan_index < mbc->num_chans; chan_index++)
14578c2ecf20Sopenharmony_ci		mbc->chans[chan_index].con_priv = pdcs;
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	/* Register mailbox controller */
14608c2ecf20Sopenharmony_ci	err = devm_mbox_controller_register(dev, mbc);
14618c2ecf20Sopenharmony_ci	if (err) {
14628c2ecf20Sopenharmony_ci		dev_crit(dev,
14638c2ecf20Sopenharmony_ci			 "Failed to register PDC mailbox controller. Error %d.",
14648c2ecf20Sopenharmony_ci			 err);
14658c2ecf20Sopenharmony_ci		return err;
14668c2ecf20Sopenharmony_ci	}
14678c2ecf20Sopenharmony_ci	return 0;
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci/* Device tree API */
14718c2ecf20Sopenharmony_cistatic const int pdc_hw = PDC_HW;
14728c2ecf20Sopenharmony_cistatic const int fa_hw = FA_HW;
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_cistatic const struct of_device_id pdc_mbox_of_match[] = {
14758c2ecf20Sopenharmony_ci	{.compatible = "brcm,iproc-pdc-mbox", .data = &pdc_hw},
14768c2ecf20Sopenharmony_ci	{.compatible = "brcm,iproc-fa2-mbox", .data = &fa_hw},
14778c2ecf20Sopenharmony_ci	{ /* sentinel */ }
14788c2ecf20Sopenharmony_ci};
14798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, pdc_mbox_of_match);
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci/**
14828c2ecf20Sopenharmony_ci * pdc_dt_read() - Read application-specific data from device tree.
14838c2ecf20Sopenharmony_ci * @pdev:  Platform device
14848c2ecf20Sopenharmony_ci * @pdcs:  PDC state
14858c2ecf20Sopenharmony_ci *
14868c2ecf20Sopenharmony_ci * Reads the number of bytes of receive status that precede each received frame.
14878c2ecf20Sopenharmony_ci * Reads whether transmit and received frames should be preceded by an 8-byte
14888c2ecf20Sopenharmony_ci * BCM header.
14898c2ecf20Sopenharmony_ci *
14908c2ecf20Sopenharmony_ci * Return: 0 if successful
14918c2ecf20Sopenharmony_ci *         -ENODEV if device not available
14928c2ecf20Sopenharmony_ci */
14938c2ecf20Sopenharmony_cistatic int pdc_dt_read(struct platform_device *pdev, struct pdc_state *pdcs)
14948c2ecf20Sopenharmony_ci{
14958c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
14968c2ecf20Sopenharmony_ci	struct device_node *dn = pdev->dev.of_node;
14978c2ecf20Sopenharmony_ci	const struct of_device_id *match;
14988c2ecf20Sopenharmony_ci	const int *hw_type;
14998c2ecf20Sopenharmony_ci	int err;
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	err = of_property_read_u32(dn, "brcm,rx-status-len",
15028c2ecf20Sopenharmony_ci				   &pdcs->rx_status_len);
15038c2ecf20Sopenharmony_ci	if (err < 0)
15048c2ecf20Sopenharmony_ci		dev_err(dev,
15058c2ecf20Sopenharmony_ci			"%s failed to get DMA receive status length from device tree",
15068c2ecf20Sopenharmony_ci			__func__);
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	pdcs->use_bcm_hdr = of_property_read_bool(dn, "brcm,use-bcm-hdr");
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	pdcs->hw_type = PDC_HW;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	match = of_match_device(of_match_ptr(pdc_mbox_of_match), dev);
15138c2ecf20Sopenharmony_ci	if (match != NULL) {
15148c2ecf20Sopenharmony_ci		hw_type = match->data;
15158c2ecf20Sopenharmony_ci		pdcs->hw_type = *hw_type;
15168c2ecf20Sopenharmony_ci	}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci	return 0;
15198c2ecf20Sopenharmony_ci}
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci/**
15228c2ecf20Sopenharmony_ci * pdc_probe() - Probe function for PDC driver.
15238c2ecf20Sopenharmony_ci * @pdev:   PDC platform device
15248c2ecf20Sopenharmony_ci *
15258c2ecf20Sopenharmony_ci * Reserve and map register regions defined in device tree.
15268c2ecf20Sopenharmony_ci * Allocate and initialize tx and rx DMA rings.
15278c2ecf20Sopenharmony_ci * Initialize a mailbox controller for each PDC.
15288c2ecf20Sopenharmony_ci *
15298c2ecf20Sopenharmony_ci * Return: 0 if successful
15308c2ecf20Sopenharmony_ci *         < 0 if an error
15318c2ecf20Sopenharmony_ci */
15328c2ecf20Sopenharmony_cistatic int pdc_probe(struct platform_device *pdev)
15338c2ecf20Sopenharmony_ci{
15348c2ecf20Sopenharmony_ci	int err = 0;
15358c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
15368c2ecf20Sopenharmony_ci	struct resource *pdc_regs;
15378c2ecf20Sopenharmony_ci	struct pdc_state *pdcs;
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	/* PDC state for one SPU */
15408c2ecf20Sopenharmony_ci	pdcs = devm_kzalloc(dev, sizeof(*pdcs), GFP_KERNEL);
15418c2ecf20Sopenharmony_ci	if (!pdcs) {
15428c2ecf20Sopenharmony_ci		err = -ENOMEM;
15438c2ecf20Sopenharmony_ci		goto cleanup;
15448c2ecf20Sopenharmony_ci	}
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	pdcs->pdev = pdev;
15478c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pdcs);
15488c2ecf20Sopenharmony_ci	pdcs->pdc_idx = pdcg.num_spu;
15498c2ecf20Sopenharmony_ci	pdcg.num_spu++;
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(39));
15528c2ecf20Sopenharmony_ci	if (err) {
15538c2ecf20Sopenharmony_ci		dev_warn(dev, "PDC device cannot perform DMA. Error %d.", err);
15548c2ecf20Sopenharmony_ci		goto cleanup;
15558c2ecf20Sopenharmony_ci	}
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	/* Create DMA pool for tx ring */
15588c2ecf20Sopenharmony_ci	pdcs->ring_pool = dma_pool_create("pdc rings", dev, PDC_RING_SIZE,
15598c2ecf20Sopenharmony_ci					  RING_ALIGN, 0);
15608c2ecf20Sopenharmony_ci	if (!pdcs->ring_pool) {
15618c2ecf20Sopenharmony_ci		err = -ENOMEM;
15628c2ecf20Sopenharmony_ci		goto cleanup;
15638c2ecf20Sopenharmony_ci	}
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	err = pdc_dt_read(pdev, pdcs);
15668c2ecf20Sopenharmony_ci	if (err)
15678c2ecf20Sopenharmony_ci		goto cleanup_ring_pool;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	pdc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
15708c2ecf20Sopenharmony_ci	if (!pdc_regs) {
15718c2ecf20Sopenharmony_ci		err = -ENODEV;
15728c2ecf20Sopenharmony_ci		goto cleanup_ring_pool;
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci	dev_dbg(dev, "PDC register region res.start = %pa, res.end = %pa",
15758c2ecf20Sopenharmony_ci		&pdc_regs->start, &pdc_regs->end);
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	pdcs->pdc_reg_vbase = devm_ioremap_resource(&pdev->dev, pdc_regs);
15788c2ecf20Sopenharmony_ci	if (IS_ERR(pdcs->pdc_reg_vbase)) {
15798c2ecf20Sopenharmony_ci		err = PTR_ERR(pdcs->pdc_reg_vbase);
15808c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to map registers: %d\n", err);
15818c2ecf20Sopenharmony_ci		goto cleanup_ring_pool;
15828c2ecf20Sopenharmony_ci	}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	/* create rx buffer pool after dt read to know how big buffers are */
15858c2ecf20Sopenharmony_ci	err = pdc_rx_buf_pool_create(pdcs);
15868c2ecf20Sopenharmony_ci	if (err)
15878c2ecf20Sopenharmony_ci		goto cleanup_ring_pool;
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	pdc_hw_init(pdcs);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	/* Init tasklet for deferred DMA rx processing */
15928c2ecf20Sopenharmony_ci	tasklet_setup(&pdcs->rx_tasklet, pdc_tasklet_cb);
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	err = pdc_interrupts_init(pdcs);
15958c2ecf20Sopenharmony_ci	if (err)
15968c2ecf20Sopenharmony_ci		goto cleanup_buf_pool;
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	/* Initialize mailbox controller */
15998c2ecf20Sopenharmony_ci	err = pdc_mb_init(pdcs);
16008c2ecf20Sopenharmony_ci	if (err)
16018c2ecf20Sopenharmony_ci		goto cleanup_buf_pool;
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	pdc_setup_debugfs(pdcs);
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	dev_dbg(dev, "pdc_probe() successful");
16068c2ecf20Sopenharmony_ci	return PDC_SUCCESS;
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_cicleanup_buf_pool:
16098c2ecf20Sopenharmony_ci	tasklet_kill(&pdcs->rx_tasklet);
16108c2ecf20Sopenharmony_ci	dma_pool_destroy(pdcs->rx_buf_pool);
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_cicleanup_ring_pool:
16138c2ecf20Sopenharmony_ci	dma_pool_destroy(pdcs->ring_pool);
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_cicleanup:
16168c2ecf20Sopenharmony_ci	return err;
16178c2ecf20Sopenharmony_ci}
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_cistatic int pdc_remove(struct platform_device *pdev)
16208c2ecf20Sopenharmony_ci{
16218c2ecf20Sopenharmony_ci	struct pdc_state *pdcs = platform_get_drvdata(pdev);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	pdc_free_debugfs();
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	tasklet_kill(&pdcs->rx_tasklet);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	pdc_hw_disable(pdcs);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	dma_pool_destroy(pdcs->rx_buf_pool);
16308c2ecf20Sopenharmony_ci	dma_pool_destroy(pdcs->ring_pool);
16318c2ecf20Sopenharmony_ci	return 0;
16328c2ecf20Sopenharmony_ci}
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_cistatic struct platform_driver pdc_mbox_driver = {
16358c2ecf20Sopenharmony_ci	.probe = pdc_probe,
16368c2ecf20Sopenharmony_ci	.remove = pdc_remove,
16378c2ecf20Sopenharmony_ci	.driver = {
16388c2ecf20Sopenharmony_ci		   .name = "brcm-iproc-pdc-mbox",
16398c2ecf20Sopenharmony_ci		   .of_match_table = of_match_ptr(pdc_mbox_of_match),
16408c2ecf20Sopenharmony_ci		   },
16418c2ecf20Sopenharmony_ci};
16428c2ecf20Sopenharmony_cimodule_platform_driver(pdc_mbox_driver);
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
16458c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Broadcom PDC mailbox driver");
16468c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1647