Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2014 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/vexpress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define SYS_MISC		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define SYS_MISC_MASTERSITE	(1 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define SYS_PROCID0		0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define SYS_PROCID1		0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define SYS_HBI_MASK		0xfff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define SYS_PROCIDx_HBI_SHIFT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define SYS_CFGDATA		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define SYS_CFGCTRL		0x44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define SYS_CFGCTRL_START	(1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define SYS_CFGCTRL_WRITE	(1 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define SYS_CFGCTRL_DCC(n)	(((n) & 0xf) << 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define SYS_CFGCTRL_FUNC(n)	(((n) & 0x3f) << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define SYS_CFGCTRL_SITE(n)	(((n) & 0x3) << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define SYS_CFGCTRL_POSITION(n)	(((n) & 0xf) << 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define SYS_CFGCTRL_DEVICE(n)	(((n) & 0xfff) << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SYS_CFGSTAT		0x48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define SYS_CFGSTAT_ERR		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define SYS_CFGSTAT_COMPLETE	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define VEXPRESS_SITE_MB		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define VEXPRESS_SITE_DB1		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define VEXPRESS_SITE_DB2		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define VEXPRESS_SITE_MASTER		0xf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct vexpress_syscfg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct list_head funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) struct vexpress_syscfg_func {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct vexpress_syscfg *syscfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int num_templates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u32 template[]; /* Keep it last! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) struct vexpress_config_bridge_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct regmap * (*regmap_init)(struct device *dev, void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	void (*regmap_exit)(struct regmap *regmap, void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) struct vexpress_config_bridge {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct vexpress_config_bridge_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	void *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static DEFINE_MUTEX(vexpress_config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static u32 vexpress_config_site_master = VEXPRESS_SITE_MASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static void vexpress_config_set_master(u32 site)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	vexpress_config_site_master = site;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static void vexpress_config_lock(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	mutex_lock(&vexpress_config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static void vexpress_config_unlock(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mutex_unlock(&vexpress_config_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static void vexpress_config_find_prop(struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		const char *name, u32 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* Default value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	of_node_get(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (of_property_read_u32(node, name, val) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		node = of_get_next_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int vexpress_config_get_topo(struct device_node *node, u32 *site,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		u32 *position, u32 *dcc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	vexpress_config_find_prop(node, "arm,vexpress,site", site);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (*site == VEXPRESS_SITE_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		*site = vexpress_config_site_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (WARN_ON(vexpress_config_site_master == VEXPRESS_SITE_MASTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	vexpress_config_find_prop(node, "arm,vexpress,position", position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	vexpress_config_find_prop(node, "arm,vexpress,dcc", dcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void vexpress_config_devres_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct vexpress_config_bridge *bridge = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct regmap *regmap = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	bridge->ops->regmap_exit(regmap, bridge->context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct regmap *devm_regmap_init_vexpress_config(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct vexpress_config_bridge *bridge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct regmap **res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	bridge = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (WARN_ON(!bridge))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	res = devres_alloc(vexpress_config_devres_release, sizeof(*res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	regmap = (bridge->ops->regmap_init)(dev, bridge->context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		devres_free(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		return regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	*res = regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	devres_add(dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) EXPORT_SYMBOL_GPL(devm_regmap_init_vexpress_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		int index, bool write, u32 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct vexpress_syscfg *syscfg = func->syscfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	u32 command, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int tries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (WARN_ON(index >= func->num_templates))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	command = readl(syscfg->base + SYS_CFGCTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (WARN_ON(command & SYS_CFGCTRL_START))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	command = func->template[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	command |= SYS_CFGCTRL_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	command |= write ? SYS_CFGCTRL_WRITE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* Use a canary for reads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		*data = 0xdeadbeef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	dev_dbg(syscfg->dev, "func %p, command %x, data %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			func, command, *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	writel(*data, syscfg->base + SYS_CFGDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	writel(0, syscfg->base + SYS_CFGSTAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	writel(command, syscfg->base + SYS_CFGCTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	/* The operation can take ages... Go to sleep, 100us initially */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	tries = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	timeout = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		if (!irqs_disabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			schedule_timeout(usecs_to_jiffies(timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			udelay(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		status = readl(syscfg->base + SYS_CFGSTAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		if (status & SYS_CFGSTAT_ERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		if (timeout > 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			timeout -= 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	} while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (WARN_ON_ONCE(!tries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (!write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		*data = readl(syscfg->base + SYS_CFGDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		dev_dbg(syscfg->dev, "func %p, read data %x\n", func, *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int vexpress_syscfg_read(void *context, unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		unsigned int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct vexpress_syscfg_func *func = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return vexpress_syscfg_exec(func, index, false, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int vexpress_syscfg_write(void *context, unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct vexpress_syscfg_func *func = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return vexpress_syscfg_exec(func, index, true, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static struct regmap_config vexpress_syscfg_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.lock = vexpress_config_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	.unlock = vexpress_config_unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	.reg_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	.val_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.reg_read = vexpress_syscfg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.reg_write = vexpress_syscfg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct vexpress_syscfg *syscfg = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct vexpress_syscfg_func *func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	const __be32 *val = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	__be32 energy_quirk[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	u32 site, position, dcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	err = vexpress_config_get_topo(dev->of_node, &site,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				&position, &dcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	prop = of_find_property(dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			"arm,vexpress-sysreg,func", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	num = prop->length / sizeof(u32) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	val = prop->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * "arm,vexpress-energy" function used to be described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 * by its first device only, now it requires both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (num == 1 && of_device_is_compatible(dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			"arm,vexpress-energy")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		num = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		energy_quirk[0] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		energy_quirk[2] = *val++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		energy_quirk[1] = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		val = energy_quirk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	func = kzalloc(struct_size(func, template, num), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (!func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	func->syscfg = syscfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	func->num_templates = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		u32 function, device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		function = be32_to_cpup(val++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		device = be32_to_cpup(val++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				func, site, position, dcc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				function, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		func->template[i] = SYS_CFGCTRL_DCC(dcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		func->template[i] |= SYS_CFGCTRL_SITE(site);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		func->template[i] |= SYS_CFGCTRL_POSITION(position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		func->template[i] |= SYS_CFGCTRL_FUNC(function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		func->template[i] |= SYS_CFGCTRL_DEVICE(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	vexpress_syscfg_regmap_config.max_register = num - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	func->regmap = regmap_init(dev, NULL, func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			&vexpress_syscfg_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (IS_ERR(func->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		void *err = func->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		kfree(func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	list_add(&func->list, &syscfg->funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return func->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct vexpress_syscfg *syscfg = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct vexpress_syscfg_func *func, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	regmap_exit(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (func->regmap == regmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			list_del(&syscfg->funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			kfree(func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	.regmap_init = vexpress_syscfg_regmap_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	.regmap_exit = vexpress_syscfg_regmap_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int vexpress_syscfg_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct vexpress_syscfg *syscfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct vexpress_config_bridge *bridge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	u32 dt_hbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (!syscfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	syscfg->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	INIT_LIST_HEAD(&syscfg->funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	syscfg->base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (IS_ERR(syscfg->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		return PTR_ERR(syscfg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	bridge = devm_kmalloc(&pdev->dev, sizeof(*bridge), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (!bridge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	bridge->ops = &vexpress_syscfg_bridge_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	bridge->context = syscfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	dev_set_drvdata(&pdev->dev, bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	master = readl(syscfg->base + SYS_MISC) & SYS_MISC_MASTERSITE ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	vexpress_config_set_master(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* Confirm board type against DT property, if available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		u32 id = readl(syscfg->base + (master == VEXPRESS_SITE_DB1 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				 SYS_PROCID0 : SYS_PROCID1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		u32 hbi = (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (WARN_ON(dt_hbi != hbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			dev_warn(&pdev->dev, "DT HBI (%x) is not matching hardware (%x)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 					dt_hbi, hbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		struct device_node *bridge_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		bridge_np = of_parse_phandle(node, "arm,vexpress,config-bridge", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		if (bridge_np != pdev->dev.parent->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		of_platform_populate(node, NULL, NULL, &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static const struct platform_device_id vexpress_syscfg_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	{ "vexpress-syscfg", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) MODULE_DEVICE_TABLE(platform, vexpress_syscfg_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static struct platform_driver vexpress_syscfg_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.driver.name = "vexpress-syscfg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.id_table = vexpress_syscfg_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.probe = vexpress_syscfg_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) module_platform_driver(vexpress_syscfg_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MODULE_LICENSE("GPL v2");