source: rtems/bsps/powerpc/mpc8260ads/start/brg.c @ 9964895

5
Last change on this file since 9964895 was a12dcff8, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 23, 2018 at 3:04:18 PM

bsp/mpc8260: Move libcpu content to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

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