xref: /freebsd/sys/dev/dwc/if_dwc_cvitek.c (revision 7a9a15eaf9170e06533f20d44540ab15af9142ef)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2025 Bojan Novković <bnovkov@FreeBSD.org>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/module.h>
34 
35 #include <net/if.h>
36 
37 #include <machine/bus.h>
38 #include <machine/param.h>
39 
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/clk/clk.h>
43 #include <dev/hwreset/hwreset.h>
44 
45 #include <dev/dwc/if_dwcvar.h>
46 #include <dev/dwc/dwc1000_reg.h>
47 
48 #include "if_dwc_if.h"
49 
50 static int
if_dwc_cvitek_probe(device_t dev)51 if_dwc_cvitek_probe(device_t dev)
52 {
53 
54 	if (!ofw_bus_status_okay(dev))
55 		return (ENXIO);
56 
57 	if (!ofw_bus_is_compatible(dev, "cvitek,ethernet"))
58 		return (ENXIO);
59 	device_set_desc(dev, "CVITEK Ethernet Controller");
60 
61 	return (BUS_PROBE_DEFAULT);
62 }
63 
64 static int
if_dwc_cvitek_mii_clk(device_t dev)65 if_dwc_cvitek_mii_clk(device_t dev)
66 {
67 	/*
68 	 * XXX: This is a hack to get the driver working on
69 	 * the Milk-V platform. For reference, the u-boot designware
70 	 * driver uses the same '150_250M' clock value, but if_dwc
71 	 * will not work on Milk-V hardware unless the lowest
72 	 * bit of the PHY register address is always set.
73 	 */
74 	return (0x10 | GMAC_MII_CLK_150_250M_DIV102);
75 }
76 
77 static device_method_t dwc_cvitek_methods[] = {
78 	DEVMETHOD(device_probe,		if_dwc_cvitek_probe),
79 
80 	DEVMETHOD(if_dwc_mii_clk,	if_dwc_cvitek_mii_clk),
81 	DEVMETHOD_END
82 };
83 
84 extern driver_t dwc_driver;
85 
86 DEFINE_CLASS_1(dwc, dwc_cvitek_driver, dwc_cvitek_methods,
87     sizeof(struct dwc_softc), dwc_driver);
88 DRIVER_MODULE(dwc_cvitek, simplebus, dwc_cvitek_driver, 0, 0);
89 MODULE_DEPEND(dwc_cvitek, dwc, 1, 1, 1);
90