eth_v10.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /* $Id: ethernet.c,v 1.31 2004/10/18 14:49:03 starvik Exp $
  2. *
  3. * e100net.c: A network driver for the ETRAX 100LX network controller.
  4. *
  5. * Copyright (c) 1998-2002 Axis Communications AB.
  6. *
  7. * The outline of this driver comes from skeleton.c.
  8. *
  9. * $Log: ethernet.c,v $
  10. * Revision 1.31 2004/10/18 14:49:03 starvik
  11. * Use RX interrupt as random source
  12. *
  13. * Revision 1.30 2004/09/29 10:44:04 starvik
  14. * Enabed MAC-address output again
  15. *
  16. * Revision 1.29 2004/08/24 07:14:05 starvik
  17. * Make use of generic MDIO interface and constants.
  18. *
  19. * Revision 1.28 2004/08/20 09:37:11 starvik
  20. * Added support for Intel LXT972A. Creds to Randy Scarborough.
  21. *
  22. * Revision 1.27 2004/08/16 12:37:22 starvik
  23. * Merge of Linux 2.6.8
  24. *
  25. * Revision 1.25 2004/06/21 10:29:57 starvik
  26. * Merge of Linux 2.6.7
  27. *
  28. * Revision 1.23 2004/06/09 05:29:22 starvik
  29. * Avoid any race where R_DMA_CH1_FIRST is NULL (may trigger cache bug).
  30. *
  31. * Revision 1.22 2004/05/14 07:58:03 starvik
  32. * Merge of changes from 2.4
  33. *
  34. * Revision 1.20 2004/03/11 11:38:40 starvik
  35. * Merge of Linux 2.6.4
  36. *
  37. * Revision 1.18 2003/12/03 13:45:46 starvik
  38. * Use hardware pad for short packets to prevent information leakage.
  39. *
  40. * Revision 1.17 2003/07/04 08:27:37 starvik
  41. * Merge of Linux 2.5.74
  42. *
  43. * Revision 1.16 2003/04/24 08:28:22 starvik
  44. * New LED behaviour: LED off when no link
  45. *
  46. * Revision 1.15 2003/04/09 05:20:47 starvik
  47. * Merge of Linux 2.5.67
  48. *
  49. * Revision 1.13 2003/03/06 16:11:01 henriken
  50. * Off by one error in group address register setting.
  51. *
  52. * Revision 1.12 2003/02/27 17:24:19 starvik
  53. * Corrected Rev to Revision
  54. *
  55. * Revision 1.11 2003/01/24 09:53:21 starvik
  56. * Oops. Initialize GA to 0, not to 1
  57. *
  58. * Revision 1.10 2003/01/24 09:50:55 starvik
  59. * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
  60. *
  61. * Revision 1.9 2002/12/13 07:40:58 starvik
  62. * Added basic ethtool interface
  63. * Handled out of memory when allocating new buffers
  64. *
  65. * Revision 1.8 2002/12/11 13:13:57 starvik
  66. * Added arch/ to v10 specific includes
  67. * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
  68. *
  69. * Revision 1.7 2002/11/26 09:41:42 starvik
  70. * Added e100_set_config (standard interface to set media type)
  71. * Added protection against preemptive scheduling
  72. * Added standard MII ioctls
  73. *
  74. * Revision 1.6 2002/11/21 07:18:18 starvik
  75. * Timers must be initialized in 2.5.48
  76. *
  77. * Revision 1.5 2002/11/20 11:56:11 starvik
  78. * Merge of Linux 2.5.48
  79. *
  80. * Revision 1.4 2002/11/18 07:26:46 starvik
  81. * Linux 2.5 port of latest Linux 2.4 ethernet driver
  82. *
  83. * Revision 1.33 2002/10/02 20:16:17 hp
  84. * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
  85. *
  86. * Revision 1.32 2002/09/16 06:05:58 starvik
  87. * Align memory returned by dev_alloc_skb
  88. * Moved handling of sent packets to interrupt to avoid reference counting problem
  89. *
  90. * Revision 1.31 2002/09/10 13:28:23 larsv
  91. * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
  92. * for supported functionality by issuing special ioctls, i.e. wireless
  93. * extensions.
  94. *
  95. * Revision 1.30 2002/05/07 18:50:08 johana
  96. * Correct spelling in comments.
  97. *
  98. * Revision 1.29 2002/05/06 05:38:49 starvik
  99. * Performance improvements:
  100. * Large packets are not copied (breakpoint set to 256 bytes)
  101. * The cache bug workaround is delayed until half of the receive list
  102. * has been used
  103. * Added transmit list
  104. * Transmit interrupts are only enabled when transmit queue is full
  105. *
  106. * Revision 1.28.2.1 2002/04/30 08:15:51 starvik
  107. * Performance improvements:
  108. * Large packets are not copied (breakpoint set to 256 bytes)
  109. * The cache bug workaround is delayed until half of the receive list
  110. * has been used.
  111. * Added transmit list
  112. * Transmit interrupts are only enabled when transmit queue is full
  113. *
  114. * Revision 1.28 2002/04/22 11:47:21 johana
  115. * Fix according to 2.4.19-pre7. time_after/time_before and
  116. * missing end of comment.
  117. * The patch has a typo for ethernet.c in e100_clear_network_leds(),
  118. * that is fixed here.
  119. *
  120. * Revision 1.27 2002/04/12 11:55:11 bjornw
  121. * Added TODO
  122. *
  123. * Revision 1.26 2002/03/15 17:11:02 bjornw
  124. * Use prepare_rx_descriptor after the CPU has touched the receiving descs
  125. *
  126. * Revision 1.25 2002/03/08 13:07:53 bjornw
  127. * Unnecessary spinlock removed
  128. *
  129. * Revision 1.24 2002/02/20 12:57:43 fredriks
  130. * Replaced MIN() with min().
  131. *
  132. * Revision 1.23 2002/02/20 10:58:14 fredriks
  133. * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
  134. *
  135. * Revision 1.22 2002/01/30 07:48:22 matsfg
  136. * Initiate R_NETWORK_TR_CTRL
  137. *
  138. * Revision 1.21 2001/11/23 11:54:49 starvik
  139. * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
  140. * Removed compiler warnings
  141. *
  142. * Revision 1.20 2001/11/12 19:26:00 pkj
  143. * * Corrected e100_negotiate() to not assign half to current_duplex when
  144. * it was supposed to compare them...
  145. * * Cleaned up failure handling in e100_open().
  146. * * Fixed compiler warnings.
  147. *
  148. * Revision 1.19 2001/11/09 07:43:09 starvik
  149. * Added full duplex support
  150. * Added ioctl to set speed and duplex
  151. * Clear LED timer only runs when LED is lit
  152. *
  153. * Revision 1.18 2001/10/03 14:40:43 jonashg
  154. * Update rx_bytes counter.
  155. *
  156. * Revision 1.17 2001/06/11 12:43:46 olof
  157. * Modified defines for network LED behavior
  158. *
  159. * Revision 1.16 2001/05/30 06:12:46 markusl
  160. * TxDesc.next should not be set to NULL
  161. *
  162. * Revision 1.15 2001/05/29 10:27:04 markusl
  163. * Updated after review remarks:
  164. * +Use IO_EXTRACT
  165. * +Handle underrun
  166. *
  167. * Revision 1.14 2001/05/29 09:20:14 jonashg
  168. * Use driver name on printk output so one can tell which driver that complains.
  169. *
  170. * Revision 1.13 2001/05/09 12:35:59 johana
  171. * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
  172. *
  173. * Revision 1.12 2001/04/05 11:43:11 tobiasa
  174. * Check dev before panic.
  175. *
  176. * Revision 1.11 2001/04/04 11:21:05 markusl
  177. * Updated according to review remarks
  178. *
  179. * Revision 1.10 2001/03/26 16:03:06 bjornw
  180. * Needs linux/config.h
  181. *
  182. * Revision 1.9 2001/03/19 14:47:48 pkj
  183. * * Make sure there is always a pause after the network LEDs are
  184. * changed so they will not look constantly lit during heavy traffic.
  185. * * Always use HZ when setting times relative to jiffies.
  186. * * Use LED_NETWORK_SET() when setting the network LEDs.
  187. *
  188. * Revision 1.8 2001/02/27 13:52:48 bjornw
  189. * malloc.h -> slab.h
  190. *
  191. * Revision 1.7 2001/02/23 13:46:38 bjornw
  192. * Spellling check
  193. *
  194. * Revision 1.6 2001/01/26 15:21:04 starvik
  195. * Don't disable interrupts while reading MDIO registers (MDIO is slow)
  196. * Corrected promiscuous mode
  197. * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
  198. *
  199. * Revision 1.5 2000/11/29 17:22:22 bjornw
  200. * Get rid of the udword types legacy stuff
  201. *
  202. * Revision 1.4 2000/11/22 16:36:09 bjornw
  203. * Please marketing by using the correct case when spelling Etrax.
  204. *
  205. * Revision 1.3 2000/11/21 16:43:04 bjornw
  206. * Minor short->int change
  207. *
  208. * Revision 1.2 2000/11/08 14:27:57 bjornw
  209. * 2.4 port
  210. *
  211. * Revision 1.1 2000/11/06 13:56:00 bjornw
  212. * Verbatim copy of the 1.24 version of e100net.c from elinux
  213. *
  214. * Revision 1.24 2000/10/04 15:55:23 bjornw
  215. * * Use virt_to_phys etc. for DMA addresses
  216. * * Removed bogus CHECKSUM_UNNECESSARY
  217. *
  218. *
  219. */
  220. #include <linux/config.h>
  221. #include <linux/module.h>
  222. #include <linux/kernel.h>
  223. #include <linux/sched.h>
  224. #include <linux/delay.h>
  225. #include <linux/types.h>
  226. #include <linux/fcntl.h>
  227. #include <linux/interrupt.h>
  228. #include <linux/ptrace.h>
  229. #include <linux/ioport.h>
  230. #include <linux/in.h>
  231. #include <linux/slab.h>
  232. #include <linux/string.h>
  233. #include <linux/spinlock.h>
  234. #include <linux/errno.h>
  235. #include <linux/init.h>
  236. #include <linux/if.h>
  237. #include <linux/mii.h>
  238. #include <linux/netdevice.h>
  239. #include <linux/etherdevice.h>
  240. #include <linux/skbuff.h>
  241. #include <linux/ethtool.h>
  242. #include <asm/arch/svinto.h>/* DMA and register descriptions */
  243. #include <asm/io.h> /* LED_* I/O functions */
  244. #include <asm/irq.h>
  245. #include <asm/dma.h>
  246. #include <asm/system.h>
  247. #include <asm/bitops.h>
  248. #include <asm/ethernet.h>
  249. #include <asm/cache.h>
  250. //#define ETHDEBUG
  251. #define D(x)
  252. /*
  253. * The name of the card. Is used for messages and in the requests for
  254. * io regions, irqs and dma channels
  255. */
  256. static const char* cardname = "ETRAX 100LX built-in ethernet controller";
  257. /* A default ethernet address. Highlevel SW will set the real one later */
  258. static struct sockaddr default_mac = {
  259. 0,
  260. { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
  261. };
  262. /* Information that need to be kept for each board. */
  263. struct net_local {
  264. struct net_device_stats stats;
  265. struct mii_if_info mii_if;
  266. /* Tx control lock. This protects the transmit buffer ring
  267. * state along with the "tx full" state of the driver. This
  268. * means all netif_queue flow control actions are protected
  269. * by this lock as well.
  270. */
  271. spinlock_t lock;
  272. };
  273. typedef struct etrax_eth_descr
  274. {
  275. etrax_dma_descr descr;
  276. struct sk_buff* skb;
  277. } etrax_eth_descr;
  278. /* Some transceivers requires special handling */
  279. struct transceiver_ops
  280. {
  281. unsigned int oui;
  282. void (*check_speed)(struct net_device* dev);
  283. void (*check_duplex)(struct net_device* dev);
  284. };
  285. struct transceiver_ops* transceiver;
  286. /* Duplex settings */
  287. enum duplex
  288. {
  289. half,
  290. full,
  291. autoneg
  292. };
  293. /* Dma descriptors etc. */
  294. #define MAX_MEDIA_DATA_SIZE 1518
  295. #define MIN_PACKET_LEN 46
  296. #define ETHER_HEAD_LEN 14
  297. /*
  298. ** MDIO constants.
  299. */
  300. #define MDIO_START 0x1
  301. #define MDIO_READ 0x2
  302. #define MDIO_WRITE 0x1
  303. #define MDIO_PREAMBLE 0xfffffffful
  304. /* Broadcom specific */
  305. #define MDIO_AUX_CTRL_STATUS_REG 0x18
  306. #define MDIO_BC_FULL_DUPLEX_IND 0x1
  307. #define MDIO_BC_SPEED 0x2
  308. /* TDK specific */
  309. #define MDIO_TDK_DIAGNOSTIC_REG 18
  310. #define MDIO_TDK_DIAGNOSTIC_RATE 0x400
  311. #define MDIO_TDK_DIAGNOSTIC_DPLX 0x800
  312. /*Intel LXT972A specific*/
  313. #define MDIO_INT_STATUS_REG_2 0x0011
  314. #define MDIO_INT_FULL_DUPLEX_IND ( 1 << 9 )
  315. #define MDIO_INT_SPEED ( 1 << 14 )
  316. /* Network flash constants */
  317. #define NET_FLASH_TIME (HZ/50) /* 20 ms */
  318. #define NET_FLASH_PAUSE (HZ/100) /* 10 ms */
  319. #define NET_LINK_UP_CHECK_INTERVAL (2*HZ) /* 2 s */
  320. #define NET_DUPLEX_CHECK_INTERVAL (2*HZ) /* 2 s */
  321. #define NO_NETWORK_ACTIVITY 0
  322. #define NETWORK_ACTIVITY 1
  323. #define NBR_OF_RX_DESC 64
  324. #define NBR_OF_TX_DESC 256
  325. /* Large packets are sent directly to upper layers while small packets are */
  326. /* copied (to reduce memory waste). The following constant decides the breakpoint */
  327. #define RX_COPYBREAK 256
  328. /* Due to a chip bug we need to flush the cache when descriptors are returned */
  329. /* to the DMA. To decrease performance impact we return descriptors in chunks. */
  330. /* The following constant determines the number of descriptors to return. */
  331. #define RX_QUEUE_THRESHOLD NBR_OF_RX_DESC/2
  332. #define GET_BIT(bit,val) (((val) >> (bit)) & 0x01)
  333. /* Define some macros to access ETRAX 100 registers */
  334. #define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  335. IO_FIELD_(reg##_, field##_, val)
  336. #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  337. IO_STATE_(reg##_, field##_, _##val)
  338. static etrax_eth_descr *myNextRxDesc; /* Points to the next descriptor to
  339. to be processed */
  340. static etrax_eth_descr *myLastRxDesc; /* The last processed descriptor */
  341. static etrax_eth_descr *myPrevRxDesc; /* The descriptor right before myNextRxDesc */
  342. static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
  343. static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
  344. static etrax_eth_descr* myLastTxDesc; /* End of send queue */
  345. static etrax_eth_descr* myNextTxDesc; /* Next descriptor to use */
  346. static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
  347. static unsigned int network_rec_config_shadow = 0;
  348. static unsigned int mdio_phy_addr; /* Transciever address */
  349. static unsigned int network_tr_ctrl_shadow = 0;
  350. /* Network speed indication. */
  351. static DEFINE_TIMER(speed_timer, NULL, 0, 0);
  352. static DEFINE_TIMER(clear_led_timer, NULL, 0, 0);
  353. static int current_speed; /* Speed read from transceiver */
  354. static int current_speed_selection; /* Speed selected by user */
  355. static unsigned long led_next_time;
  356. static int led_active;
  357. static int rx_queue_len;
  358. /* Duplex */
  359. static DEFINE_TIMER(duplex_timer, NULL, 0, 0);
  360. static int full_duplex;
  361. static enum duplex current_duplex;
  362. /* Index to functions, as function prototypes. */
  363. static int etrax_ethernet_init(void);
  364. static int e100_open(struct net_device *dev);
  365. static int e100_set_mac_address(struct net_device *dev, void *addr);
  366. static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
  367. static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  368. static irqreturn_t e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  369. static void e100_rx(struct net_device *dev);
  370. static int e100_close(struct net_device *dev);
  371. static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  372. static int e100_set_config(struct net_device* dev, struct ifmap* map);
  373. static void e100_tx_timeout(struct net_device *dev);
  374. static struct net_device_stats *e100_get_stats(struct net_device *dev);
  375. static void set_multicast_list(struct net_device *dev);
  376. static void e100_hardware_send_packet(char *buf, int length);
  377. static void update_rx_stats(struct net_device_stats *);
  378. static void update_tx_stats(struct net_device_stats *);
  379. static int e100_probe_transceiver(struct net_device* dev);
  380. static void e100_check_speed(unsigned long priv);
  381. static void e100_set_speed(struct net_device* dev, unsigned long speed);
  382. static void e100_check_duplex(unsigned long priv);
  383. static void e100_set_duplex(struct net_device* dev, enum duplex);
  384. static void e100_negotiate(struct net_device* dev);
  385. static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
  386. static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);
  387. static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
  388. static void e100_send_mdio_bit(unsigned char bit);
  389. static unsigned char e100_receive_mdio_bit(void);
  390. static void e100_reset_transceiver(struct net_device* net);
  391. static void e100_clear_network_leds(unsigned long dummy);
  392. static void e100_set_network_leds(int active);
  393. static struct ethtool_ops e100_ethtool_ops;
  394. static void broadcom_check_speed(struct net_device* dev);
  395. static void broadcom_check_duplex(struct net_device* dev);
  396. static void tdk_check_speed(struct net_device* dev);
  397. static void tdk_check_duplex(struct net_device* dev);
  398. static void intel_check_speed(struct net_device* dev);
  399. static void intel_check_duplex(struct net_device* dev);
  400. static void generic_check_speed(struct net_device* dev);
  401. static void generic_check_duplex(struct net_device* dev);
  402. struct transceiver_ops transceivers[] =
  403. {
  404. {0x1018, broadcom_check_speed, broadcom_check_duplex}, /* Broadcom */
  405. {0xC039, tdk_check_speed, tdk_check_duplex}, /* TDK 2120 */
  406. {0x039C, tdk_check_speed, tdk_check_duplex}, /* TDK 2120C */
  407. {0x04de, intel_check_speed, intel_check_duplex}, /* Intel LXT972A*/
  408. {0x0000, generic_check_speed, generic_check_duplex} /* Generic, must be last */
  409. };
  410. #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
  411. /*
  412. * Check for a network adaptor of this type, and return '0' if one exists.
  413. * If dev->base_addr == 0, probe all likely locations.
  414. * If dev->base_addr == 1, always return failure.
  415. * If dev->base_addr == 2, allocate space for the device and return success
  416. * (detachable devices only).
  417. */
  418. static int __init
  419. etrax_ethernet_init(void)
  420. {
  421. struct net_device *dev;
  422. struct net_local* np;
  423. int i, err;
  424. printk(KERN_INFO
  425. "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2003 Axis Communications AB\n");
  426. dev = alloc_etherdev(sizeof(struct net_local));
  427. np = dev->priv;
  428. if (!dev)
  429. return -ENOMEM;
  430. dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
  431. /* now setup our etrax specific stuff */
  432. dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
  433. dev->dma = NETWORK_RX_DMA_NBR;
  434. /* fill in our handlers so the network layer can talk to us in the future */
  435. dev->open = e100_open;
  436. dev->hard_start_xmit = e100_send_packet;
  437. dev->stop = e100_close;
  438. dev->get_stats = e100_get_stats;
  439. dev->set_multicast_list = set_multicast_list;
  440. dev->set_mac_address = e100_set_mac_address;
  441. dev->ethtool_ops = &e100_ethtool_ops;
  442. dev->do_ioctl = e100_ioctl;
  443. dev->set_config = e100_set_config;
  444. dev->tx_timeout = e100_tx_timeout;
  445. /* Initialise the list of Etrax DMA-descriptors */
  446. /* Initialise receive descriptors */
  447. for (i = 0; i < NBR_OF_RX_DESC; i++) {
  448. /* Allocate two extra cachelines to make sure that buffer used by DMA
  449. * does not share cacheline with any other data (to avoid cache bug)
  450. */
  451. RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
  452. RxDescList[i].descr.ctrl = 0;
  453. RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
  454. RxDescList[i].descr.next = virt_to_phys(&RxDescList[i + 1]);
  455. RxDescList[i].descr.buf = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
  456. RxDescList[i].descr.status = 0;
  457. RxDescList[i].descr.hw_len = 0;
  458. prepare_rx_descriptor(&RxDescList[i].descr);
  459. }
  460. RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl = d_eol;
  461. RxDescList[NBR_OF_RX_DESC - 1].descr.next = virt_to_phys(&RxDescList[0]);
  462. rx_queue_len = 0;
  463. /* Initialize transmit descriptors */
  464. for (i = 0; i < NBR_OF_TX_DESC; i++) {
  465. TxDescList[i].descr.ctrl = 0;
  466. TxDescList[i].descr.sw_len = 0;
  467. TxDescList[i].descr.next = virt_to_phys(&TxDescList[i + 1].descr);
  468. TxDescList[i].descr.buf = 0;
  469. TxDescList[i].descr.status = 0;
  470. TxDescList[i].descr.hw_len = 0;
  471. TxDescList[i].skb = 0;
  472. }
  473. TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl = d_eol;
  474. TxDescList[NBR_OF_TX_DESC - 1].descr.next = virt_to_phys(&TxDescList[0].descr);
  475. /* Initialise initial pointers */
  476. myNextRxDesc = &RxDescList[0];
  477. myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
  478. myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
  479. myFirstTxDesc = &TxDescList[0];
  480. myNextTxDesc = &TxDescList[0];
  481. myLastTxDesc = &TxDescList[NBR_OF_TX_DESC - 1];
  482. /* Register device */
  483. err = register_netdev(dev);
  484. if (err) {
  485. free_netdev(dev);
  486. return err;
  487. }
  488. /* set the default MAC address */
  489. e100_set_mac_address(dev, &default_mac);
  490. /* Initialize speed indicator stuff. */
  491. current_speed = 10;
  492. current_speed_selection = 0; /* Auto */
  493. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  494. duplex_timer.data = (unsigned long)dev;
  495. speed_timer.function = e100_check_speed;
  496. clear_led_timer.function = e100_clear_network_leds;
  497. full_duplex = 0;
  498. current_duplex = autoneg;
  499. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  500. duplex_timer.data = (unsigned long)dev;
  501. duplex_timer.function = e100_check_duplex;
  502. /* Initialize mii interface */
  503. np->mii_if.phy_id = mdio_phy_addr;
  504. np->mii_if.phy_id_mask = 0x1f;
  505. np->mii_if.reg_num_mask = 0x1f;
  506. np->mii_if.dev = dev;
  507. np->mii_if.mdio_read = e100_get_mdio_reg;
  508. np->mii_if.mdio_write = e100_set_mdio_reg;
  509. /* Initialize group address registers to make sure that no */
  510. /* unwanted addresses are matched */
  511. *R_NETWORK_GA_0 = 0x00000000;
  512. *R_NETWORK_GA_1 = 0x00000000;
  513. return 0;
  514. }
  515. /* set MAC address of the interface. called from the core after a
  516. * SIOCSIFADDR ioctl, and from the bootup above.
  517. */
  518. static int
  519. e100_set_mac_address(struct net_device *dev, void *p)
  520. {
  521. struct net_local *np = (struct net_local *)dev->priv;
  522. struct sockaddr *addr = p;
  523. int i;
  524. spin_lock(&np->lock); /* preemption protection */
  525. /* remember it */
  526. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  527. /* Write it to the hardware.
  528. * Note the way the address is wrapped:
  529. * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
  530. * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
  531. */
  532. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  533. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  534. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  535. *R_NETWORK_SA_2 = 0;
  536. /* show it in the log as well */
  537. printk(KERN_INFO "%s: changed MAC to ", dev->name);
  538. for (i = 0; i < 5; i++)
  539. printk("%02X:", dev->dev_addr[i]);
  540. printk("%02X\n", dev->dev_addr[i]);
  541. spin_unlock(&np->lock);
  542. return 0;
  543. }
  544. /*
  545. * Open/initialize the board. This is called (in the current kernel)
  546. * sometime after booting when the 'ifconfig' program is run.
  547. *
  548. * This routine should set everything up anew at each open, even
  549. * registers that "should" only need to be set once at boot, so that
  550. * there is non-reboot way to recover if something goes wrong.
  551. */
  552. static int
  553. e100_open(struct net_device *dev)
  554. {
  555. unsigned long flags;
  556. /* enable the MDIO output pin */
  557. *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
  558. *R_IRQ_MASK0_CLR =
  559. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  560. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  561. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  562. /* clear dma0 and 1 eop and descr irq masks */
  563. *R_IRQ_MASK2_CLR =
  564. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  565. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  566. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  567. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  568. /* Reset and wait for the DMA channels */
  569. RESET_DMA(NETWORK_TX_DMA_NBR);
  570. RESET_DMA(NETWORK_RX_DMA_NBR);
  571. WAIT_DMA(NETWORK_TX_DMA_NBR);
  572. WAIT_DMA(NETWORK_RX_DMA_NBR);
  573. /* Initialise the etrax network controller */
  574. /* allocate the irq corresponding to the receiving DMA */
  575. if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt,
  576. SA_SAMPLE_RANDOM, cardname, (void *)dev)) {
  577. goto grace_exit0;
  578. }
  579. /* allocate the irq corresponding to the transmitting DMA */
  580. if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
  581. cardname, (void *)dev)) {
  582. goto grace_exit1;
  583. }
  584. /* allocate the irq corresponding to the network errors etc */
  585. if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
  586. cardname, (void *)dev)) {
  587. goto grace_exit2;
  588. }
  589. /* give the HW an idea of what MAC address we want */
  590. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  591. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  592. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  593. *R_NETWORK_SA_2 = 0;
  594. #if 0
  595. /* use promiscuous mode for testing */
  596. *R_NETWORK_GA_0 = 0xffffffff;
  597. *R_NETWORK_GA_1 = 0xffffffff;
  598. *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
  599. #else
  600. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
  601. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
  602. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  603. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  604. #endif
  605. *R_NETWORK_GEN_CONFIG =
  606. IO_STATE(R_NETWORK_GEN_CONFIG, phy, mii_clk) |
  607. IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
  608. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  609. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
  610. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
  611. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
  612. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
  613. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
  614. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
  615. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  616. save_flags(flags);
  617. cli();
  618. /* enable the irq's for ethernet DMA */
  619. *R_IRQ_MASK2_SET =
  620. IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
  621. IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
  622. *R_IRQ_MASK0_SET =
  623. IO_STATE(R_IRQ_MASK0_SET, overrun, set) |
  624. IO_STATE(R_IRQ_MASK0_SET, underrun, set) |
  625. IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
  626. /* make sure the irqs are cleared */
  627. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  628. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  629. /* make sure the rec and transmit error counters are cleared */
  630. (void)*R_REC_COUNTERS; /* dummy read */
  631. (void)*R_TR_COUNTERS; /* dummy read */
  632. /* start the receiving DMA channel so we can receive packets from now on */
  633. *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
  634. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
  635. /* Set up transmit DMA channel so it can be restarted later */
  636. *R_DMA_CH0_FIRST = 0;
  637. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  638. restore_flags(flags);
  639. /* Probe for transceiver */
  640. if (e100_probe_transceiver(dev))
  641. goto grace_exit3;
  642. /* Start duplex/speed timers */
  643. add_timer(&speed_timer);
  644. add_timer(&duplex_timer);
  645. /* We are now ready to accept transmit requeusts from
  646. * the queueing layer of the networking.
  647. */
  648. netif_start_queue(dev);
  649. return 0;
  650. grace_exit3:
  651. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  652. grace_exit2:
  653. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  654. grace_exit1:
  655. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  656. grace_exit0:
  657. return -EAGAIN;
  658. }
  659. static void
  660. generic_check_speed(struct net_device* dev)
  661. {
  662. unsigned long data;
  663. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  664. if ((data & ADVERTISE_100FULL) ||
  665. (data & ADVERTISE_100HALF))
  666. current_speed = 100;
  667. else
  668. current_speed = 10;
  669. }
  670. static void
  671. tdk_check_speed(struct net_device* dev)
  672. {
  673. unsigned long data;
  674. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_TDK_DIAGNOSTIC_REG);
  675. current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
  676. }
  677. static void
  678. broadcom_check_speed(struct net_device* dev)
  679. {
  680. unsigned long data;
  681. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_AUX_CTRL_STATUS_REG);
  682. current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
  683. }
  684. static void
  685. intel_check_speed(struct net_device* dev)
  686. {
  687. unsigned long data;
  688. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_INT_STATUS_REG_2);
  689. current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
  690. }
  691. static void
  692. e100_check_speed(unsigned long priv)
  693. {
  694. struct net_device* dev = (struct net_device*)priv;
  695. static int led_initiated = 0;
  696. unsigned long data;
  697. int old_speed = current_speed;
  698. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMSR);
  699. if (!(data & BMSR_LSTATUS)) {
  700. current_speed = 0;
  701. } else {
  702. transceiver->check_speed(dev);
  703. }
  704. if ((old_speed != current_speed) || !led_initiated) {
  705. led_initiated = 1;
  706. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  707. }
  708. /* Reinitialize the timer. */
  709. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  710. add_timer(&speed_timer);
  711. }
  712. static void
  713. e100_negotiate(struct net_device* dev)
  714. {
  715. unsigned short data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  716. /* Discard old speed and duplex settings */
  717. data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL |
  718. ADVERTISE_10HALF | ADVERTISE_10FULL);
  719. switch (current_speed_selection) {
  720. case 10 :
  721. if (current_duplex == full)
  722. data |= ADVERTISE_10FULL;
  723. else if (current_duplex == half)
  724. data |= ADVERTISE_10HALF;
  725. else
  726. data |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  727. break;
  728. case 100 :
  729. if (current_duplex == full)
  730. data |= ADVERTISE_100FULL;
  731. else if (current_duplex == half)
  732. data |= ADVERTISE_100HALF;
  733. else
  734. data |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  735. break;
  736. case 0 : /* Auto */
  737. if (current_duplex == full)
  738. data |= ADVERTISE_100FULL | ADVERTISE_10FULL;
  739. else if (current_duplex == half)
  740. data |= ADVERTISE_100HALF | ADVERTISE_10HALF;
  741. else
  742. data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  743. ADVERTISE_100HALF | ADVERTISE_100FULL;
  744. break;
  745. default : /* assume autoneg speed and duplex */
  746. data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  747. ADVERTISE_100HALF | ADVERTISE_100FULL;
  748. }
  749. e100_set_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE, data);
  750. /* Renegotiate with link partner */
  751. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMCR);
  752. data |= BMCR_ANENABLE | BMCR_ANRESTART;
  753. e100_set_mdio_reg(dev, mdio_phy_addr, MII_BMCR, data);
  754. }
  755. static void
  756. e100_set_speed(struct net_device* dev, unsigned long speed)
  757. {
  758. if (speed != current_speed_selection) {
  759. current_speed_selection = speed;
  760. e100_negotiate(dev);
  761. }
  762. }
  763. static void
  764. e100_check_duplex(unsigned long priv)
  765. {
  766. struct net_device *dev = (struct net_device *)priv;
  767. struct net_local *np = (struct net_local *)dev->priv;
  768. int old_duplex = full_duplex;
  769. transceiver->check_duplex(dev);
  770. if (old_duplex != full_duplex) {
  771. /* Duplex changed */
  772. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  773. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  774. }
  775. /* Reinitialize the timer. */
  776. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  777. add_timer(&duplex_timer);
  778. np->mii_if.full_duplex = full_duplex;
  779. }
  780. static void
  781. generic_check_duplex(struct net_device* dev)
  782. {
  783. unsigned long data;
  784. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  785. if ((data & ADVERTISE_10FULL) ||
  786. (data & ADVERTISE_100FULL))
  787. full_duplex = 1;
  788. else
  789. full_duplex = 0;
  790. }
  791. static void
  792. tdk_check_duplex(struct net_device* dev)
  793. {
  794. unsigned long data;
  795. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_TDK_DIAGNOSTIC_REG);
  796. full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
  797. }
  798. static void
  799. broadcom_check_duplex(struct net_device* dev)
  800. {
  801. unsigned long data;
  802. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_AUX_CTRL_STATUS_REG);
  803. full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
  804. }
  805. static void
  806. intel_check_duplex(struct net_device* dev)
  807. {
  808. unsigned long data;
  809. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_INT_STATUS_REG_2);
  810. full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
  811. }
  812. static void
  813. e100_set_duplex(struct net_device* dev, enum duplex new_duplex)
  814. {
  815. if (new_duplex != current_duplex) {
  816. current_duplex = new_duplex;
  817. e100_negotiate(dev);
  818. }
  819. }
  820. static int
  821. e100_probe_transceiver(struct net_device* dev)
  822. {
  823. unsigned int phyid_high;
  824. unsigned int phyid_low;
  825. unsigned int oui;
  826. struct transceiver_ops* ops = NULL;
  827. /* Probe MDIO physical address */
  828. for (mdio_phy_addr = 0; mdio_phy_addr <= 31; mdio_phy_addr++) {
  829. if (e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMSR) != 0xffff)
  830. break;
  831. }
  832. if (mdio_phy_addr == 32)
  833. return -ENODEV;
  834. /* Get manufacturer */
  835. phyid_high = e100_get_mdio_reg(dev, mdio_phy_addr, MII_PHYSID1);
  836. phyid_low = e100_get_mdio_reg(dev, mdio_phy_addr, MII_PHYSID2);
  837. oui = (phyid_high << 6) | (phyid_low >> 10);
  838. for (ops = &transceivers[0]; ops->oui; ops++) {
  839. if (ops->oui == oui)
  840. break;
  841. }
  842. transceiver = ops;
  843. return 0;
  844. }
  845. static int
  846. e100_get_mdio_reg(struct net_device *dev, int phy_id, int location)
  847. {
  848. unsigned short cmd; /* Data to be sent on MDIO port */
  849. int data; /* Data read from MDIO */
  850. int bitCounter;
  851. /* Start of frame, OP Code, Physical Address, Register Address */
  852. cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (phy_id << 7) |
  853. (location << 2);
  854. e100_send_mdio_cmd(cmd, 0);
  855. data = 0;
  856. /* Data... */
  857. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  858. data |= (e100_receive_mdio_bit() << bitCounter);
  859. }
  860. return data;
  861. }
  862. static void
  863. e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value)
  864. {
  865. int bitCounter;
  866. unsigned short cmd;
  867. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (phy_id << 7) |
  868. (location << 2);
  869. e100_send_mdio_cmd(cmd, 1);
  870. /* Data... */
  871. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  872. e100_send_mdio_bit(GET_BIT(bitCounter, value));
  873. }
  874. }
  875. static void
  876. e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
  877. {
  878. int bitCounter;
  879. unsigned char data = 0x2;
  880. /* Preamble */
  881. for (bitCounter = 31; bitCounter>= 0; bitCounter--)
  882. e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
  883. for (bitCounter = 15; bitCounter >= 2; bitCounter--)
  884. e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
  885. /* Turnaround */
  886. for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
  887. if (write_cmd)
  888. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  889. else
  890. e100_receive_mdio_bit();
  891. }
  892. static void
  893. e100_send_mdio_bit(unsigned char bit)
  894. {
  895. *R_NETWORK_MGM_CTRL =
  896. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  897. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  898. udelay(1);
  899. *R_NETWORK_MGM_CTRL =
  900. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  901. IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
  902. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  903. udelay(1);
  904. }
  905. static unsigned char
  906. e100_receive_mdio_bit()
  907. {
  908. unsigned char bit;
  909. *R_NETWORK_MGM_CTRL = 0;
  910. bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
  911. udelay(1);
  912. *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
  913. udelay(1);
  914. return bit;
  915. }
  916. static void
  917. e100_reset_transceiver(struct net_device* dev)
  918. {
  919. unsigned short cmd;
  920. unsigned short data;
  921. int bitCounter;
  922. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMCR);
  923. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) | (MII_BMCR << 2);
  924. e100_send_mdio_cmd(cmd, 1);
  925. data |= 0x8000;
  926. for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
  927. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  928. }
  929. }
  930. /* Called by upper layers if they decide it took too long to complete
  931. * sending a packet - we need to reset and stuff.
  932. */
  933. static void
  934. e100_tx_timeout(struct net_device *dev)
  935. {
  936. struct net_local *np = (struct net_local *)dev->priv;
  937. unsigned long flags;
  938. spin_lock_irqsave(&np->lock, flags);
  939. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
  940. tx_done(dev) ? "IRQ problem" : "network cable problem");
  941. /* remember we got an error */
  942. np->stats.tx_errors++;
  943. /* reset the TX DMA in case it has hung on something */
  944. RESET_DMA(NETWORK_TX_DMA_NBR);
  945. WAIT_DMA(NETWORK_TX_DMA_NBR);
  946. /* Reset the transceiver. */
  947. e100_reset_transceiver(dev);
  948. /* and get rid of the packets that never got an interrupt */
  949. while (myFirstTxDesc != myNextTxDesc)
  950. {
  951. dev_kfree_skb(myFirstTxDesc->skb);
  952. myFirstTxDesc->skb = 0;
  953. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  954. }
  955. /* Set up transmit DMA channel so it can be restarted later */
  956. *R_DMA_CH0_FIRST = 0;
  957. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  958. /* tell the upper layers we're ok again */
  959. netif_wake_queue(dev);
  960. spin_unlock_irqrestore(&np->lock, flags);
  961. }
  962. /* This will only be invoked if the driver is _not_ in XOFF state.
  963. * What this means is that we need not check it, and that this
  964. * invariant will hold if we make sure that the netif_*_queue()
  965. * calls are done at the proper times.
  966. */
  967. static int
  968. e100_send_packet(struct sk_buff *skb, struct net_device *dev)
  969. {
  970. struct net_local *np = (struct net_local *)dev->priv;
  971. unsigned char *buf = skb->data;
  972. unsigned long flags;
  973. #ifdef ETHDEBUG
  974. printk("send packet len %d\n", length);
  975. #endif
  976. spin_lock_irqsave(&np->lock, flags); /* protect from tx_interrupt and ourself */
  977. myNextTxDesc->skb = skb;
  978. dev->trans_start = jiffies;
  979. e100_hardware_send_packet(buf, skb->len);
  980. myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
  981. /* Stop queue if full */
  982. if (myNextTxDesc == myFirstTxDesc) {
  983. netif_stop_queue(dev);
  984. }
  985. spin_unlock_irqrestore(&np->lock, flags);
  986. return 0;
  987. }
  988. /*
  989. * The typical workload of the driver:
  990. * Handle the network interface interrupts.
  991. */
  992. static irqreturn_t
  993. e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  994. {
  995. struct net_device *dev = (struct net_device *)dev_id;
  996. struct net_local *np = (struct net_local *)dev->priv;
  997. unsigned long irqbits = *R_IRQ_MASK2_RD;
  998. /* Disable RX/TX IRQs to avoid reentrancy */
  999. *R_IRQ_MASK2_CLR =
  1000. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  1001. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  1002. /* Handle received packets */
  1003. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
  1004. /* acknowledge the eop interrupt */
  1005. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  1006. /* check if one or more complete packets were indeed received */
  1007. while ((*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) &&
  1008. (myNextRxDesc != myLastRxDesc)) {
  1009. /* Take out the buffer and give it to the OS, then
  1010. * allocate a new buffer to put a packet in.
  1011. */
  1012. e100_rx(dev);
  1013. ((struct net_local *)dev->priv)->stats.rx_packets++;
  1014. /* restart/continue on the channel, for safety */
  1015. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
  1016. /* clear dma channel 1 eop/descr irq bits */
  1017. *R_DMA_CH1_CLR_INTR =
  1018. IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
  1019. IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
  1020. /* now, we might have gotten another packet
  1021. so we have to loop back and check if so */
  1022. }
  1023. }
  1024. /* Report any packets that have been sent */
  1025. while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
  1026. myFirstTxDesc != myNextTxDesc)
  1027. {
  1028. np->stats.tx_bytes += myFirstTxDesc->skb->len;
  1029. np->stats.tx_packets++;
  1030. /* dma is ready with the transmission of the data in tx_skb, so now
  1031. we can release the skb memory */
  1032. dev_kfree_skb_irq(myFirstTxDesc->skb);
  1033. myFirstTxDesc->skb = 0;
  1034. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  1035. }
  1036. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
  1037. /* acknowledge the eop interrupt and wake up queue */
  1038. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  1039. netif_wake_queue(dev);
  1040. }
  1041. /* Enable RX/TX IRQs again */
  1042. *R_IRQ_MASK2_SET =
  1043. IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
  1044. IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
  1045. return IRQ_HANDLED;
  1046. }
  1047. static irqreturn_t
  1048. e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1049. {
  1050. struct net_device *dev = (struct net_device *)dev_id;
  1051. struct net_local *np = (struct net_local *)dev->priv;
  1052. unsigned long irqbits = *R_IRQ_MASK0_RD;
  1053. /* check for underrun irq */
  1054. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
  1055. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  1056. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  1057. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
  1058. np->stats.tx_errors++;
  1059. D(printk("ethernet receiver underrun!\n"));
  1060. }
  1061. /* check for overrun irq */
  1062. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
  1063. update_rx_stats(&np->stats); /* this will ack the irq */
  1064. D(printk("ethernet receiver overrun!\n"));
  1065. }
  1066. /* check for excessive collision irq */
  1067. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
  1068. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  1069. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  1070. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
  1071. *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
  1072. np->stats.tx_errors++;
  1073. D(printk("ethernet excessive collisions!\n"));
  1074. }
  1075. return IRQ_HANDLED;
  1076. }
  1077. /* We have a good packet(s), get it/them out of the buffers. */
  1078. static void
  1079. e100_rx(struct net_device *dev)
  1080. {
  1081. struct sk_buff *skb;
  1082. int length = 0;
  1083. struct net_local *np = (struct net_local *)dev->priv;
  1084. unsigned char *skb_data_ptr;
  1085. #ifdef ETHDEBUG
  1086. int i;
  1087. #endif
  1088. if (!led_active && time_after(jiffies, led_next_time)) {
  1089. /* light the network leds depending on the current speed. */
  1090. e100_set_network_leds(NETWORK_ACTIVITY);
  1091. /* Set the earliest time we may clear the LED */
  1092. led_next_time = jiffies + NET_FLASH_TIME;
  1093. led_active = 1;
  1094. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1095. }
  1096. length = myNextRxDesc->descr.hw_len - 4;
  1097. ((struct net_local *)dev->priv)->stats.rx_bytes += length;
  1098. #ifdef ETHDEBUG
  1099. printk("Got a packet of length %d:\n", length);
  1100. /* dump the first bytes in the packet */
  1101. skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
  1102. for (i = 0; i < 8; i++) {
  1103. printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
  1104. skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
  1105. skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
  1106. skb_data_ptr += 8;
  1107. }
  1108. #endif
  1109. if (length < RX_COPYBREAK) {
  1110. /* Small packet, copy data */
  1111. skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
  1112. if (!skb) {
  1113. np->stats.rx_errors++;
  1114. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  1115. return;
  1116. }
  1117. skb_put(skb, length - ETHER_HEAD_LEN); /* allocate room for the packet body */
  1118. skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
  1119. #ifdef ETHDEBUG
  1120. printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
  1121. skb->head, skb->data, skb->tail, skb->end);
  1122. printk("copying packet to 0x%x.\n", skb_data_ptr);
  1123. #endif
  1124. memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
  1125. }
  1126. else {
  1127. /* Large packet, send directly to upper layers and allocate new
  1128. * memory (aligned to cache line boundary to avoid bug).
  1129. * Before sending the skb to upper layers we must make sure that
  1130. * skb->data points to the aligned start of the packet.
  1131. */
  1132. int align;
  1133. struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
  1134. if (!new_skb) {
  1135. np->stats.rx_errors++;
  1136. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  1137. return;
  1138. }
  1139. skb = myNextRxDesc->skb;
  1140. align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
  1141. skb_put(skb, length + align);
  1142. skb_pull(skb, align); /* Remove alignment bytes */
  1143. myNextRxDesc->skb = new_skb;
  1144. myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
  1145. }
  1146. skb->dev = dev;
  1147. skb->protocol = eth_type_trans(skb, dev);
  1148. /* Send the packet to the upper layers */
  1149. netif_rx(skb);
  1150. /* Prepare for next packet */
  1151. myNextRxDesc->descr.status = 0;
  1152. myPrevRxDesc = myNextRxDesc;
  1153. myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
  1154. rx_queue_len++;
  1155. /* Check if descriptors should be returned */
  1156. if (rx_queue_len == RX_QUEUE_THRESHOLD) {
  1157. flush_etrax_cache();
  1158. myPrevRxDesc->descr.ctrl |= d_eol;
  1159. myLastRxDesc->descr.ctrl &= ~d_eol;
  1160. myLastRxDesc = myPrevRxDesc;
  1161. rx_queue_len = 0;
  1162. }
  1163. }
  1164. /* The inverse routine to net_open(). */
  1165. static int
  1166. e100_close(struct net_device *dev)
  1167. {
  1168. struct net_local *np = (struct net_local *)dev->priv;
  1169. printk(KERN_INFO "Closing %s.\n", dev->name);
  1170. netif_stop_queue(dev);
  1171. *R_IRQ_MASK0_CLR =
  1172. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  1173. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  1174. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  1175. *R_IRQ_MASK2_CLR =
  1176. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  1177. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  1178. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  1179. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  1180. /* Stop the receiver and the transmitter */
  1181. RESET_DMA(NETWORK_TX_DMA_NBR);
  1182. RESET_DMA(NETWORK_RX_DMA_NBR);
  1183. /* Flush the Tx and disable Rx here. */
  1184. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  1185. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  1186. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  1187. /* Update the statistics here. */
  1188. update_rx_stats(&np->stats);
  1189. update_tx_stats(&np->stats);
  1190. /* Stop speed/duplex timers */
  1191. del_timer(&speed_timer);
  1192. del_timer(&duplex_timer);
  1193. return 0;
  1194. }
  1195. static int
  1196. e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1197. {
  1198. struct mii_ioctl_data *data = if_mii(ifr);
  1199. struct net_local *np = netdev_priv(dev);
  1200. spin_lock(&np->lock); /* Preempt protection */
  1201. switch (cmd) {
  1202. case SIOCGMIIPHY: /* Get PHY address */
  1203. data->phy_id = mdio_phy_addr;
  1204. break;
  1205. case SIOCGMIIREG: /* Read MII register */
  1206. data->val_out = e100_get_mdio_reg(dev, mdio_phy_addr, data->reg_num);
  1207. break;
  1208. case SIOCSMIIREG: /* Write MII register */
  1209. e100_set_mdio_reg(dev, mdio_phy_addr, data->reg_num, data->val_in);
  1210. break;
  1211. /* The ioctls below should be considered obsolete but are */
  1212. /* still present for compatability with old scripts/apps */
  1213. case SET_ETH_SPEED_10: /* 10 Mbps */
  1214. e100_set_speed(dev, 10);
  1215. break;
  1216. case SET_ETH_SPEED_100: /* 100 Mbps */
  1217. e100_set_speed(dev, 100);
  1218. break;
  1219. case SET_ETH_SPEED_AUTO: /* Auto negotiate speed */
  1220. e100_set_speed(dev, 0);
  1221. break;
  1222. case SET_ETH_DUPLEX_HALF: /* Half duplex. */
  1223. e100_set_duplex(dev, half);
  1224. break;
  1225. case SET_ETH_DUPLEX_FULL: /* Full duplex. */
  1226. e100_set_duplex(dev, full);
  1227. break;
  1228. case SET_ETH_DUPLEX_AUTO: /* Autonegotiate duplex*/
  1229. e100_set_duplex(dev, autoneg);
  1230. break;
  1231. default:
  1232. return -EINVAL;
  1233. }
  1234. spin_unlock(&np->lock);
  1235. return 0;
  1236. }
  1237. static int e100_set_settings(struct net_device *dev,
  1238. struct ethtool_cmd *ecmd)
  1239. {
  1240. ecmd->supported = SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
  1241. SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  1242. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
  1243. ecmd->port = PORT_TP;
  1244. ecmd->transceiver = XCVR_EXTERNAL;
  1245. ecmd->phy_address = mdio_phy_addr;
  1246. ecmd->speed = current_speed;
  1247. ecmd->duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1248. ecmd->advertising = ADVERTISED_TP;
  1249. if (current_duplex == autoneg && current_speed_selection == 0)
  1250. ecmd->advertising |= ADVERTISED_Autoneg;
  1251. else {
  1252. ecmd->advertising |=
  1253. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  1254. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  1255. if (current_speed_selection == 10)
  1256. ecmd->advertising &= ~(ADVERTISED_100baseT_Half |
  1257. ADVERTISED_100baseT_Full);
  1258. else if (current_speed_selection == 100)
  1259. ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
  1260. ADVERTISED_10baseT_Full);
  1261. if (current_duplex == half)
  1262. ecmd->advertising &= ~(ADVERTISED_10baseT_Full |
  1263. ADVERTISED_100baseT_Full);
  1264. else if (current_duplex == full)
  1265. ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
  1266. ADVERTISED_100baseT_Half);
  1267. }
  1268. ecmd->autoneg = AUTONEG_ENABLE;
  1269. return 0;
  1270. }
  1271. static int e100_set_settings(struct net_device *dev,
  1272. struct ethtool_cmd *ecmd)
  1273. {
  1274. if (ecmd->autoneg == AUTONEG_ENABLE) {
  1275. e100_set_duplex(dev, autoneg);
  1276. e100_set_speed(dev, 0);
  1277. } else {
  1278. e100_set_duplex(dev, ecmd->duplex == DUPLEX_HALF ? half : full);
  1279. e100_set_speed(dev, ecmd->speed == SPEED_10 ? 10: 100);
  1280. }
  1281. return 0;
  1282. }
  1283. static void e100_get_drvinfo(struct net_device *dev,
  1284. struct ethtool_drvinfo *info)
  1285. {
  1286. strncpy(info->driver, "ETRAX 100LX", sizeof(info->driver) - 1);
  1287. strncpy(info->version, "$Revision: 1.31 $", sizeof(info->version) - 1);
  1288. strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
  1289. strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
  1290. }
  1291. static int e100_nway_reset(struct net_device *dev)
  1292. {
  1293. if (current_duplex == autoneg && current_speed_selection == 0)
  1294. e100_negotiate(dev);
  1295. return 0;
  1296. }
  1297. static struct ethtool_ops e100_ethtool_ops = {
  1298. .get_settings = e100_get_settings,
  1299. .set_settings = e100_set_settings,
  1300. .get_drvinfo = e100_get_drvinfo,
  1301. .nway_reset = e100_nway_reset,
  1302. .get_link = ethtool_op_get_link,
  1303. };
  1304. static int
  1305. e100_set_config(struct net_device *dev, struct ifmap *map)
  1306. {
  1307. struct net_local *np = (struct net_local *)dev->priv;
  1308. spin_lock(&np->lock); /* Preempt protection */
  1309. switch(map->port) {
  1310. case IF_PORT_UNKNOWN:
  1311. /* Use autoneg */
  1312. e100_set_speed(dev, 0);
  1313. e100_set_duplex(dev, autoneg);
  1314. break;
  1315. case IF_PORT_10BASET:
  1316. e100_set_speed(dev, 10);
  1317. e100_set_duplex(dev, autoneg);
  1318. break;
  1319. case IF_PORT_100BASET:
  1320. case IF_PORT_100BASETX:
  1321. e100_set_speed(dev, 100);
  1322. e100_set_duplex(dev, autoneg);
  1323. break;
  1324. case IF_PORT_100BASEFX:
  1325. case IF_PORT_10BASE2:
  1326. case IF_PORT_AUI:
  1327. spin_unlock(&np->lock);
  1328. return -EOPNOTSUPP;
  1329. break;
  1330. default:
  1331. printk(KERN_ERR "%s: Invalid media selected", dev->name);
  1332. spin_unlock(&np->lock);
  1333. return -EINVAL;
  1334. }
  1335. spin_unlock(&np->lock);
  1336. return 0;
  1337. }
  1338. static void
  1339. update_rx_stats(struct net_device_stats *es)
  1340. {
  1341. unsigned long r = *R_REC_COUNTERS;
  1342. /* update stats relevant to reception errors */
  1343. es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
  1344. es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
  1345. es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
  1346. es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
  1347. }
  1348. static void
  1349. update_tx_stats(struct net_device_stats *es)
  1350. {
  1351. unsigned long r = *R_TR_COUNTERS;
  1352. /* update stats relevant to transmission errors */
  1353. es->collisions +=
  1354. IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
  1355. IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
  1356. es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
  1357. }
  1358. /*
  1359. * Get the current statistics.
  1360. * This may be called with the card open or closed.
  1361. */
  1362. static struct net_device_stats *
  1363. e100_get_stats(struct net_device *dev)
  1364. {
  1365. struct net_local *lp = (struct net_local *)dev->priv;
  1366. unsigned long flags;
  1367. spin_lock_irqsave(&lp->lock, flags);
  1368. update_rx_stats(&lp->stats);
  1369. update_tx_stats(&lp->stats);
  1370. spin_unlock_irqrestore(&lp->lock, flags);
  1371. return &lp->stats;
  1372. }
  1373. /*
  1374. * Set or clear the multicast filter for this adaptor.
  1375. * num_addrs == -1 Promiscuous mode, receive all packets
  1376. * num_addrs == 0 Normal mode, clear multicast list
  1377. * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1378. * and do best-effort filtering.
  1379. */
  1380. static void
  1381. set_multicast_list(struct net_device *dev)
  1382. {
  1383. struct net_local *lp = (struct net_local *)dev->priv;
  1384. int num_addr = dev->mc_count;
  1385. unsigned long int lo_bits;
  1386. unsigned long int hi_bits;
  1387. spin_lock(&lp->lock);
  1388. if (dev->flags & IFF_PROMISC)
  1389. {
  1390. /* promiscuous mode */
  1391. lo_bits = 0xfffffffful;
  1392. hi_bits = 0xfffffffful;
  1393. /* Enable individual receive */
  1394. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
  1395. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1396. } else if (dev->flags & IFF_ALLMULTI) {
  1397. /* enable all multicasts */
  1398. lo_bits = 0xfffffffful;
  1399. hi_bits = 0xfffffffful;
  1400. /* Disable individual receive */
  1401. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1402. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1403. } else if (num_addr == 0) {
  1404. /* Normal, clear the mc list */
  1405. lo_bits = 0x00000000ul;
  1406. hi_bits = 0x00000000ul;
  1407. /* Disable individual receive */
  1408. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1409. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1410. } else {
  1411. /* MC mode, receive normal and MC packets */
  1412. char hash_ix;
  1413. struct dev_mc_list *dmi = dev->mc_list;
  1414. int i;
  1415. char *baddr;
  1416. lo_bits = 0x00000000ul;
  1417. hi_bits = 0x00000000ul;
  1418. for (i=0; i<num_addr; i++) {
  1419. /* Calculate the hash index for the GA registers */
  1420. hash_ix = 0;
  1421. baddr = dmi->dmi_addr;
  1422. hash_ix ^= (*baddr) & 0x3f;
  1423. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1424. ++baddr;
  1425. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1426. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1427. ++baddr;
  1428. hash_ix ^= ((*baddr) << 4) & 0x30;
  1429. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1430. ++baddr;
  1431. hash_ix ^= (*baddr) & 0x3f;
  1432. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1433. ++baddr;
  1434. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1435. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1436. ++baddr;
  1437. hash_ix ^= ((*baddr) << 4) & 0x30;
  1438. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1439. hash_ix &= 0x3f;
  1440. if (hash_ix >= 32) {
  1441. hi_bits |= (1 << (hash_ix-32));
  1442. }
  1443. else {
  1444. lo_bits |= (1 << hash_ix);
  1445. }
  1446. dmi = dmi->next;
  1447. }
  1448. /* Disable individual receive */
  1449. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1450. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1451. }
  1452. *R_NETWORK_GA_0 = lo_bits;
  1453. *R_NETWORK_GA_1 = hi_bits;
  1454. spin_unlock(&lp->lock);
  1455. }
  1456. void
  1457. e100_hardware_send_packet(char *buf, int length)
  1458. {
  1459. D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
  1460. if (!led_active && time_after(jiffies, led_next_time)) {
  1461. /* light the network leds depending on the current speed. */
  1462. e100_set_network_leds(NETWORK_ACTIVITY);
  1463. /* Set the earliest time we may clear the LED */
  1464. led_next_time = jiffies + NET_FLASH_TIME;
  1465. led_active = 1;
  1466. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1467. }
  1468. /* configure the tx dma descriptor */
  1469. myNextTxDesc->descr.sw_len = length;
  1470. myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
  1471. myNextTxDesc->descr.buf = virt_to_phys(buf);
  1472. /* Move end of list */
  1473. myLastTxDesc->descr.ctrl &= ~d_eol;
  1474. myLastTxDesc = myNextTxDesc;
  1475. /* Restart DMA channel */
  1476. *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
  1477. }
  1478. static void
  1479. e100_clear_network_leds(unsigned long dummy)
  1480. {
  1481. if (led_active && time_after(jiffies, led_next_time)) {
  1482. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  1483. /* Set the earliest time we may set the LED */
  1484. led_next_time = jiffies + NET_FLASH_PAUSE;
  1485. led_active = 0;
  1486. }
  1487. }
  1488. static void
  1489. e100_set_network_leds(int active)
  1490. {
  1491. #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
  1492. int light_leds = (active == NO_NETWORK_ACTIVITY);
  1493. #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
  1494. int light_leds = (active == NETWORK_ACTIVITY);
  1495. #else
  1496. #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
  1497. #endif
  1498. if (!current_speed) {
  1499. /* Make LED red, link is down */
  1500. #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
  1501. LED_NETWORK_SET(LED_RED);
  1502. #else
  1503. LED_NETWORK_SET(LED_OFF);
  1504. #endif
  1505. }
  1506. else if (light_leds) {
  1507. if (current_speed == 10) {
  1508. LED_NETWORK_SET(LED_ORANGE);
  1509. } else {
  1510. LED_NETWORK_SET(LED_GREEN);
  1511. }
  1512. }
  1513. else {
  1514. LED_NETWORK_SET(LED_OFF);
  1515. }
  1516. }
  1517. static int
  1518. etrax_init_module(void)
  1519. {
  1520. return etrax_ethernet_init();
  1521. }
  1522. static int __init
  1523. e100_boot_setup(char* str)
  1524. {
  1525. struct sockaddr sa = {0};
  1526. int i;
  1527. /* Parse the colon separated Ethernet station address */
  1528. for (i = 0; i < ETH_ALEN; i++) {
  1529. unsigned int tmp;
  1530. if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
  1531. printk(KERN_WARNING "Malformed station address");
  1532. return 0;
  1533. }
  1534. sa.sa_data[i] = (char)tmp;
  1535. }
  1536. default_mac = sa;
  1537. return 1;
  1538. }
  1539. __setup("etrax100_eth=", e100_boot_setup);
  1540. module_init(etrax_init_module);