smsc911x.c 69 KB

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