w5100.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * Ethernet driver for the WIZnet W5100 chip.
  3. *
  4. * Copyright (C) 2006-2008 WIZnet Co.,Ltd.
  5. * Copyright (C) 2012 Mike Sinkovsky <msink@permonline.ru>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/platform_data/wiznet.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/ioport.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/gpio.h>
  27. #include "w5100.h"
  28. #define DRV_NAME "w5100"
  29. #define DRV_VERSION "2012-04-04"
  30. MODULE_DESCRIPTION("WIZnet W5100 Ethernet driver v"DRV_VERSION);
  31. MODULE_AUTHOR("Mike Sinkovsky <msink@permonline.ru>");
  32. MODULE_ALIAS("platform:"DRV_NAME);
  33. MODULE_LICENSE("GPL");
  34. /*
  35. * W5100/W5200/W5500 common registers
  36. */
  37. #define W5100_COMMON_REGS 0x0000
  38. #define W5100_MR 0x0000 /* Mode Register */
  39. #define MR_RST 0x80 /* S/W reset */
  40. #define MR_PB 0x10 /* Ping block */
  41. #define MR_AI 0x02 /* Address Auto-Increment */
  42. #define MR_IND 0x01 /* Indirect mode */
  43. #define W5100_SHAR 0x0009 /* Source MAC address */
  44. #define W5100_IR 0x0015 /* Interrupt Register */
  45. #define W5100_COMMON_REGS_LEN 0x0040
  46. #define W5100_Sn_MR 0x0000 /* Sn Mode Register */
  47. #define W5100_Sn_CR 0x0001 /* Sn Command Register */
  48. #define W5100_Sn_IR 0x0002 /* Sn Interrupt Register */
  49. #define W5100_Sn_SR 0x0003 /* Sn Status Register */
  50. #define W5100_Sn_TX_FSR 0x0020 /* Sn Transmit free memory size */
  51. #define W5100_Sn_TX_RD 0x0022 /* Sn Transmit memory read pointer */
  52. #define W5100_Sn_TX_WR 0x0024 /* Sn Transmit memory write pointer */
  53. #define W5100_Sn_RX_RSR 0x0026 /* Sn Receive free memory size */
  54. #define W5100_Sn_RX_RD 0x0028 /* Sn Receive memory read pointer */
  55. #define S0_REGS(priv) ((priv)->s0_regs)
  56. #define W5100_S0_MR(priv) (S0_REGS(priv) + W5100_Sn_MR)
  57. #define S0_MR_MACRAW 0x04 /* MAC RAW mode */
  58. #define S0_MR_MF 0x40 /* MAC Filter for W5100 and W5200 */
  59. #define W5500_S0_MR_MF 0x80 /* MAC Filter for W5500 */
  60. #define W5100_S0_CR(priv) (S0_REGS(priv) + W5100_Sn_CR)
  61. #define S0_CR_OPEN 0x01 /* OPEN command */
  62. #define S0_CR_CLOSE 0x10 /* CLOSE command */
  63. #define S0_CR_SEND 0x20 /* SEND command */
  64. #define S0_CR_RECV 0x40 /* RECV command */
  65. #define W5100_S0_IR(priv) (S0_REGS(priv) + W5100_Sn_IR)
  66. #define S0_IR_SENDOK 0x10 /* complete sending */
  67. #define S0_IR_RECV 0x04 /* receiving data */
  68. #define W5100_S0_SR(priv) (S0_REGS(priv) + W5100_Sn_SR)
  69. #define S0_SR_MACRAW 0x42 /* mac raw mode */
  70. #define W5100_S0_TX_FSR(priv) (S0_REGS(priv) + W5100_Sn_TX_FSR)
  71. #define W5100_S0_TX_RD(priv) (S0_REGS(priv) + W5100_Sn_TX_RD)
  72. #define W5100_S0_TX_WR(priv) (S0_REGS(priv) + W5100_Sn_TX_WR)
  73. #define W5100_S0_RX_RSR(priv) (S0_REGS(priv) + W5100_Sn_RX_RSR)
  74. #define W5100_S0_RX_RD(priv) (S0_REGS(priv) + W5100_Sn_RX_RD)
  75. #define W5100_S0_REGS_LEN 0x0040
  76. /*
  77. * W5100 and W5200 common registers
  78. */
  79. #define W5100_IMR 0x0016 /* Interrupt Mask Register */
  80. #define IR_S0 0x01 /* S0 interrupt */
  81. #define W5100_RTR 0x0017 /* Retry Time-value Register */
  82. #define RTR_DEFAULT 2000 /* =0x07d0 (2000) */
  83. /*
  84. * W5100 specific register and memory
  85. */
  86. #define W5100_RMSR 0x001a /* Receive Memory Size */
  87. #define W5100_TMSR 0x001b /* Transmit Memory Size */
  88. #define W5100_S0_REGS 0x0400
  89. #define W5100_TX_MEM_START 0x4000
  90. #define W5100_TX_MEM_SIZE 0x2000
  91. #define W5100_RX_MEM_START 0x6000
  92. #define W5100_RX_MEM_SIZE 0x2000
  93. /*
  94. * W5200 specific register and memory
  95. */
  96. #define W5200_S0_REGS 0x4000
  97. #define W5200_Sn_RXMEM_SIZE(n) (0x401e + (n) * 0x0100) /* Sn RX Memory Size */
  98. #define W5200_Sn_TXMEM_SIZE(n) (0x401f + (n) * 0x0100) /* Sn TX Memory Size */
  99. #define W5200_TX_MEM_START 0x8000
  100. #define W5200_TX_MEM_SIZE 0x4000
  101. #define W5200_RX_MEM_START 0xc000
  102. #define W5200_RX_MEM_SIZE 0x4000
  103. /*
  104. * W5500 specific register and memory
  105. *
  106. * W5500 register and memory are organized by multiple blocks. Each one is
  107. * selected by 16bits offset address and 5bits block select bits. So we
  108. * encode it into 32bits address. (lower 16bits is offset address and
  109. * upper 16bits is block select bits)
  110. */
  111. #define W5500_SIMR 0x0018 /* Socket Interrupt Mask Register */
  112. #define W5500_RTR 0x0019 /* Retry Time-value Register */
  113. #define W5500_S0_REGS 0x10000
  114. #define W5500_Sn_RXMEM_SIZE(n) \
  115. (0x1001e + (n) * 0x40000) /* Sn RX Memory Size */
  116. #define W5500_Sn_TXMEM_SIZE(n) \
  117. (0x1001f + (n) * 0x40000) /* Sn TX Memory Size */
  118. #define W5500_TX_MEM_START 0x20000
  119. #define W5500_TX_MEM_SIZE 0x04000
  120. #define W5500_RX_MEM_START 0x30000
  121. #define W5500_RX_MEM_SIZE 0x04000
  122. /*
  123. * Device driver private data structure
  124. */
  125. struct w5100_priv {
  126. const struct w5100_ops *ops;
  127. /* Socket 0 register offset address */
  128. u32 s0_regs;
  129. /* Socket 0 TX buffer offset address and size */
  130. u32 s0_tx_buf;
  131. u16 s0_tx_buf_size;
  132. /* Socket 0 RX buffer offset address and size */
  133. u32 s0_rx_buf;
  134. u16 s0_rx_buf_size;
  135. int irq;
  136. int link_irq;
  137. int link_gpio;
  138. struct napi_struct napi;
  139. struct net_device *ndev;
  140. bool promisc;
  141. u32 msg_enable;
  142. struct workqueue_struct *xfer_wq;
  143. struct work_struct rx_work;
  144. struct sk_buff *tx_skb;
  145. struct work_struct tx_work;
  146. struct work_struct setrx_work;
  147. struct work_struct restart_work;
  148. };
  149. /************************************************************************
  150. *
  151. * Lowlevel I/O functions
  152. *
  153. ***********************************************************************/
  154. struct w5100_mmio_priv {
  155. void __iomem *base;
  156. /* Serialize access in indirect address mode */
  157. spinlock_t reg_lock;
  158. };
  159. static inline struct w5100_mmio_priv *w5100_mmio_priv(struct net_device *dev)
  160. {
  161. return w5100_ops_priv(dev);
  162. }
  163. static inline void __iomem *w5100_mmio(struct net_device *ndev)
  164. {
  165. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  166. return mmio_priv->base;
  167. }
  168. /*
  169. * In direct address mode host system can directly access W5100 registers
  170. * after mapping to Memory-Mapped I/O space.
  171. *
  172. * 0x8000 bytes are required for memory space.
  173. */
  174. static inline int w5100_read_direct(struct net_device *ndev, u32 addr)
  175. {
  176. return ioread8(w5100_mmio(ndev) + (addr << CONFIG_WIZNET_BUS_SHIFT));
  177. }
  178. static inline int __w5100_write_direct(struct net_device *ndev, u32 addr,
  179. u8 data)
  180. {
  181. iowrite8(data, w5100_mmio(ndev) + (addr << CONFIG_WIZNET_BUS_SHIFT));
  182. return 0;
  183. }
  184. static inline int w5100_write_direct(struct net_device *ndev, u32 addr, u8 data)
  185. {
  186. __w5100_write_direct(ndev, addr, data);
  187. mmiowb();
  188. return 0;
  189. }
  190. static int w5100_read16_direct(struct net_device *ndev, u32 addr)
  191. {
  192. u16 data;
  193. data = w5100_read_direct(ndev, addr) << 8;
  194. data |= w5100_read_direct(ndev, addr + 1);
  195. return data;
  196. }
  197. static int w5100_write16_direct(struct net_device *ndev, u32 addr, u16 data)
  198. {
  199. __w5100_write_direct(ndev, addr, data >> 8);
  200. __w5100_write_direct(ndev, addr + 1, data);
  201. mmiowb();
  202. return 0;
  203. }
  204. static int w5100_readbulk_direct(struct net_device *ndev, u32 addr, u8 *buf,
  205. int len)
  206. {
  207. int i;
  208. for (i = 0; i < len; i++, addr++)
  209. *buf++ = w5100_read_direct(ndev, addr);
  210. return 0;
  211. }
  212. static int w5100_writebulk_direct(struct net_device *ndev, u32 addr,
  213. const u8 *buf, int len)
  214. {
  215. int i;
  216. for (i = 0; i < len; i++, addr++)
  217. __w5100_write_direct(ndev, addr, *buf++);
  218. mmiowb();
  219. return 0;
  220. }
  221. static int w5100_mmio_init(struct net_device *ndev)
  222. {
  223. struct platform_device *pdev = to_platform_device(ndev->dev.parent);
  224. struct w5100_priv *priv = netdev_priv(ndev);
  225. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  226. struct resource *mem;
  227. spin_lock_init(&mmio_priv->reg_lock);
  228. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  229. mmio_priv->base = devm_ioremap_resource(&pdev->dev, mem);
  230. if (IS_ERR(mmio_priv->base))
  231. return PTR_ERR(mmio_priv->base);
  232. netdev_info(ndev, "at 0x%llx irq %d\n", (u64)mem->start, priv->irq);
  233. return 0;
  234. }
  235. static const struct w5100_ops w5100_mmio_direct_ops = {
  236. .chip_id = W5100,
  237. .read = w5100_read_direct,
  238. .write = w5100_write_direct,
  239. .read16 = w5100_read16_direct,
  240. .write16 = w5100_write16_direct,
  241. .readbulk = w5100_readbulk_direct,
  242. .writebulk = w5100_writebulk_direct,
  243. .init = w5100_mmio_init,
  244. };
  245. /*
  246. * In indirect address mode host system indirectly accesses registers by
  247. * using Indirect Mode Address Register (IDM_AR) and Indirect Mode Data
  248. * Register (IDM_DR), which are directly mapped to Memory-Mapped I/O space.
  249. * Mode Register (MR) is directly accessible.
  250. *
  251. * Only 0x04 bytes are required for memory space.
  252. */
  253. #define W5100_IDM_AR 0x01 /* Indirect Mode Address Register */
  254. #define W5100_IDM_DR 0x03 /* Indirect Mode Data Register */
  255. static int w5100_read_indirect(struct net_device *ndev, u32 addr)
  256. {
  257. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  258. unsigned long flags;
  259. u8 data;
  260. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  261. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  262. data = w5100_read_direct(ndev, W5100_IDM_DR);
  263. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  264. return data;
  265. }
  266. static int w5100_write_indirect(struct net_device *ndev, u32 addr, u8 data)
  267. {
  268. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  269. unsigned long flags;
  270. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  271. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  272. w5100_write_direct(ndev, W5100_IDM_DR, data);
  273. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  274. return 0;
  275. }
  276. static int w5100_read16_indirect(struct net_device *ndev, u32 addr)
  277. {
  278. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  279. unsigned long flags;
  280. u16 data;
  281. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  282. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  283. data = w5100_read_direct(ndev, W5100_IDM_DR) << 8;
  284. data |= w5100_read_direct(ndev, W5100_IDM_DR);
  285. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  286. return data;
  287. }
  288. static int w5100_write16_indirect(struct net_device *ndev, u32 addr, u16 data)
  289. {
  290. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  291. unsigned long flags;
  292. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  293. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  294. __w5100_write_direct(ndev, W5100_IDM_DR, data >> 8);
  295. w5100_write_direct(ndev, W5100_IDM_DR, data);
  296. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  297. return 0;
  298. }
  299. static int w5100_readbulk_indirect(struct net_device *ndev, u32 addr, u8 *buf,
  300. int len)
  301. {
  302. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  303. unsigned long flags;
  304. int i;
  305. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  306. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  307. for (i = 0; i < len; i++)
  308. *buf++ = w5100_read_direct(ndev, W5100_IDM_DR);
  309. mmiowb();
  310. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  311. return 0;
  312. }
  313. static int w5100_writebulk_indirect(struct net_device *ndev, u32 addr,
  314. const u8 *buf, int len)
  315. {
  316. struct w5100_mmio_priv *mmio_priv = w5100_mmio_priv(ndev);
  317. unsigned long flags;
  318. int i;
  319. spin_lock_irqsave(&mmio_priv->reg_lock, flags);
  320. w5100_write16_direct(ndev, W5100_IDM_AR, addr);
  321. for (i = 0; i < len; i++)
  322. __w5100_write_direct(ndev, W5100_IDM_DR, *buf++);
  323. mmiowb();
  324. spin_unlock_irqrestore(&mmio_priv->reg_lock, flags);
  325. return 0;
  326. }
  327. static int w5100_reset_indirect(struct net_device *ndev)
  328. {
  329. w5100_write_direct(ndev, W5100_MR, MR_RST);
  330. mdelay(5);
  331. w5100_write_direct(ndev, W5100_MR, MR_PB | MR_AI | MR_IND);
  332. return 0;
  333. }
  334. static const struct w5100_ops w5100_mmio_indirect_ops = {
  335. .chip_id = W5100,
  336. .read = w5100_read_indirect,
  337. .write = w5100_write_indirect,
  338. .read16 = w5100_read16_indirect,
  339. .write16 = w5100_write16_indirect,
  340. .readbulk = w5100_readbulk_indirect,
  341. .writebulk = w5100_writebulk_indirect,
  342. .init = w5100_mmio_init,
  343. .reset = w5100_reset_indirect,
  344. };
  345. #if defined(CONFIG_WIZNET_BUS_DIRECT)
  346. static int w5100_read(struct w5100_priv *priv, u32 addr)
  347. {
  348. return w5100_read_direct(priv->ndev, addr);
  349. }
  350. static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
  351. {
  352. return w5100_write_direct(priv->ndev, addr, data);
  353. }
  354. static int w5100_read16(struct w5100_priv *priv, u32 addr)
  355. {
  356. return w5100_read16_direct(priv->ndev, addr);
  357. }
  358. static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
  359. {
  360. return w5100_write16_direct(priv->ndev, addr, data);
  361. }
  362. static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
  363. {
  364. return w5100_readbulk_direct(priv->ndev, addr, buf, len);
  365. }
  366. static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
  367. int len)
  368. {
  369. return w5100_writebulk_direct(priv->ndev, addr, buf, len);
  370. }
  371. #elif defined(CONFIG_WIZNET_BUS_INDIRECT)
  372. static int w5100_read(struct w5100_priv *priv, u32 addr)
  373. {
  374. return w5100_read_indirect(priv->ndev, addr);
  375. }
  376. static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
  377. {
  378. return w5100_write_indirect(priv->ndev, addr, data);
  379. }
  380. static int w5100_read16(struct w5100_priv *priv, u32 addr)
  381. {
  382. return w5100_read16_indirect(priv->ndev, addr);
  383. }
  384. static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
  385. {
  386. return w5100_write16_indirect(priv->ndev, addr, data);
  387. }
  388. static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
  389. {
  390. return w5100_readbulk_indirect(priv->ndev, addr, buf, len);
  391. }
  392. static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
  393. int len)
  394. {
  395. return w5100_writebulk_indirect(priv->ndev, addr, buf, len);
  396. }
  397. #else /* CONFIG_WIZNET_BUS_ANY */
  398. static int w5100_read(struct w5100_priv *priv, u32 addr)
  399. {
  400. return priv->ops->read(priv->ndev, addr);
  401. }
  402. static int w5100_write(struct w5100_priv *priv, u32 addr, u8 data)
  403. {
  404. return priv->ops->write(priv->ndev, addr, data);
  405. }
  406. static int w5100_read16(struct w5100_priv *priv, u32 addr)
  407. {
  408. return priv->ops->read16(priv->ndev, addr);
  409. }
  410. static int w5100_write16(struct w5100_priv *priv, u32 addr, u16 data)
  411. {
  412. return priv->ops->write16(priv->ndev, addr, data);
  413. }
  414. static int w5100_readbulk(struct w5100_priv *priv, u32 addr, u8 *buf, int len)
  415. {
  416. return priv->ops->readbulk(priv->ndev, addr, buf, len);
  417. }
  418. static int w5100_writebulk(struct w5100_priv *priv, u32 addr, const u8 *buf,
  419. int len)
  420. {
  421. return priv->ops->writebulk(priv->ndev, addr, buf, len);
  422. }
  423. #endif
  424. static int w5100_readbuf(struct w5100_priv *priv, u16 offset, u8 *buf, int len)
  425. {
  426. u32 addr;
  427. int remain = 0;
  428. int ret;
  429. const u32 mem_start = priv->s0_rx_buf;
  430. const u16 mem_size = priv->s0_rx_buf_size;
  431. offset %= mem_size;
  432. addr = mem_start + offset;
  433. if (offset + len > mem_size) {
  434. remain = (offset + len) % mem_size;
  435. len = mem_size - offset;
  436. }
  437. ret = w5100_readbulk(priv, addr, buf, len);
  438. if (ret || !remain)
  439. return ret;
  440. return w5100_readbulk(priv, mem_start, buf + len, remain);
  441. }
  442. static int w5100_writebuf(struct w5100_priv *priv, u16 offset, const u8 *buf,
  443. int len)
  444. {
  445. u32 addr;
  446. int ret;
  447. int remain = 0;
  448. const u32 mem_start = priv->s0_tx_buf;
  449. const u16 mem_size = priv->s0_tx_buf_size;
  450. offset %= mem_size;
  451. addr = mem_start + offset;
  452. if (offset + len > mem_size) {
  453. remain = (offset + len) % mem_size;
  454. len = mem_size - offset;
  455. }
  456. ret = w5100_writebulk(priv, addr, buf, len);
  457. if (ret || !remain)
  458. return ret;
  459. return w5100_writebulk(priv, mem_start, buf + len, remain);
  460. }
  461. static int w5100_reset(struct w5100_priv *priv)
  462. {
  463. if (priv->ops->reset)
  464. return priv->ops->reset(priv->ndev);
  465. w5100_write(priv, W5100_MR, MR_RST);
  466. mdelay(5);
  467. w5100_write(priv, W5100_MR, MR_PB);
  468. return 0;
  469. }
  470. static int w5100_command(struct w5100_priv *priv, u16 cmd)
  471. {
  472. unsigned long timeout;
  473. w5100_write(priv, W5100_S0_CR(priv), cmd);
  474. timeout = jiffies + msecs_to_jiffies(100);
  475. while (w5100_read(priv, W5100_S0_CR(priv)) != 0) {
  476. if (time_after(jiffies, timeout))
  477. return -EIO;
  478. cpu_relax();
  479. }
  480. return 0;
  481. }
  482. static void w5100_write_macaddr(struct w5100_priv *priv)
  483. {
  484. struct net_device *ndev = priv->ndev;
  485. w5100_writebulk(priv, W5100_SHAR, ndev->dev_addr, ETH_ALEN);
  486. }
  487. static void w5100_socket_intr_mask(struct w5100_priv *priv, u8 mask)
  488. {
  489. u32 imr;
  490. if (priv->ops->chip_id == W5500)
  491. imr = W5500_SIMR;
  492. else
  493. imr = W5100_IMR;
  494. w5100_write(priv, imr, mask);
  495. }
  496. static void w5100_enable_intr(struct w5100_priv *priv)
  497. {
  498. w5100_socket_intr_mask(priv, IR_S0);
  499. }
  500. static void w5100_disable_intr(struct w5100_priv *priv)
  501. {
  502. w5100_socket_intr_mask(priv, 0);
  503. }
  504. static void w5100_memory_configure(struct w5100_priv *priv)
  505. {
  506. /* Configure 16K of internal memory
  507. * as 8K RX buffer and 8K TX buffer
  508. */
  509. w5100_write(priv, W5100_RMSR, 0x03);
  510. w5100_write(priv, W5100_TMSR, 0x03);
  511. }
  512. static void w5200_memory_configure(struct w5100_priv *priv)
  513. {
  514. int i;
  515. /* Configure internal RX memory as 16K RX buffer and
  516. * internal TX memory as 16K TX buffer
  517. */
  518. w5100_write(priv, W5200_Sn_RXMEM_SIZE(0), 0x10);
  519. w5100_write(priv, W5200_Sn_TXMEM_SIZE(0), 0x10);
  520. for (i = 1; i < 8; i++) {
  521. w5100_write(priv, W5200_Sn_RXMEM_SIZE(i), 0);
  522. w5100_write(priv, W5200_Sn_TXMEM_SIZE(i), 0);
  523. }
  524. }
  525. static void w5500_memory_configure(struct w5100_priv *priv)
  526. {
  527. int i;
  528. /* Configure internal RX memory as 16K RX buffer and
  529. * internal TX memory as 16K TX buffer
  530. */
  531. w5100_write(priv, W5500_Sn_RXMEM_SIZE(0), 0x10);
  532. w5100_write(priv, W5500_Sn_TXMEM_SIZE(0), 0x10);
  533. for (i = 1; i < 8; i++) {
  534. w5100_write(priv, W5500_Sn_RXMEM_SIZE(i), 0);
  535. w5100_write(priv, W5500_Sn_TXMEM_SIZE(i), 0);
  536. }
  537. }
  538. static int w5100_hw_reset(struct w5100_priv *priv)
  539. {
  540. u32 rtr;
  541. w5100_reset(priv);
  542. w5100_disable_intr(priv);
  543. w5100_write_macaddr(priv);
  544. switch (priv->ops->chip_id) {
  545. case W5100:
  546. w5100_memory_configure(priv);
  547. rtr = W5100_RTR;
  548. break;
  549. case W5200:
  550. w5200_memory_configure(priv);
  551. rtr = W5100_RTR;
  552. break;
  553. case W5500:
  554. w5500_memory_configure(priv);
  555. rtr = W5500_RTR;
  556. break;
  557. default:
  558. return -EINVAL;
  559. }
  560. if (w5100_read16(priv, rtr) != RTR_DEFAULT)
  561. return -ENODEV;
  562. return 0;
  563. }
  564. static void w5100_hw_start(struct w5100_priv *priv)
  565. {
  566. u8 mode = S0_MR_MACRAW;
  567. if (!priv->promisc) {
  568. if (priv->ops->chip_id == W5500)
  569. mode |= W5500_S0_MR_MF;
  570. else
  571. mode |= S0_MR_MF;
  572. }
  573. w5100_write(priv, W5100_S0_MR(priv), mode);
  574. w5100_command(priv, S0_CR_OPEN);
  575. w5100_enable_intr(priv);
  576. }
  577. static void w5100_hw_close(struct w5100_priv *priv)
  578. {
  579. w5100_disable_intr(priv);
  580. w5100_command(priv, S0_CR_CLOSE);
  581. }
  582. /***********************************************************************
  583. *
  584. * Device driver functions / callbacks
  585. *
  586. ***********************************************************************/
  587. static void w5100_get_drvinfo(struct net_device *ndev,
  588. struct ethtool_drvinfo *info)
  589. {
  590. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  591. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  592. strlcpy(info->bus_info, dev_name(ndev->dev.parent),
  593. sizeof(info->bus_info));
  594. }
  595. static u32 w5100_get_link(struct net_device *ndev)
  596. {
  597. struct w5100_priv *priv = netdev_priv(ndev);
  598. if (gpio_is_valid(priv->link_gpio))
  599. return !!gpio_get_value(priv->link_gpio);
  600. return 1;
  601. }
  602. static u32 w5100_get_msglevel(struct net_device *ndev)
  603. {
  604. struct w5100_priv *priv = netdev_priv(ndev);
  605. return priv->msg_enable;
  606. }
  607. static void w5100_set_msglevel(struct net_device *ndev, u32 value)
  608. {
  609. struct w5100_priv *priv = netdev_priv(ndev);
  610. priv->msg_enable = value;
  611. }
  612. static int w5100_get_regs_len(struct net_device *ndev)
  613. {
  614. return W5100_COMMON_REGS_LEN + W5100_S0_REGS_LEN;
  615. }
  616. static void w5100_get_regs(struct net_device *ndev,
  617. struct ethtool_regs *regs, void *buf)
  618. {
  619. struct w5100_priv *priv = netdev_priv(ndev);
  620. regs->version = 1;
  621. w5100_readbulk(priv, W5100_COMMON_REGS, buf, W5100_COMMON_REGS_LEN);
  622. buf += W5100_COMMON_REGS_LEN;
  623. w5100_readbulk(priv, S0_REGS(priv), buf, W5100_S0_REGS_LEN);
  624. }
  625. static void w5100_restart(struct net_device *ndev)
  626. {
  627. struct w5100_priv *priv = netdev_priv(ndev);
  628. netif_stop_queue(ndev);
  629. w5100_hw_reset(priv);
  630. w5100_hw_start(priv);
  631. ndev->stats.tx_errors++;
  632. netif_trans_update(ndev);
  633. netif_wake_queue(ndev);
  634. }
  635. static void w5100_restart_work(struct work_struct *work)
  636. {
  637. struct w5100_priv *priv = container_of(work, struct w5100_priv,
  638. restart_work);
  639. w5100_restart(priv->ndev);
  640. }
  641. static void w5100_tx_timeout(struct net_device *ndev)
  642. {
  643. struct w5100_priv *priv = netdev_priv(ndev);
  644. if (priv->ops->may_sleep)
  645. schedule_work(&priv->restart_work);
  646. else
  647. w5100_restart(ndev);
  648. }
  649. static void w5100_tx_skb(struct net_device *ndev, struct sk_buff *skb)
  650. {
  651. struct w5100_priv *priv = netdev_priv(ndev);
  652. u16 offset;
  653. offset = w5100_read16(priv, W5100_S0_TX_WR(priv));
  654. w5100_writebuf(priv, offset, skb->data, skb->len);
  655. w5100_write16(priv, W5100_S0_TX_WR(priv), offset + skb->len);
  656. ndev->stats.tx_bytes += skb->len;
  657. ndev->stats.tx_packets++;
  658. dev_kfree_skb(skb);
  659. w5100_command(priv, S0_CR_SEND);
  660. }
  661. static void w5100_tx_work(struct work_struct *work)
  662. {
  663. struct w5100_priv *priv = container_of(work, struct w5100_priv,
  664. tx_work);
  665. struct sk_buff *skb = priv->tx_skb;
  666. priv->tx_skb = NULL;
  667. if (WARN_ON(!skb))
  668. return;
  669. w5100_tx_skb(priv->ndev, skb);
  670. }
  671. static int w5100_start_tx(struct sk_buff *skb, struct net_device *ndev)
  672. {
  673. struct w5100_priv *priv = netdev_priv(ndev);
  674. netif_stop_queue(ndev);
  675. if (priv->ops->may_sleep) {
  676. WARN_ON(priv->tx_skb);
  677. priv->tx_skb = skb;
  678. queue_work(priv->xfer_wq, &priv->tx_work);
  679. } else {
  680. w5100_tx_skb(ndev, skb);
  681. }
  682. return NETDEV_TX_OK;
  683. }
  684. static struct sk_buff *w5100_rx_skb(struct net_device *ndev)
  685. {
  686. struct w5100_priv *priv = netdev_priv(ndev);
  687. struct sk_buff *skb;
  688. u16 rx_len;
  689. u16 offset;
  690. u8 header[2];
  691. u16 rx_buf_len = w5100_read16(priv, W5100_S0_RX_RSR(priv));
  692. if (rx_buf_len == 0)
  693. return NULL;
  694. offset = w5100_read16(priv, W5100_S0_RX_RD(priv));
  695. w5100_readbuf(priv, offset, header, 2);
  696. rx_len = get_unaligned_be16(header) - 2;
  697. skb = netdev_alloc_skb_ip_align(ndev, rx_len);
  698. if (unlikely(!skb)) {
  699. w5100_write16(priv, W5100_S0_RX_RD(priv), offset + rx_buf_len);
  700. w5100_command(priv, S0_CR_RECV);
  701. ndev->stats.rx_dropped++;
  702. return NULL;
  703. }
  704. skb_put(skb, rx_len);
  705. w5100_readbuf(priv, offset + 2, skb->data, rx_len);
  706. w5100_write16(priv, W5100_S0_RX_RD(priv), offset + 2 + rx_len);
  707. w5100_command(priv, S0_CR_RECV);
  708. skb->protocol = eth_type_trans(skb, ndev);
  709. ndev->stats.rx_packets++;
  710. ndev->stats.rx_bytes += rx_len;
  711. return skb;
  712. }
  713. static void w5100_rx_work(struct work_struct *work)
  714. {
  715. struct w5100_priv *priv = container_of(work, struct w5100_priv,
  716. rx_work);
  717. struct sk_buff *skb;
  718. while ((skb = w5100_rx_skb(priv->ndev)))
  719. netif_rx_ni(skb);
  720. w5100_enable_intr(priv);
  721. }
  722. static int w5100_napi_poll(struct napi_struct *napi, int budget)
  723. {
  724. struct w5100_priv *priv = container_of(napi, struct w5100_priv, napi);
  725. int rx_count;
  726. for (rx_count = 0; rx_count < budget; rx_count++) {
  727. struct sk_buff *skb = w5100_rx_skb(priv->ndev);
  728. if (skb)
  729. netif_receive_skb(skb);
  730. else
  731. break;
  732. }
  733. if (rx_count < budget) {
  734. napi_complete(napi);
  735. w5100_enable_intr(priv);
  736. }
  737. return rx_count;
  738. }
  739. static irqreturn_t w5100_interrupt(int irq, void *ndev_instance)
  740. {
  741. struct net_device *ndev = ndev_instance;
  742. struct w5100_priv *priv = netdev_priv(ndev);
  743. int ir = w5100_read(priv, W5100_S0_IR(priv));
  744. if (!ir)
  745. return IRQ_NONE;
  746. w5100_write(priv, W5100_S0_IR(priv), ir);
  747. if (ir & S0_IR_SENDOK) {
  748. netif_dbg(priv, tx_done, ndev, "tx done\n");
  749. netif_wake_queue(ndev);
  750. }
  751. if (ir & S0_IR_RECV) {
  752. w5100_disable_intr(priv);
  753. if (priv->ops->may_sleep)
  754. queue_work(priv->xfer_wq, &priv->rx_work);
  755. else if (napi_schedule_prep(&priv->napi))
  756. __napi_schedule(&priv->napi);
  757. }
  758. return IRQ_HANDLED;
  759. }
  760. static irqreturn_t w5100_detect_link(int irq, void *ndev_instance)
  761. {
  762. struct net_device *ndev = ndev_instance;
  763. struct w5100_priv *priv = netdev_priv(ndev);
  764. if (netif_running(ndev)) {
  765. if (gpio_get_value(priv->link_gpio) != 0) {
  766. netif_info(priv, link, ndev, "link is up\n");
  767. netif_carrier_on(ndev);
  768. } else {
  769. netif_info(priv, link, ndev, "link is down\n");
  770. netif_carrier_off(ndev);
  771. }
  772. }
  773. return IRQ_HANDLED;
  774. }
  775. static void w5100_setrx_work(struct work_struct *work)
  776. {
  777. struct w5100_priv *priv = container_of(work, struct w5100_priv,
  778. setrx_work);
  779. w5100_hw_start(priv);
  780. }
  781. static void w5100_set_rx_mode(struct net_device *ndev)
  782. {
  783. struct w5100_priv *priv = netdev_priv(ndev);
  784. bool set_promisc = (ndev->flags & IFF_PROMISC) != 0;
  785. if (priv->promisc != set_promisc) {
  786. priv->promisc = set_promisc;
  787. if (priv->ops->may_sleep)
  788. schedule_work(&priv->setrx_work);
  789. else
  790. w5100_hw_start(priv);
  791. }
  792. }
  793. static int w5100_set_macaddr(struct net_device *ndev, void *addr)
  794. {
  795. struct w5100_priv *priv = netdev_priv(ndev);
  796. struct sockaddr *sock_addr = addr;
  797. if (!is_valid_ether_addr(sock_addr->sa_data))
  798. return -EADDRNOTAVAIL;
  799. memcpy(ndev->dev_addr, sock_addr->sa_data, ETH_ALEN);
  800. w5100_write_macaddr(priv);
  801. return 0;
  802. }
  803. static int w5100_open(struct net_device *ndev)
  804. {
  805. struct w5100_priv *priv = netdev_priv(ndev);
  806. netif_info(priv, ifup, ndev, "enabling\n");
  807. w5100_hw_start(priv);
  808. napi_enable(&priv->napi);
  809. netif_start_queue(ndev);
  810. if (!gpio_is_valid(priv->link_gpio) ||
  811. gpio_get_value(priv->link_gpio) != 0)
  812. netif_carrier_on(ndev);
  813. return 0;
  814. }
  815. static int w5100_stop(struct net_device *ndev)
  816. {
  817. struct w5100_priv *priv = netdev_priv(ndev);
  818. netif_info(priv, ifdown, ndev, "shutting down\n");
  819. w5100_hw_close(priv);
  820. netif_carrier_off(ndev);
  821. netif_stop_queue(ndev);
  822. napi_disable(&priv->napi);
  823. return 0;
  824. }
  825. static const struct ethtool_ops w5100_ethtool_ops = {
  826. .get_drvinfo = w5100_get_drvinfo,
  827. .get_msglevel = w5100_get_msglevel,
  828. .set_msglevel = w5100_set_msglevel,
  829. .get_link = w5100_get_link,
  830. .get_regs_len = w5100_get_regs_len,
  831. .get_regs = w5100_get_regs,
  832. };
  833. static const struct net_device_ops w5100_netdev_ops = {
  834. .ndo_open = w5100_open,
  835. .ndo_stop = w5100_stop,
  836. .ndo_start_xmit = w5100_start_tx,
  837. .ndo_tx_timeout = w5100_tx_timeout,
  838. .ndo_set_rx_mode = w5100_set_rx_mode,
  839. .ndo_set_mac_address = w5100_set_macaddr,
  840. .ndo_validate_addr = eth_validate_addr,
  841. .ndo_change_mtu = eth_change_mtu,
  842. };
  843. static int w5100_mmio_probe(struct platform_device *pdev)
  844. {
  845. struct wiznet_platform_data *data = dev_get_platdata(&pdev->dev);
  846. const void *mac_addr = NULL;
  847. struct resource *mem;
  848. const struct w5100_ops *ops;
  849. int irq;
  850. if (data && is_valid_ether_addr(data->mac_addr))
  851. mac_addr = data->mac_addr;
  852. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  853. if (resource_size(mem) < W5100_BUS_DIRECT_SIZE)
  854. ops = &w5100_mmio_indirect_ops;
  855. else
  856. ops = &w5100_mmio_direct_ops;
  857. irq = platform_get_irq(pdev, 0);
  858. if (irq < 0)
  859. return irq;
  860. return w5100_probe(&pdev->dev, ops, sizeof(struct w5100_mmio_priv),
  861. mac_addr, irq, data ? data->link_gpio : -EINVAL);
  862. }
  863. static int w5100_mmio_remove(struct platform_device *pdev)
  864. {
  865. return w5100_remove(&pdev->dev);
  866. }
  867. void *w5100_ops_priv(const struct net_device *ndev)
  868. {
  869. return netdev_priv(ndev) +
  870. ALIGN(sizeof(struct w5100_priv), NETDEV_ALIGN);
  871. }
  872. EXPORT_SYMBOL_GPL(w5100_ops_priv);
  873. int w5100_probe(struct device *dev, const struct w5100_ops *ops,
  874. int sizeof_ops_priv, const void *mac_addr, int irq,
  875. int link_gpio)
  876. {
  877. struct w5100_priv *priv;
  878. struct net_device *ndev;
  879. int err;
  880. size_t alloc_size;
  881. alloc_size = sizeof(*priv);
  882. if (sizeof_ops_priv) {
  883. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  884. alloc_size += sizeof_ops_priv;
  885. }
  886. alloc_size += NETDEV_ALIGN - 1;
  887. ndev = alloc_etherdev(alloc_size);
  888. if (!ndev)
  889. return -ENOMEM;
  890. SET_NETDEV_DEV(ndev, dev);
  891. dev_set_drvdata(dev, ndev);
  892. priv = netdev_priv(ndev);
  893. switch (ops->chip_id) {
  894. case W5100:
  895. priv->s0_regs = W5100_S0_REGS;
  896. priv->s0_tx_buf = W5100_TX_MEM_START;
  897. priv->s0_tx_buf_size = W5100_TX_MEM_SIZE;
  898. priv->s0_rx_buf = W5100_RX_MEM_START;
  899. priv->s0_rx_buf_size = W5100_RX_MEM_SIZE;
  900. break;
  901. case W5200:
  902. priv->s0_regs = W5200_S0_REGS;
  903. priv->s0_tx_buf = W5200_TX_MEM_START;
  904. priv->s0_tx_buf_size = W5200_TX_MEM_SIZE;
  905. priv->s0_rx_buf = W5200_RX_MEM_START;
  906. priv->s0_rx_buf_size = W5200_RX_MEM_SIZE;
  907. break;
  908. case W5500:
  909. priv->s0_regs = W5500_S0_REGS;
  910. priv->s0_tx_buf = W5500_TX_MEM_START;
  911. priv->s0_tx_buf_size = W5500_TX_MEM_SIZE;
  912. priv->s0_rx_buf = W5500_RX_MEM_START;
  913. priv->s0_rx_buf_size = W5500_RX_MEM_SIZE;
  914. break;
  915. default:
  916. err = -EINVAL;
  917. goto err_register;
  918. }
  919. priv->ndev = ndev;
  920. priv->ops = ops;
  921. priv->irq = irq;
  922. priv->link_gpio = link_gpio;
  923. ndev->netdev_ops = &w5100_netdev_ops;
  924. ndev->ethtool_ops = &w5100_ethtool_ops;
  925. netif_napi_add(ndev, &priv->napi, w5100_napi_poll, 16);
  926. /* This chip doesn't support VLAN packets with normal MTU,
  927. * so disable VLAN for this device.
  928. */
  929. ndev->features |= NETIF_F_VLAN_CHALLENGED;
  930. err = register_netdev(ndev);
  931. if (err < 0)
  932. goto err_register;
  933. priv->xfer_wq = alloc_workqueue(netdev_name(ndev), WQ_MEM_RECLAIM, 0);
  934. if (!priv->xfer_wq) {
  935. err = -ENOMEM;
  936. goto err_wq;
  937. }
  938. INIT_WORK(&priv->rx_work, w5100_rx_work);
  939. INIT_WORK(&priv->tx_work, w5100_tx_work);
  940. INIT_WORK(&priv->setrx_work, w5100_setrx_work);
  941. INIT_WORK(&priv->restart_work, w5100_restart_work);
  942. if (mac_addr)
  943. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  944. else
  945. eth_hw_addr_random(ndev);
  946. if (priv->ops->init) {
  947. err = priv->ops->init(priv->ndev);
  948. if (err)
  949. goto err_hw;
  950. }
  951. err = w5100_hw_reset(priv);
  952. if (err)
  953. goto err_hw;
  954. if (ops->may_sleep) {
  955. err = request_threaded_irq(priv->irq, NULL, w5100_interrupt,
  956. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  957. netdev_name(ndev), ndev);
  958. } else {
  959. err = request_irq(priv->irq, w5100_interrupt,
  960. IRQF_TRIGGER_LOW, netdev_name(ndev), ndev);
  961. }
  962. if (err)
  963. goto err_hw;
  964. if (gpio_is_valid(priv->link_gpio)) {
  965. char *link_name = devm_kzalloc(dev, 16, GFP_KERNEL);
  966. if (!link_name) {
  967. err = -ENOMEM;
  968. goto err_gpio;
  969. }
  970. snprintf(link_name, 16, "%s-link", netdev_name(ndev));
  971. priv->link_irq = gpio_to_irq(priv->link_gpio);
  972. if (request_any_context_irq(priv->link_irq, w5100_detect_link,
  973. IRQF_TRIGGER_RISING |
  974. IRQF_TRIGGER_FALLING,
  975. link_name, priv->ndev) < 0)
  976. priv->link_gpio = -EINVAL;
  977. }
  978. return 0;
  979. err_gpio:
  980. free_irq(priv->irq, ndev);
  981. err_hw:
  982. destroy_workqueue(priv->xfer_wq);
  983. err_wq:
  984. unregister_netdev(ndev);
  985. err_register:
  986. free_netdev(ndev);
  987. return err;
  988. }
  989. EXPORT_SYMBOL_GPL(w5100_probe);
  990. int w5100_remove(struct device *dev)
  991. {
  992. struct net_device *ndev = dev_get_drvdata(dev);
  993. struct w5100_priv *priv = netdev_priv(ndev);
  994. w5100_hw_reset(priv);
  995. free_irq(priv->irq, ndev);
  996. if (gpio_is_valid(priv->link_gpio))
  997. free_irq(priv->link_irq, ndev);
  998. flush_work(&priv->setrx_work);
  999. flush_work(&priv->restart_work);
  1000. destroy_workqueue(priv->xfer_wq);
  1001. unregister_netdev(ndev);
  1002. free_netdev(ndev);
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL_GPL(w5100_remove);
  1006. #ifdef CONFIG_PM_SLEEP
  1007. static int w5100_suspend(struct device *dev)
  1008. {
  1009. struct net_device *ndev = dev_get_drvdata(dev);
  1010. struct w5100_priv *priv = netdev_priv(ndev);
  1011. if (netif_running(ndev)) {
  1012. netif_carrier_off(ndev);
  1013. netif_device_detach(ndev);
  1014. w5100_hw_close(priv);
  1015. }
  1016. return 0;
  1017. }
  1018. static int w5100_resume(struct device *dev)
  1019. {
  1020. struct net_device *ndev = dev_get_drvdata(dev);
  1021. struct w5100_priv *priv = netdev_priv(ndev);
  1022. if (netif_running(ndev)) {
  1023. w5100_hw_reset(priv);
  1024. w5100_hw_start(priv);
  1025. netif_device_attach(ndev);
  1026. if (!gpio_is_valid(priv->link_gpio) ||
  1027. gpio_get_value(priv->link_gpio) != 0)
  1028. netif_carrier_on(ndev);
  1029. }
  1030. return 0;
  1031. }
  1032. #endif /* CONFIG_PM_SLEEP */
  1033. SIMPLE_DEV_PM_OPS(w5100_pm_ops, w5100_suspend, w5100_resume);
  1034. EXPORT_SYMBOL_GPL(w5100_pm_ops);
  1035. static struct platform_driver w5100_mmio_driver = {
  1036. .driver = {
  1037. .name = DRV_NAME,
  1038. .pm = &w5100_pm_ops,
  1039. },
  1040. .probe = w5100_mmio_probe,
  1041. .remove = w5100_mmio_remove,
  1042. };
  1043. module_platform_driver(w5100_mmio_driver);