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/errno.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/delay.h>
17 #include <linux/ioc3.h>
18 #include <linux/rwsem.h>
20 #define IOC3_PCI_SIZE 0x100000
22 static LIST_HEAD(ioc3_devices);
23 static int ioc3_counter;
24 static DECLARE_RWSEM(ioc3_devices_rwsem);
26 static struct ioc3_submodule *ioc3_submodules[IOC3_MAX_SUBMODULES];
27 static struct ioc3_submodule *ioc3_ethernet;
28 static DEFINE_RWLOCK(ioc3_submodules_lock);
30 /* NIC probing code */
32 #define GPCR_MLAN_EN 0x00200000 /* enable MCR to pin 8 */
34 static inline unsigned mcr_pack(unsigned pulse, unsigned sample)
36 return (pulse << 10) | (sample << 2);
39 static int nic_wait(struct ioc3_driver_data *idd)
44 mcr = readl(&idd->vma->mcr);
50 static int nic_reset(struct ioc3_driver_data *idd)
55 local_irq_save(flags);
56 writel(mcr_pack(500, 65), &idd->vma->mcr);
57 presence = nic_wait(idd);
58 local_irq_restore(flags);
65 static int nic_read_bit(struct ioc3_driver_data *idd)
70 local_irq_save(flags);
71 writel(mcr_pack(6, 13), &idd->vma->mcr);
72 result = nic_wait(idd);
73 local_irq_restore(flags);
80 static void nic_write_bit(struct ioc3_driver_data *idd, int bit)
83 writel(mcr_pack(6, 110), &idd->vma->mcr);
85 writel(mcr_pack(80, 30), &idd->vma->mcr);
90 static unsigned nic_read_byte(struct ioc3_driver_data *idd)
95 for (i = 0; i < 8; i++)
96 result = (result >> 1) | (nic_read_bit(idd) << 7);
101 static void nic_write_byte(struct ioc3_driver_data *idd, int byte)
105 for (i = 8; i; i--) {
109 nic_write_bit(idd, bit);
114 nic_find(struct ioc3_driver_data *idd, int *last, unsigned long addr)
116 int a, b, index, disc;
121 nic_write_byte(idd, 0xF0);
123 /* Algorithm from ``Book of iButton Standards''. */
124 for (index = 0, disc = 0; index < 64; index++) {
125 a = nic_read_bit(idd);
126 b = nic_read_bit(idd);
129 printk(KERN_WARNING "IOC3 NIC search failed.\n");
135 if (index == *last) {
136 addr |= 1UL << index;
137 } else if (index > *last) {
138 addr &= ~(1UL << index);
140 } else if ((addr & (1UL << index)) == 0)
142 nic_write_bit(idd, (addr>>index)&1);
146 addr |= 1UL << index;
148 addr &= ~(1UL << index);
149 nic_write_bit(idd, a);
157 static void nic_addr(struct ioc3_driver_data *idd, unsigned long addr)
162 nic_write_byte(idd, 0xF0);
163 for (index = 0; index < 64; index++) {
166 nic_write_bit(idd, (addr>>index)&1);
170 static void crc16_byte(unsigned int *crc, unsigned char db)
176 if((db^(*crc>>16)) & 1)
183 static unsigned int crc16_area(unsigned char *dbs, int size, unsigned int crc)
186 crc16_byte(&crc, *(dbs++));
190 static void crc8_byte(unsigned int *crc, unsigned char db)
204 static unsigned int crc8_addr(unsigned long addr)
207 unsigned int crc = 0x00;
210 crc8_byte(&crc, addr>>(i<<3));
215 read_redir_page(struct ioc3_driver_data *idd, unsigned long addr, int page,
216 unsigned char *redir, unsigned char *data)
220 while(redir[page] != 0xFF) {
221 page = redir[page]^0xFF;
224 printk(KERN_ERR "IOC3: NIC circular redirection\n");
231 nic_write_byte(idd, 0xF0);
232 nic_write_byte(idd, (page << 5) & 0xE0);
233 nic_write_byte(idd, (page >> 3) & 0x1F);
235 data[i] = nic_read_byte(idd);
236 if(crc16_area(data, 0x20, 0x0000) == 0x800d)
240 printk(KERN_ERR "IOC3: CRC error in data page\n");
246 read_redir_map(struct ioc3_driver_data *idd, unsigned long addr,
247 unsigned char *redir)
249 int i,j,loops = 3,crc_ok;
255 nic_write_byte(idd, 0xAA);
256 nic_write_byte(idd, 0x00);
257 nic_write_byte(idd, 0x01);
260 redir[i+j] = nic_read_byte(idd);
261 crc = crc16_area(redir+i, 8, (i==0)?0x8707:0x0000);
262 crc16_byte(&crc, nic_read_byte(idd));
263 crc16_byte(&crc, nic_read_byte(idd));
271 printk(KERN_ERR "IOC3: CRC error in redirection page\n");
276 static void read_nic(struct ioc3_driver_data *idd, unsigned long addr)
278 unsigned char redir[64];
279 unsigned char data[64],part[32];
282 /* read redirections */
283 read_redir_map(idd, addr, redir);
284 /* read data pages */
285 read_redir_page(idd, addr, 0, redir, data);
286 read_redir_page(idd, addr, 1, redir, data+32);
287 /* assemble the part # */
290 if(data[i+11] != ' ')
291 part[j++] = data[i+11];
293 if(data[i+32] != ' ')
294 part[j++] = data[i+32];
296 /* skip Octane power supplies */
297 if(!strncmp(part, "060-0035-", 9))
299 if(!strncmp(part, "060-0038-", 9))
301 strcpy(idd->nic_part, part);
302 /* assemble the serial # */
306 idd->nic_serial[j++] = data[i+1];
307 idd->nic_serial[j] = 0;
310 static void read_mac(struct ioc3_driver_data *idd, unsigned long addr)
313 unsigned char data[13];
317 nic_write_byte(idd, 0xF0);
318 nic_write_byte(idd, 0x00);
319 nic_write_byte(idd, 0x00);
322 data[i] = nic_read_byte(idd);
323 if(crc16_area(data, 13, 0x0000) == 0x800d) {
325 idd->nic_mac[10-i] = data[i];
330 printk(KERN_ERR "IOC3: CRC error in MAC address\n");
332 idd->nic_mac[i] = 0x00;
335 static void probe_nic(struct ioc3_driver_data *idd)
337 int save = 0, loops = 3;
338 unsigned long first, addr;
340 writel(GPCR_MLAN_EN, &idd->vma->gpcr_s);
343 idd->nic_part[0] = 0;
344 idd->nic_serial[0] = 0;
345 addr = first = nic_find(idd, &save, 0);
352 switch(addr & 0xFF) {
363 addr = nic_find(idd, &save, addr);
369 printk(KERN_ERR "IOC3: CRC error in NIC address\n");
374 static void write_ireg(struct ioc3_driver_data *idd, uint32_t val, int which)
378 spin_lock_irqsave(&idd->ir_lock, flags);
381 writel(val, &idd->vma->sio_ies);
384 writel(val, &idd->vma->sio_iec);
387 spin_unlock_irqrestore(&idd->ir_lock, flags);
389 static inline uint32_t get_pending_intrs(struct ioc3_driver_data *idd)
394 spin_lock_irqsave(&idd->ir_lock, flag);
395 intrs = readl(&idd->vma->sio_ir);
396 intrs &= readl(&idd->vma->sio_ies);
397 spin_unlock_irqrestore(&idd->ir_lock, flag);
401 static irqreturn_t ioc3_intr_io(int irq, void *arg, struct pt_regs *regs)
404 struct ioc3_driver_data *idd = (struct ioc3_driver_data *)arg;
406 unsigned int pending;
408 read_lock_irqsave(&ioc3_submodules_lock, flags);
410 if(idd->dual_irq && readb(&idd->vma->eisr)) {
411 /* send Ethernet IRQ to the driver */
412 if(ioc3_ethernet && idd->active[ioc3_ethernet->id] &&
413 ioc3_ethernet->intr) {
414 handled = handled && !ioc3_ethernet->intr(ioc3_ethernet,
418 pending = get_pending_intrs(idd); /* look at the IO IRQs */
420 for(id=0;id<IOC3_MAX_SUBMODULES;id++) {
421 if(idd->active[id] && ioc3_submodules[id]
422 && (pending & ioc3_submodules[id]->irq_mask)
423 && ioc3_submodules[id]->intr) {
424 write_ireg(idd, ioc3_submodules[id]->irq_mask,
426 if(!ioc3_submodules[id]->intr(ioc3_submodules[id],
427 idd, pending & ioc3_submodules[id]->irq_mask,
429 pending &= ~ioc3_submodules[id]->irq_mask;
430 if (ioc3_submodules[id]->reset_mask)
431 write_ireg(idd, ioc3_submodules[id]->irq_mask,
435 read_unlock_irqrestore(&ioc3_submodules_lock, flags);
438 "IOC3: Pending IRQs 0x%08x discarded and disabled\n",pending);
439 write_ireg(idd, pending, IOC3_W_IEC);
442 return handled?IRQ_HANDLED:IRQ_NONE;
445 static irqreturn_t ioc3_intr_eth(int irq, void *arg, struct pt_regs *regs)
448 struct ioc3_driver_data *idd = (struct ioc3_driver_data *)arg;
453 read_lock_irqsave(&ioc3_submodules_lock, flags);
454 if(ioc3_ethernet && idd->active[ioc3_ethernet->id]
455 && ioc3_ethernet->intr)
456 handled = handled && !ioc3_ethernet->intr(ioc3_ethernet, idd, 0,
458 read_unlock_irqrestore(&ioc3_submodules_lock, flags);
459 return handled?IRQ_HANDLED:IRQ_NONE;
462 void ioc3_enable(struct ioc3_submodule *is,
463 struct ioc3_driver_data *idd, unsigned int irqs)
465 write_ireg(idd, irqs & is->irq_mask, IOC3_W_IES);
468 void ioc3_ack(struct ioc3_submodule *is, struct ioc3_driver_data *idd,
471 writel(irqs & is->irq_mask, &idd->vma->sio_ir);
474 void ioc3_disable(struct ioc3_submodule *is,
475 struct ioc3_driver_data *idd, unsigned int irqs)
477 write_ireg(idd, irqs & is->irq_mask, IOC3_W_IEC);
480 void ioc3_gpcr_set(struct ioc3_driver_data *idd, unsigned int val)
483 spin_lock_irqsave(&idd->gpio_lock, flags);
484 writel(val, &idd->vma->gpcr_s);
485 spin_unlock_irqrestore(&idd->gpio_lock, flags);
488 /* Keep it simple, stupid! */
489 static int find_slot(void **tab, int max)
498 /* Register an IOC3 submodule */
499 int ioc3_register_submodule(struct ioc3_submodule *is)
501 struct ioc3_driver_data *idd;
505 write_lock_irqsave(&ioc3_submodules_lock, flags);
506 alloc_id = find_slot((void **)ioc3_submodules, IOC3_MAX_SUBMODULES);
508 ioc3_submodules[alloc_id] = is;
510 if(ioc3_ethernet==NULL)
514 "IOC3 Ethernet module already registered!\n");
517 write_unlock_irqrestore(&ioc3_submodules_lock, flags);
520 printk(KERN_WARNING "Increase IOC3_MAX_SUBMODULES!\n");
526 /* Initialize submodule for each IOC3 */
530 down_read(&ioc3_devices_rwsem);
531 list_for_each_entry(idd, &ioc3_devices, list) {
532 /* set to 1 for IRQs in probe */
533 idd->active[alloc_id] = 1;
534 idd->active[alloc_id] = !is->probe(is, idd);
536 up_read(&ioc3_devices_rwsem);
541 /* Unregister an IOC3 submodule */
542 void ioc3_unregister_submodule(struct ioc3_submodule *is)
544 struct ioc3_driver_data *idd;
547 write_lock_irqsave(&ioc3_submodules_lock, flags);
548 if(ioc3_submodules[is->id]==is)
549 ioc3_submodules[is->id]=NULL;
552 "IOC3 submodule %s has wrong ID.\n",is->name);
553 if(ioc3_ethernet==is)
554 ioc3_ethernet = NULL;
555 write_unlock_irqrestore(&ioc3_submodules_lock, flags);
557 /* Remove submodule for each IOC3 */
558 down_read(&ioc3_devices_rwsem);
559 list_for_each_entry(idd, &ioc3_devices, list)
560 if(idd->active[is->id]) {
562 if(is->remove(is, idd))
564 "%s: IOC3 submodule %s remove failed "
566 __FUNCTION__, module_name(is->owner),
567 pci_name(idd->pdev));
568 idd->active[is->id] = 0;
570 write_ireg(idd, is->irq_mask, IOC3_W_IEC);
572 up_read(&ioc3_devices_rwsem);
575 /*********************
576 * Device management *
577 *********************/
580 ioc3_class_names[]={"unknown", "IP27 BaseIO", "IP30 system", "MENET 1/2/3",
581 "MENET 4", "CADduo", "Altix Serial"};
583 static int ioc3_class(struct ioc3_driver_data *idd)
585 int res = IOC3_CLASS_NONE;
586 /* NIC-based logic */
587 if(!strncmp(idd->nic_part, "030-0891-", 9))
588 res = IOC3_CLASS_BASE_IP30;
589 if(!strncmp(idd->nic_part, "030-1155-", 9))
590 res = IOC3_CLASS_CADDUO;
591 if(!strncmp(idd->nic_part, "030-1657-", 9))
592 res = IOC3_CLASS_SERIAL;
593 if(!strncmp(idd->nic_part, "030-1664-", 9))
594 res = IOC3_CLASS_SERIAL;
595 /* total random heuristics */
596 #ifdef CONFIG_SGI_IP27
597 if(!idd->nic_part[0])
598 res = IOC3_CLASS_BASE_IP27;
600 /* print educational message */
601 printk(KERN_INFO "IOC3 part: [%s], serial: [%s] => class %s\n",
602 idd->nic_part, idd->nic_serial, ioc3_class_names[res]);
605 /* Adds a new instance of an IOC3 card */
606 static int ioc3_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
608 struct ioc3_driver_data *idd;
612 /* Enable IOC3 and take ownership of it */
613 if ((ret = pci_enable_device(pdev))) {
615 "%s: Failed to enable IOC3 device for pci_dev %s.\n",
616 __FUNCTION__, pci_name(pdev));
619 pci_set_master(pdev);
622 ret = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
624 ret = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
626 printk(KERN_WARNING "%s: Unable to obtain 64 bit DMA "
627 "for consistent allocations\n",
633 /* Set up per-IOC3 data */
634 idd = kmalloc(sizeof(struct ioc3_driver_data), GFP_KERNEL);
637 "%s: Failed to allocate IOC3 data for pci_dev %s.\n",
638 __FUNCTION__, pci_name(pdev));
642 memset(idd, 0, sizeof(struct ioc3_driver_data));
643 spin_lock_init(&idd->ir_lock);
644 spin_lock_init(&idd->gpio_lock);
647 /* Map all IOC3 registers. These are shared between subdevices
648 * so the main IOC3 module manages them.
650 idd->pma = pci_resource_start(pdev, 0);
653 "%s: Unable to find IOC3 resource "
655 __FUNCTION__, pci_name(pdev));
659 if (!request_region(idd->pma, IOC3_PCI_SIZE, "ioc3")) {
661 "%s: Unable to request IOC3 region "
663 __FUNCTION__, pci_name(pdev));
667 idd->vma = ioremap(idd->pma, IOC3_PCI_SIZE);
670 "%s: Unable to remap IOC3 region "
672 __FUNCTION__, pci_name(pdev));
674 goto out_misc_region;
677 /* Track PCI-device specific data */
678 pci_set_drvdata(pdev, idd);
679 down_write(&ioc3_devices_rwsem);
680 list_add_tail(&idd->list, &ioc3_devices);
681 idd->id = ioc3_counter++;
682 up_write(&ioc3_devices_rwsem);
684 idd->gpdr_shadow = readl(&idd->vma->gpdr);
686 /* Read IOC3 NIC contents */
689 /* Detect IOC3 class */
690 idd->class = ioc3_class(idd);
692 /* Initialize IOC3 */
693 pci_read_config_dword(pdev, PCI_COMMAND, &pcmd);
694 pci_write_config_dword(pdev, PCI_COMMAND,
695 pcmd | PCI_COMMAND_MEMORY |
696 PCI_COMMAND_PARITY | PCI_COMMAND_SERR |
697 PCI_SCR_DROP_MODE_EN);
699 write_ireg(idd, ~0, IOC3_W_IEC);
700 writel(~0, &idd->vma->sio_ir);
703 if(idd->class == IOC3_CLASS_BASE_IP30
704 || idd->class == IOC3_CLASS_BASE_IP27) {
705 writel(0, &idd->vma->eier);
706 writel(~0, &idd->vma->eisr);
709 if (!request_irq(pdev->irq, ioc3_intr_eth, SA_SHIRQ,
710 "ioc3-eth", (void *)idd)) {
711 idd->irq_eth = pdev->irq;
714 "%s : request_irq fails for IRQ 0x%x\n ",
715 __FUNCTION__, pdev->irq);
717 if (!request_irq(pdev->irq+2, ioc3_intr_io, SA_SHIRQ,
718 "ioc3-io", (void *)idd)) {
719 idd->irq_io = pdev->irq+2;
722 "%s : request_irq fails for IRQ 0x%x\n ",
723 __FUNCTION__, pdev->irq+2);
726 if (!request_irq(pdev->irq, ioc3_intr_io, SA_SHIRQ,
727 "ioc3", (void *)idd)) {
728 idd->irq_io = pdev->irq;
731 "%s : request_irq fails for IRQ 0x%x\n ",
732 __FUNCTION__, pdev->irq);
736 /* Add this IOC3 to all submodules */
737 for(id=0;id<IOC3_MAX_SUBMODULES;id++)
738 if(ioc3_submodules[id] && ioc3_submodules[id]->probe) {
740 idd->active[id] = !ioc3_submodules[id]->probe
741 (ioc3_submodules[id], idd);
744 printk(KERN_INFO "IOC3 Master Driver loaded for %s\n", pci_name(pdev));
749 release_region(idd->pma, IOC3_PCI_SIZE);
753 pci_disable_device(pdev);
758 /* Removes a particular instance of an IOC3 card. */
759 static void ioc3_remove(struct pci_dev *pdev)
762 struct ioc3_driver_data *idd;
764 idd = pci_get_drvdata(pdev);
766 /* Remove this IOC3 from all submodules */
767 for(id=0;id<IOC3_MAX_SUBMODULES;id++)
768 if(idd->active[id]) {
769 if(ioc3_submodules[id] && ioc3_submodules[id]->remove)
770 if(ioc3_submodules[id]->remove(ioc3_submodules[id],
773 "%s: IOC3 submodule 0x%s remove failed "
776 module_name(ioc3_submodules[id]->owner),
781 /* Clear and disable all IRQs */
782 write_ireg(idd, ~0, IOC3_W_IEC);
783 writel(~0, &idd->vma->sio_ir);
785 /* Release resources */
786 free_irq(idd->irq_io, (void *)idd);
788 free_irq(idd->irq_eth, (void *)idd);
790 release_region(idd->pma, IOC3_PCI_SIZE);
792 /* Disable IOC3 and relinquish */
793 pci_disable_device(pdev);
795 /* Remove and free driver data */
796 down_write(&ioc3_devices_rwsem);
797 list_del(&idd->list);
798 up_write(&ioc3_devices_rwsem);
802 static struct pci_device_id ioc3_id_table[] = {
803 {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID},
807 static struct pci_driver ioc3_driver = {
809 .id_table = ioc3_id_table,
811 .remove = ioc3_remove,
814 MODULE_DEVICE_TABLE(pci, ioc3_id_table);
816 /*********************
817 * Module management *
818 *********************/
821 static int __devinit ioc3_init(void)
823 if (ia64_platform_is("sn2"))
824 return pci_register_driver(&ioc3_driver);
829 static void __devexit ioc3_exit(void)
831 pci_unregister_driver(&ioc3_driver);
834 module_init(ioc3_init);
835 module_exit(ioc3_exit);
837 MODULE_AUTHOR("Stanislaw Skowronek <skylark@linux-mips.org>");
838 MODULE_DESCRIPTION("PCI driver for SGI IOC3");
839 MODULE_LICENSE("GPL");
841 EXPORT_SYMBOL_GPL(ioc3_register_submodule);
842 EXPORT_SYMBOL_GPL(ioc3_unregister_submodule);
843 EXPORT_SYMBOL_GPL(ioc3_ack);
844 EXPORT_SYMBOL_GPL(ioc3_gpcr_set);
845 EXPORT_SYMBOL_GPL(ioc3_disable);
846 EXPORT_SYMBOL_GPL(ioc3_enable);