smc911x.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  1. /*
  2. * smc911x.c
  3. * This is a driver for SMSC's LAN911{5,6,7,8} single-chip Ethernet devices.
  4. *
  5. * Copyright (C) 2005 Sensoria Corp
  6. * Derived from the unified SMC91x driver by Nicolas Pitre
  7. * and the smsc911x.c reference driver by SMSC
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Arguments:
  24. * watchdog = TX watchdog timeout
  25. * tx_fifo_kb = Size of TX FIFO in KB
  26. *
  27. * History:
  28. * 04/16/05 Dustin McIntire Initial version
  29. */
  30. static const char version[] =
  31. "smc911x.c: v1.0 04-16-2005 by Dustin McIntire <dustin@sensoria.com>\n";
  32. /* Debugging options */
  33. #define ENABLE_SMC_DEBUG_RX 0
  34. #define ENABLE_SMC_DEBUG_TX 0
  35. #define ENABLE_SMC_DEBUG_DMA 0
  36. #define ENABLE_SMC_DEBUG_PKTS 0
  37. #define ENABLE_SMC_DEBUG_MISC 0
  38. #define ENABLE_SMC_DEBUG_FUNC 0
  39. #define SMC_DEBUG_RX ((ENABLE_SMC_DEBUG_RX ? 1 : 0) << 0)
  40. #define SMC_DEBUG_TX ((ENABLE_SMC_DEBUG_TX ? 1 : 0) << 1)
  41. #define SMC_DEBUG_DMA ((ENABLE_SMC_DEBUG_DMA ? 1 : 0) << 2)
  42. #define SMC_DEBUG_PKTS ((ENABLE_SMC_DEBUG_PKTS ? 1 : 0) << 3)
  43. #define SMC_DEBUG_MISC ((ENABLE_SMC_DEBUG_MISC ? 1 : 0) << 4)
  44. #define SMC_DEBUG_FUNC ((ENABLE_SMC_DEBUG_FUNC ? 1 : 0) << 5)
  45. #ifndef SMC_DEBUG
  46. #define SMC_DEBUG ( SMC_DEBUG_RX | \
  47. SMC_DEBUG_TX | \
  48. SMC_DEBUG_DMA | \
  49. SMC_DEBUG_PKTS | \
  50. SMC_DEBUG_MISC | \
  51. SMC_DEBUG_FUNC \
  52. )
  53. #endif
  54. #include <linux/init.h>
  55. #include <linux/module.h>
  56. #include <linux/kernel.h>
  57. #include <linux/sched.h>
  58. #include <linux/slab.h>
  59. #include <linux/delay.h>
  60. #include <linux/interrupt.h>
  61. #include <linux/errno.h>
  62. #include <linux/ioport.h>
  63. #include <linux/crc32.h>
  64. #include <linux/device.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/ethtool.h>
  68. #include <linux/mii.h>
  69. #include <linux/workqueue.h>
  70. #include <linux/netdevice.h>
  71. #include <linux/etherdevice.h>
  72. #include <linux/skbuff.h>
  73. #include <asm/io.h>
  74. #include <asm/irq.h>
  75. #include "smc911x.h"
  76. /*
  77. * Transmit timeout, default 5 seconds.
  78. */
  79. static int watchdog = 5000;
  80. module_param(watchdog, int, 0400);
  81. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  82. static int tx_fifo_kb=8;
  83. module_param(tx_fifo_kb, int, 0400);
  84. MODULE_PARM_DESC(tx_fifo_kb,"transmit FIFO size in KB (1<x<15)(default=8)");
  85. MODULE_LICENSE("GPL");
  86. /*
  87. * The internal workings of the driver. If you are changing anything
  88. * here with the SMC stuff, you should have the datasheet and know
  89. * what you are doing.
  90. */
  91. #define CARDNAME "smc911x"
  92. /*
  93. * Use power-down feature of the chip
  94. */
  95. #define POWER_DOWN 1
  96. /* store this information for the driver.. */
  97. struct smc911x_local {
  98. /*
  99. * If I have to wait until the DMA is finished and ready to reload a
  100. * packet, I will store the skbuff here. Then, the DMA will send it
  101. * out and free it.
  102. */
  103. struct sk_buff *pending_tx_skb;
  104. /*
  105. * these are things that the kernel wants me to keep, so users
  106. * can find out semi-useless statistics of how well the card is
  107. * performing
  108. */
  109. struct net_device_stats stats;
  110. /* version/revision of the SMC911x chip */
  111. u16 version;
  112. u16 revision;
  113. /* FIFO sizes */
  114. int tx_fifo_kb;
  115. int tx_fifo_size;
  116. int rx_fifo_size;
  117. int afc_cfg;
  118. /* Contains the current active receive/phy mode */
  119. int ctl_rfduplx;
  120. int ctl_rspeed;
  121. u32 msg_enable;
  122. u32 phy_type;
  123. struct mii_if_info mii;
  124. /* work queue */
  125. struct work_struct phy_configure;
  126. int work_pending;
  127. int tx_throttle;
  128. spinlock_t lock;
  129. struct net_device *netdev;
  130. #ifdef SMC_USE_DMA
  131. /* DMA needs the physical address of the chip */
  132. u_long physaddr;
  133. int rxdma;
  134. int txdma;
  135. int rxdma_active;
  136. int txdma_active;
  137. struct sk_buff *current_rx_skb;
  138. struct sk_buff *current_tx_skb;
  139. struct device *dev;
  140. #endif
  141. };
  142. #if SMC_DEBUG > 0
  143. #define DBG(n, args...) \
  144. do { \
  145. if (SMC_DEBUG & (n)) \
  146. printk(args); \
  147. } while (0)
  148. #define PRINTK(args...) printk(args)
  149. #else
  150. #define DBG(n, args...) do { } while (0)
  151. #define PRINTK(args...) printk(KERN_DEBUG args)
  152. #endif
  153. #if SMC_DEBUG_PKTS > 0
  154. static void PRINT_PKT(u_char *buf, int length)
  155. {
  156. int i;
  157. int remainder;
  158. int lines;
  159. lines = length / 16;
  160. remainder = length % 16;
  161. for (i = 0; i < lines ; i ++) {
  162. int cur;
  163. for (cur = 0; cur < 8; cur++) {
  164. u_char a, b;
  165. a = *buf++;
  166. b = *buf++;
  167. printk("%02x%02x ", a, b);
  168. }
  169. printk("\n");
  170. }
  171. for (i = 0; i < remainder/2 ; i++) {
  172. u_char a, b;
  173. a = *buf++;
  174. b = *buf++;
  175. printk("%02x%02x ", a, b);
  176. }
  177. printk("\n");
  178. }
  179. #else
  180. #define PRINT_PKT(x...) do { } while (0)
  181. #endif
  182. /* this enables an interrupt in the interrupt mask register */
  183. #define SMC_ENABLE_INT(x) do { \
  184. unsigned int __mask; \
  185. unsigned long __flags; \
  186. spin_lock_irqsave(&lp->lock, __flags); \
  187. __mask = SMC_GET_INT_EN(); \
  188. __mask |= (x); \
  189. SMC_SET_INT_EN(__mask); \
  190. spin_unlock_irqrestore(&lp->lock, __flags); \
  191. } while (0)
  192. /* this disables an interrupt from the interrupt mask register */
  193. #define SMC_DISABLE_INT(x) do { \
  194. unsigned int __mask; \
  195. unsigned long __flags; \
  196. spin_lock_irqsave(&lp->lock, __flags); \
  197. __mask = SMC_GET_INT_EN(); \
  198. __mask &= ~(x); \
  199. SMC_SET_INT_EN(__mask); \
  200. spin_unlock_irqrestore(&lp->lock, __flags); \
  201. } while (0)
  202. /*
  203. * this does a soft reset on the device
  204. */
  205. static void smc911x_reset(struct net_device *dev)
  206. {
  207. unsigned long ioaddr = dev->base_addr;
  208. struct smc911x_local *lp = netdev_priv(dev);
  209. unsigned int reg, timeout=0, resets=1;
  210. unsigned long flags;
  211. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  212. /* Take out of PM setting first */
  213. if ((SMC_GET_PMT_CTRL() & PMT_CTRL_READY_) == 0) {
  214. /* Write to the bytetest will take out of powerdown */
  215. SMC_SET_BYTE_TEST(0);
  216. timeout=10;
  217. do {
  218. udelay(10);
  219. reg = SMC_GET_PMT_CTRL() & PMT_CTRL_READY_;
  220. } while ( timeout-- && !reg);
  221. if (timeout == 0) {
  222. PRINTK("%s: smc911x_reset timeout waiting for PM restore\n", dev->name);
  223. return;
  224. }
  225. }
  226. /* Disable all interrupts */
  227. spin_lock_irqsave(&lp->lock, flags);
  228. SMC_SET_INT_EN(0);
  229. spin_unlock_irqrestore(&lp->lock, flags);
  230. while (resets--) {
  231. SMC_SET_HW_CFG(HW_CFG_SRST_);
  232. timeout=10;
  233. do {
  234. udelay(10);
  235. reg = SMC_GET_HW_CFG();
  236. /* If chip indicates reset timeout then try again */
  237. if (reg & HW_CFG_SRST_TO_) {
  238. PRINTK("%s: chip reset timeout, retrying...\n", dev->name);
  239. resets++;
  240. break;
  241. }
  242. } while ( timeout-- && (reg & HW_CFG_SRST_));
  243. }
  244. if (timeout == 0) {
  245. PRINTK("%s: smc911x_reset timeout waiting for reset\n", dev->name);
  246. return;
  247. }
  248. /* make sure EEPROM has finished loading before setting GPIO_CFG */
  249. timeout=1000;
  250. while ( timeout-- && (SMC_GET_E2P_CMD() & E2P_CMD_EPC_BUSY_)) {
  251. udelay(10);
  252. }
  253. if (timeout == 0){
  254. PRINTK("%s: smc911x_reset timeout waiting for EEPROM busy\n", dev->name);
  255. return;
  256. }
  257. /* Initialize interrupts */
  258. SMC_SET_INT_EN(0);
  259. SMC_ACK_INT(-1);
  260. /* Reset the FIFO level and flow control settings */
  261. SMC_SET_HW_CFG((lp->tx_fifo_kb & 0xF) << 16);
  262. //TODO: Figure out what appropriate pause time is
  263. SMC_SET_FLOW(FLOW_FCPT_ | FLOW_FCEN_);
  264. SMC_SET_AFC_CFG(lp->afc_cfg);
  265. /* Set to LED outputs */
  266. SMC_SET_GPIO_CFG(0x70070000);
  267. /*
  268. * Deassert IRQ for 1*10us for edge type interrupts
  269. * and drive IRQ pin push-pull
  270. */
  271. SMC_SET_IRQ_CFG( (1 << 24) | INT_CFG_IRQ_EN_ | INT_CFG_IRQ_TYPE_ );
  272. /* clear anything saved */
  273. if (lp->pending_tx_skb != NULL) {
  274. dev_kfree_skb (lp->pending_tx_skb);
  275. lp->pending_tx_skb = NULL;
  276. lp->stats.tx_errors++;
  277. lp->stats.tx_aborted_errors++;
  278. }
  279. }
  280. /*
  281. * Enable Interrupts, Receive, and Transmit
  282. */
  283. static void smc911x_enable(struct net_device *dev)
  284. {
  285. unsigned long ioaddr = dev->base_addr;
  286. struct smc911x_local *lp = netdev_priv(dev);
  287. unsigned mask, cfg, cr;
  288. unsigned long flags;
  289. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  290. SMC_SET_MAC_ADDR(dev->dev_addr);
  291. /* Enable TX */
  292. cfg = SMC_GET_HW_CFG();
  293. cfg &= HW_CFG_TX_FIF_SZ_ | 0xFFF;
  294. cfg |= HW_CFG_SF_;
  295. SMC_SET_HW_CFG(cfg);
  296. SMC_SET_FIFO_TDA(0xFF);
  297. /* Update TX stats on every 64 packets received or every 1 sec */
  298. SMC_SET_FIFO_TSL(64);
  299. SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
  300. spin_lock_irqsave(&lp->lock, flags);
  301. SMC_GET_MAC_CR(cr);
  302. cr |= MAC_CR_TXEN_ | MAC_CR_HBDIS_;
  303. SMC_SET_MAC_CR(cr);
  304. SMC_SET_TX_CFG(TX_CFG_TX_ON_);
  305. spin_unlock_irqrestore(&lp->lock, flags);
  306. /* Add 2 byte padding to start of packets */
  307. SMC_SET_RX_CFG((2<<8) & RX_CFG_RXDOFF_);
  308. /* Turn on receiver and enable RX */
  309. if (cr & MAC_CR_RXEN_)
  310. DBG(SMC_DEBUG_RX, "%s: Receiver already enabled\n", dev->name);
  311. spin_lock_irqsave(&lp->lock, flags);
  312. SMC_SET_MAC_CR( cr | MAC_CR_RXEN_ );
  313. spin_unlock_irqrestore(&lp->lock, flags);
  314. /* Interrupt on every received packet */
  315. SMC_SET_FIFO_RSA(0x01);
  316. SMC_SET_FIFO_RSL(0x00);
  317. /* now, enable interrupts */
  318. mask = INT_EN_TDFA_EN_ | INT_EN_TSFL_EN_ | INT_EN_RSFL_EN_ |
  319. INT_EN_GPT_INT_EN_ | INT_EN_RXDFH_INT_EN_ | INT_EN_RXE_EN_ |
  320. INT_EN_PHY_INT_EN_;
  321. if (IS_REV_A(lp->revision))
  322. mask|=INT_EN_RDFL_EN_;
  323. else {
  324. mask|=INT_EN_RDFO_EN_;
  325. }
  326. SMC_ENABLE_INT(mask);
  327. }
  328. /*
  329. * this puts the device in an inactive state
  330. */
  331. static void smc911x_shutdown(struct net_device *dev)
  332. {
  333. unsigned long ioaddr = dev->base_addr;
  334. struct smc911x_local *lp = netdev_priv(dev);
  335. unsigned cr;
  336. unsigned long flags;
  337. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", CARDNAME, __FUNCTION__);
  338. /* Disable IRQ's */
  339. SMC_SET_INT_EN(0);
  340. /* Turn of Rx and TX */
  341. spin_lock_irqsave(&lp->lock, flags);
  342. SMC_GET_MAC_CR(cr);
  343. cr &= ~(MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
  344. SMC_SET_MAC_CR(cr);
  345. SMC_SET_TX_CFG(TX_CFG_STOP_TX_);
  346. spin_unlock_irqrestore(&lp->lock, flags);
  347. }
  348. static inline void smc911x_drop_pkt(struct net_device *dev)
  349. {
  350. unsigned long ioaddr = dev->base_addr;
  351. unsigned int fifo_count, timeout, reg;
  352. DBG(SMC_DEBUG_FUNC | SMC_DEBUG_RX, "%s: --> %s\n", CARDNAME, __FUNCTION__);
  353. fifo_count = SMC_GET_RX_FIFO_INF() & 0xFFFF;
  354. if (fifo_count <= 4) {
  355. /* Manually dump the packet data */
  356. while (fifo_count--)
  357. SMC_GET_RX_FIFO();
  358. } else {
  359. /* Fast forward through the bad packet */
  360. SMC_SET_RX_DP_CTRL(RX_DP_CTRL_FFWD_BUSY_);
  361. timeout=50;
  362. do {
  363. udelay(10);
  364. reg = SMC_GET_RX_DP_CTRL() & RX_DP_CTRL_FFWD_BUSY_;
  365. } while ( timeout-- && reg);
  366. if (timeout == 0) {
  367. PRINTK("%s: timeout waiting for RX fast forward\n", dev->name);
  368. }
  369. }
  370. }
  371. /*
  372. * This is the procedure to handle the receipt of a packet.
  373. * It should be called after checking for packet presence in
  374. * the RX status FIFO. It must be called with the spin lock
  375. * already held.
  376. */
  377. static inline void smc911x_rcv(struct net_device *dev)
  378. {
  379. struct smc911x_local *lp = netdev_priv(dev);
  380. unsigned long ioaddr = dev->base_addr;
  381. unsigned int pkt_len, status;
  382. struct sk_buff *skb;
  383. unsigned char *data;
  384. DBG(SMC_DEBUG_FUNC | SMC_DEBUG_RX, "%s: --> %s\n",
  385. dev->name, __FUNCTION__);
  386. status = SMC_GET_RX_STS_FIFO();
  387. DBG(SMC_DEBUG_RX, "%s: Rx pkt len %d status 0x%08x \n",
  388. dev->name, (status & 0x3fff0000) >> 16, status & 0xc000ffff);
  389. pkt_len = (status & RX_STS_PKT_LEN_) >> 16;
  390. if (status & RX_STS_ES_) {
  391. /* Deal with a bad packet */
  392. lp->stats.rx_errors++;
  393. if (status & RX_STS_CRC_ERR_)
  394. lp->stats.rx_crc_errors++;
  395. else {
  396. if (status & RX_STS_LEN_ERR_)
  397. lp->stats.rx_length_errors++;
  398. if (status & RX_STS_MCAST_)
  399. lp->stats.multicast++;
  400. }
  401. /* Remove the bad packet data from the RX FIFO */
  402. smc911x_drop_pkt(dev);
  403. } else {
  404. /* Receive a valid packet */
  405. /* Alloc a buffer with extra room for DMA alignment */
  406. skb=dev_alloc_skb(pkt_len+32);
  407. if (unlikely(skb == NULL)) {
  408. PRINTK( "%s: Low memory, rcvd packet dropped.\n",
  409. dev->name);
  410. lp->stats.rx_dropped++;
  411. smc911x_drop_pkt(dev);
  412. return;
  413. }
  414. /* Align IP header to 32 bits
  415. * Note that the device is configured to add a 2
  416. * byte padding to the packet start, so we really
  417. * want to write to the orignal data pointer */
  418. data = skb->data;
  419. skb_reserve(skb, 2);
  420. skb_put(skb,pkt_len-4);
  421. #ifdef SMC_USE_DMA
  422. {
  423. unsigned int fifo;
  424. /* Lower the FIFO threshold if possible */
  425. fifo = SMC_GET_FIFO_INT();
  426. if (fifo & 0xFF) fifo--;
  427. DBG(SMC_DEBUG_RX, "%s: Setting RX stat FIFO threshold to %d\n",
  428. dev->name, fifo & 0xff);
  429. SMC_SET_FIFO_INT(fifo);
  430. /* Setup RX DMA */
  431. SMC_SET_RX_CFG(RX_CFG_RX_END_ALGN16_ | ((2<<8) & RX_CFG_RXDOFF_));
  432. lp->rxdma_active = 1;
  433. lp->current_rx_skb = skb;
  434. SMC_PULL_DATA(data, (pkt_len+2+15) & ~15);
  435. /* Packet processing deferred to DMA RX interrupt */
  436. }
  437. #else
  438. SMC_SET_RX_CFG(RX_CFG_RX_END_ALGN4_ | ((2<<8) & RX_CFG_RXDOFF_));
  439. SMC_PULL_DATA(data, pkt_len+2+3);
  440. DBG(SMC_DEBUG_PKTS, "%s: Received packet\n", dev->name,);
  441. PRINT_PKT(data, ((pkt_len - 4) <= 64) ? pkt_len - 4 : 64);
  442. dev->last_rx = jiffies;
  443. skb->dev = dev;
  444. skb->protocol = eth_type_trans(skb, dev);
  445. netif_rx(skb);
  446. lp->stats.rx_packets++;
  447. lp->stats.rx_bytes += pkt_len-4;
  448. #endif
  449. }
  450. }
  451. /*
  452. * This is called to actually send a packet to the chip.
  453. */
  454. static void smc911x_hardware_send_pkt(struct net_device *dev)
  455. {
  456. struct smc911x_local *lp = netdev_priv(dev);
  457. unsigned long ioaddr = dev->base_addr;
  458. struct sk_buff *skb;
  459. unsigned int cmdA, cmdB, len;
  460. unsigned char *buf;
  461. unsigned long flags;
  462. DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n", dev->name, __FUNCTION__);
  463. BUG_ON(lp->pending_tx_skb == NULL);
  464. skb = lp->pending_tx_skb;
  465. lp->pending_tx_skb = NULL;
  466. /* cmdA {25:24] data alignment [20:16] start offset [10:0] buffer length */
  467. /* cmdB {31:16] pkt tag [10:0] length */
  468. #ifdef SMC_USE_DMA
  469. /* 16 byte buffer alignment mode */
  470. buf = (char*)((u32)(skb->data) & ~0xF);
  471. len = (skb->len + 0xF + ((u32)skb->data & 0xF)) & ~0xF;
  472. cmdA = (1<<24) | (((u32)skb->data & 0xF)<<16) |
  473. TX_CMD_A_INT_FIRST_SEG_ | TX_CMD_A_INT_LAST_SEG_ |
  474. skb->len;
  475. #else
  476. buf = (char*)((u32)skb->data & ~0x3);
  477. len = (skb->len + 3 + ((u32)skb->data & 3)) & ~0x3;
  478. cmdA = (((u32)skb->data & 0x3) << 16) |
  479. TX_CMD_A_INT_FIRST_SEG_ | TX_CMD_A_INT_LAST_SEG_ |
  480. skb->len;
  481. #endif
  482. /* tag is packet length so we can use this in stats update later */
  483. cmdB = (skb->len << 16) | (skb->len & 0x7FF);
  484. DBG(SMC_DEBUG_TX, "%s: TX PKT LENGTH 0x%04x (%d) BUF 0x%p CMDA 0x%08x CMDB 0x%08x\n",
  485. dev->name, len, len, buf, cmdA, cmdB);
  486. SMC_SET_TX_FIFO(cmdA);
  487. SMC_SET_TX_FIFO(cmdB);
  488. DBG(SMC_DEBUG_PKTS, "%s: Transmitted packet\n", dev->name);
  489. PRINT_PKT(buf, len <= 64 ? len : 64);
  490. /* Send pkt via PIO or DMA */
  491. #ifdef SMC_USE_DMA
  492. lp->current_tx_skb = skb;
  493. SMC_PUSH_DATA(buf, len);
  494. /* DMA complete IRQ will free buffer and set jiffies */
  495. #else
  496. SMC_PUSH_DATA(buf, len);
  497. dev->trans_start = jiffies;
  498. dev_kfree_skb(skb);
  499. #endif
  500. spin_lock_irqsave(&lp->lock, flags);
  501. if (!lp->tx_throttle) {
  502. netif_wake_queue(dev);
  503. }
  504. spin_unlock_irqrestore(&lp->lock, flags);
  505. SMC_ENABLE_INT(INT_EN_TDFA_EN_ | INT_EN_TSFL_EN_);
  506. }
  507. /*
  508. * Since I am not sure if I will have enough room in the chip's ram
  509. * to store the packet, I call this routine which either sends it
  510. * now, or set the card to generates an interrupt when ready
  511. * for the packet.
  512. */
  513. static int smc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  514. {
  515. struct smc911x_local *lp = netdev_priv(dev);
  516. unsigned long ioaddr = dev->base_addr;
  517. unsigned int free;
  518. unsigned long flags;
  519. DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n",
  520. dev->name, __FUNCTION__);
  521. BUG_ON(lp->pending_tx_skb != NULL);
  522. free = SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TDFREE_;
  523. DBG(SMC_DEBUG_TX, "%s: TX free space %d\n", dev->name, free);
  524. /* Turn off the flow when running out of space in FIFO */
  525. if (free <= SMC911X_TX_FIFO_LOW_THRESHOLD) {
  526. DBG(SMC_DEBUG_TX, "%s: Disabling data flow due to low FIFO space (%d)\n",
  527. dev->name, free);
  528. spin_lock_irqsave(&lp->lock, flags);
  529. /* Reenable when at least 1 packet of size MTU present */
  530. SMC_SET_FIFO_TDA((SMC911X_TX_FIFO_LOW_THRESHOLD)/64);
  531. lp->tx_throttle = 1;
  532. netif_stop_queue(dev);
  533. spin_unlock_irqrestore(&lp->lock, flags);
  534. }
  535. /* Drop packets when we run out of space in TX FIFO
  536. * Account for overhead required for:
  537. *
  538. * Tx command words 8 bytes
  539. * Start offset 15 bytes
  540. * End padding 15 bytes
  541. */
  542. if (unlikely(free < (skb->len + 8 + 15 + 15))) {
  543. printk("%s: No Tx free space %d < %d\n",
  544. dev->name, free, skb->len);
  545. lp->pending_tx_skb = NULL;
  546. lp->stats.tx_errors++;
  547. lp->stats.tx_dropped++;
  548. dev_kfree_skb(skb);
  549. return 0;
  550. }
  551. #ifdef SMC_USE_DMA
  552. {
  553. /* If the DMA is already running then defer this packet Tx until
  554. * the DMA IRQ starts it
  555. */
  556. spin_lock_irqsave(&lp->lock, flags);
  557. if (lp->txdma_active) {
  558. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: Tx DMA running, deferring packet\n", dev->name);
  559. lp->pending_tx_skb = skb;
  560. netif_stop_queue(dev);
  561. spin_unlock_irqrestore(&lp->lock, flags);
  562. return 0;
  563. } else {
  564. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: Activating Tx DMA\n", dev->name);
  565. lp->txdma_active = 1;
  566. }
  567. spin_unlock_irqrestore(&lp->lock, flags);
  568. }
  569. #endif
  570. lp->pending_tx_skb = skb;
  571. smc911x_hardware_send_pkt(dev);
  572. return 0;
  573. }
  574. /*
  575. * This handles a TX status interrupt, which is only called when:
  576. * - a TX error occurred, or
  577. * - TX of a packet completed.
  578. */
  579. static void smc911x_tx(struct net_device *dev)
  580. {
  581. unsigned long ioaddr = dev->base_addr;
  582. struct smc911x_local *lp = netdev_priv(dev);
  583. unsigned int tx_status;
  584. DBG(SMC_DEBUG_FUNC | SMC_DEBUG_TX, "%s: --> %s\n",
  585. dev->name, __FUNCTION__);
  586. /* Collect the TX status */
  587. while (((SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TSUSED_) >> 16) != 0) {
  588. DBG(SMC_DEBUG_TX, "%s: Tx stat FIFO used 0x%04x\n",
  589. dev->name,
  590. (SMC_GET_TX_FIFO_INF() & TX_FIFO_INF_TSUSED_) >> 16);
  591. tx_status = SMC_GET_TX_STS_FIFO();
  592. lp->stats.tx_packets++;
  593. lp->stats.tx_bytes+=tx_status>>16;
  594. DBG(SMC_DEBUG_TX, "%s: Tx FIFO tag 0x%04x status 0x%04x\n",
  595. dev->name, (tx_status & 0xffff0000) >> 16,
  596. tx_status & 0x0000ffff);
  597. /* count Tx errors, but ignore lost carrier errors when in
  598. * full-duplex mode */
  599. if ((tx_status & TX_STS_ES_) && !(lp->ctl_rfduplx &&
  600. !(tx_status & 0x00000306))) {
  601. lp->stats.tx_errors++;
  602. }
  603. if (tx_status & TX_STS_MANY_COLL_) {
  604. lp->stats.collisions+=16;
  605. lp->stats.tx_aborted_errors++;
  606. } else {
  607. lp->stats.collisions+=(tx_status & TX_STS_COLL_CNT_) >> 3;
  608. }
  609. /* carrier error only has meaning for half-duplex communication */
  610. if ((tx_status & (TX_STS_LOC_ | TX_STS_NO_CARR_)) &&
  611. !lp->ctl_rfduplx) {
  612. lp->stats.tx_carrier_errors++;
  613. }
  614. if (tx_status & TX_STS_LATE_COLL_) {
  615. lp->stats.collisions++;
  616. lp->stats.tx_aborted_errors++;
  617. }
  618. }
  619. }
  620. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  621. /*
  622. * Reads a register from the MII Management serial interface
  623. */
  624. static int smc911x_phy_read(struct net_device *dev, int phyaddr, int phyreg)
  625. {
  626. unsigned long ioaddr = dev->base_addr;
  627. unsigned int phydata;
  628. SMC_GET_MII(phyreg, phyaddr, phydata);
  629. DBG(SMC_DEBUG_MISC, "%s: phyaddr=0x%x, phyreg=0x%02x, phydata=0x%04x\n",
  630. __FUNCTION__, phyaddr, phyreg, phydata);
  631. return phydata;
  632. }
  633. /*
  634. * Writes a register to the MII Management serial interface
  635. */
  636. static void smc911x_phy_write(struct net_device *dev, int phyaddr, int phyreg,
  637. int phydata)
  638. {
  639. unsigned long ioaddr = dev->base_addr;
  640. DBG(SMC_DEBUG_MISC, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
  641. __FUNCTION__, phyaddr, phyreg, phydata);
  642. SMC_SET_MII(phyreg, phyaddr, phydata);
  643. }
  644. /*
  645. * Finds and reports the PHY address (115 and 117 have external
  646. * PHY interface 118 has internal only
  647. */
  648. static void smc911x_phy_detect(struct net_device *dev)
  649. {
  650. unsigned long ioaddr = dev->base_addr;
  651. struct smc911x_local *lp = netdev_priv(dev);
  652. int phyaddr;
  653. unsigned int cfg, id1, id2;
  654. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  655. lp->phy_type = 0;
  656. /*
  657. * Scan all 32 PHY addresses if necessary, starting at
  658. * PHY#1 to PHY#31, and then PHY#0 last.
  659. */
  660. switch(lp->version) {
  661. case 0x115:
  662. case 0x117:
  663. cfg = SMC_GET_HW_CFG();
  664. if (cfg & HW_CFG_EXT_PHY_DET_) {
  665. cfg &= ~HW_CFG_PHY_CLK_SEL_;
  666. cfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
  667. SMC_SET_HW_CFG(cfg);
  668. udelay(10); /* Wait for clocks to stop */
  669. cfg |= HW_CFG_EXT_PHY_EN_;
  670. SMC_SET_HW_CFG(cfg);
  671. udelay(10); /* Wait for clocks to stop */
  672. cfg &= ~HW_CFG_PHY_CLK_SEL_;
  673. cfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
  674. SMC_SET_HW_CFG(cfg);
  675. udelay(10); /* Wait for clocks to stop */
  676. cfg |= HW_CFG_SMI_SEL_;
  677. SMC_SET_HW_CFG(cfg);
  678. for (phyaddr = 1; phyaddr < 32; ++phyaddr) {
  679. /* Read the PHY identifiers */
  680. SMC_GET_PHY_ID1(phyaddr & 31, id1);
  681. SMC_GET_PHY_ID2(phyaddr & 31, id2);
  682. /* Make sure it is a valid identifier */
  683. if (id1 != 0x0000 && id1 != 0xffff &&
  684. id1 != 0x8000 && id2 != 0x0000 &&
  685. id2 != 0xffff && id2 != 0x8000) {
  686. /* Save the PHY's address */
  687. lp->mii.phy_id = phyaddr & 31;
  688. lp->phy_type = id1 << 16 | id2;
  689. break;
  690. }
  691. }
  692. }
  693. default:
  694. /* Internal media only */
  695. SMC_GET_PHY_ID1(1, id1);
  696. SMC_GET_PHY_ID2(1, id2);
  697. /* Save the PHY's address */
  698. lp->mii.phy_id = 1;
  699. lp->phy_type = id1 << 16 | id2;
  700. }
  701. DBG(SMC_DEBUG_MISC, "%s: phy_id1=0x%x, phy_id2=0x%x phyaddr=0x%d\n",
  702. dev->name, id1, id2, lp->mii.phy_id);
  703. }
  704. /*
  705. * Sets the PHY to a configuration as determined by the user.
  706. * Called with spin_lock held.
  707. */
  708. static int smc911x_phy_fixed(struct net_device *dev)
  709. {
  710. struct smc911x_local *lp = netdev_priv(dev);
  711. unsigned long ioaddr = dev->base_addr;
  712. int phyaddr = lp->mii.phy_id;
  713. int bmcr;
  714. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  715. /* Enter Link Disable state */
  716. SMC_GET_PHY_BMCR(phyaddr, bmcr);
  717. bmcr |= BMCR_PDOWN;
  718. SMC_SET_PHY_BMCR(phyaddr, bmcr);
  719. /*
  720. * Set our fixed capabilities
  721. * Disable auto-negotiation
  722. */
  723. bmcr &= ~BMCR_ANENABLE;
  724. if (lp->ctl_rfduplx)
  725. bmcr |= BMCR_FULLDPLX;
  726. if (lp->ctl_rspeed == 100)
  727. bmcr |= BMCR_SPEED100;
  728. /* Write our capabilities to the phy control register */
  729. SMC_SET_PHY_BMCR(phyaddr, bmcr);
  730. /* Re-Configure the Receive/Phy Control register */
  731. bmcr &= ~BMCR_PDOWN;
  732. SMC_SET_PHY_BMCR(phyaddr, bmcr);
  733. return 1;
  734. }
  735. /*
  736. * smc911x_phy_reset - reset the phy
  737. * @dev: net device
  738. * @phy: phy address
  739. *
  740. * Issue a software reset for the specified PHY and
  741. * wait up to 100ms for the reset to complete. We should
  742. * not access the PHY for 50ms after issuing the reset.
  743. *
  744. * The time to wait appears to be dependent on the PHY.
  745. *
  746. */
  747. static int smc911x_phy_reset(struct net_device *dev, int phy)
  748. {
  749. struct smc911x_local *lp = netdev_priv(dev);
  750. unsigned long ioaddr = dev->base_addr;
  751. int timeout;
  752. unsigned long flags;
  753. unsigned int reg;
  754. DBG(SMC_DEBUG_FUNC, "%s: --> %s()\n", dev->name, __FUNCTION__);
  755. spin_lock_irqsave(&lp->lock, flags);
  756. reg = SMC_GET_PMT_CTRL();
  757. reg &= ~0xfffff030;
  758. reg |= PMT_CTRL_PHY_RST_;
  759. SMC_SET_PMT_CTRL(reg);
  760. spin_unlock_irqrestore(&lp->lock, flags);
  761. for (timeout = 2; timeout; timeout--) {
  762. msleep(50);
  763. spin_lock_irqsave(&lp->lock, flags);
  764. reg = SMC_GET_PMT_CTRL();
  765. spin_unlock_irqrestore(&lp->lock, flags);
  766. if (!(reg & PMT_CTRL_PHY_RST_)) {
  767. /* extra delay required because the phy may
  768. * not be completed with its reset
  769. * when PHY_BCR_RESET_ is cleared. 256us
  770. * should suffice, but use 500us to be safe
  771. */
  772. udelay(500);
  773. break;
  774. }
  775. }
  776. return reg & PMT_CTRL_PHY_RST_;
  777. }
  778. /*
  779. * smc911x_phy_powerdown - powerdown phy
  780. * @dev: net device
  781. * @phy: phy address
  782. *
  783. * Power down the specified PHY
  784. */
  785. static void smc911x_phy_powerdown(struct net_device *dev, int phy)
  786. {
  787. unsigned long ioaddr = dev->base_addr;
  788. unsigned int bmcr;
  789. /* Enter Link Disable state */
  790. SMC_GET_PHY_BMCR(phy, bmcr);
  791. bmcr |= BMCR_PDOWN;
  792. SMC_SET_PHY_BMCR(phy, bmcr);
  793. }
  794. /*
  795. * smc911x_phy_check_media - check the media status and adjust BMCR
  796. * @dev: net device
  797. * @init: set true for initialisation
  798. *
  799. * Select duplex mode depending on negotiation state. This
  800. * also updates our carrier state.
  801. */
  802. static void smc911x_phy_check_media(struct net_device *dev, int init)
  803. {
  804. struct smc911x_local *lp = netdev_priv(dev);
  805. unsigned long ioaddr = dev->base_addr;
  806. int phyaddr = lp->mii.phy_id;
  807. unsigned int bmcr, cr;
  808. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  809. if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
  810. /* duplex state has changed */
  811. SMC_GET_PHY_BMCR(phyaddr, bmcr);
  812. SMC_GET_MAC_CR(cr);
  813. if (lp->mii.full_duplex) {
  814. DBG(SMC_DEBUG_MISC, "%s: Configuring for full-duplex mode\n", dev->name);
  815. bmcr |= BMCR_FULLDPLX;
  816. cr |= MAC_CR_RCVOWN_;
  817. } else {
  818. DBG(SMC_DEBUG_MISC, "%s: Configuring for half-duplex mode\n", dev->name);
  819. bmcr &= ~BMCR_FULLDPLX;
  820. cr &= ~MAC_CR_RCVOWN_;
  821. }
  822. SMC_SET_PHY_BMCR(phyaddr, bmcr);
  823. SMC_SET_MAC_CR(cr);
  824. }
  825. }
  826. /*
  827. * Configures the specified PHY through the MII management interface
  828. * using Autonegotiation.
  829. * Calls smc911x_phy_fixed() if the user has requested a certain config.
  830. * If RPC ANEG bit is set, the media selection is dependent purely on
  831. * the selection by the MII (either in the MII BMCR reg or the result
  832. * of autonegotiation.) If the RPC ANEG bit is cleared, the selection
  833. * is controlled by the RPC SPEED and RPC DPLX bits.
  834. */
  835. static void smc911x_phy_configure(struct work_struct *work)
  836. {
  837. struct smc911x_local *lp = container_of(work, struct smc911x_local,
  838. phy_configure);
  839. struct net_device *dev = lp->netdev;
  840. unsigned long ioaddr = dev->base_addr;
  841. int phyaddr = lp->mii.phy_id;
  842. int my_phy_caps; /* My PHY capabilities */
  843. int my_ad_caps; /* My Advertised capabilities */
  844. int status;
  845. unsigned long flags;
  846. DBG(SMC_DEBUG_FUNC, "%s: --> %s()\n", dev->name, __FUNCTION__);
  847. /*
  848. * We should not be called if phy_type is zero.
  849. */
  850. if (lp->phy_type == 0)
  851. goto smc911x_phy_configure_exit;
  852. if (smc911x_phy_reset(dev, phyaddr)) {
  853. printk("%s: PHY reset timed out\n", dev->name);
  854. goto smc911x_phy_configure_exit;
  855. }
  856. spin_lock_irqsave(&lp->lock, flags);
  857. /*
  858. * Enable PHY Interrupts (for register 18)
  859. * Interrupts listed here are enabled
  860. */
  861. SMC_SET_PHY_INT_MASK(phyaddr, PHY_INT_MASK_ENERGY_ON_ |
  862. PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_REMOTE_FAULT_ |
  863. PHY_INT_MASK_LINK_DOWN_);
  864. /* If the user requested no auto neg, then go set his request */
  865. if (lp->mii.force_media) {
  866. smc911x_phy_fixed(dev);
  867. goto smc911x_phy_configure_exit;
  868. }
  869. /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
  870. SMC_GET_PHY_BMSR(phyaddr, my_phy_caps);
  871. if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
  872. printk(KERN_INFO "Auto negotiation NOT supported\n");
  873. smc911x_phy_fixed(dev);
  874. goto smc911x_phy_configure_exit;
  875. }
  876. /* CSMA capable w/ both pauses */
  877. my_ad_caps = ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  878. if (my_phy_caps & BMSR_100BASE4)
  879. my_ad_caps |= ADVERTISE_100BASE4;
  880. if (my_phy_caps & BMSR_100FULL)
  881. my_ad_caps |= ADVERTISE_100FULL;
  882. if (my_phy_caps & BMSR_100HALF)
  883. my_ad_caps |= ADVERTISE_100HALF;
  884. if (my_phy_caps & BMSR_10FULL)
  885. my_ad_caps |= ADVERTISE_10FULL;
  886. if (my_phy_caps & BMSR_10HALF)
  887. my_ad_caps |= ADVERTISE_10HALF;
  888. /* Disable capabilities not selected by our user */
  889. if (lp->ctl_rspeed != 100)
  890. my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
  891. if (!lp->ctl_rfduplx)
  892. my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
  893. /* Update our Auto-Neg Advertisement Register */
  894. SMC_SET_PHY_MII_ADV(phyaddr, my_ad_caps);
  895. lp->mii.advertising = my_ad_caps;
  896. /*
  897. * Read the register back. Without this, it appears that when
  898. * auto-negotiation is restarted, sometimes it isn't ready and
  899. * the link does not come up.
  900. */
  901. udelay(10);
  902. SMC_GET_PHY_MII_ADV(phyaddr, status);
  903. DBG(SMC_DEBUG_MISC, "%s: phy caps=0x%04x\n", dev->name, my_phy_caps);
  904. DBG(SMC_DEBUG_MISC, "%s: phy advertised caps=0x%04x\n", dev->name, my_ad_caps);
  905. /* Restart auto-negotiation process in order to advertise my caps */
  906. SMC_SET_PHY_BMCR(phyaddr, BMCR_ANENABLE | BMCR_ANRESTART);
  907. smc911x_phy_check_media(dev, 1);
  908. smc911x_phy_configure_exit:
  909. spin_unlock_irqrestore(&lp->lock, flags);
  910. lp->work_pending = 0;
  911. }
  912. /*
  913. * smc911x_phy_interrupt
  914. *
  915. * Purpose: Handle interrupts relating to PHY register 18. This is
  916. * called from the "hard" interrupt handler under our private spinlock.
  917. */
  918. static void smc911x_phy_interrupt(struct net_device *dev)
  919. {
  920. struct smc911x_local *lp = netdev_priv(dev);
  921. unsigned long ioaddr = dev->base_addr;
  922. int phyaddr = lp->mii.phy_id;
  923. int status;
  924. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  925. if (lp->phy_type == 0)
  926. return;
  927. smc911x_phy_check_media(dev, 0);
  928. /* read to clear status bits */
  929. SMC_GET_PHY_INT_SRC(phyaddr,status);
  930. DBG(SMC_DEBUG_MISC, "%s: PHY interrupt status 0x%04x\n",
  931. dev->name, status & 0xffff);
  932. DBG(SMC_DEBUG_MISC, "%s: AFC_CFG 0x%08x\n",
  933. dev->name, SMC_GET_AFC_CFG());
  934. }
  935. /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
  936. /*
  937. * This is the main routine of the driver, to handle the device when
  938. * it needs some attention.
  939. */
  940. static irqreturn_t smc911x_interrupt(int irq, void *dev_id)
  941. {
  942. struct net_device *dev = dev_id;
  943. unsigned long ioaddr = dev->base_addr;
  944. struct smc911x_local *lp = netdev_priv(dev);
  945. unsigned int status, mask, timeout;
  946. unsigned int rx_overrun=0, cr, pkts;
  947. unsigned long flags;
  948. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  949. spin_lock_irqsave(&lp->lock, flags);
  950. /* Spurious interrupt check */
  951. if ((SMC_GET_IRQ_CFG() & (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) !=
  952. (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) {
  953. spin_unlock_irqrestore(&lp->lock, flags);
  954. return IRQ_NONE;
  955. }
  956. mask = SMC_GET_INT_EN();
  957. SMC_SET_INT_EN(0);
  958. /* set a timeout value, so I don't stay here forever */
  959. timeout = 8;
  960. do {
  961. status = SMC_GET_INT();
  962. DBG(SMC_DEBUG_MISC, "%s: INT 0x%08x MASK 0x%08x OUTSIDE MASK 0x%08x\n",
  963. dev->name, status, mask, status & ~mask);
  964. status &= mask;
  965. if (!status)
  966. break;
  967. /* Handle SW interrupt condition */
  968. if (status & INT_STS_SW_INT_) {
  969. SMC_ACK_INT(INT_STS_SW_INT_);
  970. mask &= ~INT_EN_SW_INT_EN_;
  971. }
  972. /* Handle various error conditions */
  973. if (status & INT_STS_RXE_) {
  974. SMC_ACK_INT(INT_STS_RXE_);
  975. lp->stats.rx_errors++;
  976. }
  977. if (status & INT_STS_RXDFH_INT_) {
  978. SMC_ACK_INT(INT_STS_RXDFH_INT_);
  979. lp->stats.rx_dropped+=SMC_GET_RX_DROP();
  980. }
  981. /* Undocumented interrupt-what is the right thing to do here? */
  982. if (status & INT_STS_RXDF_INT_) {
  983. SMC_ACK_INT(INT_STS_RXDF_INT_);
  984. }
  985. /* Rx Data FIFO exceeds set level */
  986. if (status & INT_STS_RDFL_) {
  987. if (IS_REV_A(lp->revision)) {
  988. rx_overrun=1;
  989. SMC_GET_MAC_CR(cr);
  990. cr &= ~MAC_CR_RXEN_;
  991. SMC_SET_MAC_CR(cr);
  992. DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
  993. lp->stats.rx_errors++;
  994. lp->stats.rx_fifo_errors++;
  995. }
  996. SMC_ACK_INT(INT_STS_RDFL_);
  997. }
  998. if (status & INT_STS_RDFO_) {
  999. if (!IS_REV_A(lp->revision)) {
  1000. SMC_GET_MAC_CR(cr);
  1001. cr &= ~MAC_CR_RXEN_;
  1002. SMC_SET_MAC_CR(cr);
  1003. rx_overrun=1;
  1004. DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
  1005. lp->stats.rx_errors++;
  1006. lp->stats.rx_fifo_errors++;
  1007. }
  1008. SMC_ACK_INT(INT_STS_RDFO_);
  1009. }
  1010. /* Handle receive condition */
  1011. if ((status & INT_STS_RSFL_) || rx_overrun) {
  1012. unsigned int fifo;
  1013. DBG(SMC_DEBUG_RX, "%s: RX irq\n", dev->name);
  1014. fifo = SMC_GET_RX_FIFO_INF();
  1015. pkts = (fifo & RX_FIFO_INF_RXSUSED_) >> 16;
  1016. DBG(SMC_DEBUG_RX, "%s: Rx FIFO pkts %d, bytes %d\n",
  1017. dev->name, pkts, fifo & 0xFFFF );
  1018. if (pkts != 0) {
  1019. #ifdef SMC_USE_DMA
  1020. unsigned int fifo;
  1021. if (lp->rxdma_active){
  1022. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
  1023. "%s: RX DMA active\n", dev->name);
  1024. /* The DMA is already running so up the IRQ threshold */
  1025. fifo = SMC_GET_FIFO_INT() & ~0xFF;
  1026. fifo |= pkts & 0xFF;
  1027. DBG(SMC_DEBUG_RX,
  1028. "%s: Setting RX stat FIFO threshold to %d\n",
  1029. dev->name, fifo & 0xff);
  1030. SMC_SET_FIFO_INT(fifo);
  1031. } else
  1032. #endif
  1033. smc911x_rcv(dev);
  1034. }
  1035. SMC_ACK_INT(INT_STS_RSFL_);
  1036. }
  1037. /* Handle transmit FIFO available */
  1038. if (status & INT_STS_TDFA_) {
  1039. DBG(SMC_DEBUG_TX, "%s: TX data FIFO space available irq\n", dev->name);
  1040. SMC_SET_FIFO_TDA(0xFF);
  1041. lp->tx_throttle = 0;
  1042. #ifdef SMC_USE_DMA
  1043. if (!lp->txdma_active)
  1044. #endif
  1045. netif_wake_queue(dev);
  1046. SMC_ACK_INT(INT_STS_TDFA_);
  1047. }
  1048. /* Handle transmit done condition */
  1049. #if 1
  1050. if (status & (INT_STS_TSFL_ | INT_STS_GPT_INT_)) {
  1051. DBG(SMC_DEBUG_TX | SMC_DEBUG_MISC,
  1052. "%s: Tx stat FIFO limit (%d) /GPT irq\n",
  1053. dev->name, (SMC_GET_FIFO_INT() & 0x00ff0000) >> 16);
  1054. smc911x_tx(dev);
  1055. SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
  1056. SMC_ACK_INT(INT_STS_TSFL_);
  1057. SMC_ACK_INT(INT_STS_TSFL_ | INT_STS_GPT_INT_);
  1058. }
  1059. #else
  1060. if (status & INT_STS_TSFL_) {
  1061. DBG(SMC_DEBUG_TX, "%s: TX status FIFO limit (%d) irq \n", dev->name, );
  1062. smc911x_tx(dev);
  1063. SMC_ACK_INT(INT_STS_TSFL_);
  1064. }
  1065. if (status & INT_STS_GPT_INT_) {
  1066. DBG(SMC_DEBUG_RX, "%s: IRQ_CFG 0x%08x FIFO_INT 0x%08x RX_CFG 0x%08x\n",
  1067. dev->name,
  1068. SMC_GET_IRQ_CFG(),
  1069. SMC_GET_FIFO_INT(),
  1070. SMC_GET_RX_CFG());
  1071. DBG(SMC_DEBUG_RX, "%s: Rx Stat FIFO Used 0x%02x "
  1072. "Data FIFO Used 0x%04x Stat FIFO 0x%08x\n",
  1073. dev->name,
  1074. (SMC_GET_RX_FIFO_INF() & 0x00ff0000) >> 16,
  1075. SMC_GET_RX_FIFO_INF() & 0xffff,
  1076. SMC_GET_RX_STS_FIFO_PEEK());
  1077. SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
  1078. SMC_ACK_INT(INT_STS_GPT_INT_);
  1079. }
  1080. #endif
  1081. /* Handle PHY interupt condition */
  1082. if (status & INT_STS_PHY_INT_) {
  1083. DBG(SMC_DEBUG_MISC, "%s: PHY irq\n", dev->name);
  1084. smc911x_phy_interrupt(dev);
  1085. SMC_ACK_INT(INT_STS_PHY_INT_);
  1086. }
  1087. } while (--timeout);
  1088. /* restore mask state */
  1089. SMC_SET_INT_EN(mask);
  1090. DBG(SMC_DEBUG_MISC, "%s: Interrupt done (%d loops)\n",
  1091. dev->name, 8-timeout);
  1092. spin_unlock_irqrestore(&lp->lock, flags);
  1093. DBG(3, "%s: Interrupt done (%d loops)\n", dev->name, 8-timeout);
  1094. return IRQ_HANDLED;
  1095. }
  1096. #ifdef SMC_USE_DMA
  1097. static void
  1098. smc911x_tx_dma_irq(int dma, void *data)
  1099. {
  1100. struct net_device *dev = (struct net_device *)data;
  1101. struct smc911x_local *lp = netdev_priv(dev);
  1102. struct sk_buff *skb = lp->current_tx_skb;
  1103. unsigned long flags;
  1104. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1105. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: TX DMA irq handler\n", dev->name);
  1106. /* Clear the DMA interrupt sources */
  1107. SMC_DMA_ACK_IRQ(dev, dma);
  1108. BUG_ON(skb == NULL);
  1109. dma_unmap_single(NULL, tx_dmabuf, tx_dmalen, DMA_TO_DEVICE);
  1110. dev->trans_start = jiffies;
  1111. dev_kfree_skb_irq(skb);
  1112. lp->current_tx_skb = NULL;
  1113. if (lp->pending_tx_skb != NULL)
  1114. smc911x_hardware_send_pkt(dev);
  1115. else {
  1116. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
  1117. "%s: No pending Tx packets. DMA disabled\n", dev->name);
  1118. spin_lock_irqsave(&lp->lock, flags);
  1119. lp->txdma_active = 0;
  1120. if (!lp->tx_throttle) {
  1121. netif_wake_queue(dev);
  1122. }
  1123. spin_unlock_irqrestore(&lp->lock, flags);
  1124. }
  1125. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
  1126. "%s: TX DMA irq completed\n", dev->name);
  1127. }
  1128. static void
  1129. smc911x_rx_dma_irq(int dma, void *data)
  1130. {
  1131. struct net_device *dev = (struct net_device *)data;
  1132. unsigned long ioaddr = dev->base_addr;
  1133. struct smc911x_local *lp = netdev_priv(dev);
  1134. struct sk_buff *skb = lp->current_rx_skb;
  1135. unsigned long flags;
  1136. unsigned int pkts;
  1137. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1138. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA, "%s: RX DMA irq handler\n", dev->name);
  1139. /* Clear the DMA interrupt sources */
  1140. SMC_DMA_ACK_IRQ(dev, dma);
  1141. dma_unmap_single(NULL, rx_dmabuf, rx_dmalen, DMA_FROM_DEVICE);
  1142. BUG_ON(skb == NULL);
  1143. lp->current_rx_skb = NULL;
  1144. PRINT_PKT(skb->data, skb->len);
  1145. dev->last_rx = jiffies;
  1146. skb->dev = dev;
  1147. skb->protocol = eth_type_trans(skb, dev);
  1148. netif_rx(skb);
  1149. lp->stats.rx_packets++;
  1150. lp->stats.rx_bytes += skb->len;
  1151. spin_lock_irqsave(&lp->lock, flags);
  1152. pkts = (SMC_GET_RX_FIFO_INF() & RX_FIFO_INF_RXSUSED_) >> 16;
  1153. if (pkts != 0) {
  1154. smc911x_rcv(dev);
  1155. }else {
  1156. lp->rxdma_active = 0;
  1157. }
  1158. spin_unlock_irqrestore(&lp->lock, flags);
  1159. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
  1160. "%s: RX DMA irq completed. DMA RX FIFO PKTS %d\n",
  1161. dev->name, pkts);
  1162. }
  1163. #endif /* SMC_USE_DMA */
  1164. #ifdef CONFIG_NET_POLL_CONTROLLER
  1165. /*
  1166. * Polling receive - used by netconsole and other diagnostic tools
  1167. * to allow network i/o with interrupts disabled.
  1168. */
  1169. static void smc911x_poll_controller(struct net_device *dev)
  1170. {
  1171. disable_irq(dev->irq);
  1172. smc911x_interrupt(dev->irq, dev);
  1173. enable_irq(dev->irq);
  1174. }
  1175. #endif
  1176. /* Our watchdog timed out. Called by the networking layer */
  1177. static void smc911x_timeout(struct net_device *dev)
  1178. {
  1179. struct smc911x_local *lp = netdev_priv(dev);
  1180. unsigned long ioaddr = dev->base_addr;
  1181. int status, mask;
  1182. unsigned long flags;
  1183. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1184. spin_lock_irqsave(&lp->lock, flags);
  1185. status = SMC_GET_INT();
  1186. mask = SMC_GET_INT_EN();
  1187. spin_unlock_irqrestore(&lp->lock, flags);
  1188. DBG(SMC_DEBUG_MISC, "%s: INT 0x%02x MASK 0x%02x \n",
  1189. dev->name, status, mask);
  1190. /* Dump the current TX FIFO contents and restart */
  1191. mask = SMC_GET_TX_CFG();
  1192. SMC_SET_TX_CFG(mask | TX_CFG_TXS_DUMP_ | TX_CFG_TXD_DUMP_);
  1193. /*
  1194. * Reconfiguring the PHY doesn't seem like a bad idea here, but
  1195. * smc911x_phy_configure() calls msleep() which calls schedule_timeout()
  1196. * which calls schedule(). Hence we use a work queue.
  1197. */
  1198. if (lp->phy_type != 0) {
  1199. if (schedule_work(&lp->phy_configure)) {
  1200. lp->work_pending = 1;
  1201. }
  1202. }
  1203. /* We can accept TX packets again */
  1204. dev->trans_start = jiffies;
  1205. netif_wake_queue(dev);
  1206. }
  1207. /*
  1208. * This routine will, depending on the values passed to it,
  1209. * either make it accept multicast packets, go into
  1210. * promiscuous mode (for TCPDUMP and cousins) or accept
  1211. * a select set of multicast packets
  1212. */
  1213. static void smc911x_set_multicast_list(struct net_device *dev)
  1214. {
  1215. struct smc911x_local *lp = netdev_priv(dev);
  1216. unsigned long ioaddr = dev->base_addr;
  1217. unsigned int multicast_table[2];
  1218. unsigned int mcr, update_multicast = 0;
  1219. unsigned long flags;
  1220. /* table for flipping the order of 5 bits */
  1221. static const unsigned char invert5[] =
  1222. {0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0C, 0x1C,
  1223. 0x02, 0x12, 0x0A, 0x1A, 0x06, 0x16, 0x0E, 0x1E,
  1224. 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0D, 0x1D,
  1225. 0x03, 0x13, 0x0B, 0x1B, 0x07, 0x17, 0x0F, 0x1F};
  1226. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1227. spin_lock_irqsave(&lp->lock, flags);
  1228. SMC_GET_MAC_CR(mcr);
  1229. spin_unlock_irqrestore(&lp->lock, flags);
  1230. if (dev->flags & IFF_PROMISC) {
  1231. DBG(SMC_DEBUG_MISC, "%s: RCR_PRMS\n", dev->name);
  1232. mcr |= MAC_CR_PRMS_;
  1233. }
  1234. /*
  1235. * Here, I am setting this to accept all multicast packets.
  1236. * I don't need to zero the multicast table, because the flag is
  1237. * checked before the table is
  1238. */
  1239. else if (dev->flags & IFF_ALLMULTI || dev->mc_count > 16) {
  1240. DBG(SMC_DEBUG_MISC, "%s: RCR_ALMUL\n", dev->name);
  1241. mcr |= MAC_CR_MCPAS_;
  1242. }
  1243. /*
  1244. * This sets the internal hardware table to filter out unwanted
  1245. * multicast packets before they take up memory.
  1246. *
  1247. * The SMC chip uses a hash table where the high 6 bits of the CRC of
  1248. * address are the offset into the table. If that bit is 1, then the
  1249. * multicast packet is accepted. Otherwise, it's dropped silently.
  1250. *
  1251. * To use the 6 bits as an offset into the table, the high 1 bit is
  1252. * the number of the 32 bit register, while the low 5 bits are the bit
  1253. * within that register.
  1254. */
  1255. else if (dev->mc_count) {
  1256. int i;
  1257. struct dev_mc_list *cur_addr;
  1258. /* Set the Hash perfec mode */
  1259. mcr |= MAC_CR_HPFILT_;
  1260. /* start with a table of all zeros: reject all */
  1261. memset(multicast_table, 0, sizeof(multicast_table));
  1262. cur_addr = dev->mc_list;
  1263. for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
  1264. int position;
  1265. /* do we have a pointer here? */
  1266. if (!cur_addr)
  1267. break;
  1268. /* make sure this is a multicast address -
  1269. shouldn't this be a given if we have it here ? */
  1270. if (!(*cur_addr->dmi_addr & 1))
  1271. continue;
  1272. /* only use the low order bits */
  1273. position = crc32_le(~0, cur_addr->dmi_addr, 6) & 0x3f;
  1274. /* do some messy swapping to put the bit in the right spot */
  1275. multicast_table[invert5[position&0x1F]&0x1] |=
  1276. (1<<invert5[(position>>1)&0x1F]);
  1277. }
  1278. /* be sure I get rid of flags I might have set */
  1279. mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1280. /* now, the table can be loaded into the chipset */
  1281. update_multicast = 1;
  1282. } else {
  1283. DBG(SMC_DEBUG_MISC, "%s: ~(MAC_CR_PRMS_|MAC_CR_MCPAS_)\n",
  1284. dev->name);
  1285. mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1286. /*
  1287. * since I'm disabling all multicast entirely, I need to
  1288. * clear the multicast list
  1289. */
  1290. memset(multicast_table, 0, sizeof(multicast_table));
  1291. update_multicast = 1;
  1292. }
  1293. spin_lock_irqsave(&lp->lock, flags);
  1294. SMC_SET_MAC_CR(mcr);
  1295. if (update_multicast) {
  1296. DBG(SMC_DEBUG_MISC,
  1297. "%s: update mcast hash table 0x%08x 0x%08x\n",
  1298. dev->name, multicast_table[0], multicast_table[1]);
  1299. SMC_SET_HASHL(multicast_table[0]);
  1300. SMC_SET_HASHH(multicast_table[1]);
  1301. }
  1302. spin_unlock_irqrestore(&lp->lock, flags);
  1303. }
  1304. /*
  1305. * Open and Initialize the board
  1306. *
  1307. * Set up everything, reset the card, etc..
  1308. */
  1309. static int
  1310. smc911x_open(struct net_device *dev)
  1311. {
  1312. struct smc911x_local *lp = netdev_priv(dev);
  1313. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1314. /*
  1315. * Check that the address is valid. If its not, refuse
  1316. * to bring the device up. The user must specify an
  1317. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  1318. */
  1319. if (!is_valid_ether_addr(dev->dev_addr)) {
  1320. PRINTK("%s: no valid ethernet hw addr\n", __FUNCTION__);
  1321. return -EINVAL;
  1322. }
  1323. /* reset the hardware */
  1324. smc911x_reset(dev);
  1325. /* Configure the PHY, initialize the link state */
  1326. smc911x_phy_configure(&lp->phy_configure);
  1327. /* Turn on Tx + Rx */
  1328. smc911x_enable(dev);
  1329. netif_start_queue(dev);
  1330. return 0;
  1331. }
  1332. /*
  1333. * smc911x_close
  1334. *
  1335. * this makes the board clean up everything that it can
  1336. * and not talk to the outside world. Caused by
  1337. * an 'ifconfig ethX down'
  1338. */
  1339. static int smc911x_close(struct net_device *dev)
  1340. {
  1341. struct smc911x_local *lp = netdev_priv(dev);
  1342. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1343. netif_stop_queue(dev);
  1344. netif_carrier_off(dev);
  1345. /* clear everything */
  1346. smc911x_shutdown(dev);
  1347. if (lp->phy_type != 0) {
  1348. /* We need to ensure that no calls to
  1349. * smc911x_phy_configure are pending.
  1350. * flush_scheduled_work() cannot be called because we
  1351. * are running with the netlink semaphore held (from
  1352. * devinet_ioctl()) and the pending work queue
  1353. * contains linkwatch_event() (scheduled by
  1354. * netif_carrier_off() above). linkwatch_event() also
  1355. * wants the netlink semaphore.
  1356. */
  1357. while (lp->work_pending)
  1358. schedule();
  1359. smc911x_phy_powerdown(dev, lp->mii.phy_id);
  1360. }
  1361. if (lp->pending_tx_skb) {
  1362. dev_kfree_skb(lp->pending_tx_skb);
  1363. lp->pending_tx_skb = NULL;
  1364. }
  1365. return 0;
  1366. }
  1367. /*
  1368. * Get the current statistics.
  1369. * This may be called with the card open or closed.
  1370. */
  1371. static struct net_device_stats *smc911x_query_statistics(struct net_device *dev)
  1372. {
  1373. struct smc911x_local *lp = netdev_priv(dev);
  1374. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1375. return &lp->stats;
  1376. }
  1377. /*
  1378. * Ethtool support
  1379. */
  1380. static int
  1381. smc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1382. {
  1383. struct smc911x_local *lp = netdev_priv(dev);
  1384. unsigned long ioaddr = dev->base_addr;
  1385. int ret, status;
  1386. unsigned long flags;
  1387. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1388. cmd->maxtxpkt = 1;
  1389. cmd->maxrxpkt = 1;
  1390. if (lp->phy_type != 0) {
  1391. spin_lock_irqsave(&lp->lock, flags);
  1392. ret = mii_ethtool_gset(&lp->mii, cmd);
  1393. spin_unlock_irqrestore(&lp->lock, flags);
  1394. } else {
  1395. cmd->supported = SUPPORTED_10baseT_Half |
  1396. SUPPORTED_10baseT_Full |
  1397. SUPPORTED_TP | SUPPORTED_AUI;
  1398. if (lp->ctl_rspeed == 10)
  1399. cmd->speed = SPEED_10;
  1400. else if (lp->ctl_rspeed == 100)
  1401. cmd->speed = SPEED_100;
  1402. cmd->autoneg = AUTONEG_DISABLE;
  1403. if (lp->mii.phy_id==1)
  1404. cmd->transceiver = XCVR_INTERNAL;
  1405. else
  1406. cmd->transceiver = XCVR_EXTERNAL;
  1407. cmd->port = 0;
  1408. SMC_GET_PHY_SPECIAL(lp->mii.phy_id, status);
  1409. cmd->duplex =
  1410. (status & (PHY_SPECIAL_SPD_10FULL_ | PHY_SPECIAL_SPD_100FULL_)) ?
  1411. DUPLEX_FULL : DUPLEX_HALF;
  1412. ret = 0;
  1413. }
  1414. return ret;
  1415. }
  1416. static int
  1417. smc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1418. {
  1419. struct smc911x_local *lp = netdev_priv(dev);
  1420. int ret;
  1421. unsigned long flags;
  1422. if (lp->phy_type != 0) {
  1423. spin_lock_irqsave(&lp->lock, flags);
  1424. ret = mii_ethtool_sset(&lp->mii, cmd);
  1425. spin_unlock_irqrestore(&lp->lock, flags);
  1426. } else {
  1427. if (cmd->autoneg != AUTONEG_DISABLE ||
  1428. cmd->speed != SPEED_10 ||
  1429. (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
  1430. (cmd->port != PORT_TP && cmd->port != PORT_AUI))
  1431. return -EINVAL;
  1432. lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
  1433. ret = 0;
  1434. }
  1435. return ret;
  1436. }
  1437. static void
  1438. smc911x_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 smc911x_ethtool_nwayreset(struct net_device *dev)
  1445. {
  1446. struct smc911x_local *lp = netdev_priv(dev);
  1447. int ret = -EINVAL;
  1448. unsigned long flags;
  1449. if (lp->phy_type != 0) {
  1450. spin_lock_irqsave(&lp->lock, flags);
  1451. ret = mii_nway_restart(&lp->mii);
  1452. spin_unlock_irqrestore(&lp->lock, flags);
  1453. }
  1454. return ret;
  1455. }
  1456. static u32 smc911x_ethtool_getmsglevel(struct net_device *dev)
  1457. {
  1458. struct smc911x_local *lp = netdev_priv(dev);
  1459. return lp->msg_enable;
  1460. }
  1461. static void smc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
  1462. {
  1463. struct smc911x_local *lp = netdev_priv(dev);
  1464. lp->msg_enable = level;
  1465. }
  1466. static int smc911x_ethtool_getregslen(struct net_device *dev)
  1467. {
  1468. /* System regs + MAC regs + PHY regs */
  1469. return (((E2P_CMD - ID_REV)/4 + 1) +
  1470. (WUCSR - MAC_CR)+1 + 32) * sizeof(u32);
  1471. }
  1472. static void smc911x_ethtool_getregs(struct net_device *dev,
  1473. struct ethtool_regs* regs, void *buf)
  1474. {
  1475. unsigned long ioaddr = dev->base_addr;
  1476. struct smc911x_local *lp = netdev_priv(dev);
  1477. unsigned long flags;
  1478. u32 reg,i,j=0;
  1479. u32 *data = (u32*)buf;
  1480. regs->version = lp->version;
  1481. for(i=ID_REV;i<=E2P_CMD;i+=4) {
  1482. data[j++] = SMC_inl(ioaddr,i);
  1483. }
  1484. for(i=MAC_CR;i<=WUCSR;i++) {
  1485. spin_lock_irqsave(&lp->lock, flags);
  1486. SMC_GET_MAC_CSR(i, reg);
  1487. spin_unlock_irqrestore(&lp->lock, flags);
  1488. data[j++] = reg;
  1489. }
  1490. for(i=0;i<=31;i++) {
  1491. spin_lock_irqsave(&lp->lock, flags);
  1492. SMC_GET_MII(i, lp->mii.phy_id, reg);
  1493. spin_unlock_irqrestore(&lp->lock, flags);
  1494. data[j++] = reg & 0xFFFF;
  1495. }
  1496. }
  1497. static int smc911x_ethtool_wait_eeprom_ready(struct net_device *dev)
  1498. {
  1499. unsigned long ioaddr = dev->base_addr;
  1500. unsigned int timeout;
  1501. int e2p_cmd;
  1502. e2p_cmd = SMC_GET_E2P_CMD();
  1503. for(timeout=10;(e2p_cmd & E2P_CMD_EPC_BUSY_) && timeout; timeout--) {
  1504. if (e2p_cmd & E2P_CMD_EPC_TIMEOUT_) {
  1505. PRINTK("%s: %s timeout waiting for EEPROM to respond\n",
  1506. dev->name, __FUNCTION__);
  1507. return -EFAULT;
  1508. }
  1509. mdelay(1);
  1510. e2p_cmd = SMC_GET_E2P_CMD();
  1511. }
  1512. if (timeout == 0) {
  1513. PRINTK("%s: %s timeout waiting for EEPROM CMD not busy\n",
  1514. dev->name, __FUNCTION__);
  1515. return -ETIMEDOUT;
  1516. }
  1517. return 0;
  1518. }
  1519. static inline int smc911x_ethtool_write_eeprom_cmd(struct net_device *dev,
  1520. int cmd, int addr)
  1521. {
  1522. unsigned long ioaddr = dev->base_addr;
  1523. int ret;
  1524. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1525. return ret;
  1526. SMC_SET_E2P_CMD(E2P_CMD_EPC_BUSY_ |
  1527. ((cmd) & (0x7<<28)) |
  1528. ((addr) & 0xFF));
  1529. return 0;
  1530. }
  1531. static inline int smc911x_ethtool_read_eeprom_byte(struct net_device *dev,
  1532. u8 *data)
  1533. {
  1534. unsigned long ioaddr = dev->base_addr;
  1535. int ret;
  1536. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1537. return ret;
  1538. *data = SMC_GET_E2P_DATA();
  1539. return 0;
  1540. }
  1541. static inline int smc911x_ethtool_write_eeprom_byte(struct net_device *dev,
  1542. u8 data)
  1543. {
  1544. unsigned long ioaddr = dev->base_addr;
  1545. int ret;
  1546. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1547. return ret;
  1548. SMC_SET_E2P_DATA(data);
  1549. return 0;
  1550. }
  1551. static int smc911x_ethtool_geteeprom(struct net_device *dev,
  1552. struct ethtool_eeprom *eeprom, u8 *data)
  1553. {
  1554. u8 eebuf[SMC911X_EEPROM_LEN];
  1555. int i, ret;
  1556. for(i=0;i<SMC911X_EEPROM_LEN;i++) {
  1557. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ_, i ))!=0)
  1558. return ret;
  1559. if ((ret=smc911x_ethtool_read_eeprom_byte(dev, &eebuf[i]))!=0)
  1560. return ret;
  1561. }
  1562. memcpy(data, eebuf+eeprom->offset, eeprom->len);
  1563. return 0;
  1564. }
  1565. static int smc911x_ethtool_seteeprom(struct net_device *dev,
  1566. struct ethtool_eeprom *eeprom, u8 *data)
  1567. {
  1568. int i, ret;
  1569. /* Enable erase */
  1570. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN_, 0 ))!=0)
  1571. return ret;
  1572. for(i=eeprom->offset;i<(eeprom->offset+eeprom->len);i++) {
  1573. /* erase byte */
  1574. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE_, i ))!=0)
  1575. return ret;
  1576. /* write byte */
  1577. if ((ret=smc911x_ethtool_write_eeprom_byte(dev, *data))!=0)
  1578. return ret;
  1579. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE_, i ))!=0)
  1580. return ret;
  1581. }
  1582. return 0;
  1583. }
  1584. static int smc911x_ethtool_geteeprom_len(struct net_device *dev)
  1585. {
  1586. return SMC911X_EEPROM_LEN;
  1587. }
  1588. static const struct ethtool_ops smc911x_ethtool_ops = {
  1589. .get_settings = smc911x_ethtool_getsettings,
  1590. .set_settings = smc911x_ethtool_setsettings,
  1591. .get_drvinfo = smc911x_ethtool_getdrvinfo,
  1592. .get_msglevel = smc911x_ethtool_getmsglevel,
  1593. .set_msglevel = smc911x_ethtool_setmsglevel,
  1594. .nway_reset = smc911x_ethtool_nwayreset,
  1595. .get_link = ethtool_op_get_link,
  1596. .get_regs_len = smc911x_ethtool_getregslen,
  1597. .get_regs = smc911x_ethtool_getregs,
  1598. .get_eeprom_len = smc911x_ethtool_geteeprom_len,
  1599. .get_eeprom = smc911x_ethtool_geteeprom,
  1600. .set_eeprom = smc911x_ethtool_seteeprom,
  1601. };
  1602. /*
  1603. * smc911x_findirq
  1604. *
  1605. * This routine has a simple purpose -- make the SMC chip generate an
  1606. * interrupt, so an auto-detect routine can detect it, and find the IRQ,
  1607. */
  1608. static int __init smc911x_findirq(unsigned long ioaddr)
  1609. {
  1610. int timeout = 20;
  1611. unsigned long cookie;
  1612. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1613. cookie = probe_irq_on();
  1614. /*
  1615. * Force a SW interrupt
  1616. */
  1617. SMC_SET_INT_EN(INT_EN_SW_INT_EN_);
  1618. /*
  1619. * Wait until positive that the interrupt has been generated
  1620. */
  1621. do {
  1622. int int_status;
  1623. udelay(10);
  1624. int_status = SMC_GET_INT_EN();
  1625. if (int_status & INT_EN_SW_INT_EN_)
  1626. break; /* got the interrupt */
  1627. } while (--timeout);
  1628. /*
  1629. * there is really nothing that I can do here if timeout fails,
  1630. * as autoirq_report will return a 0 anyway, which is what I
  1631. * want in this case. Plus, the clean up is needed in both
  1632. * cases.
  1633. */
  1634. /* and disable all interrupts again */
  1635. SMC_SET_INT_EN(0);
  1636. /* and return what I found */
  1637. return probe_irq_off(cookie);
  1638. }
  1639. /*
  1640. * Function: smc911x_probe(unsigned long ioaddr)
  1641. *
  1642. * Purpose:
  1643. * Tests to see if a given ioaddr points to an SMC911x chip.
  1644. * Returns a 0 on success
  1645. *
  1646. * Algorithm:
  1647. * (1) see if the endian word is OK
  1648. * (1) see if I recognize the chip ID in the appropriate register
  1649. *
  1650. * Here I do typical initialization tasks.
  1651. *
  1652. * o Initialize the structure if needed
  1653. * o print out my vanity message if not done so already
  1654. * o print out what type of hardware is detected
  1655. * o print out the ethernet address
  1656. * o find the IRQ
  1657. * o set up my private data
  1658. * o configure the dev structure with my subroutines
  1659. * o actually GRAB the irq.
  1660. * o GRAB the region
  1661. */
  1662. static int __init smc911x_probe(struct net_device *dev, unsigned long ioaddr)
  1663. {
  1664. struct smc911x_local *lp = netdev_priv(dev);
  1665. int i, retval;
  1666. unsigned int val, chip_id, revision;
  1667. const char *version_string;
  1668. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1669. /* First, see if the endian word is recognized */
  1670. val = SMC_GET_BYTE_TEST();
  1671. DBG(SMC_DEBUG_MISC, "%s: endian probe returned 0x%04x\n", CARDNAME, val);
  1672. if (val != 0x87654321) {
  1673. printk(KERN_ERR "Invalid chip endian 0x08%x\n",val);
  1674. retval = -ENODEV;
  1675. goto err_out;
  1676. }
  1677. /*
  1678. * check if the revision register is something that I
  1679. * recognize. These might need to be added to later,
  1680. * as future revisions could be added.
  1681. */
  1682. chip_id = SMC_GET_PN();
  1683. DBG(SMC_DEBUG_MISC, "%s: id probe returned 0x%04x\n", CARDNAME, chip_id);
  1684. for(i=0;chip_ids[i].id != 0; i++) {
  1685. if (chip_ids[i].id == chip_id) break;
  1686. }
  1687. if (!chip_ids[i].id) {
  1688. printk(KERN_ERR "Unknown chip ID %04x\n", chip_id);
  1689. retval = -ENODEV;
  1690. goto err_out;
  1691. }
  1692. version_string = chip_ids[i].name;
  1693. revision = SMC_GET_REV();
  1694. DBG(SMC_DEBUG_MISC, "%s: revision = 0x%04x\n", CARDNAME, revision);
  1695. /* At this point I'll assume that the chip is an SMC911x. */
  1696. DBG(SMC_DEBUG_MISC, "%s: Found a %s\n", CARDNAME, chip_ids[i].name);
  1697. /* Validate the TX FIFO size requested */
  1698. if ((tx_fifo_kb < 2) || (tx_fifo_kb > 14)) {
  1699. printk(KERN_ERR "Invalid TX FIFO size requested %d\n", tx_fifo_kb);
  1700. retval = -EINVAL;
  1701. goto err_out;
  1702. }
  1703. /* fill in some of the fields */
  1704. dev->base_addr = ioaddr;
  1705. lp->version = chip_ids[i].id;
  1706. lp->revision = revision;
  1707. lp->tx_fifo_kb = tx_fifo_kb;
  1708. /* Reverse calculate the RX FIFO size from the TX */
  1709. lp->tx_fifo_size=(lp->tx_fifo_kb<<10) - 512;
  1710. lp->rx_fifo_size= ((0x4000 - 512 - lp->tx_fifo_size) / 16) * 15;
  1711. /* Set the automatic flow control values */
  1712. switch(lp->tx_fifo_kb) {
  1713. /*
  1714. * AFC_HI is about ((Rx Data Fifo Size)*2/3)/64
  1715. * AFC_LO is AFC_HI/2
  1716. * BACK_DUR is about 5uS*(AFC_LO) rounded down
  1717. */
  1718. case 2:/* 13440 Rx Data Fifo Size */
  1719. lp->afc_cfg=0x008C46AF;break;
  1720. case 3:/* 12480 Rx Data Fifo Size */
  1721. lp->afc_cfg=0x0082419F;break;
  1722. case 4:/* 11520 Rx Data Fifo Size */
  1723. lp->afc_cfg=0x00783C9F;break;
  1724. case 5:/* 10560 Rx Data Fifo Size */
  1725. lp->afc_cfg=0x006E374F;break;
  1726. case 6:/* 9600 Rx Data Fifo Size */
  1727. lp->afc_cfg=0x0064328F;break;
  1728. case 7:/* 8640 Rx Data Fifo Size */
  1729. lp->afc_cfg=0x005A2D7F;break;
  1730. case 8:/* 7680 Rx Data Fifo Size */
  1731. lp->afc_cfg=0x0050287F;break;
  1732. case 9:/* 6720 Rx Data Fifo Size */
  1733. lp->afc_cfg=0x0046236F;break;
  1734. case 10:/* 5760 Rx Data Fifo Size */
  1735. lp->afc_cfg=0x003C1E6F;break;
  1736. case 11:/* 4800 Rx Data Fifo Size */
  1737. lp->afc_cfg=0x0032195F;break;
  1738. /*
  1739. * AFC_HI is ~1520 bytes less than RX Data Fifo Size
  1740. * AFC_LO is AFC_HI/2
  1741. * BACK_DUR is about 5uS*(AFC_LO) rounded down
  1742. */
  1743. case 12:/* 3840 Rx Data Fifo Size */
  1744. lp->afc_cfg=0x0024124F;break;
  1745. case 13:/* 2880 Rx Data Fifo Size */
  1746. lp->afc_cfg=0x0015073F;break;
  1747. case 14:/* 1920 Rx Data Fifo Size */
  1748. lp->afc_cfg=0x0006032F;break;
  1749. default:
  1750. PRINTK("%s: ERROR -- no AFC_CFG setting found",
  1751. dev->name);
  1752. break;
  1753. }
  1754. DBG(SMC_DEBUG_MISC | SMC_DEBUG_TX | SMC_DEBUG_RX,
  1755. "%s: tx_fifo %d rx_fifo %d afc_cfg 0x%08x\n", CARDNAME,
  1756. lp->tx_fifo_size, lp->rx_fifo_size, lp->afc_cfg);
  1757. spin_lock_init(&lp->lock);
  1758. /* Get the MAC address */
  1759. SMC_GET_MAC_ADDR(dev->dev_addr);
  1760. /* now, reset the chip, and put it into a known state */
  1761. smc911x_reset(dev);
  1762. /*
  1763. * If dev->irq is 0, then the device has to be banged on to see
  1764. * what the IRQ is.
  1765. *
  1766. * Specifying an IRQ is done with the assumption that the user knows
  1767. * what (s)he is doing. No checking is done!!!!
  1768. */
  1769. if (dev->irq < 1) {
  1770. int trials;
  1771. trials = 3;
  1772. while (trials--) {
  1773. dev->irq = smc911x_findirq(ioaddr);
  1774. if (dev->irq)
  1775. break;
  1776. /* kick the card and try again */
  1777. smc911x_reset(dev);
  1778. }
  1779. }
  1780. if (dev->irq == 0) {
  1781. printk("%s: Couldn't autodetect your IRQ. Use irq=xx.\n",
  1782. dev->name);
  1783. retval = -ENODEV;
  1784. goto err_out;
  1785. }
  1786. dev->irq = irq_canonicalize(dev->irq);
  1787. /* Fill in the fields of the device structure with ethernet values. */
  1788. ether_setup(dev);
  1789. dev->open = smc911x_open;
  1790. dev->stop = smc911x_close;
  1791. dev->hard_start_xmit = smc911x_hard_start_xmit;
  1792. dev->tx_timeout = smc911x_timeout;
  1793. dev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1794. dev->get_stats = smc911x_query_statistics;
  1795. dev->set_multicast_list = smc911x_set_multicast_list;
  1796. dev->ethtool_ops = &smc911x_ethtool_ops;
  1797. #ifdef CONFIG_NET_POLL_CONTROLLER
  1798. dev->poll_controller = smc911x_poll_controller;
  1799. #endif
  1800. INIT_WORK(&lp->phy_configure, smc911x_phy_configure);
  1801. lp->mii.phy_id_mask = 0x1f;
  1802. lp->mii.reg_num_mask = 0x1f;
  1803. lp->mii.force_media = 0;
  1804. lp->mii.full_duplex = 0;
  1805. lp->mii.dev = dev;
  1806. lp->mii.mdio_read = smc911x_phy_read;
  1807. lp->mii.mdio_write = smc911x_phy_write;
  1808. /*
  1809. * Locate the phy, if any.
  1810. */
  1811. smc911x_phy_detect(dev);
  1812. /* Set default parameters */
  1813. lp->msg_enable = NETIF_MSG_LINK;
  1814. lp->ctl_rfduplx = 1;
  1815. lp->ctl_rspeed = 100;
  1816. /* Grab the IRQ */
  1817. retval = request_irq(dev->irq, &smc911x_interrupt, IRQF_SHARED, dev->name, dev);
  1818. if (retval)
  1819. goto err_out;
  1820. set_irq_type(dev->irq, IRQT_FALLING);
  1821. #ifdef SMC_USE_DMA
  1822. lp->rxdma = SMC_DMA_REQUEST(dev, smc911x_rx_dma_irq);
  1823. lp->txdma = SMC_DMA_REQUEST(dev, smc911x_tx_dma_irq);
  1824. lp->rxdma_active = 0;
  1825. lp->txdma_active = 0;
  1826. dev->dma = lp->rxdma;
  1827. #endif
  1828. retval = register_netdev(dev);
  1829. if (retval == 0) {
  1830. /* now, print out the card info, in a short format.. */
  1831. printk("%s: %s (rev %d) at %#lx IRQ %d",
  1832. dev->name, version_string, lp->revision,
  1833. dev->base_addr, dev->irq);
  1834. #ifdef SMC_USE_DMA
  1835. if (lp->rxdma != -1)
  1836. printk(" RXDMA %d ", lp->rxdma);
  1837. if (lp->txdma != -1)
  1838. printk("TXDMA %d", lp->txdma);
  1839. #endif
  1840. printk("\n");
  1841. if (!is_valid_ether_addr(dev->dev_addr)) {
  1842. printk("%s: Invalid ethernet MAC address. Please "
  1843. "set using ifconfig\n", dev->name);
  1844. } else {
  1845. /* Print the Ethernet address */
  1846. printk("%s: Ethernet addr: ", dev->name);
  1847. for (i = 0; i < 5; i++)
  1848. printk("%2.2x:", dev->dev_addr[i]);
  1849. printk("%2.2x\n", dev->dev_addr[5]);
  1850. }
  1851. if (lp->phy_type == 0) {
  1852. PRINTK("%s: No PHY found\n", dev->name);
  1853. } else if ((lp->phy_type & ~0xff) == LAN911X_INTERNAL_PHY_ID) {
  1854. PRINTK("%s: LAN911x Internal PHY\n", dev->name);
  1855. } else {
  1856. PRINTK("%s: External PHY 0x%08x\n", dev->name, lp->phy_type);
  1857. }
  1858. }
  1859. err_out:
  1860. #ifdef SMC_USE_DMA
  1861. if (retval) {
  1862. if (lp->rxdma != -1) {
  1863. SMC_DMA_FREE(dev, lp->rxdma);
  1864. }
  1865. if (lp->txdma != -1) {
  1866. SMC_DMA_FREE(dev, lp->txdma);
  1867. }
  1868. }
  1869. #endif
  1870. return retval;
  1871. }
  1872. /*
  1873. * smc911x_init(void)
  1874. *
  1875. * Output:
  1876. * 0 --> there is a device
  1877. * anything else, error
  1878. */
  1879. static int smc911x_drv_probe(struct platform_device *pdev)
  1880. {
  1881. struct net_device *ndev;
  1882. struct resource *res;
  1883. struct smc911x_local *lp;
  1884. unsigned int *addr;
  1885. int ret;
  1886. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1887. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1888. if (!res) {
  1889. ret = -ENODEV;
  1890. goto out;
  1891. }
  1892. /*
  1893. * Request the regions.
  1894. */
  1895. if (!request_mem_region(res->start, SMC911X_IO_EXTENT, CARDNAME)) {
  1896. ret = -EBUSY;
  1897. goto out;
  1898. }
  1899. ndev = alloc_etherdev(sizeof(struct smc911x_local));
  1900. if (!ndev) {
  1901. printk("%s: could not allocate device.\n", CARDNAME);
  1902. ret = -ENOMEM;
  1903. goto release_1;
  1904. }
  1905. SET_MODULE_OWNER(ndev);
  1906. SET_NETDEV_DEV(ndev, &pdev->dev);
  1907. ndev->dma = (unsigned char)-1;
  1908. ndev->irq = platform_get_irq(pdev, 0);
  1909. lp = netdev_priv(ndev);
  1910. lp->netdev = ndev;
  1911. addr = ioremap(res->start, SMC911X_IO_EXTENT);
  1912. if (!addr) {
  1913. ret = -ENOMEM;
  1914. goto release_both;
  1915. }
  1916. platform_set_drvdata(pdev, ndev);
  1917. ret = smc911x_probe(ndev, (unsigned long)addr);
  1918. if (ret != 0) {
  1919. platform_set_drvdata(pdev, NULL);
  1920. iounmap(addr);
  1921. release_both:
  1922. free_netdev(ndev);
  1923. release_1:
  1924. release_mem_region(res->start, SMC911X_IO_EXTENT);
  1925. out:
  1926. printk("%s: not found (%d).\n", CARDNAME, ret);
  1927. }
  1928. #ifdef SMC_USE_DMA
  1929. else {
  1930. lp->physaddr = res->start;
  1931. lp->dev = &pdev->dev;
  1932. }
  1933. #endif
  1934. return ret;
  1935. }
  1936. static int smc911x_drv_remove(struct platform_device *pdev)
  1937. {
  1938. struct net_device *ndev = platform_get_drvdata(pdev);
  1939. struct resource *res;
  1940. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1941. platform_set_drvdata(pdev, NULL);
  1942. unregister_netdev(ndev);
  1943. free_irq(ndev->irq, ndev);
  1944. #ifdef SMC_USE_DMA
  1945. {
  1946. struct smc911x_local *lp = netdev_priv(ndev);
  1947. if (lp->rxdma != -1) {
  1948. SMC_DMA_FREE(dev, lp->rxdma);
  1949. }
  1950. if (lp->txdma != -1) {
  1951. SMC_DMA_FREE(dev, lp->txdma);
  1952. }
  1953. }
  1954. #endif
  1955. iounmap((void *)ndev->base_addr);
  1956. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1957. release_mem_region(res->start, SMC911X_IO_EXTENT);
  1958. free_netdev(ndev);
  1959. return 0;
  1960. }
  1961. static int smc911x_drv_suspend(struct platform_device *dev, pm_message_t state)
  1962. {
  1963. struct net_device *ndev = platform_get_drvdata(dev);
  1964. unsigned long ioaddr = ndev->base_addr;
  1965. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1966. if (ndev) {
  1967. if (netif_running(ndev)) {
  1968. netif_device_detach(ndev);
  1969. smc911x_shutdown(ndev);
  1970. #if POWER_DOWN
  1971. /* Set D2 - Energy detect only setting */
  1972. SMC_SET_PMT_CTRL(2<<12);
  1973. #endif
  1974. }
  1975. }
  1976. return 0;
  1977. }
  1978. static int smc911x_drv_resume(struct platform_device *dev)
  1979. {
  1980. struct net_device *ndev = platform_get_drvdata(dev);
  1981. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1982. if (ndev) {
  1983. struct smc911x_local *lp = netdev_priv(ndev);
  1984. if (netif_running(ndev)) {
  1985. smc911x_reset(ndev);
  1986. smc911x_enable(ndev);
  1987. if (lp->phy_type != 0)
  1988. smc911x_phy_configure(&lp->phy_configure);
  1989. netif_device_attach(ndev);
  1990. }
  1991. }
  1992. return 0;
  1993. }
  1994. static struct platform_driver smc911x_driver = {
  1995. .probe = smc911x_drv_probe,
  1996. .remove = smc911x_drv_remove,
  1997. .suspend = smc911x_drv_suspend,
  1998. .resume = smc911x_drv_resume,
  1999. .driver = {
  2000. .name = CARDNAME,
  2001. },
  2002. };
  2003. static int __init smc911x_init(void)
  2004. {
  2005. return platform_driver_register(&smc911x_driver);
  2006. }
  2007. static void __exit smc911x_cleanup(void)
  2008. {
  2009. platform_driver_unregister(&smc911x_driver);
  2010. }
  2011. module_init(smc911x_init);
  2012. module_exit(smc911x_cleanup);