vexpress-syscfg.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/syscore_ops.h>
  21. #include <linux/vexpress.h>
  22. #define SYS_CFGDATA 0x0
  23. #define SYS_CFGCTRL 0x4
  24. #define SYS_CFGCTRL_START (1 << 31)
  25. #define SYS_CFGCTRL_WRITE (1 << 30)
  26. #define SYS_CFGCTRL_DCC(n) (((n) & 0xf) << 26)
  27. #define SYS_CFGCTRL_FUNC(n) (((n) & 0x3f) << 20)
  28. #define SYS_CFGCTRL_SITE(n) (((n) & 0x3) << 16)
  29. #define SYS_CFGCTRL_POSITION(n) (((n) & 0xf) << 12)
  30. #define SYS_CFGCTRL_DEVICE(n) (((n) & 0xfff) << 0)
  31. #define SYS_CFGSTAT 0x8
  32. #define SYS_CFGSTAT_ERR (1 << 1)
  33. #define SYS_CFGSTAT_COMPLETE (1 << 0)
  34. struct vexpress_syscfg {
  35. struct device *dev;
  36. void __iomem *base;
  37. struct list_head funcs;
  38. };
  39. struct vexpress_syscfg_func {
  40. struct list_head list;
  41. struct vexpress_syscfg *syscfg;
  42. struct regmap *regmap;
  43. int num_templates;
  44. u32 template[0]; /* Keep it last! */
  45. };
  46. static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func,
  47. int index, bool write, u32 *data)
  48. {
  49. struct vexpress_syscfg *syscfg = func->syscfg;
  50. u32 command, status;
  51. int tries;
  52. long timeout;
  53. if (WARN_ON(index >= func->num_templates))
  54. return -EINVAL;
  55. command = readl(syscfg->base + SYS_CFGCTRL);
  56. if (WARN_ON(command & SYS_CFGCTRL_START))
  57. return -EBUSY;
  58. command = func->template[index];
  59. command |= SYS_CFGCTRL_START;
  60. command |= write ? SYS_CFGCTRL_WRITE : 0;
  61. /* Use a canary for reads */
  62. if (!write)
  63. *data = 0xdeadbeef;
  64. dev_dbg(syscfg->dev, "func %p, command %x, data %x\n",
  65. func, command, *data);
  66. writel(*data, syscfg->base + SYS_CFGDATA);
  67. writel(0, syscfg->base + SYS_CFGSTAT);
  68. writel(command, syscfg->base + SYS_CFGCTRL);
  69. mb();
  70. /* The operation can take ages... Go to sleep, 100us initially */
  71. tries = 100;
  72. timeout = 100;
  73. do {
  74. if (!irqs_disabled()) {
  75. set_current_state(TASK_INTERRUPTIBLE);
  76. schedule_timeout(usecs_to_jiffies(timeout));
  77. if (signal_pending(current))
  78. return -EINTR;
  79. } else {
  80. udelay(timeout);
  81. }
  82. status = readl(syscfg->base + SYS_CFGSTAT);
  83. if (status & SYS_CFGSTAT_ERR)
  84. return -EFAULT;
  85. if (timeout > 20)
  86. timeout -= 20;
  87. } while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
  88. if (WARN_ON_ONCE(!tries))
  89. return -ETIMEDOUT;
  90. if (!write) {
  91. *data = readl(syscfg->base + SYS_CFGDATA);
  92. dev_dbg(syscfg->dev, "func %p, read data %x\n", func, *data);
  93. }
  94. return 0;
  95. }
  96. static int vexpress_syscfg_read(void *context, unsigned int index,
  97. unsigned int *val)
  98. {
  99. struct vexpress_syscfg_func *func = context;
  100. return vexpress_syscfg_exec(func, index, false, val);
  101. }
  102. static int vexpress_syscfg_write(void *context, unsigned int index,
  103. unsigned int val)
  104. {
  105. struct vexpress_syscfg_func *func = context;
  106. return vexpress_syscfg_exec(func, index, true, &val);
  107. }
  108. static struct regmap_config vexpress_syscfg_regmap_config = {
  109. .lock = vexpress_config_lock,
  110. .unlock = vexpress_config_unlock,
  111. .reg_bits = 32,
  112. .val_bits = 32,
  113. .reg_read = vexpress_syscfg_read,
  114. .reg_write = vexpress_syscfg_write,
  115. .reg_format_endian = REGMAP_ENDIAN_LITTLE,
  116. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  117. };
  118. static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
  119. void *context)
  120. {
  121. int err;
  122. struct vexpress_syscfg *syscfg = context;
  123. struct vexpress_syscfg_func *func;
  124. struct property *prop;
  125. const __be32 *val = NULL;
  126. __be32 energy_quirk[4];
  127. int num;
  128. u32 site, position, dcc;
  129. int i;
  130. err = vexpress_config_get_topo(dev->of_node, &site,
  131. &position, &dcc);
  132. if (err)
  133. return ERR_PTR(err);
  134. prop = of_find_property(dev->of_node,
  135. "arm,vexpress-sysreg,func", NULL);
  136. if (!prop)
  137. return ERR_PTR(-EINVAL);
  138. num = prop->length / sizeof(u32) / 2;
  139. val = prop->value;
  140. /*
  141. * "arm,vexpress-energy" function used to be described
  142. * by its first device only, now it requires both
  143. */
  144. if (num == 1 && of_device_is_compatible(dev->of_node,
  145. "arm,vexpress-energy")) {
  146. num = 2;
  147. energy_quirk[0] = *val;
  148. energy_quirk[2] = *val++;
  149. energy_quirk[1] = *val;
  150. energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
  151. val = energy_quirk;
  152. }
  153. func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
  154. GFP_KERNEL);
  155. if (!func)
  156. return ERR_PTR(-ENOMEM);
  157. func->syscfg = syscfg;
  158. func->num_templates = num;
  159. for (i = 0; i < num; i++) {
  160. u32 function, device;
  161. function = be32_to_cpup(val++);
  162. device = be32_to_cpup(val++);
  163. dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
  164. func, site, position, dcc,
  165. function, device);
  166. func->template[i] = SYS_CFGCTRL_DCC(dcc);
  167. func->template[i] |= SYS_CFGCTRL_SITE(site);
  168. func->template[i] |= SYS_CFGCTRL_POSITION(position);
  169. func->template[i] |= SYS_CFGCTRL_FUNC(function);
  170. func->template[i] |= SYS_CFGCTRL_DEVICE(device);
  171. }
  172. vexpress_syscfg_regmap_config.max_register = num - 1;
  173. func->regmap = regmap_init(dev, NULL, func,
  174. &vexpress_syscfg_regmap_config);
  175. if (IS_ERR(func->regmap)) {
  176. void *err = func->regmap;
  177. kfree(func);
  178. return err;
  179. }
  180. list_add(&func->list, &syscfg->funcs);
  181. return func->regmap;
  182. }
  183. static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context)
  184. {
  185. struct vexpress_syscfg *syscfg = context;
  186. struct vexpress_syscfg_func *func, *tmp;
  187. regmap_exit(regmap);
  188. list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) {
  189. if (func->regmap == regmap) {
  190. list_del(&syscfg->funcs);
  191. kfree(func);
  192. break;
  193. }
  194. }
  195. }
  196. static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = {
  197. .regmap_init = vexpress_syscfg_regmap_init,
  198. .regmap_exit = vexpress_syscfg_regmap_exit,
  199. };
  200. static int vexpress_syscfg_probe(struct platform_device *pdev)
  201. {
  202. struct vexpress_syscfg *syscfg;
  203. struct resource *res;
  204. struct device *bridge;
  205. syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
  206. if (!syscfg)
  207. return -ENOMEM;
  208. syscfg->dev = &pdev->dev;
  209. INIT_LIST_HEAD(&syscfg->funcs);
  210. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. if (!devm_request_mem_region(&pdev->dev, res->start,
  212. resource_size(res), pdev->name))
  213. return -EBUSY;
  214. syscfg->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  215. if (!syscfg->base)
  216. return -EFAULT;
  217. /* Must use dev.parent (MFD), as that's where DT phandle points at... */
  218. bridge = vexpress_config_bridge_register(pdev->dev.parent,
  219. &vexpress_syscfg_bridge_ops, syscfg);
  220. if (IS_ERR(bridge))
  221. return PTR_ERR(bridge);
  222. return 0;
  223. }
  224. static const struct platform_device_id vexpress_syscfg_id_table[] = {
  225. { "vexpress-syscfg", },
  226. {},
  227. };
  228. static struct platform_driver vexpress_syscfg_driver = {
  229. .driver.name = "vexpress-syscfg",
  230. .id_table = vexpress_syscfg_id_table,
  231. .probe = vexpress_syscfg_probe,
  232. };
  233. static int __init vexpress_syscfg_init(void)
  234. {
  235. return platform_driver_register(&vexpress_syscfg_driver);
  236. }
  237. core_initcall(vexpress_syscfg_init);