Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2019-2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | * SPDX-License-Identifier: GPL-2.0 |
| 4 | * |
| 5 | * This program is free software and is provided to you under the terms of the |
| 6 | * GNU General Public License version 2 as published by the Free Software |
| 7 | * Foundation, and any use by you of this program is subject to the terms |
| 8 | * of such GNU licence. |
| 9 | * |
| 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, you can access it online at |
| 17 | * http://www.gnu.org/licenses/gpl-2.0.html. |
| 18 | * |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 19 | * Message Handling Unit version 2 controller driver |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 20 | * Based on drivers/mailbox/arm_mhu.c |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/amba/bus.h> |
| 30 | #include <linux/mailbox_controller.h> |
| 31 | #include <linux/of_device.h> |
| 32 | #include <linux/of_address.h> |
Anton Moberg | bdcb622 | 2021-08-25 14:06:24 +0200 | [diff] [blame] | 33 | #include <linux/version.h> |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 34 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 35 | #define MHU_V2_REG_STAT_OFS 0x0 |
| 36 | #define MHU_V2_REG_CLR_OFS 0x8 |
| 37 | #define MHU_V2_REG_SET_OFS 0xC |
| 38 | #define MHU_V2_REG_MSG_NO_CAP_OFS 0xF80 |
| 39 | #define MHU_V2_REG_ACC_REQ_OFS 0xF88 |
| 40 | #define MHU_V2_REG_ACC_RDY_OFS 0xF8C |
| 41 | #define MHU_V2_INT_EN_OFS 0xF98 |
| 42 | #define MHU_V2_AIDR_OFS 0xFCC |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 43 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 44 | #define MHU_V2_CHCOMB BIT(2) |
| 45 | #define MHU_V2_AIDR_MINOR(_reg) ((_reg) & 0xF) |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 46 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 47 | #define MHU_V2_EACH_CHANNEL_SIZE 0x20 |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 48 | |
| 49 | #define mbox_to_arm_mhuv2(c) container_of(c, struct arm_mhuv2, mbox) |
| 50 | |
| 51 | struct mhuv2_link { |
| 52 | unsigned int irq; |
| 53 | void __iomem *tx_reg; |
| 54 | void __iomem *rx_reg; |
| 55 | }; |
| 56 | |
| 57 | struct arm_mhuv2 { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 58 | void __iomem *base; |
| 59 | struct mhuv2_link *mlink; |
| 60 | struct mbox_chan *chan; |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 61 | struct mbox_controller mbox; |
| 62 | }; |
| 63 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 64 | static irqreturn_t mhuv2_rx_interrupt(int irq, |
| 65 | void *p) |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 66 | { |
| 67 | struct mbox_chan *chan = p; |
| 68 | struct mhuv2_link *mlink = chan->con_priv; |
| 69 | u32 val; |
| 70 | |
| 71 | val = readl_relaxed(mlink->rx_reg + MHU_V2_REG_STAT_OFS); |
| 72 | if (!val) |
| 73 | return IRQ_NONE; |
| 74 | |
| 75 | mbox_chan_received_data(chan, (void *)&val); |
| 76 | |
| 77 | writel_relaxed(val, mlink->rx_reg + MHU_V2_REG_CLR_OFS); |
| 78 | |
| 79 | return IRQ_HANDLED; |
| 80 | } |
| 81 | |
| 82 | static bool mhuv2_last_tx_done(struct mbox_chan *chan) |
| 83 | { |
| 84 | struct mhuv2_link *mlink = chan->con_priv; |
| 85 | u32 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS); |
| 86 | |
| 87 | return (val == 0); |
| 88 | } |
| 89 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 90 | static int mhuv2_send_data(struct mbox_chan *chan, |
| 91 | void *data) |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 92 | { |
| 93 | struct mhuv2_link *mlink = chan->con_priv; |
Per Åstrand | 8fac4a4 | 2020-10-12 13:37:16 +0200 | [diff] [blame] | 94 | struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox); |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 95 | u32 *arg = data; |
Per Åstrand | 8fac4a4 | 2020-10-12 13:37:16 +0200 | [diff] [blame] | 96 | u32 tmo = 100000; |
| 97 | |
| 98 | /* If ACCESS_REQUEST is low, we have to wait for the other side |
| 99 | * to relase ACCESS_READY before continuing. */ |
| 100 | if (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_REQ_OFS)) { |
| 101 | while (readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) && |
| 102 | --tmo != 0) |
| 103 | continue; |
| 104 | |
| 105 | if (!tmo) |
| 106 | goto err; |
| 107 | |
| 108 | /* Request access and wait for other side to ack */ |
| 109 | writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS); |
| 110 | tmo = 100000; |
| 111 | while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) && |
| 112 | --tmo != 0) |
| 113 | continue; |
| 114 | |
| 115 | if (!tmo) |
| 116 | goto err; |
| 117 | } else { |
| 118 | while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) && |
| 119 | --tmo != 0) |
| 120 | continue; |
| 121 | |
| 122 | if (!tmo) |
| 123 | goto err; |
| 124 | } |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 125 | |
| 126 | writel_relaxed(*arg, mlink->tx_reg + MHU_V2_REG_SET_OFS); |
| 127 | |
| 128 | return 0; |
Per Åstrand | 8fac4a4 | 2020-10-12 13:37:16 +0200 | [diff] [blame] | 129 | |
| 130 | err: |
| 131 | dev_err(chan->mbox->dev, "Failed to acquire access to mhu.\n"); |
| 132 | |
| 133 | return 1; |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int mhuv2_startup(struct mbox_chan *chan) |
| 137 | { |
| 138 | struct mhuv2_link *mlink = chan->con_priv; |
| 139 | u32 val; |
| 140 | int ret; |
| 141 | struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox); |
| 142 | |
| 143 | writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS); |
| 144 | |
| 145 | val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS); |
| 146 | writel_relaxed(val, mlink->tx_reg + MHU_V2_REG_CLR_OFS); |
| 147 | |
| 148 | ret = request_irq(mlink->irq, mhuv2_rx_interrupt, |
| 149 | IRQF_SHARED, "mhuv2_link", chan); |
| 150 | if (ret) { |
| 151 | dev_err(chan->mbox->dev, |
| 152 | "unable to acquire IRQ %d\n", mlink->irq); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 153 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void mhuv2_shutdown(struct mbox_chan *chan) |
| 161 | { |
| 162 | struct mhuv2_link *mlink = chan->con_priv; |
| 163 | struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox); |
| 164 | |
| 165 | writel_relaxed(0x0, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS); |
| 166 | |
| 167 | free_irq(mlink->irq, chan); |
| 168 | } |
| 169 | |
| 170 | static const struct mbox_chan_ops mhuv2_ops = { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 171 | .send_data = mhuv2_send_data, |
| 172 | .startup = mhuv2_startup, |
| 173 | .shutdown = mhuv2_shutdown, |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 174 | .last_tx_done = mhuv2_last_tx_done, |
| 175 | }; |
| 176 | |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 177 | static void mhuv2_check_enable_cmbint(struct mhuv2_link *link) |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 178 | { |
| 179 | const u32 aidr = readl_relaxed(link->rx_reg + MHU_V2_AIDR_OFS); |
| 180 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 181 | if (MHU_V2_AIDR_MINOR(aidr) == 1) |
| 182 | /* Enable combined receiver interrupt for MHUv2.1 */ |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 183 | writel_relaxed(MHU_V2_CHCOMB, link->rx_reg + MHU_V2_INT_EN_OFS); |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 186 | static int mhuv2_probe(struct amba_device *adev, |
| 187 | const struct amba_id *id) |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 188 | { |
| 189 | int i, err; |
| 190 | struct arm_mhuv2 *mhuv2; |
| 191 | struct device *dev = &adev->dev; |
| 192 | void __iomem *rx_base, *tx_base; |
| 193 | const struct device_node *np = dev->of_node; |
| 194 | unsigned int pchans; |
| 195 | struct mhuv2_link *mlink; |
| 196 | struct mbox_chan *chan; |
| 197 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 198 | /* Allocate memory for device */ |
| 199 | mhuv2 = devm_kzalloc(dev, sizeof(*mhuv2), GFP_KERNEL); |
| 200 | if (!mhuv2) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | tx_base = of_iomap((struct device_node *)np, 0); |
| 204 | if (!tx_base) { |
| 205 | dev_err(dev, "failed to map tx registers\n"); |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 206 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 207 | return -ENOMEM; |
| 208 | } |
| 209 | |
| 210 | rx_base = of_iomap((struct device_node *)np, 1); |
| 211 | if (!rx_base) { |
| 212 | dev_err(dev, "failed to map rx registers\n"); |
Jonny Svärd | 44c507e | 2021-02-09 16:37:40 +0100 | [diff] [blame] | 213 | iounmap(tx_base); |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 214 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 215 | return -ENOMEM; |
| 216 | } |
| 217 | |
| 218 | pchans = readl_relaxed(tx_base + MHU_V2_REG_MSG_NO_CAP_OFS); |
| 219 | if (pchans == 0 || pchans % 2) { |
| 220 | dev_err(dev, "invalid number of channels %d\n", pchans); |
| 221 | iounmap(rx_base); |
| 222 | iounmap(tx_base); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 223 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | mhuv2->mlink = devm_kcalloc(dev, pchans, sizeof(*mlink), GFP_KERNEL); |
| 228 | if (!mhuv2->mlink) { |
| 229 | iounmap(rx_base); |
| 230 | iounmap(tx_base); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 231 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 232 | return -ENOMEM; |
| 233 | } |
| 234 | |
| 235 | mhuv2->chan = devm_kcalloc(dev, pchans, sizeof(*chan), GFP_KERNEL); |
| 236 | if (!mhuv2->chan) { |
| 237 | iounmap(rx_base); |
| 238 | iounmap(tx_base); |
| 239 | kfree(mhuv2->mlink); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 240 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 241 | return -ENOMEM; |
| 242 | } |
| 243 | |
| 244 | for (i = 0; i < pchans; i++) { |
| 245 | mlink = mhuv2->mlink + i; |
| 246 | chan = mhuv2->chan + i; |
| 247 | chan->con_priv = mlink; |
| 248 | mlink->rx_reg = rx_base + (i * MHU_V2_EACH_CHANNEL_SIZE); |
| 249 | mlink->tx_reg = tx_base + (i * MHU_V2_EACH_CHANNEL_SIZE); |
| 250 | } |
| 251 | |
| 252 | mhuv2->mlink->irq = adev->irq[0]; |
| 253 | mhuv2_check_enable_cmbint(mhuv2->mlink); |
| 254 | |
| 255 | mhuv2->base = tx_base; |
| 256 | mhuv2->mbox.dev = dev; |
| 257 | mhuv2->mbox.chans = mhuv2->chan; |
| 258 | mhuv2->mbox.num_chans = pchans; |
| 259 | mhuv2->mbox.ops = &mhuv2_ops; |
| 260 | mhuv2->mbox.txdone_irq = false; |
| 261 | mhuv2->mbox.txdone_poll = true; |
| 262 | mhuv2->mbox.txpoll_period = 1; |
| 263 | |
| 264 | amba_set_drvdata(adev, mhuv2); |
| 265 | |
| 266 | err = mbox_controller_register(&mhuv2->mbox); |
| 267 | if (err) { |
| 268 | dev_err(dev, "failed to register mailboxes %d\n", err); |
| 269 | iounmap(rx_base); |
| 270 | iounmap(tx_base); |
| 271 | kfree(mhuv2->mlink); |
| 272 | kfree(mhuv2->chan); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 273 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 274 | return err; |
| 275 | } |
| 276 | |
| 277 | dev_info(dev, "ARM MHUv2 Mailbox driver registered\n"); |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 278 | |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Anton Moberg | bdcb622 | 2021-08-25 14:06:24 +0200 | [diff] [blame] | 282 | #if KERNEL_VERSION(5, 12, 0) <= LINUX_VERSION_CODE |
| 283 | static void mhuv2_remove(struct amba_device *adev) |
| 284 | { |
| 285 | struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev); |
| 286 | |
| 287 | mbox_controller_unregister(&mhuv2->mbox); |
| 288 | } |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 289 | |
Anton Moberg | bdcb622 | 2021-08-25 14:06:24 +0200 | [diff] [blame] | 290 | #else |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 291 | static int mhuv2_remove(struct amba_device *adev) |
| 292 | { |
| 293 | struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev); |
| 294 | |
| 295 | mbox_controller_unregister(&mhuv2->mbox); |
| 296 | |
| 297 | return 0; |
| 298 | } |
Anton Moberg | bdcb622 | 2021-08-25 14:06:24 +0200 | [diff] [blame] | 299 | |
Mikael Olsson | 404a536 | 2023-11-14 10:42:14 +0100 | [diff] [blame^] | 300 | #endif |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 301 | |
| 302 | static struct amba_id mhuv2_ids[] = { |
| 303 | { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 304 | .id = 0x4b0d1, |
| 305 | .mask = 0xfffff, |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 306 | }, |
| 307 | { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 308 | .id = 0xbb0d1, |
| 309 | .mask = 0xfffff, |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 310 | }, |
| 311 | { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 312 | .id = 0xbb076, |
| 313 | .mask = 0xfffff, |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 314 | }, |
| 315 | { 0, 0 }, |
| 316 | }; |
| 317 | MODULE_DEVICE_TABLE(amba, mhuv2_ids); |
| 318 | |
| 319 | static struct amba_driver arm_mhuv2_driver = { |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 320 | .drv = { |
| 321 | .name = "mhuv2", |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 322 | }, |
Per Åstrand | d748336 | 2020-10-07 13:50:46 +0200 | [diff] [blame] | 323 | .id_table = mhuv2_ids, |
| 324 | .probe = mhuv2_probe, |
| 325 | .remove = mhuv2_remove, |
Per Åstrand | be87baf | 2020-10-07 13:47:24 +0200 | [diff] [blame] | 326 | }; |
| 327 | module_amba_driver(arm_mhuv2_driver); |
| 328 | |
| 329 | MODULE_LICENSE("GPL v2"); |
| 330 | MODULE_DESCRIPTION("ARM MHUv2 Driver"); |
| 331 | MODULE_AUTHOR("Samarth Parikh <samarthp@ymail.com>"); |