smc91x.c 64 KB

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