This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc / syslib / cpm2_common.c
1 /*
2  * General Purpose functions for the global management of the
3  * 8260 Communication Processor Module.
4  * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
5  * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
6  *      2.3.99 Updates
7  *
8  * In addition to the individual control of the communication
9  * channels, there are a few functions that globally affect the
10  * communication processor.
11  *
12  * Buffer descriptors must be allocated from the dual ported memory
13  * space.  The allocator for that is here.  When the communication
14  * process is reset, we reclaim the memory available.  There is
15  * currently no deallocator for this memory.
16  */
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/param.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/interrupt.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
26 #include <asm/irq.h>
27 #include <asm/mpc8260.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30 #include <asm/immap_cpm2.h>
31 #include <asm/cpm2.h>
32 #include <asm/rheap.h>
33
34 static void cpm2_dpinit(void);
35 cpm_cpm2_t      *cpmp;          /* Pointer to comm processor space */
36
37 /* We allocate this here because it is used almost exclusively for
38  * the communication processor devices.
39  */
40 cpm2_map_t *cpm2_immr;
41
42 void
43 cpm2_reset(void)
44 {
45         cpm2_immr = (cpm2_map_t *)CPM_MAP_ADDR;
46
47         /* Reclaim the DP memory for our use.
48          */
49         cpm2_dpinit();
50
51         /* Tell everyone where the comm processor resides.
52          */
53         cpmp = &cpm2_immr->im_cpm;
54 }
55
56 /* Set a baud rate generator.  This needs lots of work.  There are
57  * eight BRGs, which can be connected to the CPM channels or output
58  * as clocks.  The BRGs are in two different block of internal
59  * memory mapped space.
60  * The baud rate clock is the system clock divided by something.
61  * It was set up long ago during the initial boot phase and is
62  * is given to us.
63  * Baud rate clocks are zero-based in the driver code (as that maps
64  * to port numbers).  Documentation uses 1-based numbering.
65  */
66 #define BRG_INT_CLK     (((bd_t *)__res)->bi_brgfreq)
67 #define BRG_UART_CLK    (BRG_INT_CLK/16)
68
69 /* This function is used by UARTS, or anything else that uses a 16x
70  * oversampled clock.
71  */
72 void
73 cpm2_setbrg(uint brg, uint rate)
74 {
75         volatile uint   *bp;
76
77         /* This is good enough to get SMCs running.....
78         */
79         if (brg < 4) {
80                 bp = (uint *)&cpm2_immr->im_brgc1;
81         }
82         else {
83                 bp = (uint *)&cpm2_immr->im_brgc5;
84                 brg -= 4;
85         }
86         bp += brg;
87         *bp = ((BRG_UART_CLK / rate) << 1) | CPM_BRG_EN;
88 }
89
90 /* This function is used to set high speed synchronous baud rate
91  * clocks.
92  */
93 void
94 cpm2_fastbrg(uint brg, uint rate, int div16)
95 {
96         volatile uint   *bp;
97
98         if (brg < 4) {
99                 bp = (uint *)&cpm2_immr->im_brgc1;
100         }
101         else {
102                 bp = (uint *)&cpm2_immr->im_brgc5;
103                 brg -= 4;
104         }
105         bp += brg;
106         *bp = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
107         if (div16)
108                 *bp |= CPM_BRG_DIV16;
109 }
110
111 /*
112  * dpalloc / dpfree bits.
113  */
114 static spinlock_t cpm_dpmem_lock;
115 /* 16 blocks should be enough to satisfy all requests
116  * until the memory subsystem goes up... */
117 static rh_block_t cpm_boot_dpmem_rh_block[16];
118 static rh_info_t cpm_dpmem_info;
119
120 static void cpm2_dpinit(void)
121 {
122         void *dprambase = &((cpm2_map_t *)CPM_MAP_ADDR)->im_dprambase;
123
124         spin_lock_init(&cpm_dpmem_lock);
125
126         /* initialize the info header */
127         rh_init(&cpm_dpmem_info, 1,
128                         sizeof(cpm_boot_dpmem_rh_block) /
129                         sizeof(cpm_boot_dpmem_rh_block[0]),
130                         cpm_boot_dpmem_rh_block);
131
132         /* Attach the usable dpmem area */
133         /* XXX: This is actually crap. CPM_DATAONLY_BASE and
134          * CPM_DATAONLY_SIZE is only a subset of the available dpram. It
135          * varies with the processor and the microcode patches activated.
136          * But the following should be at least safe.
137          */
138         rh_attach_region(&cpm_dpmem_info, dprambase + CPM_DATAONLY_BASE,
139                         CPM_DATAONLY_SIZE);
140 }
141
142 /* This function used to return an index into the DPRAM area.
143  * Now it returns the actuall physical address of that area.
144  * use cpm2_dpram_offset() to get the index
145  */
146 void *cpm2_dpalloc(uint size, uint align)
147 {
148         void *start;
149         unsigned long flags;
150
151         spin_lock_irqsave(&cpm_dpmem_lock, flags);
152         cpm_dpmem_info.alignment = align;
153         start = rh_alloc(&cpm_dpmem_info, size, "commproc");
154         spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
155
156         return start;
157 }
158 EXPORT_SYMBOL(cpm2_dpalloc);
159
160 int cpm2_dpfree(void *addr)
161 {
162         int ret;
163         unsigned long flags;
164
165         spin_lock_irqsave(&cpm_dpmem_lock, flags);
166         ret = rh_free(&cpm_dpmem_info, addr);
167         spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
168
169         return ret;
170 }
171 EXPORT_SYMBOL(cpm2_dpfree);
172
173 /* not sure if this is ever needed */
174 void *cpm2_dpalloc_fixed(void *addr, uint size, uint align)
175 {
176         void *start;
177         unsigned long flags;
178
179         spin_lock_irqsave(&cpm_dpmem_lock, flags);
180         cpm_dpmem_info.alignment = align;
181         start = rh_alloc_fixed(&cpm_dpmem_info, addr, size, "commproc");
182         spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
183
184         return start;
185 }
186 EXPORT_SYMBOL(cpm2_dpalloc_fixed);
187
188 void cpm2_dpdump(void)
189 {
190         rh_dump(&cpm_dpmem_info);
191 }
192 EXPORT_SYMBOL(cpm2_dpdump);
193
194 uint cpm2_dpram_offset(void *addr)
195 {
196         return (uint)((u_char *)addr -
197                         ((uint)((cpm2_map_t *)CPM_MAP_ADDR)->im_dprambase));
198 }
199 EXPORT_SYMBOL(cpm2_dpram_offset);
200
201 void *cpm2_dpram_addr(int offset)
202 {
203         return (void *)&((cpm2_map_t *)CPM_MAP_ADDR)->im_dprambase[offset];
204 }
205 EXPORT_SYMBOL(cpm2_dpram_addr);