NCR_Q720.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* NCR Quad 720 MCA SCSI Driver
  3. *
  4. * Copyright (C) 2003 by James.Bottomley@HansenPartnership.com
  5. */
  6. #include <linux/blkdev.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/mca.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <asm/io.h>
  16. #include "scsi.h"
  17. #include <scsi/scsi_host.h>
  18. #include "ncr53c8xx.h"
  19. #include "NCR_Q720.h"
  20. static struct ncr_chip q720_chip __initdata = {
  21. .revision_id = 0x0f,
  22. .burst_max = 3,
  23. .offset_max = 8,
  24. .nr_divisor = 4,
  25. .features = FE_WIDE | FE_DIFF | FE_VARCLK,
  26. };
  27. MODULE_AUTHOR("James Bottomley");
  28. MODULE_DESCRIPTION("NCR Quad 720 SCSI Driver");
  29. MODULE_LICENSE("GPL");
  30. #define NCR_Q720_VERSION "0.9"
  31. /* We needs this helper because we have up to four hosts per struct device */
  32. struct NCR_Q720_private {
  33. struct device *dev;
  34. void __iomem * mem_base;
  35. __u32 phys_mem_base;
  36. __u32 mem_size;
  37. __u8 irq;
  38. __u8 siops;
  39. __u8 irq_enable;
  40. struct Scsi_Host *hosts[4];
  41. };
  42. static struct scsi_host_template NCR_Q720_tpnt = {
  43. .module = THIS_MODULE,
  44. .proc_name = "NCR_Q720",
  45. };
  46. static irqreturn_t
  47. NCR_Q720_intr(int irq, void *data)
  48. {
  49. struct NCR_Q720_private *p = (struct NCR_Q720_private *)data;
  50. __u8 sir = (readb(p->mem_base + 0x0d) & 0xf0) >> 4;
  51. __u8 siop;
  52. sir |= ~p->irq_enable;
  53. if(sir == 0xff)
  54. return IRQ_NONE;
  55. while((siop = ffz(sir)) < p->siops) {
  56. sir |= 1<<siop;
  57. ncr53c8xx_intr(irq, p->hosts[siop]);
  58. }
  59. return IRQ_HANDLED;
  60. }
  61. static int __init
  62. NCR_Q720_probe_one(struct NCR_Q720_private *p, int siop,
  63. int irq, int slot, __u32 paddr, void __iomem *vaddr)
  64. {
  65. struct ncr_device device;
  66. __u8 scsi_id;
  67. static int unit = 0;
  68. __u8 scsr1 = readb(vaddr + NCR_Q720_SCSR_OFFSET + 1);
  69. __u8 differential = readb(vaddr + NCR_Q720_SCSR_OFFSET) & 0x20;
  70. __u8 version;
  71. int error;
  72. scsi_id = scsr1 >> 4;
  73. /* enable burst length 16 (FIXME: should allow this) */
  74. scsr1 |= 0x02;
  75. /* force a siop reset */
  76. scsr1 |= 0x04;
  77. writeb(scsr1, vaddr + NCR_Q720_SCSR_OFFSET + 1);
  78. udelay(10);
  79. version = readb(vaddr + 0x18) >> 4;
  80. memset(&device, 0, sizeof(struct ncr_device));
  81. /* Initialise ncr_device structure with items required by ncr_attach. */
  82. device.chip = q720_chip;
  83. device.chip.revision_id = version;
  84. device.host_id = scsi_id;
  85. device.dev = p->dev;
  86. device.slot.base = paddr;
  87. device.slot.base_c = paddr;
  88. device.slot.base_v = vaddr;
  89. device.slot.irq = irq;
  90. device.differential = differential ? 2 : 0;
  91. printk("Q720 probe unit %d (siop%d) at 0x%lx, diff = %d, vers = %d\n", unit, siop,
  92. (unsigned long)paddr, differential, version);
  93. p->hosts[siop] = ncr_attach(&NCR_Q720_tpnt, unit++, &device);
  94. if (!p->hosts[siop])
  95. goto fail;
  96. p->irq_enable |= (1<<siop);
  97. scsr1 = readb(vaddr + NCR_Q720_SCSR_OFFSET + 1);
  98. /* clear the disable interrupt bit */
  99. scsr1 &= ~0x01;
  100. writeb(scsr1, vaddr + NCR_Q720_SCSR_OFFSET + 1);
  101. error = scsi_add_host(p->hosts[siop], p->dev);
  102. if (error)
  103. ncr53c8xx_release(p->hosts[siop]);
  104. else
  105. scsi_scan_host(p->hosts[siop]);
  106. return error;
  107. fail:
  108. return -ENODEV;
  109. }
  110. /* Detect a Q720 card. Note, because of the setup --- the chips are
  111. * essentially connectecd to the MCA bus independently, it is easier
  112. * to set them up as two separate host adapters, rather than one
  113. * adapter with two channels */
  114. static int __init
  115. NCR_Q720_probe(struct device *dev)
  116. {
  117. struct NCR_Q720_private *p;
  118. static int banner = 1;
  119. struct mca_device *mca_dev = to_mca_device(dev);
  120. int slot = mca_dev->slot;
  121. int found = 0;
  122. int irq, i, siops;
  123. __u8 pos2, pos4, asr2, asr9, asr10;
  124. __u16 io_base;
  125. __u32 base_addr, mem_size;
  126. void __iomem *mem_base;
  127. p = kzalloc(sizeof(*p), GFP_KERNEL);
  128. if (!p)
  129. return -ENOMEM;
  130. pos2 = mca_device_read_pos(mca_dev, 2);
  131. /* enable device */
  132. pos2 |= NCR_Q720_POS2_BOARD_ENABLE | NCR_Q720_POS2_INTERRUPT_ENABLE;
  133. mca_device_write_pos(mca_dev, 2, pos2);
  134. io_base = (pos2 & NCR_Q720_POS2_IO_MASK) << NCR_Q720_POS2_IO_SHIFT;
  135. if(banner) {
  136. printk(KERN_NOTICE "NCR Q720: Driver Version " NCR_Q720_VERSION "\n"
  137. "NCR Q720: Copyright (c) 2003 by James.Bottomley@HansenPartnership.com\n"
  138. "NCR Q720:\n");
  139. banner = 0;
  140. }
  141. io_base = mca_device_transform_ioport(mca_dev, io_base);
  142. /* OK, this is phase one of the bootstrap, we now know the
  143. * I/O space base address. All the configuration registers
  144. * are mapped here (including pos) */
  145. /* sanity check I/O mapping */
  146. i = inb(io_base) | (inb(io_base+1)<<8);
  147. if(i != NCR_Q720_MCA_ID) {
  148. printk(KERN_ERR "NCR_Q720, adapter failed to I/O map registers correctly at 0x%x(0x%x)\n", io_base, i);
  149. kfree(p);
  150. return -ENODEV;
  151. }
  152. /* Phase II, find the ram base and memory map the board register */
  153. pos4 = inb(io_base + 4);
  154. /* enable streaming data */
  155. pos4 |= 0x01;
  156. outb(pos4, io_base + 4);
  157. base_addr = (pos4 & 0x7e) << 20;
  158. base_addr += (pos4 & 0x80) << 23;
  159. asr10 = inb(io_base + 0x12);
  160. base_addr += (asr10 & 0x80) << 24;
  161. base_addr += (asr10 & 0x70) << 23;
  162. /* OK, got the base addr, now we need to find the ram size,
  163. * enable and map it */
  164. asr9 = inb(io_base + 0x11);
  165. i = (asr9 & 0xc0) >> 6;
  166. if(i == 0)
  167. mem_size = 1024;
  168. else
  169. mem_size = 1 << (19 + i);
  170. /* enable the sram mapping */
  171. asr9 |= 0x20;
  172. /* disable the rom mapping */
  173. asr9 &= ~0x10;
  174. outb(asr9, io_base + 0x11);
  175. if(!request_mem_region(base_addr, mem_size, "NCR_Q720")) {
  176. printk(KERN_ERR "NCR_Q720: Failed to claim memory region 0x%lx\n-0x%lx",
  177. (unsigned long)base_addr,
  178. (unsigned long)(base_addr + mem_size));
  179. goto out_free;
  180. }
  181. if (dma_declare_coherent_memory(dev, base_addr, base_addr,
  182. mem_size, DMA_MEMORY_MAP)
  183. != DMA_MEMORY_MAP) {
  184. printk(KERN_ERR "NCR_Q720: DMA declare memory failed\n");
  185. goto out_release_region;
  186. }
  187. /* The first 1k of the memory buffer is a memory map of the registers
  188. */
  189. mem_base = dma_mark_declared_memory_occupied(dev, base_addr,
  190. 1024);
  191. if (IS_ERR(mem_base)) {
  192. printk("NCR_Q720 failed to reserve memory mapped region\n");
  193. goto out_release;
  194. }
  195. /* now also enable accesses in asr 2 */
  196. asr2 = inb(io_base + 0x0a);
  197. asr2 |= 0x01;
  198. outb(asr2, io_base + 0x0a);
  199. /* get the number of SIOPs (this should be 2 or 4) */
  200. siops = ((asr2 & 0xe0) >> 5) + 1;
  201. /* sanity check mapping (again) */
  202. i = readw(mem_base);
  203. if(i != NCR_Q720_MCA_ID) {
  204. printk(KERN_ERR "NCR_Q720, adapter failed to memory map registers correctly at 0x%lx(0x%x)\n", (unsigned long)base_addr, i);
  205. goto out_release;
  206. }
  207. irq = readb(mem_base + 5) & 0x0f;
  208. /* now do the bus related transforms */
  209. irq = mca_device_transform_irq(mca_dev, irq);
  210. printk(KERN_NOTICE "NCR Q720: found in slot %d irq = %d mem base = 0x%lx siops = %d\n", slot, irq, (unsigned long)base_addr, siops);
  211. printk(KERN_NOTICE "NCR Q720: On board ram %dk\n", mem_size/1024);
  212. p->dev = dev;
  213. p->mem_base = mem_base;
  214. p->phys_mem_base = base_addr;
  215. p->mem_size = mem_size;
  216. p->irq = irq;
  217. p->siops = siops;
  218. if (request_irq(irq, NCR_Q720_intr, IRQF_SHARED, "NCR_Q720", p)) {
  219. printk(KERN_ERR "NCR_Q720: request irq %d failed\n", irq);
  220. goto out_release;
  221. }
  222. /* disable all the siop interrupts */
  223. for(i = 0; i < siops; i++) {
  224. void __iomem *reg_scsr1 = mem_base + NCR_Q720_CHIP_REGISTER_OFFSET
  225. + i*NCR_Q720_SIOP_SHIFT + NCR_Q720_SCSR_OFFSET + 1;
  226. __u8 scsr1 = readb(reg_scsr1);
  227. scsr1 |= 0x01;
  228. writeb(scsr1, reg_scsr1);
  229. }
  230. /* plumb in all 720 chips */
  231. for (i = 0; i < siops; i++) {
  232. void __iomem *siop_v_base = mem_base + NCR_Q720_CHIP_REGISTER_OFFSET
  233. + i*NCR_Q720_SIOP_SHIFT;
  234. __u32 siop_p_base = base_addr + NCR_Q720_CHIP_REGISTER_OFFSET
  235. + i*NCR_Q720_SIOP_SHIFT;
  236. __u16 port = io_base + NCR_Q720_CHIP_REGISTER_OFFSET
  237. + i*NCR_Q720_SIOP_SHIFT;
  238. int err;
  239. outb(0xff, port + 0x40);
  240. outb(0x07, port + 0x41);
  241. if ((err = NCR_Q720_probe_one(p, i, irq, slot,
  242. siop_p_base, siop_v_base)) != 0)
  243. printk("Q720: SIOP%d: probe failed, error = %d\n",
  244. i, err);
  245. else
  246. found++;
  247. }
  248. if (!found) {
  249. kfree(p);
  250. return -ENODEV;
  251. }
  252. mca_device_set_claim(mca_dev, 1);
  253. mca_device_set_name(mca_dev, "NCR_Q720");
  254. dev_set_drvdata(dev, p);
  255. return 0;
  256. out_release:
  257. dma_release_declared_memory(dev);
  258. out_release_region:
  259. release_mem_region(base_addr, mem_size);
  260. out_free:
  261. kfree(p);
  262. return -ENODEV;
  263. }
  264. static void __exit
  265. NCR_Q720_remove_one(struct Scsi_Host *host)
  266. {
  267. scsi_remove_host(host);
  268. ncr53c8xx_release(host);
  269. }
  270. static int __exit
  271. NCR_Q720_remove(struct device *dev)
  272. {
  273. struct NCR_Q720_private *p = dev_get_drvdata(dev);
  274. int i;
  275. for (i = 0; i < p->siops; i++)
  276. if(p->hosts[i])
  277. NCR_Q720_remove_one(p->hosts[i]);
  278. dma_release_declared_memory(dev);
  279. release_mem_region(p->phys_mem_base, p->mem_size);
  280. free_irq(p->irq, p);
  281. kfree(p);
  282. return 0;
  283. }
  284. static short NCR_Q720_id_table[] = { NCR_Q720_MCA_ID, 0 };
  285. static struct mca_driver NCR_Q720_driver = {
  286. .id_table = NCR_Q720_id_table,
  287. .driver = {
  288. .name = "NCR_Q720",
  289. .bus = &mca_bus_type,
  290. .probe = NCR_Q720_probe,
  291. .remove = NCR_Q720_remove,
  292. },
  293. };
  294. static int __init
  295. NCR_Q720_init(void)
  296. {
  297. int ret = ncr53c8xx_init();
  298. if (!ret)
  299. ret = mca_register_driver(&NCR_Q720_driver);
  300. if (ret)
  301. ncr53c8xx_exit();
  302. return ret;
  303. }
  304. static void __exit
  305. NCR_Q720_exit(void)
  306. {
  307. mca_unregister_driver(&NCR_Q720_driver);
  308. ncr53c8xx_exit();
  309. }
  310. module_init(NCR_Q720_init);
  311. module_exit(NCR_Q720_exit);