Ibai Erkiaga | 4b1264d | 2019-09-27 11:36:56 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Xilinx Zynq MPSoC Mailbox driver |
| 4 | * |
| 5 | * Copyright (C) 2018-2019 Xilinx, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Ibai Erkiaga | 4b1264d | 2019-09-27 11:36:56 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <dm.h> |
| 12 | #include <mailbox-uclass.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Ibai Erkiaga | 4b1264d | 2019-09-27 11:36:56 +0100 | [diff] [blame] | 14 | #include <mach/sys_proto.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <wait_bit.h> |
| 18 | |
| 19 | /* IPI bitmasks, register base */ |
| 20 | /* TODO: move reg base to DT */ |
| 21 | #define IPI_BIT_MASK_PMU0 0x10000 |
| 22 | #define IPI_INT_REG_BASE_APU 0xFF300000 |
| 23 | |
| 24 | struct ipi_int_regs { |
| 25 | u32 trig; /* 0x0 */ |
| 26 | u32 obs; /* 0x4 */ |
| 27 | u32 ist; /* 0x8 */ |
| 28 | u32 imr; /* 0xC */ |
| 29 | u32 ier; /* 0x10 */ |
| 30 | u32 idr; /* 0x14 */ |
| 31 | }; |
| 32 | |
| 33 | #define ipi_int_apu ((struct ipi_int_regs *)IPI_INT_REG_BASE_APU) |
| 34 | |
| 35 | struct zynqmp_ipi { |
| 36 | void __iomem *local_req_regs; |
| 37 | void __iomem *local_res_regs; |
| 38 | void __iomem *remote_req_regs; |
| 39 | void __iomem *remote_res_regs; |
| 40 | }; |
| 41 | |
| 42 | static int zynqmp_ipi_send(struct mbox_chan *chan, const void *data) |
| 43 | { |
| 44 | const struct zynqmp_ipi_msg *msg = (struct zynqmp_ipi_msg *)data; |
| 45 | struct zynqmp_ipi *zynqmp = dev_get_priv(chan->dev); |
| 46 | u32 ret; |
| 47 | u32 *mbx = (u32 *)zynqmp->local_req_regs; |
| 48 | |
| 49 | for (size_t i = 0; i < msg->len; i++) |
| 50 | writel(msg->buf[i], &mbx[i]); |
| 51 | |
| 52 | /* Write trigger interrupt */ |
| 53 | writel(IPI_BIT_MASK_PMU0, &ipi_int_apu->trig); |
| 54 | |
| 55 | /* Wait until observation bit is cleared */ |
| 56 | ret = wait_for_bit_le32(&ipi_int_apu->obs, IPI_BIT_MASK_PMU0, false, |
| 57 | 100, false); |
| 58 | |
| 59 | debug("%s, send %ld bytes\n", __func__, msg->len); |
| 60 | return ret; |
| 61 | }; |
| 62 | |
| 63 | static int zynqmp_ipi_recv(struct mbox_chan *chan, void *data) |
| 64 | { |
| 65 | struct zynqmp_ipi_msg *msg = (struct zynqmp_ipi_msg *)data; |
| 66 | struct zynqmp_ipi *zynqmp = dev_get_priv(chan->dev); |
| 67 | u32 *mbx = (u32 *)zynqmp->local_res_regs; |
| 68 | |
| 69 | for (size_t i = 0; i < msg->len; i++) |
| 70 | msg->buf[i] = readl(&mbx[i]); |
| 71 | |
| 72 | debug("%s, recv %ld bytes\n", __func__, msg->len); |
| 73 | return 0; |
| 74 | }; |
| 75 | |
| 76 | static int zynqmp_ipi_probe(struct udevice *dev) |
| 77 | { |
| 78 | struct zynqmp_ipi *zynqmp = dev_get_priv(dev); |
| 79 | struct resource res; |
| 80 | ofnode node; |
| 81 | |
| 82 | debug("%s(dev=%p)\n", __func__, dev); |
| 83 | |
| 84 | /* Get subnode where the regs are defined */ |
| 85 | /* Note IPI mailbox node needs to be the first one in DT */ |
| 86 | node = ofnode_first_subnode(dev_ofnode(dev)); |
| 87 | |
| 88 | if (ofnode_read_resource_byname(node, "local_request_region", &res)) { |
| 89 | dev_err(dev, "No reg property for local_request_region\n"); |
| 90 | return -EINVAL; |
| 91 | }; |
| 92 | zynqmp->local_req_regs = devm_ioremap(dev, res.start, |
| 93 | (res.start - res.end)); |
| 94 | |
| 95 | if (ofnode_read_resource_byname(node, "local_response_region", &res)) { |
| 96 | dev_err(dev, "No reg property for local_response_region\n"); |
| 97 | return -EINVAL; |
| 98 | }; |
| 99 | zynqmp->local_res_regs = devm_ioremap(dev, res.start, |
| 100 | (res.start - res.end)); |
| 101 | |
| 102 | if (ofnode_read_resource_byname(node, "remote_request_region", &res)) { |
| 103 | dev_err(dev, "No reg property for remote_request_region\n"); |
| 104 | return -EINVAL; |
| 105 | }; |
| 106 | zynqmp->remote_req_regs = devm_ioremap(dev, res.start, |
| 107 | (res.start - res.end)); |
| 108 | |
| 109 | if (ofnode_read_resource_byname(node, "remote_response_region", &res)) { |
| 110 | dev_err(dev, "No reg property for remote_response_region\n"); |
| 111 | return -EINVAL; |
| 112 | }; |
| 113 | zynqmp->remote_res_regs = devm_ioremap(dev, res.start, |
| 114 | (res.start - res.end)); |
| 115 | |
| 116 | return 0; |
| 117 | }; |
| 118 | |
| 119 | static const struct udevice_id zynqmp_ipi_ids[] = { |
| 120 | { .compatible = "xlnx,zynqmp-ipi-mailbox" }, |
| 121 | { } |
| 122 | }; |
| 123 | |
| 124 | struct mbox_ops zynqmp_ipi_mbox_ops = { |
| 125 | .send = zynqmp_ipi_send, |
| 126 | .recv = zynqmp_ipi_recv, |
| 127 | }; |
| 128 | |
| 129 | U_BOOT_DRIVER(zynqmp_ipi) = { |
| 130 | .name = "zynqmp-ipi", |
| 131 | .id = UCLASS_MAILBOX, |
| 132 | .of_match = zynqmp_ipi_ids, |
| 133 | .probe = zynqmp_ipi_probe, |
| 134 | .priv_auto_alloc_size = sizeof(struct zynqmp_ipi), |
| 135 | .ops = &zynqmp_ipi_mbox_ops, |
| 136 | }; |