smc91x.c 60 KB

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