ether1.c 27 KB

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