xref: /linux/drivers/net/ethernet/freescale/fman/fman_muram.c (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
2 /*
3  * Copyright 2008 - 2015 Freescale Semiconductor Inc.
4  */
5 
6 #include "fman_muram.h"
7 
8 #include <linux/io.h>
9 #include <linux/slab.h>
10 #include <linux/genalloc.h>
11 
12 struct muram_info {
13 	struct gen_pool *pool;
14 	void __iomem *vbase;
15 	phys_addr_t pbase;
16 };
17 
18 static unsigned long fman_muram_vbase_to_offset(struct muram_info *muram,
19 						unsigned long vaddr)
20 {
21 	return vaddr - (unsigned long)muram->vbase;
22 }
23 
24 /**
25  * fman_muram_init
26  * @base:	Pointer to base of memory mapped FM-MURAM.
27  * @size:	Size of the FM-MURAM partition.
28  *
29  * Creates partition in the MURAM.
30  * The routine returns a pointer to the MURAM partition.
31  * This pointer must be passed as to all other FM-MURAM function calls.
32  * No actual initialization or configuration of FM_MURAM hardware is done by
33  * this routine.
34  *
35  * Return: pointer to FM-MURAM object, or NULL for Failure.
36  */
37 struct muram_info *fman_muram_init(phys_addr_t base, size_t size)
38 {
39 	struct muram_info *muram;
40 	void __iomem *vaddr;
41 	int ret;
42 
43 	muram = kzalloc(sizeof(*muram), GFP_KERNEL);
44 	if (!muram)
45 		return NULL;
46 
47 	muram->pool = gen_pool_create(ilog2(64), -1);
48 	if (!muram->pool) {
49 		pr_err("%s(): MURAM pool create failed\n", __func__);
50 		goto  muram_free;
51 	}
52 
53 	vaddr = ioremap(base, size);
54 	if (!vaddr) {
55 		pr_err("%s(): MURAM ioremap failed\n", __func__);
56 		goto pool_destroy;
57 	}
58 
59 	ret = gen_pool_add_virt(muram->pool, (unsigned long)vaddr,
60 				base, size, -1);
61 	if (ret < 0) {
62 		pr_err("%s(): MURAM pool add failed\n", __func__);
63 		iounmap(vaddr);
64 		goto pool_destroy;
65 	}
66 
67 	memset_io(vaddr, 0, (int)size);
68 
69 	muram->vbase = vaddr;
70 	muram->pbase = base;
71 	return muram;
72 
73 pool_destroy:
74 	gen_pool_destroy(muram->pool);
75 muram_free:
76 	kfree(muram);
77 	return NULL;
78 }
79 
80 /**
81  * fman_muram_offset_to_vbase
82  * @muram:	FM-MURAM module pointer.
83  * @offset:	the offset of the memory block
84  *
85  * Gives the address of the memory region from specific offset
86  *
87  * Return: The address of the memory block
88  */
89 unsigned long fman_muram_offset_to_vbase(struct muram_info *muram,
90 					 unsigned long offset)
91 {
92 	return offset + (unsigned long)muram->vbase;
93 }
94 
95 /**
96  * fman_muram_alloc
97  * @muram:	FM-MURAM module pointer.
98  * @size:	Size of the memory to be allocated.
99  *
100  * Allocate some memory from FM-MURAM partition.
101  *
102  * Return: address of the allocated memory; NULL otherwise.
103  */
104 unsigned long fman_muram_alloc(struct muram_info *muram, size_t size)
105 {
106 	unsigned long vaddr;
107 
108 	vaddr = gen_pool_alloc(muram->pool, size);
109 	if (!vaddr)
110 		return -ENOMEM;
111 
112 	memset_io((void __iomem *)vaddr, 0, size);
113 
114 	return fman_muram_vbase_to_offset(muram, vaddr);
115 }
116 
117 /**
118  * fman_muram_free_mem
119  * @muram:	FM-MURAM module pointer.
120  * @offset:	offset of the memory region to be freed.
121  * @size:	size of the memory to be freed.
122  *
123  * Free an allocated memory from FM-MURAM partition.
124  */
125 void fman_muram_free_mem(struct muram_info *muram, unsigned long offset,
126 			 size_t size)
127 {
128 	unsigned long addr = fman_muram_offset_to_vbase(muram, offset);
129 
130 	gen_pool_free(muram->pool, addr, size);
131 }
132