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