eth_v10.c 55 KB

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