mac_scsi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/ioport.h>
  17. #include <linux/init.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/hwtest.h>
  22. #include <asm/io.h>
  23. #include <asm/macints.h>
  24. #include <asm/setup.h>
  25. #include <scsi/scsi_host.h>
  26. /* Definitions for the core NCR5380 driver. */
  27. #define PSEUDO_DMA
  28. #define NCR5380_implementation_fields unsigned char *pdma_base
  29. #define NCR5380_local_declare() struct Scsi_Host *_instance
  30. #define NCR5380_setup(instance) _instance = instance
  31. #define NCR5380_read(reg) macscsi_read(_instance, reg)
  32. #define NCR5380_write(reg, value) macscsi_write(_instance, reg, value)
  33. #define NCR5380_pread macscsi_pread
  34. #define NCR5380_pwrite macscsi_pwrite
  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. #define NCR5380_show_info macscsi_show_info
  41. #define NCR5380_write_info macscsi_write_info
  42. #include "NCR5380.h"
  43. #define RESET_BOOT
  44. static int setup_can_queue = -1;
  45. module_param(setup_can_queue, int, 0);
  46. static int setup_cmd_per_lun = -1;
  47. module_param(setup_cmd_per_lun, int, 0);
  48. static int setup_sg_tablesize = -1;
  49. module_param(setup_sg_tablesize, int, 0);
  50. static int setup_use_pdma = -1;
  51. module_param(setup_use_pdma, int, 0);
  52. static int setup_use_tagged_queuing = -1;
  53. module_param(setup_use_tagged_queuing, int, 0);
  54. static int setup_hostid = -1;
  55. module_param(setup_hostid, int, 0);
  56. /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
  57. * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
  58. * need ten times the standard value... */
  59. #define TOSHIBA_DELAY
  60. #ifdef TOSHIBA_DELAY
  61. #define AFTER_RESET_DELAY (5*HZ/2)
  62. #else
  63. #define AFTER_RESET_DELAY (HZ/2)
  64. #endif
  65. /*
  66. * NCR 5380 register access functions
  67. */
  68. static inline char macscsi_read(struct Scsi_Host *instance, int reg)
  69. {
  70. return in_8(instance->base + (reg << 4));
  71. }
  72. static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
  73. {
  74. out_8(instance->base + (reg << 4), value);
  75. }
  76. #ifndef MODULE
  77. static int __init mac_scsi_setup(char *str)
  78. {
  79. int ints[7];
  80. (void)get_options(str, ARRAY_SIZE(ints), ints);
  81. if (ints[0] < 1 || ints[0] > 6) {
  82. pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>]]]]]\n");
  83. return 0;
  84. }
  85. if (ints[0] >= 1)
  86. setup_can_queue = ints[1];
  87. if (ints[0] >= 2)
  88. setup_cmd_per_lun = ints[2];
  89. if (ints[0] >= 3)
  90. setup_sg_tablesize = ints[3];
  91. if (ints[0] >= 4)
  92. setup_hostid = ints[4];
  93. if (ints[0] >= 5)
  94. setup_use_tagged_queuing = ints[5];
  95. if (ints[0] >= 6)
  96. setup_use_pdma = ints[6];
  97. return 1;
  98. }
  99. __setup("mac5380=", mac_scsi_setup);
  100. #endif /* !MODULE */
  101. #ifdef RESET_BOOT
  102. /*
  103. * Our 'bus reset on boot' function
  104. */
  105. static void mac_scsi_reset_boot(struct Scsi_Host *instance)
  106. {
  107. unsigned long end;
  108. NCR5380_local_declare();
  109. NCR5380_setup(instance);
  110. /*
  111. * Do a SCSI reset to clean up the bus during initialization. No messing
  112. * with the queues, interrupts, or locks necessary here.
  113. */
  114. printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
  115. /* get in phase */
  116. NCR5380_write( TARGET_COMMAND_REG,
  117. PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  118. /* assert RST */
  119. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  120. /* The min. reset hold time is 25us, so 40us should be enough */
  121. udelay( 50 );
  122. /* reset RST and interrupt */
  123. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  124. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  125. for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
  126. barrier();
  127. printk(KERN_INFO " done\n" );
  128. }
  129. #endif
  130. #ifdef PSEUDO_DMA
  131. /*
  132. Pseudo-DMA: (Ove Edlund)
  133. The code attempts to catch bus errors that occur if one for example
  134. "trips over the cable".
  135. XXX: Since bus errors in the PDMA routines never happen on my
  136. computer, the bus error code is untested.
  137. If the code works as intended, a bus error results in Pseudo-DMA
  138. being disabled, meaning that the driver switches to slow handshake.
  139. If bus errors are NOT extremely rare, this has to be changed.
  140. */
  141. #define CP_IO_TO_MEM(s,d,len) \
  142. __asm__ __volatile__ \
  143. (" cmp.w #4,%2\n" \
  144. " bls 8f\n" \
  145. " move.w %1,%%d0\n" \
  146. " neg.b %%d0\n" \
  147. " and.w #3,%%d0\n" \
  148. " sub.w %%d0,%2\n" \
  149. " bra 2f\n" \
  150. " 1: move.b (%0),(%1)+\n" \
  151. " 2: dbf %%d0,1b\n" \
  152. " move.w %2,%%d0\n" \
  153. " lsr.w #5,%%d0\n" \
  154. " bra 4f\n" \
  155. " 3: move.l (%0),(%1)+\n" \
  156. "31: move.l (%0),(%1)+\n" \
  157. "32: move.l (%0),(%1)+\n" \
  158. "33: move.l (%0),(%1)+\n" \
  159. "34: move.l (%0),(%1)+\n" \
  160. "35: move.l (%0),(%1)+\n" \
  161. "36: move.l (%0),(%1)+\n" \
  162. "37: move.l (%0),(%1)+\n" \
  163. " 4: dbf %%d0,3b\n" \
  164. " move.w %2,%%d0\n" \
  165. " lsr.w #2,%%d0\n" \
  166. " and.w #7,%%d0\n" \
  167. " bra 6f\n" \
  168. " 5: move.l (%0),(%1)+\n" \
  169. " 6: dbf %%d0,5b\n" \
  170. " and.w #3,%2\n" \
  171. " bra 8f\n" \
  172. " 7: move.b (%0),(%1)+\n" \
  173. " 8: dbf %2,7b\n" \
  174. " moveq.l #0, %2\n" \
  175. " 9: \n" \
  176. ".section .fixup,\"ax\"\n" \
  177. " .even\n" \
  178. "90: moveq.l #1, %2\n" \
  179. " jra 9b\n" \
  180. ".previous\n" \
  181. ".section __ex_table,\"a\"\n" \
  182. " .align 4\n" \
  183. " .long 1b,90b\n" \
  184. " .long 3b,90b\n" \
  185. " .long 31b,90b\n" \
  186. " .long 32b,90b\n" \
  187. " .long 33b,90b\n" \
  188. " .long 34b,90b\n" \
  189. " .long 35b,90b\n" \
  190. " .long 36b,90b\n" \
  191. " .long 37b,90b\n" \
  192. " .long 5b,90b\n" \
  193. " .long 7b,90b\n" \
  194. ".previous" \
  195. : "=a"(s), "=a"(d), "=d"(len) \
  196. : "0"(s), "1"(d), "2"(len) \
  197. : "d0")
  198. static int macscsi_pread(struct Scsi_Host *instance,
  199. unsigned char *dst, int len)
  200. {
  201. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  202. unsigned char *d;
  203. unsigned char *s;
  204. NCR5380_local_declare();
  205. NCR5380_setup(instance);
  206. s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
  207. d = dst;
  208. /* These conditions are derived from MacOS */
  209. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
  210. !(NCR5380_read(STATUS_REG) & SR_REQ))
  211. ;
  212. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
  213. (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
  214. pr_err("Error in macscsi_pread\n");
  215. return -1;
  216. }
  217. CP_IO_TO_MEM(s, d, len);
  218. if (len != 0) {
  219. pr_notice("Bus error in macscsi_pread\n");
  220. return -1;
  221. }
  222. return 0;
  223. }
  224. #define CP_MEM_TO_IO(s,d,len) \
  225. __asm__ __volatile__ \
  226. (" cmp.w #4,%2\n" \
  227. " bls 8f\n" \
  228. " move.w %0,%%d0\n" \
  229. " neg.b %%d0\n" \
  230. " and.w #3,%%d0\n" \
  231. " sub.w %%d0,%2\n" \
  232. " bra 2f\n" \
  233. " 1: move.b (%0)+,(%1)\n" \
  234. " 2: dbf %%d0,1b\n" \
  235. " move.w %2,%%d0\n" \
  236. " lsr.w #5,%%d0\n" \
  237. " bra 4f\n" \
  238. " 3: move.l (%0)+,(%1)\n" \
  239. "31: move.l (%0)+,(%1)\n" \
  240. "32: move.l (%0)+,(%1)\n" \
  241. "33: move.l (%0)+,(%1)\n" \
  242. "34: move.l (%0)+,(%1)\n" \
  243. "35: move.l (%0)+,(%1)\n" \
  244. "36: move.l (%0)+,(%1)\n" \
  245. "37: move.l (%0)+,(%1)\n" \
  246. " 4: dbf %%d0,3b\n" \
  247. " move.w %2,%%d0\n" \
  248. " lsr.w #2,%%d0\n" \
  249. " and.w #7,%%d0\n" \
  250. " bra 6f\n" \
  251. " 5: move.l (%0)+,(%1)\n" \
  252. " 6: dbf %%d0,5b\n" \
  253. " and.w #3,%2\n" \
  254. " bra 8f\n" \
  255. " 7: move.b (%0)+,(%1)\n" \
  256. " 8: dbf %2,7b\n" \
  257. " moveq.l #0, %2\n" \
  258. " 9: \n" \
  259. ".section .fixup,\"ax\"\n" \
  260. " .even\n" \
  261. "90: moveq.l #1, %2\n" \
  262. " jra 9b\n" \
  263. ".previous\n" \
  264. ".section __ex_table,\"a\"\n" \
  265. " .align 4\n" \
  266. " .long 1b,90b\n" \
  267. " .long 3b,90b\n" \
  268. " .long 31b,90b\n" \
  269. " .long 32b,90b\n" \
  270. " .long 33b,90b\n" \
  271. " .long 34b,90b\n" \
  272. " .long 35b,90b\n" \
  273. " .long 36b,90b\n" \
  274. " .long 37b,90b\n" \
  275. " .long 5b,90b\n" \
  276. " .long 7b,90b\n" \
  277. ".previous" \
  278. : "=a"(s), "=a"(d), "=d"(len) \
  279. : "0"(s), "1"(d), "2"(len) \
  280. : "d0")
  281. static int macscsi_pwrite(struct Scsi_Host *instance,
  282. unsigned char *src, int len)
  283. {
  284. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  285. unsigned char *s;
  286. unsigned char *d;
  287. NCR5380_local_declare();
  288. NCR5380_setup(instance);
  289. s = src;
  290. d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
  291. /* These conditions are derived from MacOS */
  292. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
  293. (!(NCR5380_read(STATUS_REG) & SR_REQ) ||
  294. (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
  295. ;
  296. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
  297. pr_err("Error in macscsi_pwrite\n");
  298. return -1;
  299. }
  300. CP_MEM_TO_IO(s, d, len);
  301. if (len != 0) {
  302. pr_notice("Bus error in macscsi_pwrite\n");
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. #endif
  308. #include "NCR5380.c"
  309. #define DRV_MODULE_NAME "mac_scsi"
  310. #define PFX DRV_MODULE_NAME ": "
  311. static struct scsi_host_template mac_scsi_template = {
  312. .module = THIS_MODULE,
  313. .proc_name = DRV_MODULE_NAME,
  314. .show_info = macscsi_show_info,
  315. .write_info = macscsi_write_info,
  316. .name = "Macintosh NCR5380 SCSI",
  317. .info = macscsi_info,
  318. .queuecommand = macscsi_queue_command,
  319. .eh_abort_handler = macscsi_abort,
  320. .eh_bus_reset_handler = macscsi_bus_reset,
  321. .can_queue = 16,
  322. .this_id = 7,
  323. .sg_tablesize = SG_ALL,
  324. .cmd_per_lun = 2,
  325. .use_clustering = DISABLE_CLUSTERING
  326. };
  327. static int __init mac_scsi_probe(struct platform_device *pdev)
  328. {
  329. struct Scsi_Host *instance;
  330. int error;
  331. int host_flags = 0;
  332. struct resource *irq, *pio_mem, *pdma_mem = NULL;
  333. pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  334. if (!pio_mem)
  335. return -ENODEV;
  336. #ifdef PSEUDO_DMA
  337. pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  338. #endif
  339. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  340. if (!hwreg_present((unsigned char *)pio_mem->start +
  341. (STATUS_REG << 4))) {
  342. pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
  343. return -ENODEV;
  344. }
  345. if (setup_can_queue > 0)
  346. mac_scsi_template.can_queue = setup_can_queue;
  347. if (setup_cmd_per_lun > 0)
  348. mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
  349. if (setup_sg_tablesize >= 0)
  350. mac_scsi_template.sg_tablesize = setup_sg_tablesize;
  351. if (setup_hostid >= 0)
  352. mac_scsi_template.this_id = setup_hostid & 7;
  353. if (setup_use_pdma < 0)
  354. setup_use_pdma = 0;
  355. instance = scsi_host_alloc(&mac_scsi_template,
  356. sizeof(struct NCR5380_hostdata));
  357. if (!instance)
  358. return -ENOMEM;
  359. instance->base = pio_mem->start;
  360. if (irq)
  361. instance->irq = irq->start;
  362. else
  363. instance->irq = NO_IRQ;
  364. if (pdma_mem && setup_use_pdma) {
  365. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  366. hostdata->pdma_base = (unsigned char *)pdma_mem->start;
  367. } else
  368. host_flags |= FLAG_NO_PSEUDO_DMA;
  369. #ifdef RESET_BOOT
  370. mac_scsi_reset_boot(instance);
  371. #endif
  372. #ifdef SUPPORT_TAGS
  373. host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
  374. #endif
  375. NCR5380_init(instance, host_flags);
  376. if (instance->irq != NO_IRQ) {
  377. error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
  378. "NCR5380", instance);
  379. if (error)
  380. goto fail_irq;
  381. }
  382. error = scsi_add_host(instance, NULL);
  383. if (error)
  384. goto fail_host;
  385. platform_set_drvdata(pdev, instance);
  386. scsi_scan_host(instance);
  387. return 0;
  388. fail_host:
  389. if (instance->irq != NO_IRQ)
  390. free_irq(instance->irq, instance);
  391. fail_irq:
  392. NCR5380_exit(instance);
  393. scsi_host_put(instance);
  394. return error;
  395. }
  396. static int __exit mac_scsi_remove(struct platform_device *pdev)
  397. {
  398. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  399. scsi_remove_host(instance);
  400. if (instance->irq != NO_IRQ)
  401. free_irq(instance->irq, instance);
  402. NCR5380_exit(instance);
  403. scsi_host_put(instance);
  404. return 0;
  405. }
  406. static struct platform_driver mac_scsi_driver = {
  407. .remove = __exit_p(mac_scsi_remove),
  408. .driver = {
  409. .name = DRV_MODULE_NAME,
  410. },
  411. };
  412. module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
  413. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  414. MODULE_LICENSE("GPL");