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