powertec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/drivers/acorn/scsi/powertec.c
  3. *
  4. * Copyright (C) 1997-2005 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/ioport.h>
  15. #include <linux/sched.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/init.h>
  20. #include <linux/dma-mapping.h>
  21. #include <asm/dma.h>
  22. #include <asm/ecard.h>
  23. #include <asm/io.h>
  24. #include <asm/pgtable.h>
  25. #include "../scsi.h"
  26. #include <scsi/scsi_host.h>
  27. #include "fas216.h"
  28. #include "scsi.h"
  29. #include <scsi/scsicam.h>
  30. #define POWERTEC_FAS216_OFFSET 0x3000
  31. #define POWERTEC_FAS216_SHIFT 6
  32. #define POWERTEC_INTR_STATUS 0x2000
  33. #define POWERTEC_INTR_BIT 0x80
  34. #define POWERTEC_RESET_CONTROL 0x1018
  35. #define POWERTEC_RESET_BIT 1
  36. #define POWERTEC_TERM_CONTROL 0x2018
  37. #define POWERTEC_TERM_ENABLE 1
  38. #define POWERTEC_INTR_CONTROL 0x101c
  39. #define POWERTEC_INTR_ENABLE 1
  40. #define POWERTEC_INTR_DISABLE 0
  41. #define VERSION "1.10 (19/01/2003 2.5.59)"
  42. /*
  43. * Use term=0,1,0,0,0 to turn terminators on/off.
  44. * One entry per slot.
  45. */
  46. static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
  47. #define NR_SG 256
  48. struct powertec_info {
  49. FAS216_Info info;
  50. struct expansion_card *ec;
  51. void __iomem *base;
  52. unsigned int term_ctl;
  53. struct scatterlist sg[NR_SG];
  54. };
  55. /* Prototype: void powertecscsi_irqenable(ec, irqnr)
  56. * Purpose : Enable interrupts on Powertec SCSI card
  57. * Params : ec - expansion card structure
  58. * : irqnr - interrupt number
  59. */
  60. static void
  61. powertecscsi_irqenable(struct expansion_card *ec, int irqnr)
  62. {
  63. struct powertec_info *info = ec->irq_data;
  64. writeb(POWERTEC_INTR_ENABLE, info->base + POWERTEC_INTR_CONTROL);
  65. }
  66. /* Prototype: void powertecscsi_irqdisable(ec, irqnr)
  67. * Purpose : Disable interrupts on Powertec SCSI card
  68. * Params : ec - expansion card structure
  69. * : irqnr - interrupt number
  70. */
  71. static void
  72. powertecscsi_irqdisable(struct expansion_card *ec, int irqnr)
  73. {
  74. struct powertec_info *info = ec->irq_data;
  75. writeb(POWERTEC_INTR_DISABLE, info->base + POWERTEC_INTR_CONTROL);
  76. }
  77. static const expansioncard_ops_t powertecscsi_ops = {
  78. .irqenable = powertecscsi_irqenable,
  79. .irqdisable = powertecscsi_irqdisable,
  80. };
  81. /* Prototype: void powertecscsi_terminator_ctl(host, on_off)
  82. * Purpose : Turn the Powertec SCSI terminators on or off
  83. * Params : host - card to turn on/off
  84. * : on_off - !0 to turn on, 0 to turn off
  85. */
  86. static void
  87. powertecscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
  88. {
  89. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  90. info->term_ctl = on_off ? POWERTEC_TERM_ENABLE : 0;
  91. writeb(info->term_ctl, info->base + POWERTEC_TERM_CONTROL);
  92. }
  93. /* Prototype: void powertecscsi_intr(irq, *dev_id, *regs)
  94. * Purpose : handle interrupts from Powertec SCSI card
  95. * Params : irq - interrupt number
  96. * dev_id - user-defined (Scsi_Host structure)
  97. * regs - processor registers at interrupt
  98. */
  99. static irqreturn_t
  100. powertecscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
  101. {
  102. struct powertec_info *info = dev_id;
  103. return fas216_intr(&info->info);
  104. }
  105. /* Prototype: fasdmatype_t powertecscsi_dma_setup(host, SCpnt, direction, min_type)
  106. * Purpose : initialises DMA/PIO
  107. * Params : host - host
  108. * SCpnt - command
  109. * direction - DMA on to/off of card
  110. * min_type - minimum DMA support that we must have for this transfer
  111. * Returns : type of transfer to be performed
  112. */
  113. static fasdmatype_t
  114. powertecscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
  115. fasdmadir_t direction, fasdmatype_t min_type)
  116. {
  117. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  118. struct device *dev = scsi_get_device(host);
  119. int dmach = info->info.scsi.dma;
  120. if (info->info.ifcfg.capabilities & FASCAP_DMA &&
  121. min_type == fasdma_real_all) {
  122. int bufs, map_dir, dma_dir;
  123. bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
  124. if (direction == DMA_OUT)
  125. map_dir = DMA_TO_DEVICE,
  126. dma_dir = DMA_MODE_WRITE;
  127. else
  128. map_dir = DMA_FROM_DEVICE,
  129. dma_dir = DMA_MODE_READ;
  130. dma_map_sg(dev, info->sg, bufs + 1, map_dir);
  131. disable_dma(dmach);
  132. set_dma_sg(dmach, info->sg, bufs + 1);
  133. set_dma_mode(dmach, dma_dir);
  134. enable_dma(dmach);
  135. return fasdma_real_all;
  136. }
  137. /*
  138. * If we're not doing DMA,
  139. * we'll do slow PIO
  140. */
  141. return fasdma_pio;
  142. }
  143. /* Prototype: int powertecscsi_dma_stop(host, SCpnt)
  144. * Purpose : stops DMA/PIO
  145. * Params : host - host
  146. * SCpnt - command
  147. */
  148. static void
  149. powertecscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
  150. {
  151. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  152. if (info->info.scsi.dma != NO_DMA)
  153. disable_dma(info->info.scsi.dma);
  154. }
  155. /* Prototype: const char *powertecscsi_info(struct Scsi_Host * host)
  156. * Purpose : returns a descriptive string about this interface,
  157. * Params : host - driver host structure to return info for.
  158. * Returns : pointer to a static buffer containing null terminated string.
  159. */
  160. const char *powertecscsi_info(struct Scsi_Host *host)
  161. {
  162. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  163. static char string[150];
  164. sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
  165. host->hostt->name, info->info.scsi.type, info->ec->slot_no,
  166. VERSION, info->term_ctl ? "n" : "ff");
  167. return string;
  168. }
  169. /* Prototype: int powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
  170. * Purpose : Set a driver specific function
  171. * Params : host - host to setup
  172. * : buffer - buffer containing string describing operation
  173. * : length - length of string
  174. * Returns : -EINVAL, or 0
  175. */
  176. static int
  177. powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
  178. {
  179. int ret = length;
  180. if (length >= 12 && strncmp(buffer, "POWERTECSCSI", 12) == 0) {
  181. buffer += 12;
  182. length -= 12;
  183. if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
  184. if (buffer[5] == '1')
  185. powertecscsi_terminator_ctl(host, 1);
  186. else if (buffer[5] == '0')
  187. powertecscsi_terminator_ctl(host, 0);
  188. else
  189. ret = -EINVAL;
  190. } else
  191. ret = -EINVAL;
  192. } else
  193. ret = -EINVAL;
  194. return ret;
  195. }
  196. /* Prototype: int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
  197. * int length, int host_no, int inout)
  198. * Purpose : Return information about the driver to a user process accessing
  199. * the /proc filesystem.
  200. * Params : buffer - a buffer to write information to
  201. * start - a pointer into this buffer set by this routine to the start
  202. * of the required information.
  203. * offset - offset into information that we have read upto.
  204. * length - length of buffer
  205. * inout - 0 for reading, 1 for writing.
  206. * Returns : length of data written to buffer.
  207. */
  208. int powertecscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  209. int length, int inout)
  210. {
  211. struct powertec_info *info;
  212. char *p = buffer;
  213. int pos;
  214. if (inout == 1)
  215. return powertecscsi_set_proc_info(host, buffer, length);
  216. info = (struct powertec_info *)host->hostdata;
  217. p += sprintf(p, "PowerTec SCSI driver v%s\n", VERSION);
  218. p += fas216_print_host(&info->info, p);
  219. p += sprintf(p, "Term : o%s\n",
  220. info->term_ctl ? "n" : "ff");
  221. p += fas216_print_stats(&info->info, p);
  222. p += fas216_print_devices(&info->info, p);
  223. *start = buffer + offset;
  224. pos = p - buffer - offset;
  225. if (pos > length)
  226. pos = length;
  227. return pos;
  228. }
  229. static ssize_t powertecscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
  230. {
  231. struct expansion_card *ec = ECARD_DEV(dev);
  232. struct Scsi_Host *host = ecard_get_drvdata(ec);
  233. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  234. return sprintf(buf, "%d\n", info->term_ctl ? 1 : 0);
  235. }
  236. static ssize_t
  237. powertecscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
  238. {
  239. struct expansion_card *ec = ECARD_DEV(dev);
  240. struct Scsi_Host *host = ecard_get_drvdata(ec);
  241. if (len > 1)
  242. powertecscsi_terminator_ctl(host, buf[0] != '0');
  243. return len;
  244. }
  245. static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
  246. powertecscsi_show_term, powertecscsi_store_term);
  247. static struct scsi_host_template powertecscsi_template = {
  248. .module = THIS_MODULE,
  249. .proc_info = powertecscsi_proc_info,
  250. .name = "PowerTec SCSI",
  251. .info = powertecscsi_info,
  252. .queuecommand = fas216_queue_command,
  253. .eh_host_reset_handler = fas216_eh_host_reset,
  254. .eh_bus_reset_handler = fas216_eh_bus_reset,
  255. .eh_device_reset_handler = fas216_eh_device_reset,
  256. .eh_abort_handler = fas216_eh_abort,
  257. .can_queue = 8,
  258. .this_id = 7,
  259. .sg_tablesize = SG_ALL,
  260. .cmd_per_lun = 2,
  261. .use_clustering = ENABLE_CLUSTERING,
  262. .proc_name = "powertec",
  263. };
  264. static int __devinit
  265. powertecscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
  266. {
  267. struct Scsi_Host *host;
  268. struct powertec_info *info;
  269. unsigned long resbase, reslen;
  270. void __iomem *base;
  271. int ret;
  272. ret = ecard_request_resources(ec);
  273. if (ret)
  274. goto out;
  275. resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
  276. reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
  277. base = ioremap(resbase, reslen);
  278. if (!base) {
  279. ret = -ENOMEM;
  280. goto out_region;
  281. }
  282. host = scsi_host_alloc(&powertecscsi_template,
  283. sizeof (struct powertec_info));
  284. if (!host) {
  285. ret = -ENOMEM;
  286. goto out_unmap;
  287. }
  288. ecard_set_drvdata(ec, host);
  289. info = (struct powertec_info *)host->hostdata;
  290. info->base = base;
  291. powertecscsi_terminator_ctl(host, term[ec->slot_no]);
  292. info->info.scsi.io_base = base + POWERTEC_FAS216_OFFSET;
  293. info->info.scsi.io_shift = POWERTEC_FAS216_SHIFT;
  294. info->info.scsi.irq = ec->irq;
  295. info->info.scsi.dma = ec->dma;
  296. info->info.ifcfg.clockrate = 40; /* MHz */
  297. info->info.ifcfg.select_timeout = 255;
  298. info->info.ifcfg.asyncperiod = 200; /* ns */
  299. info->info.ifcfg.sync_max_depth = 7;
  300. info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
  301. info->info.ifcfg.disconnect_ok = 1;
  302. info->info.ifcfg.wide_max_size = 0;
  303. info->info.ifcfg.capabilities = 0;
  304. info->info.dma.setup = powertecscsi_dma_setup;
  305. info->info.dma.pseudo = NULL;
  306. info->info.dma.stop = powertecscsi_dma_stop;
  307. ec->irqaddr = base + POWERTEC_INTR_STATUS;
  308. ec->irqmask = POWERTEC_INTR_BIT;
  309. ec->irq_data = info;
  310. ec->ops = &powertecscsi_ops;
  311. device_create_file(&ec->dev, &dev_attr_bus_term);
  312. ret = fas216_init(host);
  313. if (ret)
  314. goto out_free;
  315. ret = request_irq(ec->irq, powertecscsi_intr,
  316. SA_INTERRUPT, "powertec", info);
  317. if (ret) {
  318. printk("scsi%d: IRQ%d not free: %d\n",
  319. host->host_no, ec->irq, ret);
  320. goto out_release;
  321. }
  322. if (info->info.scsi.dma != NO_DMA) {
  323. if (request_dma(info->info.scsi.dma, "powertec")) {
  324. printk("scsi%d: DMA%d not free, using PIO\n",
  325. host->host_no, info->info.scsi.dma);
  326. info->info.scsi.dma = NO_DMA;
  327. } else {
  328. set_dma_speed(info->info.scsi.dma, 180);
  329. info->info.ifcfg.capabilities |= FASCAP_DMA;
  330. }
  331. }
  332. ret = fas216_add(host, &ec->dev);
  333. if (ret == 0)
  334. goto out;
  335. if (info->info.scsi.dma != NO_DMA)
  336. free_dma(info->info.scsi.dma);
  337. free_irq(ec->irq, host);
  338. out_release:
  339. fas216_release(host);
  340. out_free:
  341. device_remove_file(&ec->dev, &dev_attr_bus_term);
  342. scsi_host_put(host);
  343. out_unmap:
  344. iounmap(base);
  345. out_region:
  346. ecard_release_resources(ec);
  347. out:
  348. return ret;
  349. }
  350. static void __devexit powertecscsi_remove(struct expansion_card *ec)
  351. {
  352. struct Scsi_Host *host = ecard_get_drvdata(ec);
  353. struct powertec_info *info = (struct powertec_info *)host->hostdata;
  354. ecard_set_drvdata(ec, NULL);
  355. fas216_remove(host);
  356. device_remove_file(&ec->dev, &dev_attr_bus_term);
  357. if (info->info.scsi.dma != NO_DMA)
  358. free_dma(info->info.scsi.dma);
  359. free_irq(ec->irq, info);
  360. iounmap(info->base);
  361. fas216_release(host);
  362. scsi_host_put(host);
  363. ecard_release_resources(ec);
  364. }
  365. static const struct ecard_id powertecscsi_cids[] = {
  366. { MANU_ALSYSTEMS, PROD_ALSYS_SCSIATAPI },
  367. { 0xffff, 0xffff },
  368. };
  369. static struct ecard_driver powertecscsi_driver = {
  370. .probe = powertecscsi_probe,
  371. .remove = __devexit_p(powertecscsi_remove),
  372. .id_table = powertecscsi_cids,
  373. .drv = {
  374. .name = "powertecscsi",
  375. },
  376. };
  377. static int __init powertecscsi_init(void)
  378. {
  379. return ecard_register_driver(&powertecscsi_driver);
  380. }
  381. static void __exit powertecscsi_exit(void)
  382. {
  383. ecard_remove_driver(&powertecscsi_driver);
  384. }
  385. module_init(powertecscsi_init);
  386. module_exit(powertecscsi_exit);
  387. MODULE_AUTHOR("Russell King");
  388. MODULE_DESCRIPTION("Powertec SCSI driver");
  389. MODULE_PARM(term, "1-8i");
  390. MODULE_PARM_DESC(term, "SCSI bus termination");
  391. MODULE_LICENSE("GPL");