2 * Allwinner SoCs SRAM Controller Driver
4 * Copyright (C) 2015 Maxime Ripard
6 * Author: Maxime Ripard <maxime.ripard@free-electrons.com>
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.
13 #include <linux/debugfs.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
21 #include <linux/soc/sunxi/sunxi_sram.h>
23 struct sunxi_sram_func {
29 struct sunxi_sram_data {
34 struct sunxi_sram_func *func;
35 struct list_head list;
38 struct sunxi_sram_desc {
39 struct sunxi_sram_data data;
43 #define SUNXI_SRAM_MAP(_reg_val, _val, _func) \
47 .reg_val = _reg_val, \
50 #define SUNXI_SRAM_DATA(_name, _reg, _off, _width, ...) \
56 .func = (struct sunxi_sram_func[]){ \
60 static struct sunxi_sram_desc sun4i_a10_sram_a3_a4 = {
61 .data = SUNXI_SRAM_DATA("A3-A4", 0x4, 0x4, 2,
62 SUNXI_SRAM_MAP(0, 0, "cpu"),
63 SUNXI_SRAM_MAP(1, 1, "emac")),
66 static struct sunxi_sram_desc sun4i_a10_sram_d = {
67 .data = SUNXI_SRAM_DATA("D", 0x4, 0x0, 1,
68 SUNXI_SRAM_MAP(0, 0, "cpu"),
69 SUNXI_SRAM_MAP(1, 1, "usb-otg")),
72 static struct sunxi_sram_desc sun50i_a64_sram_c = {
73 .data = SUNXI_SRAM_DATA("C", 0x4, 24, 1,
74 SUNXI_SRAM_MAP(0, 1, "cpu"),
75 SUNXI_SRAM_MAP(1, 0, "de2")),
78 static const struct of_device_id sunxi_sram_dt_ids[] = {
80 .compatible = "allwinner,sun4i-a10-sram-a3-a4",
81 .data = &sun4i_a10_sram_a3_a4.data,
84 .compatible = "allwinner,sun4i-a10-sram-d",
85 .data = &sun4i_a10_sram_d.data,
88 .compatible = "allwinner,sun50i-a64-sram-c",
89 .data = &sun50i_a64_sram_c.data,
94 static struct device *sram_dev;
95 static LIST_HEAD(claimed_sram);
96 static DEFINE_SPINLOCK(sram_lock);
97 static void __iomem *base;
99 static int sunxi_sram_show(struct seq_file *s, void *data)
101 struct device_node *sram_node, *section_node;
102 const struct sunxi_sram_data *sram_data;
103 const struct of_device_id *match;
104 struct sunxi_sram_func *func;
105 const __be32 *sram_addr_p, *section_addr_p;
108 seq_puts(s, "Allwinner sunXi SRAM\n");
109 seq_puts(s, "--------------------\n\n");
111 for_each_child_of_node(sram_dev->of_node, sram_node) {
112 sram_addr_p = of_get_address(sram_node, 0, NULL, NULL);
114 seq_printf(s, "sram@%08x\n",
115 be32_to_cpu(*sram_addr_p));
117 for_each_child_of_node(sram_node, section_node) {
118 match = of_match_node(sunxi_sram_dt_ids, section_node);
121 sram_data = match->data;
123 section_addr_p = of_get_address(section_node, 0,
126 seq_printf(s, "\tsection@%04x\t(%s)\n",
127 be32_to_cpu(*section_addr_p),
130 val = readl(base + sram_data->reg);
131 val >>= sram_data->offset;
132 val &= GENMASK(sram_data->width - 1, 0);
134 for (func = sram_data->func; func->func; func++) {
135 seq_printf(s, "\t\t%s%c\n", func->func,
136 func->reg_val == val ?
147 static int sunxi_sram_open(struct inode *inode, struct file *file)
149 return single_open(file, sunxi_sram_show, inode->i_private);
152 static const struct file_operations sunxi_sram_fops = {
153 .open = sunxi_sram_open,
156 .release = single_release,
159 static inline struct sunxi_sram_desc *to_sram_desc(const struct sunxi_sram_data *data)
161 return container_of(data, struct sunxi_sram_desc, data);
164 static const struct sunxi_sram_data *sunxi_sram_of_parse(struct device_node *node,
165 unsigned int *reg_value)
167 const struct of_device_id *match;
168 const struct sunxi_sram_data *data;
169 struct sunxi_sram_func *func;
170 struct of_phandle_args args;
174 ret = of_parse_phandle_with_fixed_args(node, "allwinner,sram", 1, 0,
179 if (!of_device_is_available(args.np)) {
186 match = of_match_node(sunxi_sram_dt_ids, args.np);
198 for (func = data->func; func->func; func++) {
199 if (val == func->val) {
201 *reg_value = func->reg_val;
212 of_node_put(args.np);
216 of_node_put(args.np);
220 int sunxi_sram_claim(struct device *dev)
222 const struct sunxi_sram_data *sram_data;
223 struct sunxi_sram_desc *sram_desc;
228 return PTR_ERR(base);
231 return -EPROBE_DEFER;
233 if (!dev || !dev->of_node)
236 sram_data = sunxi_sram_of_parse(dev->of_node, &device);
237 if (IS_ERR(sram_data))
238 return PTR_ERR(sram_data);
240 sram_desc = to_sram_desc(sram_data);
242 spin_lock(&sram_lock);
244 if (sram_desc->claimed) {
245 spin_unlock(&sram_lock);
249 mask = GENMASK(sram_data->offset + sram_data->width - 1,
251 val = readl(base + sram_data->reg);
253 writel(val | ((device << sram_data->offset) & mask),
254 base + sram_data->reg);
256 spin_unlock(&sram_lock);
260 EXPORT_SYMBOL(sunxi_sram_claim);
262 int sunxi_sram_release(struct device *dev)
264 const struct sunxi_sram_data *sram_data;
265 struct sunxi_sram_desc *sram_desc;
267 if (!dev || !dev->of_node)
270 sram_data = sunxi_sram_of_parse(dev->of_node, NULL);
271 if (IS_ERR(sram_data))
274 sram_desc = to_sram_desc(sram_data);
276 spin_lock(&sram_lock);
277 sram_desc->claimed = false;
278 spin_unlock(&sram_lock);
282 EXPORT_SYMBOL(sunxi_sram_release);
284 static int sunxi_sram_probe(struct platform_device *pdev)
286 struct resource *res;
289 sram_dev = &pdev->dev;
291 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 base = devm_ioremap_resource(&pdev->dev, res);
294 return PTR_ERR(base);
296 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
298 d = debugfs_create_file("sram", S_IRUGO, NULL, NULL,
306 static const struct of_device_id sunxi_sram_dt_match[] = {
307 { .compatible = "allwinner,sun4i-a10-sram-controller" },
308 { .compatible = "allwinner,sun50i-a64-sram-controller" },
311 MODULE_DEVICE_TABLE(of, sunxi_sram_dt_match);
313 static struct platform_driver sunxi_sram_driver = {
315 .name = "sunxi-sram",
316 .of_match_table = sunxi_sram_dt_match,
318 .probe = sunxi_sram_probe,
320 module_platform_driver(sunxi_sram_driver);
322 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
323 MODULE_DESCRIPTION("Allwinner sunXi SRAM Controller Driver");
324 MODULE_LICENSE("GPL");