cycv_gmac.c revision 1.2.4.2 1 1.2.4.2 christos /* $NetBSD: cycv_gmac.c,v 1.2.4.2 2019/06/10 22:05:50 christos Exp $ */
2 1.2.4.2 christos
3 1.2.4.2 christos /*-
4 1.2.4.2 christos * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.4.2 christos * All rights reserved.
6 1.2.4.2 christos *
7 1.2.4.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 christos * modification, are permitted provided that the following conditions
9 1.2.4.2 christos * are met:
10 1.2.4.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 christos * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 christos * documentation and/or other materials provided with the distribution.
15 1.2.4.2 christos *
16 1.2.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2.4.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.4.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.4.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2.4.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.4.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.4.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.4.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.4.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.4.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.4.2 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.2.4.2 christos */
28 1.2.4.2 christos
29 1.2.4.2 christos /* Taken from sunxi_gmac.c */
30 1.2.4.2 christos
31 1.2.4.2 christos #include <sys/cdefs.h>
32 1.2.4.2 christos
33 1.2.4.2 christos __KERNEL_RCSID(0, "$NetBSD: cycv_gmac.c,v 1.2.4.2 2019/06/10 22:05:50 christos Exp $");
34 1.2.4.2 christos
35 1.2.4.2 christos #include <sys/param.h>
36 1.2.4.2 christos #include <sys/bus.h>
37 1.2.4.2 christos #include <sys/device.h>
38 1.2.4.2 christos #include <sys/intr.h>
39 1.2.4.2 christos #include <sys/systm.h>
40 1.2.4.2 christos #include <sys/gpio.h>
41 1.2.4.2 christos
42 1.2.4.2 christos #include <net/if.h>
43 1.2.4.2 christos #include <net/if_ether.h>
44 1.2.4.2 christos #include <net/if_media.h>
45 1.2.4.2 christos
46 1.2.4.2 christos #include <dev/mii/miivar.h>
47 1.2.4.2 christos
48 1.2.4.2 christos #include <dev/ic/dwc_gmac_var.h>
49 1.2.4.2 christos #include <dev/ic/dwc_gmac_reg.h>
50 1.2.4.2 christos
51 1.2.4.2 christos #include <dev/fdt/fdtvar.h>
52 1.2.4.2 christos
53 1.2.4.2 christos static const char * compatible[] = { "altr,socfpga-stmmac", NULL };
54 1.2.4.2 christos
55 1.2.4.2 christos static int
56 1.2.4.2 christos cycv_gmac_intr(void *arg)
57 1.2.4.2 christos {
58 1.2.4.2 christos return dwc_gmac_intr(arg);
59 1.2.4.2 christos }
60 1.2.4.2 christos
61 1.2.4.2 christos static int
62 1.2.4.2 christos cycv_gmac_match(device_t parent, cfdata_t cf, void *aux)
63 1.2.4.2 christos {
64 1.2.4.2 christos struct fdt_attach_args * const faa = aux;
65 1.2.4.2 christos
66 1.2.4.2 christos return of_match_compatible(faa->faa_phandle, compatible);
67 1.2.4.2 christos }
68 1.2.4.2 christos
69 1.2.4.2 christos static void
70 1.2.4.2 christos cycv_gmac_attach(device_t parent, device_t self, void *aux)
71 1.2.4.2 christos {
72 1.2.4.2 christos struct dwc_gmac_softc * const sc = device_private(self);
73 1.2.4.2 christos struct fdt_attach_args * const faa = aux;
74 1.2.4.2 christos const int phandle = faa->faa_phandle;
75 1.2.4.2 christos struct clk *clk_gmac;
76 1.2.4.2 christos struct fdtbus_reset *rst_gmac;
77 1.2.4.2 christos const char *phy_mode;
78 1.2.4.2 christos char intrstr[128];
79 1.2.4.2 christos bus_addr_t addr;
80 1.2.4.2 christos bus_size_t size;
81 1.2.4.2 christos
82 1.2.4.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
83 1.2.4.2 christos aprint_error(": couldn't get registers\n");
84 1.2.4.2 christos return;
85 1.2.4.2 christos }
86 1.2.4.2 christos
87 1.2.4.2 christos sc->sc_dev = self;
88 1.2.4.2 christos sc->sc_bst = faa->faa_bst;
89 1.2.4.2 christos if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
90 1.2.4.2 christos aprint_error(": couldn't map registers\n");
91 1.2.4.2 christos return;
92 1.2.4.2 christos }
93 1.2.4.2 christos sc->sc_dmat = faa->faa_dmat;
94 1.2.4.2 christos
95 1.2.4.2 christos if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof intrstr)) {
96 1.2.4.2 christos aprint_error(": failed to decode interrupt\n");
97 1.2.4.2 christos return;
98 1.2.4.2 christos }
99 1.2.4.2 christos
100 1.2.4.2 christos clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
101 1.2.4.2 christos if (clk_gmac == NULL) {
102 1.2.4.2 christos aprint_error(": couldn't get clock\n");
103 1.2.4.2 christos return;
104 1.2.4.2 christos }
105 1.2.4.2 christos
106 1.2.4.2 christos rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
107 1.2.4.2 christos if (rst_gmac == NULL) {
108 1.2.4.2 christos aprint_error(": couldn't get reset\n");
109 1.2.4.2 christos return;
110 1.2.4.2 christos }
111 1.2.4.2 christos
112 1.2.4.2 christos phy_mode = fdtbus_get_string(phandle, "phy-mode");
113 1.2.4.2 christos if (phy_mode == NULL) {
114 1.2.4.2 christos aprint_error(": missing 'phy-mode' property\n");
115 1.2.4.2 christos return;
116 1.2.4.2 christos }
117 1.2.4.2 christos
118 1.2.4.2 christos if (clk_enable(clk_gmac) != 0) {
119 1.2.4.2 christos aprint_error(": couldn't enable clocks\n");
120 1.2.4.2 christos return;
121 1.2.4.2 christos }
122 1.2.4.2 christos
123 1.2.4.2 christos if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
124 1.2.4.2 christos aprint_error(": couldn't de-assert reset\n");
125 1.2.4.2 christos return;
126 1.2.4.2 christos }
127 1.2.4.2 christos
128 1.2.4.2 christos aprint_naive("\n");
129 1.2.4.2 christos aprint_normal(": GMAC\n");
130 1.2.4.2 christos
131 1.2.4.2 christos if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
132 1.2.4.2 christos cycv_gmac_intr, sc) == NULL) {
133 1.2.4.2 christos aprint_error_dev(self, "failed to establish interrupt on %s\n",
134 1.2.4.2 christos intrstr);
135 1.2.4.2 christos return;
136 1.2.4.2 christos }
137 1.2.4.2 christos aprint_normal_dev(self, "interrupting on %s\n", intrstr);
138 1.2.4.2 christos
139 1.2.4.2 christos dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102);
140 1.2.4.2 christos }
141 1.2.4.2 christos
142 1.2.4.2 christos CFATTACH_DECL_NEW(cycv_gmac, sizeof(struct dwc_gmac_softc),
143 1.2.4.2 christos cycv_gmac_match, cycv_gmac_attach, NULL, NULL);
144