smc91x.c 59 KB

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