smc91x.c 62 KB

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