2 * General Purpose functions for the global management of the
3 * Communication Processor Module.
4 * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
6 * In addition to the individual control of the communication
7 * channels, there are a few functions that globally affect the
8 * communication processor.
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.
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>
30 #include <linux/interrupt.h>
31 #include <linux/irq.h>
32 #include <linux/module.h>
33 #include <asm/mpc8xx.h>
35 #include <asm/pgtable.h>
36 #include <asm/8xx_immap.h>
37 #include <asm/commproc.h>
39 #include <asm/tlbflush.h>
40 #include <asm/rheap.h>
44 #include <asm/fs_pd.h>
46 #define CPM_MAP_SIZE (0x4000)
48 #ifndef CONFIG_PPC_CPM_NEW_BINDING
49 static void m8xx_cpm_dpinit(void);
51 cpm8xx_t __iomem
*cpmp
; /* Pointer to comm processor space */
52 immap_t __iomem
*mpc8xx_immr
;
53 static cpic8xx_t __iomem
*cpic_reg
;
55 static struct irq_host
*cpm_pic_host
;
57 static void cpm_mask_irq(unsigned int irq
)
59 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
61 clrbits32(&cpic_reg
->cpic_cimr
, (1 << cpm_vec
));
64 static void cpm_unmask_irq(unsigned int irq
)
66 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
68 setbits32(&cpic_reg
->cpic_cimr
, (1 << cpm_vec
));
71 static void cpm_end_irq(unsigned int irq
)
73 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
75 out_be32(&cpic_reg
->cpic_cisr
, (1 << cpm_vec
));
78 static struct irq_chip cpm_pic
= {
79 .typename
= " CPM PIC ",
81 .unmask
= cpm_unmask_irq
,
89 /* Get the vector by setting the ACK bit and then reading
92 out_be16(&cpic_reg
->cpic_civr
, 1);
93 cpm_vec
= in_be16(&cpic_reg
->cpic_civr
);
96 return irq_linear_revmap(cpm_pic_host
, cpm_vec
);
99 static int cpm_pic_host_map(struct irq_host
*h
, unsigned int virq
,
102 pr_debug("cpm_pic_host_map(%d, 0x%lx)\n", virq
, hw
);
104 get_irq_desc(virq
)->status
|= IRQ_LEVEL
;
105 set_irq_chip_and_handler(virq
, &cpm_pic
, handle_fasteoi_irq
);
109 /* The CPM can generate the error interrupt when there is a race condition
110 * between generating and masking interrupts. All we have to do is ACK it
111 * and return. This is a no-op function so we don't need any special
112 * tests in the interrupt handler.
114 static irqreturn_t
cpm_error_interrupt(int irq
, void *dev
)
119 static struct irqaction cpm_error_irqaction
= {
120 .handler
= cpm_error_interrupt
,
121 .mask
= CPU_MASK_NONE
,
125 static struct irq_host_ops cpm_pic_host_ops
= {
126 .map
= cpm_pic_host_map
,
129 unsigned int cpm_pic_init(void)
131 struct device_node
*np
= NULL
;
133 unsigned int sirq
= NO_IRQ
, hwirq
, eirq
;
136 pr_debug("cpm_pic_init\n");
138 np
= of_find_compatible_node(NULL
, NULL
, "fsl,cpm1-pic");
140 np
= of_find_compatible_node(NULL
, "cpm-pic", "CPM");
142 printk(KERN_ERR
"CPM PIC init: can not find cpm-pic node\n");
146 ret
= of_address_to_resource(np
, 0, &res
);
150 cpic_reg
= ioremap(res
.start
, res
.end
- res
.start
+ 1);
151 if (cpic_reg
== NULL
)
154 sirq
= irq_of_parse_and_map(np
, 0);
158 /* Initialize the CPM interrupt controller. */
159 hwirq
= (unsigned int)irq_map
[sirq
].hwirq
;
160 out_be32(&cpic_reg
->cpic_cicr
,
161 (CICR_SCD_SCC4
| CICR_SCC_SCC3
| CICR_SCB_SCC2
| CICR_SCA_SCC1
) |
162 ((hwirq
/2) << 13) | CICR_HP_MASK
);
164 out_be32(&cpic_reg
->cpic_cimr
, 0);
166 cpm_pic_host
= irq_alloc_host(of_node_get(np
), IRQ_HOST_MAP_LINEAR
,
167 64, &cpm_pic_host_ops
, 64);
168 if (cpm_pic_host
== NULL
) {
169 printk(KERN_ERR
"CPM2 PIC: failed to allocate irq host!\n");
174 /* Install our own error handler. */
175 np
= of_find_compatible_node(NULL
, NULL
, "fsl,cpm1");
177 np
= of_find_node_by_type(NULL
, "cpm");
179 printk(KERN_ERR
"CPM PIC init: can not find cpm node\n");
183 eirq
= irq_of_parse_and_map(np
, 0);
187 if (setup_irq(eirq
, &cpm_error_irqaction
))
188 printk(KERN_ERR
"Could not allocate CPM error IRQ!");
190 setbits32(&cpic_reg
->cpic_cicr
, CICR_IEN
);
197 void __init
cpm_reset(void)
199 sysconf8xx_t __iomem
*siu_conf
;
201 mpc8xx_immr
= ioremap(get_immrbase(), 0x4000);
203 printk(KERN_CRIT
"Could not map IMMR\n");
207 cpmp
= &mpc8xx_immr
->im_cpm
;
209 #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
212 out_be16(&cpmp
->cp_cpcr
, CPM_CR_RST
| CPM_CR_FLG
);
216 while (in_be16(&cpmp
->cp_cpcr
) & CPM_CR_FLG
);
219 #ifdef CONFIG_UCODE_PATCH
220 cpm_load_patch(cpmp
);
223 /* Set SDMA Bus Request priority 5.
224 * On 860T, this also enables FEC priority 6. I am not sure
225 * this is what we realy want for some applications, but the
226 * manual recommends it.
227 * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
229 siu_conf
= immr_map(im_siu_conf
);
230 out_be32(&siu_conf
->sc_sdcr
, 1);
231 immr_unmap(siu_conf
);
233 #ifdef CONFIG_PPC_CPM_NEW_BINDING
236 /* Reclaim the DP memory for our use. */
241 static DEFINE_SPINLOCK(cmd_lock
);
243 #define MAX_CR_CMD_LOOPS 10000
245 int cpm_command(u32 command
, u8 opcode
)
250 if (command
& 0xffffff0f)
253 spin_lock_irqsave(&cmd_lock
, flags
);
256 out_be16(&cpmp
->cp_cpcr
, command
| CPM_CR_FLG
| (opcode
<< 8));
257 for (i
= 0; i
< MAX_CR_CMD_LOOPS
; i
++)
258 if ((in_be16(&cpmp
->cp_cpcr
) & CPM_CR_FLG
) == 0)
261 printk(KERN_ERR
"%s(): Not able to issue CPM command\n", __FUNCTION__
);
264 spin_unlock_irqrestore(&cmd_lock
, flags
);
267 EXPORT_SYMBOL(cpm_command
);
269 /* Set a baud rate generator. This needs lots of work. There are
270 * four BRGs, any of which can be wired to any channel.
271 * The internal baud rate clock is the system clock divided by 16.
272 * This assumes the baudrate is 16x oversampled by the uart.
274 #define BRG_INT_CLK (get_brgfreq())
275 #define BRG_UART_CLK (BRG_INT_CLK/16)
276 #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
279 cpm_setbrg(uint brg
, uint rate
)
283 /* This is good enough to get SMCs running.....
285 bp
= &cpmp
->cp_brgc1
;
287 /* The BRG has a 12-bit counter. For really slow baud rates (or
288 * really fast processors), we may have to further divide by 16.
290 if (((BRG_UART_CLK
/ rate
) - 1) < 4096)
291 out_be32(bp
, (((BRG_UART_CLK
/ rate
) - 1) << 1) | CPM_BRG_EN
);
293 out_be32(bp
, (((BRG_UART_CLK_DIV16
/ rate
) - 1) << 1) |
294 CPM_BRG_EN
| CPM_BRG_DIV16
);
297 #ifndef CONFIG_PPC_CPM_NEW_BINDING
299 * dpalloc / dpfree bits.
301 static spinlock_t cpm_dpmem_lock
;
303 * 16 blocks should be enough to satisfy all requests
304 * until the memory subsystem goes up...
306 static rh_block_t cpm_boot_dpmem_rh_block
[16];
307 static rh_info_t cpm_dpmem_info
;
309 #define CPM_DPMEM_ALIGNMENT 8
310 static u8 __iomem
*dpram_vbase
;
311 static phys_addr_t dpram_pbase
;
313 static void m8xx_cpm_dpinit(void)
315 spin_lock_init(&cpm_dpmem_lock
);
317 dpram_vbase
= cpmp
->cp_dpmem
;
318 dpram_pbase
= get_immrbase() + offsetof(immap_t
, im_cpm
.cp_dpmem
);
320 /* Initialize the info header */
321 rh_init(&cpm_dpmem_info
, CPM_DPMEM_ALIGNMENT
,
322 sizeof(cpm_boot_dpmem_rh_block
) /
323 sizeof(cpm_boot_dpmem_rh_block
[0]),
324 cpm_boot_dpmem_rh_block
);
327 * Attach the usable dpmem area.
328 * XXX: This is actually crap. CPM_DATAONLY_BASE and
329 * CPM_DATAONLY_SIZE are a subset of the available dparm. It varies
330 * with the processor and the microcode patches applied / activated.
331 * But the following should be at least safe.
333 rh_attach_region(&cpm_dpmem_info
, CPM_DATAONLY_BASE
, CPM_DATAONLY_SIZE
);
337 * Allocate the requested size worth of DP memory.
338 * This function returns an offset into the DPRAM area.
339 * Use cpm_dpram_addr() to get the virtual address of the area.
341 unsigned long cpm_dpalloc(uint size
, uint align
)
346 spin_lock_irqsave(&cpm_dpmem_lock
, flags
);
347 cpm_dpmem_info
.alignment
= align
;
348 start
= rh_alloc(&cpm_dpmem_info
, size
, "commproc");
349 spin_unlock_irqrestore(&cpm_dpmem_lock
, flags
);
353 EXPORT_SYMBOL(cpm_dpalloc
);
355 int cpm_dpfree(unsigned long offset
)
360 spin_lock_irqsave(&cpm_dpmem_lock
, flags
);
361 ret
= rh_free(&cpm_dpmem_info
, offset
);
362 spin_unlock_irqrestore(&cpm_dpmem_lock
, flags
);
366 EXPORT_SYMBOL(cpm_dpfree
);
368 unsigned long cpm_dpalloc_fixed(unsigned long offset
, uint size
, uint align
)
373 spin_lock_irqsave(&cpm_dpmem_lock
, flags
);
374 cpm_dpmem_info
.alignment
= align
;
375 start
= rh_alloc_fixed(&cpm_dpmem_info
, offset
, size
, "commproc");
376 spin_unlock_irqrestore(&cpm_dpmem_lock
, flags
);
380 EXPORT_SYMBOL(cpm_dpalloc_fixed
);
382 void cpm_dpdump(void)
384 rh_dump(&cpm_dpmem_info
);
386 EXPORT_SYMBOL(cpm_dpdump
);
388 void *cpm_dpram_addr(unsigned long offset
)
390 return (void *)(dpram_vbase
+ offset
);
392 EXPORT_SYMBOL(cpm_dpram_addr
);
394 uint
cpm_dpram_phys(u8
*addr
)
396 return (dpram_pbase
+ (uint
)(addr
- dpram_vbase
));
398 EXPORT_SYMBOL(cpm_dpram_phys
);
399 #endif /* !CONFIG_PPC_CPM_NEW_BINDING */
401 struct cpm_ioport16
{
402 __be16 dir
, par
, odr_sor
, dat
, intr
;
406 struct cpm_ioport32
{
407 __be32 dir
, par
, sor
;
410 static void cpm1_set_pin32(int port
, int pin
, int flags
)
412 struct cpm_ioport32 __iomem
*iop
;
413 pin
= 1 << (31 - pin
);
415 if (port
== CPM_PORTB
)
416 iop
= (struct cpm_ioport32 __iomem
*)
417 &mpc8xx_immr
->im_cpm
.cp_pbdir
;
419 iop
= (struct cpm_ioport32 __iomem
*)
420 &mpc8xx_immr
->im_cpm
.cp_pedir
;
422 if (flags
& CPM_PIN_OUTPUT
)
423 setbits32(&iop
->dir
, pin
);
425 clrbits32(&iop
->dir
, pin
);
427 if (!(flags
& CPM_PIN_GPIO
))
428 setbits32(&iop
->par
, pin
);
430 clrbits32(&iop
->par
, pin
);
432 if (port
== CPM_PORTB
) {
433 if (flags
& CPM_PIN_OPENDRAIN
)
434 setbits16(&mpc8xx_immr
->im_cpm
.cp_pbodr
, pin
);
436 clrbits16(&mpc8xx_immr
->im_cpm
.cp_pbodr
, pin
);
439 if (port
== CPM_PORTE
) {
440 if (flags
& CPM_PIN_SECONDARY
)
441 setbits32(&iop
->sor
, pin
);
443 clrbits32(&iop
->sor
, pin
);
445 if (flags
& CPM_PIN_OPENDRAIN
)
446 setbits32(&mpc8xx_immr
->im_cpm
.cp_peodr
, pin
);
448 clrbits32(&mpc8xx_immr
->im_cpm
.cp_peodr
, pin
);
452 static void cpm1_set_pin16(int port
, int pin
, int flags
)
454 struct cpm_ioport16 __iomem
*iop
=
455 (struct cpm_ioport16 __iomem
*)&mpc8xx_immr
->im_ioport
;
457 pin
= 1 << (15 - pin
);
462 if (flags
& CPM_PIN_OUTPUT
)
463 setbits16(&iop
->dir
, pin
);
465 clrbits16(&iop
->dir
, pin
);
467 if (!(flags
& CPM_PIN_GPIO
))
468 setbits16(&iop
->par
, pin
);
470 clrbits16(&iop
->par
, pin
);
472 if (port
== CPM_PORTA
) {
473 if (flags
& CPM_PIN_OPENDRAIN
)
474 setbits16(&iop
->odr_sor
, pin
);
476 clrbits16(&iop
->odr_sor
, pin
);
478 if (port
== CPM_PORTC
) {
479 if (flags
& CPM_PIN_SECONDARY
)
480 setbits16(&iop
->odr_sor
, pin
);
482 clrbits16(&iop
->odr_sor
, pin
);
486 void cpm1_set_pin(enum cpm_port port
, int pin
, int flags
)
488 if (port
== CPM_PORTB
|| port
== CPM_PORTE
)
489 cpm1_set_pin32(port
, pin
, flags
);
491 cpm1_set_pin16(port
, pin
, flags
);
494 int cpm1_clk_setup(enum cpm_clk_target target
, int clock
, int mode
)
502 {CPM_CLK_SCC1
, CPM_BRG1
, 0},
503 {CPM_CLK_SCC1
, CPM_BRG2
, 1},
504 {CPM_CLK_SCC1
, CPM_BRG3
, 2},
505 {CPM_CLK_SCC1
, CPM_BRG4
, 3},
506 {CPM_CLK_SCC1
, CPM_CLK1
, 4},
507 {CPM_CLK_SCC1
, CPM_CLK2
, 5},
508 {CPM_CLK_SCC1
, CPM_CLK3
, 6},
509 {CPM_CLK_SCC1
, CPM_CLK4
, 7},
511 {CPM_CLK_SCC2
, CPM_BRG1
, 0},
512 {CPM_CLK_SCC2
, CPM_BRG2
, 1},
513 {CPM_CLK_SCC2
, CPM_BRG3
, 2},
514 {CPM_CLK_SCC2
, CPM_BRG4
, 3},
515 {CPM_CLK_SCC2
, CPM_CLK1
, 4},
516 {CPM_CLK_SCC2
, CPM_CLK2
, 5},
517 {CPM_CLK_SCC2
, CPM_CLK3
, 6},
518 {CPM_CLK_SCC2
, CPM_CLK4
, 7},
520 {CPM_CLK_SCC3
, CPM_BRG1
, 0},
521 {CPM_CLK_SCC3
, CPM_BRG2
, 1},
522 {CPM_CLK_SCC3
, CPM_BRG3
, 2},
523 {CPM_CLK_SCC3
, CPM_BRG4
, 3},
524 {CPM_CLK_SCC3
, CPM_CLK5
, 4},
525 {CPM_CLK_SCC3
, CPM_CLK6
, 5},
526 {CPM_CLK_SCC3
, CPM_CLK7
, 6},
527 {CPM_CLK_SCC3
, CPM_CLK8
, 7},
529 {CPM_CLK_SCC4
, CPM_BRG1
, 0},
530 {CPM_CLK_SCC4
, CPM_BRG2
, 1},
531 {CPM_CLK_SCC4
, CPM_BRG3
, 2},
532 {CPM_CLK_SCC4
, CPM_BRG4
, 3},
533 {CPM_CLK_SCC4
, CPM_CLK5
, 4},
534 {CPM_CLK_SCC4
, CPM_CLK6
, 5},
535 {CPM_CLK_SCC4
, CPM_CLK7
, 6},
536 {CPM_CLK_SCC4
, CPM_CLK8
, 7},
538 {CPM_CLK_SMC1
, CPM_BRG1
, 0},
539 {CPM_CLK_SMC1
, CPM_BRG2
, 1},
540 {CPM_CLK_SMC1
, CPM_BRG3
, 2},
541 {CPM_CLK_SMC1
, CPM_BRG4
, 3},
542 {CPM_CLK_SMC1
, CPM_CLK1
, 4},
543 {CPM_CLK_SMC1
, CPM_CLK2
, 5},
544 {CPM_CLK_SMC1
, CPM_CLK3
, 6},
545 {CPM_CLK_SMC1
, CPM_CLK4
, 7},
547 {CPM_CLK_SMC2
, CPM_BRG1
, 0},
548 {CPM_CLK_SMC2
, CPM_BRG2
, 1},
549 {CPM_CLK_SMC2
, CPM_BRG3
, 2},
550 {CPM_CLK_SMC2
, CPM_BRG4
, 3},
551 {CPM_CLK_SMC2
, CPM_CLK5
, 4},
552 {CPM_CLK_SMC2
, CPM_CLK6
, 5},
553 {CPM_CLK_SMC2
, CPM_CLK7
, 6},
554 {CPM_CLK_SMC2
, CPM_CLK8
, 7},
559 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
564 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
569 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
574 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
579 reg
= &mpc8xx_immr
->im_cpm
.cp_simode
;
584 reg
= &mpc8xx_immr
->im_cpm
.cp_simode
;
589 printk(KERN_ERR
"cpm1_clock_setup: invalid clock target\n");
593 if (reg
== &mpc8xx_immr
->im_cpm
.cp_sicr
&& mode
== CPM_CLK_RX
)
596 for (i
= 0; i
< ARRAY_SIZE(clk_map
); i
++) {
597 if (clk_map
[i
][0] == target
&& clk_map
[i
][1] == clock
) {
598 bits
= clk_map
[i
][2];
603 if (i
== ARRAY_SIZE(clk_map
)) {
604 printk(KERN_ERR
"cpm1_clock_setup: invalid clock combination\n");
610 out_be32(reg
, (in_be32(reg
) & ~mask
) | bits
);