encx24j600.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /**
  2. * Microchip ENCX24J600 ethernet driver
  3. *
  4. * Copyright (C) 2015 Gridpoint
  5. * Author: Jon Ringle <jringle@gridpoint.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/regmap.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/spi/spi.h>
  24. #include "encx24j600_hw.h"
  25. #define DRV_NAME "encx24j600"
  26. #define DRV_VERSION "1.0"
  27. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  28. static int debug = -1;
  29. module_param(debug, int, 0);
  30. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  31. /* SRAM memory layout:
  32. *
  33. * 0x0000-0x05ff TX buffers 1.5KB (1*1536) reside in the GP area in SRAM
  34. * 0x0600-0x5fff RX buffers 22.5KB (15*1536) reside in the RX area in SRAM
  35. */
  36. #define ENC_TX_BUF_START 0x0000U
  37. #define ENC_RX_BUF_START 0x0600U
  38. #define ENC_RX_BUF_END 0x5fffU
  39. #define ENC_SRAM_SIZE 0x6000U
  40. enum {
  41. RXFILTER_NORMAL,
  42. RXFILTER_MULTI,
  43. RXFILTER_PROMISC
  44. };
  45. struct encx24j600_priv {
  46. struct net_device *ndev;
  47. struct mutex lock; /* device access lock */
  48. struct encx24j600_context ctx;
  49. struct sk_buff *tx_skb;
  50. struct task_struct *kworker_task;
  51. struct kthread_worker kworker;
  52. struct kthread_work tx_work;
  53. struct kthread_work setrx_work;
  54. u16 next_packet;
  55. bool hw_enabled;
  56. bool full_duplex;
  57. bool autoneg;
  58. u16 speed;
  59. int rxfilter;
  60. u32 msg_enable;
  61. };
  62. static void dump_packet(const char *msg, int len, const char *data)
  63. {
  64. pr_debug(DRV_NAME ": %s - packet len:%d\n", msg, len);
  65. print_hex_dump_bytes("pk data: ", DUMP_PREFIX_OFFSET, data, len);
  66. }
  67. static void encx24j600_dump_rsv(struct encx24j600_priv *priv, const char *msg,
  68. struct rsv *rsv)
  69. {
  70. struct net_device *dev = priv->ndev;
  71. netdev_info(dev, "RX packet Len:%d\n", rsv->len);
  72. netdev_dbg(dev, "%s - NextPk: 0x%04x\n", msg,
  73. rsv->next_packet);
  74. netdev_dbg(dev, "RxOK: %d, DribbleNibble: %d\n",
  75. RSV_GETBIT(rsv->rxstat, RSV_RXOK),
  76. RSV_GETBIT(rsv->rxstat, RSV_DRIBBLENIBBLE));
  77. netdev_dbg(dev, "CRCErr:%d, LenChkErr: %d, LenOutOfRange: %d\n",
  78. RSV_GETBIT(rsv->rxstat, RSV_CRCERROR),
  79. RSV_GETBIT(rsv->rxstat, RSV_LENCHECKERR),
  80. RSV_GETBIT(rsv->rxstat, RSV_LENOUTOFRANGE));
  81. netdev_dbg(dev, "Multicast: %d, Broadcast: %d, LongDropEvent: %d, CarrierEvent: %d\n",
  82. RSV_GETBIT(rsv->rxstat, RSV_RXMULTICAST),
  83. RSV_GETBIT(rsv->rxstat, RSV_RXBROADCAST),
  84. RSV_GETBIT(rsv->rxstat, RSV_RXLONGEVDROPEV),
  85. RSV_GETBIT(rsv->rxstat, RSV_CARRIEREV));
  86. netdev_dbg(dev, "ControlFrame: %d, PauseFrame: %d, UnknownOp: %d, VLanTagFrame: %d\n",
  87. RSV_GETBIT(rsv->rxstat, RSV_RXCONTROLFRAME),
  88. RSV_GETBIT(rsv->rxstat, RSV_RXPAUSEFRAME),
  89. RSV_GETBIT(rsv->rxstat, RSV_RXUNKNOWNOPCODE),
  90. RSV_GETBIT(rsv->rxstat, RSV_RXTYPEVLAN));
  91. }
  92. static u16 encx24j600_read_reg(struct encx24j600_priv *priv, u8 reg)
  93. {
  94. struct net_device *dev = priv->ndev;
  95. unsigned int val = 0;
  96. int ret = regmap_read(priv->ctx.regmap, reg, &val);
  97. if (unlikely(ret))
  98. netif_err(priv, drv, dev, "%s: error %d reading reg %02x\n",
  99. __func__, ret, reg);
  100. return val;
  101. }
  102. static void encx24j600_write_reg(struct encx24j600_priv *priv, u8 reg, u16 val)
  103. {
  104. struct net_device *dev = priv->ndev;
  105. int ret = regmap_write(priv->ctx.regmap, reg, val);
  106. if (unlikely(ret))
  107. netif_err(priv, drv, dev, "%s: error %d writing reg %02x=%04x\n",
  108. __func__, ret, reg, val);
  109. }
  110. static void encx24j600_update_reg(struct encx24j600_priv *priv, u8 reg,
  111. u16 mask, u16 val)
  112. {
  113. struct net_device *dev = priv->ndev;
  114. int ret = regmap_update_bits(priv->ctx.regmap, reg, mask, val);
  115. if (unlikely(ret))
  116. netif_err(priv, drv, dev, "%s: error %d updating reg %02x=%04x~%04x\n",
  117. __func__, ret, reg, val, mask);
  118. }
  119. static u16 encx24j600_read_phy(struct encx24j600_priv *priv, u8 reg)
  120. {
  121. struct net_device *dev = priv->ndev;
  122. unsigned int val = 0;
  123. int ret = regmap_read(priv->ctx.phymap, reg, &val);
  124. if (unlikely(ret))
  125. netif_err(priv, drv, dev, "%s: error %d reading %02x\n",
  126. __func__, ret, reg);
  127. return val;
  128. }
  129. static void encx24j600_write_phy(struct encx24j600_priv *priv, u8 reg, u16 val)
  130. {
  131. struct net_device *dev = priv->ndev;
  132. int ret = regmap_write(priv->ctx.phymap, reg, val);
  133. if (unlikely(ret))
  134. netif_err(priv, drv, dev, "%s: error %d writing reg %02x=%04x\n",
  135. __func__, ret, reg, val);
  136. }
  137. static void encx24j600_clr_bits(struct encx24j600_priv *priv, u8 reg, u16 mask)
  138. {
  139. encx24j600_update_reg(priv, reg, mask, 0);
  140. }
  141. static void encx24j600_set_bits(struct encx24j600_priv *priv, u8 reg, u16 mask)
  142. {
  143. encx24j600_update_reg(priv, reg, mask, mask);
  144. }
  145. static void encx24j600_cmd(struct encx24j600_priv *priv, u8 cmd)
  146. {
  147. struct net_device *dev = priv->ndev;
  148. int ret = regmap_write(priv->ctx.regmap, cmd, 0);
  149. if (unlikely(ret))
  150. netif_err(priv, drv, dev, "%s: error %d with cmd %02x\n",
  151. __func__, ret, cmd);
  152. }
  153. static int encx24j600_raw_read(struct encx24j600_priv *priv, u8 reg, u8 *data,
  154. size_t count)
  155. {
  156. int ret;
  157. mutex_lock(&priv->ctx.mutex);
  158. ret = regmap_encx24j600_spi_read(&priv->ctx, reg, data, count);
  159. mutex_unlock(&priv->ctx.mutex);
  160. return ret;
  161. }
  162. static int encx24j600_raw_write(struct encx24j600_priv *priv, u8 reg,
  163. const u8 *data, size_t count)
  164. {
  165. int ret;
  166. mutex_lock(&priv->ctx.mutex);
  167. ret = regmap_encx24j600_spi_write(&priv->ctx, reg, data, count);
  168. mutex_unlock(&priv->ctx.mutex);
  169. return ret;
  170. }
  171. static void encx24j600_update_phcon1(struct encx24j600_priv *priv)
  172. {
  173. u16 phcon1 = encx24j600_read_phy(priv, PHCON1);
  174. if (priv->autoneg == AUTONEG_ENABLE) {
  175. phcon1 |= ANEN | RENEG;
  176. } else {
  177. phcon1 &= ~ANEN;
  178. if (priv->speed == SPEED_100)
  179. phcon1 |= SPD100;
  180. else
  181. phcon1 &= ~SPD100;
  182. if (priv->full_duplex)
  183. phcon1 |= PFULDPX;
  184. else
  185. phcon1 &= ~PFULDPX;
  186. }
  187. encx24j600_write_phy(priv, PHCON1, phcon1);
  188. }
  189. /* Waits for autonegotiation to complete. */
  190. static int encx24j600_wait_for_autoneg(struct encx24j600_priv *priv)
  191. {
  192. struct net_device *dev = priv->ndev;
  193. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  194. u16 phstat1;
  195. u16 estat;
  196. int ret = 0;
  197. phstat1 = encx24j600_read_phy(priv, PHSTAT1);
  198. while ((phstat1 & ANDONE) == 0) {
  199. if (time_after(jiffies, timeout)) {
  200. u16 phstat3;
  201. netif_notice(priv, drv, dev, "timeout waiting for autoneg done\n");
  202. priv->autoneg = AUTONEG_DISABLE;
  203. phstat3 = encx24j600_read_phy(priv, PHSTAT3);
  204. priv->speed = (phstat3 & PHY3SPD100)
  205. ? SPEED_100 : SPEED_10;
  206. priv->full_duplex = (phstat3 & PHY3DPX) ? 1 : 0;
  207. encx24j600_update_phcon1(priv);
  208. netif_notice(priv, drv, dev, "Using parallel detection: %s/%s",
  209. priv->speed == SPEED_100 ? "100" : "10",
  210. priv->full_duplex ? "Full" : "Half");
  211. return -ETIMEDOUT;
  212. }
  213. cpu_relax();
  214. phstat1 = encx24j600_read_phy(priv, PHSTAT1);
  215. }
  216. estat = encx24j600_read_reg(priv, ESTAT);
  217. if (estat & PHYDPX) {
  218. encx24j600_set_bits(priv, MACON2, FULDPX);
  219. encx24j600_write_reg(priv, MABBIPG, 0x15);
  220. } else {
  221. encx24j600_clr_bits(priv, MACON2, FULDPX);
  222. encx24j600_write_reg(priv, MABBIPG, 0x12);
  223. /* Max retransmittions attempt */
  224. encx24j600_write_reg(priv, MACLCON, 0x370f);
  225. }
  226. return ret;
  227. }
  228. /* Access the PHY to determine link status */
  229. static void encx24j600_check_link_status(struct encx24j600_priv *priv)
  230. {
  231. struct net_device *dev = priv->ndev;
  232. u16 estat;
  233. estat = encx24j600_read_reg(priv, ESTAT);
  234. if (estat & PHYLNK) {
  235. if (priv->autoneg == AUTONEG_ENABLE)
  236. encx24j600_wait_for_autoneg(priv);
  237. netif_carrier_on(dev);
  238. netif_info(priv, ifup, dev, "link up\n");
  239. } else {
  240. netif_info(priv, ifdown, dev, "link down\n");
  241. /* Re-enable autoneg since we won't know what we might be
  242. * connected to when the link is brought back up again.
  243. */
  244. priv->autoneg = AUTONEG_ENABLE;
  245. priv->full_duplex = true;
  246. priv->speed = SPEED_100;
  247. netif_carrier_off(dev);
  248. }
  249. }
  250. static void encx24j600_int_link_handler(struct encx24j600_priv *priv)
  251. {
  252. struct net_device *dev = priv->ndev;
  253. netif_dbg(priv, intr, dev, "%s", __func__);
  254. encx24j600_check_link_status(priv);
  255. encx24j600_clr_bits(priv, EIR, LINKIF);
  256. }
  257. static void encx24j600_tx_complete(struct encx24j600_priv *priv, bool err)
  258. {
  259. struct net_device *dev = priv->ndev;
  260. if (!priv->tx_skb) {
  261. BUG();
  262. return;
  263. }
  264. mutex_lock(&priv->lock);
  265. if (err)
  266. dev->stats.tx_errors++;
  267. else
  268. dev->stats.tx_packets++;
  269. dev->stats.tx_bytes += priv->tx_skb->len;
  270. encx24j600_clr_bits(priv, EIR, TXIF | TXABTIF);
  271. netif_dbg(priv, tx_done, dev, "TX Done%s\n", err ? ": Err" : "");
  272. dev_kfree_skb(priv->tx_skb);
  273. priv->tx_skb = NULL;
  274. netif_wake_queue(dev);
  275. mutex_unlock(&priv->lock);
  276. }
  277. static int encx24j600_receive_packet(struct encx24j600_priv *priv,
  278. struct rsv *rsv)
  279. {
  280. struct net_device *dev = priv->ndev;
  281. struct sk_buff *skb = netdev_alloc_skb(dev, rsv->len + NET_IP_ALIGN);
  282. if (!skb) {
  283. pr_err_ratelimited("RX: OOM: packet dropped\n");
  284. dev->stats.rx_dropped++;
  285. return -ENOMEM;
  286. }
  287. skb_reserve(skb, NET_IP_ALIGN);
  288. encx24j600_raw_read(priv, RRXDATA, skb_put(skb, rsv->len), rsv->len);
  289. if (netif_msg_pktdata(priv))
  290. dump_packet("RX", skb->len, skb->data);
  291. skb->dev = dev;
  292. skb->protocol = eth_type_trans(skb, dev);
  293. skb->ip_summed = CHECKSUM_COMPLETE;
  294. /* Maintain stats */
  295. dev->stats.rx_packets++;
  296. dev->stats.rx_bytes += rsv->len;
  297. priv->next_packet = rsv->next_packet;
  298. netif_rx(skb);
  299. return 0;
  300. }
  301. static void encx24j600_rx_packets(struct encx24j600_priv *priv, u8 packet_count)
  302. {
  303. struct net_device *dev = priv->ndev;
  304. while (packet_count--) {
  305. struct rsv rsv;
  306. u16 newrxtail;
  307. encx24j600_write_reg(priv, ERXRDPT, priv->next_packet);
  308. encx24j600_raw_read(priv, RRXDATA, (u8 *)&rsv, sizeof(rsv));
  309. if (netif_msg_rx_status(priv))
  310. encx24j600_dump_rsv(priv, __func__, &rsv);
  311. if (!RSV_GETBIT(rsv.rxstat, RSV_RXOK) ||
  312. (rsv.len > MAX_FRAMELEN)) {
  313. netif_err(priv, rx_err, dev, "RX Error %04x\n",
  314. rsv.rxstat);
  315. dev->stats.rx_errors++;
  316. if (RSV_GETBIT(rsv.rxstat, RSV_CRCERROR))
  317. dev->stats.rx_crc_errors++;
  318. if (RSV_GETBIT(rsv.rxstat, RSV_LENCHECKERR))
  319. dev->stats.rx_frame_errors++;
  320. if (rsv.len > MAX_FRAMELEN)
  321. dev->stats.rx_over_errors++;
  322. } else {
  323. encx24j600_receive_packet(priv, &rsv);
  324. }
  325. newrxtail = priv->next_packet - 2;
  326. if (newrxtail == ENC_RX_BUF_START)
  327. newrxtail = SRAM_SIZE - 2;
  328. encx24j600_cmd(priv, SETPKTDEC);
  329. encx24j600_write_reg(priv, ERXTAIL, newrxtail);
  330. }
  331. }
  332. static irqreturn_t encx24j600_isr(int irq, void *dev_id)
  333. {
  334. struct encx24j600_priv *priv = dev_id;
  335. struct net_device *dev = priv->ndev;
  336. int eir;
  337. /* Clear interrupts */
  338. encx24j600_cmd(priv, CLREIE);
  339. eir = encx24j600_read_reg(priv, EIR);
  340. if (eir & LINKIF)
  341. encx24j600_int_link_handler(priv);
  342. if (eir & TXIF)
  343. encx24j600_tx_complete(priv, false);
  344. if (eir & TXABTIF)
  345. encx24j600_tx_complete(priv, true);
  346. if (eir & RXABTIF) {
  347. if (eir & PCFULIF) {
  348. /* Packet counter is full */
  349. netif_err(priv, rx_err, dev, "Packet counter full\n");
  350. }
  351. dev->stats.rx_dropped++;
  352. encx24j600_clr_bits(priv, EIR, RXABTIF);
  353. }
  354. if (eir & PKTIF) {
  355. u8 packet_count;
  356. mutex_lock(&priv->lock);
  357. packet_count = encx24j600_read_reg(priv, ESTAT) & 0xff;
  358. while (packet_count) {
  359. encx24j600_rx_packets(priv, packet_count);
  360. packet_count = encx24j600_read_reg(priv, ESTAT) & 0xff;
  361. }
  362. mutex_unlock(&priv->lock);
  363. }
  364. /* Enable interrupts */
  365. encx24j600_cmd(priv, SETEIE);
  366. return IRQ_HANDLED;
  367. }
  368. static int encx24j600_soft_reset(struct encx24j600_priv *priv)
  369. {
  370. int ret = 0;
  371. int timeout;
  372. u16 eudast;
  373. /* Write and verify a test value to EUDAST */
  374. regcache_cache_bypass(priv->ctx.regmap, true);
  375. timeout = 10;
  376. do {
  377. encx24j600_write_reg(priv, EUDAST, EUDAST_TEST_VAL);
  378. eudast = encx24j600_read_reg(priv, EUDAST);
  379. usleep_range(25, 100);
  380. } while ((eudast != EUDAST_TEST_VAL) && --timeout);
  381. regcache_cache_bypass(priv->ctx.regmap, false);
  382. if (timeout == 0) {
  383. ret = -ETIMEDOUT;
  384. goto err_out;
  385. }
  386. /* Wait for CLKRDY to become set */
  387. timeout = 10;
  388. while (!(encx24j600_read_reg(priv, ESTAT) & CLKRDY) && --timeout)
  389. usleep_range(25, 100);
  390. if (timeout == 0) {
  391. ret = -ETIMEDOUT;
  392. goto err_out;
  393. }
  394. /* Issue a System Reset command */
  395. encx24j600_cmd(priv, SETETHRST);
  396. usleep_range(25, 100);
  397. /* Confirm that EUDAST has 0000h after system reset */
  398. if (encx24j600_read_reg(priv, EUDAST) != 0) {
  399. ret = -EINVAL;
  400. goto err_out;
  401. }
  402. /* Wait for PHY register and status bits to become available */
  403. usleep_range(256, 1000);
  404. err_out:
  405. return ret;
  406. }
  407. static int encx24j600_hw_reset(struct encx24j600_priv *priv)
  408. {
  409. int ret;
  410. mutex_lock(&priv->lock);
  411. ret = encx24j600_soft_reset(priv);
  412. mutex_unlock(&priv->lock);
  413. return ret;
  414. }
  415. static void encx24j600_reset_hw_tx(struct encx24j600_priv *priv)
  416. {
  417. encx24j600_set_bits(priv, ECON2, TXRST);
  418. encx24j600_clr_bits(priv, ECON2, TXRST);
  419. }
  420. static void encx24j600_hw_init_tx(struct encx24j600_priv *priv)
  421. {
  422. /* Reset TX */
  423. encx24j600_reset_hw_tx(priv);
  424. /* Clear the TXIF flag if were previously set */
  425. encx24j600_clr_bits(priv, EIR, TXIF | TXABTIF);
  426. /* Write the Tx Buffer pointer */
  427. encx24j600_write_reg(priv, EGPWRPT, ENC_TX_BUF_START);
  428. }
  429. static void encx24j600_hw_init_rx(struct encx24j600_priv *priv)
  430. {
  431. encx24j600_cmd(priv, DISABLERX);
  432. /* Set up RX packet start address in the SRAM */
  433. encx24j600_write_reg(priv, ERXST, ENC_RX_BUF_START);
  434. /* Preload the RX Data pointer to the beginning of the RX area */
  435. encx24j600_write_reg(priv, ERXRDPT, ENC_RX_BUF_START);
  436. priv->next_packet = ENC_RX_BUF_START;
  437. /* Set up RX end address in the SRAM */
  438. encx24j600_write_reg(priv, ERXTAIL, ENC_SRAM_SIZE - 2);
  439. /* Reset the user data pointers */
  440. encx24j600_write_reg(priv, EUDAST, ENC_SRAM_SIZE);
  441. encx24j600_write_reg(priv, EUDAND, ENC_SRAM_SIZE + 1);
  442. /* Set Max Frame length */
  443. encx24j600_write_reg(priv, MAMXFL, MAX_FRAMELEN);
  444. }
  445. static void encx24j600_dump_config(struct encx24j600_priv *priv,
  446. const char *msg)
  447. {
  448. pr_info(DRV_NAME ": %s\n", msg);
  449. /* CHIP configuration */
  450. pr_info(DRV_NAME " ECON1: %04X\n", encx24j600_read_reg(priv, ECON1));
  451. pr_info(DRV_NAME " ECON2: %04X\n", encx24j600_read_reg(priv, ECON2));
  452. pr_info(DRV_NAME " ERXFCON: %04X\n", encx24j600_read_reg(priv,
  453. ERXFCON));
  454. pr_info(DRV_NAME " ESTAT: %04X\n", encx24j600_read_reg(priv, ESTAT));
  455. pr_info(DRV_NAME " EIR: %04X\n", encx24j600_read_reg(priv, EIR));
  456. pr_info(DRV_NAME " EIDLED: %04X\n", encx24j600_read_reg(priv, EIDLED));
  457. /* MAC layer configuration */
  458. pr_info(DRV_NAME " MACON1: %04X\n", encx24j600_read_reg(priv, MACON1));
  459. pr_info(DRV_NAME " MACON2: %04X\n", encx24j600_read_reg(priv, MACON2));
  460. pr_info(DRV_NAME " MAIPG: %04X\n", encx24j600_read_reg(priv, MAIPG));
  461. pr_info(DRV_NAME " MACLCON: %04X\n", encx24j600_read_reg(priv,
  462. MACLCON));
  463. pr_info(DRV_NAME " MABBIPG: %04X\n", encx24j600_read_reg(priv,
  464. MABBIPG));
  465. /* PHY configuation */
  466. pr_info(DRV_NAME " PHCON1: %04X\n", encx24j600_read_phy(priv, PHCON1));
  467. pr_info(DRV_NAME " PHCON2: %04X\n", encx24j600_read_phy(priv, PHCON2));
  468. pr_info(DRV_NAME " PHANA: %04X\n", encx24j600_read_phy(priv, PHANA));
  469. pr_info(DRV_NAME " PHANLPA: %04X\n", encx24j600_read_phy(priv,
  470. PHANLPA));
  471. pr_info(DRV_NAME " PHANE: %04X\n", encx24j600_read_phy(priv, PHANE));
  472. pr_info(DRV_NAME " PHSTAT1: %04X\n", encx24j600_read_phy(priv,
  473. PHSTAT1));
  474. pr_info(DRV_NAME " PHSTAT2: %04X\n", encx24j600_read_phy(priv,
  475. PHSTAT2));
  476. pr_info(DRV_NAME " PHSTAT3: %04X\n", encx24j600_read_phy(priv,
  477. PHSTAT3));
  478. }
  479. static void encx24j600_set_rxfilter_mode(struct encx24j600_priv *priv)
  480. {
  481. switch (priv->rxfilter) {
  482. case RXFILTER_PROMISC:
  483. encx24j600_set_bits(priv, MACON1, PASSALL);
  484. encx24j600_write_reg(priv, ERXFCON, UCEN | MCEN | NOTMEEN);
  485. break;
  486. case RXFILTER_MULTI:
  487. encx24j600_clr_bits(priv, MACON1, PASSALL);
  488. encx24j600_write_reg(priv, ERXFCON, UCEN | CRCEN | BCEN | MCEN);
  489. break;
  490. case RXFILTER_NORMAL:
  491. default:
  492. encx24j600_clr_bits(priv, MACON1, PASSALL);
  493. encx24j600_write_reg(priv, ERXFCON, UCEN | CRCEN | BCEN);
  494. break;
  495. }
  496. }
  497. static int encx24j600_hw_init(struct encx24j600_priv *priv)
  498. {
  499. int ret = 0;
  500. u16 macon2;
  501. priv->hw_enabled = false;
  502. /* PHY Leds: link status,
  503. * LEDA: Link State + collision events
  504. * LEDB: Link State + transmit/receive events
  505. */
  506. encx24j600_update_reg(priv, EIDLED, 0xff00, 0xcb00);
  507. /* Loopback disabled */
  508. encx24j600_write_reg(priv, MACON1, 0x9);
  509. /* interpacket gap value */
  510. encx24j600_write_reg(priv, MAIPG, 0x0c12);
  511. /* Write the auto negotiation pattern */
  512. encx24j600_write_phy(priv, PHANA, PHANA_DEFAULT);
  513. encx24j600_update_phcon1(priv);
  514. encx24j600_check_link_status(priv);
  515. macon2 = MACON2_RSV1 | TXCRCEN | PADCFG0 | PADCFG2 | MACON2_DEFER;
  516. if ((priv->autoneg == AUTONEG_DISABLE) && priv->full_duplex)
  517. macon2 |= FULDPX;
  518. encx24j600_set_bits(priv, MACON2, macon2);
  519. priv->rxfilter = RXFILTER_NORMAL;
  520. encx24j600_set_rxfilter_mode(priv);
  521. /* Program the Maximum frame length */
  522. encx24j600_write_reg(priv, MAMXFL, MAX_FRAMELEN);
  523. /* Init Tx pointers */
  524. encx24j600_hw_init_tx(priv);
  525. /* Init Rx pointers */
  526. encx24j600_hw_init_rx(priv);
  527. if (netif_msg_hw(priv))
  528. encx24j600_dump_config(priv, "Hw is initialized");
  529. return ret;
  530. }
  531. static void encx24j600_hw_enable(struct encx24j600_priv *priv)
  532. {
  533. /* Clear the interrupt flags in case was set */
  534. encx24j600_clr_bits(priv, EIR, (PCFULIF | RXABTIF | TXABTIF | TXIF |
  535. PKTIF | LINKIF));
  536. /* Enable the interrupts */
  537. encx24j600_write_reg(priv, EIE, (PCFULIE | RXABTIE | TXABTIE | TXIE |
  538. PKTIE | LINKIE | INTIE));
  539. /* Enable RX */
  540. encx24j600_cmd(priv, ENABLERX);
  541. priv->hw_enabled = true;
  542. }
  543. static void encx24j600_hw_disable(struct encx24j600_priv *priv)
  544. {
  545. /* Disable all interrupts */
  546. encx24j600_write_reg(priv, EIE, 0);
  547. /* Disable RX */
  548. encx24j600_cmd(priv, DISABLERX);
  549. priv->hw_enabled = false;
  550. }
  551. static int encx24j600_setlink(struct net_device *dev, u8 autoneg, u16 speed,
  552. u8 duplex)
  553. {
  554. struct encx24j600_priv *priv = netdev_priv(dev);
  555. int ret = 0;
  556. if (!priv->hw_enabled) {
  557. /* link is in low power mode now; duplex setting
  558. * will take effect on next encx24j600_hw_init()
  559. */
  560. if (speed == SPEED_10 || speed == SPEED_100) {
  561. priv->autoneg = (autoneg == AUTONEG_ENABLE);
  562. priv->full_duplex = (duplex == DUPLEX_FULL);
  563. priv->speed = (speed == SPEED_100);
  564. } else {
  565. netif_warn(priv, link, dev, "unsupported link speed setting\n");
  566. /*speeds other than SPEED_10 and SPEED_100 */
  567. /*are not supported by chip */
  568. ret = -EOPNOTSUPP;
  569. }
  570. } else {
  571. netif_warn(priv, link, dev, "Warning: hw must be disabled to set link mode\n");
  572. ret = -EBUSY;
  573. }
  574. return ret;
  575. }
  576. static void encx24j600_hw_get_macaddr(struct encx24j600_priv *priv,
  577. unsigned char *ethaddr)
  578. {
  579. unsigned short val;
  580. val = encx24j600_read_reg(priv, MAADR1);
  581. ethaddr[0] = val & 0x00ff;
  582. ethaddr[1] = (val & 0xff00) >> 8;
  583. val = encx24j600_read_reg(priv, MAADR2);
  584. ethaddr[2] = val & 0x00ffU;
  585. ethaddr[3] = (val & 0xff00U) >> 8;
  586. val = encx24j600_read_reg(priv, MAADR3);
  587. ethaddr[4] = val & 0x00ffU;
  588. ethaddr[5] = (val & 0xff00U) >> 8;
  589. }
  590. /* Program the hardware MAC address from dev->dev_addr.*/
  591. static int encx24j600_set_hw_macaddr(struct net_device *dev)
  592. {
  593. struct encx24j600_priv *priv = netdev_priv(dev);
  594. if (priv->hw_enabled) {
  595. netif_info(priv, drv, dev, "Hardware must be disabled to set Mac address\n");
  596. return -EBUSY;
  597. }
  598. mutex_lock(&priv->lock);
  599. netif_info(priv, drv, dev, "%s: Setting MAC address to %pM\n",
  600. dev->name, dev->dev_addr);
  601. encx24j600_write_reg(priv, MAADR3, (dev->dev_addr[4] |
  602. dev->dev_addr[5] << 8));
  603. encx24j600_write_reg(priv, MAADR2, (dev->dev_addr[2] |
  604. dev->dev_addr[3] << 8));
  605. encx24j600_write_reg(priv, MAADR1, (dev->dev_addr[0] |
  606. dev->dev_addr[1] << 8));
  607. mutex_unlock(&priv->lock);
  608. return 0;
  609. }
  610. /* Store the new hardware address in dev->dev_addr, and update the MAC.*/
  611. static int encx24j600_set_mac_address(struct net_device *dev, void *addr)
  612. {
  613. struct sockaddr *address = addr;
  614. if (netif_running(dev))
  615. return -EBUSY;
  616. if (!is_valid_ether_addr(address->sa_data))
  617. return -EADDRNOTAVAIL;
  618. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  619. return encx24j600_set_hw_macaddr(dev);
  620. }
  621. static int encx24j600_open(struct net_device *dev)
  622. {
  623. struct encx24j600_priv *priv = netdev_priv(dev);
  624. int ret = request_threaded_irq(priv->ctx.spi->irq, NULL, encx24j600_isr,
  625. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  626. DRV_NAME, priv);
  627. if (unlikely(ret < 0)) {
  628. netdev_err(dev, "request irq %d failed (ret = %d)\n",
  629. priv->ctx.spi->irq, ret);
  630. return ret;
  631. }
  632. encx24j600_hw_disable(priv);
  633. encx24j600_hw_init(priv);
  634. encx24j600_hw_enable(priv);
  635. netif_start_queue(dev);
  636. return 0;
  637. }
  638. static int encx24j600_stop(struct net_device *dev)
  639. {
  640. struct encx24j600_priv *priv = netdev_priv(dev);
  641. netif_stop_queue(dev);
  642. free_irq(priv->ctx.spi->irq, priv);
  643. return 0;
  644. }
  645. static void encx24j600_setrx_proc(struct kthread_work *ws)
  646. {
  647. struct encx24j600_priv *priv =
  648. container_of(ws, struct encx24j600_priv, setrx_work);
  649. mutex_lock(&priv->lock);
  650. encx24j600_set_rxfilter_mode(priv);
  651. mutex_unlock(&priv->lock);
  652. }
  653. static void encx24j600_set_multicast_list(struct net_device *dev)
  654. {
  655. struct encx24j600_priv *priv = netdev_priv(dev);
  656. int oldfilter = priv->rxfilter;
  657. if (dev->flags & IFF_PROMISC) {
  658. netif_dbg(priv, link, dev, "promiscuous mode\n");
  659. priv->rxfilter = RXFILTER_PROMISC;
  660. } else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
  661. netif_dbg(priv, link, dev, "%smulticast mode\n",
  662. (dev->flags & IFF_ALLMULTI) ? "all-" : "");
  663. priv->rxfilter = RXFILTER_MULTI;
  664. } else {
  665. netif_dbg(priv, link, dev, "normal mode\n");
  666. priv->rxfilter = RXFILTER_NORMAL;
  667. }
  668. if (oldfilter != priv->rxfilter)
  669. kthread_queue_work(&priv->kworker, &priv->setrx_work);
  670. }
  671. static void encx24j600_hw_tx(struct encx24j600_priv *priv)
  672. {
  673. struct net_device *dev = priv->ndev;
  674. netif_info(priv, tx_queued, dev, "TX Packet Len:%d\n",
  675. priv->tx_skb->len);
  676. if (netif_msg_pktdata(priv))
  677. dump_packet("TX", priv->tx_skb->len, priv->tx_skb->data);
  678. if (encx24j600_read_reg(priv, EIR) & TXABTIF)
  679. /* Last transmition aborted due to error. Reset TX interface */
  680. encx24j600_reset_hw_tx(priv);
  681. /* Clear the TXIF flag if were previously set */
  682. encx24j600_clr_bits(priv, EIR, TXIF);
  683. /* Set the data pointer to the TX buffer address in the SRAM */
  684. encx24j600_write_reg(priv, EGPWRPT, ENC_TX_BUF_START);
  685. /* Copy the packet into the SRAM */
  686. encx24j600_raw_write(priv, WGPDATA, (u8 *)priv->tx_skb->data,
  687. priv->tx_skb->len);
  688. /* Program the Tx buffer start pointer */
  689. encx24j600_write_reg(priv, ETXST, ENC_TX_BUF_START);
  690. /* Program the packet length */
  691. encx24j600_write_reg(priv, ETXLEN, priv->tx_skb->len);
  692. /* Start the transmission */
  693. encx24j600_cmd(priv, SETTXRTS);
  694. }
  695. static void encx24j600_tx_proc(struct kthread_work *ws)
  696. {
  697. struct encx24j600_priv *priv =
  698. container_of(ws, struct encx24j600_priv, tx_work);
  699. mutex_lock(&priv->lock);
  700. encx24j600_hw_tx(priv);
  701. mutex_unlock(&priv->lock);
  702. }
  703. static netdev_tx_t encx24j600_tx(struct sk_buff *skb, struct net_device *dev)
  704. {
  705. struct encx24j600_priv *priv = netdev_priv(dev);
  706. netif_stop_queue(dev);
  707. /* save the timestamp */
  708. netif_trans_update(dev);
  709. /* Remember the skb for deferred processing */
  710. priv->tx_skb = skb;
  711. kthread_queue_work(&priv->kworker, &priv->tx_work);
  712. return NETDEV_TX_OK;
  713. }
  714. /* Deal with a transmit timeout */
  715. static void encx24j600_tx_timeout(struct net_device *dev)
  716. {
  717. struct encx24j600_priv *priv = netdev_priv(dev);
  718. netif_err(priv, tx_err, dev, "TX timeout at %ld, latency %ld\n",
  719. jiffies, jiffies - dev_trans_start(dev));
  720. dev->stats.tx_errors++;
  721. netif_wake_queue(dev);
  722. return;
  723. }
  724. static int encx24j600_get_regs_len(struct net_device *dev)
  725. {
  726. return SFR_REG_COUNT;
  727. }
  728. static void encx24j600_get_regs(struct net_device *dev,
  729. struct ethtool_regs *regs, void *p)
  730. {
  731. struct encx24j600_priv *priv = netdev_priv(dev);
  732. u16 *buff = p;
  733. u8 reg;
  734. regs->version = 1;
  735. mutex_lock(&priv->lock);
  736. for (reg = 0; reg < SFR_REG_COUNT; reg += 2) {
  737. unsigned int val = 0;
  738. /* ignore errors for unreadable registers */
  739. regmap_read(priv->ctx.regmap, reg, &val);
  740. buff[reg] = val & 0xffff;
  741. }
  742. mutex_unlock(&priv->lock);
  743. }
  744. static void encx24j600_get_drvinfo(struct net_device *dev,
  745. struct ethtool_drvinfo *info)
  746. {
  747. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  748. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  749. strlcpy(info->bus_info, dev_name(dev->dev.parent),
  750. sizeof(info->bus_info));
  751. }
  752. static int encx24j600_get_settings(struct net_device *dev,
  753. struct ethtool_cmd *cmd)
  754. {
  755. struct encx24j600_priv *priv = netdev_priv(dev);
  756. cmd->transceiver = XCVR_INTERNAL;
  757. cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  758. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  759. SUPPORTED_Autoneg | SUPPORTED_TP;
  760. ethtool_cmd_speed_set(cmd, priv->speed);
  761. cmd->duplex = priv->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  762. cmd->port = PORT_TP;
  763. cmd->autoneg = priv->autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  764. return 0;
  765. }
  766. static int encx24j600_set_settings(struct net_device *dev,
  767. struct ethtool_cmd *cmd)
  768. {
  769. return encx24j600_setlink(dev, cmd->autoneg,
  770. ethtool_cmd_speed(cmd), cmd->duplex);
  771. }
  772. static u32 encx24j600_get_msglevel(struct net_device *dev)
  773. {
  774. struct encx24j600_priv *priv = netdev_priv(dev);
  775. return priv->msg_enable;
  776. }
  777. static void encx24j600_set_msglevel(struct net_device *dev, u32 val)
  778. {
  779. struct encx24j600_priv *priv = netdev_priv(dev);
  780. priv->msg_enable = val;
  781. }
  782. static const struct ethtool_ops encx24j600_ethtool_ops = {
  783. .get_settings = encx24j600_get_settings,
  784. .set_settings = encx24j600_set_settings,
  785. .get_drvinfo = encx24j600_get_drvinfo,
  786. .get_msglevel = encx24j600_get_msglevel,
  787. .set_msglevel = encx24j600_set_msglevel,
  788. .get_regs_len = encx24j600_get_regs_len,
  789. .get_regs = encx24j600_get_regs,
  790. };
  791. static const struct net_device_ops encx24j600_netdev_ops = {
  792. .ndo_open = encx24j600_open,
  793. .ndo_stop = encx24j600_stop,
  794. .ndo_start_xmit = encx24j600_tx,
  795. .ndo_set_rx_mode = encx24j600_set_multicast_list,
  796. .ndo_set_mac_address = encx24j600_set_mac_address,
  797. .ndo_tx_timeout = encx24j600_tx_timeout,
  798. .ndo_validate_addr = eth_validate_addr,
  799. };
  800. static int encx24j600_spi_probe(struct spi_device *spi)
  801. {
  802. int ret;
  803. struct net_device *ndev;
  804. struct encx24j600_priv *priv;
  805. u16 eidled;
  806. ndev = alloc_etherdev(sizeof(struct encx24j600_priv));
  807. if (!ndev) {
  808. ret = -ENOMEM;
  809. goto error_out;
  810. }
  811. priv = netdev_priv(ndev);
  812. spi_set_drvdata(spi, priv);
  813. dev_set_drvdata(&spi->dev, priv);
  814. SET_NETDEV_DEV(ndev, &spi->dev);
  815. priv->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  816. priv->ndev = ndev;
  817. /* Default configuration PHY configuration */
  818. priv->full_duplex = true;
  819. priv->autoneg = AUTONEG_ENABLE;
  820. priv->speed = SPEED_100;
  821. priv->ctx.spi = spi;
  822. devm_regmap_init_encx24j600(&spi->dev, &priv->ctx);
  823. ndev->irq = spi->irq;
  824. ndev->netdev_ops = &encx24j600_netdev_ops;
  825. mutex_init(&priv->lock);
  826. /* Reset device and check if it is connected */
  827. if (encx24j600_hw_reset(priv)) {
  828. netif_err(priv, probe, ndev,
  829. DRV_NAME ": Chip is not detected\n");
  830. ret = -EIO;
  831. goto out_free;
  832. }
  833. /* Initialize the device HW to the consistent state */
  834. if (encx24j600_hw_init(priv)) {
  835. netif_err(priv, probe, ndev,
  836. DRV_NAME ": HW initialization error\n");
  837. ret = -EIO;
  838. goto out_free;
  839. }
  840. kthread_init_worker(&priv->kworker);
  841. kthread_init_work(&priv->tx_work, encx24j600_tx_proc);
  842. kthread_init_work(&priv->setrx_work, encx24j600_setrx_proc);
  843. priv->kworker_task = kthread_run(kthread_worker_fn, &priv->kworker,
  844. "encx24j600");
  845. if (IS_ERR(priv->kworker_task)) {
  846. ret = PTR_ERR(priv->kworker_task);
  847. goto out_free;
  848. }
  849. /* Get the MAC address from the chip */
  850. encx24j600_hw_get_macaddr(priv, ndev->dev_addr);
  851. ndev->ethtool_ops = &encx24j600_ethtool_ops;
  852. ret = register_netdev(ndev);
  853. if (unlikely(ret)) {
  854. netif_err(priv, probe, ndev, "Error %d initializing card encx24j600 card\n",
  855. ret);
  856. goto out_free;
  857. }
  858. eidled = encx24j600_read_reg(priv, EIDLED);
  859. if (((eidled & DEVID_MASK) >> DEVID_SHIFT) != ENCX24J600_DEV_ID) {
  860. ret = -EINVAL;
  861. goto out_unregister;
  862. }
  863. netif_info(priv, probe, ndev, "Silicon rev ID: 0x%02x\n",
  864. (eidled & REVID_MASK) >> REVID_SHIFT);
  865. netif_info(priv, drv, priv->ndev, "MAC address %pM\n", ndev->dev_addr);
  866. return ret;
  867. out_unregister:
  868. unregister_netdev(priv->ndev);
  869. out_free:
  870. free_netdev(ndev);
  871. error_out:
  872. return ret;
  873. }
  874. static int encx24j600_spi_remove(struct spi_device *spi)
  875. {
  876. struct encx24j600_priv *priv = dev_get_drvdata(&spi->dev);
  877. unregister_netdev(priv->ndev);
  878. free_netdev(priv->ndev);
  879. return 0;
  880. }
  881. static const struct spi_device_id encx24j600_spi_id_table[] = {
  882. { .name = "encx24j600" },
  883. { /* sentinel */ }
  884. };
  885. MODULE_DEVICE_TABLE(spi, encx24j600_spi_id_table);
  886. static struct spi_driver encx24j600_spi_net_driver = {
  887. .driver = {
  888. .name = DRV_NAME,
  889. .owner = THIS_MODULE,
  890. .bus = &spi_bus_type,
  891. },
  892. .probe = encx24j600_spi_probe,
  893. .remove = encx24j600_spi_remove,
  894. .id_table = encx24j600_spi_id_table,
  895. };
  896. static int __init encx24j600_init(void)
  897. {
  898. return spi_register_driver(&encx24j600_spi_net_driver);
  899. }
  900. module_init(encx24j600_init);
  901. static void encx24j600_exit(void)
  902. {
  903. spi_unregister_driver(&encx24j600_spi_net_driver);
  904. }
  905. module_exit(encx24j600_exit);
  906. MODULE_DESCRIPTION(DRV_NAME " ethernet driver");
  907. MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
  908. MODULE_LICENSE("GPL");
  909. MODULE_ALIAS("spi:" DRV_NAME);