encx24j600.c 29 KB

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