xref: /freebsd-13-stable/sys/mips/nlm/hal/nlm_hal.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * NETLOGIC_BSD */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 
37 #include <mips/nlm/hal/mips-extns.h>
38 #include <mips/nlm/hal/haldefs.h>
39 #include <mips/nlm/hal/iomap.h>
40 #include <mips/nlm/hal/sys.h>
41 #include <mips/nlm/xlp.h>
42 
43 uint32_t
xlp_get_cpu_frequency(int node,int core)44 xlp_get_cpu_frequency(int node, int core)
45 {
46 	uint64_t sysbase = nlm_get_sys_regbase(node);
47 	unsigned int pll_divf, pll_divr, dfs_div, ext_div;
48 	unsigned int rstval, dfsval;
49 
50 	rstval = nlm_read_sys_reg(sysbase, SYS_POWER_ON_RESET_CFG);
51 	dfsval = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIV_VALUE);
52 	pll_divf = ((rstval >> 10) & 0x7f) + 1;
53 	pll_divr = ((rstval >> 8)  & 0x3) + 1;
54 	if (!nlm_is_xlp8xx_ax())
55 		ext_div = ((rstval >> 30) & 0x3) + 1;
56 	else
57 		ext_div = 1;
58 	dfs_div  = ((dfsval >> (core << 2)) & 0xf) + 1;
59 
60 	return ((800000000ULL * pll_divf)/(3 * pll_divr * ext_div * dfs_div));
61 }
62 
63 static u_int
nlm_get_device_frequency(uint64_t sysbase,int devtype)64 nlm_get_device_frequency(uint64_t sysbase, int devtype)
65 {
66 	uint32_t pllctrl, dfsdiv, spf, spr, div_val;
67 	int extra_div;
68 
69 	pllctrl = nlm_read_sys_reg(sysbase, SYS_PLL_CTRL);
70 	if (devtype <= 7)
71 		div_val = nlm_read_sys_reg(sysbase, SYS_DFS_DIV_VALUE0);
72 	else {
73 		devtype -= 8;
74 		div_val = nlm_read_sys_reg(sysbase, SYS_DFS_DIV_VALUE1);
75 	}
76 	dfsdiv = ((div_val >> (devtype << 2)) & 0xf) + 1;
77 	spf = (pllctrl >> 3 & 0x7f) + 1;
78 	spr = (pllctrl >> 1 & 0x03) + 1;
79 	if (devtype == DFS_DEVICE_NAE && !nlm_is_xlp8xx_ax())
80 		extra_div = 2;
81 	else
82 		extra_div = 1;
83 
84 	return ((400 * spf) / (3 * extra_div * spr * dfsdiv));
85 }
86 
87 int
nlm_set_device_frequency(int node,int devtype,int frequency)88 nlm_set_device_frequency(int node, int devtype, int frequency)
89 {
90 	uint64_t sysbase;
91 	u_int cur_freq;
92 	int dec_div;
93 
94 	sysbase = nlm_get_sys_regbase(node);
95 	cur_freq = nlm_get_device_frequency(sysbase, devtype);
96 	if (cur_freq < (frequency - 5))
97 		dec_div = 1;
98 	else
99 		dec_div = 0;
100 
101 	for(;;) {
102 		if ((cur_freq >= (frequency - 5)) && (cur_freq <= frequency))
103 			break;
104 		if (dec_div)
105 			nlm_write_sys_reg(sysbase, SYS_DFS_DIV_DEC_CTRL,
106 			    (1 << devtype));
107 		else
108 			nlm_write_sys_reg(sysbase, SYS_DFS_DIV_INC_CTRL,
109 			    (1 << devtype));
110 		cur_freq = nlm_get_device_frequency(sysbase, devtype);
111 	}
112 	return (nlm_get_device_frequency(sysbase, devtype));
113 }
114