cyberstorm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* cyberstorm.c: Driver for CyberStorm SCSI Controller.
  2. *
  3. * Copyright (C) 1996 Jesper Skov (jskov@cygnus.co.uk)
  4. *
  5. * The CyberStorm SCSI driver is based on David S. Miller's ESP driver
  6. * for the Sparc computers.
  7. *
  8. * This work was made possible by Phase5 who willingly (and most generously)
  9. * supported me with hardware and all the information I needed.
  10. */
  11. /* TODO:
  12. *
  13. * 1) Figure out how to make a cleaner merge with the sparc driver with regard
  14. * to the caches and the Sparc MMU mapping.
  15. * 2) Make as few routines required outside the generic driver. A lot of the
  16. * routines in this file used to be inline!
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/types.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/stat.h>
  28. #include <linux/interrupt.h>
  29. #include "scsi.h"
  30. #include <scsi/scsi_host.h>
  31. #include "NCR53C9x.h"
  32. #include <linux/zorro.h>
  33. #include <asm/irq.h>
  34. #include <asm/amigaints.h>
  35. #include <asm/amigahw.h>
  36. #include <asm/pgtable.h>
  37. /* The controller registers can be found in the Z2 config area at these
  38. * offsets:
  39. */
  40. #define CYBER_ESP_ADDR 0xf400
  41. #define CYBER_DMA_ADDR 0xf800
  42. /* The CyberStorm DMA interface */
  43. struct cyber_dma_registers {
  44. volatile unsigned char dma_addr0; /* DMA address (MSB) [0x000] */
  45. unsigned char dmapad1[1];
  46. volatile unsigned char dma_addr1; /* DMA address [0x002] */
  47. unsigned char dmapad2[1];
  48. volatile unsigned char dma_addr2; /* DMA address [0x004] */
  49. unsigned char dmapad3[1];
  50. volatile unsigned char dma_addr3; /* DMA address (LSB) [0x006] */
  51. unsigned char dmapad4[0x3fb];
  52. volatile unsigned char cond_reg; /* DMA cond (ro) [0x402] */
  53. #define ctrl_reg cond_reg /* DMA control (wo) [0x402] */
  54. };
  55. /* DMA control bits */
  56. #define CYBER_DMA_LED 0x80 /* HD led control 1 = on */
  57. #define CYBER_DMA_WRITE 0x40 /* DMA direction. 1 = write */
  58. #define CYBER_DMA_Z3 0x20 /* 16 (Z2) or 32 (CHIP/Z3) bit DMA transfer */
  59. /* DMA status bits */
  60. #define CYBER_DMA_HNDL_INTR 0x80 /* DMA IRQ pending? */
  61. /* The bits below appears to be Phase5 Debug bits only; they were not
  62. * described by Phase5 so using them may seem a bit stupid...
  63. */
  64. #define CYBER_HOST_ID 0x02 /* If set, host ID should be 7, otherwise
  65. * it should be 6.
  66. */
  67. #define CYBER_SLOW_CABLE 0x08 /* If *not* set, assume SLOW_CABLE */
  68. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
  69. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp);
  70. static void dma_dump_state(struct NCR_ESP *esp);
  71. static void dma_init_read(struct NCR_ESP *esp, __u32 addr, int length);
  72. static void dma_init_write(struct NCR_ESP *esp, __u32 addr, int length);
  73. static void dma_ints_off(struct NCR_ESP *esp);
  74. static void dma_ints_on(struct NCR_ESP *esp);
  75. static int dma_irq_p(struct NCR_ESP *esp);
  76. static void dma_led_off(struct NCR_ESP *esp);
  77. static void dma_led_on(struct NCR_ESP *esp);
  78. static int dma_ports_p(struct NCR_ESP *esp);
  79. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
  80. static unsigned char ctrl_data = 0; /* Keep backup of the stuff written
  81. * to ctrl_reg. Always write a copy
  82. * to this register when writing to
  83. * the hardware register!
  84. */
  85. static volatile unsigned char cmd_buffer[16];
  86. /* This is where all commands are put
  87. * before they are transferred to the ESP chip
  88. * via PIO.
  89. */
  90. /***************************************************************** Detection */
  91. int __init cyber_esp_detect(struct scsi_host_template *tpnt)
  92. {
  93. struct NCR_ESP *esp;
  94. struct zorro_dev *z = NULL;
  95. unsigned long address;
  96. while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
  97. unsigned long board = z->resource.start;
  98. if ((z->id == ZORRO_PROD_PHASE5_BLIZZARD_1220_CYBERSTORM ||
  99. z->id == ZORRO_PROD_PHASE5_BLIZZARD_1230_II_FASTLANE_Z3_CYBERSCSI_CYBERSTORM060) &&
  100. request_mem_region(board+CYBER_ESP_ADDR,
  101. sizeof(struct ESP_regs), "NCR53C9x")) {
  102. /* Figure out if this is a CyberStorm or really a
  103. * Fastlane/Blizzard Mk II by looking at the board size.
  104. * CyberStorm maps 64kB
  105. * (ZORRO_PROD_PHASE5_BLIZZARD_1220_CYBERSTORM does anyway)
  106. */
  107. if(z->resource.end-board != 0xffff) {
  108. release_mem_region(board+CYBER_ESP_ADDR,
  109. sizeof(struct ESP_regs));
  110. return 0;
  111. }
  112. esp = esp_allocate(tpnt, (void *)board+CYBER_ESP_ADDR);
  113. /* Do command transfer with programmed I/O */
  114. esp->do_pio_cmds = 1;
  115. /* Required functions */
  116. esp->dma_bytes_sent = &dma_bytes_sent;
  117. esp->dma_can_transfer = &dma_can_transfer;
  118. esp->dma_dump_state = &dma_dump_state;
  119. esp->dma_init_read = &dma_init_read;
  120. esp->dma_init_write = &dma_init_write;
  121. esp->dma_ints_off = &dma_ints_off;
  122. esp->dma_ints_on = &dma_ints_on;
  123. esp->dma_irq_p = &dma_irq_p;
  124. esp->dma_ports_p = &dma_ports_p;
  125. esp->dma_setup = &dma_setup;
  126. /* Optional functions */
  127. esp->dma_barrier = 0;
  128. esp->dma_drain = 0;
  129. esp->dma_invalidate = 0;
  130. esp->dma_irq_entry = 0;
  131. esp->dma_irq_exit = 0;
  132. esp->dma_led_on = &dma_led_on;
  133. esp->dma_led_off = &dma_led_off;
  134. esp->dma_poll = 0;
  135. esp->dma_reset = 0;
  136. /* SCSI chip speed */
  137. esp->cfreq = 40000000;
  138. /* The DMA registers on the CyberStorm are mapped
  139. * relative to the device (i.e. in the same Zorro
  140. * I/O block).
  141. */
  142. address = (unsigned long)ZTWO_VADDR(board);
  143. esp->dregs = (void *)(address + CYBER_DMA_ADDR);
  144. /* ESP register base */
  145. esp->eregs = (struct ESP_regs *)(address + CYBER_ESP_ADDR);
  146. /* Set the command buffer */
  147. esp->esp_command = cmd_buffer;
  148. esp->esp_command_dvma = virt_to_bus((void *)cmd_buffer);
  149. esp->irq = IRQ_AMIGA_PORTS;
  150. request_irq(IRQ_AMIGA_PORTS, esp_intr, SA_SHIRQ,
  151. "CyberStorm SCSI", esp->ehost);
  152. /* Figure out our scsi ID on the bus */
  153. /* The DMA cond flag contains a hardcoded jumper bit
  154. * which can be used to select host number 6 or 7.
  155. * However, even though it may change, we use a hardcoded
  156. * value of 7.
  157. */
  158. esp->scsi_id = 7;
  159. /* We don't have a differential SCSI-bus. */
  160. esp->diff = 0;
  161. esp_initialize(esp);
  162. printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps, esps_in_use);
  163. esps_running = esps_in_use;
  164. return esps_in_use;
  165. }
  166. }
  167. return 0;
  168. }
  169. /************************************************************* DMA Functions */
  170. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
  171. {
  172. /* Since the CyberStorm DMA is fully dedicated to the ESP chip,
  173. * the number of bytes sent (to the ESP chip) equals the number
  174. * of bytes in the FIFO - there is no buffering in the DMA controller.
  175. * XXXX Do I read this right? It is from host to ESP, right?
  176. */
  177. return fifo_count;
  178. }
  179. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  180. {
  181. /* I don't think there's any limit on the CyberDMA. So we use what
  182. * the ESP chip can handle (24 bit).
  183. */
  184. unsigned long sz = sp->SCp.this_residual;
  185. if(sz > 0x1000000)
  186. sz = 0x1000000;
  187. return sz;
  188. }
  189. static void dma_dump_state(struct NCR_ESP *esp)
  190. {
  191. ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
  192. esp->esp_id, ((struct cyber_dma_registers *)
  193. (esp->dregs))->cond_reg));
  194. ESPLOG(("intreq:<%04x>, intena:<%04x>\n",
  195. amiga_custom.intreqr, amiga_custom.intenar));
  196. }
  197. static void dma_init_read(struct NCR_ESP *esp, __u32 addr, int length)
  198. {
  199. struct cyber_dma_registers *dregs =
  200. (struct cyber_dma_registers *) esp->dregs;
  201. cache_clear(addr, length);
  202. addr &= ~(1);
  203. dregs->dma_addr0 = (addr >> 24) & 0xff;
  204. dregs->dma_addr1 = (addr >> 16) & 0xff;
  205. dregs->dma_addr2 = (addr >> 8) & 0xff;
  206. dregs->dma_addr3 = (addr ) & 0xff;
  207. ctrl_data &= ~(CYBER_DMA_WRITE);
  208. /* Check if physical address is outside Z2 space and of
  209. * block length/block aligned in memory. If this is the
  210. * case, enable 32 bit transfer. In all other cases, fall back
  211. * to 16 bit transfer.
  212. * Obviously 32 bit transfer should be enabled if the DMA address
  213. * and length are 32 bit aligned. However, this leads to some
  214. * strange behavior. Even 64 bit aligned addr/length fails.
  215. * Until I've found a reason for this, 32 bit transfer is only
  216. * used for full-block transfers (1kB).
  217. * -jskov
  218. */
  219. #if 0
  220. if((addr & 0x3fc) || length & 0x3ff || ((addr > 0x200000) &&
  221. (addr < 0xff0000)))
  222. ctrl_data &= ~(CYBER_DMA_Z3); /* Z2, do 16 bit DMA */
  223. else
  224. ctrl_data |= CYBER_DMA_Z3; /* CHIP/Z3, do 32 bit DMA */
  225. #else
  226. ctrl_data &= ~(CYBER_DMA_Z3); /* Z2, do 16 bit DMA */
  227. #endif
  228. dregs->ctrl_reg = ctrl_data;
  229. }
  230. static void dma_init_write(struct NCR_ESP *esp, __u32 addr, int length)
  231. {
  232. struct cyber_dma_registers *dregs =
  233. (struct cyber_dma_registers *) esp->dregs;
  234. cache_push(addr, length);
  235. addr |= 1;
  236. dregs->dma_addr0 = (addr >> 24) & 0xff;
  237. dregs->dma_addr1 = (addr >> 16) & 0xff;
  238. dregs->dma_addr2 = (addr >> 8) & 0xff;
  239. dregs->dma_addr3 = (addr ) & 0xff;
  240. ctrl_data |= CYBER_DMA_WRITE;
  241. /* See comment above */
  242. #if 0
  243. if((addr & 0x3fc) || length & 0x3ff || ((addr > 0x200000) &&
  244. (addr < 0xff0000)))
  245. ctrl_data &= ~(CYBER_DMA_Z3); /* Z2, do 16 bit DMA */
  246. else
  247. ctrl_data |= CYBER_DMA_Z3; /* CHIP/Z3, do 32 bit DMA */
  248. #else
  249. ctrl_data &= ~(CYBER_DMA_Z3); /* Z2, do 16 bit DMA */
  250. #endif
  251. dregs->ctrl_reg = ctrl_data;
  252. }
  253. static void dma_ints_off(struct NCR_ESP *esp)
  254. {
  255. disable_irq(esp->irq);
  256. }
  257. static void dma_ints_on(struct NCR_ESP *esp)
  258. {
  259. enable_irq(esp->irq);
  260. }
  261. static int dma_irq_p(struct NCR_ESP *esp)
  262. {
  263. /* It's important to check the DMA IRQ bit in the correct way! */
  264. return ((esp_read(esp->eregs->esp_status) & ESP_STAT_INTR) &&
  265. ((((struct cyber_dma_registers *)(esp->dregs))->cond_reg) &
  266. CYBER_DMA_HNDL_INTR));
  267. }
  268. static void dma_led_off(struct NCR_ESP *esp)
  269. {
  270. ctrl_data &= ~CYBER_DMA_LED;
  271. ((struct cyber_dma_registers *)(esp->dregs))->ctrl_reg = ctrl_data;
  272. }
  273. static void dma_led_on(struct NCR_ESP *esp)
  274. {
  275. ctrl_data |= CYBER_DMA_LED;
  276. ((struct cyber_dma_registers *)(esp->dregs))->ctrl_reg = ctrl_data;
  277. }
  278. static int dma_ports_p(struct NCR_ESP *esp)
  279. {
  280. return ((amiga_custom.intenar) & IF_PORTS);
  281. }
  282. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
  283. {
  284. /* On the Sparc, DMA_ST_WRITE means "move data from device to memory"
  285. * so when (write) is true, it actually means READ!
  286. */
  287. if(write){
  288. dma_init_read(esp, addr, count);
  289. } else {
  290. dma_init_write(esp, addr, count);
  291. }
  292. }
  293. #define HOSTS_C
  294. int cyber_esp_release(struct Scsi_Host *instance)
  295. {
  296. #ifdef MODULE
  297. unsigned long address = (unsigned long)((struct NCR_ESP *)instance->hostdata)->edev;
  298. esp_deallocate((struct NCR_ESP *)instance->hostdata);
  299. esp_release();
  300. release_mem_region(address, sizeof(struct ESP_regs));
  301. free_irq(IRQ_AMIGA_PORTS, esp_intr);
  302. #endif
  303. return 1;
  304. }
  305. static struct scsi_host_template driver_template = {
  306. .proc_name = "esp-cyberstorm",
  307. .proc_info = esp_proc_info,
  308. .name = "CyberStorm SCSI",
  309. .detect = cyber_esp_detect,
  310. .slave_alloc = esp_slave_alloc,
  311. .slave_destroy = esp_slave_destroy,
  312. .release = cyber_esp_release,
  313. .queuecommand = esp_queue,
  314. .eh_abort_handler = esp_abort,
  315. .eh_bus_reset_handler = esp_reset,
  316. .can_queue = 7,
  317. .this_id = 7,
  318. .sg_tablesize = SG_ALL,
  319. .cmd_per_lun = 1,
  320. .use_clustering = ENABLE_CLUSTERING
  321. };
  322. #include "scsi_module.c"
  323. MODULE_LICENSE("GPL");