xref: /netbsd-src/sys/arch/arm/xilinx/zynq_cemac.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: zynq_cemac.c,v 1.4 2022/10/26 11:31:11 jmcneill Exp $	*/
2 /*-
3  * Copyright (c) 2015  Genetec Corporation.  All rights reserved.
4  * Written by Hashimoto Kenichi for Genetec Corporation.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: zynq_cemac.c,v 1.4 2022/10/26 11:31:11 jmcneill Exp $");
30 
31 #include "opt_soc.h"
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40 
41 #include <dev/cadence/cemacreg.h>
42 #include <dev/cadence/if_cemacvar.h>
43 
44 #include <net/if_ether.h>
45 
46 #include <dev/fdt/fdtvar.h>
47 
48 static const struct device_compatible_entry compat_data[] = {
49 	{ .compat = "cdns,zynq-gem" },
50 	DEVICE_COMPAT_EOL
51 };
52 
53 int
54 cemac_match(device_t parent, cfdata_t cfdata, void *aux)
55 {
56 	struct fdt_attach_args * const faa = aux;
57 
58 	return of_compatible_match(faa->faa_phandle, compat_data);
59 }
60 
61 void
62 cemac_attach(device_t parent, device_t self, void *aux)
63 {
64 	struct fdt_attach_args * const faa = aux;
65 	const int phandle = faa->faa_phandle;
66 	prop_dictionary_t prop = device_properties(self);
67 	bus_space_handle_t ioh;
68 	char intrstr[128];
69 	const char *macaddr;
70 	bus_addr_t addr;
71 	bus_size_t size;
72 	int error, len;
73 
74 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
75 		aprint_error(": couldn't get registers\n");
76 		return;
77 	}
78 
79 	error = bus_space_map(faa->faa_bst, addr, size, 0, &ioh);
80 	if (error) {
81 		aprint_error(": failed to map register %#lx@%#lx: %d\n",
82 		    size, addr, error);
83 		return;
84 	}
85 
86 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
87 		aprint_error(": failed to decode interrupt\n");
88 		return;
89 	}
90 
91 	if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, cemac_intr,
92 				  device_private(self)) == NULL) {
93 		aprint_error(": failed to establish interrupt on %s\n", intrstr);
94 		return;
95 	}
96 
97 	macaddr = fdtbus_get_prop(phandle, "local-mac-address", &len);
98 	if (macaddr != NULL && len == ETHER_ADDR_LEN) {
99 		prop_dictionary_set_data(prop, "mac-address", macaddr, len);
100 	}
101 
102 	cemac_attach_common(self, faa->faa_bst, ioh, faa->faa_dmat, CEMAC_FLAG_GEM);
103 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
104 }
105 
106