ks8851_common.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* drivers/net/ethernet/micrel/ks8851.c
  3. *
  4. * Copyright 2009 Simtec Electronics
  5. * http://www.simtec.co.uk/
  6. * Ben Dooks <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/cache.h>
  16. #include <linux/crc32.h>
  17. #include <linux/mii.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/of_mdio.h>
  21. #include <linux/of_net.h>
  22. #include "ks8851.h"
  23. /**
  24. * ks8851_lock - register access lock
  25. * @ks: The chip state
  26. * @flags: Spinlock flags
  27. *
  28. * Claim chip register access lock
  29. */
  30. static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
  31. {
  32. ks->lock(ks, flags);
  33. }
  34. /**
  35. * ks8851_unlock - register access unlock
  36. * @ks: The chip state
  37. * @flags: Spinlock flags
  38. *
  39. * Release chip register access lock
  40. */
  41. static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
  42. {
  43. ks->unlock(ks, flags);
  44. }
  45. /**
  46. * ks8851_wrreg16 - write 16bit register value to chip
  47. * @ks: The chip state
  48. * @reg: The register address
  49. * @val: The value to write
  50. *
  51. * Issue a write to put the value @val into the register specified in @reg.
  52. */
  53. static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg,
  54. unsigned int val)
  55. {
  56. ks->wrreg16(ks, reg, val);
  57. }
  58. /**
  59. * ks8851_rdreg16 - read 16 bit register from device
  60. * @ks: The chip information
  61. * @reg: The register address
  62. *
  63. * Read a 16bit register from the chip, returning the result
  64. */
  65. static unsigned int ks8851_rdreg16(struct ks8851_net *ks,
  66. unsigned int reg)
  67. {
  68. return ks->rdreg16(ks, reg);
  69. }
  70. /**
  71. * ks8851_soft_reset - issue one of the soft reset to the device
  72. * @ks: The device state.
  73. * @op: The bit(s) to set in the GRR
  74. *
  75. * Issue the relevant soft-reset command to the device's GRR register
  76. * specified by @op.
  77. *
  78. * Note, the delays are in there as a caution to ensure that the reset
  79. * has time to take effect and then complete. Since the datasheet does
  80. * not currently specify the exact sequence, we have chosen something
  81. * that seems to work with our device.
  82. */
  83. static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
  84. {
  85. ks8851_wrreg16(ks, KS_GRR, op);
  86. mdelay(1); /* wait a short time to effect reset */
  87. ks8851_wrreg16(ks, KS_GRR, 0);
  88. mdelay(1); /* wait for condition to clear */
  89. }
  90. /**
  91. * ks8851_set_powermode - set power mode of the device
  92. * @ks: The device state
  93. * @pwrmode: The power mode value to write to KS_PMECR.
  94. *
  95. * Change the power mode of the chip.
  96. */
  97. static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
  98. {
  99. unsigned pmecr;
  100. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  101. pmecr = ks8851_rdreg16(ks, KS_PMECR);
  102. pmecr &= ~PMECR_PM_MASK;
  103. pmecr |= pwrmode;
  104. ks8851_wrreg16(ks, KS_PMECR, pmecr);
  105. }
  106. /**
  107. * ks8851_write_mac_addr - write mac address to device registers
  108. * @dev: The network device
  109. *
  110. * Update the KS8851 MAC address registers from the address in @dev.
  111. *
  112. * This call assumes that the chip is not running, so there is no need to
  113. * shutdown the RXQ process whilst setting this.
  114. */
  115. static int ks8851_write_mac_addr(struct net_device *dev)
  116. {
  117. struct ks8851_net *ks = netdev_priv(dev);
  118. unsigned long flags;
  119. u16 val;
  120. int i;
  121. ks8851_lock(ks, &flags);
  122. /*
  123. * Wake up chip in case it was powered off when stopped; otherwise,
  124. * the first write to the MAC address does not take effect.
  125. */
  126. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  127. for (i = 0; i < ETH_ALEN; i += 2) {
  128. val = (dev->dev_addr[i] << 8) | dev->dev_addr[i + 1];
  129. ks8851_wrreg16(ks, KS_MAR(i), val);
  130. }
  131. if (!netif_running(dev))
  132. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  133. ks8851_unlock(ks, &flags);
  134. return 0;
  135. }
  136. /**
  137. * ks8851_read_mac_addr - read mac address from device registers
  138. * @dev: The network device
  139. *
  140. * Update our copy of the KS8851 MAC address from the registers of @dev.
  141. */
  142. static void ks8851_read_mac_addr(struct net_device *dev)
  143. {
  144. struct ks8851_net *ks = netdev_priv(dev);
  145. unsigned long flags;
  146. u8 addr[ETH_ALEN];
  147. u16 reg;
  148. int i;
  149. ks8851_lock(ks, &flags);
  150. for (i = 0; i < ETH_ALEN; i += 2) {
  151. reg = ks8851_rdreg16(ks, KS_MAR(i));
  152. addr[i] = reg >> 8;
  153. addr[i + 1] = reg & 0xff;
  154. }
  155. eth_hw_addr_set(dev, addr);
  156. ks8851_unlock(ks, &flags);
  157. }
  158. /**
  159. * ks8851_init_mac - initialise the mac address
  160. * @ks: The device structure
  161. * @np: The device node pointer
  162. *
  163. * Get or create the initial mac address for the device and then set that
  164. * into the station address register. A mac address supplied in the device
  165. * tree takes precedence. Otherwise, if there is an EEPROM present, then
  166. * we try that. If no valid mac address is found we use eth_random_addr()
  167. * to create a new one.
  168. */
  169. static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
  170. {
  171. struct net_device *dev = ks->netdev;
  172. int ret;
  173. ret = of_get_ethdev_address(np, dev);
  174. if (!ret) {
  175. ks8851_write_mac_addr(dev);
  176. return;
  177. }
  178. if (ks->rc_ccr & CCR_EEPROM) {
  179. ks8851_read_mac_addr(dev);
  180. if (is_valid_ether_addr(dev->dev_addr))
  181. return;
  182. netdev_err(ks->netdev, "invalid mac address read %pM\n",
  183. dev->dev_addr);
  184. }
  185. eth_hw_addr_random(dev);
  186. ks8851_write_mac_addr(dev);
  187. }
  188. /**
  189. * ks8851_dbg_dumpkkt - dump initial packet contents to debug
  190. * @ks: The device state
  191. * @rxpkt: The data for the received packet
  192. *
  193. * Dump the initial data from the packet to dev_dbg().
  194. */
  195. static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
  196. {
  197. netdev_dbg(ks->netdev,
  198. "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  199. rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
  200. rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
  201. rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
  202. }
  203. /**
  204. * ks8851_rx_skb - receive skbuff
  205. * @ks: The device state.
  206. * @skb: The skbuff
  207. */
  208. static void ks8851_rx_skb(struct ks8851_net *ks, struct sk_buff *skb)
  209. {
  210. ks->rx_skb(ks, skb);
  211. }
  212. /**
  213. * ks8851_rx_pkts - receive packets from the host
  214. * @ks: The device information.
  215. *
  216. * This is called from the IRQ work queue when the system detects that there
  217. * are packets in the receive queue. Find out how many packets there are and
  218. * read them from the FIFO.
  219. */
  220. static void ks8851_rx_pkts(struct ks8851_net *ks)
  221. {
  222. struct sk_buff *skb;
  223. unsigned rxfc;
  224. unsigned rxlen;
  225. unsigned rxstat;
  226. u8 *rxpkt;
  227. rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
  228. netif_dbg(ks, rx_status, ks->netdev,
  229. "%s: %d packets\n", __func__, rxfc);
  230. /* Currently we're issuing a read per packet, but we could possibly
  231. * improve the code by issuing a single read, getting the receive
  232. * header, allocating the packet and then reading the packet data
  233. * out in one go.
  234. *
  235. * This form of operation would require us to hold the SPI bus'
  236. * chipselect low during the entie transaction to avoid any
  237. * reset to the data stream coming from the chip.
  238. */
  239. for (; rxfc != 0; rxfc--) {
  240. rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
  241. rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
  242. netif_dbg(ks, rx_status, ks->netdev,
  243. "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
  244. /* the length of the packet includes the 32bit CRC */
  245. /* set dma read address */
  246. ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
  247. /* start DMA access */
  248. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  249. if (rxlen > 4) {
  250. unsigned int rxalign;
  251. rxlen -= 4;
  252. rxalign = ALIGN(rxlen, 4);
  253. skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
  254. if (skb) {
  255. /* 4 bytes of status header + 4 bytes of
  256. * garbage: we put them before ethernet
  257. * header, so that they are copied,
  258. * but ignored.
  259. */
  260. rxpkt = skb_put(skb, rxlen) - 8;
  261. ks->rdfifo(ks, rxpkt, rxalign + 8);
  262. if (netif_msg_pktdata(ks))
  263. ks8851_dbg_dumpkkt(ks, rxpkt);
  264. skb->protocol = eth_type_trans(skb, ks->netdev);
  265. ks8851_rx_skb(ks, skb);
  266. ks->netdev->stats.rx_packets++;
  267. ks->netdev->stats.rx_bytes += rxlen;
  268. }
  269. }
  270. /* end DMA access and dequeue packet */
  271. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
  272. }
  273. }
  274. /**
  275. * ks8851_irq - IRQ handler for dealing with interrupt requests
  276. * @irq: IRQ number
  277. * @_ks: cookie
  278. *
  279. * This handler is invoked when the IRQ line asserts to find out what happened.
  280. * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
  281. * in thread context.
  282. *
  283. * Read the interrupt status, work out what needs to be done and then clear
  284. * any of the interrupts that are not needed.
  285. */
  286. static irqreturn_t ks8851_irq(int irq, void *_ks)
  287. {
  288. struct ks8851_net *ks = _ks;
  289. unsigned handled = 0;
  290. unsigned long flags;
  291. unsigned int status;
  292. ks8851_lock(ks, &flags);
  293. status = ks8851_rdreg16(ks, KS_ISR);
  294. netif_dbg(ks, intr, ks->netdev,
  295. "%s: status 0x%04x\n", __func__, status);
  296. if (status & IRQ_LCI)
  297. handled |= IRQ_LCI;
  298. if (status & IRQ_LDI) {
  299. u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
  300. pmecr &= ~PMECR_WKEVT_MASK;
  301. ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  302. handled |= IRQ_LDI;
  303. }
  304. if (status & IRQ_RXPSI)
  305. handled |= IRQ_RXPSI;
  306. if (status & IRQ_TXI) {
  307. handled |= IRQ_TXI;
  308. /* no lock here, tx queue should have been stopped */
  309. /* update our idea of how much tx space is available to the
  310. * system */
  311. ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
  312. netif_dbg(ks, intr, ks->netdev,
  313. "%s: txspace %d\n", __func__, ks->tx_space);
  314. }
  315. if (status & IRQ_RXI)
  316. handled |= IRQ_RXI;
  317. if (status & IRQ_SPIBEI) {
  318. netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
  319. handled |= IRQ_SPIBEI;
  320. }
  321. ks8851_wrreg16(ks, KS_ISR, handled);
  322. if (status & IRQ_RXI) {
  323. /* the datasheet says to disable the rx interrupt during
  324. * packet read-out, however we're masking the interrupt
  325. * from the device so do not bother masking just the RX
  326. * from the device. */
  327. ks8851_rx_pkts(ks);
  328. }
  329. /* if something stopped the rx process, probably due to wanting
  330. * to change the rx settings, then do something about restarting
  331. * it. */
  332. if (status & IRQ_RXPSI) {
  333. struct ks8851_rxctrl *rxc = &ks->rxctrl;
  334. /* update the multicast hash table */
  335. ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
  336. ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
  337. ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
  338. ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
  339. ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
  340. ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
  341. }
  342. ks8851_unlock(ks, &flags);
  343. if (status & IRQ_LCI)
  344. mii_check_link(&ks->mii);
  345. if (status & IRQ_TXI)
  346. netif_wake_queue(ks->netdev);
  347. return IRQ_HANDLED;
  348. }
  349. /**
  350. * ks8851_flush_tx_work - flush outstanding TX work
  351. * @ks: The device state
  352. */
  353. static void ks8851_flush_tx_work(struct ks8851_net *ks)
  354. {
  355. if (ks->flush_tx_work)
  356. ks->flush_tx_work(ks);
  357. }
  358. /**
  359. * ks8851_net_open - open network device
  360. * @dev: The network device being opened.
  361. *
  362. * Called when the network device is marked active, such as a user executing
  363. * 'ifconfig up' on the device.
  364. */
  365. static int ks8851_net_open(struct net_device *dev)
  366. {
  367. struct ks8851_net *ks = netdev_priv(dev);
  368. unsigned long flags;
  369. int ret;
  370. ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
  371. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  372. dev->name, ks);
  373. if (ret < 0) {
  374. netdev_err(dev, "failed to get irq\n");
  375. return ret;
  376. }
  377. /* lock the card, even if we may not actually be doing anything
  378. * else at the moment */
  379. ks8851_lock(ks, &flags);
  380. netif_dbg(ks, ifup, ks->netdev, "opening\n");
  381. /* bring chip out of any power saving mode it was in */
  382. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  383. /* issue a soft reset to the RX/TX QMU to put it into a known
  384. * state. */
  385. ks8851_soft_reset(ks, GRR_QMU);
  386. /* setup transmission parameters */
  387. ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
  388. TXCR_TXPE | /* pad to min length */
  389. TXCR_TXCRC | /* add CRC */
  390. TXCR_TXFCE)); /* enable flow control */
  391. /* auto-increment tx data, reset tx pointer */
  392. ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  393. /* setup receiver control */
  394. ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
  395. RXCR1_RXFCE | /* enable flow control */
  396. RXCR1_RXBE | /* broadcast enable */
  397. RXCR1_RXUE | /* unicast enable */
  398. RXCR1_RXE)); /* enable rx block */
  399. /* transfer entire frames out in one go */
  400. ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
  401. /* set receive counter timeouts */
  402. ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
  403. ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
  404. ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
  405. ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
  406. RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
  407. RXQCR_RXDTTE); /* IRQ on time exceeded */
  408. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  409. /* clear then enable interrupts */
  410. ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
  411. ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
  412. netif_start_queue(ks->netdev);
  413. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  414. ks8851_unlock(ks, &flags);
  415. mii_check_link(&ks->mii);
  416. return 0;
  417. }
  418. /**
  419. * ks8851_net_stop - close network device
  420. * @dev: The device being closed.
  421. *
  422. * Called to close down a network device which has been active. Cancell any
  423. * work, shutdown the RX and TX process and then place the chip into a low
  424. * power state whilst it is not being used.
  425. */
  426. static int ks8851_net_stop(struct net_device *dev)
  427. {
  428. struct ks8851_net *ks = netdev_priv(dev);
  429. unsigned long flags;
  430. netif_info(ks, ifdown, dev, "shutting down\n");
  431. netif_stop_queue(dev);
  432. ks8851_lock(ks, &flags);
  433. /* turn off the IRQs and ack any outstanding */
  434. ks8851_wrreg16(ks, KS_IER, 0x0000);
  435. ks8851_wrreg16(ks, KS_ISR, 0xffff);
  436. ks8851_unlock(ks, &flags);
  437. /* stop any outstanding work */
  438. ks8851_flush_tx_work(ks);
  439. flush_work(&ks->rxctrl_work);
  440. ks8851_lock(ks, &flags);
  441. /* shutdown RX process */
  442. ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
  443. /* shutdown TX process */
  444. ks8851_wrreg16(ks, KS_TXCR, 0x0000);
  445. /* set powermode to soft power down to save power */
  446. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  447. ks8851_unlock(ks, &flags);
  448. /* ensure any queued tx buffers are dumped */
  449. while (!skb_queue_empty(&ks->txq)) {
  450. struct sk_buff *txb = skb_dequeue(&ks->txq);
  451. netif_dbg(ks, ifdown, ks->netdev,
  452. "%s: freeing txb %p\n", __func__, txb);
  453. dev_kfree_skb(txb);
  454. }
  455. free_irq(dev->irq, ks);
  456. return 0;
  457. }
  458. /**
  459. * ks8851_start_xmit - transmit packet
  460. * @skb: The buffer to transmit
  461. * @dev: The device used to transmit the packet.
  462. *
  463. * Called by the network layer to transmit the @skb. Queue the packet for
  464. * the device and schedule the necessary work to transmit the packet when
  465. * it is free.
  466. *
  467. * We do this to firstly avoid sleeping with the network device locked,
  468. * and secondly so we can round up more than one packet to transmit which
  469. * means we can try and avoid generating too many transmit done interrupts.
  470. */
  471. static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
  472. struct net_device *dev)
  473. {
  474. struct ks8851_net *ks = netdev_priv(dev);
  475. return ks->start_xmit(skb, dev);
  476. }
  477. /**
  478. * ks8851_rxctrl_work - work handler to change rx mode
  479. * @work: The work structure this belongs to.
  480. *
  481. * Lock the device and issue the necessary changes to the receive mode from
  482. * the network device layer. This is done so that we can do this without
  483. * having to sleep whilst holding the network device lock.
  484. *
  485. * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
  486. * receive parameters are programmed, we issue a write to disable the RXQ and
  487. * then wait for the interrupt handler to be triggered once the RXQ shutdown is
  488. * complete. The interrupt handler then writes the new values into the chip.
  489. */
  490. static void ks8851_rxctrl_work(struct work_struct *work)
  491. {
  492. struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
  493. unsigned long flags;
  494. ks8851_lock(ks, &flags);
  495. /* need to shutdown RXQ before modifying filter parameters */
  496. ks8851_wrreg16(ks, KS_RXCR1, 0x00);
  497. ks8851_unlock(ks, &flags);
  498. }
  499. static void ks8851_set_rx_mode(struct net_device *dev)
  500. {
  501. struct ks8851_net *ks = netdev_priv(dev);
  502. struct ks8851_rxctrl rxctrl;
  503. memset(&rxctrl, 0, sizeof(rxctrl));
  504. if (dev->flags & IFF_PROMISC) {
  505. /* interface to receive everything */
  506. rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
  507. } else if (dev->flags & IFF_ALLMULTI) {
  508. /* accept all multicast packets */
  509. rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
  510. RXCR1_RXPAFMA | RXCR1_RXMAFMA);
  511. } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
  512. struct netdev_hw_addr *ha;
  513. u32 crc;
  514. /* accept some multicast */
  515. netdev_for_each_mc_addr(ha, dev) {
  516. crc = ether_crc(ETH_ALEN, ha->addr);
  517. crc >>= (32 - 6); /* get top six bits */
  518. rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
  519. }
  520. rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
  521. } else {
  522. /* just accept broadcast / unicast */
  523. rxctrl.rxcr1 = RXCR1_RXPAFMA;
  524. }
  525. rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
  526. RXCR1_RXBE | /* broadcast enable */
  527. RXCR1_RXE | /* RX process enable */
  528. RXCR1_RXFCE); /* enable flow control */
  529. rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
  530. /* schedule work to do the actual set of the data if needed */
  531. spin_lock(&ks->statelock);
  532. if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
  533. memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
  534. schedule_work(&ks->rxctrl_work);
  535. }
  536. spin_unlock(&ks->statelock);
  537. }
  538. static int ks8851_set_mac_address(struct net_device *dev, void *addr)
  539. {
  540. struct sockaddr *sa = addr;
  541. if (netif_running(dev))
  542. return -EBUSY;
  543. if (!is_valid_ether_addr(sa->sa_data))
  544. return -EADDRNOTAVAIL;
  545. eth_hw_addr_set(dev, sa->sa_data);
  546. return ks8851_write_mac_addr(dev);
  547. }
  548. static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  549. {
  550. struct ks8851_net *ks = netdev_priv(dev);
  551. if (!netif_running(dev))
  552. return -EINVAL;
  553. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  554. }
  555. static const struct net_device_ops ks8851_netdev_ops = {
  556. .ndo_open = ks8851_net_open,
  557. .ndo_stop = ks8851_net_stop,
  558. .ndo_eth_ioctl = ks8851_net_ioctl,
  559. .ndo_start_xmit = ks8851_start_xmit,
  560. .ndo_set_mac_address = ks8851_set_mac_address,
  561. .ndo_set_rx_mode = ks8851_set_rx_mode,
  562. .ndo_validate_addr = eth_validate_addr,
  563. };
  564. /* ethtool support */
  565. static void ks8851_get_drvinfo(struct net_device *dev,
  566. struct ethtool_drvinfo *di)
  567. {
  568. strscpy(di->driver, "KS8851", sizeof(di->driver));
  569. strscpy(di->version, "1.00", sizeof(di->version));
  570. strscpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
  571. }
  572. static u32 ks8851_get_msglevel(struct net_device *dev)
  573. {
  574. struct ks8851_net *ks = netdev_priv(dev);
  575. return ks->msg_enable;
  576. }
  577. static void ks8851_set_msglevel(struct net_device *dev, u32 to)
  578. {
  579. struct ks8851_net *ks = netdev_priv(dev);
  580. ks->msg_enable = to;
  581. }
  582. static int ks8851_get_link_ksettings(struct net_device *dev,
  583. struct ethtool_link_ksettings *cmd)
  584. {
  585. struct ks8851_net *ks = netdev_priv(dev);
  586. mii_ethtool_get_link_ksettings(&ks->mii, cmd);
  587. return 0;
  588. }
  589. static int ks8851_set_link_ksettings(struct net_device *dev,
  590. const struct ethtool_link_ksettings *cmd)
  591. {
  592. struct ks8851_net *ks = netdev_priv(dev);
  593. return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
  594. }
  595. static u32 ks8851_get_link(struct net_device *dev)
  596. {
  597. struct ks8851_net *ks = netdev_priv(dev);
  598. return mii_link_ok(&ks->mii);
  599. }
  600. static int ks8851_nway_reset(struct net_device *dev)
  601. {
  602. struct ks8851_net *ks = netdev_priv(dev);
  603. return mii_nway_restart(&ks->mii);
  604. }
  605. /* EEPROM support */
  606. static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
  607. {
  608. struct ks8851_net *ks = ee->data;
  609. unsigned val;
  610. val = ks8851_rdreg16(ks, KS_EEPCR);
  611. ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
  612. ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
  613. ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
  614. }
  615. static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
  616. {
  617. struct ks8851_net *ks = ee->data;
  618. unsigned val = EEPCR_EESA; /* default - eeprom access on */
  619. if (ee->drive_data)
  620. val |= EEPCR_EESRWA;
  621. if (ee->reg_data_in)
  622. val |= EEPCR_EEDO;
  623. if (ee->reg_data_clock)
  624. val |= EEPCR_EESCK;
  625. if (ee->reg_chip_select)
  626. val |= EEPCR_EECS;
  627. ks8851_wrreg16(ks, KS_EEPCR, val);
  628. }
  629. /**
  630. * ks8851_eeprom_claim - claim device EEPROM and activate the interface
  631. * @ks: The network device state.
  632. *
  633. * Check for the presence of an EEPROM, and then activate software access
  634. * to the device.
  635. */
  636. static int ks8851_eeprom_claim(struct ks8851_net *ks)
  637. {
  638. /* start with clock low, cs high */
  639. ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
  640. return 0;
  641. }
  642. /**
  643. * ks8851_eeprom_release - release the EEPROM interface
  644. * @ks: The device state
  645. *
  646. * Release the software access to the device EEPROM
  647. */
  648. static void ks8851_eeprom_release(struct ks8851_net *ks)
  649. {
  650. unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
  651. ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
  652. }
  653. #define KS_EEPROM_MAGIC (0x00008851)
  654. static int ks8851_set_eeprom(struct net_device *dev,
  655. struct ethtool_eeprom *ee, u8 *data)
  656. {
  657. struct ks8851_net *ks = netdev_priv(dev);
  658. int offset = ee->offset;
  659. unsigned long flags;
  660. int len = ee->len;
  661. u16 tmp;
  662. /* currently only support byte writing */
  663. if (len != 1)
  664. return -EINVAL;
  665. if (ee->magic != KS_EEPROM_MAGIC)
  666. return -EINVAL;
  667. if (!(ks->rc_ccr & CCR_EEPROM))
  668. return -ENOENT;
  669. ks8851_lock(ks, &flags);
  670. ks8851_eeprom_claim(ks);
  671. eeprom_93cx6_wren(&ks->eeprom, true);
  672. /* ethtool currently only supports writing bytes, which means
  673. * we have to read/modify/write our 16bit EEPROMs */
  674. eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
  675. if (offset & 1) {
  676. tmp &= 0xff;
  677. tmp |= *data << 8;
  678. } else {
  679. tmp &= 0xff00;
  680. tmp |= *data;
  681. }
  682. eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
  683. eeprom_93cx6_wren(&ks->eeprom, false);
  684. ks8851_eeprom_release(ks);
  685. ks8851_unlock(ks, &flags);
  686. return 0;
  687. }
  688. static int ks8851_get_eeprom(struct net_device *dev,
  689. struct ethtool_eeprom *ee, u8 *data)
  690. {
  691. struct ks8851_net *ks = netdev_priv(dev);
  692. int offset = ee->offset;
  693. unsigned long flags;
  694. int len = ee->len;
  695. /* must be 2 byte aligned */
  696. if (len & 1 || offset & 1)
  697. return -EINVAL;
  698. if (!(ks->rc_ccr & CCR_EEPROM))
  699. return -ENOENT;
  700. ks8851_lock(ks, &flags);
  701. ks8851_eeprom_claim(ks);
  702. ee->magic = KS_EEPROM_MAGIC;
  703. eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
  704. ks8851_eeprom_release(ks);
  705. ks8851_unlock(ks, &flags);
  706. return 0;
  707. }
  708. static int ks8851_get_eeprom_len(struct net_device *dev)
  709. {
  710. struct ks8851_net *ks = netdev_priv(dev);
  711. /* currently, we assume it is an 93C46 attached, so return 128 */
  712. return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
  713. }
  714. static const struct ethtool_ops ks8851_ethtool_ops = {
  715. .get_drvinfo = ks8851_get_drvinfo,
  716. .get_msglevel = ks8851_get_msglevel,
  717. .set_msglevel = ks8851_set_msglevel,
  718. .get_link = ks8851_get_link,
  719. .nway_reset = ks8851_nway_reset,
  720. .get_eeprom_len = ks8851_get_eeprom_len,
  721. .get_eeprom = ks8851_get_eeprom,
  722. .set_eeprom = ks8851_set_eeprom,
  723. .get_link_ksettings = ks8851_get_link_ksettings,
  724. .set_link_ksettings = ks8851_set_link_ksettings,
  725. };
  726. /* MII interface controls */
  727. /**
  728. * ks8851_phy_reg - convert MII register into a KS8851 register
  729. * @reg: MII register number.
  730. *
  731. * Return the KS8851 register number for the corresponding MII PHY register
  732. * if possible. Return zero if the MII register has no direct mapping to the
  733. * KS8851 register set.
  734. */
  735. static int ks8851_phy_reg(int reg)
  736. {
  737. switch (reg) {
  738. case MII_BMCR:
  739. return KS_P1MBCR;
  740. case MII_BMSR:
  741. return KS_P1MBSR;
  742. case MII_PHYSID1:
  743. return KS_PHY1ILR;
  744. case MII_PHYSID2:
  745. return KS_PHY1IHR;
  746. case MII_ADVERTISE:
  747. return KS_P1ANAR;
  748. case MII_LPA:
  749. return KS_P1ANLPR;
  750. }
  751. return -EOPNOTSUPP;
  752. }
  753. static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
  754. {
  755. struct ks8851_net *ks = netdev_priv(dev);
  756. unsigned long flags;
  757. int result;
  758. int ksreg;
  759. ksreg = ks8851_phy_reg(reg);
  760. if (ksreg < 0)
  761. return ksreg;
  762. ks8851_lock(ks, &flags);
  763. result = ks8851_rdreg16(ks, ksreg);
  764. ks8851_unlock(ks, &flags);
  765. return result;
  766. }
  767. /**
  768. * ks8851_phy_read - MII interface PHY register read.
  769. * @dev: The network device the PHY is on.
  770. * @phy_addr: Address of PHY (ignored as we only have one)
  771. * @reg: The register to read.
  772. *
  773. * This call reads data from the PHY register specified in @reg. Since the
  774. * device does not support all the MII registers, the non-existent values
  775. * are always returned as zero.
  776. *
  777. * We return zero for unsupported registers as the MII code does not check
  778. * the value returned for any error status, and simply returns it to the
  779. * caller. The mii-tool that the driver was tested with takes any -ve error
  780. * as real PHY capabilities, thus displaying incorrect data to the user.
  781. */
  782. static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
  783. {
  784. int ret;
  785. ret = ks8851_phy_read_common(dev, phy_addr, reg);
  786. if (ret < 0)
  787. return 0x0; /* no error return allowed, so use zero */
  788. return ret;
  789. }
  790. static void ks8851_phy_write(struct net_device *dev,
  791. int phy, int reg, int value)
  792. {
  793. struct ks8851_net *ks = netdev_priv(dev);
  794. unsigned long flags;
  795. int ksreg;
  796. ksreg = ks8851_phy_reg(reg);
  797. if (ksreg >= 0) {
  798. ks8851_lock(ks, &flags);
  799. ks8851_wrreg16(ks, ksreg, value);
  800. ks8851_unlock(ks, &flags);
  801. }
  802. }
  803. static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  804. {
  805. struct ks8851_net *ks = bus->priv;
  806. if (phy_id != 0)
  807. return -EOPNOTSUPP;
  808. /* KS8851 PHY ID registers are swapped in HW, swap them back. */
  809. if (reg == MII_PHYSID1)
  810. reg = MII_PHYSID2;
  811. else if (reg == MII_PHYSID2)
  812. reg = MII_PHYSID1;
  813. return ks8851_phy_read_common(ks->netdev, phy_id, reg);
  814. }
  815. static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  816. {
  817. struct ks8851_net *ks = bus->priv;
  818. ks8851_phy_write(ks->netdev, phy_id, reg, val);
  819. return 0;
  820. }
  821. /**
  822. * ks8851_read_selftest - read the selftest memory info.
  823. * @ks: The device state
  824. *
  825. * Read and check the TX/RX memory selftest information.
  826. */
  827. static void ks8851_read_selftest(struct ks8851_net *ks)
  828. {
  829. unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
  830. unsigned rd;
  831. rd = ks8851_rdreg16(ks, KS_MBIR);
  832. if ((rd & both_done) != both_done) {
  833. netdev_warn(ks->netdev, "Memory selftest not finished\n");
  834. return;
  835. }
  836. if (rd & MBIR_TXMBFA)
  837. netdev_err(ks->netdev, "TX memory selftest fail\n");
  838. if (rd & MBIR_RXMBFA)
  839. netdev_err(ks->netdev, "RX memory selftest fail\n");
  840. }
  841. /* driver bus management functions */
  842. #ifdef CONFIG_PM_SLEEP
  843. int ks8851_suspend(struct device *dev)
  844. {
  845. struct ks8851_net *ks = dev_get_drvdata(dev);
  846. struct net_device *netdev = ks->netdev;
  847. if (netif_running(netdev)) {
  848. netif_device_detach(netdev);
  849. ks8851_net_stop(netdev);
  850. }
  851. return 0;
  852. }
  853. EXPORT_SYMBOL_GPL(ks8851_suspend);
  854. int ks8851_resume(struct device *dev)
  855. {
  856. struct ks8851_net *ks = dev_get_drvdata(dev);
  857. struct net_device *netdev = ks->netdev;
  858. if (netif_running(netdev)) {
  859. ks8851_net_open(netdev);
  860. netif_device_attach(netdev);
  861. }
  862. return 0;
  863. }
  864. EXPORT_SYMBOL_GPL(ks8851_resume);
  865. #endif
  866. static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev)
  867. {
  868. struct mii_bus *mii_bus;
  869. int ret;
  870. mii_bus = mdiobus_alloc();
  871. if (!mii_bus)
  872. return -ENOMEM;
  873. mii_bus->name = "ks8851_eth_mii";
  874. mii_bus->read = ks8851_mdio_read;
  875. mii_bus->write = ks8851_mdio_write;
  876. mii_bus->priv = ks;
  877. mii_bus->parent = dev;
  878. mii_bus->phy_mask = ~((u32)BIT(0));
  879. snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
  880. ret = mdiobus_register(mii_bus);
  881. if (ret)
  882. goto err_mdiobus_register;
  883. ks->mii_bus = mii_bus;
  884. return 0;
  885. err_mdiobus_register:
  886. mdiobus_free(mii_bus);
  887. return ret;
  888. }
  889. static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
  890. {
  891. mdiobus_unregister(ks->mii_bus);
  892. mdiobus_free(ks->mii_bus);
  893. }
  894. int ks8851_probe_common(struct net_device *netdev, struct device *dev,
  895. int msg_en)
  896. {
  897. struct ks8851_net *ks = netdev_priv(netdev);
  898. unsigned cider;
  899. int ret;
  900. ks->netdev = netdev;
  901. ks->tx_space = 6144;
  902. ks->gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  903. ret = PTR_ERR_OR_ZERO(ks->gpio);
  904. if (ret) {
  905. if (ret != -EPROBE_DEFER)
  906. dev_err(dev, "reset gpio request failed: %d\n", ret);
  907. return ret;
  908. }
  909. ret = gpiod_set_consumer_name(ks->gpio, "ks8851_rst_n");
  910. if (ret) {
  911. dev_err(dev, "failed to set reset gpio name: %d\n", ret);
  912. return ret;
  913. }
  914. ks->vdd_io = devm_regulator_get(dev, "vdd-io");
  915. if (IS_ERR(ks->vdd_io)) {
  916. ret = PTR_ERR(ks->vdd_io);
  917. goto err_reg_io;
  918. }
  919. ret = regulator_enable(ks->vdd_io);
  920. if (ret) {
  921. dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
  922. goto err_reg_io;
  923. }
  924. ks->vdd_reg = devm_regulator_get(dev, "vdd");
  925. if (IS_ERR(ks->vdd_reg)) {
  926. ret = PTR_ERR(ks->vdd_reg);
  927. goto err_reg;
  928. }
  929. ret = regulator_enable(ks->vdd_reg);
  930. if (ret) {
  931. dev_err(dev, "regulator vdd enable fail: %d\n", ret);
  932. goto err_reg;
  933. }
  934. if (ks->gpio) {
  935. usleep_range(10000, 11000);
  936. gpiod_set_value_cansleep(ks->gpio, 0);
  937. }
  938. spin_lock_init(&ks->statelock);
  939. INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
  940. SET_NETDEV_DEV(netdev, dev);
  941. /* setup EEPROM state */
  942. ks->eeprom.data = ks;
  943. ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
  944. ks->eeprom.register_read = ks8851_eeprom_regread;
  945. ks->eeprom.register_write = ks8851_eeprom_regwrite;
  946. /* setup mii state */
  947. ks->mii.dev = netdev;
  948. ks->mii.phy_id = 1;
  949. ks->mii.phy_id_mask = 1;
  950. ks->mii.reg_num_mask = 0xf;
  951. ks->mii.mdio_read = ks8851_phy_read;
  952. ks->mii.mdio_write = ks8851_phy_write;
  953. dev_info(dev, "message enable is %d\n", msg_en);
  954. ret = ks8851_register_mdiobus(ks, dev);
  955. if (ret)
  956. goto err_mdio;
  957. /* set the default message enable */
  958. ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
  959. NETIF_MSG_PROBE |
  960. NETIF_MSG_LINK);
  961. skb_queue_head_init(&ks->txq);
  962. netdev->ethtool_ops = &ks8851_ethtool_ops;
  963. dev_set_drvdata(dev, ks);
  964. netif_carrier_off(ks->netdev);
  965. netdev->if_port = IF_PORT_100BASET;
  966. netdev->netdev_ops = &ks8851_netdev_ops;
  967. /* issue a global soft reset to reset the device. */
  968. ks8851_soft_reset(ks, GRR_GSR);
  969. /* simple check for a valid chip being connected to the bus */
  970. cider = ks8851_rdreg16(ks, KS_CIDER);
  971. if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
  972. dev_err(dev, "failed to read device ID\n");
  973. ret = -ENODEV;
  974. goto err_id;
  975. }
  976. /* cache the contents of the CCR register for EEPROM, etc. */
  977. ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
  978. ks8851_read_selftest(ks);
  979. ks8851_init_mac(ks, dev->of_node);
  980. ret = register_netdev(netdev);
  981. if (ret) {
  982. dev_err(dev, "failed to register network device\n");
  983. goto err_id;
  984. }
  985. netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
  986. CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
  987. ks->rc_ccr & CCR_EEPROM ? "has" : "no");
  988. return 0;
  989. err_id:
  990. ks8851_unregister_mdiobus(ks);
  991. err_mdio:
  992. if (ks->gpio)
  993. gpiod_set_value_cansleep(ks->gpio, 1);
  994. regulator_disable(ks->vdd_reg);
  995. err_reg:
  996. regulator_disable(ks->vdd_io);
  997. err_reg_io:
  998. return ret;
  999. }
  1000. EXPORT_SYMBOL_GPL(ks8851_probe_common);
  1001. void ks8851_remove_common(struct device *dev)
  1002. {
  1003. struct ks8851_net *priv = dev_get_drvdata(dev);
  1004. ks8851_unregister_mdiobus(priv);
  1005. if (netif_msg_drv(priv))
  1006. dev_info(dev, "remove\n");
  1007. unregister_netdev(priv->netdev);
  1008. if (priv->gpio)
  1009. gpiod_set_value_cansleep(priv->gpio, 1);
  1010. regulator_disable(priv->vdd_reg);
  1011. regulator_disable(priv->vdd_io);
  1012. }
  1013. EXPORT_SYMBOL_GPL(ks8851_remove_common);
  1014. MODULE_DESCRIPTION("KS8851 Network driver");
  1015. MODULE_AUTHOR("Ben Dooks <[email protected]>");
  1016. MODULE_LICENSE("GPL");