smc91x.c 59 KB

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