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