oktagon_esp.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Oktagon_esp.c -- Driver for bsc Oktagon
  3.  *
  4.  * Written by Carsten Pluntke 1998
  5.  *
  6.  * Based on cyber_esp.c
  7.  */
  8. #include <linux/config.h>
  9. #if defined(CONFIG_AMIGA) || defined(CONFIG_APUS)
  10. #define USE_BOTTOM_HALF
  11. #endif
  12. #define __KERNEL_SYSCALLS__
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/blk.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/stat.h>
  22. #include <linux/reboot.h>
  23. #include <asm/system.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/pgtable.h>
  26. #include "scsi.h"
  27. #include "hosts.h"
  28. #include "NCR53C9x.h"
  29. #include "oktagon_esp.h"
  30. #include <linux/zorro.h>
  31. #include <asm/irq.h>
  32. #include <asm/amigaints.h>
  33. #include <asm/amigahw.h>
  34. #ifdef USE_BOTTOM_HALF
  35. #include <linux/tqueue.h>
  36. #include <linux/interrupt.h>
  37. #endif
  38. #include <linux/unistd.h>
  39. static int  dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
  40. static int  dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp);
  41. static void dma_dump_state(struct NCR_ESP *esp);
  42. static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length);
  43. static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length);
  44. static void dma_ints_off(struct NCR_ESP *esp);
  45. static void dma_ints_on(struct NCR_ESP *esp);
  46. static int  dma_irq_p(struct NCR_ESP *esp);
  47. static void dma_led_off(struct NCR_ESP *esp);
  48. static void dma_led_on(struct NCR_ESP *esp);
  49. static int  dma_ports_p(struct NCR_ESP *esp);
  50. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
  51. static void dma_irq_exit(struct NCR_ESP *esp);
  52. static void dma_invalidate(struct NCR_ESP *esp);
  53. static void dma_mmu_get_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
  54. static void dma_mmu_get_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
  55. static void dma_mmu_release_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
  56. static void dma_mmu_release_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
  57. static void dma_advance_sg(Scsi_Cmnd *);
  58. static int  oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x);
  59. void esp_bootup_reset(struct NCR_ESP *esp,struct ESP_regs *eregs);
  60. #ifdef USE_BOTTOM_HALF
  61. static void dma_commit(void *opaque);
  62. long oktag_to_io(long *paddr, long *addr, long len);
  63. long oktag_from_io(long *addr, long *paddr, long len);
  64. static struct tq_struct tq_fake_dma = {
  65.     routine: dma_commit,
  66. };
  67. #define DMA_MAXTRANSFER 0x8000
  68. #else
  69. /*
  70.  * No bottom half. Use transfer directly from IRQ. Find a narrow path
  71.  * between too much IRQ overhead and clogging the IRQ for too long.
  72.  */
  73. #define DMA_MAXTRANSFER 0x1000
  74. #endif
  75. static struct notifier_block oktagon_notifier = { 
  76. oktagon_notify_reboot,
  77. NULL,
  78. 0
  79. };
  80. static long *paddress;
  81. static long *address;
  82. static long len;
  83. static long dma_on;
  84. static int direction;
  85. static struct NCR_ESP *current_esp;
  86. static volatile unsigned char cmd_buffer[16];
  87. /* This is where all commands are put
  88.  * before they are trasfered to the ESP chip
  89.  * via PIO.
  90.  */
  91. /***************************************************************** Detection */
  92. int oktagon_esp_detect(Scsi_Host_Template *tpnt)
  93. {
  94. struct NCR_ESP *esp;
  95. struct zorro_dev *z = NULL;
  96. unsigned long address;
  97. struct ESP_regs *eregs;
  98. while ((z = zorro_find_device(ZORRO_PROD_BSC_OKTAGON_2008, z))) {
  99.     unsigned long board = z->resource.start;
  100.     if (request_mem_region(board+OKTAGON_ESP_ADDR,
  101.    sizeof(struct ESP_regs), "NCR53C9x")) {
  102. /*
  103.  * It is a SCSI controller.
  104.  * Hardwire Host adapter to SCSI ID 7
  105.  */
  106. address = (unsigned long)ZTWO_VADDR(board);
  107. eregs = (struct ESP_regs *)(address + OKTAGON_ESP_ADDR);
  108. /* This line was 5 lines lower */
  109. esp = esp_allocate(tpnt, (void *)board+OKTAGON_ESP_ADDR);
  110. /* we have to shift the registers only one bit for oktagon */
  111. esp->shift = 1;
  112. esp_write(eregs->esp_cfg1, (ESP_CONFIG1_PENABLE | 7));
  113. udelay(5);
  114. if (esp_read(eregs->esp_cfg1) != (ESP_CONFIG1_PENABLE | 7))
  115. return 0; /* Bail out if address did not hold data */
  116. /* Do command transfer with programmed I/O */
  117. esp->do_pio_cmds = 1;
  118. /* Required functions */
  119. esp->dma_bytes_sent = &dma_bytes_sent;
  120. esp->dma_can_transfer = &dma_can_transfer;
  121. esp->dma_dump_state = &dma_dump_state;
  122. esp->dma_init_read = &dma_init_read;
  123. esp->dma_init_write = &dma_init_write;
  124. esp->dma_ints_off = &dma_ints_off;
  125. esp->dma_ints_on = &dma_ints_on;
  126. esp->dma_irq_p = &dma_irq_p;
  127. esp->dma_ports_p = &dma_ports_p;
  128. esp->dma_setup = &dma_setup;
  129. /* Optional functions */
  130. esp->dma_barrier = 0;
  131. esp->dma_drain = 0;
  132. esp->dma_invalidate = &dma_invalidate;
  133. esp->dma_irq_entry = 0;
  134. esp->dma_irq_exit = &dma_irq_exit;
  135. esp->dma_led_on = &dma_led_on;
  136. esp->dma_led_off = &dma_led_off;
  137. esp->dma_poll = 0;
  138. esp->dma_reset = 0;
  139. esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
  140. esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
  141. esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
  142. esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
  143. esp->dma_advance_sg = &dma_advance_sg;
  144. /* SCSI chip speed */
  145. /* Looking at the quartz of the SCSI board... */
  146. esp->cfreq = 25000000;
  147. /* The DMA registers on the CyberStorm are mapped
  148.  * relative to the device (i.e. in the same Zorro
  149.  * I/O block).
  150.  */
  151. esp->dregs = (void *)(address + OKTAGON_DMA_ADDR);
  152. paddress = (long *) esp->dregs;
  153. /* ESP register base */
  154. esp->eregs = eregs;
  155. /* Set the command buffer */
  156. esp->esp_command = (volatile unsigned char*) cmd_buffer;
  157. /* Yes, the virtual address. See below. */
  158. esp->esp_command_dvma = (__u32) cmd_buffer;
  159. esp->irq = IRQ_AMIGA_PORTS;
  160. request_irq(IRQ_AMIGA_PORTS, esp_intr, SA_SHIRQ,
  161.     "BSC Oktagon SCSI", esp_intr);
  162. /* Figure out our scsi ID on the bus */
  163. esp->scsi_id = 7;
  164. /* We don't have a differential SCSI-bus. */
  165. esp->diff = 0;
  166. esp_initialize(esp);
  167. printk("ESP_Oktagon Driver 1.1"
  168. #ifdef USE_BOTTOM_HALF
  169.        " [BOTTOM_HALF]"
  170. #else
  171.        " [IRQ]"
  172. #endif
  173.        " registered.n");
  174. printk("ESP: Total of %d ESP hosts found, %d actually in use.n", nesps,esps_in_use);
  175. esps_running = esps_in_use;
  176. current_esp = esp;
  177. register_reboot_notifier(&oktagon_notifier);
  178. return esps_in_use;
  179.     }
  180. }
  181. return 0;
  182. }
  183. /*
  184.  * On certain configurations the SCSI equipment gets confused on reboot,
  185.  * so we have to reset it then.
  186.  */
  187. static int
  188. oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
  189. {
  190.   struct NCR_ESP *esp;
  191.   
  192.   if((code == SYS_DOWN || code == SYS_HALT) && (esp = current_esp))
  193.    {
  194.     esp_bootup_reset(esp,esp->eregs);
  195.     udelay(500); /* Settle time. Maybe unneccessary. */
  196.    }
  197.   return NOTIFY_DONE;
  198. }
  199.     
  200. #ifdef USE_BOTTOM_HALF
  201. /*
  202.  * The bsc Oktagon controller has no real DMA, so we have to do the 'DMA
  203.  * transfer' in the interrupt (Yikes!) or use a bottom half to not to clutter
  204.  * IRQ's for longer-than-good.
  205.  *
  206.  * FIXME
  207.  * BIG PROBLEM: 'len' is usually the buffer length, not the expected length
  208.  * of the data. So DMA may finish prematurely, further reads lead to
  209.  * 'machine check' on APUS systems (don't know about m68k systems, AmigaOS
  210.  * deliberately ignores the bus faults) and a normal copy-loop can't
  211.  * be exited prematurely just at the right moment by the dma_invalidate IRQ.
  212.  * So do it the hard way, write an own copier in assembler and
  213.  * catch the exception.
  214.  *                                     -- Carsten
  215.  */
  216.  
  217.  
  218. static void dma_commit(void *opaque)
  219. {
  220.     long wait,len2,pos;
  221.     struct NCR_ESP *esp;
  222.     ESPDATA(("Transfer: %ld bytes, Address 0x%08lX, Direction: %dn",
  223.          len,(long) address,direction));
  224.     dma_ints_off(current_esp);
  225.     pos = 0;
  226.     wait = 1;
  227.     if(direction) /* write? (memory to device) */
  228.      {
  229.       while(len > 0)
  230.        {
  231.         len2 = oktag_to_io(paddress, address+pos, len);
  232. if(!len2)
  233.  {
  234.   if(wait > 1000)
  235.    {
  236.     printk("Expedited DMA exit (writing) %ldn",len);
  237.     break;
  238.    }
  239.   mdelay(wait);
  240.   wait *= 2;
  241.  }
  242. pos += len2;
  243. len -= len2*sizeof(long);
  244.        }
  245.      } else {
  246.       while(len > 0)
  247.        {
  248.         len2 = oktag_from_io(address+pos, paddress, len);
  249. if(!len2)
  250.  {
  251.   if(wait > 1000)
  252.    {
  253.     printk("Expedited DMA exit (reading) %ldn",len);
  254.     break;
  255.    }
  256.   mdelay(wait);
  257.   wait *= 2;
  258.  }
  259. pos += len2;
  260. len -= len2*sizeof(long);
  261.        }
  262.      }
  263.     /* to make esp->shift work */
  264.     esp=current_esp;
  265. #if 0
  266.     len2 = (esp_read(current_esp->eregs->esp_tclow) & 0xff) |
  267.            ((esp_read(current_esp->eregs->esp_tcmed) & 0xff) << 8);
  268.     /*
  269.      * Uh uh. If you see this, len and transfer count registers were out of
  270.      * sync. That means really serious trouble.
  271.      */
  272.     if(len2)
  273.       printk("Eeeek!! Transfer count still %ld!n",len2);
  274. #endif
  275.     /*
  276.      * Normally we just need to exit and wait for the interrupt to come.
  277.      * But at least one device (my Microtek ScanMaker 630) regularly mis-
  278.      * calculates the bytes it should send which is really ugly because
  279.      * it locks up the SCSI bus if not accounted for.
  280.      */
  281.     if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
  282.      {
  283.       long len = 100;
  284.       long trash[10];
  285.       /*
  286.        * Interrupt bit was not set. Either the device is just plain lazy
  287.        * so we give it a 10 ms chance or...
  288.        */
  289.       while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
  290.         udelay(100);
  291.       if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
  292.        {
  293.         /*
  294.  * So we think that the transfer count is out of sync. Since we
  295.  * have all we want we are happy and can ditch the trash.
  296.  */
  297.  
  298.         len = DMA_MAXTRANSFER;
  299.         while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
  300.           oktag_from_io(trash,paddress,2);
  301.         if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
  302.          {
  303.           /*
  304.            * Things really have gone wrong. If we leave the system in that
  305.            * state, the SCSI bus is locked forever. I hope that this will
  306.            * turn the system in a more or less running state.
  307.            */
  308.           printk("Device is bolixed, trying bus reset...n");
  309.   esp_bootup_reset(current_esp,current_esp->eregs);
  310.          }
  311.        }
  312.      }
  313.     ESPDATA(("Transfer_finale: do_data_finale should comen"));
  314.     len = 0;
  315.     dma_on = 0;
  316.     dma_ints_on(current_esp);
  317. }
  318. #endif
  319. /************************************************************* DMA Functions */
  320. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
  321. {
  322. /* Since the CyberStorm DMA is fully dedicated to the ESP chip,
  323.  * the number of bytes sent (to the ESP chip) equals the number
  324.  * of bytes in the FIFO - there is no buffering in the DMA controller.
  325.  * XXXX Do I read this right? It is from host to ESP, right?
  326.  */
  327. return fifo_count;
  328. }
  329. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  330. {
  331. unsigned long sz = sp->SCp.this_residual;
  332. if(sz > DMA_MAXTRANSFER)
  333. sz = DMA_MAXTRANSFER;
  334. return sz;
  335. }
  336. static void dma_dump_state(struct NCR_ESP *esp)
  337. {
  338. }
  339. /*
  340.  * What the f$@& is this?
  341.  *
  342.  * Some SCSI devices (like my Microtek ScanMaker 630 scanner) want to transfer
  343.  * more data than requested. How much? Dunno. So ditch the bogus data into
  344.  * the sink, hoping the device will advance to the next phase sooner or later.
  345.  *
  346.  *                         -- Carsten
  347.  */
  348. static long oktag_eva_buffer[16]; /* The data sink */
  349. static void oktag_check_dma(void)
  350. {
  351.   struct NCR_ESP *esp;
  352.   esp=current_esp;
  353.   if(!len)
  354.    {
  355.     address = oktag_eva_buffer;
  356.     len = 2;
  357.     /* esp_do_data sets them to zero like len */
  358.     esp_write(current_esp->eregs->esp_tclow,2);
  359.     esp_write(current_esp->eregs->esp_tcmed,0);
  360.    }
  361. }
  362. static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
  363. {
  364. /* Zorro is noncached, everything else done using processor. */
  365. /* cache_clear(addr, length); */
  366. if(dma_on)
  367.   panic("dma_init_read while dma process is initialized/running!n");
  368. direction = 0;
  369. address = (long *) vaddress;
  370. current_esp = esp;
  371. len = length;
  372. oktag_check_dma();
  373.         dma_on = 1;
  374. }
  375. static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
  376. {
  377. /* cache_push(addr, length); */
  378. if(dma_on)
  379.   panic("dma_init_write while dma process is initialized/running!n");
  380. direction = 1;
  381. address = (long *) vaddress;
  382. current_esp = esp;
  383. len = length;
  384. oktag_check_dma();
  385. dma_on = 1;
  386. }
  387. static void dma_ints_off(struct NCR_ESP *esp)
  388. {
  389. disable_irq(esp->irq);
  390. }
  391. static void dma_ints_on(struct NCR_ESP *esp)
  392. {
  393. enable_irq(esp->irq);
  394. }
  395. static int dma_irq_p(struct NCR_ESP *esp)
  396. {
  397. /* It's important to check the DMA IRQ bit in the correct way! */
  398. return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
  399. }
  400. static void dma_led_off(struct NCR_ESP *esp)
  401. {
  402. }
  403. static void dma_led_on(struct NCR_ESP *esp)
  404. {
  405. }
  406. static int dma_ports_p(struct NCR_ESP *esp)
  407. {
  408. return ((custom.intenar) & IF_PORTS);
  409. }
  410. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
  411. {
  412. /* On the Sparc, DMA_ST_WRITE means "move data from device to memory"
  413.  * so when (write) is true, it actually means READ!
  414.  */
  415. if(write){
  416. dma_init_read(esp, addr, count);
  417. } else {
  418. dma_init_write(esp, addr, count);
  419. }
  420. }
  421. /*
  422.  * IRQ entry when DMA transfer is ready to be started
  423.  */
  424. static void dma_irq_exit(struct NCR_ESP *esp)
  425. {
  426. #ifdef USE_BOTTOM_HALF
  427. if(dma_on)
  428.  {
  429.   tq_fake_dma.sync = 0;
  430.   queue_task(&tq_fake_dma,&tq_immediate);
  431.   mark_bh(IMMEDIATE_BH);
  432.  }
  433. #else
  434. while(len && !dma_irq_p(esp))
  435.  {
  436.   if(direction)
  437.     *paddress = *address++;
  438.    else
  439.     *address++ = *paddress;
  440.   len -= (sizeof(long));
  441.  }
  442. len = 0;
  443.         dma_on = 0;
  444. #endif
  445. }
  446. /*
  447.  * IRQ entry when DMA has just finished
  448.  */
  449. static void dma_invalidate(struct NCR_ESP *esp)
  450. {
  451. }
  452. /*
  453.  * Since the processor does the data transfer we have to use the custom
  454.  * mmu interface to pass the virtual address, not the physical.
  455.  */
  456. void dma_mmu_get_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  457. {
  458.         sp->SCp.have_data_in = (int) sp->SCp.ptr =
  459.                 sp->request_buffer;
  460. }
  461. void dma_mmu_get_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  462. {
  463.         sp->SCp.ptr = 
  464.                 sp->SCp.buffer->address;
  465. }
  466. void dma_mmu_release_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  467. {
  468. }
  469. void dma_mmu_release_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  470. {
  471. }
  472. void dma_advance_sg(Scsi_Cmnd *sp)
  473. {
  474.   sp->SCp.ptr = sp->SCp.buffer->address;
  475. }
  476. #define HOSTS_C
  477. #include "oktagon_esp.h"
  478. static Scsi_Host_Template driver_template = SCSI_OKTAGON_ESP;
  479. #include "scsi_module.c"
  480. int oktagon_esp_release(struct Scsi_Host *instance)
  481. {
  482. #ifdef MODULE
  483. unsigned long address = (unsigned long)((struct NCR_ESP *)instance->hostdata)->edev;
  484. esp_release();
  485. release_mem_region(address, sizeof(struct ESP_regs));
  486. free_irq(IRQ_AMIGA_PORTS, esp_intr);
  487. unregister_reboot_notifier(&oktagon_notifier);
  488. #endif
  489. return 1;
  490. }
  491. MODULE_LICENSE("GPL");