Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[sfrench/cifs-2.6.git] / 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 /* EMAC Stats Clear Mask */
334 #define EMAC_STATS_CLR_MASK    (0xFFFFFFFF)
335
336 /** net_buf_obj: EMAC network bufferdata structure
337  *
338  * EMAC network buffer data structure
339  */
340 struct emac_netbufobj {
341         void *buf_token;
342         char *data_ptr;
343         int length;
344 };
345
346 /** net_pkt_obj: EMAC network packet data structure
347  *
348  * EMAC network packet data structure - supports buffer list (for future)
349  */
350 struct emac_netpktobj {
351         void *pkt_token; /* data token may hold tx/rx chan id */
352         struct emac_netbufobj *buf_list; /* array of network buffer objects */
353         int num_bufs;
354         int pkt_length;
355 };
356
357 /** emac_tx_bd: EMAC TX Buffer descriptor data structure
358  *
359  * EMAC TX Buffer descriptor data structure
360  */
361 struct emac_tx_bd {
362         int h_next;
363         int buff_ptr;
364         int off_b_len;
365         int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
366         struct emac_tx_bd __iomem *next;
367         void *buf_token;
368 };
369
370 /** emac_txch: EMAC TX Channel data structure
371  *
372  * EMAC TX Channel data structure
373  */
374 struct emac_txch {
375         /* Config related */
376         u32 num_bd;
377         u32 service_max;
378
379         /* CPPI specific */
380         u32 alloc_size;
381         void __iomem *bd_mem;
382         struct emac_tx_bd __iomem *bd_pool_head;
383         struct emac_tx_bd __iomem *active_queue_head;
384         struct emac_tx_bd __iomem *active_queue_tail;
385         struct emac_tx_bd __iomem *last_hw_bdprocessed;
386         u32 queue_active;
387         u32 teardown_pending;
388         u32 *tx_complete;
389
390         /** statistics */
391         u32 proc_count;     /* TX: # of times emac_tx_bdproc is called */
392         u32 mis_queued_packets;
393         u32 queue_reinit;
394         u32 end_of_queue_add;
395         u32 out_of_tx_bd;
396         u32 no_active_pkts; /* IRQ when there were no packets to process */
397         u32 active_queue_count;
398 };
399
400 /** emac_rx_bd: EMAC RX Buffer descriptor data structure
401  *
402  * EMAC RX Buffer descriptor data structure
403  */
404 struct emac_rx_bd {
405         int h_next;
406         int buff_ptr;
407         int off_b_len;
408         int mode;
409         struct emac_rx_bd __iomem *next;
410         void *data_ptr;
411         void *buf_token;
412 };
413
414 /** emac_rxch: EMAC RX Channel data structure
415  *
416  * EMAC RX Channel data structure
417  */
418 struct emac_rxch {
419         /* configuration info */
420         u32 num_bd;
421         u32 service_max;
422         u32 buf_size;
423         char mac_addr[6];
424
425         /** CPPI specific */
426         u32 alloc_size;
427         void __iomem *bd_mem;
428         struct emac_rx_bd __iomem *bd_pool_head;
429         struct emac_rx_bd __iomem *active_queue_head;
430         struct emac_rx_bd __iomem *active_queue_tail;
431         u32 queue_active;
432         u32 teardown_pending;
433
434         /* packet and buffer objects */
435         struct emac_netpktobj pkt_queue;
436         struct emac_netbufobj buf_queue;
437
438         /** statistics */
439         u32 proc_count; /* number of times emac_rx_bdproc is called */
440         u32 processed_bd;
441         u32 recycled_bd;
442         u32 out_of_rx_bd;
443         u32 out_of_rx_buffers;
444         u32 queue_reinit;
445         u32 end_of_queue_add;
446         u32 end_of_queue;
447         u32 mis_queued_packets;
448 };
449
450 /* emac_priv: EMAC private data structure
451  *
452  * EMAC adapter private data structure
453  */
454 struct emac_priv {
455         u32 msg_enable;
456         struct net_device *ndev;
457         struct platform_device *pdev;
458         struct napi_struct napi;
459         char mac_addr[6];
460         spinlock_t tx_lock;
461         spinlock_t rx_lock;
462         void __iomem *remap_addr;
463         u32 emac_base_phys;
464         void __iomem *emac_base;
465         void __iomem *ctrl_base;
466         void __iomem *emac_ctrl_ram;
467         u32 ctrl_ram_size;
468         struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
469         struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
470         u32 link; /* 1=link on, 0=link off */
471         u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
472         u32 duplex; /* Link duplex: 0=Half, 1=Full */
473         u32 rx_buf_size;
474         u32 isr_count;
475         u8 rmii_en;
476         u8 version;
477         struct net_device_stats net_dev_stats;
478         u32 mac_hash1;
479         u32 mac_hash2;
480         u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
481         u32 rx_addr_type;
482         /* periodic timer required for MDIO polling */
483         struct timer_list periodic_timer;
484         u32 periodic_ticks;
485         u32 timer_active;
486         u32 phy_mask;
487         /* mii_bus,phy members */
488         struct mii_bus *mii_bus;
489         struct phy_device *phydev;
490         spinlock_t lock;
491 };
492
493 /* clock frequency for EMAC */
494 static struct clk *emac_clk;
495 static unsigned long emac_bus_frequency;
496 static unsigned long mdio_max_freq;
497
498 /* EMAC internal utility function */
499 static inline u32 emac_virt_to_phys(void __iomem *addr)
500 {
501         return (u32 __force) io_v2p(addr);
502 }
503
504 /* Cache macros - Packet buffers would be from skb pool which is cached */
505 #define EMAC_VIRT_NOCACHE(addr) (addr)
506 #define EMAC_CACHE_INVALIDATE(addr, size) \
507         dma_cache_maint((void *)addr, size, DMA_FROM_DEVICE)
508 #define EMAC_CACHE_WRITEBACK(addr, size) \
509         dma_cache_maint((void *)addr, size, DMA_TO_DEVICE)
510 #define EMAC_CACHE_WRITEBACK_INVALIDATE(addr, size) \
511         dma_cache_maint((void *)addr, size, DMA_BIDIRECTIONAL)
512
513 /* DM644x does not have BD's in cached memory - so no cache functions */
514 #define BD_CACHE_INVALIDATE(addr, size)
515 #define BD_CACHE_WRITEBACK(addr, size)
516 #define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
517
518 /* EMAC TX Host Error description strings */
519 static char *emac_txhost_errcodes[16] = {
520         "No error", "SOP error", "Ownership bit not set in SOP buffer",
521         "Zero Next Buffer Descriptor Pointer Without EOP",
522         "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
523         "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
524         "Reserved", "Reserved", "Reserved", "Reserved"
525 };
526
527 /* EMAC RX Host Error description strings */
528 static char *emac_rxhost_errcodes[16] = {
529         "No error", "Reserved", "Ownership bit not set in input buffer",
530         "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
531         "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
532         "Reserved", "Reserved", "Reserved", "Reserved"
533 };
534
535 /* Helper macros */
536 #define emac_read(reg)            ioread32(priv->emac_base + (reg))
537 #define emac_write(reg, val)      iowrite32(val, priv->emac_base + (reg))
538
539 #define emac_ctrl_read(reg)       ioread32((priv->ctrl_base + (reg)))
540 #define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
541
542 #define emac_mdio_read(reg)       ioread32(bus->priv + (reg))
543 #define emac_mdio_write(reg, val) iowrite32(val, (bus->priv + (reg)))
544
545 /**
546  * emac_dump_regs: Dump important EMAC registers to debug terminal
547  * @priv: The DaVinci EMAC private adapter structure
548  *
549  * Executes ethtool set cmd & sets phy mode
550  *
551  */
552 static void emac_dump_regs(struct emac_priv *priv)
553 {
554         struct device *emac_dev = &priv->ndev->dev;
555
556         /* Print important registers in EMAC */
557         dev_info(emac_dev, "EMAC Basic registers\n");
558         dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
559                 emac_ctrl_read(EMAC_CTRL_EWCTL),
560                 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
561         dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
562                 emac_read(EMAC_TXIDVER),
563                 ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
564                 emac_read(EMAC_RXIDVER),
565                 ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
566         dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
567                 "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
568                 emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
569         dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
570                 "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
571                 emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
572         dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
573                 "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
574                 emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
575         dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
576                 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
577         dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
578                 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
579                 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
580         dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
581                 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
582                 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
583         dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
584                 emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
585         dev_info(emac_dev, "EMAC Statistics\n");
586         dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
587                 emac_read(EMAC_RXGOODFRAMES));
588         dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
589                 emac_read(EMAC_RXBCASTFRAMES));
590         dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
591                 emac_read(EMAC_RXMCASTFRAMES));
592         dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
593                 emac_read(EMAC_RXPAUSEFRAMES));
594         dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
595                 emac_read(EMAC_RXCRCERRORS));
596         dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
597                 emac_read(EMAC_RXALIGNCODEERRORS));
598         dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
599                 emac_read(EMAC_RXOVERSIZED));
600         dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
601                 emac_read(EMAC_RXJABBER));
602         dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
603                 emac_read(EMAC_RXUNDERSIZED));
604         dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
605                 emac_read(EMAC_RXFRAGMENTS));
606         dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
607                 emac_read(EMAC_RXFILTERED));
608         dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
609                 emac_read(EMAC_RXQOSFILTERED));
610         dev_info(emac_dev, "EMAC: rx_octets:%d\n",
611                 emac_read(EMAC_RXOCTETS));
612         dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
613                 emac_read(EMAC_TXGOODFRAMES));
614         dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
615                 emac_read(EMAC_TXBCASTFRAMES));
616         dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
617                 emac_read(EMAC_TXMCASTFRAMES));
618         dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
619                 emac_read(EMAC_TXPAUSEFRAMES));
620         dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
621                 emac_read(EMAC_TXDEFERRED));
622         dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
623                 emac_read(EMAC_TXCOLLISION));
624         dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
625                 emac_read(EMAC_TXSINGLECOLL));
626         dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
627                 emac_read(EMAC_TXMULTICOLL));
628         dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
629                 emac_read(EMAC_TXEXCESSIVECOLL));
630         dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
631                 emac_read(EMAC_TXLATECOLL));
632         dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
633                 emac_read(EMAC_TXUNDERRUN));
634         dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
635                 emac_read(EMAC_TXCARRIERSENSE));
636         dev_info(emac_dev, "EMAC: tx_octets:%d\n",
637                 emac_read(EMAC_TXOCTETS));
638         dev_info(emac_dev, "EMAC: net_octets:%d\n",
639                 emac_read(EMAC_NETOCTETS));
640         dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
641                 emac_read(EMAC_RXSOFOVERRUNS));
642         dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
643                 emac_read(EMAC_RXMOFOVERRUNS));
644         dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
645                 emac_read(EMAC_RXDMAOVERRUNS));
646 }
647
648 /*************************************************************************
649  *  EMAC MDIO/Phy Functionality
650  *************************************************************************/
651 /**
652  * emac_get_drvinfo: Get EMAC driver information
653  * @ndev: The DaVinci EMAC network adapter
654  * @info: ethtool info structure containing name and version
655  *
656  * Returns EMAC driver information (name and version)
657  *
658  */
659 static void emac_get_drvinfo(struct net_device *ndev,
660                              struct ethtool_drvinfo *info)
661 {
662         strcpy(info->driver, emac_version_string);
663         strcpy(info->version, EMAC_MODULE_VERSION);
664 }
665
666 /**
667  * emac_get_settings: Get EMAC settings
668  * @ndev: The DaVinci EMAC network adapter
669  * @ecmd: ethtool command
670  *
671  * Executes ethool get command
672  *
673  */
674 static int emac_get_settings(struct net_device *ndev,
675                              struct ethtool_cmd *ecmd)
676 {
677         struct emac_priv *priv = netdev_priv(ndev);
678         if (priv->phy_mask)
679                 return phy_ethtool_gset(priv->phydev, ecmd);
680         else
681                 return -EOPNOTSUPP;
682
683 }
684
685 /**
686  * emac_set_settings: Set EMAC settings
687  * @ndev: The DaVinci EMAC network adapter
688  * @ecmd: ethtool command
689  *
690  * Executes ethool set command
691  *
692  */
693 static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
694 {
695         struct emac_priv *priv = netdev_priv(ndev);
696         if (priv->phy_mask)
697                 return phy_ethtool_sset(priv->phydev, ecmd);
698         else
699                 return -EOPNOTSUPP;
700
701 }
702
703 /**
704  * ethtool_ops: DaVinci EMAC Ethtool structure
705  *
706  * Ethtool support for EMAC adapter
707  *
708  */
709 static const struct ethtool_ops ethtool_ops = {
710         .get_drvinfo = emac_get_drvinfo,
711         .get_settings = emac_get_settings,
712         .set_settings = emac_set_settings,
713         .get_link = ethtool_op_get_link,
714 };
715
716 /**
717  * emac_update_phystatus: Update Phy status
718  * @priv: The DaVinci EMAC private adapter structure
719  *
720  * Updates phy status and takes action for network queue if required
721  * based upon link status
722  *
723  */
724 static void emac_update_phystatus(struct emac_priv *priv)
725 {
726         u32 mac_control;
727         u32 new_duplex;
728         u32 cur_duplex;
729         struct net_device *ndev = priv->ndev;
730
731         mac_control = emac_read(EMAC_MACCONTROL);
732         cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
733                         DUPLEX_FULL : DUPLEX_HALF;
734         if (priv->phy_mask)
735                 new_duplex = priv->phydev->duplex;
736         else
737                 new_duplex = DUPLEX_FULL;
738
739         /* We get called only if link has changed (speed/duplex/status) */
740         if ((priv->link) && (new_duplex != cur_duplex)) {
741                 priv->duplex = new_duplex;
742                 if (DUPLEX_FULL == priv->duplex)
743                         mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
744                 else
745                         mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
746         }
747
748         if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
749                 mac_control = emac_read(EMAC_MACCONTROL);
750                 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
751                                 EMAC_DM646X_MACCONTORL_GIGFORCE);
752         } else {
753                 /* Clear the GIG bit and GIGFORCE bit */
754                 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
755                                         EMAC_DM646X_MACCONTORL_GIG);
756
757                 if (priv->rmii_en && (priv->speed == SPEED_100))
758                         mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
759                 else
760                         mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
761         }
762
763         /* Update mac_control if changed */
764         emac_write(EMAC_MACCONTROL, mac_control);
765
766         if (priv->link) {
767                 /* link ON */
768                 if (!netif_carrier_ok(ndev))
769                         netif_carrier_on(ndev);
770         /* reactivate the transmit queue if it is stopped */
771                 if (netif_running(ndev) && netif_queue_stopped(ndev))
772                         netif_wake_queue(ndev);
773         } else {
774                 /* link OFF */
775                 if (netif_carrier_ok(ndev))
776                         netif_carrier_off(ndev);
777                 if (!netif_queue_stopped(ndev))
778                         netif_stop_queue(ndev);
779         }
780 }
781
782 /**
783  * hash_get: Calculate hash value from mac address
784  * @addr: mac address to delete from hash table
785  *
786  * Calculates hash value from mac address
787  *
788  */
789 static u32 hash_get(u8 *addr)
790 {
791         u32 hash;
792         u8 tmpval;
793         int cnt;
794         hash = 0;
795
796         for (cnt = 0; cnt < 2; cnt++) {
797                 tmpval = *addr++;
798                 hash ^= (tmpval >> 2) ^ (tmpval << 4);
799                 tmpval = *addr++;
800                 hash ^= (tmpval >> 4) ^ (tmpval << 2);
801                 tmpval = *addr++;
802                 hash ^= (tmpval >> 6) ^ (tmpval);
803         }
804
805         return hash & 0x3F;
806 }
807
808 /**
809  * hash_add: Hash function to add mac addr from hash table
810  * @priv: The DaVinci EMAC private adapter structure
811  * mac_addr: mac address to delete from hash table
812  *
813  * Adds mac address to the internal hash table
814  *
815  */
816 static int hash_add(struct emac_priv *priv, u8 *mac_addr)
817 {
818         struct device *emac_dev = &priv->ndev->dev;
819         u32 rc = 0;
820         u32 hash_bit;
821         u32 hash_value = hash_get(mac_addr);
822
823         if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
824                 if (netif_msg_drv(priv)) {
825                         dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
826                                 "Hash %08x, should not be greater than %08x",
827                                 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
828                 }
829                 return -1;
830         }
831
832         /* set the hash bit only if not previously set */
833         if (priv->multicast_hash_cnt[hash_value] == 0) {
834                 rc = 1; /* hash value changed */
835                 if (hash_value < 32) {
836                         hash_bit = BIT(hash_value);
837                         priv->mac_hash1 |= hash_bit;
838                 } else {
839                         hash_bit = BIT((hash_value - 32));
840                         priv->mac_hash2 |= hash_bit;
841                 }
842         }
843
844         /* incr counter for num of mcast addr's mapped to "this" hash bit */
845         ++priv->multicast_hash_cnt[hash_value];
846
847         return rc;
848 }
849
850 /**
851  * hash_del: Hash function to delete mac addr from hash table
852  * @priv: The DaVinci EMAC private adapter structure
853  * mac_addr: mac address to delete from hash table
854  *
855  * Removes mac address from the internal hash table
856  *
857  */
858 static int hash_del(struct emac_priv *priv, u8 *mac_addr)
859 {
860         u32 hash_value;
861         u32 hash_bit;
862
863         hash_value = hash_get(mac_addr);
864         if (priv->multicast_hash_cnt[hash_value] > 0) {
865                 /* dec cntr for num of mcast addr's mapped to this hash bit */
866                 --priv->multicast_hash_cnt[hash_value];
867         }
868
869         /* if counter still > 0, at least one multicast address refers
870          * to this hash bit. so return 0 */
871         if (priv->multicast_hash_cnt[hash_value] > 0)
872                 return 0;
873
874         if (hash_value < 32) {
875                 hash_bit = BIT(hash_value);
876                 priv->mac_hash1 &= ~hash_bit;
877         } else {
878                 hash_bit = BIT((hash_value - 32));
879                 priv->mac_hash2 &= ~hash_bit;
880         }
881
882         /* return 1 to indicate change in mac_hash registers reqd */
883         return 1;
884 }
885
886 /* EMAC multicast operation */
887 #define EMAC_MULTICAST_ADD      0
888 #define EMAC_MULTICAST_DEL      1
889 #define EMAC_ALL_MULTI_SET      2
890 #define EMAC_ALL_MULTI_CLR      3
891
892 /**
893  * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
894  * @priv: The DaVinci EMAC private adapter structure
895  * @action: multicast operation to perform
896  * mac_addr: mac address to set
897  *
898  * Set multicast addresses in EMAC adapter - internal function
899  *
900  */
901 static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
902 {
903         struct device *emac_dev = &priv->ndev->dev;
904         int update = -1;
905
906         switch (action) {
907         case EMAC_MULTICAST_ADD:
908                 update = hash_add(priv, mac_addr);
909                 break;
910         case EMAC_MULTICAST_DEL:
911                 update = hash_del(priv, mac_addr);
912                 break;
913         case EMAC_ALL_MULTI_SET:
914                 update = 1;
915                 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
916                 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
917                 break;
918         case EMAC_ALL_MULTI_CLR:
919                 update = 1;
920                 priv->mac_hash1 = 0;
921                 priv->mac_hash2 = 0;
922                 memset(&(priv->multicast_hash_cnt[0]), 0,
923                 sizeof(priv->multicast_hash_cnt[0]) *
924                        EMAC_NUM_MULTICAST_BITS);
925                 break;
926         default:
927                 if (netif_msg_drv(priv))
928                         dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
929                                 ": bad operation %d", action);
930                 break;
931         }
932
933         /* write to the hardware only if the register status chances */
934         if (update > 0) {
935                 emac_write(EMAC_MACHASH1, priv->mac_hash1);
936                 emac_write(EMAC_MACHASH2, priv->mac_hash2);
937         }
938 }
939
940 /**
941  * emac_dev_mcast_set: Set multicast address in the EMAC adapter
942  * @ndev: The DaVinci EMAC network adapter
943  *
944  * Set multicast addresses in EMAC adapter
945  *
946  */
947 static void emac_dev_mcast_set(struct net_device *ndev)
948 {
949         u32 mbp_enable;
950         struct emac_priv *priv = netdev_priv(ndev);
951
952         mbp_enable = emac_read(EMAC_RXMBPENABLE);
953         if (ndev->flags & IFF_PROMISC) {
954                 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
955                 mbp_enable |= (EMAC_MBP_RXPROMISC);
956         } else {
957                 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
958                 if ((ndev->flags & IFF_ALLMULTI) ||
959                     (ndev->mc_count > EMAC_DEF_MAX_MULTICAST_ADDRESSES)) {
960                         mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
961                         emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
962                 }
963                 if (ndev->mc_count > 0) {
964                         struct dev_mc_list *mc_ptr;
965                         mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
966                         emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
967                         /* program multicast address list into EMAC hardware */
968                         for (mc_ptr = ndev->mc_list; mc_ptr;
969                              mc_ptr = mc_ptr->next) {
970                                 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
971                                                (u8 *)mc_ptr->dmi_addr);
972                         }
973                 } else {
974                         mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
975                         emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
976                 }
977         }
978         /* Set mbp config register */
979         emac_write(EMAC_RXMBPENABLE, mbp_enable);
980 }
981
982 /*************************************************************************
983  *  EMAC Hardware manipulation
984  *************************************************************************/
985
986 /**
987  * emac_int_disable: Disable EMAC module interrupt (from adapter)
988  * @priv: The DaVinci EMAC private adapter structure
989  *
990  * Disable EMAC interrupt on the adapter
991  *
992  */
993 static void emac_int_disable(struct emac_priv *priv)
994 {
995         if (priv->version == EMAC_VERSION_2) {
996                 unsigned long flags;
997
998                 local_irq_save(flags);
999
1000                 /* Program C0_Int_En to zero to turn off
1001                 * interrupts to the CPU */
1002                 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1003                 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1004                 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
1005
1006                 local_irq_restore(flags);
1007
1008         } else {
1009                 /* Set DM644x control registers for interrupt control */
1010                 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1011         }
1012 }
1013
1014 /**
1015  * emac_int_enable: Enable EMAC module interrupt (from adapter)
1016  * @priv: The DaVinci EMAC private adapter structure
1017  *
1018  * Enable EMAC interrupt on the adapter
1019  *
1020  */
1021 static void emac_int_enable(struct emac_priv *priv)
1022 {
1023         if (priv->version == EMAC_VERSION_2) {
1024                 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
1025                 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
1026
1027                 /* In addition to turning on interrupt Enable, we need
1028                  * ack by writing appropriate values to the EOI
1029                  * register */
1030
1031                 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
1032
1033                 /* ack rxen only then a new pulse will be generated */
1034                 emac_write(EMAC_DM646X_MACEOIVECTOR,
1035                         EMAC_DM646X_MAC_EOI_C0_RXEN);
1036
1037                 /* ack txen- only then a new pulse will be generated */
1038                 emac_write(EMAC_DM646X_MACEOIVECTOR,
1039                         EMAC_DM646X_MAC_EOI_C0_TXEN);
1040
1041         } else {
1042                 /* Set DM644x control registers for interrupt control */
1043                 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1044         }
1045 }
1046
1047 /**
1048  * emac_irq: EMAC interrupt handler
1049  * @irq: interrupt number
1050  * @dev_id: EMAC network adapter data structure ptr
1051  *
1052  * EMAC Interrupt handler - we only schedule NAPI and not process any packets
1053  * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
1054  *
1055  * Returns interrupt handled condition
1056  */
1057 static irqreturn_t emac_irq(int irq, void *dev_id)
1058 {
1059         struct net_device *ndev = (struct net_device *)dev_id;
1060         struct emac_priv *priv = netdev_priv(ndev);
1061
1062         ++priv->isr_count;
1063         if (likely(netif_running(priv->ndev))) {
1064                 emac_int_disable(priv);
1065                 napi_schedule(&priv->napi);
1066         } else {
1067                 /* we are closing down, so dont process anything */
1068         }
1069         return IRQ_HANDLED;
1070 }
1071
1072 /** EMAC on-chip buffer descriptor memory
1073  *
1074  * WARNING: Please note that the on chip memory is used for both TX and RX
1075  * buffer descriptor queues and is equally divided between TX and RX desc's
1076  * If the number of TX or RX descriptors change this memory pointers need
1077  * to be adjusted. If external memory is allocated then these pointers can
1078  * pointer to the memory
1079  *
1080  */
1081 #define EMAC_TX_BD_MEM(priv)    ((priv)->emac_ctrl_ram)
1082 #define EMAC_RX_BD_MEM(priv)    ((priv)->emac_ctrl_ram + \
1083                                 (((priv)->ctrl_ram_size) >> 1))
1084
1085 /**
1086  * emac_init_txch: TX channel initialization
1087  * @priv: The DaVinci EMAC private adapter structure
1088  * @ch: RX channel number
1089  *
1090  * Called during device init to setup a TX channel (allocate buffer desc
1091  * create free pool and keep ready for transmission
1092  *
1093  * Returns success(0) or mem alloc failures error code
1094  */
1095 static int emac_init_txch(struct emac_priv *priv, u32 ch)
1096 {
1097         struct device *emac_dev = &priv->ndev->dev;
1098         u32 cnt, bd_size;
1099         void __iomem *mem;
1100         struct emac_tx_bd __iomem *curr_bd;
1101         struct emac_txch *txch = NULL;
1102
1103         txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
1104         if (NULL == txch) {
1105                 dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
1106                 return -ENOMEM;
1107         }
1108         priv->txch[ch] = txch;
1109         txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
1110         txch->active_queue_head = NULL;
1111         txch->active_queue_tail = NULL;
1112         txch->queue_active = 0;
1113         txch->teardown_pending = 0;
1114
1115         /* allocate memory for TX CPPI channel on a 4 byte boundry */
1116         txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
1117                                     GFP_KERNEL);
1118         if (NULL == txch->tx_complete) {
1119                 dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
1120                 kfree(txch);
1121                 return -ENOMEM;
1122         }
1123
1124         /* allocate buffer descriptor pool align every BD on four word
1125          * boundry for future requirements */
1126         bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
1127         txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1128         txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
1129
1130         /* alloc TX BD memory */
1131         txch->bd_mem = EMAC_TX_BD_MEM(priv);
1132         __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1133
1134         /* initialize the BD linked list */
1135         mem = (void __force __iomem *)
1136                         (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
1137         txch->bd_pool_head = NULL;
1138         for (cnt = 0; cnt < txch->num_bd; cnt++) {
1139                 curr_bd = mem + (cnt * bd_size);
1140                 curr_bd->next = txch->bd_pool_head;
1141                 txch->bd_pool_head = curr_bd;
1142         }
1143
1144         /* reset statistics counters */
1145         txch->out_of_tx_bd = 0;
1146         txch->no_active_pkts = 0;
1147         txch->active_queue_count = 0;
1148
1149         return 0;
1150 }
1151
1152 /**
1153  * emac_cleanup_txch: Book-keep function to clean TX channel resources
1154  * @priv: The DaVinci EMAC private adapter structure
1155  * @ch: TX channel number
1156  *
1157  * Called to clean up TX channel resources
1158  *
1159  */
1160 static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
1161 {
1162         struct emac_txch *txch = priv->txch[ch];
1163
1164         if (txch) {
1165                 if (txch->bd_mem)
1166                         txch->bd_mem = NULL;
1167                 kfree(txch->tx_complete);
1168                 kfree(txch);
1169                 priv->txch[ch] = NULL;
1170         }
1171 }
1172
1173 /**
1174  * emac_net_tx_complete: TX packet completion function
1175  * @priv: The DaVinci EMAC private adapter structure
1176  * @net_data_tokens: packet token - skb pointer
1177  * @num_tokens: number of skb's to free
1178  * @ch: TX channel number
1179  *
1180  * Frees the skb once packet is transmitted
1181  *
1182  */
1183 static int emac_net_tx_complete(struct emac_priv *priv,
1184                                 void **net_data_tokens,
1185                                 int num_tokens, u32 ch)
1186 {
1187         u32 cnt;
1188
1189         if (unlikely(num_tokens && netif_queue_stopped(priv->ndev)))
1190                 netif_start_queue(priv->ndev);
1191         for (cnt = 0; cnt < num_tokens; cnt++) {
1192                 struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
1193                 if (skb == NULL)
1194                         continue;
1195                 priv->net_dev_stats.tx_packets++;
1196                 priv->net_dev_stats.tx_bytes += skb->len;
1197                 dev_kfree_skb_any(skb);
1198         }
1199         return 0;
1200 }
1201
1202 /**
1203  * emac_txch_teardown: TX channel teardown
1204  * @priv: The DaVinci EMAC private adapter structure
1205  * @ch: TX channel number
1206  *
1207  * Called to teardown TX channel
1208  *
1209  */
1210 static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
1211 {
1212         struct device *emac_dev = &priv->ndev->dev;
1213         u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1214         struct emac_txch *txch = priv->txch[ch];
1215         struct emac_tx_bd __iomem *curr_bd;
1216
1217         while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1218                EMAC_TEARDOWN_VALUE) {
1219                 /* wait till tx teardown complete */
1220                 cpu_relax(); /* TODO: check if this helps ... */
1221                 --teardown_cnt;
1222                 if (0 == teardown_cnt) {
1223                         dev_err(emac_dev, "EMAC: TX teardown aborted\n");
1224                         break;
1225                 }
1226         }
1227         emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
1228
1229         /* process sent packets and return skb's to upper layer */
1230         if (1 == txch->queue_active) {
1231                 curr_bd = txch->active_queue_head;
1232                 while (curr_bd != NULL) {
1233                         emac_net_tx_complete(priv, (void __force *)
1234                                         &curr_bd->buf_token, 1, ch);
1235                         if (curr_bd != txch->active_queue_tail)
1236                                 curr_bd = curr_bd->next;
1237                         else
1238                                 break;
1239                 }
1240                 txch->bd_pool_head = txch->active_queue_head;
1241                 txch->active_queue_head =
1242                 txch->active_queue_tail = NULL;
1243         }
1244 }
1245
1246 /**
1247  * emac_stop_txch: Stop TX channel operation
1248  * @priv: The DaVinci EMAC private adapter structure
1249  * @ch: TX channel number
1250  *
1251  * Called to stop TX channel operation
1252  *
1253  */
1254 static void emac_stop_txch(struct emac_priv *priv, u32 ch)
1255 {
1256         struct emac_txch *txch = priv->txch[ch];
1257
1258         if (txch) {
1259                 txch->teardown_pending = 1;
1260                 emac_write(EMAC_TXTEARDOWN, 0);
1261                 emac_txch_teardown(priv, ch);
1262                 txch->teardown_pending = 0;
1263                 emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
1264         }
1265 }
1266
1267 /**
1268  * emac_tx_bdproc: TX buffer descriptor (packet) processing
1269  * @priv: The DaVinci EMAC private adapter structure
1270  * @ch: TX channel number to process buffer descriptors for
1271  * @budget: number of packets allowed to process
1272  * @pending: indication to caller that packets are pending to process
1273  *
1274  * Processes TX buffer descriptors after packets are transmitted - checks
1275  * ownership bit on the TX * descriptor and requeues it to free pool & frees
1276  * the SKB buffer. Only "budget" number of packets are processed and
1277  * indication of pending packets provided to the caller
1278  *
1279  * Returns number of packets processed
1280  */
1281 static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1282 {
1283         struct device *emac_dev = &priv->ndev->dev;
1284         unsigned long flags;
1285         u32 frame_status;
1286         u32 pkts_processed = 0;
1287         u32 tx_complete_cnt = 0;
1288         struct emac_tx_bd __iomem *curr_bd;
1289         struct emac_txch *txch = priv->txch[ch];
1290         u32 *tx_complete_ptr = txch->tx_complete;
1291
1292         if (unlikely(1 == txch->teardown_pending)) {
1293                 if (netif_msg_tx_err(priv) && net_ratelimit()) {
1294                         dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
1295                                 "teardown pending\n");
1296                 }
1297                 return 0;  /* dont handle any pkt completions */
1298         }
1299
1300         ++txch->proc_count;
1301         spin_lock_irqsave(&priv->tx_lock, flags);
1302         curr_bd = txch->active_queue_head;
1303         if (NULL == curr_bd) {
1304                 emac_write(EMAC_TXCP(ch),
1305                            emac_virt_to_phys(txch->last_hw_bdprocessed));
1306                 txch->no_active_pkts++;
1307                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1308                 return 0;
1309         }
1310         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1311         frame_status = curr_bd->mode;
1312         while ((curr_bd) &&
1313               ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1314               (pkts_processed < budget)) {
1315                 emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd));
1316                 txch->active_queue_head = curr_bd->next;
1317                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1318                         if (curr_bd->next) {    /* misqueued packet */
1319                                 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1320                                 ++txch->mis_queued_packets;
1321                         } else {
1322                                 txch->queue_active = 0; /* end of queue */
1323                         }
1324                 }
1325                 *tx_complete_ptr = (u32) curr_bd->buf_token;
1326                 ++tx_complete_ptr;
1327                 ++tx_complete_cnt;
1328                 curr_bd->next = txch->bd_pool_head;
1329                 txch->bd_pool_head = curr_bd;
1330                 --txch->active_queue_count;
1331                 pkts_processed++;
1332                 txch->last_hw_bdprocessed = curr_bd;
1333                 curr_bd = txch->active_queue_head;
1334                 if (curr_bd) {
1335                         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1336                         frame_status = curr_bd->mode;
1337                 }
1338         } /* end of pkt processing loop */
1339
1340         emac_net_tx_complete(priv,
1341                              (void *)&txch->tx_complete[0],
1342                              tx_complete_cnt, ch);
1343         spin_unlock_irqrestore(&priv->tx_lock, flags);
1344         return pkts_processed;
1345 }
1346
1347 #define EMAC_ERR_TX_OUT_OF_BD -1
1348
1349 /**
1350  * emac_send: EMAC Transmit function (internal)
1351  * @priv: The DaVinci EMAC private adapter structure
1352  * @pkt: packet pointer (contains skb ptr)
1353  * @ch: TX channel number
1354  *
1355  * Called by the transmit function to queue the packet in EMAC hardware queue
1356  *
1357  * Returns success(0) or error code (typically out of desc's)
1358  */
1359 static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
1360 {
1361         unsigned long flags;
1362         struct emac_tx_bd __iomem *curr_bd;
1363         struct emac_txch *txch;
1364         struct emac_netbufobj *buf_list;
1365
1366         txch = priv->txch[ch];
1367         buf_list = pkt->buf_list;   /* get handle to the buffer array */
1368
1369         /* check packet size and pad if short */
1370         if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
1371                 buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
1372                 pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
1373         }
1374
1375         spin_lock_irqsave(&priv->tx_lock, flags);
1376         curr_bd = txch->bd_pool_head;
1377         if (curr_bd == NULL) {
1378                 txch->out_of_tx_bd++;
1379                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1380                 return EMAC_ERR_TX_OUT_OF_BD;
1381         }
1382
1383         txch->bd_pool_head = curr_bd->next;
1384         curr_bd->buf_token = buf_list->buf_token;
1385         /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
1386         curr_bd->buff_ptr = virt_to_phys(buf_list->data_ptr);
1387         curr_bd->off_b_len = buf_list->length;
1388         curr_bd->h_next = 0;
1389         curr_bd->next = NULL;
1390         curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
1391                          EMAC_CPPI_EOP_BIT | pkt->pkt_length);
1392
1393         /* flush the packet from cache if write back cache is present */
1394         BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1395
1396         /* send the packet */
1397         if (txch->active_queue_head == NULL) {
1398                 txch->active_queue_head = curr_bd;
1399                 txch->active_queue_tail = curr_bd;
1400                 if (1 != txch->queue_active) {
1401                         emac_write(EMAC_TXHDP(ch),
1402                                         emac_virt_to_phys(curr_bd));
1403                         txch->queue_active = 1;
1404                 }
1405                 ++txch->queue_reinit;
1406         } else {
1407                 register struct emac_tx_bd __iomem *tail_bd;
1408                 register u32 frame_status;
1409
1410                 tail_bd = txch->active_queue_tail;
1411                 tail_bd->next = curr_bd;
1412                 txch->active_queue_tail = curr_bd;
1413                 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1414                 tail_bd->h_next = (int)emac_virt_to_phys(curr_bd);
1415                 frame_status = tail_bd->mode;
1416                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1417                         emac_write(EMAC_TXHDP(ch), emac_virt_to_phys(curr_bd));
1418                         frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1419                         tail_bd->mode = frame_status;
1420                         ++txch->end_of_queue_add;
1421                 }
1422         }
1423         txch->active_queue_count++;
1424         spin_unlock_irqrestore(&priv->tx_lock, flags);
1425         return 0;
1426 }
1427
1428 /**
1429  * emac_dev_xmit: EMAC Transmit function
1430  * @skb: SKB pointer
1431  * @ndev: The DaVinci EMAC network adapter
1432  *
1433  * Called by the system to transmit a packet  - we queue the packet in
1434  * EMAC hardware transmit queue
1435  *
1436  * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
1437  */
1438 static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1439 {
1440         struct device *emac_dev = &ndev->dev;
1441         int ret_code;
1442         struct emac_netbufobj tx_buf; /* buffer obj-only single frame support */
1443         struct emac_netpktobj tx_packet;  /* packet object */
1444         struct emac_priv *priv = netdev_priv(ndev);
1445
1446         /* If no link, return */
1447         if (unlikely(!priv->link)) {
1448                 if (netif_msg_tx_err(priv) && net_ratelimit())
1449                         dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
1450                 return NETDEV_TX_BUSY;
1451         }
1452
1453         /* Build the buffer and packet objects - Since only single fragment is
1454          * supported, need not set length and token in both packet & object.
1455          * Doing so for completeness sake & to show that this needs to be done
1456          * in multifragment case
1457          */
1458         tx_packet.buf_list = &tx_buf;
1459         tx_packet.num_bufs = 1; /* only single fragment supported */
1460         tx_packet.pkt_length = skb->len;
1461         tx_packet.pkt_token = (void *)skb;
1462         tx_buf.length = skb->len;
1463         tx_buf.buf_token = (void *)skb;
1464         tx_buf.data_ptr = skb->data;
1465         EMAC_CACHE_WRITEBACK((unsigned long)skb->data, skb->len);
1466         ndev->trans_start = jiffies;
1467         ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
1468         if (unlikely(ret_code != 0)) {
1469                 if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
1470                         if (netif_msg_tx_err(priv) && net_ratelimit())
1471                                 dev_err(emac_dev, "DaVinci EMAC: xmit() fatal"\
1472                                         " err. Out of TX BD's");
1473                         netif_stop_queue(priv->ndev);
1474                 }
1475                 priv->net_dev_stats.tx_dropped++;
1476                 return NETDEV_TX_BUSY;
1477         }
1478
1479         return NETDEV_TX_OK;
1480 }
1481
1482 /**
1483  * emac_dev_tx_timeout: EMAC Transmit timeout function
1484  * @ndev: The DaVinci EMAC network adapter
1485  *
1486  * Called when system detects that a skb timeout period has expired
1487  * potentially due to a fault in the adapter in not being able to send
1488  * it out on the wire. We teardown the TX channel assuming a hardware
1489  * error and re-initialize the TX channel for hardware operation
1490  *
1491  */
1492 static void emac_dev_tx_timeout(struct net_device *ndev)
1493 {
1494         struct emac_priv *priv = netdev_priv(ndev);
1495         struct device *emac_dev = &ndev->dev;
1496
1497         if (netif_msg_tx_err(priv))
1498                 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1499
1500         priv->net_dev_stats.tx_errors++;
1501         emac_int_disable(priv);
1502         emac_stop_txch(priv, EMAC_DEF_TX_CH);
1503         emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
1504         emac_init_txch(priv, EMAC_DEF_TX_CH);
1505         emac_write(EMAC_TXHDP(0), 0);
1506         emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
1507         emac_int_enable(priv);
1508 }
1509
1510 /**
1511  * emac_net_alloc_rx_buf: Allocate a skb for RX
1512  * @priv: The DaVinci EMAC private adapter structure
1513  * @buf_size: size of SKB data buffer to allocate
1514  * @data_token: data token returned (skb handle for storing in buffer desc)
1515  * @ch: RX channel number
1516  *
1517  * Called during RX channel setup - allocates skb buffer of required size
1518  * and provides the skb handle and allocated buffer data pointer to caller
1519  *
1520  * Returns skb data pointer or 0 on failure to alloc skb
1521  */
1522 static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
1523                 void **data_token, u32 ch)
1524 {
1525         struct net_device *ndev = priv->ndev;
1526         struct device *emac_dev = &ndev->dev;
1527         struct sk_buff *p_skb;
1528
1529         p_skb = dev_alloc_skb(buf_size);
1530         if (unlikely(NULL == p_skb)) {
1531                 if (netif_msg_rx_err(priv) && net_ratelimit())
1532                         dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
1533                 return NULL;
1534         }
1535
1536         /* set device pointer in skb and reserve space for extra bytes */
1537         p_skb->dev = ndev;
1538         skb_reserve(p_skb, NET_IP_ALIGN);
1539         *data_token = (void *) p_skb;
1540         EMAC_CACHE_WRITEBACK_INVALIDATE((unsigned long)p_skb->data, buf_size);
1541         return p_skb->data;
1542 }
1543
1544 /**
1545  * emac_init_rxch: RX channel initialization
1546  * @priv: The DaVinci EMAC private adapter structure
1547  * @ch: RX channel number
1548  * @param: mac address for RX channel
1549  *
1550  * Called during device init to setup a RX channel (allocate buffers and
1551  * buffer descriptors, create queue and keep ready for reception
1552  *
1553  * Returns success(0) or mem alloc failures error code
1554  */
1555 static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
1556 {
1557         struct device *emac_dev = &priv->ndev->dev;
1558         u32 cnt, bd_size;
1559         void __iomem *mem;
1560         struct emac_rx_bd __iomem *curr_bd;
1561         struct emac_rxch *rxch = NULL;
1562
1563         rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
1564         if (NULL == rxch) {
1565                 dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
1566                 return -ENOMEM;
1567         }
1568         priv->rxch[ch] = rxch;
1569         rxch->buf_size = priv->rx_buf_size;
1570         rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
1571         rxch->queue_active = 0;
1572         rxch->teardown_pending = 0;
1573
1574         /* save mac address */
1575         for (cnt = 0; cnt < 6; cnt++)
1576                 rxch->mac_addr[cnt] = param[cnt];
1577
1578         /* allocate buffer descriptor pool align every BD on four word
1579          * boundry for future requirements */
1580         bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
1581         rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1582         rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
1583         rxch->bd_mem = EMAC_RX_BD_MEM(priv);
1584         __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
1585         rxch->pkt_queue.buf_list = &rxch->buf_queue;
1586
1587         /* allocate RX buffer and initialize the BD linked list */
1588         mem = (void __force __iomem *)
1589                         (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
1590         rxch->active_queue_head = NULL;
1591         rxch->active_queue_tail = mem;
1592         for (cnt = 0; cnt < rxch->num_bd; cnt++) {
1593                 curr_bd = mem + (cnt * bd_size);
1594                 /* for future use the last parameter contains the BD ptr */
1595                 curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
1596                                     rxch->buf_size,
1597                                     (void __force **)&curr_bd->buf_token,
1598                                     EMAC_DEF_RX_CH);
1599                 if (curr_bd->data_ptr == NULL) {
1600                         dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
1601                                 "failed for ch %d\n", ch);
1602                         kfree(rxch);
1603                         return -ENOMEM;
1604                 }
1605
1606                 /* populate the hardware descriptor */
1607                 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
1608                 /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
1609                 curr_bd->buff_ptr = virt_to_phys(curr_bd->data_ptr);
1610                 curr_bd->off_b_len = rxch->buf_size;
1611                 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1612
1613                 /* write back to hardware memory */
1614                 BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
1615                                               EMAC_BD_LENGTH_FOR_CACHE);
1616                 curr_bd->next = rxch->active_queue_head;
1617                 rxch->active_queue_head = curr_bd;
1618         }
1619
1620         /* At this point rxCppi->activeQueueHead points to the first
1621            RX BD ready to be given to RX HDP and rxch->active_queue_tail
1622            points to the last RX BD
1623          */
1624         return 0;
1625 }
1626
1627 /**
1628  * emac_rxch_teardown: RX channel teardown
1629  * @priv: The DaVinci EMAC private adapter structure
1630  * @ch: RX channel number
1631  *
1632  * Called during device stop to teardown RX channel
1633  *
1634  */
1635 static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
1636 {
1637         struct device *emac_dev = &priv->ndev->dev;
1638         u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1639
1640         while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1641                EMAC_TEARDOWN_VALUE) {
1642                 /* wait till tx teardown complete */
1643                 cpu_relax(); /* TODO: check if this helps ... */
1644                 --teardown_cnt;
1645                 if (0 == teardown_cnt) {
1646                         dev_err(emac_dev, "EMAC: RX teardown aborted\n");
1647                         break;
1648                 }
1649         }
1650         emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
1651 }
1652
1653 /**
1654  * emac_stop_rxch: Stop RX channel operation
1655  * @priv: The DaVinci EMAC private adapter structure
1656  * @ch: RX channel number
1657  *
1658  * Called during device stop to stop RX channel operation
1659  *
1660  */
1661 static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
1662 {
1663         struct emac_rxch *rxch = priv->rxch[ch];
1664
1665         if (rxch) {
1666                 rxch->teardown_pending = 1;
1667                 emac_write(EMAC_RXTEARDOWN, ch);
1668                 /* wait for teardown complete */
1669                 emac_rxch_teardown(priv, ch);
1670                 rxch->teardown_pending = 0;
1671                 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1672         }
1673 }
1674
1675 /**
1676  * emac_cleanup_rxch: Book-keep function to clean RX channel resources
1677  * @priv: The DaVinci EMAC private adapter structure
1678  * @ch: RX channel number
1679  *
1680  * Called during device stop to clean up RX channel resources
1681  *
1682  */
1683 static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
1684 {
1685         struct emac_rxch *rxch = priv->rxch[ch];
1686         struct emac_rx_bd __iomem *curr_bd;
1687
1688         if (rxch) {
1689                 /* free the receive buffers previously allocated */
1690                 curr_bd = rxch->active_queue_head;
1691                 while (curr_bd) {
1692                         if (curr_bd->buf_token) {
1693                                 dev_kfree_skb_any((struct sk_buff *)\
1694                                                   curr_bd->buf_token);
1695                         }
1696                         curr_bd = curr_bd->next;
1697                 }
1698                 if (rxch->bd_mem)
1699                         rxch->bd_mem = NULL;
1700                 kfree(rxch);
1701                 priv->rxch[ch] = NULL;
1702         }
1703 }
1704
1705 /**
1706  * emac_set_type0addr: Set EMAC Type0 mac address
1707  * @priv: The DaVinci EMAC private adapter structure
1708  * @ch: RX channel number
1709  * @mac_addr: MAC address to set in device
1710  *
1711  * Called internally to set Type0 mac address of the adapter (Device)
1712  *
1713  * Returns success (0) or appropriate error code (none as of now)
1714  */
1715 static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1716 {
1717         u32 val;
1718         val = ((mac_addr[5] << 8) | (mac_addr[4]));
1719         emac_write(EMAC_MACSRCADDRLO, val);
1720
1721         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1722                (mac_addr[1] << 8) | (mac_addr[0]));
1723         emac_write(EMAC_MACSRCADDRHI, val);
1724         val = emac_read(EMAC_RXUNICASTSET);
1725         val |= BIT(ch);
1726         emac_write(EMAC_RXUNICASTSET, val);
1727         val = emac_read(EMAC_RXUNICASTCLEAR);
1728         val &= ~BIT(ch);
1729         emac_write(EMAC_RXUNICASTCLEAR, val);
1730 }
1731
1732 /**
1733  * emac_set_type1addr: Set EMAC Type1 mac address
1734  * @priv: The DaVinci EMAC private adapter structure
1735  * @ch: RX channel number
1736  * @mac_addr: MAC address to set in device
1737  *
1738  * Called internally to set Type1 mac address of the adapter (Device)
1739  *
1740  * Returns success (0) or appropriate error code (none as of now)
1741  */
1742 static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1743 {
1744         u32 val;
1745         emac_write(EMAC_MACINDEX, ch);
1746         val = ((mac_addr[5] << 8) | mac_addr[4]);
1747         emac_write(EMAC_MACADDRLO, val);
1748         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1749                (mac_addr[1] << 8) | (mac_addr[0]));
1750         emac_write(EMAC_MACADDRHI, val);
1751         emac_set_type0addr(priv, ch, mac_addr);
1752 }
1753
1754 /**
1755  * emac_set_type2addr: Set EMAC Type2 mac address
1756  * @priv: The DaVinci EMAC private adapter structure
1757  * @ch: RX channel number
1758  * @mac_addr: MAC address to set in device
1759  * @index: index into RX address entries
1760  * @match: match parameter for RX address matching logic
1761  *
1762  * Called internally to set Type2 mac address of the adapter (Device)
1763  *
1764  * Returns success (0) or appropriate error code (none as of now)
1765  */
1766 static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1767                                char *mac_addr, int index, int match)
1768 {
1769         u32 val;
1770         emac_write(EMAC_MACINDEX, index);
1771         val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1772                (mac_addr[1] << 8) | (mac_addr[0]));
1773         emac_write(EMAC_MACADDRHI, val);
1774         val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1775                (match << 19) | BIT(20));
1776         emac_write(EMAC_MACADDRLO, val);
1777         emac_set_type0addr(priv, ch, mac_addr);
1778 }
1779
1780 /**
1781  * emac_setmac: Set mac address in the adapter (internal function)
1782  * @priv: The DaVinci EMAC private adapter structure
1783  * @ch: RX channel number
1784  * @mac_addr: MAC address to set in device
1785  *
1786  * Called internally to set the mac address of the adapter (Device)
1787  *
1788  * Returns success (0) or appropriate error code (none as of now)
1789  */
1790 static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1791 {
1792         struct device *emac_dev = &priv->ndev->dev;
1793
1794         if (priv->rx_addr_type == 0) {
1795                 emac_set_type0addr(priv, ch, mac_addr);
1796         } else if (priv->rx_addr_type == 1) {
1797                 u32 cnt;
1798                 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1799                         emac_set_type1addr(priv, ch, mac_addr);
1800         } else if (priv->rx_addr_type == 2) {
1801                 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1802                 emac_set_type0addr(priv, ch, mac_addr);
1803         } else {
1804                 if (netif_msg_drv(priv))
1805                         dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1806         }
1807 }
1808
1809 /**
1810  * emac_dev_setmac_addr: Set mac address in the adapter
1811  * @ndev: The DaVinci EMAC network adapter
1812  * @addr: MAC address to set in device
1813  *
1814  * Called by the system to set the mac address of the adapter (Device)
1815  *
1816  * Returns success (0) or appropriate error code (none as of now)
1817  */
1818 static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1819 {
1820         struct emac_priv *priv = netdev_priv(ndev);
1821         struct emac_rxch *rxch = priv->rxch[EMAC_DEF_RX_CH];
1822         struct device *emac_dev = &priv->ndev->dev;
1823         struct sockaddr *sa = addr;
1824
1825         if (!is_valid_ether_addr(sa->sa_data))
1826                 return -EINVAL;
1827
1828         /* Store mac addr in priv and rx channel and set it in EMAC hw */
1829         memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
1830         memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
1831
1832         /* If the interface is down - rxch is NULL. */
1833         /* MAC address is configured only after the interface is enabled. */
1834         if (netif_running(ndev)) {
1835                 memcpy(rxch->mac_addr, sa->sa_data, ndev->addr_len);
1836                 emac_setmac(priv, EMAC_DEF_RX_CH, rxch->mac_addr);
1837         }
1838
1839         if (netif_msg_drv(priv))
1840                 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1841                                         priv->mac_addr);
1842
1843         return 0;
1844 }
1845
1846 /**
1847  * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
1848  * @priv: The DaVinci EMAC private adapter structure
1849  * @ch: RX channel number to process buffer descriptors for
1850  * @curr_bd: current buffer descriptor
1851  * @buffer: buffer pointer for descriptor
1852  * @buf_token: buffer token (stores skb information)
1853  *
1854  * Prepares the recycled buffer descriptor and addes it to hardware
1855  * receive queue - if queue empty this descriptor becomes the head
1856  * else addes the descriptor to end of queue
1857  *
1858  */
1859 static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
1860                 struct emac_rx_bd __iomem *curr_bd,
1861                 char *buffer, void *buf_token)
1862 {
1863         struct emac_rxch *rxch = priv->rxch[ch];
1864
1865         /* populate the hardware descriptor */
1866         curr_bd->h_next = 0;
1867         /* FIXME buff_ptr = dma_map_single(... buffer ...) */
1868         curr_bd->buff_ptr = virt_to_phys(buffer);
1869         curr_bd->off_b_len = rxch->buf_size;
1870         curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1871         curr_bd->next = NULL;
1872         curr_bd->data_ptr = buffer;
1873         curr_bd->buf_token = buf_token;
1874
1875         /* write back  */
1876         BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1877         if (rxch->active_queue_head == NULL) {
1878                 rxch->active_queue_head = curr_bd;
1879                 rxch->active_queue_tail = curr_bd;
1880                 if (0 != rxch->queue_active) {
1881                         emac_write(EMAC_RXHDP(ch),
1882                                    emac_virt_to_phys(rxch->active_queue_head));
1883                         rxch->queue_active = 1;
1884                 }
1885         } else {
1886                 struct emac_rx_bd __iomem *tail_bd;
1887                 u32 frame_status;
1888
1889                 tail_bd = rxch->active_queue_tail;
1890                 rxch->active_queue_tail = curr_bd;
1891                 tail_bd->next = curr_bd;
1892                 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1893                 tail_bd->h_next = emac_virt_to_phys(curr_bd);
1894                 frame_status = tail_bd->mode;
1895                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1896                         emac_write(EMAC_RXHDP(ch),
1897                                         emac_virt_to_phys(curr_bd));
1898                         frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1899                         tail_bd->mode = frame_status;
1900                         ++rxch->end_of_queue_add;
1901                 }
1902         }
1903         ++rxch->recycled_bd;
1904 }
1905
1906 /**
1907  * emac_net_rx_cb: Prepares packet and sends to upper layer
1908  * @priv: The DaVinci EMAC private adapter structure
1909  * @net_pkt_list: Network packet list (received packets)
1910  *
1911  * Invalidates packet buffer memory and sends the received packet to upper
1912  * layer
1913  *
1914  * Returns success or appropriate error code (none as of now)
1915  */
1916 static int emac_net_rx_cb(struct emac_priv *priv,
1917                           struct emac_netpktobj *net_pkt_list)
1918 {
1919         struct sk_buff *p_skb;
1920         p_skb = (struct sk_buff *)net_pkt_list->pkt_token;
1921         /* set length of packet */
1922         skb_put(p_skb, net_pkt_list->pkt_length);
1923         EMAC_CACHE_INVALIDATE((unsigned long)p_skb->data, p_skb->len);
1924         p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
1925         netif_receive_skb(p_skb);
1926         priv->net_dev_stats.rx_bytes += net_pkt_list->pkt_length;
1927         priv->net_dev_stats.rx_packets++;
1928         return 0;
1929 }
1930
1931 /**
1932  * emac_rx_bdproc: RX buffer descriptor (packet) processing
1933  * @priv: The DaVinci EMAC private adapter structure
1934  * @ch: RX channel number to process buffer descriptors for
1935  * @budget: number of packets allowed to process
1936  * @pending: indication to caller that packets are pending to process
1937  *
1938  * Processes RX buffer descriptors - checks ownership bit on the RX buffer
1939  * descriptor, sends the receive packet to upper layer, allocates a new SKB
1940  * and recycles the buffer descriptor (requeues it in hardware RX queue).
1941  * Only "budget" number of packets are processed and indication of pending
1942  * packets provided to the caller.
1943  *
1944  * Returns number of packets processed (and indication of pending packets)
1945  */
1946 static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1947 {
1948         unsigned long flags;
1949         u32 frame_status;
1950         u32 pkts_processed = 0;
1951         char *new_buffer;
1952         struct emac_rx_bd __iomem *curr_bd;
1953         struct emac_rx_bd __iomem *last_bd;
1954         struct emac_netpktobj *curr_pkt, pkt_obj;
1955         struct emac_netbufobj buf_obj;
1956         struct emac_netbufobj *rx_buf_obj;
1957         void *new_buf_token;
1958         struct emac_rxch *rxch = priv->rxch[ch];
1959
1960         if (unlikely(1 == rxch->teardown_pending))
1961                 return 0;
1962         ++rxch->proc_count;
1963         spin_lock_irqsave(&priv->rx_lock, flags);
1964         pkt_obj.buf_list = &buf_obj;
1965         curr_pkt = &pkt_obj;
1966         curr_bd = rxch->active_queue_head;
1967         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1968         frame_status = curr_bd->mode;
1969
1970         while ((curr_bd) &&
1971                ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1972                (pkts_processed < budget)) {
1973
1974                 new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
1975                                         &new_buf_token, EMAC_DEF_RX_CH);
1976                 if (unlikely(NULL == new_buffer)) {
1977                         ++rxch->out_of_rx_buffers;
1978                         goto end_emac_rx_bdproc;
1979                 }
1980
1981                 /* populate received packet data structure */
1982                 rx_buf_obj = &curr_pkt->buf_list[0];
1983                 rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
1984                 rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
1985                 rx_buf_obj->buf_token = curr_bd->buf_token;
1986                 curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
1987                 curr_pkt->num_bufs = 1;
1988                 curr_pkt->pkt_length =
1989                         (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
1990                 emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd));
1991                 ++rxch->processed_bd;
1992                 last_bd = curr_bd;
1993                 curr_bd = last_bd->next;
1994                 rxch->active_queue_head = curr_bd;
1995
1996                 /* check if end of RX queue ? */
1997                 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1998                         if (curr_bd) {
1999                                 ++rxch->mis_queued_packets;
2000                                 emac_write(EMAC_RXHDP(ch),
2001                                            emac_virt_to_phys(curr_bd));
2002                         } else {
2003                                 ++rxch->end_of_queue;
2004                                 rxch->queue_active = 0;
2005                         }
2006                 }
2007
2008                 /* recycle BD */
2009                 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2010                                        new_buf_token);
2011
2012                 /* return the packet to the user - BD ptr passed in
2013                  * last parameter for potential *future* use */
2014                 spin_unlock_irqrestore(&priv->rx_lock, flags);
2015                 emac_net_rx_cb(priv, curr_pkt);
2016                 spin_lock_irqsave(&priv->rx_lock, flags);
2017                 curr_bd = rxch->active_queue_head;
2018                 if (curr_bd) {
2019                         BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2020                         frame_status = curr_bd->mode;
2021                 }
2022                 ++pkts_processed;
2023         }
2024
2025 end_emac_rx_bdproc:
2026         spin_unlock_irqrestore(&priv->rx_lock, flags);
2027         return pkts_processed;
2028 }
2029
2030 /**
2031  * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
2032  * @priv: The DaVinci EMAC private adapter structure
2033  *
2034  * Enables EMAC hardware for packet processing - enables PHY, enables RX
2035  * for packet reception and enables device interrupts and then NAPI
2036  *
2037  * Returns success (0) or appropriate error code (none right now)
2038  */
2039 static int emac_hw_enable(struct emac_priv *priv)
2040 {
2041         u32 ch, val, mbp_enable, mac_control;
2042
2043         /* Soft reset */
2044         emac_write(EMAC_SOFTRESET, 1);
2045         while (emac_read(EMAC_SOFTRESET))
2046                 cpu_relax();
2047
2048         /* Disable interrupt & Set pacing for more interrupts initially */
2049         emac_int_disable(priv);
2050
2051         /* Full duplex enable bit set when auto negotiation happens */
2052         mac_control =
2053                 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
2054                 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
2055                 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
2056                 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
2057         emac_write(EMAC_MACCONTROL, mac_control);
2058
2059         mbp_enable =
2060                 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
2061                 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
2062                  ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
2063                  ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
2064                  ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
2065                  ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
2066                  ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
2067                  ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
2068                         EMAC_RXMBP_PROMCH_SHIFT) |
2069                  ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
2070                  ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
2071                         EMAC_RXMBP_BROADCH_SHIFT) |
2072                  ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
2073                  ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
2074                         EMAC_RXMBP_MULTICH_SHIFT));
2075         emac_write(EMAC_RXMBPENABLE, mbp_enable);
2076         emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
2077                                    EMAC_RX_MAX_LEN_MASK));
2078         emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
2079                                          EMAC_RX_BUFFER_OFFSET_MASK));
2080         emac_write(EMAC_RXFILTERLOWTHRESH, 0);
2081         emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
2082         priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
2083
2084         val = emac_read(EMAC_TXCONTROL);
2085         val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
2086         emac_write(EMAC_TXCONTROL, val);
2087         val = emac_read(EMAC_RXCONTROL);
2088         val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
2089         emac_write(EMAC_RXCONTROL, val);
2090         emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
2091
2092         for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
2093                 emac_write(EMAC_TXHDP(ch), 0);
2094                 emac_write(EMAC_TXINTMASKSET, BIT(ch));
2095         }
2096         for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
2097                 struct emac_rxch *rxch = priv->rxch[ch];
2098                 emac_setmac(priv, ch, rxch->mac_addr);
2099                 emac_write(EMAC_RXINTMASKSET, BIT(ch));
2100                 rxch->queue_active = 1;
2101                 emac_write(EMAC_RXHDP(ch),
2102                            emac_virt_to_phys(rxch->active_queue_head));
2103         }
2104
2105         /* Enable MII */
2106         val = emac_read(EMAC_MACCONTROL);
2107         val |= (EMAC_MACCONTROL_GMIIEN);
2108         emac_write(EMAC_MACCONTROL, val);
2109
2110         /* Enable NAPI and interrupts */
2111         napi_enable(&priv->napi);
2112         emac_int_enable(priv);
2113         return 0;
2114
2115 }
2116
2117 /**
2118  * emac_poll: EMAC NAPI Poll function
2119  * @ndev: The DaVinci EMAC network adapter
2120  * @budget: Number of receive packets to process (as told by NAPI layer)
2121  *
2122  * NAPI Poll function implemented to process packets as per budget. We check
2123  * the type of interrupt on the device and accordingly call the TX or RX
2124  * packet processing functions. We follow the budget for RX processing and
2125  * also put a cap on number of TX pkts processed through config param. The
2126  * NAPI schedule function is called if more packets pending.
2127  *
2128  * Returns number of packets received (in most cases; else TX pkts - rarely)
2129  */
2130 static int emac_poll(struct napi_struct *napi, int budget)
2131 {
2132         unsigned int mask;
2133         struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
2134         struct net_device *ndev = priv->ndev;
2135         struct device *emac_dev = &ndev->dev;
2136         u32 status = 0;
2137         u32 num_pkts = 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, ndev);
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         u32 mac_control;
2548         u32 stats_clear_mask;
2549
2550         /* update emac hardware stats and reset the registers*/
2551
2552         mac_control = emac_read(EMAC_MACCONTROL);
2553
2554         if (mac_control & EMAC_MACCONTROL_GMIIEN)
2555                 stats_clear_mask = EMAC_STATS_CLR_MASK;
2556         else
2557                 stats_clear_mask = 0;
2558
2559         priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
2560         emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
2561
2562         priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
2563                                            emac_read(EMAC_TXSINGLECOLL) +
2564                                            emac_read(EMAC_TXMULTICOLL));
2565         emac_write(EMAC_TXCOLLISION, stats_clear_mask);
2566         emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
2567         emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
2568
2569         priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
2570                                                 emac_read(EMAC_RXJABBER) +
2571                                                 emac_read(EMAC_RXUNDERSIZED));
2572         emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
2573         emac_write(EMAC_RXJABBER, stats_clear_mask);
2574         emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
2575
2576         priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
2577                                                emac_read(EMAC_RXMOFOVERRUNS));
2578         emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
2579         emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
2580
2581         priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
2582         emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
2583
2584         priv->net_dev_stats.tx_carrier_errors +=
2585                 emac_read(EMAC_TXCARRIERSENSE);
2586         emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
2587
2588         priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
2589         emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
2590
2591         return &priv->net_dev_stats;
2592 }
2593
2594 static const struct net_device_ops emac_netdev_ops = {
2595         .ndo_open               = emac_dev_open,
2596         .ndo_stop               = emac_dev_stop,
2597         .ndo_start_xmit         = emac_dev_xmit,
2598         .ndo_set_multicast_list = emac_dev_mcast_set,
2599         .ndo_set_mac_address    = emac_dev_setmac_addr,
2600         .ndo_do_ioctl           = emac_devioctl,
2601         .ndo_tx_timeout         = emac_dev_tx_timeout,
2602         .ndo_get_stats          = emac_dev_getnetstats,
2603 #ifdef CONFIG_NET_POLL_CONTROLLER
2604         .ndo_poll_controller    = emac_poll_controller,
2605 #endif
2606 };
2607
2608 /**
2609  * davinci_emac_probe: EMAC device probe
2610  * @pdev: The DaVinci EMAC device that we are removing
2611  *
2612  * Called when probing for emac devicesr. We get details of instances and
2613  * resource information from platform init and register a network device
2614  * and allocate resources necessary for driver to perform
2615  */
2616 static int __devinit davinci_emac_probe(struct platform_device *pdev)
2617 {
2618         int rc = 0;
2619         struct resource *res;
2620         struct net_device *ndev;
2621         struct emac_priv *priv;
2622         unsigned long size;
2623         struct emac_platform_data *pdata;
2624         struct device *emac_dev;
2625
2626         /* obtain emac clock from kernel */
2627         emac_clk = clk_get(&pdev->dev, NULL);
2628         if (IS_ERR(emac_clk)) {
2629                 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2630                 return -EBUSY;
2631         }
2632         emac_bus_frequency = clk_get_rate(emac_clk);
2633         /* TODO: Probe PHY here if possible */
2634
2635         ndev = alloc_etherdev(sizeof(struct emac_priv));
2636         if (!ndev) {
2637                 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2638                 clk_put(emac_clk);
2639                 return -ENOMEM;
2640         }
2641
2642         platform_set_drvdata(pdev, ndev);
2643         priv = netdev_priv(ndev);
2644         priv->pdev = pdev;
2645         priv->ndev = ndev;
2646         priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2647
2648         spin_lock_init(&priv->tx_lock);
2649         spin_lock_init(&priv->rx_lock);
2650         spin_lock_init(&priv->lock);
2651
2652         pdata = pdev->dev.platform_data;
2653         if (!pdata) {
2654                 printk(KERN_ERR "DaVinci EMAC: No platfrom data\n");
2655                 return -ENODEV;
2656         }
2657
2658         /* MAC addr and PHY mask , RMII enable info from platform_data */
2659         memcpy(priv->mac_addr, pdata->mac_addr, 6);
2660         priv->phy_mask = pdata->phy_mask;
2661         priv->rmii_en = pdata->rmii_en;
2662         priv->version = pdata->version;
2663         emac_dev = &ndev->dev;
2664         /* Get EMAC platform data */
2665         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2666         if (!res) {
2667                 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2668                 rc = -ENOENT;
2669                 goto probe_quit;
2670         }
2671
2672         priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2673         size = res->end - res->start + 1;
2674         if (!request_mem_region(res->start, size, ndev->name)) {
2675                 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() \
2676                                          for regs\n");
2677                 rc = -ENXIO;
2678                 goto probe_quit;
2679         }
2680
2681         priv->remap_addr = ioremap(res->start, size);
2682         if (!priv->remap_addr) {
2683                 dev_err(emac_dev, "Unable to map IO\n");
2684                 rc = -ENOMEM;
2685                 release_mem_region(res->start, size);
2686                 goto probe_quit;
2687         }
2688         priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2689         ndev->base_addr = (unsigned long)priv->remap_addr;
2690
2691         priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2692         priv->ctrl_ram_size = pdata->ctrl_ram_size;
2693         priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2694
2695         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2696         if (!res) {
2697                 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2698                 rc = -ENOENT;
2699                 goto no_irq_res;
2700         }
2701         ndev->irq = res->start;
2702
2703         if (!is_valid_ether_addr(priv->mac_addr)) {
2704                 /* Use random MAC if none passed */
2705                 random_ether_addr(priv->mac_addr);
2706                 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2707                                 __func__, priv->mac_addr);
2708         }
2709
2710         ndev->netdev_ops = &emac_netdev_ops;
2711         SET_ETHTOOL_OPS(ndev, &ethtool_ops);
2712         netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2713
2714         /* register the network device */
2715         SET_NETDEV_DEV(ndev, &pdev->dev);
2716         rc = register_netdev(ndev);
2717         if (rc) {
2718                 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2719                 rc = -ENODEV;
2720                 goto netdev_reg_err;
2721         }
2722
2723         clk_enable(emac_clk);
2724
2725         /* MII/Phy intialisation, mdio bus registration */
2726         emac_mii = mdiobus_alloc();
2727         if (emac_mii == NULL) {
2728                 dev_err(emac_dev, "DaVinci EMAC: Error allocating mii_bus\n");
2729                 rc = -ENOMEM;
2730                 goto mdio_alloc_err;
2731         }
2732
2733         priv->mii_bus = emac_mii;
2734         emac_mii->name  = "emac-mii",
2735         emac_mii->read  = emac_mii_read,
2736         emac_mii->write = emac_mii_write,
2737         emac_mii->reset = emac_mii_reset,
2738         emac_mii->irq   = mii_irqs,
2739         emac_mii->phy_mask = ~(priv->phy_mask);
2740         emac_mii->parent = &pdev->dev;
2741         emac_mii->priv = priv->remap_addr + pdata->mdio_reg_offset;
2742         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%x", priv->pdev->id);
2743         mdio_max_freq = pdata->mdio_max_freq;
2744         emac_mii->reset(emac_mii);
2745
2746         /* Register the MII bus */
2747         rc = mdiobus_register(emac_mii);
2748         if (rc)
2749                 goto mdiobus_quit;
2750
2751         if (netif_msg_probe(priv)) {
2752                 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2753                            "(regs: %p, irq: %d)\n",
2754                            (void *)priv->emac_base_phys, ndev->irq);
2755         }
2756         return 0;
2757
2758 mdiobus_quit:
2759         mdiobus_free(emac_mii);
2760
2761 netdev_reg_err:
2762 mdio_alloc_err:
2763 no_irq_res:
2764         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2765         release_mem_region(res->start, res->end - res->start + 1);
2766         iounmap(priv->remap_addr);
2767
2768 probe_quit:
2769         clk_put(emac_clk);
2770         free_netdev(ndev);
2771         return rc;
2772 }
2773
2774 /**
2775  * davinci_emac_remove: EMAC device remove
2776  * @pdev: The DaVinci EMAC device that we are removing
2777  *
2778  * Called when removing the device driver. We disable clock usage and release
2779  * the resources taken up by the driver and unregister network device
2780  */
2781 static int __devexit davinci_emac_remove(struct platform_device *pdev)
2782 {
2783         struct resource *res;
2784         struct net_device *ndev = platform_get_drvdata(pdev);
2785         struct emac_priv *priv = netdev_priv(ndev);
2786
2787         dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2788
2789         platform_set_drvdata(pdev, NULL);
2790         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2791         mdiobus_unregister(priv->mii_bus);
2792         mdiobus_free(priv->mii_bus);
2793
2794         release_mem_region(res->start, res->end - res->start + 1);
2795
2796         unregister_netdev(ndev);
2797         free_netdev(ndev);
2798         iounmap(priv->remap_addr);
2799
2800         clk_disable(emac_clk);
2801         clk_put(emac_clk);
2802
2803         return 0;
2804 }
2805
2806 /**
2807  * davinci_emac_driver: EMAC platform driver structure
2808  *
2809  * We implement only probe and remove functions - suspend/resume and
2810  * others not supported by this module
2811  */
2812 static struct platform_driver davinci_emac_driver = {
2813         .driver = {
2814                 .name    = "davinci_emac",
2815                 .owner   = THIS_MODULE,
2816         },
2817         .probe = davinci_emac_probe,
2818         .remove = __devexit_p(davinci_emac_remove),
2819 };
2820
2821 /**
2822  * davinci_emac_init: EMAC driver module init
2823  *
2824  * Called when initializing the driver. We register the driver with
2825  * the platform.
2826  */
2827 static int __init davinci_emac_init(void)
2828 {
2829         return platform_driver_register(&davinci_emac_driver);
2830 }
2831 late_initcall(davinci_emac_init);
2832
2833 /**
2834  * davinci_emac_exit: EMAC driver module exit
2835  *
2836  * Called when exiting the driver completely. We unregister the driver with
2837  * the platform and exit
2838  */
2839 static void __exit davinci_emac_exit(void)
2840 {
2841         platform_driver_unregister(&davinci_emac_driver);
2842 }
2843 module_exit(davinci_emac_exit);
2844
2845 MODULE_LICENSE("GPL");
2846 MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2847 MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2848 MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");