enc28j60.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /*
  2. * Microchip ENC28J60 ethernet driver (MAC + PHY)
  3. *
  4. * Copyright (C) 2007 Eurek srl
  5. * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com>
  6. * based on enc28j60.c written by David Anders for 2.4 kernel version
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * $Id: enc28j60.c,v 1.22 2007/12/20 10:47:01 claudio Exp $
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/tcp.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/delay.h>
  30. #include <linux/spi/spi.h>
  31. #include "enc28j60_hw.h"
  32. #define DRV_NAME "enc28j60"
  33. #define DRV_VERSION "1.01"
  34. #define SPI_OPLEN 1
  35. #define ENC28J60_MSG_DEFAULT \
  36. (NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_LINK)
  37. /* Buffer size required for the largest SPI transfer (i.e., reading a
  38. * frame). */
  39. #define SPI_TRANSFER_BUF_LEN (4 + MAX_FRAMELEN)
  40. #define TX_TIMEOUT (4 * HZ)
  41. /* Max TX retries in case of collision as suggested by errata datasheet */
  42. #define MAX_TX_RETRYCOUNT 16
  43. enum {
  44. RXFILTER_NORMAL,
  45. RXFILTER_MULTI,
  46. RXFILTER_PROMISC
  47. };
  48. /* Driver local data */
  49. struct enc28j60_net {
  50. struct net_device *netdev;
  51. struct spi_device *spi;
  52. struct mutex lock;
  53. struct sk_buff *tx_skb;
  54. struct work_struct tx_work;
  55. struct work_struct irq_work;
  56. struct work_struct setrx_work;
  57. struct work_struct restart_work;
  58. u8 bank; /* current register bank selected */
  59. u16 next_pk_ptr; /* next packet pointer within FIFO */
  60. u16 max_pk_counter; /* statistics: max packet counter */
  61. u16 tx_retry_count;
  62. bool hw_enable;
  63. bool full_duplex;
  64. int rxfilter;
  65. u32 msg_enable;
  66. u8 spi_transfer_buf[SPI_TRANSFER_BUF_LEN];
  67. };
  68. /* use ethtool to change the level for any given device */
  69. static struct {
  70. u32 msg_enable;
  71. } debug = { -1 };
  72. /*
  73. * SPI read buffer
  74. * wait for the SPI transfer and copy received data to destination
  75. */
  76. static int
  77. spi_read_buf(struct enc28j60_net *priv, int len, u8 *data)
  78. {
  79. u8 *rx_buf = priv->spi_transfer_buf + 4;
  80. u8 *tx_buf = priv->spi_transfer_buf;
  81. struct spi_transfer t = {
  82. .tx_buf = tx_buf,
  83. .rx_buf = rx_buf,
  84. .len = SPI_OPLEN + len,
  85. };
  86. struct spi_message msg;
  87. int ret;
  88. tx_buf[0] = ENC28J60_READ_BUF_MEM;
  89. tx_buf[1] = tx_buf[2] = tx_buf[3] = 0; /* don't care */
  90. spi_message_init(&msg);
  91. spi_message_add_tail(&t, &msg);
  92. ret = spi_sync(priv->spi, &msg);
  93. if (ret == 0) {
  94. memcpy(data, &rx_buf[SPI_OPLEN], len);
  95. ret = msg.status;
  96. }
  97. if (ret && netif_msg_drv(priv))
  98. printk(KERN_DEBUG DRV_NAME ": %s() failed: ret = %d\n",
  99. __func__, ret);
  100. return ret;
  101. }
  102. /*
  103. * SPI write buffer
  104. */
  105. static int spi_write_buf(struct enc28j60_net *priv, int len,
  106. const u8 *data)
  107. {
  108. int ret;
  109. if (len > SPI_TRANSFER_BUF_LEN - 1 || len <= 0)
  110. ret = -EINVAL;
  111. else {
  112. priv->spi_transfer_buf[0] = ENC28J60_WRITE_BUF_MEM;
  113. memcpy(&priv->spi_transfer_buf[1], data, len);
  114. ret = spi_write(priv->spi, priv->spi_transfer_buf, len + 1);
  115. if (ret && netif_msg_drv(priv))
  116. printk(KERN_DEBUG DRV_NAME ": %s() failed: ret = %d\n",
  117. __func__, ret);
  118. }
  119. return ret;
  120. }
  121. /*
  122. * basic SPI read operation
  123. */
  124. static u8 spi_read_op(struct enc28j60_net *priv, u8 op,
  125. u8 addr)
  126. {
  127. u8 tx_buf[2];
  128. u8 rx_buf[4];
  129. u8 val = 0;
  130. int ret;
  131. int slen = SPI_OPLEN;
  132. /* do dummy read if needed */
  133. if (addr & SPRD_MASK)
  134. slen++;
  135. tx_buf[0] = op | (addr & ADDR_MASK);
  136. ret = spi_write_then_read(priv->spi, tx_buf, 1, rx_buf, slen);
  137. if (ret)
  138. printk(KERN_DEBUG DRV_NAME ": %s() failed: ret = %d\n",
  139. __func__, ret);
  140. else
  141. val = rx_buf[slen - 1];
  142. return val;
  143. }
  144. /*
  145. * basic SPI write operation
  146. */
  147. static int spi_write_op(struct enc28j60_net *priv, u8 op,
  148. u8 addr, u8 val)
  149. {
  150. int ret;
  151. priv->spi_transfer_buf[0] = op | (addr & ADDR_MASK);
  152. priv->spi_transfer_buf[1] = val;
  153. ret = spi_write(priv->spi, priv->spi_transfer_buf, 2);
  154. if (ret && netif_msg_drv(priv))
  155. printk(KERN_DEBUG DRV_NAME ": %s() failed: ret = %d\n",
  156. __func__, ret);
  157. return ret;
  158. }
  159. static void enc28j60_soft_reset(struct enc28j60_net *priv)
  160. {
  161. if (netif_msg_hw(priv))
  162. printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __func__);
  163. spi_write_op(priv, ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
  164. /* Errata workaround #1, CLKRDY check is unreliable,
  165. * delay at least 1 mS instead */
  166. udelay(2000);
  167. }
  168. /*
  169. * select the current register bank if necessary
  170. */
  171. static void enc28j60_set_bank(struct enc28j60_net *priv, u8 addr)
  172. {
  173. if ((addr & BANK_MASK) != priv->bank) {
  174. u8 b = (addr & BANK_MASK) >> 5;
  175. if (b != (ECON1_BSEL1 | ECON1_BSEL0))
  176. spi_write_op(priv, ENC28J60_BIT_FIELD_CLR, ECON1,
  177. ECON1_BSEL1 | ECON1_BSEL0);
  178. if (b != 0)
  179. spi_write_op(priv, ENC28J60_BIT_FIELD_SET, ECON1, b);
  180. priv->bank = (addr & BANK_MASK);
  181. }
  182. }
  183. /*
  184. * Register access routines through the SPI bus.
  185. * Every register access comes in two flavours:
  186. * - nolock_xxx: caller needs to invoke mutex_lock, usually to access
  187. * atomically more than one register
  188. * - locked_xxx: caller doesn't need to invoke mutex_lock, single access
  189. *
  190. * Some registers can be accessed through the bit field clear and
  191. * bit field set to avoid a read modify write cycle.
  192. */
  193. /*
  194. * Register bit field Set
  195. */
  196. static void nolock_reg_bfset(struct enc28j60_net *priv,
  197. u8 addr, u8 mask)
  198. {
  199. enc28j60_set_bank(priv, addr);
  200. spi_write_op(priv, ENC28J60_BIT_FIELD_SET, addr, mask);
  201. }
  202. static void locked_reg_bfset(struct enc28j60_net *priv,
  203. u8 addr, u8 mask)
  204. {
  205. mutex_lock(&priv->lock);
  206. nolock_reg_bfset(priv, addr, mask);
  207. mutex_unlock(&priv->lock);
  208. }
  209. /*
  210. * Register bit field Clear
  211. */
  212. static void nolock_reg_bfclr(struct enc28j60_net *priv,
  213. u8 addr, u8 mask)
  214. {
  215. enc28j60_set_bank(priv, addr);
  216. spi_write_op(priv, ENC28J60_BIT_FIELD_CLR, addr, mask);
  217. }
  218. static void locked_reg_bfclr(struct enc28j60_net *priv,
  219. u8 addr, u8 mask)
  220. {
  221. mutex_lock(&priv->lock);
  222. nolock_reg_bfclr(priv, addr, mask);
  223. mutex_unlock(&priv->lock);
  224. }
  225. /*
  226. * Register byte read
  227. */
  228. static int nolock_regb_read(struct enc28j60_net *priv,
  229. u8 address)
  230. {
  231. enc28j60_set_bank(priv, address);
  232. return spi_read_op(priv, ENC28J60_READ_CTRL_REG, address);
  233. }
  234. static int locked_regb_read(struct enc28j60_net *priv,
  235. u8 address)
  236. {
  237. int ret;
  238. mutex_lock(&priv->lock);
  239. ret = nolock_regb_read(priv, address);
  240. mutex_unlock(&priv->lock);
  241. return ret;
  242. }
  243. /*
  244. * Register word read
  245. */
  246. static int nolock_regw_read(struct enc28j60_net *priv,
  247. u8 address)
  248. {
  249. int rl, rh;
  250. enc28j60_set_bank(priv, address);
  251. rl = spi_read_op(priv, ENC28J60_READ_CTRL_REG, address);
  252. rh = spi_read_op(priv, ENC28J60_READ_CTRL_REG, address + 1);
  253. return (rh << 8) | rl;
  254. }
  255. static int locked_regw_read(struct enc28j60_net *priv,
  256. u8 address)
  257. {
  258. int ret;
  259. mutex_lock(&priv->lock);
  260. ret = nolock_regw_read(priv, address);
  261. mutex_unlock(&priv->lock);
  262. return ret;
  263. }
  264. /*
  265. * Register byte write
  266. */
  267. static void nolock_regb_write(struct enc28j60_net *priv,
  268. u8 address, u8 data)
  269. {
  270. enc28j60_set_bank(priv, address);
  271. spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address, data);
  272. }
  273. static void locked_regb_write(struct enc28j60_net *priv,
  274. u8 address, u8 data)
  275. {
  276. mutex_lock(&priv->lock);
  277. nolock_regb_write(priv, address, data);
  278. mutex_unlock(&priv->lock);
  279. }
  280. /*
  281. * Register word write
  282. */
  283. static void nolock_regw_write(struct enc28j60_net *priv,
  284. u8 address, u16 data)
  285. {
  286. enc28j60_set_bank(priv, address);
  287. spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address, (u8) data);
  288. spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, address + 1,
  289. (u8) (data >> 8));
  290. }
  291. static void locked_regw_write(struct enc28j60_net *priv,
  292. u8 address, u16 data)
  293. {
  294. mutex_lock(&priv->lock);
  295. nolock_regw_write(priv, address, data);
  296. mutex_unlock(&priv->lock);
  297. }
  298. /*
  299. * Buffer memory read
  300. * Select the starting address and execute a SPI buffer read
  301. */
  302. static void enc28j60_mem_read(struct enc28j60_net *priv,
  303. u16 addr, int len, u8 *data)
  304. {
  305. mutex_lock(&priv->lock);
  306. nolock_regw_write(priv, ERDPTL, addr);
  307. #ifdef CONFIG_ENC28J60_WRITEVERIFY
  308. if (netif_msg_drv(priv)) {
  309. u16 reg;
  310. reg = nolock_regw_read(priv, ERDPTL);
  311. if (reg != addr)
  312. printk(KERN_DEBUG DRV_NAME ": %s() error writing ERDPT "
  313. "(0x%04x - 0x%04x)\n", __func__, reg, addr);
  314. }
  315. #endif
  316. spi_read_buf(priv, len, data);
  317. mutex_unlock(&priv->lock);
  318. }
  319. /*
  320. * Write packet to enc28j60 TX buffer memory
  321. */
  322. static void
  323. enc28j60_packet_write(struct enc28j60_net *priv, int len, const u8 *data)
  324. {
  325. mutex_lock(&priv->lock);
  326. /* Set the write pointer to start of transmit buffer area */
  327. nolock_regw_write(priv, EWRPTL, TXSTART_INIT);
  328. #ifdef CONFIG_ENC28J60_WRITEVERIFY
  329. if (netif_msg_drv(priv)) {
  330. u16 reg;
  331. reg = nolock_regw_read(priv, EWRPTL);
  332. if (reg != TXSTART_INIT)
  333. printk(KERN_DEBUG DRV_NAME
  334. ": %s() ERWPT:0x%04x != 0x%04x\n",
  335. __func__, reg, TXSTART_INIT);
  336. }
  337. #endif
  338. /* Set the TXND pointer to correspond to the packet size given */
  339. nolock_regw_write(priv, ETXNDL, TXSTART_INIT + len);
  340. /* write per-packet control byte */
  341. spi_write_op(priv, ENC28J60_WRITE_BUF_MEM, 0, 0x00);
  342. if (netif_msg_hw(priv))
  343. printk(KERN_DEBUG DRV_NAME
  344. ": %s() after control byte ERWPT:0x%04x\n",
  345. __func__, nolock_regw_read(priv, EWRPTL));
  346. /* copy the packet into the transmit buffer */
  347. spi_write_buf(priv, len, data);
  348. if (netif_msg_hw(priv))
  349. printk(KERN_DEBUG DRV_NAME
  350. ": %s() after write packet ERWPT:0x%04x, len=%d\n",
  351. __func__, nolock_regw_read(priv, EWRPTL), len);
  352. mutex_unlock(&priv->lock);
  353. }
  354. static unsigned long msec20_to_jiffies;
  355. static int poll_ready(struct enc28j60_net *priv, u8 reg, u8 mask, u8 val)
  356. {
  357. unsigned long timeout = jiffies + msec20_to_jiffies;
  358. /* 20 msec timeout read */
  359. while ((nolock_regb_read(priv, reg) & mask) != val) {
  360. if (time_after(jiffies, timeout)) {
  361. if (netif_msg_drv(priv))
  362. dev_dbg(&priv->spi->dev,
  363. "reg %02x ready timeout!\n", reg);
  364. return -ETIMEDOUT;
  365. }
  366. cpu_relax();
  367. }
  368. return 0;
  369. }
  370. /*
  371. * Wait until the PHY operation is complete.
  372. */
  373. static int wait_phy_ready(struct enc28j60_net *priv)
  374. {
  375. return poll_ready(priv, MISTAT, MISTAT_BUSY, 0) ? 0 : 1;
  376. }
  377. /*
  378. * PHY register read
  379. * PHY registers are not accessed directly, but through the MII
  380. */
  381. static u16 enc28j60_phy_read(struct enc28j60_net *priv, u8 address)
  382. {
  383. u16 ret;
  384. mutex_lock(&priv->lock);
  385. /* set the PHY register address */
  386. nolock_regb_write(priv, MIREGADR, address);
  387. /* start the register read operation */
  388. nolock_regb_write(priv, MICMD, MICMD_MIIRD);
  389. /* wait until the PHY read completes */
  390. wait_phy_ready(priv);
  391. /* quit reading */
  392. nolock_regb_write(priv, MICMD, 0x00);
  393. /* return the data */
  394. ret = nolock_regw_read(priv, MIRDL);
  395. mutex_unlock(&priv->lock);
  396. return ret;
  397. }
  398. static int enc28j60_phy_write(struct enc28j60_net *priv, u8 address, u16 data)
  399. {
  400. int ret;
  401. mutex_lock(&priv->lock);
  402. /* set the PHY register address */
  403. nolock_regb_write(priv, MIREGADR, address);
  404. /* write the PHY data */
  405. nolock_regw_write(priv, MIWRL, data);
  406. /* wait until the PHY write completes and return */
  407. ret = wait_phy_ready(priv);
  408. mutex_unlock(&priv->lock);
  409. return ret;
  410. }
  411. /*
  412. * Program the hardware MAC address from dev->dev_addr.
  413. */
  414. static int enc28j60_set_hw_macaddr(struct net_device *ndev)
  415. {
  416. int ret;
  417. struct enc28j60_net *priv = netdev_priv(ndev);
  418. mutex_lock(&priv->lock);
  419. if (!priv->hw_enable) {
  420. if (netif_msg_drv(priv)) {
  421. DECLARE_MAC_BUF(mac);
  422. printk(KERN_INFO DRV_NAME
  423. ": %s: Setting MAC address to %s\n",
  424. ndev->name, print_mac(mac, ndev->dev_addr));
  425. }
  426. /* NOTE: MAC address in ENC28J60 is byte-backward */
  427. nolock_regb_write(priv, MAADR5, ndev->dev_addr[0]);
  428. nolock_regb_write(priv, MAADR4, ndev->dev_addr[1]);
  429. nolock_regb_write(priv, MAADR3, ndev->dev_addr[2]);
  430. nolock_regb_write(priv, MAADR2, ndev->dev_addr[3]);
  431. nolock_regb_write(priv, MAADR1, ndev->dev_addr[4]);
  432. nolock_regb_write(priv, MAADR0, ndev->dev_addr[5]);
  433. ret = 0;
  434. } else {
  435. if (netif_msg_drv(priv))
  436. printk(KERN_DEBUG DRV_NAME
  437. ": %s() Hardware must be disabled to set "
  438. "Mac address\n", __func__);
  439. ret = -EBUSY;
  440. }
  441. mutex_unlock(&priv->lock);
  442. return ret;
  443. }
  444. /*
  445. * Store the new hardware address in dev->dev_addr, and update the MAC.
  446. */
  447. static int enc28j60_set_mac_address(struct net_device *dev, void *addr)
  448. {
  449. struct sockaddr *address = addr;
  450. if (netif_running(dev))
  451. return -EBUSY;
  452. if (!is_valid_ether_addr(address->sa_data))
  453. return -EADDRNOTAVAIL;
  454. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  455. return enc28j60_set_hw_macaddr(dev);
  456. }
  457. /*
  458. * Debug routine to dump useful register contents
  459. */
  460. static void enc28j60_dump_regs(struct enc28j60_net *priv, const char *msg)
  461. {
  462. mutex_lock(&priv->lock);
  463. printk(KERN_DEBUG DRV_NAME " %s\n"
  464. "HwRevID: 0x%02x\n"
  465. "Cntrl: ECON1 ECON2 ESTAT EIR EIE\n"
  466. " 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n"
  467. "MAC : MACON1 MACON3 MACON4\n"
  468. " 0x%02x 0x%02x 0x%02x\n"
  469. "Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n"
  470. " 0x%04x 0x%04x 0x%04x 0x%04x "
  471. "0x%02x 0x%02x 0x%04x\n"
  472. "Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\n"
  473. " 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x\n",
  474. msg, nolock_regb_read(priv, EREVID),
  475. nolock_regb_read(priv, ECON1), nolock_regb_read(priv, ECON2),
  476. nolock_regb_read(priv, ESTAT), nolock_regb_read(priv, EIR),
  477. nolock_regb_read(priv, EIE), nolock_regb_read(priv, MACON1),
  478. nolock_regb_read(priv, MACON3), nolock_regb_read(priv, MACON4),
  479. nolock_regw_read(priv, ERXSTL), nolock_regw_read(priv, ERXNDL),
  480. nolock_regw_read(priv, ERXWRPTL),
  481. nolock_regw_read(priv, ERXRDPTL),
  482. nolock_regb_read(priv, ERXFCON),
  483. nolock_regb_read(priv, EPKTCNT),
  484. nolock_regw_read(priv, MAMXFLL), nolock_regw_read(priv, ETXSTL),
  485. nolock_regw_read(priv, ETXNDL),
  486. nolock_regb_read(priv, MACLCON1),
  487. nolock_regb_read(priv, MACLCON2),
  488. nolock_regb_read(priv, MAPHSUP));
  489. mutex_unlock(&priv->lock);
  490. }
  491. /*
  492. * ERXRDPT need to be set always at odd addresses, refer to errata datasheet
  493. */
  494. static u16 erxrdpt_workaround(u16 next_packet_ptr, u16 start, u16 end)
  495. {
  496. u16 erxrdpt;
  497. if ((next_packet_ptr - 1 < start) || (next_packet_ptr - 1 > end))
  498. erxrdpt = end;
  499. else
  500. erxrdpt = next_packet_ptr - 1;
  501. return erxrdpt;
  502. }
  503. static void nolock_rxfifo_init(struct enc28j60_net *priv, u16 start, u16 end)
  504. {
  505. u16 erxrdpt;
  506. if (start > 0x1FFF || end > 0x1FFF || start > end) {
  507. if (netif_msg_drv(priv))
  508. printk(KERN_ERR DRV_NAME ": %s(%d, %d) RXFIFO "
  509. "bad parameters!\n", __func__, start, end);
  510. return;
  511. }
  512. /* set receive buffer start + end */
  513. priv->next_pk_ptr = start;
  514. nolock_regw_write(priv, ERXSTL, start);
  515. erxrdpt = erxrdpt_workaround(priv->next_pk_ptr, start, end);
  516. nolock_regw_write(priv, ERXRDPTL, erxrdpt);
  517. nolock_regw_write(priv, ERXNDL, end);
  518. }
  519. static void nolock_txfifo_init(struct enc28j60_net *priv, u16 start, u16 end)
  520. {
  521. if (start > 0x1FFF || end > 0x1FFF || start > end) {
  522. if (netif_msg_drv(priv))
  523. printk(KERN_ERR DRV_NAME ": %s(%d, %d) TXFIFO "
  524. "bad parameters!\n", __func__, start, end);
  525. return;
  526. }
  527. /* set transmit buffer start + end */
  528. nolock_regw_write(priv, ETXSTL, start);
  529. nolock_regw_write(priv, ETXNDL, end);
  530. }
  531. /*
  532. * Low power mode shrinks power consumption about 100x, so we'd like
  533. * the chip to be in that mode whenever it's inactive. (However, we
  534. * can't stay in lowpower mode during suspend with WOL active.)
  535. */
  536. static void enc28j60_lowpower(struct enc28j60_net *priv, bool is_low)
  537. {
  538. if (netif_msg_drv(priv))
  539. dev_dbg(&priv->spi->dev, "%s power...\n",
  540. is_low ? "low" : "high");
  541. mutex_lock(&priv->lock);
  542. if (is_low) {
  543. nolock_reg_bfclr(priv, ECON1, ECON1_RXEN);
  544. poll_ready(priv, ESTAT, ESTAT_RXBUSY, 0);
  545. poll_ready(priv, ECON1, ECON1_TXRTS, 0);
  546. /* ECON2_VRPS was set during initialization */
  547. nolock_reg_bfset(priv, ECON2, ECON2_PWRSV);
  548. } else {
  549. nolock_reg_bfclr(priv, ECON2, ECON2_PWRSV);
  550. poll_ready(priv, ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
  551. /* caller sets ECON1_RXEN */
  552. }
  553. mutex_unlock(&priv->lock);
  554. }
  555. static int enc28j60_hw_init(struct enc28j60_net *priv)
  556. {
  557. u8 reg;
  558. if (netif_msg_drv(priv))
  559. printk(KERN_DEBUG DRV_NAME ": %s() - %s\n", __func__,
  560. priv->full_duplex ? "FullDuplex" : "HalfDuplex");
  561. mutex_lock(&priv->lock);
  562. /* first reset the chip */
  563. enc28j60_soft_reset(priv);
  564. /* Clear ECON1 */
  565. spi_write_op(priv, ENC28J60_WRITE_CTRL_REG, ECON1, 0x00);
  566. priv->bank = 0;
  567. priv->hw_enable = false;
  568. priv->tx_retry_count = 0;
  569. priv->max_pk_counter = 0;
  570. priv->rxfilter = RXFILTER_NORMAL;
  571. /* enable address auto increment and voltage regulator powersave */
  572. nolock_regb_write(priv, ECON2, ECON2_AUTOINC | ECON2_VRPS);
  573. nolock_rxfifo_init(priv, RXSTART_INIT, RXEND_INIT);
  574. nolock_txfifo_init(priv, TXSTART_INIT, TXEND_INIT);
  575. mutex_unlock(&priv->lock);
  576. /*
  577. * Check the RevID.
  578. * If it's 0x00 or 0xFF probably the enc28j60 is not mounted or
  579. * damaged
  580. */
  581. reg = locked_regb_read(priv, EREVID);
  582. if (netif_msg_drv(priv))
  583. printk(KERN_INFO DRV_NAME ": chip RevID: 0x%02x\n", reg);
  584. if (reg == 0x00 || reg == 0xff) {
  585. if (netif_msg_drv(priv))
  586. printk(KERN_DEBUG DRV_NAME ": %s() Invalid RevId %d\n",
  587. __func__, reg);
  588. return 0;
  589. }
  590. /* default filter mode: (unicast OR broadcast) AND crc valid */
  591. locked_regb_write(priv, ERXFCON,
  592. ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN);
  593. /* enable MAC receive */
  594. locked_regb_write(priv, MACON1,
  595. MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS);
  596. /* enable automatic padding and CRC operations */
  597. if (priv->full_duplex) {
  598. locked_regb_write(priv, MACON3,
  599. MACON3_PADCFG0 | MACON3_TXCRCEN |
  600. MACON3_FRMLNEN | MACON3_FULDPX);
  601. /* set inter-frame gap (non-back-to-back) */
  602. locked_regb_write(priv, MAIPGL, 0x12);
  603. /* set inter-frame gap (back-to-back) */
  604. locked_regb_write(priv, MABBIPG, 0x15);
  605. } else {
  606. locked_regb_write(priv, MACON3,
  607. MACON3_PADCFG0 | MACON3_TXCRCEN |
  608. MACON3_FRMLNEN);
  609. locked_regb_write(priv, MACON4, 1 << 6); /* DEFER bit */
  610. /* set inter-frame gap (non-back-to-back) */
  611. locked_regw_write(priv, MAIPGL, 0x0C12);
  612. /* set inter-frame gap (back-to-back) */
  613. locked_regb_write(priv, MABBIPG, 0x12);
  614. }
  615. /*
  616. * MACLCON1 (default)
  617. * MACLCON2 (default)
  618. * Set the maximum packet size which the controller will accept
  619. */
  620. locked_regw_write(priv, MAMXFLL, MAX_FRAMELEN);
  621. /* Configure LEDs */
  622. if (!enc28j60_phy_write(priv, PHLCON, ENC28J60_LAMPS_MODE))
  623. return 0;
  624. if (priv->full_duplex) {
  625. if (!enc28j60_phy_write(priv, PHCON1, PHCON1_PDPXMD))
  626. return 0;
  627. if (!enc28j60_phy_write(priv, PHCON2, 0x00))
  628. return 0;
  629. } else {
  630. if (!enc28j60_phy_write(priv, PHCON1, 0x00))
  631. return 0;
  632. if (!enc28j60_phy_write(priv, PHCON2, PHCON2_HDLDIS))
  633. return 0;
  634. }
  635. if (netif_msg_hw(priv))
  636. enc28j60_dump_regs(priv, "Hw initialized.");
  637. return 1;
  638. }
  639. static void enc28j60_hw_enable(struct enc28j60_net *priv)
  640. {
  641. /* enable interrupts */
  642. if (netif_msg_hw(priv))
  643. printk(KERN_DEBUG DRV_NAME ": %s() enabling interrupts.\n",
  644. __func__);
  645. enc28j60_phy_write(priv, PHIE, PHIE_PGEIE | PHIE_PLNKIE);
  646. mutex_lock(&priv->lock);
  647. nolock_reg_bfclr(priv, EIR, EIR_DMAIF | EIR_LINKIF |
  648. EIR_TXIF | EIR_TXERIF | EIR_RXERIF | EIR_PKTIF);
  649. nolock_regb_write(priv, EIE, EIE_INTIE | EIE_PKTIE | EIE_LINKIE |
  650. EIE_TXIE | EIE_TXERIE | EIE_RXERIE);
  651. /* enable receive logic */
  652. nolock_reg_bfset(priv, ECON1, ECON1_RXEN);
  653. priv->hw_enable = true;
  654. mutex_unlock(&priv->lock);
  655. }
  656. static void enc28j60_hw_disable(struct enc28j60_net *priv)
  657. {
  658. mutex_lock(&priv->lock);
  659. /* disable interrutps and packet reception */
  660. nolock_regb_write(priv, EIE, 0x00);
  661. nolock_reg_bfclr(priv, ECON1, ECON1_RXEN);
  662. priv->hw_enable = false;
  663. mutex_unlock(&priv->lock);
  664. }
  665. static int
  666. enc28j60_setlink(struct net_device *ndev, u8 autoneg, u16 speed, u8 duplex)
  667. {
  668. struct enc28j60_net *priv = netdev_priv(ndev);
  669. int ret = 0;
  670. if (!priv->hw_enable) {
  671. /* link is in low power mode now; duplex setting
  672. * will take effect on next enc28j60_hw_init().
  673. */
  674. if (autoneg == AUTONEG_DISABLE && speed == SPEED_10)
  675. priv->full_duplex = (duplex == DUPLEX_FULL);
  676. else {
  677. if (netif_msg_link(priv))
  678. dev_warn(&ndev->dev,
  679. "unsupported link setting\n");
  680. ret = -EOPNOTSUPP;
  681. }
  682. } else {
  683. if (netif_msg_link(priv))
  684. dev_warn(&ndev->dev, "Warning: hw must be disabled "
  685. "to set link mode\n");
  686. ret = -EBUSY;
  687. }
  688. return ret;
  689. }
  690. /*
  691. * Read the Transmit Status Vector
  692. */
  693. static void enc28j60_read_tsv(struct enc28j60_net *priv, u8 tsv[TSV_SIZE])
  694. {
  695. int endptr;
  696. endptr = locked_regw_read(priv, ETXNDL);
  697. if (netif_msg_hw(priv))
  698. printk(KERN_DEBUG DRV_NAME ": reading TSV at addr:0x%04x\n",
  699. endptr + 1);
  700. enc28j60_mem_read(priv, endptr + 1, sizeof(tsv), tsv);
  701. }
  702. static void enc28j60_dump_tsv(struct enc28j60_net *priv, const char *msg,
  703. u8 tsv[TSV_SIZE])
  704. {
  705. u16 tmp1, tmp2;
  706. printk(KERN_DEBUG DRV_NAME ": %s - TSV:\n", msg);
  707. tmp1 = tsv[1];
  708. tmp1 <<= 8;
  709. tmp1 |= tsv[0];
  710. tmp2 = tsv[5];
  711. tmp2 <<= 8;
  712. tmp2 |= tsv[4];
  713. printk(KERN_DEBUG DRV_NAME ": ByteCount: %d, CollisionCount: %d,"
  714. " TotByteOnWire: %d\n", tmp1, tsv[2] & 0x0f, tmp2);
  715. printk(KERN_DEBUG DRV_NAME ": TxDone: %d, CRCErr:%d, LenChkErr: %d,"
  716. " LenOutOfRange: %d\n", TSV_GETBIT(tsv, TSV_TXDONE),
  717. TSV_GETBIT(tsv, TSV_TXCRCERROR),
  718. TSV_GETBIT(tsv, TSV_TXLENCHKERROR),
  719. TSV_GETBIT(tsv, TSV_TXLENOUTOFRANGE));
  720. printk(KERN_DEBUG DRV_NAME ": Multicast: %d, Broadcast: %d, "
  721. "PacketDefer: %d, ExDefer: %d\n",
  722. TSV_GETBIT(tsv, TSV_TXMULTICAST),
  723. TSV_GETBIT(tsv, TSV_TXBROADCAST),
  724. TSV_GETBIT(tsv, TSV_TXPACKETDEFER),
  725. TSV_GETBIT(tsv, TSV_TXEXDEFER));
  726. printk(KERN_DEBUG DRV_NAME ": ExCollision: %d, LateCollision: %d, "
  727. "Giant: %d, Underrun: %d\n",
  728. TSV_GETBIT(tsv, TSV_TXEXCOLLISION),
  729. TSV_GETBIT(tsv, TSV_TXLATECOLLISION),
  730. TSV_GETBIT(tsv, TSV_TXGIANT), TSV_GETBIT(tsv, TSV_TXUNDERRUN));
  731. printk(KERN_DEBUG DRV_NAME ": ControlFrame: %d, PauseFrame: %d, "
  732. "BackPressApp: %d, VLanTagFrame: %d\n",
  733. TSV_GETBIT(tsv, TSV_TXCONTROLFRAME),
  734. TSV_GETBIT(tsv, TSV_TXPAUSEFRAME),
  735. TSV_GETBIT(tsv, TSV_BACKPRESSUREAPP),
  736. TSV_GETBIT(tsv, TSV_TXVLANTAGFRAME));
  737. }
  738. /*
  739. * Receive Status vector
  740. */
  741. static void enc28j60_dump_rsv(struct enc28j60_net *priv, const char *msg,
  742. u16 pk_ptr, int len, u16 sts)
  743. {
  744. printk(KERN_DEBUG DRV_NAME ": %s - NextPk: 0x%04x - RSV:\n",
  745. msg, pk_ptr);
  746. printk(KERN_DEBUG DRV_NAME ": ByteCount: %d, DribbleNibble: %d\n", len,
  747. RSV_GETBIT(sts, RSV_DRIBBLENIBBLE));
  748. printk(KERN_DEBUG DRV_NAME ": RxOK: %d, CRCErr:%d, LenChkErr: %d,"
  749. " LenOutOfRange: %d\n", RSV_GETBIT(sts, RSV_RXOK),
  750. RSV_GETBIT(sts, RSV_CRCERROR),
  751. RSV_GETBIT(sts, RSV_LENCHECKERR),
  752. RSV_GETBIT(sts, RSV_LENOUTOFRANGE));
  753. printk(KERN_DEBUG DRV_NAME ": Multicast: %d, Broadcast: %d, "
  754. "LongDropEvent: %d, CarrierEvent: %d\n",
  755. RSV_GETBIT(sts, RSV_RXMULTICAST),
  756. RSV_GETBIT(sts, RSV_RXBROADCAST),
  757. RSV_GETBIT(sts, RSV_RXLONGEVDROPEV),
  758. RSV_GETBIT(sts, RSV_CARRIEREV));
  759. printk(KERN_DEBUG DRV_NAME ": ControlFrame: %d, PauseFrame: %d,"
  760. " UnknownOp: %d, VLanTagFrame: %d\n",
  761. RSV_GETBIT(sts, RSV_RXCONTROLFRAME),
  762. RSV_GETBIT(sts, RSV_RXPAUSEFRAME),
  763. RSV_GETBIT(sts, RSV_RXUNKNOWNOPCODE),
  764. RSV_GETBIT(sts, RSV_RXTYPEVLAN));
  765. }
  766. static void dump_packet(const char *msg, int len, const char *data)
  767. {
  768. printk(KERN_DEBUG DRV_NAME ": %s - packet len:%d\n", msg, len);
  769. print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,
  770. data, len, true);
  771. }
  772. /*
  773. * Hardware receive function.
  774. * Read the buffer memory, update the FIFO pointer to free the buffer,
  775. * check the status vector and decrement the packet counter.
  776. */
  777. static void enc28j60_hw_rx(struct net_device *ndev)
  778. {
  779. struct enc28j60_net *priv = netdev_priv(ndev);
  780. struct sk_buff *skb = NULL;
  781. u16 erxrdpt, next_packet, rxstat;
  782. u8 rsv[RSV_SIZE];
  783. int len;
  784. if (netif_msg_rx_status(priv))
  785. printk(KERN_DEBUG DRV_NAME ": RX pk_addr:0x%04x\n",
  786. priv->next_pk_ptr);
  787. if (unlikely(priv->next_pk_ptr > RXEND_INIT)) {
  788. if (netif_msg_rx_err(priv))
  789. dev_err(&ndev->dev,
  790. "%s() Invalid packet address!! 0x%04x\n",
  791. __func__, priv->next_pk_ptr);
  792. /* packet address corrupted: reset RX logic */
  793. mutex_lock(&priv->lock);
  794. nolock_reg_bfclr(priv, ECON1, ECON1_RXEN);
  795. nolock_reg_bfset(priv, ECON1, ECON1_RXRST);
  796. nolock_reg_bfclr(priv, ECON1, ECON1_RXRST);
  797. nolock_rxfifo_init(priv, RXSTART_INIT, RXEND_INIT);
  798. nolock_reg_bfclr(priv, EIR, EIR_RXERIF);
  799. nolock_reg_bfset(priv, ECON1, ECON1_RXEN);
  800. mutex_unlock(&priv->lock);
  801. ndev->stats.rx_errors++;
  802. return;
  803. }
  804. /* Read next packet pointer and rx status vector */
  805. enc28j60_mem_read(priv, priv->next_pk_ptr, sizeof(rsv), rsv);
  806. next_packet = rsv[1];
  807. next_packet <<= 8;
  808. next_packet |= rsv[0];
  809. len = rsv[3];
  810. len <<= 8;
  811. len |= rsv[2];
  812. rxstat = rsv[5];
  813. rxstat <<= 8;
  814. rxstat |= rsv[4];
  815. if (netif_msg_rx_status(priv))
  816. enc28j60_dump_rsv(priv, __func__, next_packet, len, rxstat);
  817. if (!RSV_GETBIT(rxstat, RSV_RXOK)) {
  818. if (netif_msg_rx_err(priv))
  819. dev_err(&ndev->dev, "Rx Error (%04x)\n", rxstat);
  820. ndev->stats.rx_errors++;
  821. if (RSV_GETBIT(rxstat, RSV_CRCERROR))
  822. ndev->stats.rx_crc_errors++;
  823. if (RSV_GETBIT(rxstat, RSV_LENCHECKERR))
  824. ndev->stats.rx_frame_errors++;
  825. } else {
  826. skb = dev_alloc_skb(len + NET_IP_ALIGN);
  827. if (!skb) {
  828. if (netif_msg_rx_err(priv))
  829. dev_err(&ndev->dev,
  830. "out of memory for Rx'd frame\n");
  831. ndev->stats.rx_dropped++;
  832. } else {
  833. skb->dev = ndev;
  834. skb_reserve(skb, NET_IP_ALIGN);
  835. /* copy the packet from the receive buffer */
  836. enc28j60_mem_read(priv, priv->next_pk_ptr + sizeof(rsv),
  837. len, skb_put(skb, len));
  838. if (netif_msg_pktdata(priv))
  839. dump_packet(__func__, skb->len, skb->data);
  840. skb->protocol = eth_type_trans(skb, ndev);
  841. /* update statistics */
  842. ndev->stats.rx_packets++;
  843. ndev->stats.rx_bytes += len;
  844. ndev->last_rx = jiffies;
  845. netif_rx(skb);
  846. }
  847. }
  848. /*
  849. * Move the RX read pointer to the start of the next
  850. * received packet.
  851. * This frees the memory we just read out
  852. */
  853. erxrdpt = erxrdpt_workaround(next_packet, RXSTART_INIT, RXEND_INIT);
  854. if (netif_msg_hw(priv))
  855. printk(KERN_DEBUG DRV_NAME ": %s() ERXRDPT:0x%04x\n",
  856. __func__, erxrdpt);
  857. mutex_lock(&priv->lock);
  858. nolock_regw_write(priv, ERXRDPTL, erxrdpt);
  859. #ifdef CONFIG_ENC28J60_WRITEVERIFY
  860. if (netif_msg_drv(priv)) {
  861. u16 reg;
  862. reg = nolock_regw_read(priv, ERXRDPTL);
  863. if (reg != erxrdpt)
  864. printk(KERN_DEBUG DRV_NAME ": %s() ERXRDPT verify "
  865. "error (0x%04x - 0x%04x)\n", __func__,
  866. reg, erxrdpt);
  867. }
  868. #endif
  869. priv->next_pk_ptr = next_packet;
  870. /* we are done with this packet, decrement the packet counter */
  871. nolock_reg_bfset(priv, ECON2, ECON2_PKTDEC);
  872. mutex_unlock(&priv->lock);
  873. }
  874. /*
  875. * Calculate free space in RxFIFO
  876. */
  877. static int enc28j60_get_free_rxfifo(struct enc28j60_net *priv)
  878. {
  879. int epkcnt, erxst, erxnd, erxwr, erxrd;
  880. int free_space;
  881. mutex_lock(&priv->lock);
  882. epkcnt = nolock_regb_read(priv, EPKTCNT);
  883. if (epkcnt >= 255)
  884. free_space = -1;
  885. else {
  886. erxst = nolock_regw_read(priv, ERXSTL);
  887. erxnd = nolock_regw_read(priv, ERXNDL);
  888. erxwr = nolock_regw_read(priv, ERXWRPTL);
  889. erxrd = nolock_regw_read(priv, ERXRDPTL);
  890. if (erxwr > erxrd)
  891. free_space = (erxnd - erxst) - (erxwr - erxrd);
  892. else if (erxwr == erxrd)
  893. free_space = (erxnd - erxst);
  894. else
  895. free_space = erxrd - erxwr - 1;
  896. }
  897. mutex_unlock(&priv->lock);
  898. if (netif_msg_rx_status(priv))
  899. printk(KERN_DEBUG DRV_NAME ": %s() free_space = %d\n",
  900. __func__, free_space);
  901. return free_space;
  902. }
  903. /*
  904. * Access the PHY to determine link status
  905. */
  906. static void enc28j60_check_link_status(struct net_device *ndev)
  907. {
  908. struct enc28j60_net *priv = netdev_priv(ndev);
  909. u16 reg;
  910. int duplex;
  911. reg = enc28j60_phy_read(priv, PHSTAT2);
  912. if (netif_msg_hw(priv))
  913. printk(KERN_DEBUG DRV_NAME ": %s() PHSTAT1: %04x, "
  914. "PHSTAT2: %04x\n", __func__,
  915. enc28j60_phy_read(priv, PHSTAT1), reg);
  916. duplex = reg & PHSTAT2_DPXSTAT;
  917. if (reg & PHSTAT2_LSTAT) {
  918. netif_carrier_on(ndev);
  919. if (netif_msg_ifup(priv))
  920. dev_info(&ndev->dev, "link up - %s\n",
  921. duplex ? "Full duplex" : "Half duplex");
  922. } else {
  923. if (netif_msg_ifdown(priv))
  924. dev_info(&ndev->dev, "link down\n");
  925. netif_carrier_off(ndev);
  926. }
  927. }
  928. static void enc28j60_tx_clear(struct net_device *ndev, bool err)
  929. {
  930. struct enc28j60_net *priv = netdev_priv(ndev);
  931. if (err)
  932. ndev->stats.tx_errors++;
  933. else
  934. ndev->stats.tx_packets++;
  935. if (priv->tx_skb) {
  936. if (!err)
  937. ndev->stats.tx_bytes += priv->tx_skb->len;
  938. dev_kfree_skb(priv->tx_skb);
  939. priv->tx_skb = NULL;
  940. }
  941. locked_reg_bfclr(priv, ECON1, ECON1_TXRTS);
  942. netif_wake_queue(ndev);
  943. }
  944. /*
  945. * RX handler
  946. * ignore PKTIF because is unreliable! (look at the errata datasheet)
  947. * check EPKTCNT is the suggested workaround.
  948. * We don't need to clear interrupt flag, automatically done when
  949. * enc28j60_hw_rx() decrements the packet counter.
  950. * Returns how many packet processed.
  951. */
  952. static int enc28j60_rx_interrupt(struct net_device *ndev)
  953. {
  954. struct enc28j60_net *priv = netdev_priv(ndev);
  955. int pk_counter, ret;
  956. pk_counter = locked_regb_read(priv, EPKTCNT);
  957. if (pk_counter && netif_msg_intr(priv))
  958. printk(KERN_DEBUG DRV_NAME ": intRX, pk_cnt: %d\n", pk_counter);
  959. if (pk_counter > priv->max_pk_counter) {
  960. /* update statistics */
  961. priv->max_pk_counter = pk_counter;
  962. if (netif_msg_rx_status(priv) && priv->max_pk_counter > 1)
  963. printk(KERN_DEBUG DRV_NAME ": RX max_pk_cnt: %d\n",
  964. priv->max_pk_counter);
  965. }
  966. ret = pk_counter;
  967. while (pk_counter-- > 0)
  968. enc28j60_hw_rx(ndev);
  969. return ret;
  970. }
  971. static void enc28j60_irq_work_handler(struct work_struct *work)
  972. {
  973. struct enc28j60_net *priv =
  974. container_of(work, struct enc28j60_net, irq_work);
  975. struct net_device *ndev = priv->netdev;
  976. int intflags, loop;
  977. if (netif_msg_intr(priv))
  978. printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __func__);
  979. /* disable further interrupts */
  980. locked_reg_bfclr(priv, EIE, EIE_INTIE);
  981. do {
  982. loop = 0;
  983. intflags = locked_regb_read(priv, EIR);
  984. /* DMA interrupt handler (not currently used) */
  985. if ((intflags & EIR_DMAIF) != 0) {
  986. loop++;
  987. if (netif_msg_intr(priv))
  988. printk(KERN_DEBUG DRV_NAME
  989. ": intDMA(%d)\n", loop);
  990. locked_reg_bfclr(priv, EIR, EIR_DMAIF);
  991. }
  992. /* LINK changed handler */
  993. if ((intflags & EIR_LINKIF) != 0) {
  994. loop++;
  995. if (netif_msg_intr(priv))
  996. printk(KERN_DEBUG DRV_NAME
  997. ": intLINK(%d)\n", loop);
  998. enc28j60_check_link_status(ndev);
  999. /* read PHIR to clear the flag */
  1000. enc28j60_phy_read(priv, PHIR);
  1001. }
  1002. /* TX complete handler */
  1003. if ((intflags & EIR_TXIF) != 0) {
  1004. bool err = false;
  1005. loop++;
  1006. if (netif_msg_intr(priv))
  1007. printk(KERN_DEBUG DRV_NAME
  1008. ": intTX(%d)\n", loop);
  1009. priv->tx_retry_count = 0;
  1010. if (locked_regb_read(priv, ESTAT) & ESTAT_TXABRT) {
  1011. if (netif_msg_tx_err(priv))
  1012. dev_err(&ndev->dev,
  1013. "Tx Error (aborted)\n");
  1014. err = true;
  1015. }
  1016. if (netif_msg_tx_done(priv)) {
  1017. u8 tsv[TSV_SIZE];
  1018. enc28j60_read_tsv(priv, tsv);
  1019. enc28j60_dump_tsv(priv, "Tx Done", tsv);
  1020. }
  1021. enc28j60_tx_clear(ndev, err);
  1022. locked_reg_bfclr(priv, EIR, EIR_TXIF);
  1023. }
  1024. /* TX Error handler */
  1025. if ((intflags & EIR_TXERIF) != 0) {
  1026. u8 tsv[TSV_SIZE];
  1027. loop++;
  1028. if (netif_msg_intr(priv))
  1029. printk(KERN_DEBUG DRV_NAME
  1030. ": intTXErr(%d)\n", loop);
  1031. locked_reg_bfclr(priv, ECON1, ECON1_TXRTS);
  1032. enc28j60_read_tsv(priv, tsv);
  1033. if (netif_msg_tx_err(priv))
  1034. enc28j60_dump_tsv(priv, "Tx Error", tsv);
  1035. /* Reset TX logic */
  1036. mutex_lock(&priv->lock);
  1037. nolock_reg_bfset(priv, ECON1, ECON1_TXRST);
  1038. nolock_reg_bfclr(priv, ECON1, ECON1_TXRST);
  1039. nolock_txfifo_init(priv, TXSTART_INIT, TXEND_INIT);
  1040. mutex_unlock(&priv->lock);
  1041. /* Transmit Late collision check for retransmit */
  1042. if (TSV_GETBIT(tsv, TSV_TXLATECOLLISION)) {
  1043. if (netif_msg_tx_err(priv))
  1044. printk(KERN_DEBUG DRV_NAME
  1045. ": LateCollision TXErr (%d)\n",
  1046. priv->tx_retry_count);
  1047. if (priv->tx_retry_count++ < MAX_TX_RETRYCOUNT)
  1048. locked_reg_bfset(priv, ECON1,
  1049. ECON1_TXRTS);
  1050. else
  1051. enc28j60_tx_clear(ndev, true);
  1052. } else
  1053. enc28j60_tx_clear(ndev, true);
  1054. locked_reg_bfclr(priv, EIR, EIR_TXERIF);
  1055. }
  1056. /* RX Error handler */
  1057. if ((intflags & EIR_RXERIF) != 0) {
  1058. loop++;
  1059. if (netif_msg_intr(priv))
  1060. printk(KERN_DEBUG DRV_NAME
  1061. ": intRXErr(%d)\n", loop);
  1062. /* Check free FIFO space to flag RX overrun */
  1063. if (enc28j60_get_free_rxfifo(priv) <= 0) {
  1064. if (netif_msg_rx_err(priv))
  1065. printk(KERN_DEBUG DRV_NAME
  1066. ": RX Overrun\n");
  1067. ndev->stats.rx_dropped++;
  1068. }
  1069. locked_reg_bfclr(priv, EIR, EIR_RXERIF);
  1070. }
  1071. /* RX handler */
  1072. if (enc28j60_rx_interrupt(ndev))
  1073. loop++;
  1074. } while (loop);
  1075. /* re-enable interrupts */
  1076. locked_reg_bfset(priv, EIE, EIE_INTIE);
  1077. if (netif_msg_intr(priv))
  1078. printk(KERN_DEBUG DRV_NAME ": %s() exit\n", __func__);
  1079. }
  1080. /*
  1081. * Hardware transmit function.
  1082. * Fill the buffer memory and send the contents of the transmit buffer
  1083. * onto the network
  1084. */
  1085. static void enc28j60_hw_tx(struct enc28j60_net *priv)
  1086. {
  1087. if (netif_msg_tx_queued(priv))
  1088. printk(KERN_DEBUG DRV_NAME
  1089. ": Tx Packet Len:%d\n", priv->tx_skb->len);
  1090. if (netif_msg_pktdata(priv))
  1091. dump_packet(__func__,
  1092. priv->tx_skb->len, priv->tx_skb->data);
  1093. enc28j60_packet_write(priv, priv->tx_skb->len, priv->tx_skb->data);
  1094. #ifdef CONFIG_ENC28J60_WRITEVERIFY
  1095. /* readback and verify written data */
  1096. if (netif_msg_drv(priv)) {
  1097. int test_len, k;
  1098. u8 test_buf[64]; /* limit the test to the first 64 bytes */
  1099. int okflag;
  1100. test_len = priv->tx_skb->len;
  1101. if (test_len > sizeof(test_buf))
  1102. test_len = sizeof(test_buf);
  1103. /* + 1 to skip control byte */
  1104. enc28j60_mem_read(priv, TXSTART_INIT + 1, test_len, test_buf);
  1105. okflag = 1;
  1106. for (k = 0; k < test_len; k++) {
  1107. if (priv->tx_skb->data[k] != test_buf[k]) {
  1108. printk(KERN_DEBUG DRV_NAME
  1109. ": Error, %d location differ: "
  1110. "0x%02x-0x%02x\n", k,
  1111. priv->tx_skb->data[k], test_buf[k]);
  1112. okflag = 0;
  1113. }
  1114. }
  1115. if (!okflag)
  1116. printk(KERN_DEBUG DRV_NAME ": Tx write buffer, "
  1117. "verify ERROR!\n");
  1118. }
  1119. #endif
  1120. /* set TX request flag */
  1121. locked_reg_bfset(priv, ECON1, ECON1_TXRTS);
  1122. }
  1123. static int enc28j60_send_packet(struct sk_buff *skb, struct net_device *dev)
  1124. {
  1125. struct enc28j60_net *priv = netdev_priv(dev);
  1126. if (netif_msg_tx_queued(priv))
  1127. printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __func__);
  1128. /* If some error occurs while trying to transmit this
  1129. * packet, you should return '1' from this function.
  1130. * In such a case you _may not_ do anything to the
  1131. * SKB, it is still owned by the network queueing
  1132. * layer when an error is returned. This means you
  1133. * may not modify any SKB fields, you may not free
  1134. * the SKB, etc.
  1135. */
  1136. netif_stop_queue(dev);
  1137. /* save the timestamp */
  1138. priv->netdev->trans_start = jiffies;
  1139. /* Remember the skb for deferred processing */
  1140. priv->tx_skb = skb;
  1141. schedule_work(&priv->tx_work);
  1142. return 0;
  1143. }
  1144. static void enc28j60_tx_work_handler(struct work_struct *work)
  1145. {
  1146. struct enc28j60_net *priv =
  1147. container_of(work, struct enc28j60_net, tx_work);
  1148. /* actual delivery of data */
  1149. enc28j60_hw_tx(priv);
  1150. }
  1151. static irqreturn_t enc28j60_irq(int irq, void *dev_id)
  1152. {
  1153. struct enc28j60_net *priv = dev_id;
  1154. /*
  1155. * Can't do anything in interrupt context because we need to
  1156. * block (spi_sync() is blocking) so fire of the interrupt
  1157. * handling workqueue.
  1158. * Remember that we access enc28j60 registers through SPI bus
  1159. * via spi_sync() call.
  1160. */
  1161. schedule_work(&priv->irq_work);
  1162. return IRQ_HANDLED;
  1163. }
  1164. static void enc28j60_tx_timeout(struct net_device *ndev)
  1165. {
  1166. struct enc28j60_net *priv = netdev_priv(ndev);
  1167. if (netif_msg_timer(priv))
  1168. dev_err(&ndev->dev, DRV_NAME " tx timeout\n");
  1169. ndev->stats.tx_errors++;
  1170. /* can't restart safely under softirq */
  1171. schedule_work(&priv->restart_work);
  1172. }
  1173. /*
  1174. * Open/initialize the board. This is called (in the current kernel)
  1175. * sometime after booting when the 'ifconfig' program is run.
  1176. *
  1177. * This routine should set everything up anew at each open, even
  1178. * registers that "should" only need to be set once at boot, so that
  1179. * there is non-reboot way to recover if something goes wrong.
  1180. */
  1181. static int enc28j60_net_open(struct net_device *dev)
  1182. {
  1183. struct enc28j60_net *priv = netdev_priv(dev);
  1184. if (netif_msg_drv(priv))
  1185. printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __func__);
  1186. if (!is_valid_ether_addr(dev->dev_addr)) {
  1187. if (netif_msg_ifup(priv)) {
  1188. DECLARE_MAC_BUF(mac);
  1189. dev_err(&dev->dev, "invalid MAC address %s\n",
  1190. print_mac(mac, dev->dev_addr));
  1191. }
  1192. return -EADDRNOTAVAIL;
  1193. }
  1194. /* Reset the hardware here (and take it out of low power mode) */
  1195. enc28j60_lowpower(priv, false);
  1196. enc28j60_hw_disable(priv);
  1197. if (!enc28j60_hw_init(priv)) {
  1198. if (netif_msg_ifup(priv))
  1199. dev_err(&dev->dev, "hw_reset() failed\n");
  1200. return -EINVAL;
  1201. }
  1202. /* Update the MAC address (in case user has changed it) */
  1203. enc28j60_set_hw_macaddr(dev);
  1204. /* Enable interrupts */
  1205. enc28j60_hw_enable(priv);
  1206. /* check link status */
  1207. enc28j60_check_link_status(dev);
  1208. /* We are now ready to accept transmit requests from
  1209. * the queueing layer of the networking.
  1210. */
  1211. netif_start_queue(dev);
  1212. return 0;
  1213. }
  1214. /* The inverse routine to net_open(). */
  1215. static int enc28j60_net_close(struct net_device *dev)
  1216. {
  1217. struct enc28j60_net *priv = netdev_priv(dev);
  1218. if (netif_msg_drv(priv))
  1219. printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __func__);
  1220. enc28j60_hw_disable(priv);
  1221. enc28j60_lowpower(priv, true);
  1222. netif_stop_queue(dev);
  1223. return 0;
  1224. }
  1225. /*
  1226. * Set or clear the multicast filter for this adapter
  1227. * num_addrs == -1 Promiscuous mode, receive all packets
  1228. * num_addrs == 0 Normal mode, filter out multicast packets
  1229. * num_addrs > 0 Multicast mode, receive normal and MC packets
  1230. */
  1231. static void enc28j60_set_multicast_list(struct net_device *dev)
  1232. {
  1233. struct enc28j60_net *priv = netdev_priv(dev);
  1234. int oldfilter = priv->rxfilter;
  1235. if (dev->flags & IFF_PROMISC) {
  1236. if (netif_msg_link(priv))
  1237. dev_info(&dev->dev, "promiscuous mode\n");
  1238. priv->rxfilter = RXFILTER_PROMISC;
  1239. } else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count) {
  1240. if (netif_msg_link(priv))
  1241. dev_info(&dev->dev, "%smulticast mode\n",
  1242. (dev->flags & IFF_ALLMULTI) ? "all-" : "");
  1243. priv->rxfilter = RXFILTER_MULTI;
  1244. } else {
  1245. if (netif_msg_link(priv))
  1246. dev_info(&dev->dev, "normal mode\n");
  1247. priv->rxfilter = RXFILTER_NORMAL;
  1248. }
  1249. if (oldfilter != priv->rxfilter)
  1250. schedule_work(&priv->setrx_work);
  1251. }
  1252. static void enc28j60_setrx_work_handler(struct work_struct *work)
  1253. {
  1254. struct enc28j60_net *priv =
  1255. container_of(work, struct enc28j60_net, setrx_work);
  1256. if (priv->rxfilter == RXFILTER_PROMISC) {
  1257. if (netif_msg_drv(priv))
  1258. printk(KERN_DEBUG DRV_NAME ": promiscuous mode\n");
  1259. locked_regb_write(priv, ERXFCON, 0x00);
  1260. } else if (priv->rxfilter == RXFILTER_MULTI) {
  1261. if (netif_msg_drv(priv))
  1262. printk(KERN_DEBUG DRV_NAME ": multicast mode\n");
  1263. locked_regb_write(priv, ERXFCON,
  1264. ERXFCON_UCEN | ERXFCON_CRCEN |
  1265. ERXFCON_BCEN | ERXFCON_MCEN);
  1266. } else {
  1267. if (netif_msg_drv(priv))
  1268. printk(KERN_DEBUG DRV_NAME ": normal mode\n");
  1269. locked_regb_write(priv, ERXFCON,
  1270. ERXFCON_UCEN | ERXFCON_CRCEN |
  1271. ERXFCON_BCEN);
  1272. }
  1273. }
  1274. static void enc28j60_restart_work_handler(struct work_struct *work)
  1275. {
  1276. struct enc28j60_net *priv =
  1277. container_of(work, struct enc28j60_net, restart_work);
  1278. struct net_device *ndev = priv->netdev;
  1279. int ret;
  1280. rtnl_lock();
  1281. if (netif_running(ndev)) {
  1282. enc28j60_net_close(ndev);
  1283. ret = enc28j60_net_open(ndev);
  1284. if (unlikely(ret)) {
  1285. dev_info(&ndev->dev, " could not restart %d\n", ret);
  1286. dev_close(ndev);
  1287. }
  1288. }
  1289. rtnl_unlock();
  1290. }
  1291. /* ......................... ETHTOOL SUPPORT ........................... */
  1292. static void
  1293. enc28j60_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1294. {
  1295. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1296. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1297. strlcpy(info->bus_info,
  1298. dev->dev.parent->bus_id, sizeof(info->bus_info));
  1299. }
  1300. static int
  1301. enc28j60_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1302. {
  1303. struct enc28j60_net *priv = netdev_priv(dev);
  1304. cmd->transceiver = XCVR_INTERNAL;
  1305. cmd->supported = SUPPORTED_10baseT_Half
  1306. | SUPPORTED_10baseT_Full
  1307. | SUPPORTED_TP;
  1308. cmd->speed = SPEED_10;
  1309. cmd->duplex = priv->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1310. cmd->port = PORT_TP;
  1311. cmd->autoneg = AUTONEG_DISABLE;
  1312. return 0;
  1313. }
  1314. static int
  1315. enc28j60_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1316. {
  1317. return enc28j60_setlink(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  1318. }
  1319. static u32 enc28j60_get_msglevel(struct net_device *dev)
  1320. {
  1321. struct enc28j60_net *priv = netdev_priv(dev);
  1322. return priv->msg_enable;
  1323. }
  1324. static void enc28j60_set_msglevel(struct net_device *dev, u32 val)
  1325. {
  1326. struct enc28j60_net *priv = netdev_priv(dev);
  1327. priv->msg_enable = val;
  1328. }
  1329. static const struct ethtool_ops enc28j60_ethtool_ops = {
  1330. .get_settings = enc28j60_get_settings,
  1331. .set_settings = enc28j60_set_settings,
  1332. .get_drvinfo = enc28j60_get_drvinfo,
  1333. .get_msglevel = enc28j60_get_msglevel,
  1334. .set_msglevel = enc28j60_set_msglevel,
  1335. };
  1336. static int enc28j60_chipset_init(struct net_device *dev)
  1337. {
  1338. struct enc28j60_net *priv = netdev_priv(dev);
  1339. return enc28j60_hw_init(priv);
  1340. }
  1341. static int __devinit enc28j60_probe(struct spi_device *spi)
  1342. {
  1343. struct net_device *dev;
  1344. struct enc28j60_net *priv;
  1345. int ret = 0;
  1346. if (netif_msg_drv(&debug))
  1347. dev_info(&spi->dev, DRV_NAME " Ethernet driver %s loaded\n",
  1348. DRV_VERSION);
  1349. dev = alloc_etherdev(sizeof(struct enc28j60_net));
  1350. if (!dev) {
  1351. if (netif_msg_drv(&debug))
  1352. dev_err(&spi->dev, DRV_NAME
  1353. ": unable to alloc new ethernet\n");
  1354. ret = -ENOMEM;
  1355. goto error_alloc;
  1356. }
  1357. priv = netdev_priv(dev);
  1358. priv->netdev = dev; /* priv to netdev reference */
  1359. priv->spi = spi; /* priv to spi reference */
  1360. priv->msg_enable = netif_msg_init(debug.msg_enable,
  1361. ENC28J60_MSG_DEFAULT);
  1362. mutex_init(&priv->lock);
  1363. INIT_WORK(&priv->tx_work, enc28j60_tx_work_handler);
  1364. INIT_WORK(&priv->setrx_work, enc28j60_setrx_work_handler);
  1365. INIT_WORK(&priv->irq_work, enc28j60_irq_work_handler);
  1366. INIT_WORK(&priv->restart_work, enc28j60_restart_work_handler);
  1367. dev_set_drvdata(&spi->dev, priv); /* spi to priv reference */
  1368. SET_NETDEV_DEV(dev, &spi->dev);
  1369. if (!enc28j60_chipset_init(dev)) {
  1370. if (netif_msg_probe(priv))
  1371. dev_info(&spi->dev, DRV_NAME " chip not found\n");
  1372. ret = -EIO;
  1373. goto error_irq;
  1374. }
  1375. random_ether_addr(dev->dev_addr);
  1376. enc28j60_set_hw_macaddr(dev);
  1377. /* Board setup must set the relevant edge trigger type;
  1378. * level triggers won't currently work.
  1379. */
  1380. ret = request_irq(spi->irq, enc28j60_irq, 0, DRV_NAME, priv);
  1381. if (ret < 0) {
  1382. if (netif_msg_probe(priv))
  1383. dev_err(&spi->dev, DRV_NAME ": request irq %d failed "
  1384. "(ret = %d)\n", spi->irq, ret);
  1385. goto error_irq;
  1386. }
  1387. dev->if_port = IF_PORT_10BASET;
  1388. dev->irq = spi->irq;
  1389. dev->open = enc28j60_net_open;
  1390. dev->stop = enc28j60_net_close;
  1391. dev->hard_start_xmit = enc28j60_send_packet;
  1392. dev->set_multicast_list = &enc28j60_set_multicast_list;
  1393. dev->set_mac_address = enc28j60_set_mac_address;
  1394. dev->tx_timeout = &enc28j60_tx_timeout;
  1395. dev->watchdog_timeo = TX_TIMEOUT;
  1396. SET_ETHTOOL_OPS(dev, &enc28j60_ethtool_ops);
  1397. enc28j60_lowpower(priv, true);
  1398. ret = register_netdev(dev);
  1399. if (ret) {
  1400. if (netif_msg_probe(priv))
  1401. dev_err(&spi->dev, "register netdev " DRV_NAME
  1402. " failed (ret = %d)\n", ret);
  1403. goto error_register;
  1404. }
  1405. dev_info(&dev->dev, DRV_NAME " driver registered\n");
  1406. return 0;
  1407. error_register:
  1408. free_irq(spi->irq, priv);
  1409. error_irq:
  1410. free_netdev(dev);
  1411. error_alloc:
  1412. return ret;
  1413. }
  1414. static int __devexit enc28j60_remove(struct spi_device *spi)
  1415. {
  1416. struct enc28j60_net *priv = dev_get_drvdata(&spi->dev);
  1417. if (netif_msg_drv(priv))
  1418. printk(KERN_DEBUG DRV_NAME ": remove\n");
  1419. unregister_netdev(priv->netdev);
  1420. free_irq(spi->irq, priv);
  1421. free_netdev(priv->netdev);
  1422. return 0;
  1423. }
  1424. static struct spi_driver enc28j60_driver = {
  1425. .driver = {
  1426. .name = DRV_NAME,
  1427. .owner = THIS_MODULE,
  1428. },
  1429. .probe = enc28j60_probe,
  1430. .remove = __devexit_p(enc28j60_remove),
  1431. };
  1432. static int __init enc28j60_init(void)
  1433. {
  1434. msec20_to_jiffies = msecs_to_jiffies(20);
  1435. return spi_register_driver(&enc28j60_driver);
  1436. }
  1437. module_init(enc28j60_init);
  1438. static void __exit enc28j60_exit(void)
  1439. {
  1440. spi_unregister_driver(&enc28j60_driver);
  1441. }
  1442. module_exit(enc28j60_exit);
  1443. MODULE_DESCRIPTION(DRV_NAME " ethernet driver");
  1444. MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>");
  1445. MODULE_LICENSE("GPL");
  1446. module_param_named(debug, debug.msg_enable, int, 0);
  1447. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., ffff=all)");