jazz_esp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* jazz_esp.c: ESP front-end for MIPS JAZZ systems.
  2. *
  3. * Copyright (C) 2007 Thomas Bogendörfer (tsbogend@alpha.frankende)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <asm/irq.h>
  13. #include <asm/io.h>
  14. #include <asm/dma.h>
  15. #include <asm/jazz.h>
  16. #include <asm/jazzdma.h>
  17. #include <scsi/scsi_host.h>
  18. #include "esp_scsi.h"
  19. #define DRV_MODULE_NAME "jazz_esp"
  20. #define PFX DRV_MODULE_NAME ": "
  21. #define DRV_VERSION "1.000"
  22. #define DRV_MODULE_RELDATE "May 19, 2007"
  23. static void jazz_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  24. {
  25. *(volatile u8 *)(esp->regs + reg) = val;
  26. }
  27. static u8 jazz_esp_read8(struct esp *esp, unsigned long reg)
  28. {
  29. return *(volatile u8 *)(esp->regs + reg);
  30. }
  31. static dma_addr_t jazz_esp_map_single(struct esp *esp, void *buf,
  32. size_t sz, int dir)
  33. {
  34. return dma_map_single(esp->dev, buf, sz, dir);
  35. }
  36. static int jazz_esp_map_sg(struct esp *esp, struct scatterlist *sg,
  37. int num_sg, int dir)
  38. {
  39. return dma_map_sg(esp->dev, sg, num_sg, dir);
  40. }
  41. static void jazz_esp_unmap_single(struct esp *esp, dma_addr_t addr,
  42. size_t sz, int dir)
  43. {
  44. dma_unmap_single(esp->dev, addr, sz, dir);
  45. }
  46. static void jazz_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
  47. int num_sg, int dir)
  48. {
  49. dma_unmap_sg(esp->dev, sg, num_sg, dir);
  50. }
  51. static int jazz_esp_irq_pending(struct esp *esp)
  52. {
  53. if (jazz_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR)
  54. return 1;
  55. return 0;
  56. }
  57. static void jazz_esp_reset_dma(struct esp *esp)
  58. {
  59. vdma_disable ((int)esp->dma_regs);
  60. }
  61. static void jazz_esp_dma_drain(struct esp *esp)
  62. {
  63. /* nothing to do */
  64. }
  65. static void jazz_esp_dma_invalidate(struct esp *esp)
  66. {
  67. vdma_disable ((int)esp->dma_regs);
  68. }
  69. static void jazz_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  70. u32 dma_count, int write, u8 cmd)
  71. {
  72. BUG_ON(!(cmd & ESP_CMD_DMA));
  73. jazz_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  74. jazz_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  75. vdma_disable ((int)esp->dma_regs);
  76. if (write)
  77. vdma_set_mode ((int)esp->dma_regs, DMA_MODE_READ);
  78. else
  79. vdma_set_mode ((int)esp->dma_regs, DMA_MODE_WRITE);
  80. vdma_set_addr ((int)esp->dma_regs, addr);
  81. vdma_set_count ((int)esp->dma_regs, dma_count);
  82. vdma_enable ((int)esp->dma_regs);
  83. scsi_esp_cmd(esp, cmd);
  84. }
  85. static int jazz_esp_dma_error(struct esp *esp)
  86. {
  87. u32 enable = vdma_get_enable((int)esp->dma_regs);
  88. if (enable & (R4030_MEM_INTR|R4030_ADDR_INTR))
  89. return 1;
  90. return 0;
  91. }
  92. static const struct esp_driver_ops jazz_esp_ops = {
  93. .esp_write8 = jazz_esp_write8,
  94. .esp_read8 = jazz_esp_read8,
  95. .map_single = jazz_esp_map_single,
  96. .map_sg = jazz_esp_map_sg,
  97. .unmap_single = jazz_esp_unmap_single,
  98. .unmap_sg = jazz_esp_unmap_sg,
  99. .irq_pending = jazz_esp_irq_pending,
  100. .reset_dma = jazz_esp_reset_dma,
  101. .dma_drain = jazz_esp_dma_drain,
  102. .dma_invalidate = jazz_esp_dma_invalidate,
  103. .send_dma_cmd = jazz_esp_send_dma_cmd,
  104. .dma_error = jazz_esp_dma_error,
  105. };
  106. static int __devinit esp_jazz_probe(struct platform_device *dev)
  107. {
  108. struct scsi_host_template *tpnt = &scsi_esp_template;
  109. struct Scsi_Host *host;
  110. struct esp *esp;
  111. struct resource *res;
  112. int err;
  113. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  114. err = -ENOMEM;
  115. if (!host)
  116. goto fail;
  117. host->max_id = 8;
  118. esp = shost_priv(host);
  119. esp->host = host;
  120. esp->dev = dev;
  121. esp->ops = &jazz_esp_ops;
  122. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  123. if (!res)
  124. goto fail_unlink;
  125. esp->regs = (void __iomem *)res->start;
  126. if (!esp->regs)
  127. goto fail_unlink;
  128. res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  129. if (!res)
  130. goto fail_unlink;
  131. esp->dma_regs = (void __iomem *)res->start;
  132. esp->command_block = dma_alloc_coherent(esp->dev, 16,
  133. &esp->command_block_dma,
  134. GFP_KERNEL);
  135. if (!esp->command_block)
  136. goto fail_unmap_regs;
  137. host->irq = platform_get_irq(dev, 0);
  138. err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
  139. if (err < 0)
  140. goto fail_unmap_command_block;
  141. esp->scsi_id = 7;
  142. esp->host->this_id = esp->scsi_id;
  143. esp->scsi_id_mask = (1 << esp->scsi_id);
  144. esp->cfreq = 40000000;
  145. dev_set_drvdata(&dev->dev, esp);
  146. err = scsi_esp_register(esp, &dev->dev);
  147. if (err)
  148. goto fail_free_irq;
  149. return 0;
  150. fail_free_irq:
  151. free_irq(host->irq, esp);
  152. fail_unmap_command_block:
  153. dma_free_coherent(esp->dev, 16,
  154. esp->command_block,
  155. esp->command_block_dma);
  156. fail_unmap_regs:
  157. fail_unlink:
  158. scsi_host_put(host);
  159. fail:
  160. return err;
  161. }
  162. static int __devexit esp_jazz_remove(struct platform_device *dev)
  163. {
  164. struct esp *esp = dev_get_drvdata(&dev->dev);
  165. unsigned int irq = esp->host->irq;
  166. scsi_esp_unregister(esp);
  167. free_irq(irq, esp);
  168. dma_free_coherent(esp->dev, 16,
  169. esp->command_block,
  170. esp->command_block_dma);
  171. scsi_host_put(esp->host);
  172. return 0;
  173. }
  174. /* work with hotplug and coldplug */
  175. MODULE_ALIAS("platform:jazz_esp");
  176. static struct platform_driver esp_jazz_driver = {
  177. .probe = esp_jazz_probe,
  178. .remove = __devexit_p(esp_jazz_remove),
  179. .driver = {
  180. .name = "jazz_esp",
  181. .owner = THIS_MODULE,
  182. },
  183. };
  184. static int __init jazz_esp_init(void)
  185. {
  186. return platform_driver_register(&esp_jazz_driver);
  187. }
  188. static void __exit jazz_esp_exit(void)
  189. {
  190. platform_driver_unregister(&esp_jazz_driver);
  191. }
  192. MODULE_DESCRIPTION("JAZZ ESP SCSI driver");
  193. MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
  194. MODULE_LICENSE("GPL");
  195. MODULE_VERSION(DRV_VERSION);
  196. module_init(jazz_esp_init);
  197. module_exit(jazz_esp_exit);