blob: 98d686ed69a9ac299ddcb9ae77dfa4eaf54703df [file] [log] [blame]
Florian Fainellief112912008-03-19 17:14:51 +01001/*
2 * Driver for the IDT RC32434 (Korina) on-chip ethernet controller.
3 *
4 * Copyright 2004 IDT Inc. (rischelp@idt.com)
5 * Copyright 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright 2008 Florian Fainelli <florian@openwrt.org>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * Writing to a DMA status register:
29 *
30 * When writing to the status register, you should mask the bit you have
31 * been testing the status register with. Both Tx and Rx DMA registers
32 * should stick to this procedure.
33 */
34
35#include <linux/module.h>
36#include <linux/kernel.h>
37#include <linux/moduleparam.h>
38#include <linux/sched.h>
39#include <linux/ctype.h>
40#include <linux/types.h>
41#include <linux/interrupt.h>
Florian Fainellief112912008-03-19 17:14:51 +010042#include <linux/ioport.h>
43#include <linux/in.h>
44#include <linux/slab.h>
45#include <linux/string.h>
46#include <linux/delay.h>
47#include <linux/netdevice.h>
48#include <linux/etherdevice.h>
49#include <linux/skbuff.h>
50#include <linux/errno.h>
51#include <linux/platform_device.h>
52#include <linux/mii.h>
53#include <linux/ethtool.h>
54#include <linux/crc32.h>
55
56#include <asm/bootinfo.h>
Florian Fainellief112912008-03-19 17:14:51 +010057#include <asm/bitops.h>
58#include <asm/pgtable.h>
Florian Fainellief112912008-03-19 17:14:51 +010059#include <asm/io.h>
60#include <asm/dma.h>
61
62#include <asm/mach-rc32434/rb.h>
63#include <asm/mach-rc32434/rc32434.h>
64#include <asm/mach-rc32434/eth.h>
65#include <asm/mach-rc32434/dma_v.h>
66
67#define DRV_NAME "korina"
68#define DRV_VERSION "0.10"
69#define DRV_RELDATE "04Mar2008"
70
71#define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \
72 ((dev)->dev_addr[1]))
73#define STATION_ADDRESS_LOW(dev) (((dev)->dev_addr[2] << 24) | \
74 ((dev)->dev_addr[3] << 16) | \
75 ((dev)->dev_addr[4] << 8) | \
76 ((dev)->dev_addr[5]))
77
78#define MII_CLOCK 1250000 /* no more than 2.5MHz */
79
80/* the following must be powers of two */
81#define KORINA_NUM_RDS 64 /* number of receive descriptors */
82#define KORINA_NUM_TDS 64 /* number of transmit descriptors */
83
Phil Suttera13b2782009-01-14 21:47:50 -080084/* KORINA_RBSIZE is the hardware's default maximum receive
85 * frame size in bytes. Having this hardcoded means that there
86 * is no support for MTU sizes greater than 1500. */
87#define KORINA_RBSIZE 1536 /* size of one resource buffer = Ether MTU */
Florian Fainellief112912008-03-19 17:14:51 +010088#define KORINA_RDS_MASK (KORINA_NUM_RDS - 1)
89#define KORINA_TDS_MASK (KORINA_NUM_TDS - 1)
90#define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc))
91#define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc))
92
93#define TX_TIMEOUT (6000 * HZ / 1000)
94
95enum chain_status { desc_filled, desc_empty };
96#define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0)
97#define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0)
98#define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
99
100/* Information that need to be kept for each board. */
101struct korina_private {
102 struct eth_regs *eth_regs;
103 struct dma_reg *rx_dma_regs;
104 struct dma_reg *tx_dma_regs;
105 struct dma_desc *td_ring; /* transmit descriptor ring */
106 struct dma_desc *rd_ring; /* receive descriptor ring */
107
108 struct sk_buff *tx_skb[KORINA_NUM_TDS];
109 struct sk_buff *rx_skb[KORINA_NUM_RDS];
110
111 int rx_next_done;
112 int rx_chain_head;
113 int rx_chain_tail;
114 enum chain_status rx_chain_status;
115
116 int tx_next_done;
117 int tx_chain_head;
118 int tx_chain_tail;
119 enum chain_status tx_chain_status;
120 int tx_count;
121 int tx_full;
122
123 int rx_irq;
124 int tx_irq;
Florian Fainellief112912008-03-19 17:14:51 +0100125
126 spinlock_t lock; /* NIC xmit lock */
127
128 int dma_halt_cnt;
129 int dma_run_cnt;
130 struct napi_struct napi;
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000131 struct timer_list media_check_timer;
Florian Fainellief112912008-03-19 17:14:51 +0100132 struct mii_if_info mii_if;
Phil Sutterceb3d232010-05-29 13:23:34 +0000133 struct work_struct restart_task;
Florian Fainellief112912008-03-19 17:14:51 +0100134 struct net_device *dev;
135 int phy_addr;
136};
137
138extern unsigned int idt_cpu_freq;
139
140static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr)
141{
142 writel(0, &ch->dmandptr);
143 writel(dma_addr, &ch->dmadptr);
144}
145
146static inline void korina_abort_dma(struct net_device *dev,
147 struct dma_reg *ch)
148{
149 if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
150 writel(0x10, &ch->dmac);
151
152 while (!(readl(&ch->dmas) & DMA_STAT_HALT))
Florian Westphal860e9532016-05-03 16:33:13 +0200153 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100154
155 writel(0, &ch->dmas);
156 }
157
158 writel(0, &ch->dmadptr);
159 writel(0, &ch->dmandptr);
160}
161
162static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr)
163{
164 writel(dma_addr, &ch->dmandptr);
165}
166
167static void korina_abort_tx(struct net_device *dev)
168{
169 struct korina_private *lp = netdev_priv(dev);
170
171 korina_abort_dma(dev, lp->tx_dma_regs);
172}
173
174static void korina_abort_rx(struct net_device *dev)
175{
176 struct korina_private *lp = netdev_priv(dev);
177
178 korina_abort_dma(dev, lp->rx_dma_regs);
179}
180
181static void korina_start_rx(struct korina_private *lp,
182 struct dma_desc *rd)
183{
184 korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd));
185}
186
187static void korina_chain_rx(struct korina_private *lp,
188 struct dma_desc *rd)
189{
190 korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd));
191}
192
193/* transmit packet */
194static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
195{
196 struct korina_private *lp = netdev_priv(dev);
197 unsigned long flags;
198 u32 length;
Phil Sutter97bc4772009-01-14 21:50:41 -0800199 u32 chain_prev, chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100200 struct dma_desc *td;
201
202 spin_lock_irqsave(&lp->lock, flags);
203
204 td = &lp->td_ring[lp->tx_chain_tail];
205
206 /* stop queue when full, drop pkts if queue already full */
207 if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
208 lp->tx_full = 1;
209
210 if (lp->tx_count == (KORINA_NUM_TDS - 2))
211 netif_stop_queue(dev);
212 else {
213 dev->stats.tx_dropped++;
214 dev_kfree_skb_any(skb);
215 spin_unlock_irqrestore(&lp->lock, flags);
216
217 return NETDEV_TX_BUSY;
218 }
219 }
220
221 lp->tx_count++;
222
223 lp->tx_skb[lp->tx_chain_tail] = skb;
224
225 length = skb->len;
226 dma_cache_wback((u32)skb->data, skb->len);
227
228 /* Setup the transmit descriptor. */
229 dma_cache_inv((u32) td, sizeof(*td));
230 td->ca = CPHYSADDR(skb->data);
Phil Sutter97bc4772009-01-14 21:50:41 -0800231 chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK;
232 chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK;
Florian Fainellief112912008-03-19 17:14:51 +0100233
234 if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
235 if (lp->tx_chain_status == desc_empty) {
236 /* Update tail */
237 td->control = DMA_COUNT(length) |
238 DMA_DESC_COF | DMA_DESC_IOF;
239 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800240 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100241 /* Write to NDPTR */
242 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
243 &lp->tx_dma_regs->dmandptr);
244 /* Move head to tail */
245 lp->tx_chain_head = lp->tx_chain_tail;
246 } else {
247 /* Update tail */
248 td->control = DMA_COUNT(length) |
249 DMA_DESC_COF | DMA_DESC_IOF;
250 /* Link to prev */
Phil Sutter97bc4772009-01-14 21:50:41 -0800251 lp->td_ring[chain_prev].control &=
Florian Fainellief112912008-03-19 17:14:51 +0100252 ~DMA_DESC_COF;
253 /* Link to prev */
Phil Sutter97bc4772009-01-14 21:50:41 -0800254 lp->td_ring[chain_prev].link = CPHYSADDR(td);
Florian Fainellief112912008-03-19 17:14:51 +0100255 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800256 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100257 /* Write to NDPTR */
258 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
259 &(lp->tx_dma_regs->dmandptr));
260 /* Move head to tail */
261 lp->tx_chain_head = lp->tx_chain_tail;
262 lp->tx_chain_status = desc_empty;
263 }
264 } else {
265 if (lp->tx_chain_status == desc_empty) {
266 /* Update tail */
267 td->control = DMA_COUNT(length) |
268 DMA_DESC_COF | DMA_DESC_IOF;
269 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800270 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100271 lp->tx_chain_status = desc_filled;
Florian Fainellief112912008-03-19 17:14:51 +0100272 } else {
273 /* Update tail */
274 td->control = DMA_COUNT(length) |
275 DMA_DESC_COF | DMA_DESC_IOF;
Phil Sutter97bc4772009-01-14 21:50:41 -0800276 lp->td_ring[chain_prev].control &=
Florian Fainellief112912008-03-19 17:14:51 +0100277 ~DMA_DESC_COF;
Phil Sutter97bc4772009-01-14 21:50:41 -0800278 lp->td_ring[chain_prev].link = CPHYSADDR(td);
279 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100280 }
281 }
282 dma_cache_wback((u32) td, sizeof(*td));
283
Florian Westphal860e9532016-05-03 16:33:13 +0200284 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100285 spin_unlock_irqrestore(&lp->lock, flags);
286
287 return NETDEV_TX_OK;
288}
289
290static int mdio_read(struct net_device *dev, int mii_id, int reg)
291{
292 struct korina_private *lp = netdev_priv(dev);
293 int ret;
294
295 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
296
297 writel(0, &lp->eth_regs->miimcfg);
298 writel(0, &lp->eth_regs->miimcmd);
299 writel(mii_id | reg, &lp->eth_regs->miimaddr);
300 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
301
302 ret = (int)(readl(&lp->eth_regs->miimrdd));
303 return ret;
304}
305
306static void mdio_write(struct net_device *dev, int mii_id, int reg, int val)
307{
308 struct korina_private *lp = netdev_priv(dev);
309
310 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
311
312 writel(0, &lp->eth_regs->miimcfg);
313 writel(1, &lp->eth_regs->miimcmd);
314 writel(mii_id | reg, &lp->eth_regs->miimaddr);
315 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
316 writel(val, &lp->eth_regs->miimwtd);
317}
318
319/* Ethernet Rx DMA interrupt */
320static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
321{
322 struct net_device *dev = dev_id;
323 struct korina_private *lp = netdev_priv(dev);
324 u32 dmas, dmasm;
325 irqreturn_t retval;
326
327 dmas = readl(&lp->rx_dma_regs->dmas);
328 if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
Florian Fainellief112912008-03-19 17:14:51 +0100329 dmasm = readl(&lp->rx_dma_regs->dmasm);
330 writel(dmasm | (DMA_STAT_DONE |
331 DMA_STAT_HALT | DMA_STAT_ERR),
332 &lp->rx_dma_regs->dmasm);
333
Ben Hutchings288379f2009-01-19 16:43:59 -0800334 napi_schedule(&lp->napi);
Phil Sutter60d3f982009-01-14 21:50:12 -0800335
Florian Fainellief112912008-03-19 17:14:51 +0100336 if (dmas & DMA_STAT_ERR)
Phil Sutterf16aea42009-08-12 12:22:46 +0000337 printk(KERN_ERR "%s: DMA error\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100338
339 retval = IRQ_HANDLED;
340 } else
341 retval = IRQ_NONE;
342
343 return retval;
344}
345
346static int korina_rx(struct net_device *dev, int limit)
347{
348 struct korina_private *lp = netdev_priv(dev);
349 struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
350 struct sk_buff *skb, *skb_new;
351 u8 *pkt_buf;
Phil Sutter4cf83b62009-01-14 21:48:59 -0800352 u32 devcs, pkt_len, dmas;
Florian Fainellief112912008-03-19 17:14:51 +0100353 int count;
354
355 dma_cache_inv((u32)rd, sizeof(*rd));
356
357 for (count = 0; count < limit; count++) {
Phil Sutter4cf83b62009-01-14 21:48:59 -0800358 skb = lp->rx_skb[lp->rx_next_done];
359 skb_new = NULL;
Florian Fainellief112912008-03-19 17:14:51 +0100360
361 devcs = rd->devcs;
362
Phil Sutter4cf83b62009-01-14 21:48:59 -0800363 if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0)
364 break;
365
Florian Fainellief112912008-03-19 17:14:51 +0100366 /* Update statistics counters */
367 if (devcs & ETH_RX_CRC)
368 dev->stats.rx_crc_errors++;
369 if (devcs & ETH_RX_LOR)
370 dev->stats.rx_length_errors++;
371 if (devcs & ETH_RX_LE)
372 dev->stats.rx_length_errors++;
373 if (devcs & ETH_RX_OVR)
Phil Sutterb1011b32010-05-29 13:23:36 +0000374 dev->stats.rx_fifo_errors++;
Florian Fainellief112912008-03-19 17:14:51 +0100375 if (devcs & ETH_RX_CV)
376 dev->stats.rx_frame_errors++;
377 if (devcs & ETH_RX_CES)
378 dev->stats.rx_length_errors++;
379 if (devcs & ETH_RX_MP)
380 dev->stats.multicast++;
381
382 if ((devcs & ETH_RX_LD) != ETH_RX_LD) {
383 /* check that this is a whole packet
384 * WARNING: DMA_FD bit incorrectly set
385 * in Rc32434 (errata ref #077) */
386 dev->stats.rx_errors++;
387 dev->stats.rx_dropped++;
Phil Sutter4cf83b62009-01-14 21:48:59 -0800388 } else if ((devcs & ETH_RX_ROK)) {
Florian Fainellief112912008-03-19 17:14:51 +0100389 pkt_len = RCVPKT_LENGTH(devcs);
Florian Fainellief112912008-03-19 17:14:51 +0100390
Phil Sutter4cf83b62009-01-14 21:48:59 -0800391 /* must be the (first and) last
392 * descriptor then */
393 pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
Florian Fainellief112912008-03-19 17:14:51 +0100394
Phil Sutter4cf83b62009-01-14 21:48:59 -0800395 /* invalidate the cache */
396 dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
Florian Fainellief112912008-03-19 17:14:51 +0100397
Phil Sutter4cf83b62009-01-14 21:48:59 -0800398 /* Malloc up new buffer. */
Eric Dumazet89d71a62009-10-13 05:34:20 +0000399 skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
Florian Fainellief112912008-03-19 17:14:51 +0100400
Phil Sutter4cf83b62009-01-14 21:48:59 -0800401 if (!skb_new)
402 break;
403 /* Do not count the CRC */
404 skb_put(skb, pkt_len - 4);
405 skb->protocol = eth_type_trans(skb, dev);
Florian Fainellief112912008-03-19 17:14:51 +0100406
Phil Sutter4cf83b62009-01-14 21:48:59 -0800407 /* Pass the packet to upper layers */
408 netif_receive_skb(skb);
409 dev->stats.rx_packets++;
410 dev->stats.rx_bytes += pkt_len;
Florian Fainellief112912008-03-19 17:14:51 +0100411
Phil Sutter4cf83b62009-01-14 21:48:59 -0800412 /* Update the mcast stats */
413 if (devcs & ETH_RX_MP)
414 dev->stats.multicast++;
Florian Fainellief112912008-03-19 17:14:51 +0100415
Phil Sutter4cf83b62009-01-14 21:48:59 -0800416 lp->rx_skb[lp->rx_next_done] = skb_new;
Florian Fainellief112912008-03-19 17:14:51 +0100417 }
Phil Sutter4cf83b62009-01-14 21:48:59 -0800418
419 rd->devcs = 0;
420
421 /* Restore descriptor's curr_addr */
422 if (skb_new)
423 rd->ca = CPHYSADDR(skb_new->data);
424 else
425 rd->ca = CPHYSADDR(skb->data);
426
427 rd->control = DMA_COUNT(KORINA_RBSIZE) |
428 DMA_DESC_COD | DMA_DESC_IOD;
429 lp->rd_ring[(lp->rx_next_done - 1) &
430 KORINA_RDS_MASK].control &=
431 ~DMA_DESC_COD;
432
433 lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
434 dma_cache_wback((u32)rd, sizeof(*rd));
435 rd = &lp->rd_ring[lp->rx_next_done];
436 writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
Florian Fainellief112912008-03-19 17:14:51 +0100437 }
438
439 dmas = readl(&lp->rx_dma_regs->dmas);
440
441 if (dmas & DMA_STAT_HALT) {
442 writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
443 &lp->rx_dma_regs->dmas);
444
445 lp->dma_halt_cnt++;
446 rd->devcs = 0;
447 skb = lp->rx_skb[lp->rx_next_done];
448 rd->ca = CPHYSADDR(skb->data);
449 dma_cache_wback((u32)rd, sizeof(*rd));
450 korina_chain_rx(lp, rd);
451 }
452
453 return count;
454}
455
456static int korina_poll(struct napi_struct *napi, int budget)
457{
458 struct korina_private *lp =
459 container_of(napi, struct korina_private, napi);
460 struct net_device *dev = lp->dev;
461 int work_done;
462
463 work_done = korina_rx(dev, budget);
464 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800465 napi_complete_done(napi, work_done);
Florian Fainellief112912008-03-19 17:14:51 +0100466
467 writel(readl(&lp->rx_dma_regs->dmasm) &
468 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
469 &lp->rx_dma_regs->dmasm);
470 }
471 return work_done;
472}
473
474/*
475 * Set or clear the multicast filter for this adaptor.
476 */
477static void korina_multicast_list(struct net_device *dev)
478{
479 struct korina_private *lp = netdev_priv(dev);
480 unsigned long flags;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000481 struct netdev_hw_addr *ha;
Florian Fainellief112912008-03-19 17:14:51 +0100482 u32 recognise = ETH_ARC_AB; /* always accept broadcasts */
Florian Fainellief112912008-03-19 17:14:51 +0100483
484 /* Set promiscuous mode */
485 if (dev->flags & IFF_PROMISC)
486 recognise |= ETH_ARC_PRO;
487
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000488 else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
Florian Fainellief112912008-03-19 17:14:51 +0100489 /* All multicast and broadcast */
490 recognise |= ETH_ARC_AM;
491
492 /* Build the hash table */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000493 if (netdev_mc_count(dev) > 4) {
Emilio Lópeze998fd42013-05-17 10:42:56 +0000494 u16 hash_table[4] = { 0 };
Florian Fainellief112912008-03-19 17:14:51 +0100495 u32 crc;
496
Jiri Pirko22bedad32010-04-01 21:22:57 +0000497 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +0000498 crc = ether_crc_le(6, ha->addr);
Florian Fainellief112912008-03-19 17:14:51 +0100499 crc >>= 26;
500 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
501 }
502 /* Accept filtered multicast */
503 recognise |= ETH_ARC_AFM;
504
505 /* Fill the MAC hash tables with their values */
506 writel((u32)(hash_table[1] << 16 | hash_table[0]),
507 &lp->eth_regs->ethhash0);
508 writel((u32)(hash_table[3] << 16 | hash_table[2]),
509 &lp->eth_regs->ethhash1);
510 }
511
512 spin_lock_irqsave(&lp->lock, flags);
513 writel(recognise, &lp->eth_regs->etharc);
514 spin_unlock_irqrestore(&lp->lock, flags);
515}
516
517static void korina_tx(struct net_device *dev)
518{
519 struct korina_private *lp = netdev_priv(dev);
520 struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
521 u32 devcs;
522 u32 dmas;
523
524 spin_lock(&lp->lock);
525
526 /* Process all desc that are done */
527 while (IS_DMA_FINISHED(td->control)) {
528 if (lp->tx_full == 1) {
529 netif_wake_queue(dev);
530 lp->tx_full = 0;
531 }
532
533 devcs = lp->td_ring[lp->tx_next_done].devcs;
534 if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
535 (ETH_TX_FD | ETH_TX_LD)) {
536 dev->stats.tx_errors++;
537 dev->stats.tx_dropped++;
538
539 /* Should never happen */
Phil Sutterf16aea42009-08-12 12:22:46 +0000540 printk(KERN_ERR "%s: split tx ignored\n",
Florian Fainellief112912008-03-19 17:14:51 +0100541 dev->name);
542 } else if (devcs & ETH_TX_TOK) {
543 dev->stats.tx_packets++;
544 dev->stats.tx_bytes +=
545 lp->tx_skb[lp->tx_next_done]->len;
546 } else {
547 dev->stats.tx_errors++;
548 dev->stats.tx_dropped++;
549
550 /* Underflow */
551 if (devcs & ETH_TX_UND)
552 dev->stats.tx_fifo_errors++;
553
554 /* Oversized frame */
555 if (devcs & ETH_TX_OF)
556 dev->stats.tx_aborted_errors++;
557
558 /* Excessive deferrals */
559 if (devcs & ETH_TX_ED)
560 dev->stats.tx_carrier_errors++;
561
562 /* Collisions: medium busy */
563 if (devcs & ETH_TX_EC)
564 dev->stats.collisions++;
565
566 /* Late collision */
567 if (devcs & ETH_TX_LC)
568 dev->stats.tx_window_errors++;
569 }
570
571 /* We must always free the original skb */
572 if (lp->tx_skb[lp->tx_next_done]) {
573 dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
574 lp->tx_skb[lp->tx_next_done] = NULL;
575 }
576
577 lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
578 lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
579 lp->td_ring[lp->tx_next_done].link = 0;
580 lp->td_ring[lp->tx_next_done].ca = 0;
581 lp->tx_count--;
582
583 /* Go on to next transmission */
584 lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
585 td = &lp->td_ring[lp->tx_next_done];
586
587 }
588
589 /* Clear the DMA status register */
590 dmas = readl(&lp->tx_dma_regs->dmas);
591 writel(~dmas, &lp->tx_dma_regs->dmas);
592
593 writel(readl(&lp->tx_dma_regs->dmasm) &
594 ~(DMA_STAT_FINI | DMA_STAT_ERR),
595 &lp->tx_dma_regs->dmasm);
596
597 spin_unlock(&lp->lock);
598}
599
600static irqreturn_t
601korina_tx_dma_interrupt(int irq, void *dev_id)
602{
603 struct net_device *dev = dev_id;
604 struct korina_private *lp = netdev_priv(dev);
605 u32 dmas, dmasm;
606 irqreturn_t retval;
607
608 dmas = readl(&lp->tx_dma_regs->dmas);
609
610 if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
Florian Fainellief112912008-03-19 17:14:51 +0100611 dmasm = readl(&lp->tx_dma_regs->dmasm);
612 writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
613 &lp->tx_dma_regs->dmasm);
614
Phil Sutter60d3f982009-01-14 21:50:12 -0800615 korina_tx(dev);
616
Florian Fainellief112912008-03-19 17:14:51 +0100617 if (lp->tx_chain_status == desc_filled &&
618 (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
619 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
620 &(lp->tx_dma_regs->dmandptr));
621 lp->tx_chain_status = desc_empty;
622 lp->tx_chain_head = lp->tx_chain_tail;
Florian Westphal860e9532016-05-03 16:33:13 +0200623 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100624 }
625 if (dmas & DMA_STAT_ERR)
Phil Sutterf16aea42009-08-12 12:22:46 +0000626 printk(KERN_ERR "%s: DMA error\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100627
628 retval = IRQ_HANDLED;
629 } else
630 retval = IRQ_NONE;
631
632 return retval;
633}
634
635
636static void korina_check_media(struct net_device *dev, unsigned int init_media)
637{
638 struct korina_private *lp = netdev_priv(dev);
639
640 mii_check_media(&lp->mii_if, 0, init_media);
641
642 if (lp->mii_if.full_duplex)
643 writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
644 &lp->eth_regs->ethmac2);
645 else
646 writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
647 &lp->eth_regs->ethmac2);
648}
649
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000650static void korina_poll_media(unsigned long data)
651{
652 struct net_device *dev = (struct net_device *) data;
653 struct korina_private *lp = netdev_priv(dev);
654
655 korina_check_media(dev, 0);
656 mod_timer(&lp->media_check_timer, jiffies + HZ);
657}
658
Florian Fainellief112912008-03-19 17:14:51 +0100659static void korina_set_carrier(struct mii_if_info *mii)
660{
661 if (mii->force_media) {
662 /* autoneg is off: Link is always assumed to be up */
663 if (!netif_carrier_ok(mii->dev))
664 netif_carrier_on(mii->dev);
665 } else /* Let MMI library update carrier status */
666 korina_check_media(mii->dev, 0);
667}
668
669static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
670{
671 struct korina_private *lp = netdev_priv(dev);
672 struct mii_ioctl_data *data = if_mii(rq);
673 int rc;
674
675 if (!netif_running(dev))
676 return -EINVAL;
677 spin_lock_irq(&lp->lock);
678 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
679 spin_unlock_irq(&lp->lock);
680 korina_set_carrier(&lp->mii_if);
681
682 return rc;
683}
684
685/* ethtool helpers */
686static void netdev_get_drvinfo(struct net_device *dev,
687 struct ethtool_drvinfo *info)
688{
689 struct korina_private *lp = netdev_priv(dev);
690
Jiri Pirko7826d432013-01-06 00:44:26 +0000691 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
692 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
693 strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info));
Florian Fainellief112912008-03-19 17:14:51 +0100694}
695
Philippe Reynesaf473682017-01-14 12:33:19 +0100696static int netdev_get_link_ksettings(struct net_device *dev,
697 struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100698{
699 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100700
701 spin_lock_irq(&lp->lock);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300702 mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100703 spin_unlock_irq(&lp->lock);
704
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300705 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100706}
707
Philippe Reynesaf473682017-01-14 12:33:19 +0100708static int netdev_set_link_ksettings(struct net_device *dev,
709 const struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100710{
711 struct korina_private *lp = netdev_priv(dev);
712 int rc;
713
714 spin_lock_irq(&lp->lock);
Philippe Reynesaf473682017-01-14 12:33:19 +0100715 rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100716 spin_unlock_irq(&lp->lock);
717 korina_set_carrier(&lp->mii_if);
718
719 return rc;
720}
721
722static u32 netdev_get_link(struct net_device *dev)
723{
724 struct korina_private *lp = netdev_priv(dev);
725
726 return mii_link_ok(&lp->mii_if);
727}
728
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700729static const struct ethtool_ops netdev_ethtool_ops = {
Florian Fainellief112912008-03-19 17:14:51 +0100730 .get_drvinfo = netdev_get_drvinfo,
Florian Fainellief112912008-03-19 17:14:51 +0100731 .get_link = netdev_get_link,
Philippe Reynesaf473682017-01-14 12:33:19 +0100732 .get_link_ksettings = netdev_get_link_ksettings,
733 .set_link_ksettings = netdev_set_link_ksettings,
Florian Fainellief112912008-03-19 17:14:51 +0100734};
735
Phil Sutter70108372009-08-12 12:52:32 +0000736static int korina_alloc_ring(struct net_device *dev)
Florian Fainellief112912008-03-19 17:14:51 +0100737{
738 struct korina_private *lp = netdev_priv(dev);
Phil Suttere85bf472009-01-15 12:29:57 +0000739 struct sk_buff *skb;
Florian Fainellief112912008-03-19 17:14:51 +0100740 int i;
741
742 /* Initialize the transmit descriptors */
743 for (i = 0; i < KORINA_NUM_TDS; i++) {
744 lp->td_ring[i].control = DMA_DESC_IOF;
745 lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
746 lp->td_ring[i].ca = 0;
747 lp->td_ring[i].link = 0;
748 }
749 lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
750 lp->tx_full = lp->tx_count = 0;
751 lp->tx_chain_status = desc_empty;
752
753 /* Initialize the receive descriptors */
754 for (i = 0; i < KORINA_NUM_RDS; i++) {
Phil Sutter53ee4902010-05-29 13:23:35 +0000755 skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
Florian Fainellief112912008-03-19 17:14:51 +0100756 if (!skb)
Phil Sutter70108372009-08-12 12:52:32 +0000757 return -ENOMEM;
Florian Fainellief112912008-03-19 17:14:51 +0100758 lp->rx_skb[i] = skb;
759 lp->rd_ring[i].control = DMA_DESC_IOD |
760 DMA_COUNT(KORINA_RBSIZE);
761 lp->rd_ring[i].devcs = 0;
762 lp->rd_ring[i].ca = CPHYSADDR(skb->data);
763 lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
764 }
765
Phil Sutter6a2fe982009-01-15 12:29:55 +0000766 /* loop back receive descriptors, so the last
767 * descriptor points to the first one */
768 lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
769 lp->rd_ring[i - 1].control |= DMA_DESC_COD;
Florian Fainellief112912008-03-19 17:14:51 +0100770
Phil Sutter6a2fe982009-01-15 12:29:55 +0000771 lp->rx_next_done = 0;
Florian Fainellief112912008-03-19 17:14:51 +0100772 lp->rx_chain_head = 0;
773 lp->rx_chain_tail = 0;
774 lp->rx_chain_status = desc_empty;
Phil Sutter70108372009-08-12 12:52:32 +0000775
776 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100777}
778
779static void korina_free_ring(struct net_device *dev)
780{
781 struct korina_private *lp = netdev_priv(dev);
782 int i;
783
784 for (i = 0; i < KORINA_NUM_RDS; i++) {
785 lp->rd_ring[i].control = 0;
786 if (lp->rx_skb[i])
787 dev_kfree_skb_any(lp->rx_skb[i]);
788 lp->rx_skb[i] = NULL;
789 }
790
791 for (i = 0; i < KORINA_NUM_TDS; i++) {
792 lp->td_ring[i].control = 0;
793 if (lp->tx_skb[i])
794 dev_kfree_skb_any(lp->tx_skb[i]);
795 lp->tx_skb[i] = NULL;
796 }
797}
798
799/*
800 * Initialize the RC32434 ethernet controller.
801 */
802static int korina_init(struct net_device *dev)
803{
804 struct korina_private *lp = netdev_priv(dev);
805
806 /* Disable DMA */
807 korina_abort_tx(dev);
808 korina_abort_rx(dev);
809
810 /* reset ethernet logic */
811 writel(0, &lp->eth_regs->ethintfc);
812 while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
Florian Westphal860e9532016-05-03 16:33:13 +0200813 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100814
815 /* Enable Ethernet Interface */
816 writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
817
818 /* Allocate rings */
Phil Sutter70108372009-08-12 12:52:32 +0000819 if (korina_alloc_ring(dev)) {
820 printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
821 korina_free_ring(dev);
822 return -ENOMEM;
823 }
Florian Fainellief112912008-03-19 17:14:51 +0100824
825 writel(0, &lp->rx_dma_regs->dmas);
826 /* Start Rx DMA */
827 korina_start_rx(lp, &lp->rd_ring[0]);
828
829 writel(readl(&lp->tx_dma_regs->dmasm) &
830 ~(DMA_STAT_FINI | DMA_STAT_ERR),
831 &lp->tx_dma_regs->dmasm);
832 writel(readl(&lp->rx_dma_regs->dmasm) &
833 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
834 &lp->rx_dma_regs->dmasm);
835
836 /* Accept only packets destined for this Ethernet device address */
837 writel(ETH_ARC_AB, &lp->eth_regs->etharc);
838
839 /* Set all Ether station address registers to their initial values */
840 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
841 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
842
843 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
844 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
845
846 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
847 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
848
849 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
850 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
851
852
853 /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
854 writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
855 &lp->eth_regs->ethmac2);
856
857 /* Back to back inter-packet-gap */
858 writel(0x15, &lp->eth_regs->ethipgt);
859 /* Non - Back to back inter-packet-gap */
860 writel(0x12, &lp->eth_regs->ethipgr);
861
862 /* Management Clock Prescaler Divisor
863 * Clock independent setting */
864 writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
865 &lp->eth_regs->ethmcp);
866
867 /* don't transmit until fifo contains 48b */
868 writel(48, &lp->eth_regs->ethfifott);
869
870 writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
871
872 napi_enable(&lp->napi);
873 netif_start_queue(dev);
874
875 return 0;
876}
877
878/*
879 * Restart the RC32434 ethernet controller.
Florian Fainellief112912008-03-19 17:14:51 +0100880 */
Phil Sutterceb3d232010-05-29 13:23:34 +0000881static void korina_restart_task(struct work_struct *work)
Florian Fainellief112912008-03-19 17:14:51 +0100882{
Phil Sutterceb3d232010-05-29 13:23:34 +0000883 struct korina_private *lp = container_of(work,
884 struct korina_private, restart_task);
885 struct net_device *dev = lp->dev;
Florian Fainellief112912008-03-19 17:14:51 +0100886
887 /*
888 * Disable interrupts
889 */
890 disable_irq(lp->rx_irq);
891 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100892
893 writel(readl(&lp->tx_dma_regs->dmasm) |
894 DMA_STAT_FINI | DMA_STAT_ERR,
895 &lp->tx_dma_regs->dmasm);
896 writel(readl(&lp->rx_dma_regs->dmasm) |
897 DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
898 &lp->rx_dma_regs->dmasm);
899
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800900 napi_disable(&lp->napi);
901
Florian Fainellie6afb1a2016-12-23 19:56:56 -0800902 korina_free_ring(dev);
903
Phil Sutterceb3d232010-05-29 13:23:34 +0000904 if (korina_init(dev) < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000905 printk(KERN_ERR "%s: cannot restart device\n", dev->name);
Phil Sutterceb3d232010-05-29 13:23:34 +0000906 return;
Florian Fainellief112912008-03-19 17:14:51 +0100907 }
908 korina_multicast_list(dev);
909
Florian Fainellief112912008-03-19 17:14:51 +0100910 enable_irq(lp->tx_irq);
911 enable_irq(lp->rx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100912}
913
Florian Fainellief112912008-03-19 17:14:51 +0100914static void korina_tx_timeout(struct net_device *dev)
915{
916 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100917
Phil Sutterceb3d232010-05-29 13:23:34 +0000918 schedule_work(&lp->restart_task);
Florian Fainellief112912008-03-19 17:14:51 +0100919}
920
Florian Fainellief112912008-03-19 17:14:51 +0100921#ifdef CONFIG_NET_POLL_CONTROLLER
922static void korina_poll_controller(struct net_device *dev)
923{
924 disable_irq(dev->irq);
925 korina_tx_dma_interrupt(dev->irq, dev);
926 enable_irq(dev->irq);
927}
928#endif
929
930static int korina_open(struct net_device *dev)
931{
932 struct korina_private *lp = netdev_priv(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +0200933 int ret;
Florian Fainellief112912008-03-19 17:14:51 +0100934
935 /* Initialize */
936 ret = korina_init(dev);
937 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000938 printk(KERN_ERR "%s: cannot open device\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100939 goto out;
940 }
941
942 /* Install the interrupt handler
Roman Yeryomin7ce103b2017-09-17 20:24:15 +0300943 * that handles the Done Finished */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800944 ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200945 0, "Korina ethernet Rx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100946 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000947 printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
Florian Fainellief112912008-03-19 17:14:51 +0100948 dev->name, lp->rx_irq);
949 goto err_release;
950 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800951 ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200952 0, "Korina ethernet Tx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100953 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000954 printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
Florian Fainellief112912008-03-19 17:14:51 +0100955 dev->name, lp->tx_irq);
956 goto err_free_rx_irq;
957 }
958
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000959 mod_timer(&lp->media_check_timer, jiffies + 1);
Francois Romieu751c2e42008-04-20 18:05:31 +0200960out:
961 return ret;
Florian Fainellief112912008-03-19 17:14:51 +0100962
Florian Fainellief112912008-03-19 17:14:51 +0100963err_free_rx_irq:
964 free_irq(lp->rx_irq, dev);
965err_release:
966 korina_free_ring(dev);
967 goto out;
Florian Fainellief112912008-03-19 17:14:51 +0100968}
969
970static int korina_close(struct net_device *dev)
971{
972 struct korina_private *lp = netdev_priv(dev);
973 u32 tmp;
974
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000975 del_timer(&lp->media_check_timer);
976
Florian Fainellief112912008-03-19 17:14:51 +0100977 /* Disable interrupts */
978 disable_irq(lp->rx_irq);
979 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100980
981 korina_abort_tx(dev);
982 tmp = readl(&lp->tx_dma_regs->dmasm);
983 tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
984 writel(tmp, &lp->tx_dma_regs->dmasm);
985
986 korina_abort_rx(dev);
987 tmp = readl(&lp->rx_dma_regs->dmasm);
988 tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
989 writel(tmp, &lp->rx_dma_regs->dmasm);
990
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800991 napi_disable(&lp->napi);
992
Phil Sutterceb3d232010-05-29 13:23:34 +0000993 cancel_work_sync(&lp->restart_task);
994
Florian Fainellie6afb1a2016-12-23 19:56:56 -0800995 korina_free_ring(dev);
996
Florian Fainellief112912008-03-19 17:14:51 +0100997 free_irq(lp->rx_irq, dev);
998 free_irq(lp->tx_irq, dev);
Florian Fainellief112912008-03-19 17:14:51 +0100999
1000 return 0;
1001}
1002
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001003static const struct net_device_ops korina_netdev_ops = {
1004 .ndo_open = korina_open,
1005 .ndo_stop = korina_close,
1006 .ndo_start_xmit = korina_send_packet,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001007 .ndo_set_rx_mode = korina_multicast_list,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001008 .ndo_tx_timeout = korina_tx_timeout,
1009 .ndo_do_ioctl = korina_ioctl,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001010 .ndo_validate_addr = eth_validate_addr,
1011 .ndo_set_mac_address = eth_mac_addr,
1012#ifdef CONFIG_NET_POLL_CONTROLLER
1013 .ndo_poll_controller = korina_poll_controller,
1014#endif
1015};
1016
Florian Fainellief112912008-03-19 17:14:51 +01001017static int korina_probe(struct platform_device *pdev)
1018{
1019 struct korina_device *bif = platform_get_drvdata(pdev);
1020 struct korina_private *lp;
1021 struct net_device *dev;
1022 struct resource *r;
Francois Romieue3152ab2008-04-20 18:06:13 +02001023 int rc;
Florian Fainellief112912008-03-19 17:14:51 +01001024
1025 dev = alloc_etherdev(sizeof(struct korina_private));
Joe Perches41de8d42012-01-29 13:47:52 +00001026 if (!dev)
Florian Fainellief112912008-03-19 17:14:51 +01001027 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001028
Florian Fainellief112912008-03-19 17:14:51 +01001029 SET_NETDEV_DEV(dev, &pdev->dev);
Florian Fainellief112912008-03-19 17:14:51 +01001030 lp = netdev_priv(dev);
1031
1032 bif->dev = dev;
Joe Perchesd458cdf2013-10-01 19:04:40 -07001033 memcpy(dev->dev_addr, bif->mac, ETH_ALEN);
Florian Fainellief112912008-03-19 17:14:51 +01001034
1035 lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
1036 lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
Florian Fainellief112912008-03-19 17:14:51 +01001037
1038 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
1039 dev->base_addr = r->start;
Dan Carpenter38013262010-03-22 02:11:45 +00001040 lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001041 if (!lp->eth_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001042 printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001043 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001044 goto probe_err_out;
1045 }
1046
1047 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
Dan Carpenter38013262010-03-22 02:11:45 +00001048 lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001049 if (!lp->rx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001050 printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001051 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001052 goto probe_err_dma_rx;
1053 }
1054
1055 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
Dan Carpenter38013262010-03-22 02:11:45 +00001056 lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001057 if (!lp->tx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001058 printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001059 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001060 goto probe_err_dma_tx;
1061 }
1062
1063 lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
1064 if (!lp->td_ring) {
Francois Romieue3152ab2008-04-20 18:06:13 +02001065 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001066 goto probe_err_td_ring;
1067 }
1068
1069 dma_cache_inv((unsigned long)(lp->td_ring),
1070 TD_RING_SIZE + RD_RING_SIZE);
1071
1072 /* now convert TD_RING pointer to KSEG1 */
1073 lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
1074 lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
1075
1076 spin_lock_init(&lp->lock);
1077 /* just use the rx dma irq */
1078 dev->irq = lp->rx_irq;
1079 lp->dev = dev;
1080
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001081 dev->netdev_ops = &korina_netdev_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001082 dev->ethtool_ops = &netdev_ethtool_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001083 dev->watchdog_timeo = TX_TIMEOUT;
Florian Fainellief112912008-03-19 17:14:51 +01001084 netif_napi_add(dev, &lp->napi, korina_poll, 64);
1085
1086 lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
1087 lp->mii_if.dev = dev;
1088 lp->mii_if.mdio_read = mdio_read;
1089 lp->mii_if.mdio_write = mdio_write;
1090 lp->mii_if.phy_id = lp->phy_addr;
1091 lp->mii_if.phy_id_mask = 0x1f;
1092 lp->mii_if.reg_num_mask = 0x1f;
1093
Francois Romieue3152ab2008-04-20 18:06:13 +02001094 rc = register_netdev(dev);
1095 if (rc < 0) {
Florian Fainellief112912008-03-19 17:14:51 +01001096 printk(KERN_ERR DRV_NAME
Phil Sutterf16aea42009-08-12 12:22:46 +00001097 ": cannot register net device: %d\n", rc);
Florian Fainellief112912008-03-19 17:14:51 +01001098 goto probe_err_register;
1099 }
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +00001100 setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev);
Phil Sutterf16aea42009-08-12 12:22:46 +00001101
Phil Sutterceb3d232010-05-29 13:23:34 +00001102 INIT_WORK(&lp->restart_task, korina_restart_task);
1103
Phil Sutterf16aea42009-08-12 12:22:46 +00001104 printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
1105 dev->name);
Francois Romieue3152ab2008-04-20 18:06:13 +02001106out:
1107 return rc;
Florian Fainellief112912008-03-19 17:14:51 +01001108
1109probe_err_register:
1110 kfree(lp->td_ring);
1111probe_err_td_ring:
1112 iounmap(lp->tx_dma_regs);
1113probe_err_dma_tx:
1114 iounmap(lp->rx_dma_regs);
1115probe_err_dma_rx:
1116 iounmap(lp->eth_regs);
1117probe_err_out:
1118 free_netdev(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +02001119 goto out;
Florian Fainellief112912008-03-19 17:14:51 +01001120}
1121
1122static int korina_remove(struct platform_device *pdev)
1123{
1124 struct korina_device *bif = platform_get_drvdata(pdev);
1125 struct korina_private *lp = netdev_priv(bif->dev);
1126
Francois Romieue3152ab2008-04-20 18:06:13 +02001127 iounmap(lp->eth_regs);
1128 iounmap(lp->rx_dma_regs);
1129 iounmap(lp->tx_dma_regs);
Florian Fainellief112912008-03-19 17:14:51 +01001130
Florian Fainellief112912008-03-19 17:14:51 +01001131 unregister_netdev(bif->dev);
1132 free_netdev(bif->dev);
1133
1134 return 0;
1135}
1136
1137static struct platform_driver korina_driver = {
1138 .driver.name = "korina",
1139 .probe = korina_probe,
1140 .remove = korina_remove,
1141};
1142
Axel Lindb62f682011-11-27 16:44:17 +00001143module_platform_driver(korina_driver);
Florian Fainellief112912008-03-19 17:14:51 +01001144
1145MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
1146MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1147MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
1148MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
1149MODULE_LICENSE("GPL");