smsc911x.c 62 KB

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