smc91x.c 60 KB

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