powertec.c 12 KB

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