mac_scsi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic Macintosh NCR5380 driver
  4. *
  5. * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  6. *
  7. * Copyright 2019 Finn Thain
  8. *
  9. * derived in part from:
  10. */
  11. /*
  12. * Generic Generic NCR5380 driver
  13. *
  14. * Copyright 1995, Russell King
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/types.h>
  18. #include <linux/module.h>
  19. #include <linux/ioport.h>
  20. #include <linux/init.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/hwtest.h>
  25. #include <asm/io.h>
  26. #include <asm/macintosh.h>
  27. #include <asm/macints.h>
  28. #include <asm/setup.h>
  29. #include <scsi/scsi_host.h>
  30. /* Definitions for the core NCR5380 driver. */
  31. #define NCR5380_implementation_fields int pdma_residual
  32. #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
  33. #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
  34. #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
  35. #define NCR5380_dma_recv_setup macscsi_pread
  36. #define NCR5380_dma_send_setup macscsi_pwrite
  37. #define NCR5380_dma_residual macscsi_dma_residual
  38. #define NCR5380_intr macscsi_intr
  39. #define NCR5380_queue_command macscsi_queue_command
  40. #define NCR5380_abort macscsi_abort
  41. #define NCR5380_host_reset macscsi_host_reset
  42. #define NCR5380_info macscsi_info
  43. #include "NCR5380.h"
  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 = 512;
  51. module_param(setup_use_pdma, int, 0);
  52. static int setup_hostid = -1;
  53. module_param(setup_hostid, int, 0);
  54. static int setup_toshiba_delay = -1;
  55. module_param(setup_toshiba_delay, int, 0);
  56. #ifndef MODULE
  57. static int __init mac_scsi_setup(char *str)
  58. {
  59. int ints[8];
  60. (void)get_options(str, ARRAY_SIZE(ints), ints);
  61. if (ints[0] < 1) {
  62. pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
  63. return 0;
  64. }
  65. if (ints[0] >= 1)
  66. setup_can_queue = ints[1];
  67. if (ints[0] >= 2)
  68. setup_cmd_per_lun = ints[2];
  69. if (ints[0] >= 3)
  70. setup_sg_tablesize = ints[3];
  71. if (ints[0] >= 4)
  72. setup_hostid = ints[4];
  73. /* ints[5] (use_tagged_queuing) is ignored */
  74. if (ints[0] >= 6)
  75. setup_use_pdma = ints[6];
  76. if (ints[0] >= 7)
  77. setup_toshiba_delay = ints[7];
  78. return 1;
  79. }
  80. __setup("mac5380=", mac_scsi_setup);
  81. #endif /* !MODULE */
  82. /*
  83. * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
  84. * specify the number of bytes between the delays expected from a SCSI target.
  85. * This allows the operating system to "prevent bus errors when a target fails
  86. * to deliver the next byte within the processor bus error timeout period."
  87. * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
  88. * so bus errors are unavoidable.
  89. *
  90. * If a MOVE.B instruction faults, we assume that zero bytes were transferred
  91. * and simply retry. That assumption probably depends on target behaviour but
  92. * seems to hold up okay. The NOP provides synchronization: without it the
  93. * fault can sometimes occur after the program counter has moved past the
  94. * offending instruction. Post-increment addressing can't be used.
  95. */
  96. #define MOVE_BYTE(operands) \
  97. asm volatile ( \
  98. "1: moveb " operands " \n" \
  99. "11: nop \n" \
  100. " addq #1,%0 \n" \
  101. " subq #1,%1 \n" \
  102. "40: \n" \
  103. " \n" \
  104. ".section .fixup,\"ax\" \n" \
  105. ".even \n" \
  106. "90: movel #1, %2 \n" \
  107. " jra 40b \n" \
  108. ".previous \n" \
  109. " \n" \
  110. ".section __ex_table,\"a\" \n" \
  111. ".align 4 \n" \
  112. ".long 1b,90b \n" \
  113. ".long 11b,90b \n" \
  114. ".previous \n" \
  115. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  116. /*
  117. * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
  118. * the residual byte count would be uncertain. In that situation the MOVE_WORD
  119. * macro clears n in the fixup section to abort the transfer.
  120. */
  121. #define MOVE_WORD(operands) \
  122. asm volatile ( \
  123. "1: movew " operands " \n" \
  124. "11: nop \n" \
  125. " subq #2,%1 \n" \
  126. "40: \n" \
  127. " \n" \
  128. ".section .fixup,\"ax\" \n" \
  129. ".even \n" \
  130. "90: movel #0, %1 \n" \
  131. " movel #2, %2 \n" \
  132. " jra 40b \n" \
  133. ".previous \n" \
  134. " \n" \
  135. ".section __ex_table,\"a\" \n" \
  136. ".align 4 \n" \
  137. ".long 1b,90b \n" \
  138. ".long 11b,90b \n" \
  139. ".previous \n" \
  140. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  141. #define MOVE_16_WORDS(operands) \
  142. asm volatile ( \
  143. "1: movew " operands " \n" \
  144. "2: movew " operands " \n" \
  145. "3: movew " operands " \n" \
  146. "4: movew " operands " \n" \
  147. "5: movew " operands " \n" \
  148. "6: movew " operands " \n" \
  149. "7: movew " operands " \n" \
  150. "8: movew " operands " \n" \
  151. "9: movew " operands " \n" \
  152. "10: movew " operands " \n" \
  153. "11: movew " operands " \n" \
  154. "12: movew " operands " \n" \
  155. "13: movew " operands " \n" \
  156. "14: movew " operands " \n" \
  157. "15: movew " operands " \n" \
  158. "16: movew " operands " \n" \
  159. "17: nop \n" \
  160. " subl #32,%1 \n" \
  161. "40: \n" \
  162. " \n" \
  163. ".section .fixup,\"ax\" \n" \
  164. ".even \n" \
  165. "90: movel #0, %1 \n" \
  166. " movel #2, %2 \n" \
  167. " jra 40b \n" \
  168. ".previous \n" \
  169. " \n" \
  170. ".section __ex_table,\"a\" \n" \
  171. ".align 4 \n" \
  172. ".long 1b,90b \n" \
  173. ".long 2b,90b \n" \
  174. ".long 3b,90b \n" \
  175. ".long 4b,90b \n" \
  176. ".long 5b,90b \n" \
  177. ".long 6b,90b \n" \
  178. ".long 7b,90b \n" \
  179. ".long 8b,90b \n" \
  180. ".long 9b,90b \n" \
  181. ".long 10b,90b \n" \
  182. ".long 11b,90b \n" \
  183. ".long 12b,90b \n" \
  184. ".long 13b,90b \n" \
  185. ".long 14b,90b \n" \
  186. ".long 15b,90b \n" \
  187. ".long 16b,90b \n" \
  188. ".long 17b,90b \n" \
  189. ".previous \n" \
  190. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  191. #define MAC_PDMA_DELAY 32
  192. static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
  193. {
  194. unsigned char *addr = start;
  195. int result = 0;
  196. if (n >= 1) {
  197. MOVE_BYTE("%3@,%0@");
  198. if (result)
  199. goto out;
  200. }
  201. if (n >= 1 && ((unsigned long)addr & 1)) {
  202. MOVE_BYTE("%3@,%0@");
  203. if (result)
  204. goto out;
  205. }
  206. while (n >= 32)
  207. MOVE_16_WORDS("%3@,%0@+");
  208. while (n >= 2)
  209. MOVE_WORD("%3@,%0@+");
  210. if (result)
  211. return start - addr; /* Negated to indicate uncertain length */
  212. if (n == 1)
  213. MOVE_BYTE("%3@,%0@");
  214. out:
  215. return addr - start;
  216. }
  217. static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
  218. {
  219. unsigned char *addr = start;
  220. int result = 0;
  221. if (n >= 1) {
  222. MOVE_BYTE("%0@,%3@");
  223. if (result)
  224. goto out;
  225. }
  226. if (n >= 1 && ((unsigned long)addr & 1)) {
  227. MOVE_BYTE("%0@,%3@");
  228. if (result)
  229. goto out;
  230. }
  231. while (n >= 32)
  232. MOVE_16_WORDS("%0@+,%3@");
  233. while (n >= 2)
  234. MOVE_WORD("%0@+,%3@");
  235. if (result)
  236. return start - addr; /* Negated to indicate uncertain length */
  237. if (n == 1)
  238. MOVE_BYTE("%0@,%3@");
  239. out:
  240. return addr - start;
  241. }
  242. /* The "SCSI DMA" chip on the IIfx implements this register. */
  243. #define CTRL_REG 0x8
  244. #define CTRL_INTERRUPTS_ENABLE BIT(1)
  245. #define CTRL_HANDSHAKE_MODE BIT(3)
  246. static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
  247. {
  248. out_be32(hostdata->io + (CTRL_REG << 4), value);
  249. }
  250. static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
  251. unsigned char *dst, int len)
  252. {
  253. u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
  254. unsigned char *d = dst;
  255. int result = 0;
  256. hostdata->pdma_residual = len;
  257. while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
  258. BASR_DRQ | BASR_PHASE_MATCH,
  259. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  260. int bytes;
  261. if (macintosh_config->ident == MAC_MODEL_IIFX)
  262. write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
  263. CTRL_INTERRUPTS_ENABLE);
  264. bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
  265. if (bytes > 0) {
  266. d += bytes;
  267. hostdata->pdma_residual -= bytes;
  268. }
  269. if (hostdata->pdma_residual == 0)
  270. goto out;
  271. if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
  272. BUS_AND_STATUS_REG, BASR_ACK,
  273. BASR_ACK, HZ / 64) < 0)
  274. scmd_printk(KERN_DEBUG, hostdata->connected,
  275. "%s: !REQ and !ACK\n", __func__);
  276. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  277. goto out;
  278. if (bytes == 0)
  279. udelay(MAC_PDMA_DELAY);
  280. if (bytes >= 0)
  281. continue;
  282. dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
  283. "%s: bus error (%d/%d)\n", __func__, d - dst, len);
  284. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  285. result = -1;
  286. goto out;
  287. }
  288. scmd_printk(KERN_ERR, hostdata->connected,
  289. "%s: phase mismatch or !DRQ\n", __func__);
  290. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  291. result = -1;
  292. out:
  293. if (macintosh_config->ident == MAC_MODEL_IIFX)
  294. write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
  295. return result;
  296. }
  297. static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
  298. unsigned char *src, int len)
  299. {
  300. unsigned char *s = src;
  301. u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
  302. int result = 0;
  303. hostdata->pdma_residual = len;
  304. while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
  305. BASR_DRQ | BASR_PHASE_MATCH,
  306. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  307. int bytes;
  308. if (macintosh_config->ident == MAC_MODEL_IIFX)
  309. write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
  310. CTRL_INTERRUPTS_ENABLE);
  311. bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
  312. if (bytes > 0) {
  313. s += bytes;
  314. hostdata->pdma_residual -= bytes;
  315. }
  316. if (hostdata->pdma_residual == 0) {
  317. if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
  318. TCR_LAST_BYTE_SENT,
  319. TCR_LAST_BYTE_SENT,
  320. HZ / 64) < 0) {
  321. scmd_printk(KERN_ERR, hostdata->connected,
  322. "%s: Last Byte Sent timeout\n", __func__);
  323. result = -1;
  324. }
  325. goto out;
  326. }
  327. if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
  328. BUS_AND_STATUS_REG, BASR_ACK,
  329. BASR_ACK, HZ / 64) < 0)
  330. scmd_printk(KERN_DEBUG, hostdata->connected,
  331. "%s: !REQ and !ACK\n", __func__);
  332. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  333. goto out;
  334. if (bytes == 0)
  335. udelay(MAC_PDMA_DELAY);
  336. if (bytes >= 0)
  337. continue;
  338. dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
  339. "%s: bus error (%d/%d)\n", __func__, s - src, len);
  340. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  341. result = -1;
  342. goto out;
  343. }
  344. scmd_printk(KERN_ERR, hostdata->connected,
  345. "%s: phase mismatch or !DRQ\n", __func__);
  346. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  347. result = -1;
  348. out:
  349. if (macintosh_config->ident == MAC_MODEL_IIFX)
  350. write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
  351. return result;
  352. }
  353. static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
  354. struct scsi_cmnd *cmd)
  355. {
  356. if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
  357. cmd->SCp.this_residual < setup_use_pdma)
  358. return 0;
  359. return cmd->SCp.this_residual;
  360. }
  361. static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
  362. {
  363. return hostdata->pdma_residual;
  364. }
  365. #include "NCR5380.c"
  366. #define DRV_MODULE_NAME "mac_scsi"
  367. #define PFX DRV_MODULE_NAME ": "
  368. static struct scsi_host_template mac_scsi_template = {
  369. .module = THIS_MODULE,
  370. .proc_name = DRV_MODULE_NAME,
  371. .name = "Macintosh NCR5380 SCSI",
  372. .info = macscsi_info,
  373. .queuecommand = macscsi_queue_command,
  374. .eh_abort_handler = macscsi_abort,
  375. .eh_host_reset_handler = macscsi_host_reset,
  376. .can_queue = 16,
  377. .this_id = 7,
  378. .sg_tablesize = 1,
  379. .cmd_per_lun = 2,
  380. .dma_boundary = PAGE_SIZE - 1,
  381. .cmd_size = NCR5380_CMD_SIZE,
  382. .max_sectors = 128,
  383. };
  384. static int __init mac_scsi_probe(struct platform_device *pdev)
  385. {
  386. struct Scsi_Host *instance;
  387. struct NCR5380_hostdata *hostdata;
  388. int error;
  389. int host_flags = 0;
  390. struct resource *irq, *pio_mem, *pdma_mem = NULL;
  391. pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  392. if (!pio_mem)
  393. return -ENODEV;
  394. pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  395. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  396. if (!hwreg_present((unsigned char *)pio_mem->start +
  397. (STATUS_REG << 4))) {
  398. pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
  399. return -ENODEV;
  400. }
  401. if (setup_can_queue > 0)
  402. mac_scsi_template.can_queue = setup_can_queue;
  403. if (setup_cmd_per_lun > 0)
  404. mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
  405. if (setup_sg_tablesize > 0)
  406. mac_scsi_template.sg_tablesize = setup_sg_tablesize;
  407. if (setup_hostid >= 0)
  408. mac_scsi_template.this_id = setup_hostid & 7;
  409. instance = scsi_host_alloc(&mac_scsi_template,
  410. sizeof(struct NCR5380_hostdata));
  411. if (!instance)
  412. return -ENOMEM;
  413. if (irq)
  414. instance->irq = irq->start;
  415. else
  416. instance->irq = NO_IRQ;
  417. hostdata = shost_priv(instance);
  418. hostdata->base = pio_mem->start;
  419. hostdata->io = (u8 __iomem *)pio_mem->start;
  420. if (pdma_mem && setup_use_pdma)
  421. hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
  422. else
  423. host_flags |= FLAG_NO_PSEUDO_DMA;
  424. host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
  425. error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
  426. if (error)
  427. goto fail_init;
  428. if (instance->irq != NO_IRQ) {
  429. error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
  430. "NCR5380", instance);
  431. if (error)
  432. goto fail_irq;
  433. }
  434. NCR5380_maybe_reset_bus(instance);
  435. error = scsi_add_host(instance, NULL);
  436. if (error)
  437. goto fail_host;
  438. platform_set_drvdata(pdev, instance);
  439. scsi_scan_host(instance);
  440. return 0;
  441. fail_host:
  442. if (instance->irq != NO_IRQ)
  443. free_irq(instance->irq, instance);
  444. fail_irq:
  445. NCR5380_exit(instance);
  446. fail_init:
  447. scsi_host_put(instance);
  448. return error;
  449. }
  450. static int __exit mac_scsi_remove(struct platform_device *pdev)
  451. {
  452. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  453. scsi_remove_host(instance);
  454. if (instance->irq != NO_IRQ)
  455. free_irq(instance->irq, instance);
  456. NCR5380_exit(instance);
  457. scsi_host_put(instance);
  458. return 0;
  459. }
  460. static struct platform_driver mac_scsi_driver = {
  461. .remove = __exit_p(mac_scsi_remove),
  462. .driver = {
  463. .name = DRV_MODULE_NAME,
  464. },
  465. };
  466. module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
  467. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  468. MODULE_LICENSE("GPL");