mac_scsi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Generic Macintosh NCR5380 driver
  3. *
  4. * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  5. *
  6. * derived in part from:
  7. */
  8. /*
  9. * Generic Generic NCR5380 driver
  10. *
  11. * Copyright 1995, Russell King
  12. */
  13. #include <linux/types.h>
  14. #include <linux/module.h>
  15. #include <linux/ioport.h>
  16. #include <linux/init.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/hwtest.h>
  21. #include <asm/io.h>
  22. #include <asm/macints.h>
  23. #include <asm/setup.h>
  24. #include <scsi/scsi_host.h>
  25. /* Definitions for the core NCR5380 driver. */
  26. #define NCR5380_implementation_fields unsigned char *pdma_base; \
  27. int pdma_residual
  28. #define NCR5380_read(reg) macscsi_read(instance, reg)
  29. #define NCR5380_write(reg, value) macscsi_write(instance, reg, value)
  30. #define NCR5380_dma_xfer_len(instance, cmd, phase) \
  31. macscsi_dma_xfer_len(instance, cmd)
  32. #define NCR5380_dma_recv_setup macscsi_pread
  33. #define NCR5380_dma_send_setup macscsi_pwrite
  34. #define NCR5380_dma_residual(instance) (hostdata->pdma_residual)
  35. #define NCR5380_intr macscsi_intr
  36. #define NCR5380_queue_command macscsi_queue_command
  37. #define NCR5380_abort macscsi_abort
  38. #define NCR5380_bus_reset macscsi_bus_reset
  39. #define NCR5380_info macscsi_info
  40. #include "NCR5380.h"
  41. static int setup_can_queue = -1;
  42. module_param(setup_can_queue, int, 0);
  43. static int setup_cmd_per_lun = -1;
  44. module_param(setup_cmd_per_lun, int, 0);
  45. static int setup_sg_tablesize = -1;
  46. module_param(setup_sg_tablesize, int, 0);
  47. static int setup_use_pdma = -1;
  48. module_param(setup_use_pdma, int, 0);
  49. static int setup_hostid = -1;
  50. module_param(setup_hostid, int, 0);
  51. static int setup_toshiba_delay = -1;
  52. module_param(setup_toshiba_delay, int, 0);
  53. /*
  54. * NCR 5380 register access functions
  55. */
  56. static inline char macscsi_read(struct Scsi_Host *instance, int reg)
  57. {
  58. return in_8(instance->base + (reg << 4));
  59. }
  60. static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
  61. {
  62. out_8(instance->base + (reg << 4), value);
  63. }
  64. #ifndef MODULE
  65. static int __init mac_scsi_setup(char *str)
  66. {
  67. int ints[8];
  68. (void)get_options(str, ARRAY_SIZE(ints), ints);
  69. if (ints[0] < 1) {
  70. pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
  71. return 0;
  72. }
  73. if (ints[0] >= 1)
  74. setup_can_queue = ints[1];
  75. if (ints[0] >= 2)
  76. setup_cmd_per_lun = ints[2];
  77. if (ints[0] >= 3)
  78. setup_sg_tablesize = ints[3];
  79. if (ints[0] >= 4)
  80. setup_hostid = ints[4];
  81. /* ints[5] (use_tagged_queuing) is ignored */
  82. if (ints[0] >= 6)
  83. setup_use_pdma = ints[6];
  84. if (ints[0] >= 7)
  85. setup_toshiba_delay = ints[7];
  86. return 1;
  87. }
  88. __setup("mac5380=", mac_scsi_setup);
  89. #endif /* !MODULE */
  90. /* Pseudo DMA asm originally by Ove Edlund */
  91. #define CP_IO_TO_MEM(s,d,n) \
  92. __asm__ __volatile__ \
  93. (" cmp.w #4,%2\n" \
  94. " bls 8f\n" \
  95. " move.w %1,%%d0\n" \
  96. " neg.b %%d0\n" \
  97. " and.w #3,%%d0\n" \
  98. " sub.w %%d0,%2\n" \
  99. " bra 2f\n" \
  100. " 1: move.b (%0),(%1)+\n" \
  101. " 2: dbf %%d0,1b\n" \
  102. " move.w %2,%%d0\n" \
  103. " lsr.w #5,%%d0\n" \
  104. " bra 4f\n" \
  105. " 3: move.l (%0),(%1)+\n" \
  106. "31: move.l (%0),(%1)+\n" \
  107. "32: move.l (%0),(%1)+\n" \
  108. "33: move.l (%0),(%1)+\n" \
  109. "34: move.l (%0),(%1)+\n" \
  110. "35: move.l (%0),(%1)+\n" \
  111. "36: move.l (%0),(%1)+\n" \
  112. "37: move.l (%0),(%1)+\n" \
  113. " 4: dbf %%d0,3b\n" \
  114. " move.w %2,%%d0\n" \
  115. " lsr.w #2,%%d0\n" \
  116. " and.w #7,%%d0\n" \
  117. " bra 6f\n" \
  118. " 5: move.l (%0),(%1)+\n" \
  119. " 6: dbf %%d0,5b\n" \
  120. " and.w #3,%2\n" \
  121. " bra 8f\n" \
  122. " 7: move.b (%0),(%1)+\n" \
  123. " 8: dbf %2,7b\n" \
  124. " moveq.l #0, %2\n" \
  125. " 9: \n" \
  126. ".section .fixup,\"ax\"\n" \
  127. " .even\n" \
  128. "91: moveq.l #1, %2\n" \
  129. " jra 9b\n" \
  130. "94: moveq.l #4, %2\n" \
  131. " jra 9b\n" \
  132. ".previous\n" \
  133. ".section __ex_table,\"a\"\n" \
  134. " .align 4\n" \
  135. " .long 1b,91b\n" \
  136. " .long 3b,94b\n" \
  137. " .long 31b,94b\n" \
  138. " .long 32b,94b\n" \
  139. " .long 33b,94b\n" \
  140. " .long 34b,94b\n" \
  141. " .long 35b,94b\n" \
  142. " .long 36b,94b\n" \
  143. " .long 37b,94b\n" \
  144. " .long 5b,94b\n" \
  145. " .long 7b,91b\n" \
  146. ".previous" \
  147. : "=a"(s), "=a"(d), "=d"(n) \
  148. : "0"(s), "1"(d), "2"(n) \
  149. : "d0")
  150. static int macscsi_pread(struct Scsi_Host *instance,
  151. unsigned char *dst, int len)
  152. {
  153. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  154. unsigned char *s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
  155. unsigned char *d = dst;
  156. int n = len;
  157. int transferred;
  158. while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
  159. BASR_DRQ | BASR_PHASE_MATCH,
  160. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  161. CP_IO_TO_MEM(s, d, n);
  162. transferred = d - dst - n;
  163. hostdata->pdma_residual = len - transferred;
  164. /* No bus error. */
  165. if (n == 0)
  166. return 0;
  167. /* Target changed phase early? */
  168. if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ,
  169. BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0)
  170. scmd_printk(KERN_ERR, hostdata->connected,
  171. "%s: !REQ and !ACK\n", __func__);
  172. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  173. return 0;
  174. dsprintk(NDEBUG_PSEUDO_DMA, instance,
  175. "%s: bus error (%d/%d)\n", __func__, transferred, len);
  176. NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
  177. d = dst + transferred;
  178. n = len - transferred;
  179. }
  180. scmd_printk(KERN_ERR, hostdata->connected,
  181. "%s: phase mismatch or !DRQ\n", __func__);
  182. NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
  183. return -1;
  184. }
  185. #define CP_MEM_TO_IO(s,d,n) \
  186. __asm__ __volatile__ \
  187. (" cmp.w #4,%2\n" \
  188. " bls 8f\n" \
  189. " move.w %0,%%d0\n" \
  190. " neg.b %%d0\n" \
  191. " and.w #3,%%d0\n" \
  192. " sub.w %%d0,%2\n" \
  193. " bra 2f\n" \
  194. " 1: move.b (%0)+,(%1)\n" \
  195. " 2: dbf %%d0,1b\n" \
  196. " move.w %2,%%d0\n" \
  197. " lsr.w #5,%%d0\n" \
  198. " bra 4f\n" \
  199. " 3: move.l (%0)+,(%1)\n" \
  200. "31: move.l (%0)+,(%1)\n" \
  201. "32: move.l (%0)+,(%1)\n" \
  202. "33: move.l (%0)+,(%1)\n" \
  203. "34: move.l (%0)+,(%1)\n" \
  204. "35: move.l (%0)+,(%1)\n" \
  205. "36: move.l (%0)+,(%1)\n" \
  206. "37: move.l (%0)+,(%1)\n" \
  207. " 4: dbf %%d0,3b\n" \
  208. " move.w %2,%%d0\n" \
  209. " lsr.w #2,%%d0\n" \
  210. " and.w #7,%%d0\n" \
  211. " bra 6f\n" \
  212. " 5: move.l (%0)+,(%1)\n" \
  213. " 6: dbf %%d0,5b\n" \
  214. " and.w #3,%2\n" \
  215. " bra 8f\n" \
  216. " 7: move.b (%0)+,(%1)\n" \
  217. " 8: dbf %2,7b\n" \
  218. " moveq.l #0, %2\n" \
  219. " 9: \n" \
  220. ".section .fixup,\"ax\"\n" \
  221. " .even\n" \
  222. "91: moveq.l #1, %2\n" \
  223. " jra 9b\n" \
  224. "94: moveq.l #4, %2\n" \
  225. " jra 9b\n" \
  226. ".previous\n" \
  227. ".section __ex_table,\"a\"\n" \
  228. " .align 4\n" \
  229. " .long 1b,91b\n" \
  230. " .long 3b,94b\n" \
  231. " .long 31b,94b\n" \
  232. " .long 32b,94b\n" \
  233. " .long 33b,94b\n" \
  234. " .long 34b,94b\n" \
  235. " .long 35b,94b\n" \
  236. " .long 36b,94b\n" \
  237. " .long 37b,94b\n" \
  238. " .long 5b,94b\n" \
  239. " .long 7b,91b\n" \
  240. ".previous" \
  241. : "=a"(s), "=a"(d), "=d"(n) \
  242. : "0"(s), "1"(d), "2"(n) \
  243. : "d0")
  244. static int macscsi_pwrite(struct Scsi_Host *instance,
  245. unsigned char *src, int len)
  246. {
  247. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  248. unsigned char *s = src;
  249. unsigned char *d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
  250. int n = len;
  251. int transferred;
  252. while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
  253. BASR_DRQ | BASR_PHASE_MATCH,
  254. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  255. CP_MEM_TO_IO(s, d, n);
  256. transferred = s - src - n;
  257. hostdata->pdma_residual = len - transferred;
  258. /* Target changed phase early? */
  259. if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ,
  260. BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0)
  261. scmd_printk(KERN_ERR, hostdata->connected,
  262. "%s: !REQ and !ACK\n", __func__);
  263. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  264. return 0;
  265. /* No bus error. */
  266. if (n == 0) {
  267. if (NCR5380_poll_politely(instance, TARGET_COMMAND_REG,
  268. TCR_LAST_BYTE_SENT,
  269. TCR_LAST_BYTE_SENT, HZ / 64) < 0)
  270. scmd_printk(KERN_ERR, hostdata->connected,
  271. "%s: Last Byte Sent timeout\n", __func__);
  272. return 0;
  273. }
  274. dsprintk(NDEBUG_PSEUDO_DMA, instance,
  275. "%s: bus error (%d/%d)\n", __func__, transferred, len);
  276. NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
  277. s = src + transferred;
  278. n = len - transferred;
  279. }
  280. scmd_printk(KERN_ERR, hostdata->connected,
  281. "%s: phase mismatch or !DRQ\n", __func__);
  282. NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance);
  283. return -1;
  284. }
  285. static int macscsi_dma_xfer_len(struct Scsi_Host *instance,
  286. struct scsi_cmnd *cmd)
  287. {
  288. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  289. if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
  290. cmd->SCp.this_residual < 16)
  291. return 0;
  292. return cmd->SCp.this_residual;
  293. }
  294. #include "NCR5380.c"
  295. #define DRV_MODULE_NAME "mac_scsi"
  296. #define PFX DRV_MODULE_NAME ": "
  297. static struct scsi_host_template mac_scsi_template = {
  298. .module = THIS_MODULE,
  299. .proc_name = DRV_MODULE_NAME,
  300. .name = "Macintosh NCR5380 SCSI",
  301. .info = macscsi_info,
  302. .queuecommand = macscsi_queue_command,
  303. .eh_abort_handler = macscsi_abort,
  304. .eh_bus_reset_handler = macscsi_bus_reset,
  305. .can_queue = 16,
  306. .this_id = 7,
  307. .sg_tablesize = 1,
  308. .cmd_per_lun = 2,
  309. .use_clustering = DISABLE_CLUSTERING,
  310. .cmd_size = NCR5380_CMD_SIZE,
  311. .max_sectors = 128,
  312. };
  313. static int __init mac_scsi_probe(struct platform_device *pdev)
  314. {
  315. struct Scsi_Host *instance;
  316. int error;
  317. int host_flags = 0;
  318. struct resource *irq, *pio_mem, *pdma_mem = NULL;
  319. pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  320. if (!pio_mem)
  321. return -ENODEV;
  322. pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  323. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  324. if (!hwreg_present((unsigned char *)pio_mem->start +
  325. (STATUS_REG << 4))) {
  326. pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
  327. return -ENODEV;
  328. }
  329. if (setup_can_queue > 0)
  330. mac_scsi_template.can_queue = setup_can_queue;
  331. if (setup_cmd_per_lun > 0)
  332. mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
  333. if (setup_sg_tablesize >= 0)
  334. mac_scsi_template.sg_tablesize = setup_sg_tablesize;
  335. if (setup_hostid >= 0)
  336. mac_scsi_template.this_id = setup_hostid & 7;
  337. instance = scsi_host_alloc(&mac_scsi_template,
  338. sizeof(struct NCR5380_hostdata));
  339. if (!instance)
  340. return -ENOMEM;
  341. instance->base = pio_mem->start;
  342. if (irq)
  343. instance->irq = irq->start;
  344. else
  345. instance->irq = NO_IRQ;
  346. if (pdma_mem && setup_use_pdma) {
  347. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  348. hostdata->pdma_base = (unsigned char *)pdma_mem->start;
  349. } else
  350. host_flags |= FLAG_NO_PSEUDO_DMA;
  351. host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
  352. error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
  353. if (error)
  354. goto fail_init;
  355. if (instance->irq != NO_IRQ) {
  356. error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
  357. "NCR5380", instance);
  358. if (error)
  359. goto fail_irq;
  360. }
  361. NCR5380_maybe_reset_bus(instance);
  362. error = scsi_add_host(instance, NULL);
  363. if (error)
  364. goto fail_host;
  365. platform_set_drvdata(pdev, instance);
  366. scsi_scan_host(instance);
  367. return 0;
  368. fail_host:
  369. if (instance->irq != NO_IRQ)
  370. free_irq(instance->irq, instance);
  371. fail_irq:
  372. NCR5380_exit(instance);
  373. fail_init:
  374. scsi_host_put(instance);
  375. return error;
  376. }
  377. static int __exit mac_scsi_remove(struct platform_device *pdev)
  378. {
  379. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  380. scsi_remove_host(instance);
  381. if (instance->irq != NO_IRQ)
  382. free_irq(instance->irq, instance);
  383. NCR5380_exit(instance);
  384. scsi_host_put(instance);
  385. return 0;
  386. }
  387. static struct platform_driver mac_scsi_driver = {
  388. .remove = __exit_p(mac_scsi_remove),
  389. .driver = {
  390. .name = DRV_MODULE_NAME,
  391. },
  392. };
  393. module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
  394. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  395. MODULE_LICENSE("GPL");