de620.c 26 KB

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