cumana_2.c 15 KB

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