2 * Copyright (c) 2010 Adrian Chadd
4 * Copyright (c) 2016, Hiroki Mori
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
41 #include <sys/reboot.h>
44 #include <vm/vm_page.h>
46 #include <net/ethernet.h>
48 #include <machine/clock.h>
49 #include <machine/cpu.h>
50 #include <machine/cpuregs.h>
51 #include <machine/hwfunc.h>
52 #include <machine/md_var.h>
53 #include <machine/trap.h>
54 #include <machine/vmparam.h>
56 #include <mips/atheros/ar531x/ar5315reg.h>
57 #include <mips/atheros/ar531x/ar5315_chip.h>
58 #include <mips/atheros/ar531x/ar5315_cpudef.h>
60 /* XXX these shouldn't be in here - this file is a per-chip file */
61 /* XXX these should be in the top-level ar5315 type, not ar5315 -chip */
62 uint32_t u_ar531x_cpu_freq;
63 uint32_t u_ar531x_ahb_freq;
64 uint32_t u_ar531x_ddr_freq;
66 uint32_t u_ar531x_uart_addr;
68 uint32_t u_ar531x_gpio_di;
69 uint32_t u_ar531x_gpio_do;
70 uint32_t u_ar531x_gpio_cr;
71 uint32_t u_ar531x_gpio_pins;
73 uint32_t u_ar531x_wdog_ctl;
74 uint32_t u_ar531x_wdog_timer;
77 ar5315_chip_detect_mem_size(void)
80 uint32_t memcfg, cw, rw, dw;
83 * Determine the memory size. We query the board info.
85 memcfg = ATH_READ_REG(AR5315_SDRAMCTL_BASE + AR5315_SDRAMCTL_MEM_CFG);
86 cw = __SHIFTOUT(memcfg, AR5315_MEM_CFG_COL_WIDTH);
88 rw = __SHIFTOUT(memcfg, AR5315_MEM_CFG_ROW_WIDTH);
91 /* XXX: according to redboot, this could be wrong if DDR SDRAM */
92 dw = __SHIFTOUT(memcfg, AR5315_MEM_CFG_DATA_WIDTH);
96 /* not too sure about this math, but it _seems_ to add up */
97 memsize = (1 << cw) * (1 << rw) * dw;
99 printf("SDRAM_MEM_CFG =%x, cw=%d rw=%d dw=%d xmemsize=%d\n", memcfg,
100 cw, rw, dw, memsize);
106 ar5315_chip_detect_sys_frequency(void)
108 uint32_t freq_ref, freq_pll;
109 static const uint8_t pll_divide_table[] = {
112 * these entries are bogus, but it avoids a possible
113 * bad table dereference
117 static const uint8_t pre_divide_table[] = {
121 const uint32_t pllc = ATH_READ_REG(AR5315_SYSREG_BASE +
122 AR5315_SYSREG_PLLC_CTL);
124 const uint32_t refdiv = pre_divide_table[AR5315_PLLC_REF_DIV(pllc)];
125 const uint32_t fbdiv = AR5315_PLLC_FB_DIV(pllc);
126 const uint32_t div2 = (AR5315_PLLC_DIV_2(pllc) + 1) * 2; /* results in 2 or 4 */
130 /* 40MHz reference clk, reference and feedback dividers */
131 freq_pll = (freq_ref / refdiv) * div2 * fbdiv;
133 const uint32_t pllout[4] = {
135 [0] = freq_pll / pll_divide_table[AR5315_PLLC_CLKM(pllc)],
136 [1] = freq_pll / pll_divide_table[AR5315_PLLC_CLKM(pllc)],
139 [2] = freq_pll / pll_divide_table[AR5315_PLLC_CLKC(pllc)],
142 [3] = freq_ref, /* use original reference clock */
145 const uint32_t amba_clkctl = ATH_READ_REG(AR5315_SYSREG_BASE +
146 AR5315_SYSREG_AMBACLK);
147 uint32_t ambadiv = AR5315_CLOCKCTL_DIV(amba_clkctl);
148 ambadiv = ambadiv ? (ambadiv * 2) : 1;
149 u_ar531x_ahb_freq = pllout[AR5315_CLOCKCTL_SELECT(amba_clkctl)] / ambadiv;
151 const uint32_t cpu_clkctl = ATH_READ_REG(AR5315_SYSREG_BASE +
152 AR5315_SYSREG_CPUCLK);
153 uint32_t cpudiv = AR5315_CLOCKCTL_DIV(cpu_clkctl);
154 cpudiv = cpudiv ? (cpudiv * 2) : 1;
155 u_ar531x_cpu_freq = pllout[AR5315_CLOCKCTL_SELECT(cpu_clkctl)] / cpudiv;
157 u_ar531x_ddr_freq = 0;
161 * This does not lock the CPU whilst doing the work!
164 ar5315_chip_device_reset(void)
166 ATH_WRITE_REG(AR5315_SYSREG_BASE + AR5315_SYSREG_COLDRESET,
167 AR5315_COLD_AHB | AR5315_COLD_APB | AR5315_COLD_CPU);
171 ar5315_chip_device_start(void)
173 ATH_WRITE_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_AHB_ERR0,
174 AR5315_AHB_ERROR_DET);
175 ATH_READ_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_AHB_ERR1);
176 ATH_WRITE_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_WDOG_CTL,
177 AR5315_WDOG_CTL_IGNORE);
179 // set Ethernet AHB master arbitration control
180 // Maybe RedBoot was enabled. But to make sure.
181 ATH_WRITE_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_AHB_ARB_CTL,
182 ATH_READ_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_AHB_ARB_CTL) |
185 // set Ethernet controller byteswap control
187 ATH_WRITE_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_ENDIAN,
188 ATH_READ_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_ENDIAN) |
191 /* Disable interrupts for all gpio pins. */
192 ATH_WRITE_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_GPIO_INT, 0);
194 printf("AHB Master Arbitration Control %08x\n",
195 ATH_READ_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_AHB_ARB_CTL));
196 printf("Byteswap Control %08x\n",
197 ATH_READ_REG(AR5315_SYSREG_BASE+AR5315_SYSREG_ENDIAN));
201 ar5315_chip_device_stopped(uint32_t mask)
205 reg = ATH_READ_REG(AR5315_SYSREG_BASE + AR5315_SYSREG_COLDRESET);
206 return ((reg & mask) == mask);
210 ar5315_chip_set_mii_speed(uint32_t unit, uint32_t speed)
214 /* Speed is either 10, 100 or 1000 */
216 ar5315_chip_set_pll_ge(int unit, int speed)
221 ar5315_chip_ddr_flush_ge(int unit)
226 ar5315_chip_soc_init(void)
228 u_ar531x_uart_addr = MIPS_PHYS_TO_KSEG1(AR5315_UART_BASE);
230 u_ar531x_gpio_di = AR5315_SYSREG_GPIO_DI;
231 u_ar531x_gpio_do = AR5315_SYSREG_GPIO_DO;
232 u_ar531x_gpio_cr = AR5315_SYSREG_GPIO_CR;
233 u_ar531x_gpio_pins = AR5315_GPIO_PINS;
235 u_ar531x_wdog_ctl = AR5315_SYSREG_WDOG_CTL;
236 u_ar531x_wdog_timer = AR5315_SYSREG_WDOG_TIMER;
240 ar5315_chip_get_eth_pll(unsigned int mac, int speed)
245 struct ar5315_cpu_def ar5315_chip_def = {
246 &ar5315_chip_detect_mem_size,
247 &ar5315_chip_detect_sys_frequency,
248 &ar5315_chip_device_reset,
249 &ar5315_chip_device_start,
250 &ar5315_chip_device_stopped,
251 &ar5315_chip_set_pll_ge,
252 &ar5315_chip_set_mii_speed,
253 &ar5315_chip_ddr_flush_ge,
254 &ar5315_chip_get_eth_pll,
255 &ar5315_chip_soc_init,