blob: a5c4b4621d9b58b82ca52535e1a619cd81c90b6e [file] [log] [blame]
Thomas Choud3c08572010-04-15 22:32:38 +08001/*
2 * Opencore 10/100 ethernet mac driver
3 *
4 * Copyright (C) 2007-2008 Avionic Design Development GmbH
5 * Copyright (C) 2008-2009 Avionic Design GmbH
6 * Thierry Reding <thierry.reding@avionic-design.de>
7 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
Max Filippovc91eb0c2016-08-05 18:26:17 +03008 * Copyright (C) 2016 Cadence Design Systems Inc.
Thomas Choud3c08572010-04-15 22:32:38 +08009 *
Max Filippovc91eb0c2016-08-05 18:26:17 +030010 * SPDX-License-Identifier: GPL-2.0
Thomas Choud3c08572010-04-15 22:32:38 +080011 */
12
13#include <common.h>
Max Filippovc91eb0c2016-08-05 18:26:17 +030014#include <dm/device.h>
15#include <dm/platform_data/net_ethoc.h>
Max Filippovf9ffa422016-08-05 18:26:16 +030016#include <linux/io.h>
Thomas Choud3c08572010-04-15 22:32:38 +080017#include <malloc.h>
18#include <net.h>
19#include <miiphy.h>
Thomas Choud3c08572010-04-15 22:32:38 +080020#include <asm/cache.h>
21
22/* register offsets */
23#define MODER 0x00
24#define INT_SOURCE 0x04
25#define INT_MASK 0x08
26#define IPGT 0x0c
27#define IPGR1 0x10
28#define IPGR2 0x14
29#define PACKETLEN 0x18
30#define COLLCONF 0x1c
31#define TX_BD_NUM 0x20
32#define CTRLMODER 0x24
33#define MIIMODER 0x28
34#define MIICOMMAND 0x2c
35#define MIIADDRESS 0x30
36#define MIITX_DATA 0x34
37#define MIIRX_DATA 0x38
38#define MIISTATUS 0x3c
39#define MAC_ADDR0 0x40
40#define MAC_ADDR1 0x44
41#define ETH_HASH0 0x48
42#define ETH_HASH1 0x4c
43#define ETH_TXCTRL 0x50
44
45/* mode register */
46#define MODER_RXEN (1 << 0) /* receive enable */
47#define MODER_TXEN (1 << 1) /* transmit enable */
48#define MODER_NOPRE (1 << 2) /* no preamble */
49#define MODER_BRO (1 << 3) /* broadcast address */
50#define MODER_IAM (1 << 4) /* individual address mode */
51#define MODER_PRO (1 << 5) /* promiscuous mode */
52#define MODER_IFG (1 << 6) /* interframe gap for incoming frames */
53#define MODER_LOOP (1 << 7) /* loopback */
54#define MODER_NBO (1 << 8) /* no back-off */
55#define MODER_EDE (1 << 9) /* excess defer enable */
56#define MODER_FULLD (1 << 10) /* full duplex */
57#define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */
58#define MODER_DCRC (1 << 12) /* delayed CRC enable */
59#define MODER_CRC (1 << 13) /* CRC enable */
60#define MODER_HUGE (1 << 14) /* huge packets enable */
61#define MODER_PAD (1 << 15) /* padding enabled */
62#define MODER_RSM (1 << 16) /* receive small packets */
63
64/* interrupt source and mask registers */
65#define INT_MASK_TXF (1 << 0) /* transmit frame */
66#define INT_MASK_TXE (1 << 1) /* transmit error */
67#define INT_MASK_RXF (1 << 2) /* receive frame */
68#define INT_MASK_RXE (1 << 3) /* receive error */
69#define INT_MASK_BUSY (1 << 4)
70#define INT_MASK_TXC (1 << 5) /* transmit control frame */
71#define INT_MASK_RXC (1 << 6) /* receive control frame */
72
73#define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
74#define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
75
76#define INT_MASK_ALL ( \
77 INT_MASK_TXF | INT_MASK_TXE | \
78 INT_MASK_RXF | INT_MASK_RXE | \
79 INT_MASK_TXC | INT_MASK_RXC | \
80 INT_MASK_BUSY \
81 )
82
83/* packet length register */
84#define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
85#define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
86#define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
87 PACKETLEN_MAX(max))
88
89/* transmit buffer number register */
90#define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
91
92/* control module mode register */
93#define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */
94#define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */
95#define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */
96
97/* MII mode register */
98#define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */
99#define MIIMODER_NOPRE (1 << 8) /* no preamble */
100
101/* MII command register */
102#define MIICOMMAND_SCAN (1 << 0) /* scan status */
103#define MIICOMMAND_READ (1 << 1) /* read status */
104#define MIICOMMAND_WRITE (1 << 2) /* write control data */
105
106/* MII address register */
107#define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
108#define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
109#define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
110 MIIADDRESS_RGAD(reg))
111
112/* MII transmit data register */
113#define MIITX_DATA_VAL(x) ((x) & 0xffff)
114
115/* MII receive data register */
116#define MIIRX_DATA_VAL(x) ((x) & 0xffff)
117
118/* MII status register */
119#define MIISTATUS_LINKFAIL (1 << 0)
120#define MIISTATUS_BUSY (1 << 1)
121#define MIISTATUS_INVALID (1 << 2)
122
123/* TX buffer descriptor */
124#define TX_BD_CS (1 << 0) /* carrier sense lost */
125#define TX_BD_DF (1 << 1) /* defer indication */
126#define TX_BD_LC (1 << 2) /* late collision */
127#define TX_BD_RL (1 << 3) /* retransmission limit */
128#define TX_BD_RETRY_MASK (0x00f0)
129#define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
130#define TX_BD_UR (1 << 8) /* transmitter underrun */
131#define TX_BD_CRC (1 << 11) /* TX CRC enable */
132#define TX_BD_PAD (1 << 12) /* pad enable */
133#define TX_BD_WRAP (1 << 13)
134#define TX_BD_IRQ (1 << 14) /* interrupt request enable */
135#define TX_BD_READY (1 << 15) /* TX buffer ready */
136#define TX_BD_LEN(x) (((x) & 0xffff) << 16)
137#define TX_BD_LEN_MASK (0xffff << 16)
138
139#define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
140 TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
141
142/* RX buffer descriptor */
143#define RX_BD_LC (1 << 0) /* late collision */
144#define RX_BD_CRC (1 << 1) /* RX CRC error */
145#define RX_BD_SF (1 << 2) /* short frame */
146#define RX_BD_TL (1 << 3) /* too long */
147#define RX_BD_DN (1 << 4) /* dribble nibble */
148#define RX_BD_IS (1 << 5) /* invalid symbol */
149#define RX_BD_OR (1 << 6) /* receiver overrun */
150#define RX_BD_MISS (1 << 7)
151#define RX_BD_CF (1 << 8) /* control frame */
152#define RX_BD_WRAP (1 << 13)
153#define RX_BD_IRQ (1 << 14) /* interrupt request enable */
154#define RX_BD_EMPTY (1 << 15)
155#define RX_BD_LEN(x) (((x) & 0xffff) << 16)
156
157#define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
158 RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
159
160#define ETHOC_BUFSIZ 1536
161#define ETHOC_ZLEN 64
162#define ETHOC_BD_BASE 0x400
163#define ETHOC_TIMEOUT (HZ / 2)
164#define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
Max Filippovf9ffa422016-08-05 18:26:16 +0300165#define ETHOC_IOSIZE 0x54
Thomas Choud3c08572010-04-15 22:32:38 +0800166
167/**
168 * struct ethoc - driver-private device structure
169 * @num_tx: number of send buffers
170 * @cur_tx: last send buffer written
171 * @dty_tx: last buffer actually sent
172 * @num_rx: number of receive buffers
173 * @cur_rx: current receive buffer
174 */
175struct ethoc {
176 u32 num_tx;
177 u32 cur_tx;
178 u32 dty_tx;
179 u32 num_rx;
180 u32 cur_rx;
Max Filippovf9ffa422016-08-05 18:26:16 +0300181 void __iomem *iobase;
Thomas Choud3c08572010-04-15 22:32:38 +0800182};
183
184/**
185 * struct ethoc_bd - buffer descriptor
186 * @stat: buffer statistics
187 * @addr: physical memory address
188 */
189struct ethoc_bd {
190 u32 stat;
191 u32 addr;
192};
193
Max Filippovf9ffa422016-08-05 18:26:16 +0300194static inline u32 ethoc_read(struct ethoc *priv, size_t offset)
Thomas Choud3c08572010-04-15 22:32:38 +0800195{
Max Filippovf9ffa422016-08-05 18:26:16 +0300196 return readl(priv->iobase + offset);
Thomas Choud3c08572010-04-15 22:32:38 +0800197}
198
Max Filippovf9ffa422016-08-05 18:26:16 +0300199static inline void ethoc_write(struct ethoc *priv, size_t offset, u32 data)
Thomas Choud3c08572010-04-15 22:32:38 +0800200{
Max Filippovf9ffa422016-08-05 18:26:16 +0300201 writel(data, priv->iobase + offset);
Thomas Choud3c08572010-04-15 22:32:38 +0800202}
203
Max Filippovf9ffa422016-08-05 18:26:16 +0300204static inline void ethoc_read_bd(struct ethoc *priv, int index,
Thomas Choud3c08572010-04-15 22:32:38 +0800205 struct ethoc_bd *bd)
206{
Vasili Galkaaa0caa22014-08-26 13:46:17 +0300207 size_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
Max Filippovf9ffa422016-08-05 18:26:16 +0300208 bd->stat = ethoc_read(priv, offset + 0);
209 bd->addr = ethoc_read(priv, offset + 4);
Thomas Choud3c08572010-04-15 22:32:38 +0800210}
211
Max Filippovf9ffa422016-08-05 18:26:16 +0300212static inline void ethoc_write_bd(struct ethoc *priv, int index,
Thomas Choud3c08572010-04-15 22:32:38 +0800213 const struct ethoc_bd *bd)
214{
Vasili Galkaaa0caa22014-08-26 13:46:17 +0300215 size_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
Max Filippovf9ffa422016-08-05 18:26:16 +0300216 ethoc_write(priv, offset + 0, bd->stat);
217 ethoc_write(priv, offset + 4, bd->addr);
Thomas Choud3c08572010-04-15 22:32:38 +0800218}
219
Max Filippovc91eb0c2016-08-05 18:26:17 +0300220static int ethoc_write_hwaddr_common(struct ethoc *priv, u8 *mac)
Thomas Choud3c08572010-04-15 22:32:38 +0800221{
Max Filippovf9ffa422016-08-05 18:26:16 +0300222 ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
Thomas Choud3c08572010-04-15 22:32:38 +0800223 (mac[4] << 8) | (mac[5] << 0));
Max Filippovf9ffa422016-08-05 18:26:16 +0300224 ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
Thomas Chou8d01f222010-04-27 20:20:27 +0800225 return 0;
Thomas Choud3c08572010-04-15 22:32:38 +0800226}
227
Max Filippovf9ffa422016-08-05 18:26:16 +0300228static inline void ethoc_ack_irq(struct ethoc *priv, u32 mask)
Thomas Choud3c08572010-04-15 22:32:38 +0800229{
Max Filippovf9ffa422016-08-05 18:26:16 +0300230 ethoc_write(priv, INT_SOURCE, mask);
Thomas Choud3c08572010-04-15 22:32:38 +0800231}
232
Max Filippovf9ffa422016-08-05 18:26:16 +0300233static inline void ethoc_enable_rx_and_tx(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800234{
Max Filippovf9ffa422016-08-05 18:26:16 +0300235 u32 mode = ethoc_read(priv, MODER);
Thomas Choud3c08572010-04-15 22:32:38 +0800236 mode |= MODER_RXEN | MODER_TXEN;
Max Filippovf9ffa422016-08-05 18:26:16 +0300237 ethoc_write(priv, MODER, mode);
Thomas Choud3c08572010-04-15 22:32:38 +0800238}
239
Max Filippovf9ffa422016-08-05 18:26:16 +0300240static inline void ethoc_disable_rx_and_tx(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800241{
Max Filippovf9ffa422016-08-05 18:26:16 +0300242 u32 mode = ethoc_read(priv, MODER);
Thomas Choud3c08572010-04-15 22:32:38 +0800243 mode &= ~(MODER_RXEN | MODER_TXEN);
Max Filippovf9ffa422016-08-05 18:26:16 +0300244 ethoc_write(priv, MODER, mode);
Thomas Choud3c08572010-04-15 22:32:38 +0800245}
246
Max Filippovf9ffa422016-08-05 18:26:16 +0300247static int ethoc_init_ring(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800248{
Thomas Choud3c08572010-04-15 22:32:38 +0800249 struct ethoc_bd bd;
250 int i;
251
252 priv->cur_tx = 0;
253 priv->dty_tx = 0;
254 priv->cur_rx = 0;
255
256 /* setup transmission buffers */
257 bd.stat = TX_BD_IRQ | TX_BD_CRC;
258
259 for (i = 0; i < priv->num_tx; i++) {
260 if (i == priv->num_tx - 1)
261 bd.stat |= TX_BD_WRAP;
262
Max Filippovf9ffa422016-08-05 18:26:16 +0300263 ethoc_write_bd(priv, i, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800264 }
265
266 bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
267
268 for (i = 0; i < priv->num_rx; i++) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500269 bd.addr = (u32)net_rx_packets[i];
Thomas Choud3c08572010-04-15 22:32:38 +0800270 if (i == priv->num_rx - 1)
271 bd.stat |= RX_BD_WRAP;
272
Stefan Kristiansson75242582011-11-04 02:38:06 +0000273 flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN);
Max Filippovf9ffa422016-08-05 18:26:16 +0300274 ethoc_write_bd(priv, priv->num_tx + i, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800275 }
276
277 return 0;
278}
279
Max Filippovf9ffa422016-08-05 18:26:16 +0300280static int ethoc_reset(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800281{
282 u32 mode;
283
284 /* TODO: reset controller? */
285
Max Filippovf9ffa422016-08-05 18:26:16 +0300286 ethoc_disable_rx_and_tx(priv);
Thomas Choud3c08572010-04-15 22:32:38 +0800287
288 /* TODO: setup registers */
289
290 /* enable FCS generation and automatic padding */
Max Filippovf9ffa422016-08-05 18:26:16 +0300291 mode = ethoc_read(priv, MODER);
Thomas Choud3c08572010-04-15 22:32:38 +0800292 mode |= MODER_CRC | MODER_PAD;
Max Filippovf9ffa422016-08-05 18:26:16 +0300293 ethoc_write(priv, MODER, mode);
Thomas Choud3c08572010-04-15 22:32:38 +0800294
295 /* set full-duplex mode */
Max Filippovf9ffa422016-08-05 18:26:16 +0300296 mode = ethoc_read(priv, MODER);
Thomas Choud3c08572010-04-15 22:32:38 +0800297 mode |= MODER_FULLD;
Max Filippovf9ffa422016-08-05 18:26:16 +0300298 ethoc_write(priv, MODER, mode);
299 ethoc_write(priv, IPGT, 0x15);
Thomas Choud3c08572010-04-15 22:32:38 +0800300
Max Filippovf9ffa422016-08-05 18:26:16 +0300301 ethoc_ack_irq(priv, INT_MASK_ALL);
302 ethoc_enable_rx_and_tx(priv);
Thomas Choud3c08572010-04-15 22:32:38 +0800303 return 0;
304}
305
Max Filippovc91eb0c2016-08-05 18:26:17 +0300306static int ethoc_init_common(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800307{
Thomas Choud3c08572010-04-15 22:32:38 +0800308 priv->num_tx = 1;
309 priv->num_rx = PKTBUFSRX;
Max Filippovf9ffa422016-08-05 18:26:16 +0300310 ethoc_write(priv, TX_BD_NUM, priv->num_tx);
311 ethoc_init_ring(priv);
312 ethoc_reset(priv);
Thomas Choud3c08572010-04-15 22:32:38 +0800313
314 return 0;
315}
316
317static int ethoc_update_rx_stats(struct ethoc_bd *bd)
318{
319 int ret = 0;
320
321 if (bd->stat & RX_BD_TL) {
322 debug("ETHOC: " "RX: frame too long\n");
323 ret++;
324 }
325
326 if (bd->stat & RX_BD_SF) {
327 debug("ETHOC: " "RX: frame too short\n");
328 ret++;
329 }
330
331 if (bd->stat & RX_BD_DN)
332 debug("ETHOC: " "RX: dribble nibble\n");
333
334 if (bd->stat & RX_BD_CRC) {
335 debug("ETHOC: " "RX: wrong CRC\n");
336 ret++;
337 }
338
339 if (bd->stat & RX_BD_OR) {
340 debug("ETHOC: " "RX: overrun\n");
341 ret++;
342 }
343
344 if (bd->stat & RX_BD_LC) {
345 debug("ETHOC: " "RX: late collision\n");
346 ret++;
347 }
348
349 return ret;
350}
351
Max Filippovc91eb0c2016-08-05 18:26:17 +0300352static int ethoc_rx_common(struct ethoc *priv, uchar **packetp)
Thomas Choud3c08572010-04-15 22:32:38 +0800353{
Max Filippovc91eb0c2016-08-05 18:26:17 +0300354 u32 entry;
355 struct ethoc_bd bd;
Thomas Choud3c08572010-04-15 22:32:38 +0800356
Max Filippovc91eb0c2016-08-05 18:26:17 +0300357 entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
358 ethoc_read_bd(priv, entry, &bd);
359 if (bd.stat & RX_BD_EMPTY)
360 return -EAGAIN;
Thomas Choud3c08572010-04-15 22:32:38 +0800361
Max Filippovc91eb0c2016-08-05 18:26:17 +0300362 debug("%s(): RX buffer %d, %x received\n",
363 __func__, priv->cur_rx, bd.stat);
364 if (ethoc_update_rx_stats(&bd) == 0) {
365 int size = bd.stat >> 16;
Thomas Choud3c08572010-04-15 22:32:38 +0800366
Max Filippovc91eb0c2016-08-05 18:26:17 +0300367 size -= 4; /* strip the CRC */
368 *packetp = (void *)bd.addr;
369 return size;
370 } else {
371 return 0;
372 }
373}
Thomas Choud3c08572010-04-15 22:32:38 +0800374
Max Filippovc91eb0c2016-08-05 18:26:17 +0300375static int ethoc_is_new_packet_received(struct ethoc *priv)
376{
377 u32 pending;
378
379 pending = ethoc_read(priv, INT_SOURCE);
380 ethoc_ack_irq(priv, pending);
381 if (pending & INT_MASK_BUSY)
382 debug("%s(): packet dropped\n", __func__);
383 if (pending & INT_MASK_RX) {
384 debug("%s(): rx irq\n", __func__);
385 return 1;
Thomas Choud3c08572010-04-15 22:32:38 +0800386 }
387
Max Filippovc91eb0c2016-08-05 18:26:17 +0300388 return 0;
Thomas Choud3c08572010-04-15 22:32:38 +0800389}
390
391static int ethoc_update_tx_stats(struct ethoc_bd *bd)
392{
393 if (bd->stat & TX_BD_LC)
394 debug("ETHOC: " "TX: late collision\n");
395
396 if (bd->stat & TX_BD_RL)
397 debug("ETHOC: " "TX: retransmit limit\n");
398
399 if (bd->stat & TX_BD_UR)
400 debug("ETHOC: " "TX: underrun\n");
401
402 if (bd->stat & TX_BD_CS)
403 debug("ETHOC: " "TX: carrier sense lost\n");
404
405 return 0;
406}
407
Max Filippovf9ffa422016-08-05 18:26:16 +0300408static void ethoc_tx(struct ethoc *priv)
Thomas Choud3c08572010-04-15 22:32:38 +0800409{
Thomas Choud3c08572010-04-15 22:32:38 +0800410 u32 entry = priv->dty_tx % priv->num_tx;
411 struct ethoc_bd bd;
412
Max Filippovf9ffa422016-08-05 18:26:16 +0300413 ethoc_read_bd(priv, entry, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800414 if ((bd.stat & TX_BD_READY) == 0)
415 (void)ethoc_update_tx_stats(&bd);
416}
417
Max Filippovc91eb0c2016-08-05 18:26:17 +0300418static int ethoc_send_common(struct ethoc *priv, void *packet, int length)
Thomas Choud3c08572010-04-15 22:32:38 +0800419{
Thomas Choud3c08572010-04-15 22:32:38 +0800420 struct ethoc_bd bd;
421 u32 entry;
422 u32 pending;
423 int tmo;
424
425 entry = priv->cur_tx % priv->num_tx;
Max Filippovf9ffa422016-08-05 18:26:16 +0300426 ethoc_read_bd(priv, entry, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800427 if (unlikely(length < ETHOC_ZLEN))
428 bd.stat |= TX_BD_PAD;
429 else
430 bd.stat &= ~TX_BD_PAD;
431 bd.addr = (u32)packet;
432
Stefan Kristiansson75242582011-11-04 02:38:06 +0000433 flush_dcache_range(bd.addr, bd.addr + length);
Thomas Choud3c08572010-04-15 22:32:38 +0800434 bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
435 bd.stat |= TX_BD_LEN(length);
Max Filippovf9ffa422016-08-05 18:26:16 +0300436 ethoc_write_bd(priv, entry, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800437
438 /* start transmit */
439 bd.stat |= TX_BD_READY;
Max Filippovf9ffa422016-08-05 18:26:16 +0300440 ethoc_write_bd(priv, entry, &bd);
Thomas Choud3c08572010-04-15 22:32:38 +0800441
442 /* wait for transfer to succeed */
443 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
444 while (1) {
Max Filippovf9ffa422016-08-05 18:26:16 +0300445 pending = ethoc_read(priv, INT_SOURCE);
446 ethoc_ack_irq(priv, pending & ~INT_MASK_RX);
Thomas Choud3c08572010-04-15 22:32:38 +0800447 if (pending & INT_MASK_BUSY)
448 debug("%s(): packet dropped\n", __func__);
449
450 if (pending & INT_MASK_TX) {
Max Filippovf9ffa422016-08-05 18:26:16 +0300451 ethoc_tx(priv);
Thomas Choud3c08572010-04-15 22:32:38 +0800452 break;
453 }
454 if (get_timer(0) >= tmo) {
455 debug("%s(): timed out\n", __func__);
456 return -1;
457 }
458 }
459
460 debug("%s(): packet sent\n", __func__);
461 return 0;
462}
463
Max Filippovc91eb0c2016-08-05 18:26:17 +0300464static int ethoc_free_pkt_common(struct ethoc *priv)
465{
466 u32 entry;
467 struct ethoc_bd bd;
468
469 entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
470 ethoc_read_bd(priv, entry, &bd);
471
472 /* clear the buffer descriptor so it can be reused */
473 flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN);
474 bd.stat &= ~RX_BD_STATS;
475 bd.stat |= RX_BD_EMPTY;
476 ethoc_write_bd(priv, entry, &bd);
477 priv->cur_rx++;
478
479 return 0;
480}
481
482#ifdef CONFIG_DM_ETH
483
484static int ethoc_write_hwaddr(struct udevice *dev)
485{
486 struct ethoc_eth_pdata *pdata = dev_get_platdata(dev);
487 struct ethoc *priv = dev_get_priv(dev);
488 u8 *mac = pdata->eth_pdata.enetaddr;
489
490 return ethoc_write_hwaddr_common(priv, mac);
491}
492
493static int ethoc_send(struct udevice *dev, void *packet, int length)
494{
495 return ethoc_send_common(dev_get_priv(dev), packet, length);
496}
497
498static int ethoc_free_pkt(struct udevice *dev, uchar *packet, int length)
499{
500 return ethoc_free_pkt_common(dev_get_priv(dev));
501}
502
503static int ethoc_recv(struct udevice *dev, int flags, uchar **packetp)
504{
505 struct ethoc *priv = dev_get_priv(dev);
506
507 if (flags & ETH_RECV_CHECK_DEVICE)
508 if (!ethoc_is_new_packet_received(priv))
509 return -EAGAIN;
510
511 return ethoc_rx_common(priv, packetp);
512}
513
514static int ethoc_start(struct udevice *dev)
515{
516 return ethoc_init_common(dev_get_priv(dev));
517}
518
519static void ethoc_stop(struct udevice *dev)
520{
521 struct ethoc *priv = dev_get_priv(dev);
522
523 ethoc_disable_rx_and_tx(priv);
524}
525
Max Filippova6429a92016-08-05 18:26:18 +0300526static int ethoc_ofdata_to_platdata(struct udevice *dev)
527{
528 struct ethoc_eth_pdata *pdata = dev_get_platdata(dev);
529
530 pdata->eth_pdata.iobase = dev_get_addr(dev);
531 return 0;
532}
533
Max Filippovc91eb0c2016-08-05 18:26:17 +0300534static int ethoc_probe(struct udevice *dev)
535{
536 struct ethoc_eth_pdata *pdata = dev_get_platdata(dev);
537 struct ethoc *priv = dev_get_priv(dev);
538
539 priv->iobase = ioremap(pdata->eth_pdata.iobase, ETHOC_IOSIZE);
540 return 0;
541}
542
543static int ethoc_remove(struct udevice *dev)
544{
545 struct ethoc *priv = dev_get_priv(dev);
546
547 iounmap(priv->iobase);
548 return 0;
549}
550
551static const struct eth_ops ethoc_ops = {
552 .start = ethoc_start,
553 .stop = ethoc_stop,
554 .send = ethoc_send,
555 .recv = ethoc_recv,
556 .free_pkt = ethoc_free_pkt,
557 .write_hwaddr = ethoc_write_hwaddr,
558};
559
Max Filippova6429a92016-08-05 18:26:18 +0300560static const struct udevice_id ethoc_ids[] = {
561 { .compatible = "opencores,ethoc" },
562 { }
563};
564
Max Filippovc91eb0c2016-08-05 18:26:17 +0300565U_BOOT_DRIVER(ethoc) = {
566 .name = "ethoc",
567 .id = UCLASS_ETH,
Max Filippova6429a92016-08-05 18:26:18 +0300568 .of_match = ethoc_ids,
569 .ofdata_to_platdata = ethoc_ofdata_to_platdata,
Max Filippovc91eb0c2016-08-05 18:26:17 +0300570 .probe = ethoc_probe,
571 .remove = ethoc_remove,
572 .ops = &ethoc_ops,
573 .priv_auto_alloc_size = sizeof(struct ethoc),
574 .platdata_auto_alloc_size = sizeof(struct ethoc_eth_pdata),
575};
576
577#else
578
579static int ethoc_init(struct eth_device *dev, bd_t *bd)
580{
581 struct ethoc *priv = (struct ethoc *)dev->priv;
582
583 return ethoc_init_common(priv);
584}
585
586static int ethoc_write_hwaddr(struct eth_device *dev)
587{
588 struct ethoc *priv = (struct ethoc *)dev->priv;
589 u8 *mac = dev->enetaddr;
590
591 return ethoc_write_hwaddr_common(priv, mac);
592}
593
594static int ethoc_send(struct eth_device *dev, void *packet, int length)
595{
596 return ethoc_send_common(dev->priv, packet, length);
597}
598
Thomas Choud3c08572010-04-15 22:32:38 +0800599static void ethoc_halt(struct eth_device *dev)
600{
Max Filippovf9ffa422016-08-05 18:26:16 +0300601 ethoc_disable_rx_and_tx(dev->priv);
Thomas Choud3c08572010-04-15 22:32:38 +0800602}
603
604static int ethoc_recv(struct eth_device *dev)
605{
Max Filippovf9ffa422016-08-05 18:26:16 +0300606 struct ethoc *priv = (struct ethoc *)dev->priv;
Max Filippovc91eb0c2016-08-05 18:26:17 +0300607 int count;
Thomas Choud3c08572010-04-15 22:32:38 +0800608
Max Filippovc91eb0c2016-08-05 18:26:17 +0300609 if (!ethoc_is_new_packet_received(priv))
610 return 0;
611
612 for (count = 0; count < PKTBUFSRX; ++count) {
613 uchar *packetp;
614 int size = ethoc_rx_common(priv, &packetp);
Thomas Choud3c08572010-04-15 22:32:38 +0800615
Max Filippovc91eb0c2016-08-05 18:26:17 +0300616 if (size < 0)
617 break;
618 if (size > 0)
619 net_process_received_packet(packetp, size);
620 ethoc_free_pkt_common(priv);
621 }
Thomas Choud3c08572010-04-15 22:32:38 +0800622 return 0;
623}
624
625int ethoc_initialize(u8 dev_num, int base_addr)
626{
627 struct ethoc *priv;
628 struct eth_device *dev;
629
630 priv = malloc(sizeof(*priv));
631 if (!priv)
632 return 0;
633 dev = malloc(sizeof(*dev));
634 if (!dev) {
635 free(priv);
636 return 0;
637 }
638
639 memset(dev, 0, sizeof(*dev));
640 dev->priv = priv;
641 dev->iobase = base_addr;
642 dev->init = ethoc_init;
643 dev->halt = ethoc_halt;
644 dev->send = ethoc_send;
645 dev->recv = ethoc_recv;
Max Filippovc91eb0c2016-08-05 18:26:17 +0300646 dev->write_hwaddr = ethoc_write_hwaddr;
Thomas Choud3c08572010-04-15 22:32:38 +0800647 sprintf(dev->name, "%s-%hu", "ETHOC", dev_num);
Max Filippovf9ffa422016-08-05 18:26:16 +0300648 priv->iobase = ioremap(dev->iobase, ETHOC_IOSIZE);
Thomas Choud3c08572010-04-15 22:32:38 +0800649
650 eth_register(dev);
651 return 1;
652}
Max Filippovc91eb0c2016-08-05 18:26:17 +0300653
654#endif