sun3x_esp.c 7.2 KB

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