1 | /* |
---|
2 | * Baud rate generator management functions. |
---|
3 | * |
---|
4 | * This file contains routines for allocating baud rate generators |
---|
5 | * and clock sources to the SCCs and FCCs on the MPC8260. The |
---|
6 | * allocation is a little more complex on this processor because |
---|
7 | * there are restrictions on which brgs and clks can be assigned to |
---|
8 | * a particular port. Rather than coming up with a fixed assignment |
---|
9 | * these routines try to allocate resources sensibly. |
---|
10 | * |
---|
11 | * *** All attempts to allocate a BRG or CLK line should be made via |
---|
12 | * calls to these routines or they simply won't work. |
---|
13 | * |
---|
14 | * Author: Andy Dachs <a.dachs@sstl.co.uk> |
---|
15 | * Copyright Surrey Satellite Technology Limited (SSTL), 2001 |
---|
16 | * |
---|
17 | * Derived in part from work by: |
---|
18 | * |
---|
19 | * Author: Jay Monkman (jmonkman@frasca.com) |
---|
20 | * Copyright (C) 1998 by Frasca International, Inc. |
---|
21 | * and |
---|
22 | * W. Eric Norum |
---|
23 | * Saskatchewan Accelerator Laboratory |
---|
24 | * University of Saskatchewan |
---|
25 | * Saskatoon, Saskatchewan, CANADA |
---|
26 | * eric@skatter.usask.ca |
---|
27 | * |
---|
28 | * The license and distribution terms for this file may be |
---|
29 | * found in the file LICENSE in this distribution or at |
---|
30 | * |
---|
31 | * http://www.rtems.com/license/LICENSE. |
---|
32 | * |
---|
33 | * $Id$ |
---|
34 | */ |
---|
35 | |
---|
36 | #include <rtems.h> |
---|
37 | #include <mpc8260.h> |
---|
38 | #include <mpc8260/cpm.h> |
---|
39 | #include <rtems/bspIo.h> |
---|
40 | |
---|
41 | #define NUM_BRGS 8 |
---|
42 | #define NUM_CLKS 20 |
---|
43 | |
---|
44 | /* Used to track the usage of the baud rate generators */ |
---|
45 | /* (initialised to zeros) */ |
---|
46 | static unsigned long brg_spd[NUM_BRGS]; |
---|
47 | static unsigned int brg_use_count[NUM_BRGS]; |
---|
48 | |
---|
49 | |
---|
50 | /* Used to track the usage of the clock inputs */ |
---|
51 | /* (initialised to zeros) */ |
---|
52 | static unsigned int clk_use_count[NUM_BRGS]; |
---|
53 | |
---|
54 | |
---|
55 | extern rtems_cpu_table Cpu_table; |
---|
56 | |
---|
57 | /* |
---|
58 | * Compute baud-rate-generator configuration register value |
---|
59 | */ |
---|
60 | int |
---|
61 | m8xx_get_brg_cd (int baud) |
---|
62 | { |
---|
63 | int divisor; |
---|
64 | int div16 = 0; |
---|
65 | |
---|
66 | divisor = ((Cpu_table.serial_per_sec) + (baud / 2)) / baud; |
---|
67 | if (divisor > 4096) { |
---|
68 | div16 = 1; |
---|
69 | divisor = (divisor + 8) / 16; |
---|
70 | } |
---|
71 | return M8260_BRG_EN | M8260_BRG_EXTC_BRGCLK | |
---|
72 | ((divisor - 1) << 1) | div16; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /* |
---|
77 | * Allocates an existing brg if one is already programmed for the same |
---|
78 | * baud rate. Otherwise a new brg is assigned |
---|
79 | * AFD: on the mpc8260 only some combinations of SCC/SMC and BRG are allowed |
---|
80 | * so add a mask which specifies which of the BRGs we can choose from |
---|
81 | */ |
---|
82 | int |
---|
83 | m8xx_get_brg(unsigned brgmask, int baud) |
---|
84 | { |
---|
85 | int i; |
---|
86 | |
---|
87 | /* first try to find a BRG that is already at the right speed */ |
---|
88 | for ( i = 0; i < NUM_BRGS; i++ ) { |
---|
89 | if( (1 << i) & brgmask ) /* is this brg allowed? */ |
---|
90 | if ( brg_spd[i] == baud ) { |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if ( i == NUM_BRGS ) { /* I guess we didn't find one */ |
---|
96 | for ( i = 0; i < NUM_BRGS; i++ ) { |
---|
97 | if (((1<<i) & brgmask) && (brg_use_count[i] == 0)) { |
---|
98 | break; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | if (i != NUM_BRGS) { |
---|
103 | brg_use_count[i]++; |
---|
104 | brg_spd[i]=baud; |
---|
105 | switch (i) { |
---|
106 | case 0: |
---|
107 | m8260.brgc1 = M8260_BRG_RST; |
---|
108 | m8260.brgc1 = m8xx_get_brg_cd(baud); |
---|
109 | break; |
---|
110 | case 1: |
---|
111 | m8260.brgc2 = M8260_BRG_RST; |
---|
112 | m8260.brgc2 = m8xx_get_brg_cd(baud); |
---|
113 | break; |
---|
114 | case 2: |
---|
115 | m8260.brgc3 = M8260_BRG_RST; |
---|
116 | m8260.brgc3 = m8xx_get_brg_cd(baud); |
---|
117 | break; |
---|
118 | case 3: |
---|
119 | m8260.brgc4 = M8260_BRG_RST; |
---|
120 | m8260.brgc4 = m8xx_get_brg_cd(baud); |
---|
121 | break; |
---|
122 | case 4: |
---|
123 | m8260.brgc5 = M8260_BRG_RST; |
---|
124 | m8260.brgc5 = m8xx_get_brg_cd(baud); |
---|
125 | break; |
---|
126 | case 5: |
---|
127 | m8260.brgc6 = M8260_BRG_RST; |
---|
128 | m8260.brgc6 = m8xx_get_brg_cd(baud); |
---|
129 | break; |
---|
130 | case 6: |
---|
131 | m8260.brgc7 = M8260_BRG_RST; |
---|
132 | m8260.brgc7 = m8xx_get_brg_cd(baud); |
---|
133 | break; |
---|
134 | case 7: |
---|
135 | m8260.brgc8 = M8260_BRG_RST; |
---|
136 | m8260.brgc8 = m8xx_get_brg_cd(baud); |
---|
137 | break; |
---|
138 | } |
---|
139 | return i; |
---|
140 | } |
---|
141 | |
---|
142 | else { |
---|
143 | printk( "Could not assign a brg for %d\n", baud ); |
---|
144 | return -1; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /* |
---|
150 | * When the brg is no longer needed call this routine to free the |
---|
151 | * resource for re--use. |
---|
152 | */ |
---|
153 | void |
---|
154 | m8xx_free_brg( int brg_num ) |
---|
155 | { |
---|
156 | if( (brg_num>=0) && (brg_num<NUM_BRGS) ) |
---|
157 | if(brg_use_count[brg_num] > 0 ) |
---|
158 | brg_use_count[brg_num]--; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | void m8xx_dump_brgs( void ) |
---|
163 | { |
---|
164 | int i; |
---|
165 | for(i=0; i<NUM_BRGS; i++ ) |
---|
166 | printk( "Brg[%d]: %d %d\n", i, brg_use_count[i], brg_spd[i] ); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | /* |
---|
171 | * Reserve one of a range of clock inputs |
---|
172 | */ |
---|
173 | int |
---|
174 | m8xx_get_clk( unsigned clkmask ) |
---|
175 | { |
---|
176 | int i; |
---|
177 | |
---|
178 | for ( i = 0; i < NUM_CLKS; i++ ) { |
---|
179 | if (((1<<i) & clkmask) && (clk_use_count[i] == 0)) { |
---|
180 | break; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | if (i != NUM_CLKS) { |
---|
185 | clk_use_count[i]++; |
---|
186 | return i; |
---|
187 | } else { |
---|
188 | printk( "Could not assign clock in the range %X\n", clkmask ); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /* |
---|
195 | * When the clock is no longer needed call this routine to free the |
---|
196 | * resource for re--use. |
---|
197 | */ |
---|
198 | void |
---|
199 | m8xx_free_clk( int clk_num ) |
---|
200 | { |
---|
201 | if( (clk_num>=0) && (clk_num<NUM_BRGS) ) |
---|
202 | if(clk_use_count[clk_num] > 0 ) |
---|
203 | clk_use_count[clk_num]--; |
---|
204 | } |
---|