a3000.c 7.0 KB

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