Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright (C) 2014 ARM Limited |
| 12 | */ |
| 13 | |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/syscore_ops.h> |
| 22 | #include <linux/vexpress.h> |
| 23 | |
| 24 | |
| 25 | #define SYS_CFGDATA 0x0 |
| 26 | |
| 27 | #define SYS_CFGCTRL 0x4 |
| 28 | #define SYS_CFGCTRL_START (1 << 31) |
| 29 | #define SYS_CFGCTRL_WRITE (1 << 30) |
| 30 | #define SYS_CFGCTRL_DCC(n) (((n) & 0xf) << 26) |
| 31 | #define SYS_CFGCTRL_FUNC(n) (((n) & 0x3f) << 20) |
| 32 | #define SYS_CFGCTRL_SITE(n) (((n) & 0x3) << 16) |
| 33 | #define SYS_CFGCTRL_POSITION(n) (((n) & 0xf) << 12) |
| 34 | #define SYS_CFGCTRL_DEVICE(n) (((n) & 0xfff) << 0) |
| 35 | |
| 36 | #define SYS_CFGSTAT 0x8 |
| 37 | #define SYS_CFGSTAT_ERR (1 << 1) |
| 38 | #define SYS_CFGSTAT_COMPLETE (1 << 0) |
| 39 | |
| 40 | |
| 41 | struct vexpress_syscfg { |
| 42 | struct device *dev; |
| 43 | void __iomem *base; |
| 44 | struct list_head funcs; |
| 45 | }; |
| 46 | |
| 47 | struct vexpress_syscfg_func { |
| 48 | struct list_head list; |
| 49 | struct vexpress_syscfg *syscfg; |
| 50 | struct regmap *regmap; |
| 51 | int num_templates; |
| 52 | u32 template[0]; /* Keep it last! */ |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func, |
| 57 | int index, bool write, u32 *data) |
| 58 | { |
| 59 | struct vexpress_syscfg *syscfg = func->syscfg; |
| 60 | u32 command, status; |
| 61 | int tries; |
| 62 | long timeout; |
| 63 | |
Dan Carpenter | c0b3665 | 2018-12-03 17:52:19 +0300 | [diff] [blame] | 64 | if (WARN_ON(index >= func->num_templates)) |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 65 | return -EINVAL; |
| 66 | |
| 67 | command = readl(syscfg->base + SYS_CFGCTRL); |
| 68 | if (WARN_ON(command & SYS_CFGCTRL_START)) |
| 69 | return -EBUSY; |
| 70 | |
| 71 | command = func->template[index]; |
| 72 | command |= SYS_CFGCTRL_START; |
| 73 | command |= write ? SYS_CFGCTRL_WRITE : 0; |
| 74 | |
| 75 | /* Use a canary for reads */ |
| 76 | if (!write) |
| 77 | *data = 0xdeadbeef; |
| 78 | |
| 79 | dev_dbg(syscfg->dev, "func %p, command %x, data %x\n", |
| 80 | func, command, *data); |
| 81 | writel(*data, syscfg->base + SYS_CFGDATA); |
| 82 | writel(0, syscfg->base + SYS_CFGSTAT); |
| 83 | writel(command, syscfg->base + SYS_CFGCTRL); |
| 84 | mb(); |
| 85 | |
| 86 | /* The operation can take ages... Go to sleep, 100us initially */ |
| 87 | tries = 100; |
| 88 | timeout = 100; |
| 89 | do { |
| 90 | if (!irqs_disabled()) { |
| 91 | set_current_state(TASK_INTERRUPTIBLE); |
| 92 | schedule_timeout(usecs_to_jiffies(timeout)); |
| 93 | if (signal_pending(current)) |
| 94 | return -EINTR; |
| 95 | } else { |
| 96 | udelay(timeout); |
| 97 | } |
| 98 | |
| 99 | status = readl(syscfg->base + SYS_CFGSTAT); |
| 100 | if (status & SYS_CFGSTAT_ERR) |
| 101 | return -EFAULT; |
| 102 | |
| 103 | if (timeout > 20) |
| 104 | timeout -= 20; |
| 105 | } while (--tries && !(status & SYS_CFGSTAT_COMPLETE)); |
| 106 | if (WARN_ON_ONCE(!tries)) |
| 107 | return -ETIMEDOUT; |
| 108 | |
| 109 | if (!write) { |
| 110 | *data = readl(syscfg->base + SYS_CFGDATA); |
| 111 | dev_dbg(syscfg->dev, "func %p, read data %x\n", func, *data); |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int vexpress_syscfg_read(void *context, unsigned int index, |
| 118 | unsigned int *val) |
| 119 | { |
| 120 | struct vexpress_syscfg_func *func = context; |
| 121 | |
| 122 | return vexpress_syscfg_exec(func, index, false, val); |
| 123 | } |
| 124 | |
| 125 | static int vexpress_syscfg_write(void *context, unsigned int index, |
| 126 | unsigned int val) |
| 127 | { |
| 128 | struct vexpress_syscfg_func *func = context; |
| 129 | |
| 130 | return vexpress_syscfg_exec(func, index, true, &val); |
| 131 | } |
| 132 | |
Wei Yongjun | f222447 | 2014-07-20 13:25:39 +0800 | [diff] [blame] | 133 | static struct regmap_config vexpress_syscfg_regmap_config = { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 134 | .lock = vexpress_config_lock, |
| 135 | .unlock = vexpress_config_unlock, |
| 136 | .reg_bits = 32, |
| 137 | .val_bits = 32, |
| 138 | .reg_read = vexpress_syscfg_read, |
| 139 | .reg_write = vexpress_syscfg_write, |
| 140 | .reg_format_endian = REGMAP_ENDIAN_LITTLE, |
| 141 | .val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | static struct regmap *vexpress_syscfg_regmap_init(struct device *dev, |
| 146 | void *context) |
| 147 | { |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 148 | int err; |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 149 | struct vexpress_syscfg *syscfg = context; |
| 150 | struct vexpress_syscfg_func *func; |
| 151 | struct property *prop; |
| 152 | const __be32 *val = NULL; |
| 153 | __be32 energy_quirk[4]; |
| 154 | int num; |
| 155 | u32 site, position, dcc; |
| 156 | int i; |
| 157 | |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 158 | err = vexpress_config_get_topo(dev->of_node, &site, |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 159 | &position, &dcc); |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 160 | if (err) |
| 161 | return ERR_PTR(err); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 162 | |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 163 | prop = of_find_property(dev->of_node, |
| 164 | "arm,vexpress-sysreg,func", NULL); |
| 165 | if (!prop) |
| 166 | return ERR_PTR(-EINVAL); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 167 | |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 168 | num = prop->length / sizeof(u32) / 2; |
| 169 | val = prop->value; |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * "arm,vexpress-energy" function used to be described |
| 173 | * by its first device only, now it requires both |
| 174 | */ |
| 175 | if (num == 1 && of_device_is_compatible(dev->of_node, |
| 176 | "arm,vexpress-energy")) { |
| 177 | num = 2; |
| 178 | energy_quirk[0] = *val; |
| 179 | energy_quirk[2] = *val++; |
| 180 | energy_quirk[1] = *val; |
| 181 | energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1); |
| 182 | val = energy_quirk; |
| 183 | } |
| 184 | |
| 185 | func = kzalloc(sizeof(*func) + sizeof(*func->template) * num, |
| 186 | GFP_KERNEL); |
| 187 | if (!func) |
Dan Carpenter | b86e192 | 2014-06-11 13:17:41 +0300 | [diff] [blame] | 188 | return ERR_PTR(-ENOMEM); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 189 | |
| 190 | func->syscfg = syscfg; |
| 191 | func->num_templates = num; |
| 192 | |
| 193 | for (i = 0; i < num; i++) { |
| 194 | u32 function, device; |
| 195 | |
Pawel Moll | 81cc3f8 | 2014-11-25 18:17:34 +0000 | [diff] [blame] | 196 | function = be32_to_cpup(val++); |
| 197 | device = be32_to_cpup(val++); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 198 | |
| 199 | dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n", |
| 200 | func, site, position, dcc, |
| 201 | function, device); |
| 202 | |
| 203 | func->template[i] = SYS_CFGCTRL_DCC(dcc); |
| 204 | func->template[i] |= SYS_CFGCTRL_SITE(site); |
| 205 | func->template[i] |= SYS_CFGCTRL_POSITION(position); |
| 206 | func->template[i] |= SYS_CFGCTRL_FUNC(function); |
| 207 | func->template[i] |= SYS_CFGCTRL_DEVICE(device); |
| 208 | } |
| 209 | |
| 210 | vexpress_syscfg_regmap_config.max_register = num - 1; |
| 211 | |
| 212 | func->regmap = regmap_init(dev, NULL, func, |
| 213 | &vexpress_syscfg_regmap_config); |
| 214 | |
Dan Carpenter | b86e192 | 2014-06-11 13:17:41 +0300 | [diff] [blame] | 215 | if (IS_ERR(func->regmap)) { |
| 216 | void *err = func->regmap; |
| 217 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 218 | kfree(func); |
Dan Carpenter | b86e192 | 2014-06-11 13:17:41 +0300 | [diff] [blame] | 219 | return err; |
| 220 | } |
| 221 | |
| 222 | list_add(&func->list, &syscfg->funcs); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 223 | |
| 224 | return func->regmap; |
| 225 | } |
| 226 | |
| 227 | static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context) |
| 228 | { |
| 229 | struct vexpress_syscfg *syscfg = context; |
| 230 | struct vexpress_syscfg_func *func, *tmp; |
| 231 | |
| 232 | regmap_exit(regmap); |
| 233 | |
| 234 | list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) { |
| 235 | if (func->regmap == regmap) { |
| 236 | list_del(&syscfg->funcs); |
| 237 | kfree(func); |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = { |
| 244 | .regmap_init = vexpress_syscfg_regmap_init, |
| 245 | .regmap_exit = vexpress_syscfg_regmap_exit, |
| 246 | }; |
| 247 | |
| 248 | |
Wei Yongjun | f222447 | 2014-07-20 13:25:39 +0800 | [diff] [blame] | 249 | static int vexpress_syscfg_probe(struct platform_device *pdev) |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 250 | { |
| 251 | struct vexpress_syscfg *syscfg; |
| 252 | struct resource *res; |
| 253 | struct device *bridge; |
| 254 | |
| 255 | syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL); |
| 256 | if (!syscfg) |
| 257 | return -ENOMEM; |
| 258 | syscfg->dev = &pdev->dev; |
| 259 | INIT_LIST_HEAD(&syscfg->funcs); |
| 260 | |
| 261 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 262 | if (!devm_request_mem_region(&pdev->dev, res->start, |
| 263 | resource_size(res), pdev->name)) |
| 264 | return -EBUSY; |
| 265 | |
| 266 | syscfg->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 267 | if (!syscfg->base) |
| 268 | return -EFAULT; |
| 269 | |
| 270 | /* Must use dev.parent (MFD), as that's where DT phandle points at... */ |
| 271 | bridge = vexpress_config_bridge_register(pdev->dev.parent, |
| 272 | &vexpress_syscfg_bridge_ops, syscfg); |
| 273 | if (IS_ERR(bridge)) |
| 274 | return PTR_ERR(bridge); |
| 275 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static const struct platform_device_id vexpress_syscfg_id_table[] = { |
| 280 | { "vexpress-syscfg", }, |
| 281 | {}, |
| 282 | }; |
| 283 | |
| 284 | static struct platform_driver vexpress_syscfg_driver = { |
| 285 | .driver.name = "vexpress-syscfg", |
| 286 | .id_table = vexpress_syscfg_id_table, |
| 287 | .probe = vexpress_syscfg_probe, |
| 288 | }; |
| 289 | |
| 290 | static int __init vexpress_syscfg_init(void) |
| 291 | { |
| 292 | return platform_driver_register(&vexpress_syscfg_driver); |
| 293 | } |
| 294 | core_initcall(vexpress_syscfg_init); |