w5300.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Ethernet driver for the WIZnet W5300 chip.
  3. *
  4. * Copyright (C) 2008-2009 WIZnet Co.,Ltd.
  5. * Copyright (C) 2011 Taehun Kim <kth3321 <at> gmail.com>
  6. * Copyright (C) 2012 Mike Sinkovsky <msink@permonline.ru>
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/platform_data/wiznet.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/gpio.h>
  28. #define DRV_NAME "w5300"
  29. #define DRV_VERSION "2012-04-04"
  30. MODULE_DESCRIPTION("WIZnet W5300 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. * Registers
  36. */
  37. #define W5300_MR 0x0000 /* Mode Register */
  38. #define MR_DBW (1 << 15) /* Data bus width */
  39. #define MR_MPF (1 << 14) /* Mac layer pause frame */
  40. #define MR_WDF(n) (((n)&7)<<11) /* Write data fetch time */
  41. #define MR_RDH (1 << 10) /* Read data hold time */
  42. #define MR_FS (1 << 8) /* FIFO swap */
  43. #define MR_RST (1 << 7) /* S/W reset */
  44. #define MR_PB (1 << 4) /* Ping block */
  45. #define MR_DBS (1 << 2) /* Data bus swap */
  46. #define MR_IND (1 << 0) /* Indirect mode */
  47. #define W5300_IR 0x0002 /* Interrupt Register */
  48. #define W5300_IMR 0x0004 /* Interrupt Mask Register */
  49. #define IR_S0 0x0001 /* S0 interrupt */
  50. #define W5300_SHARL 0x0008 /* Source MAC address (0123) */
  51. #define W5300_SHARH 0x000c /* Source MAC address (45) */
  52. #define W5300_TMSRL 0x0020 /* Transmit Memory Size (0123) */
  53. #define W5300_TMSRH 0x0024 /* Transmit Memory Size (4567) */
  54. #define W5300_RMSRL 0x0028 /* Receive Memory Size (0123) */
  55. #define W5300_RMSRH 0x002c /* Receive Memory Size (4567) */
  56. #define W5300_MTYPE 0x0030 /* Memory Type */
  57. #define W5300_IDR 0x00fe /* Chip ID register */
  58. #define IDR_W5300 0x5300 /* =0x5300 for WIZnet W5300 */
  59. #define W5300_S0_MR 0x0200 /* S0 Mode Register */
  60. #define S0_MR_CLOSED 0x0000 /* Close mode */
  61. #define S0_MR_MACRAW 0x0004 /* MAC RAW mode (promiscuous) */
  62. #define S0_MR_MACRAW_MF 0x0044 /* MAC RAW mode (filtered) */
  63. #define W5300_S0_CR 0x0202 /* S0 Command Register */
  64. #define S0_CR_OPEN 0x0001 /* OPEN command */
  65. #define S0_CR_CLOSE 0x0010 /* CLOSE command */
  66. #define S0_CR_SEND 0x0020 /* SEND command */
  67. #define S0_CR_RECV 0x0040 /* RECV command */
  68. #define W5300_S0_IMR 0x0204 /* S0 Interrupt Mask Register */
  69. #define W5300_S0_IR 0x0206 /* S0 Interrupt Register */
  70. #define S0_IR_RECV 0x0004 /* Receive interrupt */
  71. #define S0_IR_SENDOK 0x0010 /* Send OK interrupt */
  72. #define W5300_S0_SSR 0x0208 /* S0 Socket Status Register */
  73. #define W5300_S0_TX_WRSR 0x0220 /* S0 TX Write Size Register */
  74. #define W5300_S0_TX_FSR 0x0224 /* S0 TX Free Size Register */
  75. #define W5300_S0_RX_RSR 0x0228 /* S0 Received data Size */
  76. #define W5300_S0_TX_FIFO 0x022e /* S0 Transmit FIFO */
  77. #define W5300_S0_RX_FIFO 0x0230 /* S0 Receive FIFO */
  78. #define W5300_REGS_LEN 0x0400
  79. /*
  80. * Device driver private data structure
  81. */
  82. struct w5300_priv {
  83. void __iomem *base;
  84. spinlock_t reg_lock;
  85. bool indirect;
  86. u16 (*read) (struct w5300_priv *priv, u16 addr);
  87. void (*write)(struct w5300_priv *priv, u16 addr, u16 data);
  88. int irq;
  89. int link_irq;
  90. int link_gpio;
  91. struct napi_struct napi;
  92. struct net_device *ndev;
  93. bool promisc;
  94. u32 msg_enable;
  95. };
  96. /************************************************************************
  97. *
  98. * Lowlevel I/O functions
  99. *
  100. ***********************************************************************/
  101. /*
  102. * In direct address mode host system can directly access W5300 registers
  103. * after mapping to Memory-Mapped I/O space.
  104. *
  105. * 0x400 bytes are required for memory space.
  106. */
  107. static inline u16 w5300_read_direct(struct w5300_priv *priv, u16 addr)
  108. {
  109. return ioread16(priv->base + (addr << CONFIG_WIZNET_BUS_SHIFT));
  110. }
  111. static inline void w5300_write_direct(struct w5300_priv *priv,
  112. u16 addr, u16 data)
  113. {
  114. iowrite16(data, priv->base + (addr << CONFIG_WIZNET_BUS_SHIFT));
  115. }
  116. /*
  117. * In indirect address mode host system indirectly accesses registers by
  118. * using Indirect Mode Address Register (IDM_AR) and Indirect Mode Data
  119. * Register (IDM_DR), which are directly mapped to Memory-Mapped I/O space.
  120. * Mode Register (MR) is directly accessible.
  121. *
  122. * Only 0x06 bytes are required for memory space.
  123. */
  124. #define W5300_IDM_AR 0x0002 /* Indirect Mode Address */
  125. #define W5300_IDM_DR 0x0004 /* Indirect Mode Data */
  126. static u16 w5300_read_indirect(struct w5300_priv *priv, u16 addr)
  127. {
  128. unsigned long flags;
  129. u16 data;
  130. spin_lock_irqsave(&priv->reg_lock, flags);
  131. w5300_write_direct(priv, W5300_IDM_AR, addr);
  132. mmiowb();
  133. data = w5300_read_direct(priv, W5300_IDM_DR);
  134. spin_unlock_irqrestore(&priv->reg_lock, flags);
  135. return data;
  136. }
  137. static void w5300_write_indirect(struct w5300_priv *priv, u16 addr, u16 data)
  138. {
  139. unsigned long flags;
  140. spin_lock_irqsave(&priv->reg_lock, flags);
  141. w5300_write_direct(priv, W5300_IDM_AR, addr);
  142. mmiowb();
  143. w5300_write_direct(priv, W5300_IDM_DR, data);
  144. mmiowb();
  145. spin_unlock_irqrestore(&priv->reg_lock, flags);
  146. }
  147. #if defined(CONFIG_WIZNET_BUS_DIRECT)
  148. #define w5300_read w5300_read_direct
  149. #define w5300_write w5300_write_direct
  150. #elif defined(CONFIG_WIZNET_BUS_INDIRECT)
  151. #define w5300_read w5300_read_indirect
  152. #define w5300_write w5300_write_indirect
  153. #else /* CONFIG_WIZNET_BUS_ANY */
  154. #define w5300_read priv->read
  155. #define w5300_write priv->write
  156. #endif
  157. static u32 w5300_read32(struct w5300_priv *priv, u16 addr)
  158. {
  159. u32 data;
  160. data = w5300_read(priv, addr) << 16;
  161. data |= w5300_read(priv, addr + 2);
  162. return data;
  163. }
  164. static void w5300_write32(struct w5300_priv *priv, u16 addr, u32 data)
  165. {
  166. w5300_write(priv, addr, data >> 16);
  167. w5300_write(priv, addr + 2, data);
  168. }
  169. static int w5300_command(struct w5300_priv *priv, u16 cmd)
  170. {
  171. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  172. w5300_write(priv, W5300_S0_CR, cmd);
  173. mmiowb();
  174. while (w5300_read(priv, W5300_S0_CR) != 0) {
  175. if (time_after(jiffies, timeout))
  176. return -EIO;
  177. cpu_relax();
  178. }
  179. return 0;
  180. }
  181. static void w5300_read_frame(struct w5300_priv *priv, u8 *buf, int len)
  182. {
  183. u16 fifo;
  184. int i;
  185. for (i = 0; i < len; i += 2) {
  186. fifo = w5300_read(priv, W5300_S0_RX_FIFO);
  187. *buf++ = fifo >> 8;
  188. *buf++ = fifo;
  189. }
  190. fifo = w5300_read(priv, W5300_S0_RX_FIFO);
  191. fifo = w5300_read(priv, W5300_S0_RX_FIFO);
  192. }
  193. static void w5300_write_frame(struct w5300_priv *priv, u8 *buf, int len)
  194. {
  195. u16 fifo;
  196. int i;
  197. for (i = 0; i < len; i += 2) {
  198. fifo = *buf++ << 8;
  199. fifo |= *buf++;
  200. w5300_write(priv, W5300_S0_TX_FIFO, fifo);
  201. }
  202. w5300_write32(priv, W5300_S0_TX_WRSR, len);
  203. }
  204. static void w5300_write_macaddr(struct w5300_priv *priv)
  205. {
  206. struct net_device *ndev = priv->ndev;
  207. w5300_write32(priv, W5300_SHARL,
  208. ndev->dev_addr[0] << 24 |
  209. ndev->dev_addr[1] << 16 |
  210. ndev->dev_addr[2] << 8 |
  211. ndev->dev_addr[3]);
  212. w5300_write(priv, W5300_SHARH,
  213. ndev->dev_addr[4] << 8 |
  214. ndev->dev_addr[5]);
  215. mmiowb();
  216. }
  217. static void w5300_hw_reset(struct w5300_priv *priv)
  218. {
  219. w5300_write_direct(priv, W5300_MR, MR_RST);
  220. mmiowb();
  221. mdelay(5);
  222. w5300_write_direct(priv, W5300_MR, priv->indirect ?
  223. MR_WDF(7) | MR_PB | MR_IND :
  224. MR_WDF(7) | MR_PB);
  225. mmiowb();
  226. w5300_write(priv, W5300_IMR, 0);
  227. w5300_write_macaddr(priv);
  228. /* Configure 128K of internal memory
  229. * as 64K RX fifo and 64K TX fifo
  230. */
  231. w5300_write32(priv, W5300_RMSRL, 64 << 24);
  232. w5300_write32(priv, W5300_RMSRH, 0);
  233. w5300_write32(priv, W5300_TMSRL, 64 << 24);
  234. w5300_write32(priv, W5300_TMSRH, 0);
  235. w5300_write(priv, W5300_MTYPE, 0x00ff);
  236. mmiowb();
  237. }
  238. static void w5300_hw_start(struct w5300_priv *priv)
  239. {
  240. w5300_write(priv, W5300_S0_MR, priv->promisc ?
  241. S0_MR_MACRAW : S0_MR_MACRAW_MF);
  242. mmiowb();
  243. w5300_command(priv, S0_CR_OPEN);
  244. w5300_write(priv, W5300_S0_IMR, S0_IR_RECV | S0_IR_SENDOK);
  245. w5300_write(priv, W5300_IMR, IR_S0);
  246. mmiowb();
  247. }
  248. static void w5300_hw_close(struct w5300_priv *priv)
  249. {
  250. w5300_write(priv, W5300_IMR, 0);
  251. mmiowb();
  252. w5300_command(priv, S0_CR_CLOSE);
  253. }
  254. /***********************************************************************
  255. *
  256. * Device driver functions / callbacks
  257. *
  258. ***********************************************************************/
  259. static void w5300_get_drvinfo(struct net_device *ndev,
  260. struct ethtool_drvinfo *info)
  261. {
  262. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  263. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  264. strlcpy(info->bus_info, dev_name(ndev->dev.parent),
  265. sizeof(info->bus_info));
  266. }
  267. static u32 w5300_get_link(struct net_device *ndev)
  268. {
  269. struct w5300_priv *priv = netdev_priv(ndev);
  270. if (gpio_is_valid(priv->link_gpio))
  271. return !!gpio_get_value(priv->link_gpio);
  272. return 1;
  273. }
  274. static u32 w5300_get_msglevel(struct net_device *ndev)
  275. {
  276. struct w5300_priv *priv = netdev_priv(ndev);
  277. return priv->msg_enable;
  278. }
  279. static void w5300_set_msglevel(struct net_device *ndev, u32 value)
  280. {
  281. struct w5300_priv *priv = netdev_priv(ndev);
  282. priv->msg_enable = value;
  283. }
  284. static int w5300_get_regs_len(struct net_device *ndev)
  285. {
  286. return W5300_REGS_LEN;
  287. }
  288. static void w5300_get_regs(struct net_device *ndev,
  289. struct ethtool_regs *regs, void *_buf)
  290. {
  291. struct w5300_priv *priv = netdev_priv(ndev);
  292. u8 *buf = _buf;
  293. u16 addr;
  294. u16 data;
  295. regs->version = 1;
  296. for (addr = 0; addr < W5300_REGS_LEN; addr += 2) {
  297. switch (addr & 0x23f) {
  298. case W5300_S0_TX_FIFO: /* cannot read TX_FIFO */
  299. case W5300_S0_RX_FIFO: /* cannot read RX_FIFO */
  300. data = 0xffff;
  301. break;
  302. default:
  303. data = w5300_read(priv, addr);
  304. break;
  305. }
  306. *buf++ = data >> 8;
  307. *buf++ = data;
  308. }
  309. }
  310. static void w5300_tx_timeout(struct net_device *ndev)
  311. {
  312. struct w5300_priv *priv = netdev_priv(ndev);
  313. netif_stop_queue(ndev);
  314. w5300_hw_reset(priv);
  315. w5300_hw_start(priv);
  316. ndev->stats.tx_errors++;
  317. netif_trans_update(ndev);
  318. netif_wake_queue(ndev);
  319. }
  320. static int w5300_start_tx(struct sk_buff *skb, struct net_device *ndev)
  321. {
  322. struct w5300_priv *priv = netdev_priv(ndev);
  323. netif_stop_queue(ndev);
  324. w5300_write_frame(priv, skb->data, skb->len);
  325. mmiowb();
  326. ndev->stats.tx_packets++;
  327. ndev->stats.tx_bytes += skb->len;
  328. dev_kfree_skb(skb);
  329. netif_dbg(priv, tx_queued, ndev, "tx queued\n");
  330. w5300_command(priv, S0_CR_SEND);
  331. return NETDEV_TX_OK;
  332. }
  333. static int w5300_napi_poll(struct napi_struct *napi, int budget)
  334. {
  335. struct w5300_priv *priv = container_of(napi, struct w5300_priv, napi);
  336. struct net_device *ndev = priv->ndev;
  337. struct sk_buff *skb;
  338. int rx_count;
  339. u16 rx_len;
  340. for (rx_count = 0; rx_count < budget; rx_count++) {
  341. u32 rx_fifo_len = w5300_read32(priv, W5300_S0_RX_RSR);
  342. if (rx_fifo_len == 0)
  343. break;
  344. rx_len = w5300_read(priv, W5300_S0_RX_FIFO);
  345. skb = netdev_alloc_skb_ip_align(ndev, roundup(rx_len, 2));
  346. if (unlikely(!skb)) {
  347. u32 i;
  348. for (i = 0; i < rx_fifo_len; i += 2)
  349. w5300_read(priv, W5300_S0_RX_FIFO);
  350. ndev->stats.rx_dropped++;
  351. return -ENOMEM;
  352. }
  353. skb_put(skb, rx_len);
  354. w5300_read_frame(priv, skb->data, rx_len);
  355. skb->protocol = eth_type_trans(skb, ndev);
  356. netif_receive_skb(skb);
  357. ndev->stats.rx_packets++;
  358. ndev->stats.rx_bytes += rx_len;
  359. }
  360. if (rx_count < budget) {
  361. napi_complete(napi);
  362. w5300_write(priv, W5300_IMR, IR_S0);
  363. mmiowb();
  364. }
  365. return rx_count;
  366. }
  367. static irqreturn_t w5300_interrupt(int irq, void *ndev_instance)
  368. {
  369. struct net_device *ndev = ndev_instance;
  370. struct w5300_priv *priv = netdev_priv(ndev);
  371. int ir = w5300_read(priv, W5300_S0_IR);
  372. if (!ir)
  373. return IRQ_NONE;
  374. w5300_write(priv, W5300_S0_IR, ir);
  375. mmiowb();
  376. if (ir & S0_IR_SENDOK) {
  377. netif_dbg(priv, tx_done, ndev, "tx done\n");
  378. netif_wake_queue(ndev);
  379. }
  380. if (ir & S0_IR_RECV) {
  381. if (napi_schedule_prep(&priv->napi)) {
  382. w5300_write(priv, W5300_IMR, 0);
  383. mmiowb();
  384. __napi_schedule(&priv->napi);
  385. }
  386. }
  387. return IRQ_HANDLED;
  388. }
  389. static irqreturn_t w5300_detect_link(int irq, void *ndev_instance)
  390. {
  391. struct net_device *ndev = ndev_instance;
  392. struct w5300_priv *priv = netdev_priv(ndev);
  393. if (netif_running(ndev)) {
  394. if (gpio_get_value(priv->link_gpio) != 0) {
  395. netif_info(priv, link, ndev, "link is up\n");
  396. netif_carrier_on(ndev);
  397. } else {
  398. netif_info(priv, link, ndev, "link is down\n");
  399. netif_carrier_off(ndev);
  400. }
  401. }
  402. return IRQ_HANDLED;
  403. }
  404. static void w5300_set_rx_mode(struct net_device *ndev)
  405. {
  406. struct w5300_priv *priv = netdev_priv(ndev);
  407. bool set_promisc = (ndev->flags & IFF_PROMISC) != 0;
  408. if (priv->promisc != set_promisc) {
  409. priv->promisc = set_promisc;
  410. w5300_hw_start(priv);
  411. }
  412. }
  413. static int w5300_set_macaddr(struct net_device *ndev, void *addr)
  414. {
  415. struct w5300_priv *priv = netdev_priv(ndev);
  416. struct sockaddr *sock_addr = addr;
  417. if (!is_valid_ether_addr(sock_addr->sa_data))
  418. return -EADDRNOTAVAIL;
  419. memcpy(ndev->dev_addr, sock_addr->sa_data, ETH_ALEN);
  420. w5300_write_macaddr(priv);
  421. return 0;
  422. }
  423. static int w5300_open(struct net_device *ndev)
  424. {
  425. struct w5300_priv *priv = netdev_priv(ndev);
  426. netif_info(priv, ifup, ndev, "enabling\n");
  427. w5300_hw_start(priv);
  428. napi_enable(&priv->napi);
  429. netif_start_queue(ndev);
  430. if (!gpio_is_valid(priv->link_gpio) ||
  431. gpio_get_value(priv->link_gpio) != 0)
  432. netif_carrier_on(ndev);
  433. return 0;
  434. }
  435. static int w5300_stop(struct net_device *ndev)
  436. {
  437. struct w5300_priv *priv = netdev_priv(ndev);
  438. netif_info(priv, ifdown, ndev, "shutting down\n");
  439. w5300_hw_close(priv);
  440. netif_carrier_off(ndev);
  441. netif_stop_queue(ndev);
  442. napi_disable(&priv->napi);
  443. return 0;
  444. }
  445. static const struct ethtool_ops w5300_ethtool_ops = {
  446. .get_drvinfo = w5300_get_drvinfo,
  447. .get_msglevel = w5300_get_msglevel,
  448. .set_msglevel = w5300_set_msglevel,
  449. .get_link = w5300_get_link,
  450. .get_regs_len = w5300_get_regs_len,
  451. .get_regs = w5300_get_regs,
  452. };
  453. static const struct net_device_ops w5300_netdev_ops = {
  454. .ndo_open = w5300_open,
  455. .ndo_stop = w5300_stop,
  456. .ndo_start_xmit = w5300_start_tx,
  457. .ndo_tx_timeout = w5300_tx_timeout,
  458. .ndo_set_rx_mode = w5300_set_rx_mode,
  459. .ndo_set_mac_address = w5300_set_macaddr,
  460. .ndo_validate_addr = eth_validate_addr,
  461. .ndo_change_mtu = eth_change_mtu,
  462. };
  463. static int w5300_hw_probe(struct platform_device *pdev)
  464. {
  465. struct wiznet_platform_data *data = dev_get_platdata(&pdev->dev);
  466. struct net_device *ndev = platform_get_drvdata(pdev);
  467. struct w5300_priv *priv = netdev_priv(ndev);
  468. const char *name = netdev_name(ndev);
  469. struct resource *mem;
  470. int mem_size;
  471. int irq;
  472. int ret;
  473. if (data && is_valid_ether_addr(data->mac_addr)) {
  474. memcpy(ndev->dev_addr, data->mac_addr, ETH_ALEN);
  475. } else {
  476. eth_hw_addr_random(ndev);
  477. }
  478. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  479. priv->base = devm_ioremap_resource(&pdev->dev, mem);
  480. if (IS_ERR(priv->base))
  481. return PTR_ERR(priv->base);
  482. mem_size = resource_size(mem);
  483. spin_lock_init(&priv->reg_lock);
  484. priv->indirect = mem_size < W5300_BUS_DIRECT_SIZE;
  485. if (priv->indirect) {
  486. priv->read = w5300_read_indirect;
  487. priv->write = w5300_write_indirect;
  488. } else {
  489. priv->read = w5300_read_direct;
  490. priv->write = w5300_write_direct;
  491. }
  492. w5300_hw_reset(priv);
  493. if (w5300_read(priv, W5300_IDR) != IDR_W5300)
  494. return -ENODEV;
  495. irq = platform_get_irq(pdev, 0);
  496. if (irq < 0)
  497. return irq;
  498. ret = request_irq(irq, w5300_interrupt,
  499. IRQ_TYPE_LEVEL_LOW, name, ndev);
  500. if (ret < 0)
  501. return ret;
  502. priv->irq = irq;
  503. priv->link_gpio = data ? data->link_gpio : -EINVAL;
  504. if (gpio_is_valid(priv->link_gpio)) {
  505. char *link_name = devm_kzalloc(&pdev->dev, 16, GFP_KERNEL);
  506. if (!link_name)
  507. return -ENOMEM;
  508. snprintf(link_name, 16, "%s-link", name);
  509. priv->link_irq = gpio_to_irq(priv->link_gpio);
  510. if (request_any_context_irq(priv->link_irq, w5300_detect_link,
  511. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  512. link_name, priv->ndev) < 0)
  513. priv->link_gpio = -EINVAL;
  514. }
  515. netdev_info(ndev, "at 0x%llx irq %d\n", (u64)mem->start, irq);
  516. return 0;
  517. }
  518. static int w5300_probe(struct platform_device *pdev)
  519. {
  520. struct w5300_priv *priv;
  521. struct net_device *ndev;
  522. int err;
  523. ndev = alloc_etherdev(sizeof(*priv));
  524. if (!ndev)
  525. return -ENOMEM;
  526. SET_NETDEV_DEV(ndev, &pdev->dev);
  527. platform_set_drvdata(pdev, ndev);
  528. priv = netdev_priv(ndev);
  529. priv->ndev = ndev;
  530. ndev->netdev_ops = &w5300_netdev_ops;
  531. ndev->ethtool_ops = &w5300_ethtool_ops;
  532. ndev->watchdog_timeo = HZ;
  533. netif_napi_add(ndev, &priv->napi, w5300_napi_poll, 16);
  534. /* This chip doesn't support VLAN packets with normal MTU,
  535. * so disable VLAN for this device.
  536. */
  537. ndev->features |= NETIF_F_VLAN_CHALLENGED;
  538. err = register_netdev(ndev);
  539. if (err < 0)
  540. goto err_register;
  541. err = w5300_hw_probe(pdev);
  542. if (err < 0)
  543. goto err_hw_probe;
  544. return 0;
  545. err_hw_probe:
  546. unregister_netdev(ndev);
  547. err_register:
  548. free_netdev(ndev);
  549. return err;
  550. }
  551. static int w5300_remove(struct platform_device *pdev)
  552. {
  553. struct net_device *ndev = platform_get_drvdata(pdev);
  554. struct w5300_priv *priv = netdev_priv(ndev);
  555. w5300_hw_reset(priv);
  556. free_irq(priv->irq, ndev);
  557. if (gpio_is_valid(priv->link_gpio))
  558. free_irq(priv->link_irq, ndev);
  559. unregister_netdev(ndev);
  560. free_netdev(ndev);
  561. return 0;
  562. }
  563. #ifdef CONFIG_PM_SLEEP
  564. static int w5300_suspend(struct device *dev)
  565. {
  566. struct platform_device *pdev = to_platform_device(dev);
  567. struct net_device *ndev = platform_get_drvdata(pdev);
  568. struct w5300_priv *priv = netdev_priv(ndev);
  569. if (netif_running(ndev)) {
  570. netif_carrier_off(ndev);
  571. netif_device_detach(ndev);
  572. w5300_hw_close(priv);
  573. }
  574. return 0;
  575. }
  576. static int w5300_resume(struct device *dev)
  577. {
  578. struct platform_device *pdev = to_platform_device(dev);
  579. struct net_device *ndev = platform_get_drvdata(pdev);
  580. struct w5300_priv *priv = netdev_priv(ndev);
  581. if (!netif_running(ndev)) {
  582. w5300_hw_reset(priv);
  583. w5300_hw_start(priv);
  584. netif_device_attach(ndev);
  585. if (!gpio_is_valid(priv->link_gpio) ||
  586. gpio_get_value(priv->link_gpio) != 0)
  587. netif_carrier_on(ndev);
  588. }
  589. return 0;
  590. }
  591. #endif /* CONFIG_PM_SLEEP */
  592. static SIMPLE_DEV_PM_OPS(w5300_pm_ops, w5300_suspend, w5300_resume);
  593. static struct platform_driver w5300_driver = {
  594. .driver = {
  595. .name = DRV_NAME,
  596. .pm = &w5300_pm_ops,
  597. },
  598. .probe = w5300_probe,
  599. .remove = w5300_remove,
  600. };
  601. module_platform_driver(w5300_driver);