smsc911x.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677
  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->mdio.bus, phy_dev->mdio.addr,
  713. MII_BMCR, 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->mdio.bus, phy_dev->mdio.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->mdio.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;
  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. memcpy(pdata->mii_bus->irq, pdata->phy_irq, sizeof(pdata->mii_bus));
  888. pdata->mii_bus->parent = &pdev->dev;
  889. switch (pdata->idrev & 0xFFFF0000) {
  890. case 0x01170000:
  891. case 0x01150000:
  892. case 0x117A0000:
  893. case 0x115A0000:
  894. /* External PHY supported, try to autodetect */
  895. smsc911x_phy_initialise_external(pdata);
  896. break;
  897. default:
  898. SMSC_TRACE(pdata, hw, "External PHY is not supported, "
  899. "using internal PHY");
  900. pdata->using_extphy = 0;
  901. break;
  902. }
  903. if (!pdata->using_extphy) {
  904. /* Mask all PHYs except ID 1 (internal) */
  905. pdata->mii_bus->phy_mask = ~(1 << 1);
  906. }
  907. if (mdiobus_register(pdata->mii_bus)) {
  908. SMSC_WARN(pdata, probe, "Error registering mii bus");
  909. goto err_out_free_bus_2;
  910. }
  911. if (smsc911x_mii_probe(dev) < 0) {
  912. SMSC_WARN(pdata, probe, "Error registering mii bus");
  913. goto err_out_unregister_bus_3;
  914. }
  915. return 0;
  916. err_out_unregister_bus_3:
  917. mdiobus_unregister(pdata->mii_bus);
  918. err_out_free_bus_2:
  919. mdiobus_free(pdata->mii_bus);
  920. err_out_1:
  921. return err;
  922. }
  923. /* Gets the number of tx statuses in the fifo */
  924. static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
  925. {
  926. return (smsc911x_reg_read(pdata, TX_FIFO_INF)
  927. & TX_FIFO_INF_TSUSED_) >> 16;
  928. }
  929. /* Reads tx statuses and increments counters where necessary */
  930. static void smsc911x_tx_update_txcounters(struct net_device *dev)
  931. {
  932. struct smsc911x_data *pdata = netdev_priv(dev);
  933. unsigned int tx_stat;
  934. while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
  935. if (unlikely(tx_stat & 0x80000000)) {
  936. /* In this driver the packet tag is used as the packet
  937. * length. Since a packet length can never reach the
  938. * size of 0x8000, this bit is reserved. It is worth
  939. * noting that the "reserved bit" in the warning above
  940. * does not reference a hardware defined reserved bit
  941. * but rather a driver defined one.
  942. */
  943. SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
  944. } else {
  945. if (unlikely(tx_stat & TX_STS_ES_)) {
  946. dev->stats.tx_errors++;
  947. } else {
  948. dev->stats.tx_packets++;
  949. dev->stats.tx_bytes += (tx_stat >> 16);
  950. }
  951. if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
  952. dev->stats.collisions += 16;
  953. dev->stats.tx_aborted_errors += 1;
  954. } else {
  955. dev->stats.collisions +=
  956. ((tx_stat >> 3) & 0xF);
  957. }
  958. if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
  959. dev->stats.tx_carrier_errors += 1;
  960. if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
  961. dev->stats.collisions++;
  962. dev->stats.tx_aborted_errors++;
  963. }
  964. }
  965. }
  966. }
  967. /* Increments the Rx error counters */
  968. static void
  969. smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
  970. {
  971. int crc_err = 0;
  972. if (unlikely(rxstat & RX_STS_ES_)) {
  973. dev->stats.rx_errors++;
  974. if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
  975. dev->stats.rx_crc_errors++;
  976. crc_err = 1;
  977. }
  978. }
  979. if (likely(!crc_err)) {
  980. if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
  981. (rxstat & RX_STS_LENGTH_ERR_)))
  982. dev->stats.rx_length_errors++;
  983. if (rxstat & RX_STS_MCAST_)
  984. dev->stats.multicast++;
  985. }
  986. }
  987. /* Quickly dumps bad packets */
  988. static void
  989. smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktwords)
  990. {
  991. if (likely(pktwords >= 4)) {
  992. unsigned int timeout = 500;
  993. unsigned int val;
  994. smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
  995. do {
  996. udelay(1);
  997. val = smsc911x_reg_read(pdata, RX_DP_CTRL);
  998. } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
  999. if (unlikely(timeout == 0))
  1000. SMSC_WARN(pdata, hw, "Timed out waiting for "
  1001. "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
  1002. } else {
  1003. unsigned int temp;
  1004. while (pktwords--)
  1005. temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
  1006. }
  1007. }
  1008. /* NAPI poll function */
  1009. static int smsc911x_poll(struct napi_struct *napi, int budget)
  1010. {
  1011. struct smsc911x_data *pdata =
  1012. container_of(napi, struct smsc911x_data, napi);
  1013. struct net_device *dev = pdata->dev;
  1014. int npackets = 0;
  1015. while (npackets < budget) {
  1016. unsigned int pktlength;
  1017. unsigned int pktwords;
  1018. struct sk_buff *skb;
  1019. unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
  1020. if (!rxstat) {
  1021. unsigned int temp;
  1022. /* We processed all packets available. Tell NAPI it can
  1023. * stop polling then re-enable rx interrupts */
  1024. smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
  1025. napi_complete(napi);
  1026. temp = smsc911x_reg_read(pdata, INT_EN);
  1027. temp |= INT_EN_RSFL_EN_;
  1028. smsc911x_reg_write(pdata, INT_EN, temp);
  1029. break;
  1030. }
  1031. /* Count packet for NAPI scheduling, even if it has an error.
  1032. * Error packets still require cycles to discard */
  1033. npackets++;
  1034. pktlength = ((rxstat & 0x3FFF0000) >> 16);
  1035. pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
  1036. smsc911x_rx_counterrors(dev, rxstat);
  1037. if (unlikely(rxstat & RX_STS_ES_)) {
  1038. SMSC_WARN(pdata, rx_err,
  1039. "Discarding packet with error bit set");
  1040. /* Packet has an error, discard it and continue with
  1041. * the next */
  1042. smsc911x_rx_fastforward(pdata, pktwords);
  1043. dev->stats.rx_dropped++;
  1044. continue;
  1045. }
  1046. skb = netdev_alloc_skb(dev, pktwords << 2);
  1047. if (unlikely(!skb)) {
  1048. SMSC_WARN(pdata, rx_err,
  1049. "Unable to allocate skb for rx packet");
  1050. /* Drop the packet and stop this polling iteration */
  1051. smsc911x_rx_fastforward(pdata, pktwords);
  1052. dev->stats.rx_dropped++;
  1053. break;
  1054. }
  1055. pdata->ops->rx_readfifo(pdata,
  1056. (unsigned int *)skb->data, pktwords);
  1057. /* Align IP on 16B boundary */
  1058. skb_reserve(skb, NET_IP_ALIGN);
  1059. skb_put(skb, pktlength - 4);
  1060. skb->protocol = eth_type_trans(skb, dev);
  1061. skb_checksum_none_assert(skb);
  1062. netif_receive_skb(skb);
  1063. /* Update counters */
  1064. dev->stats.rx_packets++;
  1065. dev->stats.rx_bytes += (pktlength - 4);
  1066. }
  1067. /* Return total received packets */
  1068. return npackets;
  1069. }
  1070. /* Returns hash bit number for given MAC address
  1071. * Example:
  1072. * 01 00 5E 00 00 01 -> returns bit number 31 */
  1073. static unsigned int smsc911x_hash(char addr[ETH_ALEN])
  1074. {
  1075. return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
  1076. }
  1077. static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
  1078. {
  1079. /* Performs the multicast & mac_cr update. This is called when
  1080. * safe on the current hardware, and with the mac_lock held */
  1081. unsigned int mac_cr;
  1082. SMSC_ASSERT_MAC_LOCK(pdata);
  1083. mac_cr = smsc911x_mac_read(pdata, MAC_CR);
  1084. mac_cr |= pdata->set_bits_mask;
  1085. mac_cr &= ~(pdata->clear_bits_mask);
  1086. smsc911x_mac_write(pdata, MAC_CR, mac_cr);
  1087. smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
  1088. smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
  1089. SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
  1090. mac_cr, pdata->hashhi, pdata->hashlo);
  1091. }
  1092. static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
  1093. {
  1094. unsigned int mac_cr;
  1095. /* This function is only called for older LAN911x devices
  1096. * (revA or revB), where MAC_CR, HASHH and HASHL should not
  1097. * be modified during Rx - newer devices immediately update the
  1098. * registers.
  1099. *
  1100. * This is called from interrupt context */
  1101. spin_lock(&pdata->mac_lock);
  1102. /* Check Rx has stopped */
  1103. if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
  1104. SMSC_WARN(pdata, drv, "Rx not stopped");
  1105. /* Perform the update - safe to do now Rx has stopped */
  1106. smsc911x_rx_multicast_update(pdata);
  1107. /* Re-enable Rx */
  1108. mac_cr = smsc911x_mac_read(pdata, MAC_CR);
  1109. mac_cr |= MAC_CR_RXEN_;
  1110. smsc911x_mac_write(pdata, MAC_CR, mac_cr);
  1111. pdata->multicast_update_pending = 0;
  1112. spin_unlock(&pdata->mac_lock);
  1113. }
  1114. static int smsc911x_phy_general_power_up(struct smsc911x_data *pdata)
  1115. {
  1116. int rc = 0;
  1117. if (!pdata->phy_dev)
  1118. return rc;
  1119. /* If the internal PHY is in General Power-Down mode, all, except the
  1120. * management interface, is powered-down and stays in that condition as
  1121. * long as Phy register bit 0.11 is HIGH.
  1122. *
  1123. * In that case, clear the bit 0.11, so the PHY powers up and we can
  1124. * access to the phy registers.
  1125. */
  1126. rc = phy_read(pdata->phy_dev, MII_BMCR);
  1127. if (rc < 0) {
  1128. SMSC_WARN(pdata, drv, "Failed reading PHY control reg");
  1129. return rc;
  1130. }
  1131. /* If the PHY general power-down bit is not set is not necessary to
  1132. * disable the general power down-mode.
  1133. */
  1134. if (rc & BMCR_PDOWN) {
  1135. rc = phy_write(pdata->phy_dev, MII_BMCR, rc & ~BMCR_PDOWN);
  1136. if (rc < 0) {
  1137. SMSC_WARN(pdata, drv, "Failed writing PHY control reg");
  1138. return rc;
  1139. }
  1140. usleep_range(1000, 1500);
  1141. }
  1142. return 0;
  1143. }
  1144. static int smsc911x_phy_disable_energy_detect(struct smsc911x_data *pdata)
  1145. {
  1146. int rc = 0;
  1147. if (!pdata->phy_dev)
  1148. return rc;
  1149. rc = phy_read(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS);
  1150. if (rc < 0) {
  1151. SMSC_WARN(pdata, drv, "Failed reading PHY control reg");
  1152. return rc;
  1153. }
  1154. /* Only disable if energy detect mode is already enabled */
  1155. if (rc & MII_LAN83C185_EDPWRDOWN) {
  1156. /* Disable energy detect mode for this SMSC Transceivers */
  1157. rc = phy_write(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS,
  1158. rc & (~MII_LAN83C185_EDPWRDOWN));
  1159. if (rc < 0) {
  1160. SMSC_WARN(pdata, drv, "Failed writing PHY control reg");
  1161. return rc;
  1162. }
  1163. /* Allow PHY to wakeup */
  1164. mdelay(2);
  1165. }
  1166. return 0;
  1167. }
  1168. static int smsc911x_phy_enable_energy_detect(struct smsc911x_data *pdata)
  1169. {
  1170. int rc = 0;
  1171. if (!pdata->phy_dev)
  1172. return rc;
  1173. rc = phy_read(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS);
  1174. if (rc < 0) {
  1175. SMSC_WARN(pdata, drv, "Failed reading PHY control reg");
  1176. return rc;
  1177. }
  1178. /* Only enable if energy detect mode is already disabled */
  1179. if (!(rc & MII_LAN83C185_EDPWRDOWN)) {
  1180. /* Enable energy detect mode for this SMSC Transceivers */
  1181. rc = phy_write(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS,
  1182. rc | MII_LAN83C185_EDPWRDOWN);
  1183. if (rc < 0) {
  1184. SMSC_WARN(pdata, drv, "Failed writing PHY control reg");
  1185. return rc;
  1186. }
  1187. }
  1188. return 0;
  1189. }
  1190. static int smsc911x_soft_reset(struct smsc911x_data *pdata)
  1191. {
  1192. unsigned int timeout;
  1193. unsigned int temp;
  1194. int ret;
  1195. /*
  1196. * Make sure to power-up the PHY chip before doing a reset, otherwise
  1197. * the reset fails.
  1198. */
  1199. ret = smsc911x_phy_general_power_up(pdata);
  1200. if (ret) {
  1201. SMSC_WARN(pdata, drv, "Failed to power-up the PHY chip");
  1202. return ret;
  1203. }
  1204. /*
  1205. * LAN9210/LAN9211/LAN9220/LAN9221 chips have an internal PHY that
  1206. * are initialized in a Energy Detect Power-Down mode that prevents
  1207. * the MAC chip to be software reseted. So we have to wakeup the PHY
  1208. * before.
  1209. */
  1210. if (pdata->generation == 4) {
  1211. ret = smsc911x_phy_disable_energy_detect(pdata);
  1212. if (ret) {
  1213. SMSC_WARN(pdata, drv, "Failed to wakeup the PHY chip");
  1214. return ret;
  1215. }
  1216. }
  1217. /* Reset the LAN911x */
  1218. smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
  1219. timeout = 10;
  1220. do {
  1221. udelay(10);
  1222. temp = smsc911x_reg_read(pdata, HW_CFG);
  1223. } while ((--timeout) && (temp & HW_CFG_SRST_));
  1224. if (unlikely(temp & HW_CFG_SRST_)) {
  1225. SMSC_WARN(pdata, drv, "Failed to complete reset");
  1226. return -EIO;
  1227. }
  1228. if (pdata->generation == 4) {
  1229. ret = smsc911x_phy_enable_energy_detect(pdata);
  1230. if (ret) {
  1231. SMSC_WARN(pdata, drv, "Failed to wakeup the PHY chip");
  1232. return ret;
  1233. }
  1234. }
  1235. return 0;
  1236. }
  1237. /* Sets the device MAC address to dev_addr, called with mac_lock held */
  1238. static void
  1239. smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
  1240. {
  1241. u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
  1242. u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
  1243. (dev_addr[1] << 8) | dev_addr[0];
  1244. SMSC_ASSERT_MAC_LOCK(pdata);
  1245. smsc911x_mac_write(pdata, ADDRH, mac_high16);
  1246. smsc911x_mac_write(pdata, ADDRL, mac_low32);
  1247. }
  1248. static void smsc911x_disable_irq_chip(struct net_device *dev)
  1249. {
  1250. struct smsc911x_data *pdata = netdev_priv(dev);
  1251. smsc911x_reg_write(pdata, INT_EN, 0);
  1252. smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
  1253. }
  1254. static int smsc911x_open(struct net_device *dev)
  1255. {
  1256. struct smsc911x_data *pdata = netdev_priv(dev);
  1257. unsigned int timeout;
  1258. unsigned int temp;
  1259. unsigned int intcfg;
  1260. /* if the phy is not yet registered, retry later*/
  1261. if (!pdata->phy_dev) {
  1262. SMSC_WARN(pdata, hw, "phy_dev is NULL");
  1263. return -EAGAIN;
  1264. }
  1265. /* Reset the LAN911x */
  1266. if (smsc911x_soft_reset(pdata)) {
  1267. SMSC_WARN(pdata, hw, "soft reset failed");
  1268. return -EIO;
  1269. }
  1270. smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
  1271. smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
  1272. /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
  1273. spin_lock_irq(&pdata->mac_lock);
  1274. smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
  1275. spin_unlock_irq(&pdata->mac_lock);
  1276. /* Make sure EEPROM has finished loading before setting GPIO_CFG */
  1277. timeout = 50;
  1278. while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
  1279. --timeout) {
  1280. udelay(10);
  1281. }
  1282. if (unlikely(timeout == 0))
  1283. SMSC_WARN(pdata, ifup,
  1284. "Timed out waiting for EEPROM busy bit to clear");
  1285. smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
  1286. /* The soft reset above cleared the device's MAC address,
  1287. * restore it from local copy (set in probe) */
  1288. spin_lock_irq(&pdata->mac_lock);
  1289. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1290. spin_unlock_irq(&pdata->mac_lock);
  1291. /* Initialise irqs, but leave all sources disabled */
  1292. smsc911x_disable_irq_chip(dev);
  1293. /* Set interrupt deassertion to 100uS */
  1294. intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
  1295. if (pdata->config.irq_polarity) {
  1296. SMSC_TRACE(pdata, ifup, "irq polarity: active high");
  1297. intcfg |= INT_CFG_IRQ_POL_;
  1298. } else {
  1299. SMSC_TRACE(pdata, ifup, "irq polarity: active low");
  1300. }
  1301. if (pdata->config.irq_type) {
  1302. SMSC_TRACE(pdata, ifup, "irq type: push-pull");
  1303. intcfg |= INT_CFG_IRQ_TYPE_;
  1304. } else {
  1305. SMSC_TRACE(pdata, ifup, "irq type: open drain");
  1306. }
  1307. smsc911x_reg_write(pdata, INT_CFG, intcfg);
  1308. SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
  1309. pdata->software_irq_signal = 0;
  1310. smp_wmb();
  1311. temp = smsc911x_reg_read(pdata, INT_EN);
  1312. temp |= INT_EN_SW_INT_EN_;
  1313. smsc911x_reg_write(pdata, INT_EN, temp);
  1314. timeout = 1000;
  1315. while (timeout--) {
  1316. if (pdata->software_irq_signal)
  1317. break;
  1318. msleep(1);
  1319. }
  1320. if (!pdata->software_irq_signal) {
  1321. netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
  1322. dev->irq);
  1323. return -ENODEV;
  1324. }
  1325. SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
  1326. dev->irq);
  1327. netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
  1328. (unsigned long)pdata->ioaddr, dev->irq);
  1329. /* Reset the last known duplex and carrier */
  1330. pdata->last_duplex = -1;
  1331. pdata->last_carrier = -1;
  1332. /* Bring the PHY up */
  1333. phy_start(pdata->phy_dev);
  1334. temp = smsc911x_reg_read(pdata, HW_CFG);
  1335. /* Preserve TX FIFO size and external PHY configuration */
  1336. temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
  1337. temp |= HW_CFG_SF_;
  1338. smsc911x_reg_write(pdata, HW_CFG, temp);
  1339. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1340. temp |= FIFO_INT_TX_AVAIL_LEVEL_;
  1341. temp &= ~(FIFO_INT_RX_STS_LEVEL_);
  1342. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1343. /* set RX Data offset to 2 bytes for alignment */
  1344. smsc911x_reg_write(pdata, RX_CFG, (NET_IP_ALIGN << 8));
  1345. /* enable NAPI polling before enabling RX interrupts */
  1346. napi_enable(&pdata->napi);
  1347. temp = smsc911x_reg_read(pdata, INT_EN);
  1348. temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
  1349. smsc911x_reg_write(pdata, INT_EN, temp);
  1350. spin_lock_irq(&pdata->mac_lock);
  1351. temp = smsc911x_mac_read(pdata, MAC_CR);
  1352. temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
  1353. smsc911x_mac_write(pdata, MAC_CR, temp);
  1354. spin_unlock_irq(&pdata->mac_lock);
  1355. smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
  1356. netif_start_queue(dev);
  1357. return 0;
  1358. }
  1359. /* Entry point for stopping the interface */
  1360. static int smsc911x_stop(struct net_device *dev)
  1361. {
  1362. struct smsc911x_data *pdata = netdev_priv(dev);
  1363. unsigned int temp;
  1364. /* Disable all device interrupts */
  1365. temp = smsc911x_reg_read(pdata, INT_CFG);
  1366. temp &= ~INT_CFG_IRQ_EN_;
  1367. smsc911x_reg_write(pdata, INT_CFG, temp);
  1368. /* Stop Tx and Rx polling */
  1369. netif_stop_queue(dev);
  1370. napi_disable(&pdata->napi);
  1371. /* At this point all Rx and Tx activity is stopped */
  1372. dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
  1373. smsc911x_tx_update_txcounters(dev);
  1374. /* Bring the PHY down */
  1375. if (pdata->phy_dev)
  1376. phy_stop(pdata->phy_dev);
  1377. SMSC_TRACE(pdata, ifdown, "Interface stopped");
  1378. return 0;
  1379. }
  1380. /* Entry point for transmitting a packet */
  1381. static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1382. {
  1383. struct smsc911x_data *pdata = netdev_priv(dev);
  1384. unsigned int freespace;
  1385. unsigned int tx_cmd_a;
  1386. unsigned int tx_cmd_b;
  1387. unsigned int temp;
  1388. u32 wrsz;
  1389. ulong bufp;
  1390. freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
  1391. if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
  1392. SMSC_WARN(pdata, tx_err,
  1393. "Tx data fifo low, space available: %d", freespace);
  1394. /* Word alignment adjustment */
  1395. tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
  1396. tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
  1397. tx_cmd_a |= (unsigned int)skb->len;
  1398. tx_cmd_b = ((unsigned int)skb->len) << 16;
  1399. tx_cmd_b |= (unsigned int)skb->len;
  1400. smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
  1401. smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
  1402. bufp = (ulong)skb->data & (~0x3);
  1403. wrsz = (u32)skb->len + 3;
  1404. wrsz += (u32)((ulong)skb->data & 0x3);
  1405. wrsz >>= 2;
  1406. pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
  1407. freespace -= (skb->len + 32);
  1408. skb_tx_timestamp(skb);
  1409. dev_consume_skb_any(skb);
  1410. if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
  1411. smsc911x_tx_update_txcounters(dev);
  1412. if (freespace < TX_FIFO_LOW_THRESHOLD) {
  1413. netif_stop_queue(dev);
  1414. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1415. temp &= 0x00FFFFFF;
  1416. temp |= 0x32000000;
  1417. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1418. }
  1419. return NETDEV_TX_OK;
  1420. }
  1421. /* Entry point for getting status counters */
  1422. static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
  1423. {
  1424. struct smsc911x_data *pdata = netdev_priv(dev);
  1425. smsc911x_tx_update_txcounters(dev);
  1426. dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
  1427. return &dev->stats;
  1428. }
  1429. /* Entry point for setting addressing modes */
  1430. static void smsc911x_set_multicast_list(struct net_device *dev)
  1431. {
  1432. struct smsc911x_data *pdata = netdev_priv(dev);
  1433. unsigned long flags;
  1434. if (dev->flags & IFF_PROMISC) {
  1435. /* Enabling promiscuous mode */
  1436. pdata->set_bits_mask = MAC_CR_PRMS_;
  1437. pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
  1438. pdata->hashhi = 0;
  1439. pdata->hashlo = 0;
  1440. } else if (dev->flags & IFF_ALLMULTI) {
  1441. /* Enabling all multicast mode */
  1442. pdata->set_bits_mask = MAC_CR_MCPAS_;
  1443. pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
  1444. pdata->hashhi = 0;
  1445. pdata->hashlo = 0;
  1446. } else if (!netdev_mc_empty(dev)) {
  1447. /* Enabling specific multicast addresses */
  1448. unsigned int hash_high = 0;
  1449. unsigned int hash_low = 0;
  1450. struct netdev_hw_addr *ha;
  1451. pdata->set_bits_mask = MAC_CR_HPFILT_;
  1452. pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
  1453. netdev_for_each_mc_addr(ha, dev) {
  1454. unsigned int bitnum = smsc911x_hash(ha->addr);
  1455. unsigned int mask = 0x01 << (bitnum & 0x1F);
  1456. if (bitnum & 0x20)
  1457. hash_high |= mask;
  1458. else
  1459. hash_low |= mask;
  1460. }
  1461. pdata->hashhi = hash_high;
  1462. pdata->hashlo = hash_low;
  1463. } else {
  1464. /* Enabling local MAC address only */
  1465. pdata->set_bits_mask = 0;
  1466. pdata->clear_bits_mask =
  1467. (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
  1468. pdata->hashhi = 0;
  1469. pdata->hashlo = 0;
  1470. }
  1471. spin_lock_irqsave(&pdata->mac_lock, flags);
  1472. if (pdata->generation <= 1) {
  1473. /* Older hardware revision - cannot change these flags while
  1474. * receiving data */
  1475. if (!pdata->multicast_update_pending) {
  1476. unsigned int temp;
  1477. SMSC_TRACE(pdata, hw, "scheduling mcast update");
  1478. pdata->multicast_update_pending = 1;
  1479. /* Request the hardware to stop, then perform the
  1480. * update when we get an RX_STOP interrupt */
  1481. temp = smsc911x_mac_read(pdata, MAC_CR);
  1482. temp &= ~(MAC_CR_RXEN_);
  1483. smsc911x_mac_write(pdata, MAC_CR, temp);
  1484. } else {
  1485. /* There is another update pending, this should now
  1486. * use the newer values */
  1487. }
  1488. } else {
  1489. /* Newer hardware revision - can write immediately */
  1490. smsc911x_rx_multicast_update(pdata);
  1491. }
  1492. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  1493. }
  1494. static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
  1495. {
  1496. struct net_device *dev = dev_id;
  1497. struct smsc911x_data *pdata = netdev_priv(dev);
  1498. u32 intsts = smsc911x_reg_read(pdata, INT_STS);
  1499. u32 inten = smsc911x_reg_read(pdata, INT_EN);
  1500. int serviced = IRQ_NONE;
  1501. u32 temp;
  1502. if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
  1503. temp = smsc911x_reg_read(pdata, INT_EN);
  1504. temp &= (~INT_EN_SW_INT_EN_);
  1505. smsc911x_reg_write(pdata, INT_EN, temp);
  1506. smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
  1507. pdata->software_irq_signal = 1;
  1508. smp_wmb();
  1509. serviced = IRQ_HANDLED;
  1510. }
  1511. if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
  1512. /* Called when there is a multicast update scheduled and
  1513. * it is now safe to complete the update */
  1514. SMSC_TRACE(pdata, intr, "RX Stop interrupt");
  1515. smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
  1516. if (pdata->multicast_update_pending)
  1517. smsc911x_rx_multicast_update_workaround(pdata);
  1518. serviced = IRQ_HANDLED;
  1519. }
  1520. if (intsts & inten & INT_STS_TDFA_) {
  1521. temp = smsc911x_reg_read(pdata, FIFO_INT);
  1522. temp |= FIFO_INT_TX_AVAIL_LEVEL_;
  1523. smsc911x_reg_write(pdata, FIFO_INT, temp);
  1524. smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
  1525. netif_wake_queue(dev);
  1526. serviced = IRQ_HANDLED;
  1527. }
  1528. if (unlikely(intsts & inten & INT_STS_RXE_)) {
  1529. SMSC_TRACE(pdata, intr, "RX Error interrupt");
  1530. smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
  1531. serviced = IRQ_HANDLED;
  1532. }
  1533. if (likely(intsts & inten & INT_STS_RSFL_)) {
  1534. if (likely(napi_schedule_prep(&pdata->napi))) {
  1535. /* Disable Rx interrupts */
  1536. temp = smsc911x_reg_read(pdata, INT_EN);
  1537. temp &= (~INT_EN_RSFL_EN_);
  1538. smsc911x_reg_write(pdata, INT_EN, temp);
  1539. /* Schedule a NAPI poll */
  1540. __napi_schedule(&pdata->napi);
  1541. } else {
  1542. SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
  1543. }
  1544. serviced = IRQ_HANDLED;
  1545. }
  1546. return serviced;
  1547. }
  1548. #ifdef CONFIG_NET_POLL_CONTROLLER
  1549. static void smsc911x_poll_controller(struct net_device *dev)
  1550. {
  1551. disable_irq(dev->irq);
  1552. smsc911x_irqhandler(0, dev);
  1553. enable_irq(dev->irq);
  1554. }
  1555. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1556. static int smsc911x_set_mac_address(struct net_device *dev, void *p)
  1557. {
  1558. struct smsc911x_data *pdata = netdev_priv(dev);
  1559. struct sockaddr *addr = p;
  1560. /* On older hardware revisions we cannot change the mac address
  1561. * registers while receiving data. Newer devices can safely change
  1562. * this at any time. */
  1563. if (pdata->generation <= 1 && netif_running(dev))
  1564. return -EBUSY;
  1565. if (!is_valid_ether_addr(addr->sa_data))
  1566. return -EADDRNOTAVAIL;
  1567. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  1568. spin_lock_irq(&pdata->mac_lock);
  1569. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  1570. spin_unlock_irq(&pdata->mac_lock);
  1571. netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
  1572. return 0;
  1573. }
  1574. /* Standard ioctls for mii-tool */
  1575. static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1576. {
  1577. struct smsc911x_data *pdata = netdev_priv(dev);
  1578. if (!netif_running(dev) || !pdata->phy_dev)
  1579. return -EINVAL;
  1580. return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
  1581. }
  1582. static int
  1583. smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1584. {
  1585. struct smsc911x_data *pdata = netdev_priv(dev);
  1586. cmd->maxtxpkt = 1;
  1587. cmd->maxrxpkt = 1;
  1588. return phy_ethtool_gset(pdata->phy_dev, cmd);
  1589. }
  1590. static int
  1591. smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  1592. {
  1593. struct smsc911x_data *pdata = netdev_priv(dev);
  1594. return phy_ethtool_sset(pdata->phy_dev, cmd);
  1595. }
  1596. static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
  1597. struct ethtool_drvinfo *info)
  1598. {
  1599. strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
  1600. strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
  1601. strlcpy(info->bus_info, dev_name(dev->dev.parent),
  1602. sizeof(info->bus_info));
  1603. }
  1604. static int smsc911x_ethtool_nwayreset(struct net_device *dev)
  1605. {
  1606. struct smsc911x_data *pdata = netdev_priv(dev);
  1607. return phy_start_aneg(pdata->phy_dev);
  1608. }
  1609. static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
  1610. {
  1611. struct smsc911x_data *pdata = netdev_priv(dev);
  1612. return pdata->msg_enable;
  1613. }
  1614. static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
  1615. {
  1616. struct smsc911x_data *pdata = netdev_priv(dev);
  1617. pdata->msg_enable = level;
  1618. }
  1619. static int smsc911x_ethtool_getregslen(struct net_device *dev)
  1620. {
  1621. return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
  1622. sizeof(u32);
  1623. }
  1624. static void
  1625. smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
  1626. void *buf)
  1627. {
  1628. struct smsc911x_data *pdata = netdev_priv(dev);
  1629. struct phy_device *phy_dev = pdata->phy_dev;
  1630. unsigned long flags;
  1631. unsigned int i;
  1632. unsigned int j = 0;
  1633. u32 *data = buf;
  1634. regs->version = pdata->idrev;
  1635. for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
  1636. data[j++] = smsc911x_reg_read(pdata, i);
  1637. for (i = MAC_CR; i <= WUCSR; i++) {
  1638. spin_lock_irqsave(&pdata->mac_lock, flags);
  1639. data[j++] = smsc911x_mac_read(pdata, i);
  1640. spin_unlock_irqrestore(&pdata->mac_lock, flags);
  1641. }
  1642. for (i = 0; i <= 31; i++)
  1643. data[j++] = smsc911x_mii_read(phy_dev->mdio.bus,
  1644. phy_dev->mdio.addr, i);
  1645. }
  1646. static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
  1647. {
  1648. unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
  1649. temp &= ~GPIO_CFG_EEPR_EN_;
  1650. smsc911x_reg_write(pdata, GPIO_CFG, temp);
  1651. msleep(1);
  1652. }
  1653. static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
  1654. {
  1655. int timeout = 100;
  1656. u32 e2cmd;
  1657. SMSC_TRACE(pdata, drv, "op 0x%08x", op);
  1658. if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  1659. SMSC_WARN(pdata, drv, "Busy at start");
  1660. return -EBUSY;
  1661. }
  1662. e2cmd = op | E2P_CMD_EPC_BUSY_;
  1663. smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
  1664. do {
  1665. msleep(1);
  1666. e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
  1667. } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
  1668. if (!timeout) {
  1669. SMSC_TRACE(pdata, drv, "TIMED OUT");
  1670. return -EAGAIN;
  1671. }
  1672. if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
  1673. SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
  1674. return -EINVAL;
  1675. }
  1676. return 0;
  1677. }
  1678. static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
  1679. u8 address, u8 *data)
  1680. {
  1681. u32 op = E2P_CMD_EPC_CMD_READ_ | address;
  1682. int ret;
  1683. SMSC_TRACE(pdata, drv, "address 0x%x", address);
  1684. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1685. if (!ret)
  1686. data[address] = smsc911x_reg_read(pdata, E2P_DATA);
  1687. return ret;
  1688. }
  1689. static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
  1690. u8 address, u8 data)
  1691. {
  1692. u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
  1693. u32 temp;
  1694. int ret;
  1695. SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
  1696. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1697. if (!ret) {
  1698. op = E2P_CMD_EPC_CMD_WRITE_ | address;
  1699. smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
  1700. /* Workaround for hardware read-after-write restriction */
  1701. temp = smsc911x_reg_read(pdata, BYTE_TEST);
  1702. ret = smsc911x_eeprom_send_cmd(pdata, op);
  1703. }
  1704. return ret;
  1705. }
  1706. static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
  1707. {
  1708. return SMSC911X_EEPROM_SIZE;
  1709. }
  1710. static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
  1711. struct ethtool_eeprom *eeprom, u8 *data)
  1712. {
  1713. struct smsc911x_data *pdata = netdev_priv(dev);
  1714. u8 eeprom_data[SMSC911X_EEPROM_SIZE];
  1715. int len;
  1716. int i;
  1717. smsc911x_eeprom_enable_access(pdata);
  1718. len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
  1719. for (i = 0; i < len; i++) {
  1720. int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
  1721. if (ret < 0) {
  1722. eeprom->len = 0;
  1723. return ret;
  1724. }
  1725. }
  1726. memcpy(data, &eeprom_data[eeprom->offset], len);
  1727. eeprom->len = len;
  1728. return 0;
  1729. }
  1730. static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
  1731. struct ethtool_eeprom *eeprom, u8 *data)
  1732. {
  1733. int ret;
  1734. struct smsc911x_data *pdata = netdev_priv(dev);
  1735. smsc911x_eeprom_enable_access(pdata);
  1736. smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
  1737. ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
  1738. smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
  1739. /* Single byte write, according to man page */
  1740. eeprom->len = 1;
  1741. return ret;
  1742. }
  1743. static const struct ethtool_ops smsc911x_ethtool_ops = {
  1744. .get_settings = smsc911x_ethtool_getsettings,
  1745. .set_settings = smsc911x_ethtool_setsettings,
  1746. .get_link = ethtool_op_get_link,
  1747. .get_drvinfo = smsc911x_ethtool_getdrvinfo,
  1748. .nway_reset = smsc911x_ethtool_nwayreset,
  1749. .get_msglevel = smsc911x_ethtool_getmsglevel,
  1750. .set_msglevel = smsc911x_ethtool_setmsglevel,
  1751. .get_regs_len = smsc911x_ethtool_getregslen,
  1752. .get_regs = smsc911x_ethtool_getregs,
  1753. .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
  1754. .get_eeprom = smsc911x_ethtool_get_eeprom,
  1755. .set_eeprom = smsc911x_ethtool_set_eeprom,
  1756. .get_ts_info = ethtool_op_get_ts_info,
  1757. };
  1758. static const struct net_device_ops smsc911x_netdev_ops = {
  1759. .ndo_open = smsc911x_open,
  1760. .ndo_stop = smsc911x_stop,
  1761. .ndo_start_xmit = smsc911x_hard_start_xmit,
  1762. .ndo_get_stats = smsc911x_get_stats,
  1763. .ndo_set_rx_mode = smsc911x_set_multicast_list,
  1764. .ndo_do_ioctl = smsc911x_do_ioctl,
  1765. .ndo_change_mtu = eth_change_mtu,
  1766. .ndo_validate_addr = eth_validate_addr,
  1767. .ndo_set_mac_address = smsc911x_set_mac_address,
  1768. #ifdef CONFIG_NET_POLL_CONTROLLER
  1769. .ndo_poll_controller = smsc911x_poll_controller,
  1770. #endif
  1771. };
  1772. /* copies the current mac address from hardware to dev->dev_addr */
  1773. static void smsc911x_read_mac_address(struct net_device *dev)
  1774. {
  1775. struct smsc911x_data *pdata = netdev_priv(dev);
  1776. u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
  1777. u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
  1778. dev->dev_addr[0] = (u8)(mac_low32);
  1779. dev->dev_addr[1] = (u8)(mac_low32 >> 8);
  1780. dev->dev_addr[2] = (u8)(mac_low32 >> 16);
  1781. dev->dev_addr[3] = (u8)(mac_low32 >> 24);
  1782. dev->dev_addr[4] = (u8)(mac_high16);
  1783. dev->dev_addr[5] = (u8)(mac_high16 >> 8);
  1784. }
  1785. /* Initializing private device structures, only called from probe */
  1786. static int smsc911x_init(struct net_device *dev)
  1787. {
  1788. struct smsc911x_data *pdata = netdev_priv(dev);
  1789. unsigned int byte_test, mask;
  1790. unsigned int to = 100;
  1791. SMSC_TRACE(pdata, probe, "Driver Parameters:");
  1792. SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
  1793. (unsigned long)pdata->ioaddr);
  1794. SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
  1795. SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
  1796. spin_lock_init(&pdata->dev_lock);
  1797. spin_lock_init(&pdata->mac_lock);
  1798. if (pdata->ioaddr == NULL) {
  1799. SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
  1800. return -ENODEV;
  1801. }
  1802. /*
  1803. * poll the READY bit in PMT_CTRL. Any other access to the device is
  1804. * forbidden while this bit isn't set. Try for 100ms
  1805. *
  1806. * Note that this test is done before the WORD_SWAP register is
  1807. * programmed. So in some configurations the READY bit is at 16 before
  1808. * WORD_SWAP is written to. This issue is worked around by waiting
  1809. * until either bit 0 or bit 16 gets set in PMT_CTRL.
  1810. *
  1811. * SMSC has confirmed that checking bit 16 (marked as reserved in
  1812. * the datasheet) is fine since these bits "will either never be set
  1813. * or can only go high after READY does (so also indicate the device
  1814. * is ready)".
  1815. */
  1816. mask = PMT_CTRL_READY_ | swahw32(PMT_CTRL_READY_);
  1817. while (!(smsc911x_reg_read(pdata, PMT_CTRL) & mask) && --to)
  1818. udelay(1000);
  1819. if (to == 0) {
  1820. netdev_err(dev, "Device not READY in 100ms aborting\n");
  1821. return -ENODEV;
  1822. }
  1823. /* Check byte ordering */
  1824. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1825. SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
  1826. if (byte_test == 0x43218765) {
  1827. SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
  1828. "applying WORD_SWAP");
  1829. smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
  1830. /* 1 dummy read of BYTE_TEST is needed after a write to
  1831. * WORD_SWAP before its contents are valid */
  1832. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1833. byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
  1834. }
  1835. if (byte_test != 0x87654321) {
  1836. SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
  1837. if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
  1838. SMSC_WARN(pdata, probe,
  1839. "top 16 bits equal to bottom 16 bits");
  1840. SMSC_TRACE(pdata, probe,
  1841. "This may mean the chip is set "
  1842. "for 32 bit while the bus is reading 16 bit");
  1843. }
  1844. return -ENODEV;
  1845. }
  1846. /* Default generation to zero (all workarounds apply) */
  1847. pdata->generation = 0;
  1848. pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
  1849. switch (pdata->idrev & 0xFFFF0000) {
  1850. case 0x01180000:
  1851. case 0x01170000:
  1852. case 0x01160000:
  1853. case 0x01150000:
  1854. case 0x218A0000:
  1855. /* LAN911[5678] family */
  1856. pdata->generation = pdata->idrev & 0x0000FFFF;
  1857. break;
  1858. case 0x118A0000:
  1859. case 0x117A0000:
  1860. case 0x116A0000:
  1861. case 0x115A0000:
  1862. /* LAN921[5678] family */
  1863. pdata->generation = 3;
  1864. break;
  1865. case 0x92100000:
  1866. case 0x92110000:
  1867. case 0x92200000:
  1868. case 0x92210000:
  1869. /* LAN9210/LAN9211/LAN9220/LAN9221 */
  1870. pdata->generation = 4;
  1871. break;
  1872. default:
  1873. SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
  1874. pdata->idrev);
  1875. return -ENODEV;
  1876. }
  1877. SMSC_TRACE(pdata, probe,
  1878. "LAN911x identified, idrev: 0x%08X, generation: %d",
  1879. pdata->idrev, pdata->generation);
  1880. if (pdata->generation == 0)
  1881. SMSC_WARN(pdata, probe,
  1882. "This driver is not intended for this chip revision");
  1883. /* workaround for platforms without an eeprom, where the mac address
  1884. * is stored elsewhere and set by the bootloader. This saves the
  1885. * mac address before resetting the device */
  1886. if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
  1887. spin_lock_irq(&pdata->mac_lock);
  1888. smsc911x_read_mac_address(dev);
  1889. spin_unlock_irq(&pdata->mac_lock);
  1890. }
  1891. /* Reset the LAN911x */
  1892. if (smsc911x_phy_reset(pdata) || smsc911x_soft_reset(pdata))
  1893. return -ENODEV;
  1894. dev->flags |= IFF_MULTICAST;
  1895. netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
  1896. dev->netdev_ops = &smsc911x_netdev_ops;
  1897. dev->ethtool_ops = &smsc911x_ethtool_ops;
  1898. return 0;
  1899. }
  1900. static int smsc911x_drv_remove(struct platform_device *pdev)
  1901. {
  1902. struct net_device *dev;
  1903. struct smsc911x_data *pdata;
  1904. struct resource *res;
  1905. dev = platform_get_drvdata(pdev);
  1906. BUG_ON(!dev);
  1907. pdata = netdev_priv(dev);
  1908. BUG_ON(!pdata);
  1909. BUG_ON(!pdata->ioaddr);
  1910. BUG_ON(!pdata->phy_dev);
  1911. SMSC_TRACE(pdata, ifdown, "Stopping driver");
  1912. phy_disconnect(pdata->phy_dev);
  1913. pdata->phy_dev = NULL;
  1914. mdiobus_unregister(pdata->mii_bus);
  1915. mdiobus_free(pdata->mii_bus);
  1916. unregister_netdev(dev);
  1917. free_irq(dev->irq, dev);
  1918. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1919. "smsc911x-memory");
  1920. if (!res)
  1921. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1922. release_mem_region(res->start, resource_size(res));
  1923. iounmap(pdata->ioaddr);
  1924. (void)smsc911x_disable_resources(pdev);
  1925. smsc911x_free_resources(pdev);
  1926. free_netdev(dev);
  1927. pm_runtime_put(&pdev->dev);
  1928. pm_runtime_disable(&pdev->dev);
  1929. return 0;
  1930. }
  1931. /* standard register acces */
  1932. static const struct smsc911x_ops standard_smsc911x_ops = {
  1933. .reg_read = __smsc911x_reg_read,
  1934. .reg_write = __smsc911x_reg_write,
  1935. .rx_readfifo = smsc911x_rx_readfifo,
  1936. .tx_writefifo = smsc911x_tx_writefifo,
  1937. };
  1938. /* shifted register access */
  1939. static const struct smsc911x_ops shifted_smsc911x_ops = {
  1940. .reg_read = __smsc911x_reg_read_shift,
  1941. .reg_write = __smsc911x_reg_write_shift,
  1942. .rx_readfifo = smsc911x_rx_readfifo_shift,
  1943. .tx_writefifo = smsc911x_tx_writefifo_shift,
  1944. };
  1945. static int smsc911x_probe_config(struct smsc911x_platform_config *config,
  1946. struct device *dev)
  1947. {
  1948. int phy_interface;
  1949. u32 width = 0;
  1950. int err;
  1951. phy_interface = device_get_phy_mode(dev);
  1952. if (phy_interface < 0)
  1953. phy_interface = PHY_INTERFACE_MODE_NA;
  1954. config->phy_interface = phy_interface;
  1955. device_get_mac_address(dev, config->mac, ETH_ALEN);
  1956. err = device_property_read_u32(dev, "reg-io-width", &width);
  1957. if (err == -ENXIO)
  1958. return err;
  1959. if (!err && width == 4)
  1960. config->flags |= SMSC911X_USE_32BIT;
  1961. else
  1962. config->flags |= SMSC911X_USE_16BIT;
  1963. device_property_read_u32(dev, "reg-shift", &config->shift);
  1964. if (device_property_present(dev, "smsc,irq-active-high"))
  1965. config->irq_polarity = SMSC911X_IRQ_POLARITY_ACTIVE_HIGH;
  1966. if (device_property_present(dev, "smsc,irq-push-pull"))
  1967. config->irq_type = SMSC911X_IRQ_TYPE_PUSH_PULL;
  1968. if (device_property_present(dev, "smsc,force-internal-phy"))
  1969. config->flags |= SMSC911X_FORCE_INTERNAL_PHY;
  1970. if (device_property_present(dev, "smsc,force-external-phy"))
  1971. config->flags |= SMSC911X_FORCE_EXTERNAL_PHY;
  1972. if (device_property_present(dev, "smsc,save-mac-address"))
  1973. config->flags |= SMSC911X_SAVE_MAC_ADDRESS;
  1974. return 0;
  1975. }
  1976. static int smsc911x_drv_probe(struct platform_device *pdev)
  1977. {
  1978. struct net_device *dev;
  1979. struct smsc911x_data *pdata;
  1980. struct smsc911x_platform_config *config = dev_get_platdata(&pdev->dev);
  1981. struct resource *res;
  1982. unsigned int intcfg = 0;
  1983. int res_size, irq, irq_flags;
  1984. int retval;
  1985. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1986. "smsc911x-memory");
  1987. if (!res)
  1988. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1989. if (!res) {
  1990. pr_warn("Could not allocate resource\n");
  1991. retval = -ENODEV;
  1992. goto out_0;
  1993. }
  1994. res_size = resource_size(res);
  1995. irq = platform_get_irq(pdev, 0);
  1996. if (irq == -EPROBE_DEFER) {
  1997. retval = -EPROBE_DEFER;
  1998. goto out_0;
  1999. } else if (irq <= 0) {
  2000. pr_warn("Could not allocate irq resource\n");
  2001. retval = -ENODEV;
  2002. goto out_0;
  2003. }
  2004. if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
  2005. retval = -EBUSY;
  2006. goto out_0;
  2007. }
  2008. dev = alloc_etherdev(sizeof(struct smsc911x_data));
  2009. if (!dev) {
  2010. retval = -ENOMEM;
  2011. goto out_release_io_1;
  2012. }
  2013. SET_NETDEV_DEV(dev, &pdev->dev);
  2014. pdata = netdev_priv(dev);
  2015. dev->irq = irq;
  2016. irq_flags = irq_get_trigger_type(irq);
  2017. pdata->ioaddr = ioremap_nocache(res->start, res_size);
  2018. pdata->dev = dev;
  2019. pdata->msg_enable = ((1 << debug) - 1);
  2020. platform_set_drvdata(pdev, dev);
  2021. retval = smsc911x_request_resources(pdev);
  2022. if (retval)
  2023. goto out_request_resources_fail;
  2024. retval = smsc911x_enable_resources(pdev);
  2025. if (retval)
  2026. goto out_enable_resources_fail;
  2027. if (pdata->ioaddr == NULL) {
  2028. SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
  2029. retval = -ENOMEM;
  2030. goto out_disable_resources;
  2031. }
  2032. retval = smsc911x_probe_config(&pdata->config, &pdev->dev);
  2033. if (retval && config) {
  2034. /* copy config parameters across to pdata */
  2035. memcpy(&pdata->config, config, sizeof(pdata->config));
  2036. retval = 0;
  2037. }
  2038. if (retval) {
  2039. SMSC_WARN(pdata, probe, "Error smsc911x config not found");
  2040. goto out_disable_resources;
  2041. }
  2042. /* assume standard, non-shifted, access to HW registers */
  2043. pdata->ops = &standard_smsc911x_ops;
  2044. /* apply the right access if shifting is needed */
  2045. if (pdata->config.shift)
  2046. pdata->ops = &shifted_smsc911x_ops;
  2047. pm_runtime_enable(&pdev->dev);
  2048. pm_runtime_get_sync(&pdev->dev);
  2049. retval = smsc911x_init(dev);
  2050. if (retval < 0)
  2051. goto out_disable_resources;
  2052. /* configure irq polarity and type before connecting isr */
  2053. if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
  2054. intcfg |= INT_CFG_IRQ_POL_;
  2055. if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
  2056. intcfg |= INT_CFG_IRQ_TYPE_;
  2057. smsc911x_reg_write(pdata, INT_CFG, intcfg);
  2058. /* Ensure interrupts are globally disabled before connecting ISR */
  2059. smsc911x_disable_irq_chip(dev);
  2060. retval = request_irq(dev->irq, smsc911x_irqhandler,
  2061. irq_flags | IRQF_SHARED, dev->name, dev);
  2062. if (retval) {
  2063. SMSC_WARN(pdata, probe,
  2064. "Unable to claim requested irq: %d", dev->irq);
  2065. goto out_disable_resources;
  2066. }
  2067. netif_carrier_off(dev);
  2068. retval = register_netdev(dev);
  2069. if (retval) {
  2070. SMSC_WARN(pdata, probe, "Error %i registering device", retval);
  2071. goto out_free_irq;
  2072. } else {
  2073. SMSC_TRACE(pdata, probe,
  2074. "Network interface: \"%s\"", dev->name);
  2075. }
  2076. retval = smsc911x_mii_init(pdev, dev);
  2077. if (retval) {
  2078. SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
  2079. goto out_unregister_netdev_5;
  2080. }
  2081. spin_lock_irq(&pdata->mac_lock);
  2082. /* Check if mac address has been specified when bringing interface up */
  2083. if (is_valid_ether_addr(dev->dev_addr)) {
  2084. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  2085. SMSC_TRACE(pdata, probe,
  2086. "MAC Address is specified by configuration");
  2087. } else if (is_valid_ether_addr(pdata->config.mac)) {
  2088. memcpy(dev->dev_addr, pdata->config.mac, ETH_ALEN);
  2089. SMSC_TRACE(pdata, probe,
  2090. "MAC Address specified by platform data");
  2091. } else {
  2092. /* Try reading mac address from device. if EEPROM is present
  2093. * it will already have been set */
  2094. smsc_get_mac(dev);
  2095. if (is_valid_ether_addr(dev->dev_addr)) {
  2096. /* eeprom values are valid so use them */
  2097. SMSC_TRACE(pdata, probe,
  2098. "Mac Address is read from LAN911x EEPROM");
  2099. } else {
  2100. /* eeprom values are invalid, generate random MAC */
  2101. eth_hw_addr_random(dev);
  2102. smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
  2103. SMSC_TRACE(pdata, probe,
  2104. "MAC Address is set to eth_random_addr");
  2105. }
  2106. }
  2107. spin_unlock_irq(&pdata->mac_lock);
  2108. netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
  2109. return 0;
  2110. out_unregister_netdev_5:
  2111. unregister_netdev(dev);
  2112. out_free_irq:
  2113. free_irq(dev->irq, dev);
  2114. out_disable_resources:
  2115. pm_runtime_put(&pdev->dev);
  2116. pm_runtime_disable(&pdev->dev);
  2117. (void)smsc911x_disable_resources(pdev);
  2118. out_enable_resources_fail:
  2119. smsc911x_free_resources(pdev);
  2120. out_request_resources_fail:
  2121. iounmap(pdata->ioaddr);
  2122. free_netdev(dev);
  2123. out_release_io_1:
  2124. release_mem_region(res->start, resource_size(res));
  2125. out_0:
  2126. return retval;
  2127. }
  2128. #ifdef CONFIG_PM
  2129. /* This implementation assumes the devices remains powered on its VDDVARIO
  2130. * pins during suspend. */
  2131. /* TODO: implement freeze/thaw callbacks for hibernation.*/
  2132. static int smsc911x_suspend(struct device *dev)
  2133. {
  2134. struct net_device *ndev = dev_get_drvdata(dev);
  2135. struct smsc911x_data *pdata = netdev_priv(ndev);
  2136. /* enable wake on LAN, energy detection and the external PME
  2137. * signal. */
  2138. smsc911x_reg_write(pdata, PMT_CTRL,
  2139. PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
  2140. PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
  2141. return 0;
  2142. }
  2143. static int smsc911x_resume(struct device *dev)
  2144. {
  2145. struct net_device *ndev = dev_get_drvdata(dev);
  2146. struct smsc911x_data *pdata = netdev_priv(ndev);
  2147. unsigned int to = 100;
  2148. /* Note 3.11 from the datasheet:
  2149. * "When the LAN9220 is in a power saving state, a write of any
  2150. * data to the BYTE_TEST register will wake-up the device."
  2151. */
  2152. smsc911x_reg_write(pdata, BYTE_TEST, 0);
  2153. /* poll the READY bit in PMT_CTRL. Any other access to the device is
  2154. * forbidden while this bit isn't set. Try for 100ms and return -EIO
  2155. * if it failed. */
  2156. while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
  2157. udelay(1000);
  2158. return (to == 0) ? -EIO : 0;
  2159. }
  2160. static const struct dev_pm_ops smsc911x_pm_ops = {
  2161. .suspend = smsc911x_suspend,
  2162. .resume = smsc911x_resume,
  2163. };
  2164. #define SMSC911X_PM_OPS (&smsc911x_pm_ops)
  2165. #else
  2166. #define SMSC911X_PM_OPS NULL
  2167. #endif
  2168. #ifdef CONFIG_OF
  2169. static const struct of_device_id smsc911x_dt_ids[] = {
  2170. { .compatible = "smsc,lan9115", },
  2171. { /* sentinel */ }
  2172. };
  2173. MODULE_DEVICE_TABLE(of, smsc911x_dt_ids);
  2174. #endif
  2175. static const struct acpi_device_id smsc911x_acpi_match[] = {
  2176. { "ARMH9118", 0 },
  2177. { }
  2178. };
  2179. MODULE_DEVICE_TABLE(acpi, smsc911x_acpi_match);
  2180. static struct platform_driver smsc911x_driver = {
  2181. .probe = smsc911x_drv_probe,
  2182. .remove = smsc911x_drv_remove,
  2183. .driver = {
  2184. .name = SMSC_CHIPNAME,
  2185. .pm = SMSC911X_PM_OPS,
  2186. .of_match_table = of_match_ptr(smsc911x_dt_ids),
  2187. .acpi_match_table = ACPI_PTR(smsc911x_acpi_match),
  2188. },
  2189. };
  2190. /* Entry point for loading the module */
  2191. static int __init smsc911x_init_module(void)
  2192. {
  2193. SMSC_INITIALIZE();
  2194. return platform_driver_register(&smsc911x_driver);
  2195. }
  2196. /* entry point for unloading the module */
  2197. static void __exit smsc911x_cleanup_module(void)
  2198. {
  2199. platform_driver_unregister(&smsc911x_driver);
  2200. }
  2201. module_init(smsc911x_init_module);
  2202. module_exit(smsc911x_cleanup_module);