de620.c 28 KB

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