gvp11.c 12 KB

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