2 * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd.
3 * Copyright (C) 2015 Linaro Ltd.
4 * Author: Jassi Brar <jaswinder.singh@linaro.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/interrupt.h>
17 #include <linux/spinlock.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <linux/amba/bus.h>
25 #include <linux/mailbox_controller.h>
27 #define INTR_STAT_OFS 0x0
28 #define INTR_SET_OFS 0x8
29 #define INTR_CLR_OFS 0x10
31 #define MHU_LP_OFFSET 0x0
32 #define MHU_HP_OFFSET 0x20
33 #define MHU_SEC_OFFSET 0x200
34 #define TX_REG_OFFSET 0x100
46 struct mhu_link mlink
[MHU_CHANS
];
47 struct mbox_chan chan
[MHU_CHANS
];
48 struct mbox_controller mbox
;
51 static irqreturn_t
mhu_rx_interrupt(int irq
, void *p
)
53 struct mbox_chan
*chan
= p
;
54 struct mhu_link
*mlink
= chan
->con_priv
;
57 val
= readl_relaxed(mlink
->rx_reg
+ INTR_STAT_OFS
);
61 mbox_chan_received_data(chan
, (void *)&val
);
63 writel_relaxed(val
, mlink
->rx_reg
+ INTR_CLR_OFS
);
68 static bool mhu_last_tx_done(struct mbox_chan
*chan
)
70 struct mhu_link
*mlink
= chan
->con_priv
;
71 u32 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
76 static int mhu_send_data(struct mbox_chan
*chan
, void *data
)
78 struct mhu_link
*mlink
= chan
->con_priv
;
81 writel_relaxed(*arg
, mlink
->tx_reg
+ INTR_SET_OFS
);
86 static int mhu_startup(struct mbox_chan
*chan
)
88 struct mhu_link
*mlink
= chan
->con_priv
;
92 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
93 writel_relaxed(val
, mlink
->tx_reg
+ INTR_CLR_OFS
);
95 ret
= request_irq(mlink
->irq
, mhu_rx_interrupt
,
96 IRQF_SHARED
, "mhu_link", chan
);
98 dev_err(chan
->mbox
->dev
,
99 "Unable to aquire IRQ %d\n", mlink
->irq
);
106 static void mhu_shutdown(struct mbox_chan
*chan
)
108 struct mhu_link
*mlink
= chan
->con_priv
;
110 free_irq(mlink
->irq
, chan
);
113 static const struct mbox_chan_ops mhu_ops
= {
114 .send_data
= mhu_send_data
,
115 .startup
= mhu_startup
,
116 .shutdown
= mhu_shutdown
,
117 .last_tx_done
= mhu_last_tx_done
,
120 static int mhu_probe(struct amba_device
*adev
, const struct amba_id
*id
)
124 struct device
*dev
= &adev
->dev
;
125 int mhu_reg
[MHU_CHANS
] = {MHU_LP_OFFSET
, MHU_HP_OFFSET
, MHU_SEC_OFFSET
};
127 /* Allocate memory for device */
128 mhu
= devm_kzalloc(dev
, sizeof(*mhu
), GFP_KERNEL
);
132 mhu
->base
= devm_ioremap_resource(dev
, &adev
->res
);
133 if (IS_ERR(mhu
->base
)) {
134 dev_err(dev
, "ioremap failed\n");
135 return PTR_ERR(mhu
->base
);
138 for (i
= 0; i
< MHU_CHANS
; i
++) {
139 mhu
->chan
[i
].con_priv
= &mhu
->mlink
[i
];
140 mhu
->mlink
[i
].irq
= adev
->irq
[i
];
141 mhu
->mlink
[i
].rx_reg
= mhu
->base
+ mhu_reg
[i
];
142 mhu
->mlink
[i
].tx_reg
= mhu
->mlink
[i
].rx_reg
+ TX_REG_OFFSET
;
146 mhu
->mbox
.chans
= &mhu
->chan
[0];
147 mhu
->mbox
.num_chans
= MHU_CHANS
;
148 mhu
->mbox
.ops
= &mhu_ops
;
149 mhu
->mbox
.txdone_irq
= false;
150 mhu
->mbox
.txdone_poll
= true;
151 mhu
->mbox
.txpoll_period
= 10;
153 amba_set_drvdata(adev
, mhu
);
155 err
= mbox_controller_register(&mhu
->mbox
);
157 dev_err(dev
, "Failed to register mailboxes %d\n", err
);
161 dev_info(dev
, "ARM MHU Mailbox registered\n");
165 static int mhu_remove(struct amba_device
*adev
)
167 struct arm_mhu
*mhu
= amba_get_drvdata(adev
);
169 mbox_controller_unregister(&mhu
->mbox
);
174 static struct amba_id mhu_ids
[] = {
181 MODULE_DEVICE_TABLE(amba
, mhu_ids
);
183 static struct amba_driver arm_mhu_driver
= {
189 .remove
= mhu_remove
,
191 module_amba_driver(arm_mhu_driver
);
193 MODULE_LICENSE("GPL v2");
194 MODULE_DESCRIPTION("ARM MHU Driver");
195 MODULE_AUTHOR("Jassi Brar <jassisinghbrar@gmail.com>");