sunxi_sram.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. };
  24. struct sunxi_sram_data {
  25. char *name;
  26. u8 reg;
  27. u8 offset;
  28. u8 width;
  29. struct sunxi_sram_func *func;
  30. struct list_head list;
  31. };
  32. struct sunxi_sram_desc {
  33. struct sunxi_sram_data data;
  34. bool claimed;
  35. };
  36. #define SUNXI_SRAM_MAP(_val, _func) \
  37. { \
  38. .func = _func, \
  39. .val = _val, \
  40. }
  41. #define SUNXI_SRAM_DATA(_name, _reg, _off, _width, ...) \
  42. { \
  43. .name = _name, \
  44. .reg = _reg, \
  45. .offset = _off, \
  46. .width = _width, \
  47. .func = (struct sunxi_sram_func[]){ \
  48. __VA_ARGS__, { } }, \
  49. }
  50. static struct sunxi_sram_desc sun4i_a10_sram_a3_a4 = {
  51. .data = SUNXI_SRAM_DATA("A3-A4", 0x4, 0x4, 2,
  52. SUNXI_SRAM_MAP(0, "cpu"),
  53. SUNXI_SRAM_MAP(1, "emac")),
  54. };
  55. static struct sunxi_sram_desc sun4i_a10_sram_d = {
  56. .data = SUNXI_SRAM_DATA("D", 0x4, 0x0, 1,
  57. SUNXI_SRAM_MAP(0, "cpu"),
  58. SUNXI_SRAM_MAP(1, "usb-otg")),
  59. };
  60. static const struct of_device_id sunxi_sram_dt_ids[] = {
  61. {
  62. .compatible = "allwinner,sun4i-a10-sram-a3-a4",
  63. .data = &sun4i_a10_sram_a3_a4.data,
  64. },
  65. {
  66. .compatible = "allwinner,sun4i-a10-sram-d",
  67. .data = &sun4i_a10_sram_d.data,
  68. },
  69. {}
  70. };
  71. static struct device *sram_dev;
  72. static LIST_HEAD(claimed_sram);
  73. static DEFINE_SPINLOCK(sram_lock);
  74. static void __iomem *base;
  75. static int sunxi_sram_show(struct seq_file *s, void *data)
  76. {
  77. struct device_node *sram_node, *section_node;
  78. const struct sunxi_sram_data *sram_data;
  79. const struct of_device_id *match;
  80. struct sunxi_sram_func *func;
  81. const __be32 *sram_addr_p, *section_addr_p;
  82. u32 val;
  83. seq_puts(s, "Allwinner sunXi SRAM\n");
  84. seq_puts(s, "--------------------\n\n");
  85. for_each_child_of_node(sram_dev->of_node, sram_node) {
  86. sram_addr_p = of_get_address(sram_node, 0, NULL, NULL);
  87. seq_printf(s, "sram@%08x\n",
  88. be32_to_cpu(*sram_addr_p));
  89. for_each_child_of_node(sram_node, section_node) {
  90. match = of_match_node(sunxi_sram_dt_ids, section_node);
  91. if (!match)
  92. continue;
  93. sram_data = match->data;
  94. section_addr_p = of_get_address(section_node, 0,
  95. NULL, NULL);
  96. seq_printf(s, "\tsection@%04x\t(%s)\n",
  97. be32_to_cpu(*section_addr_p),
  98. sram_data->name);
  99. val = readl(base + sram_data->reg);
  100. val >>= sram_data->offset;
  101. val &= sram_data->width;
  102. for (func = sram_data->func; func->func; func++) {
  103. seq_printf(s, "\t\t%s%c\n", func->func,
  104. func->val == val ? '*' : ' ');
  105. }
  106. }
  107. seq_puts(s, "\n");
  108. }
  109. return 0;
  110. }
  111. static int sunxi_sram_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, sunxi_sram_show, inode->i_private);
  114. }
  115. static const struct file_operations sunxi_sram_fops = {
  116. .open = sunxi_sram_open,
  117. .read = seq_read,
  118. .llseek = seq_lseek,
  119. .release = single_release,
  120. };
  121. static inline struct sunxi_sram_desc *to_sram_desc(const struct sunxi_sram_data *data)
  122. {
  123. return container_of(data, struct sunxi_sram_desc, data);
  124. }
  125. static const struct sunxi_sram_data *sunxi_sram_of_parse(struct device_node *node,
  126. unsigned int *value)
  127. {
  128. const struct of_device_id *match;
  129. struct of_phandle_args args;
  130. int ret;
  131. ret = of_parse_phandle_with_fixed_args(node, "allwinner,sram", 1, 0,
  132. &args);
  133. if (ret)
  134. return ERR_PTR(ret);
  135. if (!of_device_is_available(args.np)) {
  136. ret = -EBUSY;
  137. goto err;
  138. }
  139. if (value)
  140. *value = args.args[0];
  141. match = of_match_node(sunxi_sram_dt_ids, args.np);
  142. if (!match) {
  143. ret = -EINVAL;
  144. goto err;
  145. }
  146. of_node_put(args.np);
  147. return match->data;
  148. err:
  149. of_node_put(args.np);
  150. return ERR_PTR(ret);
  151. }
  152. int sunxi_sram_claim(struct device *dev)
  153. {
  154. const struct sunxi_sram_data *sram_data;
  155. struct sunxi_sram_desc *sram_desc;
  156. unsigned int device;
  157. u32 val, mask;
  158. if (IS_ERR(base))
  159. return -EPROBE_DEFER;
  160. if (!dev || !dev->of_node)
  161. return -EINVAL;
  162. sram_data = sunxi_sram_of_parse(dev->of_node, &device);
  163. if (IS_ERR(sram_data))
  164. return PTR_ERR(sram_data);
  165. sram_desc = to_sram_desc(sram_data);
  166. spin_lock(&sram_lock);
  167. if (sram_desc->claimed) {
  168. spin_unlock(&sram_lock);
  169. return -EBUSY;
  170. }
  171. mask = GENMASK(sram_data->offset + sram_data->width, sram_data->offset);
  172. val = readl(base + sram_data->reg);
  173. val &= ~mask;
  174. writel(val | ((device << sram_data->offset) & mask),
  175. base + sram_data->reg);
  176. spin_unlock(&sram_lock);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(sunxi_sram_claim);
  180. int sunxi_sram_release(struct device *dev)
  181. {
  182. const struct sunxi_sram_data *sram_data;
  183. struct sunxi_sram_desc *sram_desc;
  184. if (!dev || !dev->of_node)
  185. return -EINVAL;
  186. sram_data = sunxi_sram_of_parse(dev->of_node, NULL);
  187. if (IS_ERR(sram_data))
  188. return -EINVAL;
  189. sram_desc = to_sram_desc(sram_data);
  190. spin_lock(&sram_lock);
  191. sram_desc->claimed = false;
  192. spin_unlock(&sram_lock);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(sunxi_sram_release);
  196. static int sunxi_sram_probe(struct platform_device *pdev)
  197. {
  198. struct resource *res;
  199. struct dentry *d;
  200. sram_dev = &pdev->dev;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. base = devm_ioremap_resource(&pdev->dev, res);
  203. if (IS_ERR(base))
  204. return PTR_ERR(base);
  205. of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  206. d = debugfs_create_file("sram", S_IRUGO, NULL, NULL,
  207. &sunxi_sram_fops);
  208. if (!d)
  209. return -ENOMEM;
  210. return 0;
  211. }
  212. static const struct of_device_id sunxi_sram_dt_match[] = {
  213. { .compatible = "allwinner,sun4i-a10-sram-controller" },
  214. { },
  215. };
  216. MODULE_DEVICE_TABLE(of, sunxi_sram_dt_match);
  217. static struct platform_driver sunxi_sram_driver = {
  218. .driver = {
  219. .name = "sunxi-sram",
  220. .of_match_table = sunxi_sram_dt_match,
  221. },
  222. .probe = sunxi_sram_probe,
  223. };
  224. module_platform_driver(sunxi_sram_driver);
  225. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  226. MODULE_DESCRIPTION("Allwinner sunXi SRAM Controller Driver");
  227. MODULE_LICENSE("GPL");