blob: c26f0d84ba6b802b09e6d0cdf434544c9befb288 [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
Roman Yeryomin2e5396b2017-09-17 20:25:02 +030067#define DRV_NAME "korina"
68#define DRV_VERSION "0.10"
69#define DRV_RELDATE "04Mar2008"
Florian Fainellief112912008-03-19 17:14:51 +010070
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
Roman Yeryomin2e5396b2017-09-17 20:25:02 +030078#define MII_CLOCK 1250000 /* no more than 2.5MHz */
Florian Fainellief112912008-03-19 17:14:51 +010079
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)
Roman Yeryomin2e5396b2017-09-17 20:25:02 +030090#define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc))
Florian Fainellief112912008-03-19 17:14:51 +010091#define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc))
92
Roman Yeryomin2e5396b2017-09-17 20:25:02 +030093#define TX_TIMEOUT (6000 * HZ / 1000)
Florian Fainellief112912008-03-19 17:14:51 +010094
Roman Yeryomin2e5396b2017-09-17 20:25:02 +030095enum chain_status {
96 desc_filled,
97 desc_empty
98};
99
100#define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0)
101#define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0)
102#define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
Florian Fainellief112912008-03-19 17:14:51 +0100103
104/* Information that need to be kept for each board. */
105struct korina_private {
106 struct eth_regs *eth_regs;
107 struct dma_reg *rx_dma_regs;
108 struct dma_reg *tx_dma_regs;
109 struct dma_desc *td_ring; /* transmit descriptor ring */
110 struct dma_desc *rd_ring; /* receive descriptor ring */
111
112 struct sk_buff *tx_skb[KORINA_NUM_TDS];
113 struct sk_buff *rx_skb[KORINA_NUM_RDS];
114
115 int rx_next_done;
116 int rx_chain_head;
117 int rx_chain_tail;
118 enum chain_status rx_chain_status;
119
120 int tx_next_done;
121 int tx_chain_head;
122 int tx_chain_tail;
123 enum chain_status tx_chain_status;
124 int tx_count;
125 int tx_full;
126
127 int rx_irq;
128 int tx_irq;
Florian Fainellief112912008-03-19 17:14:51 +0100129
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300130 spinlock_t lock; /* NIC xmit lock */
Florian Fainellief112912008-03-19 17:14:51 +0100131
132 int dma_halt_cnt;
133 int dma_run_cnt;
134 struct napi_struct napi;
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000135 struct timer_list media_check_timer;
Florian Fainellief112912008-03-19 17:14:51 +0100136 struct mii_if_info mii_if;
Phil Sutterceb3d232010-05-29 13:23:34 +0000137 struct work_struct restart_task;
Florian Fainellief112912008-03-19 17:14:51 +0100138 struct net_device *dev;
139 int phy_addr;
140};
141
142extern unsigned int idt_cpu_freq;
143
144static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr)
145{
146 writel(0, &ch->dmandptr);
147 writel(dma_addr, &ch->dmadptr);
148}
149
150static inline void korina_abort_dma(struct net_device *dev,
151 struct dma_reg *ch)
152{
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300153 if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
154 writel(0x10, &ch->dmac);
Florian Fainellief112912008-03-19 17:14:51 +0100155
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300156 while (!(readl(&ch->dmas) & DMA_STAT_HALT))
157 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100158
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300159 writel(0, &ch->dmas);
160 }
Florian Fainellief112912008-03-19 17:14:51 +0100161
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300162 writel(0, &ch->dmadptr);
163 writel(0, &ch->dmandptr);
Florian Fainellief112912008-03-19 17:14:51 +0100164}
165
166static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr)
167{
168 writel(dma_addr, &ch->dmandptr);
169}
170
171static void korina_abort_tx(struct net_device *dev)
172{
173 struct korina_private *lp = netdev_priv(dev);
174
175 korina_abort_dma(dev, lp->tx_dma_regs);
176}
177
178static void korina_abort_rx(struct net_device *dev)
179{
180 struct korina_private *lp = netdev_priv(dev);
181
182 korina_abort_dma(dev, lp->rx_dma_regs);
183}
184
185static void korina_start_rx(struct korina_private *lp,
186 struct dma_desc *rd)
187{
188 korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd));
189}
190
191static void korina_chain_rx(struct korina_private *lp,
192 struct dma_desc *rd)
193{
194 korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd));
195}
196
197/* transmit packet */
198static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
199{
200 struct korina_private *lp = netdev_priv(dev);
201 unsigned long flags;
202 u32 length;
Phil Sutter97bc4772009-01-14 21:50:41 -0800203 u32 chain_prev, chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100204 struct dma_desc *td;
205
206 spin_lock_irqsave(&lp->lock, flags);
207
208 td = &lp->td_ring[lp->tx_chain_tail];
209
210 /* stop queue when full, drop pkts if queue already full */
211 if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
212 lp->tx_full = 1;
213
214 if (lp->tx_count == (KORINA_NUM_TDS - 2))
215 netif_stop_queue(dev);
216 else {
217 dev->stats.tx_dropped++;
218 dev_kfree_skb_any(skb);
219 spin_unlock_irqrestore(&lp->lock, flags);
220
221 return NETDEV_TX_BUSY;
222 }
223 }
224
225 lp->tx_count++;
226
227 lp->tx_skb[lp->tx_chain_tail] = skb;
228
229 length = skb->len;
230 dma_cache_wback((u32)skb->data, skb->len);
231
232 /* Setup the transmit descriptor. */
233 dma_cache_inv((u32) td, sizeof(*td));
234 td->ca = CPHYSADDR(skb->data);
Phil Sutter97bc4772009-01-14 21:50:41 -0800235 chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK;
236 chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK;
Florian Fainellief112912008-03-19 17:14:51 +0100237
238 if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
239 if (lp->tx_chain_status == desc_empty) {
240 /* Update tail */
241 td->control = DMA_COUNT(length) |
242 DMA_DESC_COF | DMA_DESC_IOF;
243 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800244 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100245 /* Write to NDPTR */
246 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
247 &lp->tx_dma_regs->dmandptr);
248 /* Move head to tail */
249 lp->tx_chain_head = lp->tx_chain_tail;
250 } else {
251 /* Update tail */
252 td->control = DMA_COUNT(length) |
253 DMA_DESC_COF | DMA_DESC_IOF;
254 /* Link to prev */
Phil Sutter97bc4772009-01-14 21:50:41 -0800255 lp->td_ring[chain_prev].control &=
Florian Fainellief112912008-03-19 17:14:51 +0100256 ~DMA_DESC_COF;
257 /* Link to prev */
Phil Sutter97bc4772009-01-14 21:50:41 -0800258 lp->td_ring[chain_prev].link = CPHYSADDR(td);
Florian Fainellief112912008-03-19 17:14:51 +0100259 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800260 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100261 /* Write to NDPTR */
262 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
263 &(lp->tx_dma_regs->dmandptr));
264 /* Move head to tail */
265 lp->tx_chain_head = lp->tx_chain_tail;
266 lp->tx_chain_status = desc_empty;
267 }
268 } else {
269 if (lp->tx_chain_status == desc_empty) {
270 /* Update tail */
271 td->control = DMA_COUNT(length) |
272 DMA_DESC_COF | DMA_DESC_IOF;
273 /* Move tail */
Phil Sutter97bc4772009-01-14 21:50:41 -0800274 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100275 lp->tx_chain_status = desc_filled;
Florian Fainellief112912008-03-19 17:14:51 +0100276 } else {
277 /* Update tail */
278 td->control = DMA_COUNT(length) |
279 DMA_DESC_COF | DMA_DESC_IOF;
Phil Sutter97bc4772009-01-14 21:50:41 -0800280 lp->td_ring[chain_prev].control &=
Florian Fainellief112912008-03-19 17:14:51 +0100281 ~DMA_DESC_COF;
Phil Sutter97bc4772009-01-14 21:50:41 -0800282 lp->td_ring[chain_prev].link = CPHYSADDR(td);
283 lp->tx_chain_tail = chain_next;
Florian Fainellief112912008-03-19 17:14:51 +0100284 }
285 }
286 dma_cache_wback((u32) td, sizeof(*td));
287
Florian Westphal860e9532016-05-03 16:33:13 +0200288 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100289 spin_unlock_irqrestore(&lp->lock, flags);
290
291 return NETDEV_TX_OK;
292}
293
294static int mdio_read(struct net_device *dev, int mii_id, int reg)
295{
296 struct korina_private *lp = netdev_priv(dev);
297 int ret;
298
299 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
300
301 writel(0, &lp->eth_regs->miimcfg);
302 writel(0, &lp->eth_regs->miimcmd);
303 writel(mii_id | reg, &lp->eth_regs->miimaddr);
304 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
305
306 ret = (int)(readl(&lp->eth_regs->miimrdd));
307 return ret;
308}
309
310static void mdio_write(struct net_device *dev, int mii_id, int reg, int val)
311{
312 struct korina_private *lp = netdev_priv(dev);
313
314 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
315
316 writel(0, &lp->eth_regs->miimcfg);
317 writel(1, &lp->eth_regs->miimcmd);
318 writel(mii_id | reg, &lp->eth_regs->miimaddr);
319 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
320 writel(val, &lp->eth_regs->miimwtd);
321}
322
323/* Ethernet Rx DMA interrupt */
324static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
325{
326 struct net_device *dev = dev_id;
327 struct korina_private *lp = netdev_priv(dev);
328 u32 dmas, dmasm;
329 irqreturn_t retval;
330
331 dmas = readl(&lp->rx_dma_regs->dmas);
332 if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
Florian Fainellief112912008-03-19 17:14:51 +0100333 dmasm = readl(&lp->rx_dma_regs->dmasm);
334 writel(dmasm | (DMA_STAT_DONE |
335 DMA_STAT_HALT | DMA_STAT_ERR),
336 &lp->rx_dma_regs->dmasm);
337
Ben Hutchings288379f2009-01-19 16:43:59 -0800338 napi_schedule(&lp->napi);
Phil Sutter60d3f982009-01-14 21:50:12 -0800339
Florian Fainellief112912008-03-19 17:14:51 +0100340 if (dmas & DMA_STAT_ERR)
Phil Sutterf16aea42009-08-12 12:22:46 +0000341 printk(KERN_ERR "%s: DMA error\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100342
343 retval = IRQ_HANDLED;
344 } else
345 retval = IRQ_NONE;
346
347 return retval;
348}
349
350static int korina_rx(struct net_device *dev, int limit)
351{
352 struct korina_private *lp = netdev_priv(dev);
353 struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
354 struct sk_buff *skb, *skb_new;
355 u8 *pkt_buf;
Phil Sutter4cf83b62009-01-14 21:48:59 -0800356 u32 devcs, pkt_len, dmas;
Florian Fainellief112912008-03-19 17:14:51 +0100357 int count;
358
359 dma_cache_inv((u32)rd, sizeof(*rd));
360
361 for (count = 0; count < limit; count++) {
Phil Sutter4cf83b62009-01-14 21:48:59 -0800362 skb = lp->rx_skb[lp->rx_next_done];
363 skb_new = NULL;
Florian Fainellief112912008-03-19 17:14:51 +0100364
365 devcs = rd->devcs;
366
Phil Sutter4cf83b62009-01-14 21:48:59 -0800367 if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0)
368 break;
369
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300370 /* check that this is a whole packet
371 * WARNING: DMA_FD bit incorrectly set
372 * in Rc32434 (errata ref #077) */
373 if (!(devcs & ETH_RX_LD))
374 goto next;
375
376 if (!(devcs & ETH_RX_ROK)) {
377 /* Update statistics counters */
378 dev->stats.rx_errors++;
379 dev->stats.rx_dropped++;
380 if (devcs & ETH_RX_CRC)
381 dev->stats.rx_crc_errors++;
382 if (devcs & ETH_RX_LE)
383 dev->stats.rx_length_errors++;
384 if (devcs & ETH_RX_OVR)
385 dev->stats.rx_fifo_errors++;
386 if (devcs & ETH_RX_CV)
387 dev->stats.rx_frame_errors++;
388 if (devcs & ETH_RX_CES)
389 dev->stats.rx_frame_errors++;
390
391 goto next;
392 }
393
394 pkt_len = RCVPKT_LENGTH(devcs);
395
396 /* must be the (first and) last
397 * descriptor then */
398 pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
399
400 /* invalidate the cache */
401 dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
402
403 /* Malloc up new buffer. */
404 skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
405
406 if (!skb_new)
407 break;
408 /* Do not count the CRC */
409 skb_put(skb, pkt_len - 4);
410 skb->protocol = eth_type_trans(skb, dev);
411
412 /* Pass the packet to upper layers */
Roman Yeryomin247c78f2017-09-17 20:24:50 +0300413 napi_gro_receive(&lp->napi, skb);
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300414 dev->stats.rx_packets++;
415 dev->stats.rx_bytes += pkt_len;
416
417 /* Update the mcast stats */
Florian Fainellief112912008-03-19 17:14:51 +0100418 if (devcs & ETH_RX_MP)
419 dev->stats.multicast++;
420
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300421 lp->rx_skb[lp->rx_next_done] = skb_new;
Florian Fainellief112912008-03-19 17:14:51 +0100422
Roman Yeryomin364a97f2017-09-17 20:24:26 +0300423next:
Phil Sutter4cf83b62009-01-14 21:48:59 -0800424 rd->devcs = 0;
425
426 /* Restore descriptor's curr_addr */
427 if (skb_new)
428 rd->ca = CPHYSADDR(skb_new->data);
429 else
430 rd->ca = CPHYSADDR(skb->data);
431
432 rd->control = DMA_COUNT(KORINA_RBSIZE) |
433 DMA_DESC_COD | DMA_DESC_IOD;
434 lp->rd_ring[(lp->rx_next_done - 1) &
435 KORINA_RDS_MASK].control &=
436 ~DMA_DESC_COD;
437
438 lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
439 dma_cache_wback((u32)rd, sizeof(*rd));
440 rd = &lp->rd_ring[lp->rx_next_done];
441 writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
Florian Fainellief112912008-03-19 17:14:51 +0100442 }
443
444 dmas = readl(&lp->rx_dma_regs->dmas);
445
446 if (dmas & DMA_STAT_HALT) {
447 writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
448 &lp->rx_dma_regs->dmas);
449
450 lp->dma_halt_cnt++;
451 rd->devcs = 0;
452 skb = lp->rx_skb[lp->rx_next_done];
453 rd->ca = CPHYSADDR(skb->data);
454 dma_cache_wback((u32)rd, sizeof(*rd));
455 korina_chain_rx(lp, rd);
456 }
457
458 return count;
459}
460
461static int korina_poll(struct napi_struct *napi, int budget)
462{
463 struct korina_private *lp =
464 container_of(napi, struct korina_private, napi);
465 struct net_device *dev = lp->dev;
466 int work_done;
467
468 work_done = korina_rx(dev, budget);
469 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800470 napi_complete_done(napi, work_done);
Florian Fainellief112912008-03-19 17:14:51 +0100471
472 writel(readl(&lp->rx_dma_regs->dmasm) &
473 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
474 &lp->rx_dma_regs->dmasm);
475 }
476 return work_done;
477}
478
479/*
480 * Set or clear the multicast filter for this adaptor.
481 */
482static void korina_multicast_list(struct net_device *dev)
483{
484 struct korina_private *lp = netdev_priv(dev);
485 unsigned long flags;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000486 struct netdev_hw_addr *ha;
Florian Fainellief112912008-03-19 17:14:51 +0100487 u32 recognise = ETH_ARC_AB; /* always accept broadcasts */
Florian Fainellief112912008-03-19 17:14:51 +0100488
489 /* Set promiscuous mode */
490 if (dev->flags & IFF_PROMISC)
491 recognise |= ETH_ARC_PRO;
492
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000493 else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
Florian Fainellief112912008-03-19 17:14:51 +0100494 /* All multicast and broadcast */
495 recognise |= ETH_ARC_AM;
496
497 /* Build the hash table */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000498 if (netdev_mc_count(dev) > 4) {
Emilio Lópeze998fd42013-05-17 10:42:56 +0000499 u16 hash_table[4] = { 0 };
Florian Fainellief112912008-03-19 17:14:51 +0100500 u32 crc;
501
Jiri Pirko22bedad32010-04-01 21:22:57 +0000502 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +0000503 crc = ether_crc_le(6, ha->addr);
Florian Fainellief112912008-03-19 17:14:51 +0100504 crc >>= 26;
505 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
506 }
507 /* Accept filtered multicast */
508 recognise |= ETH_ARC_AFM;
509
510 /* Fill the MAC hash tables with their values */
511 writel((u32)(hash_table[1] << 16 | hash_table[0]),
512 &lp->eth_regs->ethhash0);
513 writel((u32)(hash_table[3] << 16 | hash_table[2]),
514 &lp->eth_regs->ethhash1);
515 }
516
517 spin_lock_irqsave(&lp->lock, flags);
518 writel(recognise, &lp->eth_regs->etharc);
519 spin_unlock_irqrestore(&lp->lock, flags);
520}
521
522static void korina_tx(struct net_device *dev)
523{
524 struct korina_private *lp = netdev_priv(dev);
525 struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
526 u32 devcs;
527 u32 dmas;
528
529 spin_lock(&lp->lock);
530
531 /* Process all desc that are done */
532 while (IS_DMA_FINISHED(td->control)) {
533 if (lp->tx_full == 1) {
534 netif_wake_queue(dev);
535 lp->tx_full = 0;
536 }
537
538 devcs = lp->td_ring[lp->tx_next_done].devcs;
539 if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
540 (ETH_TX_FD | ETH_TX_LD)) {
541 dev->stats.tx_errors++;
542 dev->stats.tx_dropped++;
543
544 /* Should never happen */
Phil Sutterf16aea42009-08-12 12:22:46 +0000545 printk(KERN_ERR "%s: split tx ignored\n",
Florian Fainellief112912008-03-19 17:14:51 +0100546 dev->name);
547 } else if (devcs & ETH_TX_TOK) {
548 dev->stats.tx_packets++;
549 dev->stats.tx_bytes +=
550 lp->tx_skb[lp->tx_next_done]->len;
551 } else {
552 dev->stats.tx_errors++;
553 dev->stats.tx_dropped++;
554
555 /* Underflow */
556 if (devcs & ETH_TX_UND)
557 dev->stats.tx_fifo_errors++;
558
559 /* Oversized frame */
560 if (devcs & ETH_TX_OF)
561 dev->stats.tx_aborted_errors++;
562
563 /* Excessive deferrals */
564 if (devcs & ETH_TX_ED)
565 dev->stats.tx_carrier_errors++;
566
567 /* Collisions: medium busy */
568 if (devcs & ETH_TX_EC)
569 dev->stats.collisions++;
570
571 /* Late collision */
572 if (devcs & ETH_TX_LC)
573 dev->stats.tx_window_errors++;
574 }
575
576 /* We must always free the original skb */
577 if (lp->tx_skb[lp->tx_next_done]) {
578 dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
579 lp->tx_skb[lp->tx_next_done] = NULL;
580 }
581
582 lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
583 lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
584 lp->td_ring[lp->tx_next_done].link = 0;
585 lp->td_ring[lp->tx_next_done].ca = 0;
586 lp->tx_count--;
587
588 /* Go on to next transmission */
589 lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
590 td = &lp->td_ring[lp->tx_next_done];
591
592 }
593
594 /* Clear the DMA status register */
595 dmas = readl(&lp->tx_dma_regs->dmas);
596 writel(~dmas, &lp->tx_dma_regs->dmas);
597
598 writel(readl(&lp->tx_dma_regs->dmasm) &
599 ~(DMA_STAT_FINI | DMA_STAT_ERR),
600 &lp->tx_dma_regs->dmasm);
601
602 spin_unlock(&lp->lock);
603}
604
605static irqreturn_t
606korina_tx_dma_interrupt(int irq, void *dev_id)
607{
608 struct net_device *dev = dev_id;
609 struct korina_private *lp = netdev_priv(dev);
610 u32 dmas, dmasm;
611 irqreturn_t retval;
612
613 dmas = readl(&lp->tx_dma_regs->dmas);
614
615 if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
Florian Fainellief112912008-03-19 17:14:51 +0100616 dmasm = readl(&lp->tx_dma_regs->dmasm);
617 writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
618 &lp->tx_dma_regs->dmasm);
619
Phil Sutter60d3f982009-01-14 21:50:12 -0800620 korina_tx(dev);
621
Florian Fainellief112912008-03-19 17:14:51 +0100622 if (lp->tx_chain_status == desc_filled &&
623 (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
624 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
625 &(lp->tx_dma_regs->dmandptr));
626 lp->tx_chain_status = desc_empty;
627 lp->tx_chain_head = lp->tx_chain_tail;
Florian Westphal860e9532016-05-03 16:33:13 +0200628 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100629 }
630 if (dmas & DMA_STAT_ERR)
Phil Sutterf16aea42009-08-12 12:22:46 +0000631 printk(KERN_ERR "%s: DMA error\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100632
633 retval = IRQ_HANDLED;
634 } else
635 retval = IRQ_NONE;
636
637 return retval;
638}
639
640
641static void korina_check_media(struct net_device *dev, unsigned int init_media)
642{
643 struct korina_private *lp = netdev_priv(dev);
644
645 mii_check_media(&lp->mii_if, 0, init_media);
646
647 if (lp->mii_if.full_duplex)
648 writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
649 &lp->eth_regs->ethmac2);
650 else
651 writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
652 &lp->eth_regs->ethmac2);
653}
654
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000655static void korina_poll_media(unsigned long data)
656{
657 struct net_device *dev = (struct net_device *) data;
658 struct korina_private *lp = netdev_priv(dev);
659
660 korina_check_media(dev, 0);
661 mod_timer(&lp->media_check_timer, jiffies + HZ);
662}
663
Florian Fainellief112912008-03-19 17:14:51 +0100664static void korina_set_carrier(struct mii_if_info *mii)
665{
666 if (mii->force_media) {
667 /* autoneg is off: Link is always assumed to be up */
668 if (!netif_carrier_ok(mii->dev))
669 netif_carrier_on(mii->dev);
670 } else /* Let MMI library update carrier status */
671 korina_check_media(mii->dev, 0);
672}
673
674static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
675{
676 struct korina_private *lp = netdev_priv(dev);
677 struct mii_ioctl_data *data = if_mii(rq);
678 int rc;
679
680 if (!netif_running(dev))
681 return -EINVAL;
682 spin_lock_irq(&lp->lock);
683 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
684 spin_unlock_irq(&lp->lock);
685 korina_set_carrier(&lp->mii_if);
686
687 return rc;
688}
689
690/* ethtool helpers */
691static void netdev_get_drvinfo(struct net_device *dev,
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300692 struct ethtool_drvinfo *info)
Florian Fainellief112912008-03-19 17:14:51 +0100693{
694 struct korina_private *lp = netdev_priv(dev);
695
Jiri Pirko7826d432013-01-06 00:44:26 +0000696 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
697 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
698 strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info));
Florian Fainellief112912008-03-19 17:14:51 +0100699}
700
Philippe Reynesaf473682017-01-14 12:33:19 +0100701static int netdev_get_link_ksettings(struct net_device *dev,
702 struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100703{
704 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100705
706 spin_lock_irq(&lp->lock);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300707 mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100708 spin_unlock_irq(&lp->lock);
709
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +0300710 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100711}
712
Philippe Reynesaf473682017-01-14 12:33:19 +0100713static int netdev_set_link_ksettings(struct net_device *dev,
714 const struct ethtool_link_ksettings *cmd)
Florian Fainellief112912008-03-19 17:14:51 +0100715{
716 struct korina_private *lp = netdev_priv(dev);
717 int rc;
718
719 spin_lock_irq(&lp->lock);
Philippe Reynesaf473682017-01-14 12:33:19 +0100720 rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
Florian Fainellief112912008-03-19 17:14:51 +0100721 spin_unlock_irq(&lp->lock);
722 korina_set_carrier(&lp->mii_if);
723
724 return rc;
725}
726
727static u32 netdev_get_link(struct net_device *dev)
728{
729 struct korina_private *lp = netdev_priv(dev);
730
731 return mii_link_ok(&lp->mii_if);
732}
733
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700734static const struct ethtool_ops netdev_ethtool_ops = {
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300735 .get_drvinfo = netdev_get_drvinfo,
736 .get_link = netdev_get_link,
737 .get_link_ksettings = netdev_get_link_ksettings,
738 .set_link_ksettings = netdev_set_link_ksettings,
Florian Fainellief112912008-03-19 17:14:51 +0100739};
740
Phil Sutter70108372009-08-12 12:52:32 +0000741static int korina_alloc_ring(struct net_device *dev)
Florian Fainellief112912008-03-19 17:14:51 +0100742{
743 struct korina_private *lp = netdev_priv(dev);
Phil Suttere85bf472009-01-15 12:29:57 +0000744 struct sk_buff *skb;
Florian Fainellief112912008-03-19 17:14:51 +0100745 int i;
746
747 /* Initialize the transmit descriptors */
748 for (i = 0; i < KORINA_NUM_TDS; i++) {
749 lp->td_ring[i].control = DMA_DESC_IOF;
750 lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
751 lp->td_ring[i].ca = 0;
752 lp->td_ring[i].link = 0;
753 }
754 lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
755 lp->tx_full = lp->tx_count = 0;
756 lp->tx_chain_status = desc_empty;
757
758 /* Initialize the receive descriptors */
759 for (i = 0; i < KORINA_NUM_RDS; i++) {
Phil Sutter53ee4902010-05-29 13:23:35 +0000760 skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
Florian Fainellief112912008-03-19 17:14:51 +0100761 if (!skb)
Phil Sutter70108372009-08-12 12:52:32 +0000762 return -ENOMEM;
Florian Fainellief112912008-03-19 17:14:51 +0100763 lp->rx_skb[i] = skb;
764 lp->rd_ring[i].control = DMA_DESC_IOD |
765 DMA_COUNT(KORINA_RBSIZE);
766 lp->rd_ring[i].devcs = 0;
767 lp->rd_ring[i].ca = CPHYSADDR(skb->data);
768 lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
769 }
770
Phil Sutter6a2fe982009-01-15 12:29:55 +0000771 /* loop back receive descriptors, so the last
772 * descriptor points to the first one */
773 lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
774 lp->rd_ring[i - 1].control |= DMA_DESC_COD;
Florian Fainellief112912008-03-19 17:14:51 +0100775
Phil Sutter6a2fe982009-01-15 12:29:55 +0000776 lp->rx_next_done = 0;
Florian Fainellief112912008-03-19 17:14:51 +0100777 lp->rx_chain_head = 0;
778 lp->rx_chain_tail = 0;
779 lp->rx_chain_status = desc_empty;
Phil Sutter70108372009-08-12 12:52:32 +0000780
781 return 0;
Florian Fainellief112912008-03-19 17:14:51 +0100782}
783
784static void korina_free_ring(struct net_device *dev)
785{
786 struct korina_private *lp = netdev_priv(dev);
787 int i;
788
789 for (i = 0; i < KORINA_NUM_RDS; i++) {
790 lp->rd_ring[i].control = 0;
791 if (lp->rx_skb[i])
792 dev_kfree_skb_any(lp->rx_skb[i]);
793 lp->rx_skb[i] = NULL;
794 }
795
796 for (i = 0; i < KORINA_NUM_TDS; i++) {
797 lp->td_ring[i].control = 0;
798 if (lp->tx_skb[i])
799 dev_kfree_skb_any(lp->tx_skb[i]);
800 lp->tx_skb[i] = NULL;
801 }
802}
803
804/*
805 * Initialize the RC32434 ethernet controller.
806 */
807static int korina_init(struct net_device *dev)
808{
809 struct korina_private *lp = netdev_priv(dev);
810
811 /* Disable DMA */
812 korina_abort_tx(dev);
813 korina_abort_rx(dev);
814
815 /* reset ethernet logic */
816 writel(0, &lp->eth_regs->ethintfc);
817 while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
Florian Westphal860e9532016-05-03 16:33:13 +0200818 netif_trans_update(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100819
820 /* Enable Ethernet Interface */
821 writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
822
823 /* Allocate rings */
Phil Sutter70108372009-08-12 12:52:32 +0000824 if (korina_alloc_ring(dev)) {
825 printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
826 korina_free_ring(dev);
827 return -ENOMEM;
828 }
Florian Fainellief112912008-03-19 17:14:51 +0100829
830 writel(0, &lp->rx_dma_regs->dmas);
831 /* Start Rx DMA */
832 korina_start_rx(lp, &lp->rd_ring[0]);
833
834 writel(readl(&lp->tx_dma_regs->dmasm) &
835 ~(DMA_STAT_FINI | DMA_STAT_ERR),
836 &lp->tx_dma_regs->dmasm);
837 writel(readl(&lp->rx_dma_regs->dmasm) &
838 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
839 &lp->rx_dma_regs->dmasm);
840
841 /* Accept only packets destined for this Ethernet device address */
842 writel(ETH_ARC_AB, &lp->eth_regs->etharc);
843
844 /* Set all Ether station address registers to their initial values */
845 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
846 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
847
848 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
849 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
850
851 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
852 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
853
854 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
855 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
856
857
858 /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
859 writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
860 &lp->eth_regs->ethmac2);
861
862 /* Back to back inter-packet-gap */
863 writel(0x15, &lp->eth_regs->ethipgt);
864 /* Non - Back to back inter-packet-gap */
865 writel(0x12, &lp->eth_regs->ethipgr);
866
867 /* Management Clock Prescaler Divisor
868 * Clock independent setting */
869 writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300870 &lp->eth_regs->ethmcp);
Florian Fainellief112912008-03-19 17:14:51 +0100871
872 /* don't transmit until fifo contains 48b */
873 writel(48, &lp->eth_regs->ethfifott);
874
875 writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
876
877 napi_enable(&lp->napi);
878 netif_start_queue(dev);
879
880 return 0;
881}
882
883/*
884 * Restart the RC32434 ethernet controller.
Florian Fainellief112912008-03-19 17:14:51 +0100885 */
Phil Sutterceb3d232010-05-29 13:23:34 +0000886static void korina_restart_task(struct work_struct *work)
Florian Fainellief112912008-03-19 17:14:51 +0100887{
Phil Sutterceb3d232010-05-29 13:23:34 +0000888 struct korina_private *lp = container_of(work,
889 struct korina_private, restart_task);
890 struct net_device *dev = lp->dev;
Florian Fainellief112912008-03-19 17:14:51 +0100891
892 /*
893 * Disable interrupts
894 */
895 disable_irq(lp->rx_irq);
896 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100897
898 writel(readl(&lp->tx_dma_regs->dmasm) |
899 DMA_STAT_FINI | DMA_STAT_ERR,
900 &lp->tx_dma_regs->dmasm);
901 writel(readl(&lp->rx_dma_regs->dmasm) |
902 DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
903 &lp->rx_dma_regs->dmasm);
904
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800905 napi_disable(&lp->napi);
906
Florian Fainellie6afb1a2016-12-23 19:56:56 -0800907 korina_free_ring(dev);
908
Phil Sutterceb3d232010-05-29 13:23:34 +0000909 if (korina_init(dev) < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000910 printk(KERN_ERR "%s: cannot restart device\n", dev->name);
Phil Sutterceb3d232010-05-29 13:23:34 +0000911 return;
Florian Fainellief112912008-03-19 17:14:51 +0100912 }
913 korina_multicast_list(dev);
914
Florian Fainellief112912008-03-19 17:14:51 +0100915 enable_irq(lp->tx_irq);
916 enable_irq(lp->rx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100917}
918
Florian Fainellief112912008-03-19 17:14:51 +0100919static void korina_tx_timeout(struct net_device *dev)
920{
921 struct korina_private *lp = netdev_priv(dev);
Florian Fainellief112912008-03-19 17:14:51 +0100922
Phil Sutterceb3d232010-05-29 13:23:34 +0000923 schedule_work(&lp->restart_task);
Florian Fainellief112912008-03-19 17:14:51 +0100924}
925
Florian Fainellief112912008-03-19 17:14:51 +0100926#ifdef CONFIG_NET_POLL_CONTROLLER
927static void korina_poll_controller(struct net_device *dev)
928{
929 disable_irq(dev->irq);
930 korina_tx_dma_interrupt(dev->irq, dev);
931 enable_irq(dev->irq);
932}
933#endif
934
935static int korina_open(struct net_device *dev)
936{
937 struct korina_private *lp = netdev_priv(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +0200938 int ret;
Florian Fainellief112912008-03-19 17:14:51 +0100939
940 /* Initialize */
941 ret = korina_init(dev);
942 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000943 printk(KERN_ERR "%s: cannot open device\n", dev->name);
Florian Fainellief112912008-03-19 17:14:51 +0100944 goto out;
945 }
946
947 /* Install the interrupt handler
Roman Yeryomin7ce103b2017-09-17 20:24:15 +0300948 * that handles the Done Finished */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800949 ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200950 0, "Korina ethernet Rx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100951 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000952 printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300953 dev->name, lp->rx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100954 goto err_release;
955 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800956 ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
Michael Opdenacker2414fe12013-09-07 07:20:57 +0200957 0, "Korina ethernet Tx", dev);
Florian Fainellief112912008-03-19 17:14:51 +0100958 if (ret < 0) {
Phil Sutterf16aea42009-08-12 12:22:46 +0000959 printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
Roman Yeryomin2e5396b2017-09-17 20:25:02 +0300960 dev->name, lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100961 goto err_free_rx_irq;
962 }
963
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000964 mod_timer(&lp->media_check_timer, jiffies + 1);
Francois Romieu751c2e42008-04-20 18:05:31 +0200965out:
966 return ret;
Florian Fainellief112912008-03-19 17:14:51 +0100967
Florian Fainellief112912008-03-19 17:14:51 +0100968err_free_rx_irq:
969 free_irq(lp->rx_irq, dev);
970err_release:
971 korina_free_ring(dev);
972 goto out;
Florian Fainellief112912008-03-19 17:14:51 +0100973}
974
975static int korina_close(struct net_device *dev)
976{
977 struct korina_private *lp = netdev_priv(dev);
978 u32 tmp;
979
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +0000980 del_timer(&lp->media_check_timer);
981
Florian Fainellief112912008-03-19 17:14:51 +0100982 /* Disable interrupts */
983 disable_irq(lp->rx_irq);
984 disable_irq(lp->tx_irq);
Florian Fainellief112912008-03-19 17:14:51 +0100985
986 korina_abort_tx(dev);
987 tmp = readl(&lp->tx_dma_regs->dmasm);
988 tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
989 writel(tmp, &lp->tx_dma_regs->dmasm);
990
991 korina_abort_rx(dev);
992 tmp = readl(&lp->rx_dma_regs->dmasm);
993 tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
994 writel(tmp, &lp->rx_dma_regs->dmasm);
995
Phil Sutterbeb0bab2009-01-14 21:48:24 -0800996 napi_disable(&lp->napi);
997
Phil Sutterceb3d232010-05-29 13:23:34 +0000998 cancel_work_sync(&lp->restart_task);
999
Florian Fainellie6afb1a2016-12-23 19:56:56 -08001000 korina_free_ring(dev);
1001
Florian Fainellief112912008-03-19 17:14:51 +01001002 free_irq(lp->rx_irq, dev);
1003 free_irq(lp->tx_irq, dev);
Florian Fainellief112912008-03-19 17:14:51 +01001004
1005 return 0;
1006}
1007
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001008static const struct net_device_ops korina_netdev_ops = {
1009 .ndo_open = korina_open,
1010 .ndo_stop = korina_close,
1011 .ndo_start_xmit = korina_send_packet,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001012 .ndo_set_rx_mode = korina_multicast_list,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001013 .ndo_tx_timeout = korina_tx_timeout,
1014 .ndo_do_ioctl = korina_ioctl,
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001015 .ndo_validate_addr = eth_validate_addr,
1016 .ndo_set_mac_address = eth_mac_addr,
1017#ifdef CONFIG_NET_POLL_CONTROLLER
1018 .ndo_poll_controller = korina_poll_controller,
1019#endif
1020};
1021
Florian Fainellief112912008-03-19 17:14:51 +01001022static int korina_probe(struct platform_device *pdev)
1023{
1024 struct korina_device *bif = platform_get_drvdata(pdev);
1025 struct korina_private *lp;
1026 struct net_device *dev;
1027 struct resource *r;
Francois Romieue3152ab2008-04-20 18:06:13 +02001028 int rc;
Florian Fainellief112912008-03-19 17:14:51 +01001029
1030 dev = alloc_etherdev(sizeof(struct korina_private));
Joe Perches41de8d42012-01-29 13:47:52 +00001031 if (!dev)
Florian Fainellief112912008-03-19 17:14:51 +01001032 return -ENOMEM;
Joe Perches41de8d42012-01-29 13:47:52 +00001033
Florian Fainellief112912008-03-19 17:14:51 +01001034 SET_NETDEV_DEV(dev, &pdev->dev);
Florian Fainellief112912008-03-19 17:14:51 +01001035 lp = netdev_priv(dev);
1036
1037 bif->dev = dev;
Joe Perchesd458cdf2013-10-01 19:04:40 -07001038 memcpy(dev->dev_addr, bif->mac, ETH_ALEN);
Florian Fainellief112912008-03-19 17:14:51 +01001039
1040 lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
1041 lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
Florian Fainellief112912008-03-19 17:14:51 +01001042
1043 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
1044 dev->base_addr = r->start;
Dan Carpenter38013262010-03-22 02:11:45 +00001045 lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001046 if (!lp->eth_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001047 printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001048 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001049 goto probe_err_out;
1050 }
1051
1052 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
Dan Carpenter38013262010-03-22 02:11:45 +00001053 lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001054 if (!lp->rx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001055 printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001056 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001057 goto probe_err_dma_rx;
1058 }
1059
1060 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
Dan Carpenter38013262010-03-22 02:11:45 +00001061 lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
Florian Fainellief112912008-03-19 17:14:51 +01001062 if (!lp->tx_dma_regs) {
Phil Sutterf16aea42009-08-12 12:22:46 +00001063 printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
Francois Romieue3152ab2008-04-20 18:06:13 +02001064 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001065 goto probe_err_dma_tx;
1066 }
1067
1068 lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
1069 if (!lp->td_ring) {
Francois Romieue3152ab2008-04-20 18:06:13 +02001070 rc = -ENXIO;
Florian Fainellief112912008-03-19 17:14:51 +01001071 goto probe_err_td_ring;
1072 }
1073
1074 dma_cache_inv((unsigned long)(lp->td_ring),
1075 TD_RING_SIZE + RD_RING_SIZE);
1076
1077 /* now convert TD_RING pointer to KSEG1 */
1078 lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
1079 lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
1080
1081 spin_lock_init(&lp->lock);
1082 /* just use the rx dma irq */
1083 dev->irq = lp->rx_irq;
1084 lp->dev = dev;
1085
Alexander Beregalov52b031f2009-04-15 12:52:46 +00001086 dev->netdev_ops = &korina_netdev_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001087 dev->ethtool_ops = &netdev_ethtool_ops;
Florian Fainellief112912008-03-19 17:14:51 +01001088 dev->watchdog_timeo = TX_TIMEOUT;
Roman Yeryomind609d282017-09-17 20:24:38 +03001089 netif_napi_add(dev, &lp->napi, korina_poll, NAPI_POLL_WEIGHT);
Florian Fainellief112912008-03-19 17:14:51 +01001090
1091 lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
1092 lp->mii_if.dev = dev;
1093 lp->mii_if.mdio_read = mdio_read;
1094 lp->mii_if.mdio_write = mdio_write;
1095 lp->mii_if.phy_id = lp->phy_addr;
1096 lp->mii_if.phy_id_mask = 0x1f;
1097 lp->mii_if.reg_num_mask = 0x1f;
1098
Francois Romieue3152ab2008-04-20 18:06:13 +02001099 rc = register_netdev(dev);
1100 if (rc < 0) {
Florian Fainellief112912008-03-19 17:14:51 +01001101 printk(KERN_ERR DRV_NAME
Phil Sutterf16aea42009-08-12 12:22:46 +00001102 ": cannot register net device: %d\n", rc);
Florian Fainellief112912008-03-19 17:14:51 +01001103 goto probe_err_register;
1104 }
Florian Fainelli4d5ef9f2009-05-28 00:58:41 +00001105 setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev);
Phil Sutterf16aea42009-08-12 12:22:46 +00001106
Phil Sutterceb3d232010-05-29 13:23:34 +00001107 INIT_WORK(&lp->restart_task, korina_restart_task);
1108
Phil Sutterf16aea42009-08-12 12:22:46 +00001109 printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
1110 dev->name);
Francois Romieue3152ab2008-04-20 18:06:13 +02001111out:
1112 return rc;
Florian Fainellief112912008-03-19 17:14:51 +01001113
1114probe_err_register:
1115 kfree(lp->td_ring);
1116probe_err_td_ring:
1117 iounmap(lp->tx_dma_regs);
1118probe_err_dma_tx:
1119 iounmap(lp->rx_dma_regs);
1120probe_err_dma_rx:
1121 iounmap(lp->eth_regs);
1122probe_err_out:
1123 free_netdev(dev);
Francois Romieue3152ab2008-04-20 18:06:13 +02001124 goto out;
Florian Fainellief112912008-03-19 17:14:51 +01001125}
1126
1127static int korina_remove(struct platform_device *pdev)
1128{
1129 struct korina_device *bif = platform_get_drvdata(pdev);
1130 struct korina_private *lp = netdev_priv(bif->dev);
1131
Francois Romieue3152ab2008-04-20 18:06:13 +02001132 iounmap(lp->eth_regs);
1133 iounmap(lp->rx_dma_regs);
1134 iounmap(lp->tx_dma_regs);
Florian Fainellief112912008-03-19 17:14:51 +01001135
Florian Fainellief112912008-03-19 17:14:51 +01001136 unregister_netdev(bif->dev);
1137 free_netdev(bif->dev);
1138
1139 return 0;
1140}
1141
1142static struct platform_driver korina_driver = {
1143 .driver.name = "korina",
1144 .probe = korina_probe,
1145 .remove = korina_remove,
1146};
1147
Axel Lindb62f682011-11-27 16:44:17 +00001148module_platform_driver(korina_driver);
Florian Fainellief112912008-03-19 17:14:51 +01001149
1150MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
1151MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1152MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
1153MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
1154MODULE_LICENSE("GPL");