powertec.c 12 KB

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