de620.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  2. * de620.c $Revision: 1.40 $ BETA
  3. *
  4. *
  5. * Linux driver for the D-Link DE-620 Ethernet pocket adapter.
  6. *
  7. * Portions (C) Copyright 1993, 1994 by Bjorn Ekwall <bj0rn@blox.se>
  8. *
  9. * Based on adapter information gathered from DOS packetdriver
  10. * sources from D-Link Inc: (Special thanks to Henry Ngai of D-Link.)
  11. * Portions (C) Copyright D-Link SYSTEM Inc. 1991, 1992
  12. * Copyright, 1988, Russell Nelson, Crynwr Software
  13. *
  14. * Adapted to the sample network driver core for linux,
  15. * written by: Donald Becker <becker@super.org>
  16. * (Now at <becker@scyld.com>)
  17. *
  18. * Valuable assistance from:
  19. * J. Joshua Kopper <kopper@rtsg.mot.com>
  20. * Olav Kvittem <Olav.Kvittem@uninett.no>
  21. * Germano Caronni <caronni@nessie.cs.id.ethz.ch>
  22. * Jeremy Fitzhardinge <jeremy@suite.sw.oz.au>
  23. *
  24. *****************************************************************************/
  25. /*
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2, or (at your option)
  29. * any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39. *
  40. *****************************************************************************/
  41. static const char version[] =
  42. "de620.c: $Revision: 1.40 $, Bjorn Ekwall <bj0rn@blox.se>\n";
  43. /***********************************************************************
  44. *
  45. * "Tuning" section.
  46. *
  47. * Compile-time options: (see below for descriptions)
  48. * -DDE620_IO=0x378 (lpt1)
  49. * -DDE620_IRQ=7 (lpt1)
  50. * -DDE602_DEBUG=...
  51. * -DSHUTDOWN_WHEN_LOST
  52. * -DCOUNT_LOOPS
  53. * -DLOWSPEED
  54. * -DREAD_DELAY
  55. * -DWRITE_DELAY
  56. */
  57. /*
  58. * This driver assumes that the printer port is a "normal",
  59. * dumb, uni-directional port!
  60. * If your port is "fancy" in any way, please try to set it to "normal"
  61. * with your BIOS setup. I have no access to machines with bi-directional
  62. * ports, so I can't test such a driver :-(
  63. * (Yes, I _know_ it is possible to use DE620 with bidirectional ports...)
  64. *
  65. * There are some clones of DE620 out there, with different names.
  66. * If the current driver does not recognize a clone, try to change
  67. * the following #define to:
  68. *
  69. * #define DE620_CLONE 1
  70. */
  71. #define DE620_CLONE 0
  72. /*
  73. * If the adapter has problems with high speeds, enable this #define
  74. * otherwise full printerport speed will be attempted.
  75. *
  76. * You can tune the READ_DELAY/WRITE_DELAY below if you enable LOWSPEED
  77. *
  78. #define LOWSPEED
  79. */
  80. #ifndef READ_DELAY
  81. #define READ_DELAY 100 /* adapter internal read delay in 100ns units */
  82. #endif
  83. #ifndef WRITE_DELAY
  84. #define WRITE_DELAY 100 /* adapter internal write delay in 100ns units */
  85. #endif
  86. /*
  87. * Enable this #define if you want the adapter to do a "ifconfig down" on
  88. * itself when we have detected that something is possibly wrong with it.
  89. * The default behaviour is to retry with "adapter_init()" until success.
  90. * This should be used for debugging purposes only.
  91. *
  92. #define SHUTDOWN_WHEN_LOST
  93. */
  94. /*
  95. * Enable debugging by "-DDE620_DEBUG=3" when compiling,
  96. * OR by enabling the following #define
  97. *
  98. * use 0 for production, 1 for verification, >2 for debug
  99. *
  100. #define DE620_DEBUG 3
  101. */
  102. #ifdef LOWSPEED
  103. /*
  104. * Enable this #define if you want to see debugging output that show how long
  105. * we have to wait before the DE-620 is ready for the next read/write/command.
  106. *
  107. #define COUNT_LOOPS
  108. */
  109. #endif
  110. #include <linux/module.h>
  111. #include <linux/kernel.h>
  112. #include <linux/types.h>
  113. #include <linux/fcntl.h>
  114. #include <linux/string.h>
  115. #include <linux/interrupt.h>
  116. #include <linux/ioport.h>
  117. #include <linux/in.h>
  118. #include <linux/errno.h>
  119. #include <linux/init.h>
  120. #include <linux/inet.h>
  121. #include <linux/netdevice.h>
  122. #include <linux/etherdevice.h>
  123. #include <linux/skbuff.h>
  124. #include <asm/io.h>
  125. #include <asm/system.h>
  126. /* Constant definitions for the DE-620 registers, commands and bits */
  127. #include "de620.h"
  128. typedef unsigned char byte;
  129. /*******************************************************
  130. * *
  131. * Definition of D-Link DE-620 Ethernet Pocket adapter *
  132. * See also "de620.h" *
  133. * *
  134. *******************************************************/
  135. #ifndef DE620_IO /* Compile-time configurable */
  136. #define DE620_IO 0x378
  137. #endif
  138. #ifndef DE620_IRQ /* Compile-time configurable */
  139. #define DE620_IRQ 7
  140. #endif
  141. #define DATA_PORT (dev->base_addr)
  142. #define STATUS_PORT (dev->base_addr + 1)
  143. #define COMMAND_PORT (dev->base_addr + 2)
  144. #define RUNT 60 /* Too small Ethernet packet */
  145. #define GIANT 1514 /* largest legal size packet, no fcs */
  146. #ifdef DE620_DEBUG /* Compile-time configurable */
  147. #define PRINTK(x) if (de620_debug >= 2) printk x
  148. #else
  149. #define DE620_DEBUG 0
  150. #define PRINTK(x) /**/
  151. #endif
  152. /*
  153. * Force media with insmod:
  154. * insmod de620.o bnc=1
  155. * or
  156. * insmod de620.o utp=1
  157. *
  158. * Force io and/or irq with insmod:
  159. * insmod de620.o io=0x378 irq=7
  160. *
  161. * Make a clone skip the Ethernet-address range check:
  162. * insmod de620.o clone=1
  163. */
  164. static int bnc;
  165. static int utp;
  166. static int io = DE620_IO;
  167. static int irq = DE620_IRQ;
  168. static int clone = DE620_CLONE;
  169. static unsigned int de620_debug = DE620_DEBUG;
  170. static spinlock_t de620_lock;
  171. module_param(bnc, int, 0);
  172. module_param(utp, int, 0);
  173. module_param(io, int, 0);
  174. module_param(irq, int, 0);
  175. module_param(clone, int, 0);
  176. module_param(de620_debug, int, 0);
  177. MODULE_PARM_DESC(bnc, "DE-620 set BNC medium (0-1)");
  178. MODULE_PARM_DESC(utp, "DE-620 set UTP medium (0-1)");
  179. MODULE_PARM_DESC(io, "DE-620 I/O base address,required");
  180. MODULE_PARM_DESC(irq, "DE-620 IRQ number,required");
  181. MODULE_PARM_DESC(clone, "Check also for non-D-Link DE-620 clones (0-1)");
  182. MODULE_PARM_DESC(de620_debug, "DE-620 debug level (0-2)");
  183. /***********************************************
  184. * *
  185. * Index to functions, as function prototypes. *
  186. * *
  187. ***********************************************/
  188. /*
  189. * Routines used internally. (See also "convenience macros.. below")
  190. */
  191. /* Put in the device structure. */
  192. static int de620_open(struct net_device *);
  193. static int de620_close(struct net_device *);
  194. static void de620_set_multicast_list(struct net_device *);
  195. static int de620_start_xmit(struct sk_buff *, struct net_device *);
  196. /* Dispatch from interrupts. */
  197. static irqreturn_t de620_interrupt(int, void *);
  198. static int de620_rx_intr(struct net_device *);
  199. /* Initialization */
  200. static int adapter_init(struct net_device *);
  201. static int read_eeprom(struct net_device *);
  202. /*
  203. * D-Link driver variables:
  204. */
  205. #define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
  206. #define TCR_DEF RXPB /* not used: | TXSUCINT | T16INT */
  207. #define DE620_RX_START_PAGE 12 /* 12 pages (=3k) reserved for tx */
  208. #define DEF_NIC_CMD IRQEN | ICEN | DS1
  209. static volatile byte NIC_Cmd;
  210. static volatile byte next_rx_page;
  211. static byte first_rx_page;
  212. static byte last_rx_page;
  213. static byte EIPRegister;
  214. static struct nic {
  215. byte NodeID[6];
  216. byte RAM_Size;
  217. byte Model;
  218. byte Media;
  219. byte SCR;
  220. } nic_data;
  221. /**********************************************************
  222. * *
  223. * Convenience macros/functions for D-Link DE-620 adapter *
  224. * *
  225. **********************************************************/
  226. #define de620_tx_buffs(dd) (inb(STATUS_PORT) & (TXBF0 | TXBF1))
  227. #define de620_flip_ds(dd) NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
  228. /* Check for ready-status, and return a nibble (high 4 bits) for data input */
  229. #ifdef COUNT_LOOPS
  230. static int tot_cnt;
  231. #endif
  232. static inline byte
  233. de620_ready(struct net_device *dev)
  234. {
  235. byte value;
  236. register short int cnt = 0;
  237. while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
  238. ++cnt;
  239. #ifdef COUNT_LOOPS
  240. tot_cnt += cnt;
  241. #endif
  242. return value & 0xf0; /* nibble */
  243. }
  244. static inline void
  245. de620_send_command(struct net_device *dev, byte cmd)
  246. {
  247. de620_ready(dev);
  248. if (cmd == W_DUMMY)
  249. outb(NIC_Cmd, COMMAND_PORT);
  250. outb(cmd, DATA_PORT);
  251. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  252. de620_ready(dev);
  253. outb(NIC_Cmd, COMMAND_PORT);
  254. }
  255. static inline void
  256. de620_put_byte(struct net_device *dev, byte value)
  257. {
  258. /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
  259. de620_ready(dev);
  260. outb(value, DATA_PORT);
  261. de620_flip_ds(dev);
  262. }
  263. static inline byte
  264. de620_read_byte(struct net_device *dev)
  265. {
  266. byte value;
  267. /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
  268. value = de620_ready(dev); /* High nibble */
  269. de620_flip_ds(dev);
  270. value |= de620_ready(dev) >> 4; /* Low nibble */
  271. return value;
  272. }
  273. static inline void
  274. de620_write_block(struct net_device *dev, byte *buffer, int count, int pad)
  275. {
  276. #ifndef LOWSPEED
  277. byte uflip = NIC_Cmd ^ (DS0 | DS1);
  278. byte dflip = NIC_Cmd;
  279. #else /* LOWSPEED */
  280. #ifdef COUNT_LOOPS
  281. int bytes = count;
  282. #endif /* COUNT_LOOPS */
  283. #endif /* LOWSPEED */
  284. #ifdef LOWSPEED
  285. #ifdef COUNT_LOOPS
  286. tot_cnt = 0;
  287. #endif /* COUNT_LOOPS */
  288. /* No further optimization useful, the limit is in the adapter. */
  289. for ( ; count > 0; --count, ++buffer) {
  290. de620_put_byte(dev,*buffer);
  291. }
  292. for ( count = pad ; count > 0; --count, ++buffer) {
  293. de620_put_byte(dev, 0);
  294. }
  295. de620_send_command(dev,W_DUMMY);
  296. #ifdef COUNT_LOOPS
  297. /* trial debug output: loops per byte in de620_ready() */
  298. printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));
  299. #endif /* COUNT_LOOPS */
  300. #else /* not LOWSPEED */
  301. for ( ; count > 0; count -=2) {
  302. outb(*buffer++, DATA_PORT);
  303. outb(uflip, COMMAND_PORT);
  304. outb(*buffer++, DATA_PORT);
  305. outb(dflip, COMMAND_PORT);
  306. }
  307. de620_send_command(dev,W_DUMMY);
  308. #endif /* LOWSPEED */
  309. }
  310. static inline void
  311. de620_read_block(struct net_device *dev, byte *data, int count)
  312. {
  313. #ifndef LOWSPEED
  314. byte value;
  315. byte uflip = NIC_Cmd ^ (DS0 | DS1);
  316. byte dflip = NIC_Cmd;
  317. #else /* LOWSPEED */
  318. #ifdef COUNT_LOOPS
  319. int bytes = count;
  320. tot_cnt = 0;
  321. #endif /* COUNT_LOOPS */
  322. #endif /* LOWSPEED */
  323. #ifdef LOWSPEED
  324. /* No further optimization useful, the limit is in the adapter. */
  325. while (count-- > 0) {
  326. *data++ = de620_read_byte(dev);
  327. de620_flip_ds(dev);
  328. }
  329. #ifdef COUNT_LOOPS
  330. /* trial debug output: loops per byte in de620_ready() */
  331. printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));
  332. #endif /* COUNT_LOOPS */
  333. #else /* not LOWSPEED */
  334. while (count-- > 0) {
  335. value = inb(STATUS_PORT) & 0xf0; /* High nibble */
  336. outb(uflip, COMMAND_PORT);
  337. *data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */
  338. outb(dflip , COMMAND_PORT);
  339. }
  340. #endif /* LOWSPEED */
  341. }
  342. static inline void
  343. de620_set_delay(struct net_device *dev)
  344. {
  345. de620_ready(dev);
  346. outb(W_DFR, DATA_PORT);
  347. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  348. de620_ready(dev);
  349. #ifdef LOWSPEED
  350. outb(WRITE_DELAY, DATA_PORT);
  351. #else
  352. outb(0, DATA_PORT);
  353. #endif
  354. de620_flip_ds(dev);
  355. de620_ready(dev);
  356. #ifdef LOWSPEED
  357. outb(READ_DELAY, DATA_PORT);
  358. #else
  359. outb(0, DATA_PORT);
  360. #endif
  361. de620_flip_ds(dev);
  362. }
  363. static inline void
  364. de620_set_register(struct net_device *dev, byte reg, byte value)
  365. {
  366. de620_ready(dev);
  367. outb(reg, DATA_PORT);
  368. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  369. de620_put_byte(dev, value);
  370. }
  371. static inline byte
  372. de620_get_register(struct net_device *dev, byte reg)
  373. {
  374. byte value;
  375. de620_send_command(dev,reg);
  376. value = de620_read_byte(dev);
  377. de620_send_command(dev,W_DUMMY);
  378. return value;
  379. }
  380. /*********************************************************************
  381. *
  382. * Open/initialize the board.
  383. *
  384. * This routine should set everything up anew at each open, even
  385. * registers that "should" only need to be set once at boot, so that
  386. * there is a non-reboot way to recover if something goes wrong.
  387. *
  388. */
  389. static int de620_open(struct net_device *dev)
  390. {
  391. int ret = request_irq(dev->irq, de620_interrupt, 0, dev->name, dev);
  392. if (ret) {
  393. printk (KERN_ERR "%s: unable to get IRQ %d\n", dev->name, dev->irq);
  394. return ret;
  395. }
  396. if (adapter_init(dev)) {
  397. ret = -EIO;
  398. goto out_free_irq;
  399. }
  400. netif_start_queue(dev);
  401. return 0;
  402. out_free_irq:
  403. free_irq(dev->irq, dev);
  404. return ret;
  405. }
  406. /************************************************
  407. *
  408. * The inverse routine to de620_open().
  409. *
  410. */
  411. static int de620_close(struct net_device *dev)
  412. {
  413. netif_stop_queue(dev);
  414. /* disable recv */
  415. de620_set_register(dev, W_TCR, RXOFF);
  416. free_irq(dev->irq, dev);
  417. return 0;
  418. }
  419. /*********************************************
  420. *
  421. * Set or clear the multicast filter for this adaptor.
  422. * (no real multicast implemented for the DE-620, but she can be promiscuous...)
  423. *
  424. */
  425. static void de620_set_multicast_list(struct net_device *dev)
  426. {
  427. if (dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC))
  428. { /* Enable promiscuous mode */
  429. de620_set_register(dev, W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
  430. }
  431. else
  432. { /* Disable promiscuous mode, use normal mode */
  433. de620_set_register(dev, W_TCR, TCR_DEF);
  434. }
  435. }
  436. /*******************************************************
  437. *
  438. * Handle timeouts on transmit
  439. */
  440. static void de620_timeout(struct net_device *dev)
  441. {
  442. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name, "network cable problem");
  443. /* Restart the adapter. */
  444. if (!adapter_init(dev)) /* maybe close it */
  445. netif_wake_queue(dev);
  446. }
  447. /*******************************************************
  448. *
  449. * Copy a buffer to the adapter transmit page memory.
  450. * Start sending.
  451. */
  452. static int de620_start_xmit(struct sk_buff *skb, struct net_device *dev)
  453. {
  454. unsigned long flags;
  455. int len;
  456. byte *buffer = skb->data;
  457. byte using_txbuf;
  458. using_txbuf = de620_tx_buffs(dev); /* Peek at the adapter */
  459. netif_stop_queue(dev);
  460. if ((len = skb->len) < RUNT)
  461. len = RUNT;
  462. if (len & 1) /* send an even number of bytes */
  463. ++len;
  464. /* Start real output */
  465. spin_lock_irqsave(&de620_lock, flags)
  466. PRINTK(("de620_start_xmit: len=%d, bufs 0x%02x\n",
  467. (int)skb->len, using_txbuf));
  468. /* select a free tx buffer. if there is one... */
  469. switch (using_txbuf) {
  470. default: /* both are free: use TXBF0 */
  471. case TXBF1: /* use TXBF0 */
  472. de620_send_command(dev,W_CR | RW0);
  473. using_txbuf |= TXBF0;
  474. break;
  475. case TXBF0: /* use TXBF1 */
  476. de620_send_command(dev,W_CR | RW1);
  477. using_txbuf |= TXBF1;
  478. break;
  479. case (TXBF0 | TXBF1): /* NONE!!! */
  480. printk(KERN_WARNING "%s: No tx-buffer available!\n", dev->name);
  481. spin_unlock_irqrestore(&de620_lock, flags);
  482. return 1;
  483. }
  484. de620_write_block(dev, buffer, skb->len, len-skb->len);
  485. dev->trans_start = jiffies;
  486. if(!(using_txbuf == (TXBF0 | TXBF1)))
  487. netif_wake_queue(dev);
  488. dev->stats.tx_packets++;
  489. spin_unlock_irqrestore(&de620_lock, flags);
  490. dev_kfree_skb (skb);
  491. return 0;
  492. }
  493. /*****************************************************
  494. *
  495. * Handle the network interface interrupts.
  496. *
  497. */
  498. static irqreturn_t
  499. de620_interrupt(int irq_in, void *dev_id)
  500. {
  501. struct net_device *dev = dev_id;
  502. byte irq_status;
  503. int bogus_count = 0;
  504. int again = 0;
  505. spin_lock(&de620_lock);
  506. /* Read the status register (_not_ the status port) */
  507. irq_status = de620_get_register(dev, R_STS);
  508. PRINTK(("de620_interrupt (%2.2X)\n", irq_status));
  509. if (irq_status & RXGOOD) {
  510. do {
  511. again = de620_rx_intr(dev);
  512. PRINTK(("again=%d\n", again));
  513. }
  514. while (again && (++bogus_count < 100));
  515. }
  516. if(de620_tx_buffs(dev) != (TXBF0 | TXBF1))
  517. netif_wake_queue(dev);
  518. spin_unlock(&de620_lock);
  519. return IRQ_HANDLED;
  520. }
  521. /**************************************
  522. *
  523. * Get a packet from the adapter
  524. *
  525. * Send it "upstairs"
  526. *
  527. */
  528. static int de620_rx_intr(struct net_device *dev)
  529. {
  530. struct header_buf {
  531. byte status;
  532. byte Rx_NextPage;
  533. unsigned short Rx_ByteCount;
  534. } header_buf;
  535. struct sk_buff *skb;
  536. int size;
  537. byte *buffer;
  538. byte pagelink;
  539. byte curr_page;
  540. PRINTK(("de620_rx_intr: next_rx_page = %d\n", next_rx_page));
  541. /* Tell the adapter that we are going to read data, and from where */
  542. de620_send_command(dev, W_CR | RRN);
  543. de620_set_register(dev, W_RSA1, next_rx_page);
  544. de620_set_register(dev, W_RSA0, 0);
  545. /* Deep breath, and away we goooooo */
  546. de620_read_block(dev, (byte *)&header_buf, sizeof(struct header_buf));
  547. PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%d\n",
  548. header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
  549. /* Plausible page header? */
  550. pagelink = header_buf.Rx_NextPage;
  551. if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
  552. /* Ouch... Forget it! Skip all and start afresh... */
  553. printk(KERN_WARNING "%s: Ring overrun? Restoring...\n", dev->name);
  554. /* You win some, you lose some. And sometimes plenty... */
  555. adapter_init(dev);
  556. netif_wake_queue(dev);
  557. dev->stats.rx_over_errors++;
  558. return 0;
  559. }
  560. /* OK, this look good, so far. Let's see if it's consistent... */
  561. /* Let's compute the start of the next packet, based on where we are */
  562. pagelink = next_rx_page +
  563. ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
  564. /* Are we going to wrap around the page counter? */
  565. if (pagelink > last_rx_page)
  566. pagelink -= (last_rx_page - first_rx_page + 1);
  567. /* Is the _computed_ next page number equal to what the adapter says? */
  568. if (pagelink != header_buf.Rx_NextPage) {
  569. /* Naah, we'll skip this packet. Probably bogus data as well */
  570. printk(KERN_WARNING "%s: Page link out of sync! Restoring...\n", dev->name);
  571. next_rx_page = header_buf.Rx_NextPage; /* at least a try... */
  572. de620_send_command(dev, W_DUMMY);
  573. de620_set_register(dev, W_NPRF, next_rx_page);
  574. dev->stats.rx_over_errors++;
  575. return 0;
  576. }
  577. next_rx_page = pagelink;
  578. size = header_buf.Rx_ByteCount - 4;
  579. if ((size < RUNT) || (GIANT < size)) {
  580. printk(KERN_WARNING "%s: Illegal packet size: %d!\n", dev->name, size);
  581. }
  582. else { /* Good packet? */
  583. skb = dev_alloc_skb(size+2);
  584. if (skb == NULL) { /* Yeah, but no place to put it... */
  585. printk(KERN_WARNING "%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, size);
  586. dev->stats.rx_dropped++;
  587. }
  588. else { /* Yep! Go get it! */
  589. skb_reserve(skb,2); /* Align */
  590. /* skb->data points to the start of sk_buff data area */
  591. buffer = skb_put(skb,size);
  592. /* copy the packet into the buffer */
  593. de620_read_block(dev, buffer, size);
  594. PRINTK(("Read %d bytes\n", size));
  595. skb->protocol=eth_type_trans(skb,dev);
  596. netif_rx(skb); /* deliver it "upstairs" */
  597. dev->last_rx = jiffies;
  598. /* count all receives */
  599. dev->stats.rx_packets++;
  600. dev->stats.rx_bytes += size;
  601. }
  602. }
  603. /* Let's peek ahead to see if we have read the last current packet */
  604. /* NOTE! We're _not_ checking the 'EMPTY'-flag! This seems better... */
  605. curr_page = de620_get_register(dev, R_CPR);
  606. de620_set_register(dev, W_NPRF, next_rx_page);
  607. PRINTK(("next_rx_page=%d CPR=%d\n", next_rx_page, curr_page));
  608. return (next_rx_page != curr_page); /* That was slightly tricky... */
  609. }
  610. /*********************************************
  611. *
  612. * Reset the adapter to a known state
  613. *
  614. */
  615. static int adapter_init(struct net_device *dev)
  616. {
  617. int i;
  618. static int was_down;
  619. if ((nic_data.Model == 3) || (nic_data.Model == 0)) { /* CT */
  620. EIPRegister = NCTL0;
  621. if (nic_data.Media != 1)
  622. EIPRegister |= NIS0; /* not BNC */
  623. }
  624. else if (nic_data.Model == 2) { /* UTP */
  625. EIPRegister = NCTL0 | NIS0;
  626. }
  627. if (utp)
  628. EIPRegister = NCTL0 | NIS0;
  629. if (bnc)
  630. EIPRegister = NCTL0;
  631. de620_send_command(dev, W_CR | RNOP | CLEAR);
  632. de620_send_command(dev, W_CR | RNOP);
  633. de620_set_register(dev, W_SCR, SCR_DEF);
  634. /* disable recv to wait init */
  635. de620_set_register(dev, W_TCR, RXOFF);
  636. /* Set the node ID in the adapter */
  637. for (i = 0; i < 6; ++i) { /* W_PARn = 0xaa + n */
  638. de620_set_register(dev, W_PAR0 + i, dev->dev_addr[i]);
  639. }
  640. de620_set_register(dev, W_EIP, EIPRegister);
  641. next_rx_page = first_rx_page = DE620_RX_START_PAGE;
  642. if (nic_data.RAM_Size)
  643. last_rx_page = nic_data.RAM_Size - 1;
  644. else /* 64k RAM */
  645. last_rx_page = 255;
  646. de620_set_register(dev, W_SPR, first_rx_page); /* Start Page Register*/
  647. de620_set_register(dev, W_EPR, last_rx_page); /* End Page Register */
  648. de620_set_register(dev, W_CPR, first_rx_page);/*Current Page Register*/
  649. de620_send_command(dev, W_NPR | first_rx_page); /* Next Page Register*/
  650. de620_send_command(dev, W_DUMMY);
  651. de620_set_delay(dev);
  652. /* Final sanity check: Anybody out there? */
  653. /* Let's hope some bits from the statusregister make a good check */
  654. #define CHECK_MASK ( 0 | TXSUC | T16 | 0 | RXCRC | RXSHORT | 0 | 0 )
  655. #define CHECK_OK ( 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 )
  656. /* success: X 0 0 X 0 0 X X */
  657. /* ignore: EEDI RXGOOD COLS LNKS*/
  658. if (((i = de620_get_register(dev, R_STS)) & CHECK_MASK) != CHECK_OK) {
  659. printk(KERN_ERR "%s: Something has happened to the DE-620! Please check it"
  660. #ifdef SHUTDOWN_WHEN_LOST
  661. " and do a new ifconfig"
  662. #endif
  663. "! (%02x)\n", dev->name, i);
  664. #ifdef SHUTDOWN_WHEN_LOST
  665. /* Goodbye, cruel world... */
  666. dev->flags &= ~IFF_UP;
  667. de620_close(dev);
  668. #endif
  669. was_down = 1;
  670. return 1; /* failed */
  671. }
  672. if (was_down) {
  673. printk(KERN_WARNING "%s: Thanks, I feel much better now!\n", dev->name);
  674. was_down = 0;
  675. }
  676. /* All OK, go ahead... */
  677. de620_set_register(dev, W_TCR, TCR_DEF);
  678. return 0; /* all ok */
  679. }
  680. /******************************************************************************
  681. *
  682. * Only start-up code below
  683. *
  684. */
  685. /****************************************
  686. *
  687. * Check if there is a DE-620 connected
  688. */
  689. struct net_device * __init de620_probe(int unit)
  690. {
  691. byte checkbyte = 0xa5;
  692. struct net_device *dev;
  693. int err = -ENOMEM;
  694. int i;
  695. DECLARE_MAC_BUF(mac);
  696. dev = alloc_etherdev(0);
  697. if (!dev)
  698. goto out;
  699. spin_lock_init(&de620_lock);
  700. /*
  701. * This is where the base_addr and irq gets set.
  702. * Tunable at compile-time and insmod-time
  703. */
  704. dev->base_addr = io;
  705. dev->irq = irq;
  706. /* allow overriding parameters on command line */
  707. if (unit >= 0) {
  708. sprintf(dev->name, "eth%d", unit);
  709. netdev_boot_setup_check(dev);
  710. }
  711. if (de620_debug)
  712. printk(version);
  713. printk(KERN_INFO "D-Link DE-620 pocket adapter");
  714. if (!request_region(dev->base_addr, 3, "de620")) {
  715. printk(" io 0x%3lX, which is busy.\n", dev->base_addr);
  716. err = -EBUSY;
  717. goto out1;
  718. }
  719. /* Initially, configure basic nibble mode, so we can read the EEPROM */
  720. NIC_Cmd = DEF_NIC_CMD;
  721. de620_set_register(dev, W_EIP, EIPRegister);
  722. /* Anybody out there? */
  723. de620_set_register(dev, W_CPR, checkbyte);
  724. checkbyte = de620_get_register(dev, R_CPR);
  725. if ((checkbyte != 0xa5) || (read_eeprom(dev) != 0)) {
  726. printk(" not identified in the printer port\n");
  727. err = -ENODEV;
  728. goto out2;
  729. }
  730. /* else, got it! */
  731. dev->dev_addr[0] = nic_data.NodeID[0];
  732. for (i = 1; i < ETH_ALEN; i++) {
  733. dev->dev_addr[i] = nic_data.NodeID[i];
  734. dev->broadcast[i] = 0xff;
  735. }
  736. printk(", Ethernet Address: %s", print_mac(mac, dev->dev_addr));
  737. printk(" (%dk RAM,",
  738. (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
  739. if (nic_data.Media == 1)
  740. printk(" BNC)\n");
  741. else
  742. printk(" UTP)\n");
  743. dev->open = de620_open;
  744. dev->stop = de620_close;
  745. dev->hard_start_xmit = de620_start_xmit;
  746. dev->tx_timeout = de620_timeout;
  747. dev->watchdog_timeo = HZ*2;
  748. dev->set_multicast_list = de620_set_multicast_list;
  749. /* base_addr and irq are already set, see above! */
  750. /* dump eeprom */
  751. if (de620_debug) {
  752. printk("\nEEPROM contents:\n");
  753. printk("RAM_Size = 0x%02X\n", nic_data.RAM_Size);
  754. printk("NodeID = %02X:%02X:%02X:%02X:%02X:%02X\n",
  755. nic_data.NodeID[0], nic_data.NodeID[1],
  756. nic_data.NodeID[2], nic_data.NodeID[3],
  757. nic_data.NodeID[4], nic_data.NodeID[5]);
  758. printk("Model = %d\n", nic_data.Model);
  759. printk("Media = %d\n", nic_data.Media);
  760. printk("SCR = 0x%02x\n", nic_data.SCR);
  761. }
  762. err = register_netdev(dev);
  763. if (err)
  764. goto out2;
  765. return dev;
  766. out2:
  767. release_region(dev->base_addr, 3);
  768. out1:
  769. free_netdev(dev);
  770. out:
  771. return ERR_PTR(err);
  772. }
  773. /**********************************
  774. *
  775. * Read info from on-board EEPROM
  776. *
  777. * Note: Bitwise serial I/O to/from the EEPROM vi the status _register_!
  778. */
  779. #define sendit(dev,data) de620_set_register(dev, W_EIP, data | EIPRegister);
  780. static unsigned short __init ReadAWord(struct net_device *dev, int from)
  781. {
  782. unsigned short data;
  783. int nbits;
  784. /* cs [__~~] SET SEND STATE */
  785. /* di [____] */
  786. /* sck [_~~_] */
  787. sendit(dev, 0); sendit(dev, 1); sendit(dev, 5); sendit(dev, 4);
  788. /* Send the 9-bit address from where we want to read the 16-bit word */
  789. for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
  790. if (from & 0x0100) { /* bit set? */
  791. /* cs [~~~~] SEND 1 */
  792. /* di [~~~~] */
  793. /* sck [_~~_] */
  794. sendit(dev, 6); sendit(dev, 7); sendit(dev, 7); sendit(dev, 6);
  795. }
  796. else {
  797. /* cs [~~~~] SEND 0 */
  798. /* di [____] */
  799. /* sck [_~~_] */
  800. sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
  801. }
  802. }
  803. /* Shift in the 16-bit word. The bits appear serially in EEDI (=0x80) */
  804. for (data = 0, nbits = 16; nbits > 0; --nbits) {
  805. /* cs [~~~~] SEND 0 */
  806. /* di [____] */
  807. /* sck [_~~_] */
  808. sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
  809. data = (data << 1) | ((de620_get_register(dev, R_STS) & EEDI) >> 7);
  810. }
  811. /* cs [____] RESET SEND STATE */
  812. /* di [____] */
  813. /* sck [_~~_] */
  814. sendit(dev, 0); sendit(dev, 1); sendit(dev, 1); sendit(dev, 0);
  815. return data;
  816. }
  817. static int __init read_eeprom(struct net_device *dev)
  818. {
  819. unsigned short wrd;
  820. /* D-Link Ethernet addresses are in the series 00:80:c8:7X:XX:XX:XX */
  821. wrd = ReadAWord(dev, 0x1aa); /* bytes 0 + 1 of NodeID */
  822. if (!clone && (wrd != htons(0x0080))) /* Valid D-Link ether sequence? */
  823. return -1; /* Nope, not a DE-620 */
  824. nic_data.NodeID[0] = wrd & 0xff;
  825. nic_data.NodeID[1] = wrd >> 8;
  826. wrd = ReadAWord(dev, 0x1ab); /* bytes 2 + 3 of NodeID */
  827. if (!clone && ((wrd & 0xff) != 0xc8)) /* Valid D-Link ether sequence? */
  828. return -1; /* Nope, not a DE-620 */
  829. nic_data.NodeID[2] = wrd & 0xff;
  830. nic_data.NodeID[3] = wrd >> 8;
  831. wrd = ReadAWord(dev, 0x1ac); /* bytes 4 + 5 of NodeID */
  832. nic_data.NodeID[4] = wrd & 0xff;
  833. nic_data.NodeID[5] = wrd >> 8;
  834. wrd = ReadAWord(dev, 0x1ad); /* RAM size in pages (256 bytes). 0 = 64k */
  835. nic_data.RAM_Size = (wrd >> 8);
  836. wrd = ReadAWord(dev, 0x1ae); /* hardware model (CT = 3) */
  837. nic_data.Model = (wrd & 0xff);
  838. wrd = ReadAWord(dev, 0x1af); /* media (indicates BNC/UTP) */
  839. nic_data.Media = (wrd & 0xff);
  840. wrd = ReadAWord(dev, 0x1a8); /* System Configuration Register */
  841. nic_data.SCR = (wrd >> 8);
  842. return 0; /* no errors */
  843. }
  844. /******************************************************************************
  845. *
  846. * Loadable module skeleton
  847. *
  848. */
  849. #ifdef MODULE
  850. static struct net_device *de620_dev;
  851. int __init init_module(void)
  852. {
  853. de620_dev = de620_probe(-1);
  854. if (IS_ERR(de620_dev))
  855. return PTR_ERR(de620_dev);
  856. return 0;
  857. }
  858. void cleanup_module(void)
  859. {
  860. unregister_netdev(de620_dev);
  861. release_region(de620_dev->base_addr, 3);
  862. free_netdev(de620_dev);
  863. }
  864. #endif /* MODULE */
  865. MODULE_LICENSE("GPL");
  866. /*
  867. * (add '-DMODULE' when compiling as loadable module)
  868. *
  869. * compile-command:
  870. * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O2 \
  871. * -fomit-frame-pointer -m486 \
  872. * -I/usr/src/linux/include -I../../net/inet -c de620.c
  873. */
  874. /*
  875. * Local variables:
  876. * kernel-compile-command: "gcc -D__KERNEL__ -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  877. * module-compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  878. * compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  879. * End:
  880. */