Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
   4 */
   5
   6#include <dt-bindings/firmware/imx/rsrc.h>
   7#include <linux/arm-smccc.h>
   8#include <linux/clk.h>
   9#include <linux/err.h>
  10#include <linux/firmware/imx/sci.h>
  11#include <linux/interrupt.h>
  12#include <linux/kernel.h>
  13#include <linux/mailbox_client.h>
  14#include <linux/mfd/syscon.h>
  15#include <linux/module.h>
  16#include <linux/of.h>
  17#include <linux/of_address.h>
  18#include <linux/of_reserved_mem.h>
 
  19#include <linux/platform_device.h>
  20#include <linux/pm_domain.h>
  21#include <linux/reboot.h>
  22#include <linux/regmap.h>
  23#include <linux/remoteproc.h>
  24#include <linux/workqueue.h>
  25
  26#include "imx_rproc.h"
  27#include "remoteproc_internal.h"
  28
  29#define IMX7D_SRC_SCR			0x0C
  30#define IMX7D_ENABLE_M4			BIT(3)
  31#define IMX7D_SW_M4P_RST		BIT(2)
  32#define IMX7D_SW_M4C_RST		BIT(1)
  33#define IMX7D_SW_M4C_NON_SCLR_RST	BIT(0)
  34
  35#define IMX7D_M4_RST_MASK		(IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
  36					 | IMX7D_SW_M4C_RST \
  37					 | IMX7D_SW_M4C_NON_SCLR_RST)
  38
  39#define IMX7D_M4_START			(IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
  40					 | IMX7D_SW_M4C_RST)
  41#define IMX7D_M4_STOP			(IMX7D_ENABLE_M4 | IMX7D_SW_M4C_RST | \
  42					 IMX7D_SW_M4C_NON_SCLR_RST)
  43
  44#define IMX8M_M7_STOP			(IMX7D_ENABLE_M4 | IMX7D_SW_M4C_RST)
  45#define IMX8M_M7_POLL			IMX7D_ENABLE_M4
  46
  47#define IMX8M_GPR22			0x58
  48#define IMX8M_GPR22_CM7_CPUWAIT		BIT(0)
  49
  50/* Address: 0x020D8000 */
  51#define IMX6SX_SRC_SCR			0x00
  52#define IMX6SX_ENABLE_M4		BIT(22)
  53#define IMX6SX_SW_M4P_RST		BIT(12)
  54#define IMX6SX_SW_M4C_NON_SCLR_RST	BIT(4)
  55#define IMX6SX_SW_M4C_RST		BIT(3)
  56
  57#define IMX6SX_M4_START			(IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
  58					 | IMX6SX_SW_M4C_RST)
  59#define IMX6SX_M4_STOP			(IMX6SX_ENABLE_M4 | IMX6SX_SW_M4C_RST | \
  60					 IMX6SX_SW_M4C_NON_SCLR_RST)
  61#define IMX6SX_M4_RST_MASK		(IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
  62					 | IMX6SX_SW_M4C_NON_SCLR_RST \
  63					 | IMX6SX_SW_M4C_RST)
  64
  65#define IMX_RPROC_MEM_MAX		32
  66
  67#define IMX_SIP_RPROC			0xC2000005
  68#define IMX_SIP_RPROC_START		0x00
  69#define IMX_SIP_RPROC_STARTED		0x01
  70#define IMX_SIP_RPROC_STOP		0x02
  71
  72#define IMX_SC_IRQ_GROUP_REBOOTED	5
  73
  74/**
  75 * struct imx_rproc_mem - slim internal memory structure
  76 * @cpu_addr: MPU virtual address of the memory region
  77 * @sys_addr: Bus address used to access the memory region
  78 * @size: Size of the memory region
  79 */
  80struct imx_rproc_mem {
  81	void __iomem *cpu_addr;
  82	phys_addr_t sys_addr;
  83	size_t size;
  84};
  85
  86/* att flags: lower 16 bits specifying core, higher 16 bits for flags  */
  87/* M4 own area. Can be mapped at probe */
  88#define ATT_OWN         BIT(31)
  89#define ATT_IOMEM       BIT(30)
  90
  91#define ATT_CORE_MASK   0xffff
  92#define ATT_CORE(I)     BIT((I))
  93
  94static int imx_rproc_xtr_mbox_init(struct rproc *rproc, bool tx_block);
  95static void imx_rproc_free_mbox(struct rproc *rproc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  96
  97struct imx_rproc {
  98	struct device			*dev;
  99	struct regmap			*regmap;
 100	struct regmap			*gpr;
 101	struct rproc			*rproc;
 102	const struct imx_rproc_dcfg	*dcfg;
 103	struct imx_rproc_mem		mem[IMX_RPROC_MEM_MAX];
 104	struct clk			*clk;
 105	struct mbox_client		cl;
 106	struct mbox_chan		*tx_ch;
 107	struct mbox_chan		*rx_ch;
 108	struct work_struct		rproc_work;
 109	struct workqueue_struct		*workqueue;
 110	void __iomem			*rsc_table;
 111	struct imx_sc_ipc		*ipc_handle;
 112	struct notifier_block		rproc_nb;
 113	u32				rproc_pt;	/* partition id */
 114	u32				rsrc_id;	/* resource id */
 115	u32				entry;		/* cpu start address */
 116	u32				core_index;
 117	struct dev_pm_domain_list	*pd_list;
 118};
 119
 120static const struct imx_rproc_att imx_rproc_att_imx93[] = {
 121	/* dev addr , sys addr  , size	    , flags */
 122	/* TCM CODE NON-SECURE */
 123	{ 0x0FFC0000, 0x201C0000, 0x00040000, ATT_OWN | ATT_IOMEM },
 124
 125	/* TCM CODE SECURE */
 126	{ 0x1FFC0000, 0x201C0000, 0x00040000, ATT_OWN | ATT_IOMEM },
 127
 128	/* TCM SYS NON-SECURE*/
 129	{ 0x20000000, 0x20200000, 0x00040000, ATT_OWN | ATT_IOMEM },
 130
 131	/* TCM SYS SECURE*/
 132	{ 0x30000000, 0x20200000, 0x00040000, ATT_OWN | ATT_IOMEM },
 133
 134	/* DDR */
 135	{ 0x80000000, 0x80000000, 0x10000000, 0 },
 136	{ 0x90000000, 0x80000000, 0x10000000, 0 },
 137
 138	{ 0xC0000000, 0xC0000000, 0x10000000, 0 },
 139	{ 0xD0000000, 0xC0000000, 0x10000000, 0 },
 140};
 141
 142static const struct imx_rproc_att imx_rproc_att_imx8qm[] = {
 143	/* dev addr , sys addr  , size      , flags */
 144	{ 0x08000000, 0x08000000, 0x10000000, 0},
 145	/* TCML */
 146	{ 0x1FFE0000, 0x34FE0000, 0x00020000, ATT_OWN | ATT_IOMEM | ATT_CORE(0)},
 147	{ 0x1FFE0000, 0x38FE0000, 0x00020000, ATT_OWN | ATT_IOMEM | ATT_CORE(1)},
 148	/* TCMU */
 149	{ 0x20000000, 0x35000000, 0x00020000, ATT_OWN | ATT_IOMEM | ATT_CORE(0)},
 150	{ 0x20000000, 0x39000000, 0x00020000, ATT_OWN | ATT_IOMEM | ATT_CORE(1)},
 151	/* DDR (Data) */
 152	{ 0x80000000, 0x80000000, 0x60000000, 0 },
 153};
 154
 155static const struct imx_rproc_att imx_rproc_att_imx8qxp[] = {
 156	{ 0x08000000, 0x08000000, 0x10000000, 0 },
 157	/* TCML/U */
 158	{ 0x1FFE0000, 0x34FE0000, 0x00040000, ATT_OWN | ATT_IOMEM },
 159	/* OCRAM(Low 96KB) */
 160	{ 0x21000000, 0x00100000, 0x00018000, 0 },
 161	/* OCRAM */
 162	{ 0x21100000, 0x00100000, 0x00040000, 0 },
 163	/* DDR (Data) */
 164	{ 0x80000000, 0x80000000, 0x60000000, 0 },
 165};
 166
 167static const struct imx_rproc_att imx_rproc_att_imx8mn[] = {
 168	/* dev addr , sys addr  , size	    , flags */
 169	/* ITCM   */
 170	{ 0x00000000, 0x007E0000, 0x00020000, ATT_OWN | ATT_IOMEM },
 171	/* OCRAM_S */
 172	{ 0x00180000, 0x00180000, 0x00009000, 0 },
 173	/* OCRAM */
 174	{ 0x00900000, 0x00900000, 0x00020000, 0 },
 175	/* OCRAM */
 176	{ 0x00920000, 0x00920000, 0x00020000, 0 },
 177	/* OCRAM */
 178	{ 0x00940000, 0x00940000, 0x00050000, 0 },
 179	/* QSPI Code - alias */
 180	{ 0x08000000, 0x08000000, 0x08000000, 0 },
 181	/* DDR (Code) - alias */
 182	{ 0x10000000, 0x40000000, 0x0FFE0000, 0 },
 183	/* DTCM */
 184	{ 0x20000000, 0x00800000, 0x00020000, ATT_OWN | ATT_IOMEM },
 185	/* OCRAM_S - alias */
 186	{ 0x20180000, 0x00180000, 0x00008000, ATT_OWN },
 187	/* OCRAM */
 188	{ 0x20200000, 0x00900000, 0x00020000, ATT_OWN },
 189	/* OCRAM */
 190	{ 0x20220000, 0x00920000, 0x00020000, ATT_OWN },
 191	/* OCRAM */
 192	{ 0x20240000, 0x00940000, 0x00040000, ATT_OWN },
 193	/* DDR (Data) */
 194	{ 0x40000000, 0x40000000, 0x80000000, 0 },
 195};
 196
 197static const struct imx_rproc_att imx_rproc_att_imx8mq[] = {
 198	/* dev addr , sys addr  , size	    , flags */
 199	/* TCML - alias */
 200	{ 0x00000000, 0x007e0000, 0x00020000, ATT_IOMEM},
 201	/* OCRAM_S */
 202	{ 0x00180000, 0x00180000, 0x00008000, 0 },
 203	/* OCRAM */
 204	{ 0x00900000, 0x00900000, 0x00020000, 0 },
 205	/* OCRAM */
 206	{ 0x00920000, 0x00920000, 0x00020000, 0 },
 207	/* QSPI Code - alias */
 208	{ 0x08000000, 0x08000000, 0x08000000, 0 },
 209	/* DDR (Code) - alias */
 210	{ 0x10000000, 0x40000000, 0x0FFE0000, 0 },
 211	/* TCML/U */
 212	{ 0x1FFE0000, 0x007E0000, 0x00040000, ATT_OWN  | ATT_IOMEM},
 
 
 213	/* OCRAM_S */
 214	{ 0x20180000, 0x00180000, 0x00008000, ATT_OWN },
 215	/* OCRAM */
 216	{ 0x20200000, 0x00900000, 0x00020000, ATT_OWN },
 217	/* OCRAM */
 218	{ 0x20220000, 0x00920000, 0x00020000, ATT_OWN },
 219	/* DDR (Data) */
 220	{ 0x40000000, 0x40000000, 0x80000000, 0 },
 221};
 222
 223static const struct imx_rproc_att imx_rproc_att_imx8ulp[] = {
 224	{0x1FFC0000, 0x1FFC0000, 0xC0000, ATT_OWN},
 225	{0x21000000, 0x21000000, 0x10000, ATT_OWN},
 226	{0x80000000, 0x80000000, 0x60000000, 0}
 227};
 228
 229static const struct imx_rproc_att imx_rproc_att_imx7ulp[] = {
 230	{0x1FFD0000, 0x1FFD0000, 0x30000, ATT_OWN},
 231	{0x20000000, 0x20000000, 0x10000, ATT_OWN},
 232	{0x2F000000, 0x2F000000, 0x20000, ATT_OWN},
 233	{0x2F020000, 0x2F020000, 0x20000, ATT_OWN},
 234	{0x60000000, 0x60000000, 0x40000000, 0}
 235};
 236
 237static const struct imx_rproc_att imx_rproc_att_imx7d[] = {
 238	/* dev addr , sys addr  , size	    , flags */
 239	/* OCRAM_S (M4 Boot code) - alias */
 240	{ 0x00000000, 0x00180000, 0x00008000, 0 },
 241	/* OCRAM_S (Code) */
 242	{ 0x00180000, 0x00180000, 0x00008000, ATT_OWN },
 243	/* OCRAM (Code) - alias */
 244	{ 0x00900000, 0x00900000, 0x00020000, 0 },
 245	/* OCRAM_EPDC (Code) - alias */
 246	{ 0x00920000, 0x00920000, 0x00020000, 0 },
 247	/* OCRAM_PXP (Code) - alias */
 248	{ 0x00940000, 0x00940000, 0x00008000, 0 },
 249	/* TCML (Code) */
 250	{ 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN | ATT_IOMEM },
 251	/* DDR (Code) - alias, first part of DDR (Data) */
 252	{ 0x10000000, 0x80000000, 0x0FFF0000, 0 },
 253
 254	/* TCMU (Data) */
 255	{ 0x20000000, 0x00800000, 0x00008000, ATT_OWN | ATT_IOMEM },
 256	/* OCRAM (Data) */
 257	{ 0x20200000, 0x00900000, 0x00020000, 0 },
 258	/* OCRAM_EPDC (Data) */
 259	{ 0x20220000, 0x00920000, 0x00020000, 0 },
 260	/* OCRAM_PXP (Data) */
 261	{ 0x20240000, 0x00940000, 0x00008000, 0 },
 262	/* DDR (Data) */
 263	{ 0x80000000, 0x80000000, 0x60000000, 0 },
 264};
 265
 266static const struct imx_rproc_att imx_rproc_att_imx6sx[] = {
 267	/* dev addr , sys addr  , size	    , flags */
 268	/* TCML (M4 Boot Code) - alias */
 269	{ 0x00000000, 0x007F8000, 0x00008000, ATT_IOMEM },
 270	/* OCRAM_S (Code) */
 271	{ 0x00180000, 0x008F8000, 0x00004000, 0 },
 272	/* OCRAM_S (Code) - alias */
 273	{ 0x00180000, 0x008FC000, 0x00004000, 0 },
 274	/* TCML (Code) */
 275	{ 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN | ATT_IOMEM },
 276	/* DDR (Code) - alias, first part of DDR (Data) */
 277	{ 0x10000000, 0x80000000, 0x0FFF8000, 0 },
 278
 279	/* TCMU (Data) */
 280	{ 0x20000000, 0x00800000, 0x00008000, ATT_OWN | ATT_IOMEM },
 281	/* OCRAM_S (Data) - alias? */
 282	{ 0x208F8000, 0x008F8000, 0x00004000, 0 },
 283	/* DDR (Data) */
 284	{ 0x80000000, 0x80000000, 0x60000000, 0 },
 285};
 286
 287static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mn_mmio = {
 288	.src_reg	= IMX7D_SRC_SCR,
 289	.src_mask	= IMX7D_M4_RST_MASK,
 290	.src_start	= IMX7D_M4_START,
 291	.src_stop	= IMX8M_M7_STOP,
 292	.gpr_reg	= IMX8M_GPR22,
 293	.gpr_wait	= IMX8M_GPR22_CM7_CPUWAIT,
 294	.att		= imx_rproc_att_imx8mn,
 295	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8mn),
 296	.method		= IMX_RPROC_MMIO,
 297};
 298
 299static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mn = {
 300	.att		= imx_rproc_att_imx8mn,
 301	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8mn),
 302	.method		= IMX_RPROC_SMC,
 303};
 304
 305static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mq = {
 306	.src_reg	= IMX7D_SRC_SCR,
 307	.src_mask	= IMX7D_M4_RST_MASK,
 308	.src_start	= IMX7D_M4_START,
 309	.src_stop	= IMX7D_M4_STOP,
 310	.att		= imx_rproc_att_imx8mq,
 311	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8mq),
 312	.method		= IMX_RPROC_MMIO,
 313};
 314
 315static const struct imx_rproc_dcfg imx_rproc_cfg_imx8qm = {
 316	.att            = imx_rproc_att_imx8qm,
 317	.att_size       = ARRAY_SIZE(imx_rproc_att_imx8qm),
 318	.method         = IMX_RPROC_SCU_API,
 319};
 320
 321static const struct imx_rproc_dcfg imx_rproc_cfg_imx8qxp = {
 322	.att		= imx_rproc_att_imx8qxp,
 323	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8qxp),
 324	.method		= IMX_RPROC_SCU_API,
 325};
 326
 327static const struct imx_rproc_dcfg imx_rproc_cfg_imx8ulp = {
 328	.att		= imx_rproc_att_imx8ulp,
 329	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8ulp),
 330	.method		= IMX_RPROC_NONE,
 331};
 332
 333static const struct imx_rproc_dcfg imx_rproc_cfg_imx7ulp = {
 334	.att		= imx_rproc_att_imx7ulp,
 335	.att_size	= ARRAY_SIZE(imx_rproc_att_imx7ulp),
 336	.method		= IMX_RPROC_NONE,
 337	.flags		= IMX_RPROC_NEED_SYSTEM_OFF,
 338};
 339
 340static const struct imx_rproc_dcfg imx_rproc_cfg_imx7d = {
 341	.src_reg	= IMX7D_SRC_SCR,
 342	.src_mask	= IMX7D_M4_RST_MASK,
 343	.src_start	= IMX7D_M4_START,
 344	.src_stop	= IMX7D_M4_STOP,
 345	.att		= imx_rproc_att_imx7d,
 346	.att_size	= ARRAY_SIZE(imx_rproc_att_imx7d),
 347	.method		= IMX_RPROC_MMIO,
 348};
 349
 350static const struct imx_rproc_dcfg imx_rproc_cfg_imx6sx = {
 351	.src_reg	= IMX6SX_SRC_SCR,
 352	.src_mask	= IMX6SX_M4_RST_MASK,
 353	.src_start	= IMX6SX_M4_START,
 354	.src_stop	= IMX6SX_M4_STOP,
 355	.att		= imx_rproc_att_imx6sx,
 356	.att_size	= ARRAY_SIZE(imx_rproc_att_imx6sx),
 357	.method		= IMX_RPROC_MMIO,
 358};
 359
 360static const struct imx_rproc_dcfg imx_rproc_cfg_imx93 = {
 361	.att		= imx_rproc_att_imx93,
 362	.att_size	= ARRAY_SIZE(imx_rproc_att_imx93),
 363	.method		= IMX_RPROC_SMC,
 364};
 365
 366static int imx_rproc_start(struct rproc *rproc)
 367{
 368	struct imx_rproc *priv = rproc->priv;
 369	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 370	struct device *dev = priv->dev;
 371	struct arm_smccc_res res;
 372	int ret;
 373
 374	ret = imx_rproc_xtr_mbox_init(rproc, true);
 375	if (ret)
 376		return ret;
 377
 378	switch (dcfg->method) {
 379	case IMX_RPROC_MMIO:
 380		if (priv->gpr) {
 381			ret = regmap_clear_bits(priv->gpr, dcfg->gpr_reg,
 382						dcfg->gpr_wait);
 383		} else {
 384			ret = regmap_update_bits(priv->regmap, dcfg->src_reg,
 385						 dcfg->src_mask,
 386						 dcfg->src_start);
 387		}
 388		break;
 389	case IMX_RPROC_SMC:
 390		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_START, 0, 0, 0, 0, 0, 0, &res);
 391		ret = res.a0;
 392		break;
 393	case IMX_RPROC_SCU_API:
 394		ret = imx_sc_pm_cpu_start(priv->ipc_handle, priv->rsrc_id, true, priv->entry);
 395		break;
 396	default:
 397		return -EOPNOTSUPP;
 398	}
 399
 400	if (ret)
 401		dev_err(dev, "Failed to enable remote core!\n");
 402
 403	return ret;
 404}
 405
 406static int imx_rproc_stop(struct rproc *rproc)
 407{
 408	struct imx_rproc *priv = rproc->priv;
 409	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 410	struct device *dev = priv->dev;
 411	struct arm_smccc_res res;
 412	int ret;
 413
 414	switch (dcfg->method) {
 415	case IMX_RPROC_MMIO:
 416		if (priv->gpr) {
 417			ret = regmap_set_bits(priv->gpr, dcfg->gpr_reg,
 418					      dcfg->gpr_wait);
 419			if (ret) {
 420				dev_err(priv->dev,
 421					"Failed to quiescence M4 platform!\n");
 422				return ret;
 423			}
 424		}
 425
 426		ret = regmap_update_bits(priv->regmap, dcfg->src_reg, dcfg->src_mask,
 427					 dcfg->src_stop);
 428		break;
 429	case IMX_RPROC_SMC:
 430		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_STOP, 0, 0, 0, 0, 0, 0, &res);
 431		ret = res.a0;
 432		if (res.a1)
 433			dev_info(dev, "Not in wfi, force stopped\n");
 434		break;
 435	case IMX_RPROC_SCU_API:
 436		ret = imx_sc_pm_cpu_start(priv->ipc_handle, priv->rsrc_id, false, priv->entry);
 437		break;
 438	default:
 439		return -EOPNOTSUPP;
 440	}
 441
 442	if (ret)
 443		dev_err(dev, "Failed to stop remote core\n");
 444	else
 445		imx_rproc_free_mbox(rproc);
 446
 447	return ret;
 448}
 449
 450static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
 451			       size_t len, u64 *sys, bool *is_iomem)
 452{
 453	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 454	int i;
 455
 456	/* parse address translation table */
 457	for (i = 0; i < dcfg->att_size; i++) {
 458		const struct imx_rproc_att *att = &dcfg->att[i];
 459
 460		/*
 461		 * Ignore entries not belong to current core:
 462		 * i.MX8QM has dual general M4_[0,1] cores, M4_0's own entries
 463		 * has "ATT_CORE(0) & BIT(0)" true, M4_1's own entries has
 464		 * "ATT_CORE(1) & BIT(1)" true.
 465		 */
 466		if (att->flags & ATT_CORE_MASK) {
 467			if (!((BIT(priv->core_index)) & (att->flags & ATT_CORE_MASK)))
 468				continue;
 469		}
 470
 471		if (da >= att->da && da + len < att->da + att->size) {
 472			unsigned int offset = da - att->da;
 473
 474			*sys = att->sa + offset;
 475			if (is_iomem)
 476				*is_iomem = att->flags & ATT_IOMEM;
 477			return 0;
 478		}
 479	}
 480
 481	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%zx\n",
 482		 da, len);
 483	return -ENOENT;
 484}
 485
 486static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
 487{
 488	struct imx_rproc *priv = rproc->priv;
 489	void *va = NULL;
 490	u64 sys;
 491	int i;
 492
 493	if (len == 0)
 494		return NULL;
 495
 496	/*
 497	 * On device side we have many aliases, so we need to convert device
 498	 * address (M4) to system bus address first.
 499	 */
 500	if (imx_rproc_da_to_sys(priv, da, len, &sys, is_iomem))
 501		return NULL;
 502
 503	for (i = 0; i < IMX_RPROC_MEM_MAX; i++) {
 504		if (sys >= priv->mem[i].sys_addr && sys + len <
 505		    priv->mem[i].sys_addr +  priv->mem[i].size) {
 506			unsigned int offset = sys - priv->mem[i].sys_addr;
 507			/* __force to make sparse happy with type conversion */
 508			va = (__force void *)(priv->mem[i].cpu_addr + offset);
 509			break;
 510		}
 511	}
 512
 513	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%p\n",
 514		da, len, va);
 515
 516	return va;
 517}
 518
 519static int imx_rproc_mem_alloc(struct rproc *rproc,
 520			       struct rproc_mem_entry *mem)
 521{
 522	struct device *dev = rproc->dev.parent;
 523	void *va;
 524
 525	dev_dbg(dev, "map memory: %p+%zx\n", &mem->dma, mem->len);
 526	va = ioremap_wc(mem->dma, mem->len);
 527	if (IS_ERR_OR_NULL(va)) {
 528		dev_err(dev, "Unable to map memory region: %p+%zx\n",
 529			&mem->dma, mem->len);
 530		return -ENOMEM;
 531	}
 532
 533	/* Update memory entry va */
 534	mem->va = va;
 535
 536	return 0;
 537}
 538
 539static int imx_rproc_mem_release(struct rproc *rproc,
 540				 struct rproc_mem_entry *mem)
 541{
 542	dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma);
 543	iounmap(mem->va);
 544
 545	return 0;
 546}
 547
 548static int imx_rproc_prepare(struct rproc *rproc)
 549{
 550	struct imx_rproc *priv = rproc->priv;
 551	struct device_node *np = priv->dev->of_node;
 552	struct of_phandle_iterator it;
 553	struct rproc_mem_entry *mem;
 554	struct reserved_mem *rmem;
 555	u32 da;
 556
 557	/* Register associated reserved memory regions */
 558	of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
 559	while (of_phandle_iterator_next(&it) == 0) {
 560		/*
 561		 * Ignore the first memory region which will be used vdev buffer.
 562		 * No need to do extra handlings, rproc_add_virtio_dev will handle it.
 563		 */
 564		if (!strcmp(it.node->name, "vdev0buffer"))
 565			continue;
 566
 567		if (!strcmp(it.node->name, "rsc-table"))
 568			continue;
 569
 570		rmem = of_reserved_mem_lookup(it.node);
 571		if (!rmem) {
 572			of_node_put(it.node);
 573			dev_err(priv->dev, "unable to acquire memory-region\n");
 574			return -EINVAL;
 575		}
 576
 577		/* No need to translate pa to da, i.MX use same map */
 578		da = rmem->base;
 579
 580		/* Register memory region */
 581		mem = rproc_mem_entry_init(priv->dev, NULL, (dma_addr_t)rmem->base, rmem->size, da,
 582					   imx_rproc_mem_alloc, imx_rproc_mem_release,
 583					   it.node->name);
 584
 585		if (mem) {
 586			rproc_coredump_add_segment(rproc, da, rmem->size);
 587		} else {
 588			of_node_put(it.node);
 589			return -ENOMEM;
 590		}
 591
 592		rproc_add_carveout(rproc, mem);
 593	}
 594
 595	return  0;
 596}
 597
 598static int imx_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
 599{
 600	int ret;
 601
 602	ret = rproc_elf_load_rsc_table(rproc, fw);
 603	if (ret)
 604		dev_info(&rproc->dev, "No resource table in elf\n");
 605
 606	return 0;
 607}
 608
 609static void imx_rproc_kick(struct rproc *rproc, int vqid)
 610{
 611	struct imx_rproc *priv = rproc->priv;
 612	int err;
 613	__u32 mmsg;
 614
 615	if (!priv->tx_ch) {
 616		dev_err(priv->dev, "No initialized mbox tx channel\n");
 617		return;
 618	}
 619
 620	/*
 621	 * Send the index of the triggered virtqueue as the mu payload.
 622	 * Let remote processor know which virtqueue is used.
 623	 */
 624	mmsg = vqid << 16;
 625
 626	err = mbox_send_message(priv->tx_ch, (void *)&mmsg);
 627	if (err < 0)
 628		dev_err(priv->dev, "%s: failed (%d, err:%d)\n",
 629			__func__, vqid, err);
 630}
 631
 632static int imx_rproc_attach(struct rproc *rproc)
 633{
 634	return imx_rproc_xtr_mbox_init(rproc, true);
 635}
 636
 637static int imx_rproc_detach(struct rproc *rproc)
 638{
 639	struct imx_rproc *priv = rproc->priv;
 640	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 641
 642	if (dcfg->method != IMX_RPROC_SCU_API)
 643		return -EOPNOTSUPP;
 644
 645	if (imx_sc_rm_is_resource_owned(priv->ipc_handle, priv->rsrc_id))
 646		return -EOPNOTSUPP;
 647
 648	imx_rproc_free_mbox(rproc);
 649
 650	return 0;
 651}
 652
 653static struct resource_table *imx_rproc_get_loaded_rsc_table(struct rproc *rproc, size_t *table_sz)
 654{
 655	struct imx_rproc *priv = rproc->priv;
 656
 657	/* The resource table has already been mapped in imx_rproc_addr_init */
 658	if (!priv->rsc_table)
 659		return NULL;
 660
 661	*table_sz = SZ_1K;
 662	return (struct resource_table *)priv->rsc_table;
 663}
 664
 665static struct resource_table *
 666imx_rproc_elf_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
 667{
 668	struct imx_rproc *priv = rproc->priv;
 669
 670	if (priv->rsc_table)
 671		return (struct resource_table *)priv->rsc_table;
 672
 673	return rproc_elf_find_loaded_rsc_table(rproc, fw);
 674}
 675
 676static const struct rproc_ops imx_rproc_ops = {
 677	.prepare	= imx_rproc_prepare,
 678	.attach		= imx_rproc_attach,
 679	.detach		= imx_rproc_detach,
 680	.start		= imx_rproc_start,
 681	.stop		= imx_rproc_stop,
 682	.kick		= imx_rproc_kick,
 683	.da_to_va       = imx_rproc_da_to_va,
 684	.load		= rproc_elf_load_segments,
 685	.parse_fw	= imx_rproc_parse_fw,
 686	.find_loaded_rsc_table = imx_rproc_elf_find_loaded_rsc_table,
 687	.get_loaded_rsc_table = imx_rproc_get_loaded_rsc_table,
 688	.sanity_check	= rproc_elf_sanity_check,
 689	.get_boot_addr	= rproc_elf_get_boot_addr,
 690};
 691
 692static int imx_rproc_addr_init(struct imx_rproc *priv,
 693			       struct platform_device *pdev)
 694{
 695	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 696	struct device *dev = &pdev->dev;
 697	struct device_node *np = dev->of_node;
 698	int a, b = 0, err, nph;
 699
 700	/* remap required addresses */
 701	for (a = 0; a < dcfg->att_size; a++) {
 702		const struct imx_rproc_att *att = &dcfg->att[a];
 703
 704		if (!(att->flags & ATT_OWN))
 705			continue;
 706
 707		if (b >= IMX_RPROC_MEM_MAX)
 708			break;
 709
 710		if (att->flags & ATT_IOMEM)
 711			priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev,
 712							     att->sa, att->size);
 713		else
 714			priv->mem[b].cpu_addr = devm_ioremap_wc(&pdev->dev,
 715								att->sa, att->size);
 716		if (!priv->mem[b].cpu_addr) {
 717			dev_err(dev, "failed to remap %#x bytes from %#x\n", att->size, att->sa);
 718			return -ENOMEM;
 719		}
 720		priv->mem[b].sys_addr = att->sa;
 721		priv->mem[b].size = att->size;
 722		b++;
 723	}
 724
 725	/* memory-region is optional property */
 726	nph = of_count_phandle_with_args(np, "memory-region", NULL);
 727	if (nph <= 0)
 728		return 0;
 729
 730	/* remap optional addresses */
 731	for (a = 0; a < nph; a++) {
 732		struct device_node *node;
 733		struct resource res;
 734
 735		node = of_parse_phandle(np, "memory-region", a);
 736		if (!node)
 
 737			continue;
 738		/* Not map vdevbuffer, vdevring region */
 739		if (!strncmp(node->name, "vdev", strlen("vdev"))) {
 740			of_node_put(node);
 741			continue;
 742		}
 743		err = of_address_to_resource(node, 0, &res);
 744		if (err) {
 745			dev_err(dev, "unable to resolve memory region\n");
 746			of_node_put(node);
 747			return err;
 748		}
 749
 750		if (b >= IMX_RPROC_MEM_MAX) {
 751			of_node_put(node);
 
 752			break;
 753		}
 754
 755		/* Not use resource version, because we might share region */
 756		priv->mem[b].cpu_addr = devm_ioremap_wc(&pdev->dev, res.start, resource_size(&res));
 757		if (!priv->mem[b].cpu_addr) {
 758			dev_err(dev, "failed to remap %pr\n", &res);
 759			of_node_put(node);
 760			return -ENOMEM;
 761		}
 762		priv->mem[b].sys_addr = res.start;
 763		priv->mem[b].size = resource_size(&res);
 764		if (!strcmp(node->name, "rsc-table"))
 765			priv->rsc_table = priv->mem[b].cpu_addr;
 766		of_node_put(node);
 767		b++;
 768	}
 769
 770	return 0;
 771}
 772
 773static int imx_rproc_notified_idr_cb(int id, void *ptr, void *data)
 774{
 775	struct rproc *rproc = data;
 776
 777	rproc_vq_interrupt(rproc, id);
 778
 779	return 0;
 780}
 781
 782static void imx_rproc_vq_work(struct work_struct *work)
 783{
 784	struct imx_rproc *priv = container_of(work, struct imx_rproc,
 785					      rproc_work);
 786	struct rproc *rproc = priv->rproc;
 787
 788	idr_for_each(&rproc->notifyids, imx_rproc_notified_idr_cb, rproc);
 
 789}
 790
 791static void imx_rproc_rx_callback(struct mbox_client *cl, void *msg)
 792{
 793	struct rproc *rproc = dev_get_drvdata(cl->dev);
 794	struct imx_rproc *priv = rproc->priv;
 795
 796	queue_work(priv->workqueue, &priv->rproc_work);
 797}
 798
 799static int imx_rproc_xtr_mbox_init(struct rproc *rproc, bool tx_block)
 800{
 801	struct imx_rproc *priv = rproc->priv;
 802	struct device *dev = priv->dev;
 803	struct mbox_client *cl;
 
 804
 805	/*
 806	 * stop() and detach() will free the mbox channels, so need
 807	 * to request mbox channels in start() and attach().
 808	 *
 809	 * Because start() and attach() not able to handle mbox defer
 810	 * probe, imx_rproc_xtr_mbox_init is also called in probe().
 811	 * The check is to avoid request mbox again when start() or
 812	 * attach() after probe() returns success.
 813	 */
 814	if (priv->tx_ch && priv->rx_ch)
 815		return 0;
 816
 817	if (!of_property_present(dev->of_node, "mbox-names"))
 818		return 0;
 819
 820	cl = &priv->cl;
 821	cl->dev = dev;
 822	cl->tx_block = tx_block;
 823	cl->tx_tout = 100;
 824	cl->knows_txdone = false;
 825	cl->rx_callback = imx_rproc_rx_callback;
 826
 827	priv->tx_ch = mbox_request_channel_byname(cl, "tx");
 828	if (IS_ERR(priv->tx_ch))
 829		return dev_err_probe(cl->dev, PTR_ERR(priv->tx_ch),
 830				     "failed to request tx mailbox channel\n");
 
 
 831
 832	priv->rx_ch = mbox_request_channel_byname(cl, "rx");
 833	if (IS_ERR(priv->rx_ch)) {
 834		mbox_free_channel(priv->tx_ch);
 835		return dev_err_probe(cl->dev, PTR_ERR(priv->rx_ch),
 836				     "failed to request rx mailbox channel\n");
 
 837	}
 838
 839	return 0;
 840}
 841
 842static void imx_rproc_free_mbox(struct rproc *rproc)
 843{
 844	struct imx_rproc *priv = rproc->priv;
 845
 846	if (priv->tx_ch) {
 847		mbox_free_channel(priv->tx_ch);
 848		priv->tx_ch = NULL;
 849	}
 850
 851	if (priv->rx_ch) {
 852		mbox_free_channel(priv->rx_ch);
 853		priv->rx_ch = NULL;
 854	}
 855}
 856
 857static void imx_rproc_put_scu(struct rproc *rproc)
 858{
 859	struct imx_rproc *priv = rproc->priv;
 860	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 861
 862	if (dcfg->method != IMX_RPROC_SCU_API)
 863		return;
 864
 865	if (imx_sc_rm_is_resource_owned(priv->ipc_handle, priv->rsrc_id)) {
 866		dev_pm_domain_detach_list(priv->pd_list);
 867		return;
 868	}
 869
 870	imx_scu_irq_group_enable(IMX_SC_IRQ_GROUP_REBOOTED, BIT(priv->rproc_pt), false);
 871	imx_scu_irq_unregister_notifier(&priv->rproc_nb);
 872}
 873
 874static int imx_rproc_partition_notify(struct notifier_block *nb,
 875				      unsigned long event, void *group)
 876{
 877	struct imx_rproc *priv = container_of(nb, struct imx_rproc, rproc_nb);
 878
 879	/* Ignore other irqs */
 880	if (!((event & BIT(priv->rproc_pt)) && (*(u8 *)group == IMX_SC_IRQ_GROUP_REBOOTED)))
 881		return 0;
 882
 883	rproc_report_crash(priv->rproc, RPROC_WATCHDOG);
 884
 885	pr_info("Partition%d reset!\n", priv->rproc_pt);
 886
 887	return 0;
 888}
 889
 890static int imx_rproc_attach_pd(struct imx_rproc *priv)
 891{
 892	struct device *dev = priv->dev;
 893	int ret;
 894	struct dev_pm_domain_attach_data pd_data = {
 895		.pd_flags = PD_FLAG_DEV_LINK_ON,
 896	};
 897
 898	/*
 899	 * If there is only one power-domain entry, the platform driver framework
 900	 * will handle it, no need handle it in this driver.
 901	 */
 902	if (dev->pm_domain)
 903		return 0;
 904
 905	ret = dev_pm_domain_attach_list(dev, &pd_data, &priv->pd_list);
 906	return ret < 0 ? ret : 0;
 907}
 908
 909static int imx_rproc_detect_mode(struct imx_rproc *priv)
 910{
 911	struct regmap_config config = { .name = "imx-rproc" };
 912	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
 913	struct device *dev = priv->dev;
 914	struct regmap *regmap;
 915	struct arm_smccc_res res;
 916	int ret;
 917	u32 val;
 918	u8 pt;
 919
 920	switch (dcfg->method) {
 921	case IMX_RPROC_NONE:
 922		priv->rproc->state = RPROC_DETACHED;
 923		return 0;
 924	case IMX_RPROC_SMC:
 925		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_STARTED, 0, 0, 0, 0, 0, 0, &res);
 926		if (res.a0)
 927			priv->rproc->state = RPROC_DETACHED;
 928		return 0;
 929	case IMX_RPROC_SCU_API:
 930		ret = imx_scu_get_handle(&priv->ipc_handle);
 931		if (ret)
 932			return ret;
 933		ret = of_property_read_u32(dev->of_node, "fsl,resource-id", &priv->rsrc_id);
 934		if (ret) {
 935			dev_err(dev, "No fsl,resource-id property\n");
 936			return ret;
 937		}
 938
 939		if (priv->rsrc_id == IMX_SC_R_M4_1_PID0)
 940			priv->core_index = 1;
 941		else
 942			priv->core_index = 0;
 943
 944		/*
 945		 * If Mcore resource is not owned by Acore partition, It is kicked by ROM,
 946		 * and Linux could only do IPC with Mcore and nothing else.
 947		 */
 948		if (imx_sc_rm_is_resource_owned(priv->ipc_handle, priv->rsrc_id)) {
 949			if (of_property_read_u32(dev->of_node, "fsl,entry-address", &priv->entry))
 950				return -EINVAL;
 951
 952			return imx_rproc_attach_pd(priv);
 953		}
 954
 955		priv->rproc->state = RPROC_DETACHED;
 956		priv->rproc->recovery_disabled = false;
 957		rproc_set_feature(priv->rproc, RPROC_FEAT_ATTACH_ON_RECOVERY);
 958
 959		/* Get partition id and enable irq in SCFW */
 960		ret = imx_sc_rm_get_resource_owner(priv->ipc_handle, priv->rsrc_id, &pt);
 961		if (ret) {
 962			dev_err(dev, "not able to get resource owner\n");
 963			return ret;
 964		}
 965
 966		priv->rproc_pt = pt;
 967		priv->rproc_nb.notifier_call = imx_rproc_partition_notify;
 968
 969		ret = imx_scu_irq_register_notifier(&priv->rproc_nb);
 970		if (ret) {
 971			dev_err(dev, "register scu notifier failed, %d\n", ret);
 972			return ret;
 973		}
 974
 975		ret = imx_scu_irq_group_enable(IMX_SC_IRQ_GROUP_REBOOTED, BIT(priv->rproc_pt),
 976					       true);
 977		if (ret) {
 978			imx_scu_irq_unregister_notifier(&priv->rproc_nb);
 979			dev_err(dev, "Enable irq failed, %d\n", ret);
 980			return ret;
 981		}
 982
 983		return 0;
 984	default:
 985		break;
 986	}
 987
 988	priv->gpr = syscon_regmap_lookup_by_phandle(dev->of_node, "fsl,iomuxc-gpr");
 989	if (IS_ERR(priv->gpr))
 990		priv->gpr = NULL;
 991
 992	regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
 993	if (IS_ERR(regmap)) {
 994		dev_err(dev, "failed to find syscon\n");
 995		return PTR_ERR(regmap);
 996	}
 997
 998	priv->regmap = regmap;
 999	regmap_attach_dev(dev, regmap, &config);
