smc91x.c 60 KB

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