Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[pandora-kernel.git] / arch / ppc / 8260_io / enet.c
1 /*
2  * Ethernet driver for Motorola MPC8260.
3  * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
4  * Copyright (c) 2000 MontaVista Software Inc. (source@mvista.com)
5  *      2.3.99 Updates
6  *
7  * I copied this from the 8xx CPM Ethernet driver, so follow the
8  * credits back through that.
9  *
10  * This version of the driver is somewhat selectable for the different
11  * processor/board combinations.  It works for the boards I know about
12  * now, and should be easily modified to include others.  Some of the
13  * configuration information is contained in <asm/commproc.h> and the
14  * remainder is here.
15  *
16  * Buffer descriptors are kept in the CPM dual port RAM, and the frame
17  * buffers are in the host memory.
18  *
19  * Right now, I am very watseful with the buffers.  I allocate memory
20  * pages and then divide them into 2K frame buffers.  This way I know I
21  * have buffers large enough to hold one frame within one buffer descriptor.
22  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
23  * will be much more memory efficient and will easily handle lots of
24  * small packets.
25  *
26  */
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/string.h>
30 #include <linux/ptrace.h>
31 #include <linux/errno.h>
32 #include <linux/ioport.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/skbuff.h>
41 #include <linux/spinlock.h>
42 #include <linux/bitops.h>
43
44 #include <asm/immap_cpm2.h>
45 #include <asm/pgtable.h>
46 #include <asm/mpc8260.h>
47 #include <asm/uaccess.h>
48 #include <asm/cpm2.h>
49 #include <asm/irq.h>
50
51 /*
52  *                              Theory of Operation
53  *
54  * The MPC8260 CPM performs the Ethernet processing on an SCC.  It can use
55  * an aribtrary number of buffers on byte boundaries, but must have at
56  * least two receive buffers to prevent constant overrun conditions.
57  *
58  * The buffer descriptors are allocated from the CPM dual port memory
59  * with the data buffers allocated from host memory, just like all other
60  * serial communication protocols.  The host memory buffers are allocated
61  * from the free page pool, and then divided into smaller receive and
62  * transmit buffers.  The size of the buffers should be a power of two,
63  * since that nicely divides the page.  This creates a ring buffer
64  * structure similar to the LANCE and other controllers.
65  *
66  * Like the LANCE driver:
67  * The driver runs as two independent, single-threaded flows of control.  One
68  * is the send-packet routine, which enforces single-threaded use by the
69  * cep->tx_busy flag.  The other thread is the interrupt handler, which is
70  * single threaded by the hardware and other software.
71  */
72
73 /* The transmitter timeout
74  */
75 #define TX_TIMEOUT      (2*HZ)
76
77 /* The number of Tx and Rx buffers.  These are allocated from the page
78  * pool.  The code may assume these are power of two, so it is best
79  * to keep them that size.
80  * We don't need to allocate pages for the transmitter.  We just use
81  * the skbuffer directly.
82  */
83 #define CPM_ENET_RX_PAGES       4
84 #define CPM_ENET_RX_FRSIZE      2048
85 #define CPM_ENET_RX_FRPPG       (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
86 #define RX_RING_SIZE            (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
87 #define TX_RING_SIZE            8       /* Must be power of two */
88 #define TX_RING_MOD_MASK        7       /*   for this to work */
89
90 /* The CPM stores dest/src/type, data, and checksum for receive packets.
91  */
92 #define PKT_MAXBUF_SIZE         1518
93 #define PKT_MINBUF_SIZE         64
94 #define PKT_MAXBLR_SIZE         1520
95
96 /* The CPM buffer descriptors track the ring buffers.  The rx_bd_base and
97  * tx_bd_base always point to the base of the buffer descriptors.  The
98  * cur_rx and cur_tx point to the currently available buffer.
99  * The dirty_tx tracks the current buffer that is being sent by the
100  * controller.  The cur_tx and dirty_tx are equal under both completely
101  * empty and completely full conditions.  The empty/ready indicator in
102  * the buffer descriptor determines the actual condition.
103  */
104 struct scc_enet_private {
105         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
106         struct  sk_buff* tx_skbuff[TX_RING_SIZE];
107         ushort  skb_cur;
108         ushort  skb_dirty;
109
110         /* CPM dual port RAM relative addresses.
111         */
112         cbd_t   *rx_bd_base;            /* Address of Rx and Tx buffers. */
113         cbd_t   *tx_bd_base;
114         cbd_t   *cur_rx, *cur_tx;               /* The next free ring entry */
115         cbd_t   *dirty_tx;      /* The ring entries to be free()ed. */
116         scc_t   *sccp;
117         struct  net_device_stats stats;
118         uint    tx_full;
119         spinlock_t lock;
120 };
121
122 static int scc_enet_open(struct net_device *dev);
123 static int scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
124 static int scc_enet_rx(struct net_device *dev);
125 static irqreturn_t scc_enet_interrupt(int irq, void *dev_id);
126 static int scc_enet_close(struct net_device *dev);
127 static struct net_device_stats *scc_enet_get_stats(struct net_device *dev);
128 static void set_multicast_list(struct net_device *dev);
129
130 /* These will be configurable for the SCC choice.
131 */
132 #define CPM_ENET_BLOCK  CPM_CR_SCC1_SBLOCK
133 #define CPM_ENET_PAGE   CPM_CR_SCC1_PAGE
134 #define PROFF_ENET      PROFF_SCC1
135 #define SCC_ENET        0
136 #define SIU_INT_ENET    SIU_INT_SCC1
137
138 /* These are both board and SCC dependent....
139 */
140 #define PD_ENET_RXD     ((uint)0x00000001)
141 #define PD_ENET_TXD     ((uint)0x00000002)
142 #define PD_ENET_TENA    ((uint)0x00000004)
143 #define PC_ENET_RENA    ((uint)0x00020000)
144 #define PC_ENET_CLSN    ((uint)0x00000004)
145 #define PC_ENET_TXCLK   ((uint)0x00000800)
146 #define PC_ENET_RXCLK   ((uint)0x00000400)
147 #define CMX_CLK_ROUTE   ((uint)0x25000000)
148 #define CMX_CLK_MASK    ((uint)0xff000000)
149
150 /* Specific to a board.
151 */
152 #define PC_EST8260_ENET_LOOPBACK        ((uint)0x80000000)
153 #define PC_EST8260_ENET_SQE             ((uint)0x40000000)
154 #define PC_EST8260_ENET_NOTFD           ((uint)0x20000000)
155
156 static int
157 scc_enet_open(struct net_device *dev)
158 {
159
160         /* I should reset the ring buffers here, but I don't yet know
161          * a simple way to do that.
162          */
163         netif_start_queue(dev);
164         return 0;                                       /* Always succeed */
165 }
166
167 static int
168 scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
169 {
170         struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
171         volatile cbd_t  *bdp;
172
173
174         /* Fill in a Tx ring entry */
175         bdp = cep->cur_tx;
176
177 #ifndef final_version
178         if (bdp->cbd_sc & BD_ENET_TX_READY) {
179                 /* Ooops.  All transmit buffers are full.  Bail out.
180                  * This should not happen, since cep->tx_full should be set.
181                  */
182                 printk("%s: tx queue full!.\n", dev->name);
183                 return 1;
184         }
185 #endif
186
187         /* Clear all of the status flags.
188          */
189         bdp->cbd_sc &= ~BD_ENET_TX_STATS;
190
191         /* If the frame is short, tell CPM to pad it.
192         */
193         if (skb->len <= ETH_ZLEN)
194                 bdp->cbd_sc |= BD_ENET_TX_PAD;
195         else
196                 bdp->cbd_sc &= ~BD_ENET_TX_PAD;
197
198         /* Set buffer length and buffer pointer.
199         */
200         bdp->cbd_datlen = skb->len;
201         bdp->cbd_bufaddr = __pa(skb->data);
202
203         /* Save skb pointer.
204         */
205         cep->tx_skbuff[cep->skb_cur] = skb;
206
207         cep->stats.tx_bytes += skb->len;
208         cep->skb_cur = (cep->skb_cur+1) & TX_RING_MOD_MASK;
209
210         spin_lock_irq(&cep->lock);
211
212         /* Send it on its way.  Tell CPM its ready, interrupt when done,
213          * its the last BD of the frame, and to put the CRC on the end.
214          */
215         bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR | BD_ENET_TX_LAST | BD_ENET_TX_TC);
216
217         dev->trans_start = jiffies;
218
219         /* If this was the last BD in the ring, start at the beginning again.
220         */
221         if (bdp->cbd_sc & BD_ENET_TX_WRAP)
222                 bdp = cep->tx_bd_base;
223         else
224                 bdp++;
225
226         if (bdp->cbd_sc & BD_ENET_TX_READY) {
227                 netif_stop_queue(dev);
228                 cep->tx_full = 1;
229         }
230
231         cep->cur_tx = (cbd_t *)bdp;
232
233         spin_unlock_irq(&cep->lock);
234
235         return 0;
236 }
237
238 static void
239 scc_enet_timeout(struct net_device *dev)
240 {
241         struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
242
243         printk("%s: transmit timed out.\n", dev->name);
244         cep->stats.tx_errors++;
245 #ifndef final_version
246         {
247                 int     i;
248                 cbd_t   *bdp;
249                 printk(" Ring data dump: cur_tx %p%s cur_rx %p.\n",
250                        cep->cur_tx, cep->tx_full ? " (full)" : "",
251                        cep->cur_rx);
252                 bdp = cep->tx_bd_base;
253                 printk(" Tx @base %p :\n", bdp);
254                 for (i = 0 ; i < TX_RING_SIZE; i++, bdp++)
255                         printk("%04x %04x %08x\n",
256                                bdp->cbd_sc,
257                                bdp->cbd_datlen,
258                                bdp->cbd_bufaddr);
259                 bdp = cep->rx_bd_base;
260                 printk(" Rx @base %p :\n", bdp);
261                 for (i = 0 ; i < RX_RING_SIZE; i++, bdp++)
262                         printk("%04x %04x %08x\n",
263                                bdp->cbd_sc,
264                                bdp->cbd_datlen,
265                                bdp->cbd_bufaddr);
266         }
267 #endif
268         if (!cep->tx_full)
269                 netif_wake_queue(dev);
270 }
271
272 /* The interrupt handler.
273  * This is called from the CPM handler, not the MPC core interrupt.
274  */
275 static irqreturn_t
276 scc_enet_interrupt(int irq, void * dev_id)
277 {
278         struct  net_device *dev = dev_id;
279         volatile struct scc_enet_private *cep;
280         volatile cbd_t  *bdp;
281         ushort  int_events;
282         int     must_restart;
283
284         cep = (struct scc_enet_private *)dev->priv;
285
286         /* Get the interrupt events that caused us to be here.
287         */
288         int_events = cep->sccp->scc_scce;
289         cep->sccp->scc_scce = int_events;
290         must_restart = 0;
291
292         /* Handle receive event in its own function.
293         */
294         if (int_events & SCCE_ENET_RXF)
295                 scc_enet_rx(dev_id);
296
297         /* Check for a transmit error.  The manual is a little unclear
298          * about this, so the debug code until I get it figured out.  It
299          * appears that if TXE is set, then TXB is not set.  However,
300          * if carrier sense is lost during frame transmission, the TXE
301          * bit is set, "and continues the buffer transmission normally."
302          * I don't know if "normally" implies TXB is set when the buffer
303          * descriptor is closed.....trial and error :-).
304          */
305
306         /* Transmit OK, or non-fatal error.  Update the buffer descriptors.
307         */
308         if (int_events & (SCCE_ENET_TXE | SCCE_ENET_TXB)) {
309             spin_lock(&cep->lock);
310             bdp = cep->dirty_tx;
311             while ((bdp->cbd_sc&BD_ENET_TX_READY)==0) {
312                 if ((bdp==cep->cur_tx) && (cep->tx_full == 0))
313                     break;
314
315                 if (bdp->cbd_sc & BD_ENET_TX_HB)        /* No heartbeat */
316                         cep->stats.tx_heartbeat_errors++;
317                 if (bdp->cbd_sc & BD_ENET_TX_LC)        /* Late collision */
318                         cep->stats.tx_window_errors++;
319                 if (bdp->cbd_sc & BD_ENET_TX_RL)        /* Retrans limit */
320                         cep->stats.tx_aborted_errors++;
321                 if (bdp->cbd_sc & BD_ENET_TX_UN)        /* Underrun */
322                         cep->stats.tx_fifo_errors++;
323                 if (bdp->cbd_sc & BD_ENET_TX_CSL)       /* Carrier lost */
324                         cep->stats.tx_carrier_errors++;
325
326
327                 /* No heartbeat or Lost carrier are not really bad errors.
328                  * The others require a restart transmit command.
329                  */
330                 if (bdp->cbd_sc &
331                     (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
332                         must_restart = 1;
333                         cep->stats.tx_errors++;
334                 }
335
336                 cep->stats.tx_packets++;
337
338                 /* Deferred means some collisions occurred during transmit,
339                  * but we eventually sent the packet OK.
340                  */
341                 if (bdp->cbd_sc & BD_ENET_TX_DEF)
342                         cep->stats.collisions++;
343
344                 /* Free the sk buffer associated with this last transmit.
345                 */
346                 dev_kfree_skb_irq(cep->tx_skbuff[cep->skb_dirty]);
347                 cep->skb_dirty = (cep->skb_dirty + 1) & TX_RING_MOD_MASK;
348
349                 /* Update pointer to next buffer descriptor to be transmitted.
350                 */
351                 if (bdp->cbd_sc & BD_ENET_TX_WRAP)
352                         bdp = cep->tx_bd_base;
353                 else
354                         bdp++;
355
356                 /* I don't know if we can be held off from processing these
357                  * interrupts for more than one frame time.  I really hope
358                  * not.  In such a case, we would now want to check the
359                  * currently available BD (cur_tx) and determine if any
360                  * buffers between the dirty_tx and cur_tx have also been
361                  * sent.  We would want to process anything in between that
362                  * does not have BD_ENET_TX_READY set.
363                  */
364
365                 /* Since we have freed up a buffer, the ring is no longer
366                  * full.
367                  */
368                 if (cep->tx_full) {
369                         cep->tx_full = 0;
370                         if (netif_queue_stopped(dev)) {
371                                 netif_wake_queue(dev);
372                         }
373                 }
374
375                 cep->dirty_tx = (cbd_t *)bdp;
376             }
377
378             if (must_restart) {
379                 volatile cpm_cpm2_t *cp;
380
381                 /* Some transmit errors cause the transmitter to shut
382                  * down.  We now issue a restart transmit.  Since the
383                  * errors close the BD and update the pointers, the restart
384                  * _should_ pick up without having to reset any of our
385                  * pointers either.
386                  */
387
388                 cp = cpmp;
389                 cp->cp_cpcr =
390                     mk_cr_cmd(CPM_ENET_PAGE, CPM_ENET_BLOCK, 0,
391                                         CPM_CR_RESTART_TX) | CPM_CR_FLG;
392                 while (cp->cp_cpcr & CPM_CR_FLG);
393             }
394             spin_unlock(&cep->lock);
395         }
396
397         /* Check for receive busy, i.e. packets coming but no place to
398          * put them.  This "can't happen" because the receive interrupt
399          * is tossing previous frames.
400          */
401         if (int_events & SCCE_ENET_BSY) {
402                 cep->stats.rx_dropped++;
403                 printk("SCC ENET: BSY can't happen.\n");
404         }
405
406         return IRQ_HANDLED;
407 }
408
409 /* During a receive, the cur_rx points to the current incoming buffer.
410  * When we update through the ring, if the next incoming buffer has
411  * not been given to the system, we just set the empty indicator,
412  * effectively tossing the packet.
413  */
414 static int
415 scc_enet_rx(struct net_device *dev)
416 {
417         struct  scc_enet_private *cep;
418         volatile cbd_t  *bdp;
419         struct  sk_buff *skb;
420         ushort  pkt_len;
421
422         cep = (struct scc_enet_private *)dev->priv;
423
424         /* First, grab all of the stats for the incoming packet.
425          * These get messed up if we get called due to a busy condition.
426          */
427         bdp = cep->cur_rx;
428
429 for (;;) {
430         if (bdp->cbd_sc & BD_ENET_RX_EMPTY)
431                 break;
432
433 #ifndef final_version
434         /* Since we have allocated space to hold a complete frame, both
435          * the first and last indicators should be set.
436          */
437         if ((bdp->cbd_sc & (BD_ENET_RX_FIRST | BD_ENET_RX_LAST)) !=
438                 (BD_ENET_RX_FIRST | BD_ENET_RX_LAST))
439                         printk("CPM ENET: rcv is not first+last\n");
440 #endif
441
442         /* Frame too long or too short.
443         */
444         if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
445                 cep->stats.rx_length_errors++;
446         if (bdp->cbd_sc & BD_ENET_RX_NO)        /* Frame alignment */
447                 cep->stats.rx_frame_errors++;
448         if (bdp->cbd_sc & BD_ENET_RX_CR)        /* CRC Error */
449                 cep->stats.rx_crc_errors++;
450         if (bdp->cbd_sc & BD_ENET_RX_OV)        /* FIFO overrun */
451                 cep->stats.rx_crc_errors++;
452
453         /* Report late collisions as a frame error.
454          * On this error, the BD is closed, but we don't know what we
455          * have in the buffer.  So, just drop this frame on the floor.
456          */
457         if (bdp->cbd_sc & BD_ENET_RX_CL) {
458                 cep->stats.rx_frame_errors++;
459         }
460         else {
461
462                 /* Process the incoming frame.
463                 */
464                 cep->stats.rx_packets++;
465                 pkt_len = bdp->cbd_datlen;
466                 cep->stats.rx_bytes += pkt_len;
467
468                 /* This does 16 byte alignment, much more than we need.
469                  * The packet length includes FCS, but we don't want to
470                  * include that when passing upstream as it messes up
471                  * bridging applications.
472                  */
473                 skb = dev_alloc_skb(pkt_len-4);
474
475                 if (skb == NULL) {
476                         printk("%s: Memory squeeze, dropping packet.\n", dev->name);
477                         cep->stats.rx_dropped++;
478                 }
479                 else {
480                         skb_put(skb,pkt_len-4); /* Make room */
481                         eth_copy_and_sum(skb,
482                                 (unsigned char *)__va(bdp->cbd_bufaddr),
483                                 pkt_len-4, 0);
484                         skb->protocol=eth_type_trans(skb,dev);
485                         netif_rx(skb);
486                 }
487         }
488
489         /* Clear the status flags for this buffer.
490         */
491         bdp->cbd_sc &= ~BD_ENET_RX_STATS;
492
493         /* Mark the buffer empty.
494         */
495         bdp->cbd_sc |= BD_ENET_RX_EMPTY;
496
497         /* Update BD pointer to next entry.
498         */
499         if (bdp->cbd_sc & BD_ENET_RX_WRAP)
500                 bdp = cep->rx_bd_base;
501         else
502                 bdp++;
503
504    }
505         cep->cur_rx = (cbd_t *)bdp;
506
507         return 0;
508 }
509
510 static int
511 scc_enet_close(struct net_device *dev)
512 {
513         /* Don't know what to do yet.
514         */
515         netif_stop_queue(dev);
516
517         return 0;
518 }
519
520 static struct net_device_stats *scc_enet_get_stats(struct net_device *dev)
521 {
522         struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
523
524         return &cep->stats;
525 }
526
527 /* Set or clear the multicast filter for this adaptor.
528  * Skeleton taken from sunlance driver.
529  * The CPM Ethernet implementation allows Multicast as well as individual
530  * MAC address filtering.  Some of the drivers check to make sure it is
531  * a group multicast address, and discard those that are not.  I guess I
532  * will do the same for now, but just remove the test if you want
533  * individual filtering as well (do the upper net layers want or support
534  * this kind of feature?).
535  */
536
537 static void set_multicast_list(struct net_device *dev)
538 {
539         struct  scc_enet_private *cep;
540         struct  dev_mc_list *dmi;
541         u_char  *mcptr, *tdptr;
542         volatile scc_enet_t *ep;
543         int     i, j;
544         cep = (struct scc_enet_private *)dev->priv;
545
546         /* Get pointer to SCC area in parameter RAM.
547         */
548         ep = (scc_enet_t *)dev->base_addr;
549
550         if (dev->flags&IFF_PROMISC) {
551         
552                 /* Log any net taps. */
553                 printk("%s: Promiscuous mode enabled.\n", dev->name);
554                 cep->sccp->scc_psmr |= SCC_PSMR_PRO;
555         } else {
556
557                 cep->sccp->scc_psmr &= ~SCC_PSMR_PRO;
558
559                 if (dev->flags & IFF_ALLMULTI) {
560                         /* Catch all multicast addresses, so set the
561                          * filter to all 1's.
562                          */
563                         ep->sen_gaddr1 = 0xffff;
564                         ep->sen_gaddr2 = 0xffff;
565                         ep->sen_gaddr3 = 0xffff;
566                         ep->sen_gaddr4 = 0xffff;
567                 }
568                 else {
569                         /* Clear filter and add the addresses in the list.
570                         */
571                         ep->sen_gaddr1 = 0;
572                         ep->sen_gaddr2 = 0;
573                         ep->sen_gaddr3 = 0;
574                         ep->sen_gaddr4 = 0;
575
576                         dmi = dev->mc_list;
577
578                         for (i=0; i<dev->mc_count; i++) {
579                 
580                                 /* Only support group multicast for now.
581                                 */
582                                 if (!(dmi->dmi_addr[0] & 1))
583                                         continue;
584
585                                 /* The address in dmi_addr is LSB first,
586                                  * and taddr is MSB first.  We have to
587                                  * copy bytes MSB first from dmi_addr.
588                                  */
589                                 mcptr = (u_char *)dmi->dmi_addr + 5;
590                                 tdptr = (u_char *)&ep->sen_taddrh;
591                                 for (j=0; j<6; j++)
592                                         *tdptr++ = *mcptr--;
593
594                                 /* Ask CPM to run CRC and set bit in
595                                  * filter mask.
596                                  */
597                                 cpmp->cp_cpcr = mk_cr_cmd(CPM_ENET_PAGE,
598                                                 CPM_ENET_BLOCK, 0,
599                                                 CPM_CR_SET_GADDR) | CPM_CR_FLG;
600                                 /* this delay is necessary here -- Cort */
601                                 udelay(10);
602                                 while (cpmp->cp_cpcr & CPM_CR_FLG);
603                         }
604                 }
605         }
606 }
607
608 /* Initialize the CPM Ethernet on SCC.
609  */
610 static int __init scc_enet_init(void)
611 {
612         struct net_device *dev;
613         struct scc_enet_private *cep;
614         int i, j, err;
615         uint dp_offset;
616         unsigned char   *eap;
617         unsigned long   mem_addr;
618         bd_t            *bd;
619         volatile        cbd_t           *bdp;
620         volatile        cpm_cpm2_t      *cp;
621         volatile        scc_t           *sccp;
622         volatile        scc_enet_t      *ep;
623         volatile        cpm2_map_t              *immap;
624         volatile        iop_cpm2_t      *io;
625
626         cp = cpmp;      /* Get pointer to Communication Processor */
627
628         immap = (cpm2_map_t *)CPM_MAP_ADDR;     /* and to internal registers */
629         io = &immap->im_ioport;
630
631         bd = (bd_t *)__res;
632
633         /* Create an Ethernet device instance.
634         */
635         dev = alloc_etherdev(sizeof(*cep));
636         if (!dev)
637                 return -ENOMEM;
638
639         cep = dev->priv;
640         spin_lock_init(&cep->lock);
641
642         /* Get pointer to SCC area in parameter RAM.
643         */
644         ep = (scc_enet_t *)(&immap->im_dprambase[PROFF_ENET]);
645
646         /* And another to the SCC register area.
647         */
648         sccp = (volatile scc_t *)(&immap->im_scc[SCC_ENET]);
649         cep->sccp = (scc_t *)sccp;              /* Keep the pointer handy */
650
651         /* Disable receive and transmit in case someone left it running.
652         */
653         sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
654
655         /* Configure port C and D pins for SCC Ethernet.  This
656          * won't work for all SCC possibilities....it will be
657          * board/port specific.
658          */
659         io->iop_pparc |=
660                 (PC_ENET_RENA | PC_ENET_CLSN | PC_ENET_TXCLK | PC_ENET_RXCLK);
661         io->iop_pdirc &=
662                 ~(PC_ENET_RENA | PC_ENET_CLSN | PC_ENET_TXCLK | PC_ENET_RXCLK);
663         io->iop_psorc &=
664                 ~(PC_ENET_RENA | PC_ENET_TXCLK | PC_ENET_RXCLK);
665         io->iop_psorc |= PC_ENET_CLSN;
666
667         io->iop_ppard |= (PD_ENET_RXD | PD_ENET_TXD | PD_ENET_TENA);
668         io->iop_pdird |= (PD_ENET_TXD | PD_ENET_TENA);
669         io->iop_pdird &= ~PD_ENET_RXD;
670         io->iop_psord |= PD_ENET_TXD;
671         io->iop_psord &= ~(PD_ENET_RXD | PD_ENET_TENA);
672
673         /* Configure Serial Interface clock routing.
674          * First, clear all SCC bits to zero, then set the ones we want.
675          */
676         immap->im_cpmux.cmx_scr &= ~CMX_CLK_MASK;
677         immap->im_cpmux.cmx_scr |= CMX_CLK_ROUTE;
678
679         /* Allocate space for the buffer descriptors in the DP ram.
680          * These are relative offsets in the DP ram address space.
681          * Initialize base addresses for the buffer descriptors.
682          */
683         dp_offset = cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
684         ep->sen_genscc.scc_rbase = dp_offset;
685         cep->rx_bd_base = (cbd_t *)cpm_dpram_addr(dp_offset);
686
687         dp_offset = cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
688         ep->sen_genscc.scc_tbase = dp_offset;
689         cep->tx_bd_base = (cbd_t *)cpm_dpram_addr(dp_offset);
690
691         cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
692         cep->cur_rx = cep->rx_bd_base;
693
694         ep->sen_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
695         ep->sen_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
696
697         /* Set maximum bytes per receive buffer.
698          * This appears to be an Ethernet frame size, not the buffer
699          * fragment size.  It must be a multiple of four.
700          */
701         ep->sen_genscc.scc_mrblr = PKT_MAXBLR_SIZE;
702
703         /* Set CRC preset and mask.
704         */
705         ep->sen_cpres = 0xffffffff;
706         ep->sen_cmask = 0xdebb20e3;
707
708         ep->sen_crcec = 0;      /* CRC Error counter */
709         ep->sen_alec = 0;       /* alignment error counter */
710         ep->sen_disfc = 0;      /* discard frame counter */
711
712         ep->sen_pads = 0x8888;  /* Tx short frame pad character */
713         ep->sen_retlim = 15;    /* Retry limit threshold */
714
715         ep->sen_maxflr = PKT_MAXBUF_SIZE;   /* maximum frame length register */
716         ep->sen_minflr = PKT_MINBUF_SIZE;  /* minimum frame length register */
717
718         ep->sen_maxd1 = PKT_MAXBLR_SIZE;        /* maximum DMA1 length */
719         ep->sen_maxd2 = PKT_MAXBLR_SIZE;        /* maximum DMA2 length */
720
721         /* Clear hash tables.
722         */
723         ep->sen_gaddr1 = 0;
724         ep->sen_gaddr2 = 0;
725         ep->sen_gaddr3 = 0;
726         ep->sen_gaddr4 = 0;
727         ep->sen_iaddr1 = 0;
728         ep->sen_iaddr2 = 0;
729         ep->sen_iaddr3 = 0;
730         ep->sen_iaddr4 = 0;
731
732         /* Set Ethernet station address.
733          *
734          * This is supplied in the board information structure, so we
735          * copy that into the controller.
736          */
737         eap = (unsigned char *)&(ep->sen_paddrh);
738         for (i=5; i>=0; i--)
739                 *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
740
741         ep->sen_pper = 0;       /* 'cause the book says so */
742         ep->sen_taddrl = 0;     /* temp address (LSB) */
743         ep->sen_taddrm = 0;
744         ep->sen_taddrh = 0;     /* temp address (MSB) */
745
746         /* Now allocate the host memory pages and initialize the
747          * buffer descriptors.
748          */
749         bdp = cep->tx_bd_base;
750         for (i=0; i<TX_RING_SIZE; i++) {
751
752                 /* Initialize the BD for every fragment in the page.
753                 */
754                 bdp->cbd_sc = 0;
755                 bdp->cbd_bufaddr = 0;
756                 bdp++;
757         }
758
759         /* Set the last buffer to wrap.
760         */
761         bdp--;
762         bdp->cbd_sc |= BD_SC_WRAP;
763
764         bdp = cep->rx_bd_base;
765         for (i=0; i<CPM_ENET_RX_PAGES; i++) {
766
767                 /* Allocate a page.
768                 */
769                 mem_addr = __get_free_page(GFP_KERNEL);
770                 /* BUG: no check for failure */
771
772                 /* Initialize the BD for every fragment in the page.
773                 */
774                 for (j=0; j<CPM_ENET_RX_FRPPG; j++) {
775                         bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
776                         bdp->cbd_bufaddr = __pa(mem_addr);
777                         mem_addr += CPM_ENET_RX_FRSIZE;
778                         bdp++;
779                 }
780         }
781
782         /* Set the last buffer to wrap.
783         */
784         bdp--;
785         bdp->cbd_sc |= BD_SC_WRAP;
786
787         /* Let's re-initialize the channel now.  We have to do it later
788          * than the manual describes because we have just now finished
789          * the BD initialization.
790          */
791         cpmp->cp_cpcr = mk_cr_cmd(CPM_ENET_PAGE, CPM_ENET_BLOCK, 0,
792                         CPM_CR_INIT_TRX) | CPM_CR_FLG;
793         while (cp->cp_cpcr & CPM_CR_FLG);
794
795         cep->skb_cur = cep->skb_dirty = 0;
796
797         sccp->scc_scce = 0xffff;        /* Clear any pending events */
798
799         /* Enable interrupts for transmit error, complete frame
800          * received, and any transmit buffer we have also set the
801          * interrupt flag.
802          */
803         sccp->scc_sccm = (SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
804
805         /* Install our interrupt handler.
806         */
807         request_irq(SIU_INT_ENET, scc_enet_interrupt, 0, "enet", dev);
808         /* BUG: no check for failure */
809
810         /* Set GSMR_H to enable all normal operating modes.
811          * Set GSMR_L to enable Ethernet to MC68160.
812          */
813         sccp->scc_gsmrh = 0;
814         sccp->scc_gsmrl = (SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 | SCC_GSMRL_MODE_ENET);
815
816         /* Set sync/delimiters.
817         */
818         sccp->scc_dsr = 0xd555;
819
820         /* Set processing mode.  Use Ethernet CRC, catch broadcast, and
821          * start frame search 22 bit times after RENA.
822          */
823         sccp->scc_psmr = (SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
824
825         /* It is now OK to enable the Ethernet transmitter.
826          * Unfortunately, there are board implementation differences here.
827          */
828         io->iop_pparc &= ~(PC_EST8260_ENET_LOOPBACK |
829                                 PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
830         io->iop_psorc &= ~(PC_EST8260_ENET_LOOPBACK |
831                                 PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
832         io->iop_pdirc |= (PC_EST8260_ENET_LOOPBACK |
833                                 PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
834         io->iop_pdatc &= ~(PC_EST8260_ENET_LOOPBACK | PC_EST8260_ENET_SQE);
835         io->iop_pdatc |= PC_EST8260_ENET_NOTFD;
836
837         dev->base_addr = (unsigned long)ep;
838
839         /* The CPM Ethernet specific entries in the device structure. */
840         dev->open = scc_enet_open;
841         dev->hard_start_xmit = scc_enet_start_xmit;
842         dev->tx_timeout = scc_enet_timeout;
843         dev->watchdog_timeo = TX_TIMEOUT;
844         dev->stop = scc_enet_close;
845         dev->get_stats = scc_enet_get_stats;
846         dev->set_multicast_list = set_multicast_list;
847
848         /* And last, enable the transmit and receive processing.
849         */
850         sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
851
852         err = register_netdev(dev);
853         if (err) {
854                 free_netdev(dev);
855                 return err;
856         }
857
858         printk("%s: SCC ENET Version 0.1, ", dev->name);
859         for (i=0; i<5; i++)
860                 printk("%02x:", dev->dev_addr[i]);
861         printk("%02x\n", dev->dev_addr[5]);
862
863         return 0;
864 }
865
866 module_init(scc_enet_init);