ether1.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. /* check to see if we have room for a full sized ether frame */
  636. tmp = priv(dev)->tx_head;
  637. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  638. priv(dev)->tx_head = tmp;
  639. dev_kfree_skb (skb);
  640. if (tst == -1)
  641. netif_stop_queue(dev);
  642. out:
  643. return NETDEV_TX_OK;
  644. }
  645. static void
  646. ether1_xmit_done (struct net_device *dev)
  647. {
  648. nop_t nop;
  649. int caddr, tst;
  650. caddr = priv(dev)->tx_tail;
  651. again:
  652. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  653. switch (nop.nop_command & CMD_MASK) {
  654. case CMD_TDR:
  655. /* special case */
  656. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  657. != (unsigned short)I82586_NULL) {
  658. ether1_writew(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
  659. scb_command, NORMALIRQS);
  660. writeb(CTRL_CA, REG_CONTROL);
  661. }
  662. priv(dev)->tx_tail = NOP_ADDR;
  663. return;
  664. case CMD_NOP:
  665. if (nop.nop_link == caddr) {
  666. if (priv(dev)->initialising == 0)
  667. printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
  668. else
  669. priv(dev)->initialising = 0;
  670. return;
  671. }
  672. if (caddr == nop.nop_link)
  673. return;
  674. caddr = nop.nop_link;
  675. goto again;
  676. case CMD_TX:
  677. if (nop.nop_status & STAT_COMPLETE)
  678. break;
  679. printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
  680. priv(dev)->restart = 1;
  681. return;
  682. default:
  683. printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
  684. nop.nop_command & CMD_MASK, caddr);
  685. priv(dev)->restart = 1;
  686. return;
  687. }
  688. while (nop.nop_status & STAT_COMPLETE) {
  689. if (nop.nop_status & STAT_OK) {
  690. priv(dev)->stats.tx_packets ++;
  691. priv(dev)->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
  692. } else {
  693. priv(dev)->stats.tx_errors ++;
  694. if (nop.nop_status & STAT_COLLAFTERTX)
  695. priv(dev)->stats.collisions ++;
  696. if (nop.nop_status & STAT_NOCARRIER)
  697. priv(dev)->stats.tx_carrier_errors ++;
  698. if (nop.nop_status & STAT_TXLOSTCTS)
  699. printk (KERN_WARNING "%s: cts lost\n", dev->name);
  700. if (nop.nop_status & STAT_TXSLOWDMA)
  701. priv(dev)->stats.tx_fifo_errors ++;
  702. if (nop.nop_status & STAT_COLLEXCESSIVE)
  703. priv(dev)->stats.collisions += 16;
  704. }
  705. if (nop.nop_link == caddr) {
  706. printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
  707. break;
  708. }
  709. caddr = nop.nop_link;
  710. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  711. if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
  712. printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
  713. break;
  714. }
  715. if (caddr == nop.nop_link)
  716. break;
  717. caddr = nop.nop_link;
  718. ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
  719. if ((nop.nop_command & CMD_MASK) != CMD_TX) {
  720. printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
  721. break;
  722. }
  723. }
  724. priv(dev)->tx_tail = caddr;
  725. caddr = priv(dev)->tx_head;
  726. tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
  727. priv(dev)->tx_head = caddr;
  728. if (tst != -1)
  729. netif_wake_queue(dev);
  730. }
  731. static void
  732. ether1_recv_done (struct net_device *dev)
  733. {
  734. int status;
  735. int nexttail, rbdaddr;
  736. rbd_t rbd;
  737. do {
  738. status = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_status, NORMALIRQS);
  739. if ((status & RFD_COMPLETE) == 0)
  740. break;
  741. rbdaddr = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
  742. ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
  743. if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
  744. int length = rbd.rbd_status & RBD_ACNT;
  745. struct sk_buff *skb;
  746. length = (length + 1) & ~1;
  747. skb = dev_alloc_skb (length + 2);
  748. if (skb) {
  749. skb_reserve (skb, 2);
  750. ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
  751. skb->protocol = eth_type_trans (skb, dev);
  752. netif_rx (skb);
  753. priv(dev)->stats.rx_packets ++;
  754. } else
  755. priv(dev)->stats.rx_dropped ++;
  756. } else {
  757. printk(KERN_WARNING "%s: %s\n", dev->name,
  758. (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
  759. priv(dev)->stats.rx_dropped ++;
  760. }
  761. nexttail = ether1_readw(dev, priv(dev)->rx_tail, rfd_t, rfd_link, NORMALIRQS);
  762. /* nexttail should be rx_head */
  763. if (nexttail != priv(dev)->rx_head)
  764. printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
  765. dev->name, nexttail, priv(dev)->rx_head);
  766. ether1_writew(dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
  767. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_command, NORMALIRQS);
  768. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_status, NORMALIRQS);
  769. ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
  770. priv(dev)->rx_tail = nexttail;
  771. priv(dev)->rx_head = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_link, NORMALIRQS);
  772. } while (1);
  773. }
  774. static irqreturn_t
  775. ether1_interrupt (int irq, void *dev_id)
  776. {
  777. struct net_device *dev = (struct net_device *)dev_id;
  778. int status;
  779. status = ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
  780. if (status) {
  781. ether1_writew(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
  782. SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  783. writeb(CTRL_CA | CTRL_ACK, REG_CONTROL);
  784. if (status & SCB_STCX) {
  785. ether1_xmit_done (dev);
  786. }
  787. if (status & SCB_STCNA) {
  788. if (priv(dev)->resetting == 0)
  789. printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
  790. else
  791. priv(dev)->resetting += 1;
  792. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
  793. != (unsigned short)I82586_NULL) {
  794. ether1_writew(dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  795. writeb(CTRL_CA, REG_CONTROL);
  796. }
  797. if (priv(dev)->resetting == 2)
  798. priv(dev)->resetting = 0;
  799. }
  800. if (status & SCB_STFR) {
  801. ether1_recv_done (dev);
  802. }
  803. if (status & SCB_STRNR) {
  804. if (ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
  805. printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
  806. ether1_writew(dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
  807. writeb(CTRL_CA, REG_CONTROL);
  808. priv(dev)->stats.rx_dropped ++; /* we suspended due to lack of buffer space */
  809. } else
  810. printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
  811. ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
  812. printk (KERN_WARNING "RU ptr = %04X\n", ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset,
  813. NORMALIRQS));
  814. }
  815. } else
  816. writeb(CTRL_ACK, REG_CONTROL);
  817. return IRQ_HANDLED;
  818. }
  819. static int
  820. ether1_close (struct net_device *dev)
  821. {
  822. ether1_reset (dev);
  823. free_irq(dev->irq, dev);
  824. return 0;
  825. }
  826. static struct net_device_stats *
  827. ether1_getstats (struct net_device *dev)
  828. {
  829. return &priv(dev)->stats;
  830. }
  831. /*
  832. * Set or clear the multicast filter for this adaptor.
  833. * num_addrs == -1 Promiscuous mode, receive all packets.
  834. * num_addrs == 0 Normal mode, clear multicast list.
  835. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  836. * best-effort filtering.
  837. */
  838. static void
  839. ether1_setmulticastlist (struct net_device *dev)
  840. {
  841. }
  842. /* ------------------------------------------------------------------------- */
  843. static void __devinit ether1_banner(void)
  844. {
  845. static unsigned int version_printed = 0;
  846. if (net_debug && version_printed++ == 0)
  847. printk(KERN_INFO "%s", version);
  848. }
  849. static const struct net_device_ops ether1_netdev_ops = {
  850. .ndo_open = ether1_open,
  851. .ndo_stop = ether1_close,
  852. .ndo_start_xmit = ether1_sendpacket,
  853. .ndo_get_stats = ether1_getstats,
  854. .ndo_set_multicast_list = ether1_setmulticastlist,
  855. .ndo_tx_timeout = ether1_timeout,
  856. .ndo_validate_addr = eth_validate_addr,
  857. .ndo_change_mtu = eth_change_mtu,
  858. .ndo_set_mac_address = eth_mac_addr,
  859. };
  860. static int __devinit
  861. ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
  862. {
  863. struct net_device *dev;
  864. int i, ret = 0;
  865. ether1_banner();
  866. ret = ecard_request_resources(ec);
  867. if (ret)
  868. goto out;
  869. dev = alloc_etherdev(sizeof(struct ether1_priv));
  870. if (!dev) {
  871. ret = -ENOMEM;
  872. goto release;
  873. }
  874. SET_NETDEV_DEV(dev, &ec->dev);
  875. dev->irq = ec->irq;
  876. priv(dev)->base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
  877. if (!priv(dev)->base) {
  878. ret = -ENOMEM;
  879. goto free;
  880. }
  881. if ((priv(dev)->bus_type = ether1_reset(dev)) == 0) {
  882. ret = -ENODEV;
  883. goto free;
  884. }
  885. for (i = 0; i < 6; i++)
  886. dev->dev_addr[i] = readb(IDPROM_ADDRESS + (i << 2));
  887. if (ether1_init_2(dev)) {
  888. ret = -ENODEV;
  889. goto free;
  890. }
  891. dev->netdev_ops = &ether1_netdev_ops;
  892. dev->watchdog_timeo = 5 * HZ / 100;
  893. ret = register_netdev(dev);
  894. if (ret)
  895. goto free;
  896. printk(KERN_INFO "%s: ether1 in slot %d, %pM\n",
  897. dev->name, ec->slot_no, dev->dev_addr);
  898. ecard_set_drvdata(ec, dev);
  899. return 0;
  900. free:
  901. free_netdev(dev);
  902. release:
  903. ecard_release_resources(ec);
  904. out:
  905. return ret;
  906. }
  907. static void __devexit ether1_remove(struct expansion_card *ec)
  908. {
  909. struct net_device *dev = ecard_get_drvdata(ec);
  910. ecard_set_drvdata(ec, NULL);
  911. unregister_netdev(dev);
  912. free_netdev(dev);
  913. ecard_release_resources(ec);
  914. }
  915. static const struct ecard_id ether1_ids[] = {
  916. { MANU_ACORN, PROD_ACORN_ETHER1 },
  917. { 0xffff, 0xffff }
  918. };
  919. static struct ecard_driver ether1_driver = {
  920. .probe = ether1_probe,
  921. .remove = __devexit_p(ether1_remove),
  922. .id_table = ether1_ids,
  923. .drv = {
  924. .name = "ether1",
  925. },
  926. };
  927. static int __init ether1_init(void)
  928. {
  929. return ecard_register_driver(&ether1_driver);
  930. }
  931. static void __exit ether1_exit(void)
  932. {
  933. ecard_remove_driver(&ether1_driver);
  934. }
  935. module_init(ether1_init);
  936. module_exit(ether1_exit);
  937. MODULE_LICENSE("GPL");