1 /*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <byteswap.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/ethernet.h>
33 #include <ipxe/if_ether.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/malloc.h>
36 #include <ipxe/pci.h>
37 #include <ipxe/mii.h>
38 #include "myson.h"
39
40 /** @file
41 *
42 * Myson Technology network card driver
43 *
44 */
45
46 /******************************************************************************
47 *
48 * Device reset
49 *
50 ******************************************************************************
51 */
52
53 /**
54 * Reset controller chip
55 *
56 * @v myson Myson device
57 * @ret rc Return status code
58 */
myson_soft_reset(struct myson_nic * myson)59 static int myson_soft_reset ( struct myson_nic *myson ) {
60 uint32_t bcr;
61 unsigned int i;
62
63 /* Initiate reset */
64 bcr = readl ( myson->regs + MYSON_BCR );
65 writel ( ( bcr | MYSON_BCR_SWR ), myson->regs + MYSON_BCR );
66
67 /* Wait for reset to complete */
68 for ( i = 0 ; i < MYSON_RESET_MAX_WAIT_MS ; i++ ) {
69
70 /* If reset is not complete, delay 1ms and retry */
71 if ( readl ( myson->regs + MYSON_BCR ) & MYSON_BCR_SWR ) {
72 mdelay ( 1 );
73 continue;
74 }
75
76 /* Apply a sensible default bus configuration */
77 bcr = readl ( myson->regs + MYSON_BCR );
78 bcr &= ~MYSON_BCR_PBL_MASK;
79 bcr |= ( MYSON_BCR_RLE | MYSON_BCR_RME | MYSON_BCR_WIE |
80 MYSON_BCR_PBL_DEFAULT );
81 writel ( bcr, myson->regs + MYSON_BCR );
82 DBGC ( myson, "MYSON %p using configuration %08x\n",
83 myson, bcr );
84
85 return 0;
86 }
87
88 DBGC ( myson, "MYSON %p timed out waiting for reset\n", myson );
89 return -ETIMEDOUT;
90 }
91
92 /**
93 * Reload configuration from EEPROM
94 *
95 * @v myson Myson device
96 * @ret rc Return status code
97 */
myson_reload_config(struct myson_nic * myson)98 static int myson_reload_config ( struct myson_nic *myson ) {
99 unsigned int i;
100
101 /* Initiate reload */
102 writel ( MYSON_ROM_AUTOLD, myson->regs + MYSON_ROM_MII );
103
104 /* Wait for reload to complete */
105 for ( i = 0 ; i < MYSON_AUTOLD_MAX_WAIT_MS ; i++ ) {
106
107 /* If reload is not complete, delay 1ms and retry */
108 if ( readl ( myson->regs + MYSON_ROM_MII ) & MYSON_ROM_AUTOLD ){
109 mdelay ( 1 );
110 continue;
111 }
112
113 return 0;
114 }
115
116 DBGC ( myson, "MYSON %p timed out waiting for configuration "
117 "reload\n", myson );
118 return -ETIMEDOUT;
119 }
120
121 /**
122 * Reset hardware
123 *
124 * @v myson Myson device
125 * @ret rc Return status code
126 */
myson_reset(struct myson_nic * myson)127 static int myson_reset ( struct myson_nic *myson ) {
128 int rc;
129
130 /* Disable all interrupts */
131 writel ( 0, myson->regs + MYSON_IMR );
132
133 /* Perform soft reset */
134 if ( ( rc = myson_soft_reset ( myson ) ) != 0 )
135 return rc;
136
137 /* Reload configuration from EEPROM */
138 if ( ( rc = myson_reload_config ( myson ) ) != 0 )
139 return rc;
140
141 return 0;
142 }
143
144 /******************************************************************************
145 *
146 * Network device interface
147 *
148 ******************************************************************************
149 */
150
151 /**
152 * Create descriptor ring
153 *
154 * @v myson Myson device
155 * @v ring Descriptor ring
156 * @ret rc Return status code
157 */
myson_create_ring(struct myson_nic * myson,struct myson_ring * ring)158 static int myson_create_ring ( struct myson_nic *myson,
159 struct myson_ring *ring ) {
160 size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
161 struct myson_descriptor *desc;
162 struct myson_descriptor *next;
163 physaddr_t address;
164 unsigned int i;
165 int rc;
166
167 /* Allocate descriptor ring */
168 ring->desc = malloc_dma ( len, MYSON_RING_ALIGN );
169 if ( ! ring->desc ) {
170 rc = -ENOMEM;
171 goto err_alloc;
172 }
173 address = virt_to_bus ( ring->desc );
174
175 /* Check address is usable by card */
176 if ( ! myson_address_ok ( address + len ) ) {
177 DBGC ( myson, "MYSON %p cannot support 64-bit ring address\n",
178 myson );
179 rc = -ENOTSUP;
180 goto err_64bit;
181 }
182
183 /* Initialise descriptor ring */
184 memset ( ring->desc, 0, len );
185 for ( i = 0 ; i < ring->count ; i++ ) {
186 desc = &ring->desc[i];
187 next = &ring->desc[ ( i + 1 ) % ring->count ];
188 desc->next = cpu_to_le32 ( virt_to_bus ( next ) );
189 }
190
191 /* Program ring address */
192 writel ( address, myson->regs + ring->reg );
193 DBGC ( myson, "MYSON %p ring %02x is at [%08llx,%08llx)\n",
194 myson, ring->reg, ( ( unsigned long long ) address ),
195 ( ( unsigned long long ) address + len ) );
196
197 return 0;
198
199 err_64bit:
200 free_dma ( ring->desc, len );
201 ring->desc = NULL;
202 err_alloc:
203 return rc;
204 }
205
206 /**
207 * Destroy descriptor ring
208 *
209 * @v myson Myson device
210 * @v ring Descriptor ring
211 */
myson_destroy_ring(struct myson_nic * myson,struct myson_ring * ring)212 static void myson_destroy_ring ( struct myson_nic *myson,
213 struct myson_ring *ring ) {
214 size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
215
216 /* Clear ring address */
217 writel ( 0, myson->regs + ring->reg );
218
219 /* Free descriptor ring */
220 free_dma ( ring->desc, len );
221 ring->desc = NULL;
222 ring->prod = 0;
223 ring->cons = 0;
224 }
225
226 /**
227 * Refill receive descriptor ring
228 *
229 * @v netdev Network device
230 */
myson_refill_rx(struct net_device * netdev)231 static void myson_refill_rx ( struct net_device *netdev ) {
232 struct myson_nic *myson = netdev->priv;
233 struct myson_descriptor *rx;
234 struct io_buffer *iobuf;
235 unsigned int rx_idx;
236 physaddr_t address;
237
238 while ( ( myson->rx.prod - myson->rx.cons ) < MYSON_NUM_RX_DESC ) {
239
240 /* Allocate I/O buffer */
241 iobuf = alloc_iob ( MYSON_RX_MAX_LEN );
242 if ( ! iobuf ) {
243 /* Wait for next refill */
244 return;
245 }
246
247 /* Check address is usable by card */
248 address = virt_to_bus ( iobuf->data );
249 if ( ! myson_address_ok ( address ) ) {
250 DBGC ( myson, "MYSON %p cannot support 64-bit RX "
251 "buffer address\n", myson );
252 netdev_rx_err ( netdev, iobuf, -ENOTSUP );
253 return;
254 }
255
256 /* Get next receive descriptor */
257 rx_idx = ( myson->rx.prod++ % MYSON_NUM_RX_DESC );
258 rx = &myson->rx.desc[rx_idx];
259
260 /* Populate receive descriptor */
261 rx->address = cpu_to_le32 ( address );
262 rx->control =
263 cpu_to_le32 ( MYSON_RX_CTRL_RBS ( MYSON_RX_MAX_LEN ) );
264 wmb();
265 rx->status = cpu_to_le32 ( MYSON_RX_STAT_OWN );
266 wmb();
267
268 /* Record I/O buffer */
269 assert ( myson->rx_iobuf[rx_idx] == NULL );
270 myson->rx_iobuf[rx_idx] = iobuf;
271
272 /* Notify card that there are descriptors available */
273 writel ( 0, myson->regs + MYSON_RXPDR );
274
275 DBGC2 ( myson, "MYSON %p RX %d is [%llx,%llx)\n", myson,
276 rx_idx, ( ( unsigned long long ) address ),
277 ( ( unsigned long long ) address + MYSON_RX_MAX_LEN ) );
278 }
279 }
280
281 /**
282 * Open network device
283 *
284 * @v netdev Network device
285 * @ret rc Return status code
286 */
myson_open(struct net_device * netdev)287 static int myson_open ( struct net_device *netdev ) {
288 struct myson_nic *myson = netdev->priv;
289 union myson_physical_address mac;
290 int rc;
291
292 /* Set MAC address */
293 memset ( &mac, 0, sizeof ( mac ) );
294 memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
295 writel ( le32_to_cpu ( mac.reg.low ), myson->regs + MYSON_PAR0 );
296 writel ( le32_to_cpu ( mac.reg.high ), myson->regs + MYSON_PAR4 );
297
298 /* Create transmit descriptor ring */
299 if ( ( rc = myson_create_ring ( myson, &myson->tx ) ) != 0 )
300 goto err_create_tx;
301
302 /* Create receive descriptor ring */
303 if ( ( rc = myson_create_ring ( myson, &myson->rx ) ) != 0 )
304 goto err_create_rx;
305
306 /* Configure transmitter and receiver */
307 writel ( ( MYSON_TCR_TE | MYSON_RCR_PROM | MYSON_RCR_AB | MYSON_RCR_AM |
308 MYSON_RCR_ARP | MYSON_RCR_ALP | MYSON_RCR_RE ),
309 myson->regs + MYSON_TCR_RCR );
310
311 /* Fill receive ring */
312 myson_refill_rx ( netdev );
313
314 return 0;
315
316 myson_destroy_ring ( myson, &myson->rx );
317 err_create_rx:
318 myson_destroy_ring ( myson, &myson->tx );
319 err_create_tx:
320 return rc;
321 }
322
323 /**
324 * Wait for transmit and receive to become idle
325 *
326 * @v myson Myson device
327 * @ret rc Return status code
328 */
myson_wait_idle(struct myson_nic * myson)329 static int myson_wait_idle ( struct myson_nic *myson ) {
330 uint32_t tcr_rcr;
331 unsigned int i;
332
333 /* Wait for both transmit and receive to be idle */
334 for ( i = 0 ; i < MYSON_IDLE_MAX_WAIT_MS ; i++ ) {
335
336 /* If either process is running, delay 1ms and retry */
337 tcr_rcr = readl ( myson->regs + MYSON_TCR_RCR );
338 if ( tcr_rcr & ( MYSON_TCR_TXS | MYSON_RCR_RXS ) ) {
339 mdelay ( 1 );
340 continue;
341 }
342
343 return 0;
344 }
345
346 DBGC ( myson, "MYSON %p timed out waiting for idle state (status "
347 "%08x)\n", myson, tcr_rcr );
348 return -ETIMEDOUT;
349 }
350
351 /**
352 * Close network device
353 *
354 * @v netdev Network device
355 */
myson_close(struct net_device * netdev)356 static void myson_close ( struct net_device *netdev ) {
357 struct myson_nic *myson = netdev->priv;
358 unsigned int i;
359
360 /* Disable receiver and transmitter */
361 writel ( 0, myson->regs + MYSON_TCR_RCR );
362
363 /* Allow time for receiver and transmitter to become idle */
364 myson_wait_idle ( myson );
365
366 /* Destroy receive descriptor ring */
367 myson_destroy_ring ( myson, &myson->rx );
368
369 /* Discard any unused receive buffers */
370 for ( i = 0 ; i < MYSON_NUM_RX_DESC ; i++ ) {
371 if ( myson->rx_iobuf[i] )
372 free_iob ( myson->rx_iobuf[i] );
373 myson->rx_iobuf[i] = NULL;
374 }
375
376 /* Destroy transmit descriptor ring */
377 myson_destroy_ring ( myson, &myson->tx );
378 }
379
380 /**
381 * Transmit packet
382 *
383 * @v netdev Network device
384 * @v iobuf I/O buffer
385 * @ret rc Return status code
386 */
myson_transmit(struct net_device * netdev,struct io_buffer * iobuf)387 static int myson_transmit ( struct net_device *netdev,
388 struct io_buffer *iobuf ) {
389 struct myson_nic *myson = netdev->priv;
390 struct myson_descriptor *tx;
391 unsigned int tx_idx;
392 physaddr_t address;
393
394 /* Check address is usable by card */
395 address = virt_to_bus ( iobuf->data );
396 if ( ! myson_address_ok ( address ) ) {
397 DBGC ( myson, "MYSON %p cannot support 64-bit TX buffer "
398 "address\n", myson );
399 return -ENOTSUP;
400 }
401
402 /* Get next transmit descriptor */
403 if ( ( myson->tx.prod - myson->tx.cons ) >= MYSON_NUM_TX_DESC ) {
404 DBGC ( myson, "MYSON %p out of transmit descriptors\n",
405 myson );
406 return -ENOBUFS;
407 }
408 tx_idx = ( myson->tx.prod++ % MYSON_NUM_TX_DESC );
409 tx = &myson->tx.desc[tx_idx];
410
411 /* Populate transmit descriptor */
412 tx->address = cpu_to_le32 ( address );
413 tx->control = cpu_to_le32 ( MYSON_TX_CTRL_IC | MYSON_TX_CTRL_LD |
414 MYSON_TX_CTRL_FD | MYSON_TX_CTRL_CRC |
415 MYSON_TX_CTRL_PAD | MYSON_TX_CTRL_RTLC |
416 MYSON_TX_CTRL_PKTS ( iob_len ( iobuf ) ) |
417 MYSON_TX_CTRL_TBS ( iob_len ( iobuf ) ) );
418 wmb();
419 tx->status = cpu_to_le32 ( MYSON_TX_STAT_OWN );
420 wmb();
421
422 /* Notify card that there are packets ready to transmit */
423 writel ( 0, myson->regs + MYSON_TXPDR );
424
425 DBGC2 ( myson, "MYSON %p TX %d is [%llx,%llx)\n", myson, tx_idx,
426 ( ( unsigned long long ) address ),
427 ( ( unsigned long long ) address + iob_len ( iobuf ) ) );
428
429 return 0;
430 }
431
432 /**
433 * Poll for completed packets
434 *
435 * @v netdev Network device
436 */
myson_poll_tx(struct net_device * netdev)437 static void myson_poll_tx ( struct net_device *netdev ) {
438 struct myson_nic *myson = netdev->priv;
439 struct myson_descriptor *tx;
440 unsigned int tx_idx;
441
442 /* Check for completed packets */
443 while ( myson->tx.cons != myson->tx.prod ) {
444
445 /* Get next transmit descriptor */
446 tx_idx = ( myson->tx.cons % MYSON_NUM_TX_DESC );
447 tx = &myson->tx.desc[tx_idx];
448
449 /* Stop if descriptor is still in use */
450 if ( tx->status & cpu_to_le32 ( MYSON_TX_STAT_OWN ) )
451 return;
452
453 /* Complete TX descriptor */
454 if ( tx->status & cpu_to_le32 ( MYSON_TX_STAT_ABORT |
455 MYSON_TX_STAT_CSL ) ) {
456 DBGC ( myson, "MYSON %p TX %d completion error "
457 "(%08x)\n", myson, tx_idx,
458 le32_to_cpu ( tx->status ) );
459 netdev_tx_complete_next_err ( netdev, -EIO );
460 } else {
461 DBGC2 ( myson, "MYSON %p TX %d complete\n",
462 myson, tx_idx );
463 netdev_tx_complete_next ( netdev );
464 }
465 myson->tx.cons++;
466 }
467 }
468
469 /**
470 * Poll for received packets
471 *
472 * @v netdev Network device
473 */
myson_poll_rx(struct net_device * netdev)474 static void myson_poll_rx ( struct net_device *netdev ) {
475 struct myson_nic *myson = netdev->priv;
476 struct myson_descriptor *rx;
477 struct io_buffer *iobuf;
478 unsigned int rx_idx;
479 size_t len;
480
481 /* Check for received packets */
482 while ( myson->rx.cons != myson->rx.prod ) {
483
484 /* Get next receive descriptor */
485 rx_idx = ( myson->rx.cons % MYSON_NUM_RX_DESC );
486 rx = &myson->rx.desc[rx_idx];
487
488 /* Stop if descriptor is still in use */
489 if ( rx->status & MYSON_RX_STAT_OWN )
490 return;
491
492 /* Populate I/O buffer */
493 iobuf = myson->rx_iobuf[rx_idx];
494 myson->rx_iobuf[rx_idx] = NULL;
495 len = MYSON_RX_STAT_FLNG ( le32_to_cpu ( rx->status ) );
496 iob_put ( iobuf, len - 4 /* strip CRC */ );
497
498 /* Hand off to network stack */
499 if ( rx->status & cpu_to_le32 ( MYSON_RX_STAT_ES ) ) {
500 DBGC ( myson, "MYSON %p RX %d error (length %zd, "
501 "status %08x)\n", myson, rx_idx, len,
502 le32_to_cpu ( rx->status ) );
503 netdev_rx_err ( netdev, iobuf, -EIO );
504 } else {
505 DBGC2 ( myson, "MYSON %p RX %d complete (length "
506 "%zd)\n", myson, rx_idx, len );
507 netdev_rx ( netdev, iobuf );
508 }
509 myson->rx.cons++;
510 }
511 }
512
513 /**
514 * Poll for completed and received packets
515 *
516 * @v netdev Network device
517 */
myson_poll(struct net_device * netdev)518 static void myson_poll ( struct net_device *netdev ) {
519 struct myson_nic *myson = netdev->priv;
520 uint32_t isr;
521 unsigned int i;
522
523 /* Polling the ISR seems to really upset this card; it ends up
524 * getting no useful PCI transfers done and, for some reason,
525 * flooding the network with invalid packets. Work around
526 * this by introducing deliberate delays between ISR reads.
527 */
528 for ( i = 0 ; i < MYSON_ISR_IODELAY_COUNT ; i++ )
529 iodelay();
530
531 /* Check for and acknowledge interrupts */
532 isr = readl ( myson->regs + MYSON_ISR );
533 if ( ! isr )
534 return;
535 writel ( isr, myson->regs + MYSON_ISR );
536
537 /* Poll for TX completions, if applicable */
538 if ( isr & MYSON_IRQ_TI )
539 myson_poll_tx ( netdev );
540
541 /* Poll for RX completionsm, if applicable */
542 if ( isr & MYSON_IRQ_RI )
543 myson_poll_rx ( netdev );
544
545 /* Refill RX ring */
546 myson_refill_rx ( netdev );
547 }
548
549 /**
550 * Enable or disable interrupts
551 *
552 * @v netdev Network device
553 * @v enable Interrupts should be enabled
554 */
myson_irq(struct net_device * netdev,int enable)555 static void myson_irq ( struct net_device *netdev, int enable ) {
556 struct myson_nic *myson = netdev->priv;
557 uint32_t imr;
558
559 imr = ( enable ? ( MYSON_IRQ_TI | MYSON_IRQ_RI ) : 0 );
560 writel ( imr, myson->regs + MYSON_IMR );
561 }
562
563 /** Myson network device operations */
564 static struct net_device_operations myson_operations = {
565 .open = myson_open,
566 .close = myson_close,
567 .transmit = myson_transmit,
568 .poll = myson_poll,
569 .irq = myson_irq,
570 };
571
572 /******************************************************************************
573 *
574 * PCI interface
575 *
576 ******************************************************************************
577 */
578
579 /**
580 * Probe PCI device
581 *
582 * @v pci PCI device
583 * @ret rc Return status code
584 */
myson_probe(struct pci_device * pci)585 static int myson_probe ( struct pci_device *pci ) {
586 struct net_device *netdev;
587 struct myson_nic *myson;
588 union myson_physical_address mac;
589 int rc;
590
591 /* Allocate and initialise net device */
592 netdev = alloc_etherdev ( sizeof ( *myson ) );
593 if ( ! netdev ) {
594 rc = -ENOMEM;
595 goto err_alloc;
596 }
597 netdev_init ( netdev, &myson_operations );
598 myson = netdev->priv;
599 pci_set_drvdata ( pci, netdev );
600 netdev->dev = &pci->dev;
601 memset ( myson, 0, sizeof ( *myson ) );
602 myson_init_ring ( &myson->tx, MYSON_NUM_TX_DESC, MYSON_TXLBA );
603 myson_init_ring ( &myson->rx, MYSON_NUM_RX_DESC, MYSON_RXLBA );
604
605 /* Fix up PCI device */
606 adjust_pci_device ( pci );
607
608 /* Map registers */
609 myson->regs = ioremap ( pci->membase, MYSON_BAR_SIZE );
610 if ( ! myson->regs ) {
611 rc = -ENODEV;
612 goto err_ioremap;
613 }
614
615 /* Reset the NIC */
616 if ( ( rc = myson_reset ( myson ) ) != 0 )
617 goto err_reset;
618
619 /* Read MAC address */
620 mac.reg.low = cpu_to_le32 ( readl ( myson->regs + MYSON_PAR0 ) );
621 mac.reg.high = cpu_to_le32 ( readl ( myson->regs + MYSON_PAR4 ) );
622 memcpy ( netdev->hw_addr, mac.raw, ETH_ALEN );
623
624 /* Register network device */
625 if ( ( rc = register_netdev ( netdev ) ) != 0 )
626 goto err_register_netdev;
627
628 /* Mark as link up; we don't yet handle link state */
629 netdev_link_up ( netdev );
630
631 return 0;
632
633 unregister_netdev ( netdev );
634 err_register_netdev:
635 myson_reset ( myson );
636 err_reset:
637 iounmap ( myson->regs );
638 err_ioremap:
639 netdev_nullify ( netdev );
640 netdev_put ( netdev );
641 err_alloc:
642 return rc;
643 }
644
645 /**
646 * Remove PCI device
647 *
648 * @v pci PCI device
649 */
myson_remove(struct pci_device * pci)650 static void myson_remove ( struct pci_device *pci ) {
651 struct net_device *netdev = pci_get_drvdata ( pci );
652 struct myson_nic *myson = netdev->priv;
653
654 /* Unregister network device */
655 unregister_netdev ( netdev );
656
657 /* Reset card */
658 myson_reset ( myson );
659
660 /* Free network device */
661 iounmap ( myson->regs );
662 netdev_nullify ( netdev );
663 netdev_put ( netdev );
664 }
665
666 /** Myson PCI device IDs */
667 static struct pci_device_id myson_nics[] = {
668 PCI_ROM ( 0x1516, 0x0800, "mtd800", "MTD-8xx", 0 ),
669 PCI_ROM ( 0x1516, 0x0803, "mtd803", "Surecom EP-320X-S", 0 ),
670 PCI_ROM ( 0x1516, 0x0891, "mtd891", "MTD-8xx", 0 ),
671 };
672
673 /** Myson PCI driver */
674 struct pci_driver myson_driver __pci_driver = {
675 .ids = myson_nics,
676 .id_count = ( sizeof ( myson_nics ) / sizeof ( myson_nics[0] ) ),
677 .probe = myson_probe,
678 .remove = myson_remove,
679 };
680