eth_v10.c 52 KB

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