net: Use netdev_alloc_skb_ip_align()
[safe/jmp/linux-2.6] / drivers / net / davinci_emac.c
1 /*
2  * DaVinci Ethernet Medium Access Controller
3  *
4  * DaVinci EMAC is based upon CPPI 3.0 TI DMA engine
5  *
6  * Copyright (C) 2009 Texas Instruments.
7  *
8  * ---------------------------------------------------------------------------
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * ---------------------------------------------------------------------------
24  * History:
25  * 0-5 A number of folks worked on this driver in bits and pieces but the major
26  *     contribution came from Suraj Iyer and Anant Gole
27  * 6.0 Anant Gole - rewrote the driver as per Linux conventions
28  * 6.1 Chaithrika U S - added support for Gigabit and RMII features,
29  *     PHY layer usage
30  */
31
32 /** Pending Items in this driver:
33  * 1. Use Linux cache infrastcture for DMA'ed memory (dma_xxx functions)
34  */
35
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
40 #include <linux/timer.h>
41 #include <linux/errno.h>
42 #include <linux/in.h>
43 #include <linux/ioport.h>
44 #include <linux/slab.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/init.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/ethtool.h>
52 #include <linux/highmem.h>
53 #include <linux/proc_fs.h>
54 #include <linux/ctype.h>
55 #include <linux/version.h>
56 #include <linux/spinlock.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/clk.h>
59 #include <linux/platform_device.h>
60 #include <linux/semaphore.h>
61 #include <linux/phy.h>
62 #include <linux/bitops.h>
63 #include <linux/io.h>
64 #include <linux/uaccess.h>
65
66 #include <asm/irq.h>
67 #include <asm/page.h>
68
69 #include <mach/emac.h>
70
71 static int debug_level;
72 module_param(debug_level, int, 0);
73 MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
74
75 /* Netif debug messages possible */
76 #define DAVINCI_EMAC_DEBUG      (NETIF_MSG_DRV | \
77                                 NETIF_MSG_PROBE | \
78                                 NETIF_MSG_LINK | \
79                                 NETIF_MSG_TIMER | \
80                                 NETIF_MSG_IFDOWN | \
81                                 NETIF_MSG_IFUP | \
82                                 NETIF_MSG_RX_ERR | \
83                                 NETIF_MSG_TX_ERR | \
84                                 NETIF_MSG_TX_QUEUED | \
85                                 NETIF_MSG_INTR | \
86                                 NETIF_MSG_TX_DONE | \
87                                 NETIF_MSG_RX_STATUS | \
88                                 NETIF_MSG_PKTDATA | \
89                                 NETIF_MSG_HW | \
90                                 NETIF_MSG_WOL)
91
92 /* version info */
93 #define EMAC_MAJOR_VERSION      6
94 #define EMAC_MINOR_VERSION      1
95 #define EMAC_MODULE_VERSION     "6.1"
96 MODULE_VERSION(EMAC_MODULE_VERSION);
97 static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
98
99 /* Configuration items */
100 #define EMAC_DEF_PASS_CRC               (0) /* Do not pass CRC upto frames */
101 #define EMAC_DEF_QOS_EN                 (0) /* EMAC proprietary QoS disabled */
102 #define EMAC_DEF_NO_BUFF_CHAIN          (0) /* No buffer chain */
103 #define EMAC_DEF_MACCTRL_FRAME_EN       (0) /* Discard Maccontrol frames */
104 #define EMAC_DEF_SHORT_FRAME_EN         (0) /* Discard short frames */
105 #define EMAC_DEF_ERROR_FRAME_EN         (0) /* Discard error frames */
106 #define EMAC_DEF_PROM_EN                (0) /* Promiscous disabled */
107 #define EMAC_DEF_PROM_CH                (0) /* Promiscous channel is 0 */
108 #define EMAC_DEF_BCAST_EN               (1) /* Broadcast enabled */
109 #define EMAC_DEF_BCAST_CH               (0) /* Broadcast channel is 0 */
110 #define EMAC_DEF_MCAST_EN               (1) /* Multicast enabled */
111 #define EMAC_DEF_MCAST_CH               (0) /* Multicast channel is 0 */
112
113 #define EMAC_DEF_TXPRIO_FIXED           (1) /* TX Priority is fixed */
114 #define EMAC_DEF_TXPACING_EN            (0) /* TX pacing NOT supported*/
115
116 #define EMAC_DEF_BUFFER_OFFSET          (0) /* Buffer offset to DMA (future) */
117 #define EMAC_DEF_MIN_ETHPKTSIZE         (60) /* Minimum ethernet pkt size */
118 #define EMAC_DEF_MAX_FRAME_SIZE         (1500 + 14 + 4 + 4)
119 #define EMAC_DEF_TX_CH                  (0) /* Default 0th channel */
120 #define EMAC_DEF_RX_CH                  (0) /* Default 0th channel */
121 #define EMAC_DEF_MDIO_TICK_MS           (10) /* typically 1 tick=1 ms) */
122 #define EMAC_DEF_MAX_TX_CH              (1) /* Max TX channels configured */
123 #define EMAC_DEF_MAX_RX_CH              (1) /* Max RX channels configured */
124 #define EMAC_POLL_WEIGHT                (64) /* Default NAPI poll weight */
125
126 /* Buffer descriptor parameters */
127 #define EMAC_DEF_TX_MAX_SERVICE         (32) /* TX max service BD's */
128 #define EMAC_DEF_RX_MAX_SERVICE         (64) /* should = netdev->weight */
129
130 /* EMAC register related defines */
131 #define EMAC_ALL_MULTI_REG_VALUE        (0xFFFFFFFF)
132 #define EMAC_NUM_MULTICAST_BITS         (64)
133 #define EMAC_TEARDOWN_VALUE             (0xFFFFFFFC)
134 #define EMAC_TX_CONTROL_TX_ENABLE_VAL   (0x1)
135 #define EMAC_RX_CONTROL_RX_ENABLE_VAL   (0x1)
136 #define EMAC_MAC_HOST_ERR_INTMASK_VAL   (0x2)
137 #define EMAC_RX_UNICAST_CLEAR_ALL       (0xFF)
138 #define EMAC_INT_MASK_CLEAR             (0xFF)
139
140 /* RX MBP register bit positions */
141 #define EMAC_RXMBP_PASSCRC_MASK         BIT(30)
142 #define EMAC_RXMBP_QOSEN_MASK           BIT(29)
143 #define EMAC_RXMBP_NOCHAIN_MASK         BIT(28)
144 #define EMAC_RXMBP_CMFEN_MASK           BIT(24)
145 #define EMAC_RXMBP_CSFEN_MASK           BIT(23)
146 #define EMAC_RXMBP_CEFEN_MASK           BIT(22)
147 #define EMAC_RXMBP_CAFEN_MASK           BIT(21)
148 #define EMAC_RXMBP_PROMCH_SHIFT         (16)
149 #define EMAC_RXMBP_PROMCH_MASK          (0x7 << 16)
150 #define EMAC_RXMBP_BROADEN_MASK         BIT(13)
151 #define EMAC_RXMBP_BROADCH_SHIFT        (8)
152 #define EMAC_RXMBP_BROADCH_MASK         (0x7 << 8)
153 #define EMAC_RXMBP_MULTIEN_MASK         BIT(5)
154 #define EMAC_RXMBP_MULTICH_SHIFT        (0)
155 #define EMAC_RXMBP_MULTICH_MASK         (0x7)
156 #define EMAC_RXMBP_CHMASK               (0x7)
157
158 /* EMAC register definitions/bit maps used */
159 # define EMAC_MBP_RXPROMISC             (0x00200000)
160 # define EMAC_MBP_PROMISCCH(ch)         (((ch) & 0x7) << 16)
161 # define EMAC_MBP_RXBCAST               (0x00002000)
162 # define EMAC_MBP_BCASTCHAN(ch)         (((ch) & 0x7) << 8)
163 # define EMAC_MBP_RXMCAST               (0x00000020)
164 # define EMAC_MBP_MCASTCHAN(ch)         ((ch) & 0x7)
165
166 /* EMAC mac_control register */
167 #define EMAC_MACCONTROL_TXPTYPE         BIT(9)
168 #define EMAC_MACCONTROL_TXPACEEN        BIT(6)
169 #define EMAC_MACCONTROL_GMIIEN          BIT(5)
170 #define EMAC_MACCONTROL_GIGABITEN       BIT(7)
171 #define EMAC_MACCONTROL_FULLDUPLEXEN    BIT(0)
172 #define EMAC_MACCONTROL_RMIISPEED_MASK  BIT(15)
173
174 /* GIGABIT MODE related bits */
175 #define EMAC_DM646X_MACCONTORL_GIG      BIT(7)
176 #define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
177
178 /* EMAC mac_status register */
179 #define EMAC_MACSTATUS_TXERRCODE_MASK   (0xF00000)
180 #define EMAC_MACSTATUS_TXERRCODE_SHIFT  (20)
181 #define EMAC_MACSTATUS_TXERRCH_MASK     (0x7)
182 #define EMAC_MACSTATUS_TXERRCH_SHIFT    (16)
183 #define EMAC_MACSTATUS_RXERRCODE_MASK   (0xF000)
184 #define EMAC_MACSTATUS_RXERRCODE_SHIFT  (12)
185 #define EMAC_MACSTATUS_RXERRCH_MASK     (0x7)
186 #define EMAC_MACSTATUS_RXERRCH_SHIFT    (8)
187
188 /* EMAC RX register masks */
189 #define EMAC_RX_MAX_LEN_MASK            (0xFFFF)
190 #define EMAC_RX_BUFFER_OFFSET_MASK      (0xFFFF)
191
192 /* MAC_IN_VECTOR (0x180) register bit fields */
193 #define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT      BIT(17)
194 #define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT  BIT(16)
195 #define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC    BIT(8)
196 #define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC    BIT(0)
197
198 /** NOTE:: For DM646x the IN_VECTOR has changed */
199 #define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC    BIT(EMAC_DEF_RX_CH)
200 #define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC    BIT(16 + EMAC_DEF_TX_CH)
201 #define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT      BIT(26)
202 #define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT  BIT(27)
203
204 /* CPPI bit positions */
205 #define EMAC_CPPI_SOP_BIT               BIT(31)
206 #define EMAC_CPPI_EOP_BIT               BIT(30)
207 #define EMAC_CPPI_OWNERSHIP_BIT         BIT(29)
208 #define EMAC_CPPI_EOQ_BIT               BIT(28)
209 #define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
210 #define EMAC_CPPI_PASS_CRC_BIT          BIT(26)
211 #define EMAC_RX_BD_BUF_SIZE             (0xFFFF)
212 #define EMAC_BD_LENGTH_FOR_CACHE        (16) /* only CPPI bytes */
213 #define EMAC_RX_BD_PKT_LENGTH_MASK      (0xFFFF)
214
215 /* Max hardware defines */
216 #define EMAC_MAX_TXRX_CHANNELS           (8)  /* Max hardware channels */
217 #define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
218
219 /* EMAC Peripheral Device Register Memory Layout structure */
220 #define EMAC_TXIDVER            0x0
221 #define EMAC_TXCONTROL          0x4
222 #define EMAC_TXTEARDOWN         0x8
223 #define EMAC_RXIDVER            0x10
224 #define EMAC_RXCONTROL          0x14
225 #define EMAC_RXTEARDOWN         0x18
226 #define EMAC_TXINTSTATRAW       0x80
227 #define EMAC_TXINTSTATMASKED    0x84
228 #define EMAC_TXINTMASKSET       0x88
229 #define EMAC_TXINTMASKCLEAR     0x8C
230 #define EMAC_MACINVECTOR        0x90
231
232 #define EMAC_DM646X_MACEOIVECTOR        0x94
233
234 #define EMAC_RXINTSTATRAW       0xA0
235 #define EMAC_RXINTSTATMASKED    0xA4
236 #define EMAC_RXINTMASKSET       0xA8
237 #define EMAC_RXINTMASKCLEAR     0xAC
238 #define EMAC_MACINTSTATRAW      0xB0
239 #define EMAC_MACINTSTATMASKED   0xB4
240 #define EMAC_MACINTMASKSET      0xB8
241 #define EMAC_MACINTMASKCLEAR    0xBC
242
243 #define EMAC_RXMBPENABLE        0x100
244 #define EMAC_RXUNICASTSET       0x104
245 #define EMAC_RXUNICASTCLEAR     0x108
246 #define EMAC_RXMAXLEN           0x10C
247 #define EMAC_RXBUFFEROFFSET     0x110
248 #define EMAC_RXFILTERLOWTHRESH  0x114
249
250 #define EMAC_MACCONTROL         0x160
251 #define EMAC_MACSTATUS          0x164
252 #define EMAC_EMCONTROL          0x168
253 #define EMAC_FIFOCONTROL        0x16C
254 #define EMAC_MACCONFIG          0x170
255 #define EMAC_SOFTRESET          0x174
256 #define EMAC_MACSRCADDRLO       0x1D0
257 #define EMAC_MACSRCADDRHI       0x1D4
258 #define EMAC_MACHASH1           0x1D8
259 #define EMAC_MACHASH2           0x1DC
260 #define EMAC_MACADDRLO          0x500
261 #define EMAC_MACADDRHI          0x504
262 #define EMAC_MACINDEX           0x508
263
264 /* EMAC HDP and Completion registors */
265 #define EMAC_TXHDP(ch)          (0x600 + (ch * 4))
266 #define EMAC_RXHDP(ch)          (0x620 + (ch * 4))
267 #define EMAC_TXCP(ch)           (0x640 + (ch * 4))
268 #define EMAC_RXCP(ch)           (0x660 + (ch * 4))
269
270 /* EMAC statistics registers */
271 #define EMAC_RXGOODFRAMES       0x200
272 #define EMAC_RXBCASTFRAMES      0x204
273 #define EMAC_RXMCASTFRAMES      0x208
274 #define EMAC_RXPAUSEFRAMES      0x20C
275 #define EMAC_RXCRCERRORS        0x210
276 #define EMAC_RXALIGNCODEERRORS  0x214
277 #define EMAC_RXOVERSIZED        0x218
278 #define EMAC_RXJABBER           0x21C
279 #define EMAC_RXUNDERSIZED       0x220
280 #define EMAC_RXFRAGMENTS        0x224
281 #define EMAC_RXFILTERED         0x228
282 #define EMAC_RXQOSFILTERED      0x22C
283 #define EMAC_RXOCTETS           0x230
284 #define EMAC_TXGOODFRAMES       0x234
285 #define EMAC_TXBCASTFRAMES      0x238
286 #define EMAC_TXMCASTFRAMES      0x23C
287 #define EMAC_TXPAUSEFRAMES      0x240
288 #define EMAC_TXDEFERRED         0x244
289 #define EMAC_TXCOLLISION        0x248
290 #define EMAC_TXSINGLECOLL       0x24C
291 #define EMAC_TXMULTICOLL        0x250
292 #define EMAC_TXEXCESSIVECOLL    0x254
293 #define EMAC_TXLATECOLL         0x258
294 #define EMAC_TXUNDERRUN         0x25C
295 #define EMAC_TXCARRIERSENSE     0x260
296 #define EMAC_TXOCTETS           0x264
297 #define EMAC_NETOCTETS          0x280
298 #define EMAC_RXSOFOVERRUNS      0x284
299 #define EMAC_RXMOFOVERRUNS      0x288
300 #define EMAC_RXDMAOVERRUNS      0x28C
301
302 /* EMAC DM644x control registers */
303 #define EMAC_CTRL_EWCTL         (0x4)
304 #define EMAC_CTRL_EWINTTCNT     (0x8)
305
306 /* EMAC MDIO related */
307 /* Mask & Control defines */
308 #define MDIO_CONTROL_CLKDIV     (0xFF)
309 #define MDIO_CONTROL_ENABLE     BIT(30)
310 #define MDIO_USERACCESS_GO      BIT(31)
311 #define MDIO_USERACCESS_WRITE   BIT(30)
312 #define MDIO_USERACCESS_READ    (0)
313 #define MDIO_USERACCESS_REGADR  (0x1F << 21)
314 #define MDIO_USERACCESS_PHYADR  (0x1F << 16)
315 #define MDIO_USERACCESS_DATA    (0xFFFF)
316 #define MDIO_USERPHYSEL_LINKSEL BIT(7)
317 #define MDIO_VER_MODID          (0xFFFF << 16)
318 #define MDIO_VER_REVMAJ         (0xFF   << 8)
319 #define MDIO_VER_REVMIN         (0xFF)
320
321 #define MDIO_USERACCESS(inst)   (0x80 + (inst * 8))
322 #define MDIO_USERPHYSEL(inst)   (0x84 + (inst * 8))
323 #define MDIO_CONTROL            (0x04)
324
325 /* EMAC DM646X control module registers */
326 #define EMAC_DM646X_CMRXINTEN   (0x14)
327 #define EMAC_DM646X_CMTXINTEN   (0x18)
328
329 /* EMAC EOI codes for C0 */
330 #define EMAC_DM646X_MAC_EOI_C0_RXEN     (0x01)
331 #define EMAC_DM646X_MAC_EOI_C0_TXEN     (0x02)
332
333 /** net_buf_obj: EMAC network bufferdata structure
334  *
335  * EMAC network buffer data structure
336  */
337 struct emac_netbufobj {
338         void *buf_token;
339         char *data_ptr;
340         int length;
341 };
342
343 /** net_pkt_obj: EMAC network packet data structure
344  *
345  * EMAC network packet data structure - supports buffer list (for future)
346  */
347 struct emac_netpktobj {
348         void *pkt_token; /* data token may hold tx/rx chan id */
349         struct emac_netbufobj *buf_list; /* array of network buffer objects */
350         int num_bufs;
351         int pkt_length;
352 };
353
354 /** emac_tx_bd: EMAC TX Buffer descriptor data structure
355  *
356  * EMAC TX Buffer descriptor data structure
357  */
358 struct emac_tx_bd {
359         int h_next;
360         int buff_ptr;
361         int off_b_len;
362         int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
363         struct emac_tx_bd __iomem *next;
364         void *buf_token;
365 };
366
367 /** emac_txch: EMAC TX Channel data structure
368  *
369  * EMAC TX Channel data structure
370  */
371 struct emac_txch {
372         /* Config related */
373         u32 num_bd;
374         u32 service_max;
375
376         /* CPPI specific */
377         u32 alloc_size;
378         void __iomem *bd_mem;
379         struct emac_tx_bd __iomem *bd_pool_head;
380         struct emac_tx_bd __iomem *active_queue_head;
381         struct emac_tx_bd __iomem *active_queue_tail;
382         struct emac_tx_bd __iomem *last_hw_bdprocessed;
383         u32 queue_active;
384         u32 teardown_pending;
385         u32 *tx_complete;
386
387         /** statistics */
388         u32 proc_count;     /* TX: # of times emac_tx_bdproc is called */
389         u32 mis_queued_packets;
390         u32 queue_reinit;
391         u32 end_of_queue_add;
392         u32 out_of_tx_bd;
393         u32 no_active_pkts; /* IRQ when there were no packets to process */
394         u32 active_queue_count;
395 };
396
397 /** emac_rx_bd: EMAC RX Buffer descriptor data structure
398  *
399  * EMAC RX Buffer descriptor data structure
400  */
401 struct emac_rx_bd {
402         int h_next;
403         int buff_ptr;
404         int off_b_len;
405         int mode;
406         struct emac_rx_bd __iomem *next;
407         void *data_ptr;
408         void *buf_token;
409 };
410
411 /** emac_rxch: EMAC RX Channel data structure
412  *
413  * EMAC RX Channel data structure
414  */
415 struct emac_rxch {
416         /* configuration info */
417         u32 num_bd;
418         u32 service_max;
419         u32 buf_size;
420         char mac_addr[6];
421
422         /** CPPI specific */
423         u32 alloc_size;
424         void __iomem *bd_mem;
425         struct emac_rx_bd __iomem *bd_pool_head;
426         struct emac_rx_bd __iomem *active_queue_head;
427         struct emac_rx_bd __iomem *active_queue_tail;
428         u32 queue_active;
429         u32 teardown_pending;
430
431         /* packet and buffer objects */
432         struct emac_netpktobj pkt_queue;
433         struct emac_netbufobj buf_queue;
434
435         /** statistics */
436         u32 proc_count; /* number of times emac_rx_bdproc is called */
437         u32 processed_bd;
438         u32 recycled_bd;
439         u32 out_of_rx_bd;
440         u32 out_of_rx_buffers;
441         u32 queue_reinit;
442         u32 end_of_queue_add;
443         u32 end_of_queue;
444         u32 mis_queued_packets;
445 };
446
447 /* emac_priv: EMAC private data structure
448  *
449  * EMAC adapter private data structure
450  */
451 struct emac_priv {
452         u32 msg_enable;
453         struct net_device *ndev;
454         struct platform_device *pdev;
455         struct napi_struct napi;
456         char mac_addr[6];
457         spinlock_t tx_lock;
458         spinlock_t rx_lock;
459         void __iomem *remap_addr;
460         u32 emac_base_phys;
461         void __iomem *emac_base;
462         void __iomem *ctrl_base;
463         void __iomem *emac_ctrl_ram;
464         u32 ctrl_ram_size;
465         struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
466         struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
467         u32 link; /* 1=link on, 0=link off */
468         u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
469         u32 duplex; /* Link duplex: 0=Half, 1=Full */
470         u32 rx_buf_size;
471         u32 isr_count;
472         u8 rmii_en;
473         u8 version;
474         struct net_device_stats net_dev_stats;
475         u32 mac_hash1;
476         u32 mac_hash2;
477         u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
478         u32 rx_addr_type;
479         /* periodic timer required for MDIO polling */
480         struct timer_list periodic_timer;
481         u32 periodic_ticks;
482         u32 timer_active;
483         u32 phy_mask;
484         /* mii_bus,phy members */
485         struct mii_bus *mii_bus;
486         struct phy_device *phydev;
487         spinlock_t lock;
488 };
489
490 /* clock frequency for EMAC */
491 static struct clk *emac_clk;
492 static unsigned long emac_bus_frequency;
493 static unsigned long mdio_max_freq;
494
495 /* EMAC internal utility function */
496 static inline u32 emac_virt_to_phys(void __iomem *addr)
497 {
498         return (u32 __force) io_v2p(addr);
499 }
500
501 /* Cache macros - Packet buffers would be from skb pool which is cached */
502 #define EMAC_VIRT_NOCACHE(addr) (addr)
503 #define EMAC_CACHE_INVALIDATE(addr, size) \
504         dma_cache_maint((void *)addr, size, DMA_FROM_DEVICE)
505 #define EMAC_CACHE_WRITEBACK(addr, size) \
506         dma_cache_maint((void *)addr, size, DMA_TO_DEVICE)
507 #define EMAC_CACHE_WRITEBACK_INVALIDATE(addr, size) \
508         dma_cache_maint((void *)addr, size, DMA_BIDIRECTIONAL)
509
510 /* DM644x does not have BD's in cached memory - so no cache functions */
511 #define BD_CACHE_INVALIDATE(addr, size)
512 #define BD_CACHE_WRITEBACK(addr, size)
513 #define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
514
515 /* EMAC TX Host Error description strings */
516 static char *emac_txhost_errcodes[16] = {
517         "No error", "SOP error", "Ownership bit not set in SOP buffer",
518         "Zero Next Buffer Descriptor Pointer Without EOP",
519         "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
520         "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
521         "Reserved", "Reserved", "Reserved", "Reserved"
522 };
523
524 /* EMAC RX Host Error description strings */
525 static char *emac_rxhost_errcodes[16] = {
526         "No error", "Reserved", "Ownership bit not set in input buffer",
527         "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
528         "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
529         "Reserved", "Reserved", "Reserved", "Reserved"
530 };
531
532 /* Helper macros */
533 #define emac_read(reg)            ioread32(priv->emac_base + (reg))
534 #define emac_write(reg, val)      iowrite32(val, priv->emac_base + (reg))
535
536 #define emac_ctrl_read(reg)       ioread32((priv->ctrl_base + (reg)))
537 #define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
538
539 #define emac_mdio_read(reg)       ioread32(bus->priv + (reg))
540 #define emac_mdio_write(reg, val) iowrite32(val, (bus->priv + (reg)))
541
542 /**
543  * emac_dump_regs: Dump important EMAC registers to debug terminal
544  * @priv: The DaVinci EMAC private adapter structure
545  *
546  * Executes ethtool set cmd & sets phy mode
547  *
548  */
549 static void emac_dump_regs(struct emac_priv *priv)
550 {
551         struct device *emac_dev = &priv->ndev->dev;
552
553         /* Print important registers in EMAC */
554         dev_info(emac_dev, "EMAC Basic registers\n");
555         dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
556                 emac_ctrl_read(EMAC_CTRL_EWCTL),
557                 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
558         dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
559                 emac_read(EMAC_TXIDVER),
560                 ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
561                 emac_read(EMAC_RXIDVER),
562                 ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
563         dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
564                 "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
565                 emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
566         dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
567                 "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
568                 emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
569         dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
570                 "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
571                 emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
572         dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
573                 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
574         dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
575                 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
576                 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
577         dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
578                 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
579                 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
580         dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
581                 emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
582         dev_info(emac_dev, "EMAC Statistics\n");
583         dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
584                 emac_read(EMAC_RXGOODFRAMES));
585         dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
586                 emac_read(EMAC_RXBCASTFRAMES));
587         dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
588                 emac_read(EMAC_RXMCASTFRAMES));
589         dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
590                 emac_read(EMAC_RXPAUSEFRAMES));
591         dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
592                 emac_read(EMAC_RXCRCERRORS));
593         dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
594                 emac_read(EMAC_RXALIGNCODEERRORS));
595         dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
596                 emac_read(EMAC_RXOVERSIZED));
597         dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
598                 emac_read(EMAC_RXJABBER));
599         dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
600                 emac_read(EMAC_RXUNDERSIZED));
601         dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
602                 emac_read(EMAC_RXFRAGMENTS));
603         dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
604                 emac_read(EMAC_RXFILTERED));
605         dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
606                 emac_read(EMAC_RXQOSFILTERED));
607         dev_info(emac_dev, "EMAC: rx_octets:%d\n",
608                 emac_read(EMAC_RXOCTETS));
609         dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
610                 emac_read(EMAC_TXGOODFRAMES));
611         dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
612                 emac_read(EMAC_TXBCASTFRAMES));
613         dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
614                 emac_read(EMAC_TXMCASTFRAMES));
615         dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
616                 emac_read(EMAC_TXPAUSEFRAMES));
617         dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
618                 emac_read(EMAC_TXDEFERRED));
619         dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
620                 emac_read(EMAC_TXCOLLISION));
621         dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
622                 emac_read(EMAC_TXSINGLECOLL));
623         dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
624                 emac_read(EMAC_TXMULTICOLL));
625         dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
626                 emac_read(EMAC_TXEXCESSIVECOLL));
627         dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
628                 emac_read(EMAC_TXLATECOLL));
629         dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
630                 emac_read(EMAC_TXUNDERRUN));
631         dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
632                 emac_read(EMAC_TXCARRIERSENSE));
633         dev_info(emac_dev, "EMAC: tx_octets:%d\n",
634                 emac_read(EMAC_TXOCTETS));
635         dev_info(emac_dev, "EMAC: net_octets:%d\n",
636                 emac_read(EMAC_NETOCTETS));
637         dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
638                 emac_read(EMAC_RXSOFOVERRUNS));
639         dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
640                 emac_read(EMAC_RXMOFOVERRUNS));
641         dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
642                 emac_read(EMAC_RXDMAOVERRUNS));
643 }
644
645 /*************************************************************************
646  *  EMAC MDIO/Phy Functionality
647  *************************************************************************/
648 /**
649  * emac_get_drvinfo: Get EMAC driver information
650  * @ndev: The DaVinci EMAC network adapter
651  * @info: ethtool info structure containing name and version
652  *
653  * Returns EMAC driver information (name and version)
654  *
655  */
656 static void emac_get_drvinfo(struct net_device *ndev,
657                              struct ethtool_drvinfo *info)
658 {
659         strcpy(info->driver, emac_version_string);
660         strcpy(info->version, EMAC_MODULE_VERSION);
661 }
662
663 /**
664  * emac_get_settings: Get EMAC settings
665  * @ndev: The DaVinci EMAC network adapter
666  * @ecmd: ethtool command
667  *
668  * Executes ethool get command
669  *
670  */
671 static int emac_get_settings(struct net_device *ndev,
672                              struct ethtool_cmd *ecmd)
673 {
674         struct emac_priv *priv = netdev_priv(ndev);
675         if (priv->phy_mask)
676                 return phy_ethtool_gset(priv->phydev, ecmd);
677         else
678                 return -EOPNOTSUPP;
679
680 }
681
682 /**
683  * emac_set_settings: Set EMAC settings
684  * @ndev: The DaVinci EMAC network adapter
685  * @ecmd: ethtool command
686  *
687  * Executes ethool set command
688  *
689  */
690 static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
691 {
692         struct emac_priv *priv = netdev_priv(ndev);
693         if (priv->phy_mask)
694                 return phy_ethtool_sset(priv->phydev, ecmd);
695         else
696                 return -EOPNOTSUPP;
697
698 }
699
700 /**
701  * ethtool_ops: DaVinci EMAC Ethtool structure
702  *
703  * Ethtool support for EMAC adapter
704  *
705  */
706 static const struct ethtool_ops ethtool_ops = {
707         .get_drvinfo = emac_get_drvinfo,
708         .get_settings = emac_get_settings,
709         .set_settings = emac_set_settings,
710         .get_link = ethtool_op_get_link,
711 };
712
713 /**
714  * emac_update_phystatus: Update Phy status
715  * @priv: The DaVinci EMAC private adapter structure
716  *
717  * Updates phy status and takes action for network queue if required
718  * based upon link status
719  *
720  */
721 static void emac_update_phystatus(struct emac_priv *priv)
722 {
723         u32 mac_control;
724         u32 new_duplex;
725         u32 cur_duplex;
726         struct net_device *ndev = priv->ndev;
727
728         mac_control = emac_read(EMAC_MACCONTROL);
729         cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
730                         DUPLEX_FULL : DUPLEX_HALF;
731         if (priv->phy_mask)
732                 new_duplex = priv->phydev->duplex;
733         else
734                 new_duplex = DUPLEX_FULL;
735
736         /* We get called only if link has changed (speed/duplex/status) */
737         if ((priv->link) && (new_duplex != cur_duplex)) {
738                 priv->duplex = new_duplex;
739                 if (DUPLEX_FULL == priv->duplex)
740                         mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
741                 else
742                         mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
743         }
744
745         if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
746                 mac_control = emac_read(EMAC_MACCONTROL);
747                 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
748                                 EMAC_DM646X_MACCONTORL_GIGFORCE);
749         } else {
750                 /* Clear the GIG bit and GIGFORCE bit */
751                 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
752                                         EMAC_DM646X_MACCONTORL_GIG);
753
754                 if (priv->rmii_en && (priv->speed == SPEED_100))
755                         mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
756                 else
757                         mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
758         }
759
760         /* Update mac_control if changed */
761         emac_write(EMAC_MACCONTROL, mac_control);
762
763         if (priv->link) {
764                 /* link ON */
765                 if (!netif_carrier_ok(ndev))
766                         netif_carrier_on(ndev);
767         /* reactivate the transmit queue if it is stopped */
768                 if (netif_running(ndev) && netif_queue_stopped(ndev))
769                         netif_wake_queue(ndev);
770         } else {
771                 /* link OFF */
772                 if (netif_carrier_ok(ndev))
773                         netif_carrier_off(ndev);
774                 if (!netif_queue_stopped(ndev))
775                         netif_stop_queue(ndev);
776         }
777 }
778
779 /**
780  * hash_get: Calculate hash value from mac address
781  * @addr: mac address to delete from hash table
782  *
783  * Calculates hash value from mac address
784  *
785  */
786 static u32 hash_get(u8 *addr)
787 {
788         u32 hash;
789         u8 tmpval;
790         int cnt;
791         hash = 0;
792
793         for (cnt = 0; cnt < 2; cnt++) {
794                 tmpval = *addr++;
795                 hash ^= (tmpval >> 2) ^ (tmpval << 4);
796                 tmpval = *addr++;
797                 hash ^= (tmpval >> 4) ^ (tmpval << 2);
798                 tmpval = *addr++;
799                 hash ^= (tmpval >> 6) ^ (tmpval);
800         }
801
802         return hash & 0x3F;
803 }
804
805 /**
806  * hash_add: Hash function to add mac addr from hash table
807  * @priv: The DaVinci EMAC private adapter structure
808  * mac_addr: mac address to delete from hash table
809  *
810  * Adds mac address to the internal hash table
811  *
812  */
813 static int hash_add(struct emac_priv *priv, u8 *mac_addr)
814 {
815         struct device *emac_dev = &priv->ndev->dev;
816         u32 rc = 0;
817         u32 hash_bit;
818         u32 hash_value = hash_get(mac_addr);
819
820         if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
821                 if (netif_msg_drv(priv)) {
822                         dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
823                                 "Hash %08x, should not be greater than %08x",
824                                 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
825                 }
826                 return -1;
827         }
828
829         /* set the hash bit only if not previously set */
830         if (priv->multicast_hash_cnt[hash_value] == 0) {
831                 rc = 1; /* hash value changed */
832                 if (hash_value < 32) {
833                         hash_bit = BIT(hash_value);
834                         priv->mac_hash1 |= hash_bit;
835                 } else {
836                         hash_bit = BIT((hash_value - 32));
837                         priv->mac_hash2 |= hash_bit;
838                 }
839         }
840
841         /* incr counter for num of mcast addr's mapped to "this" hash bit */
842         ++priv->multicast_hash_cnt[hash_value];
843
844         return rc;
845 }
846
847 /**
848  * hash_del: Hash function to delete mac addr from hash table
849  * @priv: The DaVinci EMAC private adapter structure
850  * mac_addr: mac address to delete from hash table
851  *
852  * Removes mac address from the internal hash table
853  *
854  */
855 static int hash_del(struct emac_priv *priv, u8 *mac_addr)
856 {
857         u32 hash_value;
858         u32 hash_bit;
859
860         hash_value = hash_get(mac_addr);
861         if (priv->multicast_hash_cnt[hash_value] > 0) {
862                 /* dec cntr for num of mcast addr's mapped to this hash bit */
863                 --priv->multicast_hash_cnt[hash_value];
864         }
865
866         /* if counter still > 0, at least one multicast address refers
867          * to this hash bit. so return 0 */
868         if (priv->multicast_hash_cnt[hash_value] > 0)
869                 return 0;
870
871         if (hash_value < 32) {
872                 hash_bit = BIT(hash_value);
873                 priv->mac_hash1 &= ~hash_bit;
874         } else {
875                 hash_bit = BIT((hash_value - 32));
876                 priv->mac_hash2 &= ~hash_bit;
877         }
878
879         /* return 1 to indicate change in mac_hash registers reqd */
880         return 1;
881 }
882
883 /* EMAC multicast operation */
884 #define EMAC_MULTICAST_ADD      0
885 #define EMAC_MULTICAST_DEL      1
886 #define EMAC_ALL_MULTI_SET      2
887 #define EMAC_ALL_MULTI_CLR      3
888
889 /**
890  * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
891  * @priv: The DaVinci EMAC private adapter structure
892  * @action: multicast operation to perform
893  * mac_addr: mac address to set
894  *
895  * Set multicast addresses in EMAC adapter - internal function
896  *
897  */
898 static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
899 {
900         struct device *emac_dev = &priv->ndev->dev;
901         int update = -1;
902
903         switch (action) {
904         case EMAC_MULTICAST_ADD:
905                 update = hash_add(priv, mac_addr);
906                 break;
907         case EMAC_MULTICAST_DEL:
908                 update = hash_del(priv, mac_addr);
909                 break;
910         case EMAC_ALL_MULTI_SET:
911                 update = 1;
912                 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
913                 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
914                 break;
915         case EMAC_ALL_MULTI_CLR:
916                 update = 1;
917                 priv->mac_hash1 = 0;
918                 priv->mac_hash2 = 0;
919                 memset(&(priv->multicast_hash_cnt[0]), 0,
920                 sizeof(priv->multicast_hash_cnt[0]) *
921                        EMAC_NUM_MULTICAST_BITS);
922                 break;
923         default:
924                 if (netif_msg_drv(priv))
925                         dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
926                                 ": bad operation %d", action);
927                 break;
928         }
929
930         /* write to the hardware only if the register status chances */
931         if (update > 0) {
932                 emac_write(EMAC_MACHASH1, priv->mac_hash1);
933                 emac_write(EMAC_MACHASH2, priv->mac_hash2);
934         }
935 }
936
937 /**
938  * emac_dev_mcast_set: Set multicast address in the EMAC adapter
939  * @ndev: The DaVinci EMAC network adapter
940  *
941  * Set multicast addresses in EMAC adapter
942  *
943  */
944 static void emac_dev_mcast_set(struct net_device *ndev)
945 {
946         u32 mbp_enable;
947         struct emac_priv *priv = netdev_priv(ndev);
948
949         mbp_enable = emac_read(EMAC_RXMBPENABLE);
950         if (ndev->flags & IFF_PROMISC) {
951                 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
952                 mbp_enable |= (EMAC_MBP_RXPROMISC);
953         } else {
954                 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
955                 if ((ndev->flags & IFF_ALLMULTI) ||
956                     (ndev->mc_count > EMAC_DEF_MAX_MULTICAST_ADDRESSES)) {
957                         mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
958                         emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
959                 }
960                 if (ndev->mc_count > 0) {
961                         struct dev_mc_list *mc_ptr;
962                         mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
963                         emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
964                         /* program multicast address list into EMAC hardware */
965                         for (mc_ptr = ndev->mc_list; mc_ptr;
966                              mc_ptr = mc_ptr->next) {
967                                 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
968                                                (u8 *)mc_ptr->dmi_addr);
969                         }
970                 } else {
971                         mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
972                         emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
973                 }
974         }
975         /* Set mbp config register */
976         emac_write(EMAC_RXMBPENABLE, mbp_enable);
977 }
978
979 /*************************************************************************
980  *  EMAC Hardware manipulation
981  *************************************************************************/
982
983 /**
984  * emac_int_disable: Disable EMAC module interrupt (from adapter)
985  * @priv: The DaVinci EMAC private adapter structure
986  *
987  * Disable EMAC interrupt on the adapter
988  *
989  */
990 static void emac_int_disable(struct emac_priv *priv)
991 {
992         if (priv->version == EMAC_VERSION_2) {
993                 unsigned long flags;
994
995                 local_irq_save(flags);
996
997                 /* Program C0_Int_En to zero to turn off
998                 * interrupts to the CPU */
999                 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1000                 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1001                 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
1002
1003                 local_irq_restore(flags);
1004
1005         } else {
1006                 /* Set DM644x control registers for interrupt control */
1007                 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1008         }
1009 }
1010
1011 /**
1012  * emac_int_enable: Enable EMAC module interrupt (from adapter)
1013  * @priv: The DaVinci EMAC private adapter structure
1014  *
1015  * Enable EMAC interrupt on the adapter
1016  *
1017  */
1018 static void emac_int_enable(struct emac_priv *priv)
1019 {
1020         if (priv->version == EMAC_VERSION_2) {
1021                 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
1022                 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
1023
1024                 /* In addition to turning on interrupt Enable, we need
1025                  * ack by writing appropriate values to the EOI
1026                  * register */
1027
1028                 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
1029
1030                 /* ack rxen only then a new pulse will be generated */
1031                 emac_write(EMAC_DM646X_MACEOIVECTOR,
1032                         EMAC_DM646X_MAC_EOI_C0_RXEN);
1033
1034                 /* ack txen- only then a new pulse will be generated */
1035                 emac_write(EMAC_DM646X_MACEOIVECTOR,
1036                         EMAC_DM646X_MAC_EOI_C0_TXEN);
1037
1038         } else {
1039                 /* Set DM644x control registers for interrupt control */
1040                 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1041         }
1042 }
1043
1044 /**
1045  * emac_irq: EMAC interrupt handler
1046  * @irq: interrupt number
1047  * @dev_id: EMAC network adapter data structure ptr
1048  *
1049  * EMAC Interrupt handler - we only schedule NAPI and not process any packets
1050  * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
1051  *
1052  * Returns interrupt handled condition
1053  */
1054 static irqreturn_t emac_irq(int irq, void *dev_id)
1055 {
1056         struct net_device *ndev = (struct net_device *)dev_id;
1057         struct emac_priv *priv = netdev_priv(ndev);
1058
1059         ++priv->isr_count;
1060         if (likely(netif_running(priv->ndev))) {
1061                 emac_int_disable(priv);
1062                 napi_schedule(&priv->napi);
1063         } else {
1064                 /* we are closing down, so dont process anything */
1065         }
1066         return IRQ_HANDLED;
1067 }
1068
1069 /** EMAC on-chip buffer descriptor memory
1070  *
1071  * WARNING: Please note that the on chip memory is used for both TX and RX
1072  * buffer descriptor queues and is equally divided between TX and RX desc's
1073  * If the number of TX or RX descriptors change this memory pointers need
1074  * to be adjusted. If external memory is allocated then these pointers can
1075  * pointer to the memory
1076  *
1077  */
1078 #define EMAC_TX_BD_MEM(priv)    ((priv)->emac_ctrl_ram)
1079 #define EMAC_RX_BD_MEM(priv)    ((priv)->emac_ctrl_ram + \
1080                                 (((priv)->ctrl_ram_size) >> 1))
1081
1082 /**
1083  * emac_init_txch: TX channel initialization
1084  * @priv: The DaVinci EMAC private adapter structure
1085  * @ch: RX channel number
1086  *
1087  * Called during device init to setup a TX channel (allocate buffer desc
1088  * create free pool and keep ready for transmission
1089  *
1090  * Returns success(0) or mem alloc failures error code
1091  */
1092 static int emac_init_txch(struct emac_priv *priv, u32 ch)
1093 {
1094         struct device *emac_dev = &priv->ndev->dev;
1095         u32 cnt, bd_size;
1096         void __iomem *mem;
1097         struct emac_tx_bd __iomem *curr_bd;
1098         struct emac_txch *txch = NULL;
1099
1100         txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
1101         if (NULL == txch) {
1102                 dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
1103                 return -ENOMEM;
1104         }
1105         priv->txch[ch] = txch;
1106         txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
1107         txch->active_queue_head = NULL;
1108         txch->active_queue_tail = NULL;
1109         txch->queue_active = 0;
1110         txch->teardown_pending = 0;
1111
1112         /* allocate memory for TX CPPI channel on a 4 byte boundry */
1113         txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
1114                                     GFP_KERNEL);
1115         if (NULL == txch->tx_complete) {
1116                 dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
1117                 kfree(txch);
1118                 return -ENOMEM;
1119         }
1120
1121         /* allocate buffer descriptor pool align every BD on four word
1122          * boundry for future requirements */
1123         bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
1124         txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1125         txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
1126
1127         /* alloc TX BD memory */
1128         txch->bd_mem = EMAC_TX_BD_MEM(priv);
1129         __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1130
1131         /* initialize the BD linked list */
1132         mem = (void __force __iomem *)
1133                         (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
1134         txch->bd_pool_head = NULL;
1135         for (cnt = 0; cnt < txch->num_bd; cnt++) {
1136                 curr_bd = mem + (cnt * bd_size);
1137                 curr_bd->next = txch->bd_pool_head;
1138                 txch->bd_pool_head = curr_bd;
1139         }
1140
1141         /* reset statistics counters */
1142         txch->out_of_tx_bd = 0;
1143         txch->no_active_pkts = 0;
1144         txch->active_queue_count = 0;
1145
1146         return 0;
1147 }
1148
1149 /**
1150  * emac_cleanup_txch: Book-keep function to clean TX channel resources
1151  * @priv: The DaVinci EMAC private adapter structure
1152  * @ch: TX channel number
1153  *
1154  * Called to clean up TX channel resources
1155  *
1156  */
1157 static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
1158 {
1159         struct emac_txch *txch = priv->txch[ch];
1160
1161         if (txch) {
1162                 if (txch->bd_mem)
1163                         txch->bd_mem = NULL;
1164                 kfree(txch->tx_complete);
1165                 kfree(txch);
1166                 priv->txch[ch] = NULL;
1167         }
1168 }
1169
1170 /**
1171  * emac_net_tx_complete: TX packet completion function
1172  * @priv: The DaVinci EMAC private adapter structure
1173  * @net_data_tokens: packet token - skb pointer
1174  * @num_tokens: number of skb's to free
1175  * @ch: TX channel number
1176  *
1177  * Frees the skb once packet is transmitted
1178  *
1179  */
1180 static int emac_net_tx_complete(struct emac_priv *priv,
1181                                 void **net_data_tokens,
1182                                 int num_tokens, u32 ch)
1183 {
1184         u32 cnt;
1185
1186         if (unlikely(num_tokens && netif_queue_stopped(priv->ndev)))
1187                 netif_start_queue(priv->ndev);
1188         for (cnt = 0; cnt < num_tokens; cnt++) {
1189                 struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
1190                 if (skb == NULL)
1191                         continue;
1192                 priv->net_dev_stats.tx_packets++;
1193                 priv->net_dev_stats.tx_bytes += skb->len;
1194                 dev_kfree_skb_any(skb);
1195         }
1196         return 0;
1197 }
1198
1199 /**
1200  * emac_txch_teardown: TX channel teardown
1201  * @priv: The DaVinci EMAC private adapter structure
1202  * @ch: TX channel number
1203  *
1204  * Called to teardown TX channel
1205  *
1206  */
1207 static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
1208 {
1209         struct device *emac_dev = &priv->ndev->dev;
1210         u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1211         struct emac_txch *txch = priv->txch[ch];
1212         struct emac_tx_bd __iomem *curr_bd;
1213
1214         while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1215                EMAC_TEARDOWN_VALUE) {
1216                 /* wait till tx teardown complete */
1217                 cpu_relax(); /* TODO: check if this helps ... */
1218                 --teardown_cnt;
1219                 if (0 == teardown_cnt) {
1220                         dev_err(emac_dev, "EMAC: TX teardown aborted\n");
1221                         break;
1222                 }
1223         }
1224         emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
1225
1226         /* process sent packets and return skb's to upper layer */
1227         if (1 == txch->queue_active) {
1228                 curr_bd = txch->active_queue_head;
1229                 while (curr_bd != NULL) {
1230                         emac_net_tx_complete(priv, (void __force *)
1231                                         &curr_bd->buf_token, 1, ch);
1232                         if (curr_bd != txch->active_queue_tail)
1233                                 curr_bd = curr_bd->next;
1234                         else
1235                                 break;
1236                 }
1237                 txch->bd_pool_head = txch->active_queue_head;
1238                 txch->active_queue_head =
1239                 txch->active_queue_tail = NULL;
1240         }
1241 }
1242
1243 /**
1244  * emac_stop_txch: Stop TX channel operation
1245  * @priv: The DaVinci EMAC private adapter structure
1246  * @ch: TX channel number
1247  *
1248  * Called to stop TX channel operation
1249  *
1250  */
1251 static void emac_stop_txch(struct emac_priv *priv, u32 ch)
1252 {
1253         struct emac_txch *txch = priv->txch[ch];
1254
1255         if (txch) {
1256                 txch->teardown_pending = 1;
1257                 emac_write(EMAC_TXTEARDOWN, 0);
1258                 emac_txch_teardown(priv, ch);
1259                 txch->teardown_pending = 0;
1260                 emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
1261         }
1262 }
1263
1264 /**
1265  * emac_tx_bdproc: TX buffer descriptor (packet) processing
1266  * @priv: The DaVinci EMAC private adapter structure
1267  * @ch: TX channel number to process buffer descriptors for
1268  * @budget: number of packets allowed to process
1269  * @pending: indication to caller that packets are pending to process
1270  *
1271  * Processes TX buffer descriptors after packets are transmitted - checks
1272  * ownership bit on the TX * descriptor and requeues it to free pool & frees
1273  * the SKB buffer. Only "budget" number of packets are processed and
1274  * indication of pending packets provided to the caller
1275  *
1276  * Returns number of packets processed
1277  */
1278 static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1279 {
1280         struct device *emac_dev = &priv->ndev->dev;
1281         unsigned long flags;
1282         u32 frame_status;
1283         u32 pkts_processed = 0;
1284         u32 tx_complete_cnt = 0;
1285         struct emac_tx_bd __iomem *curr_bd;
1286         struct emac_txch *txch = priv->txch[ch];
1287         u32 *tx_complete_ptr = txch->tx_complete;
1288
1289         if (unlikely(1 == txch->teardown_pending)) {
1290                 if (netif_msg_tx_err(priv) && net_ratelimit()) {
1291                         dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
1292                                 "teardown pending\n");
1293                 }
1294                 return 0;  /* dont handle any pkt completions */
1295         }
1296
1297         ++txch->proc_count;
1298         spin_lock_irqsave(&priv->tx_lock, flags);
1299         curr_bd = txch->active_queue_head;
1300         if (NULL == curr_bd) {
1301                 emac_write(EMAC_TXCP(ch),
1302                            emac_virt_to_phys(txch->last_hw_bdprocessed));
1303                 txch->no_active_pkts++;
1304                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1305                 return 0;
1306         }
1307         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1308         frame_status = curr_bd->mode;
1309         while ((curr_bd) &&
1310               ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1311               (pkts_processed < budget)) {
1312                 emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd));
1313                 txch->active_queue_head = curr_bd->next;
1314                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1315                         if (curr_bd->next) {    /* misqueued packet */
1316                                 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1317                                 ++txch->mis_queued_packets;
1318                         } else {
1319                                 txch->queue_active = 0; /* end of queue */
1320                         }
1321                 }
1322                 *tx_complete_ptr = (u32) curr_bd->buf_token;
1323                 ++tx_complete_ptr;
1324                 ++tx_complete_cnt;
1325                 curr_bd->next = txch->bd_pool_head;
1326                 txch->bd_pool_head = curr_bd;
1327                 --txch->active_queue_count;
1328                 pkts_processed++;
1329                 txch->last_hw_bdprocessed = curr_bd;
1330                 curr_bd = txch->active_queue_head;
1331                 if (curr_bd) {
1332                         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1333                         frame_status = curr_bd->mode;
1334                 }
1335         } /* end of pkt processing loop */
1336
1337         emac_net_tx_complete(priv,
1338                              (void *)&txch->tx_complete[0],
1339                              tx_complete_cnt, ch);
1340         spin_unlock_irqrestore(&priv->tx_lock, flags);
1341         return pkts_processed;
1342 }
1343
1344 #define EMAC_ERR_TX_OUT_OF_BD -1
1345
1346 /**
1347  * emac_send: EMAC Transmit function (internal)
1348  * @priv: The DaVinci EMAC private adapter structure
1349  * @pkt: packet pointer (contains skb ptr)
1350  * @ch: TX channel number
1351  *
1352  * Called by the transmit function to queue the packet in EMAC hardware queue
1353  *
1354  * Returns success(0) or error code (typically out of desc's)
1355  */
1356 static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
1357 {
1358         unsigned long flags;
1359         struct emac_tx_bd __iomem *curr_bd;
1360         struct emac_txch *txch;
1361         struct emac_netbufobj *buf_list;
1362
1363         txch = priv->txch[ch];
1364         buf_list = pkt->buf_list;   /* get handle to the buffer array */
1365
1366         /* check packet size and pad if short */
1367         if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
1368                 buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
1369                 pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
1370         }
1371
1372         spin_lock_irqsave(&priv->tx_lock, flags);
1373         curr_bd = txch->bd_pool_head;
1374         if (curr_bd == NULL) {
1375                 txch->out_of_tx_bd++;
1376                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1377                 return EMAC_ERR_TX_OUT_OF_BD;
1378         }
1379
1380         txch->bd_pool_head = curr_bd->next;
1381         curr_bd->buf_token = buf_list->buf_token;
1382         /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
1383         curr_bd->buff_ptr = virt_to_phys(buf_list->data_ptr);
1384         curr_bd->off_b_len = buf_list->length;
1385         curr_bd->h_next = 0;
1386         curr_bd->next = NULL;
1387         curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
1388                          EMAC_CPPI_EOP_BIT | pkt->pkt_length);
1389
1390         /* flush the packet from cache if write back cache is present */
1391         BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1392
1393         /* send the packet */
1394         if (txch->active_queue_head == NULL) {
1395                 txch->active_queue_head = curr_bd;
1396                 txch->active_queue_tail = curr_bd;
1397                 if (1 != txch->queue_active) {
1398                         emac_write(EMAC_TXHDP(ch),
1399                                         emac_virt_to_phys(curr_bd));
1400                         txch->queue_active = 1;
1401                 }
1402                 ++txch->queue_reinit;
1403         } else {
1404                 register struct emac_tx_bd __iomem *tail_bd;
1405                 register u32 frame_status;
1406
1407                 tail_bd = txch->active_queue_tail;
1408                 tail_bd->next = curr_bd;
1409                 txch->active_queue_tail = curr_bd;
1410                 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1411                 tail_bd->h_next = (int)emac_virt_to_phys(curr_bd);
1412                 frame_status = tail_bd->mode;
1413                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1414                         emac_write(EMAC_TXHDP(ch), emac_virt_to_phys(curr_bd));
1415                         frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1416                         tail_bd->mode = frame_status;
1417                         ++txch->end_of_queue_add;
1418                 }
1419         }
1420         txch->active_queue_count++;
1421         spin_unlock_irqrestore(&priv->tx_lock, flags);
1422         return 0;
1423 }
1424
1425 /**
1426  * emac_dev_xmit: EMAC Transmit function
1427  * @skb: SKB pointer
1428  * @ndev: The DaVinci EMAC network adapter
1429  *
1430  * Called by the system to transmit a packet  - we queue the packet in
1431  * EMAC hardware transmit queue
1432  *
1433  * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
1434  */
1435 static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1436 {
1437         struct device *emac_dev = &ndev->dev;
1438         int ret_code;
1439         struct emac_netbufobj tx_buf; /* buffer obj-only single frame support */
1440         struct emac_netpktobj tx_packet;  /* packet object */
1441         struct emac_priv *priv = netdev_priv(ndev);
1442
1443         /* If no link, return */
1444         if (unlikely(!priv->link)) {
1445                 if (netif_msg_tx_err(priv) && net_ratelimit())
1446                         dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
1447                 return NETDEV_TX_BUSY;
1448         }
1449
1450         /* Build the buffer and packet objects - Since only single fragment is
1451          * supported, need not set length and token in both packet & object.
1452          * Doing so for completeness sake & to show that this needs to be done
1453          * in multifragment case
1454          */
1455         tx_packet.buf_list = &tx_buf;
1456         tx_packet.num_bufs = 1; /* only single fragment supported */
1457         tx_packet.pkt_length = skb->len;
1458         tx_packet.pkt_token = (void *)skb;
1459         tx_buf.length = skb->len;
1460         tx_buf.buf_token = (void *)skb;
1461         tx_buf.data_ptr = skb->data;
1462         EMAC_CACHE_WRITEBACK((unsigned long)skb->data, skb->len);
1463         ndev->trans_start = jiffies;
1464         ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
1465         if (unlikely(ret_code != 0)) {
1466                 if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
1467                         if (netif_msg_tx_err(priv) && net_ratelimit())
1468                                 dev_err(emac_dev, "DaVinci EMAC: xmit() fatal"\
1469                                         " err. Out of TX BD's");
1470                         netif_stop_queue(priv->ndev);
1471                 }
1472                 priv->net_dev_stats.tx_dropped++;
1473                 return NETDEV_TX_BUSY;
1474         }
1475
1476         return NETDEV_TX_OK;
1477 }
1478
1479 /**
1480  * emac_dev_tx_timeout: EMAC Transmit timeout function
1481  * @ndev: The DaVinci EMAC network adapter
1482  *
1483  * Called when system detects that a skb timeout period has expired
1484  * potentially due to a fault in the adapter in not being able to send
1485  * it out on the wire. We teardown the TX channel assuming a hardware
1486  * error and re-initialize the TX channel for hardware operation
1487  *
1488  */
1489 static void emac_dev_tx_timeout(struct net_device *ndev)
1490 {
1491         struct emac_priv *priv = netdev_priv(ndev);
1492         struct device *emac_dev = &ndev->dev;
1493
1494         if (netif_msg_tx_err(priv))
1495                 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1496
1497         priv->net_dev_stats.tx_errors++;
1498         emac_int_disable(priv);
1499         emac_stop_txch(priv, EMAC_DEF_TX_CH);
1500         emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
1501         emac_init_txch(priv, EMAC_DEF_TX_CH);
1502         emac_write(EMAC_TXHDP(0), 0);
1503         emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
1504         emac_int_enable(priv);
1505 }
1506
1507 /**
1508  * emac_net_alloc_rx_buf: Allocate a skb for RX
1509  * @priv: The DaVinci EMAC private adapter structure
1510  * @buf_size: size of SKB data buffer to allocate
1511  * @data_token: data token returned (skb handle for storing in buffer desc)
1512  * @ch: RX channel number
1513  *
1514  * Called during RX channel setup - allocates skb buffer of required size
1515  * and provides the skb handle and allocated buffer data pointer to caller
1516  *
1517  * Returns skb data pointer or 0 on failure to alloc skb
1518  */
1519 static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
1520                 void **data_token, u32 ch)
1521 {
1522         struct net_device *ndev = priv->ndev;
1523         struct device *emac_dev = &ndev->dev;
1524         struct sk_buff *p_skb;
1525
1526         p_skb = dev_alloc_skb(buf_size);
1527         if (unlikely(NULL == p_skb)) {
1528                 if (netif_msg_rx_err(priv) && net_ratelimit())
1529                         dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
1530                 return NULL;
1531         }
1532
1533         /* set device pointer in skb and reserve space for extra bytes */
1534         p_skb->dev = ndev;
1535         skb_reserve(p_skb, NET_IP_ALIGN);
1536         *data_token = (void *) p_skb;
1537         EMAC_CACHE_WRITEBACK_INVALIDATE((unsigned long)p_skb->data, buf_size);
1538         return p_skb->data;
1539 }
1540
1541 /**
1542  * emac_init_rxch: RX channel initialization
1543  * @priv: The DaVinci EMAC private adapter structure
1544  * @ch: RX channel number
1545  * @param: mac address for RX channel
1546  *
1547  * Called during device init to setup a RX channel (allocate buffers and
1548  * buffer descriptors, create queue and keep ready for reception
1549  *
1550  * Returns success(0) or mem alloc failures error code
1551  */
1552 static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
1553 {
1554         struct device *emac_dev = &priv->ndev->dev;
1555         u32 cnt, bd_size;
1556         void __iomem *mem;
1557         struct emac_rx_bd __iomem *curr_bd;
1558         struct emac_rxch *rxch = NULL;
1559
1560         rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
1561         if (NULL == rxch) {
1562                 dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
1563                 return -ENOMEM;
1564         }
1565         priv->rxch[ch] = rxch;
1566         rxch->buf_size = priv->rx_buf_size;
1567         rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
1568         rxch->queue_active = 0;
1569         rxch->teardown_pending = 0;
1570
1571         /* save mac address */
1572         for (cnt = 0; cnt < 6; cnt++)
1573                 rxch->mac_addr[cnt] = param[cnt];
1574
1575         /* allocate buffer descriptor pool align every BD on four word
1576          * boundry for future requirements */
1577         bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
1578         rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1579         rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
1580         rxch->bd_mem = EMAC_RX_BD_MEM(priv);
1581         __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
1582         rxch->pkt_queue.buf_list = &rxch->buf_queue;
1583
1584         /* allocate RX buffer and initialize the BD linked list */
1585         mem = (void __force __iomem *)
1586                         (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
1587         rxch->active_queue_head = NULL;
1588         rxch->active_queue_tail = mem;
1589         for (cnt = 0; cnt < rxch->num_bd; cnt++) {
1590                 curr_bd = mem + (cnt * bd_size);
1591                 /* for future use the last parameter contains the BD ptr */
1592                 curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
1593                                     rxch->buf_size,
1594                                     (void __force **)&curr_bd->buf_token,
1595                                     EMAC_DEF_RX_CH);
1596                 if (curr_bd->data_ptr == NULL) {
1597                         dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
1598                                 "failed for ch %d\n", ch);
1599                         kfree(rxch);
1600                         return -ENOMEM;
1601                 }
1602
1603                 /* populate the hardware descriptor */
1604                 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
1605                 /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
1606                 curr_bd->buff_ptr = virt_to_phys(curr_bd->data_ptr);
1607                 curr_bd->off_b_len = rxch->buf_size;
1608                 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1609
1610                 /* write back to hardware memory */
1611                 BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
1612                                               EMAC_BD_LENGTH_FOR_CACHE);
1613                 curr_bd->next = rxch->active_queue_head;
1614                 rxch->active_queue_head = curr_bd;
1615         }
1616
1617         /* At this point rxCppi->activeQueueHead points to the first
1618            RX BD ready to be given to RX HDP and rxch->active_queue_tail
1619            points to the last RX BD
1620          */
1621         return 0;
1622 }
1623
1624 /**
1625  * emac_rxch_teardown: RX channel teardown
1626  * @priv: The DaVinci EMAC private adapter structure
1627  * @ch: RX channel number
1628  *
1629  * Called during device stop to teardown RX channel
1630  *
1631  */
1632 static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
1633 {
1634         struct device *emac_dev = &priv->ndev->dev;
1635         u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1636
1637         while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1638                EMAC_TEARDOWN_VALUE) {
1639                 /* wait till tx teardown complete */
1640                 cpu_relax(); /* TODO: check if this helps ... */
1641                 --teardown_cnt;
1642                 if (0 == teardown_cnt) {
1643                         dev_err(emac_dev, "EMAC: RX teardown aborted\n");
1644                         break;
1645                 }
1646         }
1647         emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
1648 }
1649
1650 /**
1651  * emac_stop_rxch: Stop RX channel operation
1652  * @priv: The DaVinci EMAC private adapter structure
1653  * @ch: RX channel number
1654  *
1655  * Called during device stop to stop RX channel operation
1656  *
1657  */
1658 static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
1659 {
1660         struct emac_rxch *rxch = priv->rxch[ch];
1661
1662         if (rxch) {
1663                 rxch->teardown_pending = 1;
1664                 emac_write(EMAC_RXTEARDOWN, ch);
1665                 /* wait for teardown complete */
1666                 emac_rxch_teardown(priv, ch);
1667                 rxch->teardown_pending = 0;
1668                 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1669         }
1670 }
1671
1672 /**
1673  * emac_cleanup_rxch: Book-keep function to clean RX channel resources
1674  * @priv: The DaVinci EMAC private adapter structure
1675  * @ch: RX channel number
1676  *
1677  * Called during device stop to clean up RX channel resources
1678  *
1679  */
1680 static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
1681 {
1682         struct emac_rxch *rxch = priv->rxch[ch];
1683         struct emac_rx_bd __iomem *curr_bd;
1684
1685         if (rxch) {
1686                 /* free the receive buffers previously allocated */
1687                 curr_bd = rxch->active_queue_head;
1688                 while (curr_bd) {
1689                         if (curr_bd->buf_token) {
1690                                 dev_kfree_skb_any((struct sk_buff *)\
1691                                                   curr_bd->buf_token);
1692                         }
1693                         curr_bd = curr_bd->next;
1694                 }
1695                 if (rxch->bd_mem)
1696                         rxch->bd_mem = NULL;
1697                 kfree(rxch);
1698                 priv->rxch[ch] = NULL;
1699         }
1700 }
1701
1702 /**
1703  * emac_set_type0addr: Set EMAC Type0 mac address
1704  * @priv: The DaVinci EMAC private adapter structure
1705  * @ch: RX channel number
1706  * @mac_addr: MAC address to set in device
1707  *
1708  * Called internally to set Type0 mac address of the adapter (Device)
1709  *
1710  * Returns success (0) or appropriate error code (none as of now)
1711  */
1712 static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1713 {
1714         u32 val;
1715         val = ((mac_addr[5] << 8) | (mac_addr[4]));
1716         emac_write(EMAC_MACSRCADDRLO, val);
1717
1718         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1719                (mac_addr[1] << 8) | (mac_addr[0]));
1720         emac_write(EMAC_MACSRCADDRHI, val);
1721         val = emac_read(EMAC_RXUNICASTSET);
1722         val |= BIT(ch);
1723         emac_write(EMAC_RXUNICASTSET, val);
1724         val = emac_read(EMAC_RXUNICASTCLEAR);
1725         val &= ~BIT(ch);
1726         emac_write(EMAC_RXUNICASTCLEAR, val);
1727 }
1728
1729 /**
1730  * emac_set_type1addr: Set EMAC Type1 mac address
1731  * @priv: The DaVinci EMAC private adapter structure
1732  * @ch: RX channel number
1733  * @mac_addr: MAC address to set in device
1734  *
1735  * Called internally to set Type1 mac address of the adapter (Device)
1736  *
1737  * Returns success (0) or appropriate error code (none as of now)
1738  */
1739 static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1740 {
1741         u32 val;
1742         emac_write(EMAC_MACINDEX, ch);
1743         val = ((mac_addr[5] << 8) | mac_addr[4]);
1744         emac_write(EMAC_MACADDRLO, val);
1745         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1746                (mac_addr[1] << 8) | (mac_addr[0]));
1747         emac_write(EMAC_MACADDRHI, val);
1748         emac_set_type0addr(priv, ch, mac_addr);
1749 }
1750
1751 /**
1752  * emac_set_type2addr: Set EMAC Type2 mac address
1753  * @priv: The DaVinci EMAC private adapter structure
1754  * @ch: RX channel number
1755  * @mac_addr: MAC address to set in device
1756  * @index: index into RX address entries
1757  * @match: match parameter for RX address matching logic
1758  *
1759  * Called internally to set Type2 mac address of the adapter (Device)
1760  *
1761  * Returns success (0) or appropriate error code (none as of now)
1762  */
1763 static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1764                                char *mac_addr, int index, int match)
1765 {
1766         u32 val;
1767         emac_write(EMAC_MACINDEX, index);
1768         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1769                (mac_addr[1] << 8) | (mac_addr[0]));
1770         emac_write(EMAC_MACADDRHI, val);
1771         val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1772                (match << 19) | BIT(20));
1773         emac_write(EMAC_MACADDRLO, val);
1774         emac_set_type0addr(priv, ch, mac_addr);
1775 }
1776
1777 /**
1778  * emac_setmac: Set mac address in the adapter (internal function)
1779  * @priv: The DaVinci EMAC private adapter structure
1780  * @ch: RX channel number
1781  * @mac_addr: MAC address to set in device
1782  *
1783  * Called internally to set the mac address of the adapter (Device)
1784  *
1785  * Returns success (0) or appropriate error code (none as of now)
1786  */
1787 static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1788 {
1789         struct device *emac_dev = &priv->ndev->dev;
1790
1791         if (priv->rx_addr_type == 0) {
1792                 emac_set_type0addr(priv, ch, mac_addr);
1793         } else if (priv->rx_addr_type == 1) {
1794                 u32 cnt;
1795                 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1796                         emac_set_type1addr(priv, ch, mac_addr);
1797         } else if (priv->rx_addr_type == 2) {
1798                 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1799                 emac_set_type0addr(priv, ch, mac_addr);
1800         } else {
1801                 if (netif_msg_drv(priv))
1802                         dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1803         }
1804 }
1805
1806 /**
1807  * emac_dev_setmac_addr: Set mac address in the adapter
1808  * @ndev: The DaVinci EMAC network adapter
1809  * @addr: MAC address to set in device
1810  *
1811  * Called by the system to set the mac address of the adapter (Device)
1812  *
1813  * Returns success (0) or appropriate error code (none as of now)
1814  */
1815 static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1816 {
1817         struct emac_priv *priv = netdev_priv(ndev);
1818         struct emac_rxch *rxch = priv->rxch[EMAC_DEF_RX_CH];
1819         struct device *emac_dev = &priv->ndev->dev;
1820         struct sockaddr *sa = addr;
1821
1822         if (!is_valid_ether_addr(sa->sa_data))
1823                 return -EINVAL;
1824
1825         /* Store mac addr in priv and rx channel and set it in EMAC hw */
1826         memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
1827         memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
1828
1829         /* If the interface is down - rxch is NULL. */
1830         /* MAC address is configured only after the interface is enabled. */
1831         if (netif_running(ndev)) {
1832                 memcpy(rxch->mac_addr, sa->sa_data, ndev->addr_len);
1833                 emac_setmac(priv, EMAC_DEF_RX_CH, rxch->mac_addr);
1834         }
1835
1836         if (netif_msg_drv(priv))
1837                 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1838                                         priv->mac_addr);
1839
1840         return 0;
1841 }
1842
1843 /**
1844  * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
1845  * @priv: The DaVinci EMAC private adapter structure
1846  * @ch: RX channel number to process buffer descriptors for
1847  * @curr_bd: current buffer descriptor
1848  * @buffer: buffer pointer for descriptor
1849  * @buf_token: buffer token (stores skb information)
1850  *
1851  * Prepares the recycled buffer descriptor and addes it to hardware
1852  * receive queue - if queue empty this descriptor becomes the head
1853  * else addes the descriptor to end of queue
1854  *
1855  */
1856 static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
1857                 struct emac_rx_bd __iomem *curr_bd,
1858                 char *buffer, void *buf_token)
1859 {
1860         struct emac_rxch *rxch = priv->rxch[ch];
1861
1862         /* populate the hardware descriptor */
1863         curr_bd->h_next = 0;
1864         /* FIXME buff_ptr = dma_map_single(... buffer ...) */
1865         curr_bd->buff_ptr = virt_to_phys(buffer);
1866         curr_bd->off_b_len = rxch->buf_size;
1867         curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1868         curr_bd->next = NULL;
1869         curr_bd->data_ptr = buffer;
1870         curr_bd->buf_token = buf_token;
1871
1872         /* write back  */
1873         BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1874         if (rxch->active_queue_head == NULL) {
1875                 rxch->active_queue_head = curr_bd;
1876                 rxch->active_queue_tail = curr_bd;
1877                 if (0 != rxch->queue_active) {
1878                         emac_write(EMAC_RXHDP(ch),
1879                                    emac_virt_to_phys(rxch->active_queue_head));
1880                         rxch->queue_active = 1;
1881                 }
1882         } else {
1883                 struct emac_rx_bd __iomem *tail_bd;
1884                 u32 frame_status;
1885
1886                 tail_bd = rxch->active_queue_tail;
1887                 rxch->active_queue_tail = curr_bd;
1888                 tail_bd->next = curr_bd;
1889                 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1890                 tail_bd->h_next = emac_virt_to_phys(curr_bd);
1891                 frame_status = tail_bd->mode;
1892                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1893                         emac_write(EMAC_RXHDP(ch),
1894                                         emac_virt_to_phys(curr_bd));
1895                         frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1896                         tail_bd->mode = frame_status;
1897                         ++rxch->end_of_queue_add;
1898                 }
1899         }
1900         ++rxch->recycled_bd;
1901 }
1902
1903 /**
1904  * emac_net_rx_cb: Prepares packet and sends to upper layer
1905  * @priv: The DaVinci EMAC private adapter structure
1906  * @net_pkt_list: Network packet list (received packets)
1907  *
1908  * Invalidates packet buffer memory and sends the received packet to upper
1909  * layer
1910  *
1911  * Returns success or appropriate error code (none as of now)
1912  */
1913 static int emac_net_rx_cb(struct emac_priv *priv,
1914                           struct emac_netpktobj *net_pkt_list)
1915 {
1916         struct sk_buff *p_skb;
1917         p_skb = (struct sk_buff *)net_pkt_list->pkt_token;
1918         /* set length of packet */
1919         skb_put(p_skb, net_pkt_list->pkt_length);
1920         EMAC_CACHE_INVALIDATE((unsigned long)p_skb->data, p_skb->len);
1921         p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
1922         netif_receive_skb(p_skb);
1923         priv->net_dev_stats.rx_bytes += net_pkt_list->pkt_length;
1924         priv->net_dev_stats.rx_packets++;
1925         return 0;
1926 }
1927
1928 /**
1929  * emac_rx_bdproc: RX buffer descriptor (packet) processing
1930  * @priv: The DaVinci EMAC private adapter structure
1931  * @ch: RX channel number to process buffer descriptors for
1932  * @budget: number of packets allowed to process
1933  * @pending: indication to caller that packets are pending to process
1934  *
1935  * Processes RX buffer descriptors - checks ownership bit on the RX buffer
1936  * descriptor, sends the receive packet to upper layer, allocates a new SKB
1937  * and recycles the buffer descriptor (requeues it in hardware RX queue).
1938  * Only "budget" number of packets are processed and indication of pending
1939  * packets provided to the caller.
1940  *
1941  * Returns number of packets processed (and indication of pending packets)
1942  */
1943 static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1944 {
1945         unsigned long flags;
1946         u32 frame_status;
1947         u32 pkts_processed = 0;
1948         char *new_buffer;
1949         struct emac_rx_bd __iomem *curr_bd;
1950         struct emac_rx_bd __iomem *last_bd;
1951         struct emac_netpktobj *curr_pkt, pkt_obj;
1952         struct emac_netbufobj buf_obj;
1953         struct emac_netbufobj *rx_buf_obj;
1954         void *new_buf_token;
1955         struct emac_rxch *rxch = priv->rxch[ch];
1956
1957         if (unlikely(1 == rxch->teardown_pending))
1958                 return 0;
1959         ++rxch->proc_count;
1960         spin_lock_irqsave(&priv->rx_lock, flags);
1961         pkt_obj.buf_list = &buf_obj;
1962         curr_pkt = &pkt_obj;
1963         curr_bd = rxch->active_queue_head;
1964         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1965         frame_status = curr_bd->mode;
1966
1967         while ((curr_bd) &&
1968                ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1969                (pkts_processed < budget)) {
1970
1971                 new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
1972                                         &new_buf_token, EMAC_DEF_RX_CH);
1973                 if (unlikely(NULL == new_buffer)) {
1974                         ++rxch->out_of_rx_buffers;
1975                         goto end_emac_rx_bdproc;
1976                 }
1977
1978                 /* populate received packet data structure */
1979                 rx_buf_obj = &curr_pkt->buf_list[0];
1980                 rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
1981                 rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
1982                 rx_buf_obj->buf_token = curr_bd->buf_token;
1983                 curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
1984                 curr_pkt->num_bufs = 1;
1985                 curr_pkt->pkt_length =
1986                         (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
1987                 emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd));
1988                 ++rxch->processed_bd;
1989                 last_bd = curr_bd;
1990                 curr_bd = last_bd->next;
1991                 rxch->active_queue_head = curr_bd;
1992
1993                 /* check if end of RX queue ? */
1994                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1995                         if (curr_bd) {
1996                                 ++rxch->mis_queued_packets;
1997                                 emac_write(EMAC_RXHDP(ch),
1998                                            emac_virt_to_phys(curr_bd));
1999                         } else {
2000                                 ++rxch->end_of_queue;
2001                                 rxch->queue_active = 0;
2002                         }
2003                 }
2004
2005                 /* recycle BD */
2006                 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2007                                        new_buf_token);
2008
2009                 /* return the packet to the user - BD ptr passed in
2010                  * last parameter for potential *future* use */
2011                 spin_unlock_irqrestore(&priv->rx_lock, flags);
2012                 emac_net_rx_cb(priv, curr_pkt);
2013                 spin_lock_irqsave(&priv->rx_lock, flags);
2014                 curr_bd = rxch->active_queue_head;
2015                 if (curr_bd) {
2016                         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2017                         frame_status = curr_bd->mode;
2018                 }
2019                 ++pkts_processed;
2020         }
2021
2022 end_emac_rx_bdproc:
2023         spin_unlock_irqrestore(&priv->rx_lock, flags);
2024         return pkts_processed;
2025 }
2026
2027 /**
2028  * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
2029  * @priv: The DaVinci EMAC private adapter structure
2030  *
2031  * Enables EMAC hardware for packet processing - enables PHY, enables RX
2032  * for packet reception and enables device interrupts and then NAPI
2033  *
2034  * Returns success (0) or appropriate error code (none right now)
2035  */
2036 static int emac_hw_enable(struct emac_priv *priv)
2037 {
2038         u32 ch, val, mbp_enable, mac_control;
2039
2040         /* Soft reset */
2041         emac_write(EMAC_SOFTRESET, 1);
2042         while (emac_read(EMAC_SOFTRESET))
2043                 cpu_relax();
2044
2045         /* Disable interrupt & Set pacing for more interrupts initially */
2046         emac_int_disable(priv);
2047
2048         /* Full duplex enable bit set when auto negotiation happens */
2049         mac_control =
2050                 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
2051                 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
2052                 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
2053                 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
2054         emac_write(EMAC_MACCONTROL, mac_control);
2055
2056         mbp_enable =
2057                 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
2058                 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
2059                  ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
2060                  ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
2061                  ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
2062                  ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
2063                  ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
2064                  ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
2065                         EMAC_RXMBP_PROMCH_SHIFT) |
2066                  ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
2067                  ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
2068                         EMAC_RXMBP_BROADCH_SHIFT) |
2069                  ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
2070                  ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
2071                         EMAC_RXMBP_MULTICH_SHIFT));
2072         emac_write(EMAC_RXMBPENABLE, mbp_enable);
2073         emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
2074                                    EMAC_RX_MAX_LEN_MASK));
2075         emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
2076                                          EMAC_RX_BUFFER_OFFSET_MASK));
2077         emac_write(EMAC_RXFILTERLOWTHRESH, 0);
2078         emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
2079         priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
2080
2081         val = emac_read(EMAC_TXCONTROL);
2082         val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
2083         emac_write(EMAC_TXCONTROL, val);
2084         val = emac_read(EMAC_RXCONTROL);
2085         val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
2086         emac_write(EMAC_RXCONTROL, val);
2087         emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
2088
2089         for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
2090                 emac_write(EMAC_TXHDP(ch), 0);
2091                 emac_write(EMAC_TXINTMASKSET, BIT(ch));
2092         }
2093         for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
2094                 struct emac_rxch *rxch = priv->rxch[ch];
2095                 emac_setmac(priv, ch, rxch->mac_addr);
2096                 emac_write(EMAC_RXINTMASKSET, BIT(ch));
2097                 rxch->queue_active = 1;
2098                 emac_write(EMAC_RXHDP(ch),
2099                            emac_virt_to_phys(rxch->active_queue_head));
2100         }
2101
2102         /* Enable MII */
2103         val = emac_read(EMAC_MACCONTROL);
2104         val |= (EMAC_MACCONTROL_GMIIEN);
2105         emac_write(EMAC_MACCONTROL, val);
2106
2107         /* Enable NAPI and interrupts */
2108         napi_enable(&priv->napi);
2109         emac_int_enable(priv);
2110         return 0;
2111
2112 }
2113
2114 /**
2115  * emac_poll: EMAC NAPI Poll function
2116  * @ndev: The DaVinci EMAC network adapter
2117  * @budget: Number of receive packets to process (as told by NAPI layer)
2118  *
2119  * NAPI Poll function implemented to process packets as per budget. We check
2120  * the type of interrupt on the device and accordingly call the TX or RX
2121  * packet processing functions. We follow the budget for RX processing and
2122  * also put a cap on number of TX pkts processed through config param. The
2123  * NAPI schedule function is called if more packets pending.
2124  *
2125  * Returns number of packets received (in most cases; else TX pkts - rarely)
2126  */
2127 static int emac_poll(struct napi_struct *napi, int budget)
2128 {
2129         unsigned int mask;
2130         struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
2131         struct net_device *ndev = priv->ndev;
2132         struct device *emac_dev = &ndev->dev;
2133         u32 status = 0;
2134         u32 num_pkts = 0;
2135
2136         if (!netif_running(ndev))
2137                 return 0;
2138
2139         /* Check interrupt vectors and call packet processing */
2140         status = emac_read(EMAC_MACINVECTOR);
2141
2142         mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
2143
2144         if (priv->version == EMAC_VERSION_2)
2145                 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
2146
2147         if (status & mask) {
2148                 num_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
2149                                           EMAC_DEF_TX_MAX_SERVICE);
2150         } /* TX processing */
2151
2152         if (num_pkts)
2153                 return budget;
2154
2155         mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
2156
2157         if (priv->version == EMAC_VERSION_2)
2158                 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
2159
2160         if (status & mask) {
2161                 num_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, budget);
2162         } /* RX processing */
2163
2164         if (num_pkts < budget) {
2165                 napi_complete(napi);
2166                 emac_int_enable(priv);
2167         }
2168
2169         mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
2170         if (priv->version == EMAC_VERSION_2)
2171                 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
2172
2173         if (unlikely(status & mask)) {
2174                 u32 ch, cause;
2175                 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
2176                 netif_stop_queue(ndev);
2177                 napi_disable(&priv->napi);
2178
2179                 status = emac_read(EMAC_MACSTATUS);
2180                 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
2181                          EMAC_MACSTATUS_TXERRCODE_SHIFT);
2182                 if (cause) {
2183                         ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
2184                               EMAC_MACSTATUS_TXERRCH_SHIFT);
2185                         if (net_ratelimit()) {
2186                                 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
2187                                         &emac_txhost_errcodes[cause][0], ch);
2188                         }
2189                 }
2190                 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
2191                          EMAC_MACSTATUS_RXERRCODE_SHIFT);
2192                 if (cause) {
2193                         ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
2194                               EMAC_MACSTATUS_RXERRCH_SHIFT);
2195                         if (netif_msg_hw(priv) && net_ratelimit())
2196                                 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
2197                                         &emac_rxhost_errcodes[cause][0], ch);
2198                 }
2199         } /* Host error processing */
2200
2201         return num_pkts;
2202 }
2203
2204 #ifdef CONFIG_NET_POLL_CONTROLLER
2205 /**
2206  * emac_poll_controller: EMAC Poll controller function
2207  * @ndev: The DaVinci EMAC network adapter
2208  *
2209  * Polled functionality used by netconsole and others in non interrupt mode
2210  *
2211  */
2212 void emac_poll_controller(struct net_device *ndev)
2213 {
2214         struct emac_priv *priv = netdev_priv(ndev);
2215
2216         emac_int_disable(priv);
2217         emac_irq(ndev->irq, priv);
2218         emac_int_enable(priv);
2219 }
2220 #endif
2221
2222 /* PHY/MII bus related */
2223
2224 /* Wait until mdio is ready for next command */
2225 #define MDIO_WAIT_FOR_USER_ACCESS\
2226                 while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
2227                         MDIO_USERACCESS_GO) != 0)
2228
2229 static int emac_mii_read(struct mii_bus *bus, int phy_id, int phy_reg)
2230 {
2231         unsigned int phy_data = 0;
2232         unsigned int phy_control;
2233
2234         /* Wait until mdio is ready for next command */
2235         MDIO_WAIT_FOR_USER_ACCESS;
2236
2237         phy_control = (MDIO_USERACCESS_GO |
2238                        MDIO_USERACCESS_READ |
2239                        ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2240                        ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2241                        (phy_data & MDIO_USERACCESS_DATA));
2242         emac_mdio_write(MDIO_USERACCESS(0), phy_control);
2243
2244         /* Wait until mdio is ready for next command */
2245         MDIO_WAIT_FOR_USER_ACCESS;
2246
2247         return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
2248
2249 }
2250
2251 static int emac_mii_write(struct mii_bus *bus, int phy_id,
2252                           int phy_reg, u16 phy_data)
2253 {
2254
2255         unsigned int control;
2256
2257         /*  until mdio is ready for next command */
2258         MDIO_WAIT_FOR_USER_ACCESS;
2259
2260         control = (MDIO_USERACCESS_GO |
2261                    MDIO_USERACCESS_WRITE |
2262                    ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2263                    ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2264                    (phy_data & MDIO_USERACCESS_DATA));
2265         emac_mdio_write(MDIO_USERACCESS(0), control);
2266
2267         return 0;
2268 }
2269
2270 static int emac_mii_reset(struct mii_bus *bus)
2271 {
2272         unsigned int clk_div;
2273         int mdio_bus_freq = emac_bus_frequency;
2274
2275         if (mdio_max_freq & mdio_bus_freq)
2276                 clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
2277         else
2278                 clk_div = 0xFF;
2279
2280         clk_div &= MDIO_CONTROL_CLKDIV;
2281
2282         /* Set enable and clock divider in MDIOControl */
2283         emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
2284
2285         return 0;
2286
2287 }
2288
2289 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, PHY_POLL };
2290
2291 /* emac_driver: EMAC MII bus structure */
2292
2293 static struct mii_bus *emac_mii;
2294
2295 static void emac_adjust_link(struct net_device *ndev)
2296 {
2297         struct emac_priv *priv = netdev_priv(ndev);
2298         struct phy_device *phydev = priv->phydev;
2299         unsigned long flags;
2300         int new_state = 0;
2301
2302         spin_lock_irqsave(&priv->lock, flags);
2303
2304         if (phydev->link) {
2305                 /* check the mode of operation - full/half duplex */
2306                 if (phydev->duplex != priv->duplex) {
2307                         new_state = 1;
2308                         priv->duplex = phydev->duplex;
2309                 }
2310                 if (phydev->speed != priv->speed) {
2311                         new_state = 1;
2312                         priv->speed = phydev->speed;
2313                 }
2314                 if (!priv->link) {
2315                         new_state = 1;
2316                         priv->link = 1;
2317                 }
2318
2319         } else if (priv->link) {
2320                 new_state = 1;
2321                 priv->link = 0;
2322                 priv->speed = 0;
2323                 priv->duplex = ~0;
2324         }
2325         if (new_state) {
2326                 emac_update_phystatus(priv);
2327                 phy_print_status(priv->phydev);
2328         }
2329
2330         spin_unlock_irqrestore(&priv->lock, flags);
2331 }
2332
2333 /*************************************************************************
2334  *  Linux Driver Model
2335  *************************************************************************/
2336
2337 /**
2338  * emac_devioctl: EMAC adapter ioctl
2339  * @ndev: The DaVinci EMAC network adapter
2340  * @ifrq: request parameter
2341  * @cmd: command parameter
2342  *
2343  * EMAC driver ioctl function
2344  *
2345  * Returns success(0) or appropriate error code
2346  */
2347 static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
2348 {
2349         dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
2350
2351         if (!(netif_running(ndev)))
2352                 return -EINVAL;
2353
2354         /* TODO: Add phy read and write and private statistics get feature */
2355
2356         return -EOPNOTSUPP;
2357 }
2358
2359 /**
2360  * emac_dev_open: EMAC device open
2361  * @ndev: The DaVinci EMAC network adapter
2362  *
2363  * Called when system wants to start the interface. We init TX/RX channels
2364  * and enable the hardware for packet reception/transmission and start the
2365  * network queue.
2366  *
2367  * Returns 0 for a successful open, or appropriate error code
2368  */
2369 static int emac_dev_open(struct net_device *ndev)
2370 {
2371         struct device *emac_dev = &ndev->dev;
2372         u32 rc, cnt, ch;
2373         int phy_addr;
2374         struct resource *res;
2375         int q, m;
2376         int i = 0;
2377         int k = 0;
2378         struct emac_priv *priv = netdev_priv(ndev);
2379
2380         netif_carrier_off(ndev);
2381         for (cnt = 0; cnt <= ETH_ALEN; cnt++)
2382                 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
2383
2384         /* Configuration items */
2385         priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
2386
2387         /* Clear basic hardware */
2388         for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
2389                 emac_write(EMAC_TXHDP(ch), 0);
2390                 emac_write(EMAC_RXHDP(ch), 0);
2391                 emac_write(EMAC_RXHDP(ch), 0);
2392                 emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2393                 emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2394         }
2395         priv->mac_hash1 = 0;
2396         priv->mac_hash2 = 0;
2397         emac_write(EMAC_MACHASH1, 0);
2398         emac_write(EMAC_MACHASH2, 0);
2399
2400         /* multi ch not supported - open 1 TX, 1RX ch by default */
2401         rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
2402         if (0 != rc) {
2403                 dev_err(emac_dev, "DaVinci EMAC: emac_init_txch() failed");
2404                 return rc;
2405         }
2406         rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
2407         if (0 != rc) {
2408                 dev_err(emac_dev, "DaVinci EMAC: emac_init_rxch() failed");
2409                 return rc;
2410         }
2411
2412         /* Request IRQ */
2413
2414         while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
2415                 for (i = res->start; i <= res->end; i++) {
2416                         if (request_irq(i, emac_irq, IRQF_DISABLED,
2417                                         ndev->name, ndev))
2418                                 goto rollback;
2419                 }
2420                 k++;
2421         }
2422
2423         /* Start/Enable EMAC hardware */
2424         emac_hw_enable(priv);
2425
2426         /* find the first phy */
2427         priv->phydev = NULL;
2428         if (priv->phy_mask) {
2429                 emac_mii_reset(priv->mii_bus);
2430                 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
2431                         if (priv->mii_bus->phy_map[phy_addr]) {
2432                                 priv->phydev = priv->mii_bus->phy_map[phy_addr];
2433                                 break;
2434                         }
2435                 }
2436
2437                 if (!priv->phydev) {
2438                         printk(KERN_ERR "%s: no PHY found\n", ndev->name);
2439                         return -1;
2440                 }
2441
2442                 priv->phydev = phy_connect(ndev, dev_name(&priv->phydev->dev),
2443                                 &emac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
2444
2445                 if (IS_ERR(priv->phydev)) {
2446                         printk(KERN_ERR "%s: Could not attach to PHY\n",
2447                                                                 ndev->name);
2448                         return PTR_ERR(priv->phydev);
2449                 }
2450
2451                 priv->link = 0;
2452                 priv->speed = 0;
2453                 priv->duplex = ~0;
2454
2455                 printk(KERN_INFO "%s: attached PHY driver [%s] "
2456                         "(mii_bus:phy_addr=%s, id=%x)\n", ndev->name,
2457                         priv->phydev->drv->name, dev_name(&priv->phydev->dev),
2458                         priv->phydev->phy_id);
2459         } else{
2460                 /* No PHY , fix the link, speed and duplex settings */
2461                 priv->link = 1;
2462                 priv->speed = SPEED_100;
2463                 priv->duplex = DUPLEX_FULL;
2464                 emac_update_phystatus(priv);
2465         }
2466
2467         if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
2468                 emac_dump_regs(priv);
2469
2470         if (netif_msg_drv(priv))
2471                 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
2472
2473         if (priv->phy_mask)
2474                 phy_start(priv->phydev);
2475
2476         return 0;
2477
2478 rollback:
2479
2480         dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
2481
2482         for (q = k; k >= 0; k--) {
2483                 for (m = i; m >= res->start; m--)
2484                         free_irq(m, ndev);
2485                 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
2486                 m = res->end;
2487         }
2488         return -EBUSY;
2489 }
2490
2491 /**
2492  * emac_dev_stop: EMAC device stop
2493  * @ndev: The DaVinci EMAC network adapter
2494  *
2495  * Called when system wants to stop or down the interface. We stop the network
2496  * queue, disable interrupts and cleanup TX/RX channels.
2497  *
2498  * We return the statistics in net_device_stats structure pulled from emac
2499  */
2500 static int emac_dev_stop(struct net_device *ndev)
2501 {
2502         struct resource *res;
2503         int i = 0;
2504         int irq_num;
2505         struct emac_priv *priv = netdev_priv(ndev);
2506         struct device *emac_dev = &ndev->dev;
2507
2508         /* inform the upper layers. */
2509         netif_stop_queue(ndev);
2510         napi_disable(&priv->napi);
2511
2512         netif_carrier_off(ndev);
2513         emac_int_disable(priv);
2514         emac_stop_txch(priv, EMAC_DEF_TX_CH);
2515         emac_stop_rxch(priv, EMAC_DEF_RX_CH);
2516         emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
2517         emac_cleanup_rxch(priv, EMAC_DEF_RX_CH);
2518         emac_write(EMAC_SOFTRESET, 1);
2519
2520         if (priv->phydev)
2521                 phy_disconnect(priv->phydev);
2522
2523         /* Free IRQ */
2524         while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
2525                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2526                         free_irq(irq_num, priv->ndev);
2527                 i++;
2528         }
2529
2530         if (netif_msg_drv(priv))
2531                 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
2532
2533         return 0;
2534 }
2535
2536 /**
2537  * emac_dev_getnetstats: EMAC get statistics function
2538  * @ndev: The DaVinci EMAC network adapter
2539  *
2540  * Called when system wants to get statistics from the device.
2541  *
2542  * We return the statistics in net_device_stats structure pulled from emac
2543  */
2544 static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
2545 {
2546         struct emac_priv *priv = netdev_priv(ndev);
2547
2548         /* update emac hardware stats and reset the registers*/
2549
2550         priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
2551         emac_write(EMAC_RXMCASTFRAMES, EMAC_ALL_MULTI_REG_VALUE);
2552
2553         priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
2554                                            emac_read(EMAC_TXSINGLECOLL) +
2555                                            emac_read(EMAC_TXMULTICOLL));
2556         emac_write(EMAC_TXCOLLISION, EMAC_ALL_MULTI_REG_VALUE);
2557         emac_write(EMAC_TXSINGLECOLL, EMAC_ALL_MULTI_REG_VALUE);
2558         emac_write(EMAC_TXMULTICOLL, EMAC_ALL_MULTI_REG_VALUE);
2559
2560         priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
2561                                                 emac_read(EMAC_RXJABBER) +
2562                                                 emac_read(EMAC_RXUNDERSIZED));
2563         emac_write(EMAC_RXOVERSIZED, EMAC_ALL_MULTI_REG_VALUE);
2564         emac_write(EMAC_RXJABBER, EMAC_ALL_MULTI_REG_VALUE);
2565         emac_write(EMAC_RXUNDERSIZED, EMAC_ALL_MULTI_REG_VALUE);
2566
2567         priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
2568                                                emac_read(EMAC_RXMOFOVERRUNS));
2569         emac_write(EMAC_RXSOFOVERRUNS, EMAC_ALL_MULTI_REG_VALUE);
2570         emac_write(EMAC_RXMOFOVERRUNS, EMAC_ALL_MULTI_REG_VALUE);
2571
2572         priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
2573         emac_write(EMAC_RXDMAOVERRUNS, EMAC_ALL_MULTI_REG_VALUE);
2574
2575         priv->net_dev_stats.tx_carrier_errors +=
2576                 emac_read(EMAC_TXCARRIERSENSE);
2577         emac_write(EMAC_TXCARRIERSENSE, EMAC_ALL_MULTI_REG_VALUE);
2578
2579         priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
2580         emac_write(EMAC_TXUNDERRUN, EMAC_ALL_MULTI_REG_VALUE);
2581
2582         return &priv->net_dev_stats;
2583 }
2584
2585 static const struct net_device_ops emac_netdev_ops = {
2586         .ndo_open               = emac_dev_open,
2587         .ndo_stop               = emac_dev_stop,
2588         .ndo_start_xmit         = emac_dev_xmit,
2589         .ndo_set_multicast_list = emac_dev_mcast_set,
2590         .ndo_set_mac_address    = emac_dev_setmac_addr,
2591         .ndo_do_ioctl           = emac_devioctl,
2592         .ndo_tx_timeout         = emac_dev_tx_timeout,
2593         .ndo_get_stats          = emac_dev_getnetstats,
2594 #ifdef CONFIG_NET_POLL_CONTROLLER
2595         .ndo_poll_controller    = emac_poll_controller,
2596 #endif
2597 };
2598
2599 /**
2600  * davinci_emac_probe: EMAC device probe
2601  * @pdev: The DaVinci EMAC device that we are removing
2602  *
2603  * Called when probing for emac devicesr. We get details of instances and
2604  * resource information from platform init and register a network device
2605  * and allocate resources necessary for driver to perform
2606  */
2607 static int __devinit davinci_emac_probe(struct platform_device *pdev)
2608 {
2609         int rc = 0;
2610         struct resource *res;
2611         struct net_device *ndev;
2612         struct emac_priv *priv;
2613         unsigned long size;
2614         struct emac_platform_data *pdata;
2615         struct device *emac_dev;
2616
2617         /* obtain emac clock from kernel */
2618         emac_clk = clk_get(&pdev->dev, NULL);
2619         if (IS_ERR(emac_clk)) {
2620                 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2621                 return -EBUSY;
2622         }
2623         emac_bus_frequency = clk_get_rate(emac_clk);
2624         /* TODO: Probe PHY here if possible */
2625
2626         ndev = alloc_etherdev(sizeof(struct emac_priv));
2627         if (!ndev) {
2628                 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2629                 clk_put(emac_clk);
2630                 return -ENOMEM;
2631         }
2632
2633         platform_set_drvdata(pdev, ndev);
2634         priv = netdev_priv(ndev);
2635         priv->pdev = pdev;
2636         priv->ndev = ndev;
2637         priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2638
2639         spin_lock_init(&priv->tx_lock);
2640         spin_lock_init(&priv->rx_lock);
2641         spin_lock_init(&priv->lock);
2642
2643         pdata = pdev->dev.platform_data;
2644         if (!pdata) {
2645                 printk(KERN_ERR "DaVinci EMAC: No platfrom data\n");
2646                 return -ENODEV;
2647         }
2648
2649         /* MAC addr and PHY mask , RMII enable info from platform_data */
2650         memcpy(priv->mac_addr, pdata->mac_addr, 6);
2651         priv->phy_mask = pdata->phy_mask;
2652         priv->rmii_en = pdata->rmii_en;
2653         priv->version = pdata->version;
2654         emac_dev = &ndev->dev;
2655         /* Get EMAC platform data */
2656         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2657         if (!res) {
2658                 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2659                 rc = -ENOENT;
2660                 goto probe_quit;
2661         }
2662
2663         priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2664         size = res->end - res->start + 1;
2665         if (!request_mem_region(res->start, size, ndev->name)) {
2666                 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() \
2667                                          for regs\n");
2668                 rc = -ENXIO;
2669                 goto probe_quit;
2670         }
2671
2672         priv->remap_addr = ioremap(res->start, size);
2673         if (!priv->remap_addr) {
2674                 dev_err(emac_dev, "Unable to map IO\n");
2675                 rc = -ENOMEM;
2676                 release_mem_region(res->start, size);
2677                 goto probe_quit;
2678         }
2679         priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2680         ndev->base_addr = (unsigned long)priv->remap_addr;
2681
2682         priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2683         priv->ctrl_ram_size = pdata->ctrl_ram_size;
2684         priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2685
2686         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2687         if (!res) {
2688                 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2689                 rc = -ENOENT;
2690                 goto no_irq_res;
2691         }
2692         ndev->irq = res->start;
2693
2694         if (!is_valid_ether_addr(priv->mac_addr)) {
2695                 /* Use random MAC if none passed */
2696                 random_ether_addr(priv->mac_addr);
2697                 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2698                                 __func__, priv->mac_addr);
2699         }
2700
2701         ndev->netdev_ops = &emac_netdev_ops;
2702         SET_ETHTOOL_OPS(ndev, &ethtool_ops);
2703         netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2704
2705         /* register the network device */
2706         SET_NETDEV_DEV(ndev, &pdev->dev);
2707         rc = register_netdev(ndev);
2708         if (rc) {
2709                 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2710                 rc = -ENODEV;
2711                 goto netdev_reg_err;
2712         }
2713
2714         clk_enable(emac_clk);
2715
2716         /* MII/Phy intialisation, mdio bus registration */
2717         emac_mii = mdiobus_alloc();
2718         if (emac_mii == NULL) {
2719                 dev_err(emac_dev, "DaVinci EMAC: Error allocating mii_bus\n");
2720                 rc = -ENOMEM;
2721                 goto mdio_alloc_err;
2722         }
2723
2724         priv->mii_bus = emac_mii;
2725         emac_mii->name  = "emac-mii",
2726         emac_mii->read  = emac_mii_read,
2727         emac_mii->write = emac_mii_write,
2728         emac_mii->reset = emac_mii_reset,
2729         emac_mii->irq   = mii_irqs,
2730         emac_mii->phy_mask = ~(priv->phy_mask);
2731         emac_mii->parent = &pdev->dev;
2732         emac_mii->priv = priv->remap_addr + pdata->mdio_reg_offset;
2733         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%x", priv->pdev->id);
2734         mdio_max_freq = pdata->mdio_max_freq;
2735         emac_mii->reset(emac_mii);
2736
2737         /* Register the MII bus */
2738         rc = mdiobus_register(emac_mii);
2739         if (rc)
2740                 goto mdiobus_quit;
2741
2742         if (netif_msg_probe(priv)) {
2743                 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2744                            "(regs: %p, irq: %d)\n",
2745                            (void *)priv->emac_base_phys, ndev->irq);
2746         }
2747         return 0;
2748
2749 mdiobus_quit:
2750         mdiobus_free(emac_mii);
2751
2752 netdev_reg_err:
2753 mdio_alloc_err:
2754 no_irq_res:
2755         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2756         release_mem_region(res->start, res->end - res->start + 1);
2757         iounmap(priv->remap_addr);
2758
2759 probe_quit:
2760         clk_put(emac_clk);
2761         free_netdev(ndev);
2762         return rc;
2763 }
2764
2765 /**
2766  * davinci_emac_remove: EMAC device remove
2767  * @pdev: The DaVinci EMAC device that we are removing
2768  *
2769  * Called when removing the device driver. We disable clock usage and release
2770  * the resources taken up by the driver and unregister network device
2771  */
2772 static int __devexit davinci_emac_remove(struct platform_device *pdev)
2773 {
2774         struct resource *res;
2775         struct net_device *ndev = platform_get_drvdata(pdev);
2776         struct emac_priv *priv = netdev_priv(ndev);
2777
2778         dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2779
2780         platform_set_drvdata(pdev, NULL);
2781         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2782         mdiobus_unregister(priv->mii_bus);
2783         mdiobus_free(priv->mii_bus);
2784
2785         release_mem_region(res->start, res->end - res->start + 1);
2786
2787         unregister_netdev(ndev);
2788         free_netdev(ndev);
2789         iounmap(priv->remap_addr);
2790
2791         clk_disable(emac_clk);
2792         clk_put(emac_clk);
2793
2794         return 0;
2795 }
2796
2797 /**
2798  * davinci_emac_driver: EMAC platform driver structure
2799  *
2800  * We implement only probe and remove functions - suspend/resume and
2801  * others not supported by this module
2802  */
2803 static struct platform_driver davinci_emac_driver = {
2804         .driver = {
2805                 .name    = "davinci_emac",
2806                 .owner   = THIS_MODULE,
2807         },
2808         .probe = davinci_emac_probe,
2809         .remove = __devexit_p(davinci_emac_remove),
2810 };
2811
2812 /**
2813  * davinci_emac_init: EMAC driver module init
2814  *
2815  * Called when initializing the driver. We register the driver with
2816  * the platform.
2817  */
2818 static int __init davinci_emac_init(void)
2819 {
2820         return platform_driver_register(&davinci_emac_driver);
2821 }
2822 late_initcall(davinci_emac_init);
2823
2824 /**
2825  * davinci_emac_exit: EMAC driver module exit
2826  *
2827  * Called when exiting the driver completely. We unregister the driver with
2828  * the platform and exit
2829  */
2830 static void __exit davinci_emac_exit(void)
2831 {
2832         platform_driver_unregister(&davinci_emac_driver);
2833 }
2834 module_exit(davinci_emac_exit);
2835
2836 MODULE_LICENSE("GPL");
2837 MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2838 MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2839 MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");