jazz_esp.c 8.9 KB

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