jazz_esp.c 5.4 KB

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