1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
8 * enum ratio - Description of a core clock ratio
9 * @RAT_UNK: Unknown ratio
11 * @RAT_1_TO_8: Ratio 1:8
12 * @RAT_1_TO_4: Ratio 1:4
13 * @RAT_1_TO_2: Ratio 1:2
14 * @RAT_1_TO_1: Ratio 1:1
15 * @RAT_1_5_TO_1: Ratio 1.5:1
16 * @RAT_2_TO_1: Ratio 2:1
17 * @RAT_2_5_TO_1: Ratio 2.5:1
18 * @RAT_3_TO_1: Ratio 3:1
34 * struct corecnf - Description for a core clock configuration
35 * @core_csb_ratio: Core clock frequency to CSB clock frequency ratio
36 * @vco_divider: VCO divider (Core VCO frequency = Core frequency * VCO divider)
44 * Table with all valid Core CSB frequency ratio / VCO divider combinations as
45 * indexed by the COREPLL field of the SPMR
47 static const struct corecnf corecnf_tab[] = {
48 {RAT_BYP, RAT_BYP}, /* 0x00 */
49 {RAT_BYP, RAT_BYP}, /* 0x01 */
50 {RAT_BYP, RAT_BYP}, /* 0x02 */
51 {RAT_BYP, RAT_BYP}, /* 0x03 */
52 {RAT_BYP, RAT_BYP}, /* 0x04 */
53 {RAT_BYP, RAT_BYP}, /* 0x05 */
54 {RAT_BYP, RAT_BYP}, /* 0x06 */
55 {RAT_BYP, RAT_BYP}, /* 0x07 */
56 {RAT_1_TO_1, RAT_1_TO_2}, /* 0x08 */
57 {RAT_1_TO_1, RAT_1_TO_4}, /* 0x09 */
58 {RAT_1_TO_1, RAT_1_TO_8}, /* 0x0A */
59 {RAT_1_TO_1, RAT_1_TO_8}, /* 0x0B */
60 {RAT_1_5_TO_1, RAT_1_TO_2}, /* 0x0C */
61 {RAT_1_5_TO_1, RAT_1_TO_4}, /* 0x0D */
62 {RAT_1_5_TO_1, RAT_1_TO_8}, /* 0x0E */
63 {RAT_1_5_TO_1, RAT_1_TO_8}, /* 0x0F */
64 {RAT_2_TO_1, RAT_1_TO_2}, /* 0x10 */
65 {RAT_2_TO_1, RAT_1_TO_4}, /* 0x11 */
66 {RAT_2_TO_1, RAT_1_TO_8}, /* 0x12 */
67 {RAT_2_TO_1, RAT_1_TO_8}, /* 0x13 */
68 {RAT_2_5_TO_1, RAT_1_TO_2}, /* 0x14 */
69 {RAT_2_5_TO_1, RAT_1_TO_4}, /* 0x15 */
70 {RAT_2_5_TO_1, RAT_1_TO_8}, /* 0x16 */
71 {RAT_2_5_TO_1, RAT_1_TO_8}, /* 0x17 */
72 {RAT_3_TO_1, RAT_1_TO_2}, /* 0x18 */
73 {RAT_3_TO_1, RAT_1_TO_4}, /* 0x19 */
74 {RAT_3_TO_1, RAT_1_TO_8}, /* 0x1A */
75 {RAT_3_TO_1, RAT_1_TO_8}, /* 0x1B */
79 * enum reg_type - Register to read a field from
80 * @REG_SCCR: Use the SCCR register
81 * @REG_SPMR: Use the SPMR register
89 * enum mode_type - Description of how to read a specific frequency value
90 * @TYPE_INVALID: Unknown type, will provoke error
91 * @TYPE_SCCR_STANDARD: Read a field from the SCCR register, and use it
92 * as a divider for the CSB clock to compute the
94 * @TYPE_SCCR_ONOFF: The field describes a bit flag that can turn the
96 * @TYPE_SPMR_DIRECT_MULTIPLY: Read a field from the SPMR register, and use it
97 * as a multiplier for the CSB clock to compute the
99 * @TYPE_SPECIAL: The frequency is calculated in a non-standard way
105 TYPE_SPMR_DIRECT_MULTIPLY,
109 /* Map of each clock index to its human-readable name */
110 static const char * const names[] = {
111 [MPC83XX_CLK_CORE] = "Core",
112 [MPC83XX_CLK_CSB] = "Coherent System Bus",
113 [MPC83XX_CLK_QE] = "QE",
114 [MPC83XX_CLK_BRG] = "BRG",
115 [MPC83XX_CLK_LBIU] = "Local Bus Controller",
116 [MPC83XX_CLK_LCLK] = "Local Bus",
117 [MPC83XX_CLK_MEM] = "DDR",
118 [MPC83XX_CLK_MEM_SEC] = "DDR Secondary",
119 [MPC83XX_CLK_ENC] = "SEC",
120 [MPC83XX_CLK_I2C1] = "I2C1",
121 [MPC83XX_CLK_I2C2] = "I2C2",
122 [MPC83XX_CLK_TDM] = "TDM",
123 [MPC83XX_CLK_SDHC] = "SDHC",
124 [MPC83XX_CLK_TSEC1] = "TSEC1",
125 [MPC83XX_CLK_TSEC2] = "TSEC2",
126 [MPC83XX_CLK_USBDR] = "USB DR",
127 [MPC83XX_CLK_USBMPH] = "USB MPH",
128 [MPC83XX_CLK_PCIEXP1] = "PCIEXP1",
129 [MPC83XX_CLK_PCIEXP2] = "PCIEXP2",
130 [MPC83XX_CLK_SATA] = "SATA",
131 [MPC83XX_CLK_DMAC] = "DMAC",
132 [MPC83XX_CLK_PCI] = "PCI",
136 * struct clk_mode - Structure for clock mode descriiptions
137 * @low: The low bit of the data field to read for this mode (may not apply to
139 * @high: The high bit of the data field to read for this mode (may not apply to
141 * @type: The type of the mode description (one of enum mode_type)
150 * set_mode() - Build a clock mode description from data
151 * @mode: The clock mode description to be filled out
152 * @low: The low bit of the data field to read for this mode (may not apply to
154 * @high: The high bit of the data field to read for this mode (may not apply to
156 * @type: The type of the mode description (one of enum mode_type)
158 * Clock mode descriptions are a succinct description of how to read a specific
159 * clock's rate from the hardware; usually by reading a specific field of a
160 * register, such a s the SCCR register, but some types use different methods
161 * for obtaining the clock rate.
163 static void set_mode(struct clk_mode *mode, u8 low, u8 high, int type)
171 * retrieve_mode() - Get the clock mode description for a specific clock
172 * @clk: The identifier of the clock for which the clock description should
174 * @soc_type: The type of MPC83xx SoC for which the clock description should be
176 * @mode: Pointer to a clk_mode structure to be filled with data for the
179 * Since some clock rate are stored in different places on different MPC83xx
180 * SoCs, the SoC type has to be supplied along with the clock's identifier.
182 * Return: 0 if OK, -ve on error
184 static int retrieve_mode(int clk, int soc_type, struct clk_mode *mode)
187 case MPC83XX_CLK_CORE:
188 case MPC83XX_CLK_CSB:
190 case MPC83XX_CLK_BRG:
191 case MPC83XX_CLK_LCLK:
192 case MPC83XX_CLK_I2C2:
193 set_mode(mode, 0, 0, TYPE_SPECIAL);
195 case MPC83XX_CLK_MEM:
196 set_mode(mode, 1, 1, TYPE_SPMR_DIRECT_MULTIPLY);
198 case MPC83XX_CLK_LBIU:
199 case MPC83XX_CLK_MEM_SEC:
200 set_mode(mode, 0, 0, TYPE_SPMR_DIRECT_MULTIPLY);
202 case MPC83XX_CLK_TSEC1:
203 set_mode(mode, 0, 1, TYPE_SCCR_STANDARD);
205 case MPC83XX_CLK_TSEC2:
206 if (soc_type == SOC_MPC8313) /* I2C and TSEC2 are the same register */
207 set_mode(mode, 2, 3, TYPE_SCCR_STANDARD);
208 else /* FIXME(mario.six@gdsys.cc): This has separate enable/disable bit! */
209 set_mode(mode, 0, 1, TYPE_SCCR_STANDARD);
211 case MPC83XX_CLK_SDHC:
212 set_mode(mode, 4, 5, TYPE_SCCR_STANDARD);
214 case MPC83XX_CLK_ENC:
215 set_mode(mode, 6, 7, TYPE_SCCR_STANDARD);
217 case MPC83XX_CLK_I2C1:
218 if (soc_type == SOC_MPC8349)
219 set_mode(mode, 2, 3, TYPE_SCCR_STANDARD);
220 else /* I2C and ENC are the same register */
221 set_mode(mode, 6, 7, TYPE_SCCR_STANDARD);
223 case MPC83XX_CLK_PCIEXP1:
224 set_mode(mode, 10, 11, TYPE_SCCR_STANDARD);
226 case MPC83XX_CLK_PCIEXP2:
227 set_mode(mode, 12, 13, TYPE_SCCR_STANDARD);
229 case MPC83XX_CLK_USBDR:
230 if (soc_type == SOC_MPC8313 || soc_type == SOC_MPC8349)
231 set_mode(mode, 10, 11, TYPE_SCCR_STANDARD);
233 set_mode(mode, 8, 9, TYPE_SCCR_STANDARD);
235 case MPC83XX_CLK_USBMPH:
236 set_mode(mode, 8, 9, TYPE_SCCR_STANDARD);
238 case MPC83XX_CLK_PCI:
239 set_mode(mode, 15, 15, TYPE_SCCR_ONOFF);
241 case MPC83XX_CLK_DMAC:
242 set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
244 case MPC83XX_CLK_SATA:
245 /* FIXME(mario.six@gdsys.cc): All SATA controllers must have the same clock ratio */
246 if (soc_type == SOC_MPC8379) {
247 set_mode(mode, 24, 25, TYPE_SCCR_STANDARD);
248 set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
249 set_mode(mode, 28, 29, TYPE_SCCR_STANDARD);
250 set_mode(mode, 30, 31, TYPE_SCCR_STANDARD);
252 set_mode(mode, 18, 19, TYPE_SCCR_STANDARD);
253 set_mode(mode, 20, 21, TYPE_SCCR_STANDARD);
256 case MPC83XX_CLK_TDM:
257 set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
260 debug("%s: Unknown clock type %d on soc type %d\n",
261 __func__, clk, soc_type);
262 set_mode(mode, 0, 0, TYPE_INVALID);
270 * get_spmr() - Read the SPMR (System PLL Mode Register)
271 * @im: Pointer to the MPC83xx main register map in question
273 * Return: The SPMR value as a 32-bit number.
275 static inline u32 get_spmr(immap_t *im)
277 u32 res = in_be32(&im->clk.spmr);
283 * get_sccr() - Read the SCCR (System Clock Control Register)
284 * @im: Pointer to the MPC83xx main register map in question
286 * Return: The SCCR value as a 32-bit number.
288 static inline u32 get_sccr(immap_t *im)
290 u32 res = in_be32(&im->clk.sccr);
296 * get_lcrr() - Read the LCRR (Clock Ratio Register)
297 * @im: Pointer to the MPC83xx main register map in question
299 * Return: The LCRR value as a 32-bit number.
301 static inline u32 get_lcrr(immap_t *im)
303 u32 res = in_be32(&im->im_lbc.lcrr);
309 * get_pci_sync_in() - Read the PCI synchronization clock speed
310 * @im: Pointer to the MPC83xx main register map in question
312 * Return: The PCI synchronization clock speed value as a 32-bit number.
314 static inline u32 get_pci_sync_in(immap_t *im)
318 clkin_div = (get_spmr(im) & SPMR_CKID) >> SPMR_CKID_SHIFT;
319 return CONFIG_SYS_CLK_FREQ / (1 + clkin_div);
323 * get_csb_clk() - Read the CSB (Coheren System Bus) clock speed
324 * @im: Pointer to the MPC83xx main register map in question
326 * Return: The CSB clock speed value as a 32-bit number.
328 static inline u32 get_csb_clk(immap_t *im)
332 spmf = (get_spmr(im) & SPMR_SPMF) >> SPMR_SPMF_SHIFT;
333 return CONFIG_SYS_CLK_FREQ * spmf;
337 * spmr_field() - Read a specific SPMR field
338 * @im: Pointer to the MPC83xx main register map in question
339 * @mask: A bitmask that describes the bitfield to be read
341 * Return: The value of the bit field as a 32-bit number.
343 static inline uint spmr_field(immap_t *im, u32 mask)
345 /* Extract shift from bitmask */
346 uint shift = mask ? ffs(mask) - 1 : 0;
348 return (get_spmr(im) & mask) >> shift;
352 * sccr_field() - Read a specific SCCR field
353 * @im: Pointer to the MPC83xx main register map in question
354 * @mask: A bitmask that describes the bitfield to be read
356 * Return: The value of the bit field as a 32-bit number.
358 static inline uint sccr_field(immap_t *im, u32 mask)
360 /* Extract shift from bitmask */
361 uint shift = mask ? ffs(mask) - 1 : 0;
363 return (get_sccr(im) & mask) >> shift;
367 * lcrr_field() - Read a specific LCRR field
368 * @im: Pointer to the MPC83xx main register map in question
369 * @mask: A bitmask that describes the bitfield to be read
371 * Return: The value of the bit field as a 32-bit number.
373 static inline uint lcrr_field(immap_t *im, u32 mask)
375 /* Extract shift from bitmask */
376 uint shift = mask ? ffs(mask) - 1 : 0;
378 return (get_lcrr(im) & mask) >> shift;