xref: /freebsd/sys/arm/mv/armadaxp/armadaxp.c (revision d65cdf4b9d4801a0d60dc77190467c37683f64d6)
116694521SOleksandr Tymoshenko /*-
216694521SOleksandr Tymoshenko  * Copyright (c) 2011 Semihalf.
316694521SOleksandr Tymoshenko  * All rights reserved.
416694521SOleksandr Tymoshenko  *
516694521SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
616694521SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
716694521SOleksandr Tymoshenko  * are met:
816694521SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
916694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
1016694521SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
1116694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
1216694521SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
1316694521SOleksandr Tymoshenko  *
1416694521SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1516694521SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1616694521SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1716694521SOleksandr Tymoshenko  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1816694521SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1916694521SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2016694521SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2116694521SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2216694521SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2316694521SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2416694521SOleksandr Tymoshenko  * SUCH DAMAGE.
2516694521SOleksandr Tymoshenko  *
2616694521SOleksandr Tymoshenko  * From: FreeBSD: src/sys/arm/mv/kirkwood/sheevaplug.c,v 1.2 2010/06/13 13:28:53
2716694521SOleksandr Tymoshenko  */
2816694521SOleksandr Tymoshenko 
2916694521SOleksandr Tymoshenko #include <sys/cdefs.h>
3016694521SOleksandr Tymoshenko __FBSDID("$FreeBSD$");
3116694521SOleksandr Tymoshenko 
3216694521SOleksandr Tymoshenko #include <sys/param.h>
3316694521SOleksandr Tymoshenko #include <sys/systm.h>
3416694521SOleksandr Tymoshenko #include <sys/bus.h>
3516694521SOleksandr Tymoshenko 
3616694521SOleksandr Tymoshenko #include <machine/bus.h>
37*d65cdf4bSGrzegorz Bernacki #include <machine/armreg.h>
3816694521SOleksandr Tymoshenko 
3916694521SOleksandr Tymoshenko #include <arm/mv/mvreg.h>
4016694521SOleksandr Tymoshenko #include <arm/mv/mvvar.h>
4116694521SOleksandr Tymoshenko 
4216694521SOleksandr Tymoshenko #include <dev/fdt/fdt_common.h>
4316694521SOleksandr Tymoshenko #include <dev/ofw/openfirm.h>
4416694521SOleksandr Tymoshenko 
4516694521SOleksandr Tymoshenko #include <machine/fdt.h>
4616694521SOleksandr Tymoshenko 
47*d65cdf4bSGrzegorz Bernacki #define CPU_FREQ_FIELD(sar)	(((0x01 & (sar >> 52)) << 3) | \
48*d65cdf4bSGrzegorz Bernacki 				    (0x07 & (sar >> 21)))
49*d65cdf4bSGrzegorz Bernacki #define FAB_FREQ_FIELD(sar)	(((0x01 & (sar >> 51)) << 4) | \
50*d65cdf4bSGrzegorz Bernacki 				    (0x0F & (sar >> 24)))
51*d65cdf4bSGrzegorz Bernacki 
52*d65cdf4bSGrzegorz Bernacki static uint32_t count_l2clk(void);
53*d65cdf4bSGrzegorz Bernacki 
54*d65cdf4bSGrzegorz Bernacki /* XXX Make gpio driver optional and remove it */
5516694521SOleksandr Tymoshenko struct resource_spec mv_gpio_res[] = {
5616694521SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
5716694521SOleksandr Tymoshenko 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
5816694521SOleksandr Tymoshenko 	{ -1, 0 }
5916694521SOleksandr Tymoshenko };
6016694521SOleksandr Tymoshenko 
61*d65cdf4bSGrzegorz Bernacki struct vco_freq_ratio {
62*d65cdf4bSGrzegorz Bernacki 	uint8_t	vco_cpu;	/* VCO to CLK0(CPU) clock ratio */
63*d65cdf4bSGrzegorz Bernacki 	uint8_t	vco_l2c;	/* VCO to NB(L2 cache) clock ratio */
64*d65cdf4bSGrzegorz Bernacki 	uint8_t	vco_hcl;	/* VCO to HCLK(DDR controller) clock ratio */
65*d65cdf4bSGrzegorz Bernacki 	uint8_t	vco_ddr;	/* VCO to DR(DDR memory) clock ratio */
66*d65cdf4bSGrzegorz Bernacki };
67*d65cdf4bSGrzegorz Bernacki 
68*d65cdf4bSGrzegorz Bernacki static struct vco_freq_ratio freq_conf_table[] = {
69*d65cdf4bSGrzegorz Bernacki /*00*/	{ 1, 1,	 4,  2 },
70*d65cdf4bSGrzegorz Bernacki /*01*/	{ 1, 2,	 2,  2 },
71*d65cdf4bSGrzegorz Bernacki /*02*/	{ 2, 2,	 6,  3 },
72*d65cdf4bSGrzegorz Bernacki /*03*/	{ 2, 2,	 3,  3 },
73*d65cdf4bSGrzegorz Bernacki /*04*/	{ 1, 2,	 3,  3 },
74*d65cdf4bSGrzegorz Bernacki /*05*/	{ 1, 2,	 4,  2 },
75*d65cdf4bSGrzegorz Bernacki /*06*/	{ 1, 1,	 2,  2 },
76*d65cdf4bSGrzegorz Bernacki /*07*/	{ 2, 3,	 6,  6 },
77*d65cdf4bSGrzegorz Bernacki /*08*/	{ 2, 3,	 5,  5 },
78*d65cdf4bSGrzegorz Bernacki /*09*/	{ 1, 2,	 6,  3 },
79*d65cdf4bSGrzegorz Bernacki /*10*/	{ 2, 4,	10,  5 },
80*d65cdf4bSGrzegorz Bernacki /*11*/	{ 1, 3,	 6,  6 },
81*d65cdf4bSGrzegorz Bernacki /*12*/	{ 1, 2,	 5,  5 },
82*d65cdf4bSGrzegorz Bernacki /*13*/	{ 1, 3,	 6,  3 },
83*d65cdf4bSGrzegorz Bernacki /*14*/	{ 1, 2,	 5,  5 },
84*d65cdf4bSGrzegorz Bernacki /*15*/	{ 2, 2,	 5,  5 },
85*d65cdf4bSGrzegorz Bernacki /*16*/	{ 1, 1,	 3,  3 },
86*d65cdf4bSGrzegorz Bernacki /*17*/	{ 2, 5,	10, 10 },
87*d65cdf4bSGrzegorz Bernacki /*18*/	{ 1, 3,	 8,  4 },
88*d65cdf4bSGrzegorz Bernacki /*19*/	{ 1, 1,	 2,  1 },
89*d65cdf4bSGrzegorz Bernacki /*20*/	{ 2, 3,	 6,  3 },
90*d65cdf4bSGrzegorz Bernacki /*21*/	{ 1, 2,	 8,  4 },
91*d65cdf4bSGrzegorz Bernacki /*22*/	{ 2, 5,	10,  5 }
92*d65cdf4bSGrzegorz Bernacki };
93*d65cdf4bSGrzegorz Bernacki 
94*d65cdf4bSGrzegorz Bernacki static uint16_t	cpu_clock_table[] = {
95*d65cdf4bSGrzegorz Bernacki     1000, 1066, 1200, 1333, 1500, 1666, 1800, 2000, 600,  667,  800,  1600,
96*d65cdf4bSGrzegorz Bernacki     2133, 2200, 2400 };
97*d65cdf4bSGrzegorz Bernacki 
9816694521SOleksandr Tymoshenko uint32_t
9916694521SOleksandr Tymoshenko get_tclk(void)
10016694521SOleksandr Tymoshenko {
101*d65cdf4bSGrzegorz Bernacki  	uint32_t cputype;
10216694521SOleksandr Tymoshenko 
103*d65cdf4bSGrzegorz Bernacki 	cputype = cpufunc_id();
104*d65cdf4bSGrzegorz Bernacki 	cputype &= CPU_ID_CPU_MASK;
105*d65cdf4bSGrzegorz Bernacki 
106*d65cdf4bSGrzegorz Bernacki 	if (cputype == CPU_ID_MV88SV584X_V7)
107*d65cdf4bSGrzegorz Bernacki 		return (TCLK_250MHZ);
108*d65cdf4bSGrzegorz Bernacki 	else
10916694521SOleksandr Tymoshenko 		return (TCLK_200MHZ);
11016694521SOleksandr Tymoshenko }
11116694521SOleksandr Tymoshenko 
112*d65cdf4bSGrzegorz Bernacki static uint32_t
113*d65cdf4bSGrzegorz Bernacki count_l2clk(void)
114*d65cdf4bSGrzegorz Bernacki {
115*d65cdf4bSGrzegorz Bernacki 	uint64_t sar_reg;
116*d65cdf4bSGrzegorz Bernacki 	uint32_t freq_vco, freq_l2clk;
117*d65cdf4bSGrzegorz Bernacki 	uint8_t  sar_cpu_freq, sar_fab_freq, array_size;
118*d65cdf4bSGrzegorz Bernacki 
119*d65cdf4bSGrzegorz Bernacki 	/* Get value of the SAR register and process it */
120*d65cdf4bSGrzegorz Bernacki 	sar_reg = get_sar_value();
121*d65cdf4bSGrzegorz Bernacki 	sar_cpu_freq = CPU_FREQ_FIELD(sar_reg);
122*d65cdf4bSGrzegorz Bernacki 	sar_fab_freq = FAB_FREQ_FIELD(sar_reg);
123*d65cdf4bSGrzegorz Bernacki 
124*d65cdf4bSGrzegorz Bernacki 	/* Check if CPU frequency field has correct value */
125*d65cdf4bSGrzegorz Bernacki 	array_size = sizeof(cpu_clock_table) / sizeof(cpu_clock_table[0]);
126*d65cdf4bSGrzegorz Bernacki 	if (sar_cpu_freq >= array_size)
127*d65cdf4bSGrzegorz Bernacki 		panic("Reserved value in cpu frequency configuration field: "
128*d65cdf4bSGrzegorz Bernacki 		    "%d", sar_cpu_freq);
129*d65cdf4bSGrzegorz Bernacki 
130*d65cdf4bSGrzegorz Bernacki 	/* Check if fabric frequency field has correct value */
131*d65cdf4bSGrzegorz Bernacki 	array_size = sizeof(freq_conf_table) / sizeof(freq_conf_table[0]);
132*d65cdf4bSGrzegorz Bernacki 	if (sar_fab_freq >= array_size)
133*d65cdf4bSGrzegorz Bernacki 		panic("Reserved value in fabric frequency configuration field: "
134*d65cdf4bSGrzegorz Bernacki 		    "%d", sar_fab_freq);
135*d65cdf4bSGrzegorz Bernacki 
136*d65cdf4bSGrzegorz Bernacki 	/* Get CPU clock frequency */
137*d65cdf4bSGrzegorz Bernacki 	freq_vco = cpu_clock_table[sar_cpu_freq] *
138*d65cdf4bSGrzegorz Bernacki 	    freq_conf_table[sar_fab_freq].vco_cpu;
139*d65cdf4bSGrzegorz Bernacki 
140*d65cdf4bSGrzegorz Bernacki 	/* Get L2CLK clock frequency */
141*d65cdf4bSGrzegorz Bernacki 	freq_l2clk = freq_vco / freq_conf_table[sar_fab_freq].vco_l2c;
142*d65cdf4bSGrzegorz Bernacki 
143*d65cdf4bSGrzegorz Bernacki 	/* Round L2CLK value to integer MHz */
144*d65cdf4bSGrzegorz Bernacki 	if (((freq_vco % freq_conf_table[sar_fab_freq].vco_l2c) * 10 /
145*d65cdf4bSGrzegorz Bernacki 	    freq_conf_table[sar_fab_freq].vco_l2c) >= 5)
146*d65cdf4bSGrzegorz Bernacki 		freq_l2clk++;
147*d65cdf4bSGrzegorz Bernacki 
148*d65cdf4bSGrzegorz Bernacki 	return (freq_l2clk * 1000000);
149*d65cdf4bSGrzegorz Bernacki }
150*d65cdf4bSGrzegorz Bernacki 
15116694521SOleksandr Tymoshenko uint32_t
15216694521SOleksandr Tymoshenko get_l2clk(void)
15316694521SOleksandr Tymoshenko {
154*d65cdf4bSGrzegorz Bernacki 	static uint32_t	l2clk_freq = 0;
15516694521SOleksandr Tymoshenko 
156*d65cdf4bSGrzegorz Bernacki 	/* If get_l2clk is called first time get L2CLK value from register */
157*d65cdf4bSGrzegorz Bernacki 	if (l2clk_freq == 0)
158*d65cdf4bSGrzegorz Bernacki 		l2clk_freq = count_l2clk();
159*d65cdf4bSGrzegorz Bernacki 
160*d65cdf4bSGrzegorz Bernacki 	return (l2clk_freq);
16116694521SOleksandr Tymoshenko }
16216694521SOleksandr Tymoshenko 
16316694521SOleksandr Tymoshenko int
16416694521SOleksandr Tymoshenko fdt_pci_devmap(phandle_t node, struct pmap_devmap *devmap, vm_offset_t io_va,
16516694521SOleksandr Tymoshenko     vm_offset_t mem_va)
16616694521SOleksandr Tymoshenko {
16716694521SOleksandr Tymoshenko 
16816694521SOleksandr Tymoshenko 	return (0);
16916694521SOleksandr Tymoshenko }
17016694521SOleksandr Tymoshenko 
171