cycv_gmac.c revision 1.2 1 1.2 martin /* $NetBSD: cycv_gmac.c,v 1.2 2019/02/23 17:18:38 martin 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.2 martin __KERNEL_RCSID(0, "$NetBSD: cycv_gmac.c,v 1.2 2019/02/23 17:18:38 martin 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.1 aymeric
42 1.1 aymeric #include <net/if.h>
43 1.1 aymeric #include <net/if_ether.h>
44 1.1 aymeric #include <net/if_media.h>
45 1.1 aymeric
46 1.1 aymeric #include <dev/mii/miivar.h>
47 1.1 aymeric
48 1.1 aymeric #include <dev/ic/dwc_gmac_var.h>
49 1.1 aymeric #include <dev/ic/dwc_gmac_reg.h>
50 1.1 aymeric
51 1.1 aymeric #include <dev/fdt/fdtvar.h>
52 1.1 aymeric
53 1.1 aymeric static const char * compatible[] = { "altr,socfpga-stmmac", NULL };
54 1.1 aymeric
55 1.1 aymeric static int
56 1.1 aymeric cycv_gmac_intr(void *arg)
57 1.1 aymeric {
58 1.1 aymeric return dwc_gmac_intr(arg);
59 1.1 aymeric }
60 1.1 aymeric
61 1.1 aymeric static int
62 1.1 aymeric cycv_gmac_match(device_t parent, cfdata_t cf, void *aux)
63 1.1 aymeric {
64 1.1 aymeric struct fdt_attach_args * const faa = aux;
65 1.1 aymeric
66 1.1 aymeric return of_match_compatible(faa->faa_phandle, compatible);
67 1.1 aymeric }
68 1.1 aymeric
69 1.1 aymeric static void
70 1.1 aymeric cycv_gmac_attach(device_t parent, device_t self, void *aux)
71 1.1 aymeric {
72 1.1 aymeric struct dwc_gmac_softc * const sc = device_private(self);
73 1.1 aymeric struct fdt_attach_args * const faa = aux;
74 1.1 aymeric const int phandle = faa->faa_phandle;
75 1.1 aymeric struct clk *clk_gmac;
76 1.1 aymeric struct fdtbus_reset *rst_gmac;
77 1.1 aymeric const char *phy_mode;
78 1.1 aymeric char intrstr[128];
79 1.1 aymeric bus_addr_t addr;
80 1.1 aymeric bus_size_t size;
81 1.1 aymeric
82 1.1 aymeric if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
83 1.1 aymeric aprint_error(": couldn't get registers\n");
84 1.1 aymeric return;
85 1.1 aymeric }
86 1.1 aymeric
87 1.1 aymeric sc->sc_dev = self;
88 1.1 aymeric sc->sc_bst = faa->faa_bst;
89 1.1 aymeric if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
90 1.1 aymeric aprint_error(": couldn't map registers\n");
91 1.1 aymeric return;
92 1.1 aymeric }
93 1.1 aymeric sc->sc_dmat = faa->faa_dmat;
94 1.1 aymeric
95 1.1 aymeric if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof intrstr)) {
96 1.1 aymeric aprint_error(": failed to decode interrupt\n");
97 1.1 aymeric return;
98 1.1 aymeric }
99 1.1 aymeric
100 1.1 aymeric clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
101 1.1 aymeric if (clk_gmac == NULL) {
102 1.1 aymeric aprint_error(": couldn't get clock\n");
103 1.1 aymeric return;
104 1.1 aymeric }
105 1.1 aymeric
106 1.1 aymeric rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
107 1.1 aymeric if (rst_gmac == NULL) {
108 1.1 aymeric aprint_error(": couldn't get reset\n");
109 1.1 aymeric return;
110 1.1 aymeric }
111 1.1 aymeric
112 1.1 aymeric phy_mode = fdtbus_get_string(phandle, "phy-mode");
113 1.1 aymeric if (phy_mode == NULL) {
114 1.1 aymeric aprint_error(": missing 'phy-mode' property\n");
115 1.1 aymeric return;
116 1.1 aymeric }
117 1.1 aymeric
118 1.1 aymeric if (clk_enable(clk_gmac) != 0) {
119 1.1 aymeric aprint_error(": couldn't enable clocks\n");
120 1.1 aymeric return;
121 1.1 aymeric }
122 1.1 aymeric
123 1.1 aymeric if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
124 1.1 aymeric aprint_error(": couldn't de-assert reset\n");
125 1.1 aymeric return;
126 1.1 aymeric }
127 1.1 aymeric
128 1.1 aymeric aprint_naive("\n");
129 1.1 aymeric aprint_normal(": GMAC\n");
130 1.1 aymeric
131 1.1 aymeric if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
132 1.1 aymeric cycv_gmac_intr, sc) == NULL) {
133 1.1 aymeric aprint_error_dev(self, "failed to establish interrupt on %s\n",
134 1.1 aymeric intrstr);
135 1.1 aymeric return;
136 1.1 aymeric }
137 1.1 aymeric aprint_normal_dev(self, "interrupting on %s\n", intrstr);
138 1.1 aymeric
139 1.2 martin dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102);
140 1.1 aymeric }
141 1.1 aymeric
142 1.1 aymeric CFATTACH_DECL_NEW(cycv_gmac, sizeof(struct dwc_gmac_softc),
143 1.1 aymeric cycv_gmac_match, cycv_gmac_attach, NULL, NULL);
144