de620.c 26 KB

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