1*8d9149a7Sphx /* $NetBSD: zz9k.c,v 1.1 2023/05/03 13:49:30 phx Exp $ */
2*8d9149a7Sphx
3*8d9149a7Sphx /*
4*8d9149a7Sphx * Copyright (c) 2020 The NetBSD Foundation, Inc.
5*8d9149a7Sphx * All rights reserved.
6*8d9149a7Sphx *
7*8d9149a7Sphx * This code is derived from software contributed to The NetBSD Foundation
8*8d9149a7Sphx * by Alain Runa.
9*8d9149a7Sphx *
10*8d9149a7Sphx * Redistribution and use in source and binary forms, with or without
11*8d9149a7Sphx * modification, are permitted provided that the following conditions
12*8d9149a7Sphx * are met:
13*8d9149a7Sphx * 1. Redistributions of source code must retain the above copyright
14*8d9149a7Sphx * notice, this list of conditions and the following disclaimer.
15*8d9149a7Sphx * 2. Redistributions in binary form must reproduce the above copyright
16*8d9149a7Sphx * notice, this list of conditions and the following disclaimer in the
17*8d9149a7Sphx * documentation and/or other materials provided with the distribution.
18*8d9149a7Sphx *
19*8d9149a7Sphx * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*8d9149a7Sphx * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*8d9149a7Sphx * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*8d9149a7Sphx * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*8d9149a7Sphx * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*8d9149a7Sphx * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*8d9149a7Sphx * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*8d9149a7Sphx * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*8d9149a7Sphx * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*8d9149a7Sphx * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*8d9149a7Sphx */
30*8d9149a7Sphx
31*8d9149a7Sphx #include <sys/cdefs.h>
32*8d9149a7Sphx __KERNEL_RCSID(0, "$NetBSD: zz9k.c,v 1.1 2023/05/03 13:49:30 phx Exp $");
33*8d9149a7Sphx
34*8d9149a7Sphx /* miscellaneous */
35*8d9149a7Sphx #include <sys/types.h> /* size_t */
36*8d9149a7Sphx #include <sys/stdint.h> /* uintXX_t */
37*8d9149a7Sphx #include <sys/stdbool.h> /* bool */
38*8d9149a7Sphx
39*8d9149a7Sphx /* driver(9) includes */
40*8d9149a7Sphx #include <sys/param.h> /* NODEV */
41*8d9149a7Sphx #include <sys/device.h> /* CFATTACH_DECL_NEW(), device_priv() */
42*8d9149a7Sphx #include <sys/errno.h> /* . */
43*8d9149a7Sphx
44*8d9149a7Sphx /* bus_space(9) and zorro bus includes */
45*8d9149a7Sphx #include <sys/bus.h> /* bus_space_xxx(), bus_space_xxx */
46*8d9149a7Sphx #include <sys/cpu.h> /* kvtop() */
47*8d9149a7Sphx #include <sys/systm.h> /* aprint_xxx() */
48*8d9149a7Sphx #include <amiga/dev/zbusvar.h> /* zbus_args */
49*8d9149a7Sphx
50*8d9149a7Sphx /* zz9k and amiga related */
51*8d9149a7Sphx #include <amiga/amiga/device.h> /* amiga_realconfig */
52*8d9149a7Sphx #include <amiga/dev/zz9kvar.h> /* zz9k_softc */
53*8d9149a7Sphx #include <amiga/dev/zz9kreg.h> /* ZZ9000 registers */
54*8d9149a7Sphx #include "zz9k.h" /* NZZ9K */
55*8d9149a7Sphx
56*8d9149a7Sphx
57*8d9149a7Sphx /* helper functions */
58*8d9149a7Sphx static int zz9k_print(void *aux, const char *pnp);
59*8d9149a7Sphx
60*8d9149a7Sphx /* driver(9) essentials */
61*8d9149a7Sphx static int zz9k_match(device_t parent, cfdata_t match, void *aux);
62*8d9149a7Sphx static void zz9k_attach(device_t parent, device_t self, void *aux);
63*8d9149a7Sphx CFATTACH_DECL_NEW(
64*8d9149a7Sphx zz9k, sizeof(struct zz9k_softc), zz9k_match, zz9k_attach, NULL, NULL);
65*8d9149a7Sphx
66*8d9149a7Sphx bool zz9k_exists = false; /* required by zz9k_fb ZZFB_CONSOLE */
67*8d9149a7Sphx
68*8d9149a7Sphx
69*8d9149a7Sphx /* It's the year 2020. And so it begins... */
70*8d9149a7Sphx
71*8d9149a7Sphx static int
zz9k_match(device_t parent,cfdata_t match,void * aux)72*8d9149a7Sphx zz9k_match(device_t parent, cfdata_t match, void *aux)
73*8d9149a7Sphx {
74*8d9149a7Sphx struct zbus_args *zap = aux;
75*8d9149a7Sphx bool found = false;
76*8d9149a7Sphx
77*8d9149a7Sphx if (zap->manid == ZZ9K_MANID) {
78*8d9149a7Sphx switch (zap->prodid) {
79*8d9149a7Sphx case ZZ9K_PRODID_Z2:
80*8d9149a7Sphx case ZZ9K_PRODID_Z3:
81*8d9149a7Sphx found = true;
82*8d9149a7Sphx break;
83*8d9149a7Sphx }
84*8d9149a7Sphx }
85*8d9149a7Sphx
86*8d9149a7Sphx if (amiga_realconfig == 0) { /* pre-config, just flag if zz9k exists. */
87*8d9149a7Sphx zz9k_exists = found;
88*8d9149a7Sphx return 0; /* 0, as we don't need zz9k_attach() this round. */
89*8d9149a7Sphx }
90*8d9149a7Sphx
91*8d9149a7Sphx return found;
92*8d9149a7Sphx }
93*8d9149a7Sphx
94*8d9149a7Sphx static void
zz9k_attach(device_t parent,device_t self,void * aux)95*8d9149a7Sphx zz9k_attach(device_t parent, device_t self, void *aux)
96*8d9149a7Sphx {
97*8d9149a7Sphx struct zbus_args *zap = aux;
98*8d9149a7Sphx struct zz9k_softc *sc = device_private(self);
99*8d9149a7Sphx struct zz9kbus_attach_args zz9k_fb; /* Graphics */
100*8d9149a7Sphx struct zz9kbus_attach_args zz9k_if; /* Ethernet */
101*8d9149a7Sphx struct zz9kbus_attach_args zz9k_ax; /* Audio */
102*8d9149a7Sphx struct zz9kbus_attach_args zz9k_usb; /* USB */
103*8d9149a7Sphx
104*8d9149a7Sphx const bus_addr_t regBase = ZZ9K_REG_BASE;
105*8d9149a7Sphx const bus_size_t regSize = ZZ9K_REG_SIZE;
106*8d9149a7Sphx
107*8d9149a7Sphx sc->sc_dev = self;
108*8d9149a7Sphx sc->sc_bst.base = (bus_addr_t)zap->va;
109*8d9149a7Sphx sc->sc_bst.absm = &amiga_bus_stride_1;
110*8d9149a7Sphx sc->sc_iot = &sc->sc_bst;
111*8d9149a7Sphx sc->sc_zsize = zap->size;
112*8d9149a7Sphx
113*8d9149a7Sphx if (bus_space_map(sc->sc_iot, regBase, regSize, 0, &sc->sc_regh)) {
114*8d9149a7Sphx aprint_error("Failed to map MNT ZZ9000 registers.\n");
115*8d9149a7Sphx return;
116*8d9149a7Sphx }
117*8d9149a7Sphx
118*8d9149a7Sphx uint16_t hwVer = ZZREG_R(ZZ9K_HW_VERSION);
119*8d9149a7Sphx uint16_t fwVer = ZZREG_R(ZZ9K_FW_VERSION);
120*8d9149a7Sphx
121*8d9149a7Sphx aprint_normal(": MNT ZZ9000 Zorro %s (HW: %i.%i, FW: %i.%i)\n",
122*8d9149a7Sphx (zap->prodid == ZZ9K_PRODID_Z3) ? "III" : "II",
123*8d9149a7Sphx hwVer >> 8, hwVer & 0x00ff, fwVer >> 8, fwVer & 0x00ff);
124*8d9149a7Sphx
125*8d9149a7Sphx if (fwVer < ZZ9K_FW_VER_MIN) {
126*8d9149a7Sphx aprint_error("Firmware version is not supported. "
127*8d9149a7Sphx "Please update to newer firmware from:\n");
128*8d9149a7Sphx aprint_error(
129*8d9149a7Sphx "https://source.mnt.re/amiga/zz9000-firmware/-/releases\n");
130*8d9149a7Sphx return;
131*8d9149a7Sphx }
132*8d9149a7Sphx
133*8d9149a7Sphx uint16_t tcore = ZZREG_R(ZZ9K_TEMPERATURE);
134*8d9149a7Sphx uint16_t vcore = ZZREG_R(ZZ9K_VOLTAGE_CORE);
135*8d9149a7Sphx uint16_t vaux = ZZREG_R(ZZ9K_VOLTAGE_AUX);
136*8d9149a7Sphx
137*8d9149a7Sphx aprint_normal_dev(sc->sc_dev, "Hardware status "
138*8d9149a7Sphx "<Tcore: %i.%i C, Vcore: %i.%i V, Vaux: %i.%i V>\n",
139*8d9149a7Sphx tcore/10, tcore%10, vcore/100, vcore%100, vaux/100, vaux%100);
140*8d9149a7Sphx
141*8d9149a7Sphx aprint_debug_dev(sc->sc_dev, "[DEBUG] registers at %p/%p (pa/va), "
142*8d9149a7Sphx "MNT ZZ9000 is mapped with %i MB in Zorro %s space.\n",
143*8d9149a7Sphx (void *)kvtop((void *)sc->sc_regh),
144*8d9149a7Sphx bus_space_vaddr(sc->sc_iot, sc->sc_regh),
145*8d9149a7Sphx zap->size / (1024 * 1024),
146*8d9149a7Sphx (zap->prodid == ZZ9K_PRODID_Z3) ? "III" : "II");
147*8d9149a7Sphx
148*8d9149a7Sphx if (fwVer > ZZ9K_FW_VER) {
149*8d9149a7Sphx aprint_normal_dev(sc->sc_dev, "The firmware is newer than "
150*8d9149a7Sphx "%i.%i and may not be supported yet.\n",
151*8d9149a7Sphx ZZ9K_FW_VER >> 8, ZZ9K_FW_VER & 0x00ff);
152*8d9149a7Sphx }
153*8d9149a7Sphx
154*8d9149a7Sphx /* Add framebuffer */
155*8d9149a7Sphx zz9k_fb.zzaa_base = (bus_addr_t)zap->va;
156*8d9149a7Sphx strcpy(zz9k_fb.zzaa_name, "zz9k_fb");
157*8d9149a7Sphx config_found(sc->sc_dev, &zz9k_fb, zz9k_print, CFARGS_NONE);
158*8d9149a7Sphx
159*8d9149a7Sphx /* Add zz* ethernet interface */
160*8d9149a7Sphx zz9k_if.zzaa_base = (bus_addr_t)zap->va;
161*8d9149a7Sphx strcpy(zz9k_if.zzaa_name, "zz9k_if");
162*8d9149a7Sphx config_found(sc->sc_dev, &zz9k_if, zz9k_print, CFARGS_NONE);
163*8d9149a7Sphx
164*8d9149a7Sphx /* Add AX audio interface */
165*8d9149a7Sphx zz9k_ax.zzaa_base = (bus_addr_t)zap->va;
166*8d9149a7Sphx strcpy(zz9k_ax.zzaa_name, "zz9k_ax");
167*8d9149a7Sphx config_found(sc->sc_dev, &zz9k_ax, zz9k_print, CFARGS_NONE);
168*8d9149a7Sphx
169*8d9149a7Sphx /* Add USB interface */
170*8d9149a7Sphx zz9k_usb.zzaa_base = (bus_addr_t)zap->va;
171*8d9149a7Sphx strcpy(zz9k_usb.zzaa_name, "zz9k_usb");
172*8d9149a7Sphx config_found(sc->sc_dev, &zz9k_usb, zz9k_print, CFARGS_NONE);
173*8d9149a7Sphx }
174*8d9149a7Sphx
175*8d9149a7Sphx static int
zz9k_print(void * aux,const char * pnp)176*8d9149a7Sphx zz9k_print(void *aux, const char *pnp)
177*8d9149a7Sphx {
178*8d9149a7Sphx return 0;
179*8d9149a7Sphx }
180