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