File Index
Files and directories
Sign in
FILE

ks8851_spi.c

File preview with syntax highlighting.

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