gvp11.c 12 KB

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