smsc911x.c 70 KB

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