cumana_2.c 14 KB

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