fastlane.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* fastlane.c: Driver for Phase5's Fastlane SCSI Controller.
  2. *
  3. * Copyright (C) 1996 Jesper Skov (jskov@cygnus.co.uk)
  4. *
  5. * This driver is based on the CyberStorm driver, hence the occasional
  6. * reference to CyberStorm.
  7. *
  8. * Betatesting & crucial adjustments by
  9. * Patrik Rak (prak3264@ss1000.ms.mff.cuni.cz)
  10. *
  11. */
  12. /* TODO:
  13. *
  14. * o According to the doc from laire, it is required to reset the DMA when
  15. * the transfer is done. ATM we reset DMA just before every new
  16. * dma_init_(read|write).
  17. *
  18. * 1) Figure out how to make a cleaner merge with the sparc driver with regard
  19. * to the caches and the Sparc MMU mapping.
  20. * 2) Make as few routines required outside the generic driver. A lot of the
  21. * routines in this file used to be inline!
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/types.h>
  28. #include <linux/string.h>
  29. #include <linux/slab.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/stat.h>
  33. #include <linux/interrupt.h>
  34. #include "scsi.h"
  35. #include <scsi/scsi_host.h>
  36. #include "NCR53C9x.h"
  37. #include <linux/zorro.h>
  38. #include <asm/irq.h>
  39. #include <asm/amigaints.h>
  40. #include <asm/amigahw.h>
  41. #include <asm/pgtable.h>
  42. /* Such day has just come... */
  43. #if 0
  44. /* Let this defined unless you really need to enable DMA IRQ one day */
  45. #define NODMAIRQ
  46. #endif
  47. /* The controller registers can be found in the Z2 config area at these
  48. * offsets:
  49. */
  50. #define FASTLANE_ESP_ADDR 0x1000001
  51. #define FASTLANE_DMA_ADDR 0x1000041
  52. /* The Fastlane DMA interface */
  53. struct fastlane_dma_registers {
  54. volatile unsigned char cond_reg; /* DMA status (ro) [0x0000] */
  55. #define ctrl_reg cond_reg /* DMA control (wo) [0x0000] */
  56. unsigned char dmapad1[0x3f];
  57. volatile unsigned char clear_strobe; /* DMA clear (wo) [0x0040] */
  58. };
  59. /* DMA status bits */
  60. #define FASTLANE_DMA_MINT 0x80
  61. #define FASTLANE_DMA_IACT 0x40
  62. #define FASTLANE_DMA_CREQ 0x20
  63. /* DMA control bits */
  64. #define FASTLANE_DMA_FCODE 0xa0
  65. #define FASTLANE_DMA_MASK 0xf3
  66. #define FASTLANE_DMA_LED 0x10 /* HD led control 1 = on */
  67. #define FASTLANE_DMA_WRITE 0x08 /* 1 = write */
  68. #define FASTLANE_DMA_ENABLE 0x04 /* Enable DMA */
  69. #define FASTLANE_DMA_EDI 0x02 /* Enable DMA IRQ ? */
  70. #define FASTLANE_DMA_ESI 0x01 /* Enable SCSI IRQ */
  71. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
  72. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp);
  73. static void dma_dump_state(struct NCR_ESP *esp);
  74. static void dma_init_read(struct NCR_ESP *esp, __u32 addr, int length);
  75. static void dma_init_write(struct NCR_ESP *esp, __u32 vaddr, int length);
  76. static void dma_ints_off(struct NCR_ESP *esp);
  77. static void dma_ints_on(struct NCR_ESP *esp);
  78. static int dma_irq_p(struct NCR_ESP *esp);
  79. static void dma_irq_exit(struct NCR_ESP *esp);
  80. static void dma_led_off(struct NCR_ESP *esp);
  81. static void dma_led_on(struct NCR_ESP *esp);
  82. static int dma_ports_p(struct NCR_ESP *esp);
  83. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
  84. static unsigned char ctrl_data = 0; /* Keep backup of the stuff written
  85. * to ctrl_reg. Always write a copy
  86. * to this register when writing to
  87. * the hardware register!
  88. */
  89. static volatile unsigned char cmd_buffer[16];
  90. /* This is where all commands are put
  91. * before they are transferred to the ESP chip
  92. * via PIO.
  93. */
  94. static inline void dma_clear(struct NCR_ESP *esp)
  95. {
  96. struct fastlane_dma_registers *dregs =
  97. (struct fastlane_dma_registers *) (esp->dregs);
  98. unsigned long *t;
  99. ctrl_data = (ctrl_data & FASTLANE_DMA_MASK);
  100. dregs->ctrl_reg = ctrl_data;
  101. t = (unsigned long *)(esp->edev);
  102. dregs->clear_strobe = 0;
  103. *t = 0 ;
  104. }
  105. /***************************************************************** Detection */
  106. int __init fastlane_esp_detect(Scsi_Host_Template *tpnt)
  107. {
  108. struct NCR_ESP *esp;
  109. struct zorro_dev *z = NULL;
  110. unsigned long address;
  111. if ((z = zorro_find_device(ZORRO_PROD_PHASE5_BLIZZARD_1230_II_FASTLANE_Z3_CYBERSCSI_CYBERSTORM060, z))) {
  112. unsigned long board = z->resource.start;
  113. if (request_mem_region(board+FASTLANE_ESP_ADDR,
  114. sizeof(struct ESP_regs), "NCR53C9x")) {
  115. /* Check if this is really a fastlane controller. The problem
  116. * is that also the cyberstorm and blizzard controllers use
  117. * this ID value. Fortunately only Fastlane maps in Z3 space
  118. */
  119. if (board < 0x1000000) {
  120. goto err_release;
  121. }
  122. esp = esp_allocate(tpnt, (void *)board+FASTLANE_ESP_ADDR);
  123. /* Do command transfer with programmed I/O */
  124. esp->do_pio_cmds = 1;
  125. /* Required functions */
  126. esp->dma_bytes_sent = &dma_bytes_sent;
  127. esp->dma_can_transfer = &dma_can_transfer;
  128. esp->dma_dump_state = &dma_dump_state;
  129. esp->dma_init_read = &dma_init_read;
  130. esp->dma_init_write = &dma_init_write;
  131. esp->dma_ints_off = &dma_ints_off;
  132. esp->dma_ints_on = &dma_ints_on;
  133. esp->dma_irq_p = &dma_irq_p;
  134. esp->dma_ports_p = &dma_ports_p;
  135. esp->dma_setup = &dma_setup;
  136. /* Optional functions */
  137. esp->dma_barrier = 0;
  138. esp->dma_drain = 0;
  139. esp->dma_invalidate = 0;
  140. esp->dma_irq_entry = 0;
  141. esp->dma_irq_exit = &dma_irq_exit;
  142. esp->dma_led_on = &dma_led_on;
  143. esp->dma_led_off = &dma_led_off;
  144. esp->dma_poll = 0;
  145. esp->dma_reset = 0;
  146. /* Initialize the portBits (enable IRQs) */
  147. ctrl_data = (FASTLANE_DMA_FCODE |
  148. #ifndef NODMAIRQ
  149. FASTLANE_DMA_EDI |
  150. #endif
  151. FASTLANE_DMA_ESI);
  152. /* SCSI chip clock */
  153. esp->cfreq = 40000000;
  154. /* Map the physical address space into virtual kernel space */
  155. address = (unsigned long)
  156. z_ioremap(board, z->resource.end-board+1);
  157. if(!address){
  158. printk("Could not remap Fastlane controller memory!");
  159. goto err_unregister;
  160. }
  161. /* The DMA registers on the Fastlane are mapped
  162. * relative to the device (i.e. in the same Zorro
  163. * I/O block).
  164. */
  165. esp->dregs = (void *)(address + FASTLANE_DMA_ADDR);
  166. /* ESP register base */
  167. esp->eregs = (struct ESP_regs *)(address + FASTLANE_ESP_ADDR);
  168. /* Board base */
  169. esp->edev = (void *) address;
  170. /* Set the command buffer */
  171. esp->esp_command = cmd_buffer;
  172. esp->esp_command_dvma = virt_to_bus((void *)cmd_buffer);
  173. esp->irq = IRQ_AMIGA_PORTS;
  174. esp->slot = board+FASTLANE_ESP_ADDR;
  175. if (request_irq(IRQ_AMIGA_PORTS, esp_intr, SA_SHIRQ,
  176. "Fastlane SCSI", esp->ehost)) {
  177. printk(KERN_WARNING "Fastlane: Could not get IRQ%d, aborting.\n", IRQ_AMIGA_PORTS);
  178. goto err_unmap;
  179. }
  180. /* Controller ID */
  181. esp->scsi_id = 7;
  182. /* We don't have a differential SCSI-bus. */
  183. esp->diff = 0;
  184. dma_clear(esp);
  185. esp_initialize(esp);
  186. printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps, esps_in_use);
  187. esps_running = esps_in_use;
  188. return esps_in_use;
  189. }
  190. }
  191. return 0;
  192. err_unmap:
  193. z_iounmap((void *)address);
  194. err_unregister:
  195. scsi_unregister (esp->ehost);
  196. err_release:
  197. release_mem_region(z->resource.start+FASTLANE_ESP_ADDR,
  198. sizeof(struct ESP_regs));
  199. return 0;
  200. }
  201. /************************************************************* DMA Functions */
  202. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
  203. {
  204. /* Since the Fastlane DMA is fully dedicated to the ESP chip,
  205. * the number of bytes sent (to the ESP chip) equals the number
  206. * of bytes in the FIFO - there is no buffering in the DMA controller.
  207. * XXXX Do I read this right? It is from host to ESP, right?
  208. */
  209. return fifo_count;
  210. }
  211. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  212. {
  213. unsigned long sz = sp->SCp.this_residual;
  214. if(sz > 0xfffc)
  215. sz = 0xfffc;
  216. return sz;
  217. }
  218. static void dma_dump_state(struct NCR_ESP *esp)
  219. {
  220. ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
  221. esp->esp_id, ((struct fastlane_dma_registers *)
  222. (esp->dregs))->cond_reg));
  223. ESPLOG(("intreq:<%04x>, intena:<%04x>\n",
  224. custom.intreqr, custom.intenar));
  225. }
  226. static void dma_init_read(struct NCR_ESP *esp, __u32 addr, int length)
  227. {
  228. struct fastlane_dma_registers *dregs =
  229. (struct fastlane_dma_registers *) (esp->dregs);
  230. unsigned long *t;
  231. cache_clear(addr, length);
  232. dma_clear(esp);
  233. t = (unsigned long *)((addr & 0x00ffffff) + esp->edev);
  234. dregs->clear_strobe = 0;
  235. *t = addr;
  236. ctrl_data = (ctrl_data & FASTLANE_DMA_MASK) | FASTLANE_DMA_ENABLE;
  237. dregs->ctrl_reg = ctrl_data;
  238. }
  239. static void dma_init_write(struct NCR_ESP *esp, __u32 addr, int length)
  240. {
  241. struct fastlane_dma_registers *dregs =
  242. (struct fastlane_dma_registers *) (esp->dregs);
  243. unsigned long *t;
  244. cache_push(addr, length);
  245. dma_clear(esp);
  246. t = (unsigned long *)((addr & 0x00ffffff) + (esp->edev));
  247. dregs->clear_strobe = 0;
  248. *t = addr;
  249. ctrl_data = ((ctrl_data & FASTLANE_DMA_MASK) |
  250. FASTLANE_DMA_ENABLE |
  251. FASTLANE_DMA_WRITE);
  252. dregs->ctrl_reg = ctrl_data;
  253. }
  254. static void dma_ints_off(struct NCR_ESP *esp)
  255. {
  256. disable_irq(esp->irq);
  257. }
  258. static void dma_ints_on(struct NCR_ESP *esp)
  259. {
  260. enable_irq(esp->irq);
  261. }
  262. static void dma_irq_exit(struct NCR_ESP *esp)
  263. {
  264. struct fastlane_dma_registers *dregs =
  265. (struct fastlane_dma_registers *) (esp->dregs);
  266. dregs->ctrl_reg = ctrl_data & ~(FASTLANE_DMA_EDI|FASTLANE_DMA_ESI);
  267. #ifdef __mc68000__
  268. nop();
  269. #endif
  270. dregs->ctrl_reg = ctrl_data;
  271. }
  272. static int dma_irq_p(struct NCR_ESP *esp)
  273. {
  274. struct fastlane_dma_registers *dregs =
  275. (struct fastlane_dma_registers *) (esp->dregs);
  276. unsigned char dma_status;
  277. dma_status = dregs->cond_reg;
  278. if(dma_status & FASTLANE_DMA_IACT)
  279. return 0; /* not our IRQ */
  280. /* Return non-zero if ESP requested IRQ */
  281. return (
  282. #ifndef NODMAIRQ
  283. (dma_status & FASTLANE_DMA_CREQ) &&
  284. #endif
  285. (!(dma_status & FASTLANE_DMA_MINT)) &&
  286. (esp_read(((struct ESP_regs *) (esp->eregs))->esp_status) & ESP_STAT_INTR));
  287. }
  288. static void dma_led_off(struct NCR_ESP *esp)
  289. {
  290. ctrl_data &= ~FASTLANE_DMA_LED;
  291. ((struct fastlane_dma_registers *)(esp->dregs))->ctrl_reg = ctrl_data;
  292. }
  293. static void dma_led_on(struct NCR_ESP *esp)
  294. {
  295. ctrl_data |= FASTLANE_DMA_LED;
  296. ((struct fastlane_dma_registers *)(esp->dregs))->ctrl_reg = ctrl_data;
  297. }
  298. static int dma_ports_p(struct NCR_ESP *esp)
  299. {
  300. return ((custom.intenar) & IF_PORTS);
  301. }
  302. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
  303. {
  304. /* On the Sparc, DMA_ST_WRITE means "move data from device to memory"
  305. * so when (write) is true, it actually means READ!
  306. */
  307. if(write){
  308. dma_init_read(esp, addr, count);
  309. } else {
  310. dma_init_write(esp, addr, count);
  311. }
  312. }
  313. #define HOSTS_C
  314. int fastlane_esp_release(struct Scsi_Host *instance)
  315. {
  316. #ifdef MODULE
  317. unsigned long address = (unsigned long)((struct NCR_ESP *)instance->hostdata)->edev;
  318. esp_deallocate((struct NCR_ESP *)instance->hostdata);
  319. esp_release();
  320. release_mem_region(address, sizeof(struct ESP_regs));
  321. free_irq(IRQ_AMIGA_PORTS, esp_intr);
  322. #endif
  323. return 1;
  324. }
  325. static Scsi_Host_Template driver_template = {
  326. .proc_name = "esp-fastlane",
  327. .proc_info = esp_proc_info,
  328. .name = "Fastlane SCSI",
  329. .detect = fastlane_esp_detect,
  330. .slave_alloc = esp_slave_alloc,
  331. .slave_destroy = esp_slave_destroy,
  332. .release = fastlane_esp_release,
  333. .queuecommand = esp_queue,
  334. .eh_abort_handler = esp_abort,
  335. .eh_bus_reset_handler = esp_reset,
  336. .can_queue = 7,
  337. .this_id = 7,
  338. .sg_tablesize = SG_ALL,
  339. .cmd_per_lun = 1,
  340. .use_clustering = ENABLE_CLUSTERING
  341. };
  342. #include "scsi_module.c"
  343. MODULE_LICENSE("GPL");