cumana_2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * linux/drivers/acorn/scsi/cumana_2.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. * Changelog:
  11. * 30-08-1997 RMK 0.0.0 Created, READONLY version.
  12. * 22-01-1998 RMK 0.0.1 Updated to 2.1.80.
  13. * 15-04-1998 RMK 0.0.1 Only do PIO if FAS216 will allow it.
  14. * 02-05-1998 RMK 0.0.2 Updated & added DMA support.
  15. * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
  16. * 18-08-1998 RMK 0.0.3 Fixed synchronous transfer depth.
  17. * 02-04-2000 RMK 0.0.4 Updated for new error handling code.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/ioport.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/init.h>
  28. #include <linux/dma-mapping.h>
  29. #include <asm/dma.h>
  30. #include <asm/ecard.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include "../scsi.h"
  34. #include <scsi/scsi_host.h>
  35. #include "fas216.h"
  36. #include "scsi.h"
  37. #include <scsi/scsicam.h>
  38. #define CUMANASCSI2_STATUS (0x0000)
  39. #define STATUS_INT (1 << 0)
  40. #define STATUS_DRQ (1 << 1)
  41. #define STATUS_LATCHED (1 << 3)
  42. #define CUMANASCSI2_ALATCH (0x0014)
  43. #define ALATCH_ENA_INT (3)
  44. #define ALATCH_DIS_INT (2)
  45. #define ALATCH_ENA_TERM (5)
  46. #define ALATCH_DIS_TERM (4)
  47. #define ALATCH_ENA_BIT32 (11)
  48. #define ALATCH_DIS_BIT32 (10)
  49. #define ALATCH_ENA_DMA (13)
  50. #define ALATCH_DIS_DMA (12)
  51. #define ALATCH_DMA_OUT (15)
  52. #define ALATCH_DMA_IN (14)
  53. #define CUMANASCSI2_PSEUDODMA (0x0200)
  54. #define CUMANASCSI2_FAS216_OFFSET (0x0300)
  55. #define CUMANASCSI2_FAS216_SHIFT 2
  56. /*
  57. * Version
  58. */
  59. #define VERSION "1.00 (13/11/2002 2.5.47)"
  60. /*
  61. * Use term=0,1,0,0,0 to turn terminators on/off
  62. */
  63. static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
  64. #define NR_SG 256
  65. struct cumanascsi2_info {
  66. FAS216_Info info;
  67. struct expansion_card *ec;
  68. void __iomem *base;
  69. unsigned int terms; /* Terminator state */
  70. struct scatterlist sg[NR_SG]; /* Scatter DMA list */
  71. };
  72. #define CSTATUS_IRQ (1 << 0)
  73. #define CSTATUS_DRQ (1 << 1)
  74. /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
  75. * Purpose : Enable interrupts on Cumana SCSI 2 card
  76. * Params : ec - expansion card structure
  77. * : irqnr - interrupt number
  78. */
  79. static void
  80. cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
  81. {
  82. struct cumanascsi2_info *info = ec->irq_data;
  83. writeb(ALATCH_ENA_INT, info->base + CUMANASCSI2_ALATCH);
  84. }
  85. /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
  86. * Purpose : Disable interrupts on Cumana SCSI 2 card
  87. * Params : ec - expansion card structure
  88. * : irqnr - interrupt number
  89. */
  90. static void
  91. cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
  92. {
  93. struct cumanascsi2_info *info = ec->irq_data;
  94. writeb(ALATCH_DIS_INT, info->base + CUMANASCSI2_ALATCH);
  95. }
  96. static const expansioncard_ops_t cumanascsi_2_ops = {
  97. .irqenable = cumanascsi_2_irqenable,
  98. .irqdisable = cumanascsi_2_irqdisable,
  99. };
  100. /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
  101. * Purpose : Turn the Cumana SCSI 2 terminators on or off
  102. * Params : host - card to turn on/off
  103. * : on_off - !0 to turn on, 0 to turn off
  104. */
  105. static void
  106. cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
  107. {
  108. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  109. if (on_off) {
  110. info->terms = 1;
  111. writeb(ALATCH_ENA_TERM, info->base + CUMANASCSI2_ALATCH);
  112. } else {
  113. info->terms = 0;
  114. writeb(ALATCH_DIS_TERM, info->base + CUMANASCSI2_ALATCH);
  115. }
  116. }
  117. /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
  118. * Purpose : handle interrupts from Cumana SCSI 2 card
  119. * Params : irq - interrupt number
  120. * dev_id - user-defined (Scsi_Host structure)
  121. */
  122. static irqreturn_t
  123. cumanascsi_2_intr(int irq, void *dev_id)
  124. {
  125. struct cumanascsi2_info *info = dev_id;
  126. return fas216_intr(&info->info);
  127. }
  128. /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
  129. * Purpose : initialises DMA/PIO
  130. * Params : host - host
  131. * SCpnt - command
  132. * direction - DMA on to/off of card
  133. * min_type - minimum DMA support that we must have for this transfer
  134. * Returns : type of transfer to be performed
  135. */
  136. static fasdmatype_t
  137. cumanascsi_2_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
  138. fasdmadir_t direction, fasdmatype_t min_type)
  139. {
  140. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  141. struct device *dev = scsi_get_device(host);
  142. int dmach = info->info.scsi.dma;
  143. writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
  144. if (dmach != NO_DMA &&
  145. (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
  146. int bufs, map_dir, dma_dir, alatch_dir;
  147. bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
  148. if (direction == DMA_OUT)
  149. map_dir = DMA_TO_DEVICE,
  150. dma_dir = DMA_MODE_WRITE,
  151. alatch_dir = ALATCH_DMA_OUT;
  152. else
  153. map_dir = DMA_FROM_DEVICE,
  154. dma_dir = DMA_MODE_READ,
  155. alatch_dir = ALATCH_DMA_IN;
  156. dma_map_sg(dev, info->sg, bufs, map_dir);
  157. disable_dma(dmach);
  158. set_dma_sg(dmach, info->sg, bufs);
  159. writeb(alatch_dir, info->base + CUMANASCSI2_ALATCH);
  160. set_dma_mode(dmach, dma_dir);
  161. enable_dma(dmach);
  162. writeb(ALATCH_ENA_DMA, info->base + CUMANASCSI2_ALATCH);
  163. writeb(ALATCH_DIS_BIT32, info->base + CUMANASCSI2_ALATCH);
  164. return fasdma_real_all;
  165. }
  166. /*
  167. * If we're not doing DMA,
  168. * we'll do pseudo DMA
  169. */
  170. return fasdma_pio;
  171. }
  172. /*
  173. * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
  174. * Purpose : handles pseudo DMA
  175. * Params : host - host
  176. * SCpnt - command
  177. * direction - DMA on to/off of card
  178. * transfer - minimum number of bytes we expect to transfer
  179. */
  180. static void
  181. cumanascsi_2_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
  182. fasdmadir_t direction, int transfer)
  183. {
  184. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  185. unsigned int length;
  186. unsigned char *addr;
  187. length = SCp->this_residual;
  188. addr = SCp->ptr;
  189. if (direction == DMA_OUT)
  190. #if 0
  191. while (length > 1) {
  192. unsigned long word;
  193. unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
  194. if (status & STATUS_INT)
  195. goto end;
  196. if (!(status & STATUS_DRQ))
  197. continue;
  198. word = *addr | *(addr + 1) << 8;
  199. writew(word, info->base + CUMANASCSI2_PSEUDODMA);
  200. addr += 2;
  201. length -= 2;
  202. }
  203. #else
  204. printk ("PSEUDO_OUT???\n");
  205. #endif
  206. else {
  207. if (transfer && (transfer & 255)) {
  208. while (length >= 256) {
  209. unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
  210. if (status & STATUS_INT)
  211. return;
  212. if (!(status & STATUS_DRQ))
  213. continue;
  214. readsw(info->base + CUMANASCSI2_PSEUDODMA,
  215. addr, 256 >> 1);
  216. addr += 256;
  217. length -= 256;
  218. }
  219. }
  220. while (length > 0) {
  221. unsigned long word;
  222. unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
  223. if (status & STATUS_INT)
  224. return;
  225. if (!(status & STATUS_DRQ))
  226. continue;
  227. word = readw(info->base + CUMANASCSI2_PSEUDODMA);
  228. *addr++ = word;
  229. if (--length > 0) {
  230. *addr++ = word >> 8;
  231. length --;
  232. }
  233. }
  234. }
  235. }
  236. /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
  237. * Purpose : stops DMA/PIO
  238. * Params : host - host
  239. * SCpnt - command
  240. */
  241. static void
  242. cumanascsi_2_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
  243. {
  244. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  245. if (info->info.scsi.dma != NO_DMA) {
  246. writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
  247. disable_dma(info->info.scsi.dma);
  248. }
  249. }
  250. /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
  251. * Purpose : returns a descriptive string about this interface,
  252. * Params : host - driver host structure to return info for.
  253. * Returns : pointer to a static buffer containing null terminated string.
  254. */
  255. const char *cumanascsi_2_info(struct Scsi_Host *host)
  256. {
  257. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  258. static char string[150];
  259. sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
  260. host->hostt->name, info->info.scsi.type, info->ec->slot_no,
  261. VERSION, info->terms ? "n" : "ff");
  262. return string;
  263. }
  264. /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
  265. * Purpose : Set a driver specific function
  266. * Params : host - host to setup
  267. * : buffer - buffer containing string describing operation
  268. * : length - length of string
  269. * Returns : -EINVAL, or 0
  270. */
  271. static int
  272. cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
  273. {
  274. int ret = length;
  275. if (length >= 11 && strncmp(buffer, "CUMANASCSI2", 11) == 0) {
  276. buffer += 11;
  277. length -= 11;
  278. if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
  279. if (buffer[5] == '1')
  280. cumanascsi_2_terminator_ctl(host, 1);
  281. else if (buffer[5] == '0')
  282. cumanascsi_2_terminator_ctl(host, 0);
  283. else
  284. ret = -EINVAL;
  285. } else
  286. ret = -EINVAL;
  287. } else
  288. ret = -EINVAL;
  289. return ret;
  290. }
  291. static int cumanascsi_2_show_info(struct seq_file *m, struct Scsi_Host *host)
  292. {
  293. struct cumanascsi2_info *info;
  294. info = (struct cumanascsi2_info *)host->hostdata;
  295. seq_printf(m, "Cumana SCSI II driver v%s\n", VERSION);
  296. fas216_print_host(&info->info, m);
  297. seq_printf(m, "Term : o%s\n",
  298. info->terms ? "n" : "ff");
  299. fas216_print_stats(&info->info, m);
  300. fas216_print_devices(&info->info, m);
  301. return 0;
  302. }
  303. static struct scsi_host_template cumanascsi2_template = {
  304. .module = THIS_MODULE,
  305. .show_info = cumanascsi_2_show_info,
  306. .write_info = cumanascsi_2_set_proc_info,
  307. .name = "Cumana SCSI II",
  308. .info = cumanascsi_2_info,
  309. .queuecommand = fas216_queue_command,
  310. .eh_host_reset_handler = fas216_eh_host_reset,
  311. .eh_bus_reset_handler = fas216_eh_bus_reset,
  312. .eh_device_reset_handler = fas216_eh_device_reset,
  313. .eh_abort_handler = fas216_eh_abort,
  314. .can_queue = 1,
  315. .this_id = 7,
  316. .sg_tablesize = SCSI_MAX_SG_CHAIN_SEGMENTS,
  317. .dma_boundary = IOMD_DMA_BOUNDARY,
  318. .use_clustering = DISABLE_CLUSTERING,
  319. .proc_name = "cumanascsi2",
  320. };
  321. static int cumanascsi2_probe(struct expansion_card *ec,
  322. const struct ecard_id *id)
  323. {
  324. struct Scsi_Host *host;
  325. struct cumanascsi2_info *info;
  326. void __iomem *base;
  327. int ret;
  328. ret = ecard_request_resources(ec);
  329. if (ret)
  330. goto out;
  331. base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
  332. if (!base) {
  333. ret = -ENOMEM;
  334. goto out_region;
  335. }
  336. host = scsi_host_alloc(&cumanascsi2_template,
  337. sizeof(struct cumanascsi2_info));
  338. if (!host) {
  339. ret = -ENOMEM;
  340. goto out_region;
  341. }
  342. ecard_set_drvdata(ec, host);
  343. info = (struct cumanascsi2_info *)host->hostdata;
  344. info->ec = ec;
  345. info->base = base;
  346. cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
  347. info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
  348. info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
  349. info->info.scsi.irq = ec->irq;
  350. info->info.scsi.dma = ec->dma;
  351. info->info.ifcfg.clockrate = 40; /* MHz */
  352. info->info.ifcfg.select_timeout = 255;
  353. info->info.ifcfg.asyncperiod = 200; /* ns */
  354. info->info.ifcfg.sync_max_depth = 7;
  355. info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
  356. info->info.ifcfg.disconnect_ok = 1;
  357. info->info.ifcfg.wide_max_size = 0;
  358. info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
  359. info->info.dma.setup = cumanascsi_2_dma_setup;
  360. info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
  361. info->info.dma.stop = cumanascsi_2_dma_stop;
  362. ec->irqaddr = info->base + CUMANASCSI2_STATUS;
  363. ec->irqmask = STATUS_INT;
  364. ecard_setirq(ec, &cumanascsi_2_ops, info);
  365. ret = fas216_init(host);
  366. if (ret)
  367. goto out_free;
  368. ret = request_irq(ec->irq, cumanascsi_2_intr,
  369. 0, "cumanascsi2", info);
  370. if (ret) {
  371. printk("scsi%d: IRQ%d not free: %d\n",
  372. host->host_no, ec->irq, ret);
  373. goto out_release;
  374. }
  375. if (info->info.scsi.dma != NO_DMA) {
  376. if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
  377. printk("scsi%d: DMA%d not free, using PIO\n",
  378. host->host_no, info->info.scsi.dma);
  379. info->info.scsi.dma = NO_DMA;
  380. } else {
  381. set_dma_speed(info->info.scsi.dma, 180);
  382. info->info.ifcfg.capabilities |= FASCAP_DMA;
  383. }
  384. }
  385. ret = fas216_add(host, &ec->dev);
  386. if (ret == 0)
  387. goto out;
  388. if (info->info.scsi.dma != NO_DMA)
  389. free_dma(info->info.scsi.dma);
  390. free_irq(ec->irq, host);
  391. out_release:
  392. fas216_release(host);
  393. out_free:
  394. scsi_host_put(host);
  395. out_region:
  396. ecard_release_resources(ec);
  397. out:
  398. return ret;
  399. }
  400. static void cumanascsi2_remove(struct expansion_card *ec)
  401. {
  402. struct Scsi_Host *host = ecard_get_drvdata(ec);
  403. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  404. ecard_set_drvdata(ec, NULL);
  405. fas216_remove(host);
  406. if (info->info.scsi.dma != NO_DMA)
  407. free_dma(info->info.scsi.dma);
  408. free_irq(ec->irq, info);
  409. fas216_release(host);
  410. scsi_host_put(host);
  411. ecard_release_resources(ec);
  412. }
  413. static const struct ecard_id cumanascsi2_cids[] = {
  414. { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
  415. { 0xffff, 0xffff },
  416. };
  417. static struct ecard_driver cumanascsi2_driver = {
  418. .probe = cumanascsi2_probe,
  419. .remove = cumanascsi2_remove,
  420. .id_table = cumanascsi2_cids,
  421. .drv = {
  422. .name = "cumanascsi2",
  423. },
  424. };
  425. static int __init cumanascsi2_init(void)
  426. {
  427. return ecard_register_driver(&cumanascsi2_driver);
  428. }
  429. static void __exit cumanascsi2_exit(void)
  430. {
  431. ecard_remove_driver(&cumanascsi2_driver);
  432. }
  433. module_init(cumanascsi2_init);
  434. module_exit(cumanascsi2_exit);
  435. MODULE_AUTHOR("Russell King");
  436. MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
  437. module_param_array(term, int, NULL, 0);
  438. MODULE_PARM_DESC(term, "SCSI bus termination");
  439. MODULE_LICENSE("GPL");