x86: cpa self-test, WARN_ON()
[wrt350n-kernel.git] / arch / powerpc / sysdev / cpm_common.c
blob165981c87786bc2832e2f7bef8436a69663c88de
1 /*
2 * Common CPM code
4 * Author: Scott Wood <scottwood@freescale.com>
6 * Copyright 2007 Freescale Semiconductor, Inc.
8 * Some parts derived from commproc.c/cpm2_common.c, which is:
9 * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
10 * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
11 * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
12 * 2006 (c) MontaVista Software, Inc.
13 * Vitaly Bordug <vbordug@ru.mvista.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
20 #include <linux/init.h>
21 #include <linux/of_device.h>
23 #include <asm/udbg.h>
24 #include <asm/io.h>
25 #include <asm/system.h>
26 #include <asm/rheap.h>
27 #include <asm/cpm.h>
29 #include <mm/mmu_decl.h>
31 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
32 static u32 __iomem *cpm_udbg_txdesc =
33 (u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
35 static void udbg_putc_cpm(char c)
37 u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
39 if (c == '\n')
40 udbg_putc('\r');
42 while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
45 out_8(txbuf, c);
46 out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
49 void __init udbg_init_cpm(void)
51 if (cpm_udbg_txdesc) {
52 #ifdef CONFIG_CPM2
53 setbat(1, 0xf0000000, 0xf0000000, 1024*1024, _PAGE_IO);
54 #endif
55 udbg_putc = udbg_putc_cpm;
56 udbg_putc('X');
59 #endif
61 #ifdef CONFIG_PPC_CPM_NEW_BINDING
62 static spinlock_t cpm_muram_lock;
63 static rh_block_t cpm_boot_muram_rh_block[16];
64 static rh_info_t cpm_muram_info;
65 static u8 __iomem *muram_vbase;
66 static phys_addr_t muram_pbase;
68 /* Max address size we deal with */
69 #define OF_MAX_ADDR_CELLS 4
71 int __init cpm_muram_init(void)
73 struct device_node *np;
74 struct resource r;
75 u32 zero[OF_MAX_ADDR_CELLS] = {};
76 resource_size_t max = 0;
77 int i = 0;
78 int ret = 0;
80 spin_lock_init(&cpm_muram_lock);
81 /* initialize the info header */
82 rh_init(&cpm_muram_info, 1,
83 sizeof(cpm_boot_muram_rh_block) /
84 sizeof(cpm_boot_muram_rh_block[0]),
85 cpm_boot_muram_rh_block);
87 np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
88 if (!np) {
89 printk(KERN_ERR "Cannot find CPM muram data node");
90 ret = -ENODEV;
91 goto out;
94 muram_pbase = of_translate_address(np, zero);
95 if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
96 printk(KERN_ERR "Cannot translate zero through CPM muram node");
97 ret = -ENODEV;
98 goto out;
101 while (of_address_to_resource(np, i++, &r) == 0) {
102 if (r.end > max)
103 max = r.end;
105 rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
106 r.end - r.start + 1);
109 muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
110 if (!muram_vbase) {
111 printk(KERN_ERR "Cannot map CPM muram");
112 ret = -ENOMEM;
115 out:
116 of_node_put(np);
117 return ret;
121 * cpm_muram_alloc - allocate the requested size worth of multi-user ram
122 * @size: number of bytes to allocate
123 * @align: requested alignment, in bytes
125 * This function returns an offset into the muram area.
126 * Use cpm_dpram_addr() to get the virtual address of the area.
127 * Use cpm_muram_free() to free the allocation.
129 unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
131 unsigned long start;
132 unsigned long flags;
134 spin_lock_irqsave(&cpm_muram_lock, flags);
135 cpm_muram_info.alignment = align;
136 start = rh_alloc(&cpm_muram_info, size, "commproc");
137 spin_unlock_irqrestore(&cpm_muram_lock, flags);
139 return start;
141 EXPORT_SYMBOL(cpm_muram_alloc);
144 * cpm_muram_free - free a chunk of multi-user ram
145 * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
147 int cpm_muram_free(unsigned long offset)
149 int ret;
150 unsigned long flags;
152 spin_lock_irqsave(&cpm_muram_lock, flags);
153 ret = rh_free(&cpm_muram_info, offset);
154 spin_unlock_irqrestore(&cpm_muram_lock, flags);
156 return ret;
158 EXPORT_SYMBOL(cpm_muram_free);
161 * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
162 * @offset: the offset into the muram area to reserve
163 * @size: the number of bytes to reserve
165 * This function returns "start" on success, -ENOMEM on failure.
166 * Use cpm_dpram_addr() to get the virtual address of the area.
167 * Use cpm_muram_free() to free the allocation.
169 unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
171 unsigned long start;
172 unsigned long flags;
174 spin_lock_irqsave(&cpm_muram_lock, flags);
175 cpm_muram_info.alignment = 1;
176 start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
177 spin_unlock_irqrestore(&cpm_muram_lock, flags);
179 return start;
181 EXPORT_SYMBOL(cpm_muram_alloc_fixed);
184 * cpm_muram_addr - turn a muram offset into a virtual address
185 * @offset: muram offset to convert
187 void __iomem *cpm_muram_addr(unsigned long offset)
189 return muram_vbase + offset;
191 EXPORT_SYMBOL(cpm_muram_addr);
194 * cpm_muram_dma - turn a muram virtual address into a DMA address
195 * @offset: virtual address from cpm_muram_addr() to convert
197 dma_addr_t cpm_muram_dma(void __iomem *addr)
199 return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
201 EXPORT_SYMBOL(cpm_muram_dma);
203 #endif /* CONFIG_PPC_CPM_NEW_BINDING */