]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/sh/mm/ioremap_64.c
sh: use printk_once
[net-next-2.6.git] / arch / sh / mm / ioremap_64.c
CommitLineData
1da177e4 1/*
249cfea9 2 * arch/sh/mm/ioremap_64.c
1da177e4
LT
3 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
249cfea9 5 * Copyright (C) 2003 - 2007 Paul Mundt
1da177e4
LT
6 *
7 * Mostly derived from arch/sh/mm/ioremap.c which, in turn is mostly
8 * derived from arch/i386/mm/ioremap.c .
9 *
10 * (C) Copyright 1995 1996 Linus Torvalds
249cfea9
PM
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
14 * for more details.
1da177e4 15 */
1da177e4 16#include <linux/vmalloc.h>
1da177e4 17#include <linux/ioport.h>
749c8496 18#include <linux/module.h>
ad81eb91
PM
19#include <linux/mm.h>
20#include <linux/io.h>
249cfea9 21#include <linux/bootmem.h>
2b6a8d45 22#include <linux/proc_fs.h>
8fc40238 23#include <linux/slab.h>
ad81eb91 24#include <asm/page.h>
749c8496 25#include <asm/pgalloc.h>
ad81eb91
PM
26#include <asm/addrspace.h>
27#include <asm/cacheflush.h>
749c8496 28#include <asm/tlbflush.h>
ad81eb91 29#include <asm/mmu.h>
1da177e4 30
1da177e4
LT
31static struct resource shmedia_iomap = {
32 .name = "shmedia_iomap",
33 .start = IOBASE_VADDR + PAGE_SIZE,
34 .end = IOBASE_END - 1,
35};
36
0fb849b9
PM
37static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
38 unsigned long flags);
1da177e4 39static void shmedia_unmapioaddr(unsigned long vaddr);
0fb849b9
PM
40static void __iomem *shmedia_ioremap(struct resource *res, u32 pa,
41 int sz, unsigned long flags);
1da177e4
LT
42
43/*
44 * We have the same problem as the SPARC, so lets have the same comment:
45 * Our mini-allocator...
46 * Boy this is gross! We need it because we must map I/O for
47 * timers and interrupt controller before the kmalloc is available.
48 */
49
50#define XNMLN 15
51#define XNRES 10
52
53struct xresource {
54 struct resource xres; /* Must be first */
55 int xflag; /* 1 == used */
56 char xname[XNMLN+1];
57};
58
59static struct xresource xresv[XNRES];
60
61static struct xresource *xres_alloc(void)
62{
0fb849b9
PM
63 struct xresource *xrp;
64 int n;
65
66 xrp = xresv;
67 for (n = 0; n < XNRES; n++) {
68 if (xrp->xflag == 0) {
69 xrp->xflag = 1;
70 return xrp;
71 }
72 xrp++;
73 }
74 return NULL;
1da177e4
LT
75}
76
77static void xres_free(struct xresource *xrp)
78{
79 xrp->xflag = 0;
80}
81
82static struct resource *shmedia_find_resource(struct resource *root,
83 unsigned long vaddr)
84{
85 struct resource *res;
86
87 for (res = root->child; res; res = res->sibling)
88 if (res->start <= vaddr && res->end >= vaddr)
89 return res;
90
91 return NULL;
92}
93
0fb849b9
PM
94static void __iomem *shmedia_alloc_io(unsigned long phys, unsigned long size,
95 const char *name, unsigned long flags)
1da177e4 96{
0fb849b9
PM
97 struct xresource *xres;
98 struct resource *res;
99 char *tack;
100 int tlen;
101
102 if (name == NULL)
103 name = "???";
104
105 xres = xres_alloc();
106 if (xres != 0) {
107 tack = xres->xname;
108 res = &xres->xres;
109 } else {
922b0dc5 110 printk_once(KERN_NOTICE "%s: done with statics, "
0fb849b9 111 "switching to kmalloc\n", __func__);
0fb849b9
PM
112 tlen = strlen(name);
113 tack = kmalloc(sizeof(struct resource) + tlen + 1, GFP_KERNEL);
114 if (!tack)
115 return NULL;
116 memset(tack, 0, sizeof(struct resource));
117 res = (struct resource *) tack;
118 tack += sizeof(struct resource);
119 }
120
121 strncpy(tack, name, XNMLN);
122 tack[XNMLN] = 0;
123 res->name = tack;
124
125 return shmedia_ioremap(res, phys, size, flags);
1da177e4
LT
126}
127
0fb849b9
PM
128static void __iomem *shmedia_ioremap(struct resource *res, u32 pa, int sz,
129 unsigned long flags)
1da177e4 130{
0fb849b9 131 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
1da177e4 132 unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
0fb849b9
PM
133 unsigned long va;
134 unsigned int psz;
1da177e4 135
0fb849b9 136 if (allocate_resource(&shmedia_iomap, res, round_sz,
1da177e4
LT
137 shmedia_iomap.start, shmedia_iomap.end,
138 PAGE_SIZE, NULL, NULL) != 0) {
0fb849b9
PM
139 panic("alloc_io_res(%s): cannot occupy\n",
140 (res->name != NULL) ? res->name : "???");
141 }
1da177e4 142
0fb849b9
PM
143 va = res->start;
144 pa &= PAGE_MASK;
1da177e4
LT
145
146 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
147
0fb849b9
PM
148 for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
149 shmedia_mapioaddr(pa, va, flags);
150 va += PAGE_SIZE;
151 pa += PAGE_SIZE;
152 }
1da177e4 153
0fb849b9 154 return (void __iomem *)(unsigned long)(res->start + offset);
1da177e4
LT
155}
156
157static void shmedia_free_io(struct resource *res)
158{
159 unsigned long len = res->end - res->start + 1;
160
161 BUG_ON((len & (PAGE_SIZE - 1)) != 0);
162
163 while (len) {
164 len -= PAGE_SIZE;
165 shmedia_unmapioaddr(res->start + len);
166 }
167
168 release_resource(res);
169}
170
fad9e7d9 171static __init_refok void *sh64_get_page(void)
1da177e4 172{
1da177e4
LT
173 void *page;
174
8fc40238 175 if (slab_is_available())
0fb849b9
PM
176 page = (void *)get_zeroed_page(GFP_KERNEL);
177 else
1da177e4 178 page = alloc_bootmem_pages(PAGE_SIZE);
1da177e4
LT
179
180 if (!page || ((unsigned long)page & ~PAGE_MASK))
181 panic("sh64_get_page: Out of memory already?\n");
182
183 return page;
184}
185
0fb849b9
PM
186static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
187 unsigned long flags)
1da177e4
LT
188{
189 pgd_t *pgdp;
249cfea9 190 pud_t *pudp;
1da177e4
LT
191 pmd_t *pmdp;
192 pte_t *ptep, pte;
193 pgprot_t prot;
1da177e4
LT
194
195 pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
196
0fb849b9
PM
197 if (!flags)
198 flags = 1; /* 1 = CB0-1 device */
199
1da177e4
LT
200 pgdp = pgd_offset_k(va);
201 if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
249cfea9
PM
202 pudp = (pud_t *)sh64_get_page();
203 set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
204 }
205
206 pudp = pud_offset(pgdp, va);
207 if (pud_none(*pudp) || !pud_present(*pudp)) {
1da177e4 208 pmdp = (pmd_t *)sh64_get_page();
249cfea9 209 set_pud(pudp, __pud((unsigned long)pmdp | _KERNPG_TABLE));
1da177e4
LT
210 }
211
249cfea9 212 pmdp = pmd_offset(pudp, va);
0fb849b9 213 if (pmd_none(*pmdp) || !pmd_present(*pmdp)) {
1da177e4
LT
214 ptep = (pte_t *)sh64_get_page();
215 set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
216 }
217
218 prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
219 _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
220
221 pte = pfn_pte(pa >> PAGE_SHIFT, prot);
222 ptep = pte_offset_kernel(pmdp, va);
223
224 if (!pte_none(*ptep) &&
225 pte_val(*ptep) != pte_val(pte))
226 pte_ERROR(*ptep);
227
228 set_pte(ptep, pte);
229
230 flush_tlb_kernel_range(va, PAGE_SIZE);
231}
232
233static void shmedia_unmapioaddr(unsigned long vaddr)
234{
235 pgd_t *pgdp;
249cfea9 236 pud_t *pudp;
1da177e4
LT
237 pmd_t *pmdp;
238 pte_t *ptep;
239
240 pgdp = pgd_offset_k(vaddr);
249cfea9
PM
241 if (pgd_none(*pgdp) || pgd_bad(*pgdp))
242 return;
243
244 pudp = pud_offset(pgdp, vaddr);
245 if (pud_none(*pudp) || pud_bad(*pudp))
246 return;
1da177e4 247
249cfea9 248 pmdp = pmd_offset(pudp, vaddr);
1da177e4
LT
249 if (pmd_none(*pmdp) || pmd_bad(*pmdp))
250 return;
251
252 ptep = pte_offset_kernel(pmdp, vaddr);
253
254 if (pte_none(*ptep) || !pte_present(*ptep))
255 return;
256
257 clear_page((void *)ptep);
258 pte_clear(&init_mm, vaddr, ptep);
259}
260
0fb849b9
PM
261void __iomem *__ioremap(unsigned long offset, unsigned long size,
262 unsigned long flags)
1da177e4 263{
0fb849b9 264 char name[14];
1da177e4 265
0fb849b9
PM
266 sprintf(name, "phys_%08x", (u32)offset);
267 return shmedia_alloc_io(offset, size, name, flags);
1da177e4 268}
0fb849b9 269EXPORT_SYMBOL(__ioremap);
1da177e4 270
0fb849b9 271void __iounmap(void __iomem *virtual)
1da177e4 272{
0fb849b9 273 unsigned long vaddr = (unsigned long)virtual & PAGE_MASK;
1da177e4
LT
274 struct resource *res;
275 unsigned int psz;
276
277 res = shmedia_find_resource(&shmedia_iomap, vaddr);
278 if (!res) {
279 printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
866e6b9e 280 __func__, vaddr);
1da177e4
LT
281 return;
282 }
283
0fb849b9 284 psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
1da177e4
LT
285
286 shmedia_free_io(res);
287
288 if ((char *)res >= (char *)xresv &&
289 (char *)res < (char *)&xresv[XNRES]) {
290 xres_free((struct xresource *)res);
291 } else {
292 kfree(res);
293 }
294}
0fb849b9 295EXPORT_SYMBOL(__iounmap);
1da177e4 296
1da177e4
LT
297static int
298ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
299 void *data)
300{
301 char *p = buf, *e = buf + length;
302 struct resource *r;
303 const char *nm;
304
305 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
306 if (p + 32 >= e) /* Better than nothing */
307 break;
0fb849b9
PM
308 nm = r->name;
309 if (nm == NULL)
310 nm = "???";
311
21264136
PM
312 p += sprintf(p, "%08lx-%08lx: %s\n",
313 (unsigned long)r->start,
314 (unsigned long)r->end, nm);
1da177e4
LT
315 }
316
317 return p-buf;
318}
1da177e4
LT
319
320static int __init register_proc_onchip(void)
321{
0fb849b9
PM
322 create_proc_read_entry("io_map", 0, 0, ioremap_proc_info,
323 &shmedia_iomap);
1da177e4
LT
324 return 0;
325}
0fb849b9 326late_initcall(register_proc_onchip);