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