ether1.c 27 KB

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