blob: 28cb36d9e50a24a798705ded2f8eb981c9f9ac63 [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 */
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000180#define WORK_TX_DONE (1 << 1)
181
182/*
183 * Misc definitions.
184 */
185#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
186
187struct rx_desc {
188 u32 cmd_sts; /* Descriptor command status */
189 u16 byte_cnt; /* Descriptor buffer byte count */
190 u16 buf_size; /* Buffer size */
191 u32 buf_ptr; /* Descriptor buffer pointer */
192 u32 next_desc_ptr; /* Next descriptor pointer */
193};
194
195struct tx_desc {
196 u32 cmd_sts; /* Command/status field */
197 u16 reserved;
198 u16 byte_cnt; /* buffer byte count */
199 u32 buf_ptr; /* pointer to buffer for this descriptor */
200 u32 next_desc_ptr; /* Pointer to next descriptor */
201};
202
203struct pxa168_eth_private {
204 int port_num; /* User Ethernet port number */
Antoine Ténart43d3ddf2014-09-30 16:28:08 +0200205 int phy_addr;
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +0200206 int phy_speed;
207 int phy_duplex;
208 phy_interface_t phy_intf;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000209
210 int rx_resource_err; /* Rx ring resource error flag */
211
212 /* Next available and first returning Rx resource */
213 int rx_curr_desc_q, rx_used_desc_q;
214
215 /* Next available and first returning Tx resource */
216 int tx_curr_desc_q, tx_used_desc_q;
217
218 struct rx_desc *p_rx_desc_area;
219 dma_addr_t rx_desc_dma;
220 int rx_desc_area_size;
221 struct sk_buff **rx_skb;
222
223 struct tx_desc *p_tx_desc_area;
224 dma_addr_t tx_desc_dma;
225 int tx_desc_area_size;
226 struct sk_buff **tx_skb;
227
228 struct work_struct tx_timeout_task;
229
230 struct net_device *dev;
231 struct napi_struct napi;
232 u8 work_todo;
233 int skb_size;
234
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000235 /* Size of Tx Ring per queue */
236 int tx_ring_size;
237 /* Number of tx descriptors in use */
238 int tx_desc_count;
239 /* Size of Rx Ring per queue */
240 int rx_ring_size;
241 /* Number of rx descriptors in use */
242 int rx_desc_count;
243
244 /*
245 * Used in case RX Ring is empty, which can occur when
246 * system does not have resources (skb's)
247 */
248 struct timer_list timeout;
249 struct mii_bus *smi_bus;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000250
251 /* clock */
252 struct clk *clk;
253 struct pxa168_eth_platform_data *pd;
254 /*
255 * Ethernet controller base address.
256 */
257 void __iomem *base;
258
259 /* Pointer to the hardware address filter table */
260 void *htpr;
261 dma_addr_t htpr_dma;
262};
263
264struct addr_table_entry {
265 __le32 lo;
266 __le32 hi;
267};
268
269/* Bit fields of a Hash Table Entry */
270enum hash_table_entry {
271 HASH_ENTRY_VALID = 1,
272 SKIP = 2,
273 HASH_ENTRY_RECEIVE_DISCARD = 4,
274 HASH_ENTRY_RECEIVE_DISCARD_BIT = 2
275};
276
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000277static int pxa168_init_hw(struct pxa168_eth_private *pep);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200278static int pxa168_init_phy(struct net_device *dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000279static void eth_port_reset(struct net_device *dev);
280static void eth_port_start(struct net_device *dev);
281static int pxa168_eth_open(struct net_device *dev);
282static int pxa168_eth_stop(struct net_device *dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000283
284static inline u32 rdl(struct pxa168_eth_private *pep, int offset)
285{
Jisheng Zhang3ed68782016-05-13 19:57:29 +0800286 return readl_relaxed(pep->base + offset);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000287}
288
289static inline void wrl(struct pxa168_eth_private *pep, int offset, u32 data)
290{
Jisheng Zhang3ed68782016-05-13 19:57:29 +0800291 writel_relaxed(data, pep->base + offset);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000292}
293
294static void abort_dma(struct pxa168_eth_private *pep)
295{
296 int delay;
297 int max_retries = 40;
298
299 do {
300 wrl(pep, SDMA_CMD, SDMA_CMD_AR | SDMA_CMD_AT);
301 udelay(100);
302
303 delay = 10;
304 while ((rdl(pep, SDMA_CMD) & (SDMA_CMD_AR | SDMA_CMD_AT))
305 && delay-- > 0) {
306 udelay(10);
307 }
308 } while (max_retries-- > 0 && delay <= 0);
309
310 if (max_retries <= 0)
Antoine Ténart307f6562014-09-30 16:28:07 +0200311 netdev_err(pep->dev, "%s : DMA Stuck\n", __func__);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000312}
313
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000314static void rxq_refill(struct net_device *dev)
315{
316 struct pxa168_eth_private *pep = netdev_priv(dev);
317 struct sk_buff *skb;
318 struct rx_desc *p_used_rx_desc;
319 int used_rx_desc;
320
321 while (pep->rx_desc_count < pep->rx_ring_size) {
322 int size;
323
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000324 skb = netdev_alloc_skb(dev, pep->skb_size);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000325 if (!skb)
326 break;
327 if (SKB_DMA_REALIGN)
328 skb_reserve(skb, SKB_DMA_REALIGN);
329 pep->rx_desc_count++;
330 /* Get 'used' Rx descriptor */
331 used_rx_desc = pep->rx_used_desc_q;
332 p_used_rx_desc = &pep->p_rx_desc_area[used_rx_desc];
Isaku Yamahata511efbb2013-06-14 17:58:34 +0900333 size = skb_end_pointer(skb) - skb->data;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000334 p_used_rx_desc->buf_ptr = dma_map_single(NULL,
335 skb->data,
336 size,
337 DMA_FROM_DEVICE);
338 p_used_rx_desc->buf_size = size;
339 pep->rx_skb[used_rx_desc] = skb;
340
341 /* Return the descriptor to DMA ownership */
Jisheng Zhangb17d1552016-05-13 19:57:30 +0800342 dma_wmb();
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000343 p_used_rx_desc->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
Jisheng Zhangb17d1552016-05-13 19:57:30 +0800344 dma_wmb();
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000345
346 /* Move the used descriptor pointer to the next descriptor */
347 pep->rx_used_desc_q = (used_rx_desc + 1) % pep->rx_ring_size;
348
349 /* Any Rx return cancels the Rx resource error status */
350 pep->rx_resource_err = 0;
351
352 skb_reserve(skb, ETH_HW_IP_ALIGN);
353 }
354
355 /*
356 * If RX ring is empty of SKB, set a timer to try allocating
357 * again at a later time.
358 */
359 if (pep->rx_desc_count == 0) {
360 pep->timeout.expires = jiffies + (HZ / 10);
361 add_timer(&pep->timeout);
362 }
363}
364
365static inline void rxq_refill_timer_wrapper(unsigned long data)
366{
367 struct pxa168_eth_private *pep = (void *)data;
368 napi_schedule(&pep->napi);
369}
370
371static inline u8 flip_8_bits(u8 x)
372{
373 return (((x) & 0x01) << 3) | (((x) & 0x02) << 1)
374 | (((x) & 0x04) >> 1) | (((x) & 0x08) >> 3)
375 | (((x) & 0x10) << 3) | (((x) & 0x20) << 1)
376 | (((x) & 0x40) >> 1) | (((x) & 0x80) >> 3);
377}
378
379static void nibble_swap_every_byte(unsigned char *mac_addr)
380{
381 int i;
382 for (i = 0; i < ETH_ALEN; i++) {
383 mac_addr[i] = ((mac_addr[i] & 0x0f) << 4) |
384 ((mac_addr[i] & 0xf0) >> 4);
385 }
386}
387
388static void inverse_every_nibble(unsigned char *mac_addr)
389{
390 int i;
391 for (i = 0; i < ETH_ALEN; i++)
392 mac_addr[i] = flip_8_bits(mac_addr[i]);
393}
394
395/*
396 * ----------------------------------------------------------------------------
397 * This function will calculate the hash function of the address.
398 * Inputs
399 * mac_addr_orig - MAC address.
400 * Outputs
401 * return the calculated entry.
402 */
403static u32 hash_function(unsigned char *mac_addr_orig)
404{
405 u32 hash_result;
406 u32 addr0;
407 u32 addr1;
408 u32 addr2;
409 u32 addr3;
410 unsigned char mac_addr[ETH_ALEN];
411
412 /* Make a copy of MAC address since we are going to performe bit
413 * operations on it
414 */
415 memcpy(mac_addr, mac_addr_orig, ETH_ALEN);
416
417 nibble_swap_every_byte(mac_addr);
418 inverse_every_nibble(mac_addr);
419
420 addr0 = (mac_addr[5] >> 2) & 0x3f;
421 addr1 = (mac_addr[5] & 0x03) | (((mac_addr[4] & 0x7f)) << 2);
422 addr2 = ((mac_addr[4] & 0x80) >> 7) | mac_addr[3] << 1;
423 addr3 = (mac_addr[2] & 0xff) | ((mac_addr[1] & 1) << 8);
424
425 hash_result = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
426 hash_result = hash_result & 0x07ff;
427 return hash_result;
428}
429
430/*
431 * ----------------------------------------------------------------------------
432 * This function will add/del an entry to the address table.
433 * Inputs
434 * pep - ETHERNET .
435 * mac_addr - MAC address.
436 * skip - if 1, skip this address.Used in case of deleting an entry which is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300437 * part of chain in the hash table.We can't just delete the entry since
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000438 * that will break the chain.We need to defragment the tables time to
439 * time.
440 * rd - 0 Discard packet upon match.
441 * - 1 Receive packet upon match.
442 * Outputs
443 * address table entry is added/deleted.
444 * 0 if success.
445 * -ENOSPC if table full
446 */
447static int add_del_hash_entry(struct pxa168_eth_private *pep,
448 unsigned char *mac_addr,
449 u32 rd, u32 skip, int del)
450{
451 struct addr_table_entry *entry, *start;
452 u32 new_high;
453 u32 new_low;
454 u32 i;
455
456 new_low = (((mac_addr[1] >> 4) & 0xf) << 15)
457 | (((mac_addr[1] >> 0) & 0xf) << 11)
458 | (((mac_addr[0] >> 4) & 0xf) << 7)
459 | (((mac_addr[0] >> 0) & 0xf) << 3)
460 | (((mac_addr[3] >> 4) & 0x1) << 31)
461 | (((mac_addr[3] >> 0) & 0xf) << 27)
462 | (((mac_addr[2] >> 4) & 0xf) << 23)
463 | (((mac_addr[2] >> 0) & 0xf) << 19)
464 | (skip << SKIP) | (rd << HASH_ENTRY_RECEIVE_DISCARD_BIT)
465 | HASH_ENTRY_VALID;
466
467 new_high = (((mac_addr[5] >> 4) & 0xf) << 15)
468 | (((mac_addr[5] >> 0) & 0xf) << 11)
469 | (((mac_addr[4] >> 4) & 0xf) << 7)
470 | (((mac_addr[4] >> 0) & 0xf) << 3)
471 | (((mac_addr[3] >> 5) & 0x7) << 0);
472
473 /*
474 * Pick the appropriate table, start scanning for free/reusable
475 * entries at the index obtained by hashing the specified MAC address
476 */
Joe Perches43d620c2011-06-16 19:08:06 +0000477 start = pep->htpr;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000478 entry = start + hash_function(mac_addr);
479 for (i = 0; i < HOP_NUMBER; i++) {
480 if (!(le32_to_cpu(entry->lo) & HASH_ENTRY_VALID)) {
481 break;
482 } else {
483 /* if same address put in same position */
484 if (((le32_to_cpu(entry->lo) & 0xfffffff8) ==
485 (new_low & 0xfffffff8)) &&
486 (le32_to_cpu(entry->hi) == new_high)) {
487 break;
488 }
489 }
490 if (entry == start + 0x7ff)
491 entry = start;
492 else
493 entry++;
494 }
495
496 if (((le32_to_cpu(entry->lo) & 0xfffffff8) != (new_low & 0xfffffff8)) &&
497 (le32_to_cpu(entry->hi) != new_high) && del)
498 return 0;
499
500 if (i == HOP_NUMBER) {
501 if (!del) {
Antoine Ténart307f6562014-09-30 16:28:07 +0200502 netdev_info(pep->dev,
503 "%s: table section is full, need to "
504 "move to 16kB implementation?\n",
505 __FILE__);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000506 return -ENOSPC;
507 } else
508 return 0;
509 }
510
511 /*
512 * Update the selected entry
513 */
514 if (del) {
515 entry->hi = 0;
516 entry->lo = 0;
517 } else {
518 entry->hi = cpu_to_le32(new_high);
519 entry->lo = cpu_to_le32(new_low);
520 }
521
522 return 0;
523}
524
525/*
526 * ----------------------------------------------------------------------------
527 * Create an addressTable entry from MAC address info
528 * found in the specifed net_device struct
529 *
530 * Input : pointer to ethernet interface network device structure
531 * Output : N/A
532 */
533static void update_hash_table_mac_address(struct pxa168_eth_private *pep,
534 unsigned char *oaddr,
535 unsigned char *addr)
536{
537 /* Delete old entry */
538 if (oaddr)
539 add_del_hash_entry(pep, oaddr, 1, 0, HASH_DELETE);
540 /* Add new entry */
541 add_del_hash_entry(pep, addr, 1, 0, HASH_ADD);
542}
543
544static int init_hash_table(struct pxa168_eth_private *pep)
545{
546 /*
547 * Hardware expects CPU to build a hash table based on a predefined
548 * hash function and populate it based on hardware address. The
549 * location of the hash table is identified by 32-bit pointer stored
550 * in HTPR internal register. Two possible sizes exists for the hash
551 * table 8kB (256kB of DRAM required (4 x 64 kB banks)) and 1/2kB
552 * (16kB of DRAM required (4 x 4 kB banks)).We currently only support
553 * 1/2kB.
554 */
555 /* TODO: Add support for 8kB hash table and alternative hash
556 * function.Driver can dynamically switch to them if the 1/2kB hash
557 * table is full.
558 */
559 if (pep->htpr == NULL) {
Joe Perchesede23fa82013-08-26 22:45:23 -0700560 pep->htpr = dma_zalloc_coherent(pep->dev->dev.parent,
561 HASH_ADDR_TABLE_SIZE,
562 &pep->htpr_dma, GFP_KERNEL);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000563 if (pep->htpr == NULL)
564 return -ENOMEM;
Joe Perches1f9061d22013-03-15 07:23:58 +0000565 } else {
566 memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000567 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000568 wrl(pep, HTPR, pep->htpr_dma);
569 return 0;
570}
571
572static void pxa168_eth_set_rx_mode(struct net_device *dev)
573{
574 struct pxa168_eth_private *pep = netdev_priv(dev);
575 struct netdev_hw_addr *ha;
576 u32 val;
577
578 val = rdl(pep, PORT_CONFIG);
579 if (dev->flags & IFF_PROMISC)
580 val |= PCR_PM;
581 else
582 val &= ~PCR_PM;
583 wrl(pep, PORT_CONFIG, val);
584
585 /*
586 * Remove the old list of MAC address and add dev->addr
587 * and multicast address.
588 */
589 memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
590 update_hash_table_mac_address(pep, NULL, dev->dev_addr);
591
592 netdev_for_each_mc_addr(ha, dev)
593 update_hash_table_mac_address(pep, NULL, ha->addr);
594}
595
Antoine Ténart78b9b2c2014-09-30 16:28:12 +0200596static void pxa168_eth_get_mac_address(struct net_device *dev,
597 unsigned char *addr)
598{
599 struct pxa168_eth_private *pep = netdev_priv(dev);
600 unsigned int mac_h = rdl(pep, MAC_ADDR_HIGH);
601 unsigned int mac_l = rdl(pep, MAC_ADDR_LOW);
602
603 addr[0] = (mac_h >> 24) & 0xff;
604 addr[1] = (mac_h >> 16) & 0xff;
605 addr[2] = (mac_h >> 8) & 0xff;
606 addr[3] = mac_h & 0xff;
607 addr[4] = (mac_l >> 8) & 0xff;
608 addr[5] = mac_l & 0xff;
609}
610
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000611static int pxa168_eth_set_mac_address(struct net_device *dev, void *addr)
612{
613 struct sockaddr *sa = addr;
614 struct pxa168_eth_private *pep = netdev_priv(dev);
615 unsigned char oldMac[ETH_ALEN];
Antoine Ténart39830682014-09-30 16:28:11 +0200616 u32 mac_h, mac_l;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000617
618 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +0000619 return -EADDRNOTAVAIL;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000620 memcpy(oldMac, dev->dev_addr, ETH_ALEN);
621 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
Antoine Ténart39830682014-09-30 16:28:11 +0200622
Antoine Ténarte8854392014-10-03 17:08:19 +0200623 mac_h = dev->dev_addr[0] << 24;
624 mac_h |= dev->dev_addr[1] << 16;
625 mac_h |= dev->dev_addr[2] << 8;
626 mac_h |= dev->dev_addr[3];
627 mac_l = dev->dev_addr[4] << 8;
628 mac_l |= dev->dev_addr[5];
Antoine Ténart39830682014-09-30 16:28:11 +0200629 wrl(pep, MAC_ADDR_HIGH, mac_h);
630 wrl(pep, MAC_ADDR_LOW, mac_l);
631
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000632 netif_addr_lock_bh(dev);
633 update_hash_table_mac_address(pep, oldMac, dev->dev_addr);
634 netif_addr_unlock_bh(dev);
635 return 0;
636}
637
638static void eth_port_start(struct net_device *dev)
639{
640 unsigned int val = 0;
641 struct pxa168_eth_private *pep = netdev_priv(dev);
642 int tx_curr_desc, rx_curr_desc;
643
Philippe Reynes7d321842016-07-17 23:30:45 +0200644 phy_start(dev->phydev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000645
646 /* Assignment of Tx CTRP of given queue */
647 tx_curr_desc = pep->tx_curr_desc_q;
648 wrl(pep, ETH_C_TX_DESC_1,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000649 (u32) (pep->tx_desc_dma + tx_curr_desc * sizeof(struct tx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000650
651 /* Assignment of Rx CRDP of given queue */
652 rx_curr_desc = pep->rx_curr_desc_q;
653 wrl(pep, ETH_C_RX_DESC_0,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000654 (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000655
656 wrl(pep, ETH_F_RX_DESC_0,
Dan Carpenterb2bc8562010-08-24 06:55:05 +0000657 (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000658
659 /* Clear all interrupts */
660 wrl(pep, INT_CAUSE, 0);
661
662 /* Enable all interrupts for receive, transmit and error. */
663 wrl(pep, INT_MASK, ALL_INTS);
664
665 val = rdl(pep, PORT_CONFIG);
666 val |= PCR_EN;
667 wrl(pep, PORT_CONFIG, val);
668
669 /* Start RX DMA engine */
670 val = rdl(pep, SDMA_CMD);
671 val |= SDMA_CMD_ERD;
672 wrl(pep, SDMA_CMD, val);
673}
674
675static void eth_port_reset(struct net_device *dev)
676{
677 struct pxa168_eth_private *pep = netdev_priv(dev);
678 unsigned int val = 0;
679
680 /* Stop all interrupts for receive, transmit and error. */
681 wrl(pep, INT_MASK, 0);
682
683 /* Clear all interrupts */
684 wrl(pep, INT_CAUSE, 0);
685
686 /* Stop RX DMA */
687 val = rdl(pep, SDMA_CMD);
688 val &= ~SDMA_CMD_ERD; /* abort dma command */
689
690 /* Abort any transmit and receive operations and put DMA
691 * in idle state.
692 */
693 abort_dma(pep);
694
695 /* Disable port */
696 val = rdl(pep, PORT_CONFIG);
697 val &= ~PCR_EN;
698 wrl(pep, PORT_CONFIG, val);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200699
Philippe Reynes7d321842016-07-17 23:30:45 +0200700 phy_stop(dev->phydev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000701}
702
703/*
704 * txq_reclaim - Free the tx desc data for completed descriptors
705 * If force is non-zero, frees uncompleted descriptors as well
706 */
707static int txq_reclaim(struct net_device *dev, int force)
708{
709 struct pxa168_eth_private *pep = netdev_priv(dev);
710 struct tx_desc *desc;
711 u32 cmd_sts;
712 struct sk_buff *skb;
713 int tx_index;
714 dma_addr_t addr;
715 int count;
716 int released = 0;
717
718 netif_tx_lock(dev);
719
720 pep->work_todo &= ~WORK_TX_DONE;
721 while (pep->tx_desc_count > 0) {
722 tx_index = pep->tx_used_desc_q;
723 desc = &pep->p_tx_desc_area[tx_index];
724 cmd_sts = desc->cmd_sts;
725 if (!force && (cmd_sts & BUF_OWNED_BY_DMA)) {
726 if (released > 0) {
727 goto txq_reclaim_end;
728 } else {
729 released = -1;
730 goto txq_reclaim_end;
731 }
732 }
733 pep->tx_used_desc_q = (tx_index + 1) % pep->tx_ring_size;
734 pep->tx_desc_count--;
735 addr = desc->buf_ptr;
736 count = desc->byte_cnt;
737 skb = pep->tx_skb[tx_index];
738 if (skb)
739 pep->tx_skb[tx_index] = NULL;
740
741 if (cmd_sts & TX_ERROR) {
742 if (net_ratelimit())
Antoine Ténart307f6562014-09-30 16:28:07 +0200743 netdev_err(dev, "Error in TX\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000744 dev->stats.tx_errors++;
745 }
746 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
747 if (skb)
748 dev_kfree_skb_irq(skb);
749 released++;
750 }
751txq_reclaim_end:
752 netif_tx_unlock(dev);
753 return released;
754}
755
756static void pxa168_eth_tx_timeout(struct net_device *dev)
757{
758 struct pxa168_eth_private *pep = netdev_priv(dev);
759
Antoine Ténart307f6562014-09-30 16:28:07 +0200760 netdev_info(dev, "TX timeout desc_count %d\n", pep->tx_desc_count);
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000761
762 schedule_work(&pep->tx_timeout_task);
763}
764
765static void pxa168_eth_tx_timeout_task(struct work_struct *work)
766{
767 struct pxa168_eth_private *pep = container_of(work,
768 struct pxa168_eth_private,
769 tx_timeout_task);
770 struct net_device *dev = pep->dev;
771 pxa168_eth_stop(dev);
772 pxa168_eth_open(dev);
773}
774
775static int rxq_process(struct net_device *dev, int budget)
776{
777 struct pxa168_eth_private *pep = netdev_priv(dev);
778 struct net_device_stats *stats = &dev->stats;
779 unsigned int received_packets = 0;
780 struct sk_buff *skb;
781
782 while (budget-- > 0) {
783 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
784 struct rx_desc *rx_desc;
785 unsigned int cmd_sts;
786
787 /* Do not process Rx ring in case of Rx ring resource error */
788 if (pep->rx_resource_err)
789 break;
790 rx_curr_desc = pep->rx_curr_desc_q;
791 rx_used_desc = pep->rx_used_desc_q;
792 rx_desc = &pep->p_rx_desc_area[rx_curr_desc];
793 cmd_sts = rx_desc->cmd_sts;
Jisheng Zhangb17d1552016-05-13 19:57:30 +0800794 dma_rmb();
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000795 if (cmd_sts & (BUF_OWNED_BY_DMA))
796 break;
797 skb = pep->rx_skb[rx_curr_desc];
798 pep->rx_skb[rx_curr_desc] = NULL;
799
800 rx_next_curr_desc = (rx_curr_desc + 1) % pep->rx_ring_size;
801 pep->rx_curr_desc_q = rx_next_curr_desc;
802
803 /* Rx descriptors exhausted. */
804 /* Set the Rx ring resource error flag */
805 if (rx_next_curr_desc == rx_used_desc)
806 pep->rx_resource_err = 1;
807 pep->rx_desc_count--;
808 dma_unmap_single(NULL, rx_desc->buf_ptr,
809 rx_desc->buf_size,
810 DMA_FROM_DEVICE);
811 received_packets++;
812 /*
813 * Update statistics.
814 * Note byte count includes 4 byte CRC count
815 */
816 stats->rx_packets++;
817 stats->rx_bytes += rx_desc->byte_cnt;
818 /*
819 * In case received a packet without first / last bits on OR
820 * the error summary bit is on, the packets needs to be droped.
821 */
822 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
823 (RX_FIRST_DESC | RX_LAST_DESC))
824 || (cmd_sts & RX_ERROR)) {
825
826 stats->rx_dropped++;
827 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
828 (RX_FIRST_DESC | RX_LAST_DESC)) {
829 if (net_ratelimit())
Antoine Ténart307f6562014-09-30 16:28:07 +0200830 netdev_err(dev,
831 "Rx pkt on multiple desc\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000832 }
833 if (cmd_sts & RX_ERROR)
834 stats->rx_errors++;
835 dev_kfree_skb_irq(skb);
836 } else {
837 /*
838 * The -4 is for the CRC in the trailer of the
839 * received packet
840 */
841 skb_put(skb, rx_desc->byte_cnt - 4);
842 skb->protocol = eth_type_trans(skb, dev);
843 netif_receive_skb(skb);
844 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000845 }
846 /* Fill RX ring with skb's */
847 rxq_refill(dev);
848 return received_packets;
849}
850
851static int pxa168_eth_collect_events(struct pxa168_eth_private *pep,
852 struct net_device *dev)
853{
854 u32 icr;
855 int ret = 0;
856
857 icr = rdl(pep, INT_CAUSE);
858 if (icr == 0)
859 return IRQ_NONE;
860
861 wrl(pep, INT_CAUSE, ~icr);
862 if (icr & (ICR_TXBUF_H | ICR_TXBUF_L)) {
863 pep->work_todo |= WORK_TX_DONE;
864 ret = 1;
865 }
866 if (icr & ICR_RXBUF)
867 ret = 1;
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000868 return ret;
869}
870
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000871static irqreturn_t pxa168_eth_int_handler(int irq, void *dev_id)
872{
873 struct net_device *dev = (struct net_device *)dev_id;
874 struct pxa168_eth_private *pep = netdev_priv(dev);
875
876 if (unlikely(!pxa168_eth_collect_events(pep, dev)))
877 return IRQ_NONE;
878 /* Disable interrupts */
879 wrl(pep, INT_MASK, 0);
880 napi_schedule(&pep->napi);
881 return IRQ_HANDLED;
882}
883
884static void pxa168_eth_recalc_skb_size(struct pxa168_eth_private *pep)
885{
886 int skb_size;
887
888 /*
889 * Reserve 2+14 bytes for an ethernet header (the hardware
890 * automatically prepends 2 bytes of dummy data to each
891 * received packet), 16 bytes for up to four VLAN tags, and
892 * 4 bytes for the trailing FCS -- 36 bytes total.
893 */
894 skb_size = pep->dev->mtu + 36;
895
896 /*
897 * Make sure that the skb size is a multiple of 8 bytes, as
898 * the lower three bits of the receive descriptor's buffer
899 * size field are ignored by the hardware.
900 */
901 pep->skb_size = (skb_size + 7) & ~7;
902
903 /*
904 * If NET_SKB_PAD is smaller than a cache line,
905 * netdev_alloc_skb() will cause skb->data to be misaligned
906 * to a cache line boundary. If this is the case, include
907 * some extra space to allow re-aligning the data area.
908 */
909 pep->skb_size += SKB_DMA_REALIGN;
910
911}
912
913static int set_port_config_ext(struct pxa168_eth_private *pep)
914{
915 int skb_size;
916
917 pxa168_eth_recalc_skb_size(pep);
918 if (pep->skb_size <= 1518)
919 skb_size = PCXR_MFL_1518;
920 else if (pep->skb_size <= 1536)
921 skb_size = PCXR_MFL_1536;
922 else if (pep->skb_size <= 2048)
923 skb_size = PCXR_MFL_2048;
924 else
925 skb_size = PCXR_MFL_64K;
926
927 /* Extended Port Configuration */
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200928 wrl(pep, PORT_CONFIG_EXT,
929 PCXR_AN_SPEED_DIS | /* Disable HW AN */
930 PCXR_AN_DUPLEX_DIS |
931 PCXR_AN_FLOWCTL_DIS |
932 PCXR_2BSM | /* Two byte prefix aligns IP hdr */
Sachin Sanapa49f37e2010-08-13 21:22:49 +0000933 PCXR_DSCP_EN | /* Enable DSCP in IP */
934 skb_size | PCXR_FLP | /* do not force link pass */
935 PCXR_TX_HIGH_PRI); /* Transmit - high priority queue */
936
937 return 0;
938}
939
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200940static void pxa168_eth_adjust_link(struct net_device *dev)
941{
942 struct pxa168_eth_private *pep = netdev_priv(dev);
Philippe Reynes7d321842016-07-17 23:30:45 +0200943 struct phy_device *phy = dev->phydev;
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200944 u32 cfg, cfg_o = rdl(pep, PORT_CONFIG);
945 u32 cfgext, cfgext_o = rdl(pep, PORT_CONFIG_EXT);
946
947 cfg = cfg_o & ~PCR_DUPLEX_FULL;
948 cfgext = cfgext_o & ~(PCXR_SPEED_100 | PCXR_FLOWCTL_DIS | PCXR_RMII_EN);
949
950 if (phy->interface == PHY_INTERFACE_MODE_RMII)
951 cfgext |= PCXR_RMII_EN;
952 if (phy->speed == SPEED_100)
953 cfgext |= PCXR_SPEED_100;
954 if (phy->duplex)
955 cfg |= PCR_DUPLEX_FULL;
956 if (!phy->pause)
957 cfgext |= PCXR_FLOWCTL_DIS;
958
959 /* Bail out if there has nothing changed */
960 if (cfg == cfg_o && cfgext == cfgext_o)
961 return;
962
963 wrl(pep, PORT_CONFIG, cfg);
964 wrl(pep, PORT_CONFIG_EXT, cfgext);
965
966 phy_print_status(phy);
967}
968
969static int pxa168_init_phy(struct net_device *dev)
970{
971 struct pxa168_eth_private *pep = netdev_priv(dev);
Philippe Reynes2186f6e2016-07-17 23:30:46 +0200972 struct ethtool_link_ksettings cmd;
Philippe Reynes7d321842016-07-17 23:30:45 +0200973 struct phy_device *phy = NULL;
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200974 int err;
975
Philippe Reynes7d321842016-07-17 23:30:45 +0200976 if (dev->phydev)
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200977 return 0;
978
Philippe Reynes7d321842016-07-17 23:30:45 +0200979 phy = mdiobus_scan(pep->smi_bus, pep->phy_addr);
980 if (IS_ERR(phy))
981 return PTR_ERR(phy);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200982
Philippe Reynes7d321842016-07-17 23:30:45 +0200983 err = phy_connect_direct(dev, phy, pxa168_eth_adjust_link,
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200984 pep->phy_intf);
985 if (err)
986 return err;
987
Philippe Reynes2186f6e2016-07-17 23:30:46 +0200988 cmd.base.phy_address = pep->phy_addr;
989 cmd.base.speed = pep->phy_speed;
990 cmd.base.duplex = pep->phy_duplex;
991 ethtool_convert_legacy_u32_to_link_mode(cmd.link_modes.advertising,
992 PHY_BASIC_FEATURES);
993 cmd.base.autoneg = AUTONEG_ENABLE;
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200994
Philippe Reynes2186f6e2016-07-17 23:30:46 +0200995 if (cmd.base.speed != 0)
996 cmd.base.autoneg = AUTONEG_DISABLE;
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200997
Philippe Reynes2186f6e2016-07-17 23:30:46 +0200998 return phy_ethtool_set_link_ksettings(dev, &cmd);
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +0200999}
1000
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001001static int pxa168_init_hw(struct pxa168_eth_private *pep)
1002{
1003 int err = 0;
1004
1005 /* Disable interrupts */
1006 wrl(pep, INT_MASK, 0);
1007 wrl(pep, INT_CAUSE, 0);
1008 /* Write to ICR to clear interrupts. */
1009 wrl(pep, INT_W_CLEAR, 0);
1010 /* Abort any transmit and receive operations and put DMA
1011 * in idle state.
1012 */
1013 abort_dma(pep);
1014 /* Initialize address hash table */
1015 err = init_hash_table(pep);
1016 if (err)
1017 return err;
1018 /* SDMA configuration */
1019 wrl(pep, SDMA_CONFIG, SDCR_BSZ8 | /* Burst size = 32 bytes */
1020 SDCR_RIFB | /* Rx interrupt on frame */
1021 SDCR_BLMT | /* Little endian transmit */
1022 SDCR_BLMR | /* Little endian receive */
1023 SDCR_RC_MAX_RETRANS); /* Max retransmit count */
1024 /* Port Configuration */
1025 wrl(pep, PORT_CONFIG, PCR_HS); /* Hash size is 1/2kb */
1026 set_port_config_ext(pep);
1027
1028 return err;
1029}
1030
1031static int rxq_init(struct net_device *dev)
1032{
1033 struct pxa168_eth_private *pep = netdev_priv(dev);
1034 struct rx_desc *p_rx_desc;
1035 int size = 0, i = 0;
1036 int rx_desc_num = pep->rx_ring_size;
1037
1038 /* Allocate RX skb rings */
Lubomir Rintel451bff22013-06-18 19:30:48 +02001039 pep->rx_skb = kzalloc(sizeof(*pep->rx_skb) * pep->rx_ring_size,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001040 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001041 if (!pep->rx_skb)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001042 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001043
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001044 /* Allocate RX ring */
1045 pep->rx_desc_count = 0;
1046 size = pep->rx_ring_size * sizeof(struct rx_desc);
1047 pep->rx_desc_area_size = size;
Joe Perchesede23fa82013-08-26 22:45:23 -07001048 pep->p_rx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1049 &pep->rx_desc_dma,
1050 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00001051 if (!pep->p_rx_desc_area)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001052 goto out;
Joe Perchesd0320f72013-03-14 13:07:21 +00001053
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001054 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Joe Perches64699332012-06-04 12:44:16 +00001055 p_rx_desc = pep->p_rx_desc_area;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001056 for (i = 0; i < rx_desc_num; i++) {
1057 p_rx_desc[i].next_desc_ptr = pep->rx_desc_dma +
1058 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
1059 }
1060 /* Save Rx desc pointer to driver struct. */
1061 pep->rx_curr_desc_q = 0;
1062 pep->rx_used_desc_q = 0;
1063 pep->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
1064 return 0;
1065out:
1066 kfree(pep->rx_skb);
1067 return -ENOMEM;
1068}
1069
1070static void rxq_deinit(struct net_device *dev)
1071{
1072 struct pxa168_eth_private *pep = netdev_priv(dev);
1073 int curr;
1074
1075 /* Free preallocated skb's on RX rings */
1076 for (curr = 0; pep->rx_desc_count && curr < pep->rx_ring_size; curr++) {
1077 if (pep->rx_skb[curr]) {
1078 dev_kfree_skb(pep->rx_skb[curr]);
1079 pep->rx_desc_count--;
1080 }
1081 }
1082 if (pep->rx_desc_count)
Antoine Ténart307f6562014-09-30 16:28:07 +02001083 netdev_err(dev, "Error in freeing Rx Ring. %d skb's still\n",
1084 pep->rx_desc_count);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001085 /* Free RX ring */
1086 if (pep->p_rx_desc_area)
1087 dma_free_coherent(pep->dev->dev.parent, pep->rx_desc_area_size,
1088 pep->p_rx_desc_area, pep->rx_desc_dma);
1089 kfree(pep->rx_skb);
1090}
1091
1092static int txq_init(struct net_device *dev)
1093{
1094 struct pxa168_eth_private *pep = netdev_priv(dev);
1095 struct tx_desc *p_tx_desc;
1096 int size = 0, i = 0;
1097 int tx_desc_num = pep->tx_ring_size;
1098
Lubomir Rintel451bff22013-06-18 19:30:48 +02001099 pep->tx_skb = kzalloc(sizeof(*pep->tx_skb) * pep->tx_ring_size,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001100 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001101 if (!pep->tx_skb)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001102 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001103
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001104 /* Allocate TX ring */
1105 pep->tx_desc_count = 0;
1106 size = pep->tx_ring_size * sizeof(struct tx_desc);
1107 pep->tx_desc_area_size = size;
Joe Perchesede23fa82013-08-26 22:45:23 -07001108 pep->p_tx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1109 &pep->tx_desc_dma,
1110 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00001111 if (!pep->p_tx_desc_area)
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001112 goto out;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001113 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Joe Perches64699332012-06-04 12:44:16 +00001114 p_tx_desc = pep->p_tx_desc_area;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001115 for (i = 0; i < tx_desc_num; i++) {
1116 p_tx_desc[i].next_desc_ptr = pep->tx_desc_dma +
1117 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
1118 }
1119 pep->tx_curr_desc_q = 0;
1120 pep->tx_used_desc_q = 0;
1121 pep->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
1122 return 0;
1123out:
1124 kfree(pep->tx_skb);
1125 return -ENOMEM;
1126}
1127
1128static void txq_deinit(struct net_device *dev)
1129{
1130 struct pxa168_eth_private *pep = netdev_priv(dev);
1131
1132 /* Free outstanding skb's on TX ring */
1133 txq_reclaim(dev, 1);
1134 BUG_ON(pep->tx_used_desc_q != pep->tx_curr_desc_q);
1135 /* Free TX ring */
1136 if (pep->p_tx_desc_area)
1137 dma_free_coherent(pep->dev->dev.parent, pep->tx_desc_area_size,
1138 pep->p_tx_desc_area, pep->tx_desc_dma);
1139 kfree(pep->tx_skb);
1140}
1141
1142static int pxa168_eth_open(struct net_device *dev)
1143{
1144 struct pxa168_eth_private *pep = netdev_priv(dev);
1145 int err;
1146
Sebastian Hesselbarth1a149132014-10-22 20:26:47 +02001147 err = pxa168_init_phy(dev);
1148 if (err)
1149 return err;
1150
Michael Opdenacker599c2e12013-09-13 06:04:23 +02001151 err = request_irq(dev->irq, pxa168_eth_int_handler, 0, dev->name, dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001152 if (err) {
Joe Perchesf7b4fb22012-10-27 22:05:48 +00001153 dev_err(&dev->dev, "can't assign irq\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001154 return -EAGAIN;
1155 }
1156 pep->rx_resource_err = 0;
1157 err = rxq_init(dev);
1158 if (err != 0)
1159 goto out_free_irq;
1160 err = txq_init(dev);
1161 if (err != 0)
1162 goto out_free_rx_skb;
1163 pep->rx_used_desc_q = 0;
1164 pep->rx_curr_desc_q = 0;
1165
1166 /* Fill RX ring with skb's */
1167 rxq_refill(dev);
1168 pep->rx_used_desc_q = 0;
1169 pep->rx_curr_desc_q = 0;
1170 netif_carrier_off(dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001171 napi_enable(&pep->napi);
Lino Sanfilippo8961b1942014-11-30 11:49:36 +01001172 eth_port_start(dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001173 return 0;
1174out_free_rx_skb:
1175 rxq_deinit(dev);
1176out_free_irq:
1177 free_irq(dev->irq, dev);
1178 return err;
1179}
1180
1181static int pxa168_eth_stop(struct net_device *dev)
1182{
1183 struct pxa168_eth_private *pep = netdev_priv(dev);
1184 eth_port_reset(dev);
1185
1186 /* Disable interrupts */
1187 wrl(pep, INT_MASK, 0);
1188 wrl(pep, INT_CAUSE, 0);
1189 /* Write to ICR to clear interrupts. */
1190 wrl(pep, INT_W_CLEAR, 0);
1191 napi_disable(&pep->napi);
1192 del_timer_sync(&pep->timeout);
1193 netif_carrier_off(dev);
1194 free_irq(dev->irq, dev);
1195 rxq_deinit(dev);
1196 txq_deinit(dev);
1197
1198 return 0;
1199}
1200
1201static int pxa168_eth_change_mtu(struct net_device *dev, int mtu)
1202{
1203 int retval;
1204 struct pxa168_eth_private *pep = netdev_priv(dev);
1205
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001206 dev->mtu = mtu;
1207 retval = set_port_config_ext(pep);
1208
1209 if (!netif_running(dev))
1210 return 0;
1211
1212 /*
1213 * Stop and then re-open the interface. This will allocate RX
1214 * skbs of the new MTU.
1215 * There is a possible danger that the open will not succeed,
1216 * due to memory being full.
1217 */
1218 pxa168_eth_stop(dev);
1219 if (pxa168_eth_open(dev)) {
Joe Perchesf7b4fb22012-10-27 22:05:48 +00001220 dev_err(&dev->dev,
1221 "fatal error on re-opening device after MTU change\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001222 }
1223
1224 return 0;
1225}
1226
1227static int eth_alloc_tx_desc_index(struct pxa168_eth_private *pep)
1228{
1229 int tx_desc_curr;
1230
1231 tx_desc_curr = pep->tx_curr_desc_q;
1232 pep->tx_curr_desc_q = (tx_desc_curr + 1) % pep->tx_ring_size;
1233 BUG_ON(pep->tx_curr_desc_q == pep->tx_used_desc_q);
1234 pep->tx_desc_count++;
1235
1236 return tx_desc_curr;
1237}
1238
1239static int pxa168_rx_poll(struct napi_struct *napi, int budget)
1240{
1241 struct pxa168_eth_private *pep =
1242 container_of(napi, struct pxa168_eth_private, napi);
1243 struct net_device *dev = pep->dev;
1244 int work_done = 0;
1245
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001246 /*
1247 * We call txq_reclaim every time since in NAPI interupts are disabled
1248 * and due to this we miss the TX_DONE interrupt,which is not updated in
1249 * interrupt status register.
1250 */
1251 txq_reclaim(dev, 0);
1252 if (netif_queue_stopped(dev)
1253 && pep->tx_ring_size - pep->tx_desc_count > 1) {
1254 netif_wake_queue(dev);
1255 }
1256 work_done = rxq_process(dev, budget);
1257 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001258 napi_complete_done(napi, work_done);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001259 wrl(pep, INT_MASK, ALL_INTS);
1260 }
1261
1262 return work_done;
1263}
1264
1265static int pxa168_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1266{
1267 struct pxa168_eth_private *pep = netdev_priv(dev);
1268 struct net_device_stats *stats = &dev->stats;
1269 struct tx_desc *desc;
1270 int tx_index;
1271 int length;
1272
1273 tx_index = eth_alloc_tx_desc_index(pep);
1274 desc = &pep->p_tx_desc_area[tx_index];
1275 length = skb->len;
1276 pep->tx_skb[tx_index] = skb;
1277 desc->byte_cnt = length;
1278 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Richard Cochran1f6e44a2011-06-19 21:51:31 +00001279
1280 skb_tx_timestamp(skb);
1281
Jisheng Zhangb17d1552016-05-13 19:57:30 +08001282 dma_wmb();
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001283 desc->cmd_sts = BUF_OWNED_BY_DMA | TX_GEN_CRC | TX_FIRST_DESC |
1284 TX_ZERO_PADDING | TX_LAST_DESC | TX_EN_INT;
1285 wmb();
1286 wrl(pep, SDMA_CMD, SDMA_CMD_TXDH | SDMA_CMD_ERD);
1287
Richard Cochran38442042011-06-19 21:48:06 +00001288 stats->tx_bytes += length;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001289 stats->tx_packets++;
Florian Westphal860e9532016-05-03 16:33:13 +02001290 netif_trans_update(dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001291 if (pep->tx_ring_size - pep->tx_desc_count <= 1) {
1292 /* We handled the current skb, but now we are out of space.*/
1293 netif_stop_queue(dev);
1294 }
1295
1296 return NETDEV_TX_OK;
1297}
1298
1299static int smi_wait_ready(struct pxa168_eth_private *pep)
1300{
1301 int i = 0;
1302
1303 /* wait for the SMI register to become available */
1304 for (i = 0; rdl(pep, SMI) & SMI_BUSY; i++) {
1305 if (i == PHY_WAIT_ITERATIONS)
1306 return -ETIMEDOUT;
1307 msleep(10);
1308 }
1309
1310 return 0;
1311}
1312
1313static int pxa168_smi_read(struct mii_bus *bus, int phy_addr, int regnum)
1314{
1315 struct pxa168_eth_private *pep = bus->priv;
1316 int i = 0;
1317 int val;
1318
1319 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001320 netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001321 return -ETIMEDOUT;
1322 }
1323 wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) | SMI_OP_R);
1324 /* now wait for the data to be valid */
1325 for (i = 0; !((val = rdl(pep, SMI)) & SMI_R_VALID); i++) {
1326 if (i == PHY_WAIT_ITERATIONS) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001327 netdev_warn(pep->dev,
1328 "pxa168_eth: SMI bus read not valid\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001329 return -ENODEV;
1330 }
1331 msleep(10);
1332 }
1333
1334 return val & 0xffff;
1335}
1336
1337static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
1338 u16 value)
1339{
1340 struct pxa168_eth_private *pep = bus->priv;
1341
1342 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001343 netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001344 return -ETIMEDOUT;
1345 }
1346
1347 wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) |
1348 SMI_OP_W | (value & 0xffff));
1349
1350 if (smi_wait_ready(pep)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001351 netdev_err(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001352 return -ETIMEDOUT;
1353 }
1354
1355 return 0;
1356}
1357
1358static int pxa168_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1359 int cmd)
1360{
Philippe Reynes7d321842016-07-17 23:30:45 +02001361 if (dev->phydev != NULL)
1362 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001363
1364 return -EOPNOTSUPP;
1365}
1366
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001367static void pxa168_get_drvinfo(struct net_device *dev,
1368 struct ethtool_drvinfo *info)
1369{
Jiri Pirko7826d432013-01-06 00:44:26 +00001370 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1371 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
1372 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1373 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001374}
1375
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001376static const struct ethtool_ops pxa168_ethtool_ops = {
Antoine Ténart307f6562014-09-30 16:28:07 +02001377 .get_drvinfo = pxa168_get_drvinfo,
Florian Fainelli13f0ac42016-11-15 11:19:49 -08001378 .nway_reset = phy_ethtool_nway_reset,
Antoine Ténart307f6562014-09-30 16:28:07 +02001379 .get_link = ethtool_op_get_link,
1380 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainelli482ff9f2017-02-06 15:55:21 -08001381 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Philippe Reynes2186f6e2016-07-17 23:30:46 +02001382 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001383};
1384
1385static const struct net_device_ops pxa168_eth_netdev_ops = {
Antoine Ténart307f6562014-09-30 16:28:07 +02001386 .ndo_open = pxa168_eth_open,
1387 .ndo_stop = pxa168_eth_stop,
1388 .ndo_start_xmit = pxa168_eth_start_xmit,
1389 .ndo_set_rx_mode = pxa168_eth_set_rx_mode,
1390 .ndo_set_mac_address = pxa168_eth_set_mac_address,
1391 .ndo_validate_addr = eth_validate_addr,
1392 .ndo_do_ioctl = pxa168_eth_do_ioctl,
1393 .ndo_change_mtu = pxa168_eth_change_mtu,
1394 .ndo_tx_timeout = pxa168_eth_tx_timeout,
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001395};
1396
1397static int pxa168_eth_probe(struct platform_device *pdev)
1398{
1399 struct pxa168_eth_private *pep = NULL;
1400 struct net_device *dev = NULL;
1401 struct resource *res;
1402 struct clk *clk;
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001403 struct device_node *np;
Antoine Ténart78b9b2c2014-09-30 16:28:12 +02001404 const unsigned char *mac_addr = NULL;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001405 int err;
1406
1407 printk(KERN_NOTICE "PXA168 10/100 Ethernet Driver\n");
1408
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001409 clk = devm_clk_get(&pdev->dev, NULL);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001410 if (IS_ERR(clk)) {
Antoine Ténart307f6562014-09-30 16:28:07 +02001411 dev_err(&pdev->dev, "Fast Ethernet failed to get clock\n");
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001412 return -ENODEV;
1413 }
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001414 clk_prepare_enable(clk);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001415
1416 dev = alloc_etherdev(sizeof(struct pxa168_eth_private));
1417 if (!dev) {
1418 err = -ENOMEM;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001419 goto err_clk;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001420 }
1421
1422 platform_set_drvdata(pdev, dev);
1423 pep = netdev_priv(dev);
1424 pep->dev = dev;
1425 pep->clk = clk;
Varka Bhadram7e5ae242014-10-24 07:42:08 +05301426
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001427 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001428 pep->base = devm_ioremap_resource(&pdev->dev, res);
1429 if (IS_ERR(pep->base)) {
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001430 err = -ENOMEM;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001431 goto err_netdev;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001432 }
Varka Bhadram7e5ae242014-10-24 07:42:08 +05301433
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001434 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1435 BUG_ON(!res);
1436 dev->irq = res->start;
1437 dev->netdev_ops = &pxa168_eth_netdev_ops;
1438 dev->watchdog_timeo = 2 * HZ;
1439 dev->base_addr = 0;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001440 dev->ethtool_ops = &pxa168_ethtool_ops;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001441
Jarod Wilson57779872016-10-17 15:54:06 -04001442 /* MTU range: 68 - 9500 */
1443 dev->min_mtu = ETH_MIN_MTU;
1444 dev->max_mtu = 9500;
1445
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001446 INIT_WORK(&pep->tx_timeout_task, pxa168_eth_tx_timeout_task);
1447
Antoine Ténart78b9b2c2014-09-30 16:28:12 +02001448 if (pdev->dev.of_node)
1449 mac_addr = of_get_mac_address(pdev->dev.of_node);
1450
1451 if (mac_addr && is_valid_ether_addr(mac_addr)) {
1452 ether_addr_copy(dev->dev_addr, mac_addr);
1453 } else {
1454 /* try reading the mac address, if set by the bootloader */
1455 pxa168_eth_get_mac_address(dev, dev->dev_addr);
1456 if (!is_valid_ether_addr(dev->dev_addr)) {
1457 dev_info(&pdev->dev, "Using random mac address\n");
1458 eth_hw_addr_random(dev);
1459 }
1460 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001461
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001462 pep->rx_ring_size = NUM_RX_DESCS;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001463 pep->tx_ring_size = NUM_TX_DESCS;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001464
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001465 pep->pd = dev_get_platdata(&pdev->dev);
1466 if (pep->pd) {
1467 if (pep->pd->rx_queue_size)
1468 pep->rx_ring_size = pep->pd->rx_queue_size;
1469
1470 if (pep->pd->tx_queue_size)
1471 pep->tx_ring_size = pep->pd->tx_queue_size;
1472
1473 pep->port_num = pep->pd->port_number;
1474 pep->phy_addr = pep->pd->phy_addr;
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001475 pep->phy_speed = pep->pd->speed;
1476 pep->phy_duplex = pep->pd->duplex;
1477 pep->phy_intf = pep->pd->intf;
1478
1479 if (pep->pd->init)
1480 pep->pd->init();
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001481 } else if (pdev->dev.of_node) {
1482 of_property_read_u32(pdev->dev.of_node, "port-id",
1483 &pep->port_num);
1484
1485 np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001486 if (!np) {
1487 dev_err(&pdev->dev, "missing phy-handle\n");
Alexey Khoroshilov0e03fd32015-04-25 04:07:03 +03001488 err = -EINVAL;
1489 goto err_netdev;
Sebastian Hesselbarth9d8ea732014-10-22 20:26:46 +02001490 }
1491 of_property_read_u32(np, "reg", &pep->phy_addr);
1492 pep->phy_intf = of_get_phy_mode(pdev->dev.of_node);
Peter Chenbd1026c2016-08-01 15:02:38 +08001493 of_node_put(np);
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001494 }
1495
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001496 /* Hardware supports only 3 ports */
1497 BUG_ON(pep->port_num > 2);
1498 netif_napi_add(dev, &pep->napi, pxa168_rx_poll, pep->rx_ring_size);
1499
1500 memset(&pep->timeout, 0, sizeof(struct timer_list));
1501 init_timer(&pep->timeout);
1502 pep->timeout.function = rxq_refill_timer_wrapper;
1503 pep->timeout.data = (unsigned long)pep;
1504
1505 pep->smi_bus = mdiobus_alloc();
1506 if (pep->smi_bus == NULL) {
1507 err = -ENOMEM;
Alexey Khoroshilov0e03fd32015-04-25 04:07:03 +03001508 goto err_netdev;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001509 }
1510 pep->smi_bus->priv = pep;
1511 pep->smi_bus->name = "pxa168_eth smi";
1512 pep->smi_bus->read = pxa168_smi_read;
1513 pep->smi_bus->write = pxa168_smi_write;
Florian Fainellid073a102012-01-09 23:59:16 +00001514 snprintf(pep->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1515 pdev->name, pdev->id);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001516 pep->smi_bus->parent = &pdev->dev;
1517 pep->smi_bus->phy_mask = 0xffffffff;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001518 err = mdiobus_register(pep->smi_bus);
1519 if (err)
1520 goto err_free_mdio;
1521
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001522 SET_NETDEV_DEV(dev, &pdev->dev);
Jisheng Zhang824ab782014-11-12 19:08:47 +08001523 pxa168_init_hw(pep);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001524 err = register_netdev(dev);
1525 if (err)
Dan Carpenter945c7c72010-08-24 06:53:33 +00001526 goto err_mdiobus;
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001527 return 0;
Dan Carpenter945c7c72010-08-24 06:53:33 +00001528
1529err_mdiobus:
1530 mdiobus_unregister(pep->smi_bus);
1531err_free_mdio:
1532 mdiobus_free(pep->smi_bus);
Dan Carpenter945c7c72010-08-24 06:53:33 +00001533err_netdev:
1534 free_netdev(dev);
1535err_clk:
Alexey Khoroshilov0e03fd32015-04-25 04:07:03 +03001536 clk_disable_unprepare(clk);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001537 return err;
1538}
1539
1540static int pxa168_eth_remove(struct platform_device *pdev)
1541{
1542 struct net_device *dev = platform_get_drvdata(pdev);
1543 struct pxa168_eth_private *pep = netdev_priv(dev);
1544
1545 if (pep->htpr) {
1546 dma_free_coherent(pep->dev->dev.parent, HASH_ADDR_TABLE_SIZE,
1547 pep->htpr, pep->htpr_dma);
1548 pep->htpr = NULL;
1549 }
Philippe Reynes7d321842016-07-17 23:30:45 +02001550 if (dev->phydev)
1551 phy_disconnect(dev->phydev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001552 if (pep->clk) {
Alexey Khoroshilov0e03fd32015-04-25 04:07:03 +03001553 clk_disable_unprepare(pep->clk);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001554 }
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001555
Denis Kirjanov9c01ae52010-08-29 21:21:38 +00001556 mdiobus_unregister(pep->smi_bus);
1557 mdiobus_free(pep->smi_bus);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001558 unregister_netdev(dev);
Tejun Heo23f333a2010-12-12 16:45:14 +01001559 cancel_work_sync(&pep->tx_timeout_task);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001560 free_netdev(dev);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001561 return 0;
1562}
1563
1564static void pxa168_eth_shutdown(struct platform_device *pdev)
1565{
1566 struct net_device *dev = platform_get_drvdata(pdev);
1567 eth_port_reset(dev);
1568}
1569
1570#ifdef CONFIG_PM
1571static int pxa168_eth_resume(struct platform_device *pdev)
1572{
1573 return -ENOSYS;
1574}
1575
1576static int pxa168_eth_suspend(struct platform_device *pdev, pm_message_t state)
1577{
1578 return -ENOSYS;
1579}
1580
1581#else
1582#define pxa168_eth_resume NULL
1583#define pxa168_eth_suspend NULL
1584#endif
1585
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001586static const struct of_device_id pxa168_eth_of_match[] = {
1587 { .compatible = "marvell,pxa168-eth" },
1588 { },
1589};
1590MODULE_DEVICE_TABLE(of, pxa168_eth_of_match);
1591
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001592static struct platform_driver pxa168_eth_driver = {
1593 .probe = pxa168_eth_probe,
1594 .remove = pxa168_eth_remove,
1595 .shutdown = pxa168_eth_shutdown,
1596 .resume = pxa168_eth_resume,
1597 .suspend = pxa168_eth_suspend,
1598 .driver = {
Antoine Ténart43d3ddf2014-09-30 16:28:08 +02001599 .name = DRIVER_NAME,
1600 .of_match_table = of_match_ptr(pxa168_eth_of_match),
1601 },
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001602};
1603
Axel Lindb62f682011-11-27 16:44:17 +00001604module_platform_driver(pxa168_eth_driver);
Sachin Sanapa49f37e2010-08-13 21:22:49 +00001605
1606MODULE_LICENSE("GPL");
1607MODULE_DESCRIPTION("Ethernet driver for Marvell PXA168");
1608MODULE_ALIAS("platform:pxa168_eth");