cumana_2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 && strcmp(buffer, "CUMANASCSI2") == 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. /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
  292. * int length, int host_no, int inout)
  293. * Purpose : Return information about the driver to a user process accessing
  294. * the /proc filesystem.
  295. * Params : buffer - a buffer to write information to
  296. * start - a pointer into this buffer set by this routine to the start
  297. * of the required information.
  298. * offset - offset into information that we have read upto.
  299. * length - length of buffer
  300. * host_no - host number to return information for
  301. * inout - 0 for reading, 1 for writing.
  302. * Returns : length of data written to buffer.
  303. */
  304. int cumanascsi_2_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  305. int length, int inout)
  306. {
  307. struct cumanascsi2_info *info;
  308. char *p = buffer;
  309. int pos;
  310. if (inout == 1)
  311. return cumanascsi_2_set_proc_info(host, buffer, length);
  312. info = (struct cumanascsi2_info *)host->hostdata;
  313. p += sprintf(p, "Cumana SCSI II driver v%s\n", VERSION);
  314. p += fas216_print_host(&info->info, p);
  315. p += sprintf(p, "Term : o%s\n",
  316. info->terms ? "n" : "ff");
  317. p += fas216_print_stats(&info->info, p);
  318. p += fas216_print_devices(&info->info, p);
  319. *start = buffer + offset;
  320. pos = p - buffer - offset;
  321. if (pos > length)
  322. pos = length;
  323. return pos;
  324. }
  325. static struct scsi_host_template cumanascsi2_template = {
  326. .module = THIS_MODULE,
  327. .proc_info = cumanascsi_2_proc_info,
  328. .name = "Cumana SCSI II",
  329. .info = cumanascsi_2_info,
  330. .queuecommand = fas216_queue_command,
  331. .eh_host_reset_handler = fas216_eh_host_reset,
  332. .eh_bus_reset_handler = fas216_eh_bus_reset,
  333. .eh_device_reset_handler = fas216_eh_device_reset,
  334. .eh_abort_handler = fas216_eh_abort,
  335. .can_queue = 1,
  336. .this_id = 7,
  337. .sg_tablesize = SG_ALL,
  338. .cmd_per_lun = 1,
  339. .use_clustering = DISABLE_CLUSTERING,
  340. .proc_name = "cumanascsi2",
  341. };
  342. static int __devinit
  343. cumanascsi2_probe(struct expansion_card *ec, const struct ecard_id *id)
  344. {
  345. struct Scsi_Host *host;
  346. struct cumanascsi2_info *info;
  347. void __iomem *base;
  348. int ret;
  349. ret = ecard_request_resources(ec);
  350. if (ret)
  351. goto out;
  352. base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
  353. if (!base) {
  354. ret = -ENOMEM;
  355. goto out_region;
  356. }
  357. host = scsi_host_alloc(&cumanascsi2_template,
  358. sizeof(struct cumanascsi2_info));
  359. if (!host) {
  360. ret = -ENOMEM;
  361. goto out_region;
  362. }
  363. ecard_set_drvdata(ec, host);
  364. info = (struct cumanascsi2_info *)host->hostdata;
  365. info->ec = ec;
  366. info->base = base;
  367. cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
  368. info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
  369. info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
  370. info->info.scsi.irq = ec->irq;
  371. info->info.scsi.dma = ec->dma;
  372. info->info.ifcfg.clockrate = 40; /* MHz */
  373. info->info.ifcfg.select_timeout = 255;
  374. info->info.ifcfg.asyncperiod = 200; /* ns */
  375. info->info.ifcfg.sync_max_depth = 7;
  376. info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
  377. info->info.ifcfg.disconnect_ok = 1;
  378. info->info.ifcfg.wide_max_size = 0;
  379. info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
  380. info->info.dma.setup = cumanascsi_2_dma_setup;
  381. info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
  382. info->info.dma.stop = cumanascsi_2_dma_stop;
  383. ec->irqaddr = info->base + CUMANASCSI2_STATUS;
  384. ec->irqmask = STATUS_INT;
  385. ecard_setirq(ec, &cumanascsi_2_ops, info);
  386. ret = fas216_init(host);
  387. if (ret)
  388. goto out_free;
  389. ret = request_irq(ec->irq, cumanascsi_2_intr,
  390. IRQF_DISABLED, "cumanascsi2", info);
  391. if (ret) {
  392. printk("scsi%d: IRQ%d not free: %d\n",
  393. host->host_no, ec->irq, ret);
  394. goto out_release;
  395. }
  396. if (info->info.scsi.dma != NO_DMA) {
  397. if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
  398. printk("scsi%d: DMA%d not free, using PIO\n",
  399. host->host_no, info->info.scsi.dma);
  400. info->info.scsi.dma = NO_DMA;
  401. } else {
  402. set_dma_speed(info->info.scsi.dma, 180);
  403. info->info.ifcfg.capabilities |= FASCAP_DMA;
  404. }
  405. }
  406. ret = fas216_add(host, &ec->dev);
  407. if (ret == 0)
  408. goto out;
  409. if (info->info.scsi.dma != NO_DMA)
  410. free_dma(info->info.scsi.dma);
  411. free_irq(ec->irq, host);
  412. out_release:
  413. fas216_release(host);
  414. out_free:
  415. scsi_host_put(host);
  416. out_region:
  417. ecard_release_resources(ec);
  418. out:
  419. return ret;
  420. }
  421. static void __devexit cumanascsi2_remove(struct expansion_card *ec)
  422. {
  423. struct Scsi_Host *host = ecard_get_drvdata(ec);
  424. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  425. ecard_set_drvdata(ec, NULL);
  426. fas216_remove(host);
  427. if (info->info.scsi.dma != NO_DMA)
  428. free_dma(info->info.scsi.dma);
  429. free_irq(ec->irq, info);
  430. fas216_release(host);
  431. scsi_host_put(host);
  432. ecard_release_resources(ec);
  433. }
  434. static const struct ecard_id cumanascsi2_cids[] = {
  435. { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
  436. { 0xffff, 0xffff },
  437. };
  438. static struct ecard_driver cumanascsi2_driver = {
  439. .probe = cumanascsi2_probe,
  440. .remove = __devexit_p(cumanascsi2_remove),
  441. .id_table = cumanascsi2_cids,
  442. .drv = {
  443. .name = "cumanascsi2",
  444. },
  445. };
  446. static int __init cumanascsi2_init(void)
  447. {
  448. return ecard_register_driver(&cumanascsi2_driver);
  449. }
  450. static void __exit cumanascsi2_exit(void)
  451. {
  452. ecard_remove_driver(&cumanascsi2_driver);
  453. }
  454. module_init(cumanascsi2_init);
  455. module_exit(cumanascsi2_exit);
  456. MODULE_AUTHOR("Russell King");
  457. MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
  458. module_param_array(term, int, NULL, 0);
  459. MODULE_PARM_DESC(term, "SCSI bus termination");
  460. MODULE_LICENSE("GPL");