sgiwd93.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. static int sgiwd93_bus_reset(struct scsi_cmnd *cmd)
  160. {
  161. /* FIXME perform bus-specific reset */
  162. /* FIXME 2: kill this function, and let midlayer fallback
  163. to the same result, calling wd33c93_host_reset() */
  164. spin_lock_irq(cmd->device->host->host_lock);
  165. wd33c93_host_reset(cmd);
  166. spin_unlock_irq(cmd->device->host->host_lock);
  167. return SUCCESS;
  168. }
  169. /*
  170. * Kludge alert - the SCSI code calls the abort and reset method with int
  171. * arguments not with pointers. So this is going to blow up beautyfully
  172. * on 64-bit systems with memory outside the compat address spaces.
  173. */
  174. static struct scsi_host_template sgiwd93_template = {
  175. .module = THIS_MODULE,
  176. .proc_name = "SGIWD93",
  177. .name = "SGI WD93",
  178. .queuecommand = wd33c93_queuecommand,
  179. .eh_abort_handler = wd33c93_abort,
  180. .eh_bus_reset_handler = sgiwd93_bus_reset,
  181. .eh_host_reset_handler = wd33c93_host_reset,
  182. .can_queue = 16,
  183. .this_id = 7,
  184. .sg_tablesize = SG_ALL,
  185. .cmd_per_lun = 8,
  186. .use_clustering = DISABLE_CLUSTERING,
  187. };
  188. static int __devinit sgiwd93_probe(struct platform_device *pdev)
  189. {
  190. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  191. unsigned char *wdregs = pd->wdregs;
  192. struct hpc3_scsiregs *hregs = pd->hregs;
  193. struct ip22_hostdata *hdata;
  194. struct Scsi_Host *host;
  195. wd33c93_regs regs;
  196. unsigned int unit = pd->unit;
  197. unsigned int irq = pd->irq;
  198. int err;
  199. host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
  200. if (!host) {
  201. err = -ENOMEM;
  202. goto out;
  203. }
  204. host->base = (unsigned long) hregs;
  205. host->irq = irq;
  206. hdata = host_to_hostdata(host);
  207. hdata->dev = &pdev->dev;
  208. hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE,
  209. &hdata->dma, GFP_KERNEL);
  210. if (!hdata->cpu) {
  211. printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
  212. "host %d buffer.\n", unit);
  213. err = -ENOMEM;
  214. goto out_put;
  215. }
  216. init_hpc_chain(hdata);
  217. regs.SASR = wdregs + 3;
  218. regs.SCMD = wdregs + 7;
  219. hdata->wh.no_sync = 0;
  220. hdata->wh.fast = 1;
  221. hdata->wh.dma_mode = CTRL_BURST;
  222. wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
  223. err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
  224. if (err) {
  225. printk(KERN_WARNING "sgiwd93: Could not register irq %d "
  226. "for host %d.\n", irq, unit);
  227. goto out_free;
  228. }
  229. platform_set_drvdata(pdev, host);
  230. err = scsi_add_host(host, NULL);
  231. if (err)
  232. goto out_irq;
  233. scsi_scan_host(host);
  234. return 0;
  235. out_irq:
  236. free_irq(irq, host);
  237. out_free:
  238. dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
  239. out_put:
  240. scsi_host_put(host);
  241. out:
  242. return err;
  243. }
  244. static int __exit sgiwd93_remove(struct platform_device *pdev)
  245. {
  246. struct Scsi_Host *host = platform_get_drvdata(pdev);
  247. struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
  248. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  249. scsi_remove_host(host);
  250. free_irq(pd->irq, host);
  251. dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma);
  252. scsi_host_put(host);
  253. return 0;
  254. }
  255. static struct platform_driver sgiwd93_driver = {
  256. .probe = sgiwd93_probe,
  257. .remove = __devexit_p(sgiwd93_remove),
  258. .driver = {
  259. .name = "sgiwd93",
  260. .owner = THIS_MODULE,
  261. }
  262. };
  263. static int __init sgiwd93_module_init(void)
  264. {
  265. return platform_driver_register(&sgiwd93_driver);
  266. }
  267. static void __exit sgiwd93_module_exit(void)
  268. {
  269. return platform_driver_unregister(&sgiwd93_driver);
  270. }
  271. module_init(sgiwd93_module_init);
  272. module_exit(sgiwd93_module_exit);
  273. MODULE_DESCRIPTION("SGI WD33C93 driver");
  274. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS("platform:sgiwd93");