sunxi_emac.c revision 1.4.4.5 1 1.4.4.5 martin /* $NetBSD: sunxi_emac.c,v 1.4.4.5 2019/05/12 09:19:07 martin Exp $ */
2 1.4.4.2 snj
3 1.4.4.2 snj /*-
4 1.4.4.2 snj * Copyright (c) 2016-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.4.4.2 snj * All rights reserved.
6 1.4.4.2 snj *
7 1.4.4.2 snj * Redistribution and use in source and binary forms, with or without
8 1.4.4.2 snj * modification, are permitted provided that the following conditions
9 1.4.4.2 snj * are met:
10 1.4.4.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.4.4.2 snj * notice, this list of conditions and the following disclaimer.
12 1.4.4.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.4.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.4.4.2 snj * documentation and/or other materials provided with the distribution.
15 1.4.4.2 snj *
16 1.4.4.2 snj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.4.4.2 snj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.4.4.2 snj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.4.4.2 snj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.4.4.2 snj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.4.4.2 snj * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.4.4.2 snj * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.4.4.2 snj * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.4.4.2 snj * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.4.4.2 snj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.4.4.2 snj * SUCH DAMAGE.
27 1.4.4.2 snj */
28 1.4.4.2 snj
29 1.4.4.2 snj /*
30 1.4.4.2 snj * Allwinner Gigabit Ethernet MAC (EMAC) controller
31 1.4.4.2 snj */
32 1.4.4.2 snj
33 1.4.4.2 snj #include "opt_net_mpsafe.h"
34 1.4.4.2 snj
35 1.4.4.2 snj #include <sys/cdefs.h>
36 1.4.4.5 martin __KERNEL_RCSID(0, "$NetBSD: sunxi_emac.c,v 1.4.4.5 2019/05/12 09:19:07 martin Exp $");
37 1.4.4.2 snj
38 1.4.4.2 snj #include <sys/param.h>
39 1.4.4.2 snj #include <sys/bus.h>
40 1.4.4.2 snj #include <sys/device.h>
41 1.4.4.2 snj #include <sys/intr.h>
42 1.4.4.2 snj #include <sys/systm.h>
43 1.4.4.2 snj #include <sys/kernel.h>
44 1.4.4.2 snj #include <sys/mutex.h>
45 1.4.4.2 snj #include <sys/callout.h>
46 1.4.4.2 snj #include <sys/gpio.h>
47 1.4.4.2 snj #include <sys/cprng.h>
48 1.4.4.2 snj
49 1.4.4.2 snj #include <net/if.h>
50 1.4.4.2 snj #include <net/if_dl.h>
51 1.4.4.2 snj #include <net/if_ether.h>
52 1.4.4.2 snj #include <net/if_media.h>
53 1.4.4.2 snj #include <net/bpf.h>
54 1.4.4.2 snj
55 1.4.4.2 snj #include <dev/mii/miivar.h>
56 1.4.4.2 snj
57 1.4.4.2 snj #include <dev/fdt/fdtvar.h>
58 1.4.4.2 snj
59 1.4.4.2 snj #include <arm/sunxi/sunxi_emac.h>
60 1.4.4.2 snj
61 1.4.4.2 snj #ifdef NET_MPSAFE
62 1.4.4.2 snj #define EMAC_MPSAFE 1
63 1.4.4.2 snj #define CALLOUT_FLAGS CALLOUT_MPSAFE
64 1.4.4.2 snj #define FDT_INTR_FLAGS FDT_INTR_MPSAFE
65 1.4.4.2 snj #else
66 1.4.4.2 snj #define CALLOUT_FLAGS 0
67 1.4.4.2 snj #define FDT_INTR_FLAGS 0
68 1.4.4.2 snj #endif
69 1.4.4.2 snj
70 1.4.4.2 snj #define EMAC_IFNAME "emac%d"
71 1.4.4.2 snj
72 1.4.4.2 snj #define ETHER_ALIGN 2
73 1.4.4.2 snj
74 1.4.4.2 snj #define EMAC_LOCK(sc) mutex_enter(&(sc)->mtx)
75 1.4.4.2 snj #define EMAC_UNLOCK(sc) mutex_exit(&(sc)->mtx)
76 1.4.4.2 snj #define EMAC_ASSERT_LOCKED(sc) KASSERT(mutex_owned(&(sc)->mtx))
77 1.4.4.2 snj
78 1.4.4.2 snj #define DESC_ALIGN sizeof(struct sunxi_emac_desc)
79 1.4.4.2 snj #define TX_DESC_COUNT 1024
80 1.4.4.2 snj #define TX_DESC_SIZE (sizeof(struct sunxi_emac_desc) * TX_DESC_COUNT)
81 1.4.4.2 snj #define RX_DESC_COUNT 256
82 1.4.4.2 snj #define RX_DESC_SIZE (sizeof(struct sunxi_emac_desc) * RX_DESC_COUNT)
83 1.4.4.2 snj
84 1.4.4.2 snj #define DESC_OFF(n) ((n) * sizeof(struct sunxi_emac_desc))
85 1.4.4.2 snj #define TX_NEXT(n) (((n) + 1) & (TX_DESC_COUNT - 1))
86 1.4.4.2 snj #define TX_SKIP(n, o) (((n) + (o)) & (TX_DESC_COUNT - 1))
87 1.4.4.2 snj #define RX_NEXT(n) (((n) + 1) & (RX_DESC_COUNT - 1))
88 1.4.4.2 snj
89 1.4.4.2 snj #define TX_MAX_SEGS 128
90 1.4.4.2 snj
91 1.4.4.2 snj #define SOFT_RST_RETRY 1000
92 1.4.4.2 snj #define MII_BUSY_RETRY 1000
93 1.4.4.2 snj #define MDIO_FREQ 2500000
94 1.4.4.2 snj
95 1.4.4.2 snj #define BURST_LEN_DEFAULT 8
96 1.4.4.2 snj #define RX_TX_PRI_DEFAULT 0
97 1.4.4.2 snj #define PAUSE_TIME_DEFAULT 0x400
98 1.4.4.2 snj #define TX_INTERVAL_DEFAULT 64
99 1.4.4.2 snj
100 1.4.4.2 snj /* syscon EMAC clock register */
101 1.4.4.2 snj #define EMAC_CLK_EPHY_ADDR (0x1f << 20) /* H3 */
102 1.4.4.2 snj #define EMAC_CLK_EPHY_ADDR_SHIFT 20
103 1.4.4.2 snj #define EMAC_CLK_EPHY_LED_POL (1 << 17) /* H3 */
104 1.4.4.2 snj #define EMAC_CLK_EPHY_SHUTDOWN (1 << 16) /* H3 */
105 1.4.4.2 snj #define EMAC_CLK_EPHY_SELECT (1 << 15) /* H3 */
106 1.4.4.2 snj #define EMAC_CLK_RMII_EN (1 << 13)
107 1.4.4.2 snj #define EMAC_CLK_ETXDC (0x7 << 10)
108 1.4.4.2 snj #define EMAC_CLK_ETXDC_SHIFT 10
109 1.4.4.2 snj #define EMAC_CLK_ERXDC (0x1f << 5)
110 1.4.4.2 snj #define EMAC_CLK_ERXDC_SHIFT 5
111 1.4.4.2 snj #define EMAC_CLK_PIT (0x1 << 2)
112 1.4.4.2 snj #define EMAC_CLK_PIT_MII (0 << 2)
113 1.4.4.2 snj #define EMAC_CLK_PIT_RGMII (1 << 2)
114 1.4.4.2 snj #define EMAC_CLK_SRC (0x3 << 0)
115 1.4.4.2 snj #define EMAC_CLK_SRC_MII (0 << 0)
116 1.4.4.2 snj #define EMAC_CLK_SRC_EXT_RGMII (1 << 0)
117 1.4.4.2 snj #define EMAC_CLK_SRC_RGMII (2 << 0)
118 1.4.4.2 snj
119 1.4.4.2 snj /* Burst length of RX and TX DMA transfers */
120 1.4.4.2 snj static int sunxi_emac_burst_len = BURST_LEN_DEFAULT;
121 1.4.4.2 snj
122 1.4.4.2 snj /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
123 1.4.4.2 snj static int sunxi_emac_rx_tx_pri = RX_TX_PRI_DEFAULT;
124 1.4.4.2 snj
125 1.4.4.2 snj /* Pause time field in the transmitted control frame */
126 1.4.4.2 snj static int sunxi_emac_pause_time = PAUSE_TIME_DEFAULT;
127 1.4.4.2 snj
128 1.4.4.2 snj /* Request a TX interrupt every <n> descriptors */
129 1.4.4.2 snj static int sunxi_emac_tx_interval = TX_INTERVAL_DEFAULT;
130 1.4.4.2 snj
131 1.4.4.2 snj enum sunxi_emac_type {
132 1.4.4.2 snj EMAC_A83T = 1,
133 1.4.4.2 snj EMAC_H3,
134 1.4.4.2 snj };
135 1.4.4.2 snj
136 1.4.4.2 snj static const struct of_compat_data compat_data[] = {
137 1.4.4.2 snj { "allwinner,sun8i-a83t-emac", EMAC_A83T },
138 1.4.4.2 snj { "allwinner,sun8i-h3-emac", EMAC_H3 },
139 1.4.4.2 snj { NULL }
140 1.4.4.2 snj };
141 1.4.4.2 snj
142 1.4.4.2 snj struct sunxi_emac_bufmap {
143 1.4.4.2 snj bus_dmamap_t map;
144 1.4.4.2 snj struct mbuf *mbuf;
145 1.4.4.2 snj };
146 1.4.4.2 snj
147 1.4.4.2 snj struct sunxi_emac_txring {
148 1.4.4.2 snj bus_dma_tag_t desc_tag;
149 1.4.4.2 snj bus_dmamap_t desc_map;
150 1.4.4.2 snj bus_dma_segment_t desc_dmaseg;
151 1.4.4.2 snj struct sunxi_emac_desc *desc_ring;
152 1.4.4.2 snj bus_addr_t desc_ring_paddr;
153 1.4.4.2 snj bus_dma_tag_t buf_tag;
154 1.4.4.2 snj struct sunxi_emac_bufmap buf_map[TX_DESC_COUNT];
155 1.4.4.2 snj u_int cur, next, queued;
156 1.4.4.2 snj };
157 1.4.4.2 snj
158 1.4.4.2 snj struct sunxi_emac_rxring {
159 1.4.4.2 snj bus_dma_tag_t desc_tag;
160 1.4.4.2 snj bus_dmamap_t desc_map;
161 1.4.4.2 snj bus_dma_segment_t desc_dmaseg;
162 1.4.4.2 snj struct sunxi_emac_desc *desc_ring;
163 1.4.4.2 snj bus_addr_t desc_ring_paddr;
164 1.4.4.2 snj bus_dma_tag_t buf_tag;
165 1.4.4.2 snj struct sunxi_emac_bufmap buf_map[RX_DESC_COUNT];
166 1.4.4.2 snj u_int cur;
167 1.4.4.2 snj };
168 1.4.4.2 snj
169 1.4.4.2 snj enum {
170 1.4.4.2 snj _RES_EMAC,
171 1.4.4.2 snj _RES_SYSCON,
172 1.4.4.2 snj _RES_NITEMS
173 1.4.4.2 snj };
174 1.4.4.2 snj
175 1.4.4.2 snj struct sunxi_emac_softc {
176 1.4.4.2 snj device_t dev;
177 1.4.4.2 snj int phandle;
178 1.4.4.2 snj enum sunxi_emac_type type;
179 1.4.4.2 snj bus_space_tag_t bst;
180 1.4.4.2 snj bus_dma_tag_t dmat;
181 1.4.4.2 snj
182 1.4.4.2 snj bus_space_handle_t bsh[_RES_NITEMS];
183 1.4.4.2 snj struct clk *clk_ahb;
184 1.4.4.2 snj struct clk *clk_ephy;
185 1.4.4.2 snj struct fdtbus_reset *rst_ahb;
186 1.4.4.2 snj struct fdtbus_reset *rst_ephy;
187 1.4.4.2 snj struct fdtbus_regulator *reg_phy;
188 1.4.4.2 snj struct fdtbus_gpio_pin *pin_reset;
189 1.4.4.2 snj
190 1.4.4.2 snj kmutex_t mtx;
191 1.4.4.2 snj struct ethercom ec;
192 1.4.4.2 snj struct mii_data mii;
193 1.4.4.2 snj callout_t stat_ch;
194 1.4.4.2 snj void *ih;
195 1.4.4.2 snj u_int mdc_div_ratio_m;
196 1.4.4.2 snj
197 1.4.4.2 snj struct sunxi_emac_txring tx;
198 1.4.4.2 snj struct sunxi_emac_rxring rx;
199 1.4.4.2 snj };
200 1.4.4.2 snj
201 1.4.4.2 snj #define RD4(sc, reg) \
202 1.4.4.2 snj bus_space_read_4((sc)->bst, (sc)->bsh[_RES_EMAC], (reg))
203 1.4.4.2 snj #define WR4(sc, reg, val) \
204 1.4.4.2 snj bus_space_write_4((sc)->bst, (sc)->bsh[_RES_EMAC], (reg), (val))
205 1.4.4.2 snj
206 1.4.4.2 snj #define SYSCONRD4(sc, reg) \
207 1.4.4.2 snj bus_space_read_4((sc)->bst, (sc)->bsh[_RES_SYSCON], (reg))
208 1.4.4.2 snj #define SYSCONWR4(sc, reg, val) \
209 1.4.4.2 snj bus_space_write_4((sc)->bst, (sc)->bsh[_RES_SYSCON], (reg), (val))
210 1.4.4.2 snj
211 1.4.4.2 snj static int
212 1.4.4.2 snj sunxi_emac_mii_readreg(device_t dev, int phy, int reg)
213 1.4.4.2 snj {
214 1.4.4.2 snj struct sunxi_emac_softc *sc = device_private(dev);
215 1.4.4.2 snj int retry, val;
216 1.4.4.2 snj
217 1.4.4.2 snj val = 0;
218 1.4.4.2 snj
219 1.4.4.2 snj WR4(sc, EMAC_MII_CMD,
220 1.4.4.2 snj (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
221 1.4.4.2 snj (phy << PHY_ADDR_SHIFT) |
222 1.4.4.2 snj (reg << PHY_REG_ADDR_SHIFT) |
223 1.4.4.2 snj MII_BUSY);
224 1.4.4.2 snj for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
225 1.4.4.2 snj if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
226 1.4.4.2 snj val = RD4(sc, EMAC_MII_DATA);
227 1.4.4.2 snj break;
228 1.4.4.2 snj }
229 1.4.4.2 snj delay(10);
230 1.4.4.2 snj }
231 1.4.4.2 snj
232 1.4.4.2 snj if (retry == 0)
233 1.4.4.2 snj device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
234 1.4.4.2 snj phy, reg);
235 1.4.4.2 snj
236 1.4.4.2 snj return val;
237 1.4.4.2 snj }
238 1.4.4.2 snj
239 1.4.4.2 snj static void
240 1.4.4.2 snj sunxi_emac_mii_writereg(device_t dev, int phy, int reg, int val)
241 1.4.4.2 snj {
242 1.4.4.2 snj struct sunxi_emac_softc *sc = device_private(dev);
243 1.4.4.2 snj int retry;
244 1.4.4.2 snj
245 1.4.4.2 snj WR4(sc, EMAC_MII_DATA, val);
246 1.4.4.2 snj WR4(sc, EMAC_MII_CMD,
247 1.4.4.2 snj (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
248 1.4.4.2 snj (phy << PHY_ADDR_SHIFT) |
249 1.4.4.2 snj (reg << PHY_REG_ADDR_SHIFT) |
250 1.4.4.2 snj MII_WR | MII_BUSY);
251 1.4.4.2 snj for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
252 1.4.4.2 snj if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
253 1.4.4.2 snj break;
254 1.4.4.2 snj delay(10);
255 1.4.4.2 snj }
256 1.4.4.2 snj
257 1.4.4.2 snj if (retry == 0)
258 1.4.4.2 snj device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
259 1.4.4.2 snj phy, reg);
260 1.4.4.2 snj }
261 1.4.4.2 snj
262 1.4.4.2 snj static void
263 1.4.4.2 snj sunxi_emac_update_link(struct sunxi_emac_softc *sc)
264 1.4.4.2 snj {
265 1.4.4.2 snj struct mii_data *mii = &sc->mii;
266 1.4.4.2 snj uint32_t val;
267 1.4.4.2 snj
268 1.4.4.2 snj val = RD4(sc, EMAC_BASIC_CTL_0);
269 1.4.4.2 snj val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
270 1.4.4.2 snj
271 1.4.4.2 snj if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
272 1.4.4.2 snj IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
273 1.4.4.2 snj val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
274 1.4.4.2 snj else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
275 1.4.4.2 snj val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
276 1.4.4.2 snj else
277 1.4.4.2 snj val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
278 1.4.4.2 snj
279 1.4.4.2 snj if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
280 1.4.4.2 snj val |= BASIC_CTL_DUPLEX;
281 1.4.4.2 snj
282 1.4.4.2 snj WR4(sc, EMAC_BASIC_CTL_0, val);
283 1.4.4.2 snj
284 1.4.4.2 snj val = RD4(sc, EMAC_RX_CTL_0);
285 1.4.4.2 snj val &= ~RX_FLOW_CTL_EN;
286 1.4.4.2 snj if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
287 1.4.4.2 snj val |= RX_FLOW_CTL_EN;
288 1.4.4.2 snj WR4(sc, EMAC_RX_CTL_0, val);
289 1.4.4.2 snj
290 1.4.4.2 snj val = RD4(sc, EMAC_TX_FLOW_CTL);
291 1.4.4.2 snj val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
292 1.4.4.2 snj if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
293 1.4.4.2 snj val |= TX_FLOW_CTL_EN;
294 1.4.4.2 snj if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
295 1.4.4.2 snj val |= sunxi_emac_pause_time << PAUSE_TIME_SHIFT;
296 1.4.4.2 snj WR4(sc, EMAC_TX_FLOW_CTL, val);
297 1.4.4.2 snj }
298 1.4.4.2 snj
299 1.4.4.2 snj static void
300 1.4.4.2 snj sunxi_emac_mii_statchg(struct ifnet *ifp)
301 1.4.4.2 snj {
302 1.4.4.2 snj struct sunxi_emac_softc * const sc = ifp->if_softc;
303 1.4.4.2 snj
304 1.4.4.2 snj sunxi_emac_update_link(sc);
305 1.4.4.2 snj }
306 1.4.4.2 snj
307 1.4.4.2 snj static void
308 1.4.4.2 snj sunxi_emac_dma_sync(struct sunxi_emac_softc *sc, bus_dma_tag_t dmat,
309 1.4.4.2 snj bus_dmamap_t map, int start, int end, int total, int flags)
310 1.4.4.2 snj {
311 1.4.4.2 snj if (end > start) {
312 1.4.4.2 snj bus_dmamap_sync(dmat, map, DESC_OFF(start),
313 1.4.4.2 snj DESC_OFF(end) - DESC_OFF(start), flags);
314 1.4.4.2 snj } else {
315 1.4.4.2 snj bus_dmamap_sync(dmat, map, DESC_OFF(start),
316 1.4.4.2 snj DESC_OFF(total) - DESC_OFF(start), flags);
317 1.4.4.2 snj if (DESC_OFF(end) - DESC_OFF(0) > 0)
318 1.4.4.2 snj bus_dmamap_sync(dmat, map, DESC_OFF(0),
319 1.4.4.2 snj DESC_OFF(end) - DESC_OFF(0), flags);
320 1.4.4.2 snj }
321 1.4.4.2 snj }
322 1.4.4.2 snj
323 1.4.4.2 snj static void
324 1.4.4.2 snj sunxi_emac_setup_txdesc(struct sunxi_emac_softc *sc, int index, int flags,
325 1.4.4.2 snj bus_addr_t paddr, u_int len)
326 1.4.4.2 snj {
327 1.4.4.2 snj uint32_t status, size;
328 1.4.4.2 snj
329 1.4.4.2 snj if (paddr == 0 || len == 0) {
330 1.4.4.2 snj status = 0;
331 1.4.4.2 snj size = 0;
332 1.4.4.2 snj --sc->tx.queued;
333 1.4.4.2 snj } else {
334 1.4.4.2 snj status = TX_DESC_CTL;
335 1.4.4.2 snj size = flags | len;
336 1.4.4.2 snj if ((index & (sunxi_emac_tx_interval - 1)) == 0)
337 1.4.4.2 snj size |= TX_INT_CTL;
338 1.4.4.2 snj ++sc->tx.queued;
339 1.4.4.2 snj }
340 1.4.4.2 snj
341 1.4.4.2 snj sc->tx.desc_ring[index].addr = htole32((uint32_t)paddr);
342 1.4.4.2 snj sc->tx.desc_ring[index].size = htole32(size);
343 1.4.4.2 snj sc->tx.desc_ring[index].status = htole32(status);
344 1.4.4.2 snj }
345 1.4.4.2 snj
346 1.4.4.2 snj static int
347 1.4.4.2 snj sunxi_emac_setup_txbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
348 1.4.4.2 snj {
349 1.4.4.2 snj bus_dma_segment_t *segs;
350 1.4.4.2 snj int error, nsegs, cur, i, flags;
351 1.4.4.2 snj u_int csum_flags;
352 1.4.4.2 snj
353 1.4.4.2 snj error = bus_dmamap_load_mbuf(sc->tx.buf_tag,
354 1.4.4.2 snj sc->tx.buf_map[index].map, m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
355 1.4.4.2 snj if (error == EFBIG) {
356 1.4.4.2 snj device_printf(sc->dev,
357 1.4.4.2 snj "TX packet needs too many DMA segments, dropping...\n");
358 1.4.4.2 snj m_freem(m);
359 1.4.4.2 snj return 0;
360 1.4.4.2 snj }
361 1.4.4.2 snj if (error != 0)
362 1.4.4.2 snj return 0;
363 1.4.4.2 snj
364 1.4.4.2 snj segs = sc->tx.buf_map[index].map->dm_segs;
365 1.4.4.2 snj nsegs = sc->tx.buf_map[index].map->dm_nsegs;
366 1.4.4.2 snj
367 1.4.4.2 snj flags = TX_FIR_DESC;
368 1.4.4.2 snj if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
369 1.4.4.2 snj if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 0)
370 1.4.4.2 snj csum_flags = TX_CHECKSUM_CTL_FULL;
371 1.4.4.2 snj else
372 1.4.4.2 snj csum_flags = TX_CHECKSUM_CTL_IP;
373 1.4.4.2 snj flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
374 1.4.4.2 snj }
375 1.4.4.2 snj
376 1.4.4.2 snj for (cur = index, i = 0; i < nsegs; i++) {
377 1.4.4.2 snj sc->tx.buf_map[cur].mbuf = (i == 0 ? m : NULL);
378 1.4.4.2 snj if (i == nsegs - 1)
379 1.4.4.2 snj flags |= TX_LAST_DESC;
380 1.4.4.2 snj
381 1.4.4.2 snj sunxi_emac_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
382 1.4.4.2 snj segs[i].ds_len);
383 1.4.4.2 snj flags &= ~TX_FIR_DESC;
384 1.4.4.2 snj cur = TX_NEXT(cur);
385 1.4.4.2 snj }
386 1.4.4.2 snj
387 1.4.4.2 snj bus_dmamap_sync(sc->tx.buf_tag, sc->tx.buf_map[index].map,
388 1.4.4.2 snj 0, sc->tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
389 1.4.4.2 snj
390 1.4.4.2 snj return nsegs;
391 1.4.4.2 snj }
392 1.4.4.2 snj
393 1.4.4.2 snj static void
394 1.4.4.2 snj sunxi_emac_setup_rxdesc(struct sunxi_emac_softc *sc, int index,
395 1.4.4.2 snj bus_addr_t paddr)
396 1.4.4.2 snj {
397 1.4.4.2 snj uint32_t status, size;
398 1.4.4.2 snj
399 1.4.4.2 snj status = RX_DESC_CTL;
400 1.4.4.2 snj size = MCLBYTES - 1;
401 1.4.4.2 snj
402 1.4.4.2 snj sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
403 1.4.4.2 snj sc->rx.desc_ring[index].size = htole32(size);
404 1.4.4.2 snj sc->rx.desc_ring[index].next =
405 1.4.4.2 snj htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(index)));
406 1.4.4.2 snj sc->rx.desc_ring[index].status = htole32(status);
407 1.4.4.2 snj }
408 1.4.4.2 snj
409 1.4.4.2 snj static int
410 1.4.4.2 snj sunxi_emac_setup_rxbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
411 1.4.4.2 snj {
412 1.4.4.2 snj int error;
413 1.4.4.2 snj
414 1.4.4.2 snj m_adj(m, ETHER_ALIGN);
415 1.4.4.2 snj
416 1.4.4.2 snj error = bus_dmamap_load_mbuf(sc->rx.buf_tag,
417 1.4.4.2 snj sc->rx.buf_map[index].map, m, BUS_DMA_READ|BUS_DMA_NOWAIT);
418 1.4.4.2 snj if (error != 0)
419 1.4.4.2 snj return error;
420 1.4.4.2 snj
421 1.4.4.2 snj bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
422 1.4.4.2 snj 0, sc->rx.buf_map[index].map->dm_mapsize,
423 1.4.4.2 snj BUS_DMASYNC_PREREAD);
424 1.4.4.2 snj
425 1.4.4.2 snj sc->rx.buf_map[index].mbuf = m;
426 1.4.4.2 snj sunxi_emac_setup_rxdesc(sc, index,
427 1.4.4.2 snj sc->rx.buf_map[index].map->dm_segs[0].ds_addr);
428 1.4.4.2 snj
429 1.4.4.2 snj return 0;
430 1.4.4.2 snj }
431 1.4.4.2 snj
432 1.4.4.2 snj static struct mbuf *
433 1.4.4.2 snj sunxi_emac_alloc_mbufcl(struct sunxi_emac_softc *sc)
434 1.4.4.2 snj {
435 1.4.4.2 snj struct mbuf *m;
436 1.4.4.2 snj
437 1.4.4.2 snj m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
438 1.4.4.2 snj if (m != NULL)
439 1.4.4.2 snj m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
440 1.4.4.2 snj
441 1.4.4.2 snj return m;
442 1.4.4.2 snj }
443 1.4.4.2 snj
444 1.4.4.2 snj static void
445 1.4.4.2 snj sunxi_emac_start_locked(struct sunxi_emac_softc *sc)
446 1.4.4.2 snj {
447 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
448 1.4.4.2 snj struct mbuf *m;
449 1.4.4.2 snj uint32_t val;
450 1.4.4.2 snj int cnt, nsegs, start;
451 1.4.4.2 snj
452 1.4.4.2 snj EMAC_ASSERT_LOCKED(sc);
453 1.4.4.2 snj
454 1.4.4.2 snj if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
455 1.4.4.2 snj return;
456 1.4.4.2 snj
457 1.4.4.2 snj for (cnt = 0, start = sc->tx.cur; ; cnt++) {
458 1.4.4.2 snj if (sc->tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
459 1.4.4.2 snj ifp->if_flags |= IFF_OACTIVE;
460 1.4.4.2 snj break;
461 1.4.4.2 snj }
462 1.4.4.2 snj
463 1.4.4.2 snj IFQ_POLL(&ifp->if_snd, m);
464 1.4.4.2 snj if (m == NULL)
465 1.4.4.2 snj break;
466 1.4.4.2 snj
467 1.4.4.2 snj nsegs = sunxi_emac_setup_txbuf(sc, sc->tx.cur, m);
468 1.4.4.2 snj if (nsegs == 0) {
469 1.4.4.2 snj ifp->if_flags |= IFF_OACTIVE;
470 1.4.4.2 snj break;
471 1.4.4.2 snj }
472 1.4.4.2 snj IFQ_DEQUEUE(&ifp->if_snd, m);
473 1.4.4.2 snj bpf_mtap(ifp, m);
474 1.4.4.2 snj
475 1.4.4.2 snj sc->tx.cur = TX_SKIP(sc->tx.cur, nsegs);
476 1.4.4.2 snj }
477 1.4.4.2 snj
478 1.4.4.2 snj if (cnt != 0) {
479 1.4.4.2 snj sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
480 1.4.4.2 snj start, sc->tx.cur, TX_DESC_COUNT,
481 1.4.4.2 snj BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
482 1.4.4.2 snj
483 1.4.4.2 snj /* Start and run TX DMA */
484 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_1);
485 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
486 1.4.4.2 snj }
487 1.4.4.2 snj }
488 1.4.4.2 snj
489 1.4.4.2 snj static void
490 1.4.4.2 snj sunxi_emac_start(struct ifnet *ifp)
491 1.4.4.2 snj {
492 1.4.4.2 snj struct sunxi_emac_softc *sc = ifp->if_softc;
493 1.4.4.2 snj
494 1.4.4.2 snj EMAC_LOCK(sc);
495 1.4.4.2 snj sunxi_emac_start_locked(sc);
496 1.4.4.2 snj EMAC_UNLOCK(sc);
497 1.4.4.2 snj }
498 1.4.4.2 snj
499 1.4.4.2 snj static void
500 1.4.4.2 snj sunxi_emac_tick(void *softc)
501 1.4.4.2 snj {
502 1.4.4.2 snj struct sunxi_emac_softc *sc = softc;
503 1.4.4.2 snj struct mii_data *mii = &sc->mii;
504 1.4.4.2 snj #ifndef EMAC_MPSAFE
505 1.4.4.2 snj int s = splnet();
506 1.4.4.2 snj #endif
507 1.4.4.2 snj
508 1.4.4.2 snj EMAC_LOCK(sc);
509 1.4.4.2 snj mii_tick(mii);
510 1.4.4.2 snj callout_schedule(&sc->stat_ch, hz);
511 1.4.4.2 snj EMAC_UNLOCK(sc);
512 1.4.4.2 snj
513 1.4.4.2 snj #ifndef EMAC_MPSAFE
514 1.4.4.2 snj splx(s);
515 1.4.4.2 snj #endif
516 1.4.4.2 snj }
517 1.4.4.2 snj
518 1.4.4.2 snj /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
519 1.4.4.2 snj static uint32_t
520 1.4.4.2 snj bitrev32(uint32_t x)
521 1.4.4.2 snj {
522 1.4.4.2 snj x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
523 1.4.4.2 snj x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
524 1.4.4.2 snj x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
525 1.4.4.2 snj x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
526 1.4.4.2 snj
527 1.4.4.2 snj return (x >> 16) | (x << 16);
528 1.4.4.2 snj }
529 1.4.4.2 snj
530 1.4.4.2 snj static void
531 1.4.4.2 snj sunxi_emac_setup_rxfilter(struct sunxi_emac_softc *sc)
532 1.4.4.2 snj {
533 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
534 1.4.4.2 snj uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
535 1.4.4.2 snj struct ether_multi *enm;
536 1.4.4.2 snj struct ether_multistep step;
537 1.4.4.2 snj const uint8_t *eaddr;
538 1.4.4.2 snj
539 1.4.4.2 snj EMAC_ASSERT_LOCKED(sc);
540 1.4.4.2 snj
541 1.4.4.2 snj val = 0;
542 1.4.4.2 snj hash[0] = hash[1] = 0;
543 1.4.4.2 snj
544 1.4.4.2 snj if ((ifp->if_flags & IFF_PROMISC) != 0)
545 1.4.4.2 snj val |= DIS_ADDR_FILTER;
546 1.4.4.2 snj else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
547 1.4.4.2 snj val |= RX_ALL_MULTICAST;
548 1.4.4.2 snj hash[0] = hash[1] = ~0;
549 1.4.4.2 snj } else {
550 1.4.4.2 snj val |= HASH_MULTICAST;
551 1.4.4.5 martin ETHER_LOCK(&sc->ec);
552 1.4.4.2 snj ETHER_FIRST_MULTI(step, &sc->ec, enm);
553 1.4.4.2 snj while (enm != NULL) {
554 1.4.4.2 snj crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
555 1.4.4.2 snj crc &= 0x7f;
556 1.4.4.2 snj crc = bitrev32(~crc) >> 26;
557 1.4.4.2 snj hashreg = (crc >> 5);
558 1.4.4.2 snj hashbit = (crc & 0x1f);
559 1.4.4.2 snj hash[hashreg] |= (1 << hashbit);
560 1.4.4.2 snj ETHER_NEXT_MULTI(step, enm);
561 1.4.4.2 snj }
562 1.4.4.5 martin ETHER_UNLOCK(&sc->ec);
563 1.4.4.2 snj }
564 1.4.4.2 snj
565 1.4.4.2 snj /* Write our unicast address */
566 1.4.4.2 snj eaddr = CLLADDR(ifp->if_sadl);
567 1.4.4.2 snj machi = (eaddr[5] << 8) | eaddr[4];
568 1.4.4.2 snj maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
569 1.4.4.2 snj (eaddr[0] << 0);
570 1.4.4.2 snj WR4(sc, EMAC_ADDR_HIGH(0), machi);
571 1.4.4.2 snj WR4(sc, EMAC_ADDR_LOW(0), maclo);
572 1.4.4.2 snj
573 1.4.4.2 snj /* Multicast hash filters */
574 1.4.4.2 snj WR4(sc, EMAC_RX_HASH_0, hash[1]);
575 1.4.4.2 snj WR4(sc, EMAC_RX_HASH_1, hash[0]);
576 1.4.4.2 snj
577 1.4.4.2 snj /* RX frame filter config */
578 1.4.4.2 snj WR4(sc, EMAC_RX_FRM_FLT, val);
579 1.4.4.2 snj }
580 1.4.4.2 snj
581 1.4.4.2 snj static void
582 1.4.4.2 snj sunxi_emac_enable_intr(struct sunxi_emac_softc *sc)
583 1.4.4.2 snj {
584 1.4.4.2 snj /* Enable interrupts */
585 1.4.4.2 snj WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
586 1.4.4.2 snj }
587 1.4.4.2 snj
588 1.4.4.2 snj static void
589 1.4.4.2 snj sunxi_emac_disable_intr(struct sunxi_emac_softc *sc)
590 1.4.4.2 snj {
591 1.4.4.2 snj /* Disable interrupts */
592 1.4.4.2 snj WR4(sc, EMAC_INT_EN, 0);
593 1.4.4.2 snj }
594 1.4.4.2 snj
595 1.4.4.2 snj static int
596 1.4.4.2 snj sunxi_emac_init_locked(struct sunxi_emac_softc *sc)
597 1.4.4.2 snj {
598 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
599 1.4.4.2 snj struct mii_data *mii = &sc->mii;
600 1.4.4.2 snj uint32_t val;
601 1.4.4.2 snj
602 1.4.4.2 snj EMAC_ASSERT_LOCKED(sc);
603 1.4.4.2 snj
604 1.4.4.2 snj if ((ifp->if_flags & IFF_RUNNING) != 0)
605 1.4.4.2 snj return 0;
606 1.4.4.2 snj
607 1.4.4.2 snj sunxi_emac_setup_rxfilter(sc);
608 1.4.4.2 snj
609 1.4.4.2 snj /* Configure DMA burst length and priorities */
610 1.4.4.2 snj val = sunxi_emac_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
611 1.4.4.2 snj if (sunxi_emac_rx_tx_pri)
612 1.4.4.2 snj val |= BASIC_CTL_RX_TX_PRI;
613 1.4.4.2 snj WR4(sc, EMAC_BASIC_CTL_1, val);
614 1.4.4.2 snj
615 1.4.4.2 snj /* Enable interrupts */
616 1.4.4.2 snj sunxi_emac_enable_intr(sc);
617 1.4.4.2 snj
618 1.4.4.2 snj /* Enable transmit DMA */
619 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_1);
620 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
621 1.4.4.2 snj
622 1.4.4.2 snj /* Enable receive DMA */
623 1.4.4.2 snj val = RD4(sc, EMAC_RX_CTL_1);
624 1.4.4.2 snj WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
625 1.4.4.2 snj
626 1.4.4.2 snj /* Enable transmitter */
627 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_0);
628 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
629 1.4.4.2 snj
630 1.4.4.2 snj /* Enable receiver */
631 1.4.4.2 snj val = RD4(sc, EMAC_RX_CTL_0);
632 1.4.4.2 snj WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
633 1.4.4.2 snj
634 1.4.4.2 snj ifp->if_flags |= IFF_RUNNING;
635 1.4.4.2 snj ifp->if_flags &= ~IFF_OACTIVE;
636 1.4.4.2 snj
637 1.4.4.2 snj mii_mediachg(mii);
638 1.4.4.2 snj callout_schedule(&sc->stat_ch, hz);
639 1.4.4.2 snj
640 1.4.4.2 snj return 0;
641 1.4.4.2 snj }
642 1.4.4.2 snj
643 1.4.4.2 snj static int
644 1.4.4.2 snj sunxi_emac_init(struct ifnet *ifp)
645 1.4.4.2 snj {
646 1.4.4.2 snj struct sunxi_emac_softc *sc = ifp->if_softc;
647 1.4.4.2 snj int error;
648 1.4.4.2 snj
649 1.4.4.2 snj EMAC_LOCK(sc);
650 1.4.4.2 snj error = sunxi_emac_init_locked(sc);
651 1.4.4.2 snj EMAC_UNLOCK(sc);
652 1.4.4.2 snj
653 1.4.4.2 snj return error;
654 1.4.4.2 snj }
655 1.4.4.2 snj
656 1.4.4.2 snj static void
657 1.4.4.2 snj sunxi_emac_stop_locked(struct sunxi_emac_softc *sc, int disable)
658 1.4.4.2 snj {
659 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
660 1.4.4.2 snj uint32_t val;
661 1.4.4.2 snj
662 1.4.4.2 snj EMAC_ASSERT_LOCKED(sc);
663 1.4.4.2 snj
664 1.4.4.2 snj callout_stop(&sc->stat_ch);
665 1.4.4.2 snj
666 1.4.4.2 snj mii_down(&sc->mii);
667 1.4.4.2 snj
668 1.4.4.2 snj /* Stop transmit DMA and flush data in the TX FIFO */
669 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_1);
670 1.4.4.2 snj val &= ~TX_DMA_EN;
671 1.4.4.2 snj val |= FLUSH_TX_FIFO;
672 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_1, val);
673 1.4.4.2 snj
674 1.4.4.2 snj /* Disable transmitter */
675 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_0);
676 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
677 1.4.4.2 snj
678 1.4.4.2 snj /* Disable receiver */
679 1.4.4.2 snj val = RD4(sc, EMAC_RX_CTL_0);
680 1.4.4.2 snj WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
681 1.4.4.2 snj
682 1.4.4.2 snj /* Disable interrupts */
683 1.4.4.2 snj sunxi_emac_disable_intr(sc);
684 1.4.4.2 snj
685 1.4.4.2 snj /* Disable transmit DMA */
686 1.4.4.2 snj val = RD4(sc, EMAC_TX_CTL_1);
687 1.4.4.2 snj WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
688 1.4.4.2 snj
689 1.4.4.2 snj /* Disable receive DMA */
690 1.4.4.2 snj val = RD4(sc, EMAC_RX_CTL_1);
691 1.4.4.2 snj WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
692 1.4.4.2 snj
693 1.4.4.2 snj ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
694 1.4.4.2 snj }
695 1.4.4.2 snj
696 1.4.4.2 snj static void
697 1.4.4.2 snj sunxi_emac_stop(struct ifnet *ifp, int disable)
698 1.4.4.2 snj {
699 1.4.4.2 snj struct sunxi_emac_softc * const sc = ifp->if_softc;
700 1.4.4.2 snj
701 1.4.4.2 snj EMAC_LOCK(sc);
702 1.4.4.2 snj sunxi_emac_stop_locked(sc, disable);
703 1.4.4.2 snj EMAC_UNLOCK(sc);
704 1.4.4.2 snj }
705 1.4.4.2 snj
706 1.4.4.2 snj static int
707 1.4.4.2 snj sunxi_emac_rxintr(struct sunxi_emac_softc *sc)
708 1.4.4.2 snj {
709 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
710 1.4.4.2 snj int error, index, len, npkt;
711 1.4.4.2 snj struct mbuf *m, *m0;
712 1.4.4.2 snj uint32_t status;
713 1.4.4.2 snj
714 1.4.4.2 snj npkt = 0;
715 1.4.4.2 snj
716 1.4.4.2 snj for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
717 1.4.4.2 snj sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
718 1.4.4.2 snj index, index + 1,
719 1.4.4.2 snj RX_DESC_COUNT, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
720 1.4.4.2 snj
721 1.4.4.2 snj status = le32toh(sc->rx.desc_ring[index].status);
722 1.4.4.2 snj if ((status & RX_DESC_CTL) != 0)
723 1.4.4.2 snj break;
724 1.4.4.2 snj
725 1.4.4.2 snj bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
726 1.4.4.2 snj 0, sc->rx.buf_map[index].map->dm_mapsize,
727 1.4.4.2 snj BUS_DMASYNC_POSTREAD);
728 1.4.4.2 snj bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
729 1.4.4.2 snj
730 1.4.4.2 snj len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
731 1.4.4.2 snj if (len != 0) {
732 1.4.4.2 snj m = sc->rx.buf_map[index].mbuf;
733 1.4.4.2 snj m_set_rcvif(m, ifp);
734 1.4.4.2 snj m->m_flags |= M_HASFCS;
735 1.4.4.2 snj m->m_pkthdr.len = len;
736 1.4.4.2 snj m->m_len = len;
737 1.4.4.2 snj m->m_nextpkt = NULL;
738 1.4.4.2 snj
739 1.4.4.2 snj if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0 &&
740 1.4.4.2 snj (status & RX_FRM_TYPE) != 0) {
741 1.4.4.2 snj m->m_pkthdr.csum_flags = M_CSUM_IPv4;
742 1.4.4.2 snj if ((status & RX_HEADER_ERR) != 0)
743 1.4.4.2 snj m->m_pkthdr.csum_flags |=
744 1.4.4.2 snj M_CSUM_IPv4_BAD;
745 1.4.4.2 snj if ((status & RX_PAYLOAD_ERR) == 0) {
746 1.4.4.2 snj m->m_pkthdr.csum_flags |=
747 1.4.4.2 snj M_CSUM_DATA;
748 1.4.4.2 snj m->m_pkthdr.csum_data = 0xffff;
749 1.4.4.2 snj }
750 1.4.4.2 snj }
751 1.4.4.2 snj
752 1.4.4.2 snj ++npkt;
753 1.4.4.2 snj
754 1.4.4.2 snj if_percpuq_enqueue(ifp->if_percpuq, m);
755 1.4.4.2 snj }
756 1.4.4.2 snj
757 1.4.4.2 snj if ((m0 = sunxi_emac_alloc_mbufcl(sc)) != NULL) {
758 1.4.4.2 snj error = sunxi_emac_setup_rxbuf(sc, index, m0);
759 1.4.4.2 snj if (error != 0) {
760 1.4.4.2 snj /* XXX hole in RX ring */
761 1.4.4.2 snj }
762 1.4.4.2 snj } else
763 1.4.4.2 snj ifp->if_ierrors++;
764 1.4.4.2 snj
765 1.4.4.2 snj sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
766 1.4.4.2 snj index, index + 1,
767 1.4.4.2 snj RX_DESC_COUNT, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
768 1.4.4.2 snj }
769 1.4.4.2 snj
770 1.4.4.2 snj sc->rx.cur = index;
771 1.4.4.2 snj
772 1.4.4.2 snj return npkt;
773 1.4.4.2 snj }
774 1.4.4.2 snj
775 1.4.4.2 snj static void
776 1.4.4.2 snj sunxi_emac_txintr(struct sunxi_emac_softc *sc)
777 1.4.4.2 snj {
778 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
779 1.4.4.2 snj struct sunxi_emac_bufmap *bmap;
780 1.4.4.2 snj struct sunxi_emac_desc *desc;
781 1.4.4.2 snj uint32_t status;
782 1.4.4.2 snj int i;
783 1.4.4.2 snj
784 1.4.4.2 snj EMAC_ASSERT_LOCKED(sc);
785 1.4.4.2 snj
786 1.4.4.2 snj for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
787 1.4.4.2 snj KASSERT(sc->tx.queued > 0 && sc->tx.queued <= TX_DESC_COUNT);
788 1.4.4.2 snj sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
789 1.4.4.2 snj i, i + 1, TX_DESC_COUNT,
790 1.4.4.2 snj BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
791 1.4.4.2 snj desc = &sc->tx.desc_ring[i];
792 1.4.4.2 snj status = le32toh(desc->status);
793 1.4.4.2 snj if ((status & TX_DESC_CTL) != 0)
794 1.4.4.2 snj break;
795 1.4.4.2 snj bmap = &sc->tx.buf_map[i];
796 1.4.4.2 snj if (bmap->mbuf != NULL) {
797 1.4.4.2 snj bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
798 1.4.4.2 snj 0, bmap->map->dm_mapsize,
799 1.4.4.2 snj BUS_DMASYNC_POSTWRITE);
800 1.4.4.2 snj bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
801 1.4.4.2 snj m_freem(bmap->mbuf);
802 1.4.4.2 snj bmap->mbuf = NULL;
803 1.4.4.2 snj }
804 1.4.4.2 snj
805 1.4.4.2 snj sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
806 1.4.4.2 snj sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
807 1.4.4.2 snj i, i + 1, TX_DESC_COUNT,
808 1.4.4.2 snj BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
809 1.4.4.2 snj
810 1.4.4.2 snj ifp->if_flags &= ~IFF_OACTIVE;
811 1.4.4.2 snj ifp->if_opackets++;
812 1.4.4.2 snj }
813 1.4.4.2 snj
814 1.4.4.2 snj sc->tx.next = i;
815 1.4.4.2 snj }
816 1.4.4.2 snj
817 1.4.4.2 snj static int
818 1.4.4.2 snj sunxi_emac_intr(void *arg)
819 1.4.4.2 snj {
820 1.4.4.2 snj struct sunxi_emac_softc *sc = arg;
821 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
822 1.4.4.2 snj uint32_t val;
823 1.4.4.2 snj
824 1.4.4.2 snj EMAC_LOCK(sc);
825 1.4.4.2 snj
826 1.4.4.2 snj val = RD4(sc, EMAC_INT_STA);
827 1.4.4.2 snj WR4(sc, EMAC_INT_STA, val);
828 1.4.4.2 snj
829 1.4.4.2 snj if (val & RX_INT)
830 1.4.4.2 snj sunxi_emac_rxintr(sc);
831 1.4.4.2 snj
832 1.4.4.2 snj if (val & (TX_INT|TX_BUF_UA_INT)) {
833 1.4.4.2 snj sunxi_emac_txintr(sc);
834 1.4.4.2 snj if_schedule_deferred_start(ifp);
835 1.4.4.2 snj }
836 1.4.4.2 snj
837 1.4.4.2 snj EMAC_UNLOCK(sc);
838 1.4.4.2 snj
839 1.4.4.2 snj return 1;
840 1.4.4.2 snj }
841 1.4.4.2 snj
842 1.4.4.2 snj static int
843 1.4.4.2 snj sunxi_emac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
844 1.4.4.2 snj {
845 1.4.4.2 snj struct sunxi_emac_softc *sc = ifp->if_softc;
846 1.4.4.2 snj struct mii_data *mii = &sc->mii;
847 1.4.4.2 snj struct ifreq *ifr = data;
848 1.4.4.2 snj int error, s;
849 1.4.4.2 snj
850 1.4.4.2 snj #ifndef EMAC_MPSAFE
851 1.4.4.2 snj s = splnet();
852 1.4.4.2 snj #endif
853 1.4.4.2 snj
854 1.4.4.2 snj switch (cmd) {
855 1.4.4.2 snj case SIOCSIFMEDIA:
856 1.4.4.2 snj case SIOCGIFMEDIA:
857 1.4.4.2 snj #ifdef EMAC_MPSAFE
858 1.4.4.2 snj s = splnet();
859 1.4.4.2 snj #endif
860 1.4.4.2 snj error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
861 1.4.4.2 snj #ifdef EMAC_MPSAFE
862 1.4.4.2 snj splx(s);
863 1.4.4.2 snj #endif
864 1.4.4.2 snj break;
865 1.4.4.2 snj default:
866 1.4.4.2 snj #ifdef EMAC_MPSAFE
867 1.4.4.2 snj s = splnet();
868 1.4.4.2 snj #endif
869 1.4.4.2 snj error = ether_ioctl(ifp, cmd, data);
870 1.4.4.2 snj #ifdef EMAC_MPSAFE
871 1.4.4.2 snj splx(s);
872 1.4.4.2 snj #endif
873 1.4.4.2 snj if (error != ENETRESET)
874 1.4.4.2 snj break;
875 1.4.4.2 snj
876 1.4.4.2 snj error = 0;
877 1.4.4.2 snj
878 1.4.4.2 snj if (cmd == SIOCSIFCAP)
879 1.4.4.2 snj error = (*ifp->if_init)(ifp);
880 1.4.4.2 snj else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
881 1.4.4.2 snj ;
882 1.4.4.2 snj else if ((ifp->if_flags & IFF_RUNNING) != 0) {
883 1.4.4.2 snj EMAC_LOCK(sc);
884 1.4.4.2 snj sunxi_emac_setup_rxfilter(sc);
885 1.4.4.2 snj EMAC_UNLOCK(sc);
886 1.4.4.2 snj }
887 1.4.4.2 snj break;
888 1.4.4.2 snj }
889 1.4.4.2 snj
890 1.4.4.2 snj #ifndef EMAC_MPSAFE
891 1.4.4.2 snj splx(s);
892 1.4.4.2 snj #endif
893 1.4.4.2 snj
894 1.4.4.2 snj return error;
895 1.4.4.2 snj }
896 1.4.4.2 snj
897 1.4.4.2 snj static int
898 1.4.4.2 snj sunxi_emac_setup_phy(struct sunxi_emac_softc *sc)
899 1.4.4.2 snj {
900 1.4.4.2 snj uint32_t reg, tx_delay, rx_delay;
901 1.4.4.2 snj const char *phy_type;
902 1.4.4.2 snj
903 1.4.4.2 snj phy_type = fdtbus_get_string(sc->phandle, "phy-mode");
904 1.4.4.2 snj if (phy_type == NULL)
905 1.4.4.2 snj return 0;
906 1.4.4.2 snj
907 1.4.4.2 snj aprint_debug_dev(sc->dev, "PHY type: %s\n", phy_type);
908 1.4.4.2 snj
909 1.4.4.2 snj reg = SYSCONRD4(sc, 0);
910 1.4.4.2 snj
911 1.4.4.2 snj reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
912 1.4.4.2 snj if (strcmp(phy_type, "rgmii") == 0)
913 1.4.4.2 snj reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
914 1.4.4.2 snj else if (strcmp(phy_type, "rmii") == 0)
915 1.4.4.2 snj reg |= EMAC_CLK_RMII_EN;
916 1.4.4.2 snj else
917 1.4.4.2 snj reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
918 1.4.4.2 snj
919 1.4.4.2 snj if (of_getprop_uint32(sc->phandle, "tx-delay", &tx_delay) == 0) {
920 1.4.4.2 snj reg &= ~EMAC_CLK_ETXDC;
921 1.4.4.2 snj reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
922 1.4.4.2 snj }
923 1.4.4.2 snj if (of_getprop_uint32(sc->phandle, "rx-delay", &rx_delay) == 0) {
924 1.4.4.2 snj reg &= ~EMAC_CLK_ERXDC;
925 1.4.4.2 snj reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
926 1.4.4.2 snj }
927 1.4.4.2 snj
928 1.4.4.2 snj if (sc->type == EMAC_H3) {
929 1.4.4.2 snj if (of_hasprop(sc->phandle, "allwinner,use-internal-phy")) {
930 1.4.4.2 snj reg |= EMAC_CLK_EPHY_SELECT;
931 1.4.4.2 snj reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
932 1.4.4.2 snj if (of_hasprop(sc->phandle,
933 1.4.4.2 snj "allwinner,leds-active-low"))
934 1.4.4.2 snj reg |= EMAC_CLK_EPHY_LED_POL;
935 1.4.4.2 snj else
936 1.4.4.2 snj reg &= ~EMAC_CLK_EPHY_LED_POL;
937 1.4.4.2 snj
938 1.4.4.2 snj /* Set internal PHY addr to 1 */
939 1.4.4.2 snj reg &= ~EMAC_CLK_EPHY_ADDR;
940 1.4.4.2 snj reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
941 1.4.4.2 snj } else {
942 1.4.4.2 snj reg &= ~EMAC_CLK_EPHY_SELECT;
943 1.4.4.2 snj }
944 1.4.4.2 snj }
945 1.4.4.2 snj
946 1.4.4.2 snj aprint_debug_dev(sc->dev, "EMAC clock: 0x%08x\n", reg);
947 1.4.4.2 snj
948 1.4.4.2 snj SYSCONWR4(sc, 0, reg);
949 1.4.4.2 snj
950 1.4.4.2 snj return 0;
951 1.4.4.2 snj }
952 1.4.4.2 snj
953 1.4.4.2 snj static int
954 1.4.4.2 snj sunxi_emac_setup_resources(struct sunxi_emac_softc *sc)
955 1.4.4.2 snj {
956 1.4.4.2 snj u_int freq;
957 1.4.4.2 snj int error, div;
958 1.4.4.2 snj
959 1.4.4.2 snj /* Configure PHY for MII or RGMII mode */
960 1.4.4.2 snj if (sunxi_emac_setup_phy(sc) != 0)
961 1.4.4.2 snj return ENXIO;
962 1.4.4.2 snj
963 1.4.4.2 snj /* Enable clocks */
964 1.4.4.2 snj error = clk_enable(sc->clk_ahb);
965 1.4.4.2 snj if (error != 0) {
966 1.4.4.2 snj aprint_error_dev(sc->dev, "cannot enable ahb clock\n");
967 1.4.4.2 snj return error;
968 1.4.4.2 snj }
969 1.4.4.2 snj
970 1.4.4.2 snj if (sc->clk_ephy != NULL) {
971 1.4.4.2 snj error = clk_enable(sc->clk_ephy);
972 1.4.4.2 snj if (error != 0) {
973 1.4.4.2 snj aprint_error_dev(sc->dev, "cannot enable ephy clock\n");
974 1.4.4.2 snj return error;
975 1.4.4.2 snj }
976 1.4.4.2 snj }
977 1.4.4.2 snj
978 1.4.4.2 snj /* De-assert reset */
979 1.4.4.2 snj error = fdtbus_reset_deassert(sc->rst_ahb);
980 1.4.4.2 snj if (error != 0) {
981 1.4.4.2 snj aprint_error_dev(sc->dev, "cannot de-assert ahb reset\n");
982 1.4.4.2 snj return error;
983 1.4.4.2 snj }
984 1.4.4.2 snj if (sc->rst_ephy != NULL) {
985 1.4.4.2 snj error = fdtbus_reset_deassert(sc->rst_ephy);
986 1.4.4.2 snj if (error != 0) {
987 1.4.4.2 snj aprint_error_dev(sc->dev,
988 1.4.4.2 snj "cannot de-assert ephy reset\n");
989 1.4.4.2 snj return error;
990 1.4.4.2 snj }
991 1.4.4.2 snj }
992 1.4.4.2 snj
993 1.4.4.2 snj /* Enable PHY regulator if applicable */
994 1.4.4.2 snj if (sc->reg_phy != NULL) {
995 1.4.4.2 snj error = fdtbus_regulator_enable(sc->reg_phy);
996 1.4.4.2 snj if (error != 0) {
997 1.4.4.2 snj aprint_error_dev(sc->dev,
998 1.4.4.2 snj "cannot enable PHY regulator\n");
999 1.4.4.2 snj return error;
1000 1.4.4.2 snj }
1001 1.4.4.2 snj }
1002 1.4.4.2 snj
1003 1.4.4.2 snj /* Determine MDC clock divide ratio based on AHB clock */
1004 1.4.4.2 snj freq = clk_get_rate(sc->clk_ahb);
1005 1.4.4.2 snj if (freq == 0) {
1006 1.4.4.2 snj aprint_error_dev(sc->dev, "cannot get AHB clock frequency\n");
1007 1.4.4.2 snj return ENXIO;
1008 1.4.4.2 snj }
1009 1.4.4.2 snj div = freq / MDIO_FREQ;
1010 1.4.4.2 snj if (div <= 16)
1011 1.4.4.2 snj sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1012 1.4.4.2 snj else if (div <= 32)
1013 1.4.4.2 snj sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1014 1.4.4.2 snj else if (div <= 64)
1015 1.4.4.2 snj sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1016 1.4.4.2 snj else if (div <= 128)
1017 1.4.4.2 snj sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1018 1.4.4.2 snj else {
1019 1.4.4.2 snj aprint_error_dev(sc->dev,
1020 1.4.4.2 snj "cannot determine MDC clock divide ratio\n");
1021 1.4.4.2 snj return ENXIO;
1022 1.4.4.2 snj }
1023 1.4.4.2 snj
1024 1.4.4.2 snj aprint_debug_dev(sc->dev, "AHB frequency %u Hz, MDC div: 0x%x\n",
1025 1.4.4.2 snj freq, sc->mdc_div_ratio_m);
1026 1.4.4.2 snj
1027 1.4.4.2 snj return 0;
1028 1.4.4.2 snj }
1029 1.4.4.2 snj
1030 1.4.4.2 snj static void
1031 1.4.4.2 snj sunxi_emac_get_eaddr(struct sunxi_emac_softc *sc, uint8_t *eaddr)
1032 1.4.4.2 snj {
1033 1.4.4.2 snj uint32_t maclo, machi;
1034 1.4.4.2 snj #if notyet
1035 1.4.4.2 snj u_char rootkey[16];
1036 1.4.4.2 snj #endif
1037 1.4.4.2 snj
1038 1.4.4.2 snj machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1039 1.4.4.2 snj maclo = RD4(sc, EMAC_ADDR_LOW(0));
1040 1.4.4.2 snj
1041 1.4.4.2 snj if (maclo == 0xffffffff && machi == 0xffff) {
1042 1.4.4.2 snj #if notyet
1043 1.4.4.2 snj /* MAC address in hardware is invalid, create one */
1044 1.4.4.2 snj if (aw_sid_get_rootkey(rootkey) == 0 &&
1045 1.4.4.2 snj (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1046 1.4.4.2 snj rootkey[15]) != 0) {
1047 1.4.4.2 snj /* MAC address is derived from the root key in SID */
1048 1.4.4.2 snj maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1049 1.4.4.2 snj (rootkey[3] << 8) | 0x02;
1050 1.4.4.2 snj machi = (rootkey[15] << 8) | rootkey[14];
1051 1.4.4.2 snj } else {
1052 1.4.4.2 snj #endif
1053 1.4.4.2 snj /* Create one */
1054 1.4.4.2 snj maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
1055 1.4.4.2 snj machi = cprng_strong32() & 0xffff;
1056 1.4.4.2 snj #if notyet
1057 1.4.4.2 snj }
1058 1.4.4.2 snj #endif
1059 1.4.4.2 snj }
1060 1.4.4.2 snj
1061 1.4.4.2 snj eaddr[0] = maclo & 0xff;
1062 1.4.4.2 snj eaddr[1] = (maclo >> 8) & 0xff;
1063 1.4.4.2 snj eaddr[2] = (maclo >> 16) & 0xff;
1064 1.4.4.2 snj eaddr[3] = (maclo >> 24) & 0xff;
1065 1.4.4.2 snj eaddr[4] = machi & 0xff;
1066 1.4.4.2 snj eaddr[5] = (machi >> 8) & 0xff;
1067 1.4.4.2 snj }
1068 1.4.4.2 snj
1069 1.4.4.2 snj #ifdef SUNXI_EMAC_DEBUG
1070 1.4.4.2 snj static void
1071 1.4.4.2 snj sunxi_emac_dump_regs(struct sunxi_emac_softc *sc)
1072 1.4.4.2 snj {
1073 1.4.4.2 snj static const struct {
1074 1.4.4.2 snj const char *name;
1075 1.4.4.2 snj u_int reg;
1076 1.4.4.2 snj } regs[] = {
1077 1.4.4.2 snj { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1078 1.4.4.2 snj { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1079 1.4.4.2 snj { "INT_STA", EMAC_INT_STA },
1080 1.4.4.2 snj { "INT_EN", EMAC_INT_EN },
1081 1.4.4.2 snj { "TX_CTL_0", EMAC_TX_CTL_0 },
1082 1.4.4.2 snj { "TX_CTL_1", EMAC_TX_CTL_1 },
1083 1.4.4.2 snj { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1084 1.4.4.2 snj { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1085 1.4.4.2 snj { "RX_CTL_0", EMAC_RX_CTL_0 },
1086 1.4.4.2 snj { "RX_CTL_1", EMAC_RX_CTL_1 },
1087 1.4.4.2 snj { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1088 1.4.4.2 snj { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1089 1.4.4.2 snj { "RX_HASH_0", EMAC_RX_HASH_0 },
1090 1.4.4.2 snj { "RX_HASH_1", EMAC_RX_HASH_1 },
1091 1.4.4.2 snj { "MII_CMD", EMAC_MII_CMD },
1092 1.4.4.2 snj { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1093 1.4.4.2 snj { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1094 1.4.4.2 snj { "TX_DMA_STA", EMAC_TX_DMA_STA },
1095 1.4.4.2 snj { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1096 1.4.4.2 snj { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1097 1.4.4.2 snj { "RX_DMA_STA", EMAC_RX_DMA_STA },
1098 1.4.4.2 snj { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1099 1.4.4.2 snj { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1100 1.4.4.2 snj { "RGMII_STA", EMAC_RGMII_STA },
1101 1.4.4.2 snj };
1102 1.4.4.2 snj u_int n;
1103 1.4.4.2 snj
1104 1.4.4.2 snj for (n = 0; n < __arraycount(regs); n++)
1105 1.4.4.2 snj device_printf(dev, " %-20s %08x\n", regs[n].name,
1106 1.4.4.2 snj RD4(sc, regs[n].reg));
1107 1.4.4.2 snj }
1108 1.4.4.2 snj #endif
1109 1.4.4.2 snj
1110 1.4.4.2 snj static int
1111 1.4.4.2 snj sunxi_emac_phy_reset(struct sunxi_emac_softc *sc)
1112 1.4.4.2 snj {
1113 1.4.4.2 snj uint32_t delay_prop[3];
1114 1.4.4.2 snj int pin_value;
1115 1.4.4.2 snj
1116 1.4.4.2 snj if (sc->pin_reset == NULL)
1117 1.4.4.2 snj return 0;
1118 1.4.4.2 snj
1119 1.4.4.2 snj if (OF_getprop(sc->phandle, "allwinner,reset-delays-us", delay_prop,
1120 1.4.4.2 snj sizeof(delay_prop)) <= 0)
1121 1.4.4.2 snj return ENXIO;
1122 1.4.4.2 snj
1123 1.4.4.2 snj pin_value = of_hasprop(sc->phandle, "allwinner,reset-active-low");
1124 1.4.4.2 snj
1125 1.4.4.2 snj fdtbus_gpio_write(sc->pin_reset, pin_value);
1126 1.4.4.2 snj delay(htole32(delay_prop[0]));
1127 1.4.4.2 snj fdtbus_gpio_write(sc->pin_reset, !pin_value);
1128 1.4.4.2 snj delay(htole32(delay_prop[1]));
1129 1.4.4.2 snj fdtbus_gpio_write(sc->pin_reset, pin_value);
1130 1.4.4.2 snj delay(htole32(delay_prop[2]));
1131 1.4.4.2 snj
1132 1.4.4.2 snj return 0;
1133 1.4.4.2 snj }
1134 1.4.4.2 snj
1135 1.4.4.2 snj static int
1136 1.4.4.2 snj sunxi_emac_reset(struct sunxi_emac_softc *sc)
1137 1.4.4.2 snj {
1138 1.4.4.2 snj int retry;
1139 1.4.4.2 snj
1140 1.4.4.2 snj /* Reset PHY if necessary */
1141 1.4.4.2 snj if (sunxi_emac_phy_reset(sc) != 0) {
1142 1.4.4.2 snj aprint_error_dev(sc->dev, "failed to reset PHY\n");
1143 1.4.4.2 snj return ENXIO;
1144 1.4.4.2 snj }
1145 1.4.4.2 snj
1146 1.4.4.2 snj /* Soft reset all registers and logic */
1147 1.4.4.2 snj WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1148 1.4.4.2 snj
1149 1.4.4.2 snj /* Wait for soft reset bit to self-clear */
1150 1.4.4.2 snj for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1151 1.4.4.2 snj if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1152 1.4.4.2 snj break;
1153 1.4.4.2 snj delay(10);
1154 1.4.4.2 snj }
1155 1.4.4.2 snj if (retry == 0) {
1156 1.4.4.2 snj aprint_error_dev(sc->dev, "soft reset timed out\n");
1157 1.4.4.2 snj #ifdef SUNXI_EMAC_DEBUG
1158 1.4.4.2 snj sunxi_emac_dump_regs(sc);
1159 1.4.4.2 snj #endif
1160 1.4.4.2 snj return ETIMEDOUT;
1161 1.4.4.2 snj }
1162 1.4.4.2 snj
1163 1.4.4.2 snj return 0;
1164 1.4.4.2 snj }
1165 1.4.4.2 snj
1166 1.4.4.2 snj static int
1167 1.4.4.2 snj sunxi_emac_setup_dma(struct sunxi_emac_softc *sc)
1168 1.4.4.2 snj {
1169 1.4.4.2 snj struct mbuf *m;
1170 1.4.4.2 snj int error, nsegs, i;
1171 1.4.4.2 snj
1172 1.4.4.2 snj /* Setup TX ring */
1173 1.4.4.2 snj sc->tx.buf_tag = sc->tx.desc_tag = sc->dmat;
1174 1.4.4.2 snj error = bus_dmamap_create(sc->dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE, 0,
1175 1.4.4.2 snj BUS_DMA_WAITOK, &sc->tx.desc_map);
1176 1.4.4.2 snj if (error)
1177 1.4.4.2 snj return error;
1178 1.4.4.2 snj error = bus_dmamem_alloc(sc->dmat, TX_DESC_SIZE, DESC_ALIGN, 0,
1179 1.4.4.2 snj &sc->tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1180 1.4.4.2 snj if (error)
1181 1.4.4.2 snj return error;
1182 1.4.4.2 snj error = bus_dmamem_map(sc->dmat, &sc->tx.desc_dmaseg, nsegs,
1183 1.4.4.2 snj TX_DESC_SIZE, (void *)&sc->tx.desc_ring,
1184 1.4.4.2 snj BUS_DMA_WAITOK);
1185 1.4.4.2 snj if (error)
1186 1.4.4.2 snj return error;
1187 1.4.4.2 snj error = bus_dmamap_load(sc->dmat, sc->tx.desc_map, sc->tx.desc_ring,
1188 1.4.4.2 snj TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1189 1.4.4.2 snj if (error)
1190 1.4.4.2 snj return error;
1191 1.4.4.2 snj sc->tx.desc_ring_paddr = sc->tx.desc_map->dm_segs[0].ds_addr;
1192 1.4.4.2 snj
1193 1.4.4.2 snj memset(sc->tx.desc_ring, 0, TX_DESC_SIZE);
1194 1.4.4.2 snj bus_dmamap_sync(sc->dmat, sc->tx.desc_map, 0, TX_DESC_SIZE,
1195 1.4.4.2 snj BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1196 1.4.4.2 snj
1197 1.4.4.2 snj for (i = 0; i < TX_DESC_COUNT; i++)
1198 1.4.4.2 snj sc->tx.desc_ring[i].next =
1199 1.4.4.2 snj htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1200 1.4.4.2 snj
1201 1.4.4.2 snj sc->tx.queued = TX_DESC_COUNT;
1202 1.4.4.2 snj for (i = 0; i < TX_DESC_COUNT; i++) {
1203 1.4.4.2 snj error = bus_dmamap_create(sc->tx.buf_tag, MCLBYTES,
1204 1.4.4.2 snj TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
1205 1.4.4.2 snj &sc->tx.buf_map[i].map);
1206 1.4.4.2 snj if (error != 0) {
1207 1.4.4.2 snj device_printf(sc->dev, "cannot create TX buffer map\n");
1208 1.4.4.2 snj return error;
1209 1.4.4.2 snj }
1210 1.4.4.2 snj sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
1211 1.4.4.2 snj }
1212 1.4.4.2 snj
1213 1.4.4.2 snj /* Setup RX ring */
1214 1.4.4.2 snj sc->rx.buf_tag = sc->rx.desc_tag = sc->dmat;
1215 1.4.4.2 snj error = bus_dmamap_create(sc->dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE, 0,
1216 1.4.4.2 snj BUS_DMA_WAITOK, &sc->rx.desc_map);
1217 1.4.4.2 snj if (error)
1218 1.4.4.2 snj return error;
1219 1.4.4.2 snj error = bus_dmamem_alloc(sc->dmat, RX_DESC_SIZE, DESC_ALIGN, 0,
1220 1.4.4.2 snj &sc->rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1221 1.4.4.2 snj if (error)
1222 1.4.4.2 snj return error;
1223 1.4.4.2 snj error = bus_dmamem_map(sc->dmat, &sc->rx.desc_dmaseg, nsegs,
1224 1.4.4.2 snj RX_DESC_SIZE, (void *)&sc->rx.desc_ring,
1225 1.4.4.2 snj BUS_DMA_WAITOK);
1226 1.4.4.2 snj if (error)
1227 1.4.4.2 snj return error;
1228 1.4.4.2 snj error = bus_dmamap_load(sc->dmat, sc->rx.desc_map, sc->rx.desc_ring,
1229 1.4.4.2 snj RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1230 1.4.4.2 snj if (error)
1231 1.4.4.2 snj return error;
1232 1.4.4.2 snj sc->rx.desc_ring_paddr = sc->rx.desc_map->dm_segs[0].ds_addr;
1233 1.4.4.2 snj
1234 1.4.4.2 snj memset(sc->rx.desc_ring, 0, RX_DESC_SIZE);
1235 1.4.4.2 snj
1236 1.4.4.2 snj for (i = 0; i < RX_DESC_COUNT; i++) {
1237 1.4.4.2 snj error = bus_dmamap_create(sc->rx.buf_tag, MCLBYTES,
1238 1.4.4.2 snj RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
1239 1.4.4.2 snj &sc->rx.buf_map[i].map);
1240 1.4.4.2 snj if (error != 0) {
1241 1.4.4.2 snj device_printf(sc->dev, "cannot create RX buffer map\n");
1242 1.4.4.2 snj return error;
1243 1.4.4.2 snj }
1244 1.4.4.2 snj if ((m = sunxi_emac_alloc_mbufcl(sc)) == NULL) {
1245 1.4.4.2 snj device_printf(sc->dev, "cannot allocate RX mbuf\n");
1246 1.4.4.2 snj return ENOMEM;
1247 1.4.4.2 snj }
1248 1.4.4.2 snj error = sunxi_emac_setup_rxbuf(sc, i, m);
1249 1.4.4.2 snj if (error != 0) {
1250 1.4.4.2 snj device_printf(sc->dev, "cannot create RX buffer\n");
1251 1.4.4.2 snj return error;
1252 1.4.4.2 snj }
1253 1.4.4.2 snj }
1254 1.4.4.2 snj bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1255 1.4.4.2 snj 0, sc->rx.desc_map->dm_mapsize,
1256 1.4.4.2 snj BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1257 1.4.4.2 snj
1258 1.4.4.2 snj /* Write transmit and receive descriptor base address registers */
1259 1.4.4.2 snj WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1260 1.4.4.2 snj WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1261 1.4.4.2 snj
1262 1.4.4.2 snj return 0;
1263 1.4.4.2 snj }
1264 1.4.4.2 snj
1265 1.4.4.2 snj static int
1266 1.4.4.2 snj sunxi_emac_get_resources(struct sunxi_emac_softc *sc)
1267 1.4.4.2 snj {
1268 1.4.4.2 snj const int phandle = sc->phandle;
1269 1.4.4.2 snj bus_addr_t addr, size;
1270 1.4.4.2 snj u_int n;
1271 1.4.4.2 snj
1272 1.4.4.2 snj /* Map registers */
1273 1.4.4.2 snj for (n = 0; n < _RES_NITEMS; n++) {
1274 1.4.4.2 snj if (fdtbus_get_reg(phandle, n, &addr, &size) != 0)
1275 1.4.4.2 snj return ENXIO;
1276 1.4.4.2 snj if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh[n]) != 0)
1277 1.4.4.2 snj return ENXIO;
1278 1.4.4.2 snj }
1279 1.4.4.2 snj
1280 1.4.4.2 snj /* Get clocks and resets. "ahb" is required, "ephy" is optional. */
1281 1.4.4.2 snj
1282 1.4.4.2 snj if ((sc->clk_ahb = fdtbus_clock_get(phandle, "ahb")) == NULL)
1283 1.4.4.2 snj return ENXIO;
1284 1.4.4.2 snj sc->clk_ephy = fdtbus_clock_get(phandle, "ephy");
1285 1.4.4.2 snj
1286 1.4.4.2 snj if ((sc->rst_ahb = fdtbus_reset_get(phandle, "ahb")) == NULL)
1287 1.4.4.2 snj return ENXIO;
1288 1.4.4.3 snj sc->rst_ephy = fdtbus_reset_get(phandle, "ephy");
1289 1.4.4.2 snj
1290 1.4.4.2 snj /* Regulator is optional */
1291 1.4.4.2 snj sc->reg_phy = fdtbus_regulator_acquire(phandle, "phy-supply");
1292 1.4.4.2 snj
1293 1.4.4.2 snj /* Reset GPIO is optional */
1294 1.4.4.2 snj sc->pin_reset = fdtbus_gpio_acquire(sc->phandle,
1295 1.4.4.2 snj "allwinner,reset-gpio", GPIO_PIN_OUTPUT);
1296 1.4.4.2 snj
1297 1.4.4.2 snj return 0;
1298 1.4.4.2 snj }
1299 1.4.4.2 snj
1300 1.4.4.2 snj static int
1301 1.4.4.2 snj sunxi_emac_match(device_t parent, cfdata_t cf, void *aux)
1302 1.4.4.2 snj {
1303 1.4.4.2 snj struct fdt_attach_args * const faa = aux;
1304 1.4.4.2 snj
1305 1.4.4.2 snj return of_match_compat_data(faa->faa_phandle, compat_data);
1306 1.4.4.2 snj }
1307 1.4.4.2 snj
1308 1.4.4.2 snj static void
1309 1.4.4.2 snj sunxi_emac_attach(device_t parent, device_t self, void *aux)
1310 1.4.4.2 snj {
1311 1.4.4.2 snj struct fdt_attach_args * const faa = aux;
1312 1.4.4.2 snj struct sunxi_emac_softc * const sc = device_private(self);
1313 1.4.4.2 snj const int phandle = faa->faa_phandle;
1314 1.4.4.2 snj struct mii_data *mii = &sc->mii;
1315 1.4.4.2 snj struct ifnet *ifp = &sc->ec.ec_if;
1316 1.4.4.2 snj uint8_t eaddr[ETHER_ADDR_LEN];
1317 1.4.4.2 snj char intrstr[128];
1318 1.4.4.2 snj
1319 1.4.4.2 snj sc->dev = self;
1320 1.4.4.2 snj sc->phandle = phandle;
1321 1.4.4.2 snj sc->bst = faa->faa_bst;
1322 1.4.4.2 snj sc->dmat = faa->faa_dmat;
1323 1.4.4.2 snj sc->type = of_search_compatible(phandle, compat_data)->data;
1324 1.4.4.2 snj
1325 1.4.4.2 snj if (sunxi_emac_get_resources(sc) != 0) {
1326 1.4.4.2 snj aprint_error(": cannot allocate resources for device\n");
1327 1.4.4.2 snj return;
1328 1.4.4.2 snj }
1329 1.4.4.2 snj if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1330 1.4.4.2 snj aprint_error(": cannot decode interrupt\n");
1331 1.4.4.2 snj return;
1332 1.4.4.2 snj }
1333 1.4.4.2 snj
1334 1.4.4.2 snj mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NET);
1335 1.4.4.2 snj callout_init(&sc->stat_ch, CALLOUT_FLAGS);
1336 1.4.4.2 snj callout_setfunc(&sc->stat_ch, sunxi_emac_tick, sc);
1337 1.4.4.2 snj
1338 1.4.4.2 snj aprint_naive("\n");
1339 1.4.4.2 snj aprint_normal(": EMAC\n");
1340 1.4.4.2 snj
1341 1.4.4.2 snj /* Setup clocks and regulators */
1342 1.4.4.2 snj if (sunxi_emac_setup_resources(sc) != 0)
1343 1.4.4.2 snj return;
1344 1.4.4.2 snj
1345 1.4.4.2 snj /* Read MAC address before resetting the chip */
1346 1.4.4.2 snj sunxi_emac_get_eaddr(sc, eaddr);
1347 1.4.4.2 snj
1348 1.4.4.2 snj /* Soft reset EMAC core */
1349 1.4.4.2 snj if (sunxi_emac_reset(sc) != 0)
1350 1.4.4.2 snj return;
1351 1.4.4.2 snj
1352 1.4.4.2 snj /* Setup DMA descriptors */
1353 1.4.4.2 snj if (sunxi_emac_setup_dma(sc) != 0) {
1354 1.4.4.2 snj aprint_error_dev(self, "failed to setup DMA descriptors\n");
1355 1.4.4.2 snj return;
1356 1.4.4.2 snj }
1357 1.4.4.2 snj
1358 1.4.4.2 snj /* Install interrupt handler */
1359 1.4.4.2 snj sc->ih = fdtbus_intr_establish(phandle, 0, IPL_NET,
1360 1.4.4.2 snj FDT_INTR_FLAGS, sunxi_emac_intr, sc);
1361 1.4.4.2 snj if (sc->ih == NULL) {
1362 1.4.4.2 snj aprint_error_dev(self, "failed to establish interrupt on %s\n",
1363 1.4.4.2 snj intrstr);
1364 1.4.4.2 snj return;
1365 1.4.4.2 snj }
1366 1.4.4.2 snj aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1367 1.4.4.2 snj
1368 1.4.4.2 snj /* Setup ethernet interface */
1369 1.4.4.2 snj ifp->if_softc = sc;
1370 1.4.4.2 snj snprintf(ifp->if_xname, IFNAMSIZ, EMAC_IFNAME, device_unit(self));
1371 1.4.4.2 snj ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1372 1.4.4.2 snj #ifdef EMAC_MPSAFE
1373 1.4.4.4 snj ifp->if_extflags = IFEF_MPSAFE;
1374 1.4.4.2 snj #endif
1375 1.4.4.2 snj ifp->if_start = sunxi_emac_start;
1376 1.4.4.2 snj ifp->if_ioctl = sunxi_emac_ioctl;
1377 1.4.4.2 snj ifp->if_init = sunxi_emac_init;
1378 1.4.4.2 snj ifp->if_stop = sunxi_emac_stop;
1379 1.4.4.2 snj ifp->if_capabilities = IFCAP_CSUM_IPv4_Rx |
1380 1.4.4.2 snj IFCAP_CSUM_IPv4_Tx |
1381 1.4.4.2 snj IFCAP_CSUM_TCPv4_Rx |
1382 1.4.4.2 snj IFCAP_CSUM_TCPv4_Tx |
1383 1.4.4.2 snj IFCAP_CSUM_UDPv4_Rx |
1384 1.4.4.2 snj IFCAP_CSUM_UDPv4_Tx;
1385 1.4.4.2 snj IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1386 1.4.4.2 snj IFQ_SET_READY(&ifp->if_snd);
1387 1.4.4.2 snj
1388 1.4.4.2 snj /* 802.1Q VLAN-sized frames are supported */
1389 1.4.4.2 snj sc->ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1390 1.4.4.2 snj
1391 1.4.4.2 snj /* Attach MII driver */
1392 1.4.4.2 snj sc->ec.ec_mii = mii;
1393 1.4.4.2 snj ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1394 1.4.4.2 snj mii->mii_ifp = ifp;
1395 1.4.4.2 snj mii->mii_readreg = sunxi_emac_mii_readreg;
1396 1.4.4.2 snj mii->mii_writereg = sunxi_emac_mii_writereg;
1397 1.4.4.2 snj mii->mii_statchg = sunxi_emac_mii_statchg;
1398 1.4.4.2 snj mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
1399 1.4.4.2 snj MIIF_DOPAUSE);
1400 1.4.4.2 snj
1401 1.4.4.2 snj if (LIST_EMPTY(&mii->mii_phys)) {
1402 1.4.4.2 snj aprint_error_dev(self, "no PHY found!\n");
1403 1.4.4.2 snj return;
1404 1.4.4.2 snj }
1405 1.4.4.2 snj ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
1406 1.4.4.2 snj
1407 1.4.4.2 snj /* Attach interface */
1408 1.4.4.2 snj if_attach(ifp);
1409 1.4.4.2 snj if_deferred_start_init(ifp, NULL);
1410 1.4.4.2 snj
1411 1.4.4.2 snj /* Attach ethernet interface */
1412 1.4.4.2 snj ether_ifattach(ifp, eaddr);
1413 1.4.4.2 snj }
1414 1.4.4.2 snj
1415 1.4.4.2 snj CFATTACH_DECL_NEW(sunxi_emac, sizeof(struct sunxi_emac_softc),
1416 1.4.4.2 snj sunxi_emac_match, sunxi_emac_attach, NULL, NULL);
1417