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