blob: ca60fa24d2ed77a1d58b4edfe94c26819fe99447 [file] [log] [blame]
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001/*
2 * PXA168 ethernet driver.
3 * Most of the code is derived from mv643xx ethernet driver.
4 *
5 * Copyright (C) 2010 Marvell International Ltd.
6 * Sachin Sanap <ssanap@marvell.com>
Philip Rakity10206602010-09-28 04:26:30 +00007 * Zhangfei Gao <zgao6@marvell.com>
Sachin Sanapa49f37e2010-08-13 21:22:49 +00008 * Philip Rakity <prakity@marvell.com>
9 * Mark Brown <markb@marvell.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
Jeff Kirsher0ab75ae2013-12-06 06:28:43 -080022 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sachin Sanapa49f37e2010-08-13 21:22:49 +000023 */
24
Sachin Sanapa49f37e2010-08-13 21:22:49 +000025#include <linux/bitops.h>
Sachin Sanapa49f37e2010-08-13 21:22:49 +000026#include <linux/clk.h>
Antoine Ténart307f6562014-09-30 16:28:07 +020027#include <linux/delay.h>
28#include <linux/dma-mapping.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <linux/in.h>
Tanmay Upadhyayb7e43382011-09-05 19:32:04 +000032#include <linux/interrupt.h>
Antoine Ténart307f6562014-09-30 16:28:07 +020033#include <linux/io.h>
34#include <linux/ip.h>
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/of.h>
Antoine Ténart78b9b2c2014-09-30 16:28:12 +020038#include <linux/of_net.h>
Antoine Ténart307f6562014-09-30 16:28:07 +020039#include <linux/phy.h>
40#include <linux/platform_device.h>
41#include <linux/pxa168_eth.h>
42#include <linux/tcp.h>
Sachin Sanapa49f37e2010-08-13 21:22:49 +000043#include <linux/types.h>
Antoine Ténart307f6562014-09-30 16:28:07 +020044#include <linux/udp.h>
45#include <linux/workqueue.h>
46
Sachin Sanapa49f37e2010-08-13 21:22:49 +000047#include <asm/pgtable.h>
Sachin Sanapa49f37e2010-08-13 21:22:49 +000048#include <asm/cacheflush.h>
Sachin Sanapa49f37e2010-08-13 21:22:49 +000049
50#define DRIVER_NAME "pxa168-eth"
51#define DRIVER_VERSION "0.3"
52
53/*
54 * Registers
55 */
56
57#define PHY_ADDRESS 0x0000
58#define SMI 0x0010
59#define PORT_CONFIG 0x0400
60#define PORT_CONFIG_EXT 0x0408
61#define PORT_COMMAND 0x0410
62#define PORT_STATUS 0x0418
63#define HTPR 0x0428
Antoine Ténart39830682014-09-30 16:28:11 +020064#define MAC_ADDR_LOW 0x0430
65#define MAC_ADDR_HIGH 0x0438
Sachin Sanapa49f37e2010-08-13 21:22:49 +000066#define SDMA_CONFIG 0x0440
67#define SDMA_CMD 0x0448
68#define INT_CAUSE 0x0450
69#define INT_W_CLEAR 0x0454
70#define INT_MASK 0x0458
71#define ETH_F_RX_DESC_0 0x0480
72#define ETH_C_RX_DESC_0 0x04A0
73#define ETH_C_TX_DESC_1 0x04E4
74
75/* smi register */
76#define SMI_BUSY (1 << 28) /* 0 - Write, 1 - Read */
77#define SMI_R_VALID (1 << 27) /* 0 - Write, 1 - Read */
78#define SMI_OP_W (0 << 26) /* Write operation */
79#define SMI_OP_R (1 << 26) /* Read operation */
80
81#define PHY_WAIT_ITERATIONS 10
82
83#define PXA168_ETH_PHY_ADDR_DEFAULT 0
84/* RX & TX descriptor command */
85#define BUF_OWNED_BY_DMA (1 << 31)
86
87/* RX descriptor status */
88#define RX_EN_INT (1 << 23)
89#define RX_FIRST_DESC (1 << 17)
90#define RX_LAST_DESC (1 << 16)
91#define RX_ERROR (1 << 15)
92
93/* TX descriptor command */
94#define TX_EN_INT (1 << 23)
95#define TX_GEN_CRC (1 << 22)
96#define TX_ZERO_PADDING (1 << 18)
97#define TX_FIRST_DESC (1 << 17)
98#define TX_LAST_DESC (1 << 16)
99#define TX_ERROR (1 << 15)
100
101/* SDMA_CMD */
102#define SDMA_CMD_AT (1 << 31)
103#define SDMA_CMD_TXDL (1 << 24)
104#define SDMA_CMD_TXDH (1 << 23)
105#define SDMA_CMD_AR (1 << 15)
106#define SDMA_CMD_ERD (1 << 7)
107
108/* Bit definitions of the Port Config Reg */
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200109#define PCR_DUPLEX_FULL (1 << 15)
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000110#define PCR_HS (1 << 12)
111#define PCR_EN (1 << 7)
112#define PCR_PM (1 << 0)
113
114/* Bit definitions of the Port Config Extend Reg */
115#define PCXR_2BSM (1 << 28)
116#define PCXR_DSCP_EN (1 << 21)
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200117#define PCXR_RMII_EN (1 << 20)
118#define PCXR_AN_SPEED_DIS (1 << 19)
119#define PCXR_SPEED_100 (1 << 18)
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000120#define PCXR_MFL_1518 (0 << 14)
121#define PCXR_MFL_1536 (1 << 14)
122#define PCXR_MFL_2048 (2 << 14)
123#define PCXR_MFL_64K (3 << 14)
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200124#define PCXR_FLOWCTL_DIS (1 << 12)
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000125#define PCXR_FLP (1 << 11)
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200126#define PCXR_AN_FLOWCTL_DIS (1 << 10)
127#define PCXR_AN_DUPLEX_DIS (1 << 9)
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000128#define PCXR_PRIO_TX_OFF 3
129#define PCXR_TX_HIGH_PRI (7 << PCXR_PRIO_TX_OFF)
130
131/* Bit definitions of the SDMA Config Reg */
132#define SDCR_BSZ_OFF 12
133#define SDCR_BSZ8 (3 << SDCR_BSZ_OFF)
134#define SDCR_BSZ4 (2 << SDCR_BSZ_OFF)
135#define SDCR_BSZ2 (1 << SDCR_BSZ_OFF)
136#define SDCR_BSZ1 (0 << SDCR_BSZ_OFF)
137#define SDCR_BLMR (1 << 6)
138#define SDCR_BLMT (1 << 7)
139#define SDCR_RIFB (1 << 9)
140#define SDCR_RC_OFF 2
141#define SDCR_RC_MAX_RETRANS (0xf << SDCR_RC_OFF)
142
143/*
144 * Bit definitions of the Interrupt Cause Reg
145 * and Interrupt MASK Reg is the same
146 */
147#define ICR_RXBUF (1 << 0)
148#define ICR_TXBUF_H (1 << 2)
149#define ICR_TXBUF_L (1 << 3)
150#define ICR_TXEND_H (1 << 6)
151#define ICR_TXEND_L (1 << 7)
152#define ICR_RXERR (1 << 8)
153#define ICR_TXERR_H (1 << 10)
154#define ICR_TXERR_L (1 << 11)
155#define ICR_TX_UDR (1 << 13)
156#define ICR_MII_CH (1 << 28)
157
158#define ALL_INTS (ICR_TXBUF_H | ICR_TXBUF_L | ICR_TX_UDR |\
159 ICR_TXERR_H | ICR_TXERR_L |\
160 ICR_TXEND_H | ICR_TXEND_L |\
161 ICR_RXBUF | ICR_RXERR | ICR_MII_CH)
162
163#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
164
165#define NUM_RX_DESCS 64
166#define NUM_TX_DESCS 64
167
168#define HASH_ADD 0
169#define HASH_DELETE 1
170#define HASH_ADDR_TABLE_SIZE 0x4000 /* 16K (1/2K address - PCR_HS == 1) */
171#define HOP_NUMBER 12
172
173/* Bit definitions for Port status */
174#define PORT_SPEED_100 (1 << 0)
175#define FULL_DUPLEX (1 << 1)
Antoine Ténart09f5da12014-09-30 16:28:10 +0200176#define FLOW_CONTROL_DISABLED (1 << 2)
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000177#define LINK_UP (1 << 3)
178
179/* Bit definitions for work to be done */
180#define WORK_LINK (1 << 0)
181#define WORK_TX_DONE (1 << 1)
182
183/*
184 * Misc definitions.
185 */
186#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
187
188struct rx_desc {
189 u32 cmd_sts; /* Descriptor command status */
190 u16 byte_cnt; /* Descriptor buffer byte count */
191 u16 buf_size; /* Buffer size */
192 u32 buf_ptr; /* Descriptor buffer pointer */
193 u32 next_desc_ptr; /* Next descriptor pointer */
194};
195
196struct tx_desc {
197 u32 cmd_sts; /* Command/status field */
198 u16 reserved;
199 u16 byte_cnt; /* buffer byte count */
200 u32 buf_ptr; /* pointer to buffer for this descriptor */
201 u32 next_desc_ptr; /* Pointer to next descriptor */
202};
203
204struct pxa168_eth_private {
205 int port_num; /* User Ethernet port number */
Antoine Ténart43d3ddf2014-09-30 16:28:08 +0200206 int phy_addr;
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +0200207 int phy_speed;
208 int phy_duplex;
209 phy_interface_t phy_intf;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000210
211 int rx_resource_err; /* Rx ring resource error flag */
212
213 /* Next available and first returning Rx resource */
214 int rx_curr_desc_q, rx_used_desc_q;
215
216 /* Next available and first returning Tx resource */
217 int tx_curr_desc_q, tx_used_desc_q;
218
219 struct rx_desc *p_rx_desc_area;
220 dma_addr_t rx_desc_dma;
221 int rx_desc_area_size;
222 struct sk_buff **rx_skb;
223
224 struct tx_desc *p_tx_desc_area;
225 dma_addr_t tx_desc_dma;
226 int tx_desc_area_size;
227 struct sk_buff **tx_skb;
228
229 struct work_struct tx_timeout_task;
230
231 struct net_device *dev;
232 struct napi_struct napi;
233 u8 work_todo;
234 int skb_size;
235
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000236 /* Size of Tx Ring per queue */
237 int tx_ring_size;
238 /* Number of tx descriptors in use */
239 int tx_desc_count;
240 /* Size of Rx Ring per queue */
241 int rx_ring_size;
242 /* Number of rx descriptors in use */
243 int rx_desc_count;
244
245 /*
246 * Used in case RX Ring is empty, which can occur when
247 * system does not have resources (skb's)
248 */
249 struct timer_list timeout;
250 struct mii_bus *smi_bus;
251 struct phy_device *phy;
252
253 /* clock */
254 struct clk *clk;
255 struct pxa168_eth_platform_data *pd;
256 /*
257 * Ethernet controller base address.
258 */
259 void __iomem *base;
260
261 /* Pointer to the hardware address filter table */
262 void *htpr;
263 dma_addr_t htpr_dma;
264};
265
266struct addr_table_entry {
267 __le32 lo;
268 __le32 hi;
269};
270
271/* Bit fields of a Hash Table Entry */
272enum hash_table_entry {
273 HASH_ENTRY_VALID = 1,
274 SKIP = 2,
275 HASH_ENTRY_RECEIVE_DISCARD = 4,
276 HASH_ENTRY_RECEIVE_DISCARD_BIT = 2
277};
278
279static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
280static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd);
281static int pxa168_init_hw(struct pxa168_eth_private *pep);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200282static int pxa168_init_phy(struct net_device *dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000283static void eth_port_reset(struct net_device *dev);
284static void eth_port_start(struct net_device *dev);
285static int pxa168_eth_open(struct net_device *dev);
286static int pxa168_eth_stop(struct net_device *dev);
287static int ethernet_phy_setup(struct net_device *dev);
288
289static inline u32 rdl(struct pxa168_eth_private *pep, int offset)
290{
291 return readl(pep->base + offset);
292}
293
294static inline void wrl(struct pxa168_eth_private *pep, int offset, u32 data)
295{
296 writel(data, pep->base + offset);
297}
298
299static void abort_dma(struct pxa168_eth_private *pep)
300{
301 int delay;
302 int max_retries = 40;
303
304 do {
305 wrl(pep, SDMA_CMD, SDMA_CMD_AR | SDMA_CMD_AT);
306 udelay(100);
307
308 delay = 10;
309 while ((rdl(pep, SDMA_CMD) & (SDMA_CMD_AR | SDMA_CMD_AT))
310 && delay-- > 0) {
311 udelay(10);
312 }
313 } while (max_retries-- > 0 && delay <= 0);
314
315 if (max_retries <= 0)
Antoine Ténart307f6562014-09-30 16:28:07 +0200316 netdev_err(pep->dev, "%s : DMA Stuck\n", __func__);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000317}
318
319static int ethernet_phy_get(struct pxa168_eth_private *pep)
320{
321 unsigned int reg_data;
322
323 reg_data = rdl(pep, PHY_ADDRESS);
324
325 return (reg_data >> (5 * pep->port_num)) & 0x1f;
326}
327
328static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr)
329{
330 u32 reg_data;
331 int addr_shift = 5 * pep->port_num;
332
333 reg_data = rdl(pep, PHY_ADDRESS);
334 reg_data &= ~(0x1f << addr_shift);
335 reg_data |= (phy_addr & 0x1f) << addr_shift;
336 wrl(pep, PHY_ADDRESS, reg_data);
337}
338
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000339static void rxq_refill(struct net_device *dev)
340{
341 struct pxa168_eth_private *pep = netdev_priv(dev);
342 struct sk_buff *skb;
343 struct rx_desc *p_used_rx_desc;
344 int used_rx_desc;
345
346 while (pep->rx_desc_count < pep->rx_ring_size) {
347 int size;
348
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000349 skb = netdev_alloc_skb(dev, pep->skb_size);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000350 if (!skb)
351 break;
352 if (SKB_DMA_REALIGN)
353 skb_reserve(skb, SKB_DMA_REALIGN);
354 pep->rx_desc_count++;
355 /* Get 'used' Rx descriptor */
356 used_rx_desc = pep->rx_used_desc_q;
357 p_used_rx_desc = &pep->p_rx_desc_area[used_rx_desc];
Isaku Yamahata511efbb2013-06-14 17:58:34 +0900358 size = skb_end_pointer(skb) - skb->data;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000359 p_used_rx_desc->buf_ptr = dma_map_single(NULL,
360 skb->data,
361 size,
362 DMA_FROM_DEVICE);
363 p_used_rx_desc->buf_size = size;
364 pep->rx_skb[used_rx_desc] = skb;
365
366 /* Return the descriptor to DMA ownership */
367 wmb();
368 p_used_rx_desc->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
369 wmb();
370
371 /* Move the used descriptor pointer to the next descriptor */
372 pep->rx_used_desc_q = (used_rx_desc + 1) % pep->rx_ring_size;
373
374 /* Any Rx return cancels the Rx resource error status */
375 pep->rx_resource_err = 0;
376
377 skb_reserve(skb, ETH_HW_IP_ALIGN);
378 }
379
380 /*
381 * If RX ring is empty of SKB, set a timer to try allocating
382 * again at a later time.
383 */
384 if (pep->rx_desc_count == 0) {
385 pep->timeout.expires = jiffies + (HZ / 10);
386 add_timer(&pep->timeout);
387 }
388}
389
390static inline void rxq_refill_timer_wrapper(unsigned long data)
391{
392 struct pxa168_eth_private *pep = (void *)data;
393 napi_schedule(&pep->napi);
394}
395
396static inline u8 flip_8_bits(u8 x)
397{
398 return (((x) & 0x01) << 3) | (((x) & 0x02) << 1)
399 | (((x) & 0x04) >> 1) | (((x) & 0x08) >> 3)
400 | (((x) & 0x10) << 3) | (((x) & 0x20) << 1)
401 | (((x) & 0x40) >> 1) | (((x) & 0x80) >> 3);
402}
403
404static void nibble_swap_every_byte(unsigned char *mac_addr)
405{
406 int i;
407 for (i = 0; i < ETH_ALEN; i++) {
408 mac_addr[i] = ((mac_addr[i] & 0x0f) << 4) |
409 ((mac_addr[i] & 0xf0) >> 4);
410 }
411}
412
413static void inverse_every_nibble(unsigned char *mac_addr)
414{
415 int i;
416 for (i = 0; i < ETH_ALEN; i++)
417 mac_addr[i] = flip_8_bits(mac_addr[i]);
418}
419
420/*
421 * ----------------------------------------------------------------------------
422 * This function will calculate the hash function of the address.
423 * Inputs
424 * mac_addr_orig - MAC address.
425 * Outputs
426 * return the calculated entry.
427 */
428static u32 hash_function(unsigned char *mac_addr_orig)
429{
430 u32 hash_result;
431 u32 addr0;
432 u32 addr1;
433 u32 addr2;
434 u32 addr3;
435 unsigned char mac_addr[ETH_ALEN];
436
437 /* Make a copy of MAC address since we are going to performe bit
438 * operations on it
439 */
440 memcpy(mac_addr, mac_addr_orig, ETH_ALEN);
441
442 nibble_swap_every_byte(mac_addr);
443 inverse_every_nibble(mac_addr);
444
445 addr0 = (mac_addr[5] >> 2) & 0x3f;
446 addr1 = (mac_addr[5] & 0x03) | (((mac_addr[4] & 0x7f)) << 2);
447 addr2 = ((mac_addr[4] & 0x80) >> 7) | mac_addr[3] << 1;
448 addr3 = (mac_addr[2] & 0xff) | ((mac_addr[1] & 1) << 8);
449
450 hash_result = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
451 hash_result = hash_result & 0x07ff;
452 return hash_result;
453}
454
455/*
456 * ----------------------------------------------------------------------------
457 * This function will add/del an entry to the address table.
458 * Inputs
459 * pep - ETHERNET .
460 * mac_addr - MAC address.
461 * skip - if 1, skip this address.Used in case of deleting an entry which is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300462 * part of chain in the hash table.We can't just delete the entry since
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000463 * that will break the chain.We need to defragment the tables time to
464 * time.
465 * rd - 0 Discard packet upon match.
466 * - 1 Receive packet upon match.
467 * Outputs
468 * address table entry is added/deleted.
469 * 0 if success.
470 * -ENOSPC if table full
471 */
472static int add_del_hash_entry(struct pxa168_eth_private *pep,
473 unsigned char *mac_addr,
474 u32 rd, u32 skip, int del)
475{
476 struct addr_table_entry *entry, *start;
477 u32 new_high;
478 u32 new_low;
479 u32 i;
480
481 new_low = (((mac_addr[1] >> 4) & 0xf) << 15)
482 | (((mac_addr[1] >> 0) & 0xf) << 11)
483 | (((mac_addr[0] >> 4) & 0xf) << 7)
484 | (((mac_addr[0] >> 0) & 0xf) << 3)
485 | (((mac_addr[3] >> 4) & 0x1) << 31)
486 | (((mac_addr[3] >> 0) & 0xf) << 27)
487 | (((mac_addr[2] >> 4) & 0xf) << 23)
488 | (((mac_addr[2] >> 0) & 0xf) << 19)
489 | (skip << SKIP) | (rd << HASH_ENTRY_RECEIVE_DISCARD_BIT)
490 | HASH_ENTRY_VALID;
491
492 new_high = (((mac_addr[5] >> 4) & 0xf) << 15)
493 | (((mac_addr[5] >> 0) & 0xf) << 11)
494 | (((mac_addr[4] >> 4) & 0xf) << 7)
495 | (((mac_addr[4] >> 0) & 0xf) << 3)
496 | (((mac_addr[3] >> 5) & 0x7) << 0);
497
498 /*
499 * Pick the appropriate table, start scanning for free/reusable
500 * entries at the index obtained by hashing the specified MAC address
501 */
Joe Perches43d620c2011-06-16 19:08:06 +0000502 start = pep->htpr;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000503 entry = start + hash_function(mac_addr);
504 for (i = 0; i < HOP_NUMBER; i++) {
505 if (!(le32_to_cpu(entry->lo) & HASH_ENTRY_VALID)) {
506 break;
507 } else {
508 /* if same address put in same position */
509 if (((le32_to_cpu(entry->lo) & 0xfffffff8) ==
510 (new_low & 0xfffffff8)) &&
511 (le32_to_cpu(entry->hi) == new_high)) {
512 break;
513 }
514 }
515 if (entry == start + 0x7ff)
516 entry = start;
517 else
518 entry++;
519 }
520
521 if (((le32_to_cpu(entry->lo) & 0xfffffff8) != (new_low & 0xfffffff8)) &&
522 (le32_to_cpu(entry->hi) != new_high) && del)
523 return 0;
524
525 if (i == HOP_NUMBER) {
526 if (!del) {
Antoine Ténart307f6562014-09-30 16:28:07 +0200527 netdev_info(pep->dev,
528 "%s: table section is full, need to "
529 "move to 16kB implementation?\n",
530 __FILE__);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000531 return -ENOSPC;
532 } else
533 return 0;
534 }
535
536 /*
537 * Update the selected entry
538 */
539 if (del) {
540 entry->hi = 0;
541 entry->lo = 0;
542 } else {
543 entry->hi = cpu_to_le32(new_high);
544 entry->lo = cpu_to_le32(new_low);
545 }
546
547 return 0;
548}
549
550/*
551 * ----------------------------------------------------------------------------
552 * Create an addressTable entry from MAC address info
553 * found in the specifed net_device struct
554 *
555 * Input : pointer to ethernet interface network device structure
556 * Output : N/A
557 */
558static void update_hash_table_mac_address(struct pxa168_eth_private *pep,
559 unsigned char *oaddr,
560 unsigned char *addr)
561{
562 /* Delete old entry */
563 if (oaddr)
564 add_del_hash_entry(pep, oaddr, 1, 0, HASH_DELETE);
565 /* Add new entry */
566 add_del_hash_entry(pep, addr, 1, 0, HASH_ADD);
567}
568
569static int init_hash_table(struct pxa168_eth_private *pep)
570{
571 /*
572 * Hardware expects CPU to build a hash table based on a predefined
573 * hash function and populate it based on hardware address. The
574 * location of the hash table is identified by 32-bit pointer stored
575 * in HTPR internal register. Two possible sizes exists for the hash
576 * table 8kB (256kB of DRAM required (4 x 64 kB banks)) and 1/2kB
577 * (16kB of DRAM required (4 x 4 kB banks)).We currently only support
578 * 1/2kB.
579 */
580 /* TODO: Add support for 8kB hash table and alternative hash
581 * function.Driver can dynamically switch to them if the 1/2kB hash
582 * table is full.
583 */
584 if (pep->htpr == NULL) {
Joe Perchesede23fa82013-08-26 22:45:23 -0700585 pep->htpr = dma_zalloc_coherent(pep->dev->dev.parent,
586 HASH_ADDR_TABLE_SIZE,
587 &pep->htpr_dma, GFP_KERNEL);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000588 if (pep->htpr == NULL)
589 return -ENOMEM;
Joe Perches1f9061d22013-03-15 07:23:58 +0000590 } else {
591 memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000592 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000593 wrl(pep, HTPR, pep->htpr_dma);
594 return 0;
595}
596
597static void pxa168_eth_set_rx_mode(struct net_device *dev)
598{
599 struct pxa168_eth_private *pep = netdev_priv(dev);
600 struct netdev_hw_addr *ha;
601 u32 val;
602
603 val = rdl(pep, PORT_CONFIG);
604 if (dev->flags & IFF_PROMISC)
605 val |= PCR_PM;
606 else
607 val &= ~PCR_PM;
608 wrl(pep, PORT_CONFIG, val);
609
610 /*
611 * Remove the old list of MAC address and add dev->addr
612 * and multicast address.
613 */
614 memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
615 update_hash_table_mac_address(pep, NULL, dev->dev_addr);
616
617 netdev_for_each_mc_addr(ha, dev)
618 update_hash_table_mac_address(pep, NULL, ha->addr);
619}
620
Antoine Ténart78b9b2c2014-09-30 16:28:12 +0200621static void pxa168_eth_get_mac_address(struct net_device *dev,
622 unsigned char *addr)
623{
624 struct pxa168_eth_private *pep = netdev_priv(dev);
625 unsigned int mac_h = rdl(pep, MAC_ADDR_HIGH);
626 unsigned int mac_l = rdl(pep, MAC_ADDR_LOW);
627
628 addr[0] = (mac_h >> 24) & 0xff;
629 addr[1] = (mac_h >> 16) & 0xff;
630 addr[2] = (mac_h >> 8) & 0xff;
631 addr[3] = mac_h & 0xff;
632 addr[4] = (mac_l >> 8) & 0xff;
633 addr[5] = mac_l & 0xff;
634}
635
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000636static int pxa168_eth_set_mac_address(struct net_device *dev, void *addr)
637{
638 struct sockaddr *sa = addr;
639 struct pxa168_eth_private *pep = netdev_priv(dev);
640 unsigned char oldMac[ETH_ALEN];
Antoine Ténart39830682014-09-30 16:28:11 +0200641 u32 mac_h, mac_l;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000642
643 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +0000644 return -EADDRNOTAVAIL;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000645 memcpy(oldMac, dev->dev_addr, ETH_ALEN);
646 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
Antoine Ténart39830682014-09-30 16:28:11 +0200647
Antoine Ténarte8854392014-10-03 17:08:19 +0200648 mac_h = dev->dev_addr[0] << 24;
649 mac_h |= dev->dev_addr[1] << 16;
650 mac_h |= dev->dev_addr[2] << 8;
651 mac_h |= dev->dev_addr[3];
652 mac_l = dev->dev_addr[4] << 8;
653 mac_l |= dev->dev_addr[5];
Antoine Ténart39830682014-09-30 16:28:11 +0200654 wrl(pep, MAC_ADDR_HIGH, mac_h);
655 wrl(pep, MAC_ADDR_LOW, mac_l);
656
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000657 netif_addr_lock_bh(dev);
658 update_hash_table_mac_address(pep, oldMac, dev->dev_addr);
659 netif_addr_unlock_bh(dev);
660 return 0;
661}
662
663static void eth_port_start(struct net_device *dev)
664{
665 unsigned int val = 0;
666 struct pxa168_eth_private *pep = netdev_priv(dev);
667 int tx_curr_desc, rx_curr_desc;
668
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200669 phy_start(pep->phy);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000670
671 /* Assignment of Tx CTRP of given queue */
672 tx_curr_desc = pep->tx_curr_desc_q;
673 wrl(pep, ETH_C_TX_DESC_1,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000674 (u32) (pep->tx_desc_dma + tx_curr_desc * sizeof(struct tx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000675
676 /* Assignment of Rx CRDP of given queue */
677 rx_curr_desc = pep->rx_curr_desc_q;
678 wrl(pep, ETH_C_RX_DESC_0,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000679 (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000680
681 wrl(pep, ETH_F_RX_DESC_0,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000682 (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000683
684 /* Clear all interrupts */
685 wrl(pep, INT_CAUSE, 0);
686
687 /* Enable all interrupts for receive, transmit and error. */
688 wrl(pep, INT_MASK, ALL_INTS);
689
690 val = rdl(pep, PORT_CONFIG);
691 val |= PCR_EN;
692 wrl(pep, PORT_CONFIG, val);
693
694 /* Start RX DMA engine */
695 val = rdl(pep, SDMA_CMD);
696 val |= SDMA_CMD_ERD;
697 wrl(pep, SDMA_CMD, val);
698}
699
700static void eth_port_reset(struct net_device *dev)
701{
702 struct pxa168_eth_private *pep = netdev_priv(dev);
703 unsigned int val = 0;
704
705 /* Stop all interrupts for receive, transmit and error. */
706 wrl(pep, INT_MASK, 0);
707
708 /* Clear all interrupts */
709 wrl(pep, INT_CAUSE, 0);
710
711 /* Stop RX DMA */
712 val = rdl(pep, SDMA_CMD);
713 val &= ~SDMA_CMD_ERD; /* abort dma command */
714
715 /* Abort any transmit and receive operations and put DMA
716 * in idle state.
717 */
718 abort_dma(pep);
719
720 /* Disable port */
721 val = rdl(pep, PORT_CONFIG);
722 val &= ~PCR_EN;
723 wrl(pep, PORT_CONFIG, val);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200724
725 phy_stop(pep->phy);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000726}
727
728/*
729 * txq_reclaim - Free the tx desc data for completed descriptors
730 * If force is non-zero, frees uncompleted descriptors as well
731 */
732static int txq_reclaim(struct net_device *dev, int force)
733{
734 struct pxa168_eth_private *pep = netdev_priv(dev);
735 struct tx_desc *desc;
736 u32 cmd_sts;
737 struct sk_buff *skb;
738 int tx_index;
739 dma_addr_t addr;
740 int count;
741 int released = 0;
742
743 netif_tx_lock(dev);
744
745 pep->work_todo &= ~WORK_TX_DONE;
746 while (pep->tx_desc_count > 0) {
747 tx_index = pep->tx_used_desc_q;
748 desc = &pep->p_tx_desc_area[tx_index];
749 cmd_sts = desc->cmd_sts;
750 if (!force && (cmd_sts & BUF_OWNED_BY_DMA)) {
751 if (released > 0) {
752 goto txq_reclaim_end;
753 } else {
754 released = -1;
755 goto txq_reclaim_end;
756 }
757 }
758 pep->tx_used_desc_q = (tx_index + 1) % pep->tx_ring_size;
759 pep->tx_desc_count--;
760 addr = desc->buf_ptr;
761 count = desc->byte_cnt;
762 skb = pep->tx_skb[tx_index];
763 if (skb)
764 pep->tx_skb[tx_index] = NULL;
765
766 if (cmd_sts & TX_ERROR) {
767 if (net_ratelimit())
Antoine Ténart307f6562014-09-30 16:28:07 +0200768 netdev_err(dev, "Error in TX\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000769 dev->stats.tx_errors++;
770 }
771 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
772 if (skb)
773 dev_kfree_skb_irq(skb);
774 released++;
775 }
776txq_reclaim_end:
777 netif_tx_unlock(dev);
778 return released;
779}
780
781static void pxa168_eth_tx_timeout(struct net_device *dev)
782{
783 struct pxa168_eth_private *pep = netdev_priv(dev);
784
Antoine Ténart307f6562014-09-30 16:28:07 +0200785 netdev_info(dev, "TX timeout desc_count %d\n", pep->tx_desc_count);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000786
787 schedule_work(&pep->tx_timeout_task);
788}
789
790static void pxa168_eth_tx_timeout_task(struct work_struct *work)
791{
792 struct pxa168_eth_private *pep = container_of(work,
793 struct pxa168_eth_private,
794 tx_timeout_task);
795 struct net_device *dev = pep->dev;
796 pxa168_eth_stop(dev);
797 pxa168_eth_open(dev);
798}
799
800static int rxq_process(struct net_device *dev, int budget)
801{
802 struct pxa168_eth_private *pep = netdev_priv(dev);
803 struct net_device_stats *stats = &dev->stats;
804 unsigned int received_packets = 0;
805 struct sk_buff *skb;
806
807 while (budget-- > 0) {
808 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
809 struct rx_desc *rx_desc;
810 unsigned int cmd_sts;
811
812 /* Do not process Rx ring in case of Rx ring resource error */
813 if (pep->rx_resource_err)
814 break;
815 rx_curr_desc = pep->rx_curr_desc_q;
816 rx_used_desc = pep->rx_used_desc_q;
817 rx_desc = &pep->p_rx_desc_area[rx_curr_desc];
818 cmd_sts = rx_desc->cmd_sts;
819 rmb();
820 if (cmd_sts & (BUF_OWNED_BY_DMA))
821 break;
822 skb = pep->rx_skb[rx_curr_desc];
823 pep->rx_skb[rx_curr_desc] = NULL;
824
825 rx_next_curr_desc = (rx_curr_desc + 1) % pep->rx_ring_size;
826 pep->rx_curr_desc_q = rx_next_curr_desc;
827
828 /* Rx descriptors exhausted. */
829 /* Set the Rx ring resource error flag */
830 if (rx_next_curr_desc == rx_used_desc)
831 pep->rx_resource_err = 1;
832 pep->rx_desc_count--;
833 dma_unmap_single(NULL, rx_desc->buf_ptr,
834 rx_desc->buf_size,
835 DMA_FROM_DEVICE);
836 received_packets++;
837 /*
838 * Update statistics.
839 * Note byte count includes 4 byte CRC count
840 */
841 stats->rx_packets++;
842 stats->rx_bytes += rx_desc->byte_cnt;
843 /*
844 * In case received a packet without first / last bits on OR
845 * the error summary bit is on, the packets needs to be droped.
846 */
847 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
848 (RX_FIRST_DESC | RX_LAST_DESC))
849 || (cmd_sts & RX_ERROR)) {
850
851 stats->rx_dropped++;
852 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
853 (RX_FIRST_DESC | RX_LAST_DESC)) {
854 if (net_ratelimit())
Antoine Ténart307f6562014-09-30 16:28:07 +0200855 netdev_err(dev,
856 "Rx pkt on multiple desc\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000857 }
858 if (cmd_sts & RX_ERROR)
859 stats->rx_errors++;
860 dev_kfree_skb_irq(skb);
861 } else {
862 /*
863 * The -4 is for the CRC in the trailer of the
864 * received packet
865 */
866 skb_put(skb, rx_desc->byte_cnt - 4);
867 skb->protocol = eth_type_trans(skb, dev);
868 netif_receive_skb(skb);
869 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000870 }
871 /* Fill RX ring with skb's */
872 rxq_refill(dev);
873 return received_packets;
874}
875
876static int pxa168_eth_collect_events(struct pxa168_eth_private *pep,
877 struct net_device *dev)
878{
879 u32 icr;
880 int ret = 0;
881
882 icr = rdl(pep, INT_CAUSE);
883 if (icr == 0)
884 return IRQ_NONE;
885
886 wrl(pep, INT_CAUSE, ~icr);
887 if (icr & (ICR_TXBUF_H | ICR_TXBUF_L)) {
888 pep->work_todo |= WORK_TX_DONE;
889 ret = 1;
890 }
891 if (icr & ICR_RXBUF)
892 ret = 1;
893 if (icr & ICR_MII_CH) {
894 pep->work_todo |= WORK_LINK;
895 ret = 1;
896 }
897 return ret;
898}
899
900static void handle_link_event(struct pxa168_eth_private *pep)
901{
902 struct net_device *dev = pep->dev;
903 u32 port_status;
904 int speed;
905 int duplex;
906 int fc;
907
908 port_status = rdl(pep, PORT_STATUS);
909 if (!(port_status & LINK_UP)) {
910 if (netif_carrier_ok(dev)) {
Antoine Ténart307f6562014-09-30 16:28:07 +0200911 netdev_info(dev, "link down\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000912 netif_carrier_off(dev);
913 txq_reclaim(dev, 1);
914 }
915 return;
916 }
917 if (port_status & PORT_SPEED_100)
918 speed = 100;
919 else
920 speed = 10;
921
922 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
Antoine Ténart09f5da12014-09-30 16:28:10 +0200923 fc = (port_status & FLOW_CONTROL_DISABLED) ? 0 : 1;
Antoine Ténart307f6562014-09-30 16:28:07 +0200924 netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
925 speed, duplex ? "full" : "half", fc ? "en" : "dis");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000926 if (!netif_carrier_ok(dev))
927 netif_carrier_on(dev);
928}
929
930static irqreturn_t pxa168_eth_int_handler(int irq, void *dev_id)
931{
932 struct net_device *dev = (struct net_device *)dev_id;
933 struct pxa168_eth_private *pep = netdev_priv(dev);
934
935 if (unlikely(!pxa168_eth_collect_events(pep, dev)))
936 return IRQ_NONE;
937 /* Disable interrupts */
938 wrl(pep, INT_MASK, 0);
939 napi_schedule(&pep->napi);
940 return IRQ_HANDLED;
941}
942
943static void pxa168_eth_recalc_skb_size(struct pxa168_eth_private *pep)
944{
945 int skb_size;
946
947 /*
948 * Reserve 2+14 bytes for an ethernet header (the hardware
949 * automatically prepends 2 bytes of dummy data to each
950 * received packet), 16 bytes for up to four VLAN tags, and
951 * 4 bytes for the trailing FCS -- 36 bytes total.
952 */
953 skb_size = pep->dev->mtu + 36;
954
955 /*
956 * Make sure that the skb size is a multiple of 8 bytes, as
957 * the lower three bits of the receive descriptor's buffer
958 * size field are ignored by the hardware.
959 */
960 pep->skb_size = (skb_size + 7) & ~7;
961
962 /*
963 * If NET_SKB_PAD is smaller than a cache line,
964 * netdev_alloc_skb() will cause skb->data to be misaligned
965 * to a cache line boundary. If this is the case, include
966 * some extra space to allow re-aligning the data area.
967 */
968 pep->skb_size += SKB_DMA_REALIGN;
969
970}
971
972static int set_port_config_ext(struct pxa168_eth_private *pep)
973{
974 int skb_size;
975
976 pxa168_eth_recalc_skb_size(pep);
977 if (pep->skb_size <= 1518)
978 skb_size = PCXR_MFL_1518;
979 else if (pep->skb_size <= 1536)
980 skb_size = PCXR_MFL_1536;
981 else if (pep->skb_size <= 2048)
982 skb_size = PCXR_MFL_2048;
983 else
984 skb_size = PCXR_MFL_64K;
985
986 /* Extended Port Configuration */
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200987 wrl(pep, PORT_CONFIG_EXT,
988 PCXR_AN_SPEED_DIS | /* Disable HW AN */
989 PCXR_AN_DUPLEX_DIS |
990 PCXR_AN_FLOWCTL_DIS |
991 PCXR_2BSM | /* Two byte prefix aligns IP hdr */
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000992 PCXR_DSCP_EN | /* Enable DSCP in IP */
993 skb_size | PCXR_FLP | /* do not force link pass */
994 PCXR_TX_HIGH_PRI); /* Transmit - high priority queue */
995
996 return 0;
997}
998
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200999static void pxa168_eth_adjust_link(struct net_device *dev)
1000{
1001 struct pxa168_eth_private *pep = netdev_priv(dev);
1002 struct phy_device *phy = pep->phy;
1003 u32 cfg, cfg_o = rdl(pep, PORT_CONFIG);
1004 u32 cfgext, cfgext_o = rdl(pep, PORT_CONFIG_EXT);
1005
1006 cfg = cfg_o & ~PCR_DUPLEX_FULL;
1007 cfgext = cfgext_o & ~(PCXR_SPEED_100 | PCXR_FLOWCTL_DIS | PCXR_RMII_EN);
1008
1009 if (phy->interface == PHY_INTERFACE_MODE_RMII)
1010 cfgext |= PCXR_RMII_EN;
1011 if (phy->speed == SPEED_100)
1012 cfgext |= PCXR_SPEED_100;
1013 if (phy->duplex)
1014 cfg |= PCR_DUPLEX_FULL;
1015 if (!phy->pause)
1016 cfgext |= PCXR_FLOWCTL_DIS;
1017
1018 /* Bail out if there has nothing changed */
1019 if (cfg == cfg_o && cfgext == cfgext_o)
1020 return;
1021
1022 wrl(pep, PORT_CONFIG, cfg);
1023 wrl(pep, PORT_CONFIG_EXT, cfgext);
1024
1025 phy_print_status(phy);
1026}
1027
1028static int pxa168_init_phy(struct net_device *dev)
1029{
1030 struct pxa168_eth_private *pep = netdev_priv(dev);
1031 struct ethtool_cmd cmd;
1032 int err;
1033
1034 if (pep->phy)
1035 return 0;
1036
1037 pep->phy = mdiobus_scan(pep->smi_bus, pep->phy_addr);
1038 if (!pep->phy)
1039 return -ENODEV;
1040
1041 err = phy_connect_direct(dev, pep->phy, pxa168_eth_adjust_link,
1042 pep->phy_intf);
1043 if (err)
1044 return err;
1045
1046 err = pxa168_get_settings(dev, &cmd);
1047 if (err)
1048 return err;
1049
1050 cmd.phy_address = pep->phy_addr;
1051 cmd.speed = pep->phy_speed;
1052 cmd.duplex = pep->phy_duplex;
1053 cmd.advertising = PHY_BASIC_FEATURES;
1054 cmd.autoneg = AUTONEG_ENABLE;
1055
1056 if (cmd.speed != 0)
1057 cmd.autoneg = AUTONEG_DISABLE;
1058
1059 return pxa168_set_settings(dev, &cmd);
1060}
1061
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001062static int pxa168_init_hw(struct pxa168_eth_private *pep)
1063{
1064 int err = 0;
1065
1066 /* Disable interrupts */
1067 wrl(pep, INT_MASK, 0);
1068 wrl(pep, INT_CAUSE, 0);
1069 /* Write to ICR to clear interrupts. */
1070 wrl(pep, INT_W_CLEAR, 0);
1071 /* Abort any transmit and receive operations and put DMA
1072 * in idle state.
1073 */
1074 abort_dma(pep);
1075 /* Initialize address hash table */
1076 err = init_hash_table(pep);
1077 if (err)
1078 return err;
1079 /* SDMA configuration */
1080 wrl(pep, SDMA_CONFIG, SDCR_BSZ8 | /* Burst size = 32 bytes */
1081 SDCR_RIFB | /* Rx interrupt on frame */
1082 SDCR_BLMT | /* Little endian transmit */
1083 SDCR_BLMR | /* Little endian receive */
1084 SDCR_RC_MAX_RETRANS); /* Max retransmit count */
1085 /* Port Configuration */
1086 wrl(pep, PORT_CONFIG, PCR_HS); /* Hash size is 1/2kb */
1087 set_port_config_ext(pep);
1088
1089 return err;
1090}
1091
1092static int rxq_init(struct net_device *dev)
1093{
1094 struct pxa168_eth_private *pep = netdev_priv(dev);
1095 struct rx_desc *p_rx_desc;
1096 int size = 0, i = 0;
1097 int rx_desc_num = pep->rx_ring_size;
1098
1099 /* Allocate RX skb rings */
Lubomir Rintel451bff22013-06-18 19:30:48 +02001100 pep->rx_skb = kzalloc(sizeof(*pep->rx_skb) * pep->rx_ring_size,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001101 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001102 if (!pep->rx_skb)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001103 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001104
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001105 /* Allocate RX ring */
1106 pep->rx_desc_count = 0;
1107 size = pep->rx_ring_size * sizeof(struct rx_desc);
1108 pep->rx_desc_area_size = size;
Joe Perchesede23fa82013-08-26 22:45:23 -07001109 pep->p_rx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1110 &pep->rx_desc_dma,
1111 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00001112 if (!pep->p_rx_desc_area)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001113 goto out;
Joe Perchesd0320f72013-03-14 13:07:21 +00001114
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001115 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Joe Perches64699332012-06-04 12:44:16 +00001116 p_rx_desc = pep->p_rx_desc_area;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001117 for (i = 0; i < rx_desc_num; i++) {
1118 p_rx_desc[i].next_desc_ptr = pep->rx_desc_dma +
1119 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
1120 }
1121 /* Save Rx desc pointer to driver struct. */
1122 pep->rx_curr_desc_q = 0;
1123 pep->rx_used_desc_q = 0;
1124 pep->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
1125 return 0;
1126out:
1127 kfree(pep->rx_skb);
1128 return -ENOMEM;
1129}
1130
1131static void rxq_deinit(struct net_device *dev)
1132{
1133 struct pxa168_eth_private *pep = netdev_priv(dev);
1134 int curr;
1135
1136 /* Free preallocated skb's on RX rings */
1137 for (curr = 0; pep->rx_desc_count && curr < pep->rx_ring_size; curr++) {
1138 if (pep->rx_skb[curr]) {
1139 dev_kfree_skb(pep->rx_skb[curr]);
1140 pep->rx_desc_count--;
1141 }
1142 }
1143 if (pep->rx_desc_count)
Antoine Ténart307f6562014-09-30 16:28:07 +02001144 netdev_err(dev, "Error in freeing Rx Ring. %d skb's still\n",
1145 pep->rx_desc_count);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001146 /* Free RX ring */
1147 if (pep->p_rx_desc_area)
1148 dma_free_coherent(pep->dev->dev.parent, pep->rx_desc_area_size,
1149 pep->p_rx_desc_area, pep->rx_desc_dma);
1150 kfree(pep->rx_skb);
1151}
1152
1153static int txq_init(struct net_device *dev)
1154{
1155 struct pxa168_eth_private *pep = netdev_priv(dev);
1156 struct tx_desc *p_tx_desc;
1157 int size = 0, i = 0;
1158 int tx_desc_num = pep->tx_ring_size;
1159
Lubomir Rintel451bff22013-06-18 19:30:48 +02001160 pep->tx_skb = kzalloc(sizeof(*pep->tx_skb) * pep->tx_ring_size,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001161 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001162 if (!pep->tx_skb)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001163 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001164
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001165 /* Allocate TX ring */
1166 pep->tx_desc_count = 0;
1167 size = pep->tx_ring_size * sizeof(struct tx_desc);
1168 pep->tx_desc_area_size = size;
Joe Perchesede23fa82013-08-26 22:45:23 -07001169 pep->p_tx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1170 &pep->tx_desc_dma,
1171 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00001172 if (!pep->p_tx_desc_area)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001173 goto out;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001174 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Joe Perches64699332012-06-04 12:44:16 +00001175 p_tx_desc = pep->p_tx_desc_area;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001176 for (i = 0; i < tx_desc_num; i++) {
1177 p_tx_desc[i].next_desc_ptr = pep->tx_desc_dma +
1178 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
1179 }
1180 pep->tx_curr_desc_q = 0;
1181 pep->tx_used_desc_q = 0;
1182 pep->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
1183 return 0;
1184out:
1185 kfree(pep->tx_skb);
1186 return -ENOMEM;
1187}
1188
1189static void txq_deinit(struct net_device *dev)
1190{
1191 struct pxa168_eth_private *pep = netdev_priv(dev);
1192
1193 /* Free outstanding skb's on TX ring */
1194 txq_reclaim(dev, 1);
1195 BUG_ON(pep->tx_used_desc_q != pep->tx_curr_desc_q);
1196 /* Free TX ring */
1197 if (pep->p_tx_desc_area)
1198 dma_free_coherent(pep->dev->dev.parent, pep->tx_desc_area_size,
1199 pep->p_tx_desc_area, pep->tx_desc_dma);
1200 kfree(pep->tx_skb);
1201}
1202
1203static int pxa168_eth_open(struct net_device *dev)
1204{
1205 struct pxa168_eth_private *pep = netdev_priv(dev);
1206 int err;
1207
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +02001208 err = pxa168_init_phy(dev);
1209 if (err)
1210 return err;
1211
Michael Opdenacker599c2e12013-09-13 06:04:23 +02001212 err = request_irq(dev->irq, pxa168_eth_int_handler, 0, dev->name, dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001213 if (err) {
Joe Perchesf7b4fb22012-10-27 22:05:48 +00001214 dev_err(&dev->dev, "can't assign irq\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001215 return -EAGAIN;
1216 }
1217 pep->rx_resource_err = 0;
1218 err = rxq_init(dev);
1219 if (err != 0)
1220 goto out_free_irq;
1221 err = txq_init(dev);
1222 if (err != 0)
1223 goto out_free_rx_skb;
1224 pep->rx_used_desc_q = 0;
1225 pep->rx_curr_desc_q = 0;
1226
1227 /* Fill RX ring with skb's */
1228 rxq_refill(dev);
1229 pep->rx_used_desc_q = 0;
1230 pep->rx_curr_desc_q = 0;
1231 netif_carrier_off(dev);
1232 eth_port_start(dev);
1233 napi_enable(&pep->napi);
1234 return 0;
1235out_free_rx_skb:
1236 rxq_deinit(dev);
1237out_free_irq:
1238 free_irq(dev->irq, dev);
1239 return err;
1240}
1241
1242static int pxa168_eth_stop(struct net_device *dev)
1243{
1244 struct pxa168_eth_private *pep = netdev_priv(dev);
1245 eth_port_reset(dev);
1246
1247 /* Disable interrupts */
1248 wrl(pep, INT_MASK, 0);
1249 wrl(pep, INT_CAUSE, 0);
1250 /* Write to ICR to clear interrupts. */
1251 wrl(pep, INT_W_CLEAR, 0);
1252 napi_disable(&pep->napi);
1253 del_timer_sync(&pep->timeout);
1254 netif_carrier_off(dev);
1255 free_irq(dev->irq, dev);
1256 rxq_deinit(dev);
1257 txq_deinit(dev);
1258
1259 return 0;
1260}
1261
1262static int pxa168_eth_change_mtu(struct net_device *dev, int mtu)
1263{
1264 int retval;
1265 struct pxa168_eth_private *pep = netdev_priv(dev);
1266
1267 if ((mtu > 9500) || (mtu < 68))
1268 return -EINVAL;
1269
1270 dev->mtu = mtu;
1271 retval = set_port_config_ext(pep);
1272
1273 if (!netif_running(dev))
1274 return 0;
1275
1276 /*
1277 * Stop and then re-open the interface. This will allocate RX
1278 * skbs of the new MTU.
1279 * There is a possible danger that the open will not succeed,
1280 * due to memory being full.
1281 */
1282 pxa168_eth_stop(dev);
1283 if (pxa168_eth_open(dev)) {
Joe Perchesf7b4fb22012-10-27 22:05:48 +00001284 dev_err(&dev->dev,
1285 "fatal error on re-opening device after MTU change\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001286 }
1287
1288 return 0;
1289}
1290
1291static int eth_alloc_tx_desc_index(struct pxa168_eth_private *pep)
1292{
1293 int tx_desc_curr;
1294
1295 tx_desc_curr = pep->tx_curr_desc_q;
1296 pep->tx_curr_desc_q = (tx_desc_curr + 1) % pep->tx_ring_size;
1297 BUG_ON(pep->tx_curr_desc_q == pep->tx_used_desc_q);
1298 pep->tx_desc_count++;
1299
1300 return tx_desc_curr;
1301}
1302
1303static int pxa168_rx_poll(struct napi_struct *napi, int budget)
1304{
1305 struct pxa168_eth_private *pep =
1306 container_of(napi, struct pxa168_eth_private, napi);
1307 struct net_device *dev = pep->dev;
1308 int work_done = 0;
1309
1310 if (unlikely(pep->work_todo & WORK_LINK)) {
1311 pep->work_todo &= ~(WORK_LINK);
1312 handle_link_event(pep);
1313 }
1314 /*
1315 * We call txq_reclaim every time since in NAPI interupts are disabled
1316 * and due to this we miss the TX_DONE interrupt,which is not updated in
1317 * interrupt status register.
1318 */
1319 txq_reclaim(dev, 0);
1320 if (netif_queue_stopped(dev)
1321 && pep->tx_ring_size - pep->tx_desc_count > 1) {
1322 netif_wake_queue(dev);
1323 }
1324 work_done = rxq_process(dev, budget);
1325 if (work_done < budget) {
1326 napi_complete(napi);
1327 wrl(pep, INT_MASK, ALL_INTS);
1328 }
1329
1330 return work_done;
1331}
1332
1333static int pxa168_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1334{
1335 struct pxa168_eth_private *pep = netdev_priv(dev);
1336 struct net_device_stats *stats = &dev->stats;
1337 struct tx_desc *desc;
1338 int tx_index;
1339 int length;
1340
1341 tx_index = eth_alloc_tx_desc_index(pep);
1342 desc = &pep->p_tx_desc_area[tx_index];
1343 length = skb->len;
1344 pep->tx_skb[tx_index] = skb;
1345 desc->byte_cnt = length;
1346 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Richard Cochran1f6e44a2011-06-19 21:51:31 +00001347
1348 skb_tx_timestamp(skb);
1349
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001350 wmb();
1351 desc->cmd_sts = BUF_OWNED_BY_DMA | TX_GEN_CRC | TX_FIRST_DESC |
1352 TX_ZERO_PADDING | TX_LAST_DESC | TX_EN_INT;
1353 wmb();
1354 wrl(pep, SDMA_CMD, SDMA_CMD_TXDH | SDMA_CMD_ERD);
1355
Richard Cochran38442042011-06-19 21:48:06 +00001356 stats->tx_bytes += length;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001357 stats->tx_packets++;
1358 dev->trans_start = jiffies;
1359 if (pep->tx_ring_size - pep->tx_desc_count <= 1) {
1360 /* We handled the current skb, but now we are out of space.*/
1361 netif_stop_queue(dev);
1362 }
1363
1364 return NETDEV_TX_OK;
1365}
1366
1367static int smi_wait_ready(struct pxa168_eth_private *pep)
1368{
1369 int i = 0;
1370
1371 /* wait for the SMI register to become available */
1372 for (i = 0; rdl(pep, SMI) & SMI_BUSY; i++) {
1373 if (i == PHY_WAIT_ITERATIONS)
1374 return -ETIMEDOUT;
1375 msleep(10);
1376 }
1377
1378 return 0;
1379}
1380
1381static int pxa168_smi_read(struct mii_bus *bus, int phy_addr, int regnum)
1382{
1383 struct pxa168_eth_private *pep = bus->priv;
1384 int i = 0;
1385 int val;
1386
1387 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001388 netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001389 return -ETIMEDOUT;
1390 }
1391 wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) | SMI_OP_R);
1392 /* now wait for the data to be valid */
1393 for (i = 0; !((val = rdl(pep, SMI)) & SMI_R_VALID); i++) {
1394 if (i == PHY_WAIT_ITERATIONS) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001395 netdev_warn(pep->dev,
1396 "pxa168_eth: SMI bus read not valid\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001397 return -ENODEV;
1398 }
1399 msleep(10);
1400 }
1401
1402 return val & 0xffff;
1403}
1404
1405static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
1406 u16 value)
1407{
1408 struct pxa168_eth_private *pep = bus->priv;
1409
1410 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001411 netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001412 return -ETIMEDOUT;
1413 }
1414
1415 wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) |
1416 SMI_OP_W | (value & 0xffff));
1417
1418 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001419 netdev_err(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001420 return -ETIMEDOUT;
1421 }
1422
1423 return 0;
1424}
1425
1426static int pxa168_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1427 int cmd)
1428{
1429 struct pxa168_eth_private *pep = netdev_priv(dev);
1430 if (pep->phy != NULL)
Dan Carpenter4f2c8512010-08-24 06:54:20 +00001431 return phy_mii_ioctl(pep->phy, ifr, cmd);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001432
1433 return -EOPNOTSUPP;
1434}
1435
1436static struct phy_device *phy_scan(struct pxa168_eth_private *pep, int phy_addr)
1437{
1438 struct mii_bus *bus = pep->smi_bus;
1439 struct phy_device *phydev;
1440 int start;
1441 int num;
1442 int i;
1443
1444 if (phy_addr == PXA168_ETH_PHY_ADDR_DEFAULT) {
1445 /* Scan entire range */
1446 start = ethernet_phy_get(pep);
1447 num = 32;
1448 } else {
1449 /* Use phy addr specific to platform */
1450 start = phy_addr & 0x1f;
1451 num = 1;
1452 }
1453 phydev = NULL;
1454 for (i = 0; i < num; i++) {
1455 int addr = (start + i) & 0x1f;
1456 if (bus->phy_map[addr] == NULL)
1457 mdiobus_scan(bus, addr);
1458
1459 if (phydev == NULL) {
1460 phydev = bus->phy_map[addr];
1461 if (phydev != NULL)
1462 ethernet_phy_set_addr(pep, addr);
1463 }
1464 }
1465
1466 return phydev;
1467}
1468
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001469static void phy_init(struct pxa168_eth_private *pep)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001470{
1471 struct phy_device *phy = pep->phy;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001472
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001473 phy_attach(pep->dev, dev_name(&phy->dev), pep->phy_intf);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001474
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001475 phy->speed = pep->phy_speed;
1476 phy->duplex = pep->phy_duplex;
1477 phy->autoneg = AUTONEG_ENABLE;
1478 phy->supported &= PHY_BASIC_FEATURES;
1479 phy->advertising = phy->supported | ADVERTISED_Autoneg;
1480
1481 if (pep->phy_speed != 0) {
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001482 phy->autoneg = AUTONEG_DISABLE;
1483 phy->advertising = 0;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001484 }
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001485
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001486 phy_start_aneg(phy);
1487}
1488
1489static int ethernet_phy_setup(struct net_device *dev)
1490{
1491 struct pxa168_eth_private *pep = netdev_priv(dev);
1492
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001493 pep->phy = phy_scan(pep, pep->phy_addr & 0x1f);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001494 if (pep->phy != NULL)
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001495 phy_init(pep);
1496
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001497 update_hash_table_mac_address(pep, NULL, dev->dev_addr);
1498
1499 return 0;
1500}
1501
1502static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1503{
1504 struct pxa168_eth_private *pep = netdev_priv(dev);
1505 int err;
1506
1507 err = phy_read_status(pep->phy);
1508 if (err == 0)
1509 err = phy_ethtool_gset(pep->phy, cmd);
1510
1511 return err;
1512}
1513
1514static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1515{
1516 struct pxa168_eth_private *pep = netdev_priv(dev);
1517
1518 return phy_ethtool_sset(pep->phy, cmd);
1519}
1520
1521static void pxa168_get_drvinfo(struct net_device *dev,
1522 struct ethtool_drvinfo *info)
1523{
Jiri Pirko7826d432013-01-06 00:44:26 +00001524 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1525 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
1526 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1527 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001528}
1529
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001530static const struct ethtool_ops pxa168_ethtool_ops = {
Antoine Ténart307f6562014-09-30 16:28:07 +02001531 .get_settings = pxa168_get_settings,
1532 .set_settings = pxa168_set_settings,
1533 .get_drvinfo = pxa168_get_drvinfo,
1534 .get_link = ethtool_op_get_link,
1535 .get_ts_info = ethtool_op_get_ts_info,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001536};
1537
1538static const struct net_device_ops pxa168_eth_netdev_ops = {
Antoine Ténart307f6562014-09-30 16:28:07 +02001539 .ndo_open = pxa168_eth_open,
1540 .ndo_stop = pxa168_eth_stop,
1541 .ndo_start_xmit = pxa168_eth_start_xmit,
1542 .ndo_set_rx_mode = pxa168_eth_set_rx_mode,
1543 .ndo_set_mac_address = pxa168_eth_set_mac_address,
1544 .ndo_validate_addr = eth_validate_addr,
1545 .ndo_do_ioctl = pxa168_eth_do_ioctl,
1546 .ndo_change_mtu = pxa168_eth_change_mtu,
1547 .ndo_tx_timeout = pxa168_eth_tx_timeout,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001548};
1549
1550static int pxa168_eth_probe(struct platform_device *pdev)
1551{
1552 struct pxa168_eth_private *pep = NULL;
1553 struct net_device *dev = NULL;
1554 struct resource *res;
1555 struct clk *clk;
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001556 struct device_node *np;
Antoine Ténart78b9b2c2014-09-30 16:28:12 +02001557 const unsigned char *mac_addr = NULL;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001558 int err;
1559
1560 printk(KERN_NOTICE "PXA168 10/100 Ethernet Driver\n");
1561
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001562 clk = devm_clk_get(&pdev->dev, NULL);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001563 if (IS_ERR(clk)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001564 dev_err(&pdev->dev, "Fast Ethernet failed to get clock\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001565 return -ENODEV;
1566 }
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001567 clk_prepare_enable(clk);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001568
1569 dev = alloc_etherdev(sizeof(struct pxa168_eth_private));
1570 if (!dev) {
1571 err = -ENOMEM;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001572 goto err_clk;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001573 }
1574
1575 platform_set_drvdata(pdev, dev);
1576 pep = netdev_priv(dev);
1577 pep->dev = dev;
1578 pep->clk = clk;
1579 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1580 if (res == NULL) {
1581 err = -ENODEV;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001582 goto err_netdev;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001583 }
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001584 pep->base = devm_ioremap_resource(&pdev->dev, res);
1585 if (IS_ERR(pep->base)) {
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001586 err = -ENOMEM;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001587 goto err_netdev;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001588 }
1589 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1590 BUG_ON(!res);
1591 dev->irq = res->start;
1592 dev->netdev_ops = &pxa168_eth_netdev_ops;
1593 dev->watchdog_timeo = 2 * HZ;
1594 dev->base_addr = 0;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001595 dev->ethtool_ops = &pxa168_ethtool_ops;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001596
1597 INIT_WORK(&pep->tx_timeout_task, pxa168_eth_tx_timeout_task);
1598
Antoine Ténart78b9b2c2014-09-30 16:28:12 +02001599 if (pdev->dev.of_node)
1600 mac_addr = of_get_mac_address(pdev->dev.of_node);
1601
1602 if (mac_addr && is_valid_ether_addr(mac_addr)) {
1603 ether_addr_copy(dev->dev_addr, mac_addr);
1604 } else {
1605 /* try reading the mac address, if set by the bootloader */
1606 pxa168_eth_get_mac_address(dev, dev->dev_addr);
1607 if (!is_valid_ether_addr(dev->dev_addr)) {
1608 dev_info(&pdev->dev, "Using random mac address\n");
1609 eth_hw_addr_random(dev);
1610 }
1611 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001612
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001613 pep->rx_ring_size = NUM_RX_DESCS;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001614 pep->tx_ring_size = NUM_TX_DESCS;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001615
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001616 pep->pd = dev_get_platdata(&pdev->dev);
1617 if (pep->pd) {
1618 if (pep->pd->rx_queue_size)
1619 pep->rx_ring_size = pep->pd->rx_queue_size;
1620
1621 if (pep->pd->tx_queue_size)
1622 pep->tx_ring_size = pep->pd->tx_queue_size;
1623
1624 pep->port_num = pep->pd->port_number;
1625 pep->phy_addr = pep->pd->phy_addr;
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001626 pep->phy_speed = pep->pd->speed;
1627 pep->phy_duplex = pep->pd->duplex;
1628 pep->phy_intf = pep->pd->intf;
1629
1630 if (pep->pd->init)
1631 pep->pd->init();
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001632 } else if (pdev->dev.of_node) {
1633 of_property_read_u32(pdev->dev.of_node, "port-id",
1634 &pep->port_num);
1635
1636 np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001637 if (!np) {
1638 dev_err(&pdev->dev, "missing phy-handle\n");
1639 return -EINVAL;
1640 }
1641 of_property_read_u32(np, "reg", &pep->phy_addr);
1642 pep->phy_intf = of_get_phy_mode(pdev->dev.of_node);
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001643 }
1644
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001645 /* Hardware supports only 3 ports */
1646 BUG_ON(pep->port_num > 2);
1647 netif_napi_add(dev, &pep->napi, pxa168_rx_poll, pep->rx_ring_size);
1648
1649 memset(&pep->timeout, 0, sizeof(struct timer_list));
1650 init_timer(&pep->timeout);
1651 pep->timeout.function = rxq_refill_timer_wrapper;
1652 pep->timeout.data = (unsigned long)pep;
1653
1654 pep->smi_bus = mdiobus_alloc();
1655 if (pep->smi_bus == NULL) {
1656 err = -ENOMEM;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001657 goto err_base;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001658 }
1659 pep->smi_bus->priv = pep;
1660 pep->smi_bus->name = "pxa168_eth smi";
1661 pep->smi_bus->read = pxa168_smi_read;
1662 pep->smi_bus->write = pxa168_smi_write;
Florian Fainellid073a102012-01-09 23:59:16 +00001663 snprintf(pep->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1664 pdev->name, pdev->id);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001665 pep->smi_bus->parent = &pdev->dev;
1666 pep->smi_bus->phy_mask = 0xffffffff;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001667 err = mdiobus_register(pep->smi_bus);
1668 if (err)
1669 goto err_free_mdio;
1670
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001671 pxa168_init_hw(pep);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001672 SET_NETDEV_DEV(dev, &pdev->dev);
1673 err = register_netdev(dev);
1674 if (err)
Dan Carpenter945c7c72010-08-24 06:53:33 +00001675 goto err_mdiobus;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001676 return 0;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001677
1678err_mdiobus:
1679 mdiobus_unregister(pep->smi_bus);
1680err_free_mdio:
1681 mdiobus_free(pep->smi_bus);
1682err_base:
1683 iounmap(pep->base);
1684err_netdev:
1685 free_netdev(dev);
1686err_clk:
1687 clk_disable(clk);
1688 clk_put(clk);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001689 return err;
1690}
1691
1692static int pxa168_eth_remove(struct platform_device *pdev)
1693{
1694 struct net_device *dev = platform_get_drvdata(pdev);
1695 struct pxa168_eth_private *pep = netdev_priv(dev);
1696
1697 if (pep->htpr) {
1698 dma_free_coherent(pep->dev->dev.parent, HASH_ADDR_TABLE_SIZE,
1699 pep->htpr, pep->htpr_dma);
1700 pep->htpr = NULL;
1701 }
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +02001702 if (pep->phy)
1703 phy_disconnect(pep->phy);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001704 if (pep->clk) {
1705 clk_disable(pep->clk);
1706 clk_put(pep->clk);
1707 pep->clk = NULL;
1708 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001709
1710 iounmap(pep->base);
1711 pep->base = NULL;
Denis Kirjanov9c01ae52010-08-29 21:21:38 +00001712 mdiobus_unregister(pep->smi_bus);
1713 mdiobus_free(pep->smi_bus);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001714 unregister_netdev(dev);
Tejun Heo23f333a2010-12-12 16:45:14 +01001715 cancel_work_sync(&pep->tx_timeout_task);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001716 free_netdev(dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001717 return 0;
1718}
1719
1720static void pxa168_eth_shutdown(struct platform_device *pdev)
1721{
1722 struct net_device *dev = platform_get_drvdata(pdev);
1723 eth_port_reset(dev);
1724}
1725
1726#ifdef CONFIG_PM
1727static int pxa168_eth_resume(struct platform_device *pdev)
1728{
1729 return -ENOSYS;
1730}
1731
1732static int pxa168_eth_suspend(struct platform_device *pdev, pm_message_t state)
1733{
1734 return -ENOSYS;
1735}
1736
1737#else
1738#define pxa168_eth_resume NULL
1739#define pxa168_eth_suspend NULL
1740#endif
1741
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001742static const struct of_device_id pxa168_eth_of_match[] = {
1743 { .compatible = "marvell,pxa168-eth" },
1744 { },
1745};
1746MODULE_DEVICE_TABLE(of, pxa168_eth_of_match);
1747
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001748static struct platform_driver pxa168_eth_driver = {
1749 .probe = pxa168_eth_probe,
1750 .remove = pxa168_eth_remove,
1751 .shutdown = pxa168_eth_shutdown,
1752 .resume = pxa168_eth_resume,
1753 .suspend = pxa168_eth_suspend,
1754 .driver = {
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001755 .name = DRIVER_NAME,
1756 .of_match_table = of_match_ptr(pxa168_eth_of_match),
1757 },
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001758};
1759
Axel Lindb62f682011-11-27 16:44:17 +00001760module_platform_driver(pxa168_eth_driver);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001761
1762MODULE_LICENSE("GPL");
1763MODULE_DESCRIPTION("Ethernet driver for Marvell PXA168");
1764MODULE_ALIAS("platform:pxa168_eth");