ether1.c 27 KB

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