ether1.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * linux/drivers/acorn/net/ether1.c
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Acorn ether1 driver (82586 chip) for Acorn machines
  11. *
  12. * We basically keep two queues in the cards memory - one for transmit
  13. * and one for receive. Each has a head and a tail. The head is where
  14. * we/the chip adds packets to be transmitted/received, and the tail
  15. * is where the transmitter has got to/where the receiver will stop.
  16. * Both of these queues are circular, and since the chip is running
  17. * all the time, we have to be careful when we modify the pointers etc
  18. * so that the buffer memory contents is valid all the time.
  19. *
  20. * Change log:
  21. * 1.00 RMK Released
  22. * 1.01 RMK 19/03/1996 Transfers the last odd byte onto/off of the card now.
  23. * 1.02 RMK 25/05/1997 Added code to restart RU if it goes not ready
  24. * 1.03 RMK 14/09/1997 Cleaned up the handling of a reset during the TX interrupt.
  25. * Should prevent lockup.
  26. * 1.04 RMK 17/09/1997 Added more info when initialsation of chip goes wrong.
  27. * TDR now only reports failure when chip reports non-zero
  28. * TDR time-distance.
  29. * 1.05 RMK 31/12/1997 Removed calls to dev_tint for 2.1
  30. * 1.06 RMK 10/02/2000 Updated for 2.3.43
  31. * 1.07 RMK 13/05/2000 Updated for 2.3.99-pre8
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/ioport.h>
  39. #include <linux/in.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <linux/errno.h>
  43. #include <linux/device.h>
  44. #include <linux/init.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/bitops.h>
  49. #include <asm/system.h>
  50. #include <asm/io.h>
  51. #include <asm/dma.h>
  52. #include <asm/ecard.h>
  53. #define __ETHER1_C
  54. #include "ether1.h"
  55. static unsigned int net_debug = NET_DEBUG;
  56. #define BUFFER_SIZE 0x10000
  57. #define TX_AREA_START 0x00100
  58. #define TX_AREA_END 0x05000
  59. #define RX_AREA_START 0x05000
  60. #define RX_AREA_END 0x0fc00
  61. static int ether1_open(struct net_device *dev);
  62. static int ether1_sendpacket(struct sk_buff *skb, struct net_device *dev);
  63. static irqreturn_t ether1_interrupt(int irq, void *dev_id);
  64. static int ether1_close(struct net_device *dev);
  65. static struct net_device_stats *ether1_getstats(struct net_device *dev);
  66. static void ether1_setmulticastlist(struct net_device *dev);
  67. static void ether1_timeout(struct net_device *dev);
  68. /* ------------------------------------------------------------------------- */
  69. static char version[] __devinitdata = "ether1 ethernet driver (c) 2000 Russell King v1.07\n";
  70. #define BUS_16 16
  71. #define BUS_8 8
  72. /* ------------------------------------------------------------------------- */
  73. #define DISABLEIRQS 1
  74. #define NORMALIRQS 0
  75. #define ether1_readw(dev, addr, type, offset, svflgs) ether1_inw_p (dev, addr + (int)(&((type *)0)->offset), svflgs)
  76. #define ether1_writew(dev, val, addr, type, offset, svflgs) ether1_outw_p (dev, val, addr + (int)(&((type *)0)->offset), svflgs)
  77. static inline unsigned short
  78. ether1_inw_p (struct net_device *dev, int addr, int svflgs)
  79. {
  80. unsigned long flags;
  81. unsigned short ret;
  82. if (svflgs)
  83. local_irq_save (flags);
  84. writeb(addr >> 12, REG_PAGE);
  85. ret = readw(ETHER1_RAM + ((addr & 4095) << 1));
  86. if (svflgs)
  87. local_irq_restore (flags);
  88. return ret;
  89. }
  90. static inline void
  91. ether1_outw_p (struct net_device *dev, unsigned short val, int addr, int svflgs)
  92. {
  93. unsigned long flags;
  94. if (svflgs)
  95. local_irq_save (flags);
  96. writeb(addr >> 12, REG_PAGE);
  97. writew(val, ETHER1_RAM + ((addr & 4095) << 1));
  98. if (svflgs)
  99. local_irq_restore (flags);
  100. }
  101. /*
  102. * Some inline assembler to allow fast transfers on to/off of the card.
  103. * Since this driver depends on some features presented by the ARM
  104. * specific architecture, and that you can't configure this driver
  105. * without specifiing ARM mode, this is not a problem.
  106. *
  107. * This routine is essentially an optimised memcpy from the card's
  108. * onboard RAM to kernel memory.
  109. */
  110. static void
  111. ether1_writebuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
  112. {
  113. unsigned int page, thislen, offset;
  114. void __iomem *addr;
  115. offset = start & 4095;
  116. page = start >> 12;
  117. addr = ETHER1_RAM + (offset << 1);
  118. if (offset + length > 4096)
  119. thislen = 4096 - offset;
  120. else
  121. thislen = length;
  122. do {
  123. int used;
  124. writeb(page, REG_PAGE);
  125. length -= thislen;
  126. __asm__ __volatile__(
  127. "subs %3, %3, #2\n\
  128. bmi 2f\n\
  129. 1: ldr %0, [%1], #2\n\
  130. mov %0, %0, lsl #16\n\
  131. orr %0, %0, %0, lsr #16\n\
  132. str %0, [%2], #4\n\
  133. subs %3, %3, #2\n\
  134. bmi 2f\n\
  135. ldr %0, [%1], #2\n\
  136. mov %0, %0, lsl #16\n\
  137. orr %0, %0, %0, lsr #16\n\
  138. str %0, [%2], #4\n\
  139. subs %3, %3, #2\n\
  140. bmi 2f\n\
  141. ldr %0, [%1], #2\n\
  142. mov %0, %0, lsl #16\n\
  143. orr %0, %0, %0, lsr #16\n\
  144. str %0, [%2], #4\n\
  145. subs %3, %3, #2\n\
  146. bmi 2f\n\
  147. ldr %0, [%1], #2\n\
  148. mov %0, %0, lsl #16\n\
  149. orr %0, %0, %0, lsr #16\n\
  150. str %0, [%2], #4\n\
  151. subs %3, %3, #2\n\
  152. bpl 1b\n\
  153. 2: adds %3, %3, #1\n\
  154. ldreqb %0, [%1]\n\
  155. streqb %0, [%2]"
  156. : "=&r" (used), "=&r" (data)
  157. : "r" (addr), "r" (thislen), "1" (data));
  158. addr = ETHER1_RAM;
  159. thislen = length;
  160. if (thislen > 4096)
  161. thislen = 4096;
  162. page++;
  163. } while (thislen);
  164. }
  165. static void
  166. ether1_readbuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
  167. {
  168. unsigned int page, thislen, offset;
  169. void __iomem *addr;
  170. offset = start & 4095;
  171. page = start >> 12;
  172. addr = ETHER1_RAM + (offset << 1);
  173. if (offset + length > 4096)
  174. thislen = 4096 - offset;
  175. else
  176. thislen = length;
  177. do {
  178. int used;
  179. writeb(page, REG_PAGE);
  180. length -= thislen;
  181. __asm__ __volatile__(
  182. "subs %3, %3, #2\n\
  183. bmi 2f\n\
  184. 1: ldr %0, [%2], #4\n\
  185. strb %0, [%1], #1\n\
  186. mov %0, %0, lsr #8\n\
  187. strb %0, [%1], #1\n\
  188. subs %3, %3, #2\n\
  189. bmi 2f\n\
  190. ldr %0, [%2], #4\n\
  191. strb %0, [%1], #1\n\
  192. mov %0, %0, lsr #8\n\
  193. strb %0, [%1], #1\n\
  194. subs %3, %3, #2\n\
  195. bmi 2f\n\
  196. ldr %0, [%2], #4\n\
  197. strb %0, [%1], #1\n\
  198. mov %0, %0, lsr #8\n\
  199. strb %0, [%1], #1\n\
  200. subs %3, %3, #2\n\
  201. bmi 2f\n\
  202. ldr %0, [%2], #4\n\
  203. strb %0, [%1], #1\n\
  204. mov %0, %0, lsr #8\n\
  205. strb %0, [%1], #1\n\
  206. subs %3, %3, #2\n\
  207. bpl 1b\n\
  208. 2: adds %3, %3, #1\n\
  209. ldreqb %0, [%2]\n\
  210. streqb %0, [%1]"
  211. : "=&r" (used), "=&r" (data)
  212. : "r" (addr), "r" (thislen), "1" (data));
  213. addr = ETHER1_RAM;
  214. thislen = length;
  215. if (thislen > 4096)
  216. thislen = 4096;
  217. page++;
  218. } while (thislen);
  219. }
  220. static int __devinit
  221. ether1_ramtest(struct net_device *dev, unsigned char byte)
  222. {
  223. unsigned char *buffer = kmalloc (BUFFER_SIZE, GFP_KERNEL);
  224. int i, ret = BUFFER_SIZE;
  225. int max_errors = 15;
  226. int bad = -1;
  227. int bad_start = 0;
  228. if (!buffer)
  229. return 1;
  230. memset (buffer, byte, BUFFER_SIZE);
  231. ether1_writebuffer (dev, buffer, 0, BUFFER_SIZE);
  232. memset (buffer, byte ^ 0xff, BUFFER_SIZE);
  233. ether1_readbuffer (dev, buffer, 0, BUFFER_SIZE);
  234. for (i = 0; i < BUFFER_SIZE; i++) {
  235. if (buffer[i] != byte) {
  236. if (max_errors >= 0 && bad != buffer[i]) {
  237. if (bad != -1)
  238. printk ("\n");
  239. printk (KERN_CRIT "%s: RAM failed with (%02X instead of %02X) at 0x%04X",
  240. dev->name, buffer[i], byte, i);
  241. ret = -ENODEV;
  242. max_errors --;
  243. bad = buffer[i];
  244. bad_start = i;
  245. }
  246. } else {
  247. if (bad != -1) {
  248. if (bad_start == i - 1)
  249. printk ("\n");
  250. else
  251. printk (" - 0x%04X\n", i - 1);
  252. bad = -1;
  253. }
  254. }
  255. }
  256. if (bad != -1)
  257. printk (" - 0x%04X\n", BUFFER_SIZE);
  258. kfree (buffer);
  259. return ret;
  260. }
  261. static int
  262. ether1_reset (struct net_device *dev)
  263. {
  264. writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
  265. return BUS_16;
  266. }
  267. static int __devinit
  268. ether1_init_2(struct net_device *dev)
  269. {
  270. int i;
  271. dev->mem_start = 0;
  272. i = ether1_ramtest (dev, 0x5a);
  273. if (i > 0)
  274. i = ether1_ramtest (dev, 0x1e);
  275. if (i <= 0)
  276. return -ENODEV;
  277. dev->mem_end = i;
  278. return 0;
  279. }
  280. /*
  281. * These are the structures that are loaded into the ether RAM card to
  282. * initialise the 82586
  283. */
  284. /* at 0x0100 */
  285. #define NOP_ADDR (TX_AREA_START)
  286. #define NOP_SIZE (0x06)
  287. static nop_t init_nop = {
  288. 0,
  289. CMD_NOP,
  290. NOP_ADDR
  291. };
  292. /* at 0x003a */
  293. #define TDR_ADDR (0x003a)
  294. #define TDR_SIZE (0x08)
  295. static tdr_t init_tdr = {
  296. 0,
  297. CMD_TDR | CMD_INTR,
  298. NOP_ADDR,
  299. 0
  300. };
  301. /* at 0x002e */
  302. #define MC_ADDR (0x002e)
  303. #define MC_SIZE (0x0c)
  304. static mc_t init_mc = {
  305. 0,
  306. CMD_SETMULTICAST,
  307. TDR_ADDR,
  308. 0,
  309. { { 0, } }
  310. };
  311. /* at 0x0022 */
  312. #define SA_ADDR (0x0022)
  313. #define SA_SIZE (0x0c)
  314. static sa_t init_sa = {
  315. 0,
  316. CMD_SETADDRESS,
  317. MC_ADDR,
  318. { 0, }
  319. };
  320. /* at 0x0010 */
  321. #define CFG_ADDR (0x0010)
  322. #define CFG_SIZE (0x12)
  323. static cfg_t init_cfg = {
  324. 0,
  325. CMD_CONFIG,
  326. SA_ADDR,
  327. 8,
  328. 8,
  329. CFG8_SRDY,
  330. CFG9_PREAMB8 | CFG9_ADDRLENBUF | CFG9_ADDRLEN(6),
  331. 0,
  332. 0x60,
  333. 0,
  334. CFG13_RETRY(15) | CFG13_SLOTH(2),
  335. 0,
  336. };
  337. /* at 0x0000 */
  338. #define SCB_ADDR (0x0000)
  339. #define SCB_SIZE (0x10)
  340. static scb_t init_scb = {
  341. 0,
  342. SCB_CMDACKRNR | SCB_CMDACKCNA | SCB_CMDACKFR | SCB_CMDACKCX,
  343. CFG_ADDR,
  344. RX_AREA_START,
  345. 0,
  346. 0,
  347. 0,
  348. 0
  349. };
  350. /* at 0xffee */
  351. #define ISCP_ADDR (0xffee)
  352. #define ISCP_SIZE (0x08)
  353. static iscp_t init_iscp = {
  354. 1,
  355. SCB_ADDR,
  356. 0x0000,
  357. 0x0000
  358. };
  359. /* at 0xfff6 */
  360. #define SCP_ADDR (0xfff6)
  361. #define SCP_SIZE (0x0a)
  362. static scp_t init_scp = {
  363. SCP_SY_16BBUS,
  364. { 0, 0 },
  365. ISCP_ADDR,
  366. 0
  367. };
  368. #define RFD_SIZE (0x16)
  369. static rfd_t init_rfd = {
  370. 0,
  371. 0,
  372. 0,
  373. 0,
  374. { 0, },
  375. { 0, },
  376. 0
  377. };
  378. #define RBD_SIZE (0x0a)
  379. static rbd_t init_rbd = {
  380. 0,
  381. 0,
  382. 0,
  383. 0,
  384. ETH_FRAME_LEN + 8
  385. };
  386. #define TX_SIZE (0x08)
  387. #define TBD_SIZE (0x08)
  388. static int
  389. ether1_init_for_open (struct net_device *dev)
  390. {
  391. int i, status, addr, next, next2;
  392. int failures = 0;
  393. unsigned long timeout;
  394. writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
  395. for (i = 0; i < 6; i++)
  396. init_sa.sa_addr[i] = dev->dev_addr[i];
  397. /* load data structures into ether1 RAM */
  398. ether1_writebuffer (dev, &init_scp, SCP_ADDR, SCP_SIZE);
  399. ether1_writebuffer (dev, &init_iscp, ISCP_ADDR, ISCP_SIZE);
  400. ether1_writebuffer (dev, &init_scb, SCB_ADDR, SCB_SIZE);
  401. ether1_writebuffer (dev, &init_cfg, CFG_ADDR, CFG_SIZE);
  402. ether1_writebuffer (dev, &init_sa, SA_ADDR, SA_SIZE);
  403. ether1_writebuffer (dev, &init_mc, MC_ADDR, MC_SIZE);
  404. ether1_writebuffer (dev, &init_tdr, TDR_ADDR, TDR_SIZE);
  405. ether1_writebuffer (dev, &init_nop, NOP_ADDR, NOP_SIZE);
  406. if (ether1_readw(dev, CFG_ADDR, cfg_t, cfg_command, NORMALIRQS) != CMD_CONFIG) {
  407. printk (KERN_ERR "%s: detected either RAM fault or compiler bug\n",
  408. dev->name);
  409. return 1;
  410. }
  411. /*
  412. * setup circularly linked list of { rfd, rbd, buffer }, with
  413. * all rfds circularly linked, rbds circularly linked.
  414. * First rfd is linked to scp, first rbd is linked to first
  415. * rfd. Last rbd has a suspend command.
  416. */
  417. addr = RX_AREA_START;
  418. do {
  419. next = addr + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
  420. next2 = next + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
  421. if (next2 >= RX_AREA_END) {
  422. next = RX_AREA_START;
  423. init_rfd.rfd_command = RFD_CMDEL | RFD_CMDSUSPEND;
  424. priv(dev)->rx_tail = addr;
  425. } else
  426. init_rfd.rfd_command = 0;
  427. if (addr == RX_AREA_START)
  428. init_rfd.rfd_rbdoffset = addr + RFD_SIZE;
  429. else
  430. init_rfd.rfd_rbdoffset = 0;
  431. init_rfd.rfd_link = next;
  432. init_rbd.rbd_link = next + RFD_SIZE;
  433. init_rbd.rbd_bufl = addr + RFD_SIZE + RBD_SIZE;
  434. ether1_writebuffer (dev, &init_rfd, addr, RFD_SIZE);
  435. ether1_writebuffer (dev, &init_rbd, addr + RFD_SIZE, RBD_SIZE);
  436. addr = next;
  437. } while (next2 < RX_AREA_END);
  438. priv(dev)->tx_link = NOP_ADDR;
  439. priv(dev)->tx_head = NOP_ADDR + NOP_SIZE;
  440. priv(dev)->tx_tail = TDR_ADDR;
  441. priv(dev)->rx_head = RX_AREA_START;
  442. /* release reset & give 586 a prod */
  443. priv(dev)->resetting = 1;
  444. priv(dev)->initialising = 1;
  445. writeb(CTRL_RST, REG_CONTROL);
  446. writeb(0, REG_CONTROL);
  447. writeb(CTRL_CA, REG_CONTROL);
  448. /* 586 should now unset iscp.busy */
  449. timeout = jiffies + HZ/2;
  450. while (ether1_readw(dev, ISCP_ADDR, iscp_t, iscp_busy, DISABLEIRQS) == 1) {
  451. if (time_after(jiffies, timeout)) {
  452. printk (KERN_WARNING "%s: can't initialise 82586: iscp is busy\n", dev->name);
  453. return 1;
  454. }
  455. }
  456. /* check status of commands that we issued */
  457. timeout += HZ/10;
  458. while (((status = ether1_readw(dev, CFG_ADDR, cfg_t, cfg_status, DISABLEIRQS))
  459. & STAT_COMPLETE) == 0) {
  460. if (time_after(jiffies, timeout))
  461. break;
  462. }
  463. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  464. printk (KERN_WARNING "%s: can't initialise 82586: config status %04X\n", dev->name, status);
  465. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  466. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  467. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  468. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  469. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  470. failures += 1;
  471. }
  472. timeout += HZ/10;
  473. while (((status = ether1_readw(dev, SA_ADDR, sa_t, sa_status, DISABLEIRQS))
  474. & STAT_COMPLETE) == 0) {
  475. if (time_after(jiffies, timeout))
  476. break;
  477. }
  478. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  479. printk (KERN_WARNING "%s: can't initialise 82586: set address status %04X\n", dev->name, status);
  480. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  481. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  482. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  483. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  484. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  485. failures += 1;
  486. }
  487. timeout += HZ/10;
  488. while (((status = ether1_readw(dev, MC_ADDR, mc_t, mc_status, DISABLEIRQS))
  489. & STAT_COMPLETE) == 0) {
  490. if (time_after(jiffies, timeout))
  491. break;
  492. }
  493. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  494. printk (KERN_WARNING "%s: can't initialise 82586: set multicast status %04X\n", dev->name, status);
  495. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  496. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  497. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  498. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  499. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  500. failures += 1;
  501. }
  502. timeout += HZ;
  503. while (((status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_status, DISABLEIRQS))
  504. & STAT_COMPLETE) == 0) {
  505. if (time_after(jiffies, timeout))
  506. break;
  507. }
  508. if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
  509. printk (KERN_WARNING "%s: can't tdr (ignored)\n", dev->name);
  510. printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
  511. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
  512. ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
  513. ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
  514. ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
  515. } else {
  516. status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_result, DISABLEIRQS);
  517. if (status & TDR_XCVRPROB)
  518. printk (KERN_WARNING "%s: i/f failed tdr: transceiver problem\n", dev->name);
  519. else if ((status & (TDR_SHORT|TDR_OPEN)) && (status & TDR_TIME)) {
  520. #ifdef FANCY
  521. printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d.%d us away\n", dev->name,
  522. status & TDR_SHORT ? "short" : "open", (status & TDR_TIME) / 10,
  523. (status & TDR_TIME) % 10);
  524. #else
  525. printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d clks away\n", dev->name,
  526. status & TDR_SHORT ? "short" : "open", (status & TDR_TIME));
  527. #endif
  528. }
  529. }
  530. if (failures)
  531. ether1_reset (dev);
  532. return failures ? 1 : 0;
  533. }
  534. /* ------------------------------------------------------------------------- */
  535. static int
  536. ether1_txalloc (struct net_device *dev, int size)
  537. {
  538. int start, tail;
  539. size = (size + 1) & ~1;
  540. tail = priv(dev)->tx_tail;
  541. if (priv(dev)->tx_head + size > TX_AREA_END) {
  542. if (tail > priv(dev)->tx_head)
  543. return -1;
  544. start = TX_AREA_START;
  545. if (start + size > tail)
  546. return -1;
  547. priv(dev)->tx_head = start + size;
  548. } else {
  549. if (priv(dev)->tx_head < tail && (priv(dev)->tx_head + size) > tail)
  550. return -1;
  551. start = priv(dev)->tx_head;
  552. priv(dev)->tx_head += size;
  553. }
  554. return start;
  555. }
  556. static int
  557. ether1_open (struct net_device *dev)
  558. {
  559. if (!is_valid_ether_addr(dev->dev_addr)) {
  560. printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
  561. dev->name);
  562. return -EINVAL;
  563. }
  564. if (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
  565. return -EAGAIN;
  566. memset (&priv(dev)->stats, 0, sizeof (struct net_device_stats));
  567. if (ether1_init_for_open (dev)) {
  568. free_irq (dev->irq, dev);
  569. return -EAGAIN;
  570. }
  571. netif_start_queue(dev);
  572. return 0;
  573. }
  574. static void
  575. ether1_timeout(struct net_device *dev)
  576. {
  577. printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
  578. dev->name);
  579. printk(KERN_WARNING "%s: resetting device\n", dev->name);
  580. ether1_reset (dev);
  581. if (ether1_init_for_open (dev))
  582. printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
  583. priv(dev)->stats.tx_errors++;
  584. netif_wake_queue(dev);
  585. }
  586. static int
  587. ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
  588. {
  589. int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
  590. unsigned long flags;
  591. tx_t tx;
  592. tbd_t tbd;
  593. nop_t nop;
  594. if (priv(dev)->restart) {
  595. printk(KERN_WARNING "%s: resetting device\n", dev->name);
  596. ether1_reset(dev);
  597. if (ether1_init_for_open(dev))
  598. printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
  599. else
  600. priv(dev)->restart = 0;
  601. }
  602. if (skb->len < ETH_ZLEN) {
  603. if (skb_padto(skb, ETH_ZLEN))
  604. goto out;
  605. }
  606. /*
  607. * insert packet followed by a nop
  608. */
  609. txaddr = ether1_txalloc (dev, TX_SIZE);
  610. tbdaddr = ether1_txalloc (dev, TBD_SIZE);
  611. dataddr = ether1_txalloc (dev, skb->len);
  612. nopaddr = ether1_txalloc (dev, NOP_SIZE);
  613. tx.tx_status = 0;
  614. tx.tx_command = CMD_TX | CMD_INTR;
  615. tx.tx_link = nopaddr;
  616. tx.tx_tbdoffset = tbdaddr;
  617. tbd.tbd_opts = TBD_EOL | skb->len;
  618. tbd.tbd_link = I82586_NULL;
  619. tbd.tbd_bufl = dataddr;
  620. tbd.tbd_bufh = 0;
  621. nop.nop_status = 0;
  622. nop.nop_command = CMD_NOP;
  623. nop.nop_link = nopaddr;
  624. local_irq_save(flags);
  625. ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
  626. ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
  627. ether1_writebuffer (dev, skb->data, dataddr, skb->len);
  628. ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
  629. tmp = priv(dev)->tx_link;
  630. priv(dev)->tx_link = nopaddr;
  631. /* now reset the previous nop pointer */
  632. ether1_writew(dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
  633. local_irq_restore(flags);
  634. /* handle transmit */
  635. dev->trans_start = jiffies;
  636. /* check to see if we have room for a full sized ether frame */
  637. tmp = priv(dev)->tx_head;
  638. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  639. priv(dev)->tx_head = tmp;
  640. dev_kfree_skb (skb);
  641. if (tst == -1)
  642. netif_stop_queue(dev);
  643. out:
  644. return 0;
  645. }
  646. static void
  647. ether1_xmit_done (struct net_device *dev)
  648. {
  649. nop_t nop;
  650. int caddr, tst;
  651. caddr = priv(dev)->tx_tail;
  652. again:
  653. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  654. switch (nop.nop_command & CMD_MASK) {
  655. case CMD_TDR:
  656. /* special case */
  657. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  658. != (unsigned short)I82586_NULL) {
  659. ether1_writew(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
  660. scb_command, NORMALIRQS);
  661. writeb(CTRL_CA, REG_CONTROL);
  662. }
  663. priv(dev)->tx_tail = NOP_ADDR;
  664. return;
  665. case CMD_NOP:
  666. if (nop.nop_link == caddr) {
  667. if (priv(dev)->initialising == 0)
  668. printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
  669. else
  670. priv(dev)->initialising = 0;
  671. return;
  672. }
  673. if (caddr == nop.nop_link)
  674. return;
  675. caddr = nop.nop_link;
  676. goto again;
  677. case CMD_TX:
  678. if (nop.nop_status & STAT_COMPLETE)
  679. break;
  680. printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
  681. priv(dev)->restart = 1;
  682. return;
  683. default:
  684. printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
  685. nop.nop_command & CMD_MASK, caddr);
  686. priv(dev)->restart = 1;
  687. return;
  688. }
  689. while (nop.nop_status & STAT_COMPLETE) {
  690. if (nop.nop_status & STAT_OK) {
  691. priv(dev)->stats.tx_packets ++;
  692. priv(dev)->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
  693. } else {
  694. priv(dev)->stats.tx_errors ++;
  695. if (nop.nop_status & STAT_COLLAFTERTX)
  696. priv(dev)->stats.collisions ++;
  697. if (nop.nop_status & STAT_NOCARRIER)
  698. priv(dev)->stats.tx_carrier_errors ++;
  699. if (nop.nop_status & STAT_TXLOSTCTS)
  700. printk (KERN_WARNING "%s: cts lost\n", dev->name);
  701. if (nop.nop_status & STAT_TXSLOWDMA)
  702. priv(dev)->stats.tx_fifo_errors ++;
  703. if (nop.nop_status & STAT_COLLEXCESSIVE)
  704. priv(dev)->stats.collisions += 16;
  705. }
  706. if (nop.nop_link == caddr) {
  707. printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
  708. break;
  709. }
  710. caddr = nop.nop_link;
  711. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  712. if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
  713. printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
  714. break;
  715. }
  716. if (caddr == nop.nop_link)
  717. break;
  718. caddr = nop.nop_link;
  719. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  720. if ((nop.nop_command & CMD_MASK) != CMD_TX) {
  721. printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
  722. break;
  723. }
  724. }
  725. priv(dev)->tx_tail = caddr;
  726. caddr = priv(dev)->tx_head;
  727. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  728. priv(dev)->tx_head = caddr;
  729. if (tst != -1)
  730. netif_wake_queue(dev);
  731. }
  732. static void
  733. ether1_recv_done (struct net_device *dev)
  734. {
  735. int status;
  736. int nexttail, rbdaddr;
  737. rbd_t rbd;
  738. do {
  739. status = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_status, NORMALIRQS);
  740. if ((status & RFD_COMPLETE) == 0)
  741. break;
  742. rbdaddr = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
  743. ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
  744. if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
  745. int length = rbd.rbd_status & RBD_ACNT;
  746. struct sk_buff *skb;
  747. length = (length + 1) & ~1;
  748. skb = dev_alloc_skb (length + 2);
  749. if (skb) {
  750. skb_reserve (skb, 2);
  751. ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
  752. skb->protocol = eth_type_trans (skb, dev);
  753. netif_rx (skb);
  754. priv(dev)->stats.rx_packets ++;
  755. } else
  756. priv(dev)->stats.rx_dropped ++;
  757. } else {
  758. printk(KERN_WARNING "%s: %s\n", dev->name,
  759. (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
  760. priv(dev)->stats.rx_dropped ++;
  761. }
  762. nexttail = ether1_readw(dev, priv(dev)->rx_tail, rfd_t, rfd_link, NORMALIRQS);
  763. /* nexttail should be rx_head */
  764. if (nexttail != priv(dev)->rx_head)
  765. printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
  766. dev->name, nexttail, priv(dev)->rx_head);
  767. ether1_writew(dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
  768. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_command, NORMALIRQS);
  769. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_status, NORMALIRQS);
  770. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
  771. priv(dev)->rx_tail = nexttail;
  772. priv(dev)->rx_head = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_link, NORMALIRQS);
  773. } while (1);
  774. }
  775. static irqreturn_t
  776. ether1_interrupt (int irq, void *dev_id)
  777. {
  778. struct net_device *dev = (struct net_device *)dev_id;
  779. int status;
  780. status = ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
  781. if (status) {
  782. ether1_writew(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
  783. SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  784. writeb(CTRL_CA | CTRL_ACK, REG_CONTROL);
  785. if (status & SCB_STCX) {
  786. ether1_xmit_done (dev);
  787. }
  788. if (status & SCB_STCNA) {
  789. if (priv(dev)->resetting == 0)
  790. printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
  791. else
  792. priv(dev)->resetting += 1;
  793. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  794. != (unsigned short)I82586_NULL) {
  795. ether1_writew(dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  796. writeb(CTRL_CA, REG_CONTROL);
  797. }
  798. if (priv(dev)->resetting == 2)
  799. priv(dev)->resetting = 0;
  800. }
  801. if (status & SCB_STFR) {
  802. ether1_recv_done (dev);
  803. }
  804. if (status & SCB_STRNR) {
  805. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
  806. printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
  807. ether1_writew(dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  808. writeb(CTRL_CA, REG_CONTROL);
  809. priv(dev)->stats.rx_dropped ++; /* we suspended due to lack of buffer space */
  810. } else
  811. printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
  812. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
  813. printk (KERN_WARNING "RU ptr = %04X\n", ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset,
  814. NORMALIRQS));
  815. }
  816. } else
  817. writeb(CTRL_ACK, REG_CONTROL);
  818. return IRQ_HANDLED;
  819. }
  820. static int
  821. ether1_close (struct net_device *dev)
  822. {
  823. ether1_reset (dev);
  824. free_irq(dev->irq, dev);
  825. return 0;
  826. }
  827. static struct net_device_stats *
  828. ether1_getstats (struct net_device *dev)
  829. {
  830. return &priv(dev)->stats;
  831. }
  832. /*
  833. * Set or clear the multicast filter for this adaptor.
  834. * num_addrs == -1 Promiscuous mode, receive all packets.
  835. * num_addrs == 0 Normal mode, clear multicast list.
  836. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  837. * best-effort filtering.
  838. */
  839. static void
  840. ether1_setmulticastlist (struct net_device *dev)
  841. {
  842. }
  843. /* ------------------------------------------------------------------------- */
  844. static void __devinit ether1_banner(void)
  845. {
  846. static unsigned int version_printed = 0;
  847. if (net_debug && version_printed++ == 0)
  848. printk(KERN_INFO "%s", version);
  849. }
  850. static const struct net_device_ops ether1_netdev_ops = {
  851. .ndo_open = ether1_open,
  852. .ndo_stop = ether1_close,
  853. .ndo_start_xmit = ether1_sendpacket,
  854. .ndo_get_stats = ether1_getstats,
  855. .ndo_set_multicast_list = ether1_setmulticastlist,
  856. .ndo_tx_timeout = ether1_timeout,
  857. .ndo_validate_addr = eth_validate_addr,
  858. .ndo_change_mtu = eth_change_mtu,
  859. .ndo_set_mac_address = eth_mac_addr,
  860. };
  861. static int __devinit
  862. ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
  863. {
  864. struct net_device *dev;
  865. int i, ret = 0;
  866. ether1_banner();
  867. ret = ecard_request_resources(ec);
  868. if (ret)
  869. goto out;
  870. dev = alloc_etherdev(sizeof(struct ether1_priv));
  871. if (!dev) {
  872. ret = -ENOMEM;
  873. goto release;
  874. }
  875. SET_NETDEV_DEV(dev, &ec->dev);
  876. dev->irq = ec->irq;
  877. priv(dev)->base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
  878. if (!priv(dev)->base) {
  879. ret = -ENOMEM;
  880. goto free;
  881. }
  882. if ((priv(dev)->bus_type = ether1_reset(dev)) == 0) {
  883. ret = -ENODEV;
  884. goto free;
  885. }
  886. for (i = 0; i < 6; i++)
  887. dev->dev_addr[i] = readb(IDPROM_ADDRESS + (i << 2));
  888. if (ether1_init_2(dev)) {
  889. ret = -ENODEV;
  890. goto free;
  891. }
  892. dev->netdev_ops = &ether1_netdev_ops;
  893. dev->watchdog_timeo = 5 * HZ / 100;
  894. ret = register_netdev(dev);
  895. if (ret)
  896. goto free;
  897. printk(KERN_INFO "%s: ether1 in slot %d, %pM\n",
  898. dev->name, ec->slot_no, dev->dev_addr);
  899. ecard_set_drvdata(ec, dev);
  900. return 0;
  901. free:
  902. free_netdev(dev);
  903. release:
  904. ecard_release_resources(ec);
  905. out:
  906. return ret;
  907. }
  908. static void __devexit ether1_remove(struct expansion_card *ec)
  909. {
  910. struct net_device *dev = ecard_get_drvdata(ec);
  911. ecard_set_drvdata(ec, NULL);
  912. unregister_netdev(dev);
  913. free_netdev(dev);
  914. ecard_release_resources(ec);
  915. }
  916. static const struct ecard_id ether1_ids[] = {
  917. { MANU_ACORN, PROD_ACORN_ETHER1 },
  918. { 0xffff, 0xffff }
  919. };
  920. static struct ecard_driver ether1_driver = {
  921. .probe = ether1_probe,
  922. .remove = __devexit_p(ether1_remove),
  923. .id_table = ether1_ids,
  924. .drv = {
  925. .name = "ether1",
  926. },
  927. };
  928. static int __init ether1_init(void)
  929. {
  930. return ecard_register_driver(&ether1_driver);
  931. }
  932. static void __exit ether1_exit(void)
  933. {
  934. ecard_remove_driver(&ether1_driver);
  935. }
  936. module_init(ether1_init);
  937. module_exit(ether1_exit);
  938. MODULE_LICENSE("GPL");