smsc911x.c 70 KB

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