eth_v10.c 52 KB

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