jazz_esp.c 8.4 KB

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