2 * SGI IOC3 master driver and IRQ demuxer
4 * Copyright (c) 2005 Stanislaw Skowronek <skylark@linux-mips.org>
5 * Heavily based on similar work by:
6 * Brent Casavant <bcasavan@sgi.com> - IOC4 master driver
7 * Pat Gefre <pfg@sgi.com> - IOC3 serial port IRQ demuxer
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/interrupt.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/ioc3.h>
19 #include <linux/rwsem.h>
21 #define IOC3_PCI_SIZE 0x100000
23 static LIST_HEAD(ioc3_devices
);
24 static int ioc3_counter
;
25 static DECLARE_RWSEM(ioc3_devices_rwsem
);
27 static struct ioc3_submodule
*ioc3_submodules
[IOC3_MAX_SUBMODULES
];
28 static struct ioc3_submodule
*ioc3_ethernet
;
29 static rwlock_t ioc3_submodules_lock
= RW_LOCK_UNLOCKED
;
31 /* NIC probing code */
33 #define GPCR_MLAN_EN 0x00200000 /* enable MCR to pin 8 */
35 static inline unsigned mcr_pack(unsigned pulse
, unsigned sample
)
37 return (pulse
<< 10) | (sample
<< 2);
40 static int nic_wait(struct ioc3_driver_data
*idd
)
45 mcr
= readl(&idd
->vma
->mcr
);
51 static int nic_reset(struct ioc3_driver_data
*idd
)
56 local_irq_save(flags
);
57 writel(mcr_pack(500, 65), &idd
->vma
->mcr
);
58 presence
= nic_wait(idd
);
59 local_irq_restore(flags
);
66 static int nic_read_bit(struct ioc3_driver_data
*idd
)
71 local_irq_save(flags
);
72 writel(mcr_pack(6, 13), &idd
->vma
->mcr
);
73 result
= nic_wait(idd
);
74 local_irq_restore(flags
);
81 static void nic_write_bit(struct ioc3_driver_data
*idd
, int bit
)
84 writel(mcr_pack(6, 110), &idd
->vma
->mcr
);
86 writel(mcr_pack(80, 30), &idd
->vma
->mcr
);
91 static unsigned nic_read_byte(struct ioc3_driver_data
*idd
)
96 for (i
= 0; i
< 8; i
++)
97 result
= (result
>> 1) | (nic_read_bit(idd
) << 7);
102 static void nic_write_byte(struct ioc3_driver_data
*idd
, int byte
)
106 for (i
= 8; i
; i
--) {
110 nic_write_bit(idd
, bit
);
115 nic_find(struct ioc3_driver_data
*idd
, int *last
, unsigned long addr
)
117 int a
, b
, index
, disc
;
122 nic_write_byte(idd
, 0xF0);
124 /* Algorithm from ``Book of iButton Standards''. */
125 for (index
= 0, disc
= 0; index
< 64; index
++) {
126 a
= nic_read_bit(idd
);
127 b
= nic_read_bit(idd
);
130 printk(KERN_WARNING
"IOC3 NIC search failed.\n");
136 if (index
== *last
) {
137 addr
|= 1UL << index
;
138 } else if (index
> *last
) {
139 addr
&= ~(1UL << index
);
141 } else if ((addr
& (1UL << index
)) == 0)
143 nic_write_bit(idd
, (addr
>>index
)&1);
147 addr
|= 1UL << index
;
149 addr
&= ~(1UL << index
);
150 nic_write_bit(idd
, a
);
158 static void nic_addr(struct ioc3_driver_data
*idd
, unsigned long addr
)
163 nic_write_byte(idd
, 0xF0);
164 for (index
= 0; index
< 64; index
++) {
167 nic_write_bit(idd
, (addr
>>index
)&1);
171 static void crc16_byte(unsigned int *crc
, unsigned char db
)
177 if((db
^(*crc
>>16)) & 1)
184 static unsigned int crc16_area(unsigned char *dbs
, int size
, unsigned int crc
)
187 crc16_byte(&crc
, *(dbs
++));
191 static void crc8_byte(unsigned int *crc
, unsigned char db
)
205 static unsigned int crc8_addr(unsigned long addr
)
208 unsigned int crc
= 0x00;
211 crc8_byte(&crc
, addr
>>(i
<<3));
216 read_redir_page(struct ioc3_driver_data
*idd
, unsigned long addr
, int page
,
217 unsigned char *redir
, unsigned char *data
)
221 while(redir
[page
] != 0xFF) {
222 page
= redir
[page
]^0xFF;
225 printk(KERN_ERR
"IOC3: NIC circular redirection\n");
232 nic_write_byte(idd
, 0xF0);
233 nic_write_byte(idd
, (page
<< 5) & 0xE0);
234 nic_write_byte(idd
, (page
>> 3) & 0x1F);
236 data
[i
] = nic_read_byte(idd
);
237 if(crc16_area(data
, 0x20, 0x0000) == 0x800d)
241 printk(KERN_ERR
"IOC3: CRC error in data page\n");
247 read_redir_map(struct ioc3_driver_data
*idd
, unsigned long addr
,
248 unsigned char *redir
)
250 int i
,j
,loops
= 3,crc_ok
;
256 nic_write_byte(idd
, 0xAA);
257 nic_write_byte(idd
, 0x00);
258 nic_write_byte(idd
, 0x01);
261 redir
[i
+j
] = nic_read_byte(idd
);
262 crc
= crc16_area(redir
+i
, 8, (i
==0)?0x8707:0x0000);
263 crc16_byte(&crc
, nic_read_byte(idd
));
264 crc16_byte(&crc
, nic_read_byte(idd
));
272 printk(KERN_ERR
"IOC3: CRC error in redirection page\n");
277 static void read_nic(struct ioc3_driver_data
*idd
, unsigned long addr
)
279 unsigned char redir
[64];
280 unsigned char data
[64],part
[32];
283 /* read redirections */
284 read_redir_map(idd
, addr
, redir
);
285 /* read data pages */
286 read_redir_page(idd
, addr
, 0, redir
, data
);
287 read_redir_page(idd
, addr
, 1, redir
, data
+32);
288 /* assemble the part # */
291 if(data
[i
+11] != ' ')
292 part
[j
++] = data
[i
+11];
294 if(data
[i
+32] != ' ')
295 part
[j
++] = data
[i
+32];
297 /* skip Octane power supplies */
298 if(!strncmp(part
, "060-0035-", 9))
300 if(!strncmp(part
, "060-0038-", 9))
302 strcpy(idd
->nic_part
, part
);
303 /* assemble the serial # */
307 idd
->nic_serial
[j
++] = data
[i
+1];
308 idd
->nic_serial
[j
] = 0;
311 static void read_mac(struct ioc3_driver_data
*idd
, unsigned long addr
)
314 unsigned char data
[13];
318 nic_write_byte(idd
, 0xF0);
319 nic_write_byte(idd
, 0x00);
320 nic_write_byte(idd
, 0x00);
323 data
[i
] = nic_read_byte(idd
);
324 if(crc16_area(data
, 13, 0x0000) == 0x800d) {
326 idd
->nic_mac
[10-i
] = data
[i
];
331 printk(KERN_ERR
"IOC3: CRC error in MAC address\n");
333 idd
->nic_mac
[i
] = 0x00;
336 static void probe_nic(struct ioc3_driver_data
*idd
)
338 int save
= 0, loops
= 3;
339 unsigned long first
, addr
;
341 writel(GPCR_MLAN_EN
, &idd
->vma
->gpcr_s
);
344 idd
->nic_part
[0] = 0;
345 idd
->nic_serial
[0] = 0;
346 addr
= first
= nic_find(idd
, &save
, 0);
353 switch(addr
& 0xFF) {
364 addr
= nic_find(idd
, &save
, addr
);
370 printk(KERN_ERR
"IOC3: CRC error in NIC address\n");
375 static void write_ireg(struct ioc3_driver_data
*idd
, uint32_t val
, int which
)
379 spin_lock_irqsave(&idd
->ir_lock
, flags
);
382 writel(val
, &idd
->vma
->sio_ies
);
385 writel(val
, &idd
->vma
->sio_iec
);
388 spin_unlock_irqrestore(&idd
->ir_lock
, flags
);
390 static inline uint32_t get_pending_intrs(struct ioc3_driver_data
*idd
)
395 spin_lock_irqsave(&idd
->ir_lock
, flag
);
396 intrs
= readl(&idd
->vma
->sio_ir
);
397 intrs
&= readl(&idd
->vma
->sio_ies
);
398 spin_unlock_irqrestore(&idd
->ir_lock
, flag
);
402 static irqreturn_t
ioc3_intr_io(int irq
, void *arg
, struct pt_regs
*regs
)
405 struct ioc3_driver_data
*idd
= (struct ioc3_driver_data
*)arg
;
407 unsigned int pending
;
409 read_lock_irqsave(&ioc3_submodules_lock
, flags
);
411 if(idd
->dual_irq
&& readb(&idd
->vma
->eisr
)) {
412 /* send Ethernet IRQ to the driver */
413 if(ioc3_ethernet
&& idd
->active
[ioc3_ethernet
->id
] &&
414 ioc3_ethernet
->intr
) {
415 handled
= handled
&& !ioc3_ethernet
->intr(ioc3_ethernet
,
419 pending
= get_pending_intrs(idd
); /* look at the IO IRQs */
421 for(id
=0;id
<IOC3_MAX_SUBMODULES
;id
++) {
422 if(idd
->active
[id
] && ioc3_submodules
[id
]
423 && (pending
& ioc3_submodules
[id
]->irq_mask
)
424 && ioc3_submodules
[id
]->intr
) {
425 write_ireg(idd
, ioc3_submodules
[id
]->irq_mask
,
427 if(!ioc3_submodules
[id
]->intr(ioc3_submodules
[id
],
428 idd
, pending
& ioc3_submodules
[id
]->irq_mask
,
430 pending
&= ~ioc3_submodules
[id
]->irq_mask
;
431 if (ioc3_submodules
[id
]->reset_mask
)
432 write_ireg(idd
, ioc3_submodules
[id
]->irq_mask
,
436 read_unlock_irqrestore(&ioc3_submodules_lock
, flags
);
439 "IOC3: Pending IRQs 0x%08x discarded and disabled\n",pending
);
440 write_ireg(idd
, pending
, IOC3_W_IEC
);
443 return handled
?IRQ_HANDLED
:IRQ_NONE
;
446 static irqreturn_t
ioc3_intr_eth(int irq
, void *arg
, struct pt_regs
*regs
)
449 struct ioc3_driver_data
*idd
= (struct ioc3_driver_data
*)arg
;
454 read_lock_irqsave(&ioc3_submodules_lock
, flags
);
455 if(ioc3_ethernet
&& idd
->active
[ioc3_ethernet
->id
]
456 && ioc3_ethernet
->intr
)
457 handled
= handled
&& !ioc3_ethernet
->intr(ioc3_ethernet
, idd
, 0,
459 read_unlock_irqrestore(&ioc3_submodules_lock
, flags
);
460 return handled
?IRQ_HANDLED
:IRQ_NONE
;
463 void ioc3_enable(struct ioc3_submodule
*is
,
464 struct ioc3_driver_data
*idd
, unsigned int irqs
)
466 write_ireg(idd
, irqs
& is
->irq_mask
, IOC3_W_IES
);
469 void ioc3_ack(struct ioc3_submodule
*is
, struct ioc3_driver_data
*idd
,
472 writel(irqs
& is
->irq_mask
, &idd
->vma
->sio_ir
);
475 void ioc3_disable(struct ioc3_submodule
*is
,
476 struct ioc3_driver_data
*idd
, unsigned int irqs
)
478 write_ireg(idd
, irqs
& is
->irq_mask
, IOC3_W_IEC
);
481 void ioc3_gpcr_set(struct ioc3_driver_data
*idd
, unsigned int val
)
484 spin_lock_irqsave(&idd
->gpio_lock
, flags
);
485 writel(val
, &idd
->vma
->gpcr_s
);
486 spin_unlock_irqrestore(&idd
->gpio_lock
, flags
);
489 /* Keep it simple, stupid! */
490 static int find_slot(void **tab
, int max
)
499 /* Register an IOC3 submodule */
500 int ioc3_register_submodule(struct ioc3_submodule
*is
)
502 struct ioc3_driver_data
*idd
;
506 write_lock_irqsave(&ioc3_submodules_lock
, flags
);
507 alloc_id
= find_slot((void **)ioc3_submodules
, IOC3_MAX_SUBMODULES
);
509 ioc3_submodules
[alloc_id
] = is
;
511 if(ioc3_ethernet
==NULL
)
515 "IOC3 Ethernet module already registered!\n");
518 write_unlock_irqrestore(&ioc3_submodules_lock
, flags
);
521 printk(KERN_WARNING
"Increase IOC3_MAX_SUBMODULES!\n");
527 /* Initialize submodule for each IOC3 */
531 down_read(&ioc3_devices_rwsem
);
532 list_for_each_entry(idd
, &ioc3_devices
, list
) {
533 /* set to 1 for IRQs in probe */
534 idd
->active
[alloc_id
] = 1;
535 idd
->active
[alloc_id
] = !is
->probe(is
, idd
);
537 up_read(&ioc3_devices_rwsem
);
542 /* Unregister an IOC3 submodule */
543 void ioc3_unregister_submodule(struct ioc3_submodule
*is
)
545 struct ioc3_driver_data
*idd
;
548 write_lock_irqsave(&ioc3_submodules_lock
, flags
);
549 if(ioc3_submodules
[is
->id
]==is
)
550 ioc3_submodules
[is
->id
]=NULL
;
553 "IOC3 submodule %s has wrong ID.\n",is
->name
);
554 if(ioc3_ethernet
==is
)
555 ioc3_ethernet
= NULL
;
556 write_unlock_irqrestore(&ioc3_submodules_lock
, flags
);
558 /* Remove submodule for each IOC3 */
559 down_read(&ioc3_devices_rwsem
);
560 list_for_each_entry(idd
, &ioc3_devices
, list
)
561 if(idd
->active
[is
->id
]) {
563 if(is
->remove(is
, idd
))
565 "%s: IOC3 submodule %s remove failed "
567 __FUNCTION__
, module_name(is
->owner
),
568 pci_name(idd
->pdev
));
569 idd
->active
[is
->id
] = 0;
571 write_ireg(idd
, is
->irq_mask
, IOC3_W_IEC
);
573 up_read(&ioc3_devices_rwsem
);
576 /*********************
577 * Device management *
578 *********************/
581 ioc3_class_names
[]={"unknown", "IP27 BaseIO", "IP30 system", "MENET 1/2/3",
582 "MENET 4", "CADduo", "Altix Serial"};
584 static int ioc3_class(struct ioc3_driver_data
*idd
)
586 int res
= IOC3_CLASS_NONE
;
587 /* NIC-based logic */
588 if(!strncmp(idd
->nic_part
, "030-0891-", 9))
589 res
= IOC3_CLASS_BASE_IP30
;
590 if(!strncmp(idd
->nic_part
, "030-1155-", 9))
591 res
= IOC3_CLASS_CADDUO
;
592 if(!strncmp(idd
->nic_part
, "030-1657-", 9))
593 res
= IOC3_CLASS_SERIAL
;
594 if(!strncmp(idd
->nic_part
, "030-1664-", 9))
595 res
= IOC3_CLASS_SERIAL
;
596 /* total random heuristics */
597 #ifdef CONFIG_SGI_IP27
598 if(!idd
->nic_part
[0])
599 res
= IOC3_CLASS_BASE_IP27
;
601 /* print educational message */
602 printk(KERN_INFO
"IOC3 part: [%s], serial: [%s] => class %s\n",
603 idd
->nic_part
, idd
->nic_serial
, ioc3_class_names
[res
]);
606 /* Adds a new instance of an IOC3 card */
607 static int ioc3_probe(struct pci_dev
*pdev
, const struct pci_device_id
*pci_id
)
609 struct ioc3_driver_data
*idd
;
613 /* Enable IOC3 and take ownership of it */
614 if ((ret
= pci_enable_device(pdev
))) {
616 "%s: Failed to enable IOC3 device for pci_dev %s.\n",
617 __FUNCTION__
, pci_name(pdev
));
620 pci_set_master(pdev
);
623 ret
= pci_set_dma_mask(pdev
, DMA_64BIT_MASK
);
625 ret
= pci_set_consistent_dma_mask(pdev
, DMA_64BIT_MASK
);
627 printk(KERN_WARNING
"%s: Unable to obtain 64 bit DMA "
628 "for consistent allocations\n",
634 /* Set up per-IOC3 data */
635 idd
= kmalloc(sizeof(struct ioc3_driver_data
), GFP_KERNEL
);
638 "%s: Failed to allocate IOC3 data for pci_dev %s.\n",
639 __FUNCTION__
, pci_name(pdev
));
643 memset(idd
, 0, sizeof(struct ioc3_driver_data
));
644 spin_lock_init(&idd
->ir_lock
);
645 spin_lock_init(&idd
->gpio_lock
);
648 /* Map all IOC3 registers. These are shared between subdevices
649 * so the main IOC3 module manages them.
651 idd
->pma
= pci_resource_start(pdev
, 0);
654 "%s: Unable to find IOC3 resource "
656 __FUNCTION__
, pci_name(pdev
));
660 if (!request_region(idd
->pma
, IOC3_PCI_SIZE
, "ioc3")) {
662 "%s: Unable to request IOC3 region "
664 __FUNCTION__
, pci_name(pdev
));
668 idd
->vma
= ioremap(idd
->pma
, IOC3_PCI_SIZE
);
671 "%s: Unable to remap IOC3 region "
673 __FUNCTION__
, pci_name(pdev
));
675 goto out_misc_region
;
678 /* Track PCI-device specific data */
679 pci_set_drvdata(pdev
, idd
);
680 down_write(&ioc3_devices_rwsem
);
681 list_add_tail(&idd
->list
, &ioc3_devices
);
682 idd
->id
= ioc3_counter
++;
683 up_write(&ioc3_devices_rwsem
);
685 idd
->gpdr_shadow
= readl(&idd
->vma
->gpdr
);
687 /* Read IOC3 NIC contents */
690 /* Detect IOC3 class */
691 idd
->class = ioc3_class(idd
);
693 /* Initialize IOC3 */
694 pci_read_config_dword(pdev
, PCI_COMMAND
, &pcmd
);
695 pci_write_config_dword(pdev
, PCI_COMMAND
,
696 pcmd
| PCI_COMMAND_MEMORY
|
697 PCI_COMMAND_PARITY
| PCI_COMMAND_SERR
|
698 PCI_SCR_DROP_MODE_EN
);
700 write_ireg(idd
, ~0, IOC3_W_IEC
);
701 writel(~0, &idd
->vma
->sio_ir
);
704 if(idd
->class == IOC3_CLASS_BASE_IP30
705 || idd
->class == IOC3_CLASS_BASE_IP27
) {
706 writel(0, &idd
->vma
->eier
);
707 writel(~0, &idd
->vma
->eisr
);
710 if (!request_irq(pdev
->irq
, ioc3_intr_eth
, SA_SHIRQ
,
711 "ioc3-eth", (void *)idd
)) {
712 idd
->irq_eth
= pdev
->irq
;
715 "%s : request_irq fails for IRQ 0x%x\n ",
716 __FUNCTION__
, pdev
->irq
);
718 if (!request_irq(pdev
->irq
+2, ioc3_intr_io
, SA_SHIRQ
,
719 "ioc3-io", (void *)idd
)) {
720 idd
->irq_io
= pdev
->irq
+2;
723 "%s : request_irq fails for IRQ 0x%x\n ",
724 __FUNCTION__
, pdev
->irq
+2);
727 if (!request_irq(pdev
->irq
, ioc3_intr_io
, SA_SHIRQ
,
728 "ioc3", (void *)idd
)) {
729 idd
->irq_io
= pdev
->irq
;
732 "%s : request_irq fails for IRQ 0x%x\n ",
733 __FUNCTION__
, pdev
->irq
);
737 /* Add this IOC3 to all submodules */
738 for(id
=0;id
<IOC3_MAX_SUBMODULES
;id
++)
739 if(ioc3_submodules
[id
] && ioc3_submodules
[id
]->probe
) {
741 idd
->active
[id
] = !ioc3_submodules
[id
]->probe
742 (ioc3_submodules
[id
], idd
);
745 printk(KERN_INFO
"IOC3 Master Driver loaded for %s\n", pci_name(pdev
));
750 release_region(idd
->pma
, IOC3_PCI_SIZE
);
754 pci_disable_device(pdev
);
759 /* Removes a particular instance of an IOC3 card. */
760 static void ioc3_remove(struct pci_dev
*pdev
)
763 struct ioc3_driver_data
*idd
;
765 idd
= pci_get_drvdata(pdev
);
767 /* Remove this IOC3 from all submodules */
768 for(id
=0;id
<IOC3_MAX_SUBMODULES
;id
++)
769 if(idd
->active
[id
]) {
770 if(ioc3_submodules
[id
] && ioc3_submodules
[id
]->remove
)
771 if(ioc3_submodules
[id
]->remove(ioc3_submodules
[id
],
774 "%s: IOC3 submodule 0x%s remove failed "
777 module_name(ioc3_submodules
[id
]->owner
),
782 /* Clear and disable all IRQs */
783 write_ireg(idd
, ~0, IOC3_W_IEC
);
784 writel(~0, &idd
->vma
->sio_ir
);
786 /* Release resources */
787 free_irq(idd
->irq_io
, (void *)idd
);
789 free_irq(idd
->irq_eth
, (void *)idd
);
791 release_region(idd
->pma
, IOC3_PCI_SIZE
);
793 /* Disable IOC3 and relinquish */
794 pci_disable_device(pdev
);
796 /* Remove and free driver data */
797 down_write(&ioc3_devices_rwsem
);
798 list_del(&idd
->list
);
799 up_write(&ioc3_devices_rwsem
);
803 static struct pci_device_id ioc3_id_table
[] = {
804 {PCI_VENDOR_ID_SGI
, PCI_DEVICE_ID_SGI_IOC3
, PCI_ANY_ID
, PCI_ANY_ID
},
808 static struct pci_driver ioc3_driver
= {
810 .id_table
= ioc3_id_table
,
812 .remove
= ioc3_remove
,
815 MODULE_DEVICE_TABLE(pci
, ioc3_id_table
);
817 /*********************
818 * Module management *
819 *********************/
822 static int __devinit
ioc3_init(void)
824 if (ia64_platform_is("sn2"))
825 return pci_register_driver(&ioc3_driver
);
830 static void __devexit
ioc3_exit(void)
832 pci_unregister_driver(&ioc3_driver
);
835 module_init(ioc3_init
);
836 module_exit(ioc3_exit
);
838 MODULE_AUTHOR("Stanislaw Skowronek <skylark@linux-mips.org>");
839 MODULE_DESCRIPTION("PCI driver for SGI IOC3");
840 MODULE_LICENSE("GPL");
842 EXPORT_SYMBOL_GPL(ioc3_register_submodule
);
843 EXPORT_SYMBOL_GPL(ioc3_unregister_submodule
);
844 EXPORT_SYMBOL_GPL(ioc3_ack
);
845 EXPORT_SYMBOL_GPL(ioc3_gpcr_set
);
846 EXPORT_SYMBOL_GPL(ioc3_disable
);
847 EXPORT_SYMBOL_GPL(ioc3_enable
);