a3000.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/mm.h>
  4. #include <linux/ioport.h>
  5. #include <linux/init.h>
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/module.h>
  12. #include <asm/page.h>
  13. #include <asm/amigaints.h>
  14. #include <asm/amigahw.h>
  15. #include <scsi/scsi.h>
  16. #include <scsi/scsi_cmnd.h>
  17. #include <scsi/scsi_device.h>
  18. #include <scsi/scsi_eh.h>
  19. #include <scsi/scsi_tcq.h>
  20. #include "wd33c93.h"
  21. #include "a3000.h"
  22. struct a3000_hostdata {
  23. struct WD33C93_hostdata wh;
  24. struct a3000_scsiregs *regs;
  25. struct device *dev;
  26. };
  27. #define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
  28. static irqreturn_t a3000_intr(int irq, void *data)
  29. {
  30. struct Scsi_Host *instance = data;
  31. struct a3000_hostdata *hdata = shost_priv(instance);
  32. unsigned int status = hdata->regs->ISTR;
  33. unsigned long flags;
  34. if (!(status & ISTR_INT_P))
  35. return IRQ_NONE;
  36. if (status & ISTR_INTS) {
  37. spin_lock_irqsave(instance->host_lock, flags);
  38. wd33c93_intr(instance);
  39. spin_unlock_irqrestore(instance->host_lock, flags);
  40. return IRQ_HANDLED;
  41. }
  42. pr_warn("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
  43. return IRQ_NONE;
  44. }
  45. static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
  46. {
  47. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
  48. unsigned long len = scsi_pointer->this_residual;
  49. struct Scsi_Host *instance = cmd->device->host;
  50. struct a3000_hostdata *hdata = shost_priv(instance);
  51. struct WD33C93_hostdata *wh = &hdata->wh;
  52. struct a3000_scsiregs *regs = hdata->regs;
  53. unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
  54. dma_addr_t addr;
  55. addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
  56. len, DMA_DIR(dir_in));
  57. if (dma_mapping_error(hdata->dev, addr)) {
  58. dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
  59. scsi_pointer->ptr);
  60. return 1;
  61. }
  62. scsi_pointer->dma_handle = addr;
  63. /*
  64. * if the physical address has the wrong alignment, or if
  65. * physical address is bad, or if it is a write and at the
  66. * end of a physical memory chunk, then allocate a bounce
  67. * buffer
  68. * MSch 20220629 - only wrong alignment tested - bounce
  69. * buffer returned by kmalloc is guaranteed to be aligned
  70. */
  71. if (addr & A3000_XFER_MASK) {
  72. WARN_ONCE(1, "Invalid alignment for DMA!");
  73. /* drop useless mapping */
  74. dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
  75. scsi_pointer->this_residual,
  76. DMA_DIR(dir_in));
  77. wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
  78. wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
  79. GFP_KERNEL);
  80. /* can't allocate memory; use PIO */
  81. if (!wh->dma_bounce_buffer) {
  82. wh->dma_bounce_len = 0;
  83. scsi_pointer->dma_handle = (dma_addr_t) NULL;
  84. return 1;
  85. }
  86. if (!dir_in) {
  87. /* copy to bounce buffer for a write */
  88. memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
  89. scsi_pointer->this_residual);
  90. }
  91. addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
  92. len, DMA_DIR(dir_in));
  93. if (dma_mapping_error(hdata->dev, addr)) {
  94. dev_warn(hdata->dev,
  95. "cannot map SCSI data block %p\n",
  96. scsi_pointer->ptr);
  97. return 1;
  98. }
  99. scsi_pointer->dma_handle = addr;
  100. }
  101. /* setup dma direction */
  102. if (!dir_in)
  103. cntr |= CNTR_DDIR;
  104. /* remember direction */
  105. wh->dma_dir = dir_in;
  106. regs->CNTR = cntr;
  107. /* setup DMA *physical* address */
  108. regs->ACR = addr;
  109. /* no more cache flush here - dma_map_single() takes care */
  110. /* start DMA */
  111. mb(); /* make sure setup is completed */
  112. regs->ST_DMA = 1;
  113. mb(); /* make sure DMA has started before next IO */
  114. /* return success */
  115. return 0;
  116. }
  117. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  118. int status)
  119. {
  120. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
  121. struct a3000_hostdata *hdata = shost_priv(instance);
  122. struct WD33C93_hostdata *wh = &hdata->wh;
  123. struct a3000_scsiregs *regs = hdata->regs;
  124. /* disable SCSI interrupts */
  125. unsigned short cntr = CNTR_PDMD;
  126. if (!wh->dma_dir)
  127. cntr |= CNTR_DDIR;
  128. regs->CNTR = cntr;
  129. mb(); /* make sure CNTR is updated before next IO */
  130. /* flush if we were reading */
  131. if (wh->dma_dir) {
  132. regs->FLUSH = 1;
  133. mb(); /* don't allow prefetch */
  134. while (!(regs->ISTR & ISTR_FE_FLG))
  135. barrier();
  136. mb(); /* no IO until FLUSH is done */
  137. }
  138. /* clear a possible interrupt */
  139. /* I think that this CINT is only necessary if you are
  140. * using the terminal count features. HM 7 Mar 1994
  141. */
  142. regs->CINT = 1;
  143. /* stop DMA */
  144. regs->SP_DMA = 1;
  145. mb(); /* make sure DMA is stopped before next IO */
  146. /* restore the CONTROL bits (minus the direction flag) */
  147. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  148. mb(); /* make sure CNTR is updated before next IO */
  149. dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
  150. scsi_pointer->this_residual,
  151. DMA_DIR(wh->dma_dir));
  152. /* copy from a bounce buffer, if necessary */
  153. if (status && wh->dma_bounce_buffer) {
  154. if (SCpnt) {
  155. if (wh->dma_dir && SCpnt)
  156. memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
  157. scsi_pointer->this_residual);
  158. kfree(wh->dma_bounce_buffer);
  159. wh->dma_bounce_buffer = NULL;
  160. wh->dma_bounce_len = 0;
  161. } else {
  162. kfree(wh->dma_bounce_buffer);
  163. wh->dma_bounce_buffer = NULL;
  164. wh->dma_bounce_len = 0;
  165. }
  166. }
  167. }
  168. static struct scsi_host_template amiga_a3000_scsi_template = {
  169. .module = THIS_MODULE,
  170. .name = "Amiga 3000 built-in SCSI",
  171. .show_info = wd33c93_show_info,
  172. .write_info = wd33c93_write_info,
  173. .proc_name = "A3000",
  174. .queuecommand = wd33c93_queuecommand,
  175. .eh_abort_handler = wd33c93_abort,
  176. .eh_host_reset_handler = wd33c93_host_reset,
  177. .can_queue = CAN_QUEUE,
  178. .this_id = 7,
  179. .sg_tablesize = SG_ALL,
  180. .cmd_per_lun = CMD_PER_LUN,
  181. .cmd_size = sizeof(struct scsi_pointer),
  182. };
  183. static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
  184. {
  185. struct resource *res;
  186. struct Scsi_Host *instance;
  187. int error;
  188. struct a3000_scsiregs *regs;
  189. wd33c93_regs wdregs;
  190. struct a3000_hostdata *hdata;
  191. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
  192. dev_warn(&pdev->dev, "cannot use 32 bit DMA\n");
  193. return -ENODEV;
  194. }
  195. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  196. if (!res)
  197. return -ENODEV;
  198. if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
  199. return -EBUSY;
  200. instance = scsi_host_alloc(&amiga_a3000_scsi_template,
  201. sizeof(struct a3000_hostdata));
  202. if (!instance) {
  203. error = -ENOMEM;
  204. goto fail_alloc;
  205. }
  206. instance->irq = IRQ_AMIGA_PORTS;
  207. regs = ZTWO_VADDR(res->start);
  208. regs->DAWR = DAWR_A3000;
  209. wdregs.SASR = &regs->SASR;
  210. wdregs.SCMD = &regs->SCMD;
  211. hdata = shost_priv(instance);
  212. hdata->dev = &pdev->dev;
  213. hdata->wh.no_sync = 0xff;
  214. hdata->wh.fast = 0;
  215. hdata->wh.dma_mode = CTRL_DMA;
  216. hdata->regs = regs;
  217. wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
  218. error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
  219. "A3000 SCSI", instance);
  220. if (error)
  221. goto fail_irq;
  222. regs->CNTR = CNTR_PDMD | CNTR_INTEN;
  223. error = scsi_add_host(instance, NULL);
  224. if (error)
  225. goto fail_host;
  226. platform_set_drvdata(pdev, instance);
  227. scsi_scan_host(instance);
  228. return 0;
  229. fail_host:
  230. free_irq(IRQ_AMIGA_PORTS, instance);
  231. fail_irq:
  232. scsi_host_put(instance);
  233. fail_alloc:
  234. release_mem_region(res->start, resource_size(res));
  235. return error;
  236. }
  237. static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
  238. {
  239. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  240. struct a3000_hostdata *hdata = shost_priv(instance);
  241. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. hdata->regs->CNTR = 0;
  243. scsi_remove_host(instance);
  244. free_irq(IRQ_AMIGA_PORTS, instance);
  245. scsi_host_put(instance);
  246. release_mem_region(res->start, resource_size(res));
  247. return 0;
  248. }
  249. static struct platform_driver amiga_a3000_scsi_driver = {
  250. .remove = __exit_p(amiga_a3000_scsi_remove),
  251. .driver = {
  252. .name = "amiga-a3000-scsi",
  253. },
  254. };
  255. module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
  256. MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
  257. MODULE_LICENSE("GPL");
  258. MODULE_ALIAS("platform:amiga-a3000-scsi");