a2091.c 6.1 KB

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