1000
1001	if (priv->gpr) {
1002		ret = regmap_read(priv->gpr, dcfg->gpr_reg, &val);
1003		if (val & dcfg->gpr_wait) {
1004			/*
1005			 * After cold boot, the CM indicates its in wait
1006			 * state, but not fully powered off. Power it off
1007			 * fully so firmware can be loaded into it.
1008			 */
1009			imx_rproc_stop(priv->rproc);
1010			return 0;
1011		}
1012	}
1013
1014	ret = regmap_read(regmap, dcfg->src_reg, &val);
1015	if (ret) {
1016		dev_err(dev, "Failed to read src\n");
1017		return ret;
1018	}
1019
1020	if ((val & dcfg->src_mask) != dcfg->src_stop)
1021		priv->rproc->state = RPROC_DETACHED;
1022
1023	return 0;
1024}
1025
1026static int imx_rproc_clk_enable(struct imx_rproc *priv)
1027{
1028	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
1029	struct device *dev = priv->dev;
1030	int ret;
1031
1032	/* Remote core is not under control of Linux */
1033	if (dcfg->method == IMX_RPROC_NONE)
1034		return 0;
1035
1036	priv->clk = devm_clk_get(dev, NULL);
1037	if (IS_ERR(priv->clk)) {
1038		dev_err(dev, "Failed to get clock\n");
1039		return PTR_ERR(priv->clk);
1040	}
1041
1042	/*
1043	 * clk for M4 block including memory. Should be
1044	 * enabled before .start for FW transfer.
1045	 */
1046	ret = clk_prepare_enable(priv->clk);
1047	if (ret) {
1048		dev_err(dev, "Failed to enable clock\n");
1049		return ret;
1050	}
1051
1052	return 0;
1053}
1054
1055static int imx_rproc_sys_off_handler(struct sys_off_data *data)
1056{
1057	struct rproc *rproc = data->cb_data;
1058	int ret;
1059
1060	imx_rproc_free_mbox(rproc);
1061
1062	ret = imx_rproc_xtr_mbox_init(rproc, false);
1063	if (ret) {
1064		dev_err(&rproc->dev, "Failed to request non-blocking mbox\n");
1065		return NOTIFY_BAD;
1066	}
1067
1068	return NOTIFY_DONE;
1069}
1070
1071static int imx_rproc_probe(struct platform_device *pdev)
1072{
1073	struct device *dev = &pdev->dev;
1074	struct device_node *np = dev->of_node;
1075	struct imx_rproc *priv;
1076	struct rproc *rproc;
1077	const struct imx_rproc_dcfg *dcfg;
1078	int ret;
1079
1080	/* set some other name then imx */
1081	rproc = devm_rproc_alloc(dev, "imx-rproc", &imx_rproc_ops,
1082				 NULL, sizeof(*priv));
1083	if (!rproc)
1084		return -ENOMEM;
1085
1086	dcfg = of_device_get_match_data(dev);
1087	if (!dcfg)
1088		return -EINVAL;
 
 
1089
1090	priv = rproc->priv;
1091	priv->rproc = rproc;
1092	priv->dcfg = dcfg;
1093	priv->dev = dev;
1094
1095	dev_set_drvdata(dev, rproc);
1096	priv->workqueue = create_workqueue(dev_name(dev));
1097	if (!priv->workqueue) {
1098		dev_err(dev, "cannot create workqueue\n");
1099		return -ENOMEM;
 
1100	}
1101
1102	INIT_WORK(&priv->rproc_work, imx_rproc_vq_work);
1103
1104	ret = imx_rproc_xtr_mbox_init(rproc, true);
1105	if (ret)
1106		goto err_put_wkq;
1107
1108	ret = imx_rproc_addr_init(priv, pdev);
1109	if (ret) {
1110		dev_err(dev, "failed on imx_rproc_addr_init\n");
1111		goto err_put_mbox;
1112	}
1113
1114	ret = imx_rproc_detect_mode(priv);
1115	if (ret)
1116		goto err_put_mbox;
1117
1118	ret = imx_rproc_clk_enable(priv);
1119	if (ret)
1120		goto err_put_scu;
 
 
1121
1122	if (rproc->state != RPROC_DETACHED)
1123		rproc->auto_boot = of_property_read_bool(np, "fsl,auto-boot");
1124
1125	if (dcfg->flags & IMX_RPROC_NEED_SYSTEM_OFF) {
1126		/*
1127		 * setup mailbox to non-blocking mode in
1128		 * [SYS_OFF_MODE_POWER_OFF_PREPARE, SYS_OFF_MODE_RESTART_PREPARE]
1129		 * phase before invoking [SYS_OFF_MODE_POWER_OFF, SYS_OFF_MODE_RESTART]
1130		 * atomic chain, see kernel/reboot.c.
1131		 */
1132		ret = devm_register_sys_off_handler(dev, SYS_OFF_MODE_POWER_OFF_PREPARE,
1133						    SYS_OFF_PRIO_DEFAULT,
1134						    imx_rproc_sys_off_handler, rproc);
1135		if (ret) {
1136			dev_err(dev, "register power off handler failure\n");
1137			goto err_put_clk;
1138		}
1139
1140		ret = devm_register_sys_off_handler(dev, SYS_OFF_MODE_RESTART_PREPARE,
1141						    SYS_OFF_PRIO_DEFAULT,
1142						    imx_rproc_sys_off_handler, rproc);
1143		if (ret) {
1144			dev_err(dev, "register restart handler failure\n");
1145			goto err_put_clk;
1146		}
1147	}
1148
1149	ret = rproc_add(rproc);
1150	if (ret) {
1151		dev_err(dev, "rproc_add failed\n");
1152		goto err_put_clk;
1153	}
1154
1155	return 0;
1156
1157err_put_clk:
1158	clk_disable_unprepare(priv->clk);
1159err_put_scu:
1160	imx_rproc_put_scu(rproc);
1161err_put_mbox:
1162	imx_rproc_free_mbox(rproc);
1163err_put_wkq:
1164	destroy_workqueue(priv->workqueue);
 
 
1165
1166	return ret;
1167}
1168
1169static void imx_rproc_remove(struct platform_device *pdev)
1170{
1171	struct rproc *rproc = platform_get_drvdata(pdev);
1172	struct imx_rproc *priv = rproc->priv;
1173
1174	clk_disable_unprepare(priv->clk);
1175	rproc_del(rproc);
1176	imx_rproc_put_scu(rproc);
1177	imx_rproc_free_mbox(rproc);
1178	destroy_workqueue(priv->workqueue);
 
 
1179}
1180
1181static const struct of_device_id imx_rproc_of_match[] = {
1182	{ .compatible = "fsl,imx7ulp-cm4", .data = &imx_rproc_cfg_imx7ulp },
1183	{ .compatible = "fsl,imx7d-cm4", .data = &imx_rproc_cfg_imx7d },
1184	{ .compatible = "fsl,imx6sx-cm4", .data = &imx_rproc_cfg_imx6sx },
1185	{ .compatible = "fsl,imx8mq-cm4", .data = &imx_rproc_cfg_imx8mq },
1186	{ .compatible = "fsl,imx8mm-cm4", .data = &imx_rproc_cfg_imx8mq },
1187	{ .compatible = "fsl,imx8mn-cm7", .data = &imx_rproc_cfg_imx8mn },
1188	{ .compatible = "fsl,imx8mp-cm7", .data = &imx_rproc_cfg_imx8mn },
1189	{ .compatible = "fsl,imx8mn-cm7-mmio", .data = &imx_rproc_cfg_imx8mn_mmio },
1190	{ .compatible = "fsl,imx8mp-cm7-mmio", .data = &imx_rproc_cfg_imx8mn_mmio },
1191	{ .compatible = "fsl,imx8qxp-cm4", .data = &imx_rproc_cfg_imx8qxp },
1192	{ .compatible = "fsl,imx8qm-cm4", .data = &imx_rproc_cfg_imx8qm },
1193	{ .compatible = "fsl,imx8ulp-cm33", .data = &imx_rproc_cfg_imx8ulp },
1194	{ .compatible = "fsl,imx93-cm33", .data = &imx_rproc_cfg_imx93 },
1195	{},
1196};
1197MODULE_DEVICE_TABLE(of, imx_rproc_of_match);
1198
1199static struct platform_driver imx_rproc_driver = {
1200	.probe = imx_rproc_probe,
1201	.remove = imx_rproc_remove,
1202	.driver = {
1203		.name = "imx-rproc",
1204		.of_match_table = imx_rproc_of_match,
1205	},
1206};
1207
1208module_platform_driver(imx_rproc_driver);
1209
1210MODULE_LICENSE("GPL v2");
1211MODULE_DESCRIPTION("i.MX remote processor control driver");
1212MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  4 */
  5
 
  6#include <linux/arm-smccc.h>
  7#include <linux/clk.h>
  8#include <linux/err.h>
 
  9#include <linux/interrupt.h>
 10#include <linux/kernel.h>
 11#include <linux/mailbox_client.h>
 12#include <linux/mfd/syscon.h>
 13#include <linux/module.h>
 
 14#include <linux/of_address.h>
 15#include <linux/of_reserved_mem.h>
 16#include <linux/of_device.h>
 17#include <linux/platform_device.h>
 
 
 18#include <linux/regmap.h>
 19#include <linux/remoteproc.h>
 20#include <linux/workqueue.h>
 21
 
 22#include "remoteproc_internal.h"
 23
 24#define IMX7D_SRC_SCR			0x0C
 25#define IMX7D_ENABLE_M4			BIT(3)
 26#define IMX7D_SW_M4P_RST		BIT(2)
 27#define IMX7D_SW_M4C_RST		BIT(1)
 28#define IMX7D_SW_M4C_NON_SCLR_RST	BIT(0)
 29
 30#define IMX7D_M4_RST_MASK		(IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
 31					 | IMX7D_SW_M4C_RST \
 32					 | IMX7D_SW_M4C_NON_SCLR_RST)
 33
 34#define IMX7D_M4_START			(IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
 35					 | IMX7D_SW_M4C_RST)
 36#define IMX7D_M4_STOP			IMX7D_SW_M4C_NON_SCLR_RST
 
 
 
 
 
 
 
 37
 38/* Address: 0x020D8000 */
 39#define IMX6SX_SRC_SCR			0x00
 40#define IMX6SX_ENABLE_M4		BIT(22)
 41#define IMX6SX_SW_M4P_RST		BIT(12)
 42#define IMX6SX_SW_M4C_NON_SCLR_RST	BIT(4)
 43#define IMX6SX_SW_M4C_RST		BIT(3)
 44
 45#define IMX6SX_M4_START			(IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
 46					 | IMX6SX_SW_M4C_RST)
 47#define IMX6SX_M4_STOP			IMX6SX_SW_M4C_NON_SCLR_RST
 
 48#define IMX6SX_M4_RST_MASK		(IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
 49					 | IMX6SX_SW_M4C_NON_SCLR_RST \
 50					 | IMX6SX_SW_M4C_RST)
 51
 52#define IMX_RPROC_MEM_MAX		32
 53
 54#define IMX_SIP_RPROC			0xC2000005
 55#define IMX_SIP_RPROC_START		0x00
 56#define IMX_SIP_RPROC_STARTED		0x01
 57#define IMX_SIP_RPROC_STOP		0x02
 58
 
 
 59/**
 60 * struct imx_rproc_mem - slim internal memory structure
 61 * @cpu_addr: MPU virtual address of the memory region
 62 * @sys_addr: Bus address used to access the memory region
 63 * @size: Size of the memory region
 64 */
 65struct imx_rproc_mem {
 66	void __iomem *cpu_addr;
 67	phys_addr_t sys_addr;
 68	size_t size;
 69};
 70
 71/* att flags */
 72/* M4 own area. Can be mapped at probe */
 73#define ATT_OWN		BIT(1)
 
 74
 75/* address translation table */
 76struct imx_rproc_att {
 77	u32 da;	/* device address (From Cortex M4 view)*/
 78	u32 sa;	/* system bus address */
 79	u32 size; /* size of reg range */
 80	int flags;
 81};
 82
 83/* Remote core start/stop method */
 84enum imx_rproc_method {
 85	IMX_RPROC_NONE,
 86	/* Through syscon regmap */
 87	IMX_RPROC_MMIO,
 88	/* Through ARM SMCCC */
 89	IMX_RPROC_SMC,
 90};
 91
 92struct imx_rproc_dcfg {
 93	u32				src_reg;
 94	u32				src_mask;
 95	u32				src_start;
 96	u32				src_stop;
 97	const struct imx_rproc_att	*att;
 98	size_t				att_size;
 99	enum imx_rproc_method		method;
100};
101
102struct imx_rproc {
103	struct device			*dev;
104	struct regmap			*regmap;
 
105	struct rproc			*rproc;
106	const struct imx_rproc_dcfg	*dcfg;
107	struct imx_rproc_mem		mem[IMX_RPROC_MEM_MAX];
108	struct clk			*clk;
109	struct mbox_client		cl;
110	struct mbox_chan		*tx_ch;
111	struct mbox_chan		*rx_ch;
112	struct work_struct		rproc_work;
113	struct workqueue_struct		*workqueue;
114	void __iomem			*rsc_table;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115};
116
117static const struct imx_rproc_att imx_rproc_att_imx8mn[] = {
118	/* dev addr , sys addr  , size	    , flags */
119	/* ITCM   */
120	{ 0x00000000, 0x007E0000, 0x00020000, ATT_OWN },
121	/* OCRAM_S */
122	{ 0x00180000, 0x00180000, 0x00009000, 0 },
123	/* OCRAM */
124	{ 0x00900000, 0x00900000, 0x00020000, 0 },
125	/* OCRAM */
126	{ 0x00920000, 0x00920000, 0x00020000, 0 },
127	/* OCRAM */
128	{ 0x00940000, 0x00940000, 0x00050000, 0 },
129	/* QSPI Code - alias */
130	{ 0x08000000, 0x08000000, 0x08000000, 0 },
131	/* DDR (Code) - alias */
132	{ 0x10000000, 0x40000000, 0x0FFE0000, 0 },
133	/* DTCM */
134	{ 0x20000000, 0x00800000, 0x00020000, ATT_OWN },
135	/* OCRAM_S - alias */
136	{ 0x20180000, 0x00180000, 0x00008000, ATT_OWN },
137	/* OCRAM */
138	{ 0x20200000, 0x00900000, 0x00020000, ATT_OWN },
139	/* OCRAM */
140	{ 0x20220000, 0x00920000, 0x00020000, ATT_OWN },
141	/* OCRAM */
142	{ 0x20240000, 0x00940000, 0x00040000, ATT_OWN },
143	/* DDR (Data) */
144	{ 0x40000000, 0x40000000, 0x80000000, 0 },
145};
146
147static const struct imx_rproc_att imx_rproc_att_imx8mq[] = {
148	/* dev addr , sys addr  , size	    , flags */
149	/* TCML - alias */
150	{ 0x00000000, 0x007e0000, 0x00020000, 0 },
151	/* OCRAM_S */
152	{ 0x00180000, 0x00180000, 0x00008000, 0 },
153	/* OCRAM */
154	{ 0x00900000, 0x00900000, 0x00020000, 0 },
155	/* OCRAM */
156	{ 0x00920000, 0x00920000, 0x00020000, 0 },
157	/* QSPI Code - alias */
158	{ 0x08000000, 0x08000000, 0x08000000, 0 },
159	/* DDR (Code) - alias */
160	{ 0x10000000, 0x80000000, 0x0FFE0000, 0 },
161	/* TCML */
162	{ 0x1FFE0000, 0x007E0000, 0x00020000, ATT_OWN },
163	/* TCMU */
164	{ 0x20000000, 0x00800000, 0x00020000, ATT_OWN },
165	/* OCRAM_S */
166	{ 0x20180000, 0x00180000, 0x00008000, ATT_OWN },
167	/* OCRAM */
168	{ 0x20200000, 0x00900000, 0x00020000, ATT_OWN },
169	/* OCRAM */
170	{ 0x20220000, 0x00920000, 0x00020000, ATT_OWN },
171	/* DDR (Data) */
172	{ 0x40000000, 0x40000000, 0x80000000, 0 },
173};
174
175static const struct imx_rproc_att imx_rproc_att_imx8ulp[] = {
176	{0x1FFC0000, 0x1FFC0000, 0xC0000, ATT_OWN},
177	{0x21000000, 0x21000000, 0x10000, ATT_OWN},
178	{0x80000000, 0x80000000, 0x60000000, 0}
179};
180
181static const struct imx_rproc_att imx_rproc_att_imx7ulp[] = {
182	{0x1FFD0000, 0x1FFD0000, 0x30000, ATT_OWN},
183	{0x20000000, 0x20000000, 0x10000, ATT_OWN},
184	{0x2F000000, 0x2F000000, 0x20000, ATT_OWN},
185	{0x2F020000, 0x2F020000, 0x20000, ATT_OWN},
186	{0x60000000, 0x60000000, 0x40000000, 0}
187};
188
189static const struct imx_rproc_att imx_rproc_att_imx7d[] = {
190	/* dev addr , sys addr  , size	    , flags */
191	/* OCRAM_S (M4 Boot code) - alias */
192	{ 0x00000000, 0x00180000, 0x00008000, 0 },
193	/* OCRAM_S (Code) */
194	{ 0x00180000, 0x00180000, 0x00008000, ATT_OWN },
195	/* OCRAM (Code) - alias */
196	{ 0x00900000, 0x00900000, 0x00020000, 0 },
197	/* OCRAM_EPDC (Code) - alias */
198	{ 0x00920000, 0x00920000, 0x00020000, 0 },
199	/* OCRAM_PXP (Code) - alias */
200	{ 0x00940000, 0x00940000, 0x00008000, 0 },
201	/* TCML (Code) */
202	{ 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN },
203	/* DDR (Code) - alias, first part of DDR (Data) */
204	{ 0x10000000, 0x80000000, 0x0FFF0000, 0 },
205
206	/* TCMU (Data) */
207	{ 0x20000000, 0x00800000, 0x00008000, ATT_OWN },
208	/* OCRAM (Data) */
209	{ 0x20200000, 0x00900000, 0x00020000, 0 },
210	/* OCRAM_EPDC (Data) */
211	{ 0x20220000, 0x00920000, 0x00020000, 0 },
212	/* OCRAM_PXP (Data) */
213	{ 0x20240000, 0x00940000, 0x00008000, 0 },
214	/* DDR (Data) */
215	{ 0x80000000, 0x80000000, 0x60000000, 0 },
216};
217
218static const struct imx_rproc_att imx_rproc_att_imx6sx[] = {
219	/* dev addr , sys addr  , size	    , flags */
220	/* TCML (M4 Boot Code) - alias */
221	{ 0x00000000, 0x007F8000, 0x00008000, 0 },
222	/* OCRAM_S (Code) */
223	{ 0x00180000, 0x008F8000, 0x00004000, 0 },
224	/* OCRAM_S (Code) - alias */
225	{ 0x00180000, 0x008FC000, 0x00004000, 0 },
226	/* TCML (Code) */
227	{ 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN },
228	/* DDR (Code) - alias, first part of DDR (Data) */
229	{ 0x10000000, 0x80000000, 0x0FFF8000, 0 },
230
231	/* TCMU (Data) */
232	{ 0x20000000, 0x00800000, 0x00008000, ATT_OWN },
233	/* OCRAM_S (Data) - alias? */
234	{ 0x208F8000, 0x008F8000, 0x00004000, 0 },
235	/* DDR (Data) */
236	{ 0x80000000, 0x80000000, 0x60000000, 0 },
237};
238
 
 
 
 
 
 
 
 
 
 
 
 
239static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mn = {
240	.att		= imx_rproc_att_imx8mn,
241	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8mn),
242	.method		= IMX_RPROC_SMC,
243};
244
245static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mq = {
246	.src_reg	= IMX7D_SRC_SCR,
247	.src_mask	= IMX7D_M4_RST_MASK,
248	.src_start	= IMX7D_M4_START,
249	.src_stop	= IMX7D_M4_STOP,
250	.att		= imx_rproc_att_imx8mq,
251	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8mq),
252	.method		= IMX_RPROC_MMIO,
253};
254
 
 
 
 
 
 
 
 
 
 
 
 
255static const struct imx_rproc_dcfg imx_rproc_cfg_imx8ulp = {
256	.att		= imx_rproc_att_imx8ulp,
257	.att_size	= ARRAY_SIZE(imx_rproc_att_imx8ulp),
258	.method		= IMX_RPROC_NONE,
259};
260
261static const struct imx_rproc_dcfg imx_rproc_cfg_imx7ulp = {
262	.att		= imx_rproc_att_imx7ulp,
263	.att_size	= ARRAY_SIZE(imx_rproc_att_imx7ulp),
264	.method		= IMX_RPROC_NONE,
 
265};
266
267static const struct imx_rproc_dcfg imx_rproc_cfg_imx7d = {
268	.src_reg	= IMX7D_SRC_SCR,
269	.src_mask	= IMX7D_M4_RST_MASK,
270	.src_start	= IMX7D_M4_START,
271	.src_stop	= IMX7D_M4_STOP,
272	.att		= imx_rproc_att_imx7d,
273	.att_size	= ARRAY_SIZE(imx_rproc_att_imx7d),
274	.method		= IMX_RPROC_MMIO,
275};
276
277static const struct imx_rproc_dcfg imx_rproc_cfg_imx6sx = {
278	.src_reg	= IMX6SX_SRC_SCR,
279	.src_mask	= IMX6SX_M4_RST_MASK,
280	.src_start	= IMX6SX_M4_START,
281	.src_stop	= IMX6SX_M4_STOP,
282	.att		= imx_rproc_att_imx6sx,
283	.att_size	= ARRAY_SIZE(imx_rproc_att_imx6sx),
284	.method		= IMX_RPROC_MMIO,
285};
286
 
 
 
 
 
 
287static int imx_rproc_start(struct rproc *rproc)
288{
289	struct imx_rproc *priv = rproc->priv;
290	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
291	struct device *dev = priv->dev;
292	struct arm_smccc_res res;
293	int ret;
294
 
 
 
 
295	switch (dcfg->method) {
296	case IMX_RPROC_MMIO:
297		ret = regmap_update_bits(priv->regmap, dcfg->src_reg, dcfg->src_mask,
298					 dcfg->src_start);
 
 
 
 
 
 
299		break;
300	case IMX_RPROC_SMC:
301		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_START, 0, 0, 0, 0, 0, 0, &res);
302		ret = res.a0;
303		break;
 
 
 
304	default:
305		return -EOPNOTSUPP;
306	}
307
308	if (ret)
309		dev_err(dev, "Failed to enable remote core!\n");
310
311	return ret;
312}
313
314static int imx_rproc_stop(struct rproc *rproc)
315{
316	struct imx_rproc *priv = rproc->priv;
317	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
318	struct device *dev = priv->dev;
319	struct arm_smccc_res res;
320	int ret;
321
322	switch (dcfg->method) {
323	case IMX_RPROC_MMIO:
 
 
 
 
 
 
 
 
 
 
324		ret = regmap_update_bits(priv->regmap, dcfg->src_reg, dcfg->src_mask,
325					 dcfg->src_stop);
326		break;
327	case IMX_RPROC_SMC:
328		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_STOP, 0, 0, 0, 0, 0, 0, &res);
329		ret = res.a0;
330		if (res.a1)
331			dev_info(dev, "Not in wfi, force stopped\n");
332		break;
 
 
 
333	default:
334		return -EOPNOTSUPP;
335	}
336
337	if (ret)
338		dev_err(dev, "Failed to stop remote core\n");
 
 
339
340	return ret;
341}
342
343static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
344			       size_t len, u64 *sys)
345{
346	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
347	int i;
348
349	/* parse address translation table */
350	for (i = 0; i < dcfg->att_size; i++) {
351		const struct imx_rproc_att *att = &dcfg->att[i];
352
 
 
 
 
 
 
 
 
 
 
 
353		if (da >= att->da && da + len < att->da + att->size) {
354			unsigned int offset = da - att->da;
355
356			*sys = att->sa + offset;
 
 
357			return 0;
358		}
359	}
360
361	dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%zx\n",
362		 da, len);
363	return -ENOENT;
364}
365
366static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
367{
368	struct imx_rproc *priv = rproc->priv;
369	void *va = NULL;
370	u64 sys;
371	int i;
372
373	if (len == 0)
374		return NULL;
375
376	/*
377	 * On device side we have many aliases, so we need to convert device
378	 * address (M4) to system bus address first.
379	 */
380	if (imx_rproc_da_to_sys(priv, da, len, &sys))
381		return NULL;
382
383	for (i = 0; i < IMX_RPROC_MEM_MAX; i++) {
384		if (sys >= priv->mem[i].sys_addr && sys + len <
385		    priv->mem[i].sys_addr +  priv->mem[i].size) {
386			unsigned int offset = sys - priv->mem[i].sys_addr;
387			/* __force to make sparse happy with type conversion */
388			va = (__force void *)(priv->mem[i].cpu_addr + offset);
389			break;
390		}
391	}
392
393	dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%p\n",
394		da, len, va);
395
396	return va;
397}
398
399static int imx_rproc_mem_alloc(struct rproc *rproc,
400			       struct rproc_mem_entry *mem)
401{
402	struct device *dev = rproc->dev.parent;
403	void *va;
404
405	dev_dbg(dev, "map memory: %p+%zx\n", &mem->dma, mem->len);
406	va = ioremap_wc(mem->dma, mem->len);
407	if (IS_ERR_OR_NULL(va)) {
408		dev_err(dev, "Unable to map memory region: %p+%zx\n",
409			&mem->dma, mem->len);
410		return -ENOMEM;
411	}
412
413	/* Update memory entry va */
414	mem->va = va;
415
416	return 0;
417}
418
419static int imx_rproc_mem_release(struct rproc *rproc,
420				 struct rproc_mem_entry *mem)
421{
422	dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma);
423	iounmap(mem->va);
424
425	return 0;
426}
427
428static int imx_rproc_prepare(struct rproc *rproc)
429{
430	struct imx_rproc *priv = rproc->priv;
431	struct device_node *np = priv->dev->of_node;
432	struct of_phandle_iterator it;
433	struct rproc_mem_entry *mem;
434	struct reserved_mem *rmem;
435	u32 da;
436
437	/* Register associated reserved memory regions */
438	of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
439	while (of_phandle_iterator_next(&it) == 0) {
440		/*
441		 * Ignore the first memory region which will be used vdev buffer.
442		 * No need to do extra handlings, rproc_add_virtio_dev will handle it.
443		 */
444		if (!strcmp(it.node->name, "vdev0buffer"))
445			continue;
446
 
 
 
447		rmem = of_reserved_mem_lookup(it.node);
448		if (!rmem) {
 
449			dev_err(priv->dev, "unable to acquire memory-region\n");
450			return -EINVAL;
451		}
452
453		/* No need to translate pa to da, i.MX use same map */
454		da = rmem->base;
455
456		/* Register memory region */
457		mem = rproc_mem_entry_init(priv->dev, NULL, (dma_addr_t)rmem->base, rmem->size, da,
458					   imx_rproc_mem_alloc, imx_rproc_mem_release,
459					   it.node->name);
460
461		if (mem)
462			rproc_coredump_add_segment(rproc, da, rmem->size);
463		else
 
464			return -ENOMEM;
 
465
466		rproc_add_carveout(rproc, mem);
467	}
468
469	return  0;
470}
471
472static int imx_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
473{
474	int ret;
475
476	ret = rproc_elf_load_rsc_table(rproc, fw);
477	if (ret)
478		dev_info(&rproc->dev, "No resource table in elf\n");
479
480	return 0;
481}
482
483static void imx_rproc_kick(struct rproc *rproc, int vqid)
484{
485	struct imx_rproc *priv = rproc->priv;
486	int err;
487	__u32 mmsg;
488
489	if (!priv->tx_ch) {
490		dev_err(priv->dev, "No initialized mbox tx channel\n");
491		return;
492	}
493
494	/*
495	 * Send the index of the triggered virtqueue as the mu payload.
496	 * Let remote processor know which virtqueue is used.
497	 */
498	mmsg = vqid << 16;
499
500	err = mbox_send_message(priv->tx_ch, (void *)&mmsg);
501	if (err < 0)
502		dev_err(priv->dev, "%s: failed (%d, err:%d)\n",
503			__func__, vqid, err);
504}
505
506static int imx_rproc_attach(struct rproc *rproc)
507{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508	return 0;
509}
510
511static struct resource_table *imx_rproc_get_loaded_rsc_table(struct rproc *rproc, size_t *table_sz)
512{
513	struct imx_rproc *priv = rproc->priv;
514
515	/* The resource table has already been mapped in imx_rproc_addr_init */
516	if (!priv->rsc_table)
517		return NULL;
518
519	*table_sz = SZ_1K;
520	return (struct resource_table *)priv->rsc_table;
521}
522
 
 
 
 
 
 
 
 
 
 
 
523static const struct rproc_ops imx_rproc_ops = {
524	.prepare	= imx_rproc_prepare,
525	.attach		= imx_rproc_attach,
 
526	.start		= imx_rproc_start,
527	.stop		= imx_rproc_stop,
528	.kick		= imx_rproc_kick,
529	.da_to_va       = imx_rproc_da_to_va,
530	.load		= rproc_elf_load_segments,
531	.parse_fw	= imx_rproc_parse_fw,
532	.find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
533	.get_loaded_rsc_table = imx_rproc_get_loaded_rsc_table,
534	.sanity_check	= rproc_elf_sanity_check,
535	.get_boot_addr	= rproc_elf_get_boot_addr,
536};
537
538static int imx_rproc_addr_init(struct imx_rproc *priv,
539			       struct platform_device *pdev)
540{
541	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
542	struct device *dev = &pdev->dev;
543	struct device_node *np = dev->of_node;
544	int a, b = 0, err, nph;
545
546	/* remap required addresses */
547	for (a = 0; a < dcfg->att_size; a++) {
548		const struct imx_rproc_att *att = &dcfg->att[a];
549
550		if (!(att->flags & ATT_OWN))
551			continue;
552
553		if (b >= IMX_RPROC_MEM_MAX)
554			break;
555
556		priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev,
557						     att->sa, att->size);
 
 
 
 
558		if (!priv->mem[b].cpu_addr) {
559			dev_err(dev, "failed to remap %#x bytes from %#x\n", att->size, att->sa);
560			return -ENOMEM;
561		}
562		priv->mem[b].sys_addr = att->sa;
563		priv->mem[b].size = att->size;
564		b++;
565	}
566
567	/* memory-region is optional property */
568	nph = of_count_phandle_with_args(np, "memory-region", NULL);
569	if (nph <= 0)
570		return 0;
571
572	/* remap optional addresses */
573	for (a = 0; a < nph; a++) {
574		struct device_node *node;
575		struct resource res;
576
577		node = of_parse_phandle(np, "memory-region", a);
578		/* Not map vdev region */
579		if (!strcmp(node->name, "vdev"))
580			continue;
 
 
 
 
 
581		err = of_address_to_resource(node, 0, &res);
582		if (err) {
583			dev_err(dev, "unable to resolve memory region\n");
 
584			return err;
585		}
586
587		of_node_put(node);
588
589		if (b >= IMX_RPROC_MEM_MAX)
590			break;
 
591
592		/* Not use resource version, because we might share region */
593		priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
594		if (!priv->mem[b].cpu_addr) {
595			dev_err(dev, "failed to remap %pr\n", &res);
 
596			return -ENOMEM;
597		}
598		priv->mem[b].sys_addr = res.start;
599		priv->mem[b].size = resource_size(&res);
600		if (!strcmp(node->name, "rsc_table"))
601			priv->rsc_table = priv->mem[b].cpu_addr;
 
602		b++;
603	}
604
605	return 0;
606}
607
 
 
 
 
 
 
 
 
 
608static void imx_rproc_vq_work(struct work_struct *work)
609{
610	struct imx_rproc *priv = container_of(work, struct imx_rproc,
611					      rproc_work);
 
612
613	rproc_vq_interrupt(priv->rproc, 0);
614	rproc_vq_interrupt(priv->rproc, 1);
615}
616
617static void imx_rproc_rx_callback(struct mbox_client *cl, void *msg)
618{
619	struct rproc *rproc = dev_get_drvdata(cl->dev);
620	struct imx_rproc *priv = rproc->priv;
621
622	queue_work(priv->workqueue, &priv->rproc_work);
623}
624
625static int imx_rproc_xtr_mbox_init(struct rproc *rproc)
626{
627	struct imx_rproc *priv = rproc->priv;
628	struct device *dev = priv->dev;
629	struct mbox_client *cl;
630	int ret;
631
632	if (!of_get_property(dev->of_node, "mbox-names", NULL))
 
 
 
 
 
 
 
 
 
 
 
 
633		return 0;
634
635	cl = &priv->cl;
636	cl->dev = dev;
637	cl->tx_block = true;
638	cl->tx_tout = 100;
639	cl->knows_txdone = false;
640	cl->rx_callback = imx_rproc_rx_callback;
641
642	priv->tx_ch = mbox_request_channel_byname(cl, "tx");
643	if (IS_ERR(priv->tx_ch)) {
644		ret = PTR_ERR(priv->tx_ch);
645		return dev_err_probe(cl->dev, ret,
646				     "failed to request tx mailbox channel: %d\n", ret);
647	}
648
649	priv->rx_ch = mbox_request_channel_byname(cl, "rx");
650	if (IS_ERR(priv->rx_ch)) {
651		mbox_free_channel(priv->tx_ch);
652		ret = PTR_ERR(priv->rx_ch);
653		return dev_err_probe(cl->dev, ret,
654				     "failed to request rx mailbox channel: %d\n", ret);
655	}
656
657	return 0;
658}
659
660static void imx_rproc_free_mbox(struct rproc *rproc)
661{
662	struct imx_rproc *priv = rproc->priv;
663
664	mbox_free_channel(priv->tx_ch);
665	mbox_free_channel(priv->rx_ch);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666}
667
668static int imx_rproc_detect_mode(struct imx_rproc *priv)
669{
670	struct regmap_config config = { .name = "imx-rproc" };
671	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
672	struct device *dev = priv->dev;
673	struct regmap *regmap;
674	struct arm_smccc_res res;
675	int ret;
676	u32 val;
 
677
678	switch (dcfg->method) {
679	case IMX_RPROC_NONE:
680		priv->rproc->state = RPROC_DETACHED;
681		return 0;
682	case IMX_RPROC_SMC:
683		arm_smccc_smc(IMX_SIP_RPROC, IMX_SIP_RPROC_STARTED, 0, 0, 0, 0, 0, 0, &res);
684		if (res.a0)
685			priv->rproc->state = RPROC_DETACHED;
686		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687	default:
688		break;
689	}
690
 
 
 
 
691	regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
692	if (IS_ERR(regmap)) {
693		dev_err(dev, "failed to find syscon\n");
694		return PTR_ERR(regmap);
695	}
696
697	priv->regmap = regmap;
698	regmap_attach_dev(dev, regmap, &config);
699
 
 
 
 
 
 
 
 
 
 
 
 
 
700	ret = regmap_read(regmap, dcfg->src_reg, &val);
701	if (ret) {
702		dev_err(dev, "Failed to read src\n");
703		return ret;
704	}
705
706	if (!(val & dcfg->src_stop))
707		priv->rproc->state = RPROC_DETACHED;
708
709	return 0;
710}
711
712static int imx_rproc_clk_enable(struct imx_rproc *priv)
713{
714	const struct imx_rproc_dcfg *dcfg = priv->dcfg;
715	struct device *dev = priv->dev;
716	int ret;
717
718	/* Remote core is not under control of Linux */
719	if (dcfg->method == IMX_RPROC_NONE)
720		return 0;
721
722	priv->clk = devm_clk_get(dev, NULL);
723	if (IS_ERR(priv->clk)) {
724		dev_err(dev, "Failed to get clock\n");
725		return PTR_ERR(priv->clk);
726	}
727
728	/*
729	 * clk for M4 block including memory. Should be
730	 * enabled before .start for FW transfer.
731	 */
732	ret = clk_prepare_enable(priv->clk);
733	if (ret) {
734		dev_err(dev, "Failed to enable clock\n");
735		return ret;
736	}
737
738	return 0;
739}
740
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
741static int imx_rproc_probe(struct platform_device *pdev)
742{
743	struct device *dev = &pdev->dev;
744	struct device_node *np = dev->of_node;
745	struct imx_rproc *priv;
746	struct rproc *rproc;
747	const struct imx_rproc_dcfg *dcfg;
748	int ret;
749
750	/* set some other name then imx */
751	rproc = rproc_alloc(dev, "imx-rproc", &imx_rproc_ops,
752			    NULL, sizeof(*priv));
753	if (!rproc)
754		return -ENOMEM;
755
756	dcfg = of_device_get_match_data(dev);
757	if (!dcfg) {
758		ret = -EINVAL;
759		goto err_put_rproc;
760	}
761
762	priv = rproc->priv;
763	priv->rproc = rproc;
764	priv->dcfg = dcfg;
765	priv->dev = dev;
766
767	dev_set_drvdata(dev, rproc);
768	priv->workqueue = create_workqueue(dev_name(dev));
769	if (!priv->workqueue) {
770		dev_err(dev, "cannot create workqueue\n");
771		ret = -ENOMEM;
772		goto err_put_rproc;
773	}
774
775	ret = imx_rproc_xtr_mbox_init(rproc);
 
 
776	if (ret)
777		goto err_put_wkq;
778
779	ret = imx_rproc_addr_init(priv, pdev);
780	if (ret) {
781		dev_err(dev, "failed on imx_rproc_addr_init\n");
782		goto err_put_mbox;
783	}
784
785	ret = imx_rproc_detect_mode(priv);
786	if (ret)
787		goto err_put_mbox;
788
789	ret = imx_rproc_clk_enable(priv);
790	if (ret)
791		goto err_put_mbox;
792
793	INIT_WORK(&priv->rproc_work, imx_rproc_vq_work);
794
795	if (rproc->state != RPROC_DETACHED)
796		rproc->auto_boot = of_property_read_bool(np, "fsl,auto-boot");
797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798	ret = rproc_add(rproc);
799	if (ret) {
800		dev_err(dev, "rproc_add failed\n");
801		goto err_put_clk;
802	}
803
804	return 0;
805
806err_put_clk:
807	clk_disable_unprepare(priv->clk);
 
 
808err_put_mbox:
809	imx_rproc_free_mbox(rproc);
810err_put_wkq:
811	destroy_workqueue(priv->workqueue);
812err_put_rproc:
813	rproc_free(rproc);
814
815	return ret;
816}
817
818static int imx_rproc_remove(struct platform_device *pdev)
819{
820	struct rproc *rproc = platform_get_drvdata(pdev);
821	struct imx_rproc *priv = rproc->priv;
822
823	clk_disable_unprepare(priv->clk);
824	rproc_del(rproc);
 
825	imx_rproc_free_mbox(rproc);
826	rproc_free(rproc);
827
828	return 0;
829}
830
831static const struct of_device_id imx_rproc_of_match[] = {
832	{ .compatible = "fsl,imx7ulp-cm4", .data = &imx_rproc_cfg_imx7ulp },
833	{ .compatible = "fsl,imx7d-cm4", .data = &imx_rproc_cfg_imx7d },
834	{ .compatible = "fsl,imx6sx-cm4", .data = &imx_rproc_cfg_imx6sx },
835	{ .compatible = "fsl,imx8mq-cm4", .data = &imx_rproc_cfg_imx8mq },
836	{ .compatible = "fsl,imx8mm-cm4", .data = &imx_rproc_cfg_imx8mq },
837	{ .compatible = "fsl,imx8mn-cm7", .data = &imx_rproc_cfg_imx8mn },
838	{ .compatible = "fsl,imx8mp-cm7", .data = &imx_rproc_cfg_imx8mn },
 
 
 
 
839	{ .compatible = "fsl,imx8ulp-cm33", .data = &imx_rproc_cfg_imx8ulp },
 
840	{},
841};
842MODULE_DEVICE_TABLE(of, imx_rproc_of_match);
843
844static struct platform_driver imx_rproc_driver = {
845	.probe = imx_rproc_probe,
846	.remove = imx_rproc_remove,
847	.driver = {
848		.name = "imx-rproc",
849		.of_match_table = imx_rproc_of_match,
850	},
851};
852
853module_platform_driver(imx_rproc_driver);
854
855MODULE_LICENSE("GPL v2");
856MODULE_DESCRIPTION("i.MX remote processor control driver");
857MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");