smc91x.c 64 KB

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