1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2008-2015 Freescale Semiconductor Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
5*4882a593Smuzhiyun * modification, are permitted provided that the following conditions are met:
6*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
7*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
8*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
9*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
10*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
11*4882a593Smuzhiyun * * Neither the name of Freescale Semiconductor nor the
12*4882a593Smuzhiyun * names of its contributors may be used to endorse or promote products
13*4882a593Smuzhiyun * derived from this software without specific prior written permission.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * ALTERNATIVELY, this software may be distributed under the terms of the
17*4882a593Smuzhiyun * GNU General Public License ("GPL") as published by the Free Software
18*4882a593Smuzhiyun * Foundation, either version 2 of that License or (at your option) any
19*4882a593Smuzhiyun * later version.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22*4882a593Smuzhiyun * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23*4882a593Smuzhiyun * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24*4882a593Smuzhiyun * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25*4882a593Smuzhiyun * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26*4882a593Smuzhiyun * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*4882a593Smuzhiyun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28*4882a593Smuzhiyun * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30*4882a593Smuzhiyun * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include "fman_muram.h"
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <linux/io.h>
36*4882a593Smuzhiyun #include <linux/slab.h>
37*4882a593Smuzhiyun #include <linux/genalloc.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct muram_info {
40*4882a593Smuzhiyun struct gen_pool *pool;
41*4882a593Smuzhiyun void __iomem *vbase;
42*4882a593Smuzhiyun size_t size;
43*4882a593Smuzhiyun phys_addr_t pbase;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun
fman_muram_vbase_to_offset(struct muram_info * muram,unsigned long vaddr)46*4882a593Smuzhiyun static unsigned long fman_muram_vbase_to_offset(struct muram_info *muram,
47*4882a593Smuzhiyun unsigned long vaddr)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun return vaddr - (unsigned long)muram->vbase;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun * fman_muram_init
54*4882a593Smuzhiyun * @base: Pointer to base of memory mapped FM-MURAM.
55*4882a593Smuzhiyun * @size: Size of the FM-MURAM partition.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * Creates partition in the MURAM.
58*4882a593Smuzhiyun * The routine returns a pointer to the MURAM partition.
59*4882a593Smuzhiyun * This pointer must be passed as to all other FM-MURAM function calls.
60*4882a593Smuzhiyun * No actual initialization or configuration of FM_MURAM hardware is done by
61*4882a593Smuzhiyun * this routine.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Return: pointer to FM-MURAM object, or NULL for Failure.
64*4882a593Smuzhiyun */
fman_muram_init(phys_addr_t base,size_t size)65*4882a593Smuzhiyun struct muram_info *fman_muram_init(phys_addr_t base, size_t size)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct muram_info *muram;
68*4882a593Smuzhiyun void __iomem *vaddr;
69*4882a593Smuzhiyun int ret;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun muram = kzalloc(sizeof(*muram), GFP_KERNEL);
72*4882a593Smuzhiyun if (!muram)
73*4882a593Smuzhiyun return NULL;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun muram->pool = gen_pool_create(ilog2(64), -1);
76*4882a593Smuzhiyun if (!muram->pool) {
77*4882a593Smuzhiyun pr_err("%s(): MURAM pool create failed\n", __func__);
78*4882a593Smuzhiyun goto muram_free;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun vaddr = ioremap(base, size);
82*4882a593Smuzhiyun if (!vaddr) {
83*4882a593Smuzhiyun pr_err("%s(): MURAM ioremap failed\n", __func__);
84*4882a593Smuzhiyun goto pool_destroy;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun ret = gen_pool_add_virt(muram->pool, (unsigned long)vaddr,
88*4882a593Smuzhiyun base, size, -1);
89*4882a593Smuzhiyun if (ret < 0) {
90*4882a593Smuzhiyun pr_err("%s(): MURAM pool add failed\n", __func__);
91*4882a593Smuzhiyun iounmap(vaddr);
92*4882a593Smuzhiyun goto pool_destroy;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun memset_io(vaddr, 0, (int)size);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun muram->vbase = vaddr;
98*4882a593Smuzhiyun muram->pbase = base;
99*4882a593Smuzhiyun return muram;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun pool_destroy:
102*4882a593Smuzhiyun gen_pool_destroy(muram->pool);
103*4882a593Smuzhiyun muram_free:
104*4882a593Smuzhiyun kfree(muram);
105*4882a593Smuzhiyun return NULL;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * fman_muram_offset_to_vbase
110*4882a593Smuzhiyun * @muram: FM-MURAM module pointer.
111*4882a593Smuzhiyun * @offset: the offset of the memory block
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * Gives the address of the memory region from specific offset
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * Return: The address of the memory block
116*4882a593Smuzhiyun */
fman_muram_offset_to_vbase(struct muram_info * muram,unsigned long offset)117*4882a593Smuzhiyun unsigned long fman_muram_offset_to_vbase(struct muram_info *muram,
118*4882a593Smuzhiyun unsigned long offset)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun return offset + (unsigned long)muram->vbase;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun * fman_muram_alloc
125*4882a593Smuzhiyun * @muram: FM-MURAM module pointer.
126*4882a593Smuzhiyun * @size: Size of the memory to be allocated.
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * Allocate some memory from FM-MURAM partition.
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * Return: address of the allocated memory; NULL otherwise.
131*4882a593Smuzhiyun */
fman_muram_alloc(struct muram_info * muram,size_t size)132*4882a593Smuzhiyun unsigned long fman_muram_alloc(struct muram_info *muram, size_t size)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun unsigned long vaddr;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun vaddr = gen_pool_alloc(muram->pool, size);
137*4882a593Smuzhiyun if (!vaddr)
138*4882a593Smuzhiyun return -ENOMEM;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun memset_io((void __iomem *)vaddr, 0, size);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun return fman_muram_vbase_to_offset(muram, vaddr);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * fman_muram_free_mem
147*4882a593Smuzhiyun * @muram: FM-MURAM module pointer.
148*4882a593Smuzhiyun * @offset: offset of the memory region to be freed.
149*4882a593Smuzhiyun * @size: size of the memory to be freed.
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * Free an allocated memory from FM-MURAM partition.
152*4882a593Smuzhiyun */
fman_muram_free_mem(struct muram_info * muram,unsigned long offset,size_t size)153*4882a593Smuzhiyun void fman_muram_free_mem(struct muram_info *muram, unsigned long offset,
154*4882a593Smuzhiyun size_t size)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun unsigned long addr = fman_muram_offset_to_vbase(muram, offset);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun gen_pool_free(muram->pool, addr, size);
159*4882a593Smuzhiyun }
160