smc91x.c 63 KB

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