blob: 9b75d164bdf96d9b2abc6323e2839aab9900572e [file] [log] [blame]
Scott Woodc374e002007-07-16 11:43:43 -05001/*
2 * Common CPM code
3 *
4 * Author: Scott Wood <scottwood@freescale.com>
5 *
6 * Copyright 2007 Freescale Semiconductor, Inc.
7 *
Scott Wood15f8c602007-09-28 14:06:16 -05008 * 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>
14 *
Scott Woodc374e002007-07-16 11:43:43 -050015 * 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.
18 */
19
20#include <linux/init.h>
Scott Wood15f8c602007-09-28 14:06:16 -050021#include <linux/of_device.h>
22
Scott Woodc374e002007-07-16 11:43:43 -050023#include <asm/udbg.h>
24#include <asm/io.h>
25#include <asm/system.h>
Scott Wood15f8c602007-09-28 14:06:16 -050026#include <asm/rheap.h>
27#include <asm/cpm.h>
28
Scott Woodc374e002007-07-16 11:43:43 -050029#include <mm/mmu_decl.h>
30
31#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
32static u32 __iomem *cpm_udbg_txdesc =
33 (u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
34
35static void udbg_putc_cpm(char c)
36{
37 u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
38
39 if (c == '\n')
40 udbg_putc('\r');
41
42 while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
43 ;
44
45 out_8(txbuf, c);
46 out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
47}
48
49void __init udbg_init_cpm(void)
50{
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;
Scott Wood15f8c602007-09-28 14:06:16 -050056 udbg_putc('X');
Scott Woodc374e002007-07-16 11:43:43 -050057 }
58}
59#endif
Scott Wood15f8c602007-09-28 14:06:16 -050060
Scott Wood15f8c602007-09-28 14:06:16 -050061static spinlock_t cpm_muram_lock;
62static rh_block_t cpm_boot_muram_rh_block[16];
63static rh_info_t cpm_muram_info;
64static u8 __iomem *muram_vbase;
65static phys_addr_t muram_pbase;
66
67/* Max address size we deal with */
68#define OF_MAX_ADDR_CELLS 4
69
70int __init cpm_muram_init(void)
71{
72 struct device_node *np;
73 struct resource r;
74 u32 zero[OF_MAX_ADDR_CELLS] = {};
75 resource_size_t max = 0;
76 int i = 0;
77 int ret = 0;
78
Scott Wood15f8c602007-09-28 14:06:16 -050079 spin_lock_init(&cpm_muram_lock);
80 /* initialize the info header */
81 rh_init(&cpm_muram_info, 1,
82 sizeof(cpm_boot_muram_rh_block) /
83 sizeof(cpm_boot_muram_rh_block[0]),
84 cpm_boot_muram_rh_block);
85
86 np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
87 if (!np) {
Anton Vorontsov5093bb92008-05-23 20:39:06 +040088 /* try legacy bindings */
89 np = of_find_node_by_name(NULL, "data-only");
90 if (!np) {
91 printk(KERN_ERR "Cannot find CPM muram data node");
92 ret = -ENODEV;
93 goto out;
94 }
Scott Wood15f8c602007-09-28 14:06:16 -050095 }
96
97 muram_pbase = of_translate_address(np, zero);
98 if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
99 printk(KERN_ERR "Cannot translate zero through CPM muram node");
100 ret = -ENODEV;
101 goto out;
102 }
103
104 while (of_address_to_resource(np, i++, &r) == 0) {
105 if (r.end > max)
106 max = r.end;
107
108 rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
109 r.end - r.start + 1);
110 }
111
112 muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
113 if (!muram_vbase) {
114 printk(KERN_ERR "Cannot map CPM muram");
115 ret = -ENOMEM;
116 }
117
118out:
119 of_node_put(np);
120 return ret;
121}
122
123/**
124 * cpm_muram_alloc - allocate the requested size worth of multi-user ram
125 * @size: number of bytes to allocate
126 * @align: requested alignment, in bytes
127 *
128 * This function returns an offset into the muram area.
129 * Use cpm_dpram_addr() to get the virtual address of the area.
130 * Use cpm_muram_free() to free the allocation.
131 */
132unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
133{
134 unsigned long start;
135 unsigned long flags;
136
137 spin_lock_irqsave(&cpm_muram_lock, flags);
138 cpm_muram_info.alignment = align;
139 start = rh_alloc(&cpm_muram_info, size, "commproc");
140 spin_unlock_irqrestore(&cpm_muram_lock, flags);
141
142 return start;
143}
144EXPORT_SYMBOL(cpm_muram_alloc);
145
146/**
147 * cpm_muram_free - free a chunk of multi-user ram
148 * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
149 */
150int cpm_muram_free(unsigned long offset)
151{
152 int ret;
153 unsigned long flags;
154
155 spin_lock_irqsave(&cpm_muram_lock, flags);
156 ret = rh_free(&cpm_muram_info, offset);
157 spin_unlock_irqrestore(&cpm_muram_lock, flags);
158
159 return ret;
160}
161EXPORT_SYMBOL(cpm_muram_free);
162
163/**
164 * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
165 * @offset: the offset into the muram area to reserve
166 * @size: the number of bytes to reserve
167 *
168 * This function returns "start" on success, -ENOMEM on failure.
169 * Use cpm_dpram_addr() to get the virtual address of the area.
170 * Use cpm_muram_free() to free the allocation.
171 */
172unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
173{
174 unsigned long start;
175 unsigned long flags;
176
177 spin_lock_irqsave(&cpm_muram_lock, flags);
178 cpm_muram_info.alignment = 1;
179 start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
180 spin_unlock_irqrestore(&cpm_muram_lock, flags);
181
182 return start;
183}
184EXPORT_SYMBOL(cpm_muram_alloc_fixed);
185
186/**
187 * cpm_muram_addr - turn a muram offset into a virtual address
188 * @offset: muram offset to convert
189 */
190void __iomem *cpm_muram_addr(unsigned long offset)
191{
192 return muram_vbase + offset;
193}
194EXPORT_SYMBOL(cpm_muram_addr);
195
Anton Vorontsov5093bb92008-05-23 20:39:06 +0400196unsigned long cpm_muram_offset(void __iomem *addr)
197{
198 return addr - (void __iomem *)muram_vbase;
199}
200EXPORT_SYMBOL(cpm_muram_offset);
201
Scott Wood15f8c602007-09-28 14:06:16 -0500202/**
Scott Wood4c011b1fb2007-10-12 15:19:11 -0500203 * cpm_muram_dma - turn a muram virtual address into a DMA address
Scott Wood15f8c602007-09-28 14:06:16 -0500204 * @offset: virtual address from cpm_muram_addr() to convert
205 */
206dma_addr_t cpm_muram_dma(void __iomem *addr)
207{
208 return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
209}
210EXPORT_SYMBOL(cpm_muram_dma);