patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc / 8xx_io / commproc.c
1 /*
2  * General Purpose functions for the global management of the
3  * Communication Processor Module.
4  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5  *
6  * In addition to the individual control of the communication
7  * channels, there are a few functions that globally affect the
8  * communication processor.
9  *
10  * Buffer descriptors must be allocated from the dual ported memory
11  * space.  The allocator for that is here.  When the communication
12  * process is reset, we reclaim the memory available.  There is
13  * currently no deallocator for this memory.
14  * The amount of space available is platform dependent.  On the
15  * MBX, the EPPC software loads additional microcode into the
16  * communication processor, and uses some of the DP ram for this
17  * purpose.  Current, the first 512 bytes and the last 256 bytes of
18  * memory are used.  Right now I am conservative and only use the
19  * memory that can never be used for microcode.  If there are
20  * applications that require more DP ram, we can expand the boundaries
21  * but then we have to be careful of any downloaded microcode.
22  */
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/param.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <asm/irq.h>
31 #include <asm/mpc8xx.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/8xx_immap.h>
35 #include <asm/commproc.h>
36 #include <asm/io.h>
37
38 extern int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep);
39
40 static  uint    dp_alloc_base;  /* Starting offset in DP ram */
41 static  uint    dp_alloc_top;   /* Max offset + 1 */
42 static  uint    host_buffer;    /* One page of host buffer */
43 static  uint    host_end;       /* end + 1 */
44 cpm8xx_t        *cpmp;          /* Pointer to comm processor space */
45
46 /* CPM interrupt vector functions.
47 */
48 struct  cpm_action {
49         void    (*handler)(void *, struct pt_regs * regs);
50         void    *dev_id;
51 };
52 static  struct  cpm_action cpm_vecs[CPMVEC_NR];
53 static  void    cpm_interrupt(int irq, void * dev, struct pt_regs * regs);
54 static  void    cpm_error_interrupt(void *, struct pt_regs * regs);
55 static  void    alloc_host_memory(void);
56
57 #if 1
58 void
59 m8xx_cpm_reset(void)
60 {
61         volatile immap_t         *imp;
62         volatile cpm8xx_t       *commproc;
63
64         imp = (immap_t *)IMAP_ADDR;
65         commproc = (cpm8xx_t *)&imp->im_cpm;
66
67 #ifdef CONFIG_UCODE_PATCH
68         /* Perform a reset.
69         */
70         commproc->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
71
72         /* Wait for it.
73         */
74         while (commproc->cp_cpcr & CPM_CR_FLG);
75
76         cpm_load_patch(imp);
77 #endif
78
79         /* Set SDMA Bus Request priority 5.
80          * On 860T, this also enables FEC priority 6.  I am not sure
81          * this is what we realy want for some applications, but the
82          * manual recommends it.
83          * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
84          */
85         imp->im_siu_conf.sc_sdcr = 1;
86
87         /* Reclaim the DP memory for our use.
88         */
89         dp_alloc_base = CPM_DATAONLY_BASE;
90         dp_alloc_top = dp_alloc_base + CPM_DATAONLY_SIZE;
91
92         /* Tell everyone where the comm processor resides.
93         */
94         cpmp = (cpm8xx_t *)commproc;
95 }
96
97 /* We used to do this earlier, but have to postpone as long as possible
98  * to ensure the kernel VM is now running.
99  */
100 static void
101 alloc_host_memory()
102 {
103         uint    physaddr;
104
105         /* Set the host page for allocation.
106         */
107         host_buffer = (uint)consistent_alloc(GFP_KERNEL, PAGE_SIZE, &physaddr);
108         host_end = host_buffer + PAGE_SIZE;
109 }
110 #else
111 void
112 m8xx_cpm_reset(uint host_page_addr)
113 {
114         volatile immap_t         *imp;
115         volatile cpm8xx_t       *commproc;
116         pte_t                   *pte;
117
118         imp = (immap_t *)IMAP_ADDR;
119         commproc = (cpm8xx_t *)&imp->im_cpm;
120
121 #ifdef CONFIG_UCODE_PATCH
122         /* Perform a reset.
123         */
124         commproc->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
125
126         /* Wait for it.
127         */
128         while (commproc->cp_cpcr & CPM_CR_FLG);
129
130         cpm_load_patch(imp);
131 #endif
132
133         /* Set SDMA Bus Request priority 5.
134          * On 860T, this also enables FEC priority 6.  I am not sure
135          * this is what we realy want for some applications, but the
136          * manual recommends it.
137          * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
138         */
139         imp->im_siu_conf.sc_sdcr = 1;
140
141         /* Reclaim the DP memory for our use.
142         */
143         dp_alloc_base = CPM_DATAONLY_BASE;
144         dp_alloc_top = dp_alloc_base + CPM_DATAONLY_SIZE;
145
146         /* Set the host page for allocation.
147         */
148         host_buffer = host_page_addr;   /* Host virtual page address */
149         host_end = host_page_addr + PAGE_SIZE;
150
151         /* We need to get this page early, so I have to do it the
152          * hard way.
153          */
154         if (get_pteptr(&init_mm, host_page_addr, &pte)) {
155                 pte_val(*pte) |= _PAGE_NO_CACHE;
156                 flush_tlb_page(init_mm.mmap, host_buffer);
157         }
158         else {
159                 panic("Huh?  No CPM host page?");
160         }
161
162         /* Tell everyone where the comm processor resides.
163         */
164         cpmp = (cpm8xx_t *)commproc;
165 }
166 #endif
167
168 /* This is called during init_IRQ.  We used to do it above, but this
169  * was too early since init_IRQ was not yet called.
170  */
171 void
172 cpm_interrupt_init(void)
173 {
174         /* Initialize the CPM interrupt controller.
175         */
176         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr =
177             (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
178                 ((CPM_INTERRUPT/2) << 13) | CICR_HP_MASK;
179         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr = 0;
180
181         /* Set our interrupt handler with the core CPU.
182         */
183         if (request_8xxirq(CPM_INTERRUPT, cpm_interrupt, 0, "cpm", NULL) != 0)
184                 panic("Could not allocate CPM IRQ!");
185
186         /* Install our own error handler.
187         */
188         cpm_install_handler(CPMVEC_ERROR, cpm_error_interrupt, NULL);
189         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr |= CICR_IEN;
190 }
191
192 /* CPM interrupt controller interrupt.
193 */
194 static  void
195 cpm_interrupt(int irq, void * dev, struct pt_regs * regs)
196 {
197         uint    vec;
198
199         /* Get the vector by setting the ACK bit and then reading
200          * the register.
201          */
202         ((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr = 1;
203         vec = ((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr;
204         vec >>= 11;
205
206         if (cpm_vecs[vec].handler != 0)
207                 (*cpm_vecs[vec].handler)(cpm_vecs[vec].dev_id, regs);
208         else
209                 ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr &= ~(1 << vec);
210
211         /* After servicing the interrupt, we have to remove the status
212          * indicator.
213          */
214         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cisr = (1 << vec);
215         
216 }
217
218 /* The CPM can generate the error interrupt when there is a race condition
219  * between generating and masking interrupts.  All we have to do is ACK it
220  * and return.  This is a no-op function so we don't need any special
221  * tests in the interrupt handler.
222  */
223 static  void
224 cpm_error_interrupt(void *dev, struct pt_regs *regs)
225 {
226 }
227
228 /* Install a CPM interrupt handler.
229 */
230 void
231 cpm_install_handler(int vec, void (*handler)(void *, struct pt_regs *regs),
232                     void *dev_id)
233 {
234
235         /* If null handler, assume we are trying to free the IRQ.
236         */
237         if (!handler) {
238                 cpm_free_handler(vec);
239                 return;
240         }
241
242         if (cpm_vecs[vec].handler != 0)
243                 printk("CPM interrupt %x replacing %x\n",
244                         (uint)handler, (uint)cpm_vecs[vec].handler);
245         cpm_vecs[vec].handler = handler;
246         cpm_vecs[vec].dev_id = dev_id;
247         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr |= (1 << vec);
248 }
249
250 /* Free a CPM interrupt handler.
251 */
252 void
253 cpm_free_handler(int vec)
254 {
255         cpm_vecs[vec].handler = NULL;
256         cpm_vecs[vec].dev_id = NULL;
257         ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr &= ~(1 << vec);
258 }
259
260 /* Allocate some memory from the dual ported ram.  We may want to
261  * enforce alignment restrictions, but right now everyone is a good
262  * citizen.
263  */
264 uint
265 m8xx_cpm_dpalloc(uint size)
266 {
267         uint    retloc;
268
269         if ((dp_alloc_base + size) >= dp_alloc_top)
270                 return(CPM_DP_NOSPACE);
271
272         retloc = dp_alloc_base;
273         dp_alloc_base += size;
274
275         return(retloc);
276 }
277
278 uint
279 m8xx_cpm_dpalloc_index(void)
280 {
281         return dp_alloc_base;
282 }
283
284 /* We also own one page of host buffer space for the allocation of
285  * UART "fifos" and the like.
286  */
287 uint
288 m8xx_cpm_hostalloc(uint size)
289 {
290         uint    retloc;
291
292 #if 1
293         if (host_buffer == 0)
294                 alloc_host_memory();
295 #endif
296
297         if ((host_buffer + size) >= host_end)
298                 return(0);
299
300         retloc = host_buffer;
301         host_buffer += size;
302
303         return(retloc);
304 }
305
306 /* Set a baud rate generator.  This needs lots of work.  There are
307  * four BRGs, any of which can be wired to any channel.
308  * The internal baud rate clock is the system clock divided by 16.
309  * This assumes the baudrate is 16x oversampled by the uart.
310  */
311 #define BRG_INT_CLK             (((bd_t *)__res)->bi_intfreq)
312 #define BRG_UART_CLK            (BRG_INT_CLK/16)
313 #define BRG_UART_CLK_DIV16      (BRG_UART_CLK/16)
314
315 void
316 m8xx_cpm_setbrg(uint brg, uint rate)
317 {
318         volatile uint   *bp;
319
320         /* This is good enough to get SMCs running.....
321         */
322         bp = (uint *)&cpmp->cp_brgc1;
323         bp += brg;
324         /* The BRG has a 12-bit counter.  For really slow baud rates (or
325          * really fast processors), we may have to further divide by 16.
326          */
327         if (((BRG_UART_CLK / rate) - 1) < 4096)
328                 *bp = (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN;
329         else
330                 *bp = (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
331                                                 CPM_BRG_EN | CPM_BRG_DIV16;
332 }