ether1.c 27 KB

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