jazz_esp.c 8.9 KB

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