sgiwd93.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
  7. * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
  8. * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org)
  9. * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org)
  10. *
  11. * (In all truth, Jed Schimmel wrote all this code.)
  12. */
  13. #undef DEBUG
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/gfp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/sgi/hpc3.h>
  25. #include <asm/sgi/ip22.h>
  26. #include <asm/sgi/wd.h>
  27. #include "scsi.h"
  28. #include "wd33c93.h"
  29. struct ip22_hostdata {
  30. struct WD33C93_hostdata wh;
  31. dma_addr_t dma;
  32. void *cpu;
  33. struct device *dev;
  34. };
  35. #define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))
  36. struct hpc_chunk {
  37. struct hpc_dma_desc desc;
  38. u32 _padding; /* align to quadword boundary */
  39. };
  40. /* space for hpc dma descriptors */
  41. #define HPC_DMA_SIZE PAGE_SIZE
  42. #define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
  43. static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
  44. {
  45. struct Scsi_Host * host = dev_id;
  46. unsigned long flags;
  47. spin_lock_irqsave(host->host_lock, flags);
  48. wd33c93_intr(host);
  49. spin_unlock_irqrestore(host->host_lock, flags);
  50. return IRQ_HANDLED;
  51. }
  52. static inline
  53. void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din)
  54. {
  55. unsigned long len = cmd->SCp.this_residual;
  56. void *addr = cmd->SCp.ptr;
  57. dma_addr_t physaddr;
  58. unsigned long count;
  59. struct hpc_chunk *hcp;
  60. physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
  61. cmd->SCp.dma_handle = physaddr;
  62. hcp = hd->cpu;
  63. while (len) {
  64. /*
  65. * even cntinfo could be up to 16383, without
  66. * magic only 8192 works correctly
  67. */
  68. count = len > 8192 ? 8192 : len;
  69. hcp->desc.pbuf = physaddr;
  70. hcp->desc.cntinfo = count;
  71. hcp++;
  72. len -= count;
  73. physaddr += count;
  74. }
  75. /*
  76. * To make sure, if we trip an HPC bug, that we transfer every single
  77. * byte, we tag on an extra zero length dma descriptor at the end of
  78. * the chain.
  79. */
  80. hcp->desc.pbuf = 0;
  81. hcp->desc.cntinfo = HPCDMA_EOX;
  82. dma_cache_sync(hd->dev, hd->cpu,
  83. (unsigned long)(hcp + 1) - (unsigned long)hd->cpu,
  84. DMA_TO_DEVICE);
  85. }
  86. static int dma_setup(struct scsi_cmnd *cmd, int datainp)
  87. {
  88. struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
  89. struct hpc3_scsiregs *hregs =
  90. (struct hpc3_scsiregs *) cmd->device->host->base;
  91. pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu);
  92. hdata->wh.dma_dir = datainp;
  93. /*
  94. * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
  95. * other wd33c93 drivers deal with it the same way (which isn't that
  96. * obvious). IMHO a better fix would be, not to do these dma setups
  97. * in the first place.
  98. */
  99. if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
  100. return 1;
  101. fill_hpc_entries(hdata, cmd, datainp);
  102. pr_debug(" HPCGO\n");
  103. /* Start up the HPC. */
  104. hregs->ndptr = hdata->dma;
  105. if (datainp)
  106. hregs->ctrl = HPC3_SCTRL_ACTIVE;
  107. else
  108. hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
  109. return 0;
  110. }
  111. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  112. int status)
  113. {
  114. struct ip22_hostdata *hdata = host_to_hostdata(instance);
  115. struct hpc3_scsiregs *hregs;
  116. if (!SCpnt)
  117. return;
  118. if (SCpnt->SCp.ptr == NULL || SCpnt->SCp.this_residual == 0)
  119. return;
  120. hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
  121. pr_debug("dma_stop: status<%d> ", status);
  122. /* First stop the HPC and flush it's FIFO. */
  123. if (hdata->wh.dma_dir) {
  124. hregs->ctrl |= HPC3_SCTRL_FLUSH;
  125. while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
  126. barrier();
  127. }
  128. hregs->ctrl = 0;
  129. dma_unmap_single(hdata->dev, SCpnt->SCp.dma_handle,
  130. SCpnt->SCp.this_residual,
  131. DMA_DIR(hdata->wh.dma_dir));
  132. pr_debug("\n");
  133. }
  134. void sgiwd93_reset(unsigned long base)
  135. {
  136. struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
  137. hregs->ctrl = HPC3_SCTRL_CRESET;
  138. udelay(50);
  139. hregs->ctrl = 0;
  140. }
  141. EXPORT_SYMBOL_GPL(sgiwd93_reset);
  142. static inline void init_hpc_chain(struct ip22_hostdata *hdata)
  143. {
  144. struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu;
  145. dma_addr_t dma = hdata->dma;
  146. unsigned long start, end;
  147. start = (unsigned long) hcp;
  148. end = start + HPC_DMA_SIZE;
  149. while (start < end) {
  150. hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
  151. hcp->desc.cntinfo = HPCDMA_EOX;
  152. hcp++;
  153. dma += sizeof(struct hpc_chunk);
  154. start += sizeof(struct hpc_chunk);
  155. };
  156. hcp--;
  157. hcp->desc.pnext = hdata->dma;
  158. }
  159. /*
  160. * Kludge alert - the SCSI code calls the abort and reset method with int
  161. * arguments not with pointers. So this is going to blow up beautyfully
  162. * on 64-bit systems with memory outside the compat address spaces.
  163. */
  164. static struct scsi_host_template sgiwd93_template = {
  165. .module = THIS_MODULE,
  166. .proc_name = "SGIWD93",
  167. .name = "SGI WD93",
  168. .queuecommand = wd33c93_queuecommand,
  169. .eh_abort_handler = wd33c93_abort,
  170. .eh_host_reset_handler = wd33c93_host_reset,
  171. .can_queue = 16,
  172. .this_id = 7,
  173. .sg_tablesize = SG_ALL,
  174. .cmd_per_lun = 8,
  175. .use_clustering = DISABLE_CLUSTERING,
  176. };
  177. static int sgiwd93_probe(struct platform_device *pdev)
  178. {
  179. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  180. unsigned char *wdregs = pd->wdregs;
  181. struct hpc3_scsiregs *hregs = pd->hregs;
  182. struct ip22_hostdata *hdata;
  183. struct Scsi_Host *host;
  184. wd33c93_regs regs;
  185. unsigned int unit = pd->unit;
  186. unsigned int irq = pd->irq;
  187. int err;
  188. host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
  189. if (!host) {
  190. err = -ENOMEM;
  191. goto out;
  192. }
  193. host->base = (unsigned long) hregs;
  194. host->irq = irq;
  195. hdata = host_to_hostdata(host);
  196. hdata->dev = &pdev->dev;
  197. hdata->cpu = dma_alloc_attrs(&pdev->dev, HPC_DMA_SIZE, &hdata->dma,
  198. GFP_KERNEL, DMA_ATTR_NON_CONSISTENT);
  199. if (!hdata->cpu) {
  200. printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
  201. "host %d buffer.\n", unit);
  202. err = -ENOMEM;
  203. goto out_put;
  204. }
  205. init_hpc_chain(hdata);
  206. regs.SASR = wdregs + 3;
  207. regs.SCMD = wdregs + 7;
  208. hdata->wh.no_sync = 0;
  209. hdata->wh.fast = 1;
  210. hdata->wh.dma_mode = CTRL_BURST;
  211. wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
  212. err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
  213. if (err) {
  214. printk(KERN_WARNING "sgiwd93: Could not register irq %d "
  215. "for host %d.\n", irq, unit);
  216. goto out_free;
  217. }
  218. platform_set_drvdata(pdev, host);
  219. err = scsi_add_host(host, NULL);
  220. if (err)
  221. goto out_irq;
  222. scsi_scan_host(host);
  223. return 0;
  224. out_irq:
  225. free_irq(irq, host);
  226. out_free:
  227. dma_free_attrs(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
  228. DMA_ATTR_NON_CONSISTENT);
  229. out_put:
  230. scsi_host_put(host);
  231. out:
  232. return err;
  233. }
  234. static int sgiwd93_remove(struct platform_device *pdev)
  235. {
  236. struct Scsi_Host *host = platform_get_drvdata(pdev);
  237. struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
  238. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  239. scsi_remove_host(host);
  240. free_irq(pd->irq, host);
  241. dma_free_attrs(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
  242. DMA_ATTR_NON_CONSISTENT);
  243. scsi_host_put(host);
  244. return 0;
  245. }
  246. static struct platform_driver sgiwd93_driver = {
  247. .probe = sgiwd93_probe,
  248. .remove = sgiwd93_remove,
  249. .driver = {
  250. .name = "sgiwd93",
  251. }
  252. };
  253. static int __init sgiwd93_module_init(void)
  254. {
  255. return platform_driver_register(&sgiwd93_driver);
  256. }
  257. static void __exit sgiwd93_module_exit(void)
  258. {
  259. return platform_driver_unregister(&sgiwd93_driver);
  260. }
  261. module_init(sgiwd93_module_init);
  262. module_exit(sgiwd93_module_exit);
  263. MODULE_DESCRIPTION("SGI WD33C93 driver");
  264. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:sgiwd93");