gvp11.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include <linux/types.h>
  2. #include <linux/init.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/mm.h>
  5. #include <linux/slab.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/zorro.h>
  8. #include <linux/module.h>
  9. #include <asm/page.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/amigaints.h>
  12. #include <asm/amigahw.h>
  13. #include "scsi.h"
  14. #include "wd33c93.h"
  15. #include "gvp11.h"
  16. #define CHECK_WD33C93
  17. struct gvp11_hostdata {
  18. struct WD33C93_hostdata wh;
  19. struct gvp11_scsiregs *regs;
  20. };
  21. static irqreturn_t gvp11_intr(int irq, void *data)
  22. {
  23. struct Scsi_Host *instance = data;
  24. struct gvp11_hostdata *hdata = shost_priv(instance);
  25. unsigned int status = hdata->regs->CNTR;
  26. unsigned long flags;
  27. if (!(status & GVP11_DMAC_INT_PENDING))
  28. return IRQ_NONE;
  29. spin_lock_irqsave(instance->host_lock, flags);
  30. wd33c93_intr(instance);
  31. spin_unlock_irqrestore(instance->host_lock, flags);
  32. return IRQ_HANDLED;
  33. }
  34. static int gvp11_xfer_mask = 0;
  35. void gvp11_setup(char *str, int *ints)
  36. {
  37. gvp11_xfer_mask = ints[1];
  38. }
  39. static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
  40. {
  41. struct Scsi_Host *instance = cmd->device->host;
  42. struct gvp11_hostdata *hdata = shost_priv(instance);
  43. struct WD33C93_hostdata *wh = &hdata->wh;
  44. struct gvp11_scsiregs *regs = hdata->regs;
  45. unsigned short cntr = GVP11_DMAC_INT_ENABLE;
  46. unsigned long addr = virt_to_bus(cmd->SCp.ptr);
  47. int bank_mask;
  48. static int scsi_alloc_out_of_range = 0;
  49. /* use bounce buffer if the physical address is bad */
  50. if (addr & wh->dma_xfer_mask) {
  51. wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
  52. if (!scsi_alloc_out_of_range) {
  53. wh->dma_bounce_buffer =
  54. kmalloc(wh->dma_bounce_len, GFP_KERNEL);
  55. wh->dma_buffer_pool = BUF_SCSI_ALLOCED;
  56. }
  57. if (scsi_alloc_out_of_range ||
  58. !wh->dma_bounce_buffer) {
  59. wh->dma_bounce_buffer =
  60. amiga_chip_alloc(wh->dma_bounce_len,
  61. "GVP II SCSI Bounce Buffer");
  62. if (!wh->dma_bounce_buffer) {
  63. wh->dma_bounce_len = 0;
  64. return 1;
  65. }
  66. wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
  67. }
  68. /* check if the address of the bounce buffer is OK */
  69. addr = virt_to_bus(wh->dma_bounce_buffer);
  70. if (addr & wh->dma_xfer_mask) {
  71. /* fall back to Chip RAM if address out of range */
  72. if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
  73. kfree(wh->dma_bounce_buffer);
  74. scsi_alloc_out_of_range = 1;
  75. } else {
  76. amiga_chip_free(wh->dma_bounce_buffer);
  77. }
  78. wh->dma_bounce_buffer =
  79. amiga_chip_alloc(wh->dma_bounce_len,
  80. "GVP II SCSI Bounce Buffer");
  81. if (!wh->dma_bounce_buffer) {
  82. wh->dma_bounce_len = 0;
  83. return 1;
  84. }
  85. addr = virt_to_bus(wh->dma_bounce_buffer);
  86. wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
  87. }
  88. if (!dir_in) {
  89. /* copy to bounce buffer for a write */
  90. memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
  91. cmd->SCp.this_residual);
  92. }
  93. }
  94. /* setup dma direction */
  95. if (!dir_in)
  96. cntr |= GVP11_DMAC_DIR_WRITE;
  97. wh->dma_dir = dir_in;
  98. regs->CNTR = cntr;
  99. /* setup DMA *physical* address */
  100. regs->ACR = addr;
  101. if (dir_in) {
  102. /* invalidate any cache */
  103. cache_clear(addr, cmd->SCp.this_residual);
  104. } else {
  105. /* push any dirty cache */
  106. cache_push(addr, cmd->SCp.this_residual);
  107. }
  108. bank_mask = (~wh->dma_xfer_mask >> 18) & 0x01c0;
  109. if (bank_mask)
  110. regs->BANK = bank_mask & (addr >> 18);
  111. /* start DMA */
  112. regs->ST_DMA = 1;
  113. /* return success */
  114. return 0;
  115. }
  116. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  117. int status)
  118. {
  119. struct gvp11_hostdata *hdata = shost_priv(instance);
  120. struct WD33C93_hostdata *wh = &hdata->wh;
  121. struct gvp11_scsiregs *regs = hdata->regs;
  122. /* stop DMA */
  123. regs->SP_DMA = 1;
  124. /* remove write bit from CONTROL bits */
  125. regs->CNTR = GVP11_DMAC_INT_ENABLE;
  126. /* copy from a bounce buffer, if necessary */
  127. if (status && wh->dma_bounce_buffer) {
  128. if (wh->dma_dir && SCpnt)
  129. memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
  130. SCpnt->SCp.this_residual);
  131. if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
  132. kfree(wh->dma_bounce_buffer);
  133. else
  134. amiga_chip_free(wh->dma_bounce_buffer);
  135. wh->dma_bounce_buffer = NULL;
  136. wh->dma_bounce_len = 0;
  137. }
  138. }
  139. static int gvp11_bus_reset(struct scsi_cmnd *cmd)
  140. {
  141. struct Scsi_Host *instance = cmd->device->host;
  142. /* FIXME perform bus-specific reset */
  143. /* FIXME 2: shouldn't we no-op this function (return
  144. FAILED), and fall back to host reset function,
  145. wd33c93_host_reset ? */
  146. spin_lock_irq(instance->host_lock);
  147. wd33c93_host_reset(cmd);
  148. spin_unlock_irq(instance->host_lock);
  149. return SUCCESS;
  150. }
  151. static struct scsi_host_template gvp11_scsi_template = {
  152. .module = THIS_MODULE,
  153. .name = "GVP Series II SCSI",
  154. .show_info = wd33c93_show_info,
  155. .write_info = wd33c93_write_info,
  156. .proc_name = "GVP11",
  157. .queuecommand = wd33c93_queuecommand,
  158. .eh_abort_handler = wd33c93_abort,
  159. .eh_bus_reset_handler = gvp11_bus_reset,
  160. .eh_host_reset_handler = wd33c93_host_reset,
  161. .can_queue = CAN_QUEUE,
  162. .this_id = 7,
  163. .sg_tablesize = SG_ALL,
  164. .cmd_per_lun = CMD_PER_LUN,
  165. .use_clustering = DISABLE_CLUSTERING
  166. };
  167. static int check_wd33c93(struct gvp11_scsiregs *regs)
  168. {
  169. #ifdef CHECK_WD33C93
  170. volatile unsigned char *sasr_3393, *scmd_3393;
  171. unsigned char save_sasr;
  172. unsigned char q, qq;
  173. /*
  174. * These darn GVP boards are a problem - it can be tough to tell
  175. * whether or not they include a SCSI controller. This is the
  176. * ultimate Yet-Another-GVP-Detection-Hack in that it actually
  177. * probes for a WD33c93 chip: If we find one, it's extremely
  178. * likely that this card supports SCSI, regardless of Product_
  179. * Code, Board_Size, etc.
  180. */
  181. /* Get pointers to the presumed register locations and save contents */
  182. sasr_3393 = &regs->SASR;
  183. scmd_3393 = &regs->SCMD;
  184. save_sasr = *sasr_3393;
  185. /* First test the AuxStatus Reg */
  186. q = *sasr_3393; /* read it */
  187. if (q & 0x08) /* bit 3 should always be clear */
  188. return -ENODEV;
  189. *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
  190. if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
  191. *sasr_3393 = save_sasr; /* Oops - restore this byte */
  192. return -ENODEV;
  193. }
  194. if (*sasr_3393 != q) { /* should still read the same */
  195. *sasr_3393 = save_sasr; /* Oops - restore this byte */
  196. return -ENODEV;
  197. }
  198. if (*scmd_3393 != q) /* and so should the image at 0x1f */
  199. return -ENODEV;
  200. /*
  201. * Ok, we probably have a wd33c93, but let's check a few other places
  202. * for good measure. Make sure that this works for both 'A and 'B
  203. * chip versions.
  204. */
  205. *sasr_3393 = WD_SCSI_STATUS;
  206. q = *scmd_3393;
  207. *sasr_3393 = WD_SCSI_STATUS;
  208. *scmd_3393 = ~q;
  209. *sasr_3393 = WD_SCSI_STATUS;
  210. qq = *scmd_3393;
  211. *sasr_3393 = WD_SCSI_STATUS;
  212. *scmd_3393 = q;
  213. if (qq != q) /* should be read only */
  214. return -ENODEV;
  215. *sasr_3393 = 0x1e; /* this register is unimplemented */
  216. q = *scmd_3393;
  217. *sasr_3393 = 0x1e;
  218. *scmd_3393 = ~q;
  219. *sasr_3393 = 0x1e;
  220. qq = *scmd_3393;
  221. *sasr_3393 = 0x1e;
  222. *scmd_3393 = q;
  223. if (qq != q || qq != 0xff) /* should be read only, all 1's */
  224. return -ENODEV;
  225. *sasr_3393 = WD_TIMEOUT_PERIOD;
  226. q = *scmd_3393;
  227. *sasr_3393 = WD_TIMEOUT_PERIOD;
  228. *scmd_3393 = ~q;
  229. *sasr_3393 = WD_TIMEOUT_PERIOD;
  230. qq = *scmd_3393;
  231. *sasr_3393 = WD_TIMEOUT_PERIOD;
  232. *scmd_3393 = q;
  233. if (qq != (~q & 0xff)) /* should be read/write */
  234. return -ENODEV;
  235. #endif /* CHECK_WD33C93 */
  236. return 0;
  237. }
  238. static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
  239. {
  240. struct Scsi_Host *instance;
  241. unsigned long address;
  242. int error;
  243. unsigned int epc;
  244. unsigned int default_dma_xfer_mask;
  245. struct gvp11_hostdata *hdata;
  246. struct gvp11_scsiregs *regs;
  247. wd33c93_regs wdregs;
  248. default_dma_xfer_mask = ent->driver_data;
  249. /*
  250. * Rumors state that some GVP ram boards use the same product
  251. * code as the SCSI controllers. Therefore if the board-size
  252. * is not 64KB we assume it is a ram board and bail out.
  253. */
  254. if (zorro_resource_len(z) != 0x10000)
  255. return -ENODEV;
  256. address = z->resource.start;
  257. if (!request_mem_region(address, 256, "wd33c93"))
  258. return -EBUSY;
  259. regs = ZTWO_VADDR(address);
  260. error = check_wd33c93(regs);
  261. if (error)
  262. goto fail_check_or_alloc;
  263. instance = scsi_host_alloc(&gvp11_scsi_template,
  264. sizeof(struct gvp11_hostdata));
  265. if (!instance) {
  266. error = -ENOMEM;
  267. goto fail_check_or_alloc;
  268. }
  269. instance->irq = IRQ_AMIGA_PORTS;
  270. instance->unique_id = z->slotaddr;
  271. regs->secret2 = 1;
  272. regs->secret1 = 0;
  273. regs->secret3 = 15;
  274. while (regs->CNTR & GVP11_DMAC_BUSY)
  275. ;
  276. regs->CNTR = 0;
  277. regs->BANK = 0;
  278. wdregs.SASR = &regs->SASR;
  279. wdregs.SCMD = &regs->SCMD;
  280. hdata = shost_priv(instance);
  281. if (gvp11_xfer_mask)
  282. hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
  283. else
  284. hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
  285. hdata->wh.no_sync = 0xff;
  286. hdata->wh.fast = 0;
  287. hdata->wh.dma_mode = CTRL_DMA;
  288. hdata->regs = regs;
  289. /*
  290. * Check for 14MHz SCSI clock
  291. */
  292. epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
  293. wd33c93_init(instance, wdregs, dma_setup, dma_stop,
  294. (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
  295. : WD33C93_FS_12_15);
  296. error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
  297. "GVP11 SCSI", instance);
  298. if (error)
  299. goto fail_irq;
  300. regs->CNTR = GVP11_DMAC_INT_ENABLE;
  301. error = scsi_add_host(instance, NULL);
  302. if (error)
  303. goto fail_host;
  304. zorro_set_drvdata(z, instance);
  305. scsi_scan_host(instance);
  306. return 0;
  307. fail_host:
  308. free_irq(IRQ_AMIGA_PORTS, instance);
  309. fail_irq:
  310. scsi_host_put(instance);
  311. fail_check_or_alloc:
  312. release_mem_region(address, 256);
  313. return error;
  314. }
  315. static void gvp11_remove(struct zorro_dev *z)
  316. {
  317. struct Scsi_Host *instance = zorro_get_drvdata(z);
  318. struct gvp11_hostdata *hdata = shost_priv(instance);
  319. hdata->regs->CNTR = 0;
  320. scsi_remove_host(instance);
  321. free_irq(IRQ_AMIGA_PORTS, instance);
  322. scsi_host_put(instance);
  323. release_mem_region(z->resource.start, 256);
  324. }
  325. /*
  326. * This should (hopefully) be the correct way to identify
  327. * all the different GVP SCSI controllers (except for the
  328. * SERIES I though).
  329. */
  330. static struct zorro_device_id gvp11_zorro_tbl[] = {
  331. { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
  332. { ZORRO_PROD_GVP_SERIES_II, ~0x00ffffff },
  333. { ZORRO_PROD_GVP_GFORCE_030_SCSI, ~0x01ffffff },
  334. { ZORRO_PROD_GVP_A530_SCSI, ~0x01ffffff },
  335. { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
  336. { ZORRO_PROD_GVP_A1291, ~0x07ffffff },
  337. { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
  338. { 0 }
  339. };
  340. MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
  341. static struct zorro_driver gvp11_driver = {
  342. .name = "gvp11",
  343. .id_table = gvp11_zorro_tbl,
  344. .probe = gvp11_probe,
  345. .remove = gvp11_remove,
  346. };
  347. static int __init gvp11_init(void)
  348. {
  349. return zorro_register_driver(&gvp11_driver);
  350. }
  351. module_init(gvp11_init);
  352. static void __exit gvp11_exit(void)
  353. {
  354. zorro_unregister_driver(&gvp11_driver);
  355. }
  356. module_exit(gvp11_exit);
  357. MODULE_DESCRIPTION("GVP Series II SCSI");
  358. MODULE_LICENSE("GPL");