xref: /OK3568_Linux_fs/u-boot/arch/powerpc/cpu/mpc85xx/commproc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Adapted for Motorola MPC8560 chips
3*4882a593Smuzhiyun  * Xianghua Xiao <x.xiao@motorola.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file is based on "arch/powerpc/8260_io/commproc.c" - here is it's
6*4882a593Smuzhiyun  * copyright notice:
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * General Purpose functions for the global management of the
9*4882a593Smuzhiyun  * 8220 Communication Processor Module.
10*4882a593Smuzhiyun  * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
11*4882a593Smuzhiyun  * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
12*4882a593Smuzhiyun  *	2.3.99 Updates
13*4882a593Smuzhiyun  * Copyright (c) 2003 Motorola,Inc.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * In addition to the individual control of the communication
16*4882a593Smuzhiyun  * channels, there are a few functions that globally affect the
17*4882a593Smuzhiyun  * communication processor.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * Buffer descriptors must be allocated from the dual ported memory
20*4882a593Smuzhiyun  * space.  The allocator for that is here.  When the communication
21*4882a593Smuzhiyun  * process is reset, we reclaim the memory available.  There is
22*4882a593Smuzhiyun  * currently no deallocator for this memory.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun #include <common.h>
25*4882a593Smuzhiyun #include <asm/cpm_85xx.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * because we have stack and init data in dual port ram
31*4882a593Smuzhiyun  * we must reduce the size
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun #undef	CPM_DATAONLY_SIZE
34*4882a593Smuzhiyun #define CPM_DATAONLY_SIZE	((uint)(8 * 1024) - CPM_DATAONLY_BASE)
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun void
m8560_cpm_reset(void)37*4882a593Smuzhiyun m8560_cpm_reset(void)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
40*4882a593Smuzhiyun 	volatile ulong count;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* Reclaim the DP memory for our use.
45*4882a593Smuzhiyun 	*/
46*4882a593Smuzhiyun 	gd->arch.dp_alloc_base = CPM_DATAONLY_BASE;
47*4882a593Smuzhiyun 	gd->arch.dp_alloc_top = gd->arch.dp_alloc_base + CPM_DATAONLY_SIZE;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/*
50*4882a593Smuzhiyun 	 * Reset CPM
51*4882a593Smuzhiyun 	 */
52*4882a593Smuzhiyun 	cpm->im_cpm_cp.cpcr = CPM_CR_RST;
53*4882a593Smuzhiyun 	count = 0;
54*4882a593Smuzhiyun 	do {			/* Spin until command processed		*/
55*4882a593Smuzhiyun 		__asm__ __volatile__ ("eieio");
56*4882a593Smuzhiyun 	} while ((cpm->im_cpm_cp.cpcr & CPM_CR_FLG) && ++count < 1000000);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* Allocate some memory from the dual ported ram.
60*4882a593Smuzhiyun  * To help protocols with object alignment restrictions, we do that
61*4882a593Smuzhiyun  * if they ask.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun uint
m8560_cpm_dpalloc(uint size,uint align)64*4882a593Smuzhiyun m8560_cpm_dpalloc(uint size, uint align)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
67*4882a593Smuzhiyun 	uint	retloc;
68*4882a593Smuzhiyun 	uint	align_mask, off;
69*4882a593Smuzhiyun 	uint	savebase;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	align_mask = align - 1;
72*4882a593Smuzhiyun 	savebase = gd->arch.dp_alloc_base;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	off = gd->arch.dp_alloc_base & align_mask;
75*4882a593Smuzhiyun 	if (off != 0)
76*4882a593Smuzhiyun 		gd->arch.dp_alloc_base += (align - off);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	if ((off = size & align_mask) != 0)
79*4882a593Smuzhiyun 		size += align - off;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if ((gd->arch.dp_alloc_base + size) >= gd->arch.dp_alloc_top) {
82*4882a593Smuzhiyun 		gd->arch.dp_alloc_base = savebase;
83*4882a593Smuzhiyun 		panic("m8560_cpm_dpalloc: ran out of dual port ram!");
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	retloc = gd->arch.dp_alloc_base;
87*4882a593Smuzhiyun 	gd->arch.dp_alloc_base += size;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	memset((void *)&(cpm->im_dprambase[retloc]), 0, size);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	return(retloc);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* We also own one page of host buffer space for the allocation of
95*4882a593Smuzhiyun  * UART "fifos" and the like.
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun uint
m8560_cpm_hostalloc(uint size,uint align)98*4882a593Smuzhiyun m8560_cpm_hostalloc(uint size, uint align)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	/* the host might not even have RAM yet - just use dual port RAM */
101*4882a593Smuzhiyun 	return (m8560_cpm_dpalloc(size, align));
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* Set a baud rate generator.  This needs lots of work.  There are
105*4882a593Smuzhiyun  * eight BRGs, which can be connected to the CPM channels or output
106*4882a593Smuzhiyun  * as clocks.  The BRGs are in two different block of internal
107*4882a593Smuzhiyun  * memory mapped space.
108*4882a593Smuzhiyun  * The baud rate clock is the system clock divided by something.
109*4882a593Smuzhiyun  * It was set up long ago during the initial boot phase and is
110*4882a593Smuzhiyun  * is given to us.
111*4882a593Smuzhiyun  * Baud rate clocks are zero-based in the driver code (as that maps
112*4882a593Smuzhiyun  * to port numbers).  Documentation uses 1-based numbering.
113*4882a593Smuzhiyun  */
114*4882a593Smuzhiyun #define BRG_INT_CLK	gd->arch.brg_clk
115*4882a593Smuzhiyun #define BRG_UART_CLK	((BRG_INT_CLK + 15) / 16)
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* This function is used by UARTS, or anything else that uses a 16x
118*4882a593Smuzhiyun  * oversampled clock.
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun void
m8560_cpm_setbrg(uint brg,uint rate)121*4882a593Smuzhiyun m8560_cpm_setbrg(uint brg, uint rate)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
124*4882a593Smuzhiyun 	volatile uint	*bp;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* This is good enough to get SMCs running.....
127*4882a593Smuzhiyun 	*/
128*4882a593Smuzhiyun 	if (brg < 4) {
129*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg1.brgc1);
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 	else {
132*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg2.brgc5);
133*4882a593Smuzhiyun 		brg -= 4;
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 	bp += brg;
136*4882a593Smuzhiyun 	*bp = (((((BRG_UART_CLK+rate-1)/rate)-1)&0xfff)<<1)|CPM_BRG_EN;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* This function is used to set high speed synchronous baud rate
140*4882a593Smuzhiyun  * clocks.
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun void
m8560_cpm_fastbrg(uint brg,uint rate,int div16)143*4882a593Smuzhiyun m8560_cpm_fastbrg(uint brg, uint rate, int div16)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
146*4882a593Smuzhiyun 	volatile uint	*bp;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	/* This is good enough to get SMCs running.....
149*4882a593Smuzhiyun 	*/
150*4882a593Smuzhiyun 	if (brg < 4) {
151*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg1.brgc1);
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 	else {
154*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg2.brgc5);
155*4882a593Smuzhiyun 		brg -= 4;
156*4882a593Smuzhiyun 	}
157*4882a593Smuzhiyun 	bp += brg;
158*4882a593Smuzhiyun 	*bp = (((((BRG_INT_CLK+rate-1)/rate)-1)&0xfff)<<1)|CPM_BRG_EN;
159*4882a593Smuzhiyun 	if (div16)
160*4882a593Smuzhiyun 		*bp |= CPM_BRG_DIV16;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /* This function is used to set baud rate generators using an external
164*4882a593Smuzhiyun  * clock source and 16x oversampling.
165*4882a593Smuzhiyun  */
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun void
m8560_cpm_extcbrg(uint brg,uint rate,uint extclk,int pinsel)168*4882a593Smuzhiyun m8560_cpm_extcbrg(uint brg, uint rate, uint extclk, int pinsel)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
171*4882a593Smuzhiyun 	volatile uint	*bp;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	if (brg < 4) {
174*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg1.brgc1);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	else {
177*4882a593Smuzhiyun 		bp = (uint *)&(cpm->im_cpm_brg2.brgc5);
178*4882a593Smuzhiyun 		brg -= 4;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 	bp += brg;
181*4882a593Smuzhiyun 	*bp = ((((((extclk/16)+rate-1)/rate)-1)&0xfff)<<1)|CPM_BRG_EN;
182*4882a593Smuzhiyun 	if (pinsel == 0)
183*4882a593Smuzhiyun 		*bp |= CPM_BRG_EXTC_CLK3_9;
184*4882a593Smuzhiyun 	else
185*4882a593Smuzhiyun 		*bp |= CPM_BRG_EXTC_CLK5_15;
186*4882a593Smuzhiyun }
187