smsc911x.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2004-2008 SMSC
  4. * Copyright (C) 2005-2008 ARM
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. ***************************************************************************
  21. * Rewritten, heavily based on smsc911x simple driver by SMSC.
  22. * Partly uses io macros from smc91x.c by Nicolas Pitre
  23. *
  24. * Supported devices:
  25. * LAN9115, LAN9116, LAN9117, LAN9118
  26. * LAN9215, LAN9216, LAN9217, LAN9218
  27. * LAN9210, LAN9211
  28. * LAN9220, LAN9221
  29. *
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/crc32.h>
  33. #include <linux/delay.h>
  34. #include <linux/errno.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/init.h>
  38. #include <linux/ioport.h>
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/sched.h>
  44. #include <linux/timer.h>
  45. #include <linux/bug.h>
  46. #include <linux/bitops.h>
  47. #include <linux/irq.h>
  48. #include <linux/io.h>
  49. #include <linux/swab.h>
  50. #include <linux/phy.h>
  51. #include <linux/smsc911x.h>
  52. #include <linux/device.h>
  53. #include "smsc911x.h"
  54. #define SMSC_CHIPNAME "smsc911x"
  55. #define SMSC_MDIONAME "smsc911x-mdio"
  56. #define SMSC_DRV_VERSION "2008-10-21"
  57. MODULE_LICENSE("GPL");
  58. MODULE_VERSION(SMSC_DRV_VERSION);
  59. MODULE_ALIAS("platform:smsc911x");
  60. #if USE_DEBUG > 0
  61. static int debug = 16;
  62. #else
  63. static int debug = 3;
  64. #endif
  65. module_param(debug, int, 0);
  66. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  67. struct smsc911x_data;
  68. struct smsc911x_ops {
  69. u32 (*reg_read)(struct smsc911x_data *pdata, u32 reg);
  70. void (*reg_write)(struct smsc911x_data *pdata, u32 reg, u32 val);
  71. void (*rx_readfifo)(struct smsc911x_data *pdata,
  72. unsigned int *buf, unsigned int wordcount);
  73. void (*tx_writefifo)(struct smsc911x_data *pdata,
  74. unsigned int *buf, unsigned int wordcount);
  75. };
  76. struct smsc911x_data {
  77. void __iomem *ioaddr;
  78. unsigned int idrev;
  79. /* used to decide which workarounds apply */
  80. unsigned int generation;
  81. /* device configuration (copied from platform_data during probe) */
  82. struct smsc911x_platform_config config;
  83. /* This needs to be acquired before calling any of below:
  84. * smsc911x_mac_read(), smsc911x_mac_write()
  85. */
  86. spinlock_t mac_lock;
  87. /* spinlock to ensure register accesses are serialised */
  88. spinlock_t dev_lock;
  89. struct phy_device *phy_dev;
  90. struct mii_bus *mii_bus;
  91. int phy_irq[PHY_MAX_ADDR];
  92. unsigned int using_extphy;
  93. int last_duplex;
  94. int last_carrier;
  95. u32 msg_enable;
  96. unsigned int gpio_setting;
  97. unsigned int gpio_orig_setting;
  98. struct net_device *dev;
  99. struct napi_struct napi;
  100. unsigned int software_irq_signal;
  101. #ifdef USE_PHY_WORK_AROUND
  102. #define MIN_PACKET_SIZE (64)
  103. char loopback_tx_pkt[MIN_PACKET_SIZE];
  104. char loopback_rx_pkt[MIN_PACKET_SIZE];
  105. unsigned int resetcount;
  106. #endif
  107. /* Members for Multicast filter workaround */
  108. unsigned int multicast_update_pending;
  109. unsigned int set_bits_mask;
  110. unsigned int clear_bits_mask;
  111. unsigned int hashhi;
  112. unsigned int hashlo;
  113. /* register access functions */
  114. const struct smsc911x_ops *ops;
  115. };
  116. /* Easy access to information */
  117. #define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
  118. static inline u32 __smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
  119. {
  120. if (pdata->config.flags & SMSC911X_USE_32BIT)
  121. return readl(pdata->ioaddr + reg);
  122. if (pdata->config.flags & SMSC911X_USE_16BIT)
  123. return ((readw(pdata->ioaddr + reg) & 0xFFFF) |
  124. ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
  125. BUG();
  126. return 0;
  127. }
  128. static inline u32
  129. __smsc911x_reg_read_shift(struct smsc911x_data *pdata, u32 reg)
  130. {
  131. if (pdata->config.flags & SMSC911X_USE_32BIT)
  132. return readl(pdata->ioaddr + __smsc_shift(pdata, reg));
  133. if (pdata->config.flags & SMSC911X_USE_16BIT)
  134. return (readw(pdata->ioaddr +
  135. __smsc_shift(pdata, reg)) & 0xFFFF) |
  136. ((readw(pdata->ioaddr +
  137. __smsc_shift(pdata, reg + 2)) & 0xFFFF) << 16);
  138. BUG();
  139. return 0;
  140. }
  141. static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
  142. {
  143. u32 data;
  144. unsigned long flags;
  145. spin_lock_irqsave(&pdata->dev_lock, flags);
  146. data = pdata->ops->reg_read(pdata, reg);
  147. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  148. return data;
  149. }
  150. static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
  151. u32 val)
  152. {
  153. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  154. writel(val, pdata->ioaddr + reg);
  155. return;
  156. }
  157. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  158. writew(val & 0xFFFF, pdata->ioaddr + reg);
  159. writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
  160. return;
  161. }
  162. BUG();
  163. }
  164. static inline void
  165. __smsc911x_reg_write_shift(struct smsc911x_data *pdata, u32 reg, u32 val)
  166. {
  167. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  168. writel(val, pdata->ioaddr + __smsc_shift(pdata, reg));
  169. return;
  170. }
  171. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  172. writew(val & 0xFFFF,
  173. pdata->ioaddr + __smsc_shift(pdata, reg));
  174. writew((val >> 16) & 0xFFFF,
  175. pdata->ioaddr + __smsc_shift(pdata, reg + 2));
  176. return;
  177. }
  178. BUG();
  179. }
  180. static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
  181. u32 val)
  182. {
  183. unsigned long flags;
  184. spin_lock_irqsave(&pdata->dev_lock, flags);
  185. pdata->ops->reg_write(pdata, reg, val);
  186. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  187. }
  188. /* Writes a packet to the TX_DATA_FIFO */
  189. static inline void
  190. smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
  191. unsigned int wordcount)
  192. {
  193. unsigned long flags;
  194. spin_lock_irqsave(&pdata->dev_lock, flags);
  195. if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
  196. while (wordcount--)
  197. __smsc911x_reg_write(pdata, TX_DATA_FIFO,
  198. swab32(*buf++));
  199. goto out;
  200. }
  201. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  202. writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
  203. goto out;
  204. }
  205. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  206. while (wordcount--)
  207. __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
  208. goto out;
  209. }
  210. BUG();
  211. out:
  212. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  213. }
  214. /* Writes a packet to the TX_DATA_FIFO - shifted version */
  215. static inline void
  216. smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
  217. unsigned int wordcount)
  218. {
  219. unsigned long flags;
  220. spin_lock_irqsave(&pdata->dev_lock, flags);
  221. if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
  222. while (wordcount--)
  223. __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
  224. swab32(*buf++));
  225. goto out;
  226. }
  227. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  228. writesl(pdata->ioaddr + __smsc_shift(pdata,
  229. TX_DATA_FIFO), buf, wordcount);
  230. goto out;
  231. }
  232. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  233. while (wordcount--)
  234. __smsc911x_reg_write_shift(pdata,
  235. TX_DATA_FIFO, *buf++);
  236. goto out;
  237. }
  238. BUG();
  239. out:
  240. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  241. }
  242. /* Reads a packet out of the RX_DATA_FIFO */
  243. static inline void
  244. smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
  245. unsigned int wordcount)
  246. {
  247. unsigned long flags;
  248. spin_lock_irqsave(&pdata->dev_lock, flags);
  249. if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
  250. while (wordcount--)
  251. *buf++ = swab32(__smsc911x_reg_read(pdata,
  252. RX_DATA_FIFO));
  253. goto out;
  254. }
  255. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  256. readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
  257. goto out;
  258. }
  259. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  260. while (wordcount--)
  261. *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
  262. goto out;
  263. }
  264. BUG();
  265. out:
  266. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  267. }
  268. /* Reads a packet out of the RX_DATA_FIFO - shifted version */
  269. static inline void
  270. smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
  271. unsigned int wordcount)
  272. {
  273. unsigned long flags;
  274. spin_lock_irqsave(&pdata->dev_lock, flags);
  275. if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
  276. while (wordcount--)
  277. *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
  278. RX_DATA_FIFO));
  279. goto out;
  280. }
  281. if (pdata->config.flags & SMSC911X_USE_32BIT) {
  282. readsl(pdata->ioaddr + __smsc_shift(pdata,
  283. RX_DATA_FIFO), buf, wordcount);
  284. goto out;
  285. }
  286. if (pdata->config.flags & SMSC911X_USE_16BIT) {
  287. while (wordcount--)
  288. *buf++ = __smsc911x_reg_read_shift(pdata,
  289. RX_DATA_FIFO);
  290. goto out;
  291. }
  292. BUG();
  293. out:
  294. spin_unlock_irqrestore(&pdata->dev_lock, flags);
  295. }
  296. /* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
  297. * and smsc911x_mac_write, so assumes mac_lock is held */
  298. static int smsc911x_mac_complete(struct smsc911x_data *pdata)
  299. {
  300. int i;
  301. u32 val;
  302. SMSC_ASSERT_MAC_LOCK(pdata);
  303. for (i = 0; i < 40; i++) {
  304. val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
  305. if (!(val & MAC_CSR_CMD_CSR_BUSY_))
  306. return 0;
  307. }
  308. SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
  309. "MAC_CSR_CMD: 0x%08X", val);
  310. return -EIO;
  311. }
  312. /* Fetches a MAC register value. Assumes mac_lock is acquired */
  313. static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
  314. {
  315. unsigned int temp;
  316. SMSC_ASSERT_MAC_LOCK(pdata);
  317. temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
  318. if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
  319. SMSC_WARN(pdata, hw, "MAC busy at entry");
  320. return 0xFFFFFFFF;
  321. }
  322. /* Send the MAC cmd */
  323. smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
  324. MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
  325. /* Workaround for hardware read-after-write restriction */
  326. temp = smsc911x_reg_read(pdata, BYTE_TEST);
  327. /* Wait for the read to complete */
  328. if (likely(smsc911x_mac_complete(pdata) == 0))
  329. return smsc911x_reg_read(pdata, MAC_CSR_DATA);
  330. SMSC_WARN(pdata, hw, "MAC busy after read");
  331. return 0xFFFFFFFF;
  332. }
  333. /* Set a mac register, mac_lock must be acquired before calling */
  334. static void smsc911x_mac_write(struct smsc911x_data *pdata,
  335. unsigned int offset, u32 val)
  336. {
  337. unsigned int temp;
  338. SMSC_ASSERT_MAC_LOCK(pdata);
  339. temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
  340. if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
  341. SMSC_WARN(pdata, hw,
  342. "smsc911x_mac_write failed, MAC busy at entry");
  343. return;
  344. }
  345. /* Send data to write */
  346. smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
  347. /* Write the actual data */
  348. smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
  349. MAC_CSR_CMD_CSR_BUSY_));
  350. /* Workaround for hardware read-after-write restriction */
  351. temp = smsc911x_reg_read(pdata, BYTE_TEST);
  352. /* Wait for the write to complete */
  353. if (likely(smsc911x_mac_complete(pdata) == 0))
  354. return;
  355. SMSC_WARN(pdata, hw, "smsc911x_mac_write failed, MAC busy after write");
  356. }
  357. /* Get a phy register */
  358. static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
  359. {
  360. struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
  361. unsigned long flags;
  362. unsigned int addr;
  363. int i, reg;
  364. spin_lock_irqsave(&pdata->mac_lock, flags);
  365. /* Confirm MII not busy */
  366. if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
  367. SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_read???");
  368. reg = -EIO;
  369. goto out;
  370. }
  371. /* Set the address, index & direction (read from PHY) */
  372. addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
  373. smsc911x_mac_write(pdata, MII_ACC, addr);
  374. /* Wait for read to complete w/ timeout */
  375. for (i = 0; i < 100; i++)
  376. if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
  377. reg = smsc911x_mac_read(pdata, MII_DATA);
  378. goto out;
  379. }
  380. SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
  381. reg = -EIO;
  382. out:
  383. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  384. return reg;
  385. }
  386. /* Set a phy register */
  387. static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  388. u16 val)
  389. {
  390. struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
  391. unsigned long flags;
  392. unsigned int addr;
  393. int i, reg;
  394. spin_lock_irqsave(&pdata->mac_lock, flags);
  395. /* Confirm MII not busy */
  396. if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
  397. SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_write???");
  398. reg = -EIO;
  399. goto out;
  400. }
  401. /* Put the data to write in the MAC */
  402. smsc911x_mac_write(pdata, MII_DATA, val);
  403. /* Set the address, index & direction (write to PHY) */
  404. addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
  405. MII_ACC_MII_WRITE_;
  406. smsc911x_mac_write(pdata, MII_ACC, addr);
  407. /* Wait for write to complete w/ timeout */
  408. for (i = 0; i < 100; i++)
  409. if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
  410. reg = 0;
  411. goto out;
  412. }
  413. SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
  414. reg = -EIO;
  415. out:
  416. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  417. return reg;
  418. }
  419. /* Switch to external phy. Assumes tx and rx are stopped. */
  420. static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
  421. {
  422. unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
  423. /* Disable phy clocks to the MAC */
  424. hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
  425. hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
  426. smsc911x_reg_write(pdata, HW_CFG, hwcfg);
  427. udelay(10); /* Enough time for clocks to stop */
  428. /* Switch to external phy */
  429. hwcfg |= HW_CFG_EXT_PHY_EN_;
  430. smsc911x_reg_write(pdata, HW_CFG, hwcfg);
  431. /* Enable phy clocks to the MAC */
  432. hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
  433. hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
  434. smsc911x_reg_write(pdata, HW_CFG, hwcfg);
  435. udelay(10); /* Enough time for clocks to restart */
  436. hwcfg |= HW_CFG_SMI_SEL_;
  437. smsc911x_reg_write(pdata, HW_CFG, hwcfg);
  438. }
  439. /* Autodetects and enables external phy if present on supported chips.
  440. * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
  441. * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
  442. static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
  443. {
  444. unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
  445. if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
  446. SMSC_TRACE(pdata, hw, "Forcing internal PHY");
  447. pdata->using_extphy = 0;
  448. } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
  449. SMSC_TRACE(pdata, hw, "Forcing external PHY");
  450. smsc911x_phy_enable_external(pdata);
  451. pdata->using_extphy = 1;
  452. } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
  453. SMSC_TRACE(pdata, hw,
  454. "HW_CFG EXT_PHY_DET set, using external PHY");
  455. smsc911x_phy_enable_external(pdata);
  456. pdata->using_extphy = 1;
  457. } else {
  458. SMSC_TRACE(pdata, hw,
  459. "HW_CFG EXT_PHY_DET clear, using internal PHY");
  460. pdata->using_extphy = 0;
  461. }
  462. }
  463. /* Fetches a tx status out of the status fifo */
  464. static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
  465. {
  466. unsigned int result =
  467. smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
  468. if (result != 0)
  469. result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
  470. return result;
  471. }
  472. /* Fetches the next rx status */
  473. static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
  474. {
  475. unsigned int result =
  476. smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
  477. if (result != 0)
  478. result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
  479. return result;
  480. }
  481. #ifdef USE_PHY_WORK_AROUND
  482. static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
  483. {
  484. unsigned int tries;
  485. u32 wrsz;
  486. u32 rdsz;
  487. ulong bufp;
  488. for (tries = 0; tries < 10; tries++) {
  489. unsigned int txcmd_a;
  490. unsigned int txcmd_b;
  491. unsigned int status;
  492. unsigned int pktlength;
  493. unsigned int i;
  494. /* Zero-out rx packet memory */
  495. memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
  496. /* Write tx packet to 118 */
  497. txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
  498. txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
  499. txcmd_a |= MIN_PACKET_SIZE;
  500. txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
  501. smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
  502. smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
  503. bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
  504. wrsz = MIN_PACKET_SIZE + 3;
  505. wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
  506. wrsz >>= 2;
  507. pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
  508. /* Wait till transmit is done */
  509. i = 60;
  510. do {
  511. udelay(5);
  512. status = smsc911x_tx_get_txstatus(pdata);
  513. } while ((i--) && (!status));
  514. if (!status) {
  515. SMSC_WARN(pdata, hw,
  516. "Failed to transmit during loopback test");
  517. continue;
  518. }
  519. if (status & TX_STS_ES_) {
  520. SMSC_WARN(pdata, hw,
  521. "Transmit encountered errors during loopback test");
  522. continue;
  523. }
  524. /* Wait till receive is done */
  525. i = 60;
  526. do {
  527. udelay(5);
  528. status = smsc911x_rx_get_rxstatus(pdata);
  529. } while ((i--) && (!status));
  530. if (!status) {
  531. SMSC_WARN(pdata, hw,
  532. "Failed to receive during loopback test");
  533. continue;
  534. }
  535. if (status & RX_STS_ES_) {
  536. SMSC_WARN(pdata, hw,
  537. "Receive encountered errors during loopback test");
  538. continue;
  539. }
  540. pktlength = ((status & 0x3FFF0000UL) >> 16);
  541. bufp = (ulong)pdata->loopback_rx_pkt;
  542. rdsz = pktlength + 3;
  543. rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
  544. rdsz >>= 2;
  545. pdata->ops->rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
  546. if (pktlength != (MIN_PACKET_SIZE + 4)) {
  547. SMSC_WARN(pdata, hw, "Unexpected packet size "
  548. "during loop back test, size=%d, will retry",
  549. pktlength);
  550. } else {
  551. unsigned int j;
  552. int mismatch = 0;
  553. for (j = 0; j < MIN_PACKET_SIZE; j++) {
  554. if (pdata->loopback_tx_pkt[j]
  555. != pdata->loopback_rx_pkt[j]) {
  556. mismatch = 1;
  557. break;
  558. }
  559. }
  560. if (!mismatch) {
  561. SMSC_TRACE(pdata, hw, "Successfully verified "
  562. "loopback packet");
  563. return 0;
  564. } else {
  565. SMSC_WARN(pdata, hw, "Data mismatch "
  566. "during loop back test, will retry");
  567. }
  568. }
  569. }
  570. return -EIO;
  571. }
  572. static int smsc911x_phy_reset(struct smsc911x_data *pdata)
  573. {
  574. struct phy_device *phy_dev = pdata->phy_dev;
  575. unsigned int temp;
  576. unsigned int i = 100000;
  577. BUG_ON(!phy_dev);
  578. BUG_ON(!phy_dev->bus);
  579. SMSC_TRACE(pdata, hw, "Performing PHY BCR Reset");
  580. smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
  581. do {
  582. msleep(1);
  583. temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
  584. MII_BMCR);
  585. } while ((i--) && (temp & BMCR_RESET));
  586. if (temp & BMCR_RESET) {
  587. SMSC_WARN(pdata, hw, "PHY reset failed to complete");
  588. return -EIO;
  589. }
  590. /* Extra delay required because the phy may not be completed with
  591. * its reset when BMCR_RESET is cleared. Specs say 256 uS is
  592. * enough delay but using 1ms here to be safe */
  593. msleep(1);
  594. return 0;
  595. }
  596. static int smsc911x_phy_loopbacktest(struct net_device *dev)
  597. {
  598. struct smsc911x_data *pdata = netdev_priv(dev);
  599. struct phy_device *phy_dev = pdata->phy_dev;
  600. int result = -EIO;
  601. unsigned int i, val;
  602. unsigned long flags;
  603. /* Initialise tx packet using broadcast destination address */
  604. memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
  605. /* Use incrementing source address */
  606. for (i = 6; i < 12; i++)
  607. pdata->loopback_tx_pkt[i] = (char)i;
  608. /* Set length type field */
  609. pdata->loopback_tx_pkt[12] = 0x00;
  610. pdata->loopback_tx_pkt[13] = 0x00;
  611. for (i = 14; i < MIN_PACKET_SIZE; i++)
  612. pdata->loopback_tx_pkt[i] = (char)i;
  613. val = smsc911x_reg_read(pdata, HW_CFG);
  614. val &= HW_CFG_TX_FIF_SZ_;
  615. val |= HW_CFG_SF_;
  616. smsc911x_reg_write(pdata, HW_CFG, val);
  617. smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
  618. smsc911x_reg_write(pdata, RX_CFG,
  619. (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
  620. for (i = 0; i < 10; i++) {
  621. /* Set PHY to 10/FD, no ANEG, and loopback mode */
  622. smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
  623. BMCR_LOOPBACK | BMCR_FULLDPLX);
  624. /* Enable MAC tx/rx, FD */
  625. spin_lock_irqsave(&pdata->mac_lock, flags);
  626. smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
  627. | MAC_CR_TXEN_ | MAC_CR_RXEN_);
  628. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  629. if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
  630. result = 0;
  631. break;
  632. }
  633. pdata->resetcount++;
  634. /* Disable MAC rx */
  635. spin_lock_irqsave(&pdata->mac_lock, flags);
  636. smsc911x_mac_write(pdata, MAC_CR, 0);
  637. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  638. smsc911x_phy_reset(pdata);
  639. }
  640. /* Disable MAC */
  641. spin_lock_irqsave(&pdata->mac_lock, flags);
  642. smsc911x_mac_write(pdata, MAC_CR, 0);
  643. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  644. /* Cancel PHY loopback mode */
  645. smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
  646. smsc911x_reg_write(pdata, TX_CFG, 0);
  647. smsc911x_reg_write(pdata, RX_CFG, 0);
  648. return result;
  649. }
  650. #endif /* USE_PHY_WORK_AROUND */
  651. static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
  652. {
  653. struct phy_device *phy_dev = pdata->phy_dev;
  654. u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
  655. u32 flow;
  656. unsigned long flags;
  657. if (phy_dev->duplex == DUPLEX_FULL) {
  658. u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
  659. u16 rmtadv = phy_read(phy_dev, MII_LPA);
  660. u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  661. if (cap & FLOW_CTRL_RX)
  662. flow = 0xFFFF0002;
  663. else
  664. flow = 0;
  665. if (cap & FLOW_CTRL_TX)
  666. afc |= 0xF;
  667. else
  668. afc &= ~0xF;
  669. SMSC_TRACE(pdata, hw, "rx pause %s, tx pause %s",
  670. (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
  671. (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
  672. } else {
  673. SMSC_TRACE(pdata, hw, "half duplex");
  674. flow = 0;
  675. afc |= 0xF;
  676. }
  677. spin_lock_irqsave(&pdata->mac_lock, flags);
  678. smsc911x_mac_write(pdata, FLOW, flow);
  679. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  680. smsc911x_reg_write(pdata, AFC_CFG, afc);
  681. }
  682. /* Update link mode if anything has changed. Called periodically when the
  683. * PHY is in polling mode, even if nothing has changed. */
  684. static void smsc911x_phy_adjust_link(struct net_device *dev)
  685. {
  686. struct smsc911x_data *pdata = netdev_priv(dev);
  687. struct phy_device *phy_dev = pdata->phy_dev;
  688. unsigned long flags;
  689. int carrier;
  690. if (phy_dev->duplex != pdata->last_duplex) {
  691. unsigned int mac_cr;
  692. SMSC_TRACE(pdata, hw, "duplex state has changed");
  693. spin_lock_irqsave(&pdata->mac_lock, flags);
  694. mac_cr = smsc911x_mac_read(pdata, MAC_CR);
  695. if (phy_dev->duplex) {
  696. SMSC_TRACE(pdata, hw,
  697. "configuring for full duplex mode");
  698. mac_cr |= MAC_CR_FDPX_;
  699. } else {
  700. SMSC_TRACE(pdata, hw,
  701. "configuring for half duplex mode");
  702. mac_cr &= ~MAC_CR_FDPX_;
  703. }
  704. smsc911x_mac_write(pdata, MAC_CR, mac_cr);
  705. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  706. smsc911x_phy_update_flowcontrol(pdata);
  707. pdata->last_duplex = phy_dev->duplex;
  708. }
  709. carrier = netif_carrier_ok(dev);
  710. if (carrier != pdata->last_carrier) {
  711. SMSC_TRACE(pdata, hw, "carrier state has changed");
  712. if (carrier) {
  713. SMSC_TRACE(pdata, hw, "configuring for carrier OK");
  714. if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
  715. (!pdata->using_extphy)) {
  716. /* Restore original GPIO configuration */
  717. pdata->gpio_setting = pdata->gpio_orig_setting;
  718. smsc911x_reg_write(pdata, GPIO_CFG,
  719. pdata->gpio_setting);
  720. }
  721. } else {
  722. SMSC_TRACE(pdata, hw, "configuring for no carrier");
  723. /* Check global setting that LED1
  724. * usage is 10/100 indicator */
  725. pdata->gpio_setting = smsc911x_reg_read(pdata,
  726. GPIO_CFG);
  727. if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
  728. (!pdata->using_extphy)) {
  729. /* Force 10/100 LED off, after saving
  730. * original GPIO configuration */
  731. pdata->gpio_orig_setting = pdata->gpio_setting;
  732. pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
  733. pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
  734. | GPIO_CFG_GPIODIR0_
  735. | GPIO_CFG_GPIOD0_);
  736. smsc911x_reg_write(pdata, GPIO_CFG,
  737. pdata->gpio_setting);
  738. }
  739. }
  740. pdata->last_carrier = carrier;
  741. }
  742. }
  743. static int smsc911x_mii_probe(struct net_device *dev)
  744. {
  745. struct smsc911x_data *pdata = netdev_priv(dev);
  746. struct phy_device *phydev = NULL;
  747. int ret;
  748. /* find the first phy */
  749. phydev = phy_find_first(pdata->mii_bus);
  750. if (!phydev) {
  751. netdev_err(dev, "no PHY found\n");
  752. return -ENODEV;
  753. }
  754. SMSC_TRACE(pdata, probe, "PHY: addr %d, phy_id 0x%08X",
  755. phydev->addr, phydev->phy_id);
  756. ret = phy_connect_direct(dev, phydev,
  757. &smsc911x_phy_adjust_link, 0,
  758. pdata->config.phy_interface);
  759. if (ret) {
  760. netdev_err(dev, "Could not attach to PHY\n");
  761. return ret;
  762. }
  763. netdev_info(dev,
  764. "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  765. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  766. /* mask with MAC supported features */
  767. phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  768. SUPPORTED_Asym_Pause);
  769. phydev->advertising = phydev->supported;
  770. pdata->phy_dev = phydev;
  771. pdata->last_duplex = -1;
  772. pdata->last_carrier = -1;
  773. #ifdef USE_PHY_WORK_AROUND
  774. if (smsc911x_phy_loopbacktest(dev) < 0) {
  775. SMSC_WARN(pdata, hw, "Failed Loop Back Test");
  776. return -ENODEV;
  777. }
  778. SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
  779. #endif /* USE_PHY_WORK_AROUND */
  780. SMSC_TRACE(pdata, hw, "phy initialised successfully");
  781. return 0;
  782. }
  783. static int __devinit smsc911x_mii_init(struct platform_device *pdev,
  784. struct net_device *dev)
  785. {
  786. struct smsc911x_data *pdata = netdev_priv(dev);
  787. int err = -ENXIO, i;
  788. pdata->mii_bus = mdiobus_alloc();
  789. if (!pdata->mii_bus) {
  790. err = -ENOMEM;
  791. goto err_out_1;
  792. }
  793. pdata->mii_bus->name = SMSC_MDIONAME;
  794. snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
  795. pdata->mii_bus->priv = pdata;
  796. pdata->mii_bus->read = smsc911x_mii_read;
  797. pdata->mii_bus->write = smsc911x_mii_write;
  798. pdata->mii_bus->irq = pdata->phy_irq;
  799. for (i = 0; i < PHY_MAX_ADDR; ++i)
  800. pdata->mii_bus->irq[i] = PHY_POLL;
  801. pdata->mii_bus->parent = &pdev->dev;
  802. switch (pdata->idrev & 0xFFFF0000) {
  803. case 0x01170000:
  804. case 0x01150000:
  805. case 0x117A0000:
  806. case 0x115A0000:
  807. /* External PHY supported, try to autodetect */
  808. smsc911x_phy_initialise_external(pdata);
  809. break;
  810. default:
  811. SMSC_TRACE(pdata, hw, "External PHY is not supported, "
  812. "using internal PHY");
  813. pdata->using_extphy = 0;
  814. break;
  815. }
  816. if (!pdata->using_extphy) {
  817. /* Mask all PHYs except ID 1 (internal) */
  818. pdata->mii_bus->phy_mask = ~(1 << 1);
  819. }
  820. if (mdiobus_register(pdata->mii_bus)) {
  821. SMSC_WARN(pdata, probe, "Error registering mii bus");
  822. goto err_out_free_bus_2;
  823. }
  824. if (smsc911x_mii_probe(dev) < 0) {
  825. SMSC_WARN(pdata, probe, "Error registering mii bus");
  826. goto err_out_unregister_bus_3;
  827. }
  828. return 0;
  829. err_out_unregister_bus_3:
  830. mdiobus_unregister(pdata->mii_bus);
  831. err_out_free_bus_2:
  832. mdiobus_free(pdata->mii_bus);
  833. err_out_1:
  834. return err;
  835. }
  836. /* Gets the number of tx statuses in the fifo */
  837. static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
  838. {
  839. return (smsc911x_reg_read(pdata, TX_FIFO_INF)
  840. & TX_FIFO_INF_TSUSED_) >> 16;
  841. }
  842. /* Reads tx statuses and increments counters where necessary */
  843. static void smsc911x_tx_update_txcounters(struct net_device *dev)
  844. {
  845. struct smsc911x_data *pdata = netdev_priv(dev);
  846. unsigned int tx_stat;
  847. while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
  848. if (unlikely(tx_stat & 0x80000000)) {
  849. /* In this driver the packet tag is used as the packet
  850. * length. Since a packet length can never reach the
  851. * size of 0x8000, this bit is reserved. It is worth
  852. * noting that the "reserved bit" in the warning above
  853. * does not reference a hardware defined reserved bit
  854. * but rather a driver defined one.
  855. */
  856. SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
  857. } else {
  858. if (unlikely(tx_stat & TX_STS_ES_)) {
  859. dev->stats.tx_errors++;
  860. } else {
  861. dev->stats.tx_packets++;
  862. dev->stats.tx_bytes += (tx_stat >> 16);
  863. }
  864. if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
  865. dev->stats.collisions += 16;
  866. dev->stats.tx_aborted_errors += 1;
  867. } else {
  868. dev->stats.collisions +=
  869. ((tx_stat >> 3) & 0xF);
  870. }
  871. if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
  872. dev->stats.tx_carrier_errors += 1;
  873. if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
  874. dev->stats.collisions++;
  875. dev->stats.tx_aborted_errors++;
  876. }
  877. }
  878. }
  879. }
  880. /* Increments the Rx error counters */
  881. static void
  882. smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
  883. {
  884. int crc_err = 0;
  885. if (unlikely(rxstat & RX_STS_ES_)) {
  886. dev->stats.rx_errors++;
  887. if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
  888. dev->stats.rx_crc_errors++;
  889. crc_err = 1;
  890. }
  891. }
  892. if (likely(!crc_err)) {
  893. if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
  894. (rxstat & RX_STS_LENGTH_ERR_)))
  895. dev->stats.rx_length_errors++;
  896. if (rxstat & RX_STS_MCAST_)
  897. dev->stats.multicast++;
  898. }
  899. }
  900. /* Quickly dumps bad packets */
  901. static void
  902. smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktbytes)
  903. {
  904. unsigned int pktwords = (pktbytes + NET_IP_ALIGN + 3) >> 2;
  905. if (likely(pktwords >= 4)) {
  906. unsigned int timeout = 500;
  907. unsigned int val;
  908. smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
  909. do {
  910. udelay(1);
  911. val = smsc911x_reg_read(pdata, RX_DP_CTRL);
  912. } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
  913. if (unlikely(timeout == 0))
  914. SMSC_WARN(pdata, hw, "Timed out waiting for "
  915. "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
  916. } else {
  917. unsigned int temp;
  918. while (pktwords--)
  919. temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
  920. }
  921. }
  922. /* NAPI poll function */
  923. static int smsc911x_poll(struct napi_struct *napi, int budget)
  924. {
  925. struct smsc911x_data *pdata =
  926. container_of(napi, struct smsc911x_data, napi);
  927. struct net_device *dev = pdata->dev;
  928. int npackets = 0;
  929. while (npackets < budget) {
  930. unsigned int pktlength;
  931. unsigned int pktwords;
  932. struct sk_buff *skb;
  933. unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
  934. if (!rxstat) {
  935. unsigned int temp;
  936. /* We processed all packets available. Tell NAPI it can
  937. * stop polling then re-enable rx interrupts */
  938. smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
  939. napi_complete(napi);
  940. temp = smsc911x_reg_read(pdata, INT_EN);
  941. temp |= INT_EN_RSFL_EN_;
  942. smsc911x_reg_write(pdata, INT_EN, temp);
  943. break;
  944. }
  945. /* Count packet for NAPI scheduling, even if it has an error.
  946. * Error packets still require cycles to discard */
  947. npackets++;
  948. pktlength = ((rxstat & 0x3FFF0000) >> 16);
  949. pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
  950. smsc911x_rx_counterrors(dev, rxstat);
  951. if (unlikely(rxstat & RX_STS_ES_)) {
  952. SMSC_WARN(pdata, rx_err,
  953. "Discarding packet with error bit set");
  954. /* Packet has an error, discard it and continue with
  955. * the next */
  956. smsc911x_rx_fastforward(pdata, pktwords);
  957. dev->stats.rx_dropped++;
  958. continue;
  959. }
  960. skb = netdev_alloc_skb(dev, pktlength + NET_IP_ALIGN);
  961. if (unlikely(!skb)) {
  962. SMSC_WARN(pdata, rx_err,
  963. "Unable to allocate skb for rx packet");
  964. /* Drop the packet and stop this polling iteration */
  965. smsc911x_rx_fastforward(pdata, pktwords);
  966. dev->stats.rx_dropped++;
  967. break;
  968. }
  969. skb->data = skb->head;
  970. skb_reset_tail_pointer(skb);
  971. /* Align IP on 16B boundary */
  972. skb_reserve(skb, NET_IP_ALIGN);
  973. skb_put(skb, pktlength - 4);
  974. pdata->ops->rx_readfifo(pdata,
  975. (unsigned int *)skb->head, pktwords);
  976. skb->protocol = eth_type_trans(skb, dev);
  977. skb_checksum_none_assert(skb);
  978. netif_receive_skb(skb);
  979. /* Update counters */
  980. dev->stats.rx_packets++;
  981. dev->stats.rx_bytes += (pktlength - 4);
  982. }
  983. /* Return total received packets */
  984. return npackets;
  985. }
  986. /* Returns hash bit number for given MAC address
  987. * Example:
  988. * 01 00 5E 00 00 01 -> returns bit number 31 */
  989. static unsigned int smsc911x_hash(char addr[ETH_ALEN])
  990. {
  991. return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
  992. }
  993. static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
  994. {
  995. /* Performs the multicast & mac_cr update. This is called when
  996. * safe on the current hardware, and with the mac_lock held */
  997. unsigned int mac_cr;
  998. SMSC_ASSERT_MAC_LOCK(pdata);
  999. mac_cr = smsc911x_mac_read(pdata, MAC_CR);
  1000. mac_cr |= pdata->set_bits_mask;
  1001. mac_cr &= ~(pdata->clear_bits_mask);
  1002. smsc911x_mac_write(pdata, MAC_CR, mac_cr);
  1003. smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
  1004. smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
  1005. SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
  1006. mac_cr, pdata->hashhi, pdata->hashlo);
  1007. }
  1008. static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
  1009. {
  1010. unsigned int mac_cr;
  1011. /* This function is only called for older LAN911x devices
  1012. * (revA or revB), where MAC_CR, HASHH and HASHL should not
  1013. * be modified during Rx - newer devices immediately update the
  1014. * registers.
  1015. *
  1016. * This is called from interrupt context */
  1017. spin_lock(&pdata->mac_lock);
  1018. /* Check Rx has stopped */
  1019. if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
  1020. SMSC_WARN(pdata, drv, "Rx not stopped");
  1021. /* Perform the update - safe to do now Rx has stopped */
  1022. smsc911x_rx_multicast_update(pdata);
  1023. /* Re-enable Rx */
  1024. mac_cr = smsc911x_mac_read(pdata, MAC_CR);
  1025. mac_cr |= MAC_CR_RXEN_;
  1026. smsc911x_mac_write(pdata, MAC_CR, mac_cr);
  1027. pdata->multicast_update_pending = 0;
  1028. spin_unlock(&pdata->mac_lock);
  1029. }
  1030. static int smsc911x_soft_reset(struct smsc911x_data *pdata)
  1031. {
  1032. unsigned int timeout;
  1033. unsigned int temp;
  1034. /* Reset the LAN911x */
  1035. smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
  1036. timeout = 10;
  1037. do {
  1038. udelay(10);
  1039. temp = smsc911x_reg_read(pdata, HW_CFG);
  1040. } while ((--timeout) && (temp & HW_CFG_SRST_));
  1041. if (unlikely(temp & HW_CFG_SRST_)) {
  1042. SMSC_WARN(pdata, drv, "Failed to complete reset");
  1043. return -EIO;
  1044. }
  1045. return 0;
  1046. }
  1047. /* Sets the device MAC address to dev_addr, called with mac_lock held */
  1048. static void
  1049. smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
  1050. {
  1051. u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
  1052. u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
  1053. (dev_addr[1] << 8) | dev_addr[0];
  1054. SMSC_ASSERT_MAC_LOCK(pdata);
  1055. smsc911x_mac_write(pdata, ADDRH, mac_high16);
  1056. smsc911x_mac_write(pdata, ADDRL, mac_low32);
  1057. }
  1058. static int smsc911x_open(struct net_device *dev)
  1059. {
  1060. struct smsc911x_data *pdata = netdev_priv(dev);
  1061. unsigned int timeout;
  1062. unsigned int temp;
  1063. unsigned int intcfg;
  1064. /* if the phy is not yet registered, retry later*/
  1065. if (!pdata->phy_dev) {
  1066. SMSC_WARN(pdata, hw, "phy_dev is NULL");
  1067. return -EAGAIN;
  1068. }
  1069. if (!is_valid_ether_addr(dev->dev_addr)) {
  1070. SMSC_WARN(pdata, hw, "dev_addr is not a valid MAC address");
  1071. return -EADDRNOTAVAIL;
  1072. }
  1073. /* Reset the LAN911x */
  1074. if (smsc911x_soft_reset(pdata)) {
  1075. SMSC_WARN(pdata, hw, "soft reset failed");
  1076. return -EIO;
  1077. }
  1078. smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
  1079. smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
  1080. /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
  1081. spin_lock_irq(&pdata->mac_lock);
  1082. smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
  1083. spin_unlock_irq(&pdata->mac_lock);
  1084. /* Make sure EEPROM has finished loading before setting GPIO_CFG */
  1085. timeout = 50;
  1086. while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
  1087. --timeout) {
  1088. udelay(10);
  1089. }
  1090. if (unlikely(timeout == 0))
  1091. SMSC_WARN(pdata, ifup,
  1092. "Timed out waiting for EEPROM busy bit to clear");
  1093. smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
  1094. /* The soft reset above cleared the device's MAC address,
  1095. * restore it from local copy (set in probe) */
  1096. spin_lock_irq(&pdata->mac_lock);
  1097. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1098. spin_unlock_irq(&pdata->mac_lock);
  1099. /* Initialise irqs, but leave all sources disabled */
  1100. smsc911x_reg_write(pdata, INT_EN, 0);
  1101. smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
  1102. /* Set interrupt deassertion to 100uS */
  1103. intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
  1104. if (pdata->config.irq_polarity) {
  1105. SMSC_TRACE(pdata, ifup, "irq polarity: active high");
  1106. intcfg |= INT_CFG_IRQ_POL_;
  1107. } else {
  1108. SMSC_TRACE(pdata, ifup, "irq polarity: active low");
  1109. }
  1110. if (pdata->config.irq_type) {
  1111. SMSC_TRACE(pdata, ifup, "irq type: push-pull");
  1112. intcfg |= INT_CFG_IRQ_TYPE_;
  1113. } else {
  1114. SMSC_TRACE(pdata, ifup, "irq type: open drain");
  1115. }
  1116. smsc911x_reg_write(pdata, INT_CFG, intcfg);
  1117. SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
  1118. pdata->software_irq_signal = 0;
  1119. smp_wmb();
  1120. temp = smsc911x_reg_read(pdata, INT_EN);
  1121. temp |= INT_EN_SW_INT_EN_;
  1122. smsc911x_reg_write(pdata, INT_EN, temp);
  1123. timeout = 1000;
  1124. while (timeout--) {
  1125. if (pdata->software_irq_signal)
  1126. break;
  1127. msleep(1);
  1128. }
  1129. if (!pdata->software_irq_signal) {
  1130. netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
  1131. dev->irq);
  1132. return -ENODEV;
  1133. }
  1134. SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
  1135. dev->irq);
  1136. netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
  1137. (unsigned long)pdata->ioaddr, dev->irq);
  1138. /* Reset the last known duplex and carrier */
  1139. pdata->last_duplex = -1;
  1140. pdata->last_carrier = -1;
  1141. /* Bring the PHY up */
  1142. phy_start(pdata->phy_dev);
  1143. temp = smsc911x_reg_read(pdata, HW_CFG);
  1144. /* Preserve TX FIFO size and external PHY configuration */
  1145. temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
  1146. temp |= HW_CFG_SF_;
  1147. smsc911x_reg_write(pdata, HW_CFG, temp);
  1148. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1149. temp |= FIFO_INT_TX_AVAIL_LEVEL_;
  1150. temp &= ~(FIFO_INT_RX_STS_LEVEL_);
  1151. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1152. /* set RX Data offset to 2 bytes for alignment */
  1153. smsc911x_reg_write(pdata, RX_CFG, (2 << 8));
  1154. /* enable NAPI polling before enabling RX interrupts */
  1155. napi_enable(&pdata->napi);
  1156. temp = smsc911x_reg_read(pdata, INT_EN);
  1157. temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
  1158. smsc911x_reg_write(pdata, INT_EN, temp);
  1159. spin_lock_irq(&pdata->mac_lock);
  1160. temp = smsc911x_mac_read(pdata, MAC_CR);
  1161. temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
  1162. smsc911x_mac_write(pdata, MAC_CR, temp);
  1163. spin_unlock_irq(&pdata->mac_lock);
  1164. smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
  1165. netif_start_queue(dev);
  1166. return 0;
  1167. }
  1168. /* Entry point for stopping the interface */
  1169. static int smsc911x_stop(struct net_device *dev)
  1170. {
  1171. struct smsc911x_data *pdata = netdev_priv(dev);
  1172. unsigned int temp;
  1173. /* Disable all device interrupts */
  1174. temp = smsc911x_reg_read(pdata, INT_CFG);
  1175. temp &= ~INT_CFG_IRQ_EN_;
  1176. smsc911x_reg_write(pdata, INT_CFG, temp);
  1177. /* Stop Tx and Rx polling */
  1178. netif_stop_queue(dev);
  1179. napi_disable(&pdata->napi);
  1180. /* At this point all Rx and Tx activity is stopped */
  1181. dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
  1182. smsc911x_tx_update_txcounters(dev);
  1183. /* Bring the PHY down */
  1184. if (pdata->phy_dev)
  1185. phy_stop(pdata->phy_dev);
  1186. SMSC_TRACE(pdata, ifdown, "Interface stopped");
  1187. return 0;
  1188. }
  1189. /* Entry point for transmitting a packet */
  1190. static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1191. {
  1192. struct smsc911x_data *pdata = netdev_priv(dev);
  1193. unsigned int freespace;
  1194. unsigned int tx_cmd_a;
  1195. unsigned int tx_cmd_b;
  1196. unsigned int temp;
  1197. u32 wrsz;
  1198. ulong bufp;
  1199. freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
  1200. if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
  1201. SMSC_WARN(pdata, tx_err,
  1202. "Tx data fifo low, space available: %d", freespace);
  1203. /* Word alignment adjustment */
  1204. tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
  1205. tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
  1206. tx_cmd_a |= (unsigned int)skb->len;
  1207. tx_cmd_b = ((unsigned int)skb->len) << 16;
  1208. tx_cmd_b |= (unsigned int)skb->len;
  1209. smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
  1210. smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
  1211. bufp = (ulong)skb->data & (~0x3);
  1212. wrsz = (u32)skb->len + 3;
  1213. wrsz += (u32)((ulong)skb->data & 0x3);
  1214. wrsz >>= 2;
  1215. pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
  1216. freespace -= (skb->len + 32);
  1217. dev_kfree_skb(skb);
  1218. if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
  1219. smsc911x_tx_update_txcounters(dev);
  1220. if (freespace < TX_FIFO_LOW_THRESHOLD) {
  1221. netif_stop_queue(dev);
  1222. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1223. temp &= 0x00FFFFFF;
  1224. temp |= 0x32000000;
  1225. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1226. }
  1227. return NETDEV_TX_OK;
  1228. }
  1229. /* Entry point for getting status counters */
  1230. static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
  1231. {
  1232. struct smsc911x_data *pdata = netdev_priv(dev);
  1233. smsc911x_tx_update_txcounters(dev);
  1234. dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
  1235. return &dev->stats;
  1236. }
  1237. /* Entry point for setting addressing modes */
  1238. static void smsc911x_set_multicast_list(struct net_device *dev)
  1239. {
  1240. struct smsc911x_data *pdata = netdev_priv(dev);
  1241. unsigned long flags;
  1242. if (dev->flags & IFF_PROMISC) {
  1243. /* Enabling promiscuous mode */
  1244. pdata->set_bits_mask = MAC_CR_PRMS_;
  1245. pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
  1246. pdata->hashhi = 0;
  1247. pdata->hashlo = 0;
  1248. } else if (dev->flags & IFF_ALLMULTI) {
  1249. /* Enabling all multicast mode */
  1250. pdata->set_bits_mask = MAC_CR_MCPAS_;
  1251. pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
  1252. pdata->hashhi = 0;
  1253. pdata->hashlo = 0;
  1254. } else if (!netdev_mc_empty(dev)) {
  1255. /* Enabling specific multicast addresses */
  1256. unsigned int hash_high = 0;
  1257. unsigned int hash_low = 0;
  1258. struct netdev_hw_addr *ha;
  1259. pdata->set_bits_mask = MAC_CR_HPFILT_;
  1260. pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1261. netdev_for_each_mc_addr(ha, dev) {
  1262. unsigned int bitnum = smsc911x_hash(ha->addr);
  1263. unsigned int mask = 0x01 << (bitnum & 0x1F);
  1264. if (bitnum & 0x20)
  1265. hash_high |= mask;
  1266. else
  1267. hash_low |= mask;
  1268. }
  1269. pdata->hashhi = hash_high;
  1270. pdata->hashlo = hash_low;
  1271. } else {
  1272. /* Enabling local MAC address only */
  1273. pdata->set_bits_mask = 0;
  1274. pdata->clear_bits_mask =
  1275. (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
  1276. pdata->hashhi = 0;
  1277. pdata->hashlo = 0;
  1278. }
  1279. spin_lock_irqsave(&pdata->mac_lock, flags);
  1280. if (pdata->generation <= 1) {
  1281. /* Older hardware revision - cannot change these flags while
  1282. * receiving data */
  1283. if (!pdata->multicast_update_pending) {
  1284. unsigned int temp;
  1285. SMSC_TRACE(pdata, hw, "scheduling mcast update");
  1286. pdata->multicast_update_pending = 1;
  1287. /* Request the hardware to stop, then perform the
  1288. * update when we get an RX_STOP interrupt */
  1289. temp = smsc911x_mac_read(pdata, MAC_CR);
  1290. temp &= ~(MAC_CR_RXEN_);
  1291. smsc911x_mac_write(pdata, MAC_CR, temp);
  1292. } else {
  1293. /* There is another update pending, this should now
  1294. * use the newer values */
  1295. }
  1296. } else {
  1297. /* Newer hardware revision - can write immediately */
  1298. smsc911x_rx_multicast_update(pdata);
  1299. }
  1300. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  1301. }
  1302. static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
  1303. {
  1304. struct net_device *dev = dev_id;
  1305. struct smsc911x_data *pdata = netdev_priv(dev);
  1306. u32 intsts = smsc911x_reg_read(pdata, INT_STS);
  1307. u32 inten = smsc911x_reg_read(pdata, INT_EN);
  1308. int serviced = IRQ_NONE;
  1309. u32 temp;
  1310. if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
  1311. temp = smsc911x_reg_read(pdata, INT_EN);
  1312. temp &= (~INT_EN_SW_INT_EN_);
  1313. smsc911x_reg_write(pdata, INT_EN, temp);
  1314. smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
  1315. pdata->software_irq_signal = 1;
  1316. smp_wmb();
  1317. serviced = IRQ_HANDLED;
  1318. }
  1319. if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
  1320. /* Called when there is a multicast update scheduled and
  1321. * it is now safe to complete the update */
  1322. SMSC_TRACE(pdata, intr, "RX Stop interrupt");
  1323. smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
  1324. if (pdata->multicast_update_pending)
  1325. smsc911x_rx_multicast_update_workaround(pdata);
  1326. serviced = IRQ_HANDLED;
  1327. }
  1328. if (intsts & inten & INT_STS_TDFA_) {
  1329. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1330. temp |= FIFO_INT_TX_AVAIL_LEVEL_;
  1331. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1332. smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
  1333. netif_wake_queue(dev);
  1334. serviced = IRQ_HANDLED;
  1335. }
  1336. if (unlikely(intsts & inten & INT_STS_RXE_)) {
  1337. SMSC_TRACE(pdata, intr, "RX Error interrupt");
  1338. smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
  1339. serviced = IRQ_HANDLED;
  1340. }
  1341. if (likely(intsts & inten & INT_STS_RSFL_)) {
  1342. if (likely(napi_schedule_prep(&pdata->napi))) {
  1343. /* Disable Rx interrupts */
  1344. temp = smsc911x_reg_read(pdata, INT_EN);
  1345. temp &= (~INT_EN_RSFL_EN_);
  1346. smsc911x_reg_write(pdata, INT_EN, temp);
  1347. /* Schedule a NAPI poll */
  1348. __napi_schedule(&pdata->napi);
  1349. } else {
  1350. SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
  1351. }
  1352. serviced = IRQ_HANDLED;
  1353. }
  1354. return serviced;
  1355. }
  1356. #ifdef CONFIG_NET_POLL_CONTROLLER
  1357. static void smsc911x_poll_controller(struct net_device *dev)
  1358. {
  1359. disable_irq(dev->irq);
  1360. smsc911x_irqhandler(0, dev);
  1361. enable_irq(dev->irq);
  1362. }
  1363. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1364. static int smsc911x_set_mac_address(struct net_device *dev, void *p)
  1365. {
  1366. struct smsc911x_data *pdata = netdev_priv(dev);
  1367. struct sockaddr *addr = p;
  1368. /* On older hardware revisions we cannot change the mac address
  1369. * registers while receiving data. Newer devices can safely change
  1370. * this at any time. */
  1371. if (pdata->generation <= 1 && netif_running(dev))
  1372. return -EBUSY;
  1373. if (!is_valid_ether_addr(addr->sa_data))
  1374. return -EADDRNOTAVAIL;
  1375. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  1376. spin_lock_irq(&pdata->mac_lock);
  1377. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1378. spin_unlock_irq(&pdata->mac_lock);
  1379. netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
  1380. return 0;
  1381. }
  1382. /* Standard ioctls for mii-tool */
  1383. static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1384. {
  1385. struct smsc911x_data *pdata = netdev_priv(dev);
  1386. if (!netif_running(dev) || !pdata->phy_dev)
  1387. return -EINVAL;
  1388. return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
  1389. }
  1390. static int
  1391. smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1392. {
  1393. struct smsc911x_data *pdata = netdev_priv(dev);
  1394. cmd->maxtxpkt = 1;
  1395. cmd->maxrxpkt = 1;
  1396. return phy_ethtool_gset(pdata->phy_dev, cmd);
  1397. }
  1398. static int
  1399. smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1400. {
  1401. struct smsc911x_data *pdata = netdev_priv(dev);
  1402. return phy_ethtool_sset(pdata->phy_dev, cmd);
  1403. }
  1404. static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
  1405. struct ethtool_drvinfo *info)
  1406. {
  1407. strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
  1408. strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
  1409. strlcpy(info->bus_info, dev_name(dev->dev.parent),
  1410. sizeof(info->bus_info));
  1411. }
  1412. static int smsc911x_ethtool_nwayreset(struct net_device *dev)
  1413. {
  1414. struct smsc911x_data *pdata = netdev_priv(dev);
  1415. return phy_start_aneg(pdata->phy_dev);
  1416. }
  1417. static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
  1418. {
  1419. struct smsc911x_data *pdata = netdev_priv(dev);
  1420. return pdata->msg_enable;
  1421. }
  1422. static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
  1423. {
  1424. struct smsc911x_data *pdata = netdev_priv(dev);
  1425. pdata->msg_enable = level;
  1426. }
  1427. static int smsc911x_ethtool_getregslen(struct net_device *dev)
  1428. {
  1429. return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
  1430. sizeof(u32);
  1431. }
  1432. static void
  1433. smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
  1434. void *buf)
  1435. {
  1436. struct smsc911x_data *pdata = netdev_priv(dev);
  1437. struct phy_device *phy_dev = pdata->phy_dev;
  1438. unsigned long flags;
  1439. unsigned int i;
  1440. unsigned int j = 0;
  1441. u32 *data = buf;
  1442. regs->version = pdata->idrev;
  1443. for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
  1444. data[j++] = smsc911x_reg_read(pdata, i);
  1445. for (i = MAC_CR; i <= WUCSR; i++) {
  1446. spin_lock_irqsave(&pdata->mac_lock, flags);
  1447. data[j++] = smsc911x_mac_read(pdata, i);
  1448. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  1449. }
  1450. for (i = 0; i <= 31; i++)
  1451. data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
  1452. }
  1453. static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
  1454. {
  1455. unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
  1456. temp &= ~GPIO_CFG_EEPR_EN_;
  1457. smsc911x_reg_write(pdata, GPIO_CFG, temp);
  1458. msleep(1);
  1459. }
  1460. static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
  1461. {
  1462. int timeout = 100;
  1463. u32 e2cmd;
  1464. SMSC_TRACE(pdata, drv, "op 0x%08x", op);
  1465. if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  1466. SMSC_WARN(pdata, drv, "Busy at start");
  1467. return -EBUSY;
  1468. }
  1469. e2cmd = op | E2P_CMD_EPC_BUSY_;
  1470. smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
  1471. do {
  1472. msleep(1);
  1473. e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
  1474. } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
  1475. if (!timeout) {
  1476. SMSC_TRACE(pdata, drv, "TIMED OUT");
  1477. return -EAGAIN;
  1478. }
  1479. if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
  1480. SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
  1481. return -EINVAL;
  1482. }
  1483. return 0;
  1484. }
  1485. static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
  1486. u8 address, u8 *data)
  1487. {
  1488. u32 op = E2P_CMD_EPC_CMD_READ_ | address;
  1489. int ret;
  1490. SMSC_TRACE(pdata, drv, "address 0x%x", address);
  1491. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1492. if (!ret)
  1493. data[address] = smsc911x_reg_read(pdata, E2P_DATA);
  1494. return ret;
  1495. }
  1496. static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
  1497. u8 address, u8 data)
  1498. {
  1499. u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
  1500. u32 temp;
  1501. int ret;
  1502. SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
  1503. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1504. if (!ret) {
  1505. op = E2P_CMD_EPC_CMD_WRITE_ | address;
  1506. smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
  1507. /* Workaround for hardware read-after-write restriction */
  1508. temp = smsc911x_reg_read(pdata, BYTE_TEST);
  1509. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1510. }
  1511. return ret;
  1512. }
  1513. static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
  1514. {
  1515. return SMSC911X_EEPROM_SIZE;
  1516. }
  1517. static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
  1518. struct ethtool_eeprom *eeprom, u8 *data)
  1519. {
  1520. struct smsc911x_data *pdata = netdev_priv(dev);
  1521. u8 eeprom_data[SMSC911X_EEPROM_SIZE];
  1522. int len;
  1523. int i;
  1524. smsc911x_eeprom_enable_access(pdata);
  1525. len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
  1526. for (i = 0; i < len; i++) {
  1527. int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
  1528. if (ret < 0) {
  1529. eeprom->len = 0;
  1530. return ret;
  1531. }
  1532. }
  1533. memcpy(data, &eeprom_data[eeprom->offset], len);
  1534. eeprom->len = len;
  1535. return 0;
  1536. }
  1537. static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
  1538. struct ethtool_eeprom *eeprom, u8 *data)
  1539. {
  1540. int ret;
  1541. struct smsc911x_data *pdata = netdev_priv(dev);
  1542. smsc911x_eeprom_enable_access(pdata);
  1543. smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
  1544. ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
  1545. smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
  1546. /* Single byte write, according to man page */
  1547. eeprom->len = 1;
  1548. return ret;
  1549. }
  1550. static const struct ethtool_ops smsc911x_ethtool_ops = {
  1551. .get_settings = smsc911x_ethtool_getsettings,
  1552. .set_settings = smsc911x_ethtool_setsettings,
  1553. .get_link = ethtool_op_get_link,
  1554. .get_drvinfo = smsc911x_ethtool_getdrvinfo,
  1555. .nway_reset = smsc911x_ethtool_nwayreset,
  1556. .get_msglevel = smsc911x_ethtool_getmsglevel,
  1557. .set_msglevel = smsc911x_ethtool_setmsglevel,
  1558. .get_regs_len = smsc911x_ethtool_getregslen,
  1559. .get_regs = smsc911x_ethtool_getregs,
  1560. .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
  1561. .get_eeprom = smsc911x_ethtool_get_eeprom,
  1562. .set_eeprom = smsc911x_ethtool_set_eeprom,
  1563. };
  1564. static const struct net_device_ops smsc911x_netdev_ops = {
  1565. .ndo_open = smsc911x_open,
  1566. .ndo_stop = smsc911x_stop,
  1567. .ndo_start_xmit = smsc911x_hard_start_xmit,
  1568. .ndo_get_stats = smsc911x_get_stats,
  1569. .ndo_set_multicast_list = smsc911x_set_multicast_list,
  1570. .ndo_do_ioctl = smsc911x_do_ioctl,
  1571. .ndo_change_mtu = eth_change_mtu,
  1572. .ndo_validate_addr = eth_validate_addr,
  1573. .ndo_set_mac_address = smsc911x_set_mac_address,
  1574. #ifdef CONFIG_NET_POLL_CONTROLLER
  1575. .ndo_poll_controller = smsc911x_poll_controller,
  1576. #endif
  1577. };
  1578. /* copies the current mac address from hardware to dev->dev_addr */
  1579. static void __devinit smsc911x_read_mac_address(struct net_device *dev)
  1580. {
  1581. struct smsc911x_data *pdata = netdev_priv(dev);
  1582. u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
  1583. u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
  1584. dev->dev_addr[0] = (u8)(mac_low32);
  1585. dev->dev_addr[1] = (u8)(mac_low32 >> 8);
  1586. dev->dev_addr[2] = (u8)(mac_low32 >> 16);
  1587. dev->dev_addr[3] = (u8)(mac_low32 >> 24);
  1588. dev->dev_addr[4] = (u8)(mac_high16);
  1589. dev->dev_addr[5] = (u8)(mac_high16 >> 8);
  1590. }
  1591. /* Initializing private device structures, only called from probe */
  1592. static int __devinit smsc911x_init(struct net_device *dev)
  1593. {
  1594. struct smsc911x_data *pdata = netdev_priv(dev);
  1595. unsigned int byte_test;
  1596. SMSC_TRACE(pdata, probe, "Driver Parameters:");
  1597. SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
  1598. (unsigned long)pdata->ioaddr);
  1599. SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
  1600. SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
  1601. spin_lock_init(&pdata->dev_lock);
  1602. spin_lock_init(&pdata->mac_lock);
  1603. if (pdata->ioaddr == 0) {
  1604. SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
  1605. return -ENODEV;
  1606. }
  1607. /* Check byte ordering */
  1608. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1609. SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
  1610. if (byte_test == 0x43218765) {
  1611. SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
  1612. "applying WORD_SWAP");
  1613. smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
  1614. /* 1 dummy read of BYTE_TEST is needed after a write to
  1615. * WORD_SWAP before its contents are valid */
  1616. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1617. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1618. }
  1619. if (byte_test != 0x87654321) {
  1620. SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
  1621. if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
  1622. SMSC_WARN(pdata, probe,
  1623. "top 16 bits equal to bottom 16 bits");
  1624. SMSC_TRACE(pdata, probe,
  1625. "This may mean the chip is set "
  1626. "for 32 bit while the bus is reading 16 bit");
  1627. }
  1628. return -ENODEV;
  1629. }
  1630. /* Default generation to zero (all workarounds apply) */
  1631. pdata->generation = 0;
  1632. pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
  1633. switch (pdata->idrev & 0xFFFF0000) {
  1634. case 0x01180000:
  1635. case 0x01170000:
  1636. case 0x01160000:
  1637. case 0x01150000:
  1638. /* LAN911[5678] family */
  1639. pdata->generation = pdata->idrev & 0x0000FFFF;
  1640. break;
  1641. case 0x118A0000:
  1642. case 0x117A0000:
  1643. case 0x116A0000:
  1644. case 0x115A0000:
  1645. /* LAN921[5678] family */
  1646. pdata->generation = 3;
  1647. break;
  1648. case 0x92100000:
  1649. case 0x92110000:
  1650. case 0x92200000:
  1651. case 0x92210000:
  1652. /* LAN9210/LAN9211/LAN9220/LAN9221 */
  1653. pdata->generation = 4;
  1654. break;
  1655. default:
  1656. SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
  1657. pdata->idrev);
  1658. return -ENODEV;
  1659. }
  1660. SMSC_TRACE(pdata, probe,
  1661. "LAN911x identified, idrev: 0x%08X, generation: %d",
  1662. pdata->idrev, pdata->generation);
  1663. if (pdata->generation == 0)
  1664. SMSC_WARN(pdata, probe,
  1665. "This driver is not intended for this chip revision");
  1666. /* workaround for platforms without an eeprom, where the mac address
  1667. * is stored elsewhere and set by the bootloader. This saves the
  1668. * mac address before resetting the device */
  1669. if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
  1670. spin_lock_irq(&pdata->mac_lock);
  1671. smsc911x_read_mac_address(dev);
  1672. spin_unlock_irq(&pdata->mac_lock);
  1673. }
  1674. /* Reset the LAN911x */
  1675. if (smsc911x_soft_reset(pdata))
  1676. return -ENODEV;
  1677. /* Disable all interrupt sources until we bring the device up */
  1678. smsc911x_reg_write(pdata, INT_EN, 0);
  1679. ether_setup(dev);
  1680. dev->flags |= IFF_MULTICAST;
  1681. netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
  1682. dev->netdev_ops = &smsc911x_netdev_ops;
  1683. dev->ethtool_ops = &smsc911x_ethtool_ops;
  1684. return 0;
  1685. }
  1686. static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
  1687. {
  1688. struct net_device *dev;
  1689. struct smsc911x_data *pdata;
  1690. struct resource *res;
  1691. dev = platform_get_drvdata(pdev);
  1692. BUG_ON(!dev);
  1693. pdata = netdev_priv(dev);
  1694. BUG_ON(!pdata);
  1695. BUG_ON(!pdata->ioaddr);
  1696. BUG_ON(!pdata->phy_dev);
  1697. SMSC_TRACE(pdata, ifdown, "Stopping driver");
  1698. phy_disconnect(pdata->phy_dev);
  1699. pdata->phy_dev = NULL;
  1700. mdiobus_unregister(pdata->mii_bus);
  1701. mdiobus_free(pdata->mii_bus);
  1702. platform_set_drvdata(pdev, NULL);
  1703. unregister_netdev(dev);
  1704. free_irq(dev->irq, dev);
  1705. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1706. "smsc911x-memory");
  1707. if (!res)
  1708. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1709. release_mem_region(res->start, resource_size(res));
  1710. iounmap(pdata->ioaddr);
  1711. free_netdev(dev);
  1712. return 0;
  1713. }
  1714. /* standard register acces */
  1715. static const struct smsc911x_ops standard_smsc911x_ops = {
  1716. .reg_read = __smsc911x_reg_read,
  1717. .reg_write = __smsc911x_reg_write,
  1718. .rx_readfifo = smsc911x_rx_readfifo,
  1719. .tx_writefifo = smsc911x_tx_writefifo,
  1720. };
  1721. /* shifted register access */
  1722. static const struct smsc911x_ops shifted_smsc911x_ops = {
  1723. .reg_read = __smsc911x_reg_read_shift,
  1724. .reg_write = __smsc911x_reg_write_shift,
  1725. .rx_readfifo = smsc911x_rx_readfifo_shift,
  1726. .tx_writefifo = smsc911x_tx_writefifo_shift,
  1727. };
  1728. static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
  1729. {
  1730. struct net_device *dev;
  1731. struct smsc911x_data *pdata;
  1732. struct smsc911x_platform_config *config = pdev->dev.platform_data;
  1733. struct resource *res, *irq_res;
  1734. unsigned int intcfg = 0;
  1735. int res_size, irq_flags;
  1736. int retval;
  1737. pr_info("Driver version %s\n", SMSC_DRV_VERSION);
  1738. /* platform data specifies irq & dynamic bus configuration */
  1739. if (!pdev->dev.platform_data) {
  1740. pr_warn("platform_data not provided\n");
  1741. retval = -ENODEV;
  1742. goto out_0;
  1743. }
  1744. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1745. "smsc911x-memory");
  1746. if (!res)
  1747. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1748. if (!res) {
  1749. pr_warn("Could not allocate resource\n");
  1750. retval = -ENODEV;
  1751. goto out_0;
  1752. }
  1753. res_size = resource_size(res);
  1754. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1755. if (!irq_res) {
  1756. pr_warn("Could not allocate irq resource\n");
  1757. retval = -ENODEV;
  1758. goto out_0;
  1759. }
  1760. if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
  1761. retval = -EBUSY;
  1762. goto out_0;
  1763. }
  1764. dev = alloc_etherdev(sizeof(struct smsc911x_data));
  1765. if (!dev) {
  1766. pr_warn("Could not allocate device\n");
  1767. retval = -ENOMEM;
  1768. goto out_release_io_1;
  1769. }
  1770. SET_NETDEV_DEV(dev, &pdev->dev);
  1771. pdata = netdev_priv(dev);
  1772. dev->irq = irq_res->start;
  1773. irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
  1774. pdata->ioaddr = ioremap_nocache(res->start, res_size);
  1775. /* copy config parameters across to pdata */
  1776. memcpy(&pdata->config, config, sizeof(pdata->config));
  1777. pdata->dev = dev;
  1778. pdata->msg_enable = ((1 << debug) - 1);
  1779. if (pdata->ioaddr == NULL) {
  1780. SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
  1781. retval = -ENOMEM;
  1782. goto out_free_netdev_2;
  1783. }
  1784. /* assume standard, non-shifted, access to HW registers */
  1785. pdata->ops = &standard_smsc911x_ops;
  1786. /* apply the right access if shifting is needed */
  1787. if (config->shift)
  1788. pdata->ops = &shifted_smsc911x_ops;
  1789. retval = smsc911x_init(dev);
  1790. if (retval < 0)
  1791. goto out_unmap_io_3;
  1792. /* configure irq polarity and type before connecting isr */
  1793. if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
  1794. intcfg |= INT_CFG_IRQ_POL_;
  1795. if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
  1796. intcfg |= INT_CFG_IRQ_TYPE_;
  1797. smsc911x_reg_write(pdata, INT_CFG, intcfg);
  1798. /* Ensure interrupts are globally disabled before connecting ISR */
  1799. smsc911x_reg_write(pdata, INT_EN, 0);
  1800. smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
  1801. retval = request_irq(dev->irq, smsc911x_irqhandler,
  1802. irq_flags | IRQF_SHARED, dev->name, dev);
  1803. if (retval) {
  1804. SMSC_WARN(pdata, probe,
  1805. "Unable to claim requested irq: %d", dev->irq);
  1806. goto out_unmap_io_3;
  1807. }
  1808. platform_set_drvdata(pdev, dev);
  1809. retval = register_netdev(dev);
  1810. if (retval) {
  1811. SMSC_WARN(pdata, probe, "Error %i registering device", retval);
  1812. goto out_unset_drvdata_4;
  1813. } else {
  1814. SMSC_TRACE(pdata, probe,
  1815. "Network interface: \"%s\"", dev->name);
  1816. }
  1817. retval = smsc911x_mii_init(pdev, dev);
  1818. if (retval) {
  1819. SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
  1820. goto out_unregister_netdev_5;
  1821. }
  1822. spin_lock_irq(&pdata->mac_lock);
  1823. /* Check if mac address has been specified when bringing interface up */
  1824. if (is_valid_ether_addr(dev->dev_addr)) {
  1825. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1826. SMSC_TRACE(pdata, probe,
  1827. "MAC Address is specified by configuration");
  1828. } else if (is_valid_ether_addr(pdata->config.mac)) {
  1829. memcpy(dev->dev_addr, pdata->config.mac, 6);
  1830. SMSC_TRACE(pdata, probe,
  1831. "MAC Address specified by platform data");
  1832. } else {
  1833. /* Try reading mac address from device. if EEPROM is present
  1834. * it will already have been set */
  1835. smsc_get_mac(dev);
  1836. if (is_valid_ether_addr(dev->dev_addr)) {
  1837. /* eeprom values are valid so use them */
  1838. SMSC_TRACE(pdata, probe,
  1839. "Mac Address is read from LAN911x EEPROM");
  1840. } else {
  1841. /* eeprom values are invalid, generate random MAC */
  1842. random_ether_addr(dev->dev_addr);
  1843. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1844. SMSC_TRACE(pdata, probe,
  1845. "MAC Address is set to random_ether_addr");
  1846. }
  1847. }
  1848. spin_unlock_irq(&pdata->mac_lock);
  1849. netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
  1850. return 0;
  1851. out_unregister_netdev_5:
  1852. unregister_netdev(dev);
  1853. out_unset_drvdata_4:
  1854. platform_set_drvdata(pdev, NULL);
  1855. free_irq(dev->irq, dev);
  1856. out_unmap_io_3:
  1857. iounmap(pdata->ioaddr);
  1858. out_free_netdev_2:
  1859. free_netdev(dev);
  1860. out_release_io_1:
  1861. release_mem_region(res->start, resource_size(res));
  1862. out_0:
  1863. return retval;
  1864. }
  1865. #ifdef CONFIG_PM
  1866. /* This implementation assumes the devices remains powered on its VDDVARIO
  1867. * pins during suspend. */
  1868. /* TODO: implement freeze/thaw callbacks for hibernation.*/
  1869. static int smsc911x_suspend(struct device *dev)
  1870. {
  1871. struct net_device *ndev = dev_get_drvdata(dev);
  1872. struct smsc911x_data *pdata = netdev_priv(ndev);
  1873. /* enable wake on LAN, energy detection and the external PME
  1874. * signal. */
  1875. smsc911x_reg_write(pdata, PMT_CTRL,
  1876. PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
  1877. PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
  1878. return 0;
  1879. }
  1880. static int smsc911x_resume(struct device *dev)
  1881. {
  1882. struct net_device *ndev = dev_get_drvdata(dev);
  1883. struct smsc911x_data *pdata = netdev_priv(ndev);
  1884. unsigned int to = 100;
  1885. /* Note 3.11 from the datasheet:
  1886. * "When the LAN9220 is in a power saving state, a write of any
  1887. * data to the BYTE_TEST register will wake-up the device."
  1888. */
  1889. smsc911x_reg_write(pdata, BYTE_TEST, 0);
  1890. /* poll the READY bit in PMT_CTRL. Any other access to the device is
  1891. * forbidden while this bit isn't set. Try for 100ms and return -EIO
  1892. * if it failed. */
  1893. while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
  1894. udelay(1000);
  1895. return (to == 0) ? -EIO : 0;
  1896. }
  1897. static const struct dev_pm_ops smsc911x_pm_ops = {
  1898. .suspend = smsc911x_suspend,
  1899. .resume = smsc911x_resume,
  1900. };
  1901. #define SMSC911X_PM_OPS (&smsc911x_pm_ops)
  1902. #else
  1903. #define SMSC911X_PM_OPS NULL
  1904. #endif
  1905. static struct platform_driver smsc911x_driver = {
  1906. .probe = smsc911x_drv_probe,
  1907. .remove = __devexit_p(smsc911x_drv_remove),
  1908. .driver = {
  1909. .name = SMSC_CHIPNAME,
  1910. .owner = THIS_MODULE,
  1911. .pm = SMSC911X_PM_OPS,
  1912. },
  1913. };
  1914. /* Entry point for loading the module */
  1915. static int __init smsc911x_init_module(void)
  1916. {
  1917. SMSC_INITIALIZE();
  1918. return platform_driver_register(&smsc911x_driver);
  1919. }
  1920. /* entry point for unloading the module */
  1921. static void __exit smsc911x_cleanup_module(void)
  1922. {
  1923. platform_driver_unregister(&smsc911x_driver);
  1924. }
  1925. module_init(smsc911x_init_module);
  1926. module_exit(smsc911x_cleanup_module);