smc911x.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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_nolock;
  852. if (smc911x_phy_reset(dev, phyaddr)) {
  853. printk("%s: PHY reset timed out\n", dev->name);
  854. goto smc911x_phy_configure_exit_nolock;
  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. smc911x_phy_configure_exit_nolock:
  911. lp->work_pending = 0;
  912. }
  913. /*
  914. * smc911x_phy_interrupt
  915. *
  916. * Purpose: Handle interrupts relating to PHY register 18. This is
  917. * called from the "hard" interrupt handler under our private spinlock.
  918. */
  919. static void smc911x_phy_interrupt(struct net_device *dev)
  920. {
  921. struct smc911x_local *lp = netdev_priv(dev);
  922. unsigned long ioaddr = dev->base_addr;
  923. int phyaddr = lp->mii.phy_id;
  924. int status;
  925. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  926. if (lp->phy_type == 0)
  927. return;
  928. smc911x_phy_check_media(dev, 0);
  929. /* read to clear status bits */
  930. SMC_GET_PHY_INT_SRC(phyaddr,status);
  931. DBG(SMC_DEBUG_MISC, "%s: PHY interrupt status 0x%04x\n",
  932. dev->name, status & 0xffff);
  933. DBG(SMC_DEBUG_MISC, "%s: AFC_CFG 0x%08x\n",
  934. dev->name, SMC_GET_AFC_CFG());
  935. }
  936. /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
  937. /*
  938. * This is the main routine of the driver, to handle the device when
  939. * it needs some attention.
  940. */
  941. static irqreturn_t smc911x_interrupt(int irq, void *dev_id)
  942. {
  943. struct net_device *dev = dev_id;
  944. unsigned long ioaddr = dev->base_addr;
  945. struct smc911x_local *lp = netdev_priv(dev);
  946. unsigned int status, mask, timeout;
  947. unsigned int rx_overrun=0, cr, pkts;
  948. unsigned long flags;
  949. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  950. spin_lock_irqsave(&lp->lock, flags);
  951. /* Spurious interrupt check */
  952. if ((SMC_GET_IRQ_CFG() & (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) !=
  953. (INT_CFG_IRQ_INT_ | INT_CFG_IRQ_EN_)) {
  954. spin_unlock_irqrestore(&lp->lock, flags);
  955. return IRQ_NONE;
  956. }
  957. mask = SMC_GET_INT_EN();
  958. SMC_SET_INT_EN(0);
  959. /* set a timeout value, so I don't stay here forever */
  960. timeout = 8;
  961. do {
  962. status = SMC_GET_INT();
  963. DBG(SMC_DEBUG_MISC, "%s: INT 0x%08x MASK 0x%08x OUTSIDE MASK 0x%08x\n",
  964. dev->name, status, mask, status & ~mask);
  965. status &= mask;
  966. if (!status)
  967. break;
  968. /* Handle SW interrupt condition */
  969. if (status & INT_STS_SW_INT_) {
  970. SMC_ACK_INT(INT_STS_SW_INT_);
  971. mask &= ~INT_EN_SW_INT_EN_;
  972. }
  973. /* Handle various error conditions */
  974. if (status & INT_STS_RXE_) {
  975. SMC_ACK_INT(INT_STS_RXE_);
  976. lp->stats.rx_errors++;
  977. }
  978. if (status & INT_STS_RXDFH_INT_) {
  979. SMC_ACK_INT(INT_STS_RXDFH_INT_);
  980. lp->stats.rx_dropped+=SMC_GET_RX_DROP();
  981. }
  982. /* Undocumented interrupt-what is the right thing to do here? */
  983. if (status & INT_STS_RXDF_INT_) {
  984. SMC_ACK_INT(INT_STS_RXDF_INT_);
  985. }
  986. /* Rx Data FIFO exceeds set level */
  987. if (status & INT_STS_RDFL_) {
  988. if (IS_REV_A(lp->revision)) {
  989. rx_overrun=1;
  990. SMC_GET_MAC_CR(cr);
  991. cr &= ~MAC_CR_RXEN_;
  992. SMC_SET_MAC_CR(cr);
  993. DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
  994. lp->stats.rx_errors++;
  995. lp->stats.rx_fifo_errors++;
  996. }
  997. SMC_ACK_INT(INT_STS_RDFL_);
  998. }
  999. if (status & INT_STS_RDFO_) {
  1000. if (!IS_REV_A(lp->revision)) {
  1001. SMC_GET_MAC_CR(cr);
  1002. cr &= ~MAC_CR_RXEN_;
  1003. SMC_SET_MAC_CR(cr);
  1004. rx_overrun=1;
  1005. DBG(SMC_DEBUG_RX, "%s: RX overrun\n", dev->name);
  1006. lp->stats.rx_errors++;
  1007. lp->stats.rx_fifo_errors++;
  1008. }
  1009. SMC_ACK_INT(INT_STS_RDFO_);
  1010. }
  1011. /* Handle receive condition */
  1012. if ((status & INT_STS_RSFL_) || rx_overrun) {
  1013. unsigned int fifo;
  1014. DBG(SMC_DEBUG_RX, "%s: RX irq\n", dev->name);
  1015. fifo = SMC_GET_RX_FIFO_INF();
  1016. pkts = (fifo & RX_FIFO_INF_RXSUSED_) >> 16;
  1017. DBG(SMC_DEBUG_RX, "%s: Rx FIFO pkts %d, bytes %d\n",
  1018. dev->name, pkts, fifo & 0xFFFF );
  1019. if (pkts != 0) {
  1020. #ifdef SMC_USE_DMA
  1021. unsigned int fifo;
  1022. if (lp->rxdma_active){
  1023. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
  1024. "%s: RX DMA active\n", dev->name);
  1025. /* The DMA is already running so up the IRQ threshold */
  1026. fifo = SMC_GET_FIFO_INT() & ~0xFF;
  1027. fifo |= pkts & 0xFF;
  1028. DBG(SMC_DEBUG_RX,
  1029. "%s: Setting RX stat FIFO threshold to %d\n",
  1030. dev->name, fifo & 0xff);
  1031. SMC_SET_FIFO_INT(fifo);
  1032. } else
  1033. #endif
  1034. smc911x_rcv(dev);
  1035. }
  1036. SMC_ACK_INT(INT_STS_RSFL_);
  1037. }
  1038. /* Handle transmit FIFO available */
  1039. if (status & INT_STS_TDFA_) {
  1040. DBG(SMC_DEBUG_TX, "%s: TX data FIFO space available irq\n", dev->name);
  1041. SMC_SET_FIFO_TDA(0xFF);
  1042. lp->tx_throttle = 0;
  1043. #ifdef SMC_USE_DMA
  1044. if (!lp->txdma_active)
  1045. #endif
  1046. netif_wake_queue(dev);
  1047. SMC_ACK_INT(INT_STS_TDFA_);
  1048. }
  1049. /* Handle transmit done condition */
  1050. #if 1
  1051. if (status & (INT_STS_TSFL_ | INT_STS_GPT_INT_)) {
  1052. DBG(SMC_DEBUG_TX | SMC_DEBUG_MISC,
  1053. "%s: Tx stat FIFO limit (%d) /GPT irq\n",
  1054. dev->name, (SMC_GET_FIFO_INT() & 0x00ff0000) >> 16);
  1055. smc911x_tx(dev);
  1056. SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
  1057. SMC_ACK_INT(INT_STS_TSFL_);
  1058. SMC_ACK_INT(INT_STS_TSFL_ | INT_STS_GPT_INT_);
  1059. }
  1060. #else
  1061. if (status & INT_STS_TSFL_) {
  1062. DBG(SMC_DEBUG_TX, "%s: TX status FIFO limit (%d) irq \n", dev->name, );
  1063. smc911x_tx(dev);
  1064. SMC_ACK_INT(INT_STS_TSFL_);
  1065. }
  1066. if (status & INT_STS_GPT_INT_) {
  1067. DBG(SMC_DEBUG_RX, "%s: IRQ_CFG 0x%08x FIFO_INT 0x%08x RX_CFG 0x%08x\n",
  1068. dev->name,
  1069. SMC_GET_IRQ_CFG(),
  1070. SMC_GET_FIFO_INT(),
  1071. SMC_GET_RX_CFG());
  1072. DBG(SMC_DEBUG_RX, "%s: Rx Stat FIFO Used 0x%02x "
  1073. "Data FIFO Used 0x%04x Stat FIFO 0x%08x\n",
  1074. dev->name,
  1075. (SMC_GET_RX_FIFO_INF() & 0x00ff0000) >> 16,
  1076. SMC_GET_RX_FIFO_INF() & 0xffff,
  1077. SMC_GET_RX_STS_FIFO_PEEK());
  1078. SMC_SET_GPT_CFG(GPT_CFG_TIMER_EN_ | 10000);
  1079. SMC_ACK_INT(INT_STS_GPT_INT_);
  1080. }
  1081. #endif
  1082. /* Handle PHY interupt condition */
  1083. if (status & INT_STS_PHY_INT_) {
  1084. DBG(SMC_DEBUG_MISC, "%s: PHY irq\n", dev->name);
  1085. smc911x_phy_interrupt(dev);
  1086. SMC_ACK_INT(INT_STS_PHY_INT_);
  1087. }
  1088. } while (--timeout);
  1089. /* restore mask state */
  1090. SMC_SET_INT_EN(mask);
  1091. DBG(SMC_DEBUG_MISC, "%s: Interrupt done (%d loops)\n",
  1092. dev->name, 8-timeout);
  1093. spin_unlock_irqrestore(&lp->lock, flags);
  1094. DBG(3, "%s: Interrupt done (%d loops)\n", dev->name, 8-timeout);
  1095. return IRQ_HANDLED;
  1096. }
  1097. #ifdef SMC_USE_DMA
  1098. static void
  1099. smc911x_tx_dma_irq(int dma, void *data)
  1100. {
  1101. struct net_device *dev = (struct net_device *)data;
  1102. struct smc911x_local *lp = netdev_priv(dev);
  1103. struct sk_buff *skb = lp->current_tx_skb;
  1104. unsigned long flags;
  1105. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1106. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA, "%s: TX DMA irq handler\n", dev->name);
  1107. /* Clear the DMA interrupt sources */
  1108. SMC_DMA_ACK_IRQ(dev, dma);
  1109. BUG_ON(skb == NULL);
  1110. dma_unmap_single(NULL, tx_dmabuf, tx_dmalen, DMA_TO_DEVICE);
  1111. dev->trans_start = jiffies;
  1112. dev_kfree_skb_irq(skb);
  1113. lp->current_tx_skb = NULL;
  1114. if (lp->pending_tx_skb != NULL)
  1115. smc911x_hardware_send_pkt(dev);
  1116. else {
  1117. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
  1118. "%s: No pending Tx packets. DMA disabled\n", dev->name);
  1119. spin_lock_irqsave(&lp->lock, flags);
  1120. lp->txdma_active = 0;
  1121. if (!lp->tx_throttle) {
  1122. netif_wake_queue(dev);
  1123. }
  1124. spin_unlock_irqrestore(&lp->lock, flags);
  1125. }
  1126. DBG(SMC_DEBUG_TX | SMC_DEBUG_DMA,
  1127. "%s: TX DMA irq completed\n", dev->name);
  1128. }
  1129. static void
  1130. smc911x_rx_dma_irq(int dma, void *data)
  1131. {
  1132. struct net_device *dev = (struct net_device *)data;
  1133. unsigned long ioaddr = dev->base_addr;
  1134. struct smc911x_local *lp = netdev_priv(dev);
  1135. struct sk_buff *skb = lp->current_rx_skb;
  1136. unsigned long flags;
  1137. unsigned int pkts;
  1138. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1139. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA, "%s: RX DMA irq handler\n", dev->name);
  1140. /* Clear the DMA interrupt sources */
  1141. SMC_DMA_ACK_IRQ(dev, dma);
  1142. dma_unmap_single(NULL, rx_dmabuf, rx_dmalen, DMA_FROM_DEVICE);
  1143. BUG_ON(skb == NULL);
  1144. lp->current_rx_skb = NULL;
  1145. PRINT_PKT(skb->data, skb->len);
  1146. dev->last_rx = jiffies;
  1147. skb->dev = dev;
  1148. skb->protocol = eth_type_trans(skb, dev);
  1149. netif_rx(skb);
  1150. lp->stats.rx_packets++;
  1151. lp->stats.rx_bytes += skb->len;
  1152. spin_lock_irqsave(&lp->lock, flags);
  1153. pkts = (SMC_GET_RX_FIFO_INF() & RX_FIFO_INF_RXSUSED_) >> 16;
  1154. if (pkts != 0) {
  1155. smc911x_rcv(dev);
  1156. }else {
  1157. lp->rxdma_active = 0;
  1158. }
  1159. spin_unlock_irqrestore(&lp->lock, flags);
  1160. DBG(SMC_DEBUG_RX | SMC_DEBUG_DMA,
  1161. "%s: RX DMA irq completed. DMA RX FIFO PKTS %d\n",
  1162. dev->name, pkts);
  1163. }
  1164. #endif /* SMC_USE_DMA */
  1165. #ifdef CONFIG_NET_POLL_CONTROLLER
  1166. /*
  1167. * Polling receive - used by netconsole and other diagnostic tools
  1168. * to allow network i/o with interrupts disabled.
  1169. */
  1170. static void smc911x_poll_controller(struct net_device *dev)
  1171. {
  1172. disable_irq(dev->irq);
  1173. smc911x_interrupt(dev->irq, dev);
  1174. enable_irq(dev->irq);
  1175. }
  1176. #endif
  1177. /* Our watchdog timed out. Called by the networking layer */
  1178. static void smc911x_timeout(struct net_device *dev)
  1179. {
  1180. struct smc911x_local *lp = netdev_priv(dev);
  1181. unsigned long ioaddr = dev->base_addr;
  1182. int status, mask;
  1183. unsigned long flags;
  1184. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1185. spin_lock_irqsave(&lp->lock, flags);
  1186. status = SMC_GET_INT();
  1187. mask = SMC_GET_INT_EN();
  1188. spin_unlock_irqrestore(&lp->lock, flags);
  1189. DBG(SMC_DEBUG_MISC, "%s: INT 0x%02x MASK 0x%02x \n",
  1190. dev->name, status, mask);
  1191. /* Dump the current TX FIFO contents and restart */
  1192. mask = SMC_GET_TX_CFG();
  1193. SMC_SET_TX_CFG(mask | TX_CFG_TXS_DUMP_ | TX_CFG_TXD_DUMP_);
  1194. /*
  1195. * Reconfiguring the PHY doesn't seem like a bad idea here, but
  1196. * smc911x_phy_configure() calls msleep() which calls schedule_timeout()
  1197. * which calls schedule(). Hence we use a work queue.
  1198. */
  1199. if (lp->phy_type != 0) {
  1200. if (schedule_work(&lp->phy_configure)) {
  1201. lp->work_pending = 1;
  1202. }
  1203. }
  1204. /* We can accept TX packets again */
  1205. dev->trans_start = jiffies;
  1206. netif_wake_queue(dev);
  1207. }
  1208. /*
  1209. * This routine will, depending on the values passed to it,
  1210. * either make it accept multicast packets, go into
  1211. * promiscuous mode (for TCPDUMP and cousins) or accept
  1212. * a select set of multicast packets
  1213. */
  1214. static void smc911x_set_multicast_list(struct net_device *dev)
  1215. {
  1216. struct smc911x_local *lp = netdev_priv(dev);
  1217. unsigned long ioaddr = dev->base_addr;
  1218. unsigned int multicast_table[2];
  1219. unsigned int mcr, update_multicast = 0;
  1220. unsigned long flags;
  1221. /* table for flipping the order of 5 bits */
  1222. static const unsigned char invert5[] =
  1223. {0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0C, 0x1C,
  1224. 0x02, 0x12, 0x0A, 0x1A, 0x06, 0x16, 0x0E, 0x1E,
  1225. 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0D, 0x1D,
  1226. 0x03, 0x13, 0x0B, 0x1B, 0x07, 0x17, 0x0F, 0x1F};
  1227. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1228. spin_lock_irqsave(&lp->lock, flags);
  1229. SMC_GET_MAC_CR(mcr);
  1230. spin_unlock_irqrestore(&lp->lock, flags);
  1231. if (dev->flags & IFF_PROMISC) {
  1232. DBG(SMC_DEBUG_MISC, "%s: RCR_PRMS\n", dev->name);
  1233. mcr |= MAC_CR_PRMS_;
  1234. }
  1235. /*
  1236. * Here, I am setting this to accept all multicast packets.
  1237. * I don't need to zero the multicast table, because the flag is
  1238. * checked before the table is
  1239. */
  1240. else if (dev->flags & IFF_ALLMULTI || dev->mc_count > 16) {
  1241. DBG(SMC_DEBUG_MISC, "%s: RCR_ALMUL\n", dev->name);
  1242. mcr |= MAC_CR_MCPAS_;
  1243. }
  1244. /*
  1245. * This sets the internal hardware table to filter out unwanted
  1246. * multicast packets before they take up memory.
  1247. *
  1248. * The SMC chip uses a hash table where the high 6 bits of the CRC of
  1249. * address are the offset into the table. If that bit is 1, then the
  1250. * multicast packet is accepted. Otherwise, it's dropped silently.
  1251. *
  1252. * To use the 6 bits as an offset into the table, the high 1 bit is
  1253. * the number of the 32 bit register, while the low 5 bits are the bit
  1254. * within that register.
  1255. */
  1256. else if (dev->mc_count) {
  1257. int i;
  1258. struct dev_mc_list *cur_addr;
  1259. /* Set the Hash perfec mode */
  1260. mcr |= MAC_CR_HPFILT_;
  1261. /* start with a table of all zeros: reject all */
  1262. memset(multicast_table, 0, sizeof(multicast_table));
  1263. cur_addr = dev->mc_list;
  1264. for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
  1265. int position;
  1266. /* do we have a pointer here? */
  1267. if (!cur_addr)
  1268. break;
  1269. /* make sure this is a multicast address -
  1270. shouldn't this be a given if we have it here ? */
  1271. if (!(*cur_addr->dmi_addr & 1))
  1272. continue;
  1273. /* only use the low order bits */
  1274. position = crc32_le(~0, cur_addr->dmi_addr, 6) & 0x3f;
  1275. /* do some messy swapping to put the bit in the right spot */
  1276. multicast_table[invert5[position&0x1F]&0x1] |=
  1277. (1<<invert5[(position>>1)&0x1F]);
  1278. }
  1279. /* be sure I get rid of flags I might have set */
  1280. mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1281. /* now, the table can be loaded into the chipset */
  1282. update_multicast = 1;
  1283. } else {
  1284. DBG(SMC_DEBUG_MISC, "%s: ~(MAC_CR_PRMS_|MAC_CR_MCPAS_)\n",
  1285. dev->name);
  1286. mcr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1287. /*
  1288. * since I'm disabling all multicast entirely, I need to
  1289. * clear the multicast list
  1290. */
  1291. memset(multicast_table, 0, sizeof(multicast_table));
  1292. update_multicast = 1;
  1293. }
  1294. spin_lock_irqsave(&lp->lock, flags);
  1295. SMC_SET_MAC_CR(mcr);
  1296. if (update_multicast) {
  1297. DBG(SMC_DEBUG_MISC,
  1298. "%s: update mcast hash table 0x%08x 0x%08x\n",
  1299. dev->name, multicast_table[0], multicast_table[1]);
  1300. SMC_SET_HASHL(multicast_table[0]);
  1301. SMC_SET_HASHH(multicast_table[1]);
  1302. }
  1303. spin_unlock_irqrestore(&lp->lock, flags);
  1304. }
  1305. /*
  1306. * Open and Initialize the board
  1307. *
  1308. * Set up everything, reset the card, etc..
  1309. */
  1310. static int
  1311. smc911x_open(struct net_device *dev)
  1312. {
  1313. struct smc911x_local *lp = netdev_priv(dev);
  1314. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1315. /*
  1316. * Check that the address is valid. If its not, refuse
  1317. * to bring the device up. The user must specify an
  1318. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  1319. */
  1320. if (!is_valid_ether_addr(dev->dev_addr)) {
  1321. PRINTK("%s: no valid ethernet hw addr\n", __FUNCTION__);
  1322. return -EINVAL;
  1323. }
  1324. /* reset the hardware */
  1325. smc911x_reset(dev);
  1326. /* Configure the PHY, initialize the link state */
  1327. smc911x_phy_configure(&lp->phy_configure);
  1328. /* Turn on Tx + Rx */
  1329. smc911x_enable(dev);
  1330. netif_start_queue(dev);
  1331. return 0;
  1332. }
  1333. /*
  1334. * smc911x_close
  1335. *
  1336. * this makes the board clean up everything that it can
  1337. * and not talk to the outside world. Caused by
  1338. * an 'ifconfig ethX down'
  1339. */
  1340. static int smc911x_close(struct net_device *dev)
  1341. {
  1342. struct smc911x_local *lp = netdev_priv(dev);
  1343. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1344. netif_stop_queue(dev);
  1345. netif_carrier_off(dev);
  1346. /* clear everything */
  1347. smc911x_shutdown(dev);
  1348. if (lp->phy_type != 0) {
  1349. /* We need to ensure that no calls to
  1350. * smc911x_phy_configure are pending.
  1351. * flush_scheduled_work() cannot be called because we
  1352. * are running with the netlink semaphore held (from
  1353. * devinet_ioctl()) and the pending work queue
  1354. * contains linkwatch_event() (scheduled by
  1355. * netif_carrier_off() above). linkwatch_event() also
  1356. * wants the netlink semaphore.
  1357. */
  1358. while (lp->work_pending)
  1359. schedule();
  1360. smc911x_phy_powerdown(dev, lp->mii.phy_id);
  1361. }
  1362. if (lp->pending_tx_skb) {
  1363. dev_kfree_skb(lp->pending_tx_skb);
  1364. lp->pending_tx_skb = NULL;
  1365. }
  1366. return 0;
  1367. }
  1368. /*
  1369. * Get the current statistics.
  1370. * This may be called with the card open or closed.
  1371. */
  1372. static struct net_device_stats *smc911x_query_statistics(struct net_device *dev)
  1373. {
  1374. struct smc911x_local *lp = netdev_priv(dev);
  1375. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1376. return &lp->stats;
  1377. }
  1378. /*
  1379. * Ethtool support
  1380. */
  1381. static int
  1382. smc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1383. {
  1384. struct smc911x_local *lp = netdev_priv(dev);
  1385. unsigned long ioaddr = dev->base_addr;
  1386. int ret, status;
  1387. unsigned long flags;
  1388. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1389. cmd->maxtxpkt = 1;
  1390. cmd->maxrxpkt = 1;
  1391. if (lp->phy_type != 0) {
  1392. spin_lock_irqsave(&lp->lock, flags);
  1393. ret = mii_ethtool_gset(&lp->mii, cmd);
  1394. spin_unlock_irqrestore(&lp->lock, flags);
  1395. } else {
  1396. cmd->supported = SUPPORTED_10baseT_Half |
  1397. SUPPORTED_10baseT_Full |
  1398. SUPPORTED_TP | SUPPORTED_AUI;
  1399. if (lp->ctl_rspeed == 10)
  1400. cmd->speed = SPEED_10;
  1401. else if (lp->ctl_rspeed == 100)
  1402. cmd->speed = SPEED_100;
  1403. cmd->autoneg = AUTONEG_DISABLE;
  1404. if (lp->mii.phy_id==1)
  1405. cmd->transceiver = XCVR_INTERNAL;
  1406. else
  1407. cmd->transceiver = XCVR_EXTERNAL;
  1408. cmd->port = 0;
  1409. SMC_GET_PHY_SPECIAL(lp->mii.phy_id, status);
  1410. cmd->duplex =
  1411. (status & (PHY_SPECIAL_SPD_10FULL_ | PHY_SPECIAL_SPD_100FULL_)) ?
  1412. DUPLEX_FULL : DUPLEX_HALF;
  1413. ret = 0;
  1414. }
  1415. return ret;
  1416. }
  1417. static int
  1418. smc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1419. {
  1420. struct smc911x_local *lp = netdev_priv(dev);
  1421. int ret;
  1422. unsigned long flags;
  1423. if (lp->phy_type != 0) {
  1424. spin_lock_irqsave(&lp->lock, flags);
  1425. ret = mii_ethtool_sset(&lp->mii, cmd);
  1426. spin_unlock_irqrestore(&lp->lock, flags);
  1427. } else {
  1428. if (cmd->autoneg != AUTONEG_DISABLE ||
  1429. cmd->speed != SPEED_10 ||
  1430. (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
  1431. (cmd->port != PORT_TP && cmd->port != PORT_AUI))
  1432. return -EINVAL;
  1433. lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
  1434. ret = 0;
  1435. }
  1436. return ret;
  1437. }
  1438. static void
  1439. smc911x_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1440. {
  1441. strncpy(info->driver, CARDNAME, sizeof(info->driver));
  1442. strncpy(info->version, version, sizeof(info->version));
  1443. strncpy(info->bus_info, dev->class_dev.dev->bus_id, sizeof(info->bus_info));
  1444. }
  1445. static int smc911x_ethtool_nwayreset(struct net_device *dev)
  1446. {
  1447. struct smc911x_local *lp = netdev_priv(dev);
  1448. int ret = -EINVAL;
  1449. unsigned long flags;
  1450. if (lp->phy_type != 0) {
  1451. spin_lock_irqsave(&lp->lock, flags);
  1452. ret = mii_nway_restart(&lp->mii);
  1453. spin_unlock_irqrestore(&lp->lock, flags);
  1454. }
  1455. return ret;
  1456. }
  1457. static u32 smc911x_ethtool_getmsglevel(struct net_device *dev)
  1458. {
  1459. struct smc911x_local *lp = netdev_priv(dev);
  1460. return lp->msg_enable;
  1461. }
  1462. static void smc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
  1463. {
  1464. struct smc911x_local *lp = netdev_priv(dev);
  1465. lp->msg_enable = level;
  1466. }
  1467. static int smc911x_ethtool_getregslen(struct net_device *dev)
  1468. {
  1469. /* System regs + MAC regs + PHY regs */
  1470. return (((E2P_CMD - ID_REV)/4 + 1) +
  1471. (WUCSR - MAC_CR)+1 + 32) * sizeof(u32);
  1472. }
  1473. static void smc911x_ethtool_getregs(struct net_device *dev,
  1474. struct ethtool_regs* regs, void *buf)
  1475. {
  1476. unsigned long ioaddr = dev->base_addr;
  1477. struct smc911x_local *lp = netdev_priv(dev);
  1478. unsigned long flags;
  1479. u32 reg,i,j=0;
  1480. u32 *data = (u32*)buf;
  1481. regs->version = lp->version;
  1482. for(i=ID_REV;i<=E2P_CMD;i+=4) {
  1483. data[j++] = SMC_inl(ioaddr,i);
  1484. }
  1485. for(i=MAC_CR;i<=WUCSR;i++) {
  1486. spin_lock_irqsave(&lp->lock, flags);
  1487. SMC_GET_MAC_CSR(i, reg);
  1488. spin_unlock_irqrestore(&lp->lock, flags);
  1489. data[j++] = reg;
  1490. }
  1491. for(i=0;i<=31;i++) {
  1492. spin_lock_irqsave(&lp->lock, flags);
  1493. SMC_GET_MII(i, lp->mii.phy_id, reg);
  1494. spin_unlock_irqrestore(&lp->lock, flags);
  1495. data[j++] = reg & 0xFFFF;
  1496. }
  1497. }
  1498. static int smc911x_ethtool_wait_eeprom_ready(struct net_device *dev)
  1499. {
  1500. unsigned long ioaddr = dev->base_addr;
  1501. unsigned int timeout;
  1502. int e2p_cmd;
  1503. e2p_cmd = SMC_GET_E2P_CMD();
  1504. for(timeout=10;(e2p_cmd & E2P_CMD_EPC_BUSY_) && timeout; timeout--) {
  1505. if (e2p_cmd & E2P_CMD_EPC_TIMEOUT_) {
  1506. PRINTK("%s: %s timeout waiting for EEPROM to respond\n",
  1507. dev->name, __FUNCTION__);
  1508. return -EFAULT;
  1509. }
  1510. mdelay(1);
  1511. e2p_cmd = SMC_GET_E2P_CMD();
  1512. }
  1513. if (timeout == 0) {
  1514. PRINTK("%s: %s timeout waiting for EEPROM CMD not busy\n",
  1515. dev->name, __FUNCTION__);
  1516. return -ETIMEDOUT;
  1517. }
  1518. return 0;
  1519. }
  1520. static inline int smc911x_ethtool_write_eeprom_cmd(struct net_device *dev,
  1521. int cmd, int addr)
  1522. {
  1523. unsigned long ioaddr = dev->base_addr;
  1524. int ret;
  1525. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1526. return ret;
  1527. SMC_SET_E2P_CMD(E2P_CMD_EPC_BUSY_ |
  1528. ((cmd) & (0x7<<28)) |
  1529. ((addr) & 0xFF));
  1530. return 0;
  1531. }
  1532. static inline int smc911x_ethtool_read_eeprom_byte(struct net_device *dev,
  1533. u8 *data)
  1534. {
  1535. unsigned long ioaddr = dev->base_addr;
  1536. int ret;
  1537. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1538. return ret;
  1539. *data = SMC_GET_E2P_DATA();
  1540. return 0;
  1541. }
  1542. static inline int smc911x_ethtool_write_eeprom_byte(struct net_device *dev,
  1543. u8 data)
  1544. {
  1545. unsigned long ioaddr = dev->base_addr;
  1546. int ret;
  1547. if ((ret = smc911x_ethtool_wait_eeprom_ready(dev))!=0)
  1548. return ret;
  1549. SMC_SET_E2P_DATA(data);
  1550. return 0;
  1551. }
  1552. static int smc911x_ethtool_geteeprom(struct net_device *dev,
  1553. struct ethtool_eeprom *eeprom, u8 *data)
  1554. {
  1555. u8 eebuf[SMC911X_EEPROM_LEN];
  1556. int i, ret;
  1557. for(i=0;i<SMC911X_EEPROM_LEN;i++) {
  1558. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ_, i ))!=0)
  1559. return ret;
  1560. if ((ret=smc911x_ethtool_read_eeprom_byte(dev, &eebuf[i]))!=0)
  1561. return ret;
  1562. }
  1563. memcpy(data, eebuf+eeprom->offset, eeprom->len);
  1564. return 0;
  1565. }
  1566. static int smc911x_ethtool_seteeprom(struct net_device *dev,
  1567. struct ethtool_eeprom *eeprom, u8 *data)
  1568. {
  1569. int i, ret;
  1570. /* Enable erase */
  1571. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN_, 0 ))!=0)
  1572. return ret;
  1573. for(i=eeprom->offset;i<(eeprom->offset+eeprom->len);i++) {
  1574. /* erase byte */
  1575. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE_, i ))!=0)
  1576. return ret;
  1577. /* write byte */
  1578. if ((ret=smc911x_ethtool_write_eeprom_byte(dev, *data))!=0)
  1579. return ret;
  1580. if ((ret=smc911x_ethtool_write_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE_, i ))!=0)
  1581. return ret;
  1582. }
  1583. return 0;
  1584. }
  1585. static int smc911x_ethtool_geteeprom_len(struct net_device *dev)
  1586. {
  1587. return SMC911X_EEPROM_LEN;
  1588. }
  1589. static const struct ethtool_ops smc911x_ethtool_ops = {
  1590. .get_settings = smc911x_ethtool_getsettings,
  1591. .set_settings = smc911x_ethtool_setsettings,
  1592. .get_drvinfo = smc911x_ethtool_getdrvinfo,
  1593. .get_msglevel = smc911x_ethtool_getmsglevel,
  1594. .set_msglevel = smc911x_ethtool_setmsglevel,
  1595. .nway_reset = smc911x_ethtool_nwayreset,
  1596. .get_link = ethtool_op_get_link,
  1597. .get_regs_len = smc911x_ethtool_getregslen,
  1598. .get_regs = smc911x_ethtool_getregs,
  1599. .get_eeprom_len = smc911x_ethtool_geteeprom_len,
  1600. .get_eeprom = smc911x_ethtool_geteeprom,
  1601. .set_eeprom = smc911x_ethtool_seteeprom,
  1602. };
  1603. /*
  1604. * smc911x_findirq
  1605. *
  1606. * This routine has a simple purpose -- make the SMC chip generate an
  1607. * interrupt, so an auto-detect routine can detect it, and find the IRQ,
  1608. */
  1609. static int __init smc911x_findirq(unsigned long ioaddr)
  1610. {
  1611. int timeout = 20;
  1612. unsigned long cookie;
  1613. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1614. cookie = probe_irq_on();
  1615. /*
  1616. * Force a SW interrupt
  1617. */
  1618. SMC_SET_INT_EN(INT_EN_SW_INT_EN_);
  1619. /*
  1620. * Wait until positive that the interrupt has been generated
  1621. */
  1622. do {
  1623. int int_status;
  1624. udelay(10);
  1625. int_status = SMC_GET_INT_EN();
  1626. if (int_status & INT_EN_SW_INT_EN_)
  1627. break; /* got the interrupt */
  1628. } while (--timeout);
  1629. /*
  1630. * there is really nothing that I can do here if timeout fails,
  1631. * as autoirq_report will return a 0 anyway, which is what I
  1632. * want in this case. Plus, the clean up is needed in both
  1633. * cases.
  1634. */
  1635. /* and disable all interrupts again */
  1636. SMC_SET_INT_EN(0);
  1637. /* and return what I found */
  1638. return probe_irq_off(cookie);
  1639. }
  1640. /*
  1641. * Function: smc911x_probe(unsigned long ioaddr)
  1642. *
  1643. * Purpose:
  1644. * Tests to see if a given ioaddr points to an SMC911x chip.
  1645. * Returns a 0 on success
  1646. *
  1647. * Algorithm:
  1648. * (1) see if the endian word is OK
  1649. * (1) see if I recognize the chip ID in the appropriate register
  1650. *
  1651. * Here I do typical initialization tasks.
  1652. *
  1653. * o Initialize the structure if needed
  1654. * o print out my vanity message if not done so already
  1655. * o print out what type of hardware is detected
  1656. * o print out the ethernet address
  1657. * o find the IRQ
  1658. * o set up my private data
  1659. * o configure the dev structure with my subroutines
  1660. * o actually GRAB the irq.
  1661. * o GRAB the region
  1662. */
  1663. static int __init smc911x_probe(struct net_device *dev, unsigned long ioaddr)
  1664. {
  1665. struct smc911x_local *lp = netdev_priv(dev);
  1666. int i, retval;
  1667. unsigned int val, chip_id, revision;
  1668. const char *version_string;
  1669. DBG(SMC_DEBUG_FUNC, "%s: --> %s\n", dev->name, __FUNCTION__);
  1670. /* First, see if the endian word is recognized */
  1671. val = SMC_GET_BYTE_TEST();
  1672. DBG(SMC_DEBUG_MISC, "%s: endian probe returned 0x%04x\n", CARDNAME, val);
  1673. if (val != 0x87654321) {
  1674. printk(KERN_ERR "Invalid chip endian 0x08%x\n",val);
  1675. retval = -ENODEV;
  1676. goto err_out;
  1677. }
  1678. /*
  1679. * check if the revision register is something that I
  1680. * recognize. These might need to be added to later,
  1681. * as future revisions could be added.
  1682. */
  1683. chip_id = SMC_GET_PN();
  1684. DBG(SMC_DEBUG_MISC, "%s: id probe returned 0x%04x\n", CARDNAME, chip_id);
  1685. for(i=0;chip_ids[i].id != 0; i++) {
  1686. if (chip_ids[i].id == chip_id) break;
  1687. }
  1688. if (!chip_ids[i].id) {
  1689. printk(KERN_ERR "Unknown chip ID %04x\n", chip_id);
  1690. retval = -ENODEV;
  1691. goto err_out;
  1692. }
  1693. version_string = chip_ids[i].name;
  1694. revision = SMC_GET_REV();
  1695. DBG(SMC_DEBUG_MISC, "%s: revision = 0x%04x\n", CARDNAME, revision);
  1696. /* At this point I'll assume that the chip is an SMC911x. */
  1697. DBG(SMC_DEBUG_MISC, "%s: Found a %s\n", CARDNAME, chip_ids[i].name);
  1698. /* Validate the TX FIFO size requested */
  1699. if ((tx_fifo_kb < 2) || (tx_fifo_kb > 14)) {
  1700. printk(KERN_ERR "Invalid TX FIFO size requested %d\n", tx_fifo_kb);
  1701. retval = -EINVAL;
  1702. goto err_out;
  1703. }
  1704. /* fill in some of the fields */
  1705. dev->base_addr = ioaddr;
  1706. lp->version = chip_ids[i].id;
  1707. lp->revision = revision;
  1708. lp->tx_fifo_kb = tx_fifo_kb;
  1709. /* Reverse calculate the RX FIFO size from the TX */
  1710. lp->tx_fifo_size=(lp->tx_fifo_kb<<10) - 512;
  1711. lp->rx_fifo_size= ((0x4000 - 512 - lp->tx_fifo_size) / 16) * 15;
  1712. /* Set the automatic flow control values */
  1713. switch(lp->tx_fifo_kb) {
  1714. /*
  1715. * AFC_HI is about ((Rx Data Fifo Size)*2/3)/64
  1716. * AFC_LO is AFC_HI/2
  1717. * BACK_DUR is about 5uS*(AFC_LO) rounded down
  1718. */
  1719. case 2:/* 13440 Rx Data Fifo Size */
  1720. lp->afc_cfg=0x008C46AF;break;
  1721. case 3:/* 12480 Rx Data Fifo Size */
  1722. lp->afc_cfg=0x0082419F;break;
  1723. case 4:/* 11520 Rx Data Fifo Size */
  1724. lp->afc_cfg=0x00783C9F;break;
  1725. case 5:/* 10560 Rx Data Fifo Size */
  1726. lp->afc_cfg=0x006E374F;break;
  1727. case 6:/* 9600 Rx Data Fifo Size */
  1728. lp->afc_cfg=0x0064328F;break;
  1729. case 7:/* 8640 Rx Data Fifo Size */
  1730. lp->afc_cfg=0x005A2D7F;break;
  1731. case 8:/* 7680 Rx Data Fifo Size */
  1732. lp->afc_cfg=0x0050287F;break;
  1733. case 9:/* 6720 Rx Data Fifo Size */
  1734. lp->afc_cfg=0x0046236F;break;
  1735. case 10:/* 5760 Rx Data Fifo Size */
  1736. lp->afc_cfg=0x003C1E6F;break;
  1737. case 11:/* 4800 Rx Data Fifo Size */
  1738. lp->afc_cfg=0x0032195F;break;
  1739. /*
  1740. * AFC_HI is ~1520 bytes less than RX Data Fifo Size
  1741. * AFC_LO is AFC_HI/2
  1742. * BACK_DUR is about 5uS*(AFC_LO) rounded down
  1743. */
  1744. case 12:/* 3840 Rx Data Fifo Size */
  1745. lp->afc_cfg=0x0024124F;break;
  1746. case 13:/* 2880 Rx Data Fifo Size */
  1747. lp->afc_cfg=0x0015073F;break;
  1748. case 14:/* 1920 Rx Data Fifo Size */
  1749. lp->afc_cfg=0x0006032F;break;
  1750. default:
  1751. PRINTK("%s: ERROR -- no AFC_CFG setting found",
  1752. dev->name);
  1753. break;
  1754. }
  1755. DBG(SMC_DEBUG_MISC | SMC_DEBUG_TX | SMC_DEBUG_RX,
  1756. "%s: tx_fifo %d rx_fifo %d afc_cfg 0x%08x\n", CARDNAME,
  1757. lp->tx_fifo_size, lp->rx_fifo_size, lp->afc_cfg);
  1758. spin_lock_init(&lp->lock);
  1759. /* Get the MAC address */
  1760. SMC_GET_MAC_ADDR(dev->dev_addr);
  1761. /* now, reset the chip, and put it into a known state */
  1762. smc911x_reset(dev);
  1763. /*
  1764. * If dev->irq is 0, then the device has to be banged on to see
  1765. * what the IRQ is.
  1766. *
  1767. * Specifying an IRQ is done with the assumption that the user knows
  1768. * what (s)he is doing. No checking is done!!!!
  1769. */
  1770. if (dev->irq < 1) {
  1771. int trials;
  1772. trials = 3;
  1773. while (trials--) {
  1774. dev->irq = smc911x_findirq(ioaddr);
  1775. if (dev->irq)
  1776. break;
  1777. /* kick the card and try again */
  1778. smc911x_reset(dev);
  1779. }
  1780. }
  1781. if (dev->irq == 0) {
  1782. printk("%s: Couldn't autodetect your IRQ. Use irq=xx.\n",
  1783. dev->name);
  1784. retval = -ENODEV;
  1785. goto err_out;
  1786. }
  1787. dev->irq = irq_canonicalize(dev->irq);
  1788. /* Fill in the fields of the device structure with ethernet values. */
  1789. ether_setup(dev);
  1790. dev->open = smc911x_open;
  1791. dev->stop = smc911x_close;
  1792. dev->hard_start_xmit = smc911x_hard_start_xmit;
  1793. dev->tx_timeout = smc911x_timeout;
  1794. dev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1795. dev->get_stats = smc911x_query_statistics;
  1796. dev->set_multicast_list = smc911x_set_multicast_list;
  1797. dev->ethtool_ops = &smc911x_ethtool_ops;
  1798. #ifdef CONFIG_NET_POLL_CONTROLLER
  1799. dev->poll_controller = smc911x_poll_controller;
  1800. #endif
  1801. INIT_WORK(&lp->phy_configure, smc911x_phy_configure);
  1802. lp->mii.phy_id_mask = 0x1f;
  1803. lp->mii.reg_num_mask = 0x1f;
  1804. lp->mii.force_media = 0;
  1805. lp->mii.full_duplex = 0;
  1806. lp->mii.dev = dev;
  1807. lp->mii.mdio_read = smc911x_phy_read;
  1808. lp->mii.mdio_write = smc911x_phy_write;
  1809. /*
  1810. * Locate the phy, if any.
  1811. */
  1812. smc911x_phy_detect(dev);
  1813. /* Set default parameters */
  1814. lp->msg_enable = NETIF_MSG_LINK;
  1815. lp->ctl_rfduplx = 1;
  1816. lp->ctl_rspeed = 100;
  1817. /* Grab the IRQ */
  1818. retval = request_irq(dev->irq, &smc911x_interrupt, IRQF_SHARED, dev->name, dev);
  1819. if (retval)
  1820. goto err_out;
  1821. set_irq_type(dev->irq, IRQT_FALLING);
  1822. #ifdef SMC_USE_DMA
  1823. lp->rxdma = SMC_DMA_REQUEST(dev, smc911x_rx_dma_irq);
  1824. lp->txdma = SMC_DMA_REQUEST(dev, smc911x_tx_dma_irq);
  1825. lp->rxdma_active = 0;
  1826. lp->txdma_active = 0;
  1827. dev->dma = lp->rxdma;
  1828. #endif
  1829. retval = register_netdev(dev);
  1830. if (retval == 0) {
  1831. /* now, print out the card info, in a short format.. */
  1832. printk("%s: %s (rev %d) at %#lx IRQ %d",
  1833. dev->name, version_string, lp->revision,
  1834. dev->base_addr, dev->irq);
  1835. #ifdef SMC_USE_DMA
  1836. if (lp->rxdma != -1)
  1837. printk(" RXDMA %d ", lp->rxdma);
  1838. if (lp->txdma != -1)
  1839. printk("TXDMA %d", lp->txdma);
  1840. #endif
  1841. printk("\n");
  1842. if (!is_valid_ether_addr(dev->dev_addr)) {
  1843. printk("%s: Invalid ethernet MAC address. Please "
  1844. "set using ifconfig\n", dev->name);
  1845. } else {
  1846. /* Print the Ethernet address */
  1847. printk("%s: Ethernet addr: ", dev->name);
  1848. for (i = 0; i < 5; i++)
  1849. printk("%2.2x:", dev->dev_addr[i]);
  1850. printk("%2.2x\n", dev->dev_addr[5]);
  1851. }
  1852. if (lp->phy_type == 0) {
  1853. PRINTK("%s: No PHY found\n", dev->name);
  1854. } else if ((lp->phy_type & ~0xff) == LAN911X_INTERNAL_PHY_ID) {
  1855. PRINTK("%s: LAN911x Internal PHY\n", dev->name);
  1856. } else {
  1857. PRINTK("%s: External PHY 0x%08x\n", dev->name, lp->phy_type);
  1858. }
  1859. }
  1860. err_out:
  1861. #ifdef SMC_USE_DMA
  1862. if (retval) {
  1863. if (lp->rxdma != -1) {
  1864. SMC_DMA_FREE(dev, lp->rxdma);
  1865. }
  1866. if (lp->txdma != -1) {
  1867. SMC_DMA_FREE(dev, lp->txdma);
  1868. }
  1869. }
  1870. #endif
  1871. return retval;
  1872. }
  1873. /*
  1874. * smc911x_init(void)
  1875. *
  1876. * Output:
  1877. * 0 --> there is a device
  1878. * anything else, error
  1879. */
  1880. static int smc911x_drv_probe(struct platform_device *pdev)
  1881. {
  1882. struct net_device *ndev;
  1883. struct resource *res;
  1884. struct smc911x_local *lp;
  1885. unsigned int *addr;
  1886. int ret;
  1887. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1888. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1889. if (!res) {
  1890. ret = -ENODEV;
  1891. goto out;
  1892. }
  1893. /*
  1894. * Request the regions.
  1895. */
  1896. if (!request_mem_region(res->start, SMC911X_IO_EXTENT, CARDNAME)) {
  1897. ret = -EBUSY;
  1898. goto out;
  1899. }
  1900. ndev = alloc_etherdev(sizeof(struct smc911x_local));
  1901. if (!ndev) {
  1902. printk("%s: could not allocate device.\n", CARDNAME);
  1903. ret = -ENOMEM;
  1904. goto release_1;
  1905. }
  1906. SET_MODULE_OWNER(ndev);
  1907. SET_NETDEV_DEV(ndev, &pdev->dev);
  1908. ndev->dma = (unsigned char)-1;
  1909. ndev->irq = platform_get_irq(pdev, 0);
  1910. lp = netdev_priv(ndev);
  1911. lp->netdev = ndev;
  1912. addr = ioremap(res->start, SMC911X_IO_EXTENT);
  1913. if (!addr) {
  1914. ret = -ENOMEM;
  1915. goto release_both;
  1916. }
  1917. platform_set_drvdata(pdev, ndev);
  1918. ret = smc911x_probe(ndev, (unsigned long)addr);
  1919. if (ret != 0) {
  1920. platform_set_drvdata(pdev, NULL);
  1921. iounmap(addr);
  1922. release_both:
  1923. free_netdev(ndev);
  1924. release_1:
  1925. release_mem_region(res->start, SMC911X_IO_EXTENT);
  1926. out:
  1927. printk("%s: not found (%d).\n", CARDNAME, ret);
  1928. }
  1929. #ifdef SMC_USE_DMA
  1930. else {
  1931. lp->physaddr = res->start;
  1932. lp->dev = &pdev->dev;
  1933. }
  1934. #endif
  1935. return ret;
  1936. }
  1937. static int smc911x_drv_remove(struct platform_device *pdev)
  1938. {
  1939. struct net_device *ndev = platform_get_drvdata(pdev);
  1940. struct resource *res;
  1941. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1942. platform_set_drvdata(pdev, NULL);
  1943. unregister_netdev(ndev);
  1944. free_irq(ndev->irq, ndev);
  1945. #ifdef SMC_USE_DMA
  1946. {
  1947. struct smc911x_local *lp = netdev_priv(ndev);
  1948. if (lp->rxdma != -1) {
  1949. SMC_DMA_FREE(dev, lp->rxdma);
  1950. }
  1951. if (lp->txdma != -1) {
  1952. SMC_DMA_FREE(dev, lp->txdma);
  1953. }
  1954. }
  1955. #endif
  1956. iounmap((void *)ndev->base_addr);
  1957. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1958. release_mem_region(res->start, SMC911X_IO_EXTENT);
  1959. free_netdev(ndev);
  1960. return 0;
  1961. }
  1962. static int smc911x_drv_suspend(struct platform_device *dev, pm_message_t state)
  1963. {
  1964. struct net_device *ndev = platform_get_drvdata(dev);
  1965. unsigned long ioaddr = ndev->base_addr;
  1966. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1967. if (ndev) {
  1968. if (netif_running(ndev)) {
  1969. netif_device_detach(ndev);
  1970. smc911x_shutdown(ndev);
  1971. #if POWER_DOWN
  1972. /* Set D2 - Energy detect only setting */
  1973. SMC_SET_PMT_CTRL(2<<12);
  1974. #endif
  1975. }
  1976. }
  1977. return 0;
  1978. }
  1979. static int smc911x_drv_resume(struct platform_device *dev)
  1980. {
  1981. struct net_device *ndev = platform_get_drvdata(dev);
  1982. DBG(SMC_DEBUG_FUNC, "--> %s\n", __FUNCTION__);
  1983. if (ndev) {
  1984. struct smc911x_local *lp = netdev_priv(ndev);
  1985. if (netif_running(ndev)) {
  1986. smc911x_reset(ndev);
  1987. smc911x_enable(ndev);
  1988. if (lp->phy_type != 0)
  1989. smc911x_phy_configure(&lp->phy_configure);
  1990. netif_device_attach(ndev);
  1991. }
  1992. }
  1993. return 0;
  1994. }
  1995. static struct platform_driver smc911x_driver = {
  1996. .probe = smc911x_drv_probe,
  1997. .remove = smc911x_drv_remove,
  1998. .suspend = smc911x_drv_suspend,
  1999. .resume = smc911x_drv_resume,
  2000. .driver = {
  2001. .name = CARDNAME,
  2002. },
  2003. };
  2004. static int __init smc911x_init(void)
  2005. {
  2006. return platform_driver_register(&smc911x_driver);
  2007. }
  2008. static void __exit smc911x_cleanup(void)
  2009. {
  2010. platform_driver_unregister(&smc911x_driver);
  2011. }
  2012. module_init(smc911x_init);
  2013. module_exit(smc911x_cleanup);