smsc911x.c 69 KB

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