ks8851_par.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/iopoll.h>
  16. #include <linux/mii.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_net.h>
  19. #include "ks8851.h"
  20. static int msg_enable;
  21. #define BE3 0x8000 /* Byte Enable 3 */
  22. #define BE2 0x4000 /* Byte Enable 2 */
  23. #define BE1 0x2000 /* Byte Enable 1 */
  24. #define BE0 0x1000 /* Byte Enable 0 */
  25. /**
  26. * struct ks8851_net_par - KS8851 Parallel driver private data
  27. * @ks8851: KS8851 driver common private data
  28. * @lock: Lock to ensure that the device is not accessed when busy.
  29. * @hw_addr : start address of data register.
  30. * @hw_addr_cmd : start address of command register.
  31. * @cmd_reg_cache : command register cached.
  32. *
  33. * The @lock ensures that the chip is protected when certain operations are
  34. * in progress. When the read or write packet transfer is in progress, most
  35. * of the chip registers are not accessible until the transfer is finished
  36. * and the DMA has been de-asserted.
  37. */
  38. struct ks8851_net_par {
  39. struct ks8851_net ks8851;
  40. spinlock_t lock;
  41. void __iomem *hw_addr;
  42. void __iomem *hw_addr_cmd;
  43. u16 cmd_reg_cache;
  44. };
  45. #define to_ks8851_par(ks) container_of((ks), struct ks8851_net_par, ks8851)
  46. /**
  47. * ks8851_lock_par - register access lock
  48. * @ks: The chip state
  49. * @flags: Spinlock flags
  50. *
  51. * Claim chip register access lock
  52. */
  53. static void ks8851_lock_par(struct ks8851_net *ks, unsigned long *flags)
  54. {
  55. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  56. spin_lock_irqsave(&ksp->lock, *flags);
  57. }
  58. /**
  59. * ks8851_unlock_par - register access unlock
  60. * @ks: The chip state
  61. * @flags: Spinlock flags
  62. *
  63. * Release chip register access lock
  64. */
  65. static void ks8851_unlock_par(struct ks8851_net *ks, unsigned long *flags)
  66. {
  67. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  68. spin_unlock_irqrestore(&ksp->lock, *flags);
  69. }
  70. /**
  71. * ks_check_endian - Check whether endianness of the bus is correct
  72. * @ks : The chip information
  73. *
  74. * The KS8851-16MLL EESK pin allows selecting the endianness of the 16bit
  75. * bus. To maintain optimum performance, the bus endianness should be set
  76. * such that it matches the endianness of the CPU.
  77. */
  78. static int ks_check_endian(struct ks8851_net *ks)
  79. {
  80. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  81. u16 cider;
  82. /*
  83. * Read CIDER register first, however read it the "wrong" way around.
  84. * If the endian strap on the KS8851-16MLL in incorrect and the chip
  85. * is operating in different endianness than the CPU, then the meaning
  86. * of BE[3:0] byte-enable bits is also swapped such that:
  87. * BE[3,2,1,0] becomes BE[1,0,3,2]
  88. *
  89. * Luckily for us, the byte-enable bits are the top four MSbits of
  90. * the address register and the CIDER register is at offset 0xc0.
  91. * Hence, by reading address 0xc0c0, which is not impacted by endian
  92. * swapping, we assert either BE[3:2] or BE[1:0] while reading the
  93. * CIDER register.
  94. *
  95. * If the bus configuration is correct, reading 0xc0c0 asserts
  96. * BE[3:2] and this read returns 0x0000, because to read register
  97. * with bottom two LSbits of address set to 0, BE[1:0] must be
  98. * asserted.
  99. *
  100. * If the bus configuration is NOT correct, reading 0xc0c0 asserts
  101. * BE[1:0] and this read returns non-zero 0x8872 value.
  102. */
  103. iowrite16(BE3 | BE2 | KS_CIDER, ksp->hw_addr_cmd);
  104. cider = ioread16(ksp->hw_addr);
  105. if (!cider)
  106. return 0;
  107. netdev_err(ks->netdev, "incorrect EESK endian strap setting\n");
  108. return -EINVAL;
  109. }
  110. /**
  111. * ks8851_wrreg16_par - write 16bit register value to chip
  112. * @ks: The chip state
  113. * @reg: The register address
  114. * @val: The value to write
  115. *
  116. * Issue a write to put the value @val into the register specified in @reg.
  117. */
  118. static void ks8851_wrreg16_par(struct ks8851_net *ks, unsigned int reg,
  119. unsigned int val)
  120. {
  121. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  122. ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02));
  123. iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd);
  124. iowrite16(val, ksp->hw_addr);
  125. }
  126. /**
  127. * ks8851_rdreg16_par - read 16 bit register from chip
  128. * @ks: The chip information
  129. * @reg: The register address
  130. *
  131. * Read a 16bit register from the chip, returning the result
  132. */
  133. static unsigned int ks8851_rdreg16_par(struct ks8851_net *ks, unsigned int reg)
  134. {
  135. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  136. ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02));
  137. iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd);
  138. return ioread16(ksp->hw_addr);
  139. }
  140. /**
  141. * ks8851_rdfifo_par - read data from the receive fifo
  142. * @ks: The device state.
  143. * @buff: The buffer address
  144. * @len: The length of the data to read
  145. *
  146. * Issue an RXQ FIFO read command and read the @len amount of data from
  147. * the FIFO into the buffer specified by @buff.
  148. */
  149. static void ks8851_rdfifo_par(struct ks8851_net *ks, u8 *buff, unsigned int len)
  150. {
  151. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  152. netif_dbg(ks, rx_status, ks->netdev,
  153. "%s: %d@%p\n", __func__, len, buff);
  154. ioread16_rep(ksp->hw_addr, (u16 *)buff + 1, len / 2);
  155. }
  156. /**
  157. * ks8851_wrfifo_par - write packet to TX FIFO
  158. * @ks: The device state.
  159. * @txp: The sk_buff to transmit.
  160. * @irq: IRQ on completion of the packet.
  161. *
  162. * Send the @txp to the chip. This means creating the relevant packet header
  163. * specifying the length of the packet and the other information the chip
  164. * needs, such as IRQ on completion. Send the header and the packet data to
  165. * the device.
  166. */
  167. static void ks8851_wrfifo_par(struct ks8851_net *ks, struct sk_buff *txp,
  168. bool irq)
  169. {
  170. struct ks8851_net_par *ksp = to_ks8851_par(ks);
  171. unsigned int len = ALIGN(txp->len, 4);
  172. unsigned int fid = 0;
  173. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  174. __func__, txp, txp->len, txp->data, irq);
  175. fid = ks->fid++;
  176. fid &= TXFR_TXFID_MASK;
  177. if (irq)
  178. fid |= TXFR_TXIC; /* irq on completion */
  179. iowrite16(fid, ksp->hw_addr);
  180. iowrite16(txp->len, ksp->hw_addr);
  181. iowrite16_rep(ksp->hw_addr, txp->data, len / 2);
  182. }
  183. /**
  184. * ks8851_rx_skb_par - receive skbuff
  185. * @ks: The device state.
  186. * @skb: The skbuff
  187. */
  188. static void ks8851_rx_skb_par(struct ks8851_net *ks, struct sk_buff *skb)
  189. {
  190. netif_rx(skb);
  191. }
  192. static unsigned int ks8851_rdreg16_par_txqcr(struct ks8851_net *ks)
  193. {
  194. return ks8851_rdreg16_par(ks, KS_TXQCR);
  195. }
  196. /**
  197. * ks8851_start_xmit_par - transmit packet
  198. * @skb: The buffer to transmit
  199. * @dev: The device used to transmit the packet.
  200. *
  201. * Called by the network layer to transmit the @skb. Queue the packet for
  202. * the device and schedule the necessary work to transmit the packet when
  203. * it is free.
  204. *
  205. * We do this to firstly avoid sleeping with the network device locked,
  206. * and secondly so we can round up more than one packet to transmit which
  207. * means we can try and avoid generating too many transmit done interrupts.
  208. */
  209. static netdev_tx_t ks8851_start_xmit_par(struct sk_buff *skb,
  210. struct net_device *dev)
  211. {
  212. struct ks8851_net *ks = netdev_priv(dev);
  213. netdev_tx_t ret = NETDEV_TX_OK;
  214. unsigned long flags;
  215. unsigned int txqcr;
  216. u16 txmir;
  217. int err;
  218. netif_dbg(ks, tx_queued, ks->netdev,
  219. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  220. ks8851_lock_par(ks, &flags);
  221. txmir = ks8851_rdreg16_par(ks, KS_TXMIR) & 0x1fff;
  222. if (likely(txmir >= skb->len + 12)) {
  223. ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  224. ks8851_wrfifo_par(ks, skb, false);
  225. ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr);
  226. ks8851_wrreg16_par(ks, KS_TXQCR, TXQCR_METFE);
  227. err = readx_poll_timeout_atomic(ks8851_rdreg16_par_txqcr, ks,
  228. txqcr, !(txqcr & TXQCR_METFE),
  229. 5, 1000000);
  230. if (err)
  231. ret = NETDEV_TX_BUSY;
  232. ks8851_done_tx(ks, skb);
  233. } else {
  234. ret = NETDEV_TX_BUSY;
  235. }
  236. ks8851_unlock_par(ks, &flags);
  237. return ret;
  238. }
  239. static int ks8851_probe_par(struct platform_device *pdev)
  240. {
  241. struct device *dev = &pdev->dev;
  242. struct ks8851_net_par *ksp;
  243. struct net_device *netdev;
  244. struct ks8851_net *ks;
  245. int ret;
  246. netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_par));
  247. if (!netdev)
  248. return -ENOMEM;
  249. ks = netdev_priv(netdev);
  250. ks->lock = ks8851_lock_par;
  251. ks->unlock = ks8851_unlock_par;
  252. ks->rdreg16 = ks8851_rdreg16_par;
  253. ks->wrreg16 = ks8851_wrreg16_par;
  254. ks->rdfifo = ks8851_rdfifo_par;
  255. ks->wrfifo = ks8851_wrfifo_par;
  256. ks->start_xmit = ks8851_start_xmit_par;
  257. ks->rx_skb = ks8851_rx_skb_par;
  258. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  259. IRQ_RXI | /* RX done */ \
  260. IRQ_RXPSI) /* RX process stop */
  261. ks->rc_ier = STD_IRQ;
  262. ksp = to_ks8851_par(ks);
  263. spin_lock_init(&ksp->lock);
  264. ksp->hw_addr = devm_platform_ioremap_resource(pdev, 0);
  265. if (IS_ERR(ksp->hw_addr))
  266. return PTR_ERR(ksp->hw_addr);
  267. ksp->hw_addr_cmd = devm_platform_ioremap_resource(pdev, 1);
  268. if (IS_ERR(ksp->hw_addr_cmd))
  269. return PTR_ERR(ksp->hw_addr_cmd);
  270. ret = ks_check_endian(ks);
  271. if (ret)
  272. return ret;
  273. netdev->irq = platform_get_irq(pdev, 0);
  274. if (netdev->irq < 0)
  275. return netdev->irq;
  276. return ks8851_probe_common(netdev, dev, msg_enable);
  277. }
  278. static int ks8851_remove_par(struct platform_device *pdev)
  279. {
  280. ks8851_remove_common(&pdev->dev);
  281. return 0;
  282. }
  283. static const struct of_device_id ks8851_match_table[] = {
  284. { .compatible = "micrel,ks8851-mll" },
  285. { }
  286. };
  287. MODULE_DEVICE_TABLE(of, ks8851_match_table);
  288. static struct platform_driver ks8851_driver = {
  289. .driver = {
  290. .name = "ks8851",
  291. .of_match_table = ks8851_match_table,
  292. .pm = &ks8851_pm_ops,
  293. },
  294. .probe = ks8851_probe_par,
  295. .remove = ks8851_remove_par,
  296. };
  297. module_platform_driver(ks8851_driver);
  298. MODULE_DESCRIPTION("KS8851 Network driver");
  299. MODULE_AUTHOR("Ben Dooks <[email protected]>");
  300. MODULE_LICENSE("GPL");
  301. module_param_named(message, msg_enable, int, 0);
  302. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");