jazz_esp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * jazz_esp.c: Driver for SCSI chip on Mips Magnum Boards (JAZZ architecture)
  3. *
  4. * Copyright (C) 1997 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
  5. *
  6. * jazz_esp is based on David S. Miller's ESP driver and cyber_esp
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/stat.h>
  17. #include "scsi.h"
  18. #include <scsi/scsi_host.h>
  19. #include "NCR53C9x.h"
  20. #include <asm/irq.h>
  21. #include <asm/jazz.h>
  22. #include <asm/jazzdma.h>
  23. #include <asm/dma.h>
  24. #include <asm/pgtable.h>
  25. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
  26. static int dma_can_transfer(struct NCR_ESP *esp, struct scsi_cmnd *sp);
  27. static void dma_dump_state(struct NCR_ESP *esp);
  28. static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length);
  29. static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length);
  30. static void dma_ints_off(struct NCR_ESP *esp);
  31. static void dma_ints_on(struct NCR_ESP *esp);
  32. static int dma_irq_p(struct NCR_ESP *esp);
  33. static int dma_ports_p(struct NCR_ESP *esp);
  34. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
  35. static void dma_mmu_get_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp);
  36. static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp);
  37. static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp);
  38. static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp);
  39. static void dma_advance_sg (struct scsi_cmnd *sp);
  40. static void dma_led_off(struct NCR_ESP *);
  41. static void dma_led_on(struct NCR_ESP *);
  42. static volatile unsigned char cmd_buffer[16];
  43. /* This is where all commands are put
  44. * before they are trasfered to the ESP chip
  45. * via PIO.
  46. */
  47. int jazz_esp_detect(struct scsi_host_template *tpnt);
  48. static int jazz_esp_release(struct Scsi_Host *shost)
  49. {
  50. if (shost->irq)
  51. free_irq(shost->irq, NULL);
  52. if (shost->dma_channel != 0xff)
  53. free_dma(shost->dma_channel);
  54. if (shost->io_port && shost->n_io_port)
  55. release_region(shost->io_port, shost->n_io_port);
  56. scsi_unregister(shost);
  57. return 0;
  58. }
  59. /***************************************************************** Detection */
  60. static int jazz_esp_detect(struct scsi_host_template *tpnt)
  61. {
  62. struct NCR_ESP *esp;
  63. struct ConfigDev *esp_dev;
  64. /*
  65. * first assumption it is there:-)
  66. */
  67. if (1) {
  68. esp_dev = 0;
  69. esp = esp_allocate(tpnt, (void *) esp_dev);
  70. /* Do command transfer with programmed I/O */
  71. esp->do_pio_cmds = 1;
  72. /* Required functions */
  73. esp->dma_bytes_sent = &dma_bytes_sent;
  74. esp->dma_can_transfer = &dma_can_transfer;
  75. esp->dma_dump_state = &dma_dump_state;
  76. esp->dma_init_read = &dma_init_read;
  77. esp->dma_init_write = &dma_init_write;
  78. esp->dma_ints_off = &dma_ints_off;
  79. esp->dma_ints_on = &dma_ints_on;
  80. esp->dma_irq_p = &dma_irq_p;
  81. esp->dma_ports_p = &dma_ports_p;
  82. esp->dma_setup = &dma_setup;
  83. /* Optional functions */
  84. esp->dma_barrier = 0;
  85. esp->dma_drain = 0;
  86. esp->dma_invalidate = 0;
  87. esp->dma_irq_entry = 0;
  88. esp->dma_irq_exit = 0;
  89. esp->dma_poll = 0;
  90. esp->dma_reset = 0;
  91. esp->dma_led_off = &dma_led_off;
  92. esp->dma_led_on = &dma_led_on;
  93. /* virtual DMA functions */
  94. esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
  95. esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
  96. esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
  97. esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
  98. esp->dma_advance_sg = &dma_advance_sg;
  99. /* SCSI chip speed */
  100. esp->cfreq = 40000000;
  101. /*
  102. * we don't give the address of DMA channel, but the number
  103. * of DMA channel, so we can use the jazz DMA functions
  104. *
  105. */
  106. esp->dregs = JAZZ_SCSI_DMA;
  107. /* ESP register base */
  108. esp->eregs = (struct ESP_regs *)(JAZZ_SCSI_BASE);
  109. /* Set the command buffer */
  110. esp->esp_command = (volatile unsigned char *)cmd_buffer;
  111. /* get virtual dma address for command buffer */
  112. esp->esp_command_dvma = vdma_alloc(CPHYSADDR(cmd_buffer), sizeof (cmd_buffer));
  113. esp->irq = JAZZ_SCSI_IRQ;
  114. request_irq(JAZZ_SCSI_IRQ, esp_intr, SA_INTERRUPT, "JAZZ SCSI",
  115. esp->ehost);
  116. /*
  117. * FIXME, look if the scsi id is available from NVRAM
  118. */
  119. esp->scsi_id = 7;
  120. /* Check for differential SCSI-bus */
  121. /* What is this stuff? */
  122. esp->diff = 0;
  123. esp_initialize(esp);
  124. printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,esps_in_use);
  125. esps_running = esps_in_use;
  126. return esps_in_use;
  127. }
  128. return 0;
  129. }
  130. /************************************************************* DMA Functions */
  131. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
  132. {
  133. return fifo_count;
  134. }
  135. static int dma_can_transfer(struct NCR_ESP *esp, struct scsi_cmnd *sp)
  136. {
  137. /*
  138. * maximum DMA size is 1MB
  139. */
  140. unsigned long sz = sp->SCp.this_residual;
  141. if(sz > 0x100000)
  142. sz = 0x100000;
  143. return sz;
  144. }
  145. static void dma_dump_state(struct NCR_ESP *esp)
  146. {
  147. ESPLOG(("esp%d: dma -- enable <%08x> residue <%08x\n",
  148. esp->esp_id, vdma_get_enable((int)esp->dregs), vdma_get_residue((int)esp->dregs)));
  149. }
  150. static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
  151. {
  152. dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
  153. vdma_disable ((int)esp->dregs);
  154. vdma_set_mode ((int)esp->dregs, DMA_MODE_READ);
  155. vdma_set_addr ((int)esp->dregs, vaddress);
  156. vdma_set_count ((int)esp->dregs, length);
  157. vdma_enable ((int)esp->dregs);
  158. }
  159. static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
  160. {
  161. dma_cache_wback_inv ((unsigned long)phys_to_virt(vdma_log2phys(vaddress)), length);
  162. vdma_disable ((int)esp->dregs);
  163. vdma_set_mode ((int)esp->dregs, DMA_MODE_WRITE);
  164. vdma_set_addr ((int)esp->dregs, vaddress);
  165. vdma_set_count ((int)esp->dregs, length);
  166. vdma_enable ((int)esp->dregs);
  167. }
  168. static void dma_ints_off(struct NCR_ESP *esp)
  169. {
  170. disable_irq(esp->irq);
  171. }
  172. static void dma_ints_on(struct NCR_ESP *esp)
  173. {
  174. enable_irq(esp->irq);
  175. }
  176. static int dma_irq_p(struct NCR_ESP *esp)
  177. {
  178. return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
  179. }
  180. static int dma_ports_p(struct NCR_ESP *esp)
  181. {
  182. int enable = vdma_get_enable((int)esp->dregs);
  183. return (enable & R4030_CHNL_ENABLE);
  184. }
  185. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
  186. {
  187. /*
  188. * On the Sparc, DMA_ST_WRITE means "move data from device to memory"
  189. * so when (write) is true, it actually means READ!
  190. */
  191. if(write){
  192. dma_init_read(esp, addr, count);
  193. } else {
  194. dma_init_write(esp, addr, count);
  195. }
  196. }
  197. static void dma_mmu_get_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp)
  198. {
  199. sp->SCp.have_data_in = vdma_alloc(CPHYSADDR(sp->SCp.buffer), sp->SCp.this_residual);
  200. sp->SCp.ptr = (char *)((unsigned long)sp->SCp.have_data_in);
  201. }
  202. static void dma_mmu_get_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp)
  203. {
  204. int sz = sp->SCp.buffers_residual;
  205. struct scatterlist *sg = (struct scatterlist *) sp->SCp.buffer;
  206. while (sz >= 0) {
  207. sg[sz].dma_address = vdma_alloc(CPHYSADDR(page_address(sg[sz].page) + sg[sz].offset), sg[sz].length);
  208. sz--;
  209. }
  210. sp->SCp.ptr=(char *)(sp->SCp.buffer->dma_address);
  211. }
  212. static void dma_mmu_release_scsi_one (struct NCR_ESP *esp, struct scsi_cmnd *sp)
  213. {
  214. vdma_free(sp->SCp.have_data_in);
  215. }
  216. static void dma_mmu_release_scsi_sgl (struct NCR_ESP *esp, struct scsi_cmnd *sp)
  217. {
  218. int sz = sp->use_sg - 1;
  219. struct scatterlist *sg = (struct scatterlist *)sp->buffer;
  220. while(sz >= 0) {
  221. vdma_free(sg[sz].dma_address);
  222. sz--;
  223. }
  224. }
  225. static void dma_advance_sg (struct scsi_cmnd *sp)
  226. {
  227. sp->SCp.ptr = (char *)(sp->SCp.buffer->dma_address);
  228. }
  229. #define JAZZ_HDC_LED 0xe000d100 /* FIXME, find correct address */
  230. static void dma_led_off(struct NCR_ESP *esp)
  231. {
  232. #if 0
  233. *(unsigned char *)JAZZ_HDC_LED = 0;
  234. #endif
  235. }
  236. static void dma_led_on(struct NCR_ESP *esp)
  237. {
  238. #if 0
  239. *(unsigned char *)JAZZ_HDC_LED = 1;
  240. #endif
  241. }
  242. static struct scsi_host_template driver_template = {
  243. .proc_name = "jazz_esp",
  244. .proc_info = esp_proc_info,
  245. .name = "ESP 100/100a/200",
  246. .detect = jazz_esp_detect,
  247. .slave_alloc = esp_slave_alloc,
  248. .slave_destroy = esp_slave_destroy,
  249. .release = jazz_esp_release,
  250. .info = esp_info,
  251. .queuecommand = esp_queue,
  252. .eh_abort_handler = esp_abort,
  253. .eh_bus_reset_handler = esp_reset,
  254. .can_queue = 7,
  255. .this_id = 7,
  256. .sg_tablesize = SG_ALL,
  257. .cmd_per_lun = 1,
  258. .use_clustering = DISABLE_CLUSTERING,
  259. };
  260. #include "scsi_module.c"