blob: 5f36e1703378c30c1eb71781560cc81e9afec9fd [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
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300366 /* check that this is a whole packet
367 * WARNING: DMA_FD bit incorrectly set
368 * in Rc32434 (errata ref #077) */
369 if (!(devcs & ETH_RX_LD))
370 goto next;
371
372 if (!(devcs & ETH_RX_ROK)) {
373 /* Update statistics counters */
374 dev->stats.rx_errors++;
375 dev->stats.rx_dropped++;
376 if (devcs & ETH_RX_CRC)
377 dev->stats.rx_crc_errors++;
378 if (devcs & ETH_RX_LE)
379 dev->stats.rx_length_errors++;
380 if (devcs & ETH_RX_OVR)
381 dev->stats.rx_fifo_errors++;
382 if (devcs & ETH_RX_CV)
383 dev->stats.rx_frame_errors++;
384 if (devcs & ETH_RX_CES)
385 dev->stats.rx_frame_errors++;
386
387 goto next;
388 }
389
390 pkt_len = RCVPKT_LENGTH(devcs);
391
392 /* must be the (first and) last
393 * descriptor then */
394 pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
395
396 /* invalidate the cache */
397 dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
398
399 /* Malloc up new buffer. */
400 skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
401
402 if (!skb_new)
403 break;
404 /* Do not count the CRC */
405 skb_put(skb, pkt_len - 4);
406 skb->protocol = eth_type_trans(skb, dev);
407
408 /* Pass the packet to upper layers */
Roman Yeryomin247c78f2017-09-17 20:24:50 +0300409 napi_gro_receive(&lp->napi, skb);
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300410 dev->stats.rx_packets++;
411 dev->stats.rx_bytes += pkt_len;
412
413 /* Update the mcast stats */
Florian Fainellief112912008-03-19 17:14:51 +0100414 if (devcs & ETH_RX_MP)
415 dev->stats.multicast++;
416
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300417 lp->rx_skb[lp->rx_next_done] = skb_new;
Florian Fainellief112912008-03-19 17:14:51 +0100418
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300419next:
Phil Sutter4cf83b62009-01-14 21:48:59 -0800420 rd->devcs = 0;
421
422 /* Restore descriptor's curr_addr */
423 if (skb_new)
424 rd->ca = CPHYSADDR(skb_new->data);
425 else
426 rd->ca = CPHYSADDR(skb->data);
427
428 rd->control = DMA_COUNT(KORINA_RBSIZE) |
429 DMA_DESC_COD | DMA_DESC_IOD;
430 lp->rd_ring[(lp->rx_next_done - 1) &
431 KORINA_RDS_MASK].control &=
432 ~DMA_DESC_COD;
433
434 lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
435 dma_cache_wback((u32)rd, sizeof(*rd));
436 rd = &lp->rd_ring[lp->rx_next_done];
437 writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
Florian Fainellief112912008-03-19 17:14:51 +0100438 }
439
440 dmas = readl(&lp->rx_dma_regs->dmas);
441
442 if (dmas & DMA_STAT_HALT) {
443 writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
444 &lp->rx_dma_regs->dmas);
445
446 lp->dma_halt_cnt++;
447 rd->devcs = 0;
448 skb = lp->rx_skb[lp->rx_next_done];
449 rd->ca = CPHYSADDR(skb->data);
450 dma_cache_wback((u32)rd, sizeof(*rd));
451 korina_chain_rx(lp, rd);
452 }
453
454 return count;
455}
456
457static int korina_poll(struct napi_struct *napi, int budget)
458{
459 struct korina_private *lp =
460 container_of(napi, struct korina_private, napi);
461 struct net_device *dev = lp->dev;
462 int work_done;
463
464 work_done = korina_rx(dev, budget);
465 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800466 napi_complete_done(napi, work_done);
Florian Fainellief112912008-03-19 17:14:51 +0100467
468 writel(readl(&lp->rx_dma_regs->dmasm) &
469 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
470 &lp->rx_dma_regs->dmasm);
471 }
472 return work_done;
473}
474
475/*
476 * Set or clear the multicast filter for this adaptor.
477 */
478static void korina_multicast_list(struct net_device *dev)
479{
480 struct korina_private *lp = netdev_priv(dev);
481 unsigned long flags;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000482 struct netdev_hw_addr *ha;
Florian Fainellief112912008-03-19 17:14:51 +0100483 u32 recognise = ETH_ARC_AB; /* always accept broadcasts */
Florian Fainellief112912008-03-19 17:14:51 +0100484
485 /* Set promiscuous mode */
486 if (dev->flags & IFF_PROMISC)
487 recognise |= ETH_ARC_PRO;
488
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000489 else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
Florian Fainellief112912008-03-19 17:14:51 +0100490 /* All multicast and broadcast */
491 recognise |= ETH_ARC_AM;
492
493 /* Build the hash table */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000494 if (netdev_mc_count(dev) > 4) {
Emilio Lópeze998fd42013-05-17 10:42:56 +0000495 u16 hash_table[4] = { 0 };
Florian Fainellief112912008-03-19 17:14:51 +0100496 u32 crc;
497
Jiri Pirko22bedad32010-04-01 21:22:57 +0000498 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +0000499 crc = ether_crc_le(6, ha->addr);
Florian Fainellief112912008-03-19 17:14:51 +0100500 crc >>= 26;
501 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
502 }
503 /* Accept filtered multicast */
504 recognise |= ETH_ARC_AFM;
505
506 /* Fill the MAC hash tables with their values */
507 writel((u32)(hash_table[1] << 16 | hash_table[0]),
508 &lp->eth_regs->ethhash0);
509 writel((u32)(hash_table[3] << 16 | hash_table[2]),
510 &lp->eth_regs->ethhash1);
511 }
512
513 spin_lock_irqsave(&lp->lock, flags);
514 writel(recognise, &lp->eth_regs->etharc);
515 spin_unlock_irqrestore(&lp->lock, flags);
516}
517
518static void korina_tx(struct net_device *dev)
519{
520 struct korina_private *lp = netdev_priv(dev);
521 struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
522 u32 devcs;
523 u32 dmas;
524
525 spin_lock(&lp->lock);
526
527 /* Process all desc that are done */
528 while (IS_DMA_FINISHED(td->control)) {
529 if (lp->tx_full == 1) {
530 netif_wake_queue(dev);
531 lp->tx_full = 0;
532 }
533
534 devcs = lp->td_ring[lp->tx_next_done].devcs;
535 if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
536 (ETH_TX_FD | ETH_TX_LD)) {
537 dev->stats.tx_errors++;
538 dev->stats.tx_dropped++;
539
540 /* Should never happen */
Phil Sutterf16aea42009-08-12 12:22:46 +0000541 printk(KERN_ERR "%s: split tx ignored\n",
Florian Fainellief112912008-03-19 17:14:51 +0100542 dev->name);
543 } else if (devcs & ETH_TX_TOK) {
544 dev->stats.tx_packets++;
545 dev->stats.tx_bytes +=
546 lp->tx_skb[lp->tx_next_done]->len;
547 } else {
548 dev->stats.tx_errors++;
549 dev->stats.tx_dropped++;
550
551 /* Underflow */
552 if (devcs & ETH_TX_UND)
553 dev->stats.tx_fifo_errors++;
554
555 /* Oversized frame */
556 if (devcs & ETH_TX_OF)
557 dev->stats.tx_aborted_errors++;
558
559 /* Excessive deferrals */
560 if (devcs & ETH_TX_ED)
561 dev->stats.tx_carrier_errors++;
562
563 /* Collisions: medium busy */
564 if (devcs & ETH_TX_EC)
565 dev->stats.collisions++;
566
567 /* Late collision */
568 if (devcs & ETH_TX_LC)
569 dev->stats.tx_window_errors++;
570 }
571
572 /* We must always free the original skb */
573 if (lp->tx_skb[lp->tx_next_done]) {
574 dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
575 lp->tx_skb[lp->tx_next_done] = NULL;
576 }
577
578 lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
579 lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
580 lp->td_ring[lp->tx_next_done].link = 0;
581 lp->td_ring[lp->tx_next_done].ca = 0;
582 lp->tx_count--;
583
584 /* Go on to next transmission */
585 lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
586 td = &lp->td_ring[lp->tx_next_done];
587
588 }
589
590 /* Clear the DMA status register */
591 dmas = readl(&lp->tx_dma_regs->dmas);
592 writel(~dmas, &lp->tx_dma_regs->dmas);
593
594 writel(readl(&lp->tx_dma_regs->dmasm) &
595 ~(DMA_STAT_FINI | DMA_STAT_ERR),
596 &lp->tx_dma_regs->dmasm);
597
598 spin_unlock(&lp->lock);
599}
600
601static irqreturn_t
602korina_tx_dma_interrupt(int irq, void *dev_id)
603{
604 struct net_device *dev = dev_id;
605 struct korina_private *lp = netdev_priv(dev);
606 u32 dmas, dmasm;
607 irqreturn_t retval;
608
609 dmas = readl(&lp->tx_dma_regs->dmas);
610
611 if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
Florian Fainellief112912008-03-19 17:14:51 +0100612 dmasm = readl(&lp->tx_dma_regs->dmasm);
613 writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
614 &lp->tx_dma_regs->dmasm);
615
Phil Sutter60d3f982009-01-14 21:50:12 -0800616 korina_tx(dev);
617
Florian Fainellief112912008-03-19 17:14:51 +0100618 if (lp->tx_chain_status == desc_filled &&
619 (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
620 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
621 &(lp->tx_dma_regs->dmandptr));
622 lp->tx_chain_status = desc_empty;
623 lp->tx_chain_head = lp->tx_chain_tail;
Florian Westphal860e9532016-05-03 16:33:13 +0200624 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100625 }
626 if (dmas & DMA_STAT_ERR)
Phil Sutterf16aea42009-08-12 12:22:46 +0000627 printk(KERN_ERR "%s: DMA error\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100628
629 retval = IRQ_HANDLED;
630 } else
631 retval = IRQ_NONE;
632
633 return retval;
634}
635
636
637static void korina_check_media(struct net_device *dev, unsigned int init_media)
638{
639 struct korina_private *lp = netdev_priv(dev);
640
641 mii_check_media(&lp->mii_if, 0, init_media);
642
643 if (lp->mii_if.full_duplex)
644 writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
645 &lp->eth_regs->ethmac2);
646 else
647 writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
648 &lp->eth_regs->ethmac2);
649}
650
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000651static void korina_poll_media(unsigned long data)
652{
653 struct net_device *dev = (struct net_device *) data;
654 struct korina_private *lp = netdev_priv(dev);
655
656 korina_check_media(dev, 0);
657 mod_timer(&lp->media_check_timer, jiffies + HZ);
658}
659
Florian Fainellief112912008-03-19 17:14:51 +0100660static void korina_set_carrier(struct mii_if_info *mii)
661{
662 if (mii->force_media) {
663 /* autoneg is off: Link is always assumed to be up */
664 if (!netif_carrier_ok(mii->dev))
665 netif_carrier_on(mii->dev);
666 } else /* Let MMI library update carrier status */
667 korina_check_media(mii->dev, 0);
668}
669
670static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
671{
672 struct korina_private *lp = netdev_priv(dev);
673 struct mii_ioctl_data *data = if_mii(rq);
674 int rc;
675
676 if (!netif_running(dev))
677 return -EINVAL;
678 spin_lock_irq(&lp->lock);
679 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
680 spin_unlock_irq(&lp->lock);
681 korina_set_carrier(&lp->mii_if);
682
683 return rc;
684}
685
686/* ethtool helpers */
687static void netdev_get_drvinfo(struct net_device *dev,
688 struct ethtool_drvinfo *info)
689{
690 struct korina_private *lp = netdev_priv(dev);
691
Jiri Pirko7826d432013-01-06 00:44:26 +0000692 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
693 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
694 strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info));
Florian Fainellief112912008-03-19 17:14:51 +0100695}
696
Philippe Reynesaf473682017-01-14 12:33:19 +0100697static int netdev_get_link_ksettings(struct net_device *dev,
698 struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100699{
700 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100701
702 spin_lock_irq(&lp->lock);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300703 mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100704 spin_unlock_irq(&lp->lock);
705
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300706 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100707}
708
Philippe Reynesaf473682017-01-14 12:33:19 +0100709static int netdev_set_link_ksettings(struct net_device *dev,
710 const struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100711{
712 struct korina_private *lp = netdev_priv(dev);
713 int rc;
714
715 spin_lock_irq(&lp->lock);
Philippe Reynesaf473682017-01-14 12:33:19 +0100716 rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100717 spin_unlock_irq(&lp->lock);
718 korina_set_carrier(&lp->mii_if);
719
720 return rc;
721}
722
723static u32 netdev_get_link(struct net_device *dev)
724{
725 struct korina_private *lp = netdev_priv(dev);
726
727 return mii_link_ok(&lp->mii_if);
728}
729
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700730static const struct ethtool_ops netdev_ethtool_ops = {
Florian Fainellief112912008-03-19 17:14:51 +0100731 .get_drvinfo = netdev_get_drvinfo,
Florian Fainellief112912008-03-19 17:14:51 +0100732 .get_link = netdev_get_link,
Philippe Reynesaf473682017-01-14 12:33:19 +0100733 .get_link_ksettings = netdev_get_link_ksettings,
734 .set_link_ksettings = netdev_set_link_ksettings,
Florian Fainellief112912008-03-19 17:14:51 +0100735};
736
Phil Sutter70108372009-08-12 12:52:32 +0000737static int korina_alloc_ring(struct net_device *dev)
Florian Fainellief112912008-03-19 17:14:51 +0100738{
739 struct korina_private *lp = netdev_priv(dev);
Phil Suttere85bf472009-01-15 12:29:57 +0000740 struct sk_buff *skb;
Florian Fainellief112912008-03-19 17:14:51 +0100741 int i;
742
743 /* Initialize the transmit descriptors */
744 for (i = 0; i < KORINA_NUM_TDS; i++) {
745 lp->td_ring[i].control = DMA_DESC_IOF;
746 lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
747 lp->td_ring[i].ca = 0;
748 lp->td_ring[i].link = 0;
749 }
750 lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
751 lp->tx_full = lp->tx_count = 0;
752 lp->tx_chain_status = desc_empty;
753
754 /* Initialize the receive descriptors */
755 for (i = 0; i < KORINA_NUM_RDS; i++) {
Phil Sutter53ee4902010-05-29 13:23:35 +0000756 skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
Florian Fainellief112912008-03-19 17:14:51 +0100757 if (!skb)
Phil Sutter70108372009-08-12 12:52:32 +0000758 return -ENOMEM;
Florian Fainellief112912008-03-19 17:14:51 +0100759 lp->rx_skb[i] = skb;
760 lp->rd_ring[i].control = DMA_DESC_IOD |
761 DMA_COUNT(KORINA_RBSIZE);
762 lp->rd_ring[i].devcs = 0;
763 lp->rd_ring[i].ca = CPHYSADDR(skb->data);
764 lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
765 }
766
Phil Sutter6a2fe982009-01-15 12:29:55 +0000767 /* loop back receive descriptors, so the last
768 * descriptor points to the first one */
769 lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
770 lp->rd_ring[i - 1].control |= DMA_DESC_COD;
Florian Fainellief112912008-03-19 17:14:51 +0100771
Phil Sutter6a2fe982009-01-15 12:29:55 +0000772 lp->rx_next_done = 0;
Florian Fainellief112912008-03-19 17:14:51 +0100773 lp->rx_chain_head = 0;
774 lp->rx_chain_tail = 0;
775 lp->rx_chain_status = desc_empty;
Phil Sutter70108372009-08-12 12:52:32 +0000776
777 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100778}
779
780static void korina_free_ring(struct net_device *dev)
781{
782 struct korina_private *lp = netdev_priv(dev);
783 int i;
784
785 for (i = 0; i < KORINA_NUM_RDS; i++) {
786 lp->rd_ring[i].control = 0;
787 if (lp->rx_skb[i])
788 dev_kfree_skb_any(lp->rx_skb[i]);
789 lp->rx_skb[i] = NULL;
790 }
791
792 for (i = 0; i < KORINA_NUM_TDS; i++) {
793 lp->td_ring[i].control = 0;
794 if (lp->tx_skb[i])
795 dev_kfree_skb_any(lp->tx_skb[i]);
796 lp->tx_skb[i] = NULL;
797 }
798}
799
800/*
801 * Initialize the RC32434 ethernet controller.
802 */
803static int korina_init(struct net_device *dev)
804{
805 struct korina_private *lp = netdev_priv(dev);
806
807 /* Disable DMA */
808 korina_abort_tx(dev);
809 korina_abort_rx(dev);
810
811 /* reset ethernet logic */
812 writel(0, &lp->eth_regs->ethintfc);
813 while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
Florian Westphal860e9532016-05-03 16:33:13 +0200814 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100815
816 /* Enable Ethernet Interface */
817 writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
818
819 /* Allocate rings */
Phil Sutter70108372009-08-12 12:52:32 +0000820 if (korina_alloc_ring(dev)) {
821 printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
822 korina_free_ring(dev);
823 return -ENOMEM;
824 }
Florian Fainellief112912008-03-19 17:14:51 +0100825
826 writel(0, &lp->rx_dma_regs->dmas);
827 /* Start Rx DMA */
828 korina_start_rx(lp, &lp->rd_ring[0]);
829
830 writel(readl(&lp->tx_dma_regs->dmasm) &
831 ~(DMA_STAT_FINI | DMA_STAT_ERR),
832 &lp->tx_dma_regs->dmasm);
833 writel(readl(&lp->rx_dma_regs->dmasm) &
834 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
835 &lp->rx_dma_regs->dmasm);
836
837 /* Accept only packets destined for this Ethernet device address */
838 writel(ETH_ARC_AB, &lp->eth_regs->etharc);
839
840 /* Set all Ether station address registers to their initial values */
841 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
842 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
843
844 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
845 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
846
847 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
848 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
849
850 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
851 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
852
853
854 /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
855 writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
856 &lp->eth_regs->ethmac2);
857
858 /* Back to back inter-packet-gap */
859 writel(0x15, &lp->eth_regs->ethipgt);
860 /* Non - Back to back inter-packet-gap */
861 writel(0x12, &lp->eth_regs->ethipgr);
862
863 /* Management Clock Prescaler Divisor
864 * Clock independent setting */
865 writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
866 &lp->eth_regs->ethmcp);
867
868 /* don't transmit until fifo contains 48b */
869 writel(48, &lp->eth_regs->ethfifott);
870
871 writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
872
873 napi_enable(&lp->napi);
874 netif_start_queue(dev);
875
876 return 0;
877}
878
879/*
880 * Restart the RC32434 ethernet controller.
Florian Fainellief112912008-03-19 17:14:51 +0100881 */
Phil Sutterceb3d232010-05-29 13:23:34 +0000882static void korina_restart_task(struct work_struct *work)
Florian Fainellief112912008-03-19 17:14:51 +0100883{
Phil Sutterceb3d232010-05-29 13:23:34 +0000884 struct korina_private *lp = container_of(work,
885 struct korina_private, restart_task);
886 struct net_device *dev = lp->dev;
Florian Fainellief112912008-03-19 17:14:51 +0100887
888 /*
889 * Disable interrupts
890 */
891 disable_irq(lp->rx_irq);
892 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100893
894 writel(readl(&lp->tx_dma_regs->dmasm) |
895 DMA_STAT_FINI | DMA_STAT_ERR,
896 &lp->tx_dma_regs->dmasm);
897 writel(readl(&lp->rx_dma_regs->dmasm) |
898 DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
899 &lp->rx_dma_regs->dmasm);
900
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800901 napi_disable(&lp->napi);
902
Florian Fainellie6afb1a2016-12-23 19:56:56 -0800903 korina_free_ring(dev);
904
Phil Sutterceb3d232010-05-29 13:23:34 +0000905 if (korina_init(dev) < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000906 printk(KERN_ERR "%s: cannot restart device\n", dev->name);
Phil Sutterceb3d232010-05-29 13:23:34 +0000907 return;
Florian Fainellief112912008-03-19 17:14:51 +0100908 }
909 korina_multicast_list(dev);
910
Florian Fainellief112912008-03-19 17:14:51 +0100911 enable_irq(lp->tx_irq);
912 enable_irq(lp->rx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100913}
914
Florian Fainellief112912008-03-19 17:14:51 +0100915static void korina_tx_timeout(struct net_device *dev)
916{
917 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100918
Phil Sutterceb3d232010-05-29 13:23:34 +0000919 schedule_work(&lp->restart_task);
Florian Fainellief112912008-03-19 17:14:51 +0100920}
921
Florian Fainellief112912008-03-19 17:14:51 +0100922#ifdef CONFIG_NET_POLL_CONTROLLER
923static void korina_poll_controller(struct net_device *dev)
924{
925 disable_irq(dev->irq);
926 korina_tx_dma_interrupt(dev->irq, dev);
927 enable_irq(dev->irq);
928}
929#endif
930
931static int korina_open(struct net_device *dev)
932{
933 struct korina_private *lp = netdev_priv(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +0200934 int ret;
Florian Fainellief112912008-03-19 17:14:51 +0100935
936 /* Initialize */
937 ret = korina_init(dev);
938 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000939 printk(KERN_ERR "%s: cannot open device\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100940 goto out;
941 }
942
943 /* Install the interrupt handler
Roman Yeryomin7ce103b2017-09-17 20:24:15 +0300944 * that handles the Done Finished */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800945 ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200946 0, "Korina ethernet Rx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100947 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000948 printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
Florian Fainellief112912008-03-19 17:14:51 +0100949 dev->name, lp->rx_irq);
950 goto err_release;
951 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800952 ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200953 0, "Korina ethernet Tx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100954 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000955 printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
Florian Fainellief112912008-03-19 17:14:51 +0100956 dev->name, lp->tx_irq);
957 goto err_free_rx_irq;
958 }
959
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000960 mod_timer(&lp->media_check_timer, jiffies + 1);
Francois Romieu751c2e42008-04-20 18:05:31 +0200961out:
962 return ret;
Florian Fainellief112912008-03-19 17:14:51 +0100963
Florian Fainellief112912008-03-19 17:14:51 +0100964err_free_rx_irq:
965 free_irq(lp->rx_irq, dev);
966err_release:
967 korina_free_ring(dev);
968 goto out;
Florian Fainellief112912008-03-19 17:14:51 +0100969}
970
971static int korina_close(struct net_device *dev)
972{
973 struct korina_private *lp = netdev_priv(dev);
974 u32 tmp;
975
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000976 del_timer(&lp->media_check_timer);
977
Florian Fainellief112912008-03-19 17:14:51 +0100978 /* Disable interrupts */
979 disable_irq(lp->rx_irq);
980 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100981
982 korina_abort_tx(dev);
983 tmp = readl(&lp->tx_dma_regs->dmasm);
984 tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
985 writel(tmp, &lp->tx_dma_regs->dmasm);
986
987 korina_abort_rx(dev);
988 tmp = readl(&lp->rx_dma_regs->dmasm);
989 tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
990 writel(tmp, &lp->rx_dma_regs->dmasm);
991
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800992 napi_disable(&lp->napi);
993
Phil Sutterceb3d232010-05-29 13:23:34 +0000994 cancel_work_sync(&lp->restart_task);
995
Florian Fainellie6afb1a2016-12-23 19:56:56 -0800996 korina_free_ring(dev);
997
Florian Fainellief112912008-03-19 17:14:51 +0100998 free_irq(lp->rx_irq, dev);
999 free_irq(lp->tx_irq, dev);
Florian Fainellief112912008-03-19 17:14:51 +01001000
1001 return 0;
1002}
1003
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001004static const struct net_device_ops korina_netdev_ops = {
1005 .ndo_open = korina_open,
1006 .ndo_stop = korina_close,
1007 .ndo_start_xmit = korina_send_packet,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001008 .ndo_set_rx_mode = korina_multicast_list,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001009 .ndo_tx_timeout = korina_tx_timeout,
1010 .ndo_do_ioctl = korina_ioctl,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001011 .ndo_validate_addr = eth_validate_addr,
1012 .ndo_set_mac_address = eth_mac_addr,
1013#ifdef CONFIG_NET_POLL_CONTROLLER
1014 .ndo_poll_controller = korina_poll_controller,
1015#endif
1016};
1017
Florian Fainellief112912008-03-19 17:14:51 +01001018static int korina_probe(struct platform_device *pdev)
1019{
1020 struct korina_device *bif = platform_get_drvdata(pdev);
1021 struct korina_private *lp;
1022 struct net_device *dev;
1023 struct resource *r;
Francois Romieue3152ab2008-04-20 18:06:13 +02001024 int rc;
Florian Fainellief112912008-03-19 17:14:51 +01001025
1026 dev = alloc_etherdev(sizeof(struct korina_private));
Joe Perches41de8d42012-01-29 13:47:52 +00001027 if (!dev)
Florian Fainellief112912008-03-19 17:14:51 +01001028 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001029
Florian Fainellief112912008-03-19 17:14:51 +01001030 SET_NETDEV_DEV(dev, &pdev->dev);
Florian Fainellief112912008-03-19 17:14:51 +01001031 lp = netdev_priv(dev);
1032
1033 bif->dev = dev;
Joe Perchesd458cdf2013-10-01 19:04:40 -07001034 memcpy(dev->dev_addr, bif->mac, ETH_ALEN);
Florian Fainellief112912008-03-19 17:14:51 +01001035
1036 lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
1037 lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
Florian Fainellief112912008-03-19 17:14:51 +01001038
1039 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
1040 dev->base_addr = r->start;
Dan Carpenter38013262010-03-22 02:11:45 +00001041 lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001042 if (!lp->eth_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001043 printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001044 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001045 goto probe_err_out;
1046 }
1047
1048 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
Dan Carpenter38013262010-03-22 02:11:45 +00001049 lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001050 if (!lp->rx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001051 printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001052 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001053 goto probe_err_dma_rx;
1054 }
1055
1056 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
Dan Carpenter38013262010-03-22 02:11:45 +00001057 lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001058 if (!lp->tx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001059 printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001060 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001061 goto probe_err_dma_tx;
1062 }
1063
1064 lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
1065 if (!lp->td_ring) {
Francois Romieue3152ab2008-04-20 18:06:13 +02001066 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001067 goto probe_err_td_ring;
1068 }
1069
1070 dma_cache_inv((unsigned long)(lp->td_ring),
1071 TD_RING_SIZE + RD_RING_SIZE);
1072
1073 /* now convert TD_RING pointer to KSEG1 */
1074 lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
1075 lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
1076
1077 spin_lock_init(&lp->lock);
1078 /* just use the rx dma irq */
1079 dev->irq = lp->rx_irq;
1080 lp->dev = dev;
1081
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001082 dev->netdev_ops = &korina_netdev_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001083 dev->ethtool_ops = &netdev_ethtool_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001084 dev->watchdog_timeo = TX_TIMEOUT;
Roman Yeryomind609d282017-09-17 20:24:38 +03001085 netif_napi_add(dev, &lp->napi, korina_poll, NAPI_POLL_WEIGHT);
Florian Fainellief112912008-03-19 17:14:51 +01001086
1087 lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
1088 lp->mii_if.dev = dev;
1089 lp->mii_if.mdio_read = mdio_read;
1090 lp->mii_if.mdio_write = mdio_write;
1091 lp->mii_if.phy_id = lp->phy_addr;
1092 lp->mii_if.phy_id_mask = 0x1f;
1093 lp->mii_if.reg_num_mask = 0x1f;
1094
Francois Romieue3152ab2008-04-20 18:06:13 +02001095 rc = register_netdev(dev);
1096 if (rc < 0) {
Florian Fainellief112912008-03-19 17:14:51 +01001097 printk(KERN_ERR DRV_NAME
Phil Sutterf16aea42009-08-12 12:22:46 +00001098 ": cannot register net device: %d\n", rc);
Florian Fainellief112912008-03-19 17:14:51 +01001099 goto probe_err_register;
1100 }
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +00001101 setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev);
Phil Sutterf16aea42009-08-12 12:22:46 +00001102
Phil Sutterceb3d232010-05-29 13:23:34 +00001103 INIT_WORK(&lp->restart_task, korina_restart_task);
1104
Phil Sutterf16aea42009-08-12 12:22:46 +00001105 printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
1106 dev->name);
Francois Romieue3152ab2008-04-20 18:06:13 +02001107out:
1108 return rc;
Florian Fainellief112912008-03-19 17:14:51 +01001109
1110probe_err_register:
1111 kfree(lp->td_ring);
1112probe_err_td_ring:
1113 iounmap(lp->tx_dma_regs);
1114probe_err_dma_tx:
1115 iounmap(lp->rx_dma_regs);
1116probe_err_dma_rx:
1117 iounmap(lp->eth_regs);
1118probe_err_out:
1119 free_netdev(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +02001120 goto out;
Florian Fainellief112912008-03-19 17:14:51 +01001121}
1122
1123static int korina_remove(struct platform_device *pdev)
1124{
1125 struct korina_device *bif = platform_get_drvdata(pdev);
1126 struct korina_private *lp = netdev_priv(bif->dev);
1127
Francois Romieue3152ab2008-04-20 18:06:13 +02001128 iounmap(lp->eth_regs);
1129 iounmap(lp->rx_dma_regs);
1130 iounmap(lp->tx_dma_regs);
Florian Fainellief112912008-03-19 17:14:51 +01001131
Florian Fainellief112912008-03-19 17:14:51 +01001132 unregister_netdev(bif->dev);
1133 free_netdev(bif->dev);
1134
1135 return 0;
1136}
1137
1138static struct platform_driver korina_driver = {
1139 .driver.name = "korina",
1140 .probe = korina_probe,
1141 .remove = korina_remove,
1142};
1143
Axel Lindb62f682011-11-27 16:44:17 +00001144module_platform_driver(korina_driver);
Florian Fainellief112912008-03-19 17:14:51 +01001145
1146MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
1147MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1148MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
1149MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
1150MODULE_LICENSE("GPL");