powertec.c 12 KB

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