xref: /openbmc/linux/drivers/dma/bestcomm/sram.c (revision 77423a62)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Simple memory allocator for on-board SRAM
4  *
5  * Maintainer : Sylvain Munaut <tnt@246tNt.com>
6  *
7  * Copyright (C) 2005 Sylvain Munaut <tnt@246tNt.com>
8  */
9 
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/string.h>
16 #include <linux/ioport.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 
20 #include <asm/io.h>
21 #include <asm/mmu.h>
22 
23 #include <linux/fsl/bestcomm/sram.h>
24 
25 
26 /* Struct keeping our 'state' */
27 struct bcom_sram *bcom_sram = NULL;
28 EXPORT_SYMBOL_GPL(bcom_sram);	/* needed for inline functions */
29 
30 
31 /* ======================================================================== */
32 /* Public API                                                               */
33 /* ======================================================================== */
34 /* DO NOT USE in interrupts, if needed in irq handler, we should use the
35    _irqsave version of the spin_locks */
36 
37 int bcom_sram_init(struct device_node *sram_node, char *owner)
38 {
39 	int rv;
40 	const u32 *regaddr_p;
41 	u64 regaddr64, size64;
42 	unsigned int psize;
43 
44 	/* Create our state struct */
45 	if (bcom_sram) {
46 		printk(KERN_ERR "%s: bcom_sram_init: "
47 			"Already initialized !\n", owner);
48 		return -EBUSY;
49 	}
50 
51 	bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
52 	if (!bcom_sram) {
53 		printk(KERN_ERR "%s: bcom_sram_init: "
54 			"Couldn't allocate internal state !\n", owner);
55 		return -ENOMEM;
56 	}
57 
58 	/* Get address and size of the sram */
59 	regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
60 	if (!regaddr_p) {
61 		printk(KERN_ERR "%s: bcom_sram_init: "
62 			"Invalid device node !\n", owner);
63 		rv = -EINVAL;
64 		goto error_free;
65 	}
66 
67 	regaddr64 = of_translate_address(sram_node, regaddr_p);
68 
69 	bcom_sram->base_phys = (phys_addr_t) regaddr64;
70 	bcom_sram->size = (unsigned int) size64;
71 
72 	/* Request region */
73 	if (!request_mem_region(bcom_sram->base_phys, bcom_sram->size, owner)) {
74 		printk(KERN_ERR "%s: bcom_sram_init: "
75 			"Couldn't request region !\n", owner);
76 		rv = -EBUSY;
77 		goto error_free;
78 	}
79 
80 	/* Map SRAM */
81 		/* sram is not really __iomem */
82 	bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
83 
84 	if (!bcom_sram->base_virt) {
85 		printk(KERN_ERR "%s: bcom_sram_init: "
86 			"Map error SRAM zone 0x%08lx (0x%0x)!\n",
87 			owner, (long)bcom_sram->base_phys, bcom_sram->size );
88 		rv = -ENOMEM;
89 		goto error_release;
90 	}
91 
92 	/* Create an rheap (defaults to 32 bits word alignment) */
93 	bcom_sram->rh = rh_create(4);
94 
95 	/* Attach the free zones */
96 #if 0
97 	/* Currently disabled ... for future use only */
98 	reg_addr_p = of_get_property(sram_node, "available", &psize);
99 #else
100 	regaddr_p = NULL;
101 	psize = 0;
102 #endif
103 
104 	if (!regaddr_p || !psize) {
105 		/* Attach the whole zone */
106 		rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
107 	} else {
108 		/* Attach each zone independently */
109 		while (psize >= 2 * sizeof(u32)) {
110 			phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
111 			rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
112 			regaddr_p += 2;
113 			psize -= 2 * sizeof(u32);
114 		}
115 	}
116 
117 	/* Init our spinlock */
118 	spin_lock_init(&bcom_sram->lock);
119 
120 	return 0;
121 
122 error_release:
123 	release_mem_region(bcom_sram->base_phys, bcom_sram->size);
124 error_free:
125 	kfree(bcom_sram);
126 	bcom_sram = NULL;
127 
128 	return rv;
129 }
130 EXPORT_SYMBOL_GPL(bcom_sram_init);
131 
132 void bcom_sram_cleanup(void)
133 {
134 	/* Free resources */
135 	if (bcom_sram) {
136 		rh_destroy(bcom_sram->rh);
137 		iounmap((void __iomem *)bcom_sram->base_virt);
138 		release_mem_region(bcom_sram->base_phys, bcom_sram->size);
139 		kfree(bcom_sram);
140 		bcom_sram = NULL;
141 	}
142 }
143 EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
144 
145 void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
146 {
147 	unsigned long offset;
148 
149 	spin_lock(&bcom_sram->lock);
150 	offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
151 	spin_unlock(&bcom_sram->lock);
152 
153 	if (IS_ERR_VALUE(offset))
154 		return NULL;
155 
156 	*phys = bcom_sram->base_phys + offset;
157 	return bcom_sram->base_virt + offset;
158 }
159 EXPORT_SYMBOL_GPL(bcom_sram_alloc);
160 
161 void bcom_sram_free(void *ptr)
162 {
163 	unsigned long offset;
164 
165 	if (!ptr)
166 		return;
167 
168 	offset = ptr - bcom_sram->base_virt;
169 
170 	spin_lock(&bcom_sram->lock);
171 	rh_free(bcom_sram->rh, offset);
172 	spin_unlock(&bcom_sram->lock);
173 }
174 EXPORT_SYMBOL_GPL(bcom_sram_free);
175