ks8851_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/regulator/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/gpio.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/of_net.h>
  23. #include "ks8851.h"
  24. static int msg_enable;
  25. /**
  26. * struct ks8851_net_spi - KS8851 SPI driver private data
  27. * @lock: Lock to ensure that the device is not accessed when busy.
  28. * @tx_work: Work queue for tx packets
  29. * @ks8851: KS8851 driver common private data
  30. * @spidev: The spi device we're bound to.
  31. * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
  32. * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
  33. * @spi_xfer1: @spi_msg1 SPI transfer structure
  34. * @spi_xfer2: @spi_msg2 SPI transfer structure
  35. *
  36. * The @lock ensures that the chip is protected when certain operations are
  37. * in progress. When the read or write packet transfer is in progress, most
  38. * of the chip registers are not ccessible until the transfer is finished and
  39. * the DMA has been de-asserted.
  40. */
  41. struct ks8851_net_spi {
  42. struct ks8851_net ks8851;
  43. struct mutex lock;
  44. struct work_struct tx_work;
  45. struct spi_device *spidev;
  46. struct spi_message spi_msg1;
  47. struct spi_message spi_msg2;
  48. struct spi_transfer spi_xfer1;
  49. struct spi_transfer spi_xfer2[2];
  50. };
  51. #define to_ks8851_spi(ks) container_of((ks), struct ks8851_net_spi, ks8851)
  52. /* SPI frame opcodes */
  53. #define KS_SPIOP_RD 0x00
  54. #define KS_SPIOP_WR 0x40
  55. #define KS_SPIOP_RXFIFO 0x80
  56. #define KS_SPIOP_TXFIFO 0xC0
  57. /* shift for byte-enable data */
  58. #define BYTE_EN(_x) ((_x) << 2)
  59. /* turn register number and byte-enable mask into data for start of packet */
  60. #define MK_OP(_byteen, _reg) \
  61. (BYTE_EN(_byteen) | (_reg) << (8 + 2) | (_reg) >> 6)
  62. /**
  63. * ks8851_lock_spi - register access lock
  64. * @ks: The chip state
  65. * @flags: Spinlock flags
  66. *
  67. * Claim chip register access lock
  68. */
  69. static void ks8851_lock_spi(struct ks8851_net *ks, unsigned long *flags)
  70. {
  71. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  72. mutex_lock(&kss->lock);
  73. }
  74. /**
  75. * ks8851_unlock_spi - register access unlock
  76. * @ks: The chip state
  77. * @flags: Spinlock flags
  78. *
  79. * Release chip register access lock
  80. */
  81. static void ks8851_unlock_spi(struct ks8851_net *ks, unsigned long *flags)
  82. {
  83. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  84. mutex_unlock(&kss->lock);
  85. }
  86. /* SPI register read/write calls.
  87. *
  88. * All these calls issue SPI transactions to access the chip's registers. They
  89. * all require that the necessary lock is held to prevent accesses when the
  90. * chip is busy transferring packet data (RX/TX FIFO accesses).
  91. */
  92. /**
  93. * ks8851_wrreg16_spi - write 16bit register value to chip via SPI
  94. * @ks: The chip state
  95. * @reg: The register address
  96. * @val: The value to write
  97. *
  98. * Issue a write to put the value @val into the register specified in @reg.
  99. */
  100. static void ks8851_wrreg16_spi(struct ks8851_net *ks, unsigned int reg,
  101. unsigned int val)
  102. {
  103. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  104. struct spi_transfer *xfer = &kss->spi_xfer1;
  105. struct spi_message *msg = &kss->spi_msg1;
  106. __le16 txb[2];
  107. int ret;
  108. txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
  109. txb[1] = cpu_to_le16(val);
  110. xfer->tx_buf = txb;
  111. xfer->rx_buf = NULL;
  112. xfer->len = 4;
  113. ret = spi_sync(kss->spidev, msg);
  114. if (ret < 0)
  115. netdev_err(ks->netdev, "spi_sync() failed\n");
  116. }
  117. /**
  118. * ks8851_rdreg - issue read register command and return the data
  119. * @ks: The device state
  120. * @op: The register address and byte enables in message format.
  121. * @rxb: The RX buffer to return the result into
  122. * @rxl: The length of data expected.
  123. *
  124. * This is the low level read call that issues the necessary spi message(s)
  125. * to read data from the register specified in @op.
  126. */
  127. static void ks8851_rdreg(struct ks8851_net *ks, unsigned int op,
  128. u8 *rxb, unsigned int rxl)
  129. {
  130. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  131. struct spi_transfer *xfer;
  132. struct spi_message *msg;
  133. __le16 *txb = (__le16 *)ks->txd;
  134. u8 *trx = ks->rxd;
  135. int ret;
  136. txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
  137. if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
  138. msg = &kss->spi_msg2;
  139. xfer = kss->spi_xfer2;
  140. xfer->tx_buf = txb;
  141. xfer->rx_buf = NULL;
  142. xfer->len = 2;
  143. xfer++;
  144. xfer->tx_buf = NULL;
  145. xfer->rx_buf = trx;
  146. xfer->len = rxl;
  147. } else {
  148. msg = &kss->spi_msg1;
  149. xfer = &kss->spi_xfer1;
  150. xfer->tx_buf = txb;
  151. xfer->rx_buf = trx;
  152. xfer->len = rxl + 2;
  153. }
  154. ret = spi_sync(kss->spidev, msg);
  155. if (ret < 0)
  156. netdev_err(ks->netdev, "read: spi_sync() failed\n");
  157. else if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
  158. memcpy(rxb, trx, rxl);
  159. else
  160. memcpy(rxb, trx + 2, rxl);
  161. }
  162. /**
  163. * ks8851_rdreg16_spi - read 16 bit register from device via SPI
  164. * @ks: The chip information
  165. * @reg: The register address
  166. *
  167. * Read a 16bit register from the chip, returning the result
  168. */
  169. static unsigned int ks8851_rdreg16_spi(struct ks8851_net *ks, unsigned int reg)
  170. {
  171. __le16 rx = 0;
  172. ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
  173. return le16_to_cpu(rx);
  174. }
  175. /**
  176. * ks8851_rdfifo_spi - read data from the receive fifo via SPI
  177. * @ks: The device state.
  178. * @buff: The buffer address
  179. * @len: The length of the data to read
  180. *
  181. * Issue an RXQ FIFO read command and read the @len amount of data from
  182. * the FIFO into the buffer specified by @buff.
  183. */
  184. static void ks8851_rdfifo_spi(struct ks8851_net *ks, u8 *buff, unsigned int len)
  185. {
  186. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  187. struct spi_transfer *xfer = kss->spi_xfer2;
  188. struct spi_message *msg = &kss->spi_msg2;
  189. u8 txb[1];
  190. int ret;
  191. netif_dbg(ks, rx_status, ks->netdev,
  192. "%s: %d@%p\n", __func__, len, buff);
  193. /* set the operation we're issuing */
  194. txb[0] = KS_SPIOP_RXFIFO;
  195. xfer->tx_buf = txb;
  196. xfer->rx_buf = NULL;
  197. xfer->len = 1;
  198. xfer++;
  199. xfer->rx_buf = buff;
  200. xfer->tx_buf = NULL;
  201. xfer->len = len;
  202. ret = spi_sync(kss->spidev, msg);
  203. if (ret < 0)
  204. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  205. }
  206. /**
  207. * ks8851_wrfifo_spi - write packet to TX FIFO via SPI
  208. * @ks: The device state.
  209. * @txp: The sk_buff to transmit.
  210. * @irq: IRQ on completion of the packet.
  211. *
  212. * Send the @txp to the chip. This means creating the relevant packet header
  213. * specifying the length of the packet and the other information the chip
  214. * needs, such as IRQ on completion. Send the header and the packet data to
  215. * the device.
  216. */
  217. static void ks8851_wrfifo_spi(struct ks8851_net *ks, struct sk_buff *txp,
  218. bool irq)
  219. {
  220. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  221. struct spi_transfer *xfer = kss->spi_xfer2;
  222. struct spi_message *msg = &kss->spi_msg2;
  223. unsigned int fid = 0;
  224. int ret;
  225. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  226. __func__, txp, txp->len, txp->data, irq);
  227. fid = ks->fid++;
  228. fid &= TXFR_TXFID_MASK;
  229. if (irq)
  230. fid |= TXFR_TXIC; /* irq on completion */
  231. /* start header at txb[1] to align txw entries */
  232. ks->txh.txb[1] = KS_SPIOP_TXFIFO;
  233. ks->txh.txw[1] = cpu_to_le16(fid);
  234. ks->txh.txw[2] = cpu_to_le16(txp->len);
  235. xfer->tx_buf = &ks->txh.txb[1];
  236. xfer->rx_buf = NULL;
  237. xfer->len = 5;
  238. xfer++;
  239. xfer->tx_buf = txp->data;
  240. xfer->rx_buf = NULL;
  241. xfer->len = ALIGN(txp->len, 4);
  242. ret = spi_sync(kss->spidev, msg);
  243. if (ret < 0)
  244. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  245. }
  246. /**
  247. * ks8851_rx_skb_spi - receive skbuff
  248. * @ks: The device state
  249. * @skb: The skbuff
  250. */
  251. static void ks8851_rx_skb_spi(struct ks8851_net *ks, struct sk_buff *skb)
  252. {
  253. netif_rx(skb);
  254. }
  255. /**
  256. * ks8851_tx_work - process tx packet(s)
  257. * @work: The work strucutre what was scheduled.
  258. *
  259. * This is called when a number of packets have been scheduled for
  260. * transmission and need to be sent to the device.
  261. */
  262. static void ks8851_tx_work(struct work_struct *work)
  263. {
  264. struct ks8851_net_spi *kss;
  265. struct ks8851_net *ks;
  266. unsigned long flags;
  267. struct sk_buff *txb;
  268. bool last;
  269. kss = container_of(work, struct ks8851_net_spi, tx_work);
  270. ks = &kss->ks8851;
  271. last = skb_queue_empty(&ks->txq);
  272. ks8851_lock_spi(ks, &flags);
  273. while (!last) {
  274. txb = skb_dequeue(&ks->txq);
  275. last = skb_queue_empty(&ks->txq);
  276. if (txb) {
  277. ks8851_wrreg16_spi(ks, KS_RXQCR,
  278. ks->rc_rxqcr | RXQCR_SDA);
  279. ks8851_wrfifo_spi(ks, txb, last);
  280. ks8851_wrreg16_spi(ks, KS_RXQCR, ks->rc_rxqcr);
  281. ks8851_wrreg16_spi(ks, KS_TXQCR, TXQCR_METFE);
  282. ks8851_done_tx(ks, txb);
  283. }
  284. }
  285. ks8851_unlock_spi(ks, &flags);
  286. }
  287. /**
  288. * ks8851_flush_tx_work_spi - flush outstanding TX work
  289. * @ks: The device state
  290. */
  291. static void ks8851_flush_tx_work_spi(struct ks8851_net *ks)
  292. {
  293. struct ks8851_net_spi *kss = to_ks8851_spi(ks);
  294. flush_work(&kss->tx_work);
  295. }
  296. /**
  297. * calc_txlen - calculate size of message to send packet
  298. * @len: Length of data
  299. *
  300. * Returns the size of the TXFIFO message needed to send
  301. * this packet.
  302. */
  303. static unsigned int calc_txlen(unsigned int len)
  304. {
  305. return ALIGN(len + 4, 4);
  306. }
  307. /**
  308. * ks8851_start_xmit_spi - transmit packet using SPI
  309. * @skb: The buffer to transmit
  310. * @dev: The device used to transmit the packet.
  311. *
  312. * Called by the network layer to transmit the @skb. Queue the packet for
  313. * the device and schedule the necessary work to transmit the packet when
  314. * it is free.
  315. *
  316. * We do this to firstly avoid sleeping with the network device locked,
  317. * and secondly so we can round up more than one packet to transmit which
  318. * means we can try and avoid generating too many transmit done interrupts.
  319. */
  320. static netdev_tx_t ks8851_start_xmit_spi(struct sk_buff *skb,
  321. struct net_device *dev)
  322. {
  323. unsigned int needed = calc_txlen(skb->len);
  324. struct ks8851_net *ks = netdev_priv(dev);
  325. netdev_tx_t ret = NETDEV_TX_OK;
  326. struct ks8851_net_spi *kss;
  327. kss = to_ks8851_spi(ks);
  328. netif_dbg(ks, tx_queued, ks->netdev,
  329. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  330. spin_lock(&ks->statelock);
  331. if (needed > ks->tx_space) {
  332. netif_stop_queue(dev);
  333. ret = NETDEV_TX_BUSY;
  334. } else {
  335. ks->tx_space -= needed;
  336. skb_queue_tail(&ks->txq, skb);
  337. }
  338. spin_unlock(&ks->statelock);
  339. schedule_work(&kss->tx_work);
  340. return ret;
  341. }
  342. static int ks8851_probe_spi(struct spi_device *spi)
  343. {
  344. struct device *dev = &spi->dev;
  345. struct ks8851_net_spi *kss;
  346. struct net_device *netdev;
  347. struct ks8851_net *ks;
  348. netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_spi));
  349. if (!netdev)
  350. return -ENOMEM;
  351. spi->bits_per_word = 8;
  352. kss = netdev_priv(netdev);
  353. ks = &kss->ks8851;
  354. ks->lock = ks8851_lock_spi;
  355. ks->unlock = ks8851_unlock_spi;
  356. ks->rdreg16 = ks8851_rdreg16_spi;
  357. ks->wrreg16 = ks8851_wrreg16_spi;
  358. ks->rdfifo = ks8851_rdfifo_spi;
  359. ks->wrfifo = ks8851_wrfifo_spi;
  360. ks->start_xmit = ks8851_start_xmit_spi;
  361. ks->rx_skb = ks8851_rx_skb_spi;
  362. ks->flush_tx_work = ks8851_flush_tx_work_spi;
  363. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  364. IRQ_TXI | /* TX done */ \
  365. IRQ_RXI | /* RX done */ \
  366. IRQ_SPIBEI | /* SPI bus error */ \
  367. IRQ_TXPSI | /* TX process stop */ \
  368. IRQ_RXPSI) /* RX process stop */
  369. ks->rc_ier = STD_IRQ;
  370. kss->spidev = spi;
  371. mutex_init(&kss->lock);
  372. INIT_WORK(&kss->tx_work, ks8851_tx_work);
  373. /* initialise pre-made spi transfer messages */
  374. spi_message_init(&kss->spi_msg1);
  375. spi_message_add_tail(&kss->spi_xfer1, &kss->spi_msg1);
  376. spi_message_init(&kss->spi_msg2);
  377. spi_message_add_tail(&kss->spi_xfer2[0], &kss->spi_msg2);
  378. spi_message_add_tail(&kss->spi_xfer2[1], &kss->spi_msg2);
  379. netdev->irq = spi->irq;
  380. return ks8851_probe_common(netdev, dev, msg_enable);
  381. }
  382. static void ks8851_remove_spi(struct spi_device *spi)
  383. {
  384. ks8851_remove_common(&spi->dev);
  385. }
  386. static const struct of_device_id ks8851_match_table[] = {
  387. { .compatible = "micrel,ks8851" },
  388. { }
  389. };
  390. MODULE_DEVICE_TABLE(of, ks8851_match_table);
  391. static struct spi_driver ks8851_driver = {
  392. .driver = {
  393. .name = "ks8851",
  394. .of_match_table = ks8851_match_table,
  395. .pm = &ks8851_pm_ops,
  396. },
  397. .probe = ks8851_probe_spi,
  398. .remove = ks8851_remove_spi,
  399. };
  400. module_spi_driver(ks8851_driver);
  401. MODULE_DESCRIPTION("KS8851 Network driver");
  402. MODULE_AUTHOR("Ben Dooks <[email protected]>");
  403. MODULE_LICENSE("GPL");
  404. module_param_named(message, msg_enable, int, 0);
  405. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  406. MODULE_ALIAS("spi:ks8851");