smsc911x.c 68 KB

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