blob: 69c86e059f5a5f68d42bccebbcf43317b4551420 [file] [log] [blame]
Fabien Dessenne01964d82019-05-14 11:20:34 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) STMicroelectronics 2019 - All Rights Reserved
4 */
5
Patrick Delaunayff2a09c2020-11-06 19:01:49 +01006#define LOG_CATEGORY UCLASS_MAILBOX
7
Fabien Dessenne01964d82019-05-14 11:20:34 +02008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Fabien Dessenne01964d82019-05-14 11:20:34 +020012#include <mailbox-uclass.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Fabien Dessenne01964d82019-05-14 11:20:34 +020014#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Fabien Dessenne01964d82019-05-14 11:20:34 +020017
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
38struct stm32_ipcc {
39 void __iomem *reg_base;
40 void __iomem *reg_proc;
41 u32 proc_id;
42 u32 n_chans;
43};
44
45static int stm32_ipcc_request(struct mbox_chan *chan)
46{
47 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
48
Patrick Delaunayff2a09c2020-11-06 19:01:49 +010049 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenne01964d82019-05-14 11:20:34 +020050
51 if (chan->id >= ipcc->n_chans) {
Patrick Delaunayff2a09c2020-11-06 19:01:49 +010052 dev_dbg(chan->dev, "failed to request channel: %ld\n",
53 chan->id);
Fabien Dessenne01964d82019-05-14 11:20:34 +020054 return -EINVAL;
55 }
56
57 return 0;
58}
59
60static int stm32_ipcc_free(struct mbox_chan *chan)
61{
Patrick Delaunayff2a09c2020-11-06 19:01:49 +010062 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenne01964d82019-05-14 11:20:34 +020063
64 return 0;
65}
66
67static int stm32_ipcc_send(struct mbox_chan *chan, const void *data)
68{
69 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
70
Patrick Delaunayff2a09c2020-11-06 19:01:49 +010071 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenne01964d82019-05-14 11:20:34 +020072
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
82static 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 Delaunayff2a09c2020-11-06 19:01:49 +010088 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenne01964d82019-05-14 11:20:34 +020089
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
102static int stm32_ipcc_probe(struct udevice *dev)
103{
104 struct stm32_ipcc *ipcc = dev_get_priv(dev);
105 fdt_addr_t addr;
Fabien Dessenne01964d82019-05-14 11:20:34 +0200106 struct clk clk;
Patrick Delaunay04e29ca2020-09-09 17:48:15 +0200107 int ret;
Fabien Dessenne01964d82019-05-14 11:20:34 +0200108
Patrick Delaunayff2a09c2020-11-06 19:01:49 +0100109 dev_dbg(dev, "\n");
Fabien Dessenne01964d82019-05-14 11:20:34 +0200110
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 Delaunay04e29ca2020-09-09 17:48:15 +0200118 ret = dev_read_u32_index(dev, "st,proc_id", 1, &ipcc->proc_id);
119 if (ret) {
Fabien Dessenne01964d82019-05-14 11:20:34 +0200120 dev_dbg(dev, "Missing st,proc_id\n");
121 return -EINVAL;
122 }
123
Fabien Dessenne01964d82019-05-14 11:20:34 +0200124 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)
137 goto clk_free;
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;
144
145clk_free:
146 clk_free(&clk);
147
148 return ret;
149}
150
151static const struct udevice_id stm32_ipcc_ids[] = {
152 { .compatible = "st,stm32mp1-ipcc" },
153 { }
154};
155
156struct mbox_ops stm32_ipcc_mbox_ops = {
157 .request = stm32_ipcc_request,
Simon Glasscc92c3c2020-02-03 07:35:50 -0700158 .rfree = stm32_ipcc_free,
Fabien Dessenne01964d82019-05-14 11:20:34 +0200159 .send = stm32_ipcc_send,
160 .recv = stm32_ipcc_recv,
161};
162
163U_BOOT_DRIVER(stm32_ipcc) = {
164 .name = "stm32_ipcc",
165 .id = UCLASS_MAILBOX,
166 .of_match = stm32_ipcc_ids,
167 .probe = stm32_ipcc_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700168 .priv_auto = sizeof(struct stm32_ipcc),
Fabien Dessenne01964d82019-05-14 11:20:34 +0200169 .ops = &stm32_ipcc_mbox_ops,
170};