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>
34 #include <asm/pgtable.h>
35 #include <asm/8xx_immap.h>
38 #include <asm/tlbflush.h>
39 #include <asm/rheap.h>
43 #include <asm/fs_pd.h>
45 #define CPM_MAP_SIZE (0x4000)
47 cpm8xx_t __iomem
*cpmp
; /* Pointer to comm processor space */
48 immap_t __iomem
*mpc8xx_immr
;
49 static cpic8xx_t __iomem
*cpic_reg
;
51 static struct irq_host
*cpm_pic_host
;
53 static void cpm_mask_irq(unsigned int irq
)
55 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
57 clrbits32(&cpic_reg
->cpic_cimr
, (1 << cpm_vec
));
60 static void cpm_unmask_irq(unsigned int irq
)
62 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
64 setbits32(&cpic_reg
->cpic_cimr
, (1 << cpm_vec
));
67 static void cpm_end_irq(unsigned int irq
)
69 unsigned int cpm_vec
= (unsigned int)irq_map
[irq
].hwirq
;
71 out_be32(&cpic_reg
->cpic_cisr
, (1 << cpm_vec
));
74 static struct irq_chip cpm_pic
= {
75 .typename
= " CPM PIC ",
77 .unmask
= cpm_unmask_irq
,
85 /* Get the vector by setting the ACK bit and then reading
88 out_be16(&cpic_reg
->cpic_civr
, 1);
89 cpm_vec
= in_be16(&cpic_reg
->cpic_civr
);
92 return irq_linear_revmap(cpm_pic_host
, cpm_vec
);
95 static int cpm_pic_host_map(struct irq_host
*h
, unsigned int virq
,
98 pr_debug("cpm_pic_host_map(%d, 0x%lx)\n", virq
, hw
);
100 get_irq_desc(virq
)->status
|= IRQ_LEVEL
;
101 set_irq_chip_and_handler(virq
, &cpm_pic
, handle_fasteoi_irq
);
105 /* The CPM can generate the error interrupt when there is a race condition
106 * between generating and masking interrupts. All we have to do is ACK it
107 * and return. This is a no-op function so we don't need any special
108 * tests in the interrupt handler.
110 static irqreturn_t
cpm_error_interrupt(int irq
, void *dev
)
115 static struct irqaction cpm_error_irqaction
= {
116 .handler
= cpm_error_interrupt
,
117 .mask
= CPU_MASK_NONE
,
121 static struct irq_host_ops cpm_pic_host_ops
= {
122 .map
= cpm_pic_host_map
,
125 unsigned int cpm_pic_init(void)
127 struct device_node
*np
= NULL
;
129 unsigned int sirq
= NO_IRQ
, hwirq
, eirq
;
132 pr_debug("cpm_pic_init\n");
134 np
= of_find_compatible_node(NULL
, NULL
, "fsl,cpm1-pic");
136 np
= of_find_compatible_node(NULL
, "cpm-pic", "CPM");
138 printk(KERN_ERR
"CPM PIC init: can not find cpm-pic node\n");
142 ret
= of_address_to_resource(np
, 0, &res
);
146 cpic_reg
= ioremap(res
.start
, res
.end
- res
.start
+ 1);
147 if (cpic_reg
== NULL
)
150 sirq
= irq_of_parse_and_map(np
, 0);
154 /* Initialize the CPM interrupt controller. */
155 hwirq
= (unsigned int)irq_map
[sirq
].hwirq
;
156 out_be32(&cpic_reg
->cpic_cicr
,
157 (CICR_SCD_SCC4
| CICR_SCC_SCC3
| CICR_SCB_SCC2
| CICR_SCA_SCC1
) |
158 ((hwirq
/2) << 13) | CICR_HP_MASK
);
160 out_be32(&cpic_reg
->cpic_cimr
, 0);
162 cpm_pic_host
= irq_alloc_host(of_node_get(np
), IRQ_HOST_MAP_LINEAR
,
163 64, &cpm_pic_host_ops
, 64);
164 if (cpm_pic_host
== NULL
) {
165 printk(KERN_ERR
"CPM2 PIC: failed to allocate irq host!\n");
170 /* Install our own error handler. */
171 np
= of_find_compatible_node(NULL
, NULL
, "fsl,cpm1");
173 np
= of_find_node_by_type(NULL
, "cpm");
175 printk(KERN_ERR
"CPM PIC init: can not find cpm node\n");
179 eirq
= irq_of_parse_and_map(np
, 0);
183 if (setup_irq(eirq
, &cpm_error_irqaction
))
184 printk(KERN_ERR
"Could not allocate CPM error IRQ!");
186 setbits32(&cpic_reg
->cpic_cicr
, CICR_IEN
);
193 void __init
cpm_reset(void)
195 sysconf8xx_t __iomem
*siu_conf
;
197 mpc8xx_immr
= ioremap(get_immrbase(), 0x4000);
199 printk(KERN_CRIT
"Could not map IMMR\n");
203 cpmp
= &mpc8xx_immr
->im_cpm
;
205 #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
208 out_be16(&cpmp
->cp_cpcr
, CPM_CR_RST
| CPM_CR_FLG
);
212 while (in_be16(&cpmp
->cp_cpcr
) & CPM_CR_FLG
);
215 #ifdef CONFIG_UCODE_PATCH
216 cpm_load_patch(cpmp
);
219 /* Set SDMA Bus Request priority 5.
220 * On 860T, this also enables FEC priority 6. I am not sure
221 * this is what we realy want for some applications, but the
222 * manual recommends it.
223 * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
225 siu_conf
= immr_map(im_siu_conf
);
226 out_be32(&siu_conf
->sc_sdcr
, 1);
227 immr_unmap(siu_conf
);
232 static DEFINE_SPINLOCK(cmd_lock
);
234 #define MAX_CR_CMD_LOOPS 10000
236 int cpm_command(u32 command
, u8 opcode
)
241 if (command
& 0xffffff0f)
244 spin_lock_irqsave(&cmd_lock
, flags
);
247 out_be16(&cpmp
->cp_cpcr
, command
| CPM_CR_FLG
| (opcode
<< 8));
248 for (i
= 0; i
< MAX_CR_CMD_LOOPS
; i
++)
249 if ((in_be16(&cpmp
->cp_cpcr
) & CPM_CR_FLG
) == 0)
252 printk(KERN_ERR
"%s(): Not able to issue CPM command\n", __func__
);
255 spin_unlock_irqrestore(&cmd_lock
, flags
);
258 EXPORT_SYMBOL(cpm_command
);
260 /* Set a baud rate generator. This needs lots of work. There are
261 * four BRGs, any of which can be wired to any channel.
262 * The internal baud rate clock is the system clock divided by 16.
263 * This assumes the baudrate is 16x oversampled by the uart.
265 #define BRG_INT_CLK (get_brgfreq())
266 #define BRG_UART_CLK (BRG_INT_CLK/16)
267 #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
270 cpm_setbrg(uint brg
, uint rate
)
274 /* This is good enough to get SMCs running.....
276 bp
= &cpmp
->cp_brgc1
;
278 /* The BRG has a 12-bit counter. For really slow baud rates (or
279 * really fast processors), we may have to further divide by 16.
281 if (((BRG_UART_CLK
/ rate
) - 1) < 4096)
282 out_be32(bp
, (((BRG_UART_CLK
/ rate
) - 1) << 1) | CPM_BRG_EN
);
284 out_be32(bp
, (((BRG_UART_CLK_DIV16
/ rate
) - 1) << 1) |
285 CPM_BRG_EN
| CPM_BRG_DIV16
);
288 struct cpm_ioport16
{
289 __be16 dir
, par
, odr_sor
, dat
, intr
;
293 struct cpm_ioport32
{
294 __be32 dir
, par
, sor
;
297 static void cpm1_set_pin32(int port
, int pin
, int flags
)
299 struct cpm_ioport32 __iomem
*iop
;
300 pin
= 1 << (31 - pin
);
302 if (port
== CPM_PORTB
)
303 iop
= (struct cpm_ioport32 __iomem
*)
304 &mpc8xx_immr
->im_cpm
.cp_pbdir
;
306 iop
= (struct cpm_ioport32 __iomem
*)
307 &mpc8xx_immr
->im_cpm
.cp_pedir
;
309 if (flags
& CPM_PIN_OUTPUT
)
310 setbits32(&iop
->dir
, pin
);
312 clrbits32(&iop
->dir
, pin
);
314 if (!(flags
& CPM_PIN_GPIO
))
315 setbits32(&iop
->par
, pin
);
317 clrbits32(&iop
->par
, pin
);
319 if (port
== CPM_PORTB
) {
320 if (flags
& CPM_PIN_OPENDRAIN
)
321 setbits16(&mpc8xx_immr
->im_cpm
.cp_pbodr
, pin
);
323 clrbits16(&mpc8xx_immr
->im_cpm
.cp_pbodr
, pin
);
326 if (port
== CPM_PORTE
) {
327 if (flags
& CPM_PIN_SECONDARY
)
328 setbits32(&iop
->sor
, pin
);
330 clrbits32(&iop
->sor
, pin
);
332 if (flags
& CPM_PIN_OPENDRAIN
)
333 setbits32(&mpc8xx_immr
->im_cpm
.cp_peodr
, pin
);
335 clrbits32(&mpc8xx_immr
->im_cpm
.cp_peodr
, pin
);
339 static void cpm1_set_pin16(int port
, int pin
, int flags
)
341 struct cpm_ioport16 __iomem
*iop
=
342 (struct cpm_ioport16 __iomem
*)&mpc8xx_immr
->im_ioport
;
344 pin
= 1 << (15 - pin
);
349 if (flags
& CPM_PIN_OUTPUT
)
350 setbits16(&iop
->dir
, pin
);
352 clrbits16(&iop
->dir
, pin
);
354 if (!(flags
& CPM_PIN_GPIO
))
355 setbits16(&iop
->par
, pin
);
357 clrbits16(&iop
->par
, pin
);
359 if (port
== CPM_PORTA
) {
360 if (flags
& CPM_PIN_OPENDRAIN
)
361 setbits16(&iop
->odr_sor
, pin
);
363 clrbits16(&iop
->odr_sor
, pin
);
365 if (port
== CPM_PORTC
) {
366 if (flags
& CPM_PIN_SECONDARY
)
367 setbits16(&iop
->odr_sor
, pin
);
369 clrbits16(&iop
->odr_sor
, pin
);
373 void cpm1_set_pin(enum cpm_port port
, int pin
, int flags
)
375 if (port
== CPM_PORTB
|| port
== CPM_PORTE
)
376 cpm1_set_pin32(port
, pin
, flags
);
378 cpm1_set_pin16(port
, pin
, flags
);
381 int cpm1_clk_setup(enum cpm_clk_target target
, int clock
, int mode
)
389 {CPM_CLK_SCC1
, CPM_BRG1
, 0},
390 {CPM_CLK_SCC1
, CPM_BRG2
, 1},
391 {CPM_CLK_SCC1
, CPM_BRG3
, 2},
392 {CPM_CLK_SCC1
, CPM_BRG4
, 3},
393 {CPM_CLK_SCC1
, CPM_CLK1
, 4},
394 {CPM_CLK_SCC1
, CPM_CLK2
, 5},
395 {CPM_CLK_SCC1
, CPM_CLK3
, 6},
396 {CPM_CLK_SCC1
, CPM_CLK4
, 7},
398 {CPM_CLK_SCC2
, CPM_BRG1
, 0},
399 {CPM_CLK_SCC2
, CPM_BRG2
, 1},
400 {CPM_CLK_SCC2
, CPM_BRG3
, 2},
401 {CPM_CLK_SCC2
, CPM_BRG4
, 3},
402 {CPM_CLK_SCC2
, CPM_CLK1
, 4},
403 {CPM_CLK_SCC2
, CPM_CLK2
, 5},
404 {CPM_CLK_SCC2
, CPM_CLK3
, 6},
405 {CPM_CLK_SCC2
, CPM_CLK4
, 7},
407 {CPM_CLK_SCC3
, CPM_BRG1
, 0},
408 {CPM_CLK_SCC3
, CPM_BRG2
, 1},
409 {CPM_CLK_SCC3
, CPM_BRG3
, 2},
410 {CPM_CLK_SCC3
, CPM_BRG4
, 3},
411 {CPM_CLK_SCC3
, CPM_CLK5
, 4},
412 {CPM_CLK_SCC3
, CPM_CLK6
, 5},
413 {CPM_CLK_SCC3
, CPM_CLK7
, 6},
414 {CPM_CLK_SCC3
, CPM_CLK8
, 7},
416 {CPM_CLK_SCC4
, CPM_BRG1
, 0},
417 {CPM_CLK_SCC4
, CPM_BRG2
, 1},
418 {CPM_CLK_SCC4
, CPM_BRG3
, 2},
419 {CPM_CLK_SCC4
, CPM_BRG4
, 3},
420 {CPM_CLK_SCC4
, CPM_CLK5
, 4},
421 {CPM_CLK_SCC4
, CPM_CLK6
, 5},
422 {CPM_CLK_SCC4
, CPM_CLK7
, 6},
423 {CPM_CLK_SCC4
, CPM_CLK8
, 7},
425 {CPM_CLK_SMC1
, CPM_BRG1
, 0},
426 {CPM_CLK_SMC1
, CPM_BRG2
, 1},
427 {CPM_CLK_SMC1
, CPM_BRG3
, 2},
428 {CPM_CLK_SMC1
, CPM_BRG4
, 3},
429 {CPM_CLK_SMC1
, CPM_CLK1
, 4},
430 {CPM_CLK_SMC1
, CPM_CLK2
, 5},
431 {CPM_CLK_SMC1
, CPM_CLK3
, 6},
432 {CPM_CLK_SMC1
, CPM_CLK4
, 7},
434 {CPM_CLK_SMC2
, CPM_BRG1
, 0},
435 {CPM_CLK_SMC2
, CPM_BRG2
, 1},
436 {CPM_CLK_SMC2
, CPM_BRG3
, 2},
437 {CPM_CLK_SMC2
, CPM_BRG4
, 3},
438 {CPM_CLK_SMC2
, CPM_CLK5
, 4},
439 {CPM_CLK_SMC2
, CPM_CLK6
, 5},
440 {CPM_CLK_SMC2
, CPM_CLK7
, 6},
441 {CPM_CLK_SMC2
, CPM_CLK8
, 7},
446 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
451 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
456 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
461 reg
= &mpc8xx_immr
->im_cpm
.cp_sicr
;
466 reg
= &mpc8xx_immr
->im_cpm
.cp_simode
;
471 reg
= &mpc8xx_immr
->im_cpm
.cp_simode
;
476 printk(KERN_ERR
"cpm1_clock_setup: invalid clock target\n");
480 if (reg
== &mpc8xx_immr
->im_cpm
.cp_sicr
&& mode
== CPM_CLK_RX
)
483 for (i
= 0; i
< ARRAY_SIZE(clk_map
); i
++) {
484 if (clk_map
[i
][0] == target
&& clk_map
[i
][1] == clock
) {
485 bits
= clk_map
[i
][2];
490 if (i
== ARRAY_SIZE(clk_map
)) {
491 printk(KERN_ERR
"cpm1_clock_setup: invalid clock combination\n");
497 out_be32(reg
, (in_be32(reg
) & ~mask
) | bits
);