2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Neelkanth Natu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
37 #include <sys/cpuset.h>
39 #include <machine/resource.h>
40 #include <machine/hwfunc.h>
45 * We compile a 32-bit kernel to run on the SB-1 processor which is a 64-bit
46 * processor. It has some registers that must be accessed using 64-bit load
47 * and store instructions.
49 * We use the mips_ld() and mips_sd() functions to do this for us.
51 #define sb_store64(addr, val) mips3_sd((uint64_t *)(uintptr_t)(addr), (val))
52 #define sb_load64(addr) mips3_ld((uint64_t *)(uintptr_t)(addr))
55 * System Control and Debug (SCD) unit on the Sibyte ZBbus.
59 * Extract the value starting at bit position 'b' for 'n' bits from 'x'.
61 #define GET_VAL_64(x, b, n) (((x) >> (b)) & ((1ULL << (n)) - 1))
63 #define SYSREV_ADDR MIPS_PHYS_TO_KSEG1(0x10020000)
64 #define SYSREV_NUM_PROCESSORS(x) GET_VAL_64((x), 24, 4)
66 #define SYSCFG_ADDR MIPS_PHYS_TO_KSEG1(0x10020008)
67 #define SYSCFG_PLLDIV(x) GET_VAL_64((x), 7, 5)
69 #define ZBBUS_CYCLE_COUNT_ADDR MIPS_PHYS_TO_KSEG1(0x10030000)
71 #define INTSRC_MASK_ADDR(cpu) \
72 (MIPS_PHYS_TO_KSEG1(0x10020028) | ((cpu) << 13))
74 #define INTSRC_MAP_ADDR(cpu, intsrc) \
75 (MIPS_PHYS_TO_KSEG1(0x10020200) | ((cpu) << 13)) + (intsrc * 8)
77 #define MAILBOX_SET_ADDR(cpu) \
78 (MIPS_PHYS_TO_KSEG1(0x100200C8) | ((cpu) << 13))
80 #define MAILBOX_CLEAR_ADDR(cpu) \
81 (MIPS_PHYS_TO_KSEG1(0x100200D0) | ((cpu) << 13))
87 return (sb_load64(SYSCFG_ADDR));
91 sb_write_syscfg(uint64_t val)
94 sb_store64(SYSCFG_ADDR, val);
98 sb_zbbus_cycle_count(void)
101 return (sb_load64(ZBBUS_CYCLE_COUNT_ADDR));
108 const uint64_t MHZ = 1000000;
110 plldiv = SYSCFG_PLLDIV(sb_read_syscfg());
112 printf("PLL_DIV is 0 - assuming 6 (300MHz).\n");
116 return (plldiv * 50 * MHZ);
120 sb_system_reset(void)
124 const uint64_t SYSTEM_RESET = 1ULL << 60;
125 const uint64_t EXT_RESET = 1ULL << 59;
126 const uint64_t SOFT_RESET = 1ULL << 58;
128 syscfg = sb_read_syscfg();
129 syscfg &= ~SOFT_RESET;
130 syscfg |= SYSTEM_RESET | EXT_RESET;
131 sb_write_syscfg(syscfg);
135 sb_disable_intsrc(int cpu, int src)
140 regaddr = INTSRC_MASK_ADDR(cpu);
142 val = sb_load64(regaddr);
144 sb_store64(regaddr, val);
148 sb_enable_intsrc(int cpu, int src)
153 regaddr = INTSRC_MASK_ADDR(cpu);
155 val = sb_load64(regaddr);
156 val &= ~(1ULL << src);
157 sb_store64(regaddr, val);
161 sb_write_intsrc_mask(int cpu, uint64_t val)
165 regaddr = INTSRC_MASK_ADDR(cpu);
166 sb_store64(regaddr, val);
170 sb_read_intsrc_mask(int cpu)
175 regaddr = INTSRC_MASK_ADDR(cpu);
176 val = sb_load64(regaddr);
182 sb_write_intmap(int cpu, int intsrc, int intrnum)
186 regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
187 sb_store64(regaddr, intrnum);
191 sb_read_intmap(int cpu, int intsrc)
195 regaddr = INTSRC_MAP_ADDR(cpu, intsrc);
196 return (sb_load64(regaddr) & 0x7);
200 sb_route_intsrc(int intsrc)
204 KASSERT(intsrc >= 0 && intsrc < NUM_INTSRC,
205 ("Invalid interrupt source number (%d)", intsrc));
208 * Interrupt 5 is used by sources internal to the CPU (e.g. timer).
209 * Use a deterministic mapping for the remaining sources.
212 KASSERT(platform_ipi_hardintr_num() == 4,
213 ("Unexpected interrupt number used for IPI"));
214 intrnum = intsrc % 4;
216 intrnum = intsrc % 5;
227 return (sb_load64(SYSREV_ADDR));
231 sb_set_mailbox(int cpu, uint64_t val)
235 regaddr = MAILBOX_SET_ADDR(cpu);
236 sb_store64(regaddr, val);
240 sb_clear_mailbox(int cpu, uint64_t val)
244 regaddr = MAILBOX_CLEAR_ADDR(cpu);
245 sb_store64(regaddr, val);
249 platform_cpu_mask(cpuset_t *mask)
254 s = SYSREV_NUM_PROCESSORS(sb_read_sysrev());
255 for (i = 0; i < s; i++)
260 #define SCD_PHYSADDR 0x10000000
261 #define SCD_SIZE 0x00060000
264 scd_probe(device_t dev)
267 device_set_desc(dev, "Broadcom/Sibyte System Control and Debug");
272 scd_attach(device_t dev)
275 struct resource *res;
278 device_printf(dev, "attached.\n");
281 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, SCD_PHYSADDR,
282 SCD_PHYSADDR + SCD_SIZE - 1, SCD_SIZE, 0);
284 panic("Cannot allocate resource for system control and debug.");
289 static device_method_t scd_methods[] ={
290 /* Device interface */
291 DEVMETHOD(device_probe, scd_probe),
292 DEVMETHOD(device_attach, scd_attach),
293 DEVMETHOD(device_detach, bus_generic_detach),
294 DEVMETHOD(device_shutdown, bus_generic_shutdown),
295 DEVMETHOD(device_suspend, bus_generic_suspend),
296 DEVMETHOD(device_resume, bus_generic_resume),
301 static driver_t scd_driver = {
306 static devclass_t scd_devclass;
308 DRIVER_MODULE(scd, zbbus, scd_driver, scd_devclass, 0, 0);