a3000.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <linux/types.h>
  2. #include <linux/mm.h>
  3. #include <linux/ioport.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/platform_device.h>
  9. #include <asm/page.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/amigaints.h>
  12. #include <asm/amigahw.h>
  13. #include "scsi.h"
  14. #include "wd33c93.h"
  15. #include "a3000.h"
  16. struct a3000_hostdata {
  17. struct WD33C93_hostdata wh;
  18. struct a3000_scsiregs *regs;
  19. };
  20. static irqreturn_t a3000_intr(int irq, void *data)
  21. {
  22. struct Scsi_Host *instance = data;
  23. struct a3000_hostdata *hdata = shost_priv(instance);
  24. unsigned int status = hdata->regs->ISTR;
  25. unsigned long flags;
  26. if (!(status & ISTR_INT_P))
  27. return IRQ_NONE;
  28. if (status & ISTR_INTS) {
  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. pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
  35. return IRQ_NONE;
  36. }
  37. static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
  38. {
  39. struct Scsi_Host *instance = cmd->device->host;
  40. struct a3000_hostdata *hdata = shost_priv(instance);
  41. struct WD33C93_hostdata *wh = &hdata->wh;
  42. struct a3000_scsiregs *regs = hdata->regs;
  43. unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
  44. unsigned long addr = virt_to_bus(cmd->SCp.ptr);
  45. /*
  46. * if the physical address has the wrong alignment, or if
  47. * physical address is bad, or if it is a write and at the
  48. * end of a physical memory chunk, then allocate a bounce
  49. * buffer
  50. */
  51. if (addr & A3000_XFER_MASK) {
  52. wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
  53. wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
  54. GFP_KERNEL);
  55. /* can't allocate memory; use PIO */
  56. if (!wh->dma_bounce_buffer) {
  57. wh->dma_bounce_len = 0;
  58. return 1;
  59. }
  60. if (!dir_in) {
  61. /* copy to bounce buffer for a write */
  62. memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
  63. cmd->SCp.this_residual);
  64. }
  65. addr = virt_to_bus(wh->dma_bounce_buffer);
  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. mb(); /* make sure setup is completed */
  84. regs->ST_DMA = 1;
  85. mb(); /* make sure DMA has started before next IO */
  86. /* return success */
  87. return 0;
  88. }
  89. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  90. int status)
  91. {
  92. struct a3000_hostdata *hdata = shost_priv(instance);
  93. struct WD33C93_hostdata *wh = &hdata->wh;
  94. struct a3000_scsiregs *regs = hdata->regs;
  95. /* disable SCSI interrupts */
  96. unsigned short cntr = CNTR_PDMD;
  97. if (!wh->dma_dir)
  98. cntr |= CNTR_DDIR;
  99. regs->CNTR = cntr;
  100. mb(); /* make sure CNTR is updated before next IO */
  101. /* flush if we were reading */
  102. if (wh->dma_dir) {
  103. regs->FLUSH = 1;
  104. mb(); /* don't allow prefetch */
  105. while (!(regs->ISTR & ISTR_FE_FLG))
  106. barrier();
  107. mb(); /* no IO until FLUSH is done */
  108. }
  109. /* clear a possible interrupt */
  110. /* I think that this CINT is only necessary if you are
  111. * using the terminal count features. HM 7 Mar 1994
  112. */
  113. regs->CINT = 1;
  114. /* stop DMA */
  115. regs->SP_DMA = 1;
  116. mb(); /* make sure DMA is stopped before next IO */
  117. /* restore the CONTROL bits (minus the direction flag) */
  118. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  119. mb(); /* make sure CNTR is updated before next IO */
  120. /* copy from a bounce buffer, if necessary */
  121. if (status && wh->dma_bounce_buffer) {
  122. if (SCpnt) {
  123. if (wh->dma_dir && SCpnt)
  124. memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
  125. SCpnt->SCp.this_residual);
  126. kfree(wh->dma_bounce_buffer);
  127. wh->dma_bounce_buffer = NULL;
  128. wh->dma_bounce_len = 0;
  129. } else {
  130. kfree(wh->dma_bounce_buffer);
  131. wh->dma_bounce_buffer = NULL;
  132. wh->dma_bounce_len = 0;
  133. }
  134. }
  135. }
  136. static int a3000_bus_reset(struct scsi_cmnd *cmd)
  137. {
  138. struct Scsi_Host *instance = cmd->device->host;
  139. /* FIXME perform bus-specific reset */
  140. /* FIXME 2: kill this entire function, which should
  141. cause mid-layer to call wd33c93_host_reset anyway? */
  142. spin_lock_irq(instance->host_lock);
  143. wd33c93_host_reset(cmd);
  144. spin_unlock_irq(instance->host_lock);
  145. return SUCCESS;
  146. }
  147. static struct scsi_host_template amiga_a3000_scsi_template = {
  148. .module = THIS_MODULE,
  149. .name = "Amiga 3000 built-in SCSI",
  150. .proc_info = wd33c93_proc_info,
  151. .proc_name = "A3000",
  152. .queuecommand = wd33c93_queuecommand,
  153. .eh_abort_handler = wd33c93_abort,
  154. .eh_bus_reset_handler = a3000_bus_reset,
  155. .eh_host_reset_handler = wd33c93_host_reset,
  156. .can_queue = CAN_QUEUE,
  157. .this_id = 7,
  158. .sg_tablesize = SG_ALL,
  159. .cmd_per_lun = CMD_PER_LUN,
  160. .use_clustering = ENABLE_CLUSTERING
  161. };
  162. static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
  163. {
  164. struct resource *res;
  165. struct Scsi_Host *instance;
  166. int error;
  167. struct a3000_scsiregs *regs;
  168. wd33c93_regs wdregs;
  169. struct a3000_hostdata *hdata;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (!res)
  172. return -ENODEV;
  173. if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
  174. return -EBUSY;
  175. instance = scsi_host_alloc(&amiga_a3000_scsi_template,
  176. sizeof(struct a3000_hostdata));
  177. if (!instance) {
  178. error = -ENOMEM;
  179. goto fail_alloc;
  180. }
  181. instance->irq = IRQ_AMIGA_PORTS;
  182. regs = (struct a3000_scsiregs *)ZTWO_VADDR(res->start);
  183. regs->DAWR = DAWR_A3000;
  184. wdregs.SASR = &regs->SASR;
  185. wdregs.SCMD = &regs->SCMD;
  186. hdata = shost_priv(instance);
  187. hdata->wh.no_sync = 0xff;
  188. hdata->wh.fast = 0;
  189. hdata->wh.dma_mode = CTRL_DMA;
  190. hdata->regs = regs;
  191. wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
  192. error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
  193. "A3000 SCSI", instance);
  194. if (error)
  195. goto fail_irq;
  196. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  197. error = scsi_add_host(instance, NULL);
  198. if (error)
  199. goto fail_host;
  200. platform_set_drvdata(pdev, instance);
  201. scsi_scan_host(instance);
  202. return 0;
  203. fail_host:
  204. free_irq(IRQ_AMIGA_PORTS, instance);
  205. fail_irq:
  206. scsi_host_put(instance);
  207. fail_alloc:
  208. release_mem_region(res->start, resource_size(res));
  209. return error;
  210. }
  211. static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
  212. {
  213. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  214. struct a3000_hostdata *hdata = shost_priv(instance);
  215. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  216. hdata->regs->CNTR = 0;
  217. scsi_remove_host(instance);
  218. free_irq(IRQ_AMIGA_PORTS, instance);
  219. scsi_host_put(instance);
  220. release_mem_region(res->start, resource_size(res));
  221. return 0;
  222. }
  223. static struct platform_driver amiga_a3000_scsi_driver = {
  224. .remove = __exit_p(amiga_a3000_scsi_remove),
  225. .driver = {
  226. .name = "amiga-a3000-scsi",
  227. .owner = THIS_MODULE,
  228. },
  229. };
  230. static int __init amiga_a3000_scsi_init(void)
  231. {
  232. return platform_driver_probe(&amiga_a3000_scsi_driver,
  233. amiga_a3000_scsi_probe);
  234. }
  235. module_init(amiga_a3000_scsi_init);
  236. static void __exit amiga_a3000_scsi_exit(void)
  237. {
  238. platform_driver_unregister(&amiga_a3000_scsi_driver);
  239. }
  240. module_exit(amiga_a3000_scsi_exit);
  241. MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
  242. MODULE_LICENSE("GPL");
  243. MODULE_ALIAS("platform:amiga-a3000-scsi");