mac53c94.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * SCSI low-level driver for the 53c94 SCSI bus adaptor found
  3. * on Power Macintosh computers, controlling the external SCSI chain.
  4. * We assume the 53c94 is connected to a DBDMA (descriptor-based DMA)
  5. * controller.
  6. *
  7. * Paul Mackerras, August 1996.
  8. * Copyright (C) 1996 Paul Mackerras.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/types.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/stat.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/dbdma.h>
  21. #include <asm/io.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/prom.h>
  24. #include <asm/system.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/macio.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include "mac53c94.h"
  32. enum fsc_phase {
  33. idle,
  34. selecting,
  35. dataing,
  36. completing,
  37. busfreeing,
  38. };
  39. struct fsc_state {
  40. struct mac53c94_regs __iomem *regs;
  41. int intr;
  42. struct dbdma_regs __iomem *dma;
  43. int dmaintr;
  44. int clk_freq;
  45. struct Scsi_Host *host;
  46. struct scsi_cmnd *request_q;
  47. struct scsi_cmnd *request_qtail;
  48. struct scsi_cmnd *current_req; /* req we're currently working on */
  49. enum fsc_phase phase; /* what we're currently trying to do */
  50. struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */
  51. void *dma_cmd_space;
  52. struct pci_dev *pdev;
  53. dma_addr_t dma_addr;
  54. struct macio_dev *mdev;
  55. };
  56. static void mac53c94_init(struct fsc_state *);
  57. static void mac53c94_start(struct fsc_state *);
  58. static void mac53c94_interrupt(int, void *);
  59. static irqreturn_t do_mac53c94_interrupt(int, void *);
  60. static void cmd_done(struct fsc_state *, int result);
  61. static void set_dma_cmds(struct fsc_state *, struct scsi_cmnd *);
  62. static int mac53c94_queue(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  63. {
  64. struct fsc_state *state;
  65. #if 0
  66. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  67. int i;
  68. printk(KERN_DEBUG "mac53c94_queue %p: command is", cmd);
  69. for (i = 0; i < cmd->cmd_len; ++i)
  70. printk(KERN_CONT " %.2x", cmd->cmnd[i]);
  71. printk(KERN_CONT "\n");
  72. printk(KERN_DEBUG "use_sg=%d request_bufflen=%d request_buffer=%p\n",
  73. scsi_sg_count(cmd), scsi_bufflen(cmd), scsi_sglist(cmd));
  74. }
  75. #endif
  76. cmd->scsi_done = done;
  77. cmd->host_scribble = NULL;
  78. state = (struct fsc_state *) cmd->device->host->hostdata;
  79. if (state->request_q == NULL)
  80. state->request_q = cmd;
  81. else
  82. state->request_qtail->host_scribble = (void *) cmd;
  83. state->request_qtail = cmd;
  84. if (state->phase == idle)
  85. mac53c94_start(state);
  86. return 0;
  87. }
  88. static int mac53c94_host_reset(struct scsi_cmnd *cmd)
  89. {
  90. struct fsc_state *state = (struct fsc_state *) cmd->device->host->hostdata;
  91. struct mac53c94_regs __iomem *regs = state->regs;
  92. struct dbdma_regs __iomem *dma = state->dma;
  93. unsigned long flags;
  94. spin_lock_irqsave(cmd->device->host->host_lock, flags);
  95. writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control);
  96. writeb(CMD_SCSI_RESET, &regs->command); /* assert RST */
  97. udelay(100); /* leave it on for a while (>= 25us) */
  98. writeb(CMD_RESET, &regs->command);
  99. udelay(20);
  100. mac53c94_init(state);
  101. writeb(CMD_NOP, &regs->command);
  102. spin_unlock_irqrestore(cmd->device->host->host_lock, flags);
  103. return SUCCESS;
  104. }
  105. static void mac53c94_init(struct fsc_state *state)
  106. {
  107. struct mac53c94_regs __iomem *regs = state->regs;
  108. struct dbdma_regs __iomem *dma = state->dma;
  109. int x;
  110. writeb(state->host->this_id | CF1_PAR_ENABLE, &regs->config1);
  111. writeb(TIMO_VAL(250), &regs->sel_timeout); /* 250ms */
  112. writeb(CLKF_VAL(state->clk_freq), &regs->clk_factor);
  113. writeb(CF2_FEATURE_EN, &regs->config2);
  114. writeb(0, &regs->config3);
  115. writeb(0, &regs->sync_period);
  116. writeb(0, &regs->sync_offset);
  117. x = readb(&regs->interrupt);
  118. writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control);
  119. }
  120. /*
  121. * Start the next command for a 53C94.
  122. * Should be called with interrupts disabled.
  123. */
  124. static void mac53c94_start(struct fsc_state *state)
  125. {
  126. struct scsi_cmnd *cmd;
  127. struct mac53c94_regs __iomem *regs = state->regs;
  128. int i;
  129. if (state->phase != idle || state->current_req != NULL)
  130. panic("inappropriate mac53c94_start (state=%p)", state);
  131. if (state->request_q == NULL)
  132. return;
  133. state->current_req = cmd = state->request_q;
  134. state->request_q = (struct scsi_cmnd *) cmd->host_scribble;
  135. /* Off we go */
  136. writeb(0, &regs->count_lo);
  137. writeb(0, &regs->count_mid);
  138. writeb(0, &regs->count_hi);
  139. writeb(CMD_NOP + CMD_DMA_MODE, &regs->command);
  140. udelay(1);
  141. writeb(CMD_FLUSH, &regs->command);
  142. udelay(1);
  143. writeb(cmd->device->id, &regs->dest_id);
  144. writeb(0, &regs->sync_period);
  145. writeb(0, &regs->sync_offset);
  146. /* load the command into the FIFO */
  147. for (i = 0; i < cmd->cmd_len; ++i)
  148. writeb(cmd->cmnd[i], &regs->fifo);
  149. /* do select without ATN XXX */
  150. writeb(CMD_SELECT, &regs->command);
  151. state->phase = selecting;
  152. set_dma_cmds(state, cmd);
  153. }
  154. static irqreturn_t do_mac53c94_interrupt(int irq, void *dev_id)
  155. {
  156. unsigned long flags;
  157. struct Scsi_Host *dev = ((struct fsc_state *) dev_id)->current_req->device->host;
  158. spin_lock_irqsave(dev->host_lock, flags);
  159. mac53c94_interrupt(irq, dev_id);
  160. spin_unlock_irqrestore(dev->host_lock, flags);
  161. return IRQ_HANDLED;
  162. }
  163. static void mac53c94_interrupt(int irq, void *dev_id)
  164. {
  165. struct fsc_state *state = (struct fsc_state *) dev_id;
  166. struct mac53c94_regs __iomem *regs = state->regs;
  167. struct dbdma_regs __iomem *dma = state->dma;
  168. struct scsi_cmnd *cmd = state->current_req;
  169. int nb, stat, seq, intr;
  170. static int mac53c94_errors;
  171. /*
  172. * Apparently, reading the interrupt register unlatches
  173. * the status and sequence step registers.
  174. */
  175. seq = readb(&regs->seqstep);
  176. stat = readb(&regs->status);
  177. intr = readb(&regs->interrupt);
  178. #if 0
  179. printk(KERN_DEBUG "mac53c94_intr, intr=%x stat=%x seq=%x phase=%d\n",
  180. intr, stat, seq, state->phase);
  181. #endif
  182. if (intr & INTR_RESET) {
  183. /* SCSI bus was reset */
  184. printk(KERN_INFO "external SCSI bus reset detected\n");
  185. writeb(CMD_NOP, &regs->command);
  186. writel(RUN << 16, &dma->control); /* stop dma */
  187. cmd_done(state, DID_RESET << 16);
  188. return;
  189. }
  190. if (intr & INTR_ILL_CMD) {
  191. printk(KERN_ERR "53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n",
  192. intr, stat, seq, state->phase);
  193. cmd_done(state, DID_ERROR << 16);
  194. return;
  195. }
  196. if (stat & STAT_ERROR) {
  197. #if 0
  198. /* XXX these seem to be harmless? */
  199. printk("53c94: bad error, intr=%x stat=%x seq=%x phase=%d\n",
  200. intr, stat, seq, state->phase);
  201. #endif
  202. ++mac53c94_errors;
  203. writeb(CMD_NOP + CMD_DMA_MODE, &regs->command);
  204. }
  205. if (cmd == 0) {
  206. printk(KERN_DEBUG "53c94: interrupt with no command active?\n");
  207. return;
  208. }
  209. if (stat & STAT_PARITY) {
  210. printk(KERN_ERR "mac53c94: parity error\n");
  211. cmd_done(state, DID_PARITY << 16);
  212. return;
  213. }
  214. switch (state->phase) {
  215. case selecting:
  216. if (intr & INTR_DISCONNECT) {
  217. /* selection timed out */
  218. cmd_done(state, DID_BAD_TARGET << 16);
  219. return;
  220. }
  221. if (intr != INTR_BUS_SERV + INTR_DONE) {
  222. printk(KERN_DEBUG "got intr %x during selection\n", intr);
  223. cmd_done(state, DID_ERROR << 16);
  224. return;
  225. }
  226. if ((seq & SS_MASK) != SS_DONE) {
  227. printk(KERN_DEBUG "seq step %x after command\n", seq);
  228. cmd_done(state, DID_ERROR << 16);
  229. return;
  230. }
  231. writeb(CMD_NOP, &regs->command);
  232. /* set DMA controller going if any data to transfer */
  233. if ((stat & (STAT_MSG|STAT_CD)) == 0
  234. && (scsi_sg_count(cmd) > 0 || scsi_bufflen(cmd))) {
  235. nb = cmd->SCp.this_residual;
  236. if (nb > 0xfff0)
  237. nb = 0xfff0;
  238. cmd->SCp.this_residual -= nb;
  239. writeb(nb, &regs->count_lo);
  240. writeb(nb >> 8, &regs->count_mid);
  241. writeb(CMD_DMA_MODE + CMD_NOP, &regs->command);
  242. writel(virt_to_phys(state->dma_cmds), &dma->cmdptr);
  243. writel((RUN << 16) | RUN, &dma->control);
  244. writeb(CMD_DMA_MODE + CMD_XFER_DATA, &regs->command);
  245. state->phase = dataing;
  246. break;
  247. } else if ((stat & STAT_PHASE) == STAT_CD + STAT_IO) {
  248. /* up to status phase already */
  249. writeb(CMD_I_COMPLETE, &regs->command);
  250. state->phase = completing;
  251. } else {
  252. printk(KERN_DEBUG "in unexpected phase %x after cmd\n",
  253. stat & STAT_PHASE);
  254. cmd_done(state, DID_ERROR << 16);
  255. return;
  256. }
  257. break;
  258. case dataing:
  259. if (intr != INTR_BUS_SERV) {
  260. printk(KERN_DEBUG "got intr %x before status\n", intr);
  261. cmd_done(state, DID_ERROR << 16);
  262. return;
  263. }
  264. if (cmd->SCp.this_residual != 0
  265. && (stat & (STAT_MSG|STAT_CD)) == 0) {
  266. /* Set up the count regs to transfer more */
  267. nb = cmd->SCp.this_residual;
  268. if (nb > 0xfff0)
  269. nb = 0xfff0;
  270. cmd->SCp.this_residual -= nb;
  271. writeb(nb, &regs->count_lo);
  272. writeb(nb >> 8, &regs->count_mid);
  273. writeb(CMD_DMA_MODE + CMD_NOP, &regs->command);
  274. writeb(CMD_DMA_MODE + CMD_XFER_DATA, &regs->command);
  275. break;
  276. }
  277. if ((stat & STAT_PHASE) != STAT_CD + STAT_IO) {
  278. printk(KERN_DEBUG "intr %x before data xfer complete\n", intr);
  279. }
  280. writel(RUN << 16, &dma->control); /* stop dma */
  281. scsi_dma_unmap(cmd);
  282. /* should check dma status */
  283. writeb(CMD_I_COMPLETE, &regs->command);
  284. state->phase = completing;
  285. break;
  286. case completing:
  287. if (intr != INTR_DONE) {
  288. printk(KERN_DEBUG "got intr %x on completion\n", intr);
  289. cmd_done(state, DID_ERROR << 16);
  290. return;
  291. }
  292. cmd->SCp.Status = readb(&regs->fifo);
  293. cmd->SCp.Message = readb(&regs->fifo);
  294. cmd->result = CMD_ACCEPT_MSG;
  295. writeb(CMD_ACCEPT_MSG, &regs->command);
  296. state->phase = busfreeing;
  297. break;
  298. case busfreeing:
  299. if (intr != INTR_DISCONNECT) {
  300. printk(KERN_DEBUG "got intr %x when expected disconnect\n", intr);
  301. }
  302. cmd_done(state, (DID_OK << 16) + (cmd->SCp.Message << 8)
  303. + cmd->SCp.Status);
  304. break;
  305. default:
  306. printk(KERN_DEBUG "don't know about phase %d\n", state->phase);
  307. }
  308. }
  309. static void cmd_done(struct fsc_state *state, int result)
  310. {
  311. struct scsi_cmnd *cmd;
  312. cmd = state->current_req;
  313. if (cmd != 0) {
  314. cmd->result = result;
  315. (*cmd->scsi_done)(cmd);
  316. state->current_req = NULL;
  317. }
  318. state->phase = idle;
  319. mac53c94_start(state);
  320. }
  321. /*
  322. * Set up DMA commands for transferring data.
  323. */
  324. static void set_dma_cmds(struct fsc_state *state, struct scsi_cmnd *cmd)
  325. {
  326. int i, dma_cmd, total, nseg;
  327. struct scatterlist *scl;
  328. struct dbdma_cmd *dcmds;
  329. dma_addr_t dma_addr;
  330. u32 dma_len;
  331. nseg = scsi_dma_map(cmd);
  332. BUG_ON(nseg < 0);
  333. if (!nseg)
  334. return;
  335. dma_cmd = cmd->sc_data_direction == DMA_TO_DEVICE ?
  336. OUTPUT_MORE : INPUT_MORE;
  337. dcmds = state->dma_cmds;
  338. total = 0;
  339. scsi_for_each_sg(cmd, scl, nseg, i) {
  340. dma_addr = sg_dma_address(scl);
  341. dma_len = sg_dma_len(scl);
  342. if (dma_len > 0xffff)
  343. panic("mac53c94: scatterlist element >= 64k");
  344. total += dma_len;
  345. st_le16(&dcmds->req_count, dma_len);
  346. st_le16(&dcmds->command, dma_cmd);
  347. st_le32(&dcmds->phy_addr, dma_addr);
  348. dcmds->xfer_status = 0;
  349. ++dcmds;
  350. }
  351. dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
  352. st_le16(&dcmds[-1].command, dma_cmd);
  353. st_le16(&dcmds->command, DBDMA_STOP);
  354. cmd->SCp.this_residual = total;
  355. }
  356. static struct scsi_host_template mac53c94_template = {
  357. .proc_name = "53c94",
  358. .name = "53C94",
  359. .queuecommand = mac53c94_queue,
  360. .eh_host_reset_handler = mac53c94_host_reset,
  361. .can_queue = 1,
  362. .this_id = 7,
  363. .sg_tablesize = SG_ALL,
  364. .cmd_per_lun = 1,
  365. .use_clustering = DISABLE_CLUSTERING,
  366. };
  367. static int mac53c94_probe(struct macio_dev *mdev, const struct of_device_id *match)
  368. {
  369. struct device_node *node = macio_get_of_node(mdev);
  370. struct pci_dev *pdev = macio_get_pci_dev(mdev);
  371. struct fsc_state *state;
  372. struct Scsi_Host *host;
  373. void *dma_cmd_space;
  374. const unsigned char *clkprop;
  375. int proplen, rc = -ENODEV;
  376. if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
  377. printk(KERN_ERR "mac53c94: expected 2 addrs and intrs"
  378. " (got %d/%d)\n",
  379. macio_resource_count(mdev), macio_irq_count(mdev));
  380. return -ENODEV;
  381. }
  382. if (macio_request_resources(mdev, "mac53c94") != 0) {
  383. printk(KERN_ERR "mac53c94: unable to request memory resources");
  384. return -EBUSY;
  385. }
  386. host = scsi_host_alloc(&mac53c94_template, sizeof(struct fsc_state));
  387. if (host == NULL) {
  388. printk(KERN_ERR "mac53c94: couldn't register host");
  389. rc = -ENOMEM;
  390. goto out_release;
  391. }
  392. state = (struct fsc_state *) host->hostdata;
  393. macio_set_drvdata(mdev, state);
  394. state->host = host;
  395. state->pdev = pdev;
  396. state->mdev = mdev;
  397. state->regs = (struct mac53c94_regs __iomem *)
  398. ioremap(macio_resource_start(mdev, 0), 0x1000);
  399. state->intr = macio_irq(mdev, 0);
  400. state->dma = (struct dbdma_regs __iomem *)
  401. ioremap(macio_resource_start(mdev, 1), 0x1000);
  402. state->dmaintr = macio_irq(mdev, 1);
  403. if (state->regs == NULL || state->dma == NULL) {
  404. printk(KERN_ERR "mac53c94: ioremap failed for %s\n",
  405. node->full_name);
  406. goto out_free;
  407. }
  408. clkprop = of_get_property(node, "clock-frequency", &proplen);
  409. if (clkprop == NULL || proplen != sizeof(int)) {
  410. printk(KERN_ERR "%s: can't get clock frequency, "
  411. "assuming 25MHz\n", node->full_name);
  412. state->clk_freq = 25000000;
  413. } else
  414. state->clk_freq = *(int *)clkprop;
  415. /* Space for dma command list: +1 for stop command,
  416. * +1 to allow for aligning.
  417. * XXX FIXME: Use DMA consistent routines
  418. */
  419. dma_cmd_space = kmalloc((host->sg_tablesize + 2) *
  420. sizeof(struct dbdma_cmd), GFP_KERNEL);
  421. if (dma_cmd_space == 0) {
  422. printk(KERN_ERR "mac53c94: couldn't allocate dma "
  423. "command space for %s\n", node->full_name);
  424. rc = -ENOMEM;
  425. goto out_free;
  426. }
  427. state->dma_cmds = (struct dbdma_cmd *)DBDMA_ALIGN(dma_cmd_space);
  428. memset(state->dma_cmds, 0, (host->sg_tablesize + 1)
  429. * sizeof(struct dbdma_cmd));
  430. state->dma_cmd_space = dma_cmd_space;
  431. mac53c94_init(state);
  432. if (request_irq(state->intr, do_mac53c94_interrupt, 0, "53C94",state)) {
  433. printk(KERN_ERR "mac53C94: can't get irq %d for %s\n",
  434. state->intr, node->full_name);
  435. goto out_free_dma;
  436. }
  437. rc = scsi_add_host(host, &mdev->ofdev.dev);
  438. if (rc != 0)
  439. goto out_release_irq;
  440. scsi_scan_host(host);
  441. return 0;
  442. out_release_irq:
  443. free_irq(state->intr, state);
  444. out_free_dma:
  445. kfree(state->dma_cmd_space);
  446. out_free:
  447. if (state->dma != NULL)
  448. iounmap(state->dma);
  449. if (state->regs != NULL)
  450. iounmap(state->regs);
  451. scsi_host_put(host);
  452. out_release:
  453. macio_release_resources(mdev);
  454. return rc;
  455. }
  456. static int mac53c94_remove(struct macio_dev *mdev)
  457. {
  458. struct fsc_state *fp = (struct fsc_state *)macio_get_drvdata(mdev);
  459. struct Scsi_Host *host = fp->host;
  460. scsi_remove_host(host);
  461. free_irq(fp->intr, fp);
  462. if (fp->regs)
  463. iounmap(fp->regs);
  464. if (fp->dma)
  465. iounmap(fp->dma);
  466. kfree(fp->dma_cmd_space);
  467. scsi_host_put(host);
  468. macio_release_resources(mdev);
  469. return 0;
  470. }
  471. static struct of_device_id mac53c94_match[] =
  472. {
  473. {
  474. .name = "53c94",
  475. },
  476. {},
  477. };
  478. MODULE_DEVICE_TABLE (of, mac53c94_match);
  479. static struct macio_driver mac53c94_driver =
  480. {
  481. .name = "mac53c94",
  482. .match_table = mac53c94_match,
  483. .probe = mac53c94_probe,
  484. .remove = mac53c94_remove,
  485. };
  486. static int __init init_mac53c94(void)
  487. {
  488. return macio_register_driver(&mac53c94_driver);
  489. }
  490. static void __exit exit_mac53c94(void)
  491. {
  492. return macio_unregister_driver(&mac53c94_driver);
  493. }
  494. module_init(init_mac53c94);
  495. module_exit(exit_mac53c94);
  496. MODULE_DESCRIPTION("PowerMac 53c94 SCSI driver");
  497. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  498. MODULE_LICENSE("GPL");