smsc911x.c 58 KB

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