a3000.c 7.2 KB

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