smc91x.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * smc91x.c
  4. * This is a driver for SMSC's 91C9x/91C1xx single-chip Ethernet devices.
  5. *
  6. * Copyright (C) 1996 by Erik Stahlman
  7. * Copyright (C) 2001 Standard Microsystems Corporation
  8. * Developed by Simple Network Magic Corporation
  9. * Copyright (C) 2003 Monta Vista Software, Inc.
  10. * Unified SMC91x driver by Nicolas Pitre
  11. *
  12. * Arguments:
  13. * io = for the base address
  14. * irq = for the IRQ
  15. * nowait = 0 for normal wait states, 1 eliminates additional wait states
  16. *
  17. * original author:
  18. * Erik Stahlman <erik@vt.edu>
  19. *
  20. * hardware multicast code:
  21. * Peter Cammaert <pc@denkart.be>
  22. *
  23. * contributors:
  24. * Daris A Nevil <dnevil@snmc.com>
  25. * Nicolas Pitre <nico@fluxnic.net>
  26. * Russell King <rmk@arm.linux.org.uk>
  27. *
  28. * History:
  29. * 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
  30. * 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
  31. * 03/16/01 Daris A Nevil modified smc9194.c for use with LAN91C111
  32. * 08/22/01 Scott Anderson merge changes from smc9194 to smc91111
  33. * 08/21/01 Pramod B Bhardwaj added support for RevB of LAN91C111
  34. * 12/20/01 Jeff Sutherland initial port to Xscale PXA with DMA support
  35. * 04/07/03 Nicolas Pitre unified SMC91x driver, killed irq races,
  36. * more bus abstraction, big cleanup, etc.
  37. * 29/09/03 Russell King - add driver model support
  38. * - ethtool support
  39. * - convert to use generic MII interface
  40. * - add link up/down notification
  41. * - don't try to handle full negotiation in
  42. * smc_phy_configure
  43. * - clean up (and fix stack overrun) in PHY
  44. * MII read/write functions
  45. * 22/09/04 Nicolas Pitre big update (see commit log for details)
  46. */
  47. static const char version[] =
  48. "smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@fluxnic.net>";
  49. /* Debugging level */
  50. #ifndef SMC_DEBUG
  51. #define SMC_DEBUG 0
  52. #endif
  53. #include <linux/module.h>
  54. #include <linux/kernel.h>
  55. #include <linux/sched.h>
  56. #include <linux/delay.h>
  57. #include <linux/gpio/consumer.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/irq.h>
  60. #include <linux/errno.h>
  61. #include <linux/ioport.h>
  62. #include <linux/crc32.h>
  63. #include <linux/platform_device.h>
  64. #include <linux/spinlock.h>
  65. #include <linux/ethtool.h>
  66. #include <linux/mii.h>
  67. #include <linux/workqueue.h>
  68. #include <linux/of.h>
  69. #include <linux/of_device.h>
  70. #include <linux/netdevice.h>
  71. #include <linux/etherdevice.h>
  72. #include <linux/skbuff.h>
  73. #include <asm/io.h>
  74. #include "smc91x.h"
  75. #if defined(CONFIG_ASSABET_NEPONSET)
  76. #include <mach/assabet.h>
  77. #include <mach/neponset.h>
  78. #endif
  79. #ifndef SMC_NOWAIT
  80. # define SMC_NOWAIT 0
  81. #endif
  82. static int nowait = SMC_NOWAIT;
  83. module_param(nowait, int, 0400);
  84. MODULE_PARM_DESC(nowait, "set to 1 for no wait state");
  85. /*
  86. * Transmit timeout, default 5 seconds.
  87. */
  88. static int watchdog = 1000;
  89. module_param(watchdog, int, 0400);
  90. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  91. MODULE_DESCRIPTION("SMC 91C9x/91C1xxx Ethernet driver");
  92. MODULE_LICENSE("GPL");
  93. MODULE_ALIAS("platform:smc91x");
  94. /*
  95. * The internal workings of the driver. If you are changing anything
  96. * here with the SMC stuff, you should have the datasheet and know
  97. * what you are doing.
  98. */
  99. #define CARDNAME "smc91x"
  100. /*
  101. * Use power-down feature of the chip
  102. */
  103. #define POWER_DOWN 1
  104. /*
  105. * Wait time for memory to be free. This probably shouldn't be
  106. * tuned that much, as waiting for this means nothing else happens
  107. * in the system
  108. */
  109. #define MEMORY_WAIT_TIME 16
  110. /*
  111. * The maximum number of processing loops allowed for each call to the
  112. * IRQ handler.
  113. */
  114. #define MAX_IRQ_LOOPS 8
  115. /*
  116. * This selects whether TX packets are sent one by one to the SMC91x internal
  117. * memory and throttled until transmission completes. This may prevent
  118. * RX overruns a litle by keeping much of the memory free for RX packets
  119. * but to the expense of reduced TX throughput and increased IRQ overhead.
  120. * Note this is not a cure for a too slow data bus or too high IRQ latency.
  121. */
  122. #define THROTTLE_TX_PKTS 0
  123. /*
  124. * The MII clock high/low times. 2x this number gives the MII clock period
  125. * in microseconds. (was 50, but this gives 6.4ms for each MII transaction!)
  126. */
  127. #define MII_DELAY 1
  128. #define DBG(n, dev, fmt, ...) \
  129. do { \
  130. if (SMC_DEBUG >= (n)) \
  131. netdev_dbg(dev, fmt, ##__VA_ARGS__); \
  132. } while (0)
  133. #define PRINTK(dev, fmt, ...) \
  134. do { \
  135. if (SMC_DEBUG > 0) \
  136. netdev_info(dev, fmt, ##__VA_ARGS__); \
  137. else \
  138. netdev_dbg(dev, fmt, ##__VA_ARGS__); \
  139. } while (0)
  140. #if SMC_DEBUG > 3
  141. static void PRINT_PKT(u_char *buf, int length)
  142. {
  143. int i;
  144. int remainder;
  145. int lines;
  146. lines = length / 16;
  147. remainder = length % 16;
  148. for (i = 0; i < lines ; i ++) {
  149. int cur;
  150. printk(KERN_DEBUG);
  151. for (cur = 0; cur < 8; cur++) {
  152. u_char a, b;
  153. a = *buf++;
  154. b = *buf++;
  155. pr_cont("%02x%02x ", a, b);
  156. }
  157. pr_cont("\n");
  158. }
  159. printk(KERN_DEBUG);
  160. for (i = 0; i < remainder/2 ; i++) {
  161. u_char a, b;
  162. a = *buf++;
  163. b = *buf++;
  164. pr_cont("%02x%02x ", a, b);
  165. }
  166. pr_cont("\n");
  167. }
  168. #else
  169. static inline void PRINT_PKT(u_char *buf, int length) { }
  170. #endif
  171. /* this enables an interrupt in the interrupt mask register */
  172. #define SMC_ENABLE_INT(lp, x) do { \
  173. unsigned char mask; \
  174. unsigned long smc_enable_flags; \
  175. spin_lock_irqsave(&lp->lock, smc_enable_flags); \
  176. mask = SMC_GET_INT_MASK(lp); \
  177. mask |= (x); \
  178. SMC_SET_INT_MASK(lp, mask); \
  179. spin_unlock_irqrestore(&lp->lock, smc_enable_flags); \
  180. } while (0)
  181. /* this disables an interrupt from the interrupt mask register */
  182. #define SMC_DISABLE_INT(lp, x) do { \
  183. unsigned char mask; \
  184. unsigned long smc_disable_flags; \
  185. spin_lock_irqsave(&lp->lock, smc_disable_flags); \
  186. mask = SMC_GET_INT_MASK(lp); \
  187. mask &= ~(x); \
  188. SMC_SET_INT_MASK(lp, mask); \
  189. spin_unlock_irqrestore(&lp->lock, smc_disable_flags); \
  190. } while (0)
  191. /*
  192. * Wait while MMU is busy. This is usually in the order of a few nanosecs
  193. * if at all, but let's avoid deadlocking the system if the hardware
  194. * decides to go south.
  195. */
  196. #define SMC_WAIT_MMU_BUSY(lp) do { \
  197. if (unlikely(SMC_GET_MMU_CMD(lp) & MC_BUSY)) { \
  198. unsigned long timeout = jiffies + 2; \
  199. while (SMC_GET_MMU_CMD(lp) & MC_BUSY) { \
  200. if (time_after(jiffies, timeout)) { \
  201. netdev_dbg(dev, "timeout %s line %d\n", \
  202. __FILE__, __LINE__); \
  203. break; \
  204. } \
  205. cpu_relax(); \
  206. } \
  207. } \
  208. } while (0)
  209. /*
  210. * this does a soft reset on the device
  211. */
  212. static void smc_reset(struct net_device *dev)
  213. {
  214. struct smc_local *lp = netdev_priv(dev);
  215. void __iomem *ioaddr = lp->base;
  216. unsigned int ctl, cfg;
  217. struct sk_buff *pending_skb;
  218. DBG(2, dev, "%s\n", __func__);
  219. /* Disable all interrupts, block TX tasklet */
  220. spin_lock_irq(&lp->lock);
  221. SMC_SELECT_BANK(lp, 2);
  222. SMC_SET_INT_MASK(lp, 0);
  223. pending_skb = lp->pending_tx_skb;
  224. lp->pending_tx_skb = NULL;
  225. spin_unlock_irq(&lp->lock);
  226. /* free any pending tx skb */
  227. if (pending_skb) {
  228. dev_kfree_skb(pending_skb);
  229. dev->stats.tx_errors++;
  230. dev->stats.tx_aborted_errors++;
  231. }
  232. /*
  233. * This resets the registers mostly to defaults, but doesn't
  234. * affect EEPROM. That seems unnecessary
  235. */
  236. SMC_SELECT_BANK(lp, 0);
  237. SMC_SET_RCR(lp, RCR_SOFTRST);
  238. /*
  239. * Setup the Configuration Register
  240. * This is necessary because the CONFIG_REG is not affected
  241. * by a soft reset
  242. */
  243. SMC_SELECT_BANK(lp, 1);
  244. cfg = CONFIG_DEFAULT;
  245. /*
  246. * Setup for fast accesses if requested. If the card/system
  247. * can't handle it then there will be no recovery except for
  248. * a hard reset or power cycle
  249. */
  250. if (lp->cfg.flags & SMC91X_NOWAIT)
  251. cfg |= CONFIG_NO_WAIT;
  252. /*
  253. * Release from possible power-down state
  254. * Configuration register is not affected by Soft Reset
  255. */
  256. cfg |= CONFIG_EPH_POWER_EN;
  257. SMC_SET_CONFIG(lp, cfg);
  258. /* this should pause enough for the chip to be happy */
  259. /*
  260. * elaborate? What does the chip _need_? --jgarzik
  261. *
  262. * This seems to be undocumented, but something the original
  263. * driver(s) have always done. Suspect undocumented timing
  264. * info/determined empirically. --rmk
  265. */
  266. udelay(1);
  267. /* Disable transmit and receive functionality */
  268. SMC_SELECT_BANK(lp, 0);
  269. SMC_SET_RCR(lp, RCR_CLEAR);
  270. SMC_SET_TCR(lp, TCR_CLEAR);
  271. SMC_SELECT_BANK(lp, 1);
  272. ctl = SMC_GET_CTL(lp) | CTL_LE_ENABLE;
  273. /*
  274. * Set the control register to automatically release successfully
  275. * transmitted packets, to make the best use out of our limited
  276. * memory
  277. */
  278. if(!THROTTLE_TX_PKTS)
  279. ctl |= CTL_AUTO_RELEASE;
  280. else
  281. ctl &= ~CTL_AUTO_RELEASE;
  282. SMC_SET_CTL(lp, ctl);
  283. /* Reset the MMU */
  284. SMC_SELECT_BANK(lp, 2);
  285. SMC_SET_MMU_CMD(lp, MC_RESET);
  286. SMC_WAIT_MMU_BUSY(lp);
  287. }
  288. /*
  289. * Enable Interrupts, Receive, and Transmit
  290. */
  291. static void smc_enable(struct net_device *dev)
  292. {
  293. struct smc_local *lp = netdev_priv(dev);
  294. void __iomem *ioaddr = lp->base;
  295. int mask;
  296. DBG(2, dev, "%s\n", __func__);
  297. /* see the header file for options in TCR/RCR DEFAULT */
  298. SMC_SELECT_BANK(lp, 0);
  299. SMC_SET_TCR(lp, lp->tcr_cur_mode);
  300. SMC_SET_RCR(lp, lp->rcr_cur_mode);
  301. SMC_SELECT_BANK(lp, 1);
  302. SMC_SET_MAC_ADDR(lp, dev->dev_addr);
  303. /* now, enable interrupts */
  304. mask = IM_EPH_INT|IM_RX_OVRN_INT|IM_RCV_INT;
  305. if (lp->version >= (CHIP_91100 << 4))
  306. mask |= IM_MDINT;
  307. SMC_SELECT_BANK(lp, 2);
  308. SMC_SET_INT_MASK(lp, mask);
  309. /*
  310. * From this point the register bank must _NOT_ be switched away
  311. * to something else than bank 2 without proper locking against
  312. * races with any tasklet or interrupt handlers until smc_shutdown()
  313. * or smc_reset() is called.
  314. */
  315. }
  316. /*
  317. * this puts the device in an inactive state
  318. */
  319. static void smc_shutdown(struct net_device *dev)
  320. {
  321. struct smc_local *lp = netdev_priv(dev);
  322. void __iomem *ioaddr = lp->base;
  323. struct sk_buff *pending_skb;
  324. DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
  325. /* no more interrupts for me */
  326. spin_lock_irq(&lp->lock);
  327. SMC_SELECT_BANK(lp, 2);
  328. SMC_SET_INT_MASK(lp, 0);
  329. pending_skb = lp->pending_tx_skb;
  330. lp->pending_tx_skb = NULL;
  331. spin_unlock_irq(&lp->lock);
  332. dev_kfree_skb(pending_skb);
  333. /* and tell the card to stay away from that nasty outside world */
  334. SMC_SELECT_BANK(lp, 0);
  335. SMC_SET_RCR(lp, RCR_CLEAR);
  336. SMC_SET_TCR(lp, TCR_CLEAR);
  337. #ifdef POWER_DOWN
  338. /* finally, shut the chip down */
  339. SMC_SELECT_BANK(lp, 1);
  340. SMC_SET_CONFIG(lp, SMC_GET_CONFIG(lp) & ~CONFIG_EPH_POWER_EN);
  341. #endif
  342. }
  343. /*
  344. * This is the procedure to handle the receipt of a packet.
  345. */
  346. static inline void smc_rcv(struct net_device *dev)
  347. {
  348. struct smc_local *lp = netdev_priv(dev);
  349. void __iomem *ioaddr = lp->base;
  350. unsigned int packet_number, status, packet_len;
  351. DBG(3, dev, "%s\n", __func__);
  352. packet_number = SMC_GET_RXFIFO(lp);
  353. if (unlikely(packet_number & RXFIFO_REMPTY)) {
  354. PRINTK(dev, "smc_rcv with nothing on FIFO.\n");
  355. return;
  356. }
  357. /* read from start of packet */
  358. SMC_SET_PTR(lp, PTR_READ | PTR_RCV | PTR_AUTOINC);
  359. /* First two words are status and packet length */
  360. SMC_GET_PKT_HDR(lp, status, packet_len);
  361. packet_len &= 0x07ff; /* mask off top bits */
  362. DBG(2, dev, "RX PNR 0x%x STATUS 0x%04x LENGTH 0x%04x (%d)\n",
  363. packet_number, status, packet_len, packet_len);
  364. back:
  365. if (unlikely(packet_len < 6 || status & RS_ERRORS)) {
  366. if (status & RS_TOOLONG && packet_len <= (1514 + 4 + 6)) {
  367. /* accept VLAN packets */
  368. status &= ~RS_TOOLONG;
  369. goto back;
  370. }
  371. if (packet_len < 6) {
  372. /* bloody hardware */
  373. netdev_err(dev, "fubar (rxlen %u status %x\n",
  374. packet_len, status);
  375. status |= RS_TOOSHORT;
  376. }
  377. SMC_WAIT_MMU_BUSY(lp);
  378. SMC_SET_MMU_CMD(lp, MC_RELEASE);
  379. dev->stats.rx_errors++;
  380. if (status & RS_ALGNERR)
  381. dev->stats.rx_frame_errors++;
  382. if (status & (RS_TOOSHORT | RS_TOOLONG))
  383. dev->stats.rx_length_errors++;
  384. if (status & RS_BADCRC)
  385. dev->stats.rx_crc_errors++;
  386. } else {
  387. struct sk_buff *skb;
  388. unsigned char *data;
  389. unsigned int data_len;
  390. /* set multicast stats */
  391. if (status & RS_MULTICAST)
  392. dev->stats.multicast++;
  393. /*
  394. * Actual payload is packet_len - 6 (or 5 if odd byte).
  395. * We want skb_reserve(2) and the final ctrl word
  396. * (2 bytes, possibly containing the payload odd byte).
  397. * Furthermore, we add 2 bytes to allow rounding up to
  398. * multiple of 4 bytes on 32 bit buses.
  399. * Hence packet_len - 6 + 2 + 2 + 2.
  400. */
  401. skb = netdev_alloc_skb(dev, packet_len);
  402. if (unlikely(skb == NULL)) {
  403. SMC_WAIT_MMU_BUSY(lp);
  404. SMC_SET_MMU_CMD(lp, MC_RELEASE);
  405. dev->stats.rx_dropped++;
  406. return;
  407. }
  408. /* Align IP header to 32 bits */
  409. skb_reserve(skb, 2);
  410. /* BUG: the LAN91C111 rev A never sets this bit. Force it. */
  411. if (lp->version == 0x90)
  412. status |= RS_ODDFRAME;
  413. /*
  414. * If odd length: packet_len - 5,
  415. * otherwise packet_len - 6.
  416. * With the trailing ctrl byte it's packet_len - 4.
  417. */
  418. data_len = packet_len - ((status & RS_ODDFRAME) ? 5 : 6);
  419. data = skb_put(skb, data_len);
  420. SMC_PULL_DATA(lp, data, packet_len - 4);
  421. SMC_WAIT_MMU_BUSY(lp);
  422. SMC_SET_MMU_CMD(lp, MC_RELEASE);
  423. PRINT_PKT(data, packet_len - 4);
  424. skb->protocol = eth_type_trans(skb, dev);
  425. netif_rx(skb);
  426. dev->stats.rx_packets++;
  427. dev->stats.rx_bytes += data_len;
  428. }
  429. }
  430. #ifdef CONFIG_SMP
  431. /*
  432. * On SMP we have the following problem:
  433. *
  434. * A = smc_hardware_send_pkt()
  435. * B = smc_hard_start_xmit()
  436. * C = smc_interrupt()
  437. *
  438. * A and B can never be executed simultaneously. However, at least on UP,
  439. * it is possible (and even desirable) for C to interrupt execution of
  440. * A or B in order to have better RX reliability and avoid overruns.
  441. * C, just like A and B, must have exclusive access to the chip and
  442. * each of them must lock against any other concurrent access.
  443. * Unfortunately this is not possible to have C suspend execution of A or
  444. * B taking place on another CPU. On UP this is no an issue since A and B
  445. * are run from softirq context and C from hard IRQ context, and there is
  446. * no other CPU where concurrent access can happen.
  447. * If ever there is a way to force at least B and C to always be executed
  448. * on the same CPU then we could use read/write locks to protect against
  449. * any other concurrent access and C would always interrupt B. But life
  450. * isn't that easy in a SMP world...
  451. */
  452. #define smc_special_trylock(lock, flags) \
  453. ({ \
  454. int __ret; \
  455. local_irq_save(flags); \
  456. __ret = spin_trylock(lock); \
  457. if (!__ret) \
  458. local_irq_restore(flags); \
  459. __ret; \
  460. })
  461. #define smc_special_lock(lock, flags) spin_lock_irqsave(lock, flags)
  462. #define smc_special_unlock(lock, flags) spin_unlock_irqrestore(lock, flags)
  463. #else
  464. #define smc_special_trylock(lock, flags) ((void)flags, true)
  465. #define smc_special_lock(lock, flags) do { flags = 0; } while (0)
  466. #define smc_special_unlock(lock, flags) do { flags = 0; } while (0)
  467. #endif
  468. /*
  469. * This is called to actually send a packet to the chip.
  470. */
  471. static void smc_hardware_send_pkt(struct tasklet_struct *t)
  472. {
  473. struct smc_local *lp = from_tasklet(lp, t, tx_task);
  474. struct net_device *dev = lp->dev;
  475. void __iomem *ioaddr = lp->base;
  476. struct sk_buff *skb;
  477. unsigned int packet_no, len;
  478. unsigned char *buf;
  479. unsigned long flags;
  480. DBG(3, dev, "%s\n", __func__);
  481. if (!smc_special_trylock(&lp->lock, flags)) {
  482. netif_stop_queue(dev);
  483. tasklet_schedule(&lp->tx_task);
  484. return;
  485. }
  486. skb = lp->pending_tx_skb;
  487. if (unlikely(!skb)) {
  488. smc_special_unlock(&lp->lock, flags);
  489. return;
  490. }
  491. lp->pending_tx_skb = NULL;
  492. packet_no = SMC_GET_AR(lp);
  493. if (unlikely(packet_no & AR_FAILED)) {
  494. netdev_err(dev, "Memory allocation failed.\n");
  495. dev->stats.tx_errors++;
  496. dev->stats.tx_fifo_errors++;
  497. smc_special_unlock(&lp->lock, flags);
  498. goto done;
  499. }
  500. /* point to the beginning of the packet */
  501. SMC_SET_PN(lp, packet_no);
  502. SMC_SET_PTR(lp, PTR_AUTOINC);
  503. buf = skb->data;
  504. len = skb->len;
  505. DBG(2, dev, "TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",
  506. packet_no, len, len, buf);
  507. PRINT_PKT(buf, len);
  508. /*
  509. * Send the packet length (+6 for status words, length, and ctl.
  510. * The card will pad to 64 bytes with zeroes if packet is too small.
  511. */
  512. SMC_PUT_PKT_HDR(lp, 0, len + 6);
  513. /* send the actual data */
  514. SMC_PUSH_DATA(lp, buf, len & ~1);
  515. /* Send final ctl word with the last byte if there is one */
  516. SMC_outw(lp, ((len & 1) ? (0x2000 | buf[len - 1]) : 0), ioaddr,
  517. DATA_REG(lp));
  518. /*
  519. * If THROTTLE_TX_PKTS is set, we stop the queue here. This will
  520. * have the effect of having at most one packet queued for TX
  521. * in the chip's memory at all time.
  522. *
  523. * If THROTTLE_TX_PKTS is not set then the queue is stopped only
  524. * when memory allocation (MC_ALLOC) does not succeed right away.
  525. */
  526. if (THROTTLE_TX_PKTS)
  527. netif_stop_queue(dev);
  528. /* queue the packet for TX */
  529. SMC_SET_MMU_CMD(lp, MC_ENQUEUE);
  530. smc_special_unlock(&lp->lock, flags);
  531. netif_trans_update(dev);
  532. dev->stats.tx_packets++;
  533. dev->stats.tx_bytes += len;
  534. SMC_ENABLE_INT(lp, IM_TX_INT | IM_TX_EMPTY_INT);
  535. done: if (!THROTTLE_TX_PKTS)
  536. netif_wake_queue(dev);
  537. dev_consume_skb_any(skb);
  538. }
  539. /*
  540. * Since I am not sure if I will have enough room in the chip's ram
  541. * to store the packet, I call this routine which either sends it
  542. * now, or set the card to generates an interrupt when ready
  543. * for the packet.
  544. */
  545. static netdev_tx_t
  546. smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  547. {
  548. struct smc_local *lp = netdev_priv(dev);
  549. void __iomem *ioaddr = lp->base;
  550. unsigned int numPages, poll_count, status;
  551. unsigned long flags;
  552. DBG(3, dev, "%s\n", __func__);
  553. BUG_ON(lp->pending_tx_skb != NULL);
  554. /*
  555. * The MMU wants the number of pages to be the number of 256 bytes
  556. * 'pages', minus 1 (since a packet can't ever have 0 pages :))
  557. *
  558. * The 91C111 ignores the size bits, but earlier models don't.
  559. *
  560. * Pkt size for allocating is data length +6 (for additional status
  561. * words, length and ctl)
  562. *
  563. * If odd size then last byte is included in ctl word.
  564. */
  565. numPages = ((skb->len & ~1) + (6 - 1)) >> 8;
  566. if (unlikely(numPages > 7)) {
  567. netdev_warn(dev, "Far too big packet error.\n");
  568. dev->stats.tx_errors++;
  569. dev->stats.tx_dropped++;
  570. dev_kfree_skb_any(skb);
  571. return NETDEV_TX_OK;
  572. }
  573. smc_special_lock(&lp->lock, flags);
  574. /* now, try to allocate the memory */
  575. SMC_SET_MMU_CMD(lp, MC_ALLOC | numPages);
  576. /*
  577. * Poll the chip for a short amount of time in case the
  578. * allocation succeeds quickly.
  579. */
  580. poll_count = MEMORY_WAIT_TIME;
  581. do {
  582. status = SMC_GET_INT(lp);
  583. if (status & IM_ALLOC_INT) {
  584. SMC_ACK_INT(lp, IM_ALLOC_INT);
  585. break;
  586. }
  587. } while (--poll_count);
  588. smc_special_unlock(&lp->lock, flags);
  589. lp->pending_tx_skb = skb;
  590. if (!poll_count) {
  591. /* oh well, wait until the chip finds memory later */
  592. netif_stop_queue(dev);
  593. DBG(2, dev, "TX memory allocation deferred.\n");
  594. SMC_ENABLE_INT(lp, IM_ALLOC_INT);
  595. } else {
  596. /*
  597. * Allocation succeeded: push packet to the chip's own memory
  598. * immediately.
  599. */
  600. smc_hardware_send_pkt(&lp->tx_task);
  601. }
  602. return NETDEV_TX_OK;
  603. }
  604. /*
  605. * This handles a TX interrupt, which is only called when:
  606. * - a TX error occurred, or
  607. * - CTL_AUTO_RELEASE is not set and TX of a packet completed.
  608. */
  609. static void smc_tx(struct net_device *dev)
  610. {
  611. struct smc_local *lp = netdev_priv(dev);
  612. void __iomem *ioaddr = lp->base;
  613. unsigned int saved_packet, packet_no, tx_status;
  614. unsigned int pkt_len __always_unused;
  615. DBG(3, dev, "%s\n", __func__);
  616. /* If the TX FIFO is empty then nothing to do */
  617. packet_no = SMC_GET_TXFIFO(lp);
  618. if (unlikely(packet_no & TXFIFO_TEMPTY)) {
  619. PRINTK(dev, "smc_tx with nothing on FIFO.\n");
  620. return;
  621. }
  622. /* select packet to read from */
  623. saved_packet = SMC_GET_PN(lp);
  624. SMC_SET_PN(lp, packet_no);
  625. /* read the first word (status word) from this packet */
  626. SMC_SET_PTR(lp, PTR_AUTOINC | PTR_READ);
  627. SMC_GET_PKT_HDR(lp, tx_status, pkt_len);
  628. DBG(2, dev, "TX STATUS 0x%04x PNR 0x%02x\n",
  629. tx_status, packet_no);
  630. if (!(tx_status & ES_TX_SUC))
  631. dev->stats.tx_errors++;
  632. if (tx_status & ES_LOSTCARR)
  633. dev->stats.tx_carrier_errors++;
  634. if (tx_status & (ES_LATCOL | ES_16COL)) {
  635. PRINTK(dev, "%s occurred on last xmit\n",
  636. (tx_status & ES_LATCOL) ?
  637. "late collision" : "too many collisions");
  638. dev->stats.tx_window_errors++;
  639. if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {
  640. netdev_info(dev, "unexpectedly large number of bad collisions. Please check duplex setting.\n");
  641. }
  642. }
  643. /* kill the packet */
  644. SMC_WAIT_MMU_BUSY(lp);
  645. SMC_SET_MMU_CMD(lp, MC_FREEPKT);
  646. /* Don't restore Packet Number Reg until busy bit is cleared */
  647. SMC_WAIT_MMU_BUSY(lp);
  648. SMC_SET_PN(lp, saved_packet);
  649. /* re-enable transmit */
  650. SMC_SELECT_BANK(lp, 0);
  651. SMC_SET_TCR(lp, lp->tcr_cur_mode);
  652. SMC_SELECT_BANK(lp, 2);
  653. }
  654. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  655. static void smc_mii_out(struct net_device *dev, unsigned int val, int bits)
  656. {
  657. struct smc_local *lp = netdev_priv(dev);
  658. void __iomem *ioaddr = lp->base;
  659. unsigned int mii_reg, mask;
  660. mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
  661. mii_reg |= MII_MDOE;
  662. for (mask = 1 << (bits - 1); mask; mask >>= 1) {
  663. if (val & mask)
  664. mii_reg |= MII_MDO;
  665. else
  666. mii_reg &= ~MII_MDO;
  667. SMC_SET_MII(lp, mii_reg);
  668. udelay(MII_DELAY);
  669. SMC_SET_MII(lp, mii_reg | MII_MCLK);
  670. udelay(MII_DELAY);
  671. }
  672. }
  673. static unsigned int smc_mii_in(struct net_device *dev, int bits)
  674. {
  675. struct smc_local *lp = netdev_priv(dev);
  676. void __iomem *ioaddr = lp->base;
  677. unsigned int mii_reg, mask, val;
  678. mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
  679. SMC_SET_MII(lp, mii_reg);
  680. for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {
  681. if (SMC_GET_MII(lp) & MII_MDI)
  682. val |= mask;
  683. SMC_SET_MII(lp, mii_reg);
  684. udelay(MII_DELAY);
  685. SMC_SET_MII(lp, mii_reg | MII_MCLK);
  686. udelay(MII_DELAY);
  687. }
  688. return val;
  689. }
  690. /*
  691. * Reads a register from the MII Management serial interface
  692. */
  693. static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg)
  694. {
  695. struct smc_local *lp = netdev_priv(dev);
  696. void __iomem *ioaddr = lp->base;
  697. unsigned int phydata;
  698. SMC_SELECT_BANK(lp, 3);
  699. /* Idle - 32 ones */
  700. smc_mii_out(dev, 0xffffffff, 32);
  701. /* Start code (01) + read (10) + phyaddr + phyreg */
  702. smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);
  703. /* Turnaround (2bits) + phydata */
  704. phydata = smc_mii_in(dev, 18);
  705. /* Return to idle state */
  706. SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
  707. DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
  708. __func__, phyaddr, phyreg, phydata);
  709. SMC_SELECT_BANK(lp, 2);
  710. return phydata;
  711. }
  712. /*
  713. * Writes a register to the MII Management serial interface
  714. */
  715. static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,
  716. int phydata)
  717. {
  718. struct smc_local *lp = netdev_priv(dev);
  719. void __iomem *ioaddr = lp->base;
  720. SMC_SELECT_BANK(lp, 3);
  721. /* Idle - 32 ones */
  722. smc_mii_out(dev, 0xffffffff, 32);
  723. /* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */
  724. smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);
  725. /* Return to idle state */
  726. SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
  727. DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
  728. __func__, phyaddr, phyreg, phydata);
  729. SMC_SELECT_BANK(lp, 2);
  730. }
  731. /*
  732. * Finds and reports the PHY address
  733. */
  734. static void smc_phy_detect(struct net_device *dev)
  735. {
  736. struct smc_local *lp = netdev_priv(dev);
  737. int phyaddr;
  738. DBG(2, dev, "%s\n", __func__);
  739. lp->phy_type = 0;
  740. /*
  741. * Scan all 32 PHY addresses if necessary, starting at
  742. * PHY#1 to PHY#31, and then PHY#0 last.
  743. */
  744. for (phyaddr = 1; phyaddr < 33; ++phyaddr) {
  745. unsigned int id1, id2;
  746. /* Read the PHY identifiers */
  747. id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);
  748. id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);
  749. DBG(3, dev, "phy_id1=0x%x, phy_id2=0x%x\n",
  750. id1, id2);
  751. /* Make sure it is a valid identifier */
  752. if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&
  753. id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {
  754. /* Save the PHY's address */
  755. lp->mii.phy_id = phyaddr & 31;
  756. lp->phy_type = id1 << 16 | id2;
  757. break;
  758. }
  759. }
  760. }
  761. /*
  762. * Sets the PHY to a configuration as determined by the user
  763. */
  764. static int smc_phy_fixed(struct net_device *dev)
  765. {
  766. struct smc_local *lp = netdev_priv(dev);
  767. void __iomem *ioaddr = lp->base;
  768. int phyaddr = lp->mii.phy_id;
  769. int bmcr, cfg1;
  770. DBG(3, dev, "%s\n", __func__);
  771. /* Enter Link Disable state */
  772. cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);
  773. cfg1 |= PHY_CFG1_LNKDIS;
  774. smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);
  775. /*
  776. * Set our fixed capabilities
  777. * Disable auto-negotiation
  778. */
  779. bmcr = 0;
  780. if (lp->ctl_rfduplx)
  781. bmcr |= BMCR_FULLDPLX;
  782. if (lp->ctl_rspeed == 100)
  783. bmcr |= BMCR_SPEED100;
  784. /* Write our capabilities to the phy control register */
  785. smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);
  786. /* Re-Configure the Receive/Phy Control register */
  787. SMC_SELECT_BANK(lp, 0);
  788. SMC_SET_RPC(lp, lp->rpc_cur_mode);
  789. SMC_SELECT_BANK(lp, 2);
  790. return 1;
  791. }
  792. /**
  793. * smc_phy_reset - reset the phy
  794. * @dev: net device
  795. * @phy: phy address
  796. *
  797. * Issue a software reset for the specified PHY and
  798. * wait up to 100ms for the reset to complete. We should
  799. * not access the PHY for 50ms after issuing the reset.
  800. *
  801. * The time to wait appears to be dependent on the PHY.
  802. *
  803. * Must be called with lp->lock locked.
  804. */
  805. static int smc_phy_reset(struct net_device *dev, int phy)
  806. {
  807. struct smc_local *lp = netdev_priv(dev);
  808. unsigned int bmcr;
  809. int timeout;
  810. smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);
  811. for (timeout = 2; timeout; timeout--) {
  812. spin_unlock_irq(&lp->lock);
  813. msleep(50);
  814. spin_lock_irq(&lp->lock);
  815. bmcr = smc_phy_read(dev, phy, MII_BMCR);
  816. if (!(bmcr & BMCR_RESET))
  817. break;
  818. }
  819. return bmcr & BMCR_RESET;
  820. }
  821. /**
  822. * smc_phy_powerdown - powerdown phy
  823. * @dev: net device
  824. *
  825. * Power down the specified PHY
  826. */
  827. static void smc_phy_powerdown(struct net_device *dev)
  828. {
  829. struct smc_local *lp = netdev_priv(dev);
  830. unsigned int bmcr;
  831. int phy = lp->mii.phy_id;
  832. if (lp->phy_type == 0)
  833. return;
  834. /* We need to ensure that no calls to smc_phy_configure are
  835. pending.
  836. */
  837. cancel_work_sync(&lp->phy_configure);
  838. bmcr = smc_phy_read(dev, phy, MII_BMCR);
  839. smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);
  840. }
  841. /**
  842. * smc_phy_check_media - check the media status and adjust TCR
  843. * @dev: net device
  844. * @init: set true for initialisation
  845. *
  846. * Select duplex mode depending on negotiation state. This
  847. * also updates our carrier state.
  848. */
  849. static void smc_phy_check_media(struct net_device *dev, int init)
  850. {
  851. struct smc_local *lp = netdev_priv(dev);
  852. void __iomem *ioaddr = lp->base;
  853. if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
  854. /* duplex state has changed */
  855. if (lp->mii.full_duplex) {
  856. lp->tcr_cur_mode |= TCR_SWFDUP;
  857. } else {
  858. lp->tcr_cur_mode &= ~TCR_SWFDUP;
  859. }
  860. SMC_SELECT_BANK(lp, 0);
  861. SMC_SET_TCR(lp, lp->tcr_cur_mode);
  862. }
  863. }
  864. /*
  865. * Configures the specified PHY through the MII management interface
  866. * using Autonegotiation.
  867. * Calls smc_phy_fixed() if the user has requested a certain config.
  868. * If RPC ANEG bit is set, the media selection is dependent purely on
  869. * the selection by the MII (either in the MII BMCR reg or the result
  870. * of autonegotiation.) If the RPC ANEG bit is cleared, the selection
  871. * is controlled by the RPC SPEED and RPC DPLX bits.
  872. */
  873. static void smc_phy_configure(struct work_struct *work)
  874. {
  875. struct smc_local *lp =
  876. container_of(work, struct smc_local, phy_configure);
  877. struct net_device *dev = lp->dev;
  878. void __iomem *ioaddr = lp->base;
  879. int phyaddr = lp->mii.phy_id;
  880. int my_phy_caps; /* My PHY capabilities */
  881. int my_ad_caps; /* My Advertised capabilities */
  882. DBG(3, dev, "smc_program_phy()\n");
  883. spin_lock_irq(&lp->lock);
  884. /*
  885. * We should not be called if phy_type is zero.
  886. */
  887. if (lp->phy_type == 0)
  888. goto smc_phy_configure_exit;
  889. if (smc_phy_reset(dev, phyaddr)) {
  890. netdev_info(dev, "PHY reset timed out\n");
  891. goto smc_phy_configure_exit;
  892. }
  893. /*
  894. * Enable PHY Interrupts (for register 18)
  895. * Interrupts listed here are disabled
  896. */
  897. smc_phy_write(dev, phyaddr, PHY_MASK_REG,
  898. PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
  899. PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
  900. PHY_INT_SPDDET | PHY_INT_DPLXDET);
  901. /* Configure the Receive/Phy Control register */
  902. SMC_SELECT_BANK(lp, 0);
  903. SMC_SET_RPC(lp, lp->rpc_cur_mode);
  904. /* If the user requested no auto neg, then go set his request */
  905. if (lp->mii.force_media) {
  906. smc_phy_fixed(dev);
  907. goto smc_phy_configure_exit;
  908. }
  909. /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
  910. my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);
  911. if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
  912. netdev_info(dev, "Auto negotiation NOT supported\n");
  913. smc_phy_fixed(dev);
  914. goto smc_phy_configure_exit;
  915. }
  916. my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */
  917. if (my_phy_caps & BMSR_100BASE4)
  918. my_ad_caps |= ADVERTISE_100BASE4;
  919. if (my_phy_caps & BMSR_100FULL)
  920. my_ad_caps |= ADVERTISE_100FULL;
  921. if (my_phy_caps & BMSR_100HALF)
  922. my_ad_caps |= ADVERTISE_100HALF;
  923. if (my_phy_caps & BMSR_10FULL)
  924. my_ad_caps |= ADVERTISE_10FULL;
  925. if (my_phy_caps & BMSR_10HALF)
  926. my_ad_caps |= ADVERTISE_10HALF;
  927. /* Disable capabilities not selected by our user */
  928. if (lp->ctl_rspeed != 100)
  929. my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
  930. if (!lp->ctl_rfduplx)
  931. my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
  932. /* Update our Auto-Neg Advertisement Register */
  933. smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);
  934. lp->mii.advertising = my_ad_caps;
  935. /*
  936. * Read the register back. Without this, it appears that when
  937. * auto-negotiation is restarted, sometimes it isn't ready and
  938. * the link does not come up.
  939. */
  940. smc_phy_read(dev, phyaddr, MII_ADVERTISE);
  941. DBG(2, dev, "phy caps=%x\n", my_phy_caps);
  942. DBG(2, dev, "phy advertised caps=%x\n", my_ad_caps);
  943. /* Restart auto-negotiation process in order to advertise my caps */
  944. smc_phy_write(dev, phyaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  945. smc_phy_check_media(dev, 1);
  946. smc_phy_configure_exit:
  947. SMC_SELECT_BANK(lp, 2);
  948. spin_unlock_irq(&lp->lock);
  949. }
  950. /*
  951. * smc_phy_interrupt
  952. *
  953. * Purpose: Handle interrupts relating to PHY register 18. This is
  954. * called from the "hard" interrupt handler under our private spinlock.
  955. */
  956. static void smc_phy_interrupt(struct net_device *dev)
  957. {
  958. struct smc_local *lp = netdev_priv(dev);
  959. int phyaddr = lp->mii.phy_id;
  960. int phy18;
  961. DBG(2, dev, "%s\n", __func__);
  962. if (lp->phy_type == 0)
  963. return;
  964. for(;;) {
  965. smc_phy_check_media(dev, 0);
  966. /* Read PHY Register 18, Status Output */
  967. phy18 = smc_phy_read(dev, phyaddr, PHY_INT_REG);
  968. if ((phy18 & PHY_INT_INT) == 0)
  969. break;
  970. }
  971. }
  972. /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
  973. static void smc_10bt_check_media(struct net_device *dev, int init)
  974. {
  975. struct smc_local *lp = netdev_priv(dev);
  976. void __iomem *ioaddr = lp->base;
  977. unsigned int old_carrier, new_carrier;
  978. old_carrier = netif_carrier_ok(dev) ? 1 : 0;
  979. SMC_SELECT_BANK(lp, 0);
  980. new_carrier = (SMC_GET_EPH_STATUS(lp) & ES_LINK_OK) ? 1 : 0;
  981. SMC_SELECT_BANK(lp, 2);
  982. if (init || (old_carrier != new_carrier)) {
  983. if (!new_carrier) {
  984. netif_carrier_off(dev);
  985. } else {
  986. netif_carrier_on(dev);
  987. }
  988. if (netif_msg_link(lp))
  989. netdev_info(dev, "link %s\n",
  990. new_carrier ? "up" : "down");
  991. }
  992. }
  993. static void smc_eph_interrupt(struct net_device *dev)
  994. {
  995. struct smc_local *lp = netdev_priv(dev);
  996. void __iomem *ioaddr = lp->base;
  997. unsigned int ctl;
  998. smc_10bt_check_media(dev, 0);
  999. SMC_SELECT_BANK(lp, 1);
  1000. ctl = SMC_GET_CTL(lp);
  1001. SMC_SET_CTL(lp, ctl & ~CTL_LE_ENABLE);
  1002. SMC_SET_CTL(lp, ctl);
  1003. SMC_SELECT_BANK(lp, 2);
  1004. }
  1005. /*
  1006. * This is the main routine of the driver, to handle the device when
  1007. * it needs some attention.
  1008. */
  1009. static irqreturn_t smc_interrupt(int irq, void *dev_id)
  1010. {
  1011. struct net_device *dev = dev_id;
  1012. struct smc_local *lp = netdev_priv(dev);
  1013. void __iomem *ioaddr = lp->base;
  1014. int status, mask, timeout, card_stats;
  1015. int saved_pointer;
  1016. DBG(3, dev, "%s\n", __func__);
  1017. spin_lock(&lp->lock);
  1018. /* A preamble may be used when there is a potential race
  1019. * between the interruptible transmit functions and this
  1020. * ISR. */
  1021. SMC_INTERRUPT_PREAMBLE;
  1022. saved_pointer = SMC_GET_PTR(lp);
  1023. mask = SMC_GET_INT_MASK(lp);
  1024. SMC_SET_INT_MASK(lp, 0);
  1025. /* set a timeout value, so I don't stay here forever */
  1026. timeout = MAX_IRQ_LOOPS;
  1027. do {
  1028. status = SMC_GET_INT(lp);
  1029. DBG(2, dev, "INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x\n",
  1030. status, mask,
  1031. ({ int meminfo; SMC_SELECT_BANK(lp, 0);
  1032. meminfo = SMC_GET_MIR(lp);
  1033. SMC_SELECT_BANK(lp, 2); meminfo; }),
  1034. SMC_GET_FIFO(lp));
  1035. status &= mask;
  1036. if (!status)
  1037. break;
  1038. if (status & IM_TX_INT) {
  1039. /* do this before RX as it will free memory quickly */
  1040. DBG(3, dev, "TX int\n");
  1041. smc_tx(dev);
  1042. SMC_ACK_INT(lp, IM_TX_INT);
  1043. if (THROTTLE_TX_PKTS)
  1044. netif_wake_queue(dev);
  1045. } else if (status & IM_RCV_INT) {
  1046. DBG(3, dev, "RX irq\n");
  1047. smc_rcv(dev);
  1048. } else if (status & IM_ALLOC_INT) {
  1049. DBG(3, dev, "Allocation irq\n");
  1050. tasklet_hi_schedule(&lp->tx_task);
  1051. mask &= ~IM_ALLOC_INT;
  1052. } else if (status & IM_TX_EMPTY_INT) {
  1053. DBG(3, dev, "TX empty\n");
  1054. mask &= ~IM_TX_EMPTY_INT;
  1055. /* update stats */
  1056. SMC_SELECT_BANK(lp, 0);
  1057. card_stats = SMC_GET_COUNTER(lp);
  1058. SMC_SELECT_BANK(lp, 2);
  1059. /* single collisions */
  1060. dev->stats.collisions += card_stats & 0xF;
  1061. card_stats >>= 4;
  1062. /* multiple collisions */
  1063. dev->stats.collisions += card_stats & 0xF;
  1064. } else if (status & IM_RX_OVRN_INT) {
  1065. DBG(1, dev, "RX overrun (EPH_ST 0x%04x)\n",
  1066. ({ int eph_st; SMC_SELECT_BANK(lp, 0);
  1067. eph_st = SMC_GET_EPH_STATUS(lp);
  1068. SMC_SELECT_BANK(lp, 2); eph_st; }));
  1069. SMC_ACK_INT(lp, IM_RX_OVRN_INT);
  1070. dev->stats.rx_errors++;
  1071. dev->stats.rx_fifo_errors++;
  1072. } else if (status & IM_EPH_INT) {
  1073. smc_eph_interrupt(dev);
  1074. } else if (status & IM_MDINT) {
  1075. SMC_ACK_INT(lp, IM_MDINT);
  1076. smc_phy_interrupt(dev);
  1077. } else if (status & IM_ERCV_INT) {
  1078. SMC_ACK_INT(lp, IM_ERCV_INT);
  1079. PRINTK(dev, "UNSUPPORTED: ERCV INTERRUPT\n");
  1080. }
  1081. } while (--timeout);
  1082. /* restore register states */
  1083. SMC_SET_PTR(lp, saved_pointer);
  1084. SMC_SET_INT_MASK(lp, mask);
  1085. spin_unlock(&lp->lock);
  1086. #ifndef CONFIG_NET_POLL_CONTROLLER
  1087. if (timeout == MAX_IRQ_LOOPS)
  1088. PRINTK(dev, "spurious interrupt (mask = 0x%02x)\n",
  1089. mask);
  1090. #endif
  1091. DBG(3, dev, "Interrupt done (%d loops)\n",
  1092. MAX_IRQ_LOOPS - timeout);
  1093. /*
  1094. * We return IRQ_HANDLED unconditionally here even if there was
  1095. * nothing to do. There is a possibility that a packet might
  1096. * get enqueued into the chip right after TX_EMPTY_INT is raised
  1097. * but just before the CPU acknowledges the IRQ.
  1098. * Better take an unneeded IRQ in some occasions than complexifying
  1099. * the code for all cases.
  1100. */
  1101. return IRQ_HANDLED;
  1102. }
  1103. #ifdef CONFIG_NET_POLL_CONTROLLER
  1104. /*
  1105. * Polling receive - used by netconsole and other diagnostic tools
  1106. * to allow network i/o with interrupts disabled.
  1107. */
  1108. static void smc_poll_controller(struct net_device *dev)
  1109. {
  1110. disable_irq(dev->irq);
  1111. smc_interrupt(dev->irq, dev);
  1112. enable_irq(dev->irq);
  1113. }
  1114. #endif
  1115. /* Our watchdog timed out. Called by the networking layer */
  1116. static void smc_timeout(struct net_device *dev, unsigned int txqueue)
  1117. {
  1118. struct smc_local *lp = netdev_priv(dev);
  1119. void __iomem *ioaddr = lp->base;
  1120. int status, mask, eph_st, meminfo, fifo;
  1121. DBG(2, dev, "%s\n", __func__);
  1122. spin_lock_irq(&lp->lock);
  1123. status = SMC_GET_INT(lp);
  1124. mask = SMC_GET_INT_MASK(lp);
  1125. fifo = SMC_GET_FIFO(lp);
  1126. SMC_SELECT_BANK(lp, 0);
  1127. eph_st = SMC_GET_EPH_STATUS(lp);
  1128. meminfo = SMC_GET_MIR(lp);
  1129. SMC_SELECT_BANK(lp, 2);
  1130. spin_unlock_irq(&lp->lock);
  1131. PRINTK(dev, "TX timeout (INT 0x%02x INTMASK 0x%02x MEM 0x%04x FIFO 0x%04x EPH_ST 0x%04x)\n",
  1132. status, mask, meminfo, fifo, eph_st);
  1133. smc_reset(dev);
  1134. smc_enable(dev);
  1135. /*
  1136. * Reconfiguring the PHY doesn't seem like a bad idea here, but
  1137. * smc_phy_configure() calls msleep() which calls schedule_timeout()
  1138. * which calls schedule(). Hence we use a work queue.
  1139. */
  1140. if (lp->phy_type != 0)
  1141. schedule_work(&lp->phy_configure);
  1142. /* We can accept TX packets again */
  1143. netif_trans_update(dev); /* prevent tx timeout */
  1144. netif_wake_queue(dev);
  1145. }
  1146. /*
  1147. * This routine will, depending on the values passed to it,
  1148. * either make it accept multicast packets, go into
  1149. * promiscuous mode (for TCPDUMP and cousins) or accept
  1150. * a select set of multicast packets
  1151. */
  1152. static void smc_set_multicast_list(struct net_device *dev)
  1153. {
  1154. struct smc_local *lp = netdev_priv(dev);
  1155. void __iomem *ioaddr = lp->base;
  1156. unsigned char multicast_table[8];
  1157. int update_multicast = 0;
  1158. DBG(2, dev, "%s\n", __func__);
  1159. if (dev->flags & IFF_PROMISC) {
  1160. DBG(2, dev, "RCR_PRMS\n");
  1161. lp->rcr_cur_mode |= RCR_PRMS;
  1162. }
  1163. /* BUG? I never disable promiscuous mode if multicasting was turned on.
  1164. Now, I turn off promiscuous mode, but I don't do anything to multicasting
  1165. when promiscuous mode is turned on.
  1166. */
  1167. /*
  1168. * Here, I am setting this to accept all multicast packets.
  1169. * I don't need to zero the multicast table, because the flag is
  1170. * checked before the table is
  1171. */
  1172. else if (dev->flags & IFF_ALLMULTI || netdev_mc_count(dev) > 16) {
  1173. DBG(2, dev, "RCR_ALMUL\n");
  1174. lp->rcr_cur_mode |= RCR_ALMUL;
  1175. }
  1176. /*
  1177. * This sets the internal hardware table to filter out unwanted
  1178. * multicast packets before they take up memory.
  1179. *
  1180. * The SMC chip uses a hash table where the high 6 bits of the CRC of
  1181. * address are the offset into the table. If that bit is 1, then the
  1182. * multicast packet is accepted. Otherwise, it's dropped silently.
  1183. *
  1184. * To use the 6 bits as an offset into the table, the high 3 bits are
  1185. * the number of the 8 bit register, while the low 3 bits are the bit
  1186. * within that register.
  1187. */
  1188. else if (!netdev_mc_empty(dev)) {
  1189. struct netdev_hw_addr *ha;
  1190. /* table for flipping the order of 3 bits */
  1191. static const unsigned char invert3[] = {0, 4, 2, 6, 1, 5, 3, 7};
  1192. /* start with a table of all zeros: reject all */
  1193. memset(multicast_table, 0, sizeof(multicast_table));
  1194. netdev_for_each_mc_addr(ha, dev) {
  1195. int position;
  1196. /* only use the low order bits */
  1197. position = crc32_le(~0, ha->addr, 6) & 0x3f;
  1198. /* do some messy swapping to put the bit in the right spot */
  1199. multicast_table[invert3[position&7]] |=
  1200. (1<<invert3[(position>>3)&7]);
  1201. }
  1202. /* be sure I get rid of flags I might have set */
  1203. lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
  1204. /* now, the table can be loaded into the chipset */
  1205. update_multicast = 1;
  1206. } else {
  1207. DBG(2, dev, "~(RCR_PRMS|RCR_ALMUL)\n");
  1208. lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
  1209. /*
  1210. * since I'm disabling all multicast entirely, I need to
  1211. * clear the multicast list
  1212. */
  1213. memset(multicast_table, 0, sizeof(multicast_table));
  1214. update_multicast = 1;
  1215. }
  1216. spin_lock_irq(&lp->lock);
  1217. SMC_SELECT_BANK(lp, 0);
  1218. SMC_SET_RCR(lp, lp->rcr_cur_mode);
  1219. if (update_multicast) {
  1220. SMC_SELECT_BANK(lp, 3);
  1221. SMC_SET_MCAST(lp, multicast_table);
  1222. }
  1223. SMC_SELECT_BANK(lp, 2);
  1224. spin_unlock_irq(&lp->lock);
  1225. }
  1226. /*
  1227. * Open and Initialize the board
  1228. *
  1229. * Set up everything, reset the card, etc..
  1230. */
  1231. static int
  1232. smc_open(struct net_device *dev)
  1233. {
  1234. struct smc_local *lp = netdev_priv(dev);
  1235. DBG(2, dev, "%s\n", __func__);
  1236. /* Setup the default Register Modes */
  1237. lp->tcr_cur_mode = TCR_DEFAULT;
  1238. lp->rcr_cur_mode = RCR_DEFAULT;
  1239. lp->rpc_cur_mode = RPC_DEFAULT |
  1240. lp->cfg.leda << RPC_LSXA_SHFT |
  1241. lp->cfg.ledb << RPC_LSXB_SHFT;
  1242. /*
  1243. * If we are not using a MII interface, we need to
  1244. * monitor our own carrier signal to detect faults.
  1245. */
  1246. if (lp->phy_type == 0)
  1247. lp->tcr_cur_mode |= TCR_MON_CSN;
  1248. /* reset the hardware */
  1249. smc_reset(dev);
  1250. smc_enable(dev);
  1251. /* Configure the PHY, initialize the link state */
  1252. if (lp->phy_type != 0)
  1253. smc_phy_configure(&lp->phy_configure);
  1254. else {
  1255. spin_lock_irq(&lp->lock);
  1256. smc_10bt_check_media(dev, 1);
  1257. spin_unlock_irq(&lp->lock);
  1258. }
  1259. netif_start_queue(dev);
  1260. return 0;
  1261. }
  1262. /*
  1263. * smc_close
  1264. *
  1265. * this makes the board clean up everything that it can
  1266. * and not talk to the outside world. Caused by
  1267. * an 'ifconfig ethX down'
  1268. */
  1269. static int smc_close(struct net_device *dev)
  1270. {
  1271. struct smc_local *lp = netdev_priv(dev);
  1272. DBG(2, dev, "%s\n", __func__);
  1273. netif_stop_queue(dev);
  1274. netif_carrier_off(dev);
  1275. /* clear everything */
  1276. smc_shutdown(dev);
  1277. tasklet_kill(&lp->tx_task);
  1278. smc_phy_powerdown(dev);
  1279. return 0;
  1280. }
  1281. /*
  1282. * Ethtool support
  1283. */
  1284. static int
  1285. smc_ethtool_get_link_ksettings(struct net_device *dev,
  1286. struct ethtool_link_ksettings *cmd)
  1287. {
  1288. struct smc_local *lp = netdev_priv(dev);
  1289. if (lp->phy_type != 0) {
  1290. spin_lock_irq(&lp->lock);
  1291. mii_ethtool_get_link_ksettings(&lp->mii, cmd);
  1292. spin_unlock_irq(&lp->lock);
  1293. } else {
  1294. u32 supported = SUPPORTED_10baseT_Half |
  1295. SUPPORTED_10baseT_Full |
  1296. SUPPORTED_TP | SUPPORTED_AUI;
  1297. if (lp->ctl_rspeed == 10)
  1298. cmd->base.speed = SPEED_10;
  1299. else if (lp->ctl_rspeed == 100)
  1300. cmd->base.speed = SPEED_100;
  1301. cmd->base.autoneg = AUTONEG_DISABLE;
  1302. cmd->base.port = 0;
  1303. cmd->base.duplex = lp->tcr_cur_mode & TCR_SWFDUP ?
  1304. DUPLEX_FULL : DUPLEX_HALF;
  1305. ethtool_convert_legacy_u32_to_link_mode(
  1306. cmd->link_modes.supported, supported);
  1307. }
  1308. return 0;
  1309. }
  1310. static int
  1311. smc_ethtool_set_link_ksettings(struct net_device *dev,
  1312. const struct ethtool_link_ksettings *cmd)
  1313. {
  1314. struct smc_local *lp = netdev_priv(dev);
  1315. int ret;
  1316. if (lp->phy_type != 0) {
  1317. spin_lock_irq(&lp->lock);
  1318. ret = mii_ethtool_set_link_ksettings(&lp->mii, cmd);
  1319. spin_unlock_irq(&lp->lock);
  1320. } else {
  1321. if (cmd->base.autoneg != AUTONEG_DISABLE ||
  1322. cmd->base.speed != SPEED_10 ||
  1323. (cmd->base.duplex != DUPLEX_HALF &&
  1324. cmd->base.duplex != DUPLEX_FULL) ||
  1325. (cmd->base.port != PORT_TP && cmd->base.port != PORT_AUI))
  1326. return -EINVAL;
  1327. lp->ctl_rfduplx = cmd->base.duplex == DUPLEX_FULL;
  1328. ret = 0;
  1329. }
  1330. return ret;
  1331. }
  1332. static void
  1333. smc_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1334. {
  1335. strscpy(info->driver, CARDNAME, sizeof(info->driver));
  1336. strscpy(info->version, version, sizeof(info->version));
  1337. strscpy(info->bus_info, dev_name(dev->dev.parent),
  1338. sizeof(info->bus_info));
  1339. }
  1340. static int smc_ethtool_nwayreset(struct net_device *dev)
  1341. {
  1342. struct smc_local *lp = netdev_priv(dev);
  1343. int ret = -EINVAL;
  1344. if (lp->phy_type != 0) {
  1345. spin_lock_irq(&lp->lock);
  1346. ret = mii_nway_restart(&lp->mii);
  1347. spin_unlock_irq(&lp->lock);
  1348. }
  1349. return ret;
  1350. }
  1351. static u32 smc_ethtool_getmsglevel(struct net_device *dev)
  1352. {
  1353. struct smc_local *lp = netdev_priv(dev);
  1354. return lp->msg_enable;
  1355. }
  1356. static void smc_ethtool_setmsglevel(struct net_device *dev, u32 level)
  1357. {
  1358. struct smc_local *lp = netdev_priv(dev);
  1359. lp->msg_enable = level;
  1360. }
  1361. static int smc_write_eeprom_word(struct net_device *dev, u16 addr, u16 word)
  1362. {
  1363. u16 ctl;
  1364. struct smc_local *lp = netdev_priv(dev);
  1365. void __iomem *ioaddr = lp->base;
  1366. spin_lock_irq(&lp->lock);
  1367. /* load word into GP register */
  1368. SMC_SELECT_BANK(lp, 1);
  1369. SMC_SET_GP(lp, word);
  1370. /* set the address to put the data in EEPROM */
  1371. SMC_SELECT_BANK(lp, 2);
  1372. SMC_SET_PTR(lp, addr);
  1373. /* tell it to write */
  1374. SMC_SELECT_BANK(lp, 1);
  1375. ctl = SMC_GET_CTL(lp);
  1376. SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_STORE));
  1377. /* wait for it to finish */
  1378. do {
  1379. udelay(1);
  1380. } while (SMC_GET_CTL(lp) & CTL_STORE);
  1381. /* clean up */
  1382. SMC_SET_CTL(lp, ctl);
  1383. SMC_SELECT_BANK(lp, 2);
  1384. spin_unlock_irq(&lp->lock);
  1385. return 0;
  1386. }
  1387. static int smc_read_eeprom_word(struct net_device *dev, u16 addr, u16 *word)
  1388. {
  1389. u16 ctl;
  1390. struct smc_local *lp = netdev_priv(dev);
  1391. void __iomem *ioaddr = lp->base;
  1392. spin_lock_irq(&lp->lock);
  1393. /* set the EEPROM address to get the data from */
  1394. SMC_SELECT_BANK(lp, 2);
  1395. SMC_SET_PTR(lp, addr | PTR_READ);
  1396. /* tell it to load */
  1397. SMC_SELECT_BANK(lp, 1);
  1398. SMC_SET_GP(lp, 0xffff); /* init to known */
  1399. ctl = SMC_GET_CTL(lp);
  1400. SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_RELOAD));
  1401. /* wait for it to finish */
  1402. do {
  1403. udelay(1);
  1404. } while (SMC_GET_CTL(lp) & CTL_RELOAD);
  1405. /* read word from GP register */
  1406. *word = SMC_GET_GP(lp);
  1407. /* clean up */
  1408. SMC_SET_CTL(lp, ctl);
  1409. SMC_SELECT_BANK(lp, 2);
  1410. spin_unlock_irq(&lp->lock);
  1411. return 0;
  1412. }
  1413. static int smc_ethtool_geteeprom_len(struct net_device *dev)
  1414. {
  1415. return 0x23 * 2;
  1416. }
  1417. static int smc_ethtool_geteeprom(struct net_device *dev,
  1418. struct ethtool_eeprom *eeprom, u8 *data)
  1419. {
  1420. int i;
  1421. int imax;
  1422. DBG(1, dev, "Reading %d bytes at %d(0x%x)\n",
  1423. eeprom->len, eeprom->offset, eeprom->offset);
  1424. imax = smc_ethtool_geteeprom_len(dev);
  1425. for (i = 0; i < eeprom->len; i += 2) {
  1426. int ret;
  1427. u16 wbuf;
  1428. int offset = i + eeprom->offset;
  1429. if (offset > imax)
  1430. break;
  1431. ret = smc_read_eeprom_word(dev, offset >> 1, &wbuf);
  1432. if (ret != 0)
  1433. return ret;
  1434. DBG(2, dev, "Read 0x%x from 0x%x\n", wbuf, offset >> 1);
  1435. data[i] = (wbuf >> 8) & 0xff;
  1436. data[i+1] = wbuf & 0xff;
  1437. }
  1438. return 0;
  1439. }
  1440. static int smc_ethtool_seteeprom(struct net_device *dev,
  1441. struct ethtool_eeprom *eeprom, u8 *data)
  1442. {
  1443. int i;
  1444. int imax;
  1445. DBG(1, dev, "Writing %d bytes to %d(0x%x)\n",
  1446. eeprom->len, eeprom->offset, eeprom->offset);
  1447. imax = smc_ethtool_geteeprom_len(dev);
  1448. for (i = 0; i < eeprom->len; i += 2) {
  1449. int ret;
  1450. u16 wbuf;
  1451. int offset = i + eeprom->offset;
  1452. if (offset > imax)
  1453. break;
  1454. wbuf = (data[i] << 8) | data[i + 1];
  1455. DBG(2, dev, "Writing 0x%x to 0x%x\n", wbuf, offset >> 1);
  1456. ret = smc_write_eeprom_word(dev, offset >> 1, wbuf);
  1457. if (ret != 0)
  1458. return ret;
  1459. }
  1460. return 0;
  1461. }
  1462. static const struct ethtool_ops smc_ethtool_ops = {
  1463. .get_drvinfo = smc_ethtool_getdrvinfo,
  1464. .get_msglevel = smc_ethtool_getmsglevel,
  1465. .set_msglevel = smc_ethtool_setmsglevel,
  1466. .nway_reset = smc_ethtool_nwayreset,
  1467. .get_link = ethtool_op_get_link,
  1468. .get_eeprom_len = smc_ethtool_geteeprom_len,
  1469. .get_eeprom = smc_ethtool_geteeprom,
  1470. .set_eeprom = smc_ethtool_seteeprom,
  1471. .get_link_ksettings = smc_ethtool_get_link_ksettings,
  1472. .set_link_ksettings = smc_ethtool_set_link_ksettings,
  1473. };
  1474. static const struct net_device_ops smc_netdev_ops = {
  1475. .ndo_open = smc_open,
  1476. .ndo_stop = smc_close,
  1477. .ndo_start_xmit = smc_hard_start_xmit,
  1478. .ndo_tx_timeout = smc_timeout,
  1479. .ndo_set_rx_mode = smc_set_multicast_list,
  1480. .ndo_validate_addr = eth_validate_addr,
  1481. .ndo_set_mac_address = eth_mac_addr,
  1482. #ifdef CONFIG_NET_POLL_CONTROLLER
  1483. .ndo_poll_controller = smc_poll_controller,
  1484. #endif
  1485. };
  1486. /*
  1487. * smc_findirq
  1488. *
  1489. * This routine has a simple purpose -- make the SMC chip generate an
  1490. * interrupt, so an auto-detect routine can detect it, and find the IRQ,
  1491. */
  1492. /*
  1493. * does this still work?
  1494. *
  1495. * I just deleted auto_irq.c, since it was never built...
  1496. * --jgarzik
  1497. */
  1498. static int smc_findirq(struct smc_local *lp)
  1499. {
  1500. void __iomem *ioaddr = lp->base;
  1501. int timeout = 20;
  1502. unsigned long cookie;
  1503. DBG(2, lp->dev, "%s: %s\n", CARDNAME, __func__);
  1504. cookie = probe_irq_on();
  1505. /*
  1506. * What I try to do here is trigger an ALLOC_INT. This is done
  1507. * by allocating a small chunk of memory, which will give an interrupt
  1508. * when done.
  1509. */
  1510. /* enable ALLOCation interrupts ONLY */
  1511. SMC_SELECT_BANK(lp, 2);
  1512. SMC_SET_INT_MASK(lp, IM_ALLOC_INT);
  1513. /*
  1514. * Allocate 512 bytes of memory. Note that the chip was just
  1515. * reset so all the memory is available
  1516. */
  1517. SMC_SET_MMU_CMD(lp, MC_ALLOC | 1);
  1518. /*
  1519. * Wait until positive that the interrupt has been generated
  1520. */
  1521. do {
  1522. int int_status;
  1523. udelay(10);
  1524. int_status = SMC_GET_INT(lp);
  1525. if (int_status & IM_ALLOC_INT)
  1526. break; /* got the interrupt */
  1527. } while (--timeout);
  1528. /*
  1529. * there is really nothing that I can do here if timeout fails,
  1530. * as autoirq_report will return a 0 anyway, which is what I
  1531. * want in this case. Plus, the clean up is needed in both
  1532. * cases.
  1533. */
  1534. /* and disable all interrupts again */
  1535. SMC_SET_INT_MASK(lp, 0);
  1536. /* and return what I found */
  1537. return probe_irq_off(cookie);
  1538. }
  1539. /*
  1540. * Function: smc_probe(unsigned long ioaddr)
  1541. *
  1542. * Purpose:
  1543. * Tests to see if a given ioaddr points to an SMC91x chip.
  1544. * Returns a 0 on success
  1545. *
  1546. * Algorithm:
  1547. * (1) see if the high byte of BANK_SELECT is 0x33
  1548. * (2) compare the ioaddr with the base register's address
  1549. * (3) see if I recognize the chip ID in the appropriate register
  1550. *
  1551. * Here I do typical initialization tasks.
  1552. *
  1553. * o Initialize the structure if needed
  1554. * o print out my vanity message if not done so already
  1555. * o print out what type of hardware is detected
  1556. * o print out the ethernet address
  1557. * o find the IRQ
  1558. * o set up my private data
  1559. * o configure the dev structure with my subroutines
  1560. * o actually GRAB the irq.
  1561. * o GRAB the region
  1562. */
  1563. static int smc_probe(struct net_device *dev, void __iomem *ioaddr,
  1564. unsigned long irq_flags)
  1565. {
  1566. struct smc_local *lp = netdev_priv(dev);
  1567. int retval;
  1568. unsigned int val, revision_register;
  1569. const char *version_string;
  1570. u8 addr[ETH_ALEN];
  1571. DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
  1572. /* First, see if the high byte is 0x33 */
  1573. val = SMC_CURRENT_BANK(lp);
  1574. DBG(2, dev, "%s: bank signature probe returned 0x%04x\n",
  1575. CARDNAME, val);
  1576. if ((val & 0xFF00) != 0x3300) {
  1577. if ((val & 0xFF) == 0x33) {
  1578. netdev_warn(dev,
  1579. "%s: Detected possible byte-swapped interface at IOADDR %p\n",
  1580. CARDNAME, ioaddr);
  1581. }
  1582. retval = -ENODEV;
  1583. goto err_out;
  1584. }
  1585. /*
  1586. * The above MIGHT indicate a device, but I need to write to
  1587. * further test this.
  1588. */
  1589. SMC_SELECT_BANK(lp, 0);
  1590. val = SMC_CURRENT_BANK(lp);
  1591. if ((val & 0xFF00) != 0x3300) {
  1592. retval = -ENODEV;
  1593. goto err_out;
  1594. }
  1595. /*
  1596. * well, we've already written once, so hopefully another
  1597. * time won't hurt. This time, I need to switch the bank
  1598. * register to bank 1, so I can access the base address
  1599. * register
  1600. */
  1601. SMC_SELECT_BANK(lp, 1);
  1602. val = SMC_GET_BASE(lp);
  1603. val = ((val & 0x1F00) >> 3) << SMC_IO_SHIFT;
  1604. if (((unsigned long)ioaddr & (0x3e0 << SMC_IO_SHIFT)) != val) {
  1605. netdev_warn(dev, "%s: IOADDR %p doesn't match configuration (%x).\n",
  1606. CARDNAME, ioaddr, val);
  1607. }
  1608. /*
  1609. * check if the revision register is something that I
  1610. * recognize. These might need to be added to later,
  1611. * as future revisions could be added.
  1612. */
  1613. SMC_SELECT_BANK(lp, 3);
  1614. revision_register = SMC_GET_REV(lp);
  1615. DBG(2, dev, "%s: revision = 0x%04x\n", CARDNAME, revision_register);
  1616. version_string = chip_ids[ (revision_register >> 4) & 0xF];
  1617. if (!version_string || (revision_register & 0xff00) != 0x3300) {
  1618. /* I don't recognize this chip, so... */
  1619. netdev_warn(dev, "%s: IO %p: Unrecognized revision register 0x%04x, Contact author.\n",
  1620. CARDNAME, ioaddr, revision_register);
  1621. retval = -ENODEV;
  1622. goto err_out;
  1623. }
  1624. /* At this point I'll assume that the chip is an SMC91x. */
  1625. pr_info_once("%s\n", version);
  1626. /* fill in some of the fields */
  1627. dev->base_addr = (unsigned long)ioaddr;
  1628. lp->base = ioaddr;
  1629. lp->version = revision_register & 0xff;
  1630. spin_lock_init(&lp->lock);
  1631. /* Get the MAC address */
  1632. SMC_SELECT_BANK(lp, 1);
  1633. SMC_GET_MAC_ADDR(lp, addr);
  1634. eth_hw_addr_set(dev, addr);
  1635. /* now, reset the chip, and put it into a known state */
  1636. smc_reset(dev);
  1637. /*
  1638. * If dev->irq is 0, then the device has to be banged on to see
  1639. * what the IRQ is.
  1640. *
  1641. * This banging doesn't always detect the IRQ, for unknown reasons.
  1642. * a workaround is to reset the chip and try again.
  1643. *
  1644. * Interestingly, the DOS packet driver *SETS* the IRQ on the card to
  1645. * be what is requested on the command line. I don't do that, mostly
  1646. * because the card that I have uses a non-standard method of accessing
  1647. * the IRQs, and because this _should_ work in most configurations.
  1648. *
  1649. * Specifying an IRQ is done with the assumption that the user knows
  1650. * what (s)he is doing. No checking is done!!!!
  1651. */
  1652. if (dev->irq < 1) {
  1653. int trials;
  1654. trials = 3;
  1655. while (trials--) {
  1656. dev->irq = smc_findirq(lp);
  1657. if (dev->irq)
  1658. break;
  1659. /* kick the card and try again */
  1660. smc_reset(dev);
  1661. }
  1662. }
  1663. if (dev->irq == 0) {
  1664. netdev_warn(dev, "Couldn't autodetect your IRQ. Use irq=xx.\n");
  1665. retval = -ENODEV;
  1666. goto err_out;
  1667. }
  1668. dev->irq = irq_canonicalize(dev->irq);
  1669. dev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1670. dev->netdev_ops = &smc_netdev_ops;
  1671. dev->ethtool_ops = &smc_ethtool_ops;
  1672. tasklet_setup(&lp->tx_task, smc_hardware_send_pkt);
  1673. INIT_WORK(&lp->phy_configure, smc_phy_configure);
  1674. lp->dev = dev;
  1675. lp->mii.phy_id_mask = 0x1f;
  1676. lp->mii.reg_num_mask = 0x1f;
  1677. lp->mii.force_media = 0;
  1678. lp->mii.full_duplex = 0;
  1679. lp->mii.dev = dev;
  1680. lp->mii.mdio_read = smc_phy_read;
  1681. lp->mii.mdio_write = smc_phy_write;
  1682. /*
  1683. * Locate the phy, if any.
  1684. */
  1685. if (lp->version >= (CHIP_91100 << 4))
  1686. smc_phy_detect(dev);
  1687. /* then shut everything down to save power */
  1688. smc_shutdown(dev);
  1689. smc_phy_powerdown(dev);
  1690. /* Set default parameters */
  1691. lp->msg_enable = NETIF_MSG_LINK;
  1692. lp->ctl_rfduplx = 0;
  1693. lp->ctl_rspeed = 10;
  1694. if (lp->version >= (CHIP_91100 << 4)) {
  1695. lp->ctl_rfduplx = 1;
  1696. lp->ctl_rspeed = 100;
  1697. }
  1698. /* Grab the IRQ */
  1699. retval = request_irq(dev->irq, smc_interrupt, irq_flags, dev->name, dev);
  1700. if (retval)
  1701. goto err_out;
  1702. #ifdef CONFIG_ARCH_PXA
  1703. # ifdef SMC_USE_PXA_DMA
  1704. lp->cfg.flags |= SMC91X_USE_DMA;
  1705. # endif
  1706. if (lp->cfg.flags & SMC91X_USE_DMA) {
  1707. dma_cap_mask_t mask;
  1708. dma_cap_zero(mask);
  1709. dma_cap_set(DMA_SLAVE, mask);
  1710. lp->dma_chan = dma_request_channel(mask, NULL, NULL);
  1711. }
  1712. #endif
  1713. retval = register_netdev(dev);
  1714. if (retval == 0) {
  1715. /* now, print out the card info, in a short format.. */
  1716. netdev_info(dev, "%s (rev %d) at %p IRQ %d",
  1717. version_string, revision_register & 0x0f,
  1718. lp->base, dev->irq);
  1719. if (lp->dma_chan)
  1720. pr_cont(" DMA %p", lp->dma_chan);
  1721. pr_cont("%s%s\n",
  1722. lp->cfg.flags & SMC91X_NOWAIT ? " [nowait]" : "",
  1723. THROTTLE_TX_PKTS ? " [throttle_tx]" : "");
  1724. if (!is_valid_ether_addr(dev->dev_addr)) {
  1725. netdev_warn(dev, "Invalid ethernet MAC address. Please set using ifconfig\n");
  1726. } else {
  1727. /* Print the Ethernet address */
  1728. netdev_info(dev, "Ethernet addr: %pM\n",
  1729. dev->dev_addr);
  1730. }
  1731. if (lp->phy_type == 0) {
  1732. PRINTK(dev, "No PHY found\n");
  1733. } else if ((lp->phy_type & 0xfffffff0) == 0x0016f840) {
  1734. PRINTK(dev, "PHY LAN83C183 (LAN91C111 Internal)\n");
  1735. } else if ((lp->phy_type & 0xfffffff0) == 0x02821c50) {
  1736. PRINTK(dev, "PHY LAN83C180\n");
  1737. }
  1738. }
  1739. err_out:
  1740. #ifdef CONFIG_ARCH_PXA
  1741. if (retval && lp->dma_chan)
  1742. dma_release_channel(lp->dma_chan);
  1743. #endif
  1744. return retval;
  1745. }
  1746. static int smc_enable_device(struct platform_device *pdev)
  1747. {
  1748. struct net_device *ndev = platform_get_drvdata(pdev);
  1749. struct smc_local *lp = netdev_priv(ndev);
  1750. unsigned long flags;
  1751. unsigned char ecor, ecsr;
  1752. void __iomem *addr;
  1753. struct resource * res;
  1754. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
  1755. if (!res)
  1756. return 0;
  1757. /*
  1758. * Map the attribute space. This is overkill, but clean.
  1759. */
  1760. addr = ioremap(res->start, ATTRIB_SIZE);
  1761. if (!addr)
  1762. return -ENOMEM;
  1763. /*
  1764. * Reset the device. We must disable IRQs around this
  1765. * since a reset causes the IRQ line become active.
  1766. */
  1767. local_irq_save(flags);
  1768. ecor = readb(addr + (ECOR << SMC_IO_SHIFT)) & ~ECOR_RESET;
  1769. writeb(ecor | ECOR_RESET, addr + (ECOR << SMC_IO_SHIFT));
  1770. readb(addr + (ECOR << SMC_IO_SHIFT));
  1771. /*
  1772. * Wait 100us for the chip to reset.
  1773. */
  1774. udelay(100);
  1775. /*
  1776. * The device will ignore all writes to the enable bit while
  1777. * reset is asserted, even if the reset bit is cleared in the
  1778. * same write. Must clear reset first, then enable the device.
  1779. */
  1780. writeb(ecor, addr + (ECOR << SMC_IO_SHIFT));
  1781. writeb(ecor | ECOR_ENABLE, addr + (ECOR << SMC_IO_SHIFT));
  1782. /*
  1783. * Set the appropriate byte/word mode.
  1784. */
  1785. ecsr = readb(addr + (ECSR << SMC_IO_SHIFT)) & ~ECSR_IOIS8;
  1786. if (!SMC_16BIT(lp))
  1787. ecsr |= ECSR_IOIS8;
  1788. writeb(ecsr, addr + (ECSR << SMC_IO_SHIFT));
  1789. local_irq_restore(flags);
  1790. iounmap(addr);
  1791. /*
  1792. * Wait for the chip to wake up. We could poll the control
  1793. * register in the main register space, but that isn't mapped
  1794. * yet. We know this is going to take 750us.
  1795. */
  1796. msleep(1);
  1797. return 0;
  1798. }
  1799. static int smc_request_attrib(struct platform_device *pdev,
  1800. struct net_device *ndev)
  1801. {
  1802. struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
  1803. struct smc_local *lp __maybe_unused = netdev_priv(ndev);
  1804. if (!res)
  1805. return 0;
  1806. if (!request_mem_region(res->start, ATTRIB_SIZE, CARDNAME))
  1807. return -EBUSY;
  1808. return 0;
  1809. }
  1810. static void smc_release_attrib(struct platform_device *pdev,
  1811. struct net_device *ndev)
  1812. {
  1813. struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
  1814. struct smc_local *lp __maybe_unused = netdev_priv(ndev);
  1815. if (res)
  1816. release_mem_region(res->start, ATTRIB_SIZE);
  1817. }
  1818. static inline void smc_request_datacs(struct platform_device *pdev, struct net_device *ndev)
  1819. {
  1820. if (SMC_CAN_USE_DATACS) {
  1821. struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
  1822. struct smc_local *lp = netdev_priv(ndev);
  1823. if (!res)
  1824. return;
  1825. if(!request_mem_region(res->start, SMC_DATA_EXTENT, CARDNAME)) {
  1826. netdev_info(ndev, "%s: failed to request datacs memory region.\n",
  1827. CARDNAME);
  1828. return;
  1829. }
  1830. lp->datacs = ioremap(res->start, SMC_DATA_EXTENT);
  1831. }
  1832. }
  1833. static void smc_release_datacs(struct platform_device *pdev, struct net_device *ndev)
  1834. {
  1835. if (SMC_CAN_USE_DATACS) {
  1836. struct smc_local *lp = netdev_priv(ndev);
  1837. struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
  1838. if (lp->datacs)
  1839. iounmap(lp->datacs);
  1840. lp->datacs = NULL;
  1841. if (res)
  1842. release_mem_region(res->start, SMC_DATA_EXTENT);
  1843. }
  1844. }
  1845. static const struct acpi_device_id smc91x_acpi_match[] = {
  1846. { "LNRO0003", 0 },
  1847. { }
  1848. };
  1849. MODULE_DEVICE_TABLE(acpi, smc91x_acpi_match);
  1850. #if IS_BUILTIN(CONFIG_OF)
  1851. static const struct of_device_id smc91x_match[] = {
  1852. { .compatible = "smsc,lan91c94", },
  1853. { .compatible = "smsc,lan91c111", },
  1854. {},
  1855. };
  1856. MODULE_DEVICE_TABLE(of, smc91x_match);
  1857. /**
  1858. * try_toggle_control_gpio - configure a gpio if it exists
  1859. * @dev: net device
  1860. * @desc: where to store the GPIO descriptor, if it exists
  1861. * @name: name of the GPIO in DT
  1862. * @index: index of the GPIO in DT
  1863. * @value: set the GPIO to this value
  1864. * @nsdelay: delay before setting the GPIO
  1865. */
  1866. static int try_toggle_control_gpio(struct device *dev,
  1867. struct gpio_desc **desc,
  1868. const char *name, int index,
  1869. int value, unsigned int nsdelay)
  1870. {
  1871. struct gpio_desc *gpio;
  1872. enum gpiod_flags flags = value ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
  1873. gpio = devm_gpiod_get_index_optional(dev, name, index, flags);
  1874. if (IS_ERR(gpio))
  1875. return PTR_ERR(gpio);
  1876. if (gpio) {
  1877. if (nsdelay)
  1878. usleep_range(nsdelay, 2 * nsdelay);
  1879. gpiod_set_value_cansleep(gpio, value);
  1880. }
  1881. *desc = gpio;
  1882. return 0;
  1883. }
  1884. #endif
  1885. /*
  1886. * smc_init(void)
  1887. * Input parameters:
  1888. * dev->base_addr == 0, try to find all possible locations
  1889. * dev->base_addr > 0x1ff, this is the address to check
  1890. * dev->base_addr == <anything else>, return failure code
  1891. *
  1892. * Output:
  1893. * 0 --> there is a device
  1894. * anything else, error
  1895. */
  1896. static int smc_drv_probe(struct platform_device *pdev)
  1897. {
  1898. struct smc91x_platdata *pd = dev_get_platdata(&pdev->dev);
  1899. const struct of_device_id *match = NULL;
  1900. struct smc_local *lp;
  1901. struct net_device *ndev;
  1902. struct resource *res;
  1903. unsigned int __iomem *addr;
  1904. unsigned long irq_flags = SMC_IRQ_FLAGS;
  1905. unsigned long irq_resflags;
  1906. int ret;
  1907. ndev = alloc_etherdev(sizeof(struct smc_local));
  1908. if (!ndev) {
  1909. ret = -ENOMEM;
  1910. goto out;
  1911. }
  1912. SET_NETDEV_DEV(ndev, &pdev->dev);
  1913. /* get configuration from platform data, only allow use of
  1914. * bus width if both SMC_CAN_USE_xxx and SMC91X_USE_xxx are set.
  1915. */
  1916. lp = netdev_priv(ndev);
  1917. lp->cfg.flags = 0;
  1918. if (pd) {
  1919. memcpy(&lp->cfg, pd, sizeof(lp->cfg));
  1920. lp->io_shift = SMC91X_IO_SHIFT(lp->cfg.flags);
  1921. if (!SMC_8BIT(lp) && !SMC_16BIT(lp)) {
  1922. dev_err(&pdev->dev,
  1923. "at least one of 8-bit or 16-bit access support is required.\n");
  1924. ret = -ENXIO;
  1925. goto out_free_netdev;
  1926. }
  1927. }
  1928. #if IS_BUILTIN(CONFIG_OF)
  1929. match = of_match_device(of_match_ptr(smc91x_match), &pdev->dev);
  1930. if (match) {
  1931. u32 val;
  1932. /* Optional pwrdwn GPIO configured? */
  1933. ret = try_toggle_control_gpio(&pdev->dev, &lp->power_gpio,
  1934. "power", 0, 0, 100);
  1935. if (ret)
  1936. goto out_free_netdev;
  1937. /*
  1938. * Optional reset GPIO configured? Minimum 100 ns reset needed
  1939. * according to LAN91C96 datasheet page 14.
  1940. */
  1941. ret = try_toggle_control_gpio(&pdev->dev, &lp->reset_gpio,
  1942. "reset", 0, 0, 100);
  1943. if (ret)
  1944. goto out_free_netdev;
  1945. /*
  1946. * Need to wait for optional EEPROM to load, max 750 us according
  1947. * to LAN91C96 datasheet page 55.
  1948. */
  1949. if (lp->reset_gpio)
  1950. usleep_range(750, 1000);
  1951. /* Combination of IO widths supported, default to 16-bit */
  1952. if (!device_property_read_u32(&pdev->dev, "reg-io-width",
  1953. &val)) {
  1954. if (val & 1)
  1955. lp->cfg.flags |= SMC91X_USE_8BIT;
  1956. if ((val == 0) || (val & 2))
  1957. lp->cfg.flags |= SMC91X_USE_16BIT;
  1958. if (val & 4)
  1959. lp->cfg.flags |= SMC91X_USE_32BIT;
  1960. } else {
  1961. lp->cfg.flags |= SMC91X_USE_16BIT;
  1962. }
  1963. if (!device_property_read_u32(&pdev->dev, "reg-shift",
  1964. &val))
  1965. lp->io_shift = val;
  1966. lp->cfg.pxa_u16_align4 =
  1967. device_property_read_bool(&pdev->dev, "pxa-u16-align4");
  1968. }
  1969. #endif
  1970. if (!pd && !match) {
  1971. lp->cfg.flags |= (SMC_CAN_USE_8BIT) ? SMC91X_USE_8BIT : 0;
  1972. lp->cfg.flags |= (SMC_CAN_USE_16BIT) ? SMC91X_USE_16BIT : 0;
  1973. lp->cfg.flags |= (SMC_CAN_USE_32BIT) ? SMC91X_USE_32BIT : 0;
  1974. lp->cfg.flags |= (nowait) ? SMC91X_NOWAIT : 0;
  1975. }
  1976. if (!lp->cfg.leda && !lp->cfg.ledb) {
  1977. lp->cfg.leda = RPC_LSA_DEFAULT;
  1978. lp->cfg.ledb = RPC_LSB_DEFAULT;
  1979. }
  1980. ndev->dma = (unsigned char)-1;
  1981. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
  1982. if (!res)
  1983. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1984. if (!res) {
  1985. ret = -ENODEV;
  1986. goto out_free_netdev;
  1987. }
  1988. if (!request_mem_region(res->start, SMC_IO_EXTENT, CARDNAME)) {
  1989. ret = -EBUSY;
  1990. goto out_free_netdev;
  1991. }
  1992. ndev->irq = platform_get_irq(pdev, 0);
  1993. if (ndev->irq < 0) {
  1994. ret = ndev->irq;
  1995. goto out_release_io;
  1996. }
  1997. /*
  1998. * If this platform does not specify any special irqflags, or if
  1999. * the resource supplies a trigger, override the irqflags with
  2000. * the trigger flags from the resource.
  2001. */
  2002. irq_resflags = irq_get_trigger_type(ndev->irq);
  2003. if (irq_flags == -1 || irq_resflags & IRQF_TRIGGER_MASK)
  2004. irq_flags = irq_resflags & IRQF_TRIGGER_MASK;
  2005. ret = smc_request_attrib(pdev, ndev);
  2006. if (ret)
  2007. goto out_release_io;
  2008. #if defined(CONFIG_ASSABET_NEPONSET)
  2009. if (machine_is_assabet() && machine_has_neponset())
  2010. neponset_ncr_set(NCR_ENET_OSC_EN);
  2011. #endif
  2012. platform_set_drvdata(pdev, ndev);
  2013. ret = smc_enable_device(pdev);
  2014. if (ret)
  2015. goto out_release_attrib;
  2016. addr = ioremap(res->start, SMC_IO_EXTENT);
  2017. if (!addr) {
  2018. ret = -ENOMEM;
  2019. goto out_release_attrib;
  2020. }
  2021. #ifdef CONFIG_ARCH_PXA
  2022. {
  2023. struct smc_local *lp = netdev_priv(ndev);
  2024. lp->device = &pdev->dev;
  2025. lp->physaddr = res->start;
  2026. }
  2027. #endif
  2028. ret = smc_probe(ndev, addr, irq_flags);
  2029. if (ret != 0)
  2030. goto out_iounmap;
  2031. smc_request_datacs(pdev, ndev);
  2032. return 0;
  2033. out_iounmap:
  2034. iounmap(addr);
  2035. out_release_attrib:
  2036. smc_release_attrib(pdev, ndev);
  2037. out_release_io:
  2038. release_mem_region(res->start, SMC_IO_EXTENT);
  2039. out_free_netdev:
  2040. free_netdev(ndev);
  2041. out:
  2042. pr_info("%s: not found (%d).\n", CARDNAME, ret);
  2043. return ret;
  2044. }
  2045. static void smc_drv_remove(struct platform_device *pdev)
  2046. {
  2047. struct net_device *ndev = platform_get_drvdata(pdev);
  2048. struct smc_local *lp = netdev_priv(ndev);
  2049. struct resource *res;
  2050. unregister_netdev(ndev);
  2051. free_irq(ndev->irq, ndev);
  2052. #ifdef CONFIG_ARCH_PXA
  2053. if (lp->dma_chan)
  2054. dma_release_channel(lp->dma_chan);
  2055. #endif
  2056. iounmap(lp->base);
  2057. smc_release_datacs(pdev,ndev);
  2058. smc_release_attrib(pdev,ndev);
  2059. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
  2060. if (!res)
  2061. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2062. release_mem_region(res->start, SMC_IO_EXTENT);
  2063. free_netdev(ndev);
  2064. }
  2065. static int smc_drv_suspend(struct device *dev)
  2066. {
  2067. struct net_device *ndev = dev_get_drvdata(dev);
  2068. if (ndev) {
  2069. if (netif_running(ndev)) {
  2070. netif_device_detach(ndev);
  2071. smc_shutdown(ndev);
  2072. smc_phy_powerdown(ndev);
  2073. }
  2074. }
  2075. return 0;
  2076. }
  2077. static int smc_drv_resume(struct device *dev)
  2078. {
  2079. struct platform_device *pdev = to_platform_device(dev);
  2080. struct net_device *ndev = platform_get_drvdata(pdev);
  2081. if (ndev) {
  2082. struct smc_local *lp = netdev_priv(ndev);
  2083. smc_enable_device(pdev);
  2084. if (netif_running(ndev)) {
  2085. smc_reset(ndev);
  2086. smc_enable(ndev);
  2087. if (lp->phy_type != 0)
  2088. smc_phy_configure(&lp->phy_configure);
  2089. netif_device_attach(ndev);
  2090. }
  2091. }
  2092. return 0;
  2093. }
  2094. static const struct dev_pm_ops smc_drv_pm_ops = {
  2095. .suspend = smc_drv_suspend,
  2096. .resume = smc_drv_resume,
  2097. };
  2098. static struct platform_driver smc_driver = {
  2099. .probe = smc_drv_probe,
  2100. .remove_new = smc_drv_remove,
  2101. .driver = {
  2102. .name = CARDNAME,
  2103. .pm = &smc_drv_pm_ops,
  2104. .of_match_table = of_match_ptr(smc91x_match),
  2105. .acpi_match_table = smc91x_acpi_match,
  2106. },
  2107. };
  2108. module_platform_driver(smc_driver);