sunxi_sram.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Allwinner SoCs SRAM Controller Driver
  3. *
  4. * Copyright (C) 2015 Maxime Ripard
  5. *
  6. * Author: Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/soc/sunxi/sunxi_sram.h>
  20. struct sunxi_sram_func {
  21. char *func;
  22. u8 val;
  23. u32 reg_val;
  24. };
  25. struct sunxi_sram_data {
  26. char *name;
  27. u8 reg;
  28. u8 offset;
  29. u8 width;
  30. struct sunxi_sram_func *func;
  31. struct list_head list;
  32. };
  33. struct sunxi_sram_desc {
  34. struct sunxi_sram_data data;
  35. bool claimed;
  36. };
  37. #define SUNXI_SRAM_MAP(_reg_val, _val, _func) \
  38. { \
  39. .func = _func, \
  40. .val = _val, \
  41. .reg_val = _reg_val, \
  42. }
  43. #define SUNXI_SRAM_DATA(_name, _reg, _off, _width, ...) \
  44. { \
  45. .name = _name, \
  46. .reg = _reg, \
  47. .offset = _off, \
  48. .width = _width, \
  49. .func = (struct sunxi_sram_func[]){ \
  50. __VA_ARGS__, { } }, \
  51. }
  52. static struct sunxi_sram_desc sun4i_a10_sram_a3_a4 = {
  53. .data = SUNXI_SRAM_DATA("A3-A4", 0x4, 0x4, 2,
  54. SUNXI_SRAM_MAP(0, 0, "cpu"),
  55. SUNXI_SRAM_MAP(1, 1, "emac")),
  56. };
  57. static struct sunxi_sram_desc sun4i_a10_sram_d = {
  58. .data = SUNXI_SRAM_DATA("D", 0x4, 0x0, 1,
  59. SUNXI_SRAM_MAP(0, 0, "cpu"),
  60. SUNXI_SRAM_MAP(1, 1, "usb-otg")),
  61. };
  62. static struct sunxi_sram_desc sun50i_a64_sram_c = {
  63. .data = SUNXI_SRAM_DATA("C", 0x4, 24, 1,
  64. SUNXI_SRAM_MAP(0, 1, "cpu"),
  65. SUNXI_SRAM_MAP(1, 0, "de2")),
  66. };
  67. static const struct of_device_id sunxi_sram_dt_ids[] = {
  68. {
  69. .compatible = "allwinner,sun4i-a10-sram-a3-a4",
  70. .data = &sun4i_a10_sram_a3_a4.data,
  71. },
  72. {
  73. .compatible = "allwinner,sun4i-a10-sram-d",
  74. .data = &sun4i_a10_sram_d.data,
  75. },
  76. {
  77. .compatible = "allwinner,sun50i-a64-sram-c",
  78. .data = &sun50i_a64_sram_c.data,
  79. },
  80. {}
  81. };
  82. static struct device *sram_dev;
  83. static LIST_HEAD(claimed_sram);
  84. static DEFINE_SPINLOCK(sram_lock);
  85. static void __iomem *base;
  86. static int sunxi_sram_show(struct seq_file *s, void *data)
  87. {
  88. struct device_node *sram_node, *section_node;
  89. const struct sunxi_sram_data *sram_data;
  90. const struct of_device_id *match;
  91. struct sunxi_sram_func *func;
  92. const __be32 *sram_addr_p, *section_addr_p;
  93. u32 val;
  94. seq_puts(s, "Allwinner sunXi SRAM\n");
  95. seq_puts(s, "--------------------\n\n");
  96. for_each_child_of_node(sram_dev->of_node, sram_node) {
  97. sram_addr_p = of_get_address(sram_node, 0, NULL, NULL);
  98. seq_printf(s, "sram@%08x\n",
  99. be32_to_cpu(*sram_addr_p));
  100. for_each_child_of_node(sram_node, section_node) {
  101. match = of_match_node(sunxi_sram_dt_ids, section_node);
  102. if (!match)
  103. continue;
  104. sram_data = match->data;
  105. section_addr_p = of_get_address(section_node, 0,
  106. NULL, NULL);
  107. seq_printf(s, "\tsection@%04x\t(%s)\n",
  108. be32_to_cpu(*section_addr_p),
  109. sram_data->name);
  110. val = readl(base + sram_data->reg);
  111. val >>= sram_data->offset;
  112. val &= GENMASK(sram_data->width - 1, 0);
  113. for (func = sram_data->func; func->func; func++) {
  114. seq_printf(s, "\t\t%s%c\n", func->func,
  115. func->reg_val == val ?
  116. '*' : ' ');
  117. }
  118. }
  119. seq_puts(s, "\n");
  120. }
  121. return 0;
  122. }
  123. static int sunxi_sram_open(struct inode *inode, struct file *file)
  124. {
  125. return single_open(file, sunxi_sram_show, inode->i_private);
  126. }
  127. static const struct file_operations sunxi_sram_fops = {
  128. .open = sunxi_sram_open,
  129. .read = seq_read,
  130. .llseek = seq_lseek,
  131. .release = single_release,
  132. };
  133. static inline struct sunxi_sram_desc *to_sram_desc(const struct sunxi_sram_data *data)
  134. {
  135. return container_of(data, struct sunxi_sram_desc, data);
  136. }
  137. static const struct sunxi_sram_data *sunxi_sram_of_parse(struct device_node *node,
  138. unsigned int *reg_value)
  139. {
  140. const struct of_device_id *match;
  141. const struct sunxi_sram_data *data;
  142. struct sunxi_sram_func *func;
  143. struct of_phandle_args args;
  144. u8 val;
  145. int ret;
  146. ret = of_parse_phandle_with_fixed_args(node, "allwinner,sram", 1, 0,
  147. &args);
  148. if (ret)
  149. return ERR_PTR(ret);
  150. if (!of_device_is_available(args.np)) {
  151. ret = -EBUSY;
  152. goto err;
  153. }
  154. val = args.args[0];
  155. match = of_match_node(sunxi_sram_dt_ids, args.np);
  156. if (!match) {
  157. ret = -EINVAL;
  158. goto err;
  159. }
  160. data = match->data;
  161. if (!data) {
  162. ret = -EINVAL;
  163. goto err;
  164. };
  165. for (func = data->func; func->func; func++) {
  166. if (val == func->val) {
  167. if (reg_value)
  168. *reg_value = func->reg_val;
  169. break;
  170. }
  171. }
  172. if (!func->func) {
  173. ret = -EINVAL;
  174. goto err;
  175. }
  176. of_node_put(args.np);
  177. return match->data;
  178. err:
  179. of_node_put(args.np);
  180. return ERR_PTR(ret);
  181. }
  182. int sunxi_sram_claim(struct device *dev)
  183. {
  184. const struct sunxi_sram_data *sram_data;
  185. struct sunxi_sram_desc *sram_desc;
  186. unsigned int device;
  187. u32 val, mask;
  188. if (IS_ERR(base))
  189. return PTR_ERR(base);
  190. if (!base)
  191. return -EPROBE_DEFER;
  192. if (!dev || !dev->of_node)
  193. return -EINVAL;
  194. sram_data = sunxi_sram_of_parse(dev->of_node, &device);
  195. if (IS_ERR(sram_data))
  196. return PTR_ERR(sram_data);
  197. sram_desc = to_sram_desc(sram_data);
  198. spin_lock(&sram_lock);
  199. if (sram_desc->claimed) {
  200. spin_unlock(&sram_lock);
  201. return -EBUSY;
  202. }
  203. mask = GENMASK(sram_data->offset + sram_data->width - 1,
  204. sram_data->offset);
  205. val = readl(base + sram_data->reg);
  206. val &= ~mask;
  207. writel(val | ((device << sram_data->offset) & mask),
  208. base + sram_data->reg);
  209. spin_unlock(&sram_lock);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(sunxi_sram_claim);
  213. int sunxi_sram_release(struct device *dev)
  214. {
  215. const struct sunxi_sram_data *sram_data;
  216. struct sunxi_sram_desc *sram_desc;
  217. if (!dev || !dev->of_node)
  218. return -EINVAL;
  219. sram_data = sunxi_sram_of_parse(dev->of_node, NULL);
  220. if (IS_ERR(sram_data))
  221. return -EINVAL;
  222. sram_desc = to_sram_desc(sram_data);
  223. spin_lock(&sram_lock);
  224. sram_desc->claimed = false;
  225. spin_unlock(&sram_lock);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(sunxi_sram_release);
  229. static int sunxi_sram_probe(struct platform_device *pdev)
  230. {
  231. struct resource *res;
  232. struct dentry *d;
  233. sram_dev = &pdev->dev;
  234. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. base = devm_ioremap_resource(&pdev->dev, res);
  236. if (IS_ERR(base))
  237. return PTR_ERR(base);
  238. of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  239. d = debugfs_create_file("sram", S_IRUGO, NULL, NULL,
  240. &sunxi_sram_fops);
  241. if (!d)
  242. return -ENOMEM;
  243. return 0;
  244. }
  245. static const struct of_device_id sunxi_sram_dt_match[] = {
  246. { .compatible = "allwinner,sun4i-a10-sram-controller" },
  247. { .compatible = "allwinner,sun50i-a64-sram-controller" },
  248. { },
  249. };
  250. MODULE_DEVICE_TABLE(of, sunxi_sram_dt_match);
  251. static struct platform_driver sunxi_sram_driver = {
  252. .driver = {
  253. .name = "sunxi-sram",
  254. .of_match_table = sunxi_sram_dt_match,
  255. },
  256. .probe = sunxi_sram_probe,
  257. };
  258. module_platform_driver(sunxi_sram_driver);
  259. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  260. MODULE_DESCRIPTION("Allwinner sunXi SRAM Controller Driver");
  261. MODULE_LICENSE("GPL");