2021-08-07 08:00:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2022-07-26 08:40:49 +00:00
|
|
|
* Copyright 2020-2022 NXP
|
2021-08-07 08:00:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/root.h>
|
|
|
|
#include <dm/device-internal.h>
|
2022-07-26 08:40:49 +00:00
|
|
|
#include <asm/mach-imx/s400_api.h>
|
2021-08-07 08:01:09 +00:00
|
|
|
#include <asm/arch/imx-regs.h>
|
2021-08-07 08:00:41 +00:00
|
|
|
#include <linux/iopoll.h>
|
|
|
|
#include <misc.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
struct imx8ulp_mu {
|
|
|
|
struct mu_type *base;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MU_SR_TE0_MASK BIT(0)
|
|
|
|
#define MU_SR_RF0_MASK BIT(0)
|
|
|
|
#define MU_TR_COUNT 4
|
|
|
|
#define MU_RR_COUNT 4
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
void mu_hal_init(ulong base)
|
2021-08-07 08:00:41 +00:00
|
|
|
{
|
2021-08-07 08:00:55 +00:00
|
|
|
struct mu_type *mu_base = (struct mu_type *)base;
|
|
|
|
|
|
|
|
writel(0, &mu_base->tcr);
|
|
|
|
writel(0, &mu_base->rcr);
|
2021-08-07 08:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg)
|
2021-08-07 08:00:41 +00:00
|
|
|
{
|
2021-08-07 08:00:55 +00:00
|
|
|
struct mu_type *mu_base = (struct mu_type *)base;
|
2021-08-07 08:00:41 +00:00
|
|
|
u32 mask = MU_SR_TE0_MASK << reg_index;
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(reg_index < MU_TR_COUNT);
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
debug("sendmsg sr 0x%x\n", readl(&mu_base->sr));
|
2021-08-07 08:00:41 +00:00
|
|
|
|
|
|
|
/* Wait TX register to be empty. */
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000);
|
2021-08-07 08:00:41 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
debug("%s timeout\n", __func__);
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("tr[%d] 0x%x\n", reg_index, msg);
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
writel(msg, &mu_base->tr[reg_index]);
|
2021-08-07 08:00:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg)
|
2021-08-07 08:00:41 +00:00
|
|
|
{
|
2021-08-07 08:00:55 +00:00
|
|
|
struct mu_type *mu_base = (struct mu_type *)base;
|
2021-08-07 08:00:41 +00:00
|
|
|
u32 mask = MU_SR_RF0_MASK << reg_index;
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(reg_index < MU_TR_COUNT);
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
debug("receivemsg sr 0x%x\n", readl(&mu_base->sr));
|
2021-08-07 08:00:41 +00:00
|
|
|
|
|
|
|
/* Wait RX register to be full. */
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = readl_poll_timeout(&mu_base->rsr, val, val & mask, 10000);
|
2021-08-07 08:00:41 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
debug("%s timeout\n", __func__);
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:00:55 +00:00
|
|
|
*msg = readl(&mu_base->rr[reg_index]);
|
2021-08-07 08:00:41 +00:00
|
|
|
|
|
|
|
debug("rr[%d] 0x%x\n", reg_index, *msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8ulp_mu_read(struct mu_type *base, void *data)
|
|
|
|
{
|
2022-07-26 08:40:57 +00:00
|
|
|
struct sentinel_msg *msg = (struct sentinel_msg *)data;
|
2021-08-07 08:00:41 +00:00
|
|
|
int ret;
|
|
|
|
u8 count = 0;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Read first word */
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg);
|
2021-08-07 08:00:41 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* Check size */
|
|
|
|
if (msg->size > S400_MAX_MSG) {
|
|
|
|
*((u32 *)msg) = 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read remaining words */
|
|
|
|
while (count < msg->size) {
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT,
|
2021-08-07 08:00:41 +00:00
|
|
|
&msg->data[count - 1]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8ulp_mu_write(struct mu_type *base, void *data)
|
|
|
|
{
|
2022-07-26 08:40:57 +00:00
|
|
|
struct sentinel_msg *msg = (struct sentinel_msg *)data;
|
2021-08-07 08:00:41 +00:00
|
|
|
int ret;
|
|
|
|
u8 count = 0;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Check size */
|
|
|
|
if (msg->size > S400_MAX_MSG)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Write first word */
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = mu_hal_sendmsg((ulong)base, 0, *((u32 *)msg));
|
2021-08-07 08:00:41 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* Write remaining words */
|
|
|
|
while (count < msg->size) {
|
2021-08-07 08:00:55 +00:00
|
|
|
ret = mu_hal_sendmsg((ulong)base, count % MU_TR_COUNT,
|
2021-08-07 08:00:41 +00:00
|
|
|
msg->data[count - 1]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note the function prototype use msgid as the 2nd parameter, here
|
|
|
|
* we take it as no_resp.
|
|
|
|
*/
|
|
|
|
static int imx8ulp_mu_call(struct udevice *dev, int no_resp, void *tx_msg,
|
|
|
|
int tx_size, void *rx_msg, int rx_size)
|
|
|
|
{
|
|
|
|
struct imx8ulp_mu *priv = dev_get_priv(dev);
|
|
|
|
u32 result;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Expect tx_msg, rx_msg are the same value */
|
|
|
|
if (rx_msg && tx_msg != rx_msg)
|
|
|
|
printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg);
|
|
|
|
|
|
|
|
ret = imx8ulp_mu_write(priv->base, tx_msg);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (!no_resp) {
|
|
|
|
ret = imx8ulp_mu_read(priv->base, rx_msg);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:40:57 +00:00
|
|
|
result = ((struct sentinel_msg *)rx_msg)->data[0];
|
2021-08-07 08:00:51 +00:00
|
|
|
if ((result & 0xff) == 0xd6)
|
2021-08-07 08:00:41 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8ulp_mu_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct imx8ulp_mu *priv = dev_get_priv(dev);
|
|
|
|
fdt_addr_t addr;
|
|
|
|
|
|
|
|
debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
|
|
|
|
|
|
|
|
addr = devfdt_get_addr(dev);
|
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
priv->base = (struct mu_type *)addr;
|
|
|
|
|
|
|
|
debug("mu base 0x%lx\n", (ulong)priv->base);
|
|
|
|
|
|
|
|
/* U-Boot not enable interrupts, so need to enable RX interrupts */
|
2021-08-07 08:00:55 +00:00
|
|
|
mu_hal_init((ulong)priv->base);
|
2021-08-07 08:00:41 +00:00
|
|
|
|
|
|
|
gd->arch.s400_dev = dev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8ulp_mu_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx8ulp_mu_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
debug("%s(dev=%p)\n", __func__, dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct misc_ops imx8ulp_mu_ops = {
|
|
|
|
.call = imx8ulp_mu_call,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id imx8ulp_mu_ids[] = {
|
|
|
|
{ .compatible = "fsl,imx8ulp-mu" },
|
2022-07-26 08:40:50 +00:00
|
|
|
{ .compatible = "fsl,imx93-mu-s4" },
|
2021-08-07 08:00:41 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(imx8ulp_mu) = {
|
|
|
|
.name = "imx8ulp_mu",
|
|
|
|
.id = UCLASS_MISC,
|
|
|
|
.of_match = imx8ulp_mu_ids,
|
|
|
|
.probe = imx8ulp_mu_probe,
|
|
|
|
.bind = imx8ulp_mu_bind,
|
|
|
|
.remove = imx8ulp_mu_remove,
|
|
|
|
.ops = &imx8ulp_mu_ops,
|
|
|
|
.priv_auto = sizeof(struct imx8ulp_mu),
|
|
|
|
};
|