1*77a7b674Sjmcneill /* $NetBSD: mainbus.c,v 1.10 2021/09/06 14:03:18 jmcneill Exp $ */
2f693c922Shikaru
3f693c922Shikaru /*
4f693c922Shikaru * Copyright (c) 2007
5f693c922Shikaru * Internet Initiative Japan, Inc. All rights reserved.
6f693c922Shikaru *
7f693c922Shikaru * Redistribution and use in source and binary forms, with or without
8f693c922Shikaru * modification, are permitted provided that the following conditions
9f693c922Shikaru * are met:
10f693c922Shikaru * 1. Redistributions of source code must retain the above copyright
11f693c922Shikaru * notice, this list of conditions and the following disclaimer.
12f693c922Shikaru * 2. Redistributions in binary form must reproduce the above copyright
13f693c922Shikaru * notice, this list of conditions and the following disclaimer in the
14f693c922Shikaru * documentation and/or other materials provided with the distribution.
15f693c922Shikaru *
16f693c922Shikaru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17f693c922Shikaru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f693c922Shikaru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f693c922Shikaru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20f693c922Shikaru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f693c922Shikaru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f693c922Shikaru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f693c922Shikaru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f693c922Shikaru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f693c922Shikaru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f693c922Shikaru * SUCH DAMAGE.
27f693c922Shikaru */
28f693c922Shikaru
29f693c922Shikaru #include <sys/cdefs.h>
30*77a7b674Sjmcneill __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.10 2021/09/06 14:03:18 jmcneill Exp $");
3184507484Sjmcneill
3284507484Sjmcneill #define _MIPS_BUS_DMA_PRIVATE
33f693c922Shikaru
34f693c922Shikaru #include <sys/param.h>
35f693c922Shikaru #include <sys/systm.h>
36f693c922Shikaru #include <sys/device.h>
3784507484Sjmcneill #include <sys/bus.h>
38f693c922Shikaru
39f693c922Shikaru #include <mips/cavium/include/mainbusvar.h>
4092b28f32Sjmcneill #include <mips/cavium/octeonvar.h>
41f693c922Shikaru
4284507484Sjmcneill #include <dev/fdt/fdtvar.h>
4384507484Sjmcneill
44f693c922Shikaru static int mainbus_match(device_t, struct cfdata *, void *);
45f693c922Shikaru static void mainbus_attach(device_t, device_t, void *);
4684507484Sjmcneill static void mainbus_attach_static(device_t);
4784507484Sjmcneill static void mainbus_attach_devicetree(device_t);
48f693c922Shikaru static int mainbus_submatch(device_t, cfdata_t, const int *, void *);
49f693c922Shikaru static int mainbus_print(void *, const char *);
50f693c922Shikaru
5184507484Sjmcneill static void simplebus_bus_io_init(bus_space_tag_t, void *);
5284507484Sjmcneill
53f693c922Shikaru CFATTACH_DECL_NEW(mainbus, sizeof(device_t), mainbus_match, mainbus_attach,
54f693c922Shikaru NULL, NULL);
55f693c922Shikaru
5684507484Sjmcneill static struct mips_bus_space simplebus_bus_tag;
5784507484Sjmcneill
5884507484Sjmcneill static struct mips_bus_dma_tag simplebus_dma_tag = {
5984507484Sjmcneill ._cookie = NULL,
6084507484Sjmcneill ._wbase = 0,
6184507484Sjmcneill ._bounce_alloc_lo = 0,
6284507484Sjmcneill ._bounce_alloc_hi = 0,
6384507484Sjmcneill ._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
6484507484Sjmcneill ._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
6584507484Sjmcneill ._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
6684507484Sjmcneill };
6784507484Sjmcneill
68f693c922Shikaru static int
mainbus_match(device_t parent,struct cfdata * match,void * aux)69f693c922Shikaru mainbus_match(device_t parent, struct cfdata *match, void *aux)
70f693c922Shikaru {
71f693c922Shikaru static int once = 0;
72f693c922Shikaru
73f693c922Shikaru if (once != 0)
74f693c922Shikaru return 0;
75f693c922Shikaru once = 1;
76f693c922Shikaru
77f693c922Shikaru return 1;
78f693c922Shikaru }
79f693c922Shikaru
80f693c922Shikaru static void
mainbus_attach(device_t parent,device_t self,void * aux)81f693c922Shikaru mainbus_attach(device_t parent, device_t self, void *aux)
82f693c922Shikaru {
83f693c922Shikaru aprint_normal("\n");
84f693c922Shikaru
8584507484Sjmcneill if (fdtbus_get_data() != NULL) {
8684507484Sjmcneill mainbus_attach_devicetree(self);
8784507484Sjmcneill } else {
8884507484Sjmcneill mainbus_attach_static(self);
8984507484Sjmcneill }
9084507484Sjmcneill }
9184507484Sjmcneill
9284507484Sjmcneill static void
mainbus_attach_static(device_t self)9384507484Sjmcneill mainbus_attach_static(device_t self)
9484507484Sjmcneill {
9584507484Sjmcneill struct mainbus_attach_args aa;
9684507484Sjmcneill int i;
9784507484Sjmcneill
98f693c922Shikaru for (i = 0; i < (int)mainbus_ndevs; i++) {
99f693c922Shikaru aa.aa_name = mainbus_devs[i];
1002685996bSthorpej config_found(self, &aa, mainbus_print,
101c7fb772bSthorpej CFARGS(.submatch = mainbus_submatch,
102c7fb772bSthorpej .iattr = "mainbus"));
103f693c922Shikaru }
104f693c922Shikaru }
105f693c922Shikaru
10684507484Sjmcneill extern struct octeon_config octeon_configuration;
10784507484Sjmcneill extern void octpow_bootstrap(struct octeon_config *);
10884507484Sjmcneill extern void octfpa_bootstrap(struct octeon_config *);
10984507484Sjmcneill
11084507484Sjmcneill static void
mainbus_attach_devicetree(device_t self)11184507484Sjmcneill mainbus_attach_devicetree(device_t self)
11284507484Sjmcneill {
11392b28f32Sjmcneill const struct fdt_console *cons = fdtbus_get_console();
11484507484Sjmcneill struct mainbus_attach_args aa;
11584507484Sjmcneill struct fdt_attach_args faa;
11692b28f32Sjmcneill u_int uart_freq;
11784507484Sjmcneill
11884507484Sjmcneill aa.aa_name = "cpunode";
1192685996bSthorpej config_found(self, &aa, mainbus_print,
120c7fb772bSthorpej CFARGS(.submatch = mainbus_submatch,
121c7fb772bSthorpej .iattr = "mainbus"));
12284507484Sjmcneill
12361ff6cfaSjmcneill aa.aa_name = "iobus";
1242685996bSthorpej config_found(self, &aa, mainbus_print,
125c7fb772bSthorpej CFARGS(.submatch = mainbus_submatch,
126c7fb772bSthorpej .iattr = "mainbus"));
12761ff6cfaSjmcneill
12884507484Sjmcneill simplebus_bus_io_init(&simplebus_bus_tag, NULL);
12984507484Sjmcneill
13084507484Sjmcneill faa.faa_bst = &simplebus_bus_tag;
13184507484Sjmcneill faa.faa_dmat = &simplebus_dma_tag;
13284507484Sjmcneill faa.faa_name = "";
13392b28f32Sjmcneill
13492b28f32Sjmcneill if (cons != NULL) {
13592b28f32Sjmcneill faa.faa_phandle = fdtbus_get_stdout_phandle();
13692b28f32Sjmcneill
13792b28f32Sjmcneill if (of_getprop_uint32(faa.faa_phandle, "clock-frequency",
13892b28f32Sjmcneill &uart_freq) != 0) {
13992b28f32Sjmcneill uart_freq = octeon_ioclock_speed();
14092b28f32Sjmcneill }
14192b28f32Sjmcneill
14292b28f32Sjmcneill if (uart_freq > 0)
14392b28f32Sjmcneill delay(640000000 / uart_freq);
14492b28f32Sjmcneill
14592b28f32Sjmcneill cons->consinit(&faa, uart_freq);
14692b28f32Sjmcneill }
14792b28f32Sjmcneill
14884507484Sjmcneill faa.faa_phandle = OF_peer(0);
149495d41e9Sthorpej config_found(self, &faa, NULL,
150c7fb772bSthorpej CFARGS(.iattr = "fdt"));
15184507484Sjmcneill }
15284507484Sjmcneill
153f693c922Shikaru static int
mainbus_submatch(device_t parent,cfdata_t cf,const int * locs,void * aux)154f693c922Shikaru mainbus_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
155f693c922Shikaru {
156f693c922Shikaru struct mainbus_attach_args *aa = aux;
157f693c922Shikaru
158f693c922Shikaru if (strcmp(cf->cf_name, aa->aa_name) != 0)
159f693c922Shikaru return 0;
160f693c922Shikaru
161f693c922Shikaru return config_match(parent, cf, aux);
162f693c922Shikaru }
163f693c922Shikaru
164f693c922Shikaru static int
mainbus_print(void * aux,const char * pnp)165f693c922Shikaru mainbus_print(void *aux, const char *pnp)
166f693c922Shikaru {
167f693c922Shikaru struct mainbus_attach_args *aa = aux;
168f693c922Shikaru
169f693c922Shikaru if (pnp != 0)
170f693c922Shikaru return QUIET;
171f693c922Shikaru
172f693c922Shikaru if (pnp)
173f693c922Shikaru aprint_normal("%s at %s", aa->aa_name, pnp);
174f693c922Shikaru
175f693c922Shikaru return UNCONF;
176f693c922Shikaru }
17784507484Sjmcneill
17884507484Sjmcneill /* ---- bus_space(9) */
17984507484Sjmcneill #define CHIP simplebus
18084507484Sjmcneill #define CHIP_IO
18184507484Sjmcneill #define CHIP_ACCESS_SIZE 8
18284507484Sjmcneill
18384507484Sjmcneill #define CHIP_W1_BUS_START(v) 0x0000000000000000ULL
18484507484Sjmcneill #define CHIP_W1_BUS_END(v) 0x7fffffffffffffffULL
18584507484Sjmcneill #define CHIP_W1_SYS_START(v) 0x8000000000000000ULL
18684507484Sjmcneill #define CHIP_W1_SYS_END(v) 0xffffffffffffffffULL
18784507484Sjmcneill
18884507484Sjmcneill #include <mips/mips/bus_space_alignstride_chipdep.c>
189*77a7b674Sjmcneill
190*77a7b674Sjmcneill bus_space_tag_t
fdtbus_bus_tag_create(int phandle,uint32_t flags)191*77a7b674Sjmcneill fdtbus_bus_tag_create(int phandle, uint32_t flags)
192*77a7b674Sjmcneill {
193*77a7b674Sjmcneill return &simplebus_bus_tag;
194*77a7b674Sjmcneill }
195