eth_v10.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /* $Id: ethernet.c,v 1.31 2004/10/18 14:49:03 starvik Exp $
  2. *
  3. * e100net.c: A network driver for the ETRAX 100LX network controller.
  4. *
  5. * Copyright (c) 1998-2002 Axis Communications AB.
  6. *
  7. * The outline of this driver comes from skeleton.c.
  8. *
  9. * $Log: ethernet.c,v $
  10. * Revision 1.31 2004/10/18 14:49:03 starvik
  11. * Use RX interrupt as random source
  12. *
  13. * Revision 1.30 2004/09/29 10:44:04 starvik
  14. * Enabed MAC-address output again
  15. *
  16. * Revision 1.29 2004/08/24 07:14:05 starvik
  17. * Make use of generic MDIO interface and constants.
  18. *
  19. * Revision 1.28 2004/08/20 09:37:11 starvik
  20. * Added support for Intel LXT972A. Creds to Randy Scarborough.
  21. *
  22. * Revision 1.27 2004/08/16 12:37:22 starvik
  23. * Merge of Linux 2.6.8
  24. *
  25. * Revision 1.25 2004/06/21 10:29:57 starvik
  26. * Merge of Linux 2.6.7
  27. *
  28. * Revision 1.23 2004/06/09 05:29:22 starvik
  29. * Avoid any race where R_DMA_CH1_FIRST is NULL (may trigger cache bug).
  30. *
  31. * Revision 1.22 2004/05/14 07:58:03 starvik
  32. * Merge of changes from 2.4
  33. *
  34. * Revision 1.20 2004/03/11 11:38:40 starvik
  35. * Merge of Linux 2.6.4
  36. *
  37. * Revision 1.18 2003/12/03 13:45:46 starvik
  38. * Use hardware pad for short packets to prevent information leakage.
  39. *
  40. * Revision 1.17 2003/07/04 08:27:37 starvik
  41. * Merge of Linux 2.5.74
  42. *
  43. * Revision 1.16 2003/04/24 08:28:22 starvik
  44. * New LED behaviour: LED off when no link
  45. *
  46. * Revision 1.15 2003/04/09 05:20:47 starvik
  47. * Merge of Linux 2.5.67
  48. *
  49. * Revision 1.13 2003/03/06 16:11:01 henriken
  50. * Off by one error in group address register setting.
  51. *
  52. * Revision 1.12 2003/02/27 17:24:19 starvik
  53. * Corrected Rev to Revision
  54. *
  55. * Revision 1.11 2003/01/24 09:53:21 starvik
  56. * Oops. Initialize GA to 0, not to 1
  57. *
  58. * Revision 1.10 2003/01/24 09:50:55 starvik
  59. * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
  60. *
  61. * Revision 1.9 2002/12/13 07:40:58 starvik
  62. * Added basic ethtool interface
  63. * Handled out of memory when allocating new buffers
  64. *
  65. * Revision 1.8 2002/12/11 13:13:57 starvik
  66. * Added arch/ to v10 specific includes
  67. * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
  68. *
  69. * Revision 1.7 2002/11/26 09:41:42 starvik
  70. * Added e100_set_config (standard interface to set media type)
  71. * Added protection against preemptive scheduling
  72. * Added standard MII ioctls
  73. *
  74. * Revision 1.6 2002/11/21 07:18:18 starvik
  75. * Timers must be initialized in 2.5.48
  76. *
  77. * Revision 1.5 2002/11/20 11:56:11 starvik
  78. * Merge of Linux 2.5.48
  79. *
  80. * Revision 1.4 2002/11/18 07:26:46 starvik
  81. * Linux 2.5 port of latest Linux 2.4 ethernet driver
  82. *
  83. * Revision 1.33 2002/10/02 20:16:17 hp
  84. * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
  85. *
  86. * Revision 1.32 2002/09/16 06:05:58 starvik
  87. * Align memory returned by dev_alloc_skb
  88. * Moved handling of sent packets to interrupt to avoid reference counting problem
  89. *
  90. * Revision 1.31 2002/09/10 13:28:23 larsv
  91. * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
  92. * for supported functionality by issuing special ioctls, i.e. wireless
  93. * extensions.
  94. *
  95. * Revision 1.30 2002/05/07 18:50:08 johana
  96. * Correct spelling in comments.
  97. *
  98. * Revision 1.29 2002/05/06 05:38:49 starvik
  99. * Performance improvements:
  100. * Large packets are not copied (breakpoint set to 256 bytes)
  101. * The cache bug workaround is delayed until half of the receive list
  102. * has been used
  103. * Added transmit list
  104. * Transmit interrupts are only enabled when transmit queue is full
  105. *
  106. * Revision 1.28.2.1 2002/04/30 08:15:51 starvik
  107. * Performance improvements:
  108. * Large packets are not copied (breakpoint set to 256 bytes)
  109. * The cache bug workaround is delayed until half of the receive list
  110. * has been used.
  111. * Added transmit list
  112. * Transmit interrupts are only enabled when transmit queue is full
  113. *
  114. * Revision 1.28 2002/04/22 11:47:21 johana
  115. * Fix according to 2.4.19-pre7. time_after/time_before and
  116. * missing end of comment.
  117. * The patch has a typo for ethernet.c in e100_clear_network_leds(),
  118. * that is fixed here.
  119. *
  120. * Revision 1.27 2002/04/12 11:55:11 bjornw
  121. * Added TODO
  122. *
  123. * Revision 1.26 2002/03/15 17:11:02 bjornw
  124. * Use prepare_rx_descriptor after the CPU has touched the receiving descs
  125. *
  126. * Revision 1.25 2002/03/08 13:07:53 bjornw
  127. * Unnecessary spinlock removed
  128. *
  129. * Revision 1.24 2002/02/20 12:57:43 fredriks
  130. * Replaced MIN() with min().
  131. *
  132. * Revision 1.23 2002/02/20 10:58:14 fredriks
  133. * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
  134. *
  135. * Revision 1.22 2002/01/30 07:48:22 matsfg
  136. * Initiate R_NETWORK_TR_CTRL
  137. *
  138. * Revision 1.21 2001/11/23 11:54:49 starvik
  139. * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
  140. * Removed compiler warnings
  141. *
  142. * Revision 1.20 2001/11/12 19:26:00 pkj
  143. * * Corrected e100_negotiate() to not assign half to current_duplex when
  144. * it was supposed to compare them...
  145. * * Cleaned up failure handling in e100_open().
  146. * * Fixed compiler warnings.
  147. *
  148. * Revision 1.19 2001/11/09 07:43:09 starvik
  149. * Added full duplex support
  150. * Added ioctl to set speed and duplex
  151. * Clear LED timer only runs when LED is lit
  152. *
  153. * Revision 1.18 2001/10/03 14:40:43 jonashg
  154. * Update rx_bytes counter.
  155. *
  156. * Revision 1.17 2001/06/11 12:43:46 olof
  157. * Modified defines for network LED behavior
  158. *
  159. * Revision 1.16 2001/05/30 06:12:46 markusl
  160. * TxDesc.next should not be set to NULL
  161. *
  162. * Revision 1.15 2001/05/29 10:27:04 markusl
  163. * Updated after review remarks:
  164. * +Use IO_EXTRACT
  165. * +Handle underrun
  166. *
  167. * Revision 1.14 2001/05/29 09:20:14 jonashg
  168. * Use driver name on printk output so one can tell which driver that complains.
  169. *
  170. * Revision 1.13 2001/05/09 12:35:59 johana
  171. * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
  172. *
  173. * Revision 1.12 2001/04/05 11:43:11 tobiasa
  174. * Check dev before panic.
  175. *
  176. * Revision 1.11 2001/04/04 11:21:05 markusl
  177. * Updated according to review remarks
  178. *
  179. * Revision 1.10 2001/03/26 16:03:06 bjornw
  180. * Needs linux/config.h
  181. *
  182. * Revision 1.9 2001/03/19 14:47:48 pkj
  183. * * Make sure there is always a pause after the network LEDs are
  184. * changed so they will not look constantly lit during heavy traffic.
  185. * * Always use HZ when setting times relative to jiffies.
  186. * * Use LED_NETWORK_SET() when setting the network LEDs.
  187. *
  188. * Revision 1.8 2001/02/27 13:52:48 bjornw
  189. * malloc.h -> slab.h
  190. *
  191. * Revision 1.7 2001/02/23 13:46:38 bjornw
  192. * Spellling check
  193. *
  194. * Revision 1.6 2001/01/26 15:21:04 starvik
  195. * Don't disable interrupts while reading MDIO registers (MDIO is slow)
  196. * Corrected promiscuous mode
  197. * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
  198. *
  199. * Revision 1.5 2000/11/29 17:22:22 bjornw
  200. * Get rid of the udword types legacy stuff
  201. *
  202. * Revision 1.4 2000/11/22 16:36:09 bjornw
  203. * Please marketing by using the correct case when spelling Etrax.
  204. *
  205. * Revision 1.3 2000/11/21 16:43:04 bjornw
  206. * Minor short->int change
  207. *
  208. * Revision 1.2 2000/11/08 14:27:57 bjornw
  209. * 2.4 port
  210. *
  211. * Revision 1.1 2000/11/06 13:56:00 bjornw
  212. * Verbatim copy of the 1.24 version of e100net.c from elinux
  213. *
  214. * Revision 1.24 2000/10/04 15:55:23 bjornw
  215. * * Use virt_to_phys etc. for DMA addresses
  216. * * Removed bogus CHECKSUM_UNNECESSARY
  217. *
  218. *
  219. */
  220. #include <linux/module.h>
  221. #include <linux/kernel.h>
  222. #include <linux/sched.h>
  223. #include <linux/delay.h>
  224. #include <linux/types.h>
  225. #include <linux/fcntl.h>
  226. #include <linux/interrupt.h>
  227. #include <linux/ptrace.h>
  228. #include <linux/ioport.h>
  229. #include <linux/in.h>
  230. #include <linux/slab.h>
  231. #include <linux/string.h>
  232. #include <linux/spinlock.h>
  233. #include <linux/errno.h>
  234. #include <linux/init.h>
  235. #include <linux/if.h>
  236. #include <linux/mii.h>
  237. #include <linux/netdevice.h>
  238. #include <linux/etherdevice.h>
  239. #include <linux/skbuff.h>
  240. #include <linux/ethtool.h>
  241. #include <asm/arch/svinto.h>/* DMA and register descriptions */
  242. #include <asm/io.h> /* LED_* I/O functions */
  243. #include <asm/irq.h>
  244. #include <asm/dma.h>
  245. #include <asm/system.h>
  246. #include <asm/bitops.h>
  247. #include <asm/ethernet.h>
  248. #include <asm/cache.h>
  249. //#define ETHDEBUG
  250. #define D(x)
  251. /*
  252. * The name of the card. Is used for messages and in the requests for
  253. * io regions, irqs and dma channels
  254. */
  255. static const char* cardname = "ETRAX 100LX built-in ethernet controller";
  256. /* A default ethernet address. Highlevel SW will set the real one later */
  257. static struct sockaddr default_mac = {
  258. 0,
  259. { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
  260. };
  261. /* Information that need to be kept for each board. */
  262. struct net_local {
  263. struct net_device_stats stats;
  264. struct mii_if_info mii_if;
  265. /* Tx control lock. This protects the transmit buffer ring
  266. * state along with the "tx full" state of the driver. This
  267. * means all netif_queue flow control actions are protected
  268. * by this lock as well.
  269. */
  270. spinlock_t lock;
  271. };
  272. typedef struct etrax_eth_descr
  273. {
  274. etrax_dma_descr descr;
  275. struct sk_buff* skb;
  276. } etrax_eth_descr;
  277. /* Some transceivers requires special handling */
  278. struct transceiver_ops
  279. {
  280. unsigned int oui;
  281. void (*check_speed)(struct net_device* dev);
  282. void (*check_duplex)(struct net_device* dev);
  283. };
  284. struct transceiver_ops* transceiver;
  285. /* Duplex settings */
  286. enum duplex
  287. {
  288. half,
  289. full,
  290. autoneg
  291. };
  292. /* Dma descriptors etc. */
  293. #define MAX_MEDIA_DATA_SIZE 1518
  294. #define MIN_PACKET_LEN 46
  295. #define ETHER_HEAD_LEN 14
  296. /*
  297. ** MDIO constants.
  298. */
  299. #define MDIO_START 0x1
  300. #define MDIO_READ 0x2
  301. #define MDIO_WRITE 0x1
  302. #define MDIO_PREAMBLE 0xfffffffful
  303. /* Broadcom specific */
  304. #define MDIO_AUX_CTRL_STATUS_REG 0x18
  305. #define MDIO_BC_FULL_DUPLEX_IND 0x1
  306. #define MDIO_BC_SPEED 0x2
  307. /* TDK specific */
  308. #define MDIO_TDK_DIAGNOSTIC_REG 18
  309. #define MDIO_TDK_DIAGNOSTIC_RATE 0x400
  310. #define MDIO_TDK_DIAGNOSTIC_DPLX 0x800
  311. /*Intel LXT972A specific*/
  312. #define MDIO_INT_STATUS_REG_2 0x0011
  313. #define MDIO_INT_FULL_DUPLEX_IND ( 1 << 9 )
  314. #define MDIO_INT_SPEED ( 1 << 14 )
  315. /* Network flash constants */
  316. #define NET_FLASH_TIME (HZ/50) /* 20 ms */
  317. #define NET_FLASH_PAUSE (HZ/100) /* 10 ms */
  318. #define NET_LINK_UP_CHECK_INTERVAL (2*HZ) /* 2 s */
  319. #define NET_DUPLEX_CHECK_INTERVAL (2*HZ) /* 2 s */
  320. #define NO_NETWORK_ACTIVITY 0
  321. #define NETWORK_ACTIVITY 1
  322. #define NBR_OF_RX_DESC 64
  323. #define NBR_OF_TX_DESC 256
  324. /* Large packets are sent directly to upper layers while small packets are */
  325. /* copied (to reduce memory waste). The following constant decides the breakpoint */
  326. #define RX_COPYBREAK 256
  327. /* Due to a chip bug we need to flush the cache when descriptors are returned */
  328. /* to the DMA. To decrease performance impact we return descriptors in chunks. */
  329. /* The following constant determines the number of descriptors to return. */
  330. #define RX_QUEUE_THRESHOLD NBR_OF_RX_DESC/2
  331. #define GET_BIT(bit,val) (((val) >> (bit)) & 0x01)
  332. /* Define some macros to access ETRAX 100 registers */
  333. #define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  334. IO_FIELD_(reg##_, field##_, val)
  335. #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  336. IO_STATE_(reg##_, field##_, _##val)
  337. static etrax_eth_descr *myNextRxDesc; /* Points to the next descriptor to
  338. to be processed */
  339. static etrax_eth_descr *myLastRxDesc; /* The last processed descriptor */
  340. static etrax_eth_descr *myPrevRxDesc; /* The descriptor right before myNextRxDesc */
  341. static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
  342. static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
  343. static etrax_eth_descr* myLastTxDesc; /* End of send queue */
  344. static etrax_eth_descr* myNextTxDesc; /* Next descriptor to use */
  345. static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
  346. static unsigned int network_rec_config_shadow = 0;
  347. static unsigned int mdio_phy_addr; /* Transciever address */
  348. static unsigned int network_tr_ctrl_shadow = 0;
  349. /* Network speed indication. */
  350. static DEFINE_TIMER(speed_timer, NULL, 0, 0);
  351. static DEFINE_TIMER(clear_led_timer, NULL, 0, 0);
  352. static int current_speed; /* Speed read from transceiver */
  353. static int current_speed_selection; /* Speed selected by user */
  354. static unsigned long led_next_time;
  355. static int led_active;
  356. static int rx_queue_len;
  357. /* Duplex */
  358. static DEFINE_TIMER(duplex_timer, NULL, 0, 0);
  359. static int full_duplex;
  360. static enum duplex current_duplex;
  361. /* Index to functions, as function prototypes. */
  362. static int etrax_ethernet_init(void);
  363. static int e100_open(struct net_device *dev);
  364. static int e100_set_mac_address(struct net_device *dev, void *addr);
  365. static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
  366. static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id);
  367. static irqreturn_t e100nw_interrupt(int irq, void *dev_id);
  368. static void e100_rx(struct net_device *dev);
  369. static int e100_close(struct net_device *dev);
  370. static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  371. static int e100_set_config(struct net_device* dev, struct ifmap* map);
  372. static void e100_tx_timeout(struct net_device *dev);
  373. static struct net_device_stats *e100_get_stats(struct net_device *dev);
  374. static void set_multicast_list(struct net_device *dev);
  375. static void e100_hardware_send_packet(char *buf, int length);
  376. static void update_rx_stats(struct net_device_stats *);
  377. static void update_tx_stats(struct net_device_stats *);
  378. static int e100_probe_transceiver(struct net_device* dev);
  379. static void e100_check_speed(unsigned long priv);
  380. static void e100_set_speed(struct net_device* dev, unsigned long speed);
  381. static void e100_check_duplex(unsigned long priv);
  382. static void e100_set_duplex(struct net_device* dev, enum duplex);
  383. static void e100_negotiate(struct net_device* dev);
  384. static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
  385. static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);
  386. static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
  387. static void e100_send_mdio_bit(unsigned char bit);
  388. static unsigned char e100_receive_mdio_bit(void);
  389. static void e100_reset_transceiver(struct net_device* net);
  390. static void e100_clear_network_leds(unsigned long dummy);
  391. static void e100_set_network_leds(int active);
  392. static const struct ethtool_ops e100_ethtool_ops;
  393. static void broadcom_check_speed(struct net_device* dev);
  394. static void broadcom_check_duplex(struct net_device* dev);
  395. static void tdk_check_speed(struct net_device* dev);
  396. static void tdk_check_duplex(struct net_device* dev);
  397. static void intel_check_speed(struct net_device* dev);
  398. static void intel_check_duplex(struct net_device* dev);
  399. static void generic_check_speed(struct net_device* dev);
  400. static void generic_check_duplex(struct net_device* dev);
  401. struct transceiver_ops transceivers[] =
  402. {
  403. {0x1018, broadcom_check_speed, broadcom_check_duplex}, /* Broadcom */
  404. {0xC039, tdk_check_speed, tdk_check_duplex}, /* TDK 2120 */
  405. {0x039C, tdk_check_speed, tdk_check_duplex}, /* TDK 2120C */
  406. {0x04de, intel_check_speed, intel_check_duplex}, /* Intel LXT972A*/
  407. {0x0000, generic_check_speed, generic_check_duplex} /* Generic, must be last */
  408. };
  409. #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
  410. /*
  411. * Check for a network adaptor of this type, and return '0' if one exists.
  412. * If dev->base_addr == 0, probe all likely locations.
  413. * If dev->base_addr == 1, always return failure.
  414. * If dev->base_addr == 2, allocate space for the device and return success
  415. * (detachable devices only).
  416. */
  417. static int __init
  418. etrax_ethernet_init(void)
  419. {
  420. struct net_device *dev;
  421. struct net_local* np;
  422. int i, err;
  423. printk(KERN_INFO
  424. "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2003 Axis Communications AB\n");
  425. dev = alloc_etherdev(sizeof(struct net_local));
  426. np = dev->priv;
  427. if (!dev)
  428. return -ENOMEM;
  429. dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
  430. /* now setup our etrax specific stuff */
  431. dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
  432. dev->dma = NETWORK_RX_DMA_NBR;
  433. /* fill in our handlers so the network layer can talk to us in the future */
  434. dev->open = e100_open;
  435. dev->hard_start_xmit = e100_send_packet;
  436. dev->stop = e100_close;
  437. dev->get_stats = e100_get_stats;
  438. dev->set_multicast_list = set_multicast_list;
  439. dev->set_mac_address = e100_set_mac_address;
  440. dev->ethtool_ops = &e100_ethtool_ops;
  441. dev->do_ioctl = e100_ioctl;
  442. dev->set_config = e100_set_config;
  443. dev->tx_timeout = e100_tx_timeout;
  444. /* Initialise the list of Etrax DMA-descriptors */
  445. /* Initialise receive descriptors */
  446. for (i = 0; i < NBR_OF_RX_DESC; i++) {
  447. /* Allocate two extra cachelines to make sure that buffer used by DMA
  448. * does not share cacheline with any other data (to avoid cache bug)
  449. */
  450. RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
  451. 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. IRQF_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)
  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)
  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 SIOCGMIIPHY: /* Get PHY address */
  1202. data->phy_id = mdio_phy_addr;
  1203. break;
  1204. case SIOCGMIIREG: /* Read MII register */
  1205. data->val_out = e100_get_mdio_reg(dev, mdio_phy_addr, data->reg_num);
  1206. break;
  1207. case SIOCSMIIREG: /* Write MII register */
  1208. e100_set_mdio_reg(dev, mdio_phy_addr, data->reg_num, data->val_in);
  1209. break;
  1210. /* The ioctls below should be considered obsolete but are */
  1211. /* still present for compatability with old scripts/apps */
  1212. case SET_ETH_SPEED_10: /* 10 Mbps */
  1213. e100_set_speed(dev, 10);
  1214. break;
  1215. case SET_ETH_SPEED_100: /* 100 Mbps */
  1216. e100_set_speed(dev, 100);
  1217. break;
  1218. case SET_ETH_SPEED_AUTO: /* Auto negotiate speed */
  1219. e100_set_speed(dev, 0);
  1220. break;
  1221. case SET_ETH_DUPLEX_HALF: /* Half duplex. */
  1222. e100_set_duplex(dev, half);
  1223. break;
  1224. case SET_ETH_DUPLEX_FULL: /* Full duplex. */
  1225. e100_set_duplex(dev, full);
  1226. break;
  1227. case SET_ETH_DUPLEX_AUTO: /* Autonegotiate duplex*/
  1228. e100_set_duplex(dev, autoneg);
  1229. break;
  1230. default:
  1231. return -EINVAL;
  1232. }
  1233. spin_unlock(&np->lock);
  1234. return 0;
  1235. }
  1236. static int e100_set_settings(struct net_device *dev,
  1237. struct ethtool_cmd *ecmd)
  1238. {
  1239. ecmd->supported = SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
  1240. SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  1241. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
  1242. ecmd->port = PORT_TP;
  1243. ecmd->transceiver = XCVR_EXTERNAL;
  1244. ecmd->phy_address = mdio_phy_addr;
  1245. ecmd->speed = current_speed;
  1246. ecmd->duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1247. ecmd->advertising = ADVERTISED_TP;
  1248. if (current_duplex == autoneg && current_speed_selection == 0)
  1249. ecmd->advertising |= ADVERTISED_Autoneg;
  1250. else {
  1251. ecmd->advertising |=
  1252. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  1253. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  1254. if (current_speed_selection == 10)
  1255. ecmd->advertising &= ~(ADVERTISED_100baseT_Half |
  1256. ADVERTISED_100baseT_Full);
  1257. else if (current_speed_selection == 100)
  1258. ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
  1259. ADVERTISED_10baseT_Full);
  1260. if (current_duplex == half)
  1261. ecmd->advertising &= ~(ADVERTISED_10baseT_Full |
  1262. ADVERTISED_100baseT_Full);
  1263. else if (current_duplex == full)
  1264. ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
  1265. ADVERTISED_100baseT_Half);
  1266. }
  1267. ecmd->autoneg = AUTONEG_ENABLE;
  1268. return 0;
  1269. }
  1270. static int e100_set_settings(struct net_device *dev,
  1271. struct ethtool_cmd *ecmd)
  1272. {
  1273. if (ecmd->autoneg == AUTONEG_ENABLE) {
  1274. e100_set_duplex(dev, autoneg);
  1275. e100_set_speed(dev, 0);
  1276. } else {
  1277. e100_set_duplex(dev, ecmd->duplex == DUPLEX_HALF ? half : full);
  1278. e100_set_speed(dev, ecmd->speed == SPEED_10 ? 10: 100);
  1279. }
  1280. return 0;
  1281. }
  1282. static void e100_get_drvinfo(struct net_device *dev,
  1283. struct ethtool_drvinfo *info)
  1284. {
  1285. strncpy(info->driver, "ETRAX 100LX", sizeof(info->driver) - 1);
  1286. strncpy(info->version, "$Revision: 1.31 $", sizeof(info->version) - 1);
  1287. strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
  1288. strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
  1289. }
  1290. static int e100_nway_reset(struct net_device *dev)
  1291. {
  1292. if (current_duplex == autoneg && current_speed_selection == 0)
  1293. e100_negotiate(dev);
  1294. return 0;
  1295. }
  1296. static const struct ethtool_ops e100_ethtool_ops = {
  1297. .get_settings = e100_get_settings,
  1298. .set_settings = e100_set_settings,
  1299. .get_drvinfo = e100_get_drvinfo,
  1300. .nway_reset = e100_nway_reset,
  1301. .get_link = ethtool_op_get_link,
  1302. };
  1303. static int
  1304. e100_set_config(struct net_device *dev, struct ifmap *map)
  1305. {
  1306. struct net_local *np = (struct net_local *)dev->priv;
  1307. spin_lock(&np->lock); /* Preempt protection */
  1308. switch(map->port) {
  1309. case IF_PORT_UNKNOWN:
  1310. /* Use autoneg */
  1311. e100_set_speed(dev, 0);
  1312. e100_set_duplex(dev, autoneg);
  1313. break;
  1314. case IF_PORT_10BASET:
  1315. e100_set_speed(dev, 10);
  1316. e100_set_duplex(dev, autoneg);
  1317. break;
  1318. case IF_PORT_100BASET:
  1319. case IF_PORT_100BASETX:
  1320. e100_set_speed(dev, 100);
  1321. e100_set_duplex(dev, autoneg);
  1322. break;
  1323. case IF_PORT_100BASEFX:
  1324. case IF_PORT_10BASE2:
  1325. case IF_PORT_AUI:
  1326. spin_unlock(&np->lock);
  1327. return -EOPNOTSUPP;
  1328. break;
  1329. default:
  1330. printk(KERN_ERR "%s: Invalid media selected", dev->name);
  1331. spin_unlock(&np->lock);
  1332. return -EINVAL;
  1333. }
  1334. spin_unlock(&np->lock);
  1335. return 0;
  1336. }
  1337. static void
  1338. update_rx_stats(struct net_device_stats *es)
  1339. {
  1340. unsigned long r = *R_REC_COUNTERS;
  1341. /* update stats relevant to reception errors */
  1342. es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
  1343. es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
  1344. es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
  1345. es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
  1346. }
  1347. static void
  1348. update_tx_stats(struct net_device_stats *es)
  1349. {
  1350. unsigned long r = *R_TR_COUNTERS;
  1351. /* update stats relevant to transmission errors */
  1352. es->collisions +=
  1353. IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
  1354. IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
  1355. es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
  1356. }
  1357. /*
  1358. * Get the current statistics.
  1359. * This may be called with the card open or closed.
  1360. */
  1361. static struct net_device_stats *
  1362. e100_get_stats(struct net_device *dev)
  1363. {
  1364. struct net_local *lp = (struct net_local *)dev->priv;
  1365. unsigned long flags;
  1366. spin_lock_irqsave(&lp->lock, flags);
  1367. update_rx_stats(&lp->stats);
  1368. update_tx_stats(&lp->stats);
  1369. spin_unlock_irqrestore(&lp->lock, flags);
  1370. return &lp->stats;
  1371. }
  1372. /*
  1373. * Set or clear the multicast filter for this adaptor.
  1374. * num_addrs == -1 Promiscuous mode, receive all packets
  1375. * num_addrs == 0 Normal mode, clear multicast list
  1376. * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1377. * and do best-effort filtering.
  1378. */
  1379. static void
  1380. set_multicast_list(struct net_device *dev)
  1381. {
  1382. struct net_local *lp = (struct net_local *)dev->priv;
  1383. int num_addr = dev->mc_count;
  1384. unsigned long int lo_bits;
  1385. unsigned long int hi_bits;
  1386. spin_lock(&lp->lock);
  1387. if (dev->flags & IFF_PROMISC)
  1388. {
  1389. /* promiscuous mode */
  1390. lo_bits = 0xfffffffful;
  1391. hi_bits = 0xfffffffful;
  1392. /* Enable individual receive */
  1393. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
  1394. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1395. } else if (dev->flags & IFF_ALLMULTI) {
  1396. /* enable all multicasts */
  1397. lo_bits = 0xfffffffful;
  1398. hi_bits = 0xfffffffful;
  1399. /* Disable individual receive */
  1400. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1401. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1402. } else if (num_addr == 0) {
  1403. /* Normal, clear the mc list */
  1404. lo_bits = 0x00000000ul;
  1405. hi_bits = 0x00000000ul;
  1406. /* Disable individual receive */
  1407. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1408. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1409. } else {
  1410. /* MC mode, receive normal and MC packets */
  1411. char hash_ix;
  1412. struct dev_mc_list *dmi = dev->mc_list;
  1413. int i;
  1414. char *baddr;
  1415. lo_bits = 0x00000000ul;
  1416. hi_bits = 0x00000000ul;
  1417. for (i=0; i<num_addr; i++) {
  1418. /* Calculate the hash index for the GA registers */
  1419. hash_ix = 0;
  1420. baddr = dmi->dmi_addr;
  1421. hash_ix ^= (*baddr) & 0x3f;
  1422. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1423. ++baddr;
  1424. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1425. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1426. ++baddr;
  1427. hash_ix ^= ((*baddr) << 4) & 0x30;
  1428. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1429. ++baddr;
  1430. hash_ix ^= (*baddr) & 0x3f;
  1431. hash_ix ^= ((*baddr) >> 6) & 0x03;
  1432. ++baddr;
  1433. hash_ix ^= ((*baddr) << 2) & 0x03c;
  1434. hash_ix ^= ((*baddr) >> 4) & 0xf;
  1435. ++baddr;
  1436. hash_ix ^= ((*baddr) << 4) & 0x30;
  1437. hash_ix ^= ((*baddr) >> 2) & 0x3f;
  1438. hash_ix &= 0x3f;
  1439. if (hash_ix >= 32) {
  1440. hi_bits |= (1 << (hash_ix-32));
  1441. }
  1442. else {
  1443. lo_bits |= (1 << hash_ix);
  1444. }
  1445. dmi = dmi->next;
  1446. }
  1447. /* Disable individual receive */
  1448. SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
  1449. *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
  1450. }
  1451. *R_NETWORK_GA_0 = lo_bits;
  1452. *R_NETWORK_GA_1 = hi_bits;
  1453. spin_unlock(&lp->lock);
  1454. }
  1455. void
  1456. e100_hardware_send_packet(char *buf, int length)
  1457. {
  1458. D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
  1459. if (!led_active && time_after(jiffies, led_next_time)) {
  1460. /* light the network leds depending on the current speed. */
  1461. e100_set_network_leds(NETWORK_ACTIVITY);
  1462. /* Set the earliest time we may clear the LED */
  1463. led_next_time = jiffies + NET_FLASH_TIME;
  1464. led_active = 1;
  1465. mod_timer(&clear_led_timer, jiffies + HZ/10);
  1466. }
  1467. /* configure the tx dma descriptor */
  1468. myNextTxDesc->descr.sw_len = length;
  1469. myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
  1470. myNextTxDesc->descr.buf = virt_to_phys(buf);
  1471. /* Move end of list */
  1472. myLastTxDesc->descr.ctrl &= ~d_eol;
  1473. myLastTxDesc = myNextTxDesc;
  1474. /* Restart DMA channel */
  1475. *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
  1476. }
  1477. static void
  1478. e100_clear_network_leds(unsigned long dummy)
  1479. {
  1480. if (led_active && time_after(jiffies, led_next_time)) {
  1481. e100_set_network_leds(NO_NETWORK_ACTIVITY);
  1482. /* Set the earliest time we may set the LED */
  1483. led_next_time = jiffies + NET_FLASH_PAUSE;
  1484. led_active = 0;
  1485. }
  1486. }
  1487. static void
  1488. e100_set_network_leds(int active)
  1489. {
  1490. #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
  1491. int light_leds = (active == NO_NETWORK_ACTIVITY);
  1492. #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
  1493. int light_leds = (active == NETWORK_ACTIVITY);
  1494. #else
  1495. #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
  1496. #endif
  1497. if (!current_speed) {
  1498. /* Make LED red, link is down */
  1499. #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
  1500. LED_NETWORK_SET(LED_RED);
  1501. #else
  1502. LED_NETWORK_SET(LED_OFF);
  1503. #endif
  1504. }
  1505. else if (light_leds) {
  1506. if (current_speed == 10) {
  1507. LED_NETWORK_SET(LED_ORANGE);
  1508. } else {
  1509. LED_NETWORK_SET(LED_GREEN);
  1510. }
  1511. }
  1512. else {
  1513. LED_NETWORK_SET(LED_OFF);
  1514. }
  1515. }
  1516. static int
  1517. etrax_init_module(void)
  1518. {
  1519. return etrax_ethernet_init();
  1520. }
  1521. static int __init
  1522. e100_boot_setup(char* str)
  1523. {
  1524. struct sockaddr sa = {0};
  1525. int i;
  1526. /* Parse the colon separated Ethernet station address */
  1527. for (i = 0; i < ETH_ALEN; i++) {
  1528. unsigned int tmp;
  1529. if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
  1530. printk(KERN_WARNING "Malformed station address");
  1531. return 0;
  1532. }
  1533. sa.sa_data[i] = (char)tmp;
  1534. }
  1535. default_mac = sa;
  1536. return 1;
  1537. }
  1538. __setup("etrax100_eth=", e100_boot_setup);
  1539. module_init(etrax_init_module);