a2091.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "a2091.h"
  20. struct a2091_hostdata {
  21. struct WD33C93_hostdata wh;
  22. struct a2091_scsiregs *regs;
  23. struct device *dev;
  24. };
  25. #define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
  26. static irqreturn_t a2091_intr(int irq, void *data)
  27. {
  28. struct Scsi_Host *instance = data;
  29. struct a2091_hostdata *hdata = shost_priv(instance);
  30. unsigned int status = hdata->regs->ISTR;
  31. unsigned long flags;
  32. if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
  33. return IRQ_NONE;
  34. spin_lock_irqsave(instance->host_lock, flags);
  35. wd33c93_intr(instance);
  36. spin_unlock_irqrestore(instance->host_lock, flags);
  37. return IRQ_HANDLED;
  38. }
  39. static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
  40. {
  41. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
  42. unsigned long len = scsi_pointer->this_residual;
  43. struct Scsi_Host *instance = cmd->device->host;
  44. struct a2091_hostdata *hdata = shost_priv(instance);
  45. struct WD33C93_hostdata *wh = &hdata->wh;
  46. struct a2091_scsiregs *regs = hdata->regs;
  47. unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
  48. dma_addr_t addr;
  49. addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
  50. len, DMA_DIR(dir_in));
  51. if (dma_mapping_error(hdata->dev, addr)) {
  52. dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
  53. scsi_pointer->ptr);
  54. return 1;
  55. }
  56. scsi_pointer->dma_handle = addr;
  57. /* don't allow DMA if the physical address is bad */
  58. if (addr & A2091_XFER_MASK) {
  59. /* drop useless mapping */
  60. dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
  61. scsi_pointer->this_residual,
  62. DMA_DIR(dir_in));
  63. scsi_pointer->dma_handle = (dma_addr_t) NULL;
  64. wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
  65. wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
  66. GFP_KERNEL);
  67. /* can't allocate memory; use PIO */
  68. if (!wh->dma_bounce_buffer) {
  69. wh->dma_bounce_len = 0;
  70. return 1;
  71. }
  72. if (!dir_in) {
  73. /* copy to bounce buffer for a write */
  74. memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
  75. scsi_pointer->this_residual);
  76. }
  77. /* will flush/invalidate cache for us */
  78. addr = dma_map_single(hdata->dev, wh->dma_bounce_buffer,
  79. wh->dma_bounce_len, DMA_DIR(dir_in));
  80. /* can't map buffer; use PIO */
  81. if (dma_mapping_error(hdata->dev, addr)) {
  82. dev_warn(hdata->dev, "cannot map bounce buffer %p\n",
  83. wh->dma_bounce_buffer);
  84. return 1;
  85. }
  86. /* the bounce buffer may not be in the first 16M of physmem */
  87. if (addr & A2091_XFER_MASK) {
  88. /* we could use chipmem... maybe later */
  89. kfree(wh->dma_bounce_buffer);
  90. wh->dma_bounce_buffer = NULL;
  91. wh->dma_bounce_len = 0;
  92. return 1;
  93. }
  94. scsi_pointer->dma_handle = addr;
  95. }
  96. /* setup dma direction */
  97. if (!dir_in)
  98. cntr |= CNTR_DDIR;
  99. /* remember direction */
  100. wh->dma_dir = dir_in;
  101. regs->CNTR = cntr;
  102. /* setup DMA *physical* address */
  103. regs->ACR = addr;
  104. /* no more cache flush here - dma_map_single() takes care */
  105. /* start DMA */
  106. regs->ST_DMA = 1;
  107. /* return success */
  108. return 0;
  109. }
  110. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  111. int status)
  112. {
  113. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
  114. struct a2091_hostdata *hdata = shost_priv(instance);
  115. struct WD33C93_hostdata *wh = &hdata->wh;
  116. struct a2091_scsiregs *regs = hdata->regs;
  117. /* disable SCSI interrupts */
  118. unsigned short cntr = CNTR_PDMD;
  119. if (!wh->dma_dir)
  120. cntr |= CNTR_DDIR;
  121. /* disable SCSI interrupts */
  122. regs->CNTR = cntr;
  123. /* flush if we were reading */
  124. if (wh->dma_dir) {
  125. regs->FLUSH = 1;
  126. while (!(regs->ISTR & ISTR_FE_FLG))
  127. ;
  128. }
  129. /* clear a possible interrupt */
  130. regs->CINT = 1;
  131. /* stop DMA */
  132. regs->SP_DMA = 1;
  133. /* restore the CONTROL bits (minus the direction flag) */
  134. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  135. dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
  136. scsi_pointer->this_residual,
  137. DMA_DIR(wh->dma_dir));
  138. /* copy from a bounce buffer, if necessary */
  139. if (status && wh->dma_bounce_buffer) {
  140. if (wh->dma_dir)
  141. memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
  142. scsi_pointer->this_residual);
  143. kfree(wh->dma_bounce_buffer);
  144. wh->dma_bounce_buffer = NULL;
  145. wh->dma_bounce_len = 0;
  146. }
  147. }
  148. static struct scsi_host_template a2091_scsi_template = {
  149. .module = THIS_MODULE,
  150. .name = "Commodore A2091/A590 SCSI",
  151. .show_info = wd33c93_show_info,
  152. .write_info = wd33c93_write_info,
  153. .proc_name = "A2901",
  154. .queuecommand = wd33c93_queuecommand,
  155. .eh_abort_handler = wd33c93_abort,
  156. .eh_host_reset_handler = wd33c93_host_reset,
  157. .can_queue = CAN_QUEUE,
  158. .this_id = 7,
  159. .sg_tablesize = SG_ALL,
  160. .cmd_per_lun = CMD_PER_LUN,
  161. .dma_boundary = PAGE_SIZE - 1,
  162. .cmd_size = sizeof(struct scsi_pointer),
  163. };
  164. static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
  165. {
  166. struct Scsi_Host *instance;
  167. int error;
  168. struct a2091_scsiregs *regs;
  169. wd33c93_regs wdregs;
  170. struct a2091_hostdata *hdata;
  171. if (dma_set_mask_and_coherent(&z->dev, DMA_BIT_MASK(24))) {
  172. dev_warn(&z->dev, "cannot use 24 bit DMA\n");
  173. return -ENODEV;
  174. }
  175. if (!request_mem_region(z->resource.start, 256, "wd33c93"))
  176. return -EBUSY;
  177. instance = scsi_host_alloc(&a2091_scsi_template,
  178. sizeof(struct a2091_hostdata));
  179. if (!instance) {
  180. error = -ENOMEM;
  181. goto fail_alloc;
  182. }
  183. instance->irq = IRQ_AMIGA_PORTS;
  184. instance->unique_id = z->slotaddr;
  185. regs = ZTWO_VADDR(z->resource.start);
  186. regs->DAWR = DAWR_A2091;
  187. wdregs.SASR = &regs->SASR;
  188. wdregs.SCMD = &regs->SCMD;
  189. hdata = shost_priv(instance);
  190. hdata->dev = &z->dev;
  191. hdata->wh.no_sync = 0xff;
  192. hdata->wh.fast = 0;
  193. hdata->wh.dma_mode = CTRL_DMA;
  194. hdata->regs = regs;
  195. wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
  196. error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
  197. "A2091 SCSI", instance);
  198. if (error)
  199. goto fail_irq;
  200. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  201. error = scsi_add_host(instance, NULL);
  202. if (error)
  203. goto fail_host;
  204. zorro_set_drvdata(z, instance);
  205. scsi_scan_host(instance);
  206. return 0;
  207. fail_host:
  208. free_irq(IRQ_AMIGA_PORTS, instance);
  209. fail_irq:
  210. scsi_host_put(instance);
  211. fail_alloc:
  212. release_mem_region(z->resource.start, 256);
  213. return error;
  214. }
  215. static void a2091_remove(struct zorro_dev *z)
  216. {
  217. struct Scsi_Host *instance = zorro_get_drvdata(z);
  218. struct a2091_hostdata *hdata = shost_priv(instance);
  219. hdata->regs->CNTR = 0;
  220. scsi_remove_host(instance);
  221. free_irq(IRQ_AMIGA_PORTS, instance);
  222. scsi_host_put(instance);
  223. release_mem_region(z->resource.start, 256);
  224. }
  225. static struct zorro_device_id a2091_zorro_tbl[] = {
  226. { ZORRO_PROD_CBM_A590_A2091_1 },
  227. { ZORRO_PROD_CBM_A590_A2091_2 },
  228. { 0 }
  229. };
  230. MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
  231. static struct zorro_driver a2091_driver = {
  232. .name = "a2091",
  233. .id_table = a2091_zorro_tbl,
  234. .probe = a2091_probe,
  235. .remove = a2091_remove,
  236. };
  237. static int __init a2091_init(void)
  238. {
  239. return zorro_register_driver(&a2091_driver);
  240. }
  241. module_init(a2091_init);
  242. static void __exit a2091_exit(void)
  243. {
  244. zorro_unregister_driver(&a2091_driver);
  245. }
  246. module_exit(a2091_exit);
  247. MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
  248. MODULE_LICENSE("GPL");