smsc911x.c 58 KB

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