Commit | Line | Data |
---|---|---|
3320eae5 | 1 | /* drivers/net/ks8851.c |
3ba81f3e BD |
2 | * |
3 | * Copyright 2009 Simtec Electronics | |
4 | * http://www.simtec.co.uk/ | |
5 | * Ben Dooks <ben@simtec.co.uk> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
0dc7d2b3 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
3ba81f3e BD |
14 | #define DEBUG |
15 | ||
16 | #include <linux/module.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/netdevice.h> | |
19 | #include <linux/etherdevice.h> | |
20 | #include <linux/ethtool.h> | |
21 | #include <linux/cache.h> | |
22 | #include <linux/crc32.h> | |
23 | #include <linux/mii.h> | |
24 | ||
25 | #include <linux/spi/spi.h> | |
26 | ||
27 | #include "ks8851.h" | |
28 | ||
29 | /** | |
30 | * struct ks8851_rxctrl - KS8851 driver rx control | |
31 | * @mchash: Multicast hash-table data. | |
32 | * @rxcr1: KS_RXCR1 register setting | |
33 | * @rxcr2: KS_RXCR2 register setting | |
34 | * | |
35 | * Representation of the settings needs to control the receive filtering | |
36 | * such as the multicast hash-filter and the receive register settings. This | |
37 | * is used to make the job of working out if the receive settings change and | |
38 | * then issuing the new settings to the worker that will send the necessary | |
39 | * commands. | |
40 | */ | |
41 | struct ks8851_rxctrl { | |
42 | u16 mchash[4]; | |
43 | u16 rxcr1; | |
44 | u16 rxcr2; | |
45 | }; | |
46 | ||
47 | /** | |
48 | * union ks8851_tx_hdr - tx header data | |
49 | * @txb: The header as bytes | |
50 | * @txw: The header as 16bit, little-endian words | |
51 | * | |
52 | * A dual representation of the tx header data to allow | |
53 | * access to individual bytes, and to allow 16bit accesses | |
54 | * with 16bit alignment. | |
55 | */ | |
56 | union ks8851_tx_hdr { | |
57 | u8 txb[6]; | |
58 | __le16 txw[3]; | |
59 | }; | |
60 | ||
61 | /** | |
62 | * struct ks8851_net - KS8851 driver private data | |
63 | * @netdev: The network device we're bound to | |
64 | * @spidev: The spi device we're bound to. | |
65 | * @lock: Lock to ensure that the device is not accessed when busy. | |
66 | * @statelock: Lock on this structure for tx list. | |
67 | * @mii: The MII state information for the mii calls. | |
68 | * @rxctrl: RX settings for @rxctrl_work. | |
69 | * @tx_work: Work queue for tx packets | |
70 | * @irq_work: Work queue for servicing interrupts | |
71 | * @rxctrl_work: Work queue for updating RX mode and multicast lists | |
72 | * @txq: Queue of packets for transmission. | |
73 | * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1. | |
74 | * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2. | |
75 | * @txh: Space for generating packet TX header in DMA-able data | |
76 | * @rxd: Space for receiving SPI data, in DMA-able space. | |
77 | * @txd: Space for transmitting SPI data, in DMA-able space. | |
78 | * @msg_enable: The message flags controlling driver output (see ethtool). | |
79 | * @fid: Incrementing frame id tag. | |
80 | * @rc_ier: Cached copy of KS_IER. | |
81 | * @rc_rxqcr: Cached copy of KS_RXQCR. | |
82 | * | |
83 | * The @lock ensures that the chip is protected when certain operations are | |
84 | * in progress. When the read or write packet transfer is in progress, most | |
85 | * of the chip registers are not ccessible until the transfer is finished and | |
86 | * the DMA has been de-asserted. | |
87 | * | |
88 | * The @statelock is used to protect information in the structure which may | |
89 | * need to be accessed via several sources, such as the network driver layer | |
90 | * or one of the work queues. | |
91 | * | |
92 | * We align the buffers we may use for rx/tx to ensure that if the SPI driver | |
93 | * wants to DMA map them, it will not have any problems with data the driver | |
94 | * modifies. | |
95 | */ | |
96 | struct ks8851_net { | |
97 | struct net_device *netdev; | |
98 | struct spi_device *spidev; | |
99 | struct mutex lock; | |
100 | spinlock_t statelock; | |
101 | ||
102 | union ks8851_tx_hdr txh ____cacheline_aligned; | |
103 | u8 rxd[8]; | |
104 | u8 txd[8]; | |
105 | ||
106 | u32 msg_enable ____cacheline_aligned; | |
107 | u16 tx_space; | |
108 | u8 fid; | |
109 | ||
110 | u16 rc_ier; | |
111 | u16 rc_rxqcr; | |
112 | ||
113 | struct mii_if_info mii; | |
114 | struct ks8851_rxctrl rxctrl; | |
115 | ||
116 | struct work_struct tx_work; | |
117 | struct work_struct irq_work; | |
118 | struct work_struct rxctrl_work; | |
119 | ||
120 | struct sk_buff_head txq; | |
121 | ||
122 | struct spi_message spi_msg1; | |
123 | struct spi_message spi_msg2; | |
124 | struct spi_transfer spi_xfer1; | |
125 | struct spi_transfer spi_xfer2[2]; | |
126 | }; | |
127 | ||
128 | static int msg_enable; | |
129 | ||
3ba81f3e BD |
130 | /* shift for byte-enable data */ |
131 | #define BYTE_EN(_x) ((_x) << 2) | |
132 | ||
133 | /* turn register number and byte-enable mask into data for start of packet */ | |
134 | #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6) | |
135 | ||
136 | /* SPI register read/write calls. | |
137 | * | |
138 | * All these calls issue SPI transactions to access the chip's registers. They | |
139 | * all require that the necessary lock is held to prevent accesses when the | |
140 | * chip is busy transfering packet data (RX/TX FIFO accesses). | |
141 | */ | |
142 | ||
143 | /** | |
144 | * ks8851_wrreg16 - write 16bit register value to chip | |
145 | * @ks: The chip state | |
146 | * @reg: The register address | |
147 | * @val: The value to write | |
148 | * | |
149 | * Issue a write to put the value @val into the register specified in @reg. | |
150 | */ | |
151 | static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val) | |
152 | { | |
153 | struct spi_transfer *xfer = &ks->spi_xfer1; | |
154 | struct spi_message *msg = &ks->spi_msg1; | |
155 | __le16 txb[2]; | |
156 | int ret; | |
157 | ||
158 | txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR); | |
159 | txb[1] = cpu_to_le16(val); | |
160 | ||
161 | xfer->tx_buf = txb; | |
162 | xfer->rx_buf = NULL; | |
163 | xfer->len = 4; | |
164 | ||
165 | ret = spi_sync(ks->spidev, msg); | |
166 | if (ret < 0) | |
0dc7d2b3 | 167 | netdev_err(ks->netdev, "spi_sync() failed\n"); |
3ba81f3e BD |
168 | } |
169 | ||
160d0fad BD |
170 | /** |
171 | * ks8851_wrreg8 - write 8bit register value to chip | |
172 | * @ks: The chip state | |
173 | * @reg: The register address | |
174 | * @val: The value to write | |
175 | * | |
176 | * Issue a write to put the value @val into the register specified in @reg. | |
177 | */ | |
178 | static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val) | |
179 | { | |
180 | struct spi_transfer *xfer = &ks->spi_xfer1; | |
181 | struct spi_message *msg = &ks->spi_msg1; | |
182 | __le16 txb[2]; | |
183 | int ret; | |
184 | int bit; | |
185 | ||
186 | bit = 1 << (reg & 3); | |
187 | ||
188 | txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR); | |
189 | txb[1] = val; | |
190 | ||
191 | xfer->tx_buf = txb; | |
192 | xfer->rx_buf = NULL; | |
193 | xfer->len = 3; | |
194 | ||
195 | ret = spi_sync(ks->spidev, msg); | |
196 | if (ret < 0) | |
0dc7d2b3 | 197 | netdev_err(ks->netdev, "spi_sync() failed\n"); |
160d0fad BD |
198 | } |
199 | ||
3ba81f3e BD |
200 | /** |
201 | * ks8851_rx_1msg - select whether to use one or two messages for spi read | |
202 | * @ks: The device structure | |
203 | * | |
204 | * Return whether to generate a single message with a tx and rx buffer | |
205 | * supplied to spi_sync(), or alternatively send the tx and rx buffers | |
206 | * as separate messages. | |
207 | * | |
208 | * Depending on the hardware in use, a single message may be more efficient | |
209 | * on interrupts or work done by the driver. | |
210 | * | |
211 | * This currently always returns true until we add some per-device data passed | |
212 | * from the platform code to specify which mode is better. | |
213 | */ | |
214 | static inline bool ks8851_rx_1msg(struct ks8851_net *ks) | |
215 | { | |
216 | return true; | |
217 | } | |
218 | ||
219 | /** | |
220 | * ks8851_rdreg - issue read register command and return the data | |
221 | * @ks: The device state | |
222 | * @op: The register address and byte enables in message format. | |
223 | * @rxb: The RX buffer to return the result into | |
224 | * @rxl: The length of data expected. | |
225 | * | |
226 | * This is the low level read call that issues the necessary spi message(s) | |
227 | * to read data from the register specified in @op. | |
228 | */ | |
229 | static void ks8851_rdreg(struct ks8851_net *ks, unsigned op, | |
230 | u8 *rxb, unsigned rxl) | |
231 | { | |
232 | struct spi_transfer *xfer; | |
233 | struct spi_message *msg; | |
234 | __le16 *txb = (__le16 *)ks->txd; | |
235 | u8 *trx = ks->rxd; | |
236 | int ret; | |
237 | ||
238 | txb[0] = cpu_to_le16(op | KS_SPIOP_RD); | |
239 | ||
240 | if (ks8851_rx_1msg(ks)) { | |
241 | msg = &ks->spi_msg1; | |
242 | xfer = &ks->spi_xfer1; | |
243 | ||
244 | xfer->tx_buf = txb; | |
245 | xfer->rx_buf = trx; | |
246 | xfer->len = rxl + 2; | |
247 | } else { | |
248 | msg = &ks->spi_msg2; | |
249 | xfer = ks->spi_xfer2; | |
250 | ||
251 | xfer->tx_buf = txb; | |
252 | xfer->rx_buf = NULL; | |
253 | xfer->len = 2; | |
254 | ||
255 | xfer++; | |
256 | xfer->tx_buf = NULL; | |
257 | xfer->rx_buf = trx; | |
258 | xfer->len = rxl; | |
259 | } | |
260 | ||
261 | ret = spi_sync(ks->spidev, msg); | |
262 | if (ret < 0) | |
0dc7d2b3 | 263 | netdev_err(ks->netdev, "read: spi_sync() failed\n"); |
3ba81f3e BD |
264 | else if (ks8851_rx_1msg(ks)) |
265 | memcpy(rxb, trx + 2, rxl); | |
266 | else | |
267 | memcpy(rxb, trx, rxl); | |
268 | } | |
269 | ||
270 | /** | |
271 | * ks8851_rdreg8 - read 8 bit register from device | |
272 | * @ks: The chip information | |
273 | * @reg: The register address | |
274 | * | |
275 | * Read a 8bit register from the chip, returning the result | |
276 | */ | |
277 | static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg) | |
278 | { | |
279 | u8 rxb[1]; | |
280 | ||
281 | ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1); | |
282 | return rxb[0]; | |
283 | } | |
284 | ||
285 | /** | |
286 | * ks8851_rdreg16 - read 16 bit register from device | |
287 | * @ks: The chip information | |
288 | * @reg: The register address | |
289 | * | |
290 | * Read a 16bit register from the chip, returning the result | |
291 | */ | |
292 | static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg) | |
293 | { | |
294 | __le16 rx = 0; | |
295 | ||
296 | ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2); | |
297 | return le16_to_cpu(rx); | |
298 | } | |
299 | ||
300 | /** | |
301 | * ks8851_rdreg32 - read 32 bit register from device | |
302 | * @ks: The chip information | |
303 | * @reg: The register address | |
304 | * | |
305 | * Read a 32bit register from the chip. | |
306 | * | |
307 | * Note, this read requires the address be aligned to 4 bytes. | |
308 | */ | |
309 | static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg) | |
310 | { | |
311 | __le32 rx = 0; | |
312 | ||
313 | WARN_ON(reg & 3); | |
314 | ||
315 | ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4); | |
316 | return le32_to_cpu(rx); | |
317 | } | |
318 | ||
319 | /** | |
320 | * ks8851_soft_reset - issue one of the soft reset to the device | |
321 | * @ks: The device state. | |
322 | * @op: The bit(s) to set in the GRR | |
323 | * | |
324 | * Issue the relevant soft-reset command to the device's GRR register | |
325 | * specified by @op. | |
326 | * | |
327 | * Note, the delays are in there as a caution to ensure that the reset | |
328 | * has time to take effect and then complete. Since the datasheet does | |
329 | * not currently specify the exact sequence, we have chosen something | |
330 | * that seems to work with our device. | |
331 | */ | |
332 | static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op) | |
333 | { | |
334 | ks8851_wrreg16(ks, KS_GRR, op); | |
335 | mdelay(1); /* wait a short time to effect reset */ | |
336 | ks8851_wrreg16(ks, KS_GRR, 0); | |
337 | mdelay(1); /* wait for condition to clear */ | |
338 | } | |
339 | ||
340 | /** | |
341 | * ks8851_write_mac_addr - write mac address to device registers | |
342 | * @dev: The network device | |
343 | * | |
344 | * Update the KS8851 MAC address registers from the address in @dev. | |
345 | * | |
346 | * This call assumes that the chip is not running, so there is no need to | |
347 | * shutdown the RXQ process whilst setting this. | |
348 | */ | |
349 | static int ks8851_write_mac_addr(struct net_device *dev) | |
350 | { | |
351 | struct ks8851_net *ks = netdev_priv(dev); | |
160d0fad | 352 | int i; |
3ba81f3e BD |
353 | |
354 | mutex_lock(&ks->lock); | |
355 | ||
160d0fad BD |
356 | for (i = 0; i < ETH_ALEN; i++) |
357 | ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]); | |
3ba81f3e BD |
358 | |
359 | mutex_unlock(&ks->lock); | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | /** | |
365 | * ks8851_init_mac - initialise the mac address | |
366 | * @ks: The device structure | |
367 | * | |
368 | * Get or create the initial mac address for the device and then set that | |
369 | * into the station address register. Currently we assume that the device | |
370 | * does not have a valid mac address in it, and so we use random_ether_addr() | |
371 | * to create a new one. | |
372 | * | |
373 | * In future, the driver should check to see if the device has an EEPROM | |
374 | * attached and whether that has a valid ethernet address in it. | |
375 | */ | |
376 | static void ks8851_init_mac(struct ks8851_net *ks) | |
377 | { | |
378 | struct net_device *dev = ks->netdev; | |
379 | ||
380 | random_ether_addr(dev->dev_addr); | |
381 | ks8851_write_mac_addr(dev); | |
382 | } | |
383 | ||
384 | /** | |
385 | * ks8851_irq - device interrupt handler | |
386 | * @irq: Interrupt number passed from the IRQ hnalder. | |
387 | * @pw: The private word passed to register_irq(), our struct ks8851_net. | |
388 | * | |
389 | * Disable the interrupt from happening again until we've processed the | |
390 | * current status by scheduling ks8851_irq_work(). | |
391 | */ | |
392 | static irqreturn_t ks8851_irq(int irq, void *pw) | |
393 | { | |
394 | struct ks8851_net *ks = pw; | |
395 | ||
396 | disable_irq_nosync(irq); | |
397 | schedule_work(&ks->irq_work); | |
398 | return IRQ_HANDLED; | |
399 | } | |
400 | ||
401 | /** | |
402 | * ks8851_rdfifo - read data from the receive fifo | |
403 | * @ks: The device state. | |
404 | * @buff: The buffer address | |
405 | * @len: The length of the data to read | |
406 | * | |
9ddc5b6f | 407 | * Issue an RXQ FIFO read command and read the @len amount of data from |
3ba81f3e BD |
408 | * the FIFO into the buffer specified by @buff. |
409 | */ | |
410 | static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len) | |
411 | { | |
412 | struct spi_transfer *xfer = ks->spi_xfer2; | |
413 | struct spi_message *msg = &ks->spi_msg2; | |
414 | u8 txb[1]; | |
415 | int ret; | |
416 | ||
0dc7d2b3 JP |
417 | netif_dbg(ks, rx_status, ks->netdev, |
418 | "%s: %d@%p\n", __func__, len, buff); | |
3ba81f3e BD |
419 | |
420 | /* set the operation we're issuing */ | |
421 | txb[0] = KS_SPIOP_RXFIFO; | |
422 | ||
423 | xfer->tx_buf = txb; | |
424 | xfer->rx_buf = NULL; | |
425 | xfer->len = 1; | |
426 | ||
427 | xfer++; | |
428 | xfer->rx_buf = buff; | |
429 | xfer->tx_buf = NULL; | |
430 | xfer->len = len; | |
431 | ||
432 | ret = spi_sync(ks->spidev, msg); | |
433 | if (ret < 0) | |
0dc7d2b3 | 434 | netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__); |
3ba81f3e BD |
435 | } |
436 | ||
437 | /** | |
438 | * ks8851_dbg_dumpkkt - dump initial packet contents to debug | |
439 | * @ks: The device state | |
440 | * @rxpkt: The data for the received packet | |
441 | * | |
442 | * Dump the initial data from the packet to dev_dbg(). | |
443 | */ | |
444 | static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt) | |
445 | { | |
0dc7d2b3 JP |
446 | netdev_dbg(ks->netdev, |
447 | "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n", | |
448 | rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7], | |
449 | rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11], | |
450 | rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]); | |
3ba81f3e BD |
451 | } |
452 | ||
453 | /** | |
454 | * ks8851_rx_pkts - receive packets from the host | |
455 | * @ks: The device information. | |
456 | * | |
457 | * This is called from the IRQ work queue when the system detects that there | |
458 | * are packets in the receive queue. Find out how many packets there are and | |
459 | * read them from the FIFO. | |
460 | */ | |
461 | static void ks8851_rx_pkts(struct ks8851_net *ks) | |
462 | { | |
463 | struct sk_buff *skb; | |
464 | unsigned rxfc; | |
465 | unsigned rxlen; | |
466 | unsigned rxstat; | |
467 | u32 rxh; | |
468 | u8 *rxpkt; | |
469 | ||
470 | rxfc = ks8851_rdreg8(ks, KS_RXFC); | |
471 | ||
0dc7d2b3 JP |
472 | netif_dbg(ks, rx_status, ks->netdev, |
473 | "%s: %d packets\n", __func__, rxfc); | |
3ba81f3e BD |
474 | |
475 | /* Currently we're issuing a read per packet, but we could possibly | |
476 | * improve the code by issuing a single read, getting the receive | |
477 | * header, allocating the packet and then reading the packet data | |
478 | * out in one go. | |
479 | * | |
480 | * This form of operation would require us to hold the SPI bus' | |
481 | * chipselect low during the entie transaction to avoid any | |
482 | * reset to the data stream comming from the chip. | |
483 | */ | |
484 | ||
485 | for (; rxfc != 0; rxfc--) { | |
486 | rxh = ks8851_rdreg32(ks, KS_RXFHSR); | |
487 | rxstat = rxh & 0xffff; | |
488 | rxlen = rxh >> 16; | |
489 | ||
0dc7d2b3 JP |
490 | netif_dbg(ks, rx_status, ks->netdev, |
491 | "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen); | |
3ba81f3e BD |
492 | |
493 | /* the length of the packet includes the 32bit CRC */ | |
494 | ||
495 | /* set dma read address */ | |
496 | ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00); | |
497 | ||
498 | /* start the packet dma process, and set auto-dequeue rx */ | |
499 | ks8851_wrreg16(ks, KS_RXQCR, | |
500 | ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE); | |
501 | ||
502 | if (rxlen > 0) { | |
503 | skb = netdev_alloc_skb(ks->netdev, rxlen + 2 + 8); | |
504 | if (!skb) { | |
505 | /* todo - dump frame and move on */ | |
506 | } | |
507 | ||
508 | /* two bytes to ensure ip is aligned, and four bytes | |
509 | * for the status header and 4 bytes of garbage */ | |
510 | skb_reserve(skb, 2 + 4 + 4); | |
511 | ||
512 | rxpkt = skb_put(skb, rxlen - 4) - 8; | |
513 | ||
514 | /* align the packet length to 4 bytes, and add 4 bytes | |
515 | * as we're getting the rx status header as well */ | |
516 | ks8851_rdfifo(ks, rxpkt, ALIGN(rxlen, 4) + 8); | |
517 | ||
518 | if (netif_msg_pktdata(ks)) | |
519 | ks8851_dbg_dumpkkt(ks, rxpkt); | |
520 | ||
521 | skb->protocol = eth_type_trans(skb, ks->netdev); | |
522 | netif_rx(skb); | |
523 | ||
524 | ks->netdev->stats.rx_packets++; | |
525 | ks->netdev->stats.rx_bytes += rxlen - 4; | |
526 | } | |
527 | ||
528 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); | |
529 | } | |
530 | } | |
531 | ||
532 | /** | |
533 | * ks8851_irq_work - work queue handler for dealing with interrupt requests | |
534 | * @work: The work structure that was scheduled by schedule_work() | |
535 | * | |
536 | * This is the handler invoked when the ks8851_irq() is called to find out | |
537 | * what happened, as we cannot allow ourselves to sleep whilst waiting for | |
538 | * anything other process has the chip's lock. | |
539 | * | |
540 | * Read the interrupt status, work out what needs to be done and then clear | |
541 | * any of the interrupts that are not needed. | |
542 | */ | |
543 | static void ks8851_irq_work(struct work_struct *work) | |
544 | { | |
545 | struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work); | |
546 | unsigned status; | |
547 | unsigned handled = 0; | |
548 | ||
549 | mutex_lock(&ks->lock); | |
550 | ||
551 | status = ks8851_rdreg16(ks, KS_ISR); | |
552 | ||
0dc7d2b3 JP |
553 | netif_dbg(ks, intr, ks->netdev, |
554 | "%s: status 0x%04x\n", __func__, status); | |
3ba81f3e BD |
555 | |
556 | if (status & IRQ_LCI) { | |
557 | /* should do something about checking link status */ | |
558 | handled |= IRQ_LCI; | |
559 | } | |
560 | ||
561 | if (status & IRQ_LDI) { | |
562 | u16 pmecr = ks8851_rdreg16(ks, KS_PMECR); | |
563 | pmecr &= ~PMECR_WKEVT_MASK; | |
564 | ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK); | |
565 | ||
566 | handled |= IRQ_LDI; | |
567 | } | |
568 | ||
569 | if (status & IRQ_RXPSI) | |
570 | handled |= IRQ_RXPSI; | |
571 | ||
572 | if (status & IRQ_TXI) { | |
573 | handled |= IRQ_TXI; | |
574 | ||
575 | /* no lock here, tx queue should have been stopped */ | |
576 | ||
577 | /* update our idea of how much tx space is available to the | |
578 | * system */ | |
579 | ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR); | |
580 | ||
0dc7d2b3 JP |
581 | netif_dbg(ks, intr, ks->netdev, |
582 | "%s: txspace %d\n", __func__, ks->tx_space); | |
3ba81f3e BD |
583 | } |
584 | ||
585 | if (status & IRQ_RXI) | |
586 | handled |= IRQ_RXI; | |
587 | ||
588 | if (status & IRQ_SPIBEI) { | |
589 | dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__); | |
590 | handled |= IRQ_SPIBEI; | |
591 | } | |
592 | ||
593 | ks8851_wrreg16(ks, KS_ISR, handled); | |
594 | ||
595 | if (status & IRQ_RXI) { | |
596 | /* the datasheet says to disable the rx interrupt during | |
597 | * packet read-out, however we're masking the interrupt | |
598 | * from the device so do not bother masking just the RX | |
599 | * from the device. */ | |
600 | ||
601 | ks8851_rx_pkts(ks); | |
602 | } | |
603 | ||
604 | /* if something stopped the rx process, probably due to wanting | |
605 | * to change the rx settings, then do something about restarting | |
606 | * it. */ | |
607 | if (status & IRQ_RXPSI) { | |
608 | struct ks8851_rxctrl *rxc = &ks->rxctrl; | |
609 | ||
610 | /* update the multicast hash table */ | |
611 | ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]); | |
612 | ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]); | |
613 | ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]); | |
614 | ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]); | |
615 | ||
616 | ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2); | |
617 | ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1); | |
618 | } | |
619 | ||
620 | mutex_unlock(&ks->lock); | |
621 | ||
622 | if (status & IRQ_TXI) | |
623 | netif_wake_queue(ks->netdev); | |
624 | ||
625 | enable_irq(ks->netdev->irq); | |
626 | } | |
627 | ||
628 | /** | |
629 | * calc_txlen - calculate size of message to send packet | |
630 | * @len: Lenght of data | |
631 | * | |
632 | * Returns the size of the TXFIFO message needed to send | |
633 | * this packet. | |
634 | */ | |
635 | static inline unsigned calc_txlen(unsigned len) | |
636 | { | |
637 | return ALIGN(len + 4, 4); | |
638 | } | |
639 | ||
640 | /** | |
641 | * ks8851_wrpkt - write packet to TX FIFO | |
642 | * @ks: The device state. | |
643 | * @txp: The sk_buff to transmit. | |
644 | * @irq: IRQ on completion of the packet. | |
645 | * | |
646 | * Send the @txp to the chip. This means creating the relevant packet header | |
647 | * specifying the length of the packet and the other information the chip | |
648 | * needs, such as IRQ on completion. Send the header and the packet data to | |
649 | * the device. | |
650 | */ | |
651 | static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq) | |
652 | { | |
653 | struct spi_transfer *xfer = ks->spi_xfer2; | |
654 | struct spi_message *msg = &ks->spi_msg2; | |
655 | unsigned fid = 0; | |
656 | int ret; | |
657 | ||
0dc7d2b3 JP |
658 | netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n", |
659 | __func__, txp, txp->len, txp->data, irq); | |
3ba81f3e BD |
660 | |
661 | fid = ks->fid++; | |
662 | fid &= TXFR_TXFID_MASK; | |
663 | ||
664 | if (irq) | |
665 | fid |= TXFR_TXIC; /* irq on completion */ | |
666 | ||
667 | /* start header at txb[1] to align txw entries */ | |
668 | ks->txh.txb[1] = KS_SPIOP_TXFIFO; | |
669 | ks->txh.txw[1] = cpu_to_le16(fid); | |
670 | ks->txh.txw[2] = cpu_to_le16(txp->len); | |
671 | ||
672 | xfer->tx_buf = &ks->txh.txb[1]; | |
673 | xfer->rx_buf = NULL; | |
674 | xfer->len = 5; | |
675 | ||
676 | xfer++; | |
677 | xfer->tx_buf = txp->data; | |
678 | xfer->rx_buf = NULL; | |
679 | xfer->len = ALIGN(txp->len, 4); | |
680 | ||
681 | ret = spi_sync(ks->spidev, msg); | |
682 | if (ret < 0) | |
0dc7d2b3 | 683 | netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__); |
3ba81f3e BD |
684 | } |
685 | ||
686 | /** | |
687 | * ks8851_done_tx - update and then free skbuff after transmitting | |
688 | * @ks: The device state | |
689 | * @txb: The buffer transmitted | |
690 | */ | |
691 | static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb) | |
692 | { | |
693 | struct net_device *dev = ks->netdev; | |
694 | ||
695 | dev->stats.tx_bytes += txb->len; | |
696 | dev->stats.tx_packets++; | |
697 | ||
698 | dev_kfree_skb(txb); | |
699 | } | |
700 | ||
701 | /** | |
702 | * ks8851_tx_work - process tx packet(s) | |
703 | * @work: The work strucutre what was scheduled. | |
704 | * | |
705 | * This is called when a number of packets have been scheduled for | |
706 | * transmission and need to be sent to the device. | |
707 | */ | |
708 | static void ks8851_tx_work(struct work_struct *work) | |
709 | { | |
710 | struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work); | |
711 | struct sk_buff *txb; | |
3320eae5 | 712 | bool last = skb_queue_empty(&ks->txq); |
3ba81f3e BD |
713 | |
714 | mutex_lock(&ks->lock); | |
715 | ||
716 | while (!last) { | |
717 | txb = skb_dequeue(&ks->txq); | |
718 | last = skb_queue_empty(&ks->txq); | |
719 | ||
720 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA); | |
721 | ks8851_wrpkt(ks, txb, last); | |
722 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); | |
723 | ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE); | |
724 | ||
725 | ks8851_done_tx(ks, txb); | |
726 | } | |
727 | ||
728 | mutex_unlock(&ks->lock); | |
729 | } | |
730 | ||
731 | /** | |
732 | * ks8851_set_powermode - set power mode of the device | |
733 | * @ks: The device state | |
734 | * @pwrmode: The power mode value to write to KS_PMECR. | |
735 | * | |
736 | * Change the power mode of the chip. | |
737 | */ | |
738 | static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode) | |
739 | { | |
740 | unsigned pmecr; | |
741 | ||
0dc7d2b3 | 742 | netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode); |
3ba81f3e BD |
743 | |
744 | pmecr = ks8851_rdreg16(ks, KS_PMECR); | |
745 | pmecr &= ~PMECR_PM_MASK; | |
746 | pmecr |= pwrmode; | |
747 | ||
748 | ks8851_wrreg16(ks, KS_PMECR, pmecr); | |
749 | } | |
750 | ||
751 | /** | |
752 | * ks8851_net_open - open network device | |
753 | * @dev: The network device being opened. | |
754 | * | |
755 | * Called when the network device is marked active, such as a user executing | |
756 | * 'ifconfig up' on the device. | |
757 | */ | |
758 | static int ks8851_net_open(struct net_device *dev) | |
759 | { | |
760 | struct ks8851_net *ks = netdev_priv(dev); | |
761 | ||
762 | /* lock the card, even if we may not actually be doing anything | |
763 | * else at the moment */ | |
764 | mutex_lock(&ks->lock); | |
765 | ||
0dc7d2b3 | 766 | netif_dbg(ks, ifup, ks->netdev, "opening\n"); |
3ba81f3e BD |
767 | |
768 | /* bring chip out of any power saving mode it was in */ | |
769 | ks8851_set_powermode(ks, PMECR_PM_NORMAL); | |
770 | ||
771 | /* issue a soft reset to the RX/TX QMU to put it into a known | |
772 | * state. */ | |
773 | ks8851_soft_reset(ks, GRR_QMU); | |
774 | ||
775 | /* setup transmission parameters */ | |
776 | ||
777 | ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */ | |
778 | TXCR_TXPE | /* pad to min length */ | |
779 | TXCR_TXCRC | /* add CRC */ | |
780 | TXCR_TXFCE)); /* enable flow control */ | |
781 | ||
782 | /* auto-increment tx data, reset tx pointer */ | |
783 | ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); | |
784 | ||
785 | /* setup receiver control */ | |
786 | ||
787 | ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */ | |
788 | RXCR1_RXFCE | /* enable flow control */ | |
789 | RXCR1_RXBE | /* broadcast enable */ | |
790 | RXCR1_RXUE | /* unicast enable */ | |
791 | RXCR1_RXE)); /* enable rx block */ | |
792 | ||
793 | /* transfer entire frames out in one go */ | |
794 | ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME); | |
795 | ||
796 | /* set receive counter timeouts */ | |
797 | ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */ | |
798 | ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */ | |
799 | ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */ | |
800 | ||
801 | ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */ | |
802 | RXQCR_RXDBCTE | /* IRQ on byte count exceeded */ | |
803 | RXQCR_RXDTTE); /* IRQ on time exceeded */ | |
804 | ||
805 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); | |
806 | ||
807 | /* clear then enable interrupts */ | |
808 | ||
809 | #define STD_IRQ (IRQ_LCI | /* Link Change */ \ | |
810 | IRQ_TXI | /* TX done */ \ | |
811 | IRQ_RXI | /* RX done */ \ | |
812 | IRQ_SPIBEI | /* SPI bus error */ \ | |
813 | IRQ_TXPSI | /* TX process stop */ \ | |
814 | IRQ_RXPSI) /* RX process stop */ | |
815 | ||
816 | ks->rc_ier = STD_IRQ; | |
817 | ks8851_wrreg16(ks, KS_ISR, STD_IRQ); | |
818 | ks8851_wrreg16(ks, KS_IER, STD_IRQ); | |
819 | ||
820 | netif_start_queue(ks->netdev); | |
821 | ||
0dc7d2b3 | 822 | netif_dbg(ks, ifup, ks->netdev, "network device up\n"); |
3ba81f3e BD |
823 | |
824 | mutex_unlock(&ks->lock); | |
825 | return 0; | |
826 | } | |
827 | ||
828 | /** | |
829 | * ks8851_net_stop - close network device | |
830 | * @dev: The device being closed. | |
831 | * | |
832 | * Called to close down a network device which has been active. Cancell any | |
833 | * work, shutdown the RX and TX process and then place the chip into a low | |
834 | * power state whilst it is not being used. | |
835 | */ | |
836 | static int ks8851_net_stop(struct net_device *dev) | |
837 | { | |
838 | struct ks8851_net *ks = netdev_priv(dev); | |
839 | ||
0dc7d2b3 | 840 | netif_info(ks, ifdown, dev, "shutting down\n"); |
3ba81f3e BD |
841 | |
842 | netif_stop_queue(dev); | |
843 | ||
844 | mutex_lock(&ks->lock); | |
845 | ||
846 | /* stop any outstanding work */ | |
847 | flush_work(&ks->irq_work); | |
848 | flush_work(&ks->tx_work); | |
849 | flush_work(&ks->rxctrl_work); | |
850 | ||
851 | /* turn off the IRQs and ack any outstanding */ | |
852 | ks8851_wrreg16(ks, KS_IER, 0x0000); | |
853 | ks8851_wrreg16(ks, KS_ISR, 0xffff); | |
854 | ||
855 | /* shutdown RX process */ | |
856 | ks8851_wrreg16(ks, KS_RXCR1, 0x0000); | |
857 | ||
858 | /* shutdown TX process */ | |
859 | ks8851_wrreg16(ks, KS_TXCR, 0x0000); | |
860 | ||
861 | /* set powermode to soft power down to save power */ | |
862 | ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN); | |
863 | ||
864 | /* ensure any queued tx buffers are dumped */ | |
865 | while (!skb_queue_empty(&ks->txq)) { | |
866 | struct sk_buff *txb = skb_dequeue(&ks->txq); | |
867 | ||
0dc7d2b3 JP |
868 | netif_dbg(ks, ifdown, ks->netdev, |
869 | "%s: freeing txb %p\n", __func__, txb); | |
3ba81f3e BD |
870 | |
871 | dev_kfree_skb(txb); | |
872 | } | |
873 | ||
874 | mutex_unlock(&ks->lock); | |
875 | return 0; | |
876 | } | |
877 | ||
878 | /** | |
879 | * ks8851_start_xmit - transmit packet | |
880 | * @skb: The buffer to transmit | |
881 | * @dev: The device used to transmit the packet. | |
882 | * | |
883 | * Called by the network layer to transmit the @skb. Queue the packet for | |
884 | * the device and schedule the necessary work to transmit the packet when | |
885 | * it is free. | |
886 | * | |
887 | * We do this to firstly avoid sleeping with the network device locked, | |
888 | * and secondly so we can round up more than one packet to transmit which | |
889 | * means we can try and avoid generating too many transmit done interrupts. | |
890 | */ | |
61357325 SH |
891 | static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb, |
892 | struct net_device *dev) | |
3ba81f3e BD |
893 | { |
894 | struct ks8851_net *ks = netdev_priv(dev); | |
895 | unsigned needed = calc_txlen(skb->len); | |
61357325 | 896 | netdev_tx_t ret = NETDEV_TX_OK; |
3ba81f3e | 897 | |
0dc7d2b3 JP |
898 | netif_dbg(ks, tx_queued, ks->netdev, |
899 | "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data); | |
3ba81f3e BD |
900 | |
901 | spin_lock(&ks->statelock); | |
902 | ||
903 | if (needed > ks->tx_space) { | |
904 | netif_stop_queue(dev); | |
905 | ret = NETDEV_TX_BUSY; | |
906 | } else { | |
907 | ks->tx_space -= needed; | |
908 | skb_queue_tail(&ks->txq, skb); | |
909 | } | |
910 | ||
911 | spin_unlock(&ks->statelock); | |
912 | schedule_work(&ks->tx_work); | |
913 | ||
914 | return ret; | |
915 | } | |
916 | ||
917 | /** | |
918 | * ks8851_rxctrl_work - work handler to change rx mode | |
919 | * @work: The work structure this belongs to. | |
920 | * | |
921 | * Lock the device and issue the necessary changes to the receive mode from | |
922 | * the network device layer. This is done so that we can do this without | |
923 | * having to sleep whilst holding the network device lock. | |
924 | * | |
925 | * Since the recommendation from Micrel is that the RXQ is shutdown whilst the | |
926 | * receive parameters are programmed, we issue a write to disable the RXQ and | |
927 | * then wait for the interrupt handler to be triggered once the RXQ shutdown is | |
928 | * complete. The interrupt handler then writes the new values into the chip. | |
929 | */ | |
930 | static void ks8851_rxctrl_work(struct work_struct *work) | |
931 | { | |
932 | struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work); | |
933 | ||
934 | mutex_lock(&ks->lock); | |
935 | ||
936 | /* need to shutdown RXQ before modifying filter parameters */ | |
937 | ks8851_wrreg16(ks, KS_RXCR1, 0x00); | |
938 | ||
939 | mutex_unlock(&ks->lock); | |
940 | } | |
941 | ||
942 | static void ks8851_set_rx_mode(struct net_device *dev) | |
943 | { | |
944 | struct ks8851_net *ks = netdev_priv(dev); | |
945 | struct ks8851_rxctrl rxctrl; | |
946 | ||
947 | memset(&rxctrl, 0, sizeof(rxctrl)); | |
948 | ||
949 | if (dev->flags & IFF_PROMISC) { | |
950 | /* interface to receive everything */ | |
951 | ||
952 | rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF; | |
953 | } else if (dev->flags & IFF_ALLMULTI) { | |
954 | /* accept all multicast packets */ | |
955 | ||
956 | rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE | | |
957 | RXCR1_RXPAFMA | RXCR1_RXMAFMA); | |
4cd24eaf | 958 | } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) { |
f9dcbcc9 | 959 | struct dev_mc_list *mcptr; |
3ba81f3e | 960 | u32 crc; |
3ba81f3e BD |
961 | |
962 | /* accept some multicast */ | |
963 | ||
f9dcbcc9 | 964 | netdev_for_each_mc_addr(mcptr, dev) { |
3ba81f3e BD |
965 | crc = ether_crc(ETH_ALEN, mcptr->dmi_addr); |
966 | crc >>= (32 - 6); /* get top six bits */ | |
967 | ||
968 | rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf)); | |
969 | mcptr = mcptr->next; | |
970 | } | |
971 | ||
b6a71bfa | 972 | rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA; |
3ba81f3e BD |
973 | } else { |
974 | /* just accept broadcast / unicast */ | |
975 | rxctrl.rxcr1 = RXCR1_RXPAFMA; | |
976 | } | |
977 | ||
978 | rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */ | |
979 | RXCR1_RXBE | /* broadcast enable */ | |
980 | RXCR1_RXE | /* RX process enable */ | |
981 | RXCR1_RXFCE); /* enable flow control */ | |
982 | ||
983 | rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME; | |
984 | ||
985 | /* schedule work to do the actual set of the data if needed */ | |
986 | ||
987 | spin_lock(&ks->statelock); | |
988 | ||
989 | if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) { | |
990 | memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl)); | |
991 | schedule_work(&ks->rxctrl_work); | |
992 | } | |
993 | ||
994 | spin_unlock(&ks->statelock); | |
995 | } | |
996 | ||
997 | static int ks8851_set_mac_address(struct net_device *dev, void *addr) | |
998 | { | |
999 | struct sockaddr *sa = addr; | |
1000 | ||
1001 | if (netif_running(dev)) | |
1002 | return -EBUSY; | |
1003 | ||
1004 | if (!is_valid_ether_addr(sa->sa_data)) | |
1005 | return -EADDRNOTAVAIL; | |
1006 | ||
1007 | memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN); | |
1008 | return ks8851_write_mac_addr(dev); | |
1009 | } | |
1010 | ||
1011 | static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd) | |
1012 | { | |
1013 | struct ks8851_net *ks = netdev_priv(dev); | |
1014 | ||
1015 | if (!netif_running(dev)) | |
1016 | return -EINVAL; | |
1017 | ||
1018 | return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL); | |
1019 | } | |
1020 | ||
1021 | static const struct net_device_ops ks8851_netdev_ops = { | |
1022 | .ndo_open = ks8851_net_open, | |
1023 | .ndo_stop = ks8851_net_stop, | |
1024 | .ndo_do_ioctl = ks8851_net_ioctl, | |
1025 | .ndo_start_xmit = ks8851_start_xmit, | |
1026 | .ndo_set_mac_address = ks8851_set_mac_address, | |
1027 | .ndo_set_rx_mode = ks8851_set_rx_mode, | |
1028 | .ndo_change_mtu = eth_change_mtu, | |
1029 | .ndo_validate_addr = eth_validate_addr, | |
1030 | }; | |
1031 | ||
1032 | /* ethtool support */ | |
1033 | ||
1034 | static void ks8851_get_drvinfo(struct net_device *dev, | |
1035 | struct ethtool_drvinfo *di) | |
1036 | { | |
1037 | strlcpy(di->driver, "KS8851", sizeof(di->driver)); | |
1038 | strlcpy(di->version, "1.00", sizeof(di->version)); | |
1039 | strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info)); | |
1040 | } | |
1041 | ||
1042 | static u32 ks8851_get_msglevel(struct net_device *dev) | |
1043 | { | |
1044 | struct ks8851_net *ks = netdev_priv(dev); | |
1045 | return ks->msg_enable; | |
1046 | } | |
1047 | ||
1048 | static void ks8851_set_msglevel(struct net_device *dev, u32 to) | |
1049 | { | |
1050 | struct ks8851_net *ks = netdev_priv(dev); | |
1051 | ks->msg_enable = to; | |
1052 | } | |
1053 | ||
1054 | static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
1055 | { | |
1056 | struct ks8851_net *ks = netdev_priv(dev); | |
1057 | return mii_ethtool_gset(&ks->mii, cmd); | |
1058 | } | |
1059 | ||
1060 | static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
1061 | { | |
1062 | struct ks8851_net *ks = netdev_priv(dev); | |
1063 | return mii_ethtool_sset(&ks->mii, cmd); | |
1064 | } | |
1065 | ||
1066 | static u32 ks8851_get_link(struct net_device *dev) | |
1067 | { | |
1068 | struct ks8851_net *ks = netdev_priv(dev); | |
1069 | return mii_link_ok(&ks->mii); | |
1070 | } | |
1071 | ||
1072 | static int ks8851_nway_reset(struct net_device *dev) | |
1073 | { | |
1074 | struct ks8851_net *ks = netdev_priv(dev); | |
1075 | return mii_nway_restart(&ks->mii); | |
1076 | } | |
1077 | ||
1078 | static const struct ethtool_ops ks8851_ethtool_ops = { | |
1079 | .get_drvinfo = ks8851_get_drvinfo, | |
1080 | .get_msglevel = ks8851_get_msglevel, | |
1081 | .set_msglevel = ks8851_set_msglevel, | |
1082 | .get_settings = ks8851_get_settings, | |
1083 | .set_settings = ks8851_set_settings, | |
1084 | .get_link = ks8851_get_link, | |
1085 | .nway_reset = ks8851_nway_reset, | |
1086 | }; | |
1087 | ||
1088 | /* MII interface controls */ | |
1089 | ||
1090 | /** | |
1091 | * ks8851_phy_reg - convert MII register into a KS8851 register | |
1092 | * @reg: MII register number. | |
1093 | * | |
1094 | * Return the KS8851 register number for the corresponding MII PHY register | |
1095 | * if possible. Return zero if the MII register has no direct mapping to the | |
1096 | * KS8851 register set. | |
1097 | */ | |
1098 | static int ks8851_phy_reg(int reg) | |
1099 | { | |
1100 | switch (reg) { | |
1101 | case MII_BMCR: | |
1102 | return KS_P1MBCR; | |
1103 | case MII_BMSR: | |
1104 | return KS_P1MBSR; | |
1105 | case MII_PHYSID1: | |
1106 | return KS_PHY1ILR; | |
1107 | case MII_PHYSID2: | |
1108 | return KS_PHY1IHR; | |
1109 | case MII_ADVERTISE: | |
1110 | return KS_P1ANAR; | |
1111 | case MII_LPA: | |
1112 | return KS_P1ANLPR; | |
1113 | } | |
1114 | ||
1115 | return 0x0; | |
1116 | } | |
1117 | ||
1118 | /** | |
1119 | * ks8851_phy_read - MII interface PHY register read. | |
1120 | * @dev: The network device the PHY is on. | |
1121 | * @phy_addr: Address of PHY (ignored as we only have one) | |
1122 | * @reg: The register to read. | |
1123 | * | |
1124 | * This call reads data from the PHY register specified in @reg. Since the | |
1125 | * device does not support all the MII registers, the non-existant values | |
1126 | * are always returned as zero. | |
1127 | * | |
1128 | * We return zero for unsupported registers as the MII code does not check | |
1129 | * the value returned for any error status, and simply returns it to the | |
1130 | * caller. The mii-tool that the driver was tested with takes any -ve error | |
1131 | * as real PHY capabilities, thus displaying incorrect data to the user. | |
1132 | */ | |
1133 | static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg) | |
1134 | { | |
1135 | struct ks8851_net *ks = netdev_priv(dev); | |
1136 | int ksreg; | |
1137 | int result; | |
1138 | ||
1139 | ksreg = ks8851_phy_reg(reg); | |
1140 | if (!ksreg) | |
1141 | return 0x0; /* no error return allowed, so use zero */ | |
1142 | ||
1143 | mutex_lock(&ks->lock); | |
1144 | result = ks8851_rdreg16(ks, ksreg); | |
1145 | mutex_unlock(&ks->lock); | |
1146 | ||
1147 | return result; | |
1148 | } | |
1149 | ||
1150 | static void ks8851_phy_write(struct net_device *dev, | |
1151 | int phy, int reg, int value) | |
1152 | { | |
1153 | struct ks8851_net *ks = netdev_priv(dev); | |
1154 | int ksreg; | |
1155 | ||
1156 | ksreg = ks8851_phy_reg(reg); | |
1157 | if (ksreg) { | |
1158 | mutex_lock(&ks->lock); | |
1159 | ks8851_wrreg16(ks, ksreg, value); | |
1160 | mutex_unlock(&ks->lock); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | /** | |
1165 | * ks8851_read_selftest - read the selftest memory info. | |
1166 | * @ks: The device state | |
1167 | * | |
1168 | * Read and check the TX/RX memory selftest information. | |
1169 | */ | |
1170 | static int ks8851_read_selftest(struct ks8851_net *ks) | |
1171 | { | |
1172 | unsigned both_done = MBIR_TXMBF | MBIR_RXMBF; | |
1173 | int ret = 0; | |
1174 | unsigned rd; | |
1175 | ||
1176 | rd = ks8851_rdreg16(ks, KS_MBIR); | |
1177 | ||
1178 | if ((rd & both_done) != both_done) { | |
0dc7d2b3 | 1179 | netdev_warn(ks->netdev, "Memory selftest not finished\n"); |
3ba81f3e BD |
1180 | return 0; |
1181 | } | |
1182 | ||
1183 | if (rd & MBIR_TXMBFA) { | |
0dc7d2b3 | 1184 | netdev_err(ks->netdev, "TX memory selftest fail\n"); |
3ba81f3e BD |
1185 | ret |= 1; |
1186 | } | |
1187 | ||
1188 | if (rd & MBIR_RXMBFA) { | |
0dc7d2b3 | 1189 | netdev_err(ks->netdev, "RX memory selftest fail\n"); |
3ba81f3e BD |
1190 | ret |= 2; |
1191 | } | |
1192 | ||
1193 | return 0; | |
1194 | } | |
1195 | ||
1196 | /* driver bus management functions */ | |
1197 | ||
1198 | static int __devinit ks8851_probe(struct spi_device *spi) | |
1199 | { | |
1200 | struct net_device *ndev; | |
1201 | struct ks8851_net *ks; | |
1202 | int ret; | |
1203 | ||
1204 | ndev = alloc_etherdev(sizeof(struct ks8851_net)); | |
1205 | if (!ndev) { | |
1206 | dev_err(&spi->dev, "failed to alloc ethernet device\n"); | |
1207 | return -ENOMEM; | |
1208 | } | |
1209 | ||
1210 | spi->bits_per_word = 8; | |
1211 | ||
1212 | ks = netdev_priv(ndev); | |
1213 | ||
1214 | ks->netdev = ndev; | |
1215 | ks->spidev = spi; | |
1216 | ks->tx_space = 6144; | |
1217 | ||
1218 | mutex_init(&ks->lock); | |
1219 | spin_lock_init(&ks->statelock); | |
1220 | ||
1221 | INIT_WORK(&ks->tx_work, ks8851_tx_work); | |
1222 | INIT_WORK(&ks->irq_work, ks8851_irq_work); | |
1223 | INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work); | |
1224 | ||
1225 | /* initialise pre-made spi transfer messages */ | |
1226 | ||
1227 | spi_message_init(&ks->spi_msg1); | |
1228 | spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1); | |
1229 | ||
1230 | spi_message_init(&ks->spi_msg2); | |
1231 | spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2); | |
1232 | spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2); | |
1233 | ||
1234 | /* setup mii state */ | |
1235 | ks->mii.dev = ndev; | |
1236 | ks->mii.phy_id = 1, | |
1237 | ks->mii.phy_id_mask = 1; | |
1238 | ks->mii.reg_num_mask = 0xf; | |
1239 | ks->mii.mdio_read = ks8851_phy_read; | |
1240 | ks->mii.mdio_write = ks8851_phy_write; | |
1241 | ||
1242 | dev_info(&spi->dev, "message enable is %d\n", msg_enable); | |
1243 | ||
1244 | /* set the default message enable */ | |
1245 | ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV | | |
1246 | NETIF_MSG_PROBE | | |
1247 | NETIF_MSG_LINK)); | |
1248 | ||
1249 | skb_queue_head_init(&ks->txq); | |
1250 | ||
1251 | SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops); | |
1252 | SET_NETDEV_DEV(ndev, &spi->dev); | |
1253 | ||
1254 | dev_set_drvdata(&spi->dev, ks); | |
1255 | ||
1256 | ndev->if_port = IF_PORT_100BASET; | |
1257 | ndev->netdev_ops = &ks8851_netdev_ops; | |
1258 | ndev->irq = spi->irq; | |
1259 | ||
57dada68 BD |
1260 | /* issue a global soft reset to reset the device. */ |
1261 | ks8851_soft_reset(ks, GRR_GSR); | |
1262 | ||
3ba81f3e BD |
1263 | /* simple check for a valid chip being connected to the bus */ |
1264 | ||
1265 | if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) { | |
1266 | dev_err(&spi->dev, "failed to read device ID\n"); | |
1267 | ret = -ENODEV; | |
1268 | goto err_id; | |
1269 | } | |
1270 | ||
1271 | ks8851_read_selftest(ks); | |
1272 | ks8851_init_mac(ks); | |
1273 | ||
1274 | ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW, | |
1275 | ndev->name, ks); | |
1276 | if (ret < 0) { | |
1277 | dev_err(&spi->dev, "failed to get irq\n"); | |
1278 | goto err_irq; | |
1279 | } | |
1280 | ||
1281 | ret = register_netdev(ndev); | |
1282 | if (ret) { | |
1283 | dev_err(&spi->dev, "failed to register network device\n"); | |
1284 | goto err_netdev; | |
1285 | } | |
1286 | ||
0dc7d2b3 JP |
1287 | netdev_info(ndev, "revision %d, MAC %pM, IRQ %d\n", |
1288 | CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)), | |
1289 | ndev->dev_addr, ndev->irq); | |
3ba81f3e BD |
1290 | |
1291 | return 0; | |
1292 | ||
1293 | ||
1294 | err_netdev: | |
1295 | free_irq(ndev->irq, ndev); | |
1296 | ||
1297 | err_id: | |
1298 | err_irq: | |
1299 | free_netdev(ndev); | |
1300 | return ret; | |
1301 | } | |
1302 | ||
1303 | static int __devexit ks8851_remove(struct spi_device *spi) | |
1304 | { | |
1305 | struct ks8851_net *priv = dev_get_drvdata(&spi->dev); | |
1306 | ||
1307 | if (netif_msg_drv(priv)) | |
0dc7d2b3 | 1308 | dev_info(&spi->dev, "remove\n"); |
3ba81f3e BD |
1309 | |
1310 | unregister_netdev(priv->netdev); | |
1311 | free_irq(spi->irq, priv); | |
1312 | free_netdev(priv->netdev); | |
1313 | ||
1314 | return 0; | |
1315 | } | |
1316 | ||
1317 | static struct spi_driver ks8851_driver = { | |
1318 | .driver = { | |
1319 | .name = "ks8851", | |
1320 | .owner = THIS_MODULE, | |
1321 | }, | |
1322 | .probe = ks8851_probe, | |
1323 | .remove = __devexit_p(ks8851_remove), | |
1324 | }; | |
1325 | ||
1326 | static int __init ks8851_init(void) | |
1327 | { | |
1328 | return spi_register_driver(&ks8851_driver); | |
1329 | } | |
1330 | ||
1331 | static void __exit ks8851_exit(void) | |
1332 | { | |
1333 | spi_unregister_driver(&ks8851_driver); | |
1334 | } | |
1335 | ||
1336 | module_init(ks8851_init); | |
1337 | module_exit(ks8851_exit); | |
1338 | ||
1339 | MODULE_DESCRIPTION("KS8851 Network driver"); | |
1340 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); | |
1341 | MODULE_LICENSE("GPL"); | |
1342 | ||
1343 | module_param_named(message, msg_enable, int, 0); | |
1344 | MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)"); | |
e0626e38 | 1345 | MODULE_ALIAS("spi:ks8851"); |