smc91x.c 60 KB

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