vexpress-syscfg.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. 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. struct platform_device *pdev = to_platform_device(dev);
  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. if (dev->of_node) {
  131. int err = vexpress_config_get_topo(dev->of_node, &site,
  132. &position, &dcc);
  133. if (err)
  134. return ERR_PTR(err);
  135. prop = of_find_property(dev->of_node,
  136. "arm,vexpress-sysreg,func", NULL);
  137. if (!prop)
  138. return ERR_PTR(-EINVAL);
  139. num = prop->length / sizeof(u32) / 2;
  140. val = prop->value;
  141. } else {
  142. if (pdev->num_resources != 1 ||
  143. pdev->resource[0].flags != IORESOURCE_BUS)
  144. return ERR_PTR(-EFAULT);
  145. site = pdev->resource[0].start;
  146. if (site == VEXPRESS_SITE_MASTER)
  147. site = vexpress_config_get_master();
  148. position = 0;
  149. dcc = 0;
  150. num = 1;
  151. }
  152. /*
  153. * "arm,vexpress-energy" function used to be described
  154. * by its first device only, now it requires both
  155. */
  156. if (num == 1 && of_device_is_compatible(dev->of_node,
  157. "arm,vexpress-energy")) {
  158. num = 2;
  159. energy_quirk[0] = *val;
  160. energy_quirk[2] = *val++;
  161. energy_quirk[1] = *val;
  162. energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
  163. val = energy_quirk;
  164. }
  165. func = kzalloc(sizeof(*func) + sizeof(*func->template) * num,
  166. GFP_KERNEL);
  167. if (!func)
  168. return ERR_PTR(-ENOMEM);
  169. func->syscfg = syscfg;
  170. func->num_templates = num;
  171. for (i = 0; i < num; i++) {
  172. u32 function, device;
  173. if (dev->of_node) {
  174. function = be32_to_cpup(val++);
  175. device = be32_to_cpup(val++);
  176. } else {
  177. function = pdev->resource[0].end;
  178. device = pdev->id;
  179. }
  180. dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
  181. func, site, position, dcc,
  182. function, device);
  183. func->template[i] = SYS_CFGCTRL_DCC(dcc);
  184. func->template[i] |= SYS_CFGCTRL_SITE(site);
  185. func->template[i] |= SYS_CFGCTRL_POSITION(position);
  186. func->template[i] |= SYS_CFGCTRL_FUNC(function);
  187. func->template[i] |= SYS_CFGCTRL_DEVICE(device);
  188. }
  189. vexpress_syscfg_regmap_config.max_register = num - 1;
  190. func->regmap = regmap_init(dev, NULL, func,
  191. &vexpress_syscfg_regmap_config);
  192. if (IS_ERR(func->regmap)) {
  193. void *err = func->regmap;
  194. kfree(func);
  195. return err;
  196. }
  197. list_add(&func->list, &syscfg->funcs);
  198. return func->regmap;
  199. }
  200. static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context)
  201. {
  202. struct vexpress_syscfg *syscfg = context;
  203. struct vexpress_syscfg_func *func, *tmp;
  204. regmap_exit(regmap);
  205. list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) {
  206. if (func->regmap == regmap) {
  207. list_del(&syscfg->funcs);
  208. kfree(func);
  209. break;
  210. }
  211. }
  212. }
  213. static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = {
  214. .regmap_init = vexpress_syscfg_regmap_init,
  215. .regmap_exit = vexpress_syscfg_regmap_exit,
  216. };
  217. /* Non-DT hack, to be gone... */
  218. static struct device *vexpress_syscfg_bridge;
  219. int vexpress_syscfg_device_register(struct platform_device *pdev)
  220. {
  221. pdev->dev.parent = vexpress_syscfg_bridge;
  222. return platform_device_register(pdev);
  223. }
  224. int vexpress_syscfg_probe(struct platform_device *pdev)
  225. {
  226. struct vexpress_syscfg *syscfg;
  227. struct resource *res;
  228. struct device *bridge;
  229. syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
  230. if (!syscfg)
  231. return -ENOMEM;
  232. syscfg->dev = &pdev->dev;
  233. INIT_LIST_HEAD(&syscfg->funcs);
  234. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. if (!devm_request_mem_region(&pdev->dev, res->start,
  236. resource_size(res), pdev->name))
  237. return -EBUSY;
  238. syscfg->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  239. if (!syscfg->base)
  240. return -EFAULT;
  241. /* Must use dev.parent (MFD), as that's where DT phandle points at... */
  242. bridge = vexpress_config_bridge_register(pdev->dev.parent,
  243. &vexpress_syscfg_bridge_ops, syscfg);
  244. if (IS_ERR(bridge))
  245. return PTR_ERR(bridge);
  246. /* Non-DT case */
  247. if (!pdev->dev.of_node)
  248. vexpress_syscfg_bridge = bridge;
  249. return 0;
  250. }
  251. static const struct platform_device_id vexpress_syscfg_id_table[] = {
  252. { "vexpress-syscfg", },
  253. {},
  254. };
  255. static struct platform_driver vexpress_syscfg_driver = {
  256. .driver.name = "vexpress-syscfg",
  257. .id_table = vexpress_syscfg_id_table,
  258. .probe = vexpress_syscfg_probe,
  259. };
  260. static int __init vexpress_syscfg_init(void)
  261. {
  262. return platform_driver_register(&vexpress_syscfg_driver);
  263. }
  264. core_initcall(vexpress_syscfg_init);