Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / marvell / pxa168_eth.c
1 /*
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>
7  *              Zhangfei Gao <zgao6@marvell.com>
8  *              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
22  * along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include <linux/init.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/in.h>
28 #include <linux/ip.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/etherdevice.h>
32 #include <linux/bitops.h>
33 #include <linux/delay.h>
34 #include <linux/ethtool.h>
35 #include <linux/platform_device.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/workqueue.h>
39 #include <linux/clk.h>
40 #include <linux/phy.h>
41 #include <linux/io.h>
42 #include <linux/interrupt.h>
43 #include <linux/types.h>
44 #include <asm/pgtable.h>
45 #include <asm/cacheflush.h>
46 #include <linux/pxa168_eth.h>
47
48 #define DRIVER_NAME     "pxa168-eth"
49 #define DRIVER_VERSION  "0.3"
50
51 /*
52  * Registers
53  */
54
55 #define PHY_ADDRESS             0x0000
56 #define SMI                     0x0010
57 #define PORT_CONFIG             0x0400
58 #define PORT_CONFIG_EXT         0x0408
59 #define PORT_COMMAND            0x0410
60 #define PORT_STATUS             0x0418
61 #define HTPR                    0x0428
62 #define SDMA_CONFIG             0x0440
63 #define SDMA_CMD                0x0448
64 #define INT_CAUSE               0x0450
65 #define INT_W_CLEAR             0x0454
66 #define INT_MASK                0x0458
67 #define ETH_F_RX_DESC_0         0x0480
68 #define ETH_C_RX_DESC_0         0x04A0
69 #define ETH_C_TX_DESC_1         0x04E4
70
71 /* smi register */
72 #define SMI_BUSY                (1 << 28)       /* 0 - Write, 1 - Read  */
73 #define SMI_R_VALID             (1 << 27)       /* 0 - Write, 1 - Read  */
74 #define SMI_OP_W                (0 << 26)       /* Write operation      */
75 #define SMI_OP_R                (1 << 26)       /* Read operation */
76
77 #define PHY_WAIT_ITERATIONS     10
78
79 #define PXA168_ETH_PHY_ADDR_DEFAULT     0
80 /* RX & TX descriptor command */
81 #define BUF_OWNED_BY_DMA        (1 << 31)
82
83 /* RX descriptor status */
84 #define RX_EN_INT               (1 << 23)
85 #define RX_FIRST_DESC           (1 << 17)
86 #define RX_LAST_DESC            (1 << 16)
87 #define RX_ERROR                (1 << 15)
88
89 /* TX descriptor command */
90 #define TX_EN_INT               (1 << 23)
91 #define TX_GEN_CRC              (1 << 22)
92 #define TX_ZERO_PADDING         (1 << 18)
93 #define TX_FIRST_DESC           (1 << 17)
94 #define TX_LAST_DESC            (1 << 16)
95 #define TX_ERROR                (1 << 15)
96
97 /* SDMA_CMD */
98 #define SDMA_CMD_AT             (1 << 31)
99 #define SDMA_CMD_TXDL           (1 << 24)
100 #define SDMA_CMD_TXDH           (1 << 23)
101 #define SDMA_CMD_AR             (1 << 15)
102 #define SDMA_CMD_ERD            (1 << 7)
103
104 /* Bit definitions of the Port Config Reg */
105 #define PCR_HS                  (1 << 12)
106 #define PCR_EN                  (1 << 7)
107 #define PCR_PM                  (1 << 0)
108
109 /* Bit definitions of the Port Config Extend Reg */
110 #define PCXR_2BSM               (1 << 28)
111 #define PCXR_DSCP_EN            (1 << 21)
112 #define PCXR_MFL_1518           (0 << 14)
113 #define PCXR_MFL_1536           (1 << 14)
114 #define PCXR_MFL_2048           (2 << 14)
115 #define PCXR_MFL_64K            (3 << 14)
116 #define PCXR_FLP                (1 << 11)
117 #define PCXR_PRIO_TX_OFF        3
118 #define PCXR_TX_HIGH_PRI        (7 << PCXR_PRIO_TX_OFF)
119
120 /* Bit definitions of the SDMA Config Reg */
121 #define SDCR_BSZ_OFF            12
122 #define SDCR_BSZ8               (3 << SDCR_BSZ_OFF)
123 #define SDCR_BSZ4               (2 << SDCR_BSZ_OFF)
124 #define SDCR_BSZ2               (1 << SDCR_BSZ_OFF)
125 #define SDCR_BSZ1               (0 << SDCR_BSZ_OFF)
126 #define SDCR_BLMR               (1 << 6)
127 #define SDCR_BLMT               (1 << 7)
128 #define SDCR_RIFB               (1 << 9)
129 #define SDCR_RC_OFF             2
130 #define SDCR_RC_MAX_RETRANS     (0xf << SDCR_RC_OFF)
131
132 /*
133  * Bit definitions of the Interrupt Cause Reg
134  * and Interrupt MASK Reg is the same
135  */
136 #define ICR_RXBUF               (1 << 0)
137 #define ICR_TXBUF_H             (1 << 2)
138 #define ICR_TXBUF_L             (1 << 3)
139 #define ICR_TXEND_H             (1 << 6)
140 #define ICR_TXEND_L             (1 << 7)
141 #define ICR_RXERR               (1 << 8)
142 #define ICR_TXERR_H             (1 << 10)
143 #define ICR_TXERR_L             (1 << 11)
144 #define ICR_TX_UDR              (1 << 13)
145 #define ICR_MII_CH              (1 << 28)
146
147 #define ALL_INTS (ICR_TXBUF_H  | ICR_TXBUF_L  | ICR_TX_UDR |\
148                                 ICR_TXERR_H  | ICR_TXERR_L |\
149                                 ICR_TXEND_H  | ICR_TXEND_L |\
150                                 ICR_RXBUF | ICR_RXERR  | ICR_MII_CH)
151
152 #define ETH_HW_IP_ALIGN         2       /* hw aligns IP header */
153
154 #define NUM_RX_DESCS            64
155 #define NUM_TX_DESCS            64
156
157 #define HASH_ADD                0
158 #define HASH_DELETE             1
159 #define HASH_ADDR_TABLE_SIZE    0x4000  /* 16K (1/2K address - PCR_HS == 1) */
160 #define HOP_NUMBER              12
161
162 /* Bit definitions for Port status */
163 #define PORT_SPEED_100          (1 << 0)
164 #define FULL_DUPLEX             (1 << 1)
165 #define FLOW_CONTROL_ENABLED    (1 << 2)
166 #define LINK_UP                 (1 << 3)
167
168 /* Bit definitions for work to be done */
169 #define WORK_LINK               (1 << 0)
170 #define WORK_TX_DONE            (1 << 1)
171
172 /*
173  * Misc definitions.
174  */
175 #define SKB_DMA_REALIGN         ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
176
177 struct rx_desc {
178         u32 cmd_sts;            /* Descriptor command status            */
179         u16 byte_cnt;           /* Descriptor buffer byte count         */
180         u16 buf_size;           /* Buffer size                          */
181         u32 buf_ptr;            /* Descriptor buffer pointer            */
182         u32 next_desc_ptr;      /* Next descriptor pointer              */
183 };
184
185 struct tx_desc {
186         u32 cmd_sts;            /* Command/status field                 */
187         u16 reserved;
188         u16 byte_cnt;           /* buffer byte count                    */
189         u32 buf_ptr;            /* pointer to buffer for this descriptor */
190         u32 next_desc_ptr;      /* Pointer to next descriptor           */
191 };
192
193 struct pxa168_eth_private {
194         int port_num;           /* User Ethernet port number    */
195
196         int rx_resource_err;    /* Rx ring resource error flag */
197
198         /* Next available and first returning Rx resource */
199         int rx_curr_desc_q, rx_used_desc_q;
200
201         /* Next available and first returning Tx resource */
202         int tx_curr_desc_q, tx_used_desc_q;
203
204         struct rx_desc *p_rx_desc_area;
205         dma_addr_t rx_desc_dma;
206         int rx_desc_area_size;
207         struct sk_buff **rx_skb;
208
209         struct tx_desc *p_tx_desc_area;
210         dma_addr_t tx_desc_dma;
211         int tx_desc_area_size;
212         struct sk_buff **tx_skb;
213
214         struct work_struct tx_timeout_task;
215
216         struct net_device *dev;
217         struct napi_struct napi;
218         u8 work_todo;
219         int skb_size;
220
221         /* Size of Tx Ring per queue */
222         int tx_ring_size;
223         /* Number of tx descriptors in use */
224         int tx_desc_count;
225         /* Size of Rx Ring per queue */
226         int rx_ring_size;
227         /* Number of rx descriptors in use */
228         int rx_desc_count;
229
230         /*
231          * Used in case RX Ring is empty, which can occur when
232          * system does not have resources (skb's)
233          */
234         struct timer_list timeout;
235         struct mii_bus *smi_bus;
236         struct phy_device *phy;
237
238         /* clock */
239         struct clk *clk;
240         struct pxa168_eth_platform_data *pd;
241         /*
242          * Ethernet controller base address.
243          */
244         void __iomem *base;
245
246         /* Pointer to the hardware address filter table */
247         void *htpr;
248         dma_addr_t htpr_dma;
249 };
250
251 struct addr_table_entry {
252         __le32 lo;
253         __le32 hi;
254 };
255
256 /* Bit fields of a Hash Table Entry */
257 enum hash_table_entry {
258         HASH_ENTRY_VALID = 1,
259         SKIP = 2,
260         HASH_ENTRY_RECEIVE_DISCARD = 4,
261         HASH_ENTRY_RECEIVE_DISCARD_BIT = 2
262 };
263
264 static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
265 static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd);
266 static int pxa168_init_hw(struct pxa168_eth_private *pep);
267 static void eth_port_reset(struct net_device *dev);
268 static void eth_port_start(struct net_device *dev);
269 static int pxa168_eth_open(struct net_device *dev);
270 static int pxa168_eth_stop(struct net_device *dev);
271 static int ethernet_phy_setup(struct net_device *dev);
272
273 static inline u32 rdl(struct pxa168_eth_private *pep, int offset)
274 {
275         return readl(pep->base + offset);
276 }
277
278 static inline void wrl(struct pxa168_eth_private *pep, int offset, u32 data)
279 {
280         writel(data, pep->base + offset);
281 }
282
283 static void abort_dma(struct pxa168_eth_private *pep)
284 {
285         int delay;
286         int max_retries = 40;
287
288         do {
289                 wrl(pep, SDMA_CMD, SDMA_CMD_AR | SDMA_CMD_AT);
290                 udelay(100);
291
292                 delay = 10;
293                 while ((rdl(pep, SDMA_CMD) & (SDMA_CMD_AR | SDMA_CMD_AT))
294                        && delay-- > 0) {
295                         udelay(10);
296                 }
297         } while (max_retries-- > 0 && delay <= 0);
298
299         if (max_retries <= 0)
300                 printk(KERN_ERR "%s : DMA Stuck\n", __func__);
301 }
302
303 static int ethernet_phy_get(struct pxa168_eth_private *pep)
304 {
305         unsigned int reg_data;
306
307         reg_data = rdl(pep, PHY_ADDRESS);
308
309         return (reg_data >> (5 * pep->port_num)) & 0x1f;
310 }
311
312 static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr)
313 {
314         u32 reg_data;
315         int addr_shift = 5 * pep->port_num;
316
317         reg_data = rdl(pep, PHY_ADDRESS);
318         reg_data &= ~(0x1f << addr_shift);
319         reg_data |= (phy_addr & 0x1f) << addr_shift;
320         wrl(pep, PHY_ADDRESS, reg_data);
321 }
322
323 static void rxq_refill(struct net_device *dev)
324 {
325         struct pxa168_eth_private *pep = netdev_priv(dev);
326         struct sk_buff *skb;
327         struct rx_desc *p_used_rx_desc;
328         int used_rx_desc;
329
330         while (pep->rx_desc_count < pep->rx_ring_size) {
331                 int size;
332
333                 skb = netdev_alloc_skb(dev, pep->skb_size);
334                 if (!skb)
335                         break;
336                 if (SKB_DMA_REALIGN)
337                         skb_reserve(skb, SKB_DMA_REALIGN);
338                 pep->rx_desc_count++;
339                 /* Get 'used' Rx descriptor */
340                 used_rx_desc = pep->rx_used_desc_q;
341                 p_used_rx_desc = &pep->p_rx_desc_area[used_rx_desc];
342                 size = skb_end_pointer(skb) - skb->data;
343                 p_used_rx_desc->buf_ptr = dma_map_single(NULL,
344                                                          skb->data,
345                                                          size,
346                                                          DMA_FROM_DEVICE);
347                 p_used_rx_desc->buf_size = size;
348                 pep->rx_skb[used_rx_desc] = skb;
349
350                 /* Return the descriptor to DMA ownership */
351                 wmb();
352                 p_used_rx_desc->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
353                 wmb();
354
355                 /* Move the used descriptor pointer to the next descriptor */
356                 pep->rx_used_desc_q = (used_rx_desc + 1) % pep->rx_ring_size;
357
358                 /* Any Rx return cancels the Rx resource error status */
359                 pep->rx_resource_err = 0;
360
361                 skb_reserve(skb, ETH_HW_IP_ALIGN);
362         }
363
364         /*
365          * If RX ring is empty of SKB, set a timer to try allocating
366          * again at a later time.
367          */
368         if (pep->rx_desc_count == 0) {
369                 pep->timeout.expires = jiffies + (HZ / 10);
370                 add_timer(&pep->timeout);
371         }
372 }
373
374 static inline void rxq_refill_timer_wrapper(unsigned long data)
375 {
376         struct pxa168_eth_private *pep = (void *)data;
377         napi_schedule(&pep->napi);
378 }
379
380 static inline u8 flip_8_bits(u8 x)
381 {
382         return (((x) & 0x01) << 3) | (((x) & 0x02) << 1)
383             | (((x) & 0x04) >> 1) | (((x) & 0x08) >> 3)
384             | (((x) & 0x10) << 3) | (((x) & 0x20) << 1)
385             | (((x) & 0x40) >> 1) | (((x) & 0x80) >> 3);
386 }
387
388 static void nibble_swap_every_byte(unsigned char *mac_addr)
389 {
390         int i;
391         for (i = 0; i < ETH_ALEN; i++) {
392                 mac_addr[i] = ((mac_addr[i] & 0x0f) << 4) |
393                                 ((mac_addr[i] & 0xf0) >> 4);
394         }
395 }
396
397 static void inverse_every_nibble(unsigned char *mac_addr)
398 {
399         int i;
400         for (i = 0; i < ETH_ALEN; i++)
401                 mac_addr[i] = flip_8_bits(mac_addr[i]);
402 }
403
404 /*
405  * ----------------------------------------------------------------------------
406  * This function will calculate the hash function of the address.
407  * Inputs
408  * mac_addr_orig    - MAC address.
409  * Outputs
410  * return the calculated entry.
411  */
412 static u32 hash_function(unsigned char *mac_addr_orig)
413 {
414         u32 hash_result;
415         u32 addr0;
416         u32 addr1;
417         u32 addr2;
418         u32 addr3;
419         unsigned char mac_addr[ETH_ALEN];
420
421         /* Make a copy of MAC address since we are going to performe bit
422          * operations on it
423          */
424         memcpy(mac_addr, mac_addr_orig, ETH_ALEN);
425
426         nibble_swap_every_byte(mac_addr);
427         inverse_every_nibble(mac_addr);
428
429         addr0 = (mac_addr[5] >> 2) & 0x3f;
430         addr1 = (mac_addr[5] & 0x03) | (((mac_addr[4] & 0x7f)) << 2);
431         addr2 = ((mac_addr[4] & 0x80) >> 7) | mac_addr[3] << 1;
432         addr3 = (mac_addr[2] & 0xff) | ((mac_addr[1] & 1) << 8);
433
434         hash_result = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
435         hash_result = hash_result & 0x07ff;
436         return hash_result;
437 }
438
439 /*
440  * ----------------------------------------------------------------------------
441  * This function will add/del an entry to the address table.
442  * Inputs
443  * pep - ETHERNET .
444  * mac_addr - MAC address.
445  * skip - if 1, skip this address.Used in case of deleting an entry which is a
446  *        part of chain in the hash table.We can't just delete the entry since
447  *        that will break the chain.We need to defragment the tables time to
448  *        time.
449  * rd   - 0 Discard packet upon match.
450  *      - 1 Receive packet upon match.
451  * Outputs
452  * address table entry is added/deleted.
453  * 0 if success.
454  * -ENOSPC if table full
455  */
456 static int add_del_hash_entry(struct pxa168_eth_private *pep,
457                               unsigned char *mac_addr,
458                               u32 rd, u32 skip, int del)
459 {
460         struct addr_table_entry *entry, *start;
461         u32 new_high;
462         u32 new_low;
463         u32 i;
464
465         new_low = (((mac_addr[1] >> 4) & 0xf) << 15)
466             | (((mac_addr[1] >> 0) & 0xf) << 11)
467             | (((mac_addr[0] >> 4) & 0xf) << 7)
468             | (((mac_addr[0] >> 0) & 0xf) << 3)
469             | (((mac_addr[3] >> 4) & 0x1) << 31)
470             | (((mac_addr[3] >> 0) & 0xf) << 27)
471             | (((mac_addr[2] >> 4) & 0xf) << 23)
472             | (((mac_addr[2] >> 0) & 0xf) << 19)
473             | (skip << SKIP) | (rd << HASH_ENTRY_RECEIVE_DISCARD_BIT)
474             | HASH_ENTRY_VALID;
475
476         new_high = (((mac_addr[5] >> 4) & 0xf) << 15)
477             | (((mac_addr[5] >> 0) & 0xf) << 11)
478             | (((mac_addr[4] >> 4) & 0xf) << 7)
479             | (((mac_addr[4] >> 0) & 0xf) << 3)
480             | (((mac_addr[3] >> 5) & 0x7) << 0);
481
482         /*
483          * Pick the appropriate table, start scanning for free/reusable
484          * entries at the index obtained by hashing the specified MAC address
485          */
486         start = pep->htpr;
487         entry = start + hash_function(mac_addr);
488         for (i = 0; i < HOP_NUMBER; i++) {
489                 if (!(le32_to_cpu(entry->lo) & HASH_ENTRY_VALID)) {
490                         break;
491                 } else {
492                         /* if same address put in same position */
493                         if (((le32_to_cpu(entry->lo) & 0xfffffff8) ==
494                                 (new_low & 0xfffffff8)) &&
495                                 (le32_to_cpu(entry->hi) == new_high)) {
496                                 break;
497                         }
498                 }
499                 if (entry == start + 0x7ff)
500                         entry = start;
501                 else
502                         entry++;
503         }
504
505         if (((le32_to_cpu(entry->lo) & 0xfffffff8) != (new_low & 0xfffffff8)) &&
506             (le32_to_cpu(entry->hi) != new_high) && del)
507                 return 0;
508
509         if (i == HOP_NUMBER) {
510                 if (!del) {
511                         printk(KERN_INFO "%s: table section is full, need to "
512                                         "move to 16kB implementation?\n",
513                                          __FILE__);
514                         return -ENOSPC;
515                 } else
516                         return 0;
517         }
518
519         /*
520          * Update the selected entry
521          */
522         if (del) {
523                 entry->hi = 0;
524                 entry->lo = 0;
525         } else {
526                 entry->hi = cpu_to_le32(new_high);
527                 entry->lo = cpu_to_le32(new_low);
528         }
529
530         return 0;
531 }
532
533 /*
534  * ----------------------------------------------------------------------------
535  *  Create an addressTable entry from MAC address info
536  *  found in the specifed net_device struct
537  *
538  *  Input : pointer to ethernet interface network device structure
539  *  Output : N/A
540  */
541 static void update_hash_table_mac_address(struct pxa168_eth_private *pep,
542                                           unsigned char *oaddr,
543                                           unsigned char *addr)
544 {
545         /* Delete old entry */
546         if (oaddr)
547                 add_del_hash_entry(pep, oaddr, 1, 0, HASH_DELETE);
548         /* Add new entry */
549         add_del_hash_entry(pep, addr, 1, 0, HASH_ADD);
550 }
551
552 static int init_hash_table(struct pxa168_eth_private *pep)
553 {
554         /*
555          * Hardware expects CPU to build a hash table based on a predefined
556          * hash function and populate it based on hardware address. The
557          * location of the hash table is identified by 32-bit pointer stored
558          * in HTPR internal register. Two possible sizes exists for the hash
559          * table 8kB (256kB of DRAM required (4 x 64 kB banks)) and 1/2kB
560          * (16kB of DRAM required (4 x 4 kB banks)).We currently only support
561          * 1/2kB.
562          */
563         /* TODO: Add support for 8kB hash table and alternative hash
564          * function.Driver can dynamically switch to them if the 1/2kB hash
565          * table is full.
566          */
567         if (pep->htpr == NULL) {
568                 pep->htpr = dma_zalloc_coherent(pep->dev->dev.parent,
569                                                 HASH_ADDR_TABLE_SIZE,
570                                                 &pep->htpr_dma, GFP_KERNEL);
571                 if (pep->htpr == NULL)
572                         return -ENOMEM;
573         } else {
574                 memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
575         }
576         wrl(pep, HTPR, pep->htpr_dma);
577         return 0;
578 }
579
580 static void pxa168_eth_set_rx_mode(struct net_device *dev)
581 {
582         struct pxa168_eth_private *pep = netdev_priv(dev);
583         struct netdev_hw_addr *ha;
584         u32 val;
585
586         val = rdl(pep, PORT_CONFIG);
587         if (dev->flags & IFF_PROMISC)
588                 val |= PCR_PM;
589         else
590                 val &= ~PCR_PM;
591         wrl(pep, PORT_CONFIG, val);
592
593         /*
594          * Remove the old list of MAC address and add dev->addr
595          * and multicast address.
596          */
597         memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
598         update_hash_table_mac_address(pep, NULL, dev->dev_addr);
599
600         netdev_for_each_mc_addr(ha, dev)
601                 update_hash_table_mac_address(pep, NULL, ha->addr);
602 }
603
604 static int pxa168_eth_set_mac_address(struct net_device *dev, void *addr)
605 {
606         struct sockaddr *sa = addr;
607         struct pxa168_eth_private *pep = netdev_priv(dev);
608         unsigned char oldMac[ETH_ALEN];
609
610         if (!is_valid_ether_addr(sa->sa_data))
611                 return -EADDRNOTAVAIL;
612         memcpy(oldMac, dev->dev_addr, ETH_ALEN);
613         memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
614         netif_addr_lock_bh(dev);
615         update_hash_table_mac_address(pep, oldMac, dev->dev_addr);
616         netif_addr_unlock_bh(dev);
617         return 0;
618 }
619
620 static void eth_port_start(struct net_device *dev)
621 {
622         unsigned int val = 0;
623         struct pxa168_eth_private *pep = netdev_priv(dev);
624         int tx_curr_desc, rx_curr_desc;
625
626         /* Perform PHY reset, if there is a PHY. */
627         if (pep->phy != NULL) {
628                 struct ethtool_cmd cmd;
629
630                 pxa168_get_settings(pep->dev, &cmd);
631                 phy_init_hw(pep->phy);
632                 pxa168_set_settings(pep->dev, &cmd);
633         }
634
635         /* Assignment of Tx CTRP of given queue */
636         tx_curr_desc = pep->tx_curr_desc_q;
637         wrl(pep, ETH_C_TX_DESC_1,
638             (u32) (pep->tx_desc_dma + tx_curr_desc * sizeof(struct tx_desc)));
639
640         /* Assignment of Rx CRDP of given queue */
641         rx_curr_desc = pep->rx_curr_desc_q;
642         wrl(pep, ETH_C_RX_DESC_0,
643             (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
644
645         wrl(pep, ETH_F_RX_DESC_0,
646             (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
647
648         /* Clear all interrupts */
649         wrl(pep, INT_CAUSE, 0);
650
651         /* Enable all interrupts for receive, transmit and error. */
652         wrl(pep, INT_MASK, ALL_INTS);
653
654         val = rdl(pep, PORT_CONFIG);
655         val |= PCR_EN;
656         wrl(pep, PORT_CONFIG, val);
657
658         /* Start RX DMA engine */
659         val = rdl(pep, SDMA_CMD);
660         val |= SDMA_CMD_ERD;
661         wrl(pep, SDMA_CMD, val);
662 }
663
664 static void eth_port_reset(struct net_device *dev)
665 {
666         struct pxa168_eth_private *pep = netdev_priv(dev);
667         unsigned int val = 0;
668
669         /* Stop all interrupts for receive, transmit and error. */
670         wrl(pep, INT_MASK, 0);
671
672         /* Clear all interrupts */
673         wrl(pep, INT_CAUSE, 0);
674
675         /* Stop RX DMA */
676         val = rdl(pep, SDMA_CMD);
677         val &= ~SDMA_CMD_ERD;   /* abort dma command */
678
679         /* Abort any transmit and receive operations and put DMA
680          * in idle state.
681          */
682         abort_dma(pep);
683
684         /* Disable port */
685         val = rdl(pep, PORT_CONFIG);
686         val &= ~PCR_EN;
687         wrl(pep, PORT_CONFIG, val);
688 }
689
690 /*
691  * txq_reclaim - Free the tx desc data for completed descriptors
692  * If force is non-zero, frees uncompleted descriptors as well
693  */
694 static int txq_reclaim(struct net_device *dev, int force)
695 {
696         struct pxa168_eth_private *pep = netdev_priv(dev);
697         struct tx_desc *desc;
698         u32 cmd_sts;
699         struct sk_buff *skb;
700         int tx_index;
701         dma_addr_t addr;
702         int count;
703         int released = 0;
704
705         netif_tx_lock(dev);
706
707         pep->work_todo &= ~WORK_TX_DONE;
708         while (pep->tx_desc_count > 0) {
709                 tx_index = pep->tx_used_desc_q;
710                 desc = &pep->p_tx_desc_area[tx_index];
711                 cmd_sts = desc->cmd_sts;
712                 if (!force && (cmd_sts & BUF_OWNED_BY_DMA)) {
713                         if (released > 0) {
714                                 goto txq_reclaim_end;
715                         } else {
716                                 released = -1;
717                                 goto txq_reclaim_end;
718                         }
719                 }
720                 pep->tx_used_desc_q = (tx_index + 1) % pep->tx_ring_size;
721                 pep->tx_desc_count--;
722                 addr = desc->buf_ptr;
723                 count = desc->byte_cnt;
724                 skb = pep->tx_skb[tx_index];
725                 if (skb)
726                         pep->tx_skb[tx_index] = NULL;
727
728                 if (cmd_sts & TX_ERROR) {
729                         if (net_ratelimit())
730                                 printk(KERN_ERR "%s: Error in TX\n", dev->name);
731                         dev->stats.tx_errors++;
732                 }
733                 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
734                 if (skb)
735                         dev_kfree_skb_irq(skb);
736                 released++;
737         }
738 txq_reclaim_end:
739         netif_tx_unlock(dev);
740         return released;
741 }
742
743 static void pxa168_eth_tx_timeout(struct net_device *dev)
744 {
745         struct pxa168_eth_private *pep = netdev_priv(dev);
746
747         printk(KERN_INFO "%s: TX timeout  desc_count %d\n",
748                dev->name, pep->tx_desc_count);
749
750         schedule_work(&pep->tx_timeout_task);
751 }
752
753 static void pxa168_eth_tx_timeout_task(struct work_struct *work)
754 {
755         struct pxa168_eth_private *pep = container_of(work,
756                                                  struct pxa168_eth_private,
757                                                  tx_timeout_task);
758         struct net_device *dev = pep->dev;
759         pxa168_eth_stop(dev);
760         pxa168_eth_open(dev);
761 }
762
763 static int rxq_process(struct net_device *dev, int budget)
764 {
765         struct pxa168_eth_private *pep = netdev_priv(dev);
766         struct net_device_stats *stats = &dev->stats;
767         unsigned int received_packets = 0;
768         struct sk_buff *skb;
769
770         while (budget-- > 0) {
771                 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
772                 struct rx_desc *rx_desc;
773                 unsigned int cmd_sts;
774
775                 /* Do not process Rx ring in case of Rx ring resource error */
776                 if (pep->rx_resource_err)
777                         break;
778                 rx_curr_desc = pep->rx_curr_desc_q;
779                 rx_used_desc = pep->rx_used_desc_q;
780                 rx_desc = &pep->p_rx_desc_area[rx_curr_desc];
781                 cmd_sts = rx_desc->cmd_sts;
782                 rmb();
783                 if (cmd_sts & (BUF_OWNED_BY_DMA))
784                         break;
785                 skb = pep->rx_skb[rx_curr_desc];
786                 pep->rx_skb[rx_curr_desc] = NULL;
787
788                 rx_next_curr_desc = (rx_curr_desc + 1) % pep->rx_ring_size;
789                 pep->rx_curr_desc_q = rx_next_curr_desc;
790
791                 /* Rx descriptors exhausted. */
792                 /* Set the Rx ring resource error flag */
793                 if (rx_next_curr_desc == rx_used_desc)
794                         pep->rx_resource_err = 1;
795                 pep->rx_desc_count--;
796                 dma_unmap_single(NULL, rx_desc->buf_ptr,
797                                  rx_desc->buf_size,
798                                  DMA_FROM_DEVICE);
799                 received_packets++;
800                 /*
801                  * Update statistics.
802                  * Note byte count includes 4 byte CRC count
803                  */
804                 stats->rx_packets++;
805                 stats->rx_bytes += rx_desc->byte_cnt;
806                 /*
807                  * In case received a packet without first / last bits on OR
808                  * the error summary bit is on, the packets needs to be droped.
809                  */
810                 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
811                      (RX_FIRST_DESC | RX_LAST_DESC))
812                     || (cmd_sts & RX_ERROR)) {
813
814                         stats->rx_dropped++;
815                         if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
816                             (RX_FIRST_DESC | RX_LAST_DESC)) {
817                                 if (net_ratelimit())
818                                         printk(KERN_ERR
819                                                "%s: Rx pkt on multiple desc\n",
820                                                dev->name);
821                         }
822                         if (cmd_sts & RX_ERROR)
823                                 stats->rx_errors++;
824                         dev_kfree_skb_irq(skb);
825                 } else {
826                         /*
827                          * The -4 is for the CRC in the trailer of the
828                          * received packet
829                          */
830                         skb_put(skb, rx_desc->byte_cnt - 4);
831                         skb->protocol = eth_type_trans(skb, dev);
832                         netif_receive_skb(skb);
833                 }
834         }
835         /* Fill RX ring with skb's */
836         rxq_refill(dev);
837         return received_packets;
838 }
839
840 static int pxa168_eth_collect_events(struct pxa168_eth_private *pep,
841                                      struct net_device *dev)
842 {
843         u32 icr;
844         int ret = 0;
845
846         icr = rdl(pep, INT_CAUSE);
847         if (icr == 0)
848                 return IRQ_NONE;
849
850         wrl(pep, INT_CAUSE, ~icr);
851         if (icr & (ICR_TXBUF_H | ICR_TXBUF_L)) {
852                 pep->work_todo |= WORK_TX_DONE;
853                 ret = 1;
854         }
855         if (icr & ICR_RXBUF)
856                 ret = 1;
857         if (icr & ICR_MII_CH) {
858                 pep->work_todo |= WORK_LINK;
859                 ret = 1;
860         }
861         return ret;
862 }
863
864 static void handle_link_event(struct pxa168_eth_private *pep)
865 {
866         struct net_device *dev = pep->dev;
867         u32 port_status;
868         int speed;
869         int duplex;
870         int fc;
871
872         port_status = rdl(pep, PORT_STATUS);
873         if (!(port_status & LINK_UP)) {
874                 if (netif_carrier_ok(dev)) {
875                         printk(KERN_INFO "%s: link down\n", dev->name);
876                         netif_carrier_off(dev);
877                         txq_reclaim(dev, 1);
878                 }
879                 return;
880         }
881         if (port_status & PORT_SPEED_100)
882                 speed = 100;
883         else
884                 speed = 10;
885
886         duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
887         fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
888         printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
889                "flow control %sabled\n", dev->name,
890                speed, duplex ? "full" : "half", fc ? "en" : "dis");
891         if (!netif_carrier_ok(dev))
892                 netif_carrier_on(dev);
893 }
894
895 static irqreturn_t pxa168_eth_int_handler(int irq, void *dev_id)
896 {
897         struct net_device *dev = (struct net_device *)dev_id;
898         struct pxa168_eth_private *pep = netdev_priv(dev);
899
900         if (unlikely(!pxa168_eth_collect_events(pep, dev)))
901                 return IRQ_NONE;
902         /* Disable interrupts */
903         wrl(pep, INT_MASK, 0);
904         napi_schedule(&pep->napi);
905         return IRQ_HANDLED;
906 }
907
908 static void pxa168_eth_recalc_skb_size(struct pxa168_eth_private *pep)
909 {
910         int skb_size;
911
912         /*
913          * Reserve 2+14 bytes for an ethernet header (the hardware
914          * automatically prepends 2 bytes of dummy data to each
915          * received packet), 16 bytes for up to four VLAN tags, and
916          * 4 bytes for the trailing FCS -- 36 bytes total.
917          */
918         skb_size = pep->dev->mtu + 36;
919
920         /*
921          * Make sure that the skb size is a multiple of 8 bytes, as
922          * the lower three bits of the receive descriptor's buffer
923          * size field are ignored by the hardware.
924          */
925         pep->skb_size = (skb_size + 7) & ~7;
926
927         /*
928          * If NET_SKB_PAD is smaller than a cache line,
929          * netdev_alloc_skb() will cause skb->data to be misaligned
930          * to a cache line boundary.  If this is the case, include
931          * some extra space to allow re-aligning the data area.
932          */
933         pep->skb_size += SKB_DMA_REALIGN;
934
935 }
936
937 static int set_port_config_ext(struct pxa168_eth_private *pep)
938 {
939         int skb_size;
940
941         pxa168_eth_recalc_skb_size(pep);
942         if  (pep->skb_size <= 1518)
943                 skb_size = PCXR_MFL_1518;
944         else if (pep->skb_size <= 1536)
945                 skb_size = PCXR_MFL_1536;
946         else if (pep->skb_size <= 2048)
947                 skb_size = PCXR_MFL_2048;
948         else
949                 skb_size = PCXR_MFL_64K;
950
951         /* Extended Port Configuration */
952         wrl(pep,
953             PORT_CONFIG_EXT, PCXR_2BSM | /* Two byte prefix aligns IP hdr */
954             PCXR_DSCP_EN |               /* Enable DSCP in IP */
955             skb_size | PCXR_FLP |        /* do not force link pass */
956             PCXR_TX_HIGH_PRI);           /* Transmit - high priority queue */
957
958         return 0;
959 }
960
961 static int pxa168_init_hw(struct pxa168_eth_private *pep)
962 {
963         int err = 0;
964
965         /* Disable interrupts */
966         wrl(pep, INT_MASK, 0);
967         wrl(pep, INT_CAUSE, 0);
968         /* Write to ICR to clear interrupts. */
969         wrl(pep, INT_W_CLEAR, 0);
970         /* Abort any transmit and receive operations and put DMA
971          * in idle state.
972          */
973         abort_dma(pep);
974         /* Initialize address hash table */
975         err = init_hash_table(pep);
976         if (err)
977                 return err;
978         /* SDMA configuration */
979         wrl(pep, SDMA_CONFIG, SDCR_BSZ8 |       /* Burst size = 32 bytes */
980             SDCR_RIFB |                         /* Rx interrupt on frame */
981             SDCR_BLMT |                         /* Little endian transmit */
982             SDCR_BLMR |                         /* Little endian receive */
983             SDCR_RC_MAX_RETRANS);               /* Max retransmit count */
984         /* Port Configuration */
985         wrl(pep, PORT_CONFIG, PCR_HS);          /* Hash size is 1/2kb */
986         set_port_config_ext(pep);
987
988         return err;
989 }
990
991 static int rxq_init(struct net_device *dev)
992 {
993         struct pxa168_eth_private *pep = netdev_priv(dev);
994         struct rx_desc *p_rx_desc;
995         int size = 0, i = 0;
996         int rx_desc_num = pep->rx_ring_size;
997
998         /* Allocate RX skb rings */
999         pep->rx_skb = kzalloc(sizeof(*pep->rx_skb) * pep->rx_ring_size,
1000                              GFP_KERNEL);
1001         if (!pep->rx_skb)
1002                 return -ENOMEM;
1003
1004         /* Allocate RX ring */
1005         pep->rx_desc_count = 0;
1006         size = pep->rx_ring_size * sizeof(struct rx_desc);
1007         pep->rx_desc_area_size = size;
1008         pep->p_rx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1009                                                   &pep->rx_desc_dma,
1010                                                   GFP_KERNEL);
1011         if (!pep->p_rx_desc_area)
1012                 goto out;
1013
1014         /* initialize the next_desc_ptr links in the Rx descriptors ring */
1015         p_rx_desc = pep->p_rx_desc_area;
1016         for (i = 0; i < rx_desc_num; i++) {
1017                 p_rx_desc[i].next_desc_ptr = pep->rx_desc_dma +
1018                     ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
1019         }
1020         /* Save Rx desc pointer to driver struct. */
1021         pep->rx_curr_desc_q = 0;
1022         pep->rx_used_desc_q = 0;
1023         pep->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
1024         return 0;
1025 out:
1026         kfree(pep->rx_skb);
1027         return -ENOMEM;
1028 }
1029
1030 static void rxq_deinit(struct net_device *dev)
1031 {
1032         struct pxa168_eth_private *pep = netdev_priv(dev);
1033         int curr;
1034
1035         /* Free preallocated skb's on RX rings */
1036         for (curr = 0; pep->rx_desc_count && curr < pep->rx_ring_size; curr++) {
1037                 if (pep->rx_skb[curr]) {
1038                         dev_kfree_skb(pep->rx_skb[curr]);
1039                         pep->rx_desc_count--;
1040                 }
1041         }
1042         if (pep->rx_desc_count)
1043                 printk(KERN_ERR
1044                        "Error in freeing Rx Ring. %d skb's still\n",
1045                        pep->rx_desc_count);
1046         /* Free RX ring */
1047         if (pep->p_rx_desc_area)
1048                 dma_free_coherent(pep->dev->dev.parent, pep->rx_desc_area_size,
1049                                   pep->p_rx_desc_area, pep->rx_desc_dma);
1050         kfree(pep->rx_skb);
1051 }
1052
1053 static int txq_init(struct net_device *dev)
1054 {
1055         struct pxa168_eth_private *pep = netdev_priv(dev);
1056         struct tx_desc *p_tx_desc;
1057         int size = 0, i = 0;
1058         int tx_desc_num = pep->tx_ring_size;
1059
1060         pep->tx_skb = kzalloc(sizeof(*pep->tx_skb) * pep->tx_ring_size,
1061                              GFP_KERNEL);
1062         if (!pep->tx_skb)
1063                 return -ENOMEM;
1064
1065         /* Allocate TX ring */
1066         pep->tx_desc_count = 0;
1067         size = pep->tx_ring_size * sizeof(struct tx_desc);
1068         pep->tx_desc_area_size = size;
1069         pep->p_tx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
1070                                                   &pep->tx_desc_dma,
1071                                                   GFP_KERNEL);
1072         if (!pep->p_tx_desc_area)
1073                 goto out;
1074         /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1075         p_tx_desc = pep->p_tx_desc_area;
1076         for (i = 0; i < tx_desc_num; i++) {
1077                 p_tx_desc[i].next_desc_ptr = pep->tx_desc_dma +
1078                     ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
1079         }
1080         pep->tx_curr_desc_q = 0;
1081         pep->tx_used_desc_q = 0;
1082         pep->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
1083         return 0;
1084 out:
1085         kfree(pep->tx_skb);
1086         return -ENOMEM;
1087 }
1088
1089 static void txq_deinit(struct net_device *dev)
1090 {
1091         struct pxa168_eth_private *pep = netdev_priv(dev);
1092
1093         /* Free outstanding skb's on TX ring */
1094         txq_reclaim(dev, 1);
1095         BUG_ON(pep->tx_used_desc_q != pep->tx_curr_desc_q);
1096         /* Free TX ring */
1097         if (pep->p_tx_desc_area)
1098                 dma_free_coherent(pep->dev->dev.parent, pep->tx_desc_area_size,
1099                                   pep->p_tx_desc_area, pep->tx_desc_dma);
1100         kfree(pep->tx_skb);
1101 }
1102
1103 static int pxa168_eth_open(struct net_device *dev)
1104 {
1105         struct pxa168_eth_private *pep = netdev_priv(dev);
1106         int err;
1107
1108         err = request_irq(dev->irq, pxa168_eth_int_handler, 0, dev->name, dev);
1109         if (err) {
1110                 dev_err(&dev->dev, "can't assign irq\n");
1111                 return -EAGAIN;
1112         }
1113         pep->rx_resource_err = 0;
1114         err = rxq_init(dev);
1115         if (err != 0)
1116                 goto out_free_irq;
1117         err = txq_init(dev);
1118         if (err != 0)
1119                 goto out_free_rx_skb;
1120         pep->rx_used_desc_q = 0;
1121         pep->rx_curr_desc_q = 0;
1122
1123         /* Fill RX ring with skb's */
1124         rxq_refill(dev);
1125         pep->rx_used_desc_q = 0;
1126         pep->rx_curr_desc_q = 0;
1127         netif_carrier_off(dev);
1128         eth_port_start(dev);
1129         napi_enable(&pep->napi);
1130         return 0;
1131 out_free_rx_skb:
1132         rxq_deinit(dev);
1133 out_free_irq:
1134         free_irq(dev->irq, dev);
1135         return err;
1136 }
1137
1138 static int pxa168_eth_stop(struct net_device *dev)
1139 {
1140         struct pxa168_eth_private *pep = netdev_priv(dev);
1141         eth_port_reset(dev);
1142
1143         /* Disable interrupts */
1144         wrl(pep, INT_MASK, 0);
1145         wrl(pep, INT_CAUSE, 0);
1146         /* Write to ICR to clear interrupts. */
1147         wrl(pep, INT_W_CLEAR, 0);
1148         napi_disable(&pep->napi);
1149         del_timer_sync(&pep->timeout);
1150         netif_carrier_off(dev);
1151         free_irq(dev->irq, dev);
1152         rxq_deinit(dev);
1153         txq_deinit(dev);
1154
1155         return 0;
1156 }
1157
1158 static int pxa168_eth_change_mtu(struct net_device *dev, int mtu)
1159 {
1160         int retval;
1161         struct pxa168_eth_private *pep = netdev_priv(dev);
1162
1163         if ((mtu > 9500) || (mtu < 68))
1164                 return -EINVAL;
1165
1166         dev->mtu = mtu;
1167         retval = set_port_config_ext(pep);
1168
1169         if (!netif_running(dev))
1170                 return 0;
1171
1172         /*
1173          * Stop and then re-open the interface. This will allocate RX
1174          * skbs of the new MTU.
1175          * There is a possible danger that the open will not succeed,
1176          * due to memory being full.
1177          */
1178         pxa168_eth_stop(dev);
1179         if (pxa168_eth_open(dev)) {
1180                 dev_err(&dev->dev,
1181                         "fatal error on re-opening device after MTU change\n");
1182         }
1183
1184         return 0;
1185 }
1186
1187 static int eth_alloc_tx_desc_index(struct pxa168_eth_private *pep)
1188 {
1189         int tx_desc_curr;
1190
1191         tx_desc_curr = pep->tx_curr_desc_q;
1192         pep->tx_curr_desc_q = (tx_desc_curr + 1) % pep->tx_ring_size;
1193         BUG_ON(pep->tx_curr_desc_q == pep->tx_used_desc_q);
1194         pep->tx_desc_count++;
1195
1196         return tx_desc_curr;
1197 }
1198
1199 static int pxa168_rx_poll(struct napi_struct *napi, int budget)
1200 {
1201         struct pxa168_eth_private *pep =
1202             container_of(napi, struct pxa168_eth_private, napi);
1203         struct net_device *dev = pep->dev;
1204         int work_done = 0;
1205
1206         if (unlikely(pep->work_todo & WORK_LINK)) {
1207                 pep->work_todo &= ~(WORK_LINK);
1208                 handle_link_event(pep);
1209         }
1210         /*
1211          * We call txq_reclaim every time since in NAPI interupts are disabled
1212          * and due to this we miss the TX_DONE interrupt,which is not updated in
1213          * interrupt status register.
1214          */
1215         txq_reclaim(dev, 0);
1216         if (netif_queue_stopped(dev)
1217             && pep->tx_ring_size - pep->tx_desc_count > 1) {
1218                 netif_wake_queue(dev);
1219         }
1220         work_done = rxq_process(dev, budget);
1221         if (work_done < budget) {
1222                 napi_complete(napi);
1223                 wrl(pep, INT_MASK, ALL_INTS);
1224         }
1225
1226         return work_done;
1227 }
1228
1229 static int pxa168_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1230 {
1231         struct pxa168_eth_private *pep = netdev_priv(dev);
1232         struct net_device_stats *stats = &dev->stats;
1233         struct tx_desc *desc;
1234         int tx_index;
1235         int length;
1236
1237         tx_index = eth_alloc_tx_desc_index(pep);
1238         desc = &pep->p_tx_desc_area[tx_index];
1239         length = skb->len;
1240         pep->tx_skb[tx_index] = skb;
1241         desc->byte_cnt = length;
1242         desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
1243
1244         skb_tx_timestamp(skb);
1245
1246         wmb();
1247         desc->cmd_sts = BUF_OWNED_BY_DMA | TX_GEN_CRC | TX_FIRST_DESC |
1248                         TX_ZERO_PADDING | TX_LAST_DESC | TX_EN_INT;
1249         wmb();
1250         wrl(pep, SDMA_CMD, SDMA_CMD_TXDH | SDMA_CMD_ERD);
1251
1252         stats->tx_bytes += length;
1253         stats->tx_packets++;
1254         dev->trans_start = jiffies;
1255         if (pep->tx_ring_size - pep->tx_desc_count <= 1) {
1256                 /* We handled the current skb, but now we are out of space.*/
1257                 netif_stop_queue(dev);
1258         }
1259
1260         return NETDEV_TX_OK;
1261 }
1262
1263 static int smi_wait_ready(struct pxa168_eth_private *pep)
1264 {
1265         int i = 0;
1266
1267         /* wait for the SMI register to become available */
1268         for (i = 0; rdl(pep, SMI) & SMI_BUSY; i++) {
1269                 if (i == PHY_WAIT_ITERATIONS)
1270                         return -ETIMEDOUT;
1271                 msleep(10);
1272         }
1273
1274         return 0;
1275 }
1276
1277 static int pxa168_smi_read(struct mii_bus *bus, int phy_addr, int regnum)
1278 {
1279         struct pxa168_eth_private *pep = bus->priv;
1280         int i = 0;
1281         int val;
1282
1283         if (smi_wait_ready(pep)) {
1284                 printk(KERN_WARNING "pxa168_eth: SMI bus busy timeout\n");
1285                 return -ETIMEDOUT;
1286         }
1287         wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) | SMI_OP_R);
1288         /* now wait for the data to be valid */
1289         for (i = 0; !((val = rdl(pep, SMI)) & SMI_R_VALID); i++) {
1290                 if (i == PHY_WAIT_ITERATIONS) {
1291                         printk(KERN_WARNING
1292                                 "pxa168_eth: SMI bus read not valid\n");
1293                         return -ENODEV;
1294                 }
1295                 msleep(10);
1296         }
1297
1298         return val & 0xffff;
1299 }
1300
1301 static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
1302                             u16 value)
1303 {
1304         struct pxa168_eth_private *pep = bus->priv;
1305
1306         if (smi_wait_ready(pep)) {
1307                 printk(KERN_WARNING "pxa168_eth: SMI bus busy timeout\n");
1308                 return -ETIMEDOUT;
1309         }
1310
1311         wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) |
1312             SMI_OP_W | (value & 0xffff));
1313
1314         if (smi_wait_ready(pep)) {
1315                 printk(KERN_ERR "pxa168_eth: SMI bus busy timeout\n");
1316                 return -ETIMEDOUT;
1317         }
1318
1319         return 0;
1320 }
1321
1322 static int pxa168_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr,
1323                                int cmd)
1324 {
1325         struct pxa168_eth_private *pep = netdev_priv(dev);
1326         if (pep->phy != NULL)
1327                 return phy_mii_ioctl(pep->phy, ifr, cmd);
1328
1329         return -EOPNOTSUPP;
1330 }
1331
1332 static struct phy_device *phy_scan(struct pxa168_eth_private *pep, int phy_addr)
1333 {
1334         struct mii_bus *bus = pep->smi_bus;
1335         struct phy_device *phydev;
1336         int start;
1337         int num;
1338         int i;
1339
1340         if (phy_addr == PXA168_ETH_PHY_ADDR_DEFAULT) {
1341                 /* Scan entire range */
1342                 start = ethernet_phy_get(pep);
1343                 num = 32;
1344         } else {
1345                 /* Use phy addr specific to platform */
1346                 start = phy_addr & 0x1f;
1347                 num = 1;
1348         }
1349         phydev = NULL;
1350         for (i = 0; i < num; i++) {
1351                 int addr = (start + i) & 0x1f;
1352                 if (bus->phy_map[addr] == NULL)
1353                         mdiobus_scan(bus, addr);
1354
1355                 if (phydev == NULL) {
1356                         phydev = bus->phy_map[addr];
1357                         if (phydev != NULL)
1358                                 ethernet_phy_set_addr(pep, addr);
1359                 }
1360         }
1361
1362         return phydev;
1363 }
1364
1365 static void phy_init(struct pxa168_eth_private *pep, int speed, int duplex)
1366 {
1367         struct phy_device *phy = pep->phy;
1368
1369         phy_attach(pep->dev, dev_name(&phy->dev), PHY_INTERFACE_MODE_MII);
1370
1371         if (speed == 0) {
1372                 phy->autoneg = AUTONEG_ENABLE;
1373                 phy->speed = 0;
1374                 phy->duplex = 0;
1375                 phy->supported &= PHY_BASIC_FEATURES;
1376                 phy->advertising = phy->supported | ADVERTISED_Autoneg;
1377         } else {
1378                 phy->autoneg = AUTONEG_DISABLE;
1379                 phy->advertising = 0;
1380                 phy->speed = speed;
1381                 phy->duplex = duplex;
1382         }
1383         phy_start_aneg(phy);
1384 }
1385
1386 static int ethernet_phy_setup(struct net_device *dev)
1387 {
1388         struct pxa168_eth_private *pep = netdev_priv(dev);
1389
1390         if (pep->pd->init)
1391                 pep->pd->init();
1392         pep->phy = phy_scan(pep, pep->pd->phy_addr & 0x1f);
1393         if (pep->phy != NULL)
1394                 phy_init(pep, pep->pd->speed, pep->pd->duplex);
1395         update_hash_table_mac_address(pep, NULL, dev->dev_addr);
1396
1397         return 0;
1398 }
1399
1400 static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1401 {
1402         struct pxa168_eth_private *pep = netdev_priv(dev);
1403         int err;
1404
1405         err = phy_read_status(pep->phy);
1406         if (err == 0)
1407                 err = phy_ethtool_gset(pep->phy, cmd);
1408
1409         return err;
1410 }
1411
1412 static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1413 {
1414         struct pxa168_eth_private *pep = netdev_priv(dev);
1415
1416         return phy_ethtool_sset(pep->phy, cmd);
1417 }
1418
1419 static void pxa168_get_drvinfo(struct net_device *dev,
1420                                struct ethtool_drvinfo *info)
1421 {
1422         strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1423         strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
1424         strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1425         strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
1426 }
1427
1428 static const struct ethtool_ops pxa168_ethtool_ops = {
1429         .get_settings = pxa168_get_settings,
1430         .set_settings = pxa168_set_settings,
1431         .get_drvinfo = pxa168_get_drvinfo,
1432         .get_link = ethtool_op_get_link,
1433         .get_ts_info = ethtool_op_get_ts_info,
1434 };
1435
1436 static const struct net_device_ops pxa168_eth_netdev_ops = {
1437         .ndo_open = pxa168_eth_open,
1438         .ndo_stop = pxa168_eth_stop,
1439         .ndo_start_xmit = pxa168_eth_start_xmit,
1440         .ndo_set_rx_mode = pxa168_eth_set_rx_mode,
1441         .ndo_set_mac_address = pxa168_eth_set_mac_address,
1442         .ndo_validate_addr = eth_validate_addr,
1443         .ndo_do_ioctl = pxa168_eth_do_ioctl,
1444         .ndo_change_mtu = pxa168_eth_change_mtu,
1445         .ndo_tx_timeout = pxa168_eth_tx_timeout,
1446 };
1447
1448 static int pxa168_eth_probe(struct platform_device *pdev)
1449 {
1450         struct pxa168_eth_private *pep = NULL;
1451         struct net_device *dev = NULL;
1452         struct resource *res;
1453         struct clk *clk;
1454         int err;
1455
1456         printk(KERN_NOTICE "PXA168 10/100 Ethernet Driver\n");
1457
1458         clk = clk_get(&pdev->dev, "MFUCLK");
1459         if (IS_ERR(clk)) {
1460                 printk(KERN_ERR "%s: Fast Ethernet failed to get clock\n",
1461                         DRIVER_NAME);
1462                 return -ENODEV;
1463         }
1464         clk_enable(clk);
1465
1466         dev = alloc_etherdev(sizeof(struct pxa168_eth_private));
1467         if (!dev) {
1468                 err = -ENOMEM;
1469                 goto err_clk;
1470         }
1471
1472         platform_set_drvdata(pdev, dev);
1473         pep = netdev_priv(dev);
1474         pep->dev = dev;
1475         pep->clk = clk;
1476         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1477         if (res == NULL) {
1478                 err = -ENODEV;
1479                 goto err_netdev;
1480         }
1481         pep->base = ioremap(res->start, resource_size(res));
1482         if (pep->base == NULL) {
1483                 err = -ENOMEM;
1484                 goto err_netdev;
1485         }
1486         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1487         BUG_ON(!res);
1488         dev->irq = res->start;
1489         dev->netdev_ops = &pxa168_eth_netdev_ops;
1490         dev->watchdog_timeo = 2 * HZ;
1491         dev->base_addr = 0;
1492         SET_ETHTOOL_OPS(dev, &pxa168_ethtool_ops);
1493
1494         INIT_WORK(&pep->tx_timeout_task, pxa168_eth_tx_timeout_task);
1495
1496         printk(KERN_INFO "%s:Using random mac address\n", DRIVER_NAME);
1497         eth_hw_addr_random(dev);
1498
1499         pep->pd = dev_get_platdata(&pdev->dev);
1500         pep->rx_ring_size = NUM_RX_DESCS;
1501         if (pep->pd->rx_queue_size)
1502                 pep->rx_ring_size = pep->pd->rx_queue_size;
1503
1504         pep->tx_ring_size = NUM_TX_DESCS;
1505         if (pep->pd->tx_queue_size)
1506                 pep->tx_ring_size = pep->pd->tx_queue_size;
1507
1508         pep->port_num = pep->pd->port_number;
1509         /* Hardware supports only 3 ports */
1510         BUG_ON(pep->port_num > 2);
1511         netif_napi_add(dev, &pep->napi, pxa168_rx_poll, pep->rx_ring_size);
1512
1513         memset(&pep->timeout, 0, sizeof(struct timer_list));
1514         init_timer(&pep->timeout);
1515         pep->timeout.function = rxq_refill_timer_wrapper;
1516         pep->timeout.data = (unsigned long)pep;
1517
1518         pep->smi_bus = mdiobus_alloc();
1519         if (pep->smi_bus == NULL) {
1520                 err = -ENOMEM;
1521                 goto err_base;
1522         }
1523         pep->smi_bus->priv = pep;
1524         pep->smi_bus->name = "pxa168_eth smi";
1525         pep->smi_bus->read = pxa168_smi_read;
1526         pep->smi_bus->write = pxa168_smi_write;
1527         snprintf(pep->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1528                 pdev->name, pdev->id);
1529         pep->smi_bus->parent = &pdev->dev;
1530         pep->smi_bus->phy_mask = 0xffffffff;
1531         err = mdiobus_register(pep->smi_bus);
1532         if (err)
1533                 goto err_free_mdio;
1534
1535         pxa168_init_hw(pep);
1536         err = ethernet_phy_setup(dev);
1537         if (err)
1538                 goto err_mdiobus;
1539         SET_NETDEV_DEV(dev, &pdev->dev);
1540         err = register_netdev(dev);
1541         if (err)
1542                 goto err_mdiobus;
1543         return 0;
1544
1545 err_mdiobus:
1546         mdiobus_unregister(pep->smi_bus);
1547 err_free_mdio:
1548         mdiobus_free(pep->smi_bus);
1549 err_base:
1550         iounmap(pep->base);
1551 err_netdev:
1552         free_netdev(dev);
1553 err_clk:
1554         clk_disable(clk);
1555         clk_put(clk);
1556         return err;
1557 }
1558
1559 static int pxa168_eth_remove(struct platform_device *pdev)
1560 {
1561         struct net_device *dev = platform_get_drvdata(pdev);
1562         struct pxa168_eth_private *pep = netdev_priv(dev);
1563
1564         if (pep->htpr) {
1565                 dma_free_coherent(pep->dev->dev.parent, HASH_ADDR_TABLE_SIZE,
1566                                   pep->htpr, pep->htpr_dma);
1567                 pep->htpr = NULL;
1568         }
1569         if (pep->clk) {
1570                 clk_disable(pep->clk);
1571                 clk_put(pep->clk);
1572                 pep->clk = NULL;
1573         }
1574         if (pep->phy != NULL)
1575                 phy_detach(pep->phy);
1576
1577         iounmap(pep->base);
1578         pep->base = NULL;
1579         mdiobus_unregister(pep->smi_bus);
1580         mdiobus_free(pep->smi_bus);
1581         unregister_netdev(dev);
1582         cancel_work_sync(&pep->tx_timeout_task);
1583         free_netdev(dev);
1584         return 0;
1585 }
1586
1587 static void pxa168_eth_shutdown(struct platform_device *pdev)
1588 {
1589         struct net_device *dev = platform_get_drvdata(pdev);
1590         eth_port_reset(dev);
1591 }
1592
1593 #ifdef CONFIG_PM
1594 static int pxa168_eth_resume(struct platform_device *pdev)
1595 {
1596         return -ENOSYS;
1597 }
1598
1599 static int pxa168_eth_suspend(struct platform_device *pdev, pm_message_t state)
1600 {
1601         return -ENOSYS;
1602 }
1603
1604 #else
1605 #define pxa168_eth_resume NULL
1606 #define pxa168_eth_suspend NULL
1607 #endif
1608
1609 static struct platform_driver pxa168_eth_driver = {
1610         .probe = pxa168_eth_probe,
1611         .remove = pxa168_eth_remove,
1612         .shutdown = pxa168_eth_shutdown,
1613         .resume = pxa168_eth_resume,
1614         .suspend = pxa168_eth_suspend,
1615         .driver = {
1616                    .name = DRIVER_NAME,
1617                    },
1618 };
1619
1620 module_platform_driver(pxa168_eth_driver);
1621
1622 MODULE_LICENSE("GPL");
1623 MODULE_DESCRIPTION("Ethernet driver for Marvell PXA168");
1624 MODULE_ALIAS("platform:pxa168_eth");