cumana_2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/acorn/scsi/cumana_2.c
  4. *
  5. * Copyright (C) 1997-2005 Russell King
  6. *
  7. * Changelog:
  8. * 30-08-1997 RMK 0.0.0 Created, READONLY version.
  9. * 22-01-1998 RMK 0.0.1 Updated to 2.1.80.
  10. * 15-04-1998 RMK 0.0.1 Only do PIO if FAS216 will allow it.
  11. * 02-05-1998 RMK 0.0.2 Updated & added DMA support.
  12. * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
  13. * 18-08-1998 RMK 0.0.3 Fixed synchronous transfer depth.
  14. * 02-04-2000 RMK 0.0.4 Updated for new error handling code.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/ioport.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pgtable.h>
  27. #include <asm/dma.h>
  28. #include <asm/ecard.h>
  29. #include <asm/io.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_eh.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include "fas216.h"
  37. #include "arm_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. }
  158. dma_map_sg(dev, info->sg, bufs, map_dir);
  159. disable_dma(dmach);
  160. set_dma_sg(dmach, info->sg, bufs);
  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 && strncmp(buffer, "CUMANASCSI2", 11) == 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. }
  290. } else {
  291. ret = -EINVAL;
  292. }
  293. return ret;
  294. }
  295. static int cumanascsi_2_show_info(struct seq_file *m, struct Scsi_Host *host)
  296. {
  297. struct cumanascsi2_info *info;
  298. info = (struct cumanascsi2_info *)host->hostdata;
  299. seq_printf(m, "Cumana SCSI II driver v%s\n", VERSION);
  300. fas216_print_host(&info->info, m);
  301. seq_printf(m, "Term : o%s\n",
  302. info->terms ? "n" : "ff");
  303. fas216_print_stats(&info->info, m);
  304. fas216_print_devices(&info->info, m);
  305. return 0;
  306. }
  307. static struct scsi_host_template cumanascsi2_template = {
  308. .module = THIS_MODULE,
  309. .show_info = cumanascsi_2_show_info,
  310. .write_info = cumanascsi_2_set_proc_info,
  311. .name = "Cumana SCSI II",
  312. .info = cumanascsi_2_info,
  313. .queuecommand = fas216_queue_command,
  314. .eh_host_reset_handler = fas216_eh_host_reset,
  315. .eh_bus_reset_handler = fas216_eh_bus_reset,
  316. .eh_device_reset_handler = fas216_eh_device_reset,
  317. .eh_abort_handler = fas216_eh_abort,
  318. .cmd_size = sizeof(struct fas216_cmd_priv),
  319. .can_queue = 1,
  320. .this_id = 7,
  321. .sg_tablesize = SG_MAX_SEGMENTS,
  322. .dma_boundary = IOMD_DMA_BOUNDARY,
  323. .proc_name = "cumanascsi2",
  324. };
  325. static int cumanascsi2_probe(struct expansion_card *ec,
  326. const struct ecard_id *id)
  327. {
  328. struct Scsi_Host *host;
  329. struct cumanascsi2_info *info;
  330. void __iomem *base;
  331. int ret;
  332. ret = ecard_request_resources(ec);
  333. if (ret)
  334. goto out;
  335. base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
  336. if (!base) {
  337. ret = -ENOMEM;
  338. goto out_region;
  339. }
  340. host = scsi_host_alloc(&cumanascsi2_template,
  341. sizeof(struct cumanascsi2_info));
  342. if (!host) {
  343. ret = -ENOMEM;
  344. goto out_region;
  345. }
  346. ecard_set_drvdata(ec, host);
  347. info = (struct cumanascsi2_info *)host->hostdata;
  348. info->ec = ec;
  349. info->base = base;
  350. cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
  351. info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
  352. info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
  353. info->info.scsi.irq = ec->irq;
  354. info->info.scsi.dma = ec->dma;
  355. info->info.ifcfg.clockrate = 40; /* MHz */
  356. info->info.ifcfg.select_timeout = 255;
  357. info->info.ifcfg.asyncperiod = 200; /* ns */
  358. info->info.ifcfg.sync_max_depth = 7;
  359. info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
  360. info->info.ifcfg.disconnect_ok = 1;
  361. info->info.ifcfg.wide_max_size = 0;
  362. info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
  363. info->info.dma.setup = cumanascsi_2_dma_setup;
  364. info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
  365. info->info.dma.stop = cumanascsi_2_dma_stop;
  366. ec->irqaddr = info->base + CUMANASCSI2_STATUS;
  367. ec->irqmask = STATUS_INT;
  368. ecard_setirq(ec, &cumanascsi_2_ops, info);
  369. ret = fas216_init(host);
  370. if (ret)
  371. goto out_free;
  372. ret = request_irq(ec->irq, cumanascsi_2_intr,
  373. 0, "cumanascsi2", info);
  374. if (ret) {
  375. printk("scsi%d: IRQ%d not free: %d\n",
  376. host->host_no, ec->irq, ret);
  377. goto out_release;
  378. }
  379. if (info->info.scsi.dma != NO_DMA) {
  380. if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
  381. printk("scsi%d: DMA%d not free, using PIO\n",
  382. host->host_no, info->info.scsi.dma);
  383. info->info.scsi.dma = NO_DMA;
  384. } else {
  385. set_dma_speed(info->info.scsi.dma, 180);
  386. info->info.ifcfg.capabilities |= FASCAP_DMA;
  387. }
  388. }
  389. ret = fas216_add(host, &ec->dev);
  390. if (ret == 0)
  391. goto out;
  392. if (info->info.scsi.dma != NO_DMA)
  393. free_dma(info->info.scsi.dma);
  394. free_irq(ec->irq, info);
  395. out_release:
  396. fas216_release(host);
  397. out_free:
  398. scsi_host_put(host);
  399. out_region:
  400. ecard_release_resources(ec);
  401. out:
  402. return ret;
  403. }
  404. static void cumanascsi2_remove(struct expansion_card *ec)
  405. {
  406. struct Scsi_Host *host = ecard_get_drvdata(ec);
  407. struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
  408. ecard_set_drvdata(ec, NULL);
  409. fas216_remove(host);
  410. if (info->info.scsi.dma != NO_DMA)
  411. free_dma(info->info.scsi.dma);
  412. free_irq(ec->irq, info);
  413. fas216_release(host);
  414. scsi_host_put(host);
  415. ecard_release_resources(ec);
  416. }
  417. static const struct ecard_id cumanascsi2_cids[] = {
  418. { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
  419. { 0xffff, 0xffff },
  420. };
  421. static struct ecard_driver cumanascsi2_driver = {
  422. .probe = cumanascsi2_probe,
  423. .remove = cumanascsi2_remove,
  424. .id_table = cumanascsi2_cids,
  425. .drv = {
  426. .name = "cumanascsi2",
  427. },
  428. };
  429. static int __init cumanascsi2_init(void)
  430. {
  431. return ecard_register_driver(&cumanascsi2_driver);
  432. }
  433. static void __exit cumanascsi2_exit(void)
  434. {
  435. ecard_remove_driver(&cumanascsi2_driver);
  436. }
  437. module_init(cumanascsi2_init);
  438. module_exit(cumanascsi2_exit);
  439. MODULE_AUTHOR("Russell King");
  440. MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
  441. module_param_array(term, int, NULL, 0);
  442. MODULE_PARM_DESC(term, "SCSI bus termination");
  443. MODULE_LICENSE("GPL");