1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Calxeda, Inc.
5 #include <linux/types.h>
7 #include <linux/delay.h>
8 #include <linux/export.h>
10 #include <linux/interrupt.h>
11 #include <linux/completion.h>
12 #include <linux/mutex.h>
13 #include <linux/notifier.h>
14 #include <linux/spinlock.h>
15 #include <linux/device.h>
16 #include <linux/amba/bus.h>
18 #include <linux/pl320-ipc.h>
20 #define IPCMxSOURCE(m) ((m) * 0x40)
21 #define IPCMxDSET(m) (((m) * 0x40) + 0x004)
22 #define IPCMxDCLEAR(m) (((m) * 0x40) + 0x008)
23 #define IPCMxDSTATUS(m) (((m) * 0x40) + 0x00C)
24 #define IPCMxMODE(m) (((m) * 0x40) + 0x010)
25 #define IPCMxMSET(m) (((m) * 0x40) + 0x014)
26 #define IPCMxMCLEAR(m) (((m) * 0x40) + 0x018)
27 #define IPCMxMSTATUS(m) (((m) * 0x40) + 0x01C)
28 #define IPCMxSEND(m) (((m) * 0x40) + 0x020)
29 #define IPCMxDR(m, dr) (((m) * 0x40) + ((dr) * 4) + 0x024)
31 #define IPCMMIS(irq) (((irq) * 8) + 0x800)
32 #define IPCMRIS(irq) (((irq) * 8) + 0x804)
34 #define MBOX_MASK(n) (1 << (n))
38 #define CHAN_MASK(n) (1 << (n))
42 static void __iomem
*ipc_base
;
44 static DEFINE_MUTEX(ipc_m1_lock
);
45 static DECLARE_COMPLETION(ipc_completion
);
46 static ATOMIC_NOTIFIER_HEAD(ipc_notifier
);
48 static inline void set_destination(int source
, int mbox
)
50 writel_relaxed(CHAN_MASK(source
), ipc_base
+ IPCMxDSET(mbox
));
51 writel_relaxed(CHAN_MASK(source
), ipc_base
+ IPCMxMSET(mbox
));
54 static inline void clear_destination(int source
, int mbox
)
56 writel_relaxed(CHAN_MASK(source
), ipc_base
+ IPCMxDCLEAR(mbox
));
57 writel_relaxed(CHAN_MASK(source
), ipc_base
+ IPCMxMCLEAR(mbox
));
60 static void __ipc_send(int mbox
, u32
*data
)
63 for (i
= 0; i
< 7; i
++)
64 writel_relaxed(data
[i
], ipc_base
+ IPCMxDR(mbox
, i
));
65 writel_relaxed(0x1, ipc_base
+ IPCMxSEND(mbox
));
68 static u32
__ipc_rcv(int mbox
, u32
*data
)
71 for (i
= 0; i
< 7; i
++)
72 data
[i
] = readl_relaxed(ipc_base
+ IPCMxDR(mbox
, i
));
76 /* blocking implmentation from the A9 side, not usuable in interrupts! */
77 int pl320_ipc_transmit(u32
*data
)
81 mutex_lock(&ipc_m1_lock
);
83 init_completion(&ipc_completion
);
84 __ipc_send(IPC_TX_MBOX
, data
);
85 ret
= wait_for_completion_timeout(&ipc_completion
,
86 msecs_to_jiffies(1000));
92 ret
= __ipc_rcv(IPC_TX_MBOX
, data
);
94 mutex_unlock(&ipc_m1_lock
);
97 EXPORT_SYMBOL_GPL(pl320_ipc_transmit
);
99 static irqreturn_t
ipc_handler(int irq
, void *dev
)
104 irq_stat
= readl_relaxed(ipc_base
+ IPCMMIS(1));
105 if (irq_stat
& MBOX_MASK(IPC_TX_MBOX
)) {
106 writel_relaxed(0, ipc_base
+ IPCMxSEND(IPC_TX_MBOX
));
107 complete(&ipc_completion
);
109 if (irq_stat
& MBOX_MASK(IPC_RX_MBOX
)) {
110 __ipc_rcv(IPC_RX_MBOX
, data
);
111 atomic_notifier_call_chain(&ipc_notifier
, data
[0], data
+ 1);
112 writel_relaxed(2, ipc_base
+ IPCMxSEND(IPC_RX_MBOX
));
118 int pl320_ipc_register_notifier(struct notifier_block
*nb
)
120 return atomic_notifier_chain_register(&ipc_notifier
, nb
);
122 EXPORT_SYMBOL_GPL(pl320_ipc_register_notifier
);
124 int pl320_ipc_unregister_notifier(struct notifier_block
*nb
)
126 return atomic_notifier_chain_unregister(&ipc_notifier
, nb
);
128 EXPORT_SYMBOL_GPL(pl320_ipc_unregister_notifier
);
130 static int pl320_probe(struct amba_device
*adev
, const struct amba_id
*id
)
134 ipc_base
= ioremap(adev
->res
.start
, resource_size(&adev
->res
));
135 if (ipc_base
== NULL
)
138 writel_relaxed(0, ipc_base
+ IPCMxSEND(IPC_TX_MBOX
));
140 ipc_irq
= adev
->irq
[0];
141 ret
= request_irq(ipc_irq
, ipc_handler
, 0, dev_name(&adev
->dev
), NULL
);
145 /* Init slow mailbox */
146 writel_relaxed(CHAN_MASK(A9_SOURCE
),
147 ipc_base
+ IPCMxSOURCE(IPC_TX_MBOX
));
148 writel_relaxed(CHAN_MASK(M3_SOURCE
),
149 ipc_base
+ IPCMxDSET(IPC_TX_MBOX
));
150 writel_relaxed(CHAN_MASK(M3_SOURCE
) | CHAN_MASK(A9_SOURCE
),
151 ipc_base
+ IPCMxMSET(IPC_TX_MBOX
));
153 /* Init receive mailbox */
154 writel_relaxed(CHAN_MASK(M3_SOURCE
),
155 ipc_base
+ IPCMxSOURCE(IPC_RX_MBOX
));
156 writel_relaxed(CHAN_MASK(A9_SOURCE
),
157 ipc_base
+ IPCMxDSET(IPC_RX_MBOX
));
158 writel_relaxed(CHAN_MASK(M3_SOURCE
) | CHAN_MASK(A9_SOURCE
),
159 ipc_base
+ IPCMxMSET(IPC_RX_MBOX
));
167 static struct amba_id pl320_ids
[] = {
175 static struct amba_driver pl320_driver
= {
179 .id_table
= pl320_ids
,
180 .probe
= pl320_probe
,
183 static int __init
ipc_init(void)
185 return amba_driver_register(&pl320_driver
);
187 subsys_initcall(ipc_init
);