Merge branch 'i2c-mux/for-next' of https://github.com/peda-r/i2c-mux into i2c/for-5.2
[sfrench/cifs-2.6.git] / drivers / net / ethernet / 8390 / mac8390.c
1 /* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
2    Ethernet cards on Linux */
3 /* Based on the former daynaport.c driver, by Alan Cox.  Some code
4    taken from or inspired by skeleton.c by Donald Becker, acenic.c by
5    Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.
6
7    This software may be used and distributed according to the terms of
8    the GNU Public License, incorporated herein by reference.  */
9
10 /* 2000-02-28: support added for Dayna and Kinetics cards by
11    A.G.deWijn@phys.uu.nl */
12 /* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
13 /* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
14 /* 2001-05-15: support for Cabletron ported from old daynaport driver
15  * and fixed access to Sonic Sys card which masquerades as a Farallon
16  * by rayk@knightsmanor.org */
17 /* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18 /* 2003-12-26: Make sure Asante cards always work. */
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
27 #include <linux/ptrace.h>
28 #include <linux/ioport.h>
29 #include <linux/nubus.h>
30 #include <linux/in.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/bitops.h>
38 #include <linux/io.h>
39
40 #include <asm/dma.h>
41 #include <asm/hwtest.h>
42 #include <asm/macints.h>
43
44 static char version[] =
45         "v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
46
47 #define EI_SHIFT(x)     (ei_local->reg_offset[x])
48 #define ei_inb(port)    in_8(port)
49 #define ei_outb(val, port)      out_8(port, val)
50 #define ei_inb_p(port)  in_8(port)
51 #define ei_outb_p(val, port)    out_8(port, val)
52
53 #include "lib8390.c"
54
55 #define WD_START_PG                     0x00    /* First page of TX buffer */
56 #define CABLETRON_RX_START_PG           0x00    /* First page of RX buffer */
57 #define CABLETRON_RX_STOP_PG            0x30    /* Last page +1 of RX ring */
58 #define CABLETRON_TX_START_PG           CABLETRON_RX_STOP_PG
59                                                 /* First page of TX buffer */
60
61 /*
62  * Unfortunately it seems we have to hardcode these for the moment
63  * Shouldn't the card know about this?
64  * Does anyone know where to read it off the card?
65  * Do we trust the data provided by the card?
66  */
67
68 #define DAYNA_8390_BASE         0x80000
69 #define DAYNA_8390_MEM          0x00000
70
71 #define CABLETRON_8390_BASE     0x90000
72 #define CABLETRON_8390_MEM      0x00000
73
74 #define INTERLAN_8390_BASE      0xE0000
75 #define INTERLAN_8390_MEM       0xD0000
76
77 enum mac8390_type {
78         MAC8390_NONE = -1,
79         MAC8390_APPLE,
80         MAC8390_ASANTE,
81         MAC8390_FARALLON,
82         MAC8390_CABLETRON,
83         MAC8390_DAYNA,
84         MAC8390_INTERLAN,
85         MAC8390_KINETICS,
86 };
87
88 static const char *cardname[] = {
89         "apple",
90         "asante",
91         "farallon",
92         "cabletron",
93         "dayna",
94         "interlan",
95         "kinetics",
96 };
97
98 static const int word16[] = {
99         1, /* apple */
100         1, /* asante */
101         1, /* farallon */
102         1, /* cabletron */
103         0, /* dayna */
104         1, /* interlan */
105         0, /* kinetics */
106 };
107
108 /* on which cards do we use NuBus resources? */
109 static const int useresources[] = {
110         1, /* apple */
111         1, /* asante */
112         1, /* farallon */
113         0, /* cabletron */
114         0, /* dayna */
115         0, /* interlan */
116         0, /* kinetics */
117 };
118
119 enum mac8390_access {
120         ACCESS_UNKNOWN = 0,
121         ACCESS_32,
122         ACCESS_16,
123 };
124
125 extern int mac8390_memtest(struct net_device *dev);
126 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
127                            enum mac8390_type type);
128
129 static int mac8390_open(struct net_device *dev);
130 static int mac8390_close(struct net_device *dev);
131 static void mac8390_no_reset(struct net_device *dev);
132 static void interlan_reset(struct net_device *dev);
133
134 /* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
135 static void sane_get_8390_hdr(struct net_device *dev,
136                               struct e8390_pkt_hdr *hdr, int ring_page);
137 static void sane_block_input(struct net_device *dev, int count,
138                              struct sk_buff *skb, int ring_offset);
139 static void sane_block_output(struct net_device *dev, int count,
140                               const unsigned char *buf, const int start_page);
141
142 /* dayna_memcpy to and from card */
143 static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
144                                 int from, int count);
145 static void dayna_memcpy_tocard(struct net_device *dev, int to,
146                               const void *from, int count);
147
148 /* Dayna - Dayna/Kinetics use this */
149 static void dayna_get_8390_hdr(struct net_device *dev,
150                                struct e8390_pkt_hdr *hdr, int ring_page);
151 static void dayna_block_input(struct net_device *dev, int count,
152                               struct sk_buff *skb, int ring_offset);
153 static void dayna_block_output(struct net_device *dev, int count,
154                                const unsigned char *buf, int start_page);
155
156 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
157 static void slow_sane_get_8390_hdr(struct net_device *dev,
158                                    struct e8390_pkt_hdr *hdr, int ring_page);
159 static void slow_sane_block_input(struct net_device *dev, int count,
160                                   struct sk_buff *skb, int ring_offset);
161 static void slow_sane_block_output(struct net_device *dev, int count,
162                                    const unsigned char *buf, int start_page);
163 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
164 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
165
166 static enum mac8390_type mac8390_ident(struct nubus_rsrc *fres)
167 {
168         switch (fres->dr_sw) {
169         case NUBUS_DRSW_3COM:
170                 switch (fres->dr_hw) {
171                 case NUBUS_DRHW_APPLE_SONIC_NB:
172                 case NUBUS_DRHW_APPLE_SONIC_LC:
173                 case NUBUS_DRHW_SONNET:
174                         return MAC8390_NONE;
175                 default:
176                         return MAC8390_APPLE;
177                 }
178                 break;
179
180         case NUBUS_DRSW_APPLE:
181                 switch (fres->dr_hw) {
182                 case NUBUS_DRHW_ASANTE_LC:
183                         return MAC8390_NONE;
184                 case NUBUS_DRHW_CABLETRON:
185                         return MAC8390_CABLETRON;
186                 default:
187                         return MAC8390_APPLE;
188                 }
189                 break;
190
191         case NUBUS_DRSW_ASANTE:
192                 return MAC8390_ASANTE;
193                 break;
194
195         case NUBUS_DRSW_TECHWORKS:
196         case NUBUS_DRSW_DAYNA2:
197         case NUBUS_DRSW_DAYNA_LC:
198                 if (fres->dr_hw == NUBUS_DRHW_CABLETRON)
199                         return MAC8390_CABLETRON;
200                 else
201                         return MAC8390_APPLE;
202                 break;
203
204         case NUBUS_DRSW_FARALLON:
205                 return MAC8390_FARALLON;
206                 break;
207
208         case NUBUS_DRSW_KINETICS:
209                 switch (fres->dr_hw) {
210                 case NUBUS_DRHW_INTERLAN:
211                         return MAC8390_INTERLAN;
212                 default:
213                         return MAC8390_KINETICS;
214                 }
215                 break;
216
217         case NUBUS_DRSW_DAYNA:
218                 /*
219                  * These correspond to Dayna Sonic cards
220                  * which use the macsonic driver
221                  */
222                 if (fres->dr_hw == NUBUS_DRHW_SMC9194 ||
223                     fres->dr_hw == NUBUS_DRHW_INTERLAN)
224                         return MAC8390_NONE;
225                 else
226                         return MAC8390_DAYNA;
227                 break;
228         }
229         return MAC8390_NONE;
230 }
231
232 static enum mac8390_access mac8390_testio(unsigned long membase)
233 {
234         u32 outdata = 0xA5A0B5B0;
235         u32 indata = 0;
236
237         /* Try writing 32 bits */
238         nubus_writel(outdata, membase);
239         /* Now read it back */
240         indata = nubus_readl(membase);
241         if (outdata == indata)
242                 return ACCESS_32;
243
244         outdata = 0xC5C0D5D0;
245         indata = 0;
246
247         /* Write 16 bit output */
248         word_memcpy_tocard(membase, &outdata, 4);
249         /* Now read it back */
250         word_memcpy_fromcard(&indata, membase, 4);
251         if (outdata == indata)
252                 return ACCESS_16;
253
254         return ACCESS_UNKNOWN;
255 }
256
257 static int mac8390_memsize(unsigned long membase)
258 {
259         unsigned long flags;
260         int i, j;
261
262         local_irq_save(flags);
263         /* Check up to 32K in 4K increments */
264         for (i = 0; i < 8; i++) {
265                 volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
266
267                 /* Unwriteable - we have a fully decoded card and the
268                    RAM end located */
269                 if (hwreg_present(m) == 0)
270                         break;
271
272                 /* write a distinctive byte */
273                 *m = 0xA5A0 | i;
274                 /* check that we read back what we wrote */
275                 if (*m != (0xA5A0 | i))
276                         break;
277
278                 /* check for partial decode and wrap */
279                 for (j = 0; j < i; j++) {
280                         volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
281                         if (*p != (0xA5A0 | j))
282                                 break;
283                 }
284         }
285         local_irq_restore(flags);
286         /*
287          * in any case, we stopped once we tried one block too many,
288          * or once we reached 32K
289          */
290         return i * 0x1000;
291 }
292
293 static bool mac8390_rsrc_init(struct net_device *dev,
294                               struct nubus_rsrc *fres,
295                               enum mac8390_type cardtype)
296 {
297         struct nubus_board *board = fres->board;
298         struct nubus_dir dir;
299         struct nubus_dirent ent;
300         int offset;
301         volatile unsigned short *i;
302
303         dev->irq = SLOT2IRQ(board->slot);
304         /* This is getting to be a habit */
305         dev->base_addr = board->slot_addr | ((board->slot & 0xf) << 20);
306
307         /*
308          * Get some Nubus info - we will trust the card's idea
309          * of where its memory and registers are.
310          */
311
312         if (nubus_get_func_dir(fres, &dir) == -1) {
313                 dev_err(&board->dev,
314                         "Unable to get Nubus functional directory\n");
315                 return false;
316         }
317
318         /* Get the MAC address */
319         if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
320                 dev_info(&board->dev, "MAC address resource not found\n");
321                 return false;
322         }
323
324         nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
325
326         if (useresources[cardtype] == 1) {
327                 nubus_rewinddir(&dir);
328                 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
329                                     &ent) == -1) {
330                         dev_err(&board->dev,
331                                 "Memory offset resource not found\n");
332                         return false;
333                 }
334                 nubus_get_rsrc_mem(&offset, &ent, 4);
335                 dev->mem_start = dev->base_addr + offset;
336                 /* yes, this is how the Apple driver does it */
337                 dev->base_addr = dev->mem_start + 0x10000;
338                 nubus_rewinddir(&dir);
339                 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
340                                     &ent) == -1) {
341                         dev_info(&board->dev,
342                                  "Memory length resource not found, probing\n");
343                         offset = mac8390_memsize(dev->mem_start);
344                 } else {
345                         nubus_get_rsrc_mem(&offset, &ent, 4);
346                 }
347                 dev->mem_end = dev->mem_start + offset;
348         } else {
349                 switch (cardtype) {
350                 case MAC8390_KINETICS:
351                 case MAC8390_DAYNA: /* it's the same */
352                         dev->base_addr = (int)(board->slot_addr +
353                                                DAYNA_8390_BASE);
354                         dev->mem_start = (int)(board->slot_addr +
355                                                DAYNA_8390_MEM);
356                         dev->mem_end = dev->mem_start +
357                                        mac8390_memsize(dev->mem_start);
358                         break;
359                 case MAC8390_INTERLAN:
360                         dev->base_addr = (int)(board->slot_addr +
361                                                INTERLAN_8390_BASE);
362                         dev->mem_start = (int)(board->slot_addr +
363                                                INTERLAN_8390_MEM);
364                         dev->mem_end = dev->mem_start +
365                                        mac8390_memsize(dev->mem_start);
366                         break;
367                 case MAC8390_CABLETRON:
368                         dev->base_addr = (int)(board->slot_addr +
369                                                CABLETRON_8390_BASE);
370                         dev->mem_start = (int)(board->slot_addr +
371                                                CABLETRON_8390_MEM);
372                         /* The base address is unreadable if 0x00
373                          * has been written to the command register
374                          * Reset the chip by writing E8390_NODMA +
375                          *   E8390_PAGE0 + E8390_STOP just to be
376                          *   sure
377                          */
378                         i = (void *)dev->base_addr;
379                         *i = 0x21;
380                         dev->mem_end = dev->mem_start +
381                                        mac8390_memsize(dev->mem_start);
382                         break;
383
384                 default:
385                         dev_err(&board->dev,
386                                 "No known base address for card type\n");
387                         return false;
388                 }
389         }
390
391         return true;
392 }
393
394 static int mac8390_device_probe(struct nubus_board *board)
395 {
396         struct net_device *dev;
397         int err = -ENODEV;
398         struct nubus_rsrc *fres;
399         enum mac8390_type cardtype = MAC8390_NONE;
400
401         dev = ____alloc_ei_netdev(0);
402         if (!dev)
403                 return -ENOMEM;
404
405         SET_NETDEV_DEV(dev, &board->dev);
406
407         for_each_board_func_rsrc(board, fres) {
408                 if (fres->category != NUBUS_CAT_NETWORK ||
409                     fres->type != NUBUS_TYPE_ETHERNET)
410                         continue;
411
412                 cardtype = mac8390_ident(fres);
413                 if (cardtype == MAC8390_NONE)
414                         continue;
415
416                 if (mac8390_rsrc_init(dev, fres, cardtype))
417                         break;
418         }
419         if (!fres)
420                 goto out;
421
422         err = mac8390_initdev(dev, board, cardtype);
423         if (err)
424                 goto out;
425
426         err = register_netdev(dev);
427         if (err)
428                 goto out;
429
430         nubus_set_drvdata(board, dev);
431         return 0;
432
433 out:
434         free_netdev(dev);
435         return err;
436 }
437
438 static int mac8390_device_remove(struct nubus_board *board)
439 {
440         struct net_device *dev = nubus_get_drvdata(board);
441
442         unregister_netdev(dev);
443         free_netdev(dev);
444         return 0;
445 }
446
447 static struct nubus_driver mac8390_driver = {
448         .probe = mac8390_device_probe,
449         .remove = mac8390_device_remove,
450         .driver = {
451                 .name = KBUILD_MODNAME,
452                 .owner = THIS_MODULE,
453         }
454 };
455
456 MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
457 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
458 MODULE_LICENSE("GPL");
459
460 static int __init mac8390_init(void)
461 {
462         return nubus_driver_register(&mac8390_driver);
463 }
464 module_init(mac8390_init);
465
466 static void __exit mac8390_exit(void)
467 {
468         nubus_driver_unregister(&mac8390_driver);
469 }
470 module_exit(mac8390_exit);
471
472 static const struct net_device_ops mac8390_netdev_ops = {
473         .ndo_open               = mac8390_open,
474         .ndo_stop               = mac8390_close,
475         .ndo_start_xmit         = __ei_start_xmit,
476         .ndo_tx_timeout         = __ei_tx_timeout,
477         .ndo_get_stats          = __ei_get_stats,
478         .ndo_set_rx_mode        = __ei_set_multicast_list,
479         .ndo_validate_addr      = eth_validate_addr,
480         .ndo_set_mac_address    = eth_mac_addr,
481 #ifdef CONFIG_NET_POLL_CONTROLLER
482         .ndo_poll_controller    = __ei_poll,
483 #endif
484 };
485
486 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
487                            enum mac8390_type type)
488 {
489         static u32 fwrd4_offsets[16] = {
490                 0,      4,      8,      12,
491                 16,     20,     24,     28,
492                 32,     36,     40,     44,
493                 48,     52,     56,     60
494         };
495         static u32 back4_offsets[16] = {
496                 60,     56,     52,     48,
497                 44,     40,     36,     32,
498                 28,     24,     20,     16,
499                 12,     8,      4,      0
500         };
501         static u32 fwrd2_offsets[16] = {
502                 0,      2,      4,      6,
503                 8,     10,     12,     14,
504                 16,    18,     20,     22,
505                 24,    26,     28,     30
506         };
507
508         int access_bitmode = 0;
509
510         /* Now fill in our stuff */
511         dev->netdev_ops = &mac8390_netdev_ops;
512
513         /* GAR, ei_status is actually a macro even though it looks global */
514         ei_status.name = cardname[type];
515         ei_status.word16 = word16[type];
516
517         /* Cabletron's TX/RX buffers are backwards */
518         if (type == MAC8390_CABLETRON) {
519                 ei_status.tx_start_page = CABLETRON_TX_START_PG;
520                 ei_status.rx_start_page = CABLETRON_RX_START_PG;
521                 ei_status.stop_page = CABLETRON_RX_STOP_PG;
522                 ei_status.rmem_start = dev->mem_start;
523                 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
524         } else {
525                 ei_status.tx_start_page = WD_START_PG;
526                 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
527                 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
528                 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
529                 ei_status.rmem_end = dev->mem_end;
530         }
531
532         /* Fill in model-specific information and functions */
533         switch (type) {
534         case MAC8390_FARALLON:
535         case MAC8390_APPLE:
536                 switch (mac8390_testio(dev->mem_start)) {
537                 case ACCESS_UNKNOWN:
538                         dev_err(&board->dev,
539                                 "Don't know how to access card memory\n");
540                         return -ENODEV;
541
542                 case ACCESS_16:
543                         /* 16 bit card, register map is reversed */
544                         ei_status.reset_8390 = mac8390_no_reset;
545                         ei_status.block_input = slow_sane_block_input;
546                         ei_status.block_output = slow_sane_block_output;
547                         ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
548                         ei_status.reg_offset = back4_offsets;
549                         break;
550
551                 case ACCESS_32:
552                         /* 32 bit card, register map is reversed */
553                         ei_status.reset_8390 = mac8390_no_reset;
554                         ei_status.block_input = sane_block_input;
555                         ei_status.block_output = sane_block_output;
556                         ei_status.get_8390_hdr = sane_get_8390_hdr;
557                         ei_status.reg_offset = back4_offsets;
558                         access_bitmode = 1;
559                         break;
560                 }
561                 break;
562
563         case MAC8390_ASANTE:
564                 /* Some Asante cards pass the 32 bit test
565                  * but overwrite system memory when run at 32 bit.
566                  * so we run them all at 16 bit.
567                  */
568                 ei_status.reset_8390 = mac8390_no_reset;
569                 ei_status.block_input = slow_sane_block_input;
570                 ei_status.block_output = slow_sane_block_output;
571                 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
572                 ei_status.reg_offset = back4_offsets;
573                 break;
574
575         case MAC8390_CABLETRON:
576                 /* 16 bit card, register map is short forward */
577                 ei_status.reset_8390 = mac8390_no_reset;
578                 ei_status.block_input = slow_sane_block_input;
579                 ei_status.block_output = slow_sane_block_output;
580                 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
581                 ei_status.reg_offset = fwrd2_offsets;
582                 break;
583
584         case MAC8390_DAYNA:
585         case MAC8390_KINETICS:
586                 /* 16 bit memory, register map is forward */
587                 /* dayna and similar */
588                 ei_status.reset_8390 = mac8390_no_reset;
589                 ei_status.block_input = dayna_block_input;
590                 ei_status.block_output = dayna_block_output;
591                 ei_status.get_8390_hdr = dayna_get_8390_hdr;
592                 ei_status.reg_offset = fwrd4_offsets;
593                 break;
594
595         case MAC8390_INTERLAN:
596                 /* 16 bit memory, register map is forward */
597                 ei_status.reset_8390 = interlan_reset;
598                 ei_status.block_input = slow_sane_block_input;
599                 ei_status.block_output = slow_sane_block_output;
600                 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
601                 ei_status.reg_offset = fwrd4_offsets;
602                 break;
603
604         default:
605                 dev_err(&board->dev, "Unsupported card type\n");
606                 return -ENODEV;
607         }
608
609         __NS8390_init(dev, 0);
610
611         /* Good, done, now spit out some messages */
612         dev_info(&board->dev, "%s (type %s)\n", board->name, cardname[type]);
613         dev_info(&board->dev, "MAC %pM, IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
614                  dev->dev_addr, dev->irq,
615                  (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
616                  dev->mem_start, access_bitmode ? 32 : 16);
617         return 0;
618 }
619
620 static int mac8390_open(struct net_device *dev)
621 {
622         int err;
623
624         __ei_open(dev);
625         err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
626         if (err)
627                 pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
628         return err;
629 }
630
631 static int mac8390_close(struct net_device *dev)
632 {
633         free_irq(dev->irq, dev);
634         __ei_close(dev);
635         return 0;
636 }
637
638 static void mac8390_no_reset(struct net_device *dev)
639 {
640         struct ei_device *ei_local = netdev_priv(dev);
641
642         ei_status.txing = 0;
643         netif_info(ei_local, hw, dev, "reset not supported\n");
644 }
645
646 static void interlan_reset(struct net_device *dev)
647 {
648         unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
649         struct ei_device *ei_local = netdev_priv(dev);
650
651         netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
652                    jiffies);
653         ei_status.txing = 0;
654         target[0xC0000] = 0;
655         if (netif_msg_hw(ei_local))
656                 pr_cont("reset complete\n");
657 }
658
659 /* dayna_memcpy_fromio/dayna_memcpy_toio */
660 /* directly from daynaport.c by Alan Cox */
661 static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
662                                   int count)
663 {
664         volatile unsigned char *ptr;
665         unsigned char *target = to;
666         from <<= 1;     /* word, skip overhead */
667         ptr = (unsigned char *)(dev->mem_start+from);
668         /* Leading byte? */
669         if (from & 2) {
670                 *target++ = ptr[-1];
671                 ptr += 2;
672                 count--;
673         }
674         while (count >= 2) {
675                 *(unsigned short *)target = *(unsigned short volatile *)ptr;
676                 ptr += 4;                       /* skip cruft */
677                 target += 2;
678                 count -= 2;
679         }
680         /* Trailing byte? */
681         if (count)
682                 *target = *ptr;
683 }
684
685 static void dayna_memcpy_tocard(struct net_device *dev, int to,
686                                 const void *from, int count)
687 {
688         volatile unsigned short *ptr;
689         const unsigned char *src = from;
690         to <<= 1;       /* word, skip overhead */
691         ptr = (unsigned short *)(dev->mem_start+to);
692         /* Leading byte? */
693         if (to & 2) {           /* avoid a byte write (stomps on other data) */
694                 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
695                 ptr++;
696                 count--;
697         }
698         while (count >= 2) {
699                 *ptr++ = *(unsigned short *)src;        /* Copy and */
700                 ptr++;                  /* skip cruft */
701                 src += 2;
702                 count -= 2;
703         }
704         /* Trailing byte? */
705         if (count) {
706                 /* card doesn't like byte writes */
707                 *ptr = (*ptr & 0x00FF) | (*src << 8);
708         }
709 }
710
711 /* sane block input/output */
712 static void sane_get_8390_hdr(struct net_device *dev,
713                               struct e8390_pkt_hdr *hdr, int ring_page)
714 {
715         unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
716         memcpy_fromio(hdr, (void __iomem *)dev->mem_start + hdr_start, 4);
717         /* Fix endianness */
718         hdr->count = swab16(hdr->count);
719 }
720
721 static void sane_block_input(struct net_device *dev, int count,
722                              struct sk_buff *skb, int ring_offset)
723 {
724         unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
725         unsigned long xfer_start = xfer_base + dev->mem_start;
726
727         if (xfer_start + count > ei_status.rmem_end) {
728                 /* We must wrap the input move. */
729                 int semi_count = ei_status.rmem_end - xfer_start;
730                 memcpy_fromio(skb->data,
731                               (void __iomem *)dev->mem_start + xfer_base,
732                               semi_count);
733                 count -= semi_count;
734                 memcpy_fromio(skb->data + semi_count,
735                               (void __iomem *)ei_status.rmem_start, count);
736         } else {
737                 memcpy_fromio(skb->data,
738                               (void __iomem *)dev->mem_start + xfer_base,
739                               count);
740         }
741 }
742
743 static void sane_block_output(struct net_device *dev, int count,
744                               const unsigned char *buf, int start_page)
745 {
746         long shmem = (start_page - WD_START_PG)<<8;
747
748         memcpy_toio((void __iomem *)dev->mem_start + shmem, buf, count);
749 }
750
751 /* dayna block input/output */
752 static void dayna_get_8390_hdr(struct net_device *dev,
753                                struct e8390_pkt_hdr *hdr, int ring_page)
754 {
755         unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
756
757         dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
758         /* Fix endianness */
759         hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
760 }
761
762 static void dayna_block_input(struct net_device *dev, int count,
763                               struct sk_buff *skb, int ring_offset)
764 {
765         unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
766         unsigned long xfer_start = xfer_base+dev->mem_start;
767
768         /* Note the offset math is done in card memory space which is word
769            per long onto our space. */
770
771         if (xfer_start + count > ei_status.rmem_end) {
772                 /* We must wrap the input move. */
773                 int semi_count = ei_status.rmem_end - xfer_start;
774                 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
775                 count -= semi_count;
776                 dayna_memcpy_fromcard(dev, skb->data + semi_count,
777                                       ei_status.rmem_start - dev->mem_start,
778                                       count);
779         } else {
780                 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
781         }
782 }
783
784 static void dayna_block_output(struct net_device *dev, int count,
785                                const unsigned char *buf,
786                                int start_page)
787 {
788         long shmem = (start_page - WD_START_PG)<<8;
789
790         dayna_memcpy_tocard(dev, shmem, buf, count);
791 }
792
793 /* Cabletron block I/O */
794 static void slow_sane_get_8390_hdr(struct net_device *dev,
795                                    struct e8390_pkt_hdr *hdr,
796                                    int ring_page)
797 {
798         unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
799         word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
800         /* Register endianism - fix here rather than 8390.c */
801         hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
802 }
803
804 static void slow_sane_block_input(struct net_device *dev, int count,
805                                   struct sk_buff *skb, int ring_offset)
806 {
807         unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
808         unsigned long xfer_start = xfer_base+dev->mem_start;
809
810         if (xfer_start + count > ei_status.rmem_end) {
811                 /* We must wrap the input move. */
812                 int semi_count = ei_status.rmem_end - xfer_start;
813                 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
814                                      semi_count);
815                 count -= semi_count;
816                 word_memcpy_fromcard(skb->data + semi_count,
817                                      ei_status.rmem_start, count);
818         } else {
819                 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
820                                      count);
821         }
822 }
823
824 static void slow_sane_block_output(struct net_device *dev, int count,
825                                    const unsigned char *buf, int start_page)
826 {
827         long shmem = (start_page - WD_START_PG)<<8;
828
829         word_memcpy_tocard(dev->mem_start + shmem, buf, count);
830 }
831
832 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
833 {
834         volatile unsigned short *to = (void *)tp;
835         const unsigned short *from = fp;
836
837         count++;
838         count /= 2;
839
840         while (count--)
841                 *to++ = *from++;
842 }
843
844 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
845 {
846         unsigned short *to = tp;
847         const volatile unsigned short *from = (const void *)fp;
848
849         count++;
850         count /= 2;
851
852         while (count--)
853                 *to++ = *from++;
854 }
855
856