sun3x_esp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* sun3x_esp.c: ESP front-end for Sun3x systems.
  2. *
  3. * Copyright (C) 2007,2008 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/gfp.h>
  7. #include <linux/types.h>
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/sun3x.h>
  15. #include <asm/io.h>
  16. #include <asm/dma.h>
  17. #include <asm/dvma.h>
  18. /* DMA controller reg offsets */
  19. #define DMA_CSR 0x00UL /* rw DMA control/status register 0x00 */
  20. #define DMA_ADDR 0x04UL /* rw DMA transfer address register 0x04 */
  21. #define DMA_COUNT 0x08UL /* rw DMA transfer count register 0x08 */
  22. #define DMA_TEST 0x0cUL /* rw DMA test/debug register 0x0c */
  23. #include <scsi/scsi_host.h>
  24. #include "esp_scsi.h"
  25. #define DRV_MODULE_NAME "sun3x_esp"
  26. #define PFX DRV_MODULE_NAME ": "
  27. #define DRV_VERSION "1.000"
  28. #define DRV_MODULE_RELDATE "Nov 1, 2007"
  29. /*
  30. * m68k always assumes readl/writel operate on little endian
  31. * mmio space; this is wrong at least for Sun3x, so we
  32. * need to workaround this until a proper way is found
  33. */
  34. #if 0
  35. #define dma_read32(REG) \
  36. readl(esp->dma_regs + (REG))
  37. #define dma_write32(VAL, REG) \
  38. writel((VAL), esp->dma_regs + (REG))
  39. #else
  40. #define dma_read32(REG) \
  41. *(volatile u32 *)(esp->dma_regs + (REG))
  42. #define dma_write32(VAL, REG) \
  43. do { *(volatile u32 *)(esp->dma_regs + (REG)) = (VAL); } while (0)
  44. #endif
  45. static void sun3x_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  46. {
  47. writeb(val, esp->regs + (reg * 4UL));
  48. }
  49. static u8 sun3x_esp_read8(struct esp *esp, unsigned long reg)
  50. {
  51. return readb(esp->regs + (reg * 4UL));
  52. }
  53. static dma_addr_t sun3x_esp_map_single(struct esp *esp, void *buf,
  54. size_t sz, int dir)
  55. {
  56. return dma_map_single(esp->dev, buf, sz, dir);
  57. }
  58. static int sun3x_esp_map_sg(struct esp *esp, struct scatterlist *sg,
  59. int num_sg, int dir)
  60. {
  61. return dma_map_sg(esp->dev, sg, num_sg, dir);
  62. }
  63. static void sun3x_esp_unmap_single(struct esp *esp, dma_addr_t addr,
  64. size_t sz, int dir)
  65. {
  66. dma_unmap_single(esp->dev, addr, sz, dir);
  67. }
  68. static void sun3x_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
  69. int num_sg, int dir)
  70. {
  71. dma_unmap_sg(esp->dev, sg, num_sg, dir);
  72. }
  73. static int sun3x_esp_irq_pending(struct esp *esp)
  74. {
  75. if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
  76. return 1;
  77. return 0;
  78. }
  79. static void sun3x_esp_reset_dma(struct esp *esp)
  80. {
  81. u32 val;
  82. val = dma_read32(DMA_CSR);
  83. dma_write32(val | DMA_RST_SCSI, DMA_CSR);
  84. dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
  85. /* Enable interrupts. */
  86. val = dma_read32(DMA_CSR);
  87. dma_write32(val | DMA_INT_ENAB, DMA_CSR);
  88. }
  89. static void sun3x_esp_dma_drain(struct esp *esp)
  90. {
  91. u32 csr;
  92. int lim;
  93. csr = dma_read32(DMA_CSR);
  94. if (!(csr & DMA_FIFO_ISDRAIN))
  95. return;
  96. dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
  97. lim = 1000;
  98. while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
  99. if (--lim == 0) {
  100. printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
  101. esp->host->unique_id);
  102. break;
  103. }
  104. udelay(1);
  105. }
  106. }
  107. static void sun3x_esp_dma_invalidate(struct esp *esp)
  108. {
  109. u32 val;
  110. int lim;
  111. lim = 1000;
  112. while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
  113. if (--lim == 0) {
  114. printk(KERN_ALERT PFX "esp%d: DMA will not "
  115. "invalidate!\n", esp->host->unique_id);
  116. break;
  117. }
  118. udelay(1);
  119. }
  120. val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
  121. val |= DMA_FIFO_INV;
  122. dma_write32(val, DMA_CSR);
  123. val &= ~DMA_FIFO_INV;
  124. dma_write32(val, DMA_CSR);
  125. }
  126. static void sun3x_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  127. u32 dma_count, int write, u8 cmd)
  128. {
  129. u32 csr;
  130. BUG_ON(!(cmd & ESP_CMD_DMA));
  131. sun3x_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  132. sun3x_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  133. csr = dma_read32(DMA_CSR);
  134. csr |= DMA_ENABLE;
  135. if (write)
  136. csr |= DMA_ST_WRITE;
  137. else
  138. csr &= ~DMA_ST_WRITE;
  139. dma_write32(csr, DMA_CSR);
  140. dma_write32(addr, DMA_ADDR);
  141. scsi_esp_cmd(esp, cmd);
  142. }
  143. static int sun3x_esp_dma_error(struct esp *esp)
  144. {
  145. u32 csr = dma_read32(DMA_CSR);
  146. if (csr & DMA_HNDL_ERROR)
  147. return 1;
  148. return 0;
  149. }
  150. static const struct esp_driver_ops sun3x_esp_ops = {
  151. .esp_write8 = sun3x_esp_write8,
  152. .esp_read8 = sun3x_esp_read8,
  153. .map_single = sun3x_esp_map_single,
  154. .map_sg = sun3x_esp_map_sg,
  155. .unmap_single = sun3x_esp_unmap_single,
  156. .unmap_sg = sun3x_esp_unmap_sg,
  157. .irq_pending = sun3x_esp_irq_pending,
  158. .reset_dma = sun3x_esp_reset_dma,
  159. .dma_drain = sun3x_esp_dma_drain,
  160. .dma_invalidate = sun3x_esp_dma_invalidate,
  161. .send_dma_cmd = sun3x_esp_send_dma_cmd,
  162. .dma_error = sun3x_esp_dma_error,
  163. };
  164. static int __devinit esp_sun3x_probe(struct platform_device *dev)
  165. {
  166. struct scsi_host_template *tpnt = &scsi_esp_template;
  167. struct Scsi_Host *host;
  168. struct esp *esp;
  169. struct resource *res;
  170. int err = -ENOMEM;
  171. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  172. if (!host)
  173. goto fail;
  174. host->max_id = 8;
  175. esp = shost_priv(host);
  176. esp->host = host;
  177. esp->dev = dev;
  178. esp->ops = &sun3x_esp_ops;
  179. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  180. if (!res || !res->start)
  181. goto fail_unlink;
  182. esp->regs = ioremap_nocache(res->start, 0x20);
  183. if (!esp->regs)
  184. goto fail_unmap_regs;
  185. res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  186. if (!res || !res->start)
  187. goto fail_unmap_regs;
  188. esp->dma_regs = ioremap_nocache(res->start, 0x10);
  189. esp->command_block = dma_alloc_coherent(esp->dev, 16,
  190. &esp->command_block_dma,
  191. GFP_KERNEL);
  192. if (!esp->command_block)
  193. goto fail_unmap_regs_dma;
  194. host->irq = platform_get_irq(dev, 0);
  195. err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED,
  196. "SUN3X ESP", esp);
  197. if (err < 0)
  198. goto fail_unmap_command_block;
  199. esp->scsi_id = 7;
  200. esp->host->this_id = esp->scsi_id;
  201. esp->scsi_id_mask = (1 << esp->scsi_id);
  202. esp->cfreq = 20000000;
  203. dev_set_drvdata(&dev->dev, esp);
  204. err = scsi_esp_register(esp, &dev->dev);
  205. if (err)
  206. goto fail_free_irq;
  207. return 0;
  208. fail_free_irq:
  209. free_irq(host->irq, esp);
  210. fail_unmap_command_block:
  211. dma_free_coherent(esp->dev, 16,
  212. esp->command_block,
  213. esp->command_block_dma);
  214. fail_unmap_regs_dma:
  215. iounmap(esp->dma_regs);
  216. fail_unmap_regs:
  217. iounmap(esp->regs);
  218. fail_unlink:
  219. scsi_host_put(host);
  220. fail:
  221. return err;
  222. }
  223. static int __devexit esp_sun3x_remove(struct platform_device *dev)
  224. {
  225. struct esp *esp = dev_get_drvdata(&dev->dev);
  226. unsigned int irq = esp->host->irq;
  227. u32 val;
  228. scsi_esp_unregister(esp);
  229. /* Disable interrupts. */
  230. val = dma_read32(DMA_CSR);
  231. dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
  232. free_irq(irq, esp);
  233. dma_free_coherent(esp->dev, 16,
  234. esp->command_block,
  235. esp->command_block_dma);
  236. scsi_host_put(esp->host);
  237. return 0;
  238. }
  239. static struct platform_driver esp_sun3x_driver = {
  240. .probe = esp_sun3x_probe,
  241. .remove = __devexit_p(esp_sun3x_remove),
  242. .driver = {
  243. .name = "sun3x_esp",
  244. .owner = THIS_MODULE,
  245. },
  246. };
  247. static int __init sun3x_esp_init(void)
  248. {
  249. return platform_driver_register(&esp_sun3x_driver);
  250. }
  251. static void __exit sun3x_esp_exit(void)
  252. {
  253. platform_driver_unregister(&esp_sun3x_driver);
  254. }
  255. MODULE_DESCRIPTION("Sun3x ESP SCSI driver");
  256. MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
  257. MODULE_LICENSE("GPL");
  258. MODULE_VERSION(DRV_VERSION);
  259. module_init(sun3x_esp_init);
  260. module_exit(sun3x_esp_exit);
  261. MODULE_ALIAS("platform:sun3x_esp");