eth_v10.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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_ethtool_ioctl(struct net_device* dev, struct ifreq *ifr);
  373. static int e100_set_config(struct net_device* dev, struct ifmap* map);
  374. static void e100_tx_timeout(struct net_device *dev);
  375. static struct net_device_stats *e100_get_stats(struct net_device *dev);
  376. static void set_multicast_list(struct net_device *dev);
  377. static void e100_hardware_send_packet(char *buf, int length);
  378. static void update_rx_stats(struct net_device_stats *);
  379. static void update_tx_stats(struct net_device_stats *);
  380. static int e100_probe_transceiver(struct net_device* dev);
  381. static void e100_check_speed(unsigned long priv);
  382. static void e100_set_speed(struct net_device* dev, unsigned long speed);
  383. static void e100_check_duplex(unsigned long priv);
  384. static void e100_set_duplex(struct net_device* dev, enum duplex);
  385. static void e100_negotiate(struct net_device* dev);
  386. static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
  387. static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);
  388. static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
  389. static void e100_send_mdio_bit(unsigned char bit);
  390. static unsigned char e100_receive_mdio_bit(void);
  391. static void e100_reset_transceiver(struct net_device* net);
  392. static void e100_clear_network_leds(unsigned long dummy);
  393. static void e100_set_network_leds(int active);
  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->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. RxDescList[i].descr.ctrl = 0;
  452. RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
  453. RxDescList[i].descr.next = virt_to_phys(&RxDescList[i + 1]);
  454. RxDescList[i].descr.buf = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
  455. RxDescList[i].descr.status = 0;
  456. RxDescList[i].descr.hw_len = 0;
  457. prepare_rx_descriptor(&RxDescList[i].descr);
  458. }
  459. RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl = d_eol;
  460. RxDescList[NBR_OF_RX_DESC - 1].descr.next = virt_to_phys(&RxDescList[0]);
  461. rx_queue_len = 0;
  462. /* Initialize transmit descriptors */
  463. for (i = 0; i < NBR_OF_TX_DESC; i++) {
  464. TxDescList[i].descr.ctrl = 0;
  465. TxDescList[i].descr.sw_len = 0;
  466. TxDescList[i].descr.next = virt_to_phys(&TxDescList[i + 1].descr);
  467. TxDescList[i].descr.buf = 0;
  468. TxDescList[i].descr.status = 0;
  469. TxDescList[i].descr.hw_len = 0;
  470. TxDescList[i].skb = 0;
  471. }
  472. TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl = d_eol;
  473. TxDescList[NBR_OF_TX_DESC - 1].descr.next = virt_to_phys(&TxDescList[0].descr);
  474. /* Initialise initial pointers */
  475. myNextRxDesc = &RxDescList[0];
  476. myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
  477. myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
  478. myFirstTxDesc = &TxDescList[0];
  479. myNextTxDesc = &TxDescList[0];
  480. myLastTxDesc = &TxDescList[NBR_OF_TX_DESC - 1];
  481. /* Register device */
  482. err = register_netdev(dev);
  483. if (err) {
  484. free_netdev(dev);
  485. return err;
  486. }
  487. /* set the default MAC address */
  488. e100_set_mac_address(dev, &default_mac);
  489. /* Initialize speed indicator stuff. */
  490. current_speed = 10;
  491. current_speed_selection = 0; /* Auto */
  492. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  493. duplex_timer.data = (unsigned long)dev;
  494. speed_timer.function = e100_check_speed;
  495. clear_led_timer.function = e100_clear_network_leds;
  496. full_duplex = 0;
  497. current_duplex = autoneg;
  498. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  499. duplex_timer.data = (unsigned long)dev;
  500. duplex_timer.function = e100_check_duplex;
  501. /* Initialize mii interface */
  502. np->mii_if.phy_id = mdio_phy_addr;
  503. np->mii_if.phy_id_mask = 0x1f;
  504. np->mii_if.reg_num_mask = 0x1f;
  505. np->mii_if.dev = dev;
  506. np->mii_if.mdio_read = e100_get_mdio_reg;
  507. np->mii_if.mdio_write = e100_set_mdio_reg;
  508. /* Initialize group address registers to make sure that no */
  509. /* unwanted addresses are matched */
  510. *R_NETWORK_GA_0 = 0x00000000;
  511. *R_NETWORK_GA_1 = 0x00000000;
  512. return 0;
  513. }
  514. /* set MAC address of the interface. called from the core after a
  515. * SIOCSIFADDR ioctl, and from the bootup above.
  516. */
  517. static int
  518. e100_set_mac_address(struct net_device *dev, void *p)
  519. {
  520. struct net_local *np = (struct net_local *)dev->priv;
  521. struct sockaddr *addr = p;
  522. int i;
  523. spin_lock(&np->lock); /* preemption protection */
  524. /* remember it */
  525. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  526. /* Write it to the hardware.
  527. * Note the way the address is wrapped:
  528. * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
  529. * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
  530. */
  531. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  532. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  533. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  534. *R_NETWORK_SA_2 = 0;
  535. /* show it in the log as well */
  536. printk(KERN_INFO "%s: changed MAC to ", dev->name);
  537. for (i = 0; i < 5; i++)
  538. printk("%02X:", dev->dev_addr[i]);
  539. printk("%02X\n", dev->dev_addr[i]);
  540. spin_unlock(&np->lock);
  541. return 0;
  542. }
  543. /*
  544. * Open/initialize the board. This is called (in the current kernel)
  545. * sometime after booting when the 'ifconfig' program is run.
  546. *
  547. * This routine should set everything up anew at each open, even
  548. * registers that "should" only need to be set once at boot, so that
  549. * there is non-reboot way to recover if something goes wrong.
  550. */
  551. static int
  552. e100_open(struct net_device *dev)
  553. {
  554. unsigned long flags;
  555. /* enable the MDIO output pin */
  556. *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
  557. *R_IRQ_MASK0_CLR =
  558. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  559. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  560. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  561. /* clear dma0 and 1 eop and descr irq masks */
  562. *R_IRQ_MASK2_CLR =
  563. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  564. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  565. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  566. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  567. /* Reset and wait for the DMA channels */
  568. RESET_DMA(NETWORK_TX_DMA_NBR);
  569. RESET_DMA(NETWORK_RX_DMA_NBR);
  570. WAIT_DMA(NETWORK_TX_DMA_NBR);
  571. WAIT_DMA(NETWORK_RX_DMA_NBR);
  572. /* Initialise the etrax network controller */
  573. /* allocate the irq corresponding to the receiving DMA */
  574. if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt,
  575. SA_SAMPLE_RANDOM, cardname, (void *)dev)) {
  576. goto grace_exit0;
  577. }
  578. /* allocate the irq corresponding to the transmitting DMA */
  579. if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
  580. cardname, (void *)dev)) {
  581. goto grace_exit1;
  582. }
  583. /* allocate the irq corresponding to the network errors etc */
  584. if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
  585. cardname, (void *)dev)) {
  586. goto grace_exit2;
  587. }
  588. /* give the HW an idea of what MAC address we want */
  589. *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
  590. (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
  591. *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
  592. *R_NETWORK_SA_2 = 0;
  593. #if 0
  594. /* use promiscuous mode for testing */
  595. *R_NETWORK_GA_0 = 0xffffffff;
  596. *R_NETWORK_GA_1 = 0xffffffff;
  597. *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
  598. #else
  599. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
  600. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
  601. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  602. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  603. #endif
  604. *R_NETWORK_GEN_CONFIG =
  605. IO_STATE(R_NETWORK_GEN_CONFIG, phy, mii_clk) |
  606. IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
  607. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  608. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
  609. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
  610. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
  611. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
  612. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
  613. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
  614. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  615. save_flags(flags);
  616. cli();
  617. /* enable the irq's for ethernet DMA */
  618. *R_IRQ_MASK2_SET =
  619. IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
  620. IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
  621. *R_IRQ_MASK0_SET =
  622. IO_STATE(R_IRQ_MASK0_SET, overrun, set) |
  623. IO_STATE(R_IRQ_MASK0_SET, underrun, set) |
  624. IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
  625. /* make sure the irqs are cleared */
  626. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  627. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  628. /* make sure the rec and transmit error counters are cleared */
  629. (void)*R_REC_COUNTERS; /* dummy read */
  630. (void)*R_TR_COUNTERS; /* dummy read */
  631. /* start the receiving DMA channel so we can receive packets from now on */
  632. *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
  633. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
  634. /* Set up transmit DMA channel so it can be restarted later */
  635. *R_DMA_CH0_FIRST = 0;
  636. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  637. restore_flags(flags);
  638. /* Probe for transceiver */
  639. if (e100_probe_transceiver(dev))
  640. goto grace_exit3;
  641. /* Start duplex/speed timers */
  642. add_timer(&speed_timer);
  643. add_timer(&duplex_timer);
  644. /* We are now ready to accept transmit requeusts from
  645. * the queueing layer of the networking.
  646. */
  647. netif_start_queue(dev);
  648. return 0;
  649. grace_exit3:
  650. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  651. grace_exit2:
  652. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  653. grace_exit1:
  654. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  655. grace_exit0:
  656. return -EAGAIN;
  657. }
  658. static void
  659. generic_check_speed(struct net_device* dev)
  660. {
  661. unsigned long data;
  662. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  663. if ((data & ADVERTISE_100FULL) ||
  664. (data & ADVERTISE_100HALF))
  665. current_speed = 100;
  666. else
  667. current_speed = 10;
  668. }
  669. static void
  670. tdk_check_speed(struct net_device* dev)
  671. {
  672. unsigned long data;
  673. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_TDK_DIAGNOSTIC_REG);
  674. current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
  675. }
  676. static void
  677. broadcom_check_speed(struct net_device* dev)
  678. {
  679. unsigned long data;
  680. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_AUX_CTRL_STATUS_REG);
  681. current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
  682. }
  683. static void
  684. intel_check_speed(struct net_device* dev)
  685. {
  686. unsigned long data;
  687. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_INT_STATUS_REG_2);
  688. current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
  689. }
  690. static void
  691. e100_check_speed(unsigned long priv)
  692. {
  693. struct net_device* dev = (struct net_device*)priv;
  694. static int led_initiated = 0;
  695. unsigned long data;
  696. int old_speed = current_speed;
  697. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMSR);
  698. if (!(data & BMSR_LSTATUS)) {
  699. current_speed = 0;
  700. } else {
  701. transceiver->check_speed(dev);
  702. }
  703. if ((old_speed != current_speed) || !led_initiated) {
  704. led_initiated = 1;
  705. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  706. }
  707. /* Reinitialize the timer. */
  708. speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
  709. add_timer(&speed_timer);
  710. }
  711. static void
  712. e100_negotiate(struct net_device* dev)
  713. {
  714. unsigned short data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  715. /* Discard old speed and duplex settings */
  716. data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL |
  717. ADVERTISE_10HALF | ADVERTISE_10FULL);
  718. switch (current_speed_selection) {
  719. case 10 :
  720. if (current_duplex == full)
  721. data |= ADVERTISE_10FULL;
  722. else if (current_duplex == half)
  723. data |= ADVERTISE_10HALF;
  724. else
  725. data |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  726. break;
  727. case 100 :
  728. if (current_duplex == full)
  729. data |= ADVERTISE_100FULL;
  730. else if (current_duplex == half)
  731. data |= ADVERTISE_100HALF;
  732. else
  733. data |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  734. break;
  735. case 0 : /* Auto */
  736. if (current_duplex == full)
  737. data |= ADVERTISE_100FULL | ADVERTISE_10FULL;
  738. else if (current_duplex == half)
  739. data |= ADVERTISE_100HALF | ADVERTISE_10HALF;
  740. else
  741. data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  742. ADVERTISE_100HALF | ADVERTISE_100FULL;
  743. break;
  744. default : /* assume autoneg speed and duplex */
  745. data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  746. ADVERTISE_100HALF | ADVERTISE_100FULL;
  747. }
  748. e100_set_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE, data);
  749. /* Renegotiate with link partner */
  750. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMCR);
  751. data |= BMCR_ANENABLE | BMCR_ANRESTART;
  752. e100_set_mdio_reg(dev, mdio_phy_addr, MII_BMCR, data);
  753. }
  754. static void
  755. e100_set_speed(struct net_device* dev, unsigned long speed)
  756. {
  757. if (speed != current_speed_selection) {
  758. current_speed_selection = speed;
  759. e100_negotiate(dev);
  760. }
  761. }
  762. static void
  763. e100_check_duplex(unsigned long priv)
  764. {
  765. struct net_device *dev = (struct net_device *)priv;
  766. struct net_local *np = (struct net_local *)dev->priv;
  767. int old_duplex = full_duplex;
  768. transceiver->check_duplex(dev);
  769. if (old_duplex != full_duplex) {
  770. /* Duplex changed */
  771. SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
  772. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  773. }
  774. /* Reinitialize the timer. */
  775. duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
  776. add_timer(&duplex_timer);
  777. np->mii_if.full_duplex = full_duplex;
  778. }
  779. static void
  780. generic_check_duplex(struct net_device* dev)
  781. {
  782. unsigned long data;
  783. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_ADVERTISE);
  784. if ((data & ADVERTISE_10FULL) ||
  785. (data & ADVERTISE_100FULL))
  786. full_duplex = 1;
  787. else
  788. full_duplex = 0;
  789. }
  790. static void
  791. tdk_check_duplex(struct net_device* dev)
  792. {
  793. unsigned long data;
  794. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_TDK_DIAGNOSTIC_REG);
  795. full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
  796. }
  797. static void
  798. broadcom_check_duplex(struct net_device* dev)
  799. {
  800. unsigned long data;
  801. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_AUX_CTRL_STATUS_REG);
  802. full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
  803. }
  804. static void
  805. intel_check_duplex(struct net_device* dev)
  806. {
  807. unsigned long data;
  808. data = e100_get_mdio_reg(dev, mdio_phy_addr, MDIO_INT_STATUS_REG_2);
  809. full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
  810. }
  811. static void
  812. e100_set_duplex(struct net_device* dev, enum duplex new_duplex)
  813. {
  814. if (new_duplex != current_duplex) {
  815. current_duplex = new_duplex;
  816. e100_negotiate(dev);
  817. }
  818. }
  819. static int
  820. e100_probe_transceiver(struct net_device* dev)
  821. {
  822. unsigned int phyid_high;
  823. unsigned int phyid_low;
  824. unsigned int oui;
  825. struct transceiver_ops* ops = NULL;
  826. /* Probe MDIO physical address */
  827. for (mdio_phy_addr = 0; mdio_phy_addr <= 31; mdio_phy_addr++) {
  828. if (e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMSR) != 0xffff)
  829. break;
  830. }
  831. if (mdio_phy_addr == 32)
  832. return -ENODEV;
  833. /* Get manufacturer */
  834. phyid_high = e100_get_mdio_reg(dev, mdio_phy_addr, MII_PHYSID1);
  835. phyid_low = e100_get_mdio_reg(dev, mdio_phy_addr, MII_PHYSID2);
  836. oui = (phyid_high << 6) | (phyid_low >> 10);
  837. for (ops = &transceivers[0]; ops->oui; ops++) {
  838. if (ops->oui == oui)
  839. break;
  840. }
  841. transceiver = ops;
  842. return 0;
  843. }
  844. static int
  845. e100_get_mdio_reg(struct net_device *dev, int phy_id, int location)
  846. {
  847. unsigned short cmd; /* Data to be sent on MDIO port */
  848. int data; /* Data read from MDIO */
  849. int bitCounter;
  850. /* Start of frame, OP Code, Physical Address, Register Address */
  851. cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (phy_id << 7) |
  852. (location << 2);
  853. e100_send_mdio_cmd(cmd, 0);
  854. data = 0;
  855. /* Data... */
  856. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  857. data |= (e100_receive_mdio_bit() << bitCounter);
  858. }
  859. return data;
  860. }
  861. static void
  862. e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value)
  863. {
  864. int bitCounter;
  865. unsigned short cmd;
  866. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (phy_id << 7) |
  867. (location << 2);
  868. e100_send_mdio_cmd(cmd, 1);
  869. /* Data... */
  870. for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
  871. e100_send_mdio_bit(GET_BIT(bitCounter, value));
  872. }
  873. }
  874. static void
  875. e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
  876. {
  877. int bitCounter;
  878. unsigned char data = 0x2;
  879. /* Preamble */
  880. for (bitCounter = 31; bitCounter>= 0; bitCounter--)
  881. e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
  882. for (bitCounter = 15; bitCounter >= 2; bitCounter--)
  883. e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
  884. /* Turnaround */
  885. for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
  886. if (write_cmd)
  887. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  888. else
  889. e100_receive_mdio_bit();
  890. }
  891. static void
  892. e100_send_mdio_bit(unsigned char bit)
  893. {
  894. *R_NETWORK_MGM_CTRL =
  895. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  896. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  897. udelay(1);
  898. *R_NETWORK_MGM_CTRL =
  899. IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
  900. IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
  901. IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
  902. udelay(1);
  903. }
  904. static unsigned char
  905. e100_receive_mdio_bit()
  906. {
  907. unsigned char bit;
  908. *R_NETWORK_MGM_CTRL = 0;
  909. bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
  910. udelay(1);
  911. *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
  912. udelay(1);
  913. return bit;
  914. }
  915. static void
  916. e100_reset_transceiver(struct net_device* dev)
  917. {
  918. unsigned short cmd;
  919. unsigned short data;
  920. int bitCounter;
  921. data = e100_get_mdio_reg(dev, mdio_phy_addr, MII_BMCR);
  922. cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) | (MII_BMCR << 2);
  923. e100_send_mdio_cmd(cmd, 1);
  924. data |= 0x8000;
  925. for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
  926. e100_send_mdio_bit(GET_BIT(bitCounter, data));
  927. }
  928. }
  929. /* Called by upper layers if they decide it took too long to complete
  930. * sending a packet - we need to reset and stuff.
  931. */
  932. static void
  933. e100_tx_timeout(struct net_device *dev)
  934. {
  935. struct net_local *np = (struct net_local *)dev->priv;
  936. unsigned long flags;
  937. spin_lock_irqsave(&np->lock, flags);
  938. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
  939. tx_done(dev) ? "IRQ problem" : "network cable problem");
  940. /* remember we got an error */
  941. np->stats.tx_errors++;
  942. /* reset the TX DMA in case it has hung on something */
  943. RESET_DMA(NETWORK_TX_DMA_NBR);
  944. WAIT_DMA(NETWORK_TX_DMA_NBR);
  945. /* Reset the transceiver. */
  946. e100_reset_transceiver(dev);
  947. /* and get rid of the packets that never got an interrupt */
  948. while (myFirstTxDesc != myNextTxDesc)
  949. {
  950. dev_kfree_skb(myFirstTxDesc->skb);
  951. myFirstTxDesc->skb = 0;
  952. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  953. }
  954. /* Set up transmit DMA channel so it can be restarted later */
  955. *R_DMA_CH0_FIRST = 0;
  956. *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
  957. /* tell the upper layers we're ok again */
  958. netif_wake_queue(dev);
  959. spin_unlock_irqrestore(&np->lock, flags);
  960. }
  961. /* This will only be invoked if the driver is _not_ in XOFF state.
  962. * What this means is that we need not check it, and that this
  963. * invariant will hold if we make sure that the netif_*_queue()
  964. * calls are done at the proper times.
  965. */
  966. static int
  967. e100_send_packet(struct sk_buff *skb, struct net_device *dev)
  968. {
  969. struct net_local *np = (struct net_local *)dev->priv;
  970. unsigned char *buf = skb->data;
  971. unsigned long flags;
  972. #ifdef ETHDEBUG
  973. printk("send packet len %d\n", length);
  974. #endif
  975. spin_lock_irqsave(&np->lock, flags); /* protect from tx_interrupt and ourself */
  976. myNextTxDesc->skb = skb;
  977. dev->trans_start = jiffies;
  978. e100_hardware_send_packet(buf, skb->len);
  979. myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
  980. /* Stop queue if full */
  981. if (myNextTxDesc == myFirstTxDesc) {
  982. netif_stop_queue(dev);
  983. }
  984. spin_unlock_irqrestore(&np->lock, flags);
  985. return 0;
  986. }
  987. /*
  988. * The typical workload of the driver:
  989. * Handle the network interface interrupts.
  990. */
  991. static irqreturn_t
  992. e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  993. {
  994. struct net_device *dev = (struct net_device *)dev_id;
  995. struct net_local *np = (struct net_local *)dev->priv;
  996. unsigned long irqbits = *R_IRQ_MASK2_RD;
  997. /* Disable RX/TX IRQs to avoid reentrancy */
  998. *R_IRQ_MASK2_CLR =
  999. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  1000. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  1001. /* Handle received packets */
  1002. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
  1003. /* acknowledge the eop interrupt */
  1004. *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
  1005. /* check if one or more complete packets were indeed received */
  1006. while ((*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) &&
  1007. (myNextRxDesc != myLastRxDesc)) {
  1008. /* Take out the buffer and give it to the OS, then
  1009. * allocate a new buffer to put a packet in.
  1010. */
  1011. e100_rx(dev);
  1012. ((struct net_local *)dev->priv)->stats.rx_packets++;
  1013. /* restart/continue on the channel, for safety */
  1014. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
  1015. /* clear dma channel 1 eop/descr irq bits */
  1016. *R_DMA_CH1_CLR_INTR =
  1017. IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
  1018. IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
  1019. /* now, we might have gotten another packet
  1020. so we have to loop back and check if so */
  1021. }
  1022. }
  1023. /* Report any packets that have been sent */
  1024. while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
  1025. myFirstTxDesc != myNextTxDesc)
  1026. {
  1027. np->stats.tx_bytes += myFirstTxDesc->skb->len;
  1028. np->stats.tx_packets++;
  1029. /* dma is ready with the transmission of the data in tx_skb, so now
  1030. we can release the skb memory */
  1031. dev_kfree_skb_irq(myFirstTxDesc->skb);
  1032. myFirstTxDesc->skb = 0;
  1033. myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
  1034. }
  1035. if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
  1036. /* acknowledge the eop interrupt and wake up queue */
  1037. *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
  1038. netif_wake_queue(dev);
  1039. }
  1040. /* Enable RX/TX IRQs again */
  1041. *R_IRQ_MASK2_SET =
  1042. IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
  1043. IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
  1044. return IRQ_HANDLED;
  1045. }
  1046. static irqreturn_t
  1047. e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1048. {
  1049. struct net_device *dev = (struct net_device *)dev_id;
  1050. struct net_local *np = (struct net_local *)dev->priv;
  1051. unsigned long irqbits = *R_IRQ_MASK0_RD;
  1052. /* check for underrun irq */
  1053. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
  1054. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  1055. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  1056. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
  1057. np->stats.tx_errors++;
  1058. D(printk("ethernet receiver underrun!\n"));
  1059. }
  1060. /* check for overrun irq */
  1061. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
  1062. update_rx_stats(&np->stats); /* this will ack the irq */
  1063. D(printk("ethernet receiver overrun!\n"));
  1064. }
  1065. /* check for excessive collision irq */
  1066. if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
  1067. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
  1068. *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
  1069. SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
  1070. *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
  1071. np->stats.tx_errors++;
  1072. D(printk("ethernet excessive collisions!\n"));
  1073. }
  1074. return IRQ_HANDLED;
  1075. }
  1076. /* We have a good packet(s), get it/them out of the buffers. */
  1077. static void
  1078. e100_rx(struct net_device *dev)
  1079. {
  1080. struct sk_buff *skb;
  1081. int length = 0;
  1082. struct net_local *np = (struct net_local *)dev->priv;
  1083. unsigned char *skb_data_ptr;
  1084. #ifdef ETHDEBUG
  1085. int i;
  1086. #endif
  1087. if (!led_active && time_after(jiffies, led_next_time)) {
  1088. /* light the network leds depending on the current speed. */
  1089. e100_set_network_leds(NETWORK_ACTIVITY);
  1090. /* Set the earliest time we may clear the LED */
  1091. led_next_time = jiffies + NET_FLASH_TIME;
  1092. led_active = 1;
  1093. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1094. }
  1095. length = myNextRxDesc->descr.hw_len - 4;
  1096. ((struct net_local *)dev->priv)->stats.rx_bytes += length;
  1097. #ifdef ETHDEBUG
  1098. printk("Got a packet of length %d:\n", length);
  1099. /* dump the first bytes in the packet */
  1100. skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
  1101. for (i = 0; i < 8; i++) {
  1102. printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
  1103. skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
  1104. skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
  1105. skb_data_ptr += 8;
  1106. }
  1107. #endif
  1108. if (length < RX_COPYBREAK) {
  1109. /* Small packet, copy data */
  1110. skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
  1111. if (!skb) {
  1112. np->stats.rx_errors++;
  1113. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  1114. return;
  1115. }
  1116. skb_put(skb, length - ETHER_HEAD_LEN); /* allocate room for the packet body */
  1117. skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
  1118. #ifdef ETHDEBUG
  1119. printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
  1120. skb->head, skb->data, skb->tail, skb->end);
  1121. printk("copying packet to 0x%x.\n", skb_data_ptr);
  1122. #endif
  1123. memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
  1124. }
  1125. else {
  1126. /* Large packet, send directly to upper layers and allocate new
  1127. * memory (aligned to cache line boundary to avoid bug).
  1128. * Before sending the skb to upper layers we must make sure that
  1129. * skb->data points to the aligned start of the packet.
  1130. */
  1131. int align;
  1132. struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
  1133. if (!new_skb) {
  1134. np->stats.rx_errors++;
  1135. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  1136. return;
  1137. }
  1138. skb = myNextRxDesc->skb;
  1139. align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
  1140. skb_put(skb, length + align);
  1141. skb_pull(skb, align); /* Remove alignment bytes */
  1142. myNextRxDesc->skb = new_skb;
  1143. myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
  1144. }
  1145. skb->dev = dev;
  1146. skb->protocol = eth_type_trans(skb, dev);
  1147. /* Send the packet to the upper layers */
  1148. netif_rx(skb);
  1149. /* Prepare for next packet */
  1150. myNextRxDesc->descr.status = 0;
  1151. myPrevRxDesc = myNextRxDesc;
  1152. myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
  1153. rx_queue_len++;
  1154. /* Check if descriptors should be returned */
  1155. if (rx_queue_len == RX_QUEUE_THRESHOLD) {
  1156. flush_etrax_cache();
  1157. myPrevRxDesc->descr.ctrl |= d_eol;
  1158. myLastRxDesc->descr.ctrl &= ~d_eol;
  1159. myLastRxDesc = myPrevRxDesc;
  1160. rx_queue_len = 0;
  1161. }
  1162. }
  1163. /* The inverse routine to net_open(). */
  1164. static int
  1165. e100_close(struct net_device *dev)
  1166. {
  1167. struct net_local *np = (struct net_local *)dev->priv;
  1168. printk(KERN_INFO "Closing %s.\n", dev->name);
  1169. netif_stop_queue(dev);
  1170. *R_IRQ_MASK0_CLR =
  1171. IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
  1172. IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
  1173. IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
  1174. *R_IRQ_MASK2_CLR =
  1175. IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
  1176. IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
  1177. IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
  1178. IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
  1179. /* Stop the receiver and the transmitter */
  1180. RESET_DMA(NETWORK_TX_DMA_NBR);
  1181. RESET_DMA(NETWORK_RX_DMA_NBR);
  1182. /* Flush the Tx and disable Rx here. */
  1183. free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
  1184. free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
  1185. free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
  1186. /* Update the statistics here. */
  1187. update_rx_stats(&np->stats);
  1188. update_tx_stats(&np->stats);
  1189. /* Stop speed/duplex timers */
  1190. del_timer(&speed_timer);
  1191. del_timer(&duplex_timer);
  1192. return 0;
  1193. }
  1194. static int
  1195. e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1196. {
  1197. struct mii_ioctl_data *data = if_mii(ifr);
  1198. struct net_local *np = netdev_priv(dev);
  1199. spin_lock(&np->lock); /* Preempt protection */
  1200. switch (cmd) {
  1201. case SIOCETHTOOL:
  1202. return e100_ethtool_ioctl(dev,ifr);
  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
  1239. e100_ethtool_ioctl(struct net_device *dev, struct ifreq *ifr)
  1240. {
  1241. struct ethtool_cmd ecmd;
  1242. if (copy_from_user(&ecmd, ifr->ifr_data, sizeof (ecmd)))
  1243. return -EFAULT;
  1244. switch (ecmd.cmd) {
  1245. case ETHTOOL_GSET:
  1246. {
  1247. memset((void *) &ecmd, 0, sizeof (ecmd));
  1248. ecmd.supported =
  1249. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
  1250. SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  1251. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
  1252. ecmd.port = PORT_TP;
  1253. ecmd.transceiver = XCVR_EXTERNAL;
  1254. ecmd.phy_address = mdio_phy_addr;
  1255. ecmd.speed = current_speed;
  1256. ecmd.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1257. ecmd.advertising = ADVERTISED_TP;
  1258. if (current_duplex == autoneg && current_speed_selection == 0)
  1259. ecmd.advertising |= ADVERTISED_Autoneg;
  1260. else {
  1261. ecmd.advertising |=
  1262. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  1263. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  1264. if (current_speed_selection == 10)
  1265. ecmd.advertising &= ~(ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full);
  1266. else if (current_speed_selection == 100)
  1267. ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full);
  1268. if (current_duplex == half)
  1269. ecmd.advertising &= ~(ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full);
  1270. else if (current_duplex == full)
  1271. ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_100baseT_Half);
  1272. }
  1273. ecmd.autoneg = AUTONEG_ENABLE;
  1274. if (copy_to_user(ifr->ifr_data, &ecmd, sizeof (ecmd)))
  1275. return -EFAULT;
  1276. }
  1277. break;
  1278. case ETHTOOL_SSET:
  1279. {
  1280. if (!capable(CAP_NET_ADMIN)) {
  1281. return -EPERM;
  1282. }
  1283. if (ecmd.autoneg == AUTONEG_ENABLE) {
  1284. e100_set_duplex(dev, autoneg);
  1285. e100_set_speed(dev, 0);
  1286. } else {
  1287. e100_set_duplex(dev, ecmd.duplex == DUPLEX_HALF ? half : full);
  1288. e100_set_speed(dev, ecmd.speed == SPEED_10 ? 10: 100);
  1289. }
  1290. }
  1291. break;
  1292. case ETHTOOL_GDRVINFO:
  1293. {
  1294. struct ethtool_drvinfo info;
  1295. memset((void *) &info, 0, sizeof (info));
  1296. strncpy(info.driver, "ETRAX 100LX", sizeof(info.driver) - 1);
  1297. strncpy(info.version, "$Revision: 1.31 $", sizeof(info.version) - 1);
  1298. strncpy(info.fw_version, "N/A", sizeof(info.fw_version) - 1);
  1299. strncpy(info.bus_info, "N/A", sizeof(info.bus_info) - 1);
  1300. info.regdump_len = 0;
  1301. info.eedump_len = 0;
  1302. info.testinfo_len = 0;
  1303. if (copy_to_user(ifr->ifr_data, &info, sizeof (info)))
  1304. return -EFAULT;
  1305. }
  1306. break;
  1307. case ETHTOOL_NWAY_RST:
  1308. if (current_duplex == autoneg && current_speed_selection == 0)
  1309. e100_negotiate(dev);
  1310. break;
  1311. default:
  1312. return -EOPNOTSUPP;
  1313. break;
  1314. }
  1315. return 0;
  1316. }
  1317. static int
  1318. e100_set_config(struct net_device *dev, struct ifmap *map)
  1319. {
  1320. struct net_local *np = (struct net_local *)dev->priv;
  1321. spin_lock(&np->lock); /* Preempt protection */
  1322. switch(map->port) {
  1323. case IF_PORT_UNKNOWN:
  1324. /* Use autoneg */
  1325. e100_set_speed(dev, 0);
  1326. e100_set_duplex(dev, autoneg);
  1327. break;
  1328. case IF_PORT_10BASET:
  1329. e100_set_speed(dev, 10);
  1330. e100_set_duplex(dev, autoneg);
  1331. break;
  1332. case IF_PORT_100BASET:
  1333. case IF_PORT_100BASETX:
  1334. e100_set_speed(dev, 100);
  1335. e100_set_duplex(dev, autoneg);
  1336. break;
  1337. case IF_PORT_100BASEFX:
  1338. case IF_PORT_10BASE2:
  1339. case IF_PORT_AUI:
  1340. spin_unlock(&np->lock);
  1341. return -EOPNOTSUPP;
  1342. break;
  1343. default:
  1344. printk(KERN_ERR "%s: Invalid media selected", dev->name);
  1345. spin_unlock(&np->lock);
  1346. return -EINVAL;
  1347. }
  1348. spin_unlock(&np->lock);
  1349. return 0;
  1350. }
  1351. static void
  1352. update_rx_stats(struct net_device_stats *es)
  1353. {
  1354. unsigned long r = *R_REC_COUNTERS;
  1355. /* update stats relevant to reception errors */
  1356. es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
  1357. es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
  1358. es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
  1359. es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
  1360. }
  1361. static void
  1362. update_tx_stats(struct net_device_stats *es)
  1363. {
  1364. unsigned long r = *R_TR_COUNTERS;
  1365. /* update stats relevant to transmission errors */
  1366. es->collisions +=
  1367. IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
  1368. IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
  1369. es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
  1370. }
  1371. /*
  1372. * Get the current statistics.
  1373. * This may be called with the card open or closed.
  1374. */
  1375. static struct net_device_stats *
  1376. e100_get_stats(struct net_device *dev)
  1377. {
  1378. struct net_local *lp = (struct net_local *)dev->priv;
  1379. unsigned long flags;
  1380. spin_lock_irqsave(&lp->lock, flags);
  1381. update_rx_stats(&lp->stats);
  1382. update_tx_stats(&lp->stats);
  1383. spin_unlock_irqrestore(&lp->lock, flags);
  1384. return &lp->stats;
  1385. }
  1386. /*
  1387. * Set or clear the multicast filter for this adaptor.
  1388. * num_addrs == -1 Promiscuous mode, receive all packets
  1389. * num_addrs == 0 Normal mode, clear multicast list
  1390. * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1391. * and do best-effort filtering.
  1392. */
  1393. static void
  1394. set_multicast_list(struct net_device *dev)
  1395. {
  1396. struct net_local *lp = (struct net_local *)dev->priv;
  1397. int num_addr = dev->mc_count;
  1398. unsigned long int lo_bits;
  1399. unsigned long int hi_bits;
  1400. spin_lock(&lp->lock);
  1401. if (dev->flags & IFF_PROMISC)
  1402. {
  1403. /* promiscuous mode */
  1404. lo_bits = 0xfffffffful;
  1405. hi_bits = 0xfffffffful;
  1406. /* Enable individual receive */
  1407. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
  1408. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1409. } else if (dev->flags & IFF_ALLMULTI) {
  1410. /* enable all multicasts */
  1411. lo_bits = 0xfffffffful;
  1412. hi_bits = 0xfffffffful;
  1413. /* Disable individual receive */
  1414. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1415. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1416. } else if (num_addr == 0) {
  1417. /* Normal, clear the mc list */
  1418. lo_bits = 0x00000000ul;
  1419. hi_bits = 0x00000000ul;
  1420. /* Disable individual receive */
  1421. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1422. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1423. } else {
  1424. /* MC mode, receive normal and MC packets */
  1425. char hash_ix;
  1426. struct dev_mc_list *dmi = dev->mc_list;
  1427. int i;
  1428. char *baddr;
  1429. lo_bits = 0x00000000ul;
  1430. hi_bits = 0x00000000ul;
  1431. for (i=0; i<num_addr; i++) {
  1432. /* Calculate the hash index for the GA registers */
  1433. hash_ix = 0;
  1434. baddr = dmi->dmi_addr;
  1435. hash_ix ^= (*baddr) & 0x3f;
  1436. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1437. ++baddr;
  1438. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1439. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1440. ++baddr;
  1441. hash_ix ^= ((*baddr) << 4) & 0x30;
  1442. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1443. ++baddr;
  1444. hash_ix ^= (*baddr) & 0x3f;
  1445. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1446. ++baddr;
  1447. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1448. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1449. ++baddr;
  1450. hash_ix ^= ((*baddr) << 4) & 0x30;
  1451. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1452. hash_ix &= 0x3f;
  1453. if (hash_ix >= 32) {
  1454. hi_bits |= (1 << (hash_ix-32));
  1455. }
  1456. else {
  1457. lo_bits |= (1 << hash_ix);
  1458. }
  1459. dmi = dmi->next;
  1460. }
  1461. /* Disable individual receive */
  1462. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1463. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1464. }
  1465. *R_NETWORK_GA_0 = lo_bits;
  1466. *R_NETWORK_GA_1 = hi_bits;
  1467. spin_unlock(&lp->lock);
  1468. }
  1469. void
  1470. e100_hardware_send_packet(char *buf, int length)
  1471. {
  1472. D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
  1473. if (!led_active && time_after(jiffies, led_next_time)) {
  1474. /* light the network leds depending on the current speed. */
  1475. e100_set_network_leds(NETWORK_ACTIVITY);
  1476. /* Set the earliest time we may clear the LED */
  1477. led_next_time = jiffies + NET_FLASH_TIME;
  1478. led_active = 1;
  1479. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1480. }
  1481. /* configure the tx dma descriptor */
  1482. myNextTxDesc->descr.sw_len = length;
  1483. myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
  1484. myNextTxDesc->descr.buf = virt_to_phys(buf);
  1485. /* Move end of list */
  1486. myLastTxDesc->descr.ctrl &= ~d_eol;
  1487. myLastTxDesc = myNextTxDesc;
  1488. /* Restart DMA channel */
  1489. *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
  1490. }
  1491. static void
  1492. e100_clear_network_leds(unsigned long dummy)
  1493. {
  1494. if (led_active && time_after(jiffies, led_next_time)) {
  1495. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  1496. /* Set the earliest time we may set the LED */
  1497. led_next_time = jiffies + NET_FLASH_PAUSE;
  1498. led_active = 0;
  1499. }
  1500. }
  1501. static void
  1502. e100_set_network_leds(int active)
  1503. {
  1504. #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
  1505. int light_leds = (active == NO_NETWORK_ACTIVITY);
  1506. #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
  1507. int light_leds = (active == NETWORK_ACTIVITY);
  1508. #else
  1509. #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
  1510. #endif
  1511. if (!current_speed) {
  1512. /* Make LED red, link is down */
  1513. #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
  1514. LED_NETWORK_SET(LED_RED);
  1515. #else
  1516. LED_NETWORK_SET(LED_OFF);
  1517. #endif
  1518. }
  1519. else if (light_leds) {
  1520. if (current_speed == 10) {
  1521. LED_NETWORK_SET(LED_ORANGE);
  1522. } else {
  1523. LED_NETWORK_SET(LED_GREEN);
  1524. }
  1525. }
  1526. else {
  1527. LED_NETWORK_SET(LED_OFF);
  1528. }
  1529. }
  1530. static int
  1531. etrax_init_module(void)
  1532. {
  1533. return etrax_ethernet_init();
  1534. }
  1535. static int __init
  1536. e100_boot_setup(char* str)
  1537. {
  1538. struct sockaddr sa = {0};
  1539. int i;
  1540. /* Parse the colon separated Ethernet station address */
  1541. for (i = 0; i < ETH_ALEN; i++) {
  1542. unsigned int tmp;
  1543. if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
  1544. printk(KERN_WARNING "Malformed station address");
  1545. return 0;
  1546. }
  1547. sa.sa_data[i] = (char)tmp;
  1548. }
  1549. default_mac = sa;
  1550. return 1;
  1551. }
  1552. __setup("etrax100_eth=", e100_boot_setup);
  1553. module_init(etrax_init_module);