2 * Copyright 2016 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
18 * Broadcom PDC Mailbox Driver
19 * The PDC provides a ring based programming interface to one or more hardware
20 * offload engines. For example, the PDC driver works with both SPU-M and SPU2
21 * cryptographic offload hardware. In some chips the PDC is referred to as MDE.
23 * The PDC driver registers with the Linux mailbox framework as a mailbox
24 * controller, once for each PDC instance. Ring 0 for each PDC is registered as
25 * a mailbox channel. The PDC driver uses interrupts to determine when data
26 * transfers to and from an offload engine are complete. The PDC driver uses
27 * threaded IRQs so that response messages are handled outside of interrupt
30 * The PDC driver allows multiple messages to be pending in the descriptor
31 * rings. The tx_msg_start descriptor index indicates where the last message
32 * starts. The txin_numd value at this index indicates how many descriptor
33 * indexes make up the message. Similar state is kept on the receive side. When
34 * an rx interrupt indicates a response is ready, the PDC driver processes numd
35 * descriptors from the tx and rx ring, thus processing one response at a time.
38 #include <linux/errno.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/debugfs.h>
43 #include <linux/interrupt.h>
44 #include <linux/wait.h>
45 #include <linux/platform_device.h>
48 #include <linux/of_device.h>
49 #include <linux/of_address.h>
50 #include <linux/of_irq.h>
51 #include <linux/mailbox_controller.h>
52 #include <linux/mailbox/brcm-message.h>
53 #include <linux/scatterlist.h>
54 #include <linux/dma-direction.h>
55 #include <linux/dma-mapping.h>
56 #include <linux/dmapool.h>
60 #define RING_ENTRY_SIZE sizeof(struct dma64dd)
62 /* # entries in PDC dma ring */
63 #define PDC_RING_ENTRIES 128
64 #define PDC_RING_SIZE (PDC_RING_ENTRIES * RING_ENTRY_SIZE)
65 /* Rings are 8k aligned */
66 #define RING_ALIGN_ORDER 13
67 #define RING_ALIGN BIT(RING_ALIGN_ORDER)
69 #define RX_BUF_ALIGN_ORDER 5
70 #define RX_BUF_ALIGN BIT(RX_BUF_ALIGN_ORDER)
72 /* descriptor bumping macros */
73 #define XXD(x, max_mask) ((x) & (max_mask))
74 #define TXD(x, max_mask) XXD((x), (max_mask))
75 #define RXD(x, max_mask) XXD((x), (max_mask))
76 #define NEXTTXD(i, max_mask) TXD((i) + 1, (max_mask))
77 #define PREVTXD(i, max_mask) TXD((i) - 1, (max_mask))
78 #define NEXTRXD(i, max_mask) RXD((i) + 1, (max_mask))
79 #define PREVRXD(i, max_mask) RXD((i) - 1, (max_mask))
80 #define NTXDACTIVE(h, t, max_mask) TXD((t) - (h), (max_mask))
81 #define NRXDACTIVE(h, t, max_mask) RXD((t) - (h), (max_mask))
83 /* Length of BCM header at start of SPU msg, in bytes */
87 * PDC driver reserves ringset 0 on each SPU for its own use. The driver does
88 * not currently support use of multiple ringsets on a single PDC engine.
93 * Interrupt mask and status definitions. Enable interrupts for tx and rx on
96 #define PDC_XMTINT_0 (24 + PDC_RINGSET)
97 #define PDC_RCVINT_0 (16 + PDC_RINGSET)
98 #define PDC_XMTINTEN_0 BIT(PDC_XMTINT_0)
99 #define PDC_RCVINTEN_0 BIT(PDC_RCVINT_0)
100 #define PDC_INTMASK (PDC_XMTINTEN_0 | PDC_RCVINTEN_0)
101 #define PDC_LAZY_FRAMECOUNT 1
102 #define PDC_LAZY_TIMEOUT 10000
103 #define PDC_LAZY_INT (PDC_LAZY_TIMEOUT | (PDC_LAZY_FRAMECOUNT << 24))
104 #define PDC_INTMASK_OFFSET 0x24
105 #define PDC_INTSTATUS_OFFSET 0x20
106 #define PDC_RCVLAZY0_OFFSET (0x30 + 4 * PDC_RINGSET)
109 * For SPU2, configure MDE_CKSUM_CONTROL to write 17 bytes of metadata
112 #define PDC_SPU2_RESP_HDR_LEN 17
113 #define PDC_CKSUM_CTRL BIT(27)
114 #define PDC_CKSUM_CTRL_OFFSET 0x400
116 #define PDC_SPUM_RESP_HDR_LEN 32
119 * Sets the following bits for write to transmit control reg:
120 * 11 - PtyChkDisable - parity check is disabled
121 * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
123 #define PDC_TX_CTL 0x000C0800
125 /* Bit in tx control reg to enable tx channel */
126 #define PDC_TX_ENABLE 0x1
129 * Sets the following bits for write to receive control reg:
130 * 7:1 - RcvOffset - size in bytes of status region at start of rx frame buf
131 * 9 - SepRxHdrDescEn - place start of new frames only in descriptors
132 * that have StartOfFrame set
133 * 10 - OflowContinue - on rx FIFO overflow, clear rx fifo, discard all
134 * remaining bytes in current frame, report error
135 * in rx frame status for current frame
136 * 11 - PtyChkDisable - parity check is disabled
137 * 20:18 - BurstLen = 3 -> 2^7 = 128 byte data reads from memory
139 #define PDC_RX_CTL 0x000C0E00
141 /* Bit in rx control reg to enable rx channel */
142 #define PDC_RX_ENABLE 0x1
144 #define CRYPTO_D64_RS0_CD_MASK ((PDC_RING_ENTRIES * RING_ENTRY_SIZE) - 1)
146 /* descriptor flags */
147 #define D64_CTRL1_EOT BIT(28) /* end of descriptor table */
148 #define D64_CTRL1_IOC BIT(29) /* interrupt on complete */
149 #define D64_CTRL1_EOF BIT(30) /* end of frame */
150 #define D64_CTRL1_SOF BIT(31) /* start of frame */
152 #define RX_STATUS_OVERFLOW 0x00800000
153 #define RX_STATUS_LEN 0x0000FFFF
155 #define PDC_TXREGS_OFFSET 0x200
156 #define PDC_RXREGS_OFFSET 0x220
158 /* Maximum size buffer the DMA engine can handle */
159 #define PDC_DMA_BUF_MAX 16384
162 void *ctx; /* opaque context associated with frame */
167 u32 ctrl1; /* misc control bits */
168 u32 ctrl2; /* buffer count and address extension */
169 u32 addrlow; /* memory address of the date buffer, bits 31:0 */
170 u32 addrhigh; /* memory address of the date buffer, bits 63:32 */
173 /* dma registers per channel(xmt or rcv) */
175 u32 control; /* enable, et al */
176 u32 ptr; /* last descriptor posted to chip */
177 u32 addrlow; /* descriptor ring base address low 32-bits */
178 u32 addrhigh; /* descriptor ring base address bits 63:32 */
179 u32 status0; /* last rx descriptor written by hw */
180 u32 status1; /* driver does not use */
183 /* cpp contortions to concatenate w/arg prescan */
185 #define _PADLINE(line) pad ## line
186 #define _XSTR(line) _PADLINE(line)
187 #define PAD _XSTR(__LINE__)
190 /* dma registers. matches hw layout. */
192 struct dma64_regs dmaxmt; /* dma tx */
194 struct dma64_regs dmarcv; /* dma rx */
200 u32 devcontrol; /* 0x000 */
201 u32 devstatus; /* 0x004 */
203 u32 biststatus; /* 0x00c */
205 u32 intstatus; /* 0x020 */
206 u32 intmask; /* 0x024 */
207 u32 gptimer; /* 0x028 */
210 u32 intrcvlazy_0; /* 0x030 */
211 u32 intrcvlazy_1; /* 0x034 */
212 u32 intrcvlazy_2; /* 0x038 */
213 u32 intrcvlazy_3; /* 0x03c */
216 u32 removed_intrecvlazy; /* 0x100 */
217 u32 flowctlthresh; /* 0x104 */
218 u32 wrrthresh; /* 0x108 */
219 u32 gmac_idle_cnt_thresh; /* 0x10c */
222 u32 ifioaccessaddr; /* 0x120 */
223 u32 ifioaccessbyte; /* 0x124 */
224 u32 ifioaccessdata; /* 0x128 */
227 u32 phyaccess; /* 0x180 */
229 u32 phycontrol; /* 0x188 */
230 u32 txqctl; /* 0x18c */
231 u32 rxqctl; /* 0x190 */
232 u32 gpioselect; /* 0x194 */
233 u32 gpio_output_en; /* 0x198 */
235 u32 txq_rxq_mem_ctl; /* 0x1a0 */
236 u32 memory_ecc_status; /* 0x1a4 */
237 u32 serdes_ctl; /* 0x1a8 */
238 u32 serdes_status0; /* 0x1ac */
239 u32 serdes_status1; /* 0x1b0 */
240 u32 PAD[11]; /* 0x1b4-1dc */
241 u32 clk_ctl_st; /* 0x1e0 */
242 u32 hw_war; /* 0x1e4 */
243 u32 pwrctl; /* 0x1e8 */
246 #define PDC_NUM_DMA_RINGS 4
247 struct dma64 dmaregs[PDC_NUM_DMA_RINGS]; /* 0x0200 - 0x2fc */
249 /* more registers follow, but we don't use them */
252 /* structure for allocating/freeing DMA rings */
253 struct pdc_ring_alloc {
254 dma_addr_t dmabase; /* DMA address of start of ring */
255 void *vbase; /* base kernel virtual address of ring */
256 u32 size; /* ring allocation size in bytes */
259 /* PDC state structure */
261 /* synchronize access to this PDC state structure */
264 /* Index of the PDC whose state is in this structure instance */
267 /* Platform device for this PDC instance */
268 struct platform_device *pdev;
271 * Each PDC instance has a mailbox controller. PDC receives request
272 * messages through mailboxes, and sends response messages through the
275 struct mbox_controller mbc;
277 unsigned int pdc_irq;
280 * Last interrupt status read from PDC device. Saved in interrupt
281 * handler so the handler can clear the interrupt in the device,
282 * and the interrupt thread called later can know which interrupt
285 unsigned long intstatus;
287 /* Number of bytes of receive status prior to each rx frame */
289 /* Whether a BCM header is prepended to each frame */
291 /* Sum of length of BCM header and rx status header */
292 u32 pdc_resp_hdr_len;
294 /* The base virtual address of DMA hw registers */
295 void __iomem *pdc_reg_vbase;
297 /* Pool for allocation of DMA rings */
298 struct dma_pool *ring_pool;
300 /* Pool for allocation of metadata buffers for response messages */
301 struct dma_pool *rx_buf_pool;
304 * The base virtual address of DMA tx/rx descriptor rings. Corresponding
305 * DMA address and size of ring allocation.
307 struct pdc_ring_alloc tx_ring_alloc;
308 struct pdc_ring_alloc rx_ring_alloc;
310 struct pdc_regs *regs; /* start of PDC registers */
312 struct dma64_regs *txregs_64; /* dma tx engine registers */
313 struct dma64_regs *rxregs_64; /* dma rx engine registers */
316 * Arrays of PDC_RING_ENTRIES descriptors
317 * To use multiple ringsets, this needs to be extended
319 struct dma64dd *txd_64; /* tx descriptor ring */
320 struct dma64dd *rxd_64; /* rx descriptor ring */
322 /* descriptor ring sizes */
323 u32 ntxd; /* # tx descriptors */
324 u32 nrxd; /* # rx descriptors */
325 u32 nrxpost; /* # rx buffers to keep posted */
326 u32 ntxpost; /* max number of tx buffers that can be posted */
329 * Index of next tx descriptor to reclaim. That is, the descriptor
330 * index of the oldest tx buffer for which the host has yet to process
331 * the corresponding response.
336 * Index of the first receive descriptor for the sequence of
337 * message fragments currently under construction. Used to build up
338 * the rxin_numd count for a message. Updated to rxout when the host
339 * starts a new sequence of rx buffers for a new message.
343 /* Index of next tx descriptor to post. */
347 * Number of tx descriptors associated with the message that starts
348 * at this tx descriptor index.
350 u32 txin_numd[PDC_RING_ENTRIES];
353 * Index of next rx descriptor to reclaim. This is the index of
354 * the next descriptor whose data has yet to be processed by the host.
359 * Index of the first receive descriptor for the sequence of
360 * message fragments currently under construction. Used to build up
361 * the rxin_numd count for a message. Updated to rxout when the host
362 * starts a new sequence of rx buffers for a new message.
367 * Saved value of current hardware rx descriptor index.
368 * The last rx buffer written by the hw is the index previous to
373 /* Index of next rx descriptor to post. */
377 * opaque context associated with frame that starts at each
380 void *rxp_ctx[PDC_RING_ENTRIES];
383 * Scatterlists used to form request and reply frames beginning at a
384 * given ring index. Retained in order to unmap each sg after reply
387 struct scatterlist *src_sg[PDC_RING_ENTRIES];
388 struct scatterlist *dst_sg[PDC_RING_ENTRIES];
391 * Number of rx descriptors associated with the message that starts
392 * at this descriptor index. Not set for every index. For example,
393 * if descriptor index i points to a scatterlist with 4 entries, then
394 * the next three descriptor indexes don't have a value set.
396 u32 rxin_numd[PDC_RING_ENTRIES];
398 void *resp_hdr[PDC_RING_ENTRIES];
399 dma_addr_t resp_hdr_daddr[PDC_RING_ENTRIES];
401 struct dentry *debugfs_stats; /* debug FS stats file for this PDC */
404 u32 pdc_requests; /* number of request messages submitted */
405 u32 pdc_replies; /* number of reply messages received */
406 u32 txnobuf; /* count of tx ring full */
407 u32 rxnobuf; /* count of rx ring full */
408 u32 rx_oflow; /* count of rx overflows */
411 /* Global variables */
414 /* Actual number of SPUs in hardware, as reported by device tree */
418 static struct pdc_globals pdcg;
420 /* top level debug FS directory for PDC driver */
421 static struct dentry *debugfs_dir;
423 static ssize_t pdc_debugfs_read(struct file *filp, char __user *ubuf,
424 size_t count, loff_t *offp)
426 struct pdc_state *pdcs;
428 ssize_t ret, out_offset, out_count;
432 buf = kmalloc(out_count, GFP_KERNEL);
436 pdcs = filp->private_data;
438 out_offset += snprintf(buf + out_offset, out_count - out_offset,
439 "SPU %u stats:\n", pdcs->pdc_idx);
440 out_offset += snprintf(buf + out_offset, out_count - out_offset,
441 "PDC requests............%u\n",
443 out_offset += snprintf(buf + out_offset, out_count - out_offset,
444 "PDC responses...........%u\n",
446 out_offset += snprintf(buf + out_offset, out_count - out_offset,
447 "Tx err ring full........%u\n",
449 out_offset += snprintf(buf + out_offset, out_count - out_offset,
450 "Rx err ring full........%u\n",
452 out_offset += snprintf(buf + out_offset, out_count - out_offset,
453 "Receive overflow........%u\n",
456 if (out_offset > out_count)
457 out_offset = out_count;
459 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
464 static const struct file_operations pdc_debugfs_stats = {
465 .owner = THIS_MODULE,
467 .read = pdc_debugfs_read,
471 * pdc_setup_debugfs() - Create the debug FS directories. If the top-level
472 * directory has not yet been created, create it now. Create a stats file in
473 * this directory for a SPU.
474 * @pdcs: PDC state structure
476 static void pdc_setup_debugfs(struct pdc_state *pdcs)
478 char spu_stats_name[16];
480 if (!debugfs_initialized())
483 snprintf(spu_stats_name, 16, "pdc%d_stats", pdcs->pdc_idx);
485 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
487 /* S_IRUSR == 0400 */
488 pdcs->debugfs_stats = debugfs_create_file(spu_stats_name, 0400,
493 static void pdc_free_debugfs(void)
495 debugfs_remove_recursive(debugfs_dir);
500 * pdc_build_rxd() - Build DMA descriptor to receive SPU result.
501 * @pdcs: PDC state for SPU that will generate result
502 * @dma_addr: DMA address of buffer that descriptor is being built for
503 * @buf_len: Length of the receive buffer, in bytes
504 * @flags: Flags to be stored in descriptor
507 pdc_build_rxd(struct pdc_state *pdcs, dma_addr_t dma_addr,
508 u32 buf_len, u32 flags)
510 struct device *dev = &pdcs->pdev->dev;
513 "Writing rx descriptor for PDC %u at index %u with length %u. flags %#x\n",
514 pdcs->pdc_idx, pdcs->rxout, buf_len, flags);
516 iowrite32(lower_32_bits(dma_addr),
517 (void *)&pdcs->rxd_64[pdcs->rxout].addrlow);
518 iowrite32(upper_32_bits(dma_addr),
519 (void *)&pdcs->rxd_64[pdcs->rxout].addrhigh);
520 iowrite32(flags, (void *)&pdcs->rxd_64[pdcs->rxout].ctrl1);
521 iowrite32(buf_len, (void *)&pdcs->rxd_64[pdcs->rxout].ctrl2);
522 /* bump ring index and return */
523 pdcs->rxout = NEXTRXD(pdcs->rxout, pdcs->nrxpost);
527 * pdc_build_txd() - Build a DMA descriptor to transmit a SPU request to
529 * @pdcs: PDC state for the SPU that will process this request
530 * @dma_addr: DMA address of packet to be transmitted
531 * @buf_len: Length of tx buffer, in bytes
532 * @flags: Flags to be stored in descriptor
535 pdc_build_txd(struct pdc_state *pdcs, dma_addr_t dma_addr, u32 buf_len,
538 struct device *dev = &pdcs->pdev->dev;
541 "Writing tx descriptor for PDC %u at index %u with length %u, flags %#x\n",
542 pdcs->pdc_idx, pdcs->txout, buf_len, flags);
544 iowrite32(lower_32_bits(dma_addr),
545 (void *)&pdcs->txd_64[pdcs->txout].addrlow);
546 iowrite32(upper_32_bits(dma_addr),
547 (void *)&pdcs->txd_64[pdcs->txout].addrhigh);
548 iowrite32(flags, (void *)&pdcs->txd_64[pdcs->txout].ctrl1);
549 iowrite32(buf_len, (void *)&pdcs->txd_64[pdcs->txout].ctrl2);
551 /* bump ring index and return */
552 pdcs->txout = NEXTTXD(pdcs->txout, pdcs->ntxpost);
556 * pdc_receive() - Receive a response message from a given SPU.
557 * @pdcs: PDC state for the SPU to receive from
558 * @mssg: mailbox message to be returned to client
560 * When the return code indicates success, the response message is available in
561 * the receive buffers provided prior to submission of the request.
564 * pdcs - PDC state structure for the SPU to be polled
565 * mssg - mailbox message to be returned to client. This function sets the
566 * context pointer on the message to help the client associate the
567 * response with a request.
569 * Return: PDC_SUCCESS if one or more receive descriptors was processed
570 * -EAGAIN indicates that no response message is available
571 * -EIO an error occurred
574 pdc_receive(struct pdc_state *pdcs, struct brcm_message *mssg)
576 struct device *dev = &pdcs->pdev->dev;
580 u8 *resp_hdr; /* virtual addr of start of resp message DMA header */
581 u32 frags_rdy; /* number of fragments ready to read */
582 u32 rx_idx; /* ring index of start of receive frame */
583 dma_addr_t resp_hdr_daddr;
585 spin_lock(&pdcs->pdc_lock);
588 * return if a complete response message is not yet ready.
589 * rxin_numd[rxin] is the number of fragments in the next msg
592 frags_rdy = NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr, pdcs->nrxpost);
593 if ((frags_rdy == 0) || (frags_rdy < pdcs->rxin_numd[pdcs->rxin])) {
594 /* See if the hw has written more fragments than we know */
596 (ioread32((void *)&pdcs->rxregs_64->status0) &
597 CRYPTO_D64_RS0_CD_MASK) / RING_ENTRY_SIZE;
598 frags_rdy = NRXDACTIVE(pdcs->rxin, pdcs->last_rx_curr,
600 if ((frags_rdy == 0) ||
601 (frags_rdy < pdcs->rxin_numd[pdcs->rxin])) {
602 /* No response ready */
603 spin_unlock(&pdcs->pdc_lock);
606 /* can't read descriptors/data until write index is read */
610 num_frags = pdcs->txin_numd[pdcs->txin];
611 dma_unmap_sg(dev, pdcs->src_sg[pdcs->txin],
612 sg_nents(pdcs->src_sg[pdcs->txin]), DMA_TO_DEVICE);
614 for (i = 0; i < num_frags; i++)
615 pdcs->txin = NEXTTXD(pdcs->txin, pdcs->ntxpost);
617 dev_dbg(dev, "PDC %u reclaimed %d tx descriptors",
618 pdcs->pdc_idx, num_frags);
621 num_frags = pdcs->rxin_numd[rx_idx];
622 /* Return opaque context with result */
623 mssg->ctx = pdcs->rxp_ctx[rx_idx];
624 pdcs->rxp_ctx[rx_idx] = NULL;
625 resp_hdr = pdcs->resp_hdr[rx_idx];
626 resp_hdr_daddr = pdcs->resp_hdr_daddr[rx_idx];
627 dma_unmap_sg(dev, pdcs->dst_sg[rx_idx],
628 sg_nents(pdcs->dst_sg[rx_idx]), DMA_FROM_DEVICE);
630 for (i = 0; i < num_frags; i++)
631 pdcs->rxin = NEXTRXD(pdcs->rxin, pdcs->nrxpost);
633 spin_unlock(&pdcs->pdc_lock);
635 dev_dbg(dev, "PDC %u reclaimed %d rx descriptors",
636 pdcs->pdc_idx, num_frags);
639 "PDC %u txin %u, txout %u, rxin %u, rxout %u, last_rx_curr %u\n",
640 pdcs->pdc_idx, pdcs->txin, pdcs->txout, pdcs->rxin,
641 pdcs->rxout, pdcs->last_rx_curr);
643 if (pdcs->pdc_resp_hdr_len == PDC_SPUM_RESP_HDR_LEN) {
645 * For SPU-M, get length of response msg and rx overflow status.
647 rx_status = *((u32 *)resp_hdr);
648 len = rx_status & RX_STATUS_LEN;
650 "SPU response length %u bytes", len);
651 if (unlikely(((rx_status & RX_STATUS_OVERFLOW) || (!len)))) {
652 if (rx_status & RX_STATUS_OVERFLOW) {
653 dev_err_ratelimited(dev,
654 "crypto receive overflow");
657 dev_info_ratelimited(dev, "crypto rx len = 0");
663 dma_pool_free(pdcs->rx_buf_pool, resp_hdr, resp_hdr_daddr);
666 /* if we read one or more rx descriptors, claim success */
674 * pdc_tx_list_sg_add() - Add the buffers in a scatterlist to the transmit
675 * descriptors for a given SPU. The scatterlist buffers contain the data for a
676 * SPU request message.
677 * @spu_idx: The index of the SPU to submit the request to, [0, max_spu)
678 * @sg: Scatterlist whose buffers contain part of the SPU request
680 * If a scatterlist buffer is larger than PDC_DMA_BUF_MAX, multiple descriptors
681 * are written for that buffer, each <= PDC_DMA_BUF_MAX byte in length.
683 * Return: PDC_SUCCESS if successful
686 static int pdc_tx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
693 * Num descriptors needed. Conservatively assume we need a descriptor
694 * for every entry in sg.
697 u32 desc_w = 0; /* Number of tx descriptors written */
698 u32 bufcnt; /* Number of bytes of buffer pointed to by descriptor */
699 dma_addr_t databufptr; /* DMA address to put in descriptor */
701 num_desc = (u32)sg_nents(sg);
703 /* check whether enough tx descriptors are available */
704 tx_avail = pdcs->ntxpost - NTXDACTIVE(pdcs->txin, pdcs->txout,
706 if (unlikely(num_desc > tx_avail)) {
711 /* build tx descriptors */
712 if (pdcs->tx_msg_start == pdcs->txout) {
714 pdcs->txin_numd[pdcs->tx_msg_start] = 0;
715 pdcs->src_sg[pdcs->txout] = sg;
716 flags = D64_CTRL1_SOF;
720 if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
726 * If sg buffer larger than PDC limit, split across
727 * multiple descriptors
729 bufcnt = sg_dma_len(sg);
730 databufptr = sg_dma_address(sg);
731 while (bufcnt > PDC_DMA_BUF_MAX) {
732 pdc_build_txd(pdcs, databufptr, PDC_DMA_BUF_MAX,
735 bufcnt -= PDC_DMA_BUF_MAX;
736 databufptr += PDC_DMA_BUF_MAX;
737 if (unlikely(pdcs->txout == (pdcs->ntxd - 1)))
744 /* Writing last descriptor for frame */
745 flags |= (D64_CTRL1_EOF | D64_CTRL1_IOC);
746 pdc_build_txd(pdcs, databufptr, bufcnt, flags | eot);
748 /* Clear start of frame after first descriptor */
749 flags &= ~D64_CTRL1_SOF;
751 pdcs->txin_numd[pdcs->tx_msg_start] += desc_w;
757 * pdc_tx_list_final() - Initiate DMA transfer of last frame written to tx
759 * @pdcs: PDC state for SPU to process the request
761 * Sets the index of the last descriptor written in both the rx and tx ring.
763 * Return: PDC_SUCCESS
765 static int pdc_tx_list_final(struct pdc_state *pdcs)
768 * write barrier to ensure all register writes are complete
769 * before chip starts to process new request
772 iowrite32(pdcs->rxout << 4, (void *)&pdcs->rxregs_64->ptr);
773 iowrite32(pdcs->txout << 4, (void *)&pdcs->txregs_64->ptr);
774 pdcs->pdc_requests++;
780 * pdc_rx_list_init() - Start a new receive descriptor list for a given PDC.
781 * @pdcs: PDC state for SPU handling request
782 * @dst_sg: scatterlist providing rx buffers for response to be returned to
784 * @ctx: Opaque context for this request
786 * Posts a single receive descriptor to hold the metadata that precedes a
787 * response. For example, with SPU-M, the metadata is a 32-byte DMA header and
788 * an 8-byte BCM header. Moves the msg_start descriptor indexes for both tx and
789 * rx to indicate the start of a new message.
791 * Return: PDC_SUCCESS if successful
792 * < 0 if an error (e.g., rx ring is full)
794 static int pdc_rx_list_init(struct pdc_state *pdcs, struct scatterlist *dst_sg,
799 u32 rx_pkt_cnt = 1; /* Adding a single rx buffer */
803 rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
805 if (unlikely(rx_pkt_cnt > rx_avail)) {
810 /* allocate a buffer for the dma rx status */
811 vaddr = dma_pool_zalloc(pdcs->rx_buf_pool, GFP_ATOMIC, &daddr);
816 * Update msg_start indexes for both tx and rx to indicate the start
817 * of a new sequence of descriptor indexes that contain the fragments
818 * of the same message.
820 pdcs->rx_msg_start = pdcs->rxout;
821 pdcs->tx_msg_start = pdcs->txout;
823 /* This is always the first descriptor in the receive sequence */
824 flags = D64_CTRL1_SOF;
825 pdcs->rxin_numd[pdcs->rx_msg_start] = 1;
827 if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
828 flags |= D64_CTRL1_EOT;
830 pdcs->rxp_ctx[pdcs->rxout] = ctx;
831 pdcs->dst_sg[pdcs->rxout] = dst_sg;
832 pdcs->resp_hdr[pdcs->rxout] = vaddr;
833 pdcs->resp_hdr_daddr[pdcs->rxout] = daddr;
834 pdc_build_rxd(pdcs, daddr, pdcs->pdc_resp_hdr_len, flags);
839 * pdc_rx_list_sg_add() - Add the buffers in a scatterlist to the receive
840 * descriptors for a given SPU. The caller must have already DMA mapped the
842 * @spu_idx: Indicates which SPU the buffers are for
843 * @sg: Scatterlist whose buffers are added to the receive ring
845 * If a receive buffer in the scatterlist is larger than PDC_DMA_BUF_MAX,
846 * multiple receive descriptors are written, each with a buffer <=
849 * Return: PDC_SUCCESS if successful
850 * < 0 otherwise (e.g., receive ring is full)
852 static int pdc_rx_list_sg_add(struct pdc_state *pdcs, struct scatterlist *sg)
858 * Num descriptors needed. Conservatively assume we need a descriptor
859 * for every entry from our starting point in the scatterlist.
862 u32 desc_w = 0; /* Number of tx descriptors written */
863 u32 bufcnt; /* Number of bytes of buffer pointed to by descriptor */
864 dma_addr_t databufptr; /* DMA address to put in descriptor */
866 num_desc = (u32)sg_nents(sg);
868 rx_avail = pdcs->nrxpost - NRXDACTIVE(pdcs->rxin, pdcs->rxout,
870 if (unlikely(num_desc > rx_avail)) {
876 if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
877 flags = D64_CTRL1_EOT;
882 * If sg buffer larger than PDC limit, split across
883 * multiple descriptors
885 bufcnt = sg_dma_len(sg);
886 databufptr = sg_dma_address(sg);
887 while (bufcnt > PDC_DMA_BUF_MAX) {
888 pdc_build_rxd(pdcs, databufptr, PDC_DMA_BUF_MAX, flags);
890 bufcnt -= PDC_DMA_BUF_MAX;
891 databufptr += PDC_DMA_BUF_MAX;
892 if (unlikely(pdcs->rxout == (pdcs->nrxd - 1)))
893 flags = D64_CTRL1_EOT;
897 pdc_build_rxd(pdcs, databufptr, bufcnt, flags);
901 pdcs->rxin_numd[pdcs->rx_msg_start] += desc_w;
907 * pdc_irq_handler() - Interrupt handler called in interrupt context.
908 * @irq: Interrupt number that has fired
909 * @cookie: PDC state for DMA engine that generated the interrupt
911 * We have to clear the device interrupt status flags here. So cache the
912 * status for later use in the thread function. Other than that, just return
913 * WAKE_THREAD to invoke the thread function.
915 * Return: IRQ_WAKE_THREAD if interrupt is ours
918 static irqreturn_t pdc_irq_handler(int irq, void *cookie)
920 struct pdc_state *pdcs = cookie;
921 u32 intstatus = ioread32(pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
923 if (intstatus & PDC_XMTINTEN_0)
924 set_bit(PDC_XMTINT_0, &pdcs->intstatus);
925 if (intstatus & PDC_RCVINTEN_0)
926 set_bit(PDC_RCVINT_0, &pdcs->intstatus);
928 /* Clear interrupt flags in device */
929 iowrite32(intstatus, pdcs->pdc_reg_vbase + PDC_INTSTATUS_OFFSET);
931 /* Wakeup IRQ thread */
932 if (pdcs && (irq == pdcs->pdc_irq) && (intstatus & PDC_INTMASK))
933 return IRQ_WAKE_THREAD;
939 * pdc_irq_thread() - Function invoked on deferred thread when a DMA tx has
940 * completed or data is available to receive.
941 * @irq: Interrupt number
942 * @cookie: PDC state for PDC that generated the interrupt
944 * On DMA tx complete, notify the mailbox client. On DMA rx complete, process
945 * as many SPU response messages as are available and send each to the mailbox
948 * Return: IRQ_HANDLED if we recognized and handled the interrupt
951 static irqreturn_t pdc_irq_thread(int irq, void *cookie)
953 struct pdc_state *pdcs = cookie;
954 struct mbox_controller *mbc;
955 struct mbox_chan *chan;
959 struct brcm_message mssg;
961 tx_int = test_and_clear_bit(PDC_XMTINT_0, &pdcs->intstatus);
962 rx_int = test_and_clear_bit(PDC_RCVINT_0, &pdcs->intstatus);
964 if (pdcs && (tx_int || rx_int)) {
965 dev_dbg(&pdcs->pdev->dev,
966 "%s() got irq %d with tx_int %s, rx_int %s",
968 tx_int ? "set" : "clear", rx_int ? "set" : "clear");
971 chan = &mbc->chans[0];
974 dev_dbg(&pdcs->pdev->dev, "%s(): tx done", __func__);
975 /* only one frame in flight at a time */
976 mbox_chan_txdone(chan, PDC_SUCCESS);
980 /* Could be many frames ready */
981 memset(&mssg, 0, sizeof(mssg));
982 mssg.type = BRCM_MESSAGE_SPU;
983 rx_status = pdc_receive(pdcs, &mssg);
984 if (rx_status >= 0) {
985 dev_dbg(&pdcs->pdev->dev,
986 "%s(): invoking client rx cb",
988 mbox_chan_received_data(chan, &mssg);
990 dev_dbg(&pdcs->pdev->dev,
991 "%s(): no SPU response available",
1003 * pdc_ring_init() - Allocate DMA rings and initialize constant fields of
1004 * descriptors in one ringset.
1005 * @pdcs: PDC instance state
1006 * @ringset: index of ringset being used
1008 * Return: PDC_SUCCESS if ring initialized
1011 static int pdc_ring_init(struct pdc_state *pdcs, int ringset)
1014 int err = PDC_SUCCESS;
1015 struct dma64 *dma_reg;
1016 struct device *dev = &pdcs->pdev->dev;
1017 struct pdc_ring_alloc tx;
1018 struct pdc_ring_alloc rx;
1020 /* Allocate tx ring */
1021 tx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &tx.dmabase);
1027 /* Allocate rx ring */
1028 rx.vbase = dma_pool_zalloc(pdcs->ring_pool, GFP_KERNEL, &rx.dmabase);
1034 dev_dbg(dev, " - base DMA addr of tx ring %pad", &tx.dmabase);
1035 dev_dbg(dev, " - base virtual addr of tx ring %p", tx.vbase);
1036 dev_dbg(dev, " - base DMA addr of rx ring %pad", &rx.dmabase);
1037 dev_dbg(dev, " - base virtual addr of rx ring %p", rx.vbase);
1039 /* lock after ring allocation to avoid scheduling while atomic */
1040 spin_lock(&pdcs->pdc_lock);
1042 memcpy(&pdcs->tx_ring_alloc, &tx, sizeof(tx));
1043 memcpy(&pdcs->rx_ring_alloc, &rx, sizeof(rx));
1046 pdcs->rx_msg_start = 0;
1047 pdcs->last_rx_curr = 0;
1050 pdcs->tx_msg_start = 0;
1053 /* Set descriptor array base addresses */
1054 pdcs->txd_64 = (struct dma64dd *)pdcs->tx_ring_alloc.vbase;
1055 pdcs->rxd_64 = (struct dma64dd *)pdcs->rx_ring_alloc.vbase;
1057 /* Tell device the base DMA address of each ring */
1058 dma_reg = &pdcs->regs->dmaregs[ringset];
1060 /* But first disable DMA and set curptr to 0 for both TX & RX */
1061 iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
1062 iowrite32((PDC_RX_CTL + (pdcs->rx_status_len << 1)),
1063 (void *)&dma_reg->dmarcv.control);
1064 iowrite32(0, (void *)&dma_reg->dmaxmt.ptr);
1065 iowrite32(0, (void *)&dma_reg->dmarcv.ptr);
1067 /* Set base DMA addresses */
1068 iowrite32(lower_32_bits(pdcs->tx_ring_alloc.dmabase),
1069 (void *)&dma_reg->dmaxmt.addrlow);
1070 iowrite32(upper_32_bits(pdcs->tx_ring_alloc.dmabase),
1071 (void *)&dma_reg->dmaxmt.addrhigh);
1073 iowrite32(lower_32_bits(pdcs->rx_ring_alloc.dmabase),
1074 (void *)&dma_reg->dmarcv.addrlow);
1075 iowrite32(upper_32_bits(pdcs->rx_ring_alloc.dmabase),
1076 (void *)&dma_reg->dmarcv.addrhigh);
1079 iowrite32(PDC_TX_CTL | PDC_TX_ENABLE, &dma_reg->dmaxmt.control);
1080 iowrite32((PDC_RX_CTL | PDC_RX_ENABLE | (pdcs->rx_status_len << 1)),
1081 (void *)&dma_reg->dmarcv.control);
1083 /* Initialize descriptors */
1084 for (i = 0; i < PDC_RING_ENTRIES; i++) {
1085 /* Every tx descriptor can be used for start of frame. */
1086 if (i != pdcs->ntxpost) {
1087 iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF,
1088 (void *)&pdcs->txd_64[i].ctrl1);
1090 /* Last descriptor in ringset. Set End of Table. */
1091 iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOF |
1093 (void *)&pdcs->txd_64[i].ctrl1);
1096 /* Every rx descriptor can be used for start of frame */
1097 if (i != pdcs->nrxpost) {
1098 iowrite32(D64_CTRL1_SOF,
1099 (void *)&pdcs->rxd_64[i].ctrl1);
1101 /* Last descriptor in ringset. Set End of Table. */
1102 iowrite32(D64_CTRL1_SOF | D64_CTRL1_EOT,
1103 (void *)&pdcs->rxd_64[i].ctrl1);
1106 spin_unlock(&pdcs->pdc_lock);
1110 dma_pool_free(pdcs->ring_pool, tx.vbase, tx.dmabase);
1115 static void pdc_ring_free(struct pdc_state *pdcs)
1117 if (pdcs->tx_ring_alloc.vbase) {
1118 dma_pool_free(pdcs->ring_pool, pdcs->tx_ring_alloc.vbase,
1119 pdcs->tx_ring_alloc.dmabase);
1120 pdcs->tx_ring_alloc.vbase = NULL;
1123 if (pdcs->rx_ring_alloc.vbase) {
1124 dma_pool_free(pdcs->ring_pool, pdcs->rx_ring_alloc.vbase,
1125 pdcs->rx_ring_alloc.dmabase);
1126 pdcs->rx_ring_alloc.vbase = NULL;
1131 * pdc_send_data() - mailbox send_data function
1132 * @chan: The mailbox channel on which the data is sent. The channel
1133 * corresponds to a DMA ringset.
1134 * @data: The mailbox message to be sent. The message must be a
1135 * brcm_message structure.
1137 * This function is registered as the send_data function for the mailbox
1138 * controller. From the destination scatterlist in the mailbox message, it
1139 * creates a sequence of receive descriptors in the rx ring. From the source
1140 * scatterlist, it creates a sequence of transmit descriptors in the tx ring.
1141 * After creating the descriptors, it writes the rx ptr and tx ptr registers to
1142 * initiate the DMA transfer.
1144 * This function does the DMA map and unmap of the src and dst scatterlists in
1145 * the mailbox message.
1147 * Return: 0 if successful
1148 * -ENOTSUPP if the mailbox message is a type this driver does not
1152 static int pdc_send_data(struct mbox_chan *chan, void *data)
1154 struct pdc_state *pdcs = chan->con_priv;
1155 struct device *dev = &pdcs->pdev->dev;
1156 struct brcm_message *mssg = data;
1157 int err = PDC_SUCCESS;
1162 if (mssg->type != BRCM_MESSAGE_SPU)
1165 src_nent = sg_nents(mssg->spu.src);
1167 nent = dma_map_sg(dev, mssg->spu.src, src_nent, DMA_TO_DEVICE);
1172 dst_nent = sg_nents(mssg->spu.dst);
1174 nent = dma_map_sg(dev, mssg->spu.dst, dst_nent,
1177 dma_unmap_sg(dev, mssg->spu.src, src_nent,
1183 spin_lock(&pdcs->pdc_lock);
1185 /* Create rx descriptors to SPU catch response */
1186 err = pdc_rx_list_init(pdcs, mssg->spu.dst, mssg->ctx);
1187 err |= pdc_rx_list_sg_add(pdcs, mssg->spu.dst);
1189 /* Create tx descriptors to submit SPU request */
1190 err |= pdc_tx_list_sg_add(pdcs, mssg->spu.src);
1191 err |= pdc_tx_list_final(pdcs); /* initiate transfer */
1193 spin_unlock(&pdcs->pdc_lock);
1196 dev_err(&pdcs->pdev->dev,
1197 "%s failed with error %d", __func__, err);
1202 static int pdc_startup(struct mbox_chan *chan)
1204 return pdc_ring_init(chan->con_priv, PDC_RINGSET);
1207 static void pdc_shutdown(struct mbox_chan *chan)
1209 struct pdc_state *pdcs = chan->con_priv;
1214 dev_dbg(&pdcs->pdev->dev,
1215 "Shutdown mailbox channel for PDC %u", pdcs->pdc_idx);
1216 pdc_ring_free(pdcs);
1220 * pdc_hw_init() - Use the given initialization parameters to initialize the
1221 * state for one of the PDCs.
1222 * @pdcs: state of the PDC
1225 void pdc_hw_init(struct pdc_state *pdcs)
1227 struct platform_device *pdev;
1229 struct dma64 *dma_reg;
1230 int ringset = PDC_RINGSET;
1235 dev_dbg(dev, "PDC %u initial values:", pdcs->pdc_idx);
1236 dev_dbg(dev, "state structure: %p",
1238 dev_dbg(dev, " - base virtual addr of hw regs %p",
1239 pdcs->pdc_reg_vbase);
1241 /* initialize data structures */
1242 pdcs->regs = (struct pdc_regs *)pdcs->pdc_reg_vbase;
1243 pdcs->txregs_64 = (struct dma64_regs *)
1244 (void *)(((u8 *)pdcs->pdc_reg_vbase) +
1245 PDC_TXREGS_OFFSET + (sizeof(struct dma64) * ringset));
1246 pdcs->rxregs_64 = (struct dma64_regs *)
1247 (void *)(((u8 *)pdcs->pdc_reg_vbase) +
1248 PDC_RXREGS_OFFSET + (sizeof(struct dma64) * ringset));
1250 pdcs->ntxd = PDC_RING_ENTRIES;
1251 pdcs->nrxd = PDC_RING_ENTRIES;
1252 pdcs->ntxpost = PDC_RING_ENTRIES - 1;
1253 pdcs->nrxpost = PDC_RING_ENTRIES - 1;
1254 iowrite32(0, &pdcs->regs->intmask);
1256 dma_reg = &pdcs->regs->dmaregs[ringset];
1258 /* Configure DMA but will enable later in pdc_ring_init() */
1259 iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
1261 iowrite32(PDC_RX_CTL + (pdcs->rx_status_len << 1),
1262 (void *)&dma_reg->dmarcv.control);
1264 /* Reset current index pointers after making sure DMA is disabled */
1265 iowrite32(0, &dma_reg->dmaxmt.ptr);
1266 iowrite32(0, &dma_reg->dmarcv.ptr);
1268 if (pdcs->pdc_resp_hdr_len == PDC_SPU2_RESP_HDR_LEN)
1269 iowrite32(PDC_CKSUM_CTRL,
1270 pdcs->pdc_reg_vbase + PDC_CKSUM_CTRL_OFFSET);
1274 * pdc_hw_disable() - Disable the tx and rx control in the hw.
1275 * @pdcs: PDC state structure
1278 static void pdc_hw_disable(struct pdc_state *pdcs)
1280 struct dma64 *dma_reg;
1282 dma_reg = &pdcs->regs->dmaregs[PDC_RINGSET];
1283 iowrite32(PDC_TX_CTL, &dma_reg->dmaxmt.control);
1284 iowrite32(PDC_RX_CTL + (pdcs->rx_status_len << 1),
1285 &dma_reg->dmarcv.control);
1289 * pdc_rx_buf_pool_create() - Pool of receive buffers used to catch the metadata
1290 * header returned with each response message.
1291 * @pdcs: PDC state structure
1293 * The metadata is not returned to the mailbox client. So the PDC driver
1294 * manages these buffers.
1296 * Return: PDC_SUCCESS
1297 * -ENOMEM if pool creation fails
1299 static int pdc_rx_buf_pool_create(struct pdc_state *pdcs)
1301 struct platform_device *pdev;
1307 pdcs->pdc_resp_hdr_len = pdcs->rx_status_len;
1308 if (pdcs->use_bcm_hdr)
1309 pdcs->pdc_resp_hdr_len += BCM_HDR_LEN;
1311 pdcs->rx_buf_pool = dma_pool_create("pdc rx bufs", dev,
1312 pdcs->pdc_resp_hdr_len,
1314 if (!pdcs->rx_buf_pool)
1321 * pdc_interrupts_init() - Initialize the interrupt configuration for a PDC and
1322 * specify a threaded IRQ handler for deferred handling of interrupts outside of
1323 * interrupt context.
1326 * Set the interrupt mask for transmit and receive done.
1327 * Set the lazy interrupt frame count to generate an interrupt for just one pkt.
1329 * Return: PDC_SUCCESS
1330 * <0 if threaded irq request fails
1332 static int pdc_interrupts_init(struct pdc_state *pdcs)
1334 struct platform_device *pdev = pdcs->pdev;
1335 struct device *dev = &pdev->dev;
1336 struct device_node *dn = pdev->dev.of_node;
1339 pdcs->intstatus = 0;
1341 /* interrupt configuration */
1342 iowrite32(PDC_INTMASK, pdcs->pdc_reg_vbase + PDC_INTMASK_OFFSET);
1343 iowrite32(PDC_LAZY_INT, pdcs->pdc_reg_vbase + PDC_RCVLAZY0_OFFSET);
1345 /* read irq from device tree */
1346 pdcs->pdc_irq = irq_of_parse_and_map(dn, 0);
1347 dev_dbg(dev, "pdc device %s irq %u for pdcs %p",
1348 dev_name(dev), pdcs->pdc_irq, pdcs);
1349 err = devm_request_threaded_irq(dev, pdcs->pdc_irq,
1351 pdc_irq_thread, 0, dev_name(dev), pdcs);
1353 dev_err(dev, "threaded tx IRQ %u request failed with err %d\n",
1354 pdcs->pdc_irq, err);
1360 static const struct mbox_chan_ops pdc_mbox_chan_ops = {
1361 .send_data = pdc_send_data,
1362 .startup = pdc_startup,
1363 .shutdown = pdc_shutdown
1367 * pdc_mb_init() - Initialize the mailbox controller.
1370 * Each PDC is a mailbox controller. Each ringset is a mailbox channel. Kernel
1371 * driver only uses one ringset and thus one mb channel. PDC uses the transmit
1372 * complete interrupt to determine when a mailbox message has successfully been
1375 * Return: 0 on success
1376 * < 0 if there is an allocation or registration failure
1378 static int pdc_mb_init(struct pdc_state *pdcs)
1380 struct device *dev = &pdcs->pdev->dev;
1381 struct mbox_controller *mbc;
1387 mbc->ops = &pdc_mbox_chan_ops;
1389 mbc->chans = devm_kcalloc(dev, mbc->num_chans, sizeof(*mbc->chans),
1394 mbc->txdone_irq = true;
1395 mbc->txdone_poll = false;
1396 for (chan_index = 0; chan_index < mbc->num_chans; chan_index++)
1397 mbc->chans[chan_index].con_priv = pdcs;
1399 /* Register mailbox controller */
1400 err = mbox_controller_register(mbc);
1403 "Failed to register PDC mailbox controller. Error %d.",
1411 * pdc_dt_read() - Read application-specific data from device tree.
1412 * @pdev: Platform device
1415 * Reads the number of bytes of receive status that precede each received frame.
1416 * Reads whether transmit and received frames should be preceded by an 8-byte
1419 * Return: 0 if successful
1420 * -ENODEV if device not available
1422 static int pdc_dt_read(struct platform_device *pdev, struct pdc_state *pdcs)
1424 struct device *dev = &pdev->dev;
1425 struct device_node *dn = pdev->dev.of_node;
1428 err = of_property_read_u32(dn, "brcm,rx-status-len",
1429 &pdcs->rx_status_len);
1432 "%s failed to get DMA receive status length from device tree",
1435 pdcs->use_bcm_hdr = of_property_read_bool(dn, "brcm,use-bcm-hdr");
1441 * pdc_probe() - Probe function for PDC driver.
1442 * @pdev: PDC platform device
1444 * Reserve and map register regions defined in device tree.
1445 * Allocate and initialize tx and rx DMA rings.
1446 * Initialize a mailbox controller for each PDC.
1448 * Return: 0 if successful
1451 static int pdc_probe(struct platform_device *pdev)
1454 struct device *dev = &pdev->dev;
1455 struct resource *pdc_regs;
1456 struct pdc_state *pdcs;
1458 /* PDC state for one SPU */
1459 pdcs = devm_kzalloc(dev, sizeof(*pdcs), GFP_KERNEL);
1465 spin_lock_init(&pdcs->pdc_lock);
1467 platform_set_drvdata(pdev, pdcs);
1468 pdcs->pdc_idx = pdcg.num_spu;
1471 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1473 dev_warn(dev, "PDC device cannot perform DMA. Error %d.", err);
1477 /* Create DMA pool for tx ring */
1478 pdcs->ring_pool = dma_pool_create("pdc rings", dev, PDC_RING_SIZE,
1480 if (!pdcs->ring_pool) {
1485 err = pdc_dt_read(pdev, pdcs);
1487 goto cleanup_ring_pool;
1489 pdc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1492 goto cleanup_ring_pool;
1494 dev_dbg(dev, "PDC register region res.start = %pa, res.end = %pa",
1495 &pdc_regs->start, &pdc_regs->end);
1497 pdcs->pdc_reg_vbase = devm_ioremap_resource(&pdev->dev, pdc_regs);
1498 if (IS_ERR(pdcs->pdc_reg_vbase)) {
1499 err = PTR_ERR(pdcs->pdc_reg_vbase);
1500 dev_err(&pdev->dev, "Failed to map registers: %d\n", err);
1501 goto cleanup_ring_pool;
1504 /* create rx buffer pool after dt read to know how big buffers are */
1505 err = pdc_rx_buf_pool_create(pdcs);
1507 goto cleanup_ring_pool;
1511 err = pdc_interrupts_init(pdcs);
1513 goto cleanup_buf_pool;
1515 /* Initialize mailbox controller */
1516 err = pdc_mb_init(pdcs);
1518 goto cleanup_buf_pool;
1520 pdcs->debugfs_stats = NULL;
1521 pdc_setup_debugfs(pdcs);
1523 dev_dbg(dev, "pdc_probe() successful");
1527 dma_pool_destroy(pdcs->rx_buf_pool);
1530 dma_pool_destroy(pdcs->ring_pool);
1536 static int pdc_remove(struct platform_device *pdev)
1538 struct pdc_state *pdcs = platform_get_drvdata(pdev);
1542 pdc_hw_disable(pdcs);
1544 mbox_controller_unregister(&pdcs->mbc);
1546 dma_pool_destroy(pdcs->rx_buf_pool);
1547 dma_pool_destroy(pdcs->ring_pool);
1551 static const struct of_device_id pdc_mbox_of_match[] = {
1552 {.compatible = "brcm,iproc-pdc-mbox"},
1555 MODULE_DEVICE_TABLE(of, pdc_mbox_of_match);
1557 static struct platform_driver pdc_mbox_driver = {
1559 .remove = pdc_remove,
1561 .name = "brcm-iproc-pdc-mbox",
1562 .of_match_table = of_match_ptr(pdc_mbox_of_match),
1565 module_platform_driver(pdc_mbox_driver);
1567 MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
1568 MODULE_DESCRIPTION("Broadcom PDC mailbox driver");
1569 MODULE_LICENSE("GPL v2");