smc91x.c 64 KB

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