smc91x.c 63 KB

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