smsc911x.c 62 KB

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