Debian Codegraph @

drivers/net/ethernet/micrel/ks8851_spi.c

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