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