blob: 7088428e1fe22e32e9abd27bbb015b5639123d6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/dma-mapping.h>
27#include <linux/param.h>
28#include <linux/string.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31#include <linux/irq.h>
32#include <linux/module.h>
33#include <asm/mpc8xx.h>
34#include <asm/page.h>
35#include <asm/pgtable.h>
36#include <asm/8xx_immap.h>
37#include <asm/commproc.h>
38#include <asm/io.h>
39#include <asm/tlbflush.h>
40#include <asm/rheap.h>
41
Vitaly Bordug7a6491d2007-04-10 21:52:04 +040042#define immr_map(member) \
43({ \
44 u32 offset = offsetof(immap_t, member); \
45 void *addr = ioremap (IMAP_ADDR + offset, \
46 sizeof( ((immap_t*)0)->member)); \
47 addr; \
48})
49
50#define immr_map_size(member, size) \
51({ \
52 u32 offset = offsetof(immap_t, member); \
53 void *addr = ioremap (IMAP_ADDR + offset, size); \
54 addr; \
55})
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void m8xx_cpm_dpinit(void);
58static uint host_buffer; /* One page of host buffer */
59static uint host_end; /* end + 1 */
60cpm8xx_t *cpmp; /* Pointer to comm processor space */
61
62/* CPM interrupt vector functions.
63*/
64struct cpm_action {
Al Viro39e3eb72006-10-09 12:48:42 +010065 void (*handler)(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 void *dev_id;
67};
68static struct cpm_action cpm_vecs[CPMVEC_NR];
Al Viro39e3eb72006-10-09 12:48:42 +010069static irqreturn_t cpm_interrupt(int irq, void * dev);
70static irqreturn_t cpm_error_interrupt(int irq, void *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void alloc_host_memory(void);
72/* Define a table of names to identify CPM interrupt handlers in
73 * /proc/interrupts.
74 */
75const char *cpm_int_name[] =
76 { "error", "PC4", "PC5", "SMC2",
77 "SMC1", "SPI", "PC6", "Timer 4",
78 "", "PC7", "PC8", "PC9",
79 "Timer 3", "", "PC10", "PC11",
80 "I2C", "RISC Timer", "Timer 2", "",
81 "IDMA2", "IDMA1", "SDMA error", "PC12",
82 "PC13", "Timer 1", "PC14", "SCC4",
83 "SCC3", "SCC2", "SCC1", "PC15"
84 };
85
86static void
87cpm_mask_irq(unsigned int irq)
88{
89 int cpm_vec = irq - CPM_IRQ_OFFSET;
90
Vitaly Bordug0ce928e2006-01-15 17:30:29 +030091 clrbits32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr, (1 << cpm_vec));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static void
95cpm_unmask_irq(unsigned int irq)
96{
97 int cpm_vec = irq - CPM_IRQ_OFFSET;
98
Vitaly Bordug0ce928e2006-01-15 17:30:29 +030099 setbits32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr, (1 << cpm_vec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void
103cpm_ack(unsigned int irq)
104{
105 /* We do not need to do anything here. */
106}
107
108static void
109cpm_eoi(unsigned int irq)
110{
111 int cpm_vec = irq - CPM_IRQ_OFFSET;
112
Marcelo Tosattie37b0c92005-10-28 17:46:10 -0700113 out_be32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cisr, (1 << cpm_vec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116struct hw_interrupt_type cpm_pic = {
117 .typename = " CPM ",
118 .enable = cpm_unmask_irq,
119 .disable = cpm_mask_irq,
120 .ack = cpm_ack,
121 .end = cpm_eoi,
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124void
Marcelo Tosatti079da352005-08-07 09:42:47 -0700125m8xx_cpm_reset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 volatile immap_t *imp;
128 volatile cpm8xx_t *commproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 imp = (immap_t *)IMAP_ADDR;
131 commproc = (cpm8xx_t *)&imp->im_cpm;
132
133#ifdef CONFIG_UCODE_PATCH
134 /* Perform a reset.
135 */
136 commproc->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
137
138 /* Wait for it.
139 */
140 while (commproc->cp_cpcr & CPM_CR_FLG);
141
142 cpm_load_patch(imp);
143#endif
144
145 /* Set SDMA Bus Request priority 5.
146 * On 860T, this also enables FEC priority 6. I am not sure
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000147 * this is what we really want for some applications, but the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * manual recommends it.
149 * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
150 */
Marcelo Tosattie37b0c92005-10-28 17:46:10 -0700151 out_be32(&imp->im_siu_conf.sc_sdcr, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Reclaim the DP memory for our use. */
154 m8xx_cpm_dpinit();
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* Tell everyone where the comm processor resides.
157 */
158 cpmp = (cpm8xx_t *)commproc;
159}
160
161/* We used to do this earlier, but have to postpone as long as possible
162 * to ensure the kernel VM is now running.
163 */
164static void
165alloc_host_memory(void)
166{
167 dma_addr_t physaddr;
168
169 /* Set the host page for allocation.
170 */
171 host_buffer = (uint)dma_alloc_coherent(NULL, PAGE_SIZE, &physaddr,
172 GFP_KERNEL);
173 host_end = host_buffer + PAGE_SIZE;
174}
175
176/* This is called during init_IRQ. We used to do it above, but this
177 * was too early since init_IRQ was not yet called.
178 */
179static struct irqaction cpm_error_irqaction = {
180 .handler = cpm_error_interrupt,
181 .mask = CPU_MASK_NONE,
182};
183static struct irqaction cpm_interrupt_irqaction = {
184 .handler = cpm_interrupt,
185 .mask = CPU_MASK_NONE,
186 .name = "CPM cascade",
187};
188
189void
190cpm_interrupt_init(void)
191{
192 int i;
193
194 /* Initialize the CPM interrupt controller.
195 */
Marcelo Tosattie37b0c92005-10-28 17:46:10 -0700196 out_be32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
Marcelo Tosattie37b0c92005-10-28 17:46:10 -0700198 ((CPM_INTERRUPT/2) << 13) | CICR_HP_MASK);
199 out_be32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* install the CPM interrupt controller routines for the CPM
202 * interrupt vectors
203 */
204 for ( i = CPM_IRQ_OFFSET ; i < CPM_IRQ_OFFSET + NR_CPM_INTS ; i++ )
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700205 irq_desc[i].chip = &cpm_pic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* Set our interrupt handler with the core CPU. */
208 if (setup_irq(CPM_INTERRUPT, &cpm_interrupt_irqaction))
209 panic("Could not allocate CPM IRQ!");
210
211 /* Install our own error handler. */
212 cpm_error_irqaction.name = cpm_int_name[CPMVEC_ERROR];
213 if (setup_irq(CPM_IRQ_OFFSET + CPMVEC_ERROR, &cpm_error_irqaction))
214 panic("Could not allocate CPM error IRQ!");
215
Vitaly Bordug0ce928e2006-01-15 17:30:29 +0300216 setbits32(&((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr, CICR_IEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/*
220 * Get the CPM interrupt vector.
221 */
222int
Al Viro39e3eb72006-10-09 12:48:42 +0100223cpm_get_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 int cpm_vec;
226
227 /* Get the vector by setting the ACK bit and then reading
228 * the register.
229 */
Marcelo Tosattie37b0c92005-10-28 17:46:10 -0700230 out_be16(&((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr, 1);
231 cpm_vec = in_be16(&((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cpm_vec >>= 11;
233
234 return cpm_vec;
235}
236
237/* CPM interrupt controller cascade interrupt.
238*/
239static irqreturn_t
Al Viro39e3eb72006-10-09 12:48:42 +0100240cpm_interrupt(int irq, void * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 /* This interrupt handler never actually gets called. It is
243 * installed only to unmask the CPM cascade interrupt in the SIU
244 * and to make the CPM cascade interrupt visible in /proc/interrupts.
245 */
246 return IRQ_HANDLED;
247}
248
249/* The CPM can generate the error interrupt when there is a race condition
250 * between generating and masking interrupts. All we have to do is ACK it
251 * and return. This is a no-op function so we don't need any special
252 * tests in the interrupt handler.
253 */
254static irqreturn_t
Al Viro39e3eb72006-10-09 12:48:42 +0100255cpm_error_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 return IRQ_HANDLED;
258}
259
260/* A helper function to translate the handler prototype required by
261 * request_irq() to the handler prototype required by cpm_install_handler().
262 */
263static irqreturn_t
Al Viro39e3eb72006-10-09 12:48:42 +0100264cpm_handler_helper(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int cpm_vec = irq - CPM_IRQ_OFFSET;
267
Al Viro39e3eb72006-10-09 12:48:42 +0100268 (*cpm_vecs[cpm_vec].handler)(dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return IRQ_HANDLED;
271}
272
273/* Install a CPM interrupt handler.
274 * This routine accepts a CPM interrupt vector in the range 0 to 31.
275 * This routine is retained for backward compatibility. Rather than using
276 * this routine to install a CPM interrupt handler, you can now use
277 * request_irq() with an IRQ in the range CPM_IRQ_OFFSET to
278 * CPM_IRQ_OFFSET + NR_CPM_INTS - 1 (16 to 47).
279 *
280 * Notice that the prototype of the interrupt handler function must be
281 * different depending on whether you install the handler with
282 * request_irq() or cpm_install_handler().
283 */
284void
Al Viro39e3eb72006-10-09 12:48:42 +0100285cpm_install_handler(int cpm_vec, void (*handler)(void *), void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 int err;
288
289 /* If null handler, assume we are trying to free the IRQ.
290 */
291 if (!handler) {
292 free_irq(CPM_IRQ_OFFSET + cpm_vec, dev_id);
293 return;
294 }
295
296 if (cpm_vecs[cpm_vec].handler != 0)
297 printk(KERN_INFO "CPM interrupt %x replacing %x\n",
298 (uint)handler, (uint)cpm_vecs[cpm_vec].handler);
299 cpm_vecs[cpm_vec].handler = handler;
300 cpm_vecs[cpm_vec].dev_id = dev_id;
301
302 if ((err = request_irq(CPM_IRQ_OFFSET + cpm_vec, cpm_handler_helper,
303 0, cpm_int_name[cpm_vec], dev_id)))
304 printk(KERN_ERR "request_irq() returned %d for CPM vector %d\n",
305 err, cpm_vec);
306}
307
308/* Free a CPM interrupt handler.
309 * This routine accepts a CPM interrupt vector in the range 0 to 31.
310 * This routine is retained for backward compatibility.
311 */
312void
313cpm_free_handler(int cpm_vec)
314{
315 request_irq(CPM_IRQ_OFFSET + cpm_vec, NULL, 0, 0,
316 cpm_vecs[cpm_vec].dev_id);
317
318 cpm_vecs[cpm_vec].handler = NULL;
319 cpm_vecs[cpm_vec].dev_id = NULL;
320}
321
322/* We also own one page of host buffer space for the allocation of
323 * UART "fifos" and the like.
324 */
325uint
326m8xx_cpm_hostalloc(uint size)
327{
328 uint retloc;
329
330 if (host_buffer == 0)
331 alloc_host_memory();
332
333 if ((host_buffer + size) >= host_end)
334 return(0);
335
336 retloc = host_buffer;
337 host_buffer += size;
338
339 return(retloc);
340}
341
342/* Set a baud rate generator. This needs lots of work. There are
343 * four BRGs, any of which can be wired to any channel.
344 * The internal baud rate clock is the system clock divided by 16.
345 * This assumes the baudrate is 16x oversampled by the uart.
346 */
347#define BRG_INT_CLK (((bd_t *)__res)->bi_intfreq)
348#define BRG_UART_CLK (BRG_INT_CLK/16)
349#define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
350
351void
352cpm_setbrg(uint brg, uint rate)
353{
354 volatile uint *bp;
355
356 /* This is good enough to get SMCs running.....
357 */
358 bp = (uint *)&cpmp->cp_brgc1;
359 bp += brg;
360 /* The BRG has a 12-bit counter. For really slow baud rates (or
361 * really fast processors), we may have to further divide by 16.
362 */
363 if (((BRG_UART_CLK / rate) - 1) < 4096)
364 *bp = (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN;
365 else
366 *bp = (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
367 CPM_BRG_EN | CPM_BRG_DIV16;
368}
369
370/*
371 * dpalloc / dpfree bits.
372 */
373static spinlock_t cpm_dpmem_lock;
374/*
375 * 16 blocks should be enough to satisfy all requests
376 * until the memory subsystem goes up...
377 */
378static rh_block_t cpm_boot_dpmem_rh_block[16];
379static rh_info_t cpm_dpmem_info;
380
381#define CPM_DPMEM_ALIGNMENT 8
Vitaly Bordug7a6491d2007-04-10 21:52:04 +0400382static u8* dpram_vbase;
383static uint dpram_pbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385void m8xx_cpm_dpinit(void)
386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock_init(&cpm_dpmem_lock);
388
Vitaly Bordug7a6491d2007-04-10 21:52:04 +0400389 dpram_vbase = immr_map_size(im_cpm.cp_dpmem, CPM_DATAONLY_BASE + CPM_DATAONLY_SIZE);
390 dpram_pbase = (uint)&((immap_t *)IMAP_ADDR)->im_cpm.cp_dpmem;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* Initialize the info header */
393 rh_init(&cpm_dpmem_info, CPM_DPMEM_ALIGNMENT,
394 sizeof(cpm_boot_dpmem_rh_block) /
395 sizeof(cpm_boot_dpmem_rh_block[0]),
396 cpm_boot_dpmem_rh_block);
397
398 /*
399 * Attach the usable dpmem area.
400 * XXX: This is actually crap. CPM_DATAONLY_BASE and
401 * CPM_DATAONLY_SIZE are a subset of the available dparm. It varies
402 * with the processor and the microcode patches applied / activated.
403 * But the following should be at least safe.
404 */
Timur Tabi4c356302007-05-08 14:46:36 -0500405 rh_attach_region(&cpm_dpmem_info, CPM_DATAONLY_BASE, CPM_DATAONLY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408/*
409 * Allocate the requested size worth of DP memory.
Marcelo Tosatti3d9e9dc2005-11-02 12:42:16 -0200410 * This function returns an offset into the DPRAM area.
411 * Use cpm_dpram_addr() to get the virtual address of the area.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Timur Tabi4c356302007-05-08 14:46:36 -0500413unsigned long cpm_dpalloc(uint size, uint align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Timur Tabi4c356302007-05-08 14:46:36 -0500415 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long flags;
417
418 spin_lock_irqsave(&cpm_dpmem_lock, flags);
419 cpm_dpmem_info.alignment = align;
420 start = rh_alloc(&cpm_dpmem_info, size, "commproc");
421 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
422
Timur Tabi4c356302007-05-08 14:46:36 -0500423 return start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425EXPORT_SYMBOL(cpm_dpalloc);
426
Timur Tabi4c356302007-05-08 14:46:36 -0500427int cpm_dpfree(unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int ret;
430 unsigned long flags;
431
432 spin_lock_irqsave(&cpm_dpmem_lock, flags);
Timur Tabi4c356302007-05-08 14:46:36 -0500433 ret = rh_free(&cpm_dpmem_info, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
435
436 return ret;
437}
438EXPORT_SYMBOL(cpm_dpfree);
439
Timur Tabi4c356302007-05-08 14:46:36 -0500440unsigned long cpm_dpalloc_fixed(unsigned long offset, uint size, uint align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Timur Tabi4c356302007-05-08 14:46:36 -0500442 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long flags;
444
445 spin_lock_irqsave(&cpm_dpmem_lock, flags);
446 cpm_dpmem_info.alignment = align;
Timur Tabi4c356302007-05-08 14:46:36 -0500447 start = rh_alloc_fixed(&cpm_dpmem_info, offset, size, "commproc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
449
Timur Tabi4c356302007-05-08 14:46:36 -0500450 return start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452EXPORT_SYMBOL(cpm_dpalloc_fixed);
453
454void cpm_dpdump(void)
455{
456 rh_dump(&cpm_dpmem_info);
457}
458EXPORT_SYMBOL(cpm_dpdump);
459
Timur Tabi4c356302007-05-08 14:46:36 -0500460void *cpm_dpram_addr(unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 return ((immap_t *)IMAP_ADDR)->im_cpm.cp_dpmem + offset;
463}
464EXPORT_SYMBOL(cpm_dpram_addr);
Vitaly Bordug7a6491d2007-04-10 21:52:04 +0400465
466uint cpm_dpram_phys(u8* addr)
467{
468 return (dpram_pbase + (uint)(addr - dpram_vbase));
469}
470EXPORT_SYMBOL(cpm_dpram_phys);