Fabien Dessenne | 01964d8 | 2019-05-14 11:20:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) STMicroelectronics 2019 - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Fabien Dessenne | 01964d8 | 2019-05-14 11:20:34 +0200 | [diff] [blame] | 10 | #include <mailbox-uclass.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Fabien Dessenne | 01964d8 | 2019-05-14 11:20:34 +0200 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Fabien Dessenne | 01964d8 | 2019-05-14 11:20:34 +0200 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * IPCC has one set of registers per CPU |
| 18 | * IPCC_PROC_OFFST allows to define cpu registers set base address |
| 19 | * according to the assigned proc_id. |
| 20 | */ |
| 21 | |
| 22 | #define IPCC_PROC_OFFST 0x010 |
| 23 | |
| 24 | #define IPCC_XSCR 0x008 |
| 25 | #define IPCC_XTOYSR 0x00c |
| 26 | |
| 27 | #define IPCC_HWCFGR 0x3f0 |
| 28 | #define IPCFGR_CHAN_MASK GENMASK(7, 0) |
| 29 | |
| 30 | #define RX_BIT_CHAN(chan) BIT(chan) |
| 31 | #define TX_BIT_SHIFT 16 |
| 32 | #define TX_BIT_CHAN(chan) BIT(TX_BIT_SHIFT + (chan)) |
| 33 | |
| 34 | #define STM32_MAX_PROCS 2 |
| 35 | |
| 36 | struct stm32_ipcc { |
| 37 | void __iomem *reg_base; |
| 38 | void __iomem *reg_proc; |
| 39 | u32 proc_id; |
| 40 | u32 n_chans; |
| 41 | }; |
| 42 | |
| 43 | static int stm32_ipcc_request(struct mbox_chan *chan) |
| 44 | { |
| 45 | struct stm32_ipcc *ipcc = dev_get_priv(chan->dev); |
| 46 | |
| 47 | debug("%s(chan=%p)\n", __func__, chan); |
| 48 | |
| 49 | if (chan->id >= ipcc->n_chans) { |
| 50 | debug("%s failed to request channel: %ld\n", |
| 51 | __func__, chan->id); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int stm32_ipcc_free(struct mbox_chan *chan) |
| 59 | { |
| 60 | debug("%s(chan=%p)\n", __func__, chan); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int stm32_ipcc_send(struct mbox_chan *chan, const void *data) |
| 66 | { |
| 67 | struct stm32_ipcc *ipcc = dev_get_priv(chan->dev); |
| 68 | |
| 69 | debug("%s(chan=%p, data=%p)\n", __func__, chan, data); |
| 70 | |
| 71 | if (readl(ipcc->reg_proc + IPCC_XTOYSR) & BIT(chan->id)) |
| 72 | return -EBUSY; |
| 73 | |
| 74 | /* set channel n occupied */ |
| 75 | setbits_le32(ipcc->reg_proc + IPCC_XSCR, TX_BIT_CHAN(chan->id)); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int stm32_ipcc_recv(struct mbox_chan *chan, void *data) |
| 81 | { |
| 82 | struct stm32_ipcc *ipcc = dev_get_priv(chan->dev); |
| 83 | u32 val; |
| 84 | int proc_offset; |
| 85 | |
| 86 | debug("%s(chan=%p, data=%p)\n", __func__, chan, data); |
| 87 | |
| 88 | /* read 'channel occupied' status from other proc */ |
| 89 | proc_offset = ipcc->proc_id ? -IPCC_PROC_OFFST : IPCC_PROC_OFFST; |
| 90 | val = readl(ipcc->reg_proc + proc_offset + IPCC_XTOYSR); |
| 91 | |
| 92 | if (!(val & BIT(chan->id))) |
| 93 | return -ENODATA; |
| 94 | |
| 95 | setbits_le32(ipcc->reg_proc + IPCC_XSCR, RX_BIT_CHAN(chan->id)); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int stm32_ipcc_probe(struct udevice *dev) |
| 101 | { |
| 102 | struct stm32_ipcc *ipcc = dev_get_priv(dev); |
| 103 | fdt_addr_t addr; |
| 104 | const fdt32_t *cell; |
| 105 | struct clk clk; |
| 106 | int len, ret; |
| 107 | |
| 108 | debug("%s(dev=%p)\n", __func__, dev); |
| 109 | |
| 110 | addr = dev_read_addr(dev); |
| 111 | if (addr == FDT_ADDR_T_NONE) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | ipcc->reg_base = (void __iomem *)addr; |
| 115 | |
| 116 | /* proc_id */ |
| 117 | cell = dev_read_prop(dev, "st,proc_id", &len); |
| 118 | if (len < sizeof(fdt32_t)) { |
| 119 | dev_dbg(dev, "Missing st,proc_id\n"); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | ipcc->proc_id = fdtdec_get_number(cell, 1); |
| 124 | |
| 125 | if (ipcc->proc_id >= STM32_MAX_PROCS) { |
| 126 | dev_err(dev, "Invalid proc_id (%d)\n", ipcc->proc_id); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | ipcc->reg_proc = ipcc->reg_base + ipcc->proc_id * IPCC_PROC_OFFST; |
| 131 | |
| 132 | ret = clk_get_by_index(dev, 0, &clk); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
| 136 | ret = clk_enable(&clk); |
| 137 | if (ret) |
| 138 | goto clk_free; |
| 139 | |
| 140 | /* get channel number */ |
| 141 | ipcc->n_chans = readl(ipcc->reg_base + IPCC_HWCFGR); |
| 142 | ipcc->n_chans &= IPCFGR_CHAN_MASK; |
| 143 | |
| 144 | return 0; |
| 145 | |
| 146 | clk_free: |
| 147 | clk_free(&clk); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static const struct udevice_id stm32_ipcc_ids[] = { |
| 153 | { .compatible = "st,stm32mp1-ipcc" }, |
| 154 | { } |
| 155 | }; |
| 156 | |
| 157 | struct mbox_ops stm32_ipcc_mbox_ops = { |
| 158 | .request = stm32_ipcc_request, |
Simon Glass | cc92c3c | 2020-02-03 07:35:50 -0700 | [diff] [blame] | 159 | .rfree = stm32_ipcc_free, |
Fabien Dessenne | 01964d8 | 2019-05-14 11:20:34 +0200 | [diff] [blame] | 160 | .send = stm32_ipcc_send, |
| 161 | .recv = stm32_ipcc_recv, |
| 162 | }; |
| 163 | |
| 164 | U_BOOT_DRIVER(stm32_ipcc) = { |
| 165 | .name = "stm32_ipcc", |
| 166 | .id = UCLASS_MAILBOX, |
| 167 | .of_match = stm32_ipcc_ids, |
| 168 | .probe = stm32_ipcc_probe, |
| 169 | .priv_auto_alloc_size = sizeof(struct stm32_ipcc), |
| 170 | .ops = &stm32_ipcc_mbox_ops, |
| 171 | }; |