cumana_2.c 15 KB

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