smc91x.c 64 KB

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