gvp11.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/init.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/zorro.h>
  9. #include <linux/module.h>
  10. #include <asm/page.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 struct scsi_host_template gvp11_scsi_template = {
  140. .module = THIS_MODULE,
  141. .name = "GVP Series II SCSI",
  142. .show_info = wd33c93_show_info,
  143. .write_info = wd33c93_write_info,
  144. .proc_name = "GVP11",
  145. .queuecommand = wd33c93_queuecommand,
  146. .eh_abort_handler = wd33c93_abort,
  147. .eh_host_reset_handler = wd33c93_host_reset,
  148. .can_queue = CAN_QUEUE,
  149. .this_id = 7,
  150. .sg_tablesize = SG_ALL,
  151. .cmd_per_lun = CMD_PER_LUN,
  152. .dma_boundary = PAGE_SIZE - 1,
  153. };
  154. static int check_wd33c93(struct gvp11_scsiregs *regs)
  155. {
  156. #ifdef CHECK_WD33C93
  157. volatile unsigned char *sasr_3393, *scmd_3393;
  158. unsigned char save_sasr;
  159. unsigned char q, qq;
  160. /*
  161. * These darn GVP boards are a problem - it can be tough to tell
  162. * whether or not they include a SCSI controller. This is the
  163. * ultimate Yet-Another-GVP-Detection-Hack in that it actually
  164. * probes for a WD33c93 chip: If we find one, it's extremely
  165. * likely that this card supports SCSI, regardless of Product_
  166. * Code, Board_Size, etc.
  167. */
  168. /* Get pointers to the presumed register locations and save contents */
  169. sasr_3393 = &regs->SASR;
  170. scmd_3393 = &regs->SCMD;
  171. save_sasr = *sasr_3393;
  172. /* First test the AuxStatus Reg */
  173. q = *sasr_3393; /* read it */
  174. if (q & 0x08) /* bit 3 should always be clear */
  175. return -ENODEV;
  176. *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
  177. if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
  178. *sasr_3393 = save_sasr; /* Oops - restore this byte */
  179. return -ENODEV;
  180. }
  181. if (*sasr_3393 != q) { /* should still read the same */
  182. *sasr_3393 = save_sasr; /* Oops - restore this byte */
  183. return -ENODEV;
  184. }
  185. if (*scmd_3393 != q) /* and so should the image at 0x1f */
  186. return -ENODEV;
  187. /*
  188. * Ok, we probably have a wd33c93, but let's check a few other places
  189. * for good measure. Make sure that this works for both 'A and 'B
  190. * chip versions.
  191. */
  192. *sasr_3393 = WD_SCSI_STATUS;
  193. q = *scmd_3393;
  194. *sasr_3393 = WD_SCSI_STATUS;
  195. *scmd_3393 = ~q;
  196. *sasr_3393 = WD_SCSI_STATUS;
  197. qq = *scmd_3393;
  198. *sasr_3393 = WD_SCSI_STATUS;
  199. *scmd_3393 = q;
  200. if (qq != q) /* should be read only */
  201. return -ENODEV;
  202. *sasr_3393 = 0x1e; /* this register is unimplemented */
  203. q = *scmd_3393;
  204. *sasr_3393 = 0x1e;
  205. *scmd_3393 = ~q;
  206. *sasr_3393 = 0x1e;
  207. qq = *scmd_3393;
  208. *sasr_3393 = 0x1e;
  209. *scmd_3393 = q;
  210. if (qq != q || qq != 0xff) /* should be read only, all 1's */
  211. return -ENODEV;
  212. *sasr_3393 = WD_TIMEOUT_PERIOD;
  213. q = *scmd_3393;
  214. *sasr_3393 = WD_TIMEOUT_PERIOD;
  215. *scmd_3393 = ~q;
  216. *sasr_3393 = WD_TIMEOUT_PERIOD;
  217. qq = *scmd_3393;
  218. *sasr_3393 = WD_TIMEOUT_PERIOD;
  219. *scmd_3393 = q;
  220. if (qq != (~q & 0xff)) /* should be read/write */
  221. return -ENODEV;
  222. #endif /* CHECK_WD33C93 */
  223. return 0;
  224. }
  225. static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
  226. {
  227. struct Scsi_Host *instance;
  228. unsigned long address;
  229. int error;
  230. unsigned int epc;
  231. unsigned int default_dma_xfer_mask;
  232. struct gvp11_hostdata *hdata;
  233. struct gvp11_scsiregs *regs;
  234. wd33c93_regs wdregs;
  235. default_dma_xfer_mask = ent->driver_data;
  236. /*
  237. * Rumors state that some GVP ram boards use the same product
  238. * code as the SCSI controllers. Therefore if the board-size
  239. * is not 64KB we assume it is a ram board and bail out.
  240. */
  241. if (zorro_resource_len(z) != 0x10000)
  242. return -ENODEV;
  243. address = z->resource.start;
  244. if (!request_mem_region(address, 256, "wd33c93"))
  245. return -EBUSY;
  246. regs = ZTWO_VADDR(address);
  247. error = check_wd33c93(regs);
  248. if (error)
  249. goto fail_check_or_alloc;
  250. instance = scsi_host_alloc(&gvp11_scsi_template,
  251. sizeof(struct gvp11_hostdata));
  252. if (!instance) {
  253. error = -ENOMEM;
  254. goto fail_check_or_alloc;
  255. }
  256. instance->irq = IRQ_AMIGA_PORTS;
  257. instance->unique_id = z->slotaddr;
  258. regs->secret2 = 1;
  259. regs->secret1 = 0;
  260. regs->secret3 = 15;
  261. while (regs->CNTR & GVP11_DMAC_BUSY)
  262. ;
  263. regs->CNTR = 0;
  264. regs->BANK = 0;
  265. wdregs.SASR = &regs->SASR;
  266. wdregs.SCMD = &regs->SCMD;
  267. hdata = shost_priv(instance);
  268. if (gvp11_xfer_mask)
  269. hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
  270. else
  271. hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
  272. hdata->wh.no_sync = 0xff;
  273. hdata->wh.fast = 0;
  274. hdata->wh.dma_mode = CTRL_DMA;
  275. hdata->regs = regs;
  276. /*
  277. * Check for 14MHz SCSI clock
  278. */
  279. epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
  280. wd33c93_init(instance, wdregs, dma_setup, dma_stop,
  281. (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
  282. : WD33C93_FS_12_15);
  283. error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
  284. "GVP11 SCSI", instance);
  285. if (error)
  286. goto fail_irq;
  287. regs->CNTR = GVP11_DMAC_INT_ENABLE;
  288. error = scsi_add_host(instance, NULL);
  289. if (error)
  290. goto fail_host;
  291. zorro_set_drvdata(z, instance);
  292. scsi_scan_host(instance);
  293. return 0;
  294. fail_host:
  295. free_irq(IRQ_AMIGA_PORTS, instance);
  296. fail_irq:
  297. scsi_host_put(instance);
  298. fail_check_or_alloc:
  299. release_mem_region(address, 256);
  300. return error;
  301. }
  302. static void gvp11_remove(struct zorro_dev *z)
  303. {
  304. struct Scsi_Host *instance = zorro_get_drvdata(z);
  305. struct gvp11_hostdata *hdata = shost_priv(instance);
  306. hdata->regs->CNTR = 0;
  307. scsi_remove_host(instance);
  308. free_irq(IRQ_AMIGA_PORTS, instance);
  309. scsi_host_put(instance);
  310. release_mem_region(z->resource.start, 256);
  311. }
  312. /*
  313. * This should (hopefully) be the correct way to identify
  314. * all the different GVP SCSI controllers (except for the
  315. * SERIES I though).
  316. */
  317. static struct zorro_device_id gvp11_zorro_tbl[] = {
  318. { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
  319. { ZORRO_PROD_GVP_SERIES_II, ~0x00ffffff },
  320. { ZORRO_PROD_GVP_GFORCE_030_SCSI, ~0x01ffffff },
  321. { ZORRO_PROD_GVP_A530_SCSI, ~0x01ffffff },
  322. { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
  323. { ZORRO_PROD_GVP_A1291, ~0x07ffffff },
  324. { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
  325. { 0 }
  326. };
  327. MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
  328. static struct zorro_driver gvp11_driver = {
  329. .name = "gvp11",
  330. .id_table = gvp11_zorro_tbl,
  331. .probe = gvp11_probe,
  332. .remove = gvp11_remove,
  333. };
  334. static int __init gvp11_init(void)
  335. {
  336. return zorro_register_driver(&gvp11_driver);
  337. }
  338. module_init(gvp11_init);
  339. static void __exit gvp11_exit(void)
  340. {
  341. zorro_unregister_driver(&gvp11_driver);
  342. }
  343. module_exit(gvp11_exit);
  344. MODULE_DESCRIPTION("GVP Series II SCSI");
  345. MODULE_LICENSE("GPL");