smc911x.c 56 KB

